blob: a3785f538018dc8a777f7768752d8f5f7cedb1e1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * xfrm_state.c
3 *
4 * Changes:
5 * Mitsuru KANDA @USAGI
6 * Kazunori MIYAZAWA @USAGI
7 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
8 * IPv6 support
9 * YOSHIFUJI Hideaki @USAGI
10 * Split up af-specific functions
11 * Derek Atkins <derek@ihtfp.com>
12 * Add UDP Encapsulation
Trent Jaegerdf718372005-12-13 23:12:27 -080013 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 */
15
16#include <linux/workqueue.h>
17#include <net/xfrm.h>
18#include <linux/pfkeyv2.h>
19#include <linux/ipsec.h>
20#include <linux/module.h>
David S. Millerf034b5d2006-08-24 03:08:07 -070021#include <linux/cache.h>
Paul Moore68277ac2007-12-20 20:49:33 -080022#include <linux/audit.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080023#include <linux/uaccess.h>
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -080024#include <linux/ktime.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -080026#include <linux/interrupt.h>
27#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
David S. Miller44e36b42006-08-24 04:50:50 -070029#include "xfrm_hash.h"
30
Florian Westphalc8406992016-08-09 12:16:08 +020031#define xfrm_state_deref_prot(table, net) \
32 rcu_dereference_protected((table), lockdep_is_held(&(net)->xfrm.xfrm_state_lock))
33
Florian Westphal35db57bb2016-08-23 16:00:12 +020034static void xfrm_state_gc_task(struct work_struct *work);
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036/* Each xfrm_state may be linked to two tables:
37
38 1. Hash table by (spi,daddr,ah/esp) to find SA by SPI. (input,ctl)
David S. Millera624c102006-08-24 03:24:33 -070039 2. Hash table by (daddr,family,reqid) to find what SAs exist for given
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 destination/tunnel endpoint. (output)
41 */
42
David S. Millerf034b5d2006-08-24 03:08:07 -070043static unsigned int xfrm_state_hashmax __read_mostly = 1 * 1024 * 1024;
Florian Westphalb65e3d72016-08-09 12:16:07 +020044static __read_mostly seqcount_t xfrm_state_hash_generation = SEQCNT_ZERO(xfrm_state_hash_generation);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Florian Westphal35db57bb2016-08-23 16:00:12 +020046static DECLARE_WORK(xfrm_state_gc_work, xfrm_state_gc_task);
47static HLIST_HEAD(xfrm_state_gc_list);
48
Florian Westphal02efdff2016-08-09 12:16:05 +020049static inline bool xfrm_state_hold_rcu(struct xfrm_state __rcu *x)
50{
Reshetova, Elena88755e9c2017-07-04 15:53:21 +030051 return refcount_inc_not_zero(&x->refcnt);
Florian Westphal02efdff2016-08-09 12:16:05 +020052}
53
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -080054static inline unsigned int xfrm_dst_hash(struct net *net,
David S. Miller2ab38502011-02-24 01:47:16 -050055 const xfrm_address_t *daddr,
56 const xfrm_address_t *saddr,
David S. Millerc1969f22006-08-24 04:00:03 -070057 u32 reqid,
David S. Millera624c102006-08-24 03:24:33 -070058 unsigned short family)
59{
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -080060 return __xfrm_dst_hash(daddr, saddr, reqid, family, net->xfrm.state_hmask);
David S. Millera624c102006-08-24 03:24:33 -070061}
62
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -080063static inline unsigned int xfrm_src_hash(struct net *net,
David S. Miller2ab38502011-02-24 01:47:16 -050064 const xfrm_address_t *daddr,
65 const xfrm_address_t *saddr,
David S. Miller44e36b42006-08-24 04:50:50 -070066 unsigned short family)
David S. Millerf034b5d2006-08-24 03:08:07 -070067{
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -080068 return __xfrm_src_hash(daddr, saddr, family, net->xfrm.state_hmask);
David S. Millerf034b5d2006-08-24 03:08:07 -070069}
70
David S. Miller2575b652006-08-24 03:26:44 -070071static inline unsigned int
David S. Miller2ab38502011-02-24 01:47:16 -050072xfrm_spi_hash(struct net *net, const xfrm_address_t *daddr,
73 __be32 spi, u8 proto, unsigned short family)
David S. Millerf034b5d2006-08-24 03:08:07 -070074{
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -080075 return __xfrm_spi_hash(daddr, spi, proto, family, net->xfrm.state_hmask);
David S. Millerf034b5d2006-08-24 03:08:07 -070076}
77
David S. Millerf034b5d2006-08-24 03:08:07 -070078static void xfrm_hash_transfer(struct hlist_head *list,
79 struct hlist_head *ndsttable,
80 struct hlist_head *nsrctable,
81 struct hlist_head *nspitable,
82 unsigned int nhashmask)
83{
Sasha Levinb67bfe02013-02-27 17:06:00 -080084 struct hlist_node *tmp;
David S. Millerf034b5d2006-08-24 03:08:07 -070085 struct xfrm_state *x;
86
Sasha Levinb67bfe02013-02-27 17:06:00 -080087 hlist_for_each_entry_safe(x, tmp, list, bydst) {
David S. Millerf034b5d2006-08-24 03:08:07 -070088 unsigned int h;
89
David S. Millerc1969f22006-08-24 04:00:03 -070090 h = __xfrm_dst_hash(&x->id.daddr, &x->props.saddr,
91 x->props.reqid, x->props.family,
92 nhashmask);
Florian Westphalae3fb6d2016-08-09 12:16:04 +020093 hlist_add_head_rcu(&x->bydst, ndsttable + h);
David S. Millerf034b5d2006-08-24 03:08:07 -070094
Masahide NAKAMURA667bbcb2006-10-03 15:56:09 -070095 h = __xfrm_src_hash(&x->id.daddr, &x->props.saddr,
96 x->props.family,
David S. Millerf034b5d2006-08-24 03:08:07 -070097 nhashmask);
Florian Westphalae3fb6d2016-08-09 12:16:04 +020098 hlist_add_head_rcu(&x->bysrc, nsrctable + h);
David S. Millerf034b5d2006-08-24 03:08:07 -070099
Masahide NAKAMURA7b4dc3602006-09-27 22:21:52 -0700100 if (x->id.spi) {
101 h = __xfrm_spi_hash(&x->id.daddr, x->id.spi,
102 x->id.proto, x->props.family,
103 nhashmask);
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200104 hlist_add_head_rcu(&x->byspi, nspitable + h);
Masahide NAKAMURA7b4dc3602006-09-27 22:21:52 -0700105 }
David S. Millerf034b5d2006-08-24 03:08:07 -0700106 }
107}
108
Alexey Dobriyan63082732008-11-25 17:19:07 -0800109static unsigned long xfrm_hash_new_size(unsigned int state_hmask)
David S. Millerf034b5d2006-08-24 03:08:07 -0700110{
Alexey Dobriyan63082732008-11-25 17:19:07 -0800111 return ((state_hmask + 1) << 1) * sizeof(struct hlist_head);
David S. Millerf034b5d2006-08-24 03:08:07 -0700112}
113
Alexey Dobriyan63082732008-11-25 17:19:07 -0800114static void xfrm_hash_resize(struct work_struct *work)
David S. Millerf034b5d2006-08-24 03:08:07 -0700115{
Alexey Dobriyan63082732008-11-25 17:19:07 -0800116 struct net *net = container_of(work, struct net, xfrm.state_hash_work);
David S. Millerf034b5d2006-08-24 03:08:07 -0700117 struct hlist_head *ndst, *nsrc, *nspi, *odst, *osrc, *ospi;
118 unsigned long nsize, osize;
119 unsigned int nhashmask, ohashmask;
120 int i;
121
Alexey Dobriyan63082732008-11-25 17:19:07 -0800122 nsize = xfrm_hash_new_size(net->xfrm.state_hmask);
David S. Miller44e36b42006-08-24 04:50:50 -0700123 ndst = xfrm_hash_alloc(nsize);
David S. Millerf034b5d2006-08-24 03:08:07 -0700124 if (!ndst)
Ying Xue02447902014-08-29 17:09:07 +0800125 return;
David S. Miller44e36b42006-08-24 04:50:50 -0700126 nsrc = xfrm_hash_alloc(nsize);
David S. Millerf034b5d2006-08-24 03:08:07 -0700127 if (!nsrc) {
David S. Miller44e36b42006-08-24 04:50:50 -0700128 xfrm_hash_free(ndst, nsize);
Ying Xue02447902014-08-29 17:09:07 +0800129 return;
David S. Millerf034b5d2006-08-24 03:08:07 -0700130 }
David S. Miller44e36b42006-08-24 04:50:50 -0700131 nspi = xfrm_hash_alloc(nsize);
David S. Millerf034b5d2006-08-24 03:08:07 -0700132 if (!nspi) {
David S. Miller44e36b42006-08-24 04:50:50 -0700133 xfrm_hash_free(ndst, nsize);
134 xfrm_hash_free(nsrc, nsize);
Ying Xue02447902014-08-29 17:09:07 +0800135 return;
David S. Millerf034b5d2006-08-24 03:08:07 -0700136 }
137
Fan Du283bc9f2013-11-07 17:47:50 +0800138 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Florian Westphalb65e3d72016-08-09 12:16:07 +0200139 write_seqcount_begin(&xfrm_state_hash_generation);
David S. Millerf034b5d2006-08-24 03:08:07 -0700140
141 nhashmask = (nsize / sizeof(struct hlist_head)) - 1U;
Florian Westphalc8406992016-08-09 12:16:08 +0200142 odst = xfrm_state_deref_prot(net->xfrm.state_bydst, net);
Alexey Dobriyan63082732008-11-25 17:19:07 -0800143 for (i = net->xfrm.state_hmask; i >= 0; i--)
Florian Westphalc8406992016-08-09 12:16:08 +0200144 xfrm_hash_transfer(odst + i, ndst, nsrc, nspi, nhashmask);
David S. Millerf034b5d2006-08-24 03:08:07 -0700145
Florian Westphalc8406992016-08-09 12:16:08 +0200146 osrc = xfrm_state_deref_prot(net->xfrm.state_bysrc, net);
147 ospi = xfrm_state_deref_prot(net->xfrm.state_byspi, net);
Alexey Dobriyan63082732008-11-25 17:19:07 -0800148 ohashmask = net->xfrm.state_hmask;
David S. Millerf034b5d2006-08-24 03:08:07 -0700149
Florian Westphalc8406992016-08-09 12:16:08 +0200150 rcu_assign_pointer(net->xfrm.state_bydst, ndst);
151 rcu_assign_pointer(net->xfrm.state_bysrc, nsrc);
152 rcu_assign_pointer(net->xfrm.state_byspi, nspi);
Alexey Dobriyan63082732008-11-25 17:19:07 -0800153 net->xfrm.state_hmask = nhashmask;
David S. Millerf034b5d2006-08-24 03:08:07 -0700154
Florian Westphalb65e3d72016-08-09 12:16:07 +0200155 write_seqcount_end(&xfrm_state_hash_generation);
Fan Du283bc9f2013-11-07 17:47:50 +0800156 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
David S. Millerf034b5d2006-08-24 03:08:07 -0700157
158 osize = (ohashmask + 1) * sizeof(struct hlist_head);
Florian Westphaldf7274e2016-08-09 12:16:06 +0200159
160 synchronize_rcu();
161
David S. Miller44e36b42006-08-24 04:50:50 -0700162 xfrm_hash_free(odst, osize);
163 xfrm_hash_free(osrc, osize);
164 xfrm_hash_free(ospi, osize);
David S. Millerf034b5d2006-08-24 03:08:07 -0700165}
166
Cong Wang44abdc32013-01-16 16:05:05 +0800167static DEFINE_SPINLOCK(xfrm_state_afinfo_lock);
168static struct xfrm_state_afinfo __rcu *xfrm_state_afinfo[NPROTO];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170static DEFINE_SPINLOCK(xfrm_state_gc_lock);
171
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -0800172int __xfrm_state_delete(struct xfrm_state *x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -0800174int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol);
Horia Geanta0f245582014-02-12 16:20:06 +0200175bool km_is_alive(const struct km_event *c);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000176void km_state_expired(struct xfrm_state *x, int hard, u32 portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Cong Wang7a9885b2013-01-17 16:34:11 +0800178static DEFINE_SPINLOCK(xfrm_type_lock);
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800179int xfrm_register_type(const struct xfrm_type *type, unsigned short family)
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700180{
Cong Wang7a9885b2013-01-17 16:34:11 +0800181 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800182 const struct xfrm_type **typemap;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700183 int err = 0;
184
185 if (unlikely(afinfo == NULL))
186 return -EAFNOSUPPORT;
187 typemap = afinfo->type_map;
Cong Wang7a9885b2013-01-17 16:34:11 +0800188 spin_lock_bh(&xfrm_type_lock);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700189
190 if (likely(typemap[type->proto] == NULL))
191 typemap[type->proto] = type;
192 else
193 err = -EEXIST;
Cong Wang7a9885b2013-01-17 16:34:11 +0800194 spin_unlock_bh(&xfrm_type_lock);
Florian Westphalaf5d27c2017-01-09 14:20:47 +0100195 rcu_read_unlock();
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700196 return err;
197}
198EXPORT_SYMBOL(xfrm_register_type);
199
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800200int xfrm_unregister_type(const struct xfrm_type *type, unsigned short family)
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700201{
Cong Wang7a9885b2013-01-17 16:34:11 +0800202 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800203 const struct xfrm_type **typemap;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700204 int err = 0;
205
206 if (unlikely(afinfo == NULL))
207 return -EAFNOSUPPORT;
208 typemap = afinfo->type_map;
Cong Wang7a9885b2013-01-17 16:34:11 +0800209 spin_lock_bh(&xfrm_type_lock);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700210
211 if (unlikely(typemap[type->proto] != type))
212 err = -ENOENT;
213 else
214 typemap[type->proto] = NULL;
Cong Wang7a9885b2013-01-17 16:34:11 +0800215 spin_unlock_bh(&xfrm_type_lock);
Florian Westphalaf5d27c2017-01-09 14:20:47 +0100216 rcu_read_unlock();
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700217 return err;
218}
219EXPORT_SYMBOL(xfrm_unregister_type);
220
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800221static const struct xfrm_type *xfrm_get_type(u8 proto, unsigned short family)
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700222{
223 struct xfrm_state_afinfo *afinfo;
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800224 const struct xfrm_type **typemap;
225 const struct xfrm_type *type;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700226 int modload_attempted = 0;
227
228retry:
229 afinfo = xfrm_state_get_afinfo(family);
230 if (unlikely(afinfo == NULL))
231 return NULL;
232 typemap = afinfo->type_map;
233
Florian Westphal75cda622017-01-09 14:20:49 +0100234 type = READ_ONCE(typemap[proto]);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700235 if (unlikely(type && !try_module_get(type->owner)))
236 type = NULL;
Florian Westphal75cda622017-01-09 14:20:49 +0100237
238 rcu_read_unlock();
239
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700240 if (!type && !modload_attempted) {
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700241 request_module("xfrm-type-%d-%d", family, proto);
242 modload_attempted = 1;
243 goto retry;
244 }
245
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700246 return type;
247}
248
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800249static void xfrm_put_type(const struct xfrm_type *type)
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700250{
251 module_put(type->owner);
252}
253
Steffen Klassert9d389d72017-04-14 10:05:44 +0200254static DEFINE_SPINLOCK(xfrm_type_offload_lock);
255int xfrm_register_type_offload(const struct xfrm_type_offload *type,
256 unsigned short family)
257{
258 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
259 const struct xfrm_type_offload **typemap;
260 int err = 0;
261
262 if (unlikely(afinfo == NULL))
263 return -EAFNOSUPPORT;
264 typemap = afinfo->type_offload_map;
265 spin_lock_bh(&xfrm_type_offload_lock);
266
267 if (likely(typemap[type->proto] == NULL))
268 typemap[type->proto] = type;
269 else
270 err = -EEXIST;
271 spin_unlock_bh(&xfrm_type_offload_lock);
272 rcu_read_unlock();
273 return err;
274}
275EXPORT_SYMBOL(xfrm_register_type_offload);
276
277int xfrm_unregister_type_offload(const struct xfrm_type_offload *type,
278 unsigned short family)
279{
280 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
281 const struct xfrm_type_offload **typemap;
282 int err = 0;
283
284 if (unlikely(afinfo == NULL))
285 return -EAFNOSUPPORT;
286 typemap = afinfo->type_offload_map;
287 spin_lock_bh(&xfrm_type_offload_lock);
288
289 if (unlikely(typemap[type->proto] != type))
290 err = -ENOENT;
291 else
292 typemap[type->proto] = NULL;
293 spin_unlock_bh(&xfrm_type_offload_lock);
294 rcu_read_unlock();
295 return err;
296}
297EXPORT_SYMBOL(xfrm_unregister_type_offload);
298
Ilan Tayariffdb5212017-08-01 12:49:08 +0300299static const struct xfrm_type_offload *
300xfrm_get_type_offload(u8 proto, unsigned short family, bool try_load)
Steffen Klassert9d389d72017-04-14 10:05:44 +0200301{
302 struct xfrm_state_afinfo *afinfo;
303 const struct xfrm_type_offload **typemap;
304 const struct xfrm_type_offload *type;
305
Ilan Tayariffdb5212017-08-01 12:49:08 +0300306retry:
Steffen Klassert9d389d72017-04-14 10:05:44 +0200307 afinfo = xfrm_state_get_afinfo(family);
308 if (unlikely(afinfo == NULL))
309 return NULL;
310 typemap = afinfo->type_offload_map;
311
312 type = typemap[proto];
313 if ((type && !try_module_get(type->owner)))
314 type = NULL;
315
Sabrina Dubroca2f10a612017-12-31 16:18:56 +0100316 rcu_read_unlock();
317
Ilan Tayariffdb5212017-08-01 12:49:08 +0300318 if (!type && try_load) {
319 request_module("xfrm-offload-%d-%d", family, proto);
Gustavo A. R. Silva545d8ae2018-01-22 16:34:09 -0600320 try_load = false;
Ilan Tayariffdb5212017-08-01 12:49:08 +0300321 goto retry;
322 }
323
Steffen Klassert9d389d72017-04-14 10:05:44 +0200324 return type;
325}
326
327static void xfrm_put_type_offload(const struct xfrm_type_offload *type)
328{
329 module_put(type->owner);
330}
331
Cong Wang7a9885b2013-01-17 16:34:11 +0800332static DEFINE_SPINLOCK(xfrm_mode_lock);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700333int xfrm_register_mode(struct xfrm_mode *mode, int family)
334{
335 struct xfrm_state_afinfo *afinfo;
336 struct xfrm_mode **modemap;
337 int err;
338
339 if (unlikely(mode->encap >= XFRM_MODE_MAX))
340 return -EINVAL;
341
Cong Wang7a9885b2013-01-17 16:34:11 +0800342 afinfo = xfrm_state_get_afinfo(family);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700343 if (unlikely(afinfo == NULL))
344 return -EAFNOSUPPORT;
345
346 err = -EEXIST;
347 modemap = afinfo->mode_map;
Cong Wang7a9885b2013-01-17 16:34:11 +0800348 spin_lock_bh(&xfrm_mode_lock);
Herbert Xu17c2a422007-10-17 21:33:12 -0700349 if (modemap[mode->encap])
350 goto out;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700351
Herbert Xu17c2a422007-10-17 21:33:12 -0700352 err = -ENOENT;
353 if (!try_module_get(afinfo->owner))
354 goto out;
355
356 mode->afinfo = afinfo;
357 modemap[mode->encap] = mode;
358 err = 0;
359
360out:
Cong Wang7a9885b2013-01-17 16:34:11 +0800361 spin_unlock_bh(&xfrm_mode_lock);
Florian Westphalaf5d27c2017-01-09 14:20:47 +0100362 rcu_read_unlock();
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700363 return err;
364}
365EXPORT_SYMBOL(xfrm_register_mode);
366
367int xfrm_unregister_mode(struct xfrm_mode *mode, int family)
368{
369 struct xfrm_state_afinfo *afinfo;
370 struct xfrm_mode **modemap;
371 int err;
372
373 if (unlikely(mode->encap >= XFRM_MODE_MAX))
374 return -EINVAL;
375
Cong Wang7a9885b2013-01-17 16:34:11 +0800376 afinfo = xfrm_state_get_afinfo(family);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700377 if (unlikely(afinfo == NULL))
378 return -EAFNOSUPPORT;
379
380 err = -ENOENT;
381 modemap = afinfo->mode_map;
Cong Wang7a9885b2013-01-17 16:34:11 +0800382 spin_lock_bh(&xfrm_mode_lock);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700383 if (likely(modemap[mode->encap] == mode)) {
384 modemap[mode->encap] = NULL;
Herbert Xu17c2a422007-10-17 21:33:12 -0700385 module_put(mode->afinfo->owner);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700386 err = 0;
387 }
388
Cong Wang7a9885b2013-01-17 16:34:11 +0800389 spin_unlock_bh(&xfrm_mode_lock);
Florian Westphalaf5d27c2017-01-09 14:20:47 +0100390 rcu_read_unlock();
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700391 return err;
392}
393EXPORT_SYMBOL(xfrm_unregister_mode);
394
395static struct xfrm_mode *xfrm_get_mode(unsigned int encap, int family)
396{
397 struct xfrm_state_afinfo *afinfo;
398 struct xfrm_mode *mode;
399 int modload_attempted = 0;
400
401 if (unlikely(encap >= XFRM_MODE_MAX))
402 return NULL;
403
404retry:
405 afinfo = xfrm_state_get_afinfo(family);
406 if (unlikely(afinfo == NULL))
407 return NULL;
408
Florian Westphal75cda622017-01-09 14:20:49 +0100409 mode = READ_ONCE(afinfo->mode_map[encap]);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700410 if (unlikely(mode && !try_module_get(mode->owner)))
411 mode = NULL;
Florian Westphal75cda622017-01-09 14:20:49 +0100412
413 rcu_read_unlock();
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700414 if (!mode && !modload_attempted) {
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700415 request_module("xfrm-mode-%d-%d", family, encap);
416 modload_attempted = 1;
417 goto retry;
418 }
419
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700420 return mode;
421}
422
423static void xfrm_put_mode(struct xfrm_mode *mode)
424{
425 module_put(mode->owner);
426}
427
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428static void xfrm_state_gc_destroy(struct xfrm_state *x)
429{
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800430 tasklet_hrtimer_cancel(&x->mtimer);
David S. Millera47f0ce2006-08-24 03:54:22 -0700431 del_timer_sync(&x->rtimer);
Ilan Tayarib5884792016-09-18 07:42:53 +0000432 kfree(x->aead);
Jesper Juhla51482b2005-11-08 09:41:34 -0800433 kfree(x->aalg);
434 kfree(x->ealg);
435 kfree(x->calg);
436 kfree(x->encap);
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700437 kfree(x->coaddr);
Steffen Klassertd8647b72011-03-08 00:10:27 +0000438 kfree(x->replay_esn);
439 kfree(x->preplay_esn);
Herbert Xu13996372007-10-17 21:35:51 -0700440 if (x->inner_mode)
441 xfrm_put_mode(x->inner_mode);
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700442 if (x->inner_mode_iaf)
443 xfrm_put_mode(x->inner_mode_iaf);
Herbert Xu13996372007-10-17 21:35:51 -0700444 if (x->outer_mode)
445 xfrm_put_mode(x->outer_mode);
Steffen Klassert9d389d72017-04-14 10:05:44 +0200446 if (x->type_offload)
447 xfrm_put_type_offload(x->type_offload);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 if (x->type) {
449 x->type->destructor(x);
450 xfrm_put_type(x->type);
451 }
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200452 xfrm_dev_state_free(x);
Trent Jaegerdf718372005-12-13 23:12:27 -0800453 security_xfrm_state_free(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 kfree(x);
455}
456
Alexey Dobriyanc7837142008-11-25 17:20:36 -0800457static void xfrm_state_gc_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
Herbert Xu12a169e2008-10-01 07:03:24 -0700459 struct xfrm_state *x;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800460 struct hlist_node *tmp;
Herbert Xu12a169e2008-10-01 07:03:24 -0700461 struct hlist_head gc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 spin_lock_bh(&xfrm_state_gc_lock);
Florian Westphal35db57bb2016-08-23 16:00:12 +0200464 hlist_move_list(&xfrm_state_gc_list, &gc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 spin_unlock_bh(&xfrm_state_gc_lock);
466
Florian Westphaldf7274e2016-08-09 12:16:06 +0200467 synchronize_rcu();
468
Sasha Levinb67bfe02013-02-27 17:06:00 -0800469 hlist_for_each_entry_safe(x, tmp, &gc_list, gclist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 xfrm_state_gc_destroy(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471}
472
Weilong Chen3e94c2d2013-12-24 09:43:47 +0800473static enum hrtimer_restart xfrm_timer_handler(struct hrtimer *me)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800475 struct tasklet_hrtimer *thr = container_of(me, struct tasklet_hrtimer, timer);
476 struct xfrm_state *x = container_of(thr, struct xfrm_state, mtimer);
James Morris9d729f72007-03-04 16:12:44 -0800477 unsigned long now = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 long next = LONG_MAX;
479 int warn = 0;
Joy Latten161a09e2006-11-27 13:11:54 -0600480 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
482 spin_lock(&x->lock);
483 if (x->km.state == XFRM_STATE_DEAD)
484 goto out;
485 if (x->km.state == XFRM_STATE_EXPIRED)
486 goto expired;
487 if (x->lft.hard_add_expires_seconds) {
488 long tmo = x->lft.hard_add_expires_seconds +
489 x->curlft.add_time - now;
Fan Due3c0d042012-07-30 21:43:54 +0000490 if (tmo <= 0) {
491 if (x->xflags & XFRM_SOFT_EXPIRE) {
492 /* enter hard expire without soft expire first?!
493 * setting a new date could trigger this.
Alexander Alemayhu1365e547c2017-01-03 17:13:20 +0100494 * workaround: fix x->curflt.add_time by below:
Fan Due3c0d042012-07-30 21:43:54 +0000495 */
496 x->curlft.add_time = now - x->saved_tmo - 1;
497 tmo = x->lft.hard_add_expires_seconds - x->saved_tmo;
498 } else
499 goto expired;
500 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 if (tmo < next)
502 next = tmo;
503 }
504 if (x->lft.hard_use_expires_seconds) {
505 long tmo = x->lft.hard_use_expires_seconds +
506 (x->curlft.use_time ? : now) - now;
507 if (tmo <= 0)
508 goto expired;
509 if (tmo < next)
510 next = tmo;
511 }
512 if (x->km.dying)
513 goto resched;
514 if (x->lft.soft_add_expires_seconds) {
515 long tmo = x->lft.soft_add_expires_seconds +
516 x->curlft.add_time - now;
Fan Due3c0d042012-07-30 21:43:54 +0000517 if (tmo <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 warn = 1;
Fan Due3c0d042012-07-30 21:43:54 +0000519 x->xflags &= ~XFRM_SOFT_EXPIRE;
520 } else if (tmo < next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 next = tmo;
Fan Due3c0d042012-07-30 21:43:54 +0000522 x->xflags |= XFRM_SOFT_EXPIRE;
523 x->saved_tmo = tmo;
524 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 }
526 if (x->lft.soft_use_expires_seconds) {
527 long tmo = x->lft.soft_use_expires_seconds +
528 (x->curlft.use_time ? : now) - now;
529 if (tmo <= 0)
530 warn = 1;
531 else if (tmo < next)
532 next = tmo;
533 }
534
Herbert Xu4666faa2005-06-18 22:43:22 -0700535 x->km.dying = warn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 if (warn)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -0800537 km_state_expired(x, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538resched:
Weilong Chen9b7a7872013-12-24 09:43:46 +0800539 if (next != LONG_MAX) {
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800540 tasklet_hrtimer_start(&x->mtimer, ktime_set(next, 0), HRTIMER_MODE_REL);
541 }
David S. Millera47f0ce2006-08-24 03:54:22 -0700542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 goto out;
544
545expired:
Steffen Klassert5b8ef342013-08-27 13:43:30 +0200546 if (x->km.state == XFRM_STATE_ACQ && x->id.spi == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 x->km.state = XFRM_STATE_EXPIRED;
Joy Latten161a09e2006-11-27 13:11:54 -0600548
549 err = __xfrm_state_delete(x);
Nicolas Dichtel0806ae42013-08-23 15:46:08 +0200550 if (!err)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -0800551 km_state_expired(x, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Tetsuo Handa2e710292014-04-22 21:48:30 +0900553 xfrm_audit_state_delete(x, err ? 0 : 1, true);
Joy Latten161a09e2006-11-27 13:11:54 -0600554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555out:
556 spin_unlock(&x->lock);
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800557 return HRTIMER_NORESTART;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558}
559
Kees Cooke99e88a2017-10-16 14:43:17 -0700560static void xfrm_replay_timer_handler(struct timer_list *t);
David S. Miller0ac84752006-03-20 19:18:23 -0800561
Alexey Dobriyan673c09b2008-11-25 17:15:16 -0800562struct xfrm_state *xfrm_state_alloc(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
564 struct xfrm_state *x;
565
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700566 x = kzalloc(sizeof(struct xfrm_state), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
568 if (x) {
Alexey Dobriyan673c09b2008-11-25 17:15:16 -0800569 write_pnet(&x->xs_net, net);
Reshetova, Elena88755e9c2017-07-04 15:53:21 +0300570 refcount_set(&x->refcnt, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 atomic_set(&x->tunnel_users, 0);
Herbert Xu12a169e2008-10-01 07:03:24 -0700572 INIT_LIST_HEAD(&x->km.all);
David S. Miller8f126e32006-08-24 02:45:07 -0700573 INIT_HLIST_NODE(&x->bydst);
574 INIT_HLIST_NODE(&x->bysrc);
575 INIT_HLIST_NODE(&x->byspi);
Fan Du99565a6c2013-08-15 15:49:06 +0800576 tasklet_hrtimer_init(&x->mtimer, xfrm_timer_handler,
577 CLOCK_BOOTTIME, HRTIMER_MODE_ABS);
Kees Cooke99e88a2017-10-16 14:43:17 -0700578 timer_setup(&x->rtimer, xfrm_replay_timer_handler, 0);
James Morris9d729f72007-03-04 16:12:44 -0800579 x->curlft.add_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 x->lft.soft_byte_limit = XFRM_INF;
581 x->lft.soft_packet_limit = XFRM_INF;
582 x->lft.hard_byte_limit = XFRM_INF;
583 x->lft.hard_packet_limit = XFRM_INF;
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -0800584 x->replay_maxage = 0;
585 x->replay_maxdiff = 0;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700586 x->inner_mode = NULL;
587 x->inner_mode_iaf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 spin_lock_init(&x->lock);
589 }
590 return x;
591}
592EXPORT_SYMBOL(xfrm_state_alloc);
593
594void __xfrm_state_destroy(struct xfrm_state *x)
595{
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700596 WARN_ON(x->km.state != XFRM_STATE_DEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
598 spin_lock_bh(&xfrm_state_gc_lock);
Florian Westphal35db57bb2016-08-23 16:00:12 +0200599 hlist_add_head(&x->gclist, &xfrm_state_gc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 spin_unlock_bh(&xfrm_state_gc_lock);
Florian Westphal35db57bb2016-08-23 16:00:12 +0200601 schedule_work(&xfrm_state_gc_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602}
603EXPORT_SYMBOL(__xfrm_state_destroy);
604
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -0800605int __xfrm_state_delete(struct xfrm_state *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800607 struct net *net = xs_net(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700608 int err = -ESRCH;
609
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 if (x->km.state != XFRM_STATE_DEAD) {
611 x->km.state = XFRM_STATE_DEAD;
Fan Du283bc9f2013-11-07 17:47:50 +0800612 spin_lock(&net->xfrm.xfrm_state_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -0700613 list_del(&x->km.all);
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200614 hlist_del_rcu(&x->bydst);
615 hlist_del_rcu(&x->bysrc);
David S. Millera47f0ce2006-08-24 03:54:22 -0700616 if (x->id.spi)
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200617 hlist_del_rcu(&x->byspi);
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800618 net->xfrm.state_num--;
Fan Du283bc9f2013-11-07 17:47:50 +0800619 spin_unlock(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200621 xfrm_dev_state_delete(x);
622
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 /* All xfrm_state objects are created by xfrm_state_alloc.
624 * The xfrm_state_alloc call gives a reference, and that
625 * is what we are dropping here.
626 */
Patrick McHardy5dba4792007-11-27 11:10:07 +0800627 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700628 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700630
631 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632}
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -0800633EXPORT_SYMBOL(__xfrm_state_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700635int xfrm_state_delete(struct xfrm_state *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700637 int err;
638
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 spin_lock_bh(&x->lock);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700640 err = __xfrm_state_delete(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 spin_unlock_bh(&x->lock);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700642
643 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644}
645EXPORT_SYMBOL(xfrm_state_delete);
646
Joy Latten4aa2e622007-06-04 19:05:57 -0400647#ifdef CONFIG_SECURITY_NETWORK_XFRM
648static inline int
Tetsuo Handa2e710292014-04-22 21:48:30 +0900649xfrm_state_flush_secctx_check(struct net *net, u8 proto, bool task_valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650{
Joy Latten4aa2e622007-06-04 19:05:57 -0400651 int i, err = 0;
652
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800653 for (i = 0; i <= net->xfrm.state_hmask; i++) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400654 struct xfrm_state *x;
655
Sasha Levinb67bfe02013-02-27 17:06:00 -0800656 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400657 if (xfrm_id_proto_match(x->id.proto, proto) &&
658 (err = security_xfrm_state_delete(x)) != 0) {
Tetsuo Handa2e710292014-04-22 21:48:30 +0900659 xfrm_audit_state_delete(x, 0, task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400660 return err;
661 }
662 }
663 }
664
665 return err;
666}
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200667
668static inline int
669xfrm_dev_state_flush_secctx_check(struct net *net, struct net_device *dev, bool task_valid)
670{
671 int i, err = 0;
672
673 for (i = 0; i <= net->xfrm.state_hmask; i++) {
674 struct xfrm_state *x;
675 struct xfrm_state_offload *xso;
676
677 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
678 xso = &x->xso;
679
680 if (xso->dev == dev &&
681 (err = security_xfrm_state_delete(x)) != 0) {
682 xfrm_audit_state_delete(x, 0, task_valid);
683 return err;
684 }
685 }
686 }
687
688 return err;
689}
Joy Latten4aa2e622007-06-04 19:05:57 -0400690#else
691static inline int
Tetsuo Handa2e710292014-04-22 21:48:30 +0900692xfrm_state_flush_secctx_check(struct net *net, u8 proto, bool task_valid)
Joy Latten4aa2e622007-06-04 19:05:57 -0400693{
694 return 0;
695}
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200696
697static inline int
698xfrm_dev_state_flush_secctx_check(struct net *net, struct net_device *dev, bool task_valid)
699{
700 return 0;
701}
Joy Latten4aa2e622007-06-04 19:05:57 -0400702#endif
703
Tetsuo Handa2e710292014-04-22 21:48:30 +0900704int xfrm_state_flush(struct net *net, u8 proto, bool task_valid)
Joy Latten4aa2e622007-06-04 19:05:57 -0400705{
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000706 int i, err = 0, cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
Fan Du283bc9f2013-11-07 17:47:50 +0800708 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Tetsuo Handa2e710292014-04-22 21:48:30 +0900709 err = xfrm_state_flush_secctx_check(net, proto, task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400710 if (err)
711 goto out;
712
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000713 err = -ESRCH;
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800714 for (i = 0; i <= net->xfrm.state_hmask; i++) {
David S. Miller8f126e32006-08-24 02:45:07 -0700715 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716restart:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800717 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 if (!xfrm_state_kern(x) &&
Masahide NAKAMURA57947082006-09-22 15:06:24 -0700719 xfrm_id_proto_match(x->id.proto, proto)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 xfrm_state_hold(x);
Fan Du283bc9f2013-11-07 17:47:50 +0800721 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Joy Latten161a09e2006-11-27 13:11:54 -0600723 err = xfrm_state_delete(x);
Joy Lattenab5f5e82007-09-17 11:51:22 -0700724 xfrm_audit_state_delete(x, err ? 0 : 1,
Tetsuo Handa2e710292014-04-22 21:48:30 +0900725 task_valid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 xfrm_state_put(x);
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000727 if (!err)
728 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Fan Du283bc9f2013-11-07 17:47:50 +0800730 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 goto restart;
732 }
733 }
734 }
Artem Savkovdd269db2017-09-27 14:25:37 +0200735out:
736 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Florian Westphalec30d782017-07-17 13:57:27 +0200737 if (cnt) {
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000738 err = 0;
Florian Westphalec30d782017-07-17 13:57:27 +0200739 xfrm_policy_cache_flush();
740 }
Joy Latten4aa2e622007-06-04 19:05:57 -0400741 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742}
743EXPORT_SYMBOL(xfrm_state_flush);
744
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200745int xfrm_dev_state_flush(struct net *net, struct net_device *dev, bool task_valid)
746{
747 int i, err = 0, cnt = 0;
748
749 spin_lock_bh(&net->xfrm.xfrm_state_lock);
750 err = xfrm_dev_state_flush_secctx_check(net, dev, task_valid);
751 if (err)
752 goto out;
753
754 err = -ESRCH;
755 for (i = 0; i <= net->xfrm.state_hmask; i++) {
756 struct xfrm_state *x;
757 struct xfrm_state_offload *xso;
758restart:
759 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
760 xso = &x->xso;
761
762 if (!xfrm_state_kern(x) && xso->dev == dev) {
763 xfrm_state_hold(x);
764 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
765
766 err = xfrm_state_delete(x);
767 xfrm_audit_state_delete(x, err ? 0 : 1,
768 task_valid);
769 xfrm_state_put(x);
770 if (!err)
771 cnt++;
772
773 spin_lock_bh(&net->xfrm.xfrm_state_lock);
774 goto restart;
775 }
776 }
777 }
778 if (cnt)
779 err = 0;
780
781out:
782 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
783 return err;
784}
785EXPORT_SYMBOL(xfrm_dev_state_flush);
786
Alexey Dobriyane0710412010-01-23 13:37:10 +0000787void xfrm_sad_getinfo(struct net *net, struct xfrmk_sadinfo *si)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700788{
Fan Du283bc9f2013-11-07 17:47:50 +0800789 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Alexey Dobriyane0710412010-01-23 13:37:10 +0000790 si->sadcnt = net->xfrm.state_num;
791 si->sadhcnt = net->xfrm.state_hmask;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700792 si->sadhmcnt = xfrm_state_hashmax;
Fan Du283bc9f2013-11-07 17:47:50 +0800793 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700794}
795EXPORT_SYMBOL(xfrm_sad_getinfo);
796
Florian Westphal711059b2017-01-09 14:20:48 +0100797static void
David S. Miller1a898592011-02-22 18:22:34 -0800798xfrm_init_tempstate(struct xfrm_state *x, const struct flowi *fl,
David S. Miller04686012011-02-24 01:50:12 -0500799 const struct xfrm_tmpl *tmpl,
David S. Miller33765d02011-02-24 01:55:45 -0500800 const xfrm_address_t *daddr, const xfrm_address_t *saddr,
Thomas Egerer8444cf72010-09-20 11:11:38 -0700801 unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802{
Florian Westphal711059b2017-01-09 14:20:48 +0100803 struct xfrm_state_afinfo *afinfo = xfrm_state_afinfo_get_rcu(family);
804
Florian Westphal3819a352017-01-13 14:55:14 +0100805 if (!afinfo)
806 return;
807
808 afinfo->init_tempsel(&x->sel, fl);
Thomas Egerer8444cf72010-09-20 11:11:38 -0700809
810 if (family != tmpl->encap_family) {
Florian Westphal711059b2017-01-09 14:20:48 +0100811 afinfo = xfrm_state_afinfo_get_rcu(tmpl->encap_family);
Thomas Egerer8444cf72010-09-20 11:11:38 -0700812 if (!afinfo)
Florian Westphal711059b2017-01-09 14:20:48 +0100813 return;
Thomas Egerer8444cf72010-09-20 11:11:38 -0700814 }
815 afinfo->init_temprop(x, tmpl, daddr, saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816}
817
David S. Miller9aa60082011-02-24 01:51:36 -0500818static struct xfrm_state *__xfrm_state_lookup(struct net *net, u32 mark,
819 const xfrm_address_t *daddr,
820 __be32 spi, u8 proto,
821 unsigned short family)
David S. Milleredcd5822006-08-24 00:42:45 -0700822{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800823 unsigned int h = xfrm_spi_hash(net, daddr, spi, proto, family);
David S. Milleredcd5822006-08-24 00:42:45 -0700824 struct xfrm_state *x;
825
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200826 hlist_for_each_entry_rcu(x, net->xfrm.state_byspi + h, byspi) {
David S. Milleredcd5822006-08-24 00:42:45 -0700827 if (x->props.family != family ||
828 x->id.spi != spi ||
Wei Yongjun18025712009-06-28 18:42:53 +0000829 x->id.proto != proto ||
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +0000830 !xfrm_addr_equal(&x->id.daddr, daddr, family))
David S. Milleredcd5822006-08-24 00:42:45 -0700831 continue;
832
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000833 if ((mark & x->mark.m) != x->mark.v)
834 continue;
Florian Westphal02efdff2016-08-09 12:16:05 +0200835 if (!xfrm_state_hold_rcu(x))
836 continue;
David S. Milleredcd5822006-08-24 00:42:45 -0700837 return x;
838 }
839
840 return NULL;
841}
842
David S. Miller9aa60082011-02-24 01:51:36 -0500843static struct xfrm_state *__xfrm_state_lookup_byaddr(struct net *net, u32 mark,
844 const xfrm_address_t *daddr,
845 const xfrm_address_t *saddr,
846 u8 proto, unsigned short family)
David S. Milleredcd5822006-08-24 00:42:45 -0700847{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800848 unsigned int h = xfrm_src_hash(net, daddr, saddr, family);
David S. Milleredcd5822006-08-24 00:42:45 -0700849 struct xfrm_state *x;
850
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200851 hlist_for_each_entry_rcu(x, net->xfrm.state_bysrc + h, bysrc) {
David S. Milleredcd5822006-08-24 00:42:45 -0700852 if (x->props.family != family ||
Wei Yongjun18025712009-06-28 18:42:53 +0000853 x->id.proto != proto ||
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +0000854 !xfrm_addr_equal(&x->id.daddr, daddr, family) ||
855 !xfrm_addr_equal(&x->props.saddr, saddr, family))
David S. Milleredcd5822006-08-24 00:42:45 -0700856 continue;
857
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000858 if ((mark & x->mark.m) != x->mark.v)
859 continue;
Florian Westphal02efdff2016-08-09 12:16:05 +0200860 if (!xfrm_state_hold_rcu(x))
861 continue;
David S. Milleredcd5822006-08-24 00:42:45 -0700862 return x;
863 }
864
865 return NULL;
866}
867
868static inline struct xfrm_state *
869__xfrm_state_locate(struct xfrm_state *x, int use_spi, int family)
870{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800871 struct net *net = xs_net(x);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800872 u32 mark = x->mark.v & x->mark.m;
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800873
David S. Milleredcd5822006-08-24 00:42:45 -0700874 if (use_spi)
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800875 return __xfrm_state_lookup(net, mark, &x->id.daddr,
876 x->id.spi, x->id.proto, family);
David S. Milleredcd5822006-08-24 00:42:45 -0700877 else
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800878 return __xfrm_state_lookup_byaddr(net, mark,
879 &x->id.daddr,
David S. Milleredcd5822006-08-24 00:42:45 -0700880 &x->props.saddr,
881 x->id.proto, family);
882}
883
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800884static void xfrm_hash_grow_check(struct net *net, int have_hash_collision)
Patrick McHardy2fab22f2006-10-24 15:34:00 -0700885{
886 if (have_hash_collision &&
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800887 (net->xfrm.state_hmask + 1) < xfrm_state_hashmax &&
888 net->xfrm.state_num > net->xfrm.state_hmask)
889 schedule_work(&net->xfrm.state_hash_work);
Patrick McHardy2fab22f2006-10-24 15:34:00 -0700890}
891
David S. Miller08ec9af2009-03-13 14:22:40 -0700892static void xfrm_state_look_at(struct xfrm_policy *pol, struct xfrm_state *x,
David S. Miller4a08ab02011-02-22 18:21:31 -0800893 const struct flowi *fl, unsigned short family,
David S. Miller08ec9af2009-03-13 14:22:40 -0700894 struct xfrm_state **best, int *acq_in_progress,
895 int *error)
896{
897 /* Resolution logic:
898 * 1. There is a valid state with matching selector. Done.
899 * 2. Valid state with inappropriate selector. Skip.
900 *
901 * Entering area of "sysdeps".
902 *
903 * 3. If state is not valid, selector is temporary, it selects
904 * only session which triggered previous resolution. Key
905 * manager will do something to install a state with proper
906 * selector.
907 */
908 if (x->km.state == XFRM_STATE_VALID) {
909 if ((x->sel.family &&
910 !xfrm_selector_match(&x->sel, fl, x->sel.family)) ||
911 !security_xfrm_state_pol_flow_match(x, pol, fl))
912 return;
913
914 if (!*best ||
915 (*best)->km.dying > x->km.dying ||
916 ((*best)->km.dying == x->km.dying &&
917 (*best)->curlft.add_time < x->curlft.add_time))
918 *best = x;
919 } else if (x->km.state == XFRM_STATE_ACQ) {
920 *acq_in_progress = 1;
921 } else if (x->km.state == XFRM_STATE_ERROR ||
922 x->km.state == XFRM_STATE_EXPIRED) {
923 if (xfrm_selector_match(&x->sel, fl, x->sel.family) &&
924 security_xfrm_state_pol_flow_match(x, pol, fl))
925 *error = -ESRCH;
926 }
927}
928
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929struct xfrm_state *
David S. Miller33765d02011-02-24 01:55:45 -0500930xfrm_state_find(const xfrm_address_t *daddr, const xfrm_address_t *saddr,
David S. Millerb520e9f2011-02-22 18:24:19 -0800931 const struct flowi *fl, struct xfrm_tmpl *tmpl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 struct xfrm_policy *pol, int *err,
933 unsigned short family)
934{
David S. Miller08ec9af2009-03-13 14:22:40 -0700935 static xfrm_address_t saddr_wildcard = { };
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800936 struct net *net = xp_net(pol);
Nicolas Dichtel6a783c92009-04-27 02:58:59 -0700937 unsigned int h, h_wildcard;
David S. Miller37b08e32008-09-02 20:14:15 -0700938 struct xfrm_state *x, *x0, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 int acquire_in_progress = 0;
940 int error = 0;
941 struct xfrm_state *best = NULL;
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800942 u32 mark = pol->mark.v & pol->mark.m;
Thomas Egerer8444cf72010-09-20 11:11:38 -0700943 unsigned short encap_family = tmpl->encap_family;
Florian Westphalb65e3d72016-08-09 12:16:07 +0200944 unsigned int sequence;
Horia Geanta0f245582014-02-12 16:20:06 +0200945 struct km_event c;
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900946
David S. Miller37b08e32008-09-02 20:14:15 -0700947 to_put = NULL;
948
Florian Westphalb65e3d72016-08-09 12:16:07 +0200949 sequence = read_seqcount_begin(&xfrm_state_hash_generation);
950
Florian Westphald737a582016-08-09 12:16:09 +0200951 rcu_read_lock();
Thomas Egerer8444cf72010-09-20 11:11:38 -0700952 h = xfrm_dst_hash(net, daddr, saddr, tmpl->reqid, encap_family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200953 hlist_for_each_entry_rcu(x, net->xfrm.state_bydst + h, bydst) {
Thomas Egerer8444cf72010-09-20 11:11:38 -0700954 if (x->props.family == encap_family &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 x->props.reqid == tmpl->reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000956 (mark & x->mark.m) == x->mark.v &&
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -0700957 !(x->props.flags & XFRM_STATE_WILDRECV) &&
Thomas Egerer8444cf72010-09-20 11:11:38 -0700958 xfrm_state_addr_check(x, daddr, saddr, encap_family) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 tmpl->mode == x->props.mode &&
960 tmpl->id.proto == x->id.proto &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700961 (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
David S. Miller1f673c52011-02-24 01:53:13 -0500962 xfrm_state_look_at(pol, x, fl, encap_family,
David S. Miller08ec9af2009-03-13 14:22:40 -0700963 &best, &acquire_in_progress, &error);
964 }
Fan Du6f115632013-09-23 17:18:25 +0800965 if (best || acquire_in_progress)
David S. Miller08ec9af2009-03-13 14:22:40 -0700966 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
Thomas Egerer8444cf72010-09-20 11:11:38 -0700968 h_wildcard = xfrm_dst_hash(net, daddr, &saddr_wildcard, tmpl->reqid, encap_family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200969 hlist_for_each_entry_rcu(x, net->xfrm.state_bydst + h_wildcard, bydst) {
Thomas Egerer8444cf72010-09-20 11:11:38 -0700970 if (x->props.family == encap_family &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700971 x->props.reqid == tmpl->reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000972 (mark & x->mark.m) == x->mark.v &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700973 !(x->props.flags & XFRM_STATE_WILDRECV) &&
Fan Duf59bbdf2013-09-27 16:32:50 +0800974 xfrm_addr_equal(&x->id.daddr, daddr, encap_family) &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700975 tmpl->mode == x->props.mode &&
976 tmpl->id.proto == x->id.proto &&
977 (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
David S. Miller1f673c52011-02-24 01:53:13 -0500978 xfrm_state_look_at(pol, x, fl, encap_family,
David S. Miller08ec9af2009-03-13 14:22:40 -0700979 &best, &acquire_in_progress, &error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 }
981
David S. Miller08ec9af2009-03-13 14:22:40 -0700982found:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 x = best;
984 if (!x && !error && !acquire_in_progress) {
Patrick McHardy5c5d2812005-04-21 20:12:32 -0700985 if (tmpl->id.spi &&
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800986 (x0 = __xfrm_state_lookup(net, mark, daddr, tmpl->id.spi,
Thomas Egerer8444cf72010-09-20 11:11:38 -0700987 tmpl->id.proto, encap_family)) != NULL) {
David S. Miller37b08e32008-09-02 20:14:15 -0700988 to_put = x0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 error = -EEXIST;
990 goto out;
991 }
Horia Geanta0f245582014-02-12 16:20:06 +0200992
993 c.net = net;
994 /* If the KMs have no listeners (yet...), avoid allocating an SA
995 * for each and every packet - garbage collection might not
996 * handle the flood.
997 */
998 if (!km_is_alive(&c)) {
999 error = -ESRCH;
1000 goto out;
1001 }
1002
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001003 x = xfrm_state_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 if (x == NULL) {
1005 error = -ENOMEM;
1006 goto out;
1007 }
Thomas Egerer8444cf72010-09-20 11:11:38 -07001008 /* Initialize temporary state matching only
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 * to current session. */
Thomas Egerer8444cf72010-09-20 11:11:38 -07001010 xfrm_init_tempstate(x, fl, tmpl, daddr, saddr, family);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001011 memcpy(&x->mark, &pol->mark, sizeof(x->mark));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
David S. Miller1d28f422011-03-12 00:29:39 -05001013 error = security_xfrm_state_alloc_acquire(x, pol->security, fl->flowi_secid);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001014 if (error) {
1015 x->km.state = XFRM_STATE_DEAD;
David S. Miller37b08e32008-09-02 20:14:15 -07001016 to_put = x;
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001017 x = NULL;
1018 goto out;
1019 }
1020
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 if (km_query(x, tmpl, pol) == 0) {
Florian Westphald737a582016-08-09 12:16:09 +02001022 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 x->km.state = XFRM_STATE_ACQ;
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001024 list_add(&x->km.all, &net->xfrm.state_all);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001025 hlist_add_head_rcu(&x->bydst, net->xfrm.state_bydst + h);
Thomas Egerer8444cf72010-09-20 11:11:38 -07001026 h = xfrm_src_hash(net, daddr, saddr, encap_family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001027 hlist_add_head_rcu(&x->bysrc, net->xfrm.state_bysrc + h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 if (x->id.spi) {
Thomas Egerer8444cf72010-09-20 11:11:38 -07001029 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, encap_family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001030 hlist_add_head_rcu(&x->byspi, net->xfrm.state_byspi + h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 }
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08001032 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -08001033 tasklet_hrtimer_start(&x->mtimer, ktime_set(net->xfrm.sysctl_acq_expires, 0), HRTIMER_MODE_REL);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001034 net->xfrm.state_num++;
1035 xfrm_hash_grow_check(net, x->bydst.next != NULL);
Florian Westphald737a582016-08-09 12:16:09 +02001036 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 } else {
1038 x->km.state = XFRM_STATE_DEAD;
David S. Miller37b08e32008-09-02 20:14:15 -07001039 to_put = x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 x = NULL;
1041 error = -ESRCH;
1042 }
1043 }
1044out:
Florian Westphal02efdff2016-08-09 12:16:05 +02001045 if (x) {
1046 if (!xfrm_state_hold_rcu(x)) {
1047 *err = -EAGAIN;
1048 x = NULL;
1049 }
1050 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 *err = acquire_in_progress ? -EAGAIN : error;
Florian Westphal02efdff2016-08-09 12:16:05 +02001052 }
Florian Westphald737a582016-08-09 12:16:09 +02001053 rcu_read_unlock();
David S. Miller37b08e32008-09-02 20:14:15 -07001054 if (to_put)
1055 xfrm_state_put(to_put);
Florian Westphalb65e3d72016-08-09 12:16:07 +02001056
1057 if (read_seqcount_retry(&xfrm_state_hash_generation, sequence)) {
1058 *err = -EAGAIN;
1059 if (x) {
1060 xfrm_state_put(x);
1061 x = NULL;
1062 }
1063 }
1064
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 return x;
1066}
1067
Jamal Hadi Salim628529b2007-07-02 22:41:14 -07001068struct xfrm_state *
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001069xfrm_stateonly_find(struct net *net, u32 mark,
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001070 xfrm_address_t *daddr, xfrm_address_t *saddr,
Jamal Hadi Salim628529b2007-07-02 22:41:14 -07001071 unsigned short family, u8 mode, u8 proto, u32 reqid)
1072{
Pavel Emelyanov4bda4f22007-12-14 11:38:04 -08001073 unsigned int h;
Jamal Hadi Salim628529b2007-07-02 22:41:14 -07001074 struct xfrm_state *rx = NULL, *x = NULL;
Jamal Hadi Salim628529b2007-07-02 22:41:14 -07001075
Fan Du4ae770b2014-01-03 11:18:29 +08001076 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001077 h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001078 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
Jamal Hadi Salim628529b2007-07-02 22:41:14 -07001079 if (x->props.family == family &&
1080 x->props.reqid == reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001081 (mark & x->mark.m) == x->mark.v &&
Jamal Hadi Salim628529b2007-07-02 22:41:14 -07001082 !(x->props.flags & XFRM_STATE_WILDRECV) &&
1083 xfrm_state_addr_check(x, daddr, saddr, family) &&
1084 mode == x->props.mode &&
1085 proto == x->id.proto &&
1086 x->km.state == XFRM_STATE_VALID) {
1087 rx = x;
1088 break;
1089 }
1090 }
1091
1092 if (rx)
1093 xfrm_state_hold(rx);
Fan Du4ae770b2014-01-03 11:18:29 +08001094 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salim628529b2007-07-02 22:41:14 -07001095
1096
1097 return rx;
1098}
1099EXPORT_SYMBOL(xfrm_stateonly_find);
1100
Fan Duc4549972014-01-03 11:18:32 +08001101struct xfrm_state *xfrm_state_lookup_byspi(struct net *net, __be32 spi,
1102 unsigned short family)
1103{
1104 struct xfrm_state *x;
1105 struct xfrm_state_walk *w;
1106
1107 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1108 list_for_each_entry(w, &net->xfrm.state_all, all) {
1109 x = container_of(w, struct xfrm_state, km);
1110 if (x->props.family != family ||
1111 x->id.spi != spi)
1112 continue;
1113
Fan Duc4549972014-01-03 11:18:32 +08001114 xfrm_state_hold(x);
Li RongQingbdddbf62015-04-29 08:42:44 +08001115 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Fan Duc4549972014-01-03 11:18:32 +08001116 return x;
1117 }
1118 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1119 return NULL;
1120}
1121EXPORT_SYMBOL(xfrm_state_lookup_byspi);
1122
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123static void __xfrm_state_insert(struct xfrm_state *x)
1124{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001125 struct net *net = xs_net(x);
David S. Millera624c102006-08-24 03:24:33 -07001126 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001128 list_add(&x->km.all, &net->xfrm.state_all);
Timo Teras4c563f72008-02-28 21:31:08 -08001129
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001130 h = xfrm_dst_hash(net, &x->id.daddr, &x->props.saddr,
David S. Millerc1969f22006-08-24 04:00:03 -07001131 x->props.reqid, x->props.family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001132 hlist_add_head_rcu(&x->bydst, net->xfrm.state_bydst + h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001134 h = xfrm_src_hash(net, &x->id.daddr, &x->props.saddr, x->props.family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001135 hlist_add_head_rcu(&x->bysrc, net->xfrm.state_bysrc + h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
Masahide NAKAMURA7b4dc3602006-09-27 22:21:52 -07001137 if (x->id.spi) {
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001138 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto,
Masahide NAKAMURA6c44e6b2006-08-23 17:53:57 -07001139 x->props.family);
1140
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001141 hlist_add_head_rcu(&x->byspi, net->xfrm.state_byspi + h);
Masahide NAKAMURA6c44e6b2006-08-23 17:53:57 -07001142 }
1143
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -08001144 tasklet_hrtimer_start(&x->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL);
David S. Millera47f0ce2006-08-24 03:54:22 -07001145 if (x->replay_maxage)
1146 mod_timer(&x->rtimer, jiffies + x->replay_maxage);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001147
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001148 net->xfrm.state_num++;
David S. Millerf034b5d2006-08-24 03:08:07 -07001149
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001150 xfrm_hash_grow_check(net, x->bydst.next != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151}
1152
Fan Du283bc9f2013-11-07 17:47:50 +08001153/* net->xfrm.xfrm_state_lock is held */
David S. Millerc7f5ea32006-08-24 03:29:04 -07001154static void __xfrm_state_bump_genids(struct xfrm_state *xnew)
1155{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001156 struct net *net = xs_net(xnew);
David S. Millerc7f5ea32006-08-24 03:29:04 -07001157 unsigned short family = xnew->props.family;
1158 u32 reqid = xnew->props.reqid;
1159 struct xfrm_state *x;
David S. Millerc7f5ea32006-08-24 03:29:04 -07001160 unsigned int h;
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001161 u32 mark = xnew->mark.v & xnew->mark.m;
David S. Millerc7f5ea32006-08-24 03:29:04 -07001162
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001163 h = xfrm_dst_hash(net, &xnew->id.daddr, &xnew->props.saddr, reqid, family);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001164 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
David S. Millerc7f5ea32006-08-24 03:29:04 -07001165 if (x->props.family == family &&
1166 x->props.reqid == reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001167 (mark & x->mark.m) == x->mark.v &&
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001168 xfrm_addr_equal(&x->id.daddr, &xnew->id.daddr, family) &&
1169 xfrm_addr_equal(&x->props.saddr, &xnew->props.saddr, family))
Herbert Xu34996cb2010-03-31 01:19:49 +00001170 x->genid++;
David S. Millerc7f5ea32006-08-24 03:29:04 -07001171 }
1172}
1173
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174void xfrm_state_insert(struct xfrm_state *x)
1175{
Fan Du283bc9f2013-11-07 17:47:50 +08001176 struct net *net = xs_net(x);
1177
1178 spin_lock_bh(&net->xfrm.xfrm_state_lock);
David S. Millerc7f5ea32006-08-24 03:29:04 -07001179 __xfrm_state_bump_genids(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 __xfrm_state_insert(x);
Fan Du283bc9f2013-11-07 17:47:50 +08001181 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182}
1183EXPORT_SYMBOL(xfrm_state_insert);
1184
Fan Du283bc9f2013-11-07 17:47:50 +08001185/* net->xfrm.xfrm_state_lock is held */
Mathias Krausee473fcb2013-06-26 23:56:58 +02001186static struct xfrm_state *__find_acq_core(struct net *net,
1187 const struct xfrm_mark *m,
David S. Millera70486f2011-02-27 23:17:24 -08001188 unsigned short family, u8 mode,
1189 u32 reqid, u8 proto,
1190 const xfrm_address_t *daddr,
Mathias Krausee473fcb2013-06-26 23:56:58 +02001191 const xfrm_address_t *saddr,
1192 int create)
David S. Miller27708342006-08-24 00:13:10 -07001193{
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001194 unsigned int h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
David S. Miller27708342006-08-24 00:13:10 -07001195 struct xfrm_state *x;
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001196 u32 mark = m->v & m->m;
David S. Miller27708342006-08-24 00:13:10 -07001197
Sasha Levinb67bfe02013-02-27 17:06:00 -08001198 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
David S. Miller27708342006-08-24 00:13:10 -07001199 if (x->props.reqid != reqid ||
1200 x->props.mode != mode ||
1201 x->props.family != family ||
1202 x->km.state != XFRM_STATE_ACQ ||
Joy Latten75e252d2007-03-12 17:14:07 -07001203 x->id.spi != 0 ||
Wei Yongjun18025712009-06-28 18:42:53 +00001204 x->id.proto != proto ||
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001205 (mark & x->mark.m) != x->mark.v ||
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001206 !xfrm_addr_equal(&x->id.daddr, daddr, family) ||
1207 !xfrm_addr_equal(&x->props.saddr, saddr, family))
David S. Miller27708342006-08-24 00:13:10 -07001208 continue;
1209
David S. Miller27708342006-08-24 00:13:10 -07001210 xfrm_state_hold(x);
1211 return x;
1212 }
1213
1214 if (!create)
1215 return NULL;
1216
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001217 x = xfrm_state_alloc(net);
David S. Miller27708342006-08-24 00:13:10 -07001218 if (likely(x)) {
1219 switch (family) {
1220 case AF_INET:
1221 x->sel.daddr.a4 = daddr->a4;
1222 x->sel.saddr.a4 = saddr->a4;
1223 x->sel.prefixlen_d = 32;
1224 x->sel.prefixlen_s = 32;
1225 x->props.saddr.a4 = saddr->a4;
1226 x->id.daddr.a4 = daddr->a4;
1227 break;
1228
1229 case AF_INET6:
Jiri Benc15e318b2015-03-29 16:59:24 +02001230 x->sel.daddr.in6 = daddr->in6;
1231 x->sel.saddr.in6 = saddr->in6;
David S. Miller27708342006-08-24 00:13:10 -07001232 x->sel.prefixlen_d = 128;
1233 x->sel.prefixlen_s = 128;
Jiri Benc15e318b2015-03-29 16:59:24 +02001234 x->props.saddr.in6 = saddr->in6;
1235 x->id.daddr.in6 = daddr->in6;
David S. Miller27708342006-08-24 00:13:10 -07001236 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001237 }
David S. Miller27708342006-08-24 00:13:10 -07001238
1239 x->km.state = XFRM_STATE_ACQ;
1240 x->id.proto = proto;
1241 x->props.family = family;
1242 x->props.mode = mode;
1243 x->props.reqid = reqid;
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001244 x->mark.v = m->v;
1245 x->mark.m = m->m;
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08001246 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
David S. Miller27708342006-08-24 00:13:10 -07001247 xfrm_state_hold(x);
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -08001248 tasklet_hrtimer_start(&x->mtimer, ktime_set(net->xfrm.sysctl_acq_expires, 0), HRTIMER_MODE_REL);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001249 list_add(&x->km.all, &net->xfrm.state_all);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001250 hlist_add_head_rcu(&x->bydst, net->xfrm.state_bydst + h);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001251 h = xfrm_src_hash(net, daddr, saddr, family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001252 hlist_add_head_rcu(&x->bysrc, net->xfrm.state_bysrc + h);
David S. Miller918049f2006-10-12 22:03:24 -07001253
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001254 net->xfrm.state_num++;
David S. Miller918049f2006-10-12 22:03:24 -07001255
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001256 xfrm_hash_grow_check(net, x->bydst.next != NULL);
David S. Miller27708342006-08-24 00:13:10 -07001257 }
1258
1259 return x;
1260}
1261
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001262static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
1264int xfrm_state_add(struct xfrm_state *x)
1265{
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001266 struct net *net = xs_net(x);
David S. Miller37b08e32008-09-02 20:14:15 -07001267 struct xfrm_state *x1, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 int family;
1269 int err;
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001270 u32 mark = x->mark.v & x->mark.m;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001271 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
1273 family = x->props.family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
David S. Miller37b08e32008-09-02 20:14:15 -07001275 to_put = NULL;
1276
Fan Du283bc9f2013-11-07 17:47:50 +08001277 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
David S. Milleredcd5822006-08-24 00:42:45 -07001279 x1 = __xfrm_state_locate(x, use_spi, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 if (x1) {
David S. Miller37b08e32008-09-02 20:14:15 -07001281 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 x1 = NULL;
1283 err = -EEXIST;
1284 goto out;
1285 }
1286
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001287 if (use_spi && x->km.seq) {
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001288 x1 = __xfrm_find_acq_byseq(net, mark, x->km.seq);
Joy Latten75e252d2007-03-12 17:14:07 -07001289 if (x1 && ((x1->id.proto != x->id.proto) ||
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001290 !xfrm_addr_equal(&x1->id.daddr, &x->id.daddr, family))) {
David S. Miller37b08e32008-09-02 20:14:15 -07001291 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 x1 = NULL;
1293 }
1294 }
1295
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001296 if (use_spi && !x1)
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001297 x1 = __find_acq_core(net, &x->mark, family, x->props.mode,
1298 x->props.reqid, x->id.proto,
David S. Miller27708342006-08-24 00:13:10 -07001299 &x->id.daddr, &x->props.saddr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
David S. Millerc7f5ea32006-08-24 03:29:04 -07001301 __xfrm_state_bump_genids(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 __xfrm_state_insert(x);
1303 err = 0;
1304
1305out:
Fan Du283bc9f2013-11-07 17:47:50 +08001306 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
1308 if (x1) {
1309 xfrm_state_delete(x1);
1310 xfrm_state_put(x1);
1311 }
1312
David S. Miller37b08e32008-09-02 20:14:15 -07001313 if (to_put)
1314 xfrm_state_put(to_put);
1315
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 return err;
1317}
1318EXPORT_SYMBOL(xfrm_state_add);
1319
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001320#ifdef CONFIG_XFRM_MIGRATE
Antony Antony4ab47d42017-06-06 12:12:13 +02001321static struct xfrm_state *xfrm_state_clone(struct xfrm_state *orig,
1322 struct xfrm_encap_tmpl *encap)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001323{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001324 struct net *net = xs_net(orig);
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001325 struct xfrm_state *x = xfrm_state_alloc(net);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001326 if (!x)
Herbert Xu553f9112010-02-15 20:00:51 +00001327 goto out;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001328
1329 memcpy(&x->id, &orig->id, sizeof(x->id));
1330 memcpy(&x->sel, &orig->sel, sizeof(x->sel));
1331 memcpy(&x->lft, &orig->lft, sizeof(x->lft));
1332 x->props.mode = orig->props.mode;
1333 x->props.replay_window = orig->props.replay_window;
1334 x->props.reqid = orig->props.reqid;
1335 x->props.family = orig->props.family;
1336 x->props.saddr = orig->props.saddr;
1337
1338 if (orig->aalg) {
Martin Willi4447bb32009-11-25 00:29:52 +00001339 x->aalg = xfrm_algo_auth_clone(orig->aalg);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001340 if (!x->aalg)
1341 goto error;
1342 }
1343 x->props.aalgo = orig->props.aalgo;
1344
Steffen Klassertee5c2312014-02-19 13:33:24 +01001345 if (orig->aead) {
1346 x->aead = xfrm_algo_aead_clone(orig->aead);
Antony Antony75bf50f2017-12-07 21:54:27 +01001347 x->geniv = orig->geniv;
Steffen Klassertee5c2312014-02-19 13:33:24 +01001348 if (!x->aead)
1349 goto error;
1350 }
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001351 if (orig->ealg) {
1352 x->ealg = xfrm_algo_clone(orig->ealg);
1353 if (!x->ealg)
1354 goto error;
1355 }
1356 x->props.ealgo = orig->props.ealgo;
1357
1358 if (orig->calg) {
1359 x->calg = xfrm_algo_clone(orig->calg);
1360 if (!x->calg)
1361 goto error;
1362 }
1363 x->props.calgo = orig->props.calgo;
1364
Antony Antony4ab47d42017-06-06 12:12:13 +02001365 if (encap || orig->encap) {
1366 if (encap)
1367 x->encap = kmemdup(encap, sizeof(*x->encap),
1368 GFP_KERNEL);
1369 else
1370 x->encap = kmemdup(orig->encap, sizeof(*x->encap),
1371 GFP_KERNEL);
1372
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001373 if (!x->encap)
1374 goto error;
1375 }
1376
1377 if (orig->coaddr) {
1378 x->coaddr = kmemdup(orig->coaddr, sizeof(*x->coaddr),
1379 GFP_KERNEL);
1380 if (!x->coaddr)
1381 goto error;
1382 }
1383
Steffen Klassertaf2f4642011-03-28 19:46:39 +00001384 if (orig->replay_esn) {
Steffen Klassertcc9ab602014-02-19 13:33:24 +01001385 if (xfrm_replay_clone(x, orig))
Steffen Klassertaf2f4642011-03-28 19:46:39 +00001386 goto error;
1387 }
1388
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001389 memcpy(&x->mark, &orig->mark, sizeof(x->mark));
1390
Steffen Klassertcc9ab602014-02-19 13:33:24 +01001391 if (xfrm_init_state(x) < 0)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001392 goto error;
1393
1394 x->props.flags = orig->props.flags;
Nicolas Dichtela947b0a2013-02-22 10:54:54 +01001395 x->props.extra_flags = orig->props.extra_flags;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001396
Steffen Klassertee5c2312014-02-19 13:33:24 +01001397 x->tfcpad = orig->tfcpad;
1398 x->replay_maxdiff = orig->replay_maxdiff;
1399 x->replay_maxage = orig->replay_maxage;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001400 x->curlft.add_time = orig->curlft.add_time;
1401 x->km.state = orig->km.state;
1402 x->km.seq = orig->km.seq;
Antony Antonya486cd22017-05-19 12:47:00 +02001403 x->replay = orig->replay;
1404 x->preplay = orig->preplay;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001405
1406 return x;
1407
1408 error:
Herbert Xu553f9112010-02-15 20:00:51 +00001409 xfrm_state_put(x);
1410out:
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001411 return NULL;
1412}
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001413
Fan Du283bc9f2013-11-07 17:47:50 +08001414struct xfrm_state *xfrm_migrate_state_find(struct xfrm_migrate *m, struct net *net)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001415{
1416 unsigned int h;
Steffen Klassert8c0cba22014-02-19 13:33:24 +01001417 struct xfrm_state *x = NULL;
1418
1419 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001420
1421 if (m->reqid) {
Fan Du283bc9f2013-11-07 17:47:50 +08001422 h = xfrm_dst_hash(net, &m->old_daddr, &m->old_saddr,
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001423 m->reqid, m->old_family);
Fan Du283bc9f2013-11-07 17:47:50 +08001424 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001425 if (x->props.mode != m->mode ||
1426 x->id.proto != m->proto)
1427 continue;
1428 if (m->reqid && x->props.reqid != m->reqid)
1429 continue;
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001430 if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr,
1431 m->old_family) ||
1432 !xfrm_addr_equal(&x->props.saddr, &m->old_saddr,
1433 m->old_family))
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001434 continue;
1435 xfrm_state_hold(x);
Steffen Klassert8c0cba22014-02-19 13:33:24 +01001436 break;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001437 }
1438 } else {
Fan Du283bc9f2013-11-07 17:47:50 +08001439 h = xfrm_src_hash(net, &m->old_daddr, &m->old_saddr,
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001440 m->old_family);
Fan Du283bc9f2013-11-07 17:47:50 +08001441 hlist_for_each_entry(x, net->xfrm.state_bysrc+h, bysrc) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001442 if (x->props.mode != m->mode ||
1443 x->id.proto != m->proto)
1444 continue;
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001445 if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr,
1446 m->old_family) ||
1447 !xfrm_addr_equal(&x->props.saddr, &m->old_saddr,
1448 m->old_family))
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001449 continue;
1450 xfrm_state_hold(x);
Steffen Klassert8c0cba22014-02-19 13:33:24 +01001451 break;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001452 }
1453 }
1454
Steffen Klassert8c0cba22014-02-19 13:33:24 +01001455 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1456
1457 return x;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001458}
1459EXPORT_SYMBOL(xfrm_migrate_state_find);
1460
Weilong Chen3e94c2d2013-12-24 09:43:47 +08001461struct xfrm_state *xfrm_state_migrate(struct xfrm_state *x,
Antony Antony4ab47d42017-06-06 12:12:13 +02001462 struct xfrm_migrate *m,
1463 struct xfrm_encap_tmpl *encap)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001464{
1465 struct xfrm_state *xc;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001466
Antony Antony4ab47d42017-06-06 12:12:13 +02001467 xc = xfrm_state_clone(x, encap);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001468 if (!xc)
1469 return NULL;
1470
1471 memcpy(&xc->id.daddr, &m->new_daddr, sizeof(xc->id.daddr));
1472 memcpy(&xc->props.saddr, &m->new_saddr, sizeof(xc->props.saddr));
1473
1474 /* add state */
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001475 if (xfrm_addr_equal(&x->id.daddr, &m->new_daddr, m->new_family)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001476 /* a care is needed when the destination address of the
1477 state is to be updated as it is a part of triplet */
1478 xfrm_state_insert(xc);
1479 } else {
Steffen Klassertcc9ab602014-02-19 13:33:24 +01001480 if (xfrm_state_add(xc) < 0)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001481 goto error;
1482 }
1483
1484 return xc;
1485error:
Thomas Egerer78347c82010-12-06 23:28:56 +00001486 xfrm_state_put(xc);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001487 return NULL;
1488}
1489EXPORT_SYMBOL(xfrm_state_migrate);
1490#endif
1491
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492int xfrm_state_update(struct xfrm_state *x)
1493{
David S. Miller37b08e32008-09-02 20:14:15 -07001494 struct xfrm_state *x1, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 int err;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001496 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
Fan Du283bc9f2013-11-07 17:47:50 +08001497 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
David S. Miller37b08e32008-09-02 20:14:15 -07001499 to_put = NULL;
1500
Fan Du283bc9f2013-11-07 17:47:50 +08001501 spin_lock_bh(&net->xfrm.xfrm_state_lock);
David S. Milleredcd5822006-08-24 00:42:45 -07001502 x1 = __xfrm_state_locate(x, use_spi, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
1504 err = -ESRCH;
1505 if (!x1)
1506 goto out;
1507
1508 if (xfrm_state_kern(x1)) {
David S. Miller37b08e32008-09-02 20:14:15 -07001509 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 err = -EEXIST;
1511 goto out;
1512 }
1513
1514 if (x1->km.state == XFRM_STATE_ACQ) {
1515 __xfrm_state_insert(x);
1516 x = NULL;
1517 }
1518 err = 0;
1519
1520out:
Fan Du283bc9f2013-11-07 17:47:50 +08001521 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
David S. Miller37b08e32008-09-02 20:14:15 -07001523 if (to_put)
1524 xfrm_state_put(to_put);
1525
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 if (err)
1527 return err;
1528
1529 if (!x) {
1530 xfrm_state_delete(x1);
1531 xfrm_state_put(x1);
1532 return 0;
1533 }
1534
1535 err = -EINVAL;
1536 spin_lock_bh(&x1->lock);
1537 if (likely(x1->km.state == XFRM_STATE_VALID)) {
Herbert Xu257a4b02017-12-26 17:34:44 +11001538 if (x->encap && x1->encap &&
1539 x->encap->encap_type == x1->encap->encap_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 memcpy(x1->encap, x->encap, sizeof(*x1->encap));
Herbert Xu257a4b02017-12-26 17:34:44 +11001541 else if (x->encap || x1->encap)
1542 goto fail;
1543
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -07001544 if (x->coaddr && x1->coaddr) {
1545 memcpy(x1->coaddr, x->coaddr, sizeof(*x1->coaddr));
1546 }
1547 if (!use_spi && memcmp(&x1->sel, &x->sel, sizeof(x1->sel)))
1548 memcpy(&x1->sel, &x->sel, sizeof(x1->sel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 memcpy(&x1->lft, &x->lft, sizeof(x1->lft));
1550 x1->km.dying = 0;
1551
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -08001552 tasklet_hrtimer_start(&x1->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 if (x1->curlft.use_time)
1554 xfrm_state_check_expire(x1);
1555
1556 err = 0;
Tushar Gohad8fcbc632011-07-07 15:38:52 +00001557 x->km.state = XFRM_STATE_DEAD;
1558 __xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 }
Herbert Xu257a4b02017-12-26 17:34:44 +11001560
1561fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 spin_unlock_bh(&x1->lock);
1563
1564 xfrm_state_put(x1);
1565
1566 return err;
1567}
1568EXPORT_SYMBOL(xfrm_state_update);
1569
1570int xfrm_state_check_expire(struct xfrm_state *x)
1571{
1572 if (!x->curlft.use_time)
James Morris9d729f72007-03-04 16:12:44 -08001573 x->curlft.use_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 if (x->curlft.bytes >= x->lft.hard_byte_limit ||
1576 x->curlft.packets >= x->lft.hard_packet_limit) {
Herbert Xu4666faa2005-06-18 22:43:22 -07001577 x->km.state = XFRM_STATE_EXPIRED;
Thomas Gleixner8b0e1952016-12-25 12:30:41 +01001578 tasklet_hrtimer_start(&x->mtimer, 0, HRTIMER_MODE_REL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 return -EINVAL;
1580 }
1581
1582 if (!x->km.dying &&
1583 (x->curlft.bytes >= x->lft.soft_byte_limit ||
Herbert Xu4666faa2005-06-18 22:43:22 -07001584 x->curlft.packets >= x->lft.soft_packet_limit)) {
1585 x->km.dying = 1;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001586 km_state_expired(x, 0, 0);
Herbert Xu4666faa2005-06-18 22:43:22 -07001587 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 return 0;
1589}
1590EXPORT_SYMBOL(xfrm_state_check_expire);
1591
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592struct xfrm_state *
David S. Millera70486f2011-02-27 23:17:24 -08001593xfrm_state_lookup(struct net *net, u32 mark, const xfrm_address_t *daddr, __be32 spi,
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001594 u8 proto, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595{
1596 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
Florian Westphalc2f672f2016-09-20 15:45:26 +02001598 rcu_read_lock();
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001599 x = __xfrm_state_lookup(net, mark, daddr, spi, proto, family);
Florian Westphalc2f672f2016-09-20 15:45:26 +02001600 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 return x;
1602}
1603EXPORT_SYMBOL(xfrm_state_lookup);
1604
1605struct xfrm_state *
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001606xfrm_state_lookup_byaddr(struct net *net, u32 mark,
David S. Millera70486f2011-02-27 23:17:24 -08001607 const xfrm_address_t *daddr, const xfrm_address_t *saddr,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001608 u8 proto, unsigned short family)
1609{
1610 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001611
Fan Du283bc9f2013-11-07 17:47:50 +08001612 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001613 x = __xfrm_state_lookup_byaddr(net, mark, daddr, saddr, proto, family);
Fan Du283bc9f2013-11-07 17:47:50 +08001614 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001615 return x;
1616}
1617EXPORT_SYMBOL(xfrm_state_lookup_byaddr);
1618
1619struct xfrm_state *
Mathias Krausee473fcb2013-06-26 23:56:58 +02001620xfrm_find_acq(struct net *net, const struct xfrm_mark *mark, u8 mode, u32 reqid,
1621 u8 proto, const xfrm_address_t *daddr,
1622 const xfrm_address_t *saddr, int create, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623{
1624 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
Fan Du283bc9f2013-11-07 17:47:50 +08001626 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001627 x = __find_acq_core(net, mark, family, mode, reqid, proto, daddr, saddr, create);
Fan Du283bc9f2013-11-07 17:47:50 +08001628 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
David S. Miller27708342006-08-24 00:13:10 -07001629
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 return x;
1631}
1632EXPORT_SYMBOL(xfrm_find_acq);
1633
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001634#ifdef CONFIG_XFRM_SUB_POLICY
1635int
1636xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n,
Fan Du283bc9f2013-11-07 17:47:50 +08001637 unsigned short family, struct net *net)
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001638{
Koichiro Den3f5a95a2017-08-01 23:21:46 +09001639 int i;
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001640 int err = 0;
1641 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
1642 if (!afinfo)
1643 return -EAFNOSUPPORT;
1644
Fan Du283bc9f2013-11-07 17:47:50 +08001645 spin_lock_bh(&net->xfrm.xfrm_state_lock); /*FIXME*/
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001646 if (afinfo->tmpl_sort)
1647 err = afinfo->tmpl_sort(dst, src, n);
Koichiro Den3f5a95a2017-08-01 23:21:46 +09001648 else
1649 for (i = 0; i < n; i++)
1650 dst[i] = src[i];
Fan Du283bc9f2013-11-07 17:47:50 +08001651 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Florian Westphalaf5d27c2017-01-09 14:20:47 +01001652 rcu_read_unlock();
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001653 return err;
1654}
1655EXPORT_SYMBOL(xfrm_tmpl_sort);
1656
1657int
1658xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src, int n,
1659 unsigned short family)
1660{
Koichiro Den3f5a95a2017-08-01 23:21:46 +09001661 int i;
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001662 int err = 0;
1663 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
Steffen Klassert35ea790d2014-02-19 13:33:23 +01001664 struct net *net = xs_net(*src);
Fan Du283bc9f2013-11-07 17:47:50 +08001665
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001666 if (!afinfo)
1667 return -EAFNOSUPPORT;
1668
Fan Du283bc9f2013-11-07 17:47:50 +08001669 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001670 if (afinfo->state_sort)
1671 err = afinfo->state_sort(dst, src, n);
Koichiro Den3f5a95a2017-08-01 23:21:46 +09001672 else
1673 for (i = 0; i < n; i++)
1674 dst[i] = src[i];
Fan Du283bc9f2013-11-07 17:47:50 +08001675 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Florian Westphalaf5d27c2017-01-09 14:20:47 +01001676 rcu_read_unlock();
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001677 return err;
1678}
1679EXPORT_SYMBOL(xfrm_state_sort);
1680#endif
1681
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682/* Silly enough, but I'm lazy to build resolution list */
1683
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001684static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685{
1686 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001688 for (i = 0; i <= net->xfrm.state_hmask; i++) {
David S. Miller8f126e32006-08-24 02:45:07 -07001689 struct xfrm_state *x;
1690
Sasha Levinb67bfe02013-02-27 17:06:00 -08001691 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
David S. Miller8f126e32006-08-24 02:45:07 -07001692 if (x->km.seq == seq &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001693 (mark & x->mark.m) == x->mark.v &&
David S. Miller8f126e32006-08-24 02:45:07 -07001694 x->km.state == XFRM_STATE_ACQ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 xfrm_state_hold(x);
1696 return x;
1697 }
1698 }
1699 }
1700 return NULL;
1701}
1702
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001703struct xfrm_state *xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704{
1705 struct xfrm_state *x;
1706
Fan Du283bc9f2013-11-07 17:47:50 +08001707 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001708 x = __xfrm_find_acq_byseq(net, mark, seq);
Fan Du283bc9f2013-11-07 17:47:50 +08001709 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 return x;
1711}
1712EXPORT_SYMBOL(xfrm_find_acq_byseq);
1713
1714u32 xfrm_get_acqseq(void)
1715{
1716 u32 res;
jamal6836b9b2010-02-16 02:01:22 +00001717 static atomic_t acqseq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
jamal6836b9b2010-02-16 02:01:22 +00001719 do {
1720 res = atomic_inc_return(&acqseq);
1721 } while (!res);
1722
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 return res;
1724}
1725EXPORT_SYMBOL(xfrm_get_acqseq);
1726
Fan Du776e9dd2013-12-16 18:47:49 +08001727int verify_spi_info(u8 proto, u32 min, u32 max)
1728{
1729 switch (proto) {
1730 case IPPROTO_AH:
1731 case IPPROTO_ESP:
1732 break;
1733
1734 case IPPROTO_COMP:
1735 /* IPCOMP spi is 16-bits. */
1736 if (max >= 0x10000)
1737 return -EINVAL;
1738 break;
1739
1740 default:
1741 return -EINVAL;
1742 }
1743
1744 if (min > max)
1745 return -EINVAL;
1746
1747 return 0;
1748}
1749EXPORT_SYMBOL(verify_spi_info);
1750
Herbert Xu658b2192007-10-09 13:29:52 -07001751int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -08001753 struct net *net = xs_net(x);
David S. Millerf034b5d2006-08-24 03:08:07 -07001754 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 struct xfrm_state *x0;
Herbert Xu658b2192007-10-09 13:29:52 -07001756 int err = -ENOENT;
1757 __be32 minspi = htonl(low);
1758 __be32 maxspi = htonl(high);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001759 u32 mark = x->mark.v & x->mark.m;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760
Herbert Xu658b2192007-10-09 13:29:52 -07001761 spin_lock_bh(&x->lock);
1762 if (x->km.state == XFRM_STATE_DEAD)
1763 goto unlock;
1764
1765 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 if (x->id.spi)
Herbert Xu658b2192007-10-09 13:29:52 -07001767 goto unlock;
1768
1769 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
1771 if (minspi == maxspi) {
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001772 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, minspi, x->id.proto, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 if (x0) {
1774 xfrm_state_put(x0);
Herbert Xu658b2192007-10-09 13:29:52 -07001775 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 }
1777 x->id.spi = minspi;
1778 } else {
1779 u32 spi = 0;
Weilong Chen9b7a7872013-12-24 09:43:46 +08001780 for (h = 0; h < high-low+1; h++) {
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05001781 spi = low + prandom_u32()%(high-low+1);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001782 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, htonl(spi), x->id.proto, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 if (x0 == NULL) {
1784 x->id.spi = htonl(spi);
1785 break;
1786 }
1787 xfrm_state_put(x0);
1788 }
1789 }
1790 if (x->id.spi) {
Fan Du283bc9f2013-11-07 17:47:50 +08001791 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Alexey Dobriyan12604d82008-11-25 17:31:18 -08001792 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, x->props.family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001793 hlist_add_head_rcu(&x->byspi, net->xfrm.state_byspi + h);
Fan Du283bc9f2013-11-07 17:47:50 +08001794 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Herbert Xu658b2192007-10-09 13:29:52 -07001795
1796 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 }
Herbert Xu658b2192007-10-09 13:29:52 -07001798
1799unlock:
1800 spin_unlock_bh(&x->lock);
1801
1802 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803}
1804EXPORT_SYMBOL(xfrm_alloc_spi);
1805
Nicolas Dichteld3623092014-02-14 15:30:36 +01001806static bool __xfrm_state_filter_match(struct xfrm_state *x,
Nicolas Dichtel870a2df2014-03-06 18:24:29 +01001807 struct xfrm_address_filter *filter)
Nicolas Dichteld3623092014-02-14 15:30:36 +01001808{
1809 if (filter) {
1810 if ((filter->family == AF_INET ||
1811 filter->family == AF_INET6) &&
1812 x->props.family != filter->family)
1813 return false;
1814
1815 return addr_match(&x->props.saddr, &filter->saddr,
1816 filter->splen) &&
1817 addr_match(&x->id.daddr, &filter->daddr,
1818 filter->dplen);
1819 }
1820 return true;
1821}
1822
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08001823int xfrm_state_walk(struct net *net, struct xfrm_state_walk *walk,
Timo Teras4c563f72008-02-28 21:31:08 -08001824 int (*func)(struct xfrm_state *, int, void*),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 void *data)
1826{
Herbert Xu12a169e2008-10-01 07:03:24 -07001827 struct xfrm_state *state;
1828 struct xfrm_state_walk *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 int err = 0;
1830
Herbert Xu12a169e2008-10-01 07:03:24 -07001831 if (walk->seq != 0 && list_empty(&walk->all))
Timo Teras4c563f72008-02-28 21:31:08 -08001832 return 0;
1833
Fan Du283bc9f2013-11-07 17:47:50 +08001834 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001835 if (list_empty(&walk->all))
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08001836 x = list_first_entry(&net->xfrm.state_all, struct xfrm_state_walk, all);
Herbert Xu12a169e2008-10-01 07:03:24 -07001837 else
Li RongQing80077702015-04-22 17:09:54 +08001838 x = list_first_entry(&walk->all, struct xfrm_state_walk, all);
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08001839 list_for_each_entry_from(x, &net->xfrm.state_all, all) {
Herbert Xu12a169e2008-10-01 07:03:24 -07001840 if (x->state == XFRM_STATE_DEAD)
Timo Teras4c563f72008-02-28 21:31:08 -08001841 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07001842 state = container_of(x, struct xfrm_state, km);
1843 if (!xfrm_id_proto_match(state->id.proto, walk->proto))
Timo Teras4c563f72008-02-28 21:31:08 -08001844 continue;
Nicolas Dichteld3623092014-02-14 15:30:36 +01001845 if (!__xfrm_state_filter_match(state, walk->filter))
1846 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07001847 err = func(state, walk->seq, data);
1848 if (err) {
1849 list_move_tail(&walk->all, &x->all);
1850 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001852 walk->seq++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001854 if (walk->seq == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 err = -ENOENT;
1856 goto out;
1857 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001858 list_del_init(&walk->all);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859out:
Fan Du283bc9f2013-11-07 17:47:50 +08001860 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 return err;
1862}
1863EXPORT_SYMBOL(xfrm_state_walk);
1864
Nicolas Dichteld3623092014-02-14 15:30:36 +01001865void xfrm_state_walk_init(struct xfrm_state_walk *walk, u8 proto,
Nicolas Dichtel870a2df2014-03-06 18:24:29 +01001866 struct xfrm_address_filter *filter)
Herbert Xu5c182452008-09-22 19:48:19 -07001867{
Herbert Xu12a169e2008-10-01 07:03:24 -07001868 INIT_LIST_HEAD(&walk->all);
Herbert Xu5c182452008-09-22 19:48:19 -07001869 walk->proto = proto;
Herbert Xu12a169e2008-10-01 07:03:24 -07001870 walk->state = XFRM_STATE_DEAD;
1871 walk->seq = 0;
Nicolas Dichteld3623092014-02-14 15:30:36 +01001872 walk->filter = filter;
Herbert Xu5c182452008-09-22 19:48:19 -07001873}
1874EXPORT_SYMBOL(xfrm_state_walk_init);
1875
Fan Du283bc9f2013-11-07 17:47:50 +08001876void xfrm_state_walk_done(struct xfrm_state_walk *walk, struct net *net)
Herbert Xuabb81c42008-09-09 19:58:29 -07001877{
Nicolas Dichteld3623092014-02-14 15:30:36 +01001878 kfree(walk->filter);
1879
Herbert Xu12a169e2008-10-01 07:03:24 -07001880 if (list_empty(&walk->all))
Herbert Xu5c182452008-09-22 19:48:19 -07001881 return;
Herbert Xu5c182452008-09-22 19:48:19 -07001882
Fan Du283bc9f2013-11-07 17:47:50 +08001883 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001884 list_del(&walk->all);
Fan Du283bc9f2013-11-07 17:47:50 +08001885 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Herbert Xuabb81c42008-09-09 19:58:29 -07001886}
1887EXPORT_SYMBOL(xfrm_state_walk_done);
1888
Kees Cooke99e88a2017-10-16 14:43:17 -07001889static void xfrm_replay_timer_handler(struct timer_list *t)
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001890{
Kees Cooke99e88a2017-10-16 14:43:17 -07001891 struct xfrm_state *x = from_timer(x, t, rtimer);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001892
1893 spin_lock(&x->lock);
1894
Jamal Hadi Salim27170962006-04-14 15:03:05 -07001895 if (x->km.state == XFRM_STATE_VALID) {
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001896 if (xfrm_aevent_is_on(xs_net(x)))
Steffen Klassert9fdc4882011-03-08 00:08:32 +00001897 x->repl->notify(x, XFRM_REPLAY_TIMEOUT);
Jamal Hadi Salim27170962006-04-14 15:03:05 -07001898 else
1899 x->xflags |= XFRM_TIME_DEFER;
1900 }
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001901
1902 spin_unlock(&x->lock);
1903}
1904
Denis Chengdf018122007-12-07 00:51:11 -08001905static LIST_HEAD(xfrm_km_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906
David S. Miller214e0052011-02-24 00:02:38 -05001907void km_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908{
1909 struct xfrm_mgr *km;
1910
Cong Wang85168c02013-01-16 16:05:06 +08001911 rcu_read_lock();
1912 list_for_each_entry_rcu(km, &xfrm_km_list, list)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001913 if (km->notify_policy)
1914 km->notify_policy(xp, dir, c);
Cong Wang85168c02013-01-16 16:05:06 +08001915 rcu_read_unlock();
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001916}
1917
David S. Miller214e0052011-02-24 00:02:38 -05001918void km_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001919{
1920 struct xfrm_mgr *km;
Cong Wang85168c02013-01-16 16:05:06 +08001921 rcu_read_lock();
1922 list_for_each_entry_rcu(km, &xfrm_km_list, list)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001923 if (km->notify)
1924 km->notify(x, c);
Cong Wang85168c02013-01-16 16:05:06 +08001925 rcu_read_unlock();
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001926}
1927
1928EXPORT_SYMBOL(km_policy_notify);
1929EXPORT_SYMBOL(km_state_notify);
1930
Eric W. Biederman15e47302012-09-07 20:12:54 +00001931void km_state_expired(struct xfrm_state *x, int hard, u32 portid)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001932{
1933 struct km_event c;
1934
Herbert Xubf08867f92005-06-18 22:44:00 -07001935 c.data.hard = hard;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001936 c.portid = portid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001937 c.event = XFRM_MSG_EXPIRE;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001938 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939}
1940
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001941EXPORT_SYMBOL(km_state_expired);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001942/*
1943 * We send to all registered managers regardless of failure
1944 * We are happy with one success
1945*/
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001946int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001948 int err = -EINVAL, acqret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 struct xfrm_mgr *km;
1950
Cong Wang85168c02013-01-16 16:05:06 +08001951 rcu_read_lock();
1952 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Fan Du65e07362012-08-15 10:13:47 +08001953 acqret = km->acquire(x, t, pol);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001954 if (!acqret)
1955 err = acqret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 }
Cong Wang85168c02013-01-16 16:05:06 +08001957 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 return err;
1959}
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001960EXPORT_SYMBOL(km_query);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961
Al Viro5d36b182006-11-08 00:24:06 -08001962int km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963{
1964 int err = -EINVAL;
1965 struct xfrm_mgr *km;
1966
Cong Wang85168c02013-01-16 16:05:06 +08001967 rcu_read_lock();
1968 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 if (km->new_mapping)
1970 err = km->new_mapping(x, ipaddr, sport);
1971 if (!err)
1972 break;
1973 }
Cong Wang85168c02013-01-16 16:05:06 +08001974 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 return err;
1976}
1977EXPORT_SYMBOL(km_new_mapping);
1978
Eric W. Biederman15e47302012-09-07 20:12:54 +00001979void km_policy_expired(struct xfrm_policy *pol, int dir, int hard, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001981 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982
Herbert Xubf08867f92005-06-18 22:44:00 -07001983 c.data.hard = hard;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001984 c.portid = portid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001985 c.event = XFRM_MSG_POLEXPIRE;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001986 km_policy_notify(pol, dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987}
David S. Millera70fcb02006-03-20 19:18:52 -08001988EXPORT_SYMBOL(km_policy_expired);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989
Eric Dumazet2d60abc2008-01-03 20:43:21 -08001990#ifdef CONFIG_XFRM_MIGRATE
David S. Miller183cad12011-02-24 00:28:01 -05001991int km_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
1992 const struct xfrm_migrate *m, int num_migrate,
Antony Antony8bafd732017-06-06 12:12:14 +02001993 const struct xfrm_kmaddress *k,
1994 const struct xfrm_encap_tmpl *encap)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001995{
1996 int err = -EINVAL;
1997 int ret;
1998 struct xfrm_mgr *km;
1999
Cong Wang85168c02013-01-16 16:05:06 +08002000 rcu_read_lock();
2001 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002002 if (km->migrate) {
Antony Antony8bafd732017-06-06 12:12:14 +02002003 ret = km->migrate(sel, dir, type, m, num_migrate, k,
2004 encap);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002005 if (!ret)
2006 err = ret;
2007 }
2008 }
Cong Wang85168c02013-01-16 16:05:06 +08002009 rcu_read_unlock();
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002010 return err;
2011}
2012EXPORT_SYMBOL(km_migrate);
Eric Dumazet2d60abc2008-01-03 20:43:21 -08002013#endif
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002014
Alexey Dobriyandb983c12008-11-25 17:51:01 -08002015int km_report(struct net *net, u8 proto, struct xfrm_selector *sel, xfrm_address_t *addr)
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002016{
2017 int err = -EINVAL;
2018 int ret;
2019 struct xfrm_mgr *km;
2020
Cong Wang85168c02013-01-16 16:05:06 +08002021 rcu_read_lock();
2022 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002023 if (km->report) {
Alexey Dobriyandb983c12008-11-25 17:51:01 -08002024 ret = km->report(net, proto, sel, addr);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002025 if (!ret)
2026 err = ret;
2027 }
2028 }
Cong Wang85168c02013-01-16 16:05:06 +08002029 rcu_read_unlock();
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002030 return err;
2031}
2032EXPORT_SYMBOL(km_report);
2033
Horia Geanta0f245582014-02-12 16:20:06 +02002034bool km_is_alive(const struct km_event *c)
2035{
2036 struct xfrm_mgr *km;
2037 bool is_alive = false;
2038
2039 rcu_read_lock();
2040 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
2041 if (km->is_alive && km->is_alive(c)) {
2042 is_alive = true;
2043 break;
2044 }
2045 }
2046 rcu_read_unlock();
2047
2048 return is_alive;
2049}
2050EXPORT_SYMBOL(km_is_alive);
2051
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052int xfrm_user_policy(struct sock *sk, int optname, u8 __user *optval, int optlen)
2053{
2054 int err;
2055 u8 *data;
2056 struct xfrm_mgr *km;
2057 struct xfrm_policy *pol = NULL;
2058
2059 if (optlen <= 0 || optlen > PAGE_SIZE)
2060 return -EMSGSIZE;
2061
Geliang Tanga133d932017-05-06 23:42:21 +08002062 data = memdup_user(optval, optlen);
2063 if (IS_ERR(data))
2064 return PTR_ERR(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065
2066 err = -EINVAL;
Cong Wang85168c02013-01-16 16:05:06 +08002067 rcu_read_lock();
2068 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002069 pol = km->compile_policy(sk, optname, data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 optlen, &err);
2071 if (err >= 0)
2072 break;
2073 }
Cong Wang85168c02013-01-16 16:05:06 +08002074 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075
2076 if (err >= 0) {
2077 xfrm_sk_policy_insert(sk, err, pol);
2078 xfrm_pol_put(pol);
Jonathan Basseri2b06cdf2017-10-25 09:52:27 -07002079 __sk_dst_reset(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 err = 0;
2081 }
2082
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 kfree(data);
2084 return err;
2085}
2086EXPORT_SYMBOL(xfrm_user_policy);
2087
Cong Wang85168c02013-01-16 16:05:06 +08002088static DEFINE_SPINLOCK(xfrm_km_lock);
2089
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090int xfrm_register_km(struct xfrm_mgr *km)
2091{
Cong Wang85168c02013-01-16 16:05:06 +08002092 spin_lock_bh(&xfrm_km_lock);
2093 list_add_tail_rcu(&km->list, &xfrm_km_list);
2094 spin_unlock_bh(&xfrm_km_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 return 0;
2096}
2097EXPORT_SYMBOL(xfrm_register_km);
2098
2099int xfrm_unregister_km(struct xfrm_mgr *km)
2100{
Cong Wang85168c02013-01-16 16:05:06 +08002101 spin_lock_bh(&xfrm_km_lock);
2102 list_del_rcu(&km->list);
2103 spin_unlock_bh(&xfrm_km_lock);
2104 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 return 0;
2106}
2107EXPORT_SYMBOL(xfrm_unregister_km);
2108
2109int xfrm_state_register_afinfo(struct xfrm_state_afinfo *afinfo)
2110{
2111 int err = 0;
Florian Westphal423826a2017-01-09 14:20:46 +01002112
2113 if (WARN_ON(afinfo->family >= NPROTO))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 return -EAFNOSUPPORT;
Florian Westphal423826a2017-01-09 14:20:46 +01002115
Cong Wang44abdc32013-01-16 16:05:05 +08002116 spin_lock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 if (unlikely(xfrm_state_afinfo[afinfo->family] != NULL))
Li RongQingf31e8d4f2015-04-23 11:06:53 +08002118 err = -EEXIST;
David S. Milleredcd5822006-08-24 00:42:45 -07002119 else
Cong Wang44abdc32013-01-16 16:05:05 +08002120 rcu_assign_pointer(xfrm_state_afinfo[afinfo->family], afinfo);
2121 spin_unlock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 return err;
2123}
2124EXPORT_SYMBOL(xfrm_state_register_afinfo);
2125
2126int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo)
2127{
Florian Westphal423826a2017-01-09 14:20:46 +01002128 int err = 0, family = afinfo->family;
2129
2130 if (WARN_ON(family >= NPROTO))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 return -EAFNOSUPPORT;
Florian Westphal423826a2017-01-09 14:20:46 +01002132
Cong Wang44abdc32013-01-16 16:05:05 +08002133 spin_lock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 if (likely(xfrm_state_afinfo[afinfo->family] != NULL)) {
Florian Westphal423826a2017-01-09 14:20:46 +01002135 if (rcu_access_pointer(xfrm_state_afinfo[family]) != afinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 err = -EINVAL;
David S. Milleredcd5822006-08-24 00:42:45 -07002137 else
Cong Wang44abdc32013-01-16 16:05:05 +08002138 RCU_INIT_POINTER(xfrm_state_afinfo[afinfo->family], NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 }
Cong Wang44abdc32013-01-16 16:05:05 +08002140 spin_unlock_bh(&xfrm_state_afinfo_lock);
2141 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 return err;
2143}
2144EXPORT_SYMBOL(xfrm_state_unregister_afinfo);
2145
Florian Westphal711059b2017-01-09 14:20:48 +01002146struct xfrm_state_afinfo *xfrm_state_afinfo_get_rcu(unsigned int family)
2147{
2148 if (unlikely(family >= NPROTO))
2149 return NULL;
2150
2151 return rcu_dereference(xfrm_state_afinfo[family]);
2152}
2153
Hannes Frederic Sowa628e3412013-08-14 13:05:23 +02002154struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155{
2156 struct xfrm_state_afinfo *afinfo;
2157 if (unlikely(family >= NPROTO))
2158 return NULL;
Cong Wang44abdc32013-01-16 16:05:05 +08002159 rcu_read_lock();
2160 afinfo = rcu_dereference(xfrm_state_afinfo[family]);
Herbert Xu546be242006-05-27 23:03:58 -07002161 if (unlikely(!afinfo))
Cong Wang44abdc32013-01-16 16:05:05 +08002162 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 return afinfo;
2164}
2165
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166/* Temporarily located here until net/xfrm/xfrm_tunnel.c is created */
2167void xfrm_state_delete_tunnel(struct xfrm_state *x)
2168{
2169 if (x->tunnel) {
2170 struct xfrm_state *t = x->tunnel;
2171
2172 if (atomic_read(&t->tunnel_users) == 2)
2173 xfrm_state_delete(t);
2174 atomic_dec(&t->tunnel_users);
2175 xfrm_state_put(t);
2176 x->tunnel = NULL;
2177 }
2178}
2179EXPORT_SYMBOL(xfrm_state_delete_tunnel);
2180
2181int xfrm_state_mtu(struct xfrm_state *x, int mtu)
2182{
Florian Westphalb3b73b82017-01-05 13:23:58 +01002183 const struct xfrm_type *type = READ_ONCE(x->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184
Patrick McHardyc5c25232007-04-09 11:47:18 -07002185 if (x->km.state == XFRM_STATE_VALID &&
Florian Westphalb3b73b82017-01-05 13:23:58 +01002186 type && type->get_mtu)
2187 return type->get_mtu(x, mtu);
2188
2189 return mtu - x->props.header_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190}
2191
Ilan Tayariffdb5212017-08-01 12:49:08 +03002192int __xfrm_init_state(struct xfrm_state *x, bool init_replay, bool offload)
Herbert Xu72cb6962005-06-20 13:18:08 -07002193{
Herbert Xud094cd82005-06-20 13:19:41 -07002194 struct xfrm_state_afinfo *afinfo;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002195 struct xfrm_mode *inner_mode;
Herbert Xud094cd82005-06-20 13:19:41 -07002196 int family = x->props.family;
Herbert Xu72cb6962005-06-20 13:18:08 -07002197 int err;
2198
Herbert Xud094cd82005-06-20 13:19:41 -07002199 err = -EAFNOSUPPORT;
2200 afinfo = xfrm_state_get_afinfo(family);
2201 if (!afinfo)
2202 goto error;
2203
2204 err = 0;
2205 if (afinfo->init_flags)
2206 err = afinfo->init_flags(x);
2207
Florian Westphalaf5d27c2017-01-09 14:20:47 +01002208 rcu_read_unlock();
Herbert Xud094cd82005-06-20 13:19:41 -07002209
2210 if (err)
2211 goto error;
2212
2213 err = -EPROTONOSUPPORT;
Herbert Xu13996372007-10-17 21:35:51 -07002214
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002215 if (x->sel.family != AF_UNSPEC) {
2216 inner_mode = xfrm_get_mode(x->props.mode, x->sel.family);
2217 if (inner_mode == NULL)
2218 goto error;
2219
2220 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL) &&
2221 family != x->sel.family) {
2222 xfrm_put_mode(inner_mode);
2223 goto error;
2224 }
2225
2226 x->inner_mode = inner_mode;
2227 } else {
2228 struct xfrm_mode *inner_mode_iaf;
Martin Willid81d2282008-12-03 15:38:07 -08002229 int iafamily = AF_INET;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002230
Martin Willid81d2282008-12-03 15:38:07 -08002231 inner_mode = xfrm_get_mode(x->props.mode, x->props.family);
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002232 if (inner_mode == NULL)
2233 goto error;
2234
2235 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL)) {
2236 xfrm_put_mode(inner_mode);
2237 goto error;
2238 }
Martin Willid81d2282008-12-03 15:38:07 -08002239 x->inner_mode = inner_mode;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002240
Martin Willid81d2282008-12-03 15:38:07 -08002241 if (x->props.family == AF_INET)
2242 iafamily = AF_INET6;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002243
Martin Willid81d2282008-12-03 15:38:07 -08002244 inner_mode_iaf = xfrm_get_mode(x->props.mode, iafamily);
2245 if (inner_mode_iaf) {
2246 if (inner_mode_iaf->flags & XFRM_MODE_FLAG_TUNNEL)
2247 x->inner_mode_iaf = inner_mode_iaf;
2248 else
2249 xfrm_put_mode(inner_mode_iaf);
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002250 }
2251 }
Herbert Xu13996372007-10-17 21:35:51 -07002252
Herbert Xud094cd82005-06-20 13:19:41 -07002253 x->type = xfrm_get_type(x->id.proto, family);
Herbert Xu72cb6962005-06-20 13:18:08 -07002254 if (x->type == NULL)
2255 goto error;
2256
Ilan Tayariffdb5212017-08-01 12:49:08 +03002257 x->type_offload = xfrm_get_type_offload(x->id.proto, family, offload);
Steffen Klassert9d389d72017-04-14 10:05:44 +02002258
Herbert Xu72cb6962005-06-20 13:18:08 -07002259 err = x->type->init_state(x);
2260 if (err)
2261 goto error;
2262
Herbert Xu13996372007-10-17 21:35:51 -07002263 x->outer_mode = xfrm_get_mode(x->props.mode, family);
Julia Lawall599901c2012-08-29 06:49:15 +00002264 if (x->outer_mode == NULL) {
2265 err = -EPROTONOSUPPORT;
Herbert Xub59f45d2006-05-27 23:05:54 -07002266 goto error;
Julia Lawall599901c2012-08-29 06:49:15 +00002267 }
Herbert Xub59f45d2006-05-27 23:05:54 -07002268
Wei Yongjuna454f0c2011-03-21 18:08:28 -07002269 if (init_replay) {
2270 err = xfrm_init_replay(x);
2271 if (err)
2272 goto error;
2273 }
2274
Herbert Xu72cb6962005-06-20 13:18:08 -07002275error:
2276 return err;
2277}
2278
Wei Yongjuna454f0c2011-03-21 18:08:28 -07002279EXPORT_SYMBOL(__xfrm_init_state);
2280
2281int xfrm_init_state(struct xfrm_state *x)
2282{
Yossi Kupermancc015722018-01-17 15:52:41 +02002283 int err;
2284
2285 err = __xfrm_init_state(x, true, false);
2286 if (!err)
2287 x->km.state = XFRM_STATE_VALID;
2288
2289 return err;
Wei Yongjuna454f0c2011-03-21 18:08:28 -07002290}
2291
Herbert Xu72cb6962005-06-20 13:18:08 -07002292EXPORT_SYMBOL(xfrm_init_state);
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09002293
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002294int __net_init xfrm_state_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295{
David S. Millerf034b5d2006-08-24 03:08:07 -07002296 unsigned int sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297
Alexey Dobriyan9d4139c2008-11-25 17:16:11 -08002298 INIT_LIST_HEAD(&net->xfrm.state_all);
2299
David S. Millerf034b5d2006-08-24 03:08:07 -07002300 sz = sizeof(struct hlist_head) * 8;
2301
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002302 net->xfrm.state_bydst = xfrm_hash_alloc(sz);
2303 if (!net->xfrm.state_bydst)
2304 goto out_bydst;
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002305 net->xfrm.state_bysrc = xfrm_hash_alloc(sz);
2306 if (!net->xfrm.state_bysrc)
2307 goto out_bysrc;
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002308 net->xfrm.state_byspi = xfrm_hash_alloc(sz);
2309 if (!net->xfrm.state_byspi)
2310 goto out_byspi;
Alexey Dobriyan529983e2008-11-25 17:18:12 -08002311 net->xfrm.state_hmask = ((sz / sizeof(struct hlist_head)) - 1);
David S. Millerf034b5d2006-08-24 03:08:07 -07002312
Alexey Dobriyan0bf7c5b2008-11-25 17:18:39 -08002313 net->xfrm.state_num = 0;
Alexey Dobriyan63082732008-11-25 17:19:07 -08002314 INIT_WORK(&net->xfrm.state_hash_work, xfrm_hash_resize);
Fan Du283bc9f2013-11-07 17:47:50 +08002315 spin_lock_init(&net->xfrm.xfrm_state_lock);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002316 return 0;
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002317
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002318out_byspi:
2319 xfrm_hash_free(net->xfrm.state_bysrc, sz);
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002320out_bysrc:
2321 xfrm_hash_free(net->xfrm.state_bydst, sz);
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002322out_bydst:
2323 return -ENOMEM;
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002324}
2325
2326void xfrm_state_fini(struct net *net)
2327{
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002328 unsigned int sz;
2329
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002330 flush_work(&net->xfrm.state_hash_work);
Tetsuo Handa2e710292014-04-22 21:48:30 +09002331 xfrm_state_flush(net, IPSEC_PROTO_ANY, false);
Florian Westphal35db57bb2016-08-23 16:00:12 +02002332 flush_work(&xfrm_state_gc_work);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002333
Alexey Dobriyan9d4139c2008-11-25 17:16:11 -08002334 WARN_ON(!list_empty(&net->xfrm.state_all));
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002335
Alexey Dobriyan529983e2008-11-25 17:18:12 -08002336 sz = (net->xfrm.state_hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002337 WARN_ON(!hlist_empty(net->xfrm.state_byspi));
2338 xfrm_hash_free(net->xfrm.state_byspi, sz);
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002339 WARN_ON(!hlist_empty(net->xfrm.state_bysrc));
2340 xfrm_hash_free(net->xfrm.state_bysrc, sz);
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002341 WARN_ON(!hlist_empty(net->xfrm.state_bydst));
2342 xfrm_hash_free(net->xfrm.state_bydst, sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343}
2344
Joy Lattenab5f5e82007-09-17 11:51:22 -07002345#ifdef CONFIG_AUDITSYSCALL
Ilpo Järvinencf35f432008-01-05 23:13:20 -08002346static void xfrm_audit_helper_sainfo(struct xfrm_state *x,
2347 struct audit_buffer *audit_buf)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002348{
Paul Moore68277ac2007-12-20 20:49:33 -08002349 struct xfrm_sec_ctx *ctx = x->security;
2350 u32 spi = ntohl(x->id.spi);
2351
2352 if (ctx)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002353 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
Paul Moore68277ac2007-12-20 20:49:33 -08002354 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002355
Weilong Chen9b7a7872013-12-24 09:43:46 +08002356 switch (x->props.family) {
Joy Lattenab5f5e82007-09-17 11:51:22 -07002357 case AF_INET:
Harvey Harrison21454aa2008-10-31 00:54:56 -07002358 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2359 &x->props.saddr.a4, &x->id.daddr.a4);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002360 break;
2361 case AF_INET6:
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002362 audit_log_format(audit_buf, " src=%pI6 dst=%pI6",
Harvey Harrisonfdb46ee2008-10-28 16:10:17 -07002363 x->props.saddr.a6, x->id.daddr.a6);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002364 break;
2365 }
Paul Moore68277ac2007-12-20 20:49:33 -08002366
2367 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002368}
2369
Ilpo Järvinencf35f432008-01-05 23:13:20 -08002370static void xfrm_audit_helper_pktinfo(struct sk_buff *skb, u16 family,
2371 struct audit_buffer *audit_buf)
Paul Mooreafeb14b2007-12-21 14:58:11 -08002372{
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002373 const struct iphdr *iph4;
2374 const struct ipv6hdr *iph6;
Paul Mooreafeb14b2007-12-21 14:58:11 -08002375
2376 switch (family) {
2377 case AF_INET:
2378 iph4 = ip_hdr(skb);
Harvey Harrison21454aa2008-10-31 00:54:56 -07002379 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2380 &iph4->saddr, &iph4->daddr);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002381 break;
2382 case AF_INET6:
2383 iph6 = ipv6_hdr(skb);
2384 audit_log_format(audit_buf,
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002385 " src=%pI6 dst=%pI6 flowlbl=0x%x%02x%02x",
Weilong Chen9b7a7872013-12-24 09:43:46 +08002386 &iph6->saddr, &iph6->daddr,
Paul Mooreafeb14b2007-12-21 14:58:11 -08002387 iph6->flow_lbl[0] & 0x0f,
2388 iph6->flow_lbl[1],
2389 iph6->flow_lbl[2]);
2390 break;
2391 }
2392}
2393
Tetsuo Handa2e710292014-04-22 21:48:30 +09002394void xfrm_audit_state_add(struct xfrm_state *x, int result, bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002395{
2396 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002397
Paul Mooreafeb14b2007-12-21 14:58:11 -08002398 audit_buf = xfrm_audit_start("SAD-add");
Joy Lattenab5f5e82007-09-17 11:51:22 -07002399 if (audit_buf == NULL)
2400 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09002401 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002402 xfrm_audit_helper_sainfo(x, audit_buf);
2403 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002404 audit_log_end(audit_buf);
2405}
2406EXPORT_SYMBOL_GPL(xfrm_audit_state_add);
2407
Tetsuo Handa2e710292014-04-22 21:48:30 +09002408void xfrm_audit_state_delete(struct xfrm_state *x, int result, bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002409{
2410 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002411
Paul Mooreafeb14b2007-12-21 14:58:11 -08002412 audit_buf = xfrm_audit_start("SAD-delete");
Joy Lattenab5f5e82007-09-17 11:51:22 -07002413 if (audit_buf == NULL)
2414 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09002415 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002416 xfrm_audit_helper_sainfo(x, audit_buf);
2417 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002418 audit_log_end(audit_buf);
2419}
2420EXPORT_SYMBOL_GPL(xfrm_audit_state_delete);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002421
2422void xfrm_audit_state_replay_overflow(struct xfrm_state *x,
2423 struct sk_buff *skb)
2424{
2425 struct audit_buffer *audit_buf;
2426 u32 spi;
2427
2428 audit_buf = xfrm_audit_start("SA-replay-overflow");
2429 if (audit_buf == NULL)
2430 return;
2431 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2432 /* don't record the sequence number because it's inherent in this kind
2433 * of audit message */
2434 spi = ntohl(x->id.spi);
2435 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
2436 audit_log_end(audit_buf);
2437}
2438EXPORT_SYMBOL_GPL(xfrm_audit_state_replay_overflow);
2439
Steffen Klassert9fdc4882011-03-08 00:08:32 +00002440void xfrm_audit_state_replay(struct xfrm_state *x,
Paul Mooreafeb14b2007-12-21 14:58:11 -08002441 struct sk_buff *skb, __be32 net_seq)
2442{
2443 struct audit_buffer *audit_buf;
2444 u32 spi;
2445
2446 audit_buf = xfrm_audit_start("SA-replayed-pkt");
2447 if (audit_buf == NULL)
2448 return;
2449 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2450 spi = ntohl(x->id.spi);
2451 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2452 spi, spi, ntohl(net_seq));
2453 audit_log_end(audit_buf);
2454}
Steffen Klassert9fdc4882011-03-08 00:08:32 +00002455EXPORT_SYMBOL_GPL(xfrm_audit_state_replay);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002456
2457void xfrm_audit_state_notfound_simple(struct sk_buff *skb, u16 family)
2458{
2459 struct audit_buffer *audit_buf;
2460
2461 audit_buf = xfrm_audit_start("SA-notfound");
2462 if (audit_buf == NULL)
2463 return;
2464 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2465 audit_log_end(audit_buf);
2466}
2467EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound_simple);
2468
2469void xfrm_audit_state_notfound(struct sk_buff *skb, u16 family,
2470 __be32 net_spi, __be32 net_seq)
2471{
2472 struct audit_buffer *audit_buf;
2473 u32 spi;
2474
2475 audit_buf = xfrm_audit_start("SA-notfound");
2476 if (audit_buf == NULL)
2477 return;
2478 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2479 spi = ntohl(net_spi);
2480 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2481 spi, spi, ntohl(net_seq));
2482 audit_log_end(audit_buf);
2483}
2484EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound);
2485
2486void xfrm_audit_state_icvfail(struct xfrm_state *x,
2487 struct sk_buff *skb, u8 proto)
2488{
2489 struct audit_buffer *audit_buf;
2490 __be32 net_spi;
2491 __be32 net_seq;
2492
2493 audit_buf = xfrm_audit_start("SA-icv-failure");
2494 if (audit_buf == NULL)
2495 return;
2496 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2497 if (xfrm_parse_spi(skb, proto, &net_spi, &net_seq) == 0) {
2498 u32 spi = ntohl(net_spi);
2499 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2500 spi, spi, ntohl(net_seq));
2501 }
2502 audit_log_end(audit_buf);
2503}
2504EXPORT_SYMBOL_GPL(xfrm_audit_state_icvfail);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002505#endif /* CONFIG_AUDITSYSCALL */