blob: 1e80f68e226665e5393dd07eeeaaf9fa7090ca9b [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
Ilan Tayariffdb5212017-08-01 12:49:08 +0300316 if (!type && try_load) {
317 request_module("xfrm-offload-%d-%d", family, proto);
318 try_load = 0;
319 goto retry;
320 }
321
Steffen Klassert9d389d72017-04-14 10:05:44 +0200322 rcu_read_unlock();
323 return type;
324}
325
326static void xfrm_put_type_offload(const struct xfrm_type_offload *type)
327{
328 module_put(type->owner);
329}
330
Cong Wang7a9885b2013-01-17 16:34:11 +0800331static DEFINE_SPINLOCK(xfrm_mode_lock);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700332int xfrm_register_mode(struct xfrm_mode *mode, int family)
333{
334 struct xfrm_state_afinfo *afinfo;
335 struct xfrm_mode **modemap;
336 int err;
337
338 if (unlikely(mode->encap >= XFRM_MODE_MAX))
339 return -EINVAL;
340
Cong Wang7a9885b2013-01-17 16:34:11 +0800341 afinfo = xfrm_state_get_afinfo(family);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700342 if (unlikely(afinfo == NULL))
343 return -EAFNOSUPPORT;
344
345 err = -EEXIST;
346 modemap = afinfo->mode_map;
Cong Wang7a9885b2013-01-17 16:34:11 +0800347 spin_lock_bh(&xfrm_mode_lock);
Herbert Xu17c2a422007-10-17 21:33:12 -0700348 if (modemap[mode->encap])
349 goto out;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700350
Herbert Xu17c2a422007-10-17 21:33:12 -0700351 err = -ENOENT;
352 if (!try_module_get(afinfo->owner))
353 goto out;
354
355 mode->afinfo = afinfo;
356 modemap[mode->encap] = mode;
357 err = 0;
358
359out:
Cong Wang7a9885b2013-01-17 16:34:11 +0800360 spin_unlock_bh(&xfrm_mode_lock);
Florian Westphalaf5d27c2017-01-09 14:20:47 +0100361 rcu_read_unlock();
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700362 return err;
363}
364EXPORT_SYMBOL(xfrm_register_mode);
365
366int xfrm_unregister_mode(struct xfrm_mode *mode, int family)
367{
368 struct xfrm_state_afinfo *afinfo;
369 struct xfrm_mode **modemap;
370 int err;
371
372 if (unlikely(mode->encap >= XFRM_MODE_MAX))
373 return -EINVAL;
374
Cong Wang7a9885b2013-01-17 16:34:11 +0800375 afinfo = xfrm_state_get_afinfo(family);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700376 if (unlikely(afinfo == NULL))
377 return -EAFNOSUPPORT;
378
379 err = -ENOENT;
380 modemap = afinfo->mode_map;
Cong Wang7a9885b2013-01-17 16:34:11 +0800381 spin_lock_bh(&xfrm_mode_lock);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700382 if (likely(modemap[mode->encap] == mode)) {
383 modemap[mode->encap] = NULL;
Herbert Xu17c2a422007-10-17 21:33:12 -0700384 module_put(mode->afinfo->owner);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700385 err = 0;
386 }
387
Cong Wang7a9885b2013-01-17 16:34:11 +0800388 spin_unlock_bh(&xfrm_mode_lock);
Florian Westphalaf5d27c2017-01-09 14:20:47 +0100389 rcu_read_unlock();
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700390 return err;
391}
392EXPORT_SYMBOL(xfrm_unregister_mode);
393
394static struct xfrm_mode *xfrm_get_mode(unsigned int encap, int family)
395{
396 struct xfrm_state_afinfo *afinfo;
397 struct xfrm_mode *mode;
398 int modload_attempted = 0;
399
400 if (unlikely(encap >= XFRM_MODE_MAX))
401 return NULL;
402
403retry:
404 afinfo = xfrm_state_get_afinfo(family);
405 if (unlikely(afinfo == NULL))
406 return NULL;
407
Florian Westphal75cda622017-01-09 14:20:49 +0100408 mode = READ_ONCE(afinfo->mode_map[encap]);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700409 if (unlikely(mode && !try_module_get(mode->owner)))
410 mode = NULL;
Florian Westphal75cda622017-01-09 14:20:49 +0100411
412 rcu_read_unlock();
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700413 if (!mode && !modload_attempted) {
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700414 request_module("xfrm-mode-%d-%d", family, encap);
415 modload_attempted = 1;
416 goto retry;
417 }
418
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700419 return mode;
420}
421
422static void xfrm_put_mode(struct xfrm_mode *mode)
423{
424 module_put(mode->owner);
425}
426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427static void xfrm_state_gc_destroy(struct xfrm_state *x)
428{
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800429 tasklet_hrtimer_cancel(&x->mtimer);
David S. Millera47f0ce2006-08-24 03:54:22 -0700430 del_timer_sync(&x->rtimer);
Ilan Tayarib5884792016-09-18 07:42:53 +0000431 kfree(x->aead);
Jesper Juhla51482b2005-11-08 09:41:34 -0800432 kfree(x->aalg);
433 kfree(x->ealg);
434 kfree(x->calg);
435 kfree(x->encap);
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700436 kfree(x->coaddr);
Steffen Klassertd8647b72011-03-08 00:10:27 +0000437 kfree(x->replay_esn);
438 kfree(x->preplay_esn);
Herbert Xu13996372007-10-17 21:35:51 -0700439 if (x->inner_mode)
440 xfrm_put_mode(x->inner_mode);
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700441 if (x->inner_mode_iaf)
442 xfrm_put_mode(x->inner_mode_iaf);
Herbert Xu13996372007-10-17 21:35:51 -0700443 if (x->outer_mode)
444 xfrm_put_mode(x->outer_mode);
Steffen Klassert9d389d72017-04-14 10:05:44 +0200445 if (x->type_offload)
446 xfrm_put_type_offload(x->type_offload);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 if (x->type) {
448 x->type->destructor(x);
449 xfrm_put_type(x->type);
450 }
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200451 xfrm_dev_state_free(x);
Trent Jaegerdf718372005-12-13 23:12:27 -0800452 security_xfrm_state_free(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 kfree(x);
454}
455
Alexey Dobriyanc7837142008-11-25 17:20:36 -0800456static void xfrm_state_gc_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
Herbert Xu12a169e2008-10-01 07:03:24 -0700458 struct xfrm_state *x;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800459 struct hlist_node *tmp;
Herbert Xu12a169e2008-10-01 07:03:24 -0700460 struct hlist_head gc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 spin_lock_bh(&xfrm_state_gc_lock);
Florian Westphal35db57bb2016-08-23 16:00:12 +0200463 hlist_move_list(&xfrm_state_gc_list, &gc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 spin_unlock_bh(&xfrm_state_gc_lock);
465
Florian Westphaldf7274e2016-08-09 12:16:06 +0200466 synchronize_rcu();
467
Sasha Levinb67bfe02013-02-27 17:06:00 -0800468 hlist_for_each_entry_safe(x, tmp, &gc_list, gclist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 xfrm_state_gc_destroy(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470}
471
Weilong Chen3e94c2d2013-12-24 09:43:47 +0800472static enum hrtimer_restart xfrm_timer_handler(struct hrtimer *me)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473{
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800474 struct tasklet_hrtimer *thr = container_of(me, struct tasklet_hrtimer, timer);
475 struct xfrm_state *x = container_of(thr, struct xfrm_state, mtimer);
James Morris9d729f72007-03-04 16:12:44 -0800476 unsigned long now = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 long next = LONG_MAX;
478 int warn = 0;
Joy Latten161a09e2006-11-27 13:11:54 -0600479 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
481 spin_lock(&x->lock);
482 if (x->km.state == XFRM_STATE_DEAD)
483 goto out;
484 if (x->km.state == XFRM_STATE_EXPIRED)
485 goto expired;
486 if (x->lft.hard_add_expires_seconds) {
487 long tmo = x->lft.hard_add_expires_seconds +
488 x->curlft.add_time - now;
Fan Due3c0d042012-07-30 21:43:54 +0000489 if (tmo <= 0) {
490 if (x->xflags & XFRM_SOFT_EXPIRE) {
491 /* enter hard expire without soft expire first?!
492 * setting a new date could trigger this.
Alexander Alemayhu1365e547c2017-01-03 17:13:20 +0100493 * workaround: fix x->curflt.add_time by below:
Fan Due3c0d042012-07-30 21:43:54 +0000494 */
495 x->curlft.add_time = now - x->saved_tmo - 1;
496 tmo = x->lft.hard_add_expires_seconds - x->saved_tmo;
497 } else
498 goto expired;
499 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 if (tmo < next)
501 next = tmo;
502 }
503 if (x->lft.hard_use_expires_seconds) {
504 long tmo = x->lft.hard_use_expires_seconds +
505 (x->curlft.use_time ? : now) - now;
506 if (tmo <= 0)
507 goto expired;
508 if (tmo < next)
509 next = tmo;
510 }
511 if (x->km.dying)
512 goto resched;
513 if (x->lft.soft_add_expires_seconds) {
514 long tmo = x->lft.soft_add_expires_seconds +
515 x->curlft.add_time - now;
Fan Due3c0d042012-07-30 21:43:54 +0000516 if (tmo <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 warn = 1;
Fan Due3c0d042012-07-30 21:43:54 +0000518 x->xflags &= ~XFRM_SOFT_EXPIRE;
519 } else if (tmo < next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 next = tmo;
Fan Due3c0d042012-07-30 21:43:54 +0000521 x->xflags |= XFRM_SOFT_EXPIRE;
522 x->saved_tmo = tmo;
523 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 }
525 if (x->lft.soft_use_expires_seconds) {
526 long tmo = x->lft.soft_use_expires_seconds +
527 (x->curlft.use_time ? : now) - now;
528 if (tmo <= 0)
529 warn = 1;
530 else if (tmo < next)
531 next = tmo;
532 }
533
Herbert Xu4666faa2005-06-18 22:43:22 -0700534 x->km.dying = warn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 if (warn)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -0800536 km_state_expired(x, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537resched:
Weilong Chen9b7a7872013-12-24 09:43:46 +0800538 if (next != LONG_MAX) {
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800539 tasklet_hrtimer_start(&x->mtimer, ktime_set(next, 0), HRTIMER_MODE_REL);
540 }
David S. Millera47f0ce2006-08-24 03:54:22 -0700541
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 goto out;
543
544expired:
Steffen Klassert5b8ef342013-08-27 13:43:30 +0200545 if (x->km.state == XFRM_STATE_ACQ && x->id.spi == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 x->km.state = XFRM_STATE_EXPIRED;
Joy Latten161a09e2006-11-27 13:11:54 -0600547
548 err = __xfrm_state_delete(x);
Nicolas Dichtel0806ae42013-08-23 15:46:08 +0200549 if (!err)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -0800550 km_state_expired(x, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
Tetsuo Handa2e710292014-04-22 21:48:30 +0900552 xfrm_audit_state_delete(x, err ? 0 : 1, true);
Joy Latten161a09e2006-11-27 13:11:54 -0600553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554out:
555 spin_unlock(&x->lock);
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800556 return HRTIMER_NORESTART;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557}
558
Kees Cooke99e88a2017-10-16 14:43:17 -0700559static void xfrm_replay_timer_handler(struct timer_list *t);
David S. Miller0ac84752006-03-20 19:18:23 -0800560
Alexey Dobriyan673c09b2008-11-25 17:15:16 -0800561struct xfrm_state *xfrm_state_alloc(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
563 struct xfrm_state *x;
564
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700565 x = kzalloc(sizeof(struct xfrm_state), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
567 if (x) {
Alexey Dobriyan673c09b2008-11-25 17:15:16 -0800568 write_pnet(&x->xs_net, net);
Reshetova, Elena88755e9c2017-07-04 15:53:21 +0300569 refcount_set(&x->refcnt, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 atomic_set(&x->tunnel_users, 0);
Herbert Xu12a169e2008-10-01 07:03:24 -0700571 INIT_LIST_HEAD(&x->km.all);
David S. Miller8f126e32006-08-24 02:45:07 -0700572 INIT_HLIST_NODE(&x->bydst);
573 INIT_HLIST_NODE(&x->bysrc);
574 INIT_HLIST_NODE(&x->byspi);
Fan Du99565a6c2013-08-15 15:49:06 +0800575 tasklet_hrtimer_init(&x->mtimer, xfrm_timer_handler,
576 CLOCK_BOOTTIME, HRTIMER_MODE_ABS);
Kees Cooke99e88a2017-10-16 14:43:17 -0700577 timer_setup(&x->rtimer, xfrm_replay_timer_handler, 0);
James Morris9d729f72007-03-04 16:12:44 -0800578 x->curlft.add_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 x->lft.soft_byte_limit = XFRM_INF;
580 x->lft.soft_packet_limit = XFRM_INF;
581 x->lft.hard_byte_limit = XFRM_INF;
582 x->lft.hard_packet_limit = XFRM_INF;
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -0800583 x->replay_maxage = 0;
584 x->replay_maxdiff = 0;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700585 x->inner_mode = NULL;
586 x->inner_mode_iaf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 spin_lock_init(&x->lock);
588 }
589 return x;
590}
591EXPORT_SYMBOL(xfrm_state_alloc);
592
593void __xfrm_state_destroy(struct xfrm_state *x)
594{
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700595 WARN_ON(x->km.state != XFRM_STATE_DEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
597 spin_lock_bh(&xfrm_state_gc_lock);
Florian Westphal35db57bb2016-08-23 16:00:12 +0200598 hlist_add_head(&x->gclist, &xfrm_state_gc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 spin_unlock_bh(&xfrm_state_gc_lock);
Florian Westphal35db57bb2016-08-23 16:00:12 +0200600 schedule_work(&xfrm_state_gc_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601}
602EXPORT_SYMBOL(__xfrm_state_destroy);
603
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -0800604int __xfrm_state_delete(struct xfrm_state *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800606 struct net *net = xs_net(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700607 int err = -ESRCH;
608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 if (x->km.state != XFRM_STATE_DEAD) {
610 x->km.state = XFRM_STATE_DEAD;
Fan Du283bc9f2013-11-07 17:47:50 +0800611 spin_lock(&net->xfrm.xfrm_state_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -0700612 list_del(&x->km.all);
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200613 hlist_del_rcu(&x->bydst);
614 hlist_del_rcu(&x->bysrc);
David S. Millera47f0ce2006-08-24 03:54:22 -0700615 if (x->id.spi)
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200616 hlist_del_rcu(&x->byspi);
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800617 net->xfrm.state_num--;
Fan Du283bc9f2013-11-07 17:47:50 +0800618 spin_unlock(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200620 xfrm_dev_state_delete(x);
621
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 /* All xfrm_state objects are created by xfrm_state_alloc.
623 * The xfrm_state_alloc call gives a reference, and that
624 * is what we are dropping here.
625 */
Patrick McHardy5dba4792007-11-27 11:10:07 +0800626 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700627 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700629
630 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631}
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -0800632EXPORT_SYMBOL(__xfrm_state_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700634int xfrm_state_delete(struct xfrm_state *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700636 int err;
637
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 spin_lock_bh(&x->lock);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700639 err = __xfrm_state_delete(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 spin_unlock_bh(&x->lock);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700641
642 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643}
644EXPORT_SYMBOL(xfrm_state_delete);
645
Joy Latten4aa2e622007-06-04 19:05:57 -0400646#ifdef CONFIG_SECURITY_NETWORK_XFRM
647static inline int
Tetsuo Handa2e710292014-04-22 21:48:30 +0900648xfrm_state_flush_secctx_check(struct net *net, u8 proto, bool task_valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649{
Joy Latten4aa2e622007-06-04 19:05:57 -0400650 int i, err = 0;
651
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800652 for (i = 0; i <= net->xfrm.state_hmask; i++) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400653 struct xfrm_state *x;
654
Sasha Levinb67bfe02013-02-27 17:06:00 -0800655 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400656 if (xfrm_id_proto_match(x->id.proto, proto) &&
657 (err = security_xfrm_state_delete(x)) != 0) {
Tetsuo Handa2e710292014-04-22 21:48:30 +0900658 xfrm_audit_state_delete(x, 0, task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400659 return err;
660 }
661 }
662 }
663
664 return err;
665}
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200666
667static inline int
668xfrm_dev_state_flush_secctx_check(struct net *net, struct net_device *dev, bool task_valid)
669{
670 int i, err = 0;
671
672 for (i = 0; i <= net->xfrm.state_hmask; i++) {
673 struct xfrm_state *x;
674 struct xfrm_state_offload *xso;
675
676 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
677 xso = &x->xso;
678
679 if (xso->dev == dev &&
680 (err = security_xfrm_state_delete(x)) != 0) {
681 xfrm_audit_state_delete(x, 0, task_valid);
682 return err;
683 }
684 }
685 }
686
687 return err;
688}
Joy Latten4aa2e622007-06-04 19:05:57 -0400689#else
690static inline int
Tetsuo Handa2e710292014-04-22 21:48:30 +0900691xfrm_state_flush_secctx_check(struct net *net, u8 proto, bool task_valid)
Joy Latten4aa2e622007-06-04 19:05:57 -0400692{
693 return 0;
694}
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200695
696static inline int
697xfrm_dev_state_flush_secctx_check(struct net *net, struct net_device *dev, bool task_valid)
698{
699 return 0;
700}
Joy Latten4aa2e622007-06-04 19:05:57 -0400701#endif
702
Tetsuo Handa2e710292014-04-22 21:48:30 +0900703int xfrm_state_flush(struct net *net, u8 proto, bool task_valid)
Joy Latten4aa2e622007-06-04 19:05:57 -0400704{
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000705 int i, err = 0, cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Fan Du283bc9f2013-11-07 17:47:50 +0800707 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Tetsuo Handa2e710292014-04-22 21:48:30 +0900708 err = xfrm_state_flush_secctx_check(net, proto, task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400709 if (err)
710 goto out;
711
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000712 err = -ESRCH;
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800713 for (i = 0; i <= net->xfrm.state_hmask; i++) {
David S. Miller8f126e32006-08-24 02:45:07 -0700714 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715restart:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800716 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 if (!xfrm_state_kern(x) &&
Masahide NAKAMURA57947082006-09-22 15:06:24 -0700718 xfrm_id_proto_match(x->id.proto, proto)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 xfrm_state_hold(x);
Fan Du283bc9f2013-11-07 17:47:50 +0800720 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
Joy Latten161a09e2006-11-27 13:11:54 -0600722 err = xfrm_state_delete(x);
Joy Lattenab5f5e82007-09-17 11:51:22 -0700723 xfrm_audit_state_delete(x, err ? 0 : 1,
Tetsuo Handa2e710292014-04-22 21:48:30 +0900724 task_valid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 xfrm_state_put(x);
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000726 if (!err)
727 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
Fan Du283bc9f2013-11-07 17:47:50 +0800729 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 goto restart;
731 }
732 }
733 }
Artem Savkovdd269db2017-09-27 14:25:37 +0200734out:
735 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Florian Westphalec30d782017-07-17 13:57:27 +0200736 if (cnt) {
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000737 err = 0;
Florian Westphalec30d782017-07-17 13:57:27 +0200738 xfrm_policy_cache_flush();
739 }
Joy Latten4aa2e622007-06-04 19:05:57 -0400740 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741}
742EXPORT_SYMBOL(xfrm_state_flush);
743
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200744int xfrm_dev_state_flush(struct net *net, struct net_device *dev, bool task_valid)
745{
746 int i, err = 0, cnt = 0;
747
748 spin_lock_bh(&net->xfrm.xfrm_state_lock);
749 err = xfrm_dev_state_flush_secctx_check(net, dev, task_valid);
750 if (err)
751 goto out;
752
753 err = -ESRCH;
754 for (i = 0; i <= net->xfrm.state_hmask; i++) {
755 struct xfrm_state *x;
756 struct xfrm_state_offload *xso;
757restart:
758 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
759 xso = &x->xso;
760
761 if (!xfrm_state_kern(x) && xso->dev == dev) {
762 xfrm_state_hold(x);
763 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
764
765 err = xfrm_state_delete(x);
766 xfrm_audit_state_delete(x, err ? 0 : 1,
767 task_valid);
768 xfrm_state_put(x);
769 if (!err)
770 cnt++;
771
772 spin_lock_bh(&net->xfrm.xfrm_state_lock);
773 goto restart;
774 }
775 }
776 }
777 if (cnt)
778 err = 0;
779
780out:
781 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
782 return err;
783}
784EXPORT_SYMBOL(xfrm_dev_state_flush);
785
Alexey Dobriyane0710412010-01-23 13:37:10 +0000786void xfrm_sad_getinfo(struct net *net, struct xfrmk_sadinfo *si)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700787{
Fan Du283bc9f2013-11-07 17:47:50 +0800788 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Alexey Dobriyane0710412010-01-23 13:37:10 +0000789 si->sadcnt = net->xfrm.state_num;
790 si->sadhcnt = net->xfrm.state_hmask;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700791 si->sadhmcnt = xfrm_state_hashmax;
Fan Du283bc9f2013-11-07 17:47:50 +0800792 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700793}
794EXPORT_SYMBOL(xfrm_sad_getinfo);
795
Florian Westphal711059b2017-01-09 14:20:48 +0100796static void
David S. Miller1a898592011-02-22 18:22:34 -0800797xfrm_init_tempstate(struct xfrm_state *x, const struct flowi *fl,
David S. Miller04686012011-02-24 01:50:12 -0500798 const struct xfrm_tmpl *tmpl,
David S. Miller33765d02011-02-24 01:55:45 -0500799 const xfrm_address_t *daddr, const xfrm_address_t *saddr,
Thomas Egerer8444cf72010-09-20 11:11:38 -0700800 unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
Florian Westphal711059b2017-01-09 14:20:48 +0100802 struct xfrm_state_afinfo *afinfo = xfrm_state_afinfo_get_rcu(family);
803
Florian Westphal3819a352017-01-13 14:55:14 +0100804 if (!afinfo)
805 return;
806
807 afinfo->init_tempsel(&x->sel, fl);
Thomas Egerer8444cf72010-09-20 11:11:38 -0700808
809 if (family != tmpl->encap_family) {
Florian Westphal711059b2017-01-09 14:20:48 +0100810 afinfo = xfrm_state_afinfo_get_rcu(tmpl->encap_family);
Thomas Egerer8444cf72010-09-20 11:11:38 -0700811 if (!afinfo)
Florian Westphal711059b2017-01-09 14:20:48 +0100812 return;
Thomas Egerer8444cf72010-09-20 11:11:38 -0700813 }
814 afinfo->init_temprop(x, tmpl, daddr, saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815}
816
David S. Miller9aa60082011-02-24 01:51:36 -0500817static struct xfrm_state *__xfrm_state_lookup(struct net *net, u32 mark,
818 const xfrm_address_t *daddr,
819 __be32 spi, u8 proto,
820 unsigned short family)
David S. Milleredcd5822006-08-24 00:42:45 -0700821{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800822 unsigned int h = xfrm_spi_hash(net, daddr, spi, proto, family);
David S. Milleredcd5822006-08-24 00:42:45 -0700823 struct xfrm_state *x;
824
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200825 hlist_for_each_entry_rcu(x, net->xfrm.state_byspi + h, byspi) {
David S. Milleredcd5822006-08-24 00:42:45 -0700826 if (x->props.family != family ||
827 x->id.spi != spi ||
Wei Yongjun18025712009-06-28 18:42:53 +0000828 x->id.proto != proto ||
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +0000829 !xfrm_addr_equal(&x->id.daddr, daddr, family))
David S. Milleredcd5822006-08-24 00:42:45 -0700830 continue;
831
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000832 if ((mark & x->mark.m) != x->mark.v)
833 continue;
Florian Westphal02efdff2016-08-09 12:16:05 +0200834 if (!xfrm_state_hold_rcu(x))
835 continue;
David S. Milleredcd5822006-08-24 00:42:45 -0700836 return x;
837 }
838
839 return NULL;
840}
841
David S. Miller9aa60082011-02-24 01:51:36 -0500842static struct xfrm_state *__xfrm_state_lookup_byaddr(struct net *net, u32 mark,
843 const xfrm_address_t *daddr,
844 const xfrm_address_t *saddr,
845 u8 proto, unsigned short family)
David S. Milleredcd5822006-08-24 00:42:45 -0700846{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800847 unsigned int h = xfrm_src_hash(net, daddr, saddr, family);
David S. Milleredcd5822006-08-24 00:42:45 -0700848 struct xfrm_state *x;
849
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200850 hlist_for_each_entry_rcu(x, net->xfrm.state_bysrc + h, bysrc) {
David S. Milleredcd5822006-08-24 00:42:45 -0700851 if (x->props.family != family ||
Wei Yongjun18025712009-06-28 18:42:53 +0000852 x->id.proto != proto ||
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +0000853 !xfrm_addr_equal(&x->id.daddr, daddr, family) ||
854 !xfrm_addr_equal(&x->props.saddr, saddr, family))
David S. Milleredcd5822006-08-24 00:42:45 -0700855 continue;
856
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000857 if ((mark & x->mark.m) != x->mark.v)
858 continue;
Florian Westphal02efdff2016-08-09 12:16:05 +0200859 if (!xfrm_state_hold_rcu(x))
860 continue;
David S. Milleredcd5822006-08-24 00:42:45 -0700861 return x;
862 }
863
864 return NULL;
865}
866
867static inline struct xfrm_state *
868__xfrm_state_locate(struct xfrm_state *x, int use_spi, int family)
869{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800870 struct net *net = xs_net(x);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800871 u32 mark = x->mark.v & x->mark.m;
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800872
David S. Milleredcd5822006-08-24 00:42:45 -0700873 if (use_spi)
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800874 return __xfrm_state_lookup(net, mark, &x->id.daddr,
875 x->id.spi, x->id.proto, family);
David S. Milleredcd5822006-08-24 00:42:45 -0700876 else
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800877 return __xfrm_state_lookup_byaddr(net, mark,
878 &x->id.daddr,
David S. Milleredcd5822006-08-24 00:42:45 -0700879 &x->props.saddr,
880 x->id.proto, family);
881}
882
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800883static void xfrm_hash_grow_check(struct net *net, int have_hash_collision)
Patrick McHardy2fab22f2006-10-24 15:34:00 -0700884{
885 if (have_hash_collision &&
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800886 (net->xfrm.state_hmask + 1) < xfrm_state_hashmax &&
887 net->xfrm.state_num > net->xfrm.state_hmask)
888 schedule_work(&net->xfrm.state_hash_work);
Patrick McHardy2fab22f2006-10-24 15:34:00 -0700889}
890
David S. Miller08ec9af2009-03-13 14:22:40 -0700891static void xfrm_state_look_at(struct xfrm_policy *pol, struct xfrm_state *x,
David S. Miller4a08ab02011-02-22 18:21:31 -0800892 const struct flowi *fl, unsigned short family,
David S. Miller08ec9af2009-03-13 14:22:40 -0700893 struct xfrm_state **best, int *acq_in_progress,
894 int *error)
895{
896 /* Resolution logic:
897 * 1. There is a valid state with matching selector. Done.
898 * 2. Valid state with inappropriate selector. Skip.
899 *
900 * Entering area of "sysdeps".
901 *
902 * 3. If state is not valid, selector is temporary, it selects
903 * only session which triggered previous resolution. Key
904 * manager will do something to install a state with proper
905 * selector.
906 */
907 if (x->km.state == XFRM_STATE_VALID) {
908 if ((x->sel.family &&
909 !xfrm_selector_match(&x->sel, fl, x->sel.family)) ||
910 !security_xfrm_state_pol_flow_match(x, pol, fl))
911 return;
912
913 if (!*best ||
914 (*best)->km.dying > x->km.dying ||
915 ((*best)->km.dying == x->km.dying &&
916 (*best)->curlft.add_time < x->curlft.add_time))
917 *best = x;
918 } else if (x->km.state == XFRM_STATE_ACQ) {
919 *acq_in_progress = 1;
920 } else if (x->km.state == XFRM_STATE_ERROR ||
921 x->km.state == XFRM_STATE_EXPIRED) {
922 if (xfrm_selector_match(&x->sel, fl, x->sel.family) &&
923 security_xfrm_state_pol_flow_match(x, pol, fl))
924 *error = -ESRCH;
925 }
926}
927
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928struct xfrm_state *
David S. Miller33765d02011-02-24 01:55:45 -0500929xfrm_state_find(const xfrm_address_t *daddr, const xfrm_address_t *saddr,
David S. Millerb520e9f2011-02-22 18:24:19 -0800930 const struct flowi *fl, struct xfrm_tmpl *tmpl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 struct xfrm_policy *pol, int *err,
932 unsigned short family)
933{
David S. Miller08ec9af2009-03-13 14:22:40 -0700934 static xfrm_address_t saddr_wildcard = { };
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800935 struct net *net = xp_net(pol);
Nicolas Dichtel6a783c92009-04-27 02:58:59 -0700936 unsigned int h, h_wildcard;
David S. Miller37b08e32008-09-02 20:14:15 -0700937 struct xfrm_state *x, *x0, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 int acquire_in_progress = 0;
939 int error = 0;
940 struct xfrm_state *best = NULL;
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800941 u32 mark = pol->mark.v & pol->mark.m;
Thomas Egerer8444cf72010-09-20 11:11:38 -0700942 unsigned short encap_family = tmpl->encap_family;
Florian Westphalb65e3d72016-08-09 12:16:07 +0200943 unsigned int sequence;
Horia Geanta0f245582014-02-12 16:20:06 +0200944 struct km_event c;
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900945
David S. Miller37b08e32008-09-02 20:14:15 -0700946 to_put = NULL;
947
Florian Westphalb65e3d72016-08-09 12:16:07 +0200948 sequence = read_seqcount_begin(&xfrm_state_hash_generation);
949
Florian Westphald737a582016-08-09 12:16:09 +0200950 rcu_read_lock();
Thomas Egerer8444cf72010-09-20 11:11:38 -0700951 h = xfrm_dst_hash(net, daddr, saddr, tmpl->reqid, encap_family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200952 hlist_for_each_entry_rcu(x, net->xfrm.state_bydst + h, bydst) {
Thomas Egerer8444cf72010-09-20 11:11:38 -0700953 if (x->props.family == encap_family &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 x->props.reqid == tmpl->reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000955 (mark & x->mark.m) == x->mark.v &&
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -0700956 !(x->props.flags & XFRM_STATE_WILDRECV) &&
Thomas Egerer8444cf72010-09-20 11:11:38 -0700957 xfrm_state_addr_check(x, daddr, saddr, encap_family) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 tmpl->mode == x->props.mode &&
959 tmpl->id.proto == x->id.proto &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700960 (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
David S. Miller1f673c52011-02-24 01:53:13 -0500961 xfrm_state_look_at(pol, x, fl, encap_family,
David S. Miller08ec9af2009-03-13 14:22:40 -0700962 &best, &acquire_in_progress, &error);
963 }
Fan Du6f115632013-09-23 17:18:25 +0800964 if (best || acquire_in_progress)
David S. Miller08ec9af2009-03-13 14:22:40 -0700965 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
Thomas Egerer8444cf72010-09-20 11:11:38 -0700967 h_wildcard = xfrm_dst_hash(net, daddr, &saddr_wildcard, tmpl->reqid, encap_family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200968 hlist_for_each_entry_rcu(x, net->xfrm.state_bydst + h_wildcard, bydst) {
Thomas Egerer8444cf72010-09-20 11:11:38 -0700969 if (x->props.family == encap_family &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700970 x->props.reqid == tmpl->reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000971 (mark & x->mark.m) == x->mark.v &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700972 !(x->props.flags & XFRM_STATE_WILDRECV) &&
Fan Duf59bbdf2013-09-27 16:32:50 +0800973 xfrm_addr_equal(&x->id.daddr, daddr, encap_family) &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700974 tmpl->mode == x->props.mode &&
975 tmpl->id.proto == x->id.proto &&
976 (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
David S. Miller1f673c52011-02-24 01:53:13 -0500977 xfrm_state_look_at(pol, x, fl, encap_family,
David S. Miller08ec9af2009-03-13 14:22:40 -0700978 &best, &acquire_in_progress, &error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 }
980
David S. Miller08ec9af2009-03-13 14:22:40 -0700981found:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 x = best;
983 if (!x && !error && !acquire_in_progress) {
Patrick McHardy5c5d2812005-04-21 20:12:32 -0700984 if (tmpl->id.spi &&
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800985 (x0 = __xfrm_state_lookup(net, mark, daddr, tmpl->id.spi,
Thomas Egerer8444cf72010-09-20 11:11:38 -0700986 tmpl->id.proto, encap_family)) != NULL) {
David S. Miller37b08e32008-09-02 20:14:15 -0700987 to_put = x0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 error = -EEXIST;
989 goto out;
990 }
Horia Geanta0f245582014-02-12 16:20:06 +0200991
992 c.net = net;
993 /* If the KMs have no listeners (yet...), avoid allocating an SA
994 * for each and every packet - garbage collection might not
995 * handle the flood.
996 */
997 if (!km_is_alive(&c)) {
998 error = -ESRCH;
999 goto out;
1000 }
1001
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001002 x = xfrm_state_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 if (x == NULL) {
1004 error = -ENOMEM;
1005 goto out;
1006 }
Thomas Egerer8444cf72010-09-20 11:11:38 -07001007 /* Initialize temporary state matching only
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 * to current session. */
Thomas Egerer8444cf72010-09-20 11:11:38 -07001009 xfrm_init_tempstate(x, fl, tmpl, daddr, saddr, family);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001010 memcpy(&x->mark, &pol->mark, sizeof(x->mark));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
David S. Miller1d28f422011-03-12 00:29:39 -05001012 error = security_xfrm_state_alloc_acquire(x, pol->security, fl->flowi_secid);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001013 if (error) {
1014 x->km.state = XFRM_STATE_DEAD;
David S. Miller37b08e32008-09-02 20:14:15 -07001015 to_put = x;
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001016 x = NULL;
1017 goto out;
1018 }
1019
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 if (km_query(x, tmpl, pol) == 0) {
Florian Westphald737a582016-08-09 12:16:09 +02001021 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 x->km.state = XFRM_STATE_ACQ;
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001023 list_add(&x->km.all, &net->xfrm.state_all);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001024 hlist_add_head_rcu(&x->bydst, net->xfrm.state_bydst + h);
Thomas Egerer8444cf72010-09-20 11:11:38 -07001025 h = xfrm_src_hash(net, daddr, saddr, encap_family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001026 hlist_add_head_rcu(&x->bysrc, net->xfrm.state_bysrc + h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 if (x->id.spi) {
Thomas Egerer8444cf72010-09-20 11:11:38 -07001028 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, encap_family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001029 hlist_add_head_rcu(&x->byspi, net->xfrm.state_byspi + h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 }
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08001031 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -08001032 tasklet_hrtimer_start(&x->mtimer, ktime_set(net->xfrm.sysctl_acq_expires, 0), HRTIMER_MODE_REL);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001033 net->xfrm.state_num++;
1034 xfrm_hash_grow_check(net, x->bydst.next != NULL);
Florian Westphald737a582016-08-09 12:16:09 +02001035 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 } else {
1037 x->km.state = XFRM_STATE_DEAD;
David S. Miller37b08e32008-09-02 20:14:15 -07001038 to_put = x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 x = NULL;
1040 error = -ESRCH;
1041 }
1042 }
1043out:
Florian Westphal02efdff2016-08-09 12:16:05 +02001044 if (x) {
1045 if (!xfrm_state_hold_rcu(x)) {
1046 *err = -EAGAIN;
1047 x = NULL;
1048 }
1049 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 *err = acquire_in_progress ? -EAGAIN : error;
Florian Westphal02efdff2016-08-09 12:16:05 +02001051 }
Florian Westphald737a582016-08-09 12:16:09 +02001052 rcu_read_unlock();
David S. Miller37b08e32008-09-02 20:14:15 -07001053 if (to_put)
1054 xfrm_state_put(to_put);
Florian Westphalb65e3d72016-08-09 12:16:07 +02001055
1056 if (read_seqcount_retry(&xfrm_state_hash_generation, sequence)) {
1057 *err = -EAGAIN;
1058 if (x) {
1059 xfrm_state_put(x);
1060 x = NULL;
1061 }
1062 }
1063
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 return x;
1065}
1066
Jamal Hadi Salim628529b2007-07-02 22:41:14 -07001067struct xfrm_state *
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001068xfrm_stateonly_find(struct net *net, u32 mark,
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001069 xfrm_address_t *daddr, xfrm_address_t *saddr,
Jamal Hadi Salim628529b2007-07-02 22:41:14 -07001070 unsigned short family, u8 mode, u8 proto, u32 reqid)
1071{
Pavel Emelyanov4bda4f22007-12-14 11:38:04 -08001072 unsigned int h;
Jamal Hadi Salim628529b2007-07-02 22:41:14 -07001073 struct xfrm_state *rx = NULL, *x = NULL;
Jamal Hadi Salim628529b2007-07-02 22:41:14 -07001074
Fan Du4ae770b2014-01-03 11:18:29 +08001075 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001076 h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001077 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
Jamal Hadi Salim628529b2007-07-02 22:41:14 -07001078 if (x->props.family == family &&
1079 x->props.reqid == reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001080 (mark & x->mark.m) == x->mark.v &&
Jamal Hadi Salim628529b2007-07-02 22:41:14 -07001081 !(x->props.flags & XFRM_STATE_WILDRECV) &&
1082 xfrm_state_addr_check(x, daddr, saddr, family) &&
1083 mode == x->props.mode &&
1084 proto == x->id.proto &&
1085 x->km.state == XFRM_STATE_VALID) {
1086 rx = x;
1087 break;
1088 }
1089 }
1090
1091 if (rx)
1092 xfrm_state_hold(rx);
Fan Du4ae770b2014-01-03 11:18:29 +08001093 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salim628529b2007-07-02 22:41:14 -07001094
1095
1096 return rx;
1097}
1098EXPORT_SYMBOL(xfrm_stateonly_find);
1099
Fan Duc4549972014-01-03 11:18:32 +08001100struct xfrm_state *xfrm_state_lookup_byspi(struct net *net, __be32 spi,
1101 unsigned short family)
1102{
1103 struct xfrm_state *x;
1104 struct xfrm_state_walk *w;
1105
1106 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1107 list_for_each_entry(w, &net->xfrm.state_all, all) {
1108 x = container_of(w, struct xfrm_state, km);
1109 if (x->props.family != family ||
1110 x->id.spi != spi)
1111 continue;
1112
Fan Duc4549972014-01-03 11:18:32 +08001113 xfrm_state_hold(x);
Li RongQingbdddbf62015-04-29 08:42:44 +08001114 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Fan Duc4549972014-01-03 11:18:32 +08001115 return x;
1116 }
1117 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1118 return NULL;
1119}
1120EXPORT_SYMBOL(xfrm_state_lookup_byspi);
1121
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122static void __xfrm_state_insert(struct xfrm_state *x)
1123{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001124 struct net *net = xs_net(x);
David S. Millera624c102006-08-24 03:24:33 -07001125 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001127 list_add(&x->km.all, &net->xfrm.state_all);
Timo Teras4c563f72008-02-28 21:31:08 -08001128
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001129 h = xfrm_dst_hash(net, &x->id.daddr, &x->props.saddr,
David S. Millerc1969f22006-08-24 04:00:03 -07001130 x->props.reqid, x->props.family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001131 hlist_add_head_rcu(&x->bydst, net->xfrm.state_bydst + h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001133 h = xfrm_src_hash(net, &x->id.daddr, &x->props.saddr, x->props.family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001134 hlist_add_head_rcu(&x->bysrc, net->xfrm.state_bysrc + h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
Masahide NAKAMURA7b4dc3602006-09-27 22:21:52 -07001136 if (x->id.spi) {
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001137 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto,
Masahide NAKAMURA6c44e6b2006-08-23 17:53:57 -07001138 x->props.family);
1139
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001140 hlist_add_head_rcu(&x->byspi, net->xfrm.state_byspi + h);
Masahide NAKAMURA6c44e6b2006-08-23 17:53:57 -07001141 }
1142
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -08001143 tasklet_hrtimer_start(&x->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL);
David S. Millera47f0ce2006-08-24 03:54:22 -07001144 if (x->replay_maxage)
1145 mod_timer(&x->rtimer, jiffies + x->replay_maxage);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001146
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001147 net->xfrm.state_num++;
David S. Millerf034b5d2006-08-24 03:08:07 -07001148
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001149 xfrm_hash_grow_check(net, x->bydst.next != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150}
1151
Fan Du283bc9f2013-11-07 17:47:50 +08001152/* net->xfrm.xfrm_state_lock is held */
David S. Millerc7f5ea32006-08-24 03:29:04 -07001153static void __xfrm_state_bump_genids(struct xfrm_state *xnew)
1154{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001155 struct net *net = xs_net(xnew);
David S. Millerc7f5ea32006-08-24 03:29:04 -07001156 unsigned short family = xnew->props.family;
1157 u32 reqid = xnew->props.reqid;
1158 struct xfrm_state *x;
David S. Millerc7f5ea32006-08-24 03:29:04 -07001159 unsigned int h;
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001160 u32 mark = xnew->mark.v & xnew->mark.m;
David S. Millerc7f5ea32006-08-24 03:29:04 -07001161
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001162 h = xfrm_dst_hash(net, &xnew->id.daddr, &xnew->props.saddr, reqid, family);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001163 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
David S. Millerc7f5ea32006-08-24 03:29:04 -07001164 if (x->props.family == family &&
1165 x->props.reqid == reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001166 (mark & x->mark.m) == x->mark.v &&
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001167 xfrm_addr_equal(&x->id.daddr, &xnew->id.daddr, family) &&
1168 xfrm_addr_equal(&x->props.saddr, &xnew->props.saddr, family))
Herbert Xu34996cb2010-03-31 01:19:49 +00001169 x->genid++;
David S. Millerc7f5ea32006-08-24 03:29:04 -07001170 }
1171}
1172
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173void xfrm_state_insert(struct xfrm_state *x)
1174{
Fan Du283bc9f2013-11-07 17:47:50 +08001175 struct net *net = xs_net(x);
1176
1177 spin_lock_bh(&net->xfrm.xfrm_state_lock);
David S. Millerc7f5ea32006-08-24 03:29:04 -07001178 __xfrm_state_bump_genids(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 __xfrm_state_insert(x);
Fan Du283bc9f2013-11-07 17:47:50 +08001180 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181}
1182EXPORT_SYMBOL(xfrm_state_insert);
1183
Fan Du283bc9f2013-11-07 17:47:50 +08001184/* net->xfrm.xfrm_state_lock is held */
Mathias Krausee473fcb2013-06-26 23:56:58 +02001185static struct xfrm_state *__find_acq_core(struct net *net,
1186 const struct xfrm_mark *m,
David S. Millera70486f2011-02-27 23:17:24 -08001187 unsigned short family, u8 mode,
1188 u32 reqid, u8 proto,
1189 const xfrm_address_t *daddr,
Mathias Krausee473fcb2013-06-26 23:56:58 +02001190 const xfrm_address_t *saddr,
1191 int create)
David S. Miller27708342006-08-24 00:13:10 -07001192{
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001193 unsigned int h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
David S. Miller27708342006-08-24 00:13:10 -07001194 struct xfrm_state *x;
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001195 u32 mark = m->v & m->m;
David S. Miller27708342006-08-24 00:13:10 -07001196
Sasha Levinb67bfe02013-02-27 17:06:00 -08001197 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
David S. Miller27708342006-08-24 00:13:10 -07001198 if (x->props.reqid != reqid ||
1199 x->props.mode != mode ||
1200 x->props.family != family ||
1201 x->km.state != XFRM_STATE_ACQ ||
Joy Latten75e252d2007-03-12 17:14:07 -07001202 x->id.spi != 0 ||
Wei Yongjun18025712009-06-28 18:42:53 +00001203 x->id.proto != proto ||
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001204 (mark & x->mark.m) != x->mark.v ||
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001205 !xfrm_addr_equal(&x->id.daddr, daddr, family) ||
1206 !xfrm_addr_equal(&x->props.saddr, saddr, family))
David S. Miller27708342006-08-24 00:13:10 -07001207 continue;
1208
David S. Miller27708342006-08-24 00:13:10 -07001209 xfrm_state_hold(x);
1210 return x;
1211 }
1212
1213 if (!create)
1214 return NULL;
1215
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001216 x = xfrm_state_alloc(net);
David S. Miller27708342006-08-24 00:13:10 -07001217 if (likely(x)) {
1218 switch (family) {
1219 case AF_INET:
1220 x->sel.daddr.a4 = daddr->a4;
1221 x->sel.saddr.a4 = saddr->a4;
1222 x->sel.prefixlen_d = 32;
1223 x->sel.prefixlen_s = 32;
1224 x->props.saddr.a4 = saddr->a4;
1225 x->id.daddr.a4 = daddr->a4;
1226 break;
1227
1228 case AF_INET6:
Jiri Benc15e318b2015-03-29 16:59:24 +02001229 x->sel.daddr.in6 = daddr->in6;
1230 x->sel.saddr.in6 = saddr->in6;
David S. Miller27708342006-08-24 00:13:10 -07001231 x->sel.prefixlen_d = 128;
1232 x->sel.prefixlen_s = 128;
Jiri Benc15e318b2015-03-29 16:59:24 +02001233 x->props.saddr.in6 = saddr->in6;
1234 x->id.daddr.in6 = daddr->in6;
David S. Miller27708342006-08-24 00:13:10 -07001235 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001236 }
David S. Miller27708342006-08-24 00:13:10 -07001237
1238 x->km.state = XFRM_STATE_ACQ;
1239 x->id.proto = proto;
1240 x->props.family = family;
1241 x->props.mode = mode;
1242 x->props.reqid = reqid;
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001243 x->mark.v = m->v;
1244 x->mark.m = m->m;
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08001245 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
David S. Miller27708342006-08-24 00:13:10 -07001246 xfrm_state_hold(x);
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -08001247 tasklet_hrtimer_start(&x->mtimer, ktime_set(net->xfrm.sysctl_acq_expires, 0), HRTIMER_MODE_REL);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001248 list_add(&x->km.all, &net->xfrm.state_all);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001249 hlist_add_head_rcu(&x->bydst, net->xfrm.state_bydst + h);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001250 h = xfrm_src_hash(net, daddr, saddr, family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001251 hlist_add_head_rcu(&x->bysrc, net->xfrm.state_bysrc + h);
David S. Miller918049f2006-10-12 22:03:24 -07001252
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001253 net->xfrm.state_num++;
David S. Miller918049f2006-10-12 22:03:24 -07001254
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001255 xfrm_hash_grow_check(net, x->bydst.next != NULL);
David S. Miller27708342006-08-24 00:13:10 -07001256 }
1257
1258 return x;
1259}
1260
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001261static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
1263int xfrm_state_add(struct xfrm_state *x)
1264{
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001265 struct net *net = xs_net(x);
David S. Miller37b08e32008-09-02 20:14:15 -07001266 struct xfrm_state *x1, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 int family;
1268 int err;
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001269 u32 mark = x->mark.v & x->mark.m;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001270 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
1272 family = x->props.family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
David S. Miller37b08e32008-09-02 20:14:15 -07001274 to_put = NULL;
1275
Fan Du283bc9f2013-11-07 17:47:50 +08001276 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
David S. Milleredcd5822006-08-24 00:42:45 -07001278 x1 = __xfrm_state_locate(x, use_spi, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 if (x1) {
David S. Miller37b08e32008-09-02 20:14:15 -07001280 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 x1 = NULL;
1282 err = -EEXIST;
1283 goto out;
1284 }
1285
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001286 if (use_spi && x->km.seq) {
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001287 x1 = __xfrm_find_acq_byseq(net, mark, x->km.seq);
Joy Latten75e252d2007-03-12 17:14:07 -07001288 if (x1 && ((x1->id.proto != x->id.proto) ||
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001289 !xfrm_addr_equal(&x1->id.daddr, &x->id.daddr, family))) {
David S. Miller37b08e32008-09-02 20:14:15 -07001290 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 x1 = NULL;
1292 }
1293 }
1294
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001295 if (use_spi && !x1)
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001296 x1 = __find_acq_core(net, &x->mark, family, x->props.mode,
1297 x->props.reqid, x->id.proto,
David S. Miller27708342006-08-24 00:13:10 -07001298 &x->id.daddr, &x->props.saddr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
David S. Millerc7f5ea32006-08-24 03:29:04 -07001300 __xfrm_state_bump_genids(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 __xfrm_state_insert(x);
1302 err = 0;
1303
1304out:
Fan Du283bc9f2013-11-07 17:47:50 +08001305 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
1307 if (x1) {
1308 xfrm_state_delete(x1);
1309 xfrm_state_put(x1);
1310 }
1311
David S. Miller37b08e32008-09-02 20:14:15 -07001312 if (to_put)
1313 xfrm_state_put(to_put);
1314
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 return err;
1316}
1317EXPORT_SYMBOL(xfrm_state_add);
1318
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001319#ifdef CONFIG_XFRM_MIGRATE
Antony Antony4ab47d42017-06-06 12:12:13 +02001320static struct xfrm_state *xfrm_state_clone(struct xfrm_state *orig,
1321 struct xfrm_encap_tmpl *encap)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001322{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001323 struct net *net = xs_net(orig);
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001324 struct xfrm_state *x = xfrm_state_alloc(net);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001325 if (!x)
Herbert Xu553f9112010-02-15 20:00:51 +00001326 goto out;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001327
1328 memcpy(&x->id, &orig->id, sizeof(x->id));
1329 memcpy(&x->sel, &orig->sel, sizeof(x->sel));
1330 memcpy(&x->lft, &orig->lft, sizeof(x->lft));
1331 x->props.mode = orig->props.mode;
1332 x->props.replay_window = orig->props.replay_window;
1333 x->props.reqid = orig->props.reqid;
1334 x->props.family = orig->props.family;
1335 x->props.saddr = orig->props.saddr;
1336
1337 if (orig->aalg) {
Martin Willi4447bb32009-11-25 00:29:52 +00001338 x->aalg = xfrm_algo_auth_clone(orig->aalg);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001339 if (!x->aalg)
1340 goto error;
1341 }
1342 x->props.aalgo = orig->props.aalgo;
1343
Steffen Klassertee5c2312014-02-19 13:33:24 +01001344 if (orig->aead) {
1345 x->aead = xfrm_algo_aead_clone(orig->aead);
Antony Antony75bf50f2017-12-07 21:54:27 +01001346 x->geniv = orig->geniv;
Steffen Klassertee5c2312014-02-19 13:33:24 +01001347 if (!x->aead)
1348 goto error;
1349 }
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001350 if (orig->ealg) {
1351 x->ealg = xfrm_algo_clone(orig->ealg);
1352 if (!x->ealg)
1353 goto error;
1354 }
1355 x->props.ealgo = orig->props.ealgo;
1356
1357 if (orig->calg) {
1358 x->calg = xfrm_algo_clone(orig->calg);
1359 if (!x->calg)
1360 goto error;
1361 }
1362 x->props.calgo = orig->props.calgo;
1363
Antony Antony4ab47d42017-06-06 12:12:13 +02001364 if (encap || orig->encap) {
1365 if (encap)
1366 x->encap = kmemdup(encap, sizeof(*x->encap),
1367 GFP_KERNEL);
1368 else
1369 x->encap = kmemdup(orig->encap, sizeof(*x->encap),
1370 GFP_KERNEL);
1371
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001372 if (!x->encap)
1373 goto error;
1374 }
1375
1376 if (orig->coaddr) {
1377 x->coaddr = kmemdup(orig->coaddr, sizeof(*x->coaddr),
1378 GFP_KERNEL);
1379 if (!x->coaddr)
1380 goto error;
1381 }
1382
Steffen Klassertaf2f4642011-03-28 19:46:39 +00001383 if (orig->replay_esn) {
Steffen Klassertcc9ab602014-02-19 13:33:24 +01001384 if (xfrm_replay_clone(x, orig))
Steffen Klassertaf2f4642011-03-28 19:46:39 +00001385 goto error;
1386 }
1387
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001388 memcpy(&x->mark, &orig->mark, sizeof(x->mark));
1389
Steffen Klassertcc9ab602014-02-19 13:33:24 +01001390 if (xfrm_init_state(x) < 0)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001391 goto error;
1392
1393 x->props.flags = orig->props.flags;
Nicolas Dichtela947b0a2013-02-22 10:54:54 +01001394 x->props.extra_flags = orig->props.extra_flags;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001395
Steffen Klassertee5c2312014-02-19 13:33:24 +01001396 x->tfcpad = orig->tfcpad;
1397 x->replay_maxdiff = orig->replay_maxdiff;
1398 x->replay_maxage = orig->replay_maxage;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001399 x->curlft.add_time = orig->curlft.add_time;
1400 x->km.state = orig->km.state;
1401 x->km.seq = orig->km.seq;
Antony Antonya486cd22017-05-19 12:47:00 +02001402 x->replay = orig->replay;
1403 x->preplay = orig->preplay;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001404
1405 return x;
1406
1407 error:
Herbert Xu553f9112010-02-15 20:00:51 +00001408 xfrm_state_put(x);
1409out:
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001410 return NULL;
1411}
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001412
Fan Du283bc9f2013-11-07 17:47:50 +08001413struct xfrm_state *xfrm_migrate_state_find(struct xfrm_migrate *m, struct net *net)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001414{
1415 unsigned int h;
Steffen Klassert8c0cba22014-02-19 13:33:24 +01001416 struct xfrm_state *x = NULL;
1417
1418 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001419
1420 if (m->reqid) {
Fan Du283bc9f2013-11-07 17:47:50 +08001421 h = xfrm_dst_hash(net, &m->old_daddr, &m->old_saddr,
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001422 m->reqid, m->old_family);
Fan Du283bc9f2013-11-07 17:47:50 +08001423 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001424 if (x->props.mode != m->mode ||
1425 x->id.proto != m->proto)
1426 continue;
1427 if (m->reqid && x->props.reqid != m->reqid)
1428 continue;
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001429 if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr,
1430 m->old_family) ||
1431 !xfrm_addr_equal(&x->props.saddr, &m->old_saddr,
1432 m->old_family))
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001433 continue;
1434 xfrm_state_hold(x);
Steffen Klassert8c0cba22014-02-19 13:33:24 +01001435 break;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001436 }
1437 } else {
Fan Du283bc9f2013-11-07 17:47:50 +08001438 h = xfrm_src_hash(net, &m->old_daddr, &m->old_saddr,
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001439 m->old_family);
Fan Du283bc9f2013-11-07 17:47:50 +08001440 hlist_for_each_entry(x, net->xfrm.state_bysrc+h, bysrc) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001441 if (x->props.mode != m->mode ||
1442 x->id.proto != m->proto)
1443 continue;
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001444 if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr,
1445 m->old_family) ||
1446 !xfrm_addr_equal(&x->props.saddr, &m->old_saddr,
1447 m->old_family))
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001448 continue;
1449 xfrm_state_hold(x);
Steffen Klassert8c0cba22014-02-19 13:33:24 +01001450 break;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001451 }
1452 }
1453
Steffen Klassert8c0cba22014-02-19 13:33:24 +01001454 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1455
1456 return x;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001457}
1458EXPORT_SYMBOL(xfrm_migrate_state_find);
1459
Weilong Chen3e94c2d2013-12-24 09:43:47 +08001460struct xfrm_state *xfrm_state_migrate(struct xfrm_state *x,
Antony Antony4ab47d42017-06-06 12:12:13 +02001461 struct xfrm_migrate *m,
1462 struct xfrm_encap_tmpl *encap)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001463{
1464 struct xfrm_state *xc;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001465
Antony Antony4ab47d42017-06-06 12:12:13 +02001466 xc = xfrm_state_clone(x, encap);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001467 if (!xc)
1468 return NULL;
1469
1470 memcpy(&xc->id.daddr, &m->new_daddr, sizeof(xc->id.daddr));
1471 memcpy(&xc->props.saddr, &m->new_saddr, sizeof(xc->props.saddr));
1472
1473 /* add state */
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001474 if (xfrm_addr_equal(&x->id.daddr, &m->new_daddr, m->new_family)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001475 /* a care is needed when the destination address of the
1476 state is to be updated as it is a part of triplet */
1477 xfrm_state_insert(xc);
1478 } else {
Steffen Klassertcc9ab602014-02-19 13:33:24 +01001479 if (xfrm_state_add(xc) < 0)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001480 goto error;
1481 }
1482
1483 return xc;
1484error:
Thomas Egerer78347c82010-12-06 23:28:56 +00001485 xfrm_state_put(xc);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001486 return NULL;
1487}
1488EXPORT_SYMBOL(xfrm_state_migrate);
1489#endif
1490
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491int xfrm_state_update(struct xfrm_state *x)
1492{
David S. Miller37b08e32008-09-02 20:14:15 -07001493 struct xfrm_state *x1, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 int err;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001495 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
Fan Du283bc9f2013-11-07 17:47:50 +08001496 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497
David S. Miller37b08e32008-09-02 20:14:15 -07001498 to_put = NULL;
1499
Fan Du283bc9f2013-11-07 17:47:50 +08001500 spin_lock_bh(&net->xfrm.xfrm_state_lock);
David S. Milleredcd5822006-08-24 00:42:45 -07001501 x1 = __xfrm_state_locate(x, use_spi, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
1503 err = -ESRCH;
1504 if (!x1)
1505 goto out;
1506
1507 if (xfrm_state_kern(x1)) {
David S. Miller37b08e32008-09-02 20:14:15 -07001508 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 err = -EEXIST;
1510 goto out;
1511 }
1512
1513 if (x1->km.state == XFRM_STATE_ACQ) {
1514 __xfrm_state_insert(x);
1515 x = NULL;
1516 }
1517 err = 0;
1518
1519out:
Fan Du283bc9f2013-11-07 17:47:50 +08001520 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
David S. Miller37b08e32008-09-02 20:14:15 -07001522 if (to_put)
1523 xfrm_state_put(to_put);
1524
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 if (err)
1526 return err;
1527
1528 if (!x) {
1529 xfrm_state_delete(x1);
1530 xfrm_state_put(x1);
1531 return 0;
1532 }
1533
1534 err = -EINVAL;
1535 spin_lock_bh(&x1->lock);
1536 if (likely(x1->km.state == XFRM_STATE_VALID)) {
Herbert Xu257a4b02017-12-26 17:34:44 +11001537 if (x->encap && x1->encap &&
1538 x->encap->encap_type == x1->encap->encap_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 memcpy(x1->encap, x->encap, sizeof(*x1->encap));
Herbert Xu257a4b02017-12-26 17:34:44 +11001540 else if (x->encap || x1->encap)
1541 goto fail;
1542
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -07001543 if (x->coaddr && x1->coaddr) {
1544 memcpy(x1->coaddr, x->coaddr, sizeof(*x1->coaddr));
1545 }
1546 if (!use_spi && memcmp(&x1->sel, &x->sel, sizeof(x1->sel)))
1547 memcpy(&x1->sel, &x->sel, sizeof(x1->sel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 memcpy(&x1->lft, &x->lft, sizeof(x1->lft));
1549 x1->km.dying = 0;
1550
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -08001551 tasklet_hrtimer_start(&x1->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 if (x1->curlft.use_time)
1553 xfrm_state_check_expire(x1);
1554
1555 err = 0;
Tushar Gohad8fcbc632011-07-07 15:38:52 +00001556 x->km.state = XFRM_STATE_DEAD;
1557 __xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 }
Herbert Xu257a4b02017-12-26 17:34:44 +11001559
1560fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 spin_unlock_bh(&x1->lock);
1562
1563 xfrm_state_put(x1);
1564
1565 return err;
1566}
1567EXPORT_SYMBOL(xfrm_state_update);
1568
1569int xfrm_state_check_expire(struct xfrm_state *x)
1570{
1571 if (!x->curlft.use_time)
James Morris9d729f72007-03-04 16:12:44 -08001572 x->curlft.use_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 if (x->curlft.bytes >= x->lft.hard_byte_limit ||
1575 x->curlft.packets >= x->lft.hard_packet_limit) {
Herbert Xu4666faa2005-06-18 22:43:22 -07001576 x->km.state = XFRM_STATE_EXPIRED;
Thomas Gleixner8b0e1952016-12-25 12:30:41 +01001577 tasklet_hrtimer_start(&x->mtimer, 0, HRTIMER_MODE_REL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 return -EINVAL;
1579 }
1580
1581 if (!x->km.dying &&
1582 (x->curlft.bytes >= x->lft.soft_byte_limit ||
Herbert Xu4666faa2005-06-18 22:43:22 -07001583 x->curlft.packets >= x->lft.soft_packet_limit)) {
1584 x->km.dying = 1;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001585 km_state_expired(x, 0, 0);
Herbert Xu4666faa2005-06-18 22:43:22 -07001586 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 return 0;
1588}
1589EXPORT_SYMBOL(xfrm_state_check_expire);
1590
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591struct xfrm_state *
David S. Millera70486f2011-02-27 23:17:24 -08001592xfrm_state_lookup(struct net *net, u32 mark, const xfrm_address_t *daddr, __be32 spi,
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001593 u8 proto, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594{
1595 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
Florian Westphalc2f672f2016-09-20 15:45:26 +02001597 rcu_read_lock();
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001598 x = __xfrm_state_lookup(net, mark, daddr, spi, proto, family);
Florian Westphalc2f672f2016-09-20 15:45:26 +02001599 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 return x;
1601}
1602EXPORT_SYMBOL(xfrm_state_lookup);
1603
1604struct xfrm_state *
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001605xfrm_state_lookup_byaddr(struct net *net, u32 mark,
David S. Millera70486f2011-02-27 23:17:24 -08001606 const xfrm_address_t *daddr, const xfrm_address_t *saddr,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001607 u8 proto, unsigned short family)
1608{
1609 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001610
Fan Du283bc9f2013-11-07 17:47:50 +08001611 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001612 x = __xfrm_state_lookup_byaddr(net, mark, daddr, saddr, proto, family);
Fan Du283bc9f2013-11-07 17:47:50 +08001613 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001614 return x;
1615}
1616EXPORT_SYMBOL(xfrm_state_lookup_byaddr);
1617
1618struct xfrm_state *
Mathias Krausee473fcb2013-06-26 23:56:58 +02001619xfrm_find_acq(struct net *net, const struct xfrm_mark *mark, u8 mode, u32 reqid,
1620 u8 proto, const xfrm_address_t *daddr,
1621 const xfrm_address_t *saddr, int create, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622{
1623 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
Fan Du283bc9f2013-11-07 17:47:50 +08001625 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001626 x = __find_acq_core(net, mark, family, mode, reqid, proto, daddr, saddr, create);
Fan Du283bc9f2013-11-07 17:47:50 +08001627 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
David S. Miller27708342006-08-24 00:13:10 -07001628
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 return x;
1630}
1631EXPORT_SYMBOL(xfrm_find_acq);
1632
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001633#ifdef CONFIG_XFRM_SUB_POLICY
1634int
1635xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n,
Fan Du283bc9f2013-11-07 17:47:50 +08001636 unsigned short family, struct net *net)
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001637{
Koichiro Den3f5a95a2017-08-01 23:21:46 +09001638 int i;
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001639 int err = 0;
1640 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
1641 if (!afinfo)
1642 return -EAFNOSUPPORT;
1643
Fan Du283bc9f2013-11-07 17:47:50 +08001644 spin_lock_bh(&net->xfrm.xfrm_state_lock); /*FIXME*/
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001645 if (afinfo->tmpl_sort)
1646 err = afinfo->tmpl_sort(dst, src, n);
Koichiro Den3f5a95a2017-08-01 23:21:46 +09001647 else
1648 for (i = 0; i < n; i++)
1649 dst[i] = src[i];
Fan Du283bc9f2013-11-07 17:47:50 +08001650 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Florian Westphalaf5d27c2017-01-09 14:20:47 +01001651 rcu_read_unlock();
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001652 return err;
1653}
1654EXPORT_SYMBOL(xfrm_tmpl_sort);
1655
1656int
1657xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src, int n,
1658 unsigned short family)
1659{
Koichiro Den3f5a95a2017-08-01 23:21:46 +09001660 int i;
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001661 int err = 0;
1662 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
Steffen Klassert35ea790d2014-02-19 13:33:23 +01001663 struct net *net = xs_net(*src);
Fan Du283bc9f2013-11-07 17:47:50 +08001664
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001665 if (!afinfo)
1666 return -EAFNOSUPPORT;
1667
Fan Du283bc9f2013-11-07 17:47:50 +08001668 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001669 if (afinfo->state_sort)
1670 err = afinfo->state_sort(dst, src, n);
Koichiro Den3f5a95a2017-08-01 23:21:46 +09001671 else
1672 for (i = 0; i < n; i++)
1673 dst[i] = src[i];
Fan Du283bc9f2013-11-07 17:47:50 +08001674 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Florian Westphalaf5d27c2017-01-09 14:20:47 +01001675 rcu_read_unlock();
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001676 return err;
1677}
1678EXPORT_SYMBOL(xfrm_state_sort);
1679#endif
1680
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681/* Silly enough, but I'm lazy to build resolution list */
1682
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001683static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684{
1685 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001687 for (i = 0; i <= net->xfrm.state_hmask; i++) {
David S. Miller8f126e32006-08-24 02:45:07 -07001688 struct xfrm_state *x;
1689
Sasha Levinb67bfe02013-02-27 17:06:00 -08001690 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
David S. Miller8f126e32006-08-24 02:45:07 -07001691 if (x->km.seq == seq &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001692 (mark & x->mark.m) == x->mark.v &&
David S. Miller8f126e32006-08-24 02:45:07 -07001693 x->km.state == XFRM_STATE_ACQ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 xfrm_state_hold(x);
1695 return x;
1696 }
1697 }
1698 }
1699 return NULL;
1700}
1701
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001702struct xfrm_state *xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703{
1704 struct xfrm_state *x;
1705
Fan Du283bc9f2013-11-07 17:47:50 +08001706 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001707 x = __xfrm_find_acq_byseq(net, mark, seq);
Fan Du283bc9f2013-11-07 17:47:50 +08001708 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 return x;
1710}
1711EXPORT_SYMBOL(xfrm_find_acq_byseq);
1712
1713u32 xfrm_get_acqseq(void)
1714{
1715 u32 res;
jamal6836b9b2010-02-16 02:01:22 +00001716 static atomic_t acqseq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
jamal6836b9b2010-02-16 02:01:22 +00001718 do {
1719 res = atomic_inc_return(&acqseq);
1720 } while (!res);
1721
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 return res;
1723}
1724EXPORT_SYMBOL(xfrm_get_acqseq);
1725
Fan Du776e9dd2013-12-16 18:47:49 +08001726int verify_spi_info(u8 proto, u32 min, u32 max)
1727{
1728 switch (proto) {
1729 case IPPROTO_AH:
1730 case IPPROTO_ESP:
1731 break;
1732
1733 case IPPROTO_COMP:
1734 /* IPCOMP spi is 16-bits. */
1735 if (max >= 0x10000)
1736 return -EINVAL;
1737 break;
1738
1739 default:
1740 return -EINVAL;
1741 }
1742
1743 if (min > max)
1744 return -EINVAL;
1745
1746 return 0;
1747}
1748EXPORT_SYMBOL(verify_spi_info);
1749
Herbert Xu658b2192007-10-09 13:29:52 -07001750int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -08001752 struct net *net = xs_net(x);
David S. Millerf034b5d2006-08-24 03:08:07 -07001753 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 struct xfrm_state *x0;
Herbert Xu658b2192007-10-09 13:29:52 -07001755 int err = -ENOENT;
1756 __be32 minspi = htonl(low);
1757 __be32 maxspi = htonl(high);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001758 u32 mark = x->mark.v & x->mark.m;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759
Herbert Xu658b2192007-10-09 13:29:52 -07001760 spin_lock_bh(&x->lock);
1761 if (x->km.state == XFRM_STATE_DEAD)
1762 goto unlock;
1763
1764 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 if (x->id.spi)
Herbert Xu658b2192007-10-09 13:29:52 -07001766 goto unlock;
1767
1768 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
1770 if (minspi == maxspi) {
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001771 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, minspi, x->id.proto, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 if (x0) {
1773 xfrm_state_put(x0);
Herbert Xu658b2192007-10-09 13:29:52 -07001774 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 }
1776 x->id.spi = minspi;
1777 } else {
1778 u32 spi = 0;
Weilong Chen9b7a7872013-12-24 09:43:46 +08001779 for (h = 0; h < high-low+1; h++) {
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05001780 spi = low + prandom_u32()%(high-low+1);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001781 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, htonl(spi), x->id.proto, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 if (x0 == NULL) {
1783 x->id.spi = htonl(spi);
1784 break;
1785 }
1786 xfrm_state_put(x0);
1787 }
1788 }
1789 if (x->id.spi) {
Fan Du283bc9f2013-11-07 17:47:50 +08001790 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Alexey Dobriyan12604d82008-11-25 17:31:18 -08001791 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, x->props.family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001792 hlist_add_head_rcu(&x->byspi, net->xfrm.state_byspi + h);
Fan Du283bc9f2013-11-07 17:47:50 +08001793 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Herbert Xu658b2192007-10-09 13:29:52 -07001794
1795 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 }
Herbert Xu658b2192007-10-09 13:29:52 -07001797
1798unlock:
1799 spin_unlock_bh(&x->lock);
1800
1801 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802}
1803EXPORT_SYMBOL(xfrm_alloc_spi);
1804
Nicolas Dichteld3623092014-02-14 15:30:36 +01001805static bool __xfrm_state_filter_match(struct xfrm_state *x,
Nicolas Dichtel870a2df2014-03-06 18:24:29 +01001806 struct xfrm_address_filter *filter)
Nicolas Dichteld3623092014-02-14 15:30:36 +01001807{
1808 if (filter) {
1809 if ((filter->family == AF_INET ||
1810 filter->family == AF_INET6) &&
1811 x->props.family != filter->family)
1812 return false;
1813
1814 return addr_match(&x->props.saddr, &filter->saddr,
1815 filter->splen) &&
1816 addr_match(&x->id.daddr, &filter->daddr,
1817 filter->dplen);
1818 }
1819 return true;
1820}
1821
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08001822int xfrm_state_walk(struct net *net, struct xfrm_state_walk *walk,
Timo Teras4c563f72008-02-28 21:31:08 -08001823 int (*func)(struct xfrm_state *, int, void*),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 void *data)
1825{
Herbert Xu12a169e2008-10-01 07:03:24 -07001826 struct xfrm_state *state;
1827 struct xfrm_state_walk *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 int err = 0;
1829
Herbert Xu12a169e2008-10-01 07:03:24 -07001830 if (walk->seq != 0 && list_empty(&walk->all))
Timo Teras4c563f72008-02-28 21:31:08 -08001831 return 0;
1832
Fan Du283bc9f2013-11-07 17:47:50 +08001833 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001834 if (list_empty(&walk->all))
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08001835 x = list_first_entry(&net->xfrm.state_all, struct xfrm_state_walk, all);
Herbert Xu12a169e2008-10-01 07:03:24 -07001836 else
Li RongQing80077702015-04-22 17:09:54 +08001837 x = list_first_entry(&walk->all, struct xfrm_state_walk, all);
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08001838 list_for_each_entry_from(x, &net->xfrm.state_all, all) {
Herbert Xu12a169e2008-10-01 07:03:24 -07001839 if (x->state == XFRM_STATE_DEAD)
Timo Teras4c563f72008-02-28 21:31:08 -08001840 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07001841 state = container_of(x, struct xfrm_state, km);
1842 if (!xfrm_id_proto_match(state->id.proto, walk->proto))
Timo Teras4c563f72008-02-28 21:31:08 -08001843 continue;
Nicolas Dichteld3623092014-02-14 15:30:36 +01001844 if (!__xfrm_state_filter_match(state, walk->filter))
1845 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07001846 err = func(state, walk->seq, data);
1847 if (err) {
1848 list_move_tail(&walk->all, &x->all);
1849 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001851 walk->seq++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001853 if (walk->seq == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 err = -ENOENT;
1855 goto out;
1856 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001857 list_del_init(&walk->all);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858out:
Fan Du283bc9f2013-11-07 17:47:50 +08001859 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 return err;
1861}
1862EXPORT_SYMBOL(xfrm_state_walk);
1863
Nicolas Dichteld3623092014-02-14 15:30:36 +01001864void xfrm_state_walk_init(struct xfrm_state_walk *walk, u8 proto,
Nicolas Dichtel870a2df2014-03-06 18:24:29 +01001865 struct xfrm_address_filter *filter)
Herbert Xu5c182452008-09-22 19:48:19 -07001866{
Herbert Xu12a169e2008-10-01 07:03:24 -07001867 INIT_LIST_HEAD(&walk->all);
Herbert Xu5c182452008-09-22 19:48:19 -07001868 walk->proto = proto;
Herbert Xu12a169e2008-10-01 07:03:24 -07001869 walk->state = XFRM_STATE_DEAD;
1870 walk->seq = 0;
Nicolas Dichteld3623092014-02-14 15:30:36 +01001871 walk->filter = filter;
Herbert Xu5c182452008-09-22 19:48:19 -07001872}
1873EXPORT_SYMBOL(xfrm_state_walk_init);
1874
Fan Du283bc9f2013-11-07 17:47:50 +08001875void xfrm_state_walk_done(struct xfrm_state_walk *walk, struct net *net)
Herbert Xuabb81c42008-09-09 19:58:29 -07001876{
Nicolas Dichteld3623092014-02-14 15:30:36 +01001877 kfree(walk->filter);
1878
Herbert Xu12a169e2008-10-01 07:03:24 -07001879 if (list_empty(&walk->all))
Herbert Xu5c182452008-09-22 19:48:19 -07001880 return;
Herbert Xu5c182452008-09-22 19:48:19 -07001881
Fan Du283bc9f2013-11-07 17:47:50 +08001882 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001883 list_del(&walk->all);
Fan Du283bc9f2013-11-07 17:47:50 +08001884 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Herbert Xuabb81c42008-09-09 19:58:29 -07001885}
1886EXPORT_SYMBOL(xfrm_state_walk_done);
1887
Kees Cooke99e88a2017-10-16 14:43:17 -07001888static void xfrm_replay_timer_handler(struct timer_list *t)
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001889{
Kees Cooke99e88a2017-10-16 14:43:17 -07001890 struct xfrm_state *x = from_timer(x, t, rtimer);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001891
1892 spin_lock(&x->lock);
1893
Jamal Hadi Salim27170962006-04-14 15:03:05 -07001894 if (x->km.state == XFRM_STATE_VALID) {
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001895 if (xfrm_aevent_is_on(xs_net(x)))
Steffen Klassert9fdc4882011-03-08 00:08:32 +00001896 x->repl->notify(x, XFRM_REPLAY_TIMEOUT);
Jamal Hadi Salim27170962006-04-14 15:03:05 -07001897 else
1898 x->xflags |= XFRM_TIME_DEFER;
1899 }
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001900
1901 spin_unlock(&x->lock);
1902}
1903
Denis Chengdf018122007-12-07 00:51:11 -08001904static LIST_HEAD(xfrm_km_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905
David S. Miller214e0052011-02-24 00:02:38 -05001906void km_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907{
1908 struct xfrm_mgr *km;
1909
Cong Wang85168c02013-01-16 16:05:06 +08001910 rcu_read_lock();
1911 list_for_each_entry_rcu(km, &xfrm_km_list, list)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001912 if (km->notify_policy)
1913 km->notify_policy(xp, dir, c);
Cong Wang85168c02013-01-16 16:05:06 +08001914 rcu_read_unlock();
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001915}
1916
David S. Miller214e0052011-02-24 00:02:38 -05001917void km_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001918{
1919 struct xfrm_mgr *km;
Cong Wang85168c02013-01-16 16:05:06 +08001920 rcu_read_lock();
1921 list_for_each_entry_rcu(km, &xfrm_km_list, list)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001922 if (km->notify)
1923 km->notify(x, c);
Cong Wang85168c02013-01-16 16:05:06 +08001924 rcu_read_unlock();
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001925}
1926
1927EXPORT_SYMBOL(km_policy_notify);
1928EXPORT_SYMBOL(km_state_notify);
1929
Eric W. Biederman15e47302012-09-07 20:12:54 +00001930void km_state_expired(struct xfrm_state *x, int hard, u32 portid)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001931{
1932 struct km_event c;
1933
Herbert Xubf08867f92005-06-18 22:44:00 -07001934 c.data.hard = hard;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001935 c.portid = portid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001936 c.event = XFRM_MSG_EXPIRE;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001937 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938}
1939
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001940EXPORT_SYMBOL(km_state_expired);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001941/*
1942 * We send to all registered managers regardless of failure
1943 * We are happy with one success
1944*/
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001945int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001947 int err = -EINVAL, acqret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 struct xfrm_mgr *km;
1949
Cong Wang85168c02013-01-16 16:05:06 +08001950 rcu_read_lock();
1951 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Fan Du65e07362012-08-15 10:13:47 +08001952 acqret = km->acquire(x, t, pol);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001953 if (!acqret)
1954 err = acqret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 }
Cong Wang85168c02013-01-16 16:05:06 +08001956 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 return err;
1958}
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001959EXPORT_SYMBOL(km_query);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960
Al Viro5d36b182006-11-08 00:24:06 -08001961int km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962{
1963 int err = -EINVAL;
1964 struct xfrm_mgr *km;
1965
Cong Wang85168c02013-01-16 16:05:06 +08001966 rcu_read_lock();
1967 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 if (km->new_mapping)
1969 err = km->new_mapping(x, ipaddr, sport);
1970 if (!err)
1971 break;
1972 }
Cong Wang85168c02013-01-16 16:05:06 +08001973 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 return err;
1975}
1976EXPORT_SYMBOL(km_new_mapping);
1977
Eric W. Biederman15e47302012-09-07 20:12:54 +00001978void km_policy_expired(struct xfrm_policy *pol, int dir, int hard, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001980 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981
Herbert Xubf08867f92005-06-18 22:44:00 -07001982 c.data.hard = hard;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001983 c.portid = portid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001984 c.event = XFRM_MSG_POLEXPIRE;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001985 km_policy_notify(pol, dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986}
David S. Millera70fcb02006-03-20 19:18:52 -08001987EXPORT_SYMBOL(km_policy_expired);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988
Eric Dumazet2d60abc2008-01-03 20:43:21 -08001989#ifdef CONFIG_XFRM_MIGRATE
David S. Miller183cad12011-02-24 00:28:01 -05001990int km_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
1991 const struct xfrm_migrate *m, int num_migrate,
Antony Antony8bafd732017-06-06 12:12:14 +02001992 const struct xfrm_kmaddress *k,
1993 const struct xfrm_encap_tmpl *encap)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001994{
1995 int err = -EINVAL;
1996 int ret;
1997 struct xfrm_mgr *km;
1998
Cong Wang85168c02013-01-16 16:05:06 +08001999 rcu_read_lock();
2000 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002001 if (km->migrate) {
Antony Antony8bafd732017-06-06 12:12:14 +02002002 ret = km->migrate(sel, dir, type, m, num_migrate, k,
2003 encap);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002004 if (!ret)
2005 err = ret;
2006 }
2007 }
Cong Wang85168c02013-01-16 16:05:06 +08002008 rcu_read_unlock();
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002009 return err;
2010}
2011EXPORT_SYMBOL(km_migrate);
Eric Dumazet2d60abc2008-01-03 20:43:21 -08002012#endif
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002013
Alexey Dobriyandb983c12008-11-25 17:51:01 -08002014int km_report(struct net *net, u8 proto, struct xfrm_selector *sel, xfrm_address_t *addr)
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002015{
2016 int err = -EINVAL;
2017 int ret;
2018 struct xfrm_mgr *km;
2019
Cong Wang85168c02013-01-16 16:05:06 +08002020 rcu_read_lock();
2021 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002022 if (km->report) {
Alexey Dobriyandb983c12008-11-25 17:51:01 -08002023 ret = km->report(net, proto, sel, addr);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002024 if (!ret)
2025 err = ret;
2026 }
2027 }
Cong Wang85168c02013-01-16 16:05:06 +08002028 rcu_read_unlock();
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002029 return err;
2030}
2031EXPORT_SYMBOL(km_report);
2032
Horia Geanta0f245582014-02-12 16:20:06 +02002033bool km_is_alive(const struct km_event *c)
2034{
2035 struct xfrm_mgr *km;
2036 bool is_alive = false;
2037
2038 rcu_read_lock();
2039 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
2040 if (km->is_alive && km->is_alive(c)) {
2041 is_alive = true;
2042 break;
2043 }
2044 }
2045 rcu_read_unlock();
2046
2047 return is_alive;
2048}
2049EXPORT_SYMBOL(km_is_alive);
2050
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051int xfrm_user_policy(struct sock *sk, int optname, u8 __user *optval, int optlen)
2052{
2053 int err;
2054 u8 *data;
2055 struct xfrm_mgr *km;
2056 struct xfrm_policy *pol = NULL;
2057
2058 if (optlen <= 0 || optlen > PAGE_SIZE)
2059 return -EMSGSIZE;
2060
Geliang Tanga133d932017-05-06 23:42:21 +08002061 data = memdup_user(optval, optlen);
2062 if (IS_ERR(data))
2063 return PTR_ERR(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064
2065 err = -EINVAL;
Cong Wang85168c02013-01-16 16:05:06 +08002066 rcu_read_lock();
2067 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002068 pol = km->compile_policy(sk, optname, data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 optlen, &err);
2070 if (err >= 0)
2071 break;
2072 }
Cong Wang85168c02013-01-16 16:05:06 +08002073 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074
2075 if (err >= 0) {
2076 xfrm_sk_policy_insert(sk, err, pol);
2077 xfrm_pol_put(pol);
Jonathan Basseri2b06cdf2017-10-25 09:52:27 -07002078 __sk_dst_reset(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 err = 0;
2080 }
2081
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 kfree(data);
2083 return err;
2084}
2085EXPORT_SYMBOL(xfrm_user_policy);
2086
Cong Wang85168c02013-01-16 16:05:06 +08002087static DEFINE_SPINLOCK(xfrm_km_lock);
2088
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089int xfrm_register_km(struct xfrm_mgr *km)
2090{
Cong Wang85168c02013-01-16 16:05:06 +08002091 spin_lock_bh(&xfrm_km_lock);
2092 list_add_tail_rcu(&km->list, &xfrm_km_list);
2093 spin_unlock_bh(&xfrm_km_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 return 0;
2095}
2096EXPORT_SYMBOL(xfrm_register_km);
2097
2098int xfrm_unregister_km(struct xfrm_mgr *km)
2099{
Cong Wang85168c02013-01-16 16:05:06 +08002100 spin_lock_bh(&xfrm_km_lock);
2101 list_del_rcu(&km->list);
2102 spin_unlock_bh(&xfrm_km_lock);
2103 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 return 0;
2105}
2106EXPORT_SYMBOL(xfrm_unregister_km);
2107
2108int xfrm_state_register_afinfo(struct xfrm_state_afinfo *afinfo)
2109{
2110 int err = 0;
Florian Westphal423826a2017-01-09 14:20:46 +01002111
2112 if (WARN_ON(afinfo->family >= NPROTO))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 return -EAFNOSUPPORT;
Florian Westphal423826a2017-01-09 14:20:46 +01002114
Cong Wang44abdc32013-01-16 16:05:05 +08002115 spin_lock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 if (unlikely(xfrm_state_afinfo[afinfo->family] != NULL))
Li RongQingf31e8d4f2015-04-23 11:06:53 +08002117 err = -EEXIST;
David S. Milleredcd5822006-08-24 00:42:45 -07002118 else
Cong Wang44abdc32013-01-16 16:05:05 +08002119 rcu_assign_pointer(xfrm_state_afinfo[afinfo->family], afinfo);
2120 spin_unlock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 return err;
2122}
2123EXPORT_SYMBOL(xfrm_state_register_afinfo);
2124
2125int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo)
2126{
Florian Westphal423826a2017-01-09 14:20:46 +01002127 int err = 0, family = afinfo->family;
2128
2129 if (WARN_ON(family >= NPROTO))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 return -EAFNOSUPPORT;
Florian Westphal423826a2017-01-09 14:20:46 +01002131
Cong Wang44abdc32013-01-16 16:05:05 +08002132 spin_lock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 if (likely(xfrm_state_afinfo[afinfo->family] != NULL)) {
Florian Westphal423826a2017-01-09 14:20:46 +01002134 if (rcu_access_pointer(xfrm_state_afinfo[family]) != afinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 err = -EINVAL;
David S. Milleredcd5822006-08-24 00:42:45 -07002136 else
Cong Wang44abdc32013-01-16 16:05:05 +08002137 RCU_INIT_POINTER(xfrm_state_afinfo[afinfo->family], NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 }
Cong Wang44abdc32013-01-16 16:05:05 +08002139 spin_unlock_bh(&xfrm_state_afinfo_lock);
2140 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 return err;
2142}
2143EXPORT_SYMBOL(xfrm_state_unregister_afinfo);
2144
Florian Westphal711059b2017-01-09 14:20:48 +01002145struct xfrm_state_afinfo *xfrm_state_afinfo_get_rcu(unsigned int family)
2146{
2147 if (unlikely(family >= NPROTO))
2148 return NULL;
2149
2150 return rcu_dereference(xfrm_state_afinfo[family]);
2151}
2152
Hannes Frederic Sowa628e3412013-08-14 13:05:23 +02002153struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154{
2155 struct xfrm_state_afinfo *afinfo;
2156 if (unlikely(family >= NPROTO))
2157 return NULL;
Cong Wang44abdc32013-01-16 16:05:05 +08002158 rcu_read_lock();
2159 afinfo = rcu_dereference(xfrm_state_afinfo[family]);
Herbert Xu546be242006-05-27 23:03:58 -07002160 if (unlikely(!afinfo))
Cong Wang44abdc32013-01-16 16:05:05 +08002161 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 return afinfo;
2163}
2164
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165/* Temporarily located here until net/xfrm/xfrm_tunnel.c is created */
2166void xfrm_state_delete_tunnel(struct xfrm_state *x)
2167{
2168 if (x->tunnel) {
2169 struct xfrm_state *t = x->tunnel;
2170
2171 if (atomic_read(&t->tunnel_users) == 2)
2172 xfrm_state_delete(t);
2173 atomic_dec(&t->tunnel_users);
2174 xfrm_state_put(t);
2175 x->tunnel = NULL;
2176 }
2177}
2178EXPORT_SYMBOL(xfrm_state_delete_tunnel);
2179
2180int xfrm_state_mtu(struct xfrm_state *x, int mtu)
2181{
Florian Westphalb3b73b82017-01-05 13:23:58 +01002182 const struct xfrm_type *type = READ_ONCE(x->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183
Patrick McHardyc5c25232007-04-09 11:47:18 -07002184 if (x->km.state == XFRM_STATE_VALID &&
Florian Westphalb3b73b82017-01-05 13:23:58 +01002185 type && type->get_mtu)
2186 return type->get_mtu(x, mtu);
2187
2188 return mtu - x->props.header_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189}
2190
Ilan Tayariffdb5212017-08-01 12:49:08 +03002191int __xfrm_init_state(struct xfrm_state *x, bool init_replay, bool offload)
Herbert Xu72cb6962005-06-20 13:18:08 -07002192{
Herbert Xud094cd82005-06-20 13:19:41 -07002193 struct xfrm_state_afinfo *afinfo;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002194 struct xfrm_mode *inner_mode;
Herbert Xud094cd82005-06-20 13:19:41 -07002195 int family = x->props.family;
Herbert Xu72cb6962005-06-20 13:18:08 -07002196 int err;
2197
Herbert Xud094cd82005-06-20 13:19:41 -07002198 err = -EAFNOSUPPORT;
2199 afinfo = xfrm_state_get_afinfo(family);
2200 if (!afinfo)
2201 goto error;
2202
2203 err = 0;
2204 if (afinfo->init_flags)
2205 err = afinfo->init_flags(x);
2206
Florian Westphalaf5d27c2017-01-09 14:20:47 +01002207 rcu_read_unlock();
Herbert Xud094cd82005-06-20 13:19:41 -07002208
2209 if (err)
2210 goto error;
2211
2212 err = -EPROTONOSUPPORT;
Herbert Xu13996372007-10-17 21:35:51 -07002213
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002214 if (x->sel.family != AF_UNSPEC) {
2215 inner_mode = xfrm_get_mode(x->props.mode, x->sel.family);
2216 if (inner_mode == NULL)
2217 goto error;
2218
2219 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL) &&
2220 family != x->sel.family) {
2221 xfrm_put_mode(inner_mode);
2222 goto error;
2223 }
2224
2225 x->inner_mode = inner_mode;
2226 } else {
2227 struct xfrm_mode *inner_mode_iaf;
Martin Willid81d2282008-12-03 15:38:07 -08002228 int iafamily = AF_INET;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002229
Martin Willid81d2282008-12-03 15:38:07 -08002230 inner_mode = xfrm_get_mode(x->props.mode, x->props.family);
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002231 if (inner_mode == NULL)
2232 goto error;
2233
2234 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL)) {
2235 xfrm_put_mode(inner_mode);
2236 goto error;
2237 }
Martin Willid81d2282008-12-03 15:38:07 -08002238 x->inner_mode = inner_mode;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002239
Martin Willid81d2282008-12-03 15:38:07 -08002240 if (x->props.family == AF_INET)
2241 iafamily = AF_INET6;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002242
Martin Willid81d2282008-12-03 15:38:07 -08002243 inner_mode_iaf = xfrm_get_mode(x->props.mode, iafamily);
2244 if (inner_mode_iaf) {
2245 if (inner_mode_iaf->flags & XFRM_MODE_FLAG_TUNNEL)
2246 x->inner_mode_iaf = inner_mode_iaf;
2247 else
2248 xfrm_put_mode(inner_mode_iaf);
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002249 }
2250 }
Herbert Xu13996372007-10-17 21:35:51 -07002251
Herbert Xud094cd82005-06-20 13:19:41 -07002252 x->type = xfrm_get_type(x->id.proto, family);
Herbert Xu72cb6962005-06-20 13:18:08 -07002253 if (x->type == NULL)
2254 goto error;
2255
Ilan Tayariffdb5212017-08-01 12:49:08 +03002256 x->type_offload = xfrm_get_type_offload(x->id.proto, family, offload);
Steffen Klassert9d389d72017-04-14 10:05:44 +02002257
Herbert Xu72cb6962005-06-20 13:18:08 -07002258 err = x->type->init_state(x);
2259 if (err)
2260 goto error;
2261
Herbert Xu13996372007-10-17 21:35:51 -07002262 x->outer_mode = xfrm_get_mode(x->props.mode, family);
Julia Lawall599901c2012-08-29 06:49:15 +00002263 if (x->outer_mode == NULL) {
2264 err = -EPROTONOSUPPORT;
Herbert Xub59f45d2006-05-27 23:05:54 -07002265 goto error;
Julia Lawall599901c2012-08-29 06:49:15 +00002266 }
Herbert Xub59f45d2006-05-27 23:05:54 -07002267
Wei Yongjuna454f0c2011-03-21 18:08:28 -07002268 if (init_replay) {
2269 err = xfrm_init_replay(x);
2270 if (err)
2271 goto error;
2272 }
2273
Herbert Xu72cb6962005-06-20 13:18:08 -07002274 x->km.state = XFRM_STATE_VALID;
2275
2276error:
2277 return err;
2278}
2279
Wei Yongjuna454f0c2011-03-21 18:08:28 -07002280EXPORT_SYMBOL(__xfrm_init_state);
2281
2282int xfrm_init_state(struct xfrm_state *x)
2283{
Ilan Tayariffdb5212017-08-01 12:49:08 +03002284 return __xfrm_init_state(x, true, false);
Wei Yongjuna454f0c2011-03-21 18:08:28 -07002285}
2286
Herbert Xu72cb6962005-06-20 13:18:08 -07002287EXPORT_SYMBOL(xfrm_init_state);
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09002288
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002289int __net_init xfrm_state_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290{
David S. Millerf034b5d2006-08-24 03:08:07 -07002291 unsigned int sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292
Alexey Dobriyan9d4139c2008-11-25 17:16:11 -08002293 INIT_LIST_HEAD(&net->xfrm.state_all);
2294
David S. Millerf034b5d2006-08-24 03:08:07 -07002295 sz = sizeof(struct hlist_head) * 8;
2296
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002297 net->xfrm.state_bydst = xfrm_hash_alloc(sz);
2298 if (!net->xfrm.state_bydst)
2299 goto out_bydst;
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002300 net->xfrm.state_bysrc = xfrm_hash_alloc(sz);
2301 if (!net->xfrm.state_bysrc)
2302 goto out_bysrc;
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002303 net->xfrm.state_byspi = xfrm_hash_alloc(sz);
2304 if (!net->xfrm.state_byspi)
2305 goto out_byspi;
Alexey Dobriyan529983e2008-11-25 17:18:12 -08002306 net->xfrm.state_hmask = ((sz / sizeof(struct hlist_head)) - 1);
David S. Millerf034b5d2006-08-24 03:08:07 -07002307
Alexey Dobriyan0bf7c5b2008-11-25 17:18:39 -08002308 net->xfrm.state_num = 0;
Alexey Dobriyan63082732008-11-25 17:19:07 -08002309 INIT_WORK(&net->xfrm.state_hash_work, xfrm_hash_resize);
Fan Du283bc9f2013-11-07 17:47:50 +08002310 spin_lock_init(&net->xfrm.xfrm_state_lock);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002311 return 0;
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002312
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002313out_byspi:
2314 xfrm_hash_free(net->xfrm.state_bysrc, sz);
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002315out_bysrc:
2316 xfrm_hash_free(net->xfrm.state_bydst, sz);
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002317out_bydst:
2318 return -ENOMEM;
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002319}
2320
2321void xfrm_state_fini(struct net *net)
2322{
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002323 unsigned int sz;
2324
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002325 flush_work(&net->xfrm.state_hash_work);
Tetsuo Handa2e710292014-04-22 21:48:30 +09002326 xfrm_state_flush(net, IPSEC_PROTO_ANY, false);
Florian Westphal35db57bb2016-08-23 16:00:12 +02002327 flush_work(&xfrm_state_gc_work);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002328
Alexey Dobriyan9d4139c2008-11-25 17:16:11 -08002329 WARN_ON(!list_empty(&net->xfrm.state_all));
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002330
Alexey Dobriyan529983e2008-11-25 17:18:12 -08002331 sz = (net->xfrm.state_hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002332 WARN_ON(!hlist_empty(net->xfrm.state_byspi));
2333 xfrm_hash_free(net->xfrm.state_byspi, sz);
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002334 WARN_ON(!hlist_empty(net->xfrm.state_bysrc));
2335 xfrm_hash_free(net->xfrm.state_bysrc, sz);
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002336 WARN_ON(!hlist_empty(net->xfrm.state_bydst));
2337 xfrm_hash_free(net->xfrm.state_bydst, sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338}
2339
Joy Lattenab5f5e82007-09-17 11:51:22 -07002340#ifdef CONFIG_AUDITSYSCALL
Ilpo Järvinencf35f432008-01-05 23:13:20 -08002341static void xfrm_audit_helper_sainfo(struct xfrm_state *x,
2342 struct audit_buffer *audit_buf)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002343{
Paul Moore68277ac2007-12-20 20:49:33 -08002344 struct xfrm_sec_ctx *ctx = x->security;
2345 u32 spi = ntohl(x->id.spi);
2346
2347 if (ctx)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002348 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
Paul Moore68277ac2007-12-20 20:49:33 -08002349 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002350
Weilong Chen9b7a7872013-12-24 09:43:46 +08002351 switch (x->props.family) {
Joy Lattenab5f5e82007-09-17 11:51:22 -07002352 case AF_INET:
Harvey Harrison21454aa2008-10-31 00:54:56 -07002353 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2354 &x->props.saddr.a4, &x->id.daddr.a4);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002355 break;
2356 case AF_INET6:
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002357 audit_log_format(audit_buf, " src=%pI6 dst=%pI6",
Harvey Harrisonfdb46ee2008-10-28 16:10:17 -07002358 x->props.saddr.a6, x->id.daddr.a6);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002359 break;
2360 }
Paul Moore68277ac2007-12-20 20:49:33 -08002361
2362 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002363}
2364
Ilpo Järvinencf35f432008-01-05 23:13:20 -08002365static void xfrm_audit_helper_pktinfo(struct sk_buff *skb, u16 family,
2366 struct audit_buffer *audit_buf)
Paul Mooreafeb14b2007-12-21 14:58:11 -08002367{
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002368 const struct iphdr *iph4;
2369 const struct ipv6hdr *iph6;
Paul Mooreafeb14b2007-12-21 14:58:11 -08002370
2371 switch (family) {
2372 case AF_INET:
2373 iph4 = ip_hdr(skb);
Harvey Harrison21454aa2008-10-31 00:54:56 -07002374 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2375 &iph4->saddr, &iph4->daddr);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002376 break;
2377 case AF_INET6:
2378 iph6 = ipv6_hdr(skb);
2379 audit_log_format(audit_buf,
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002380 " src=%pI6 dst=%pI6 flowlbl=0x%x%02x%02x",
Weilong Chen9b7a7872013-12-24 09:43:46 +08002381 &iph6->saddr, &iph6->daddr,
Paul Mooreafeb14b2007-12-21 14:58:11 -08002382 iph6->flow_lbl[0] & 0x0f,
2383 iph6->flow_lbl[1],
2384 iph6->flow_lbl[2]);
2385 break;
2386 }
2387}
2388
Tetsuo Handa2e710292014-04-22 21:48:30 +09002389void xfrm_audit_state_add(struct xfrm_state *x, int result, bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002390{
2391 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002392
Paul Mooreafeb14b2007-12-21 14:58:11 -08002393 audit_buf = xfrm_audit_start("SAD-add");
Joy Lattenab5f5e82007-09-17 11:51:22 -07002394 if (audit_buf == NULL)
2395 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09002396 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002397 xfrm_audit_helper_sainfo(x, audit_buf);
2398 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002399 audit_log_end(audit_buf);
2400}
2401EXPORT_SYMBOL_GPL(xfrm_audit_state_add);
2402
Tetsuo Handa2e710292014-04-22 21:48:30 +09002403void xfrm_audit_state_delete(struct xfrm_state *x, int result, bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002404{
2405 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002406
Paul Mooreafeb14b2007-12-21 14:58:11 -08002407 audit_buf = xfrm_audit_start("SAD-delete");
Joy Lattenab5f5e82007-09-17 11:51:22 -07002408 if (audit_buf == NULL)
2409 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09002410 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002411 xfrm_audit_helper_sainfo(x, audit_buf);
2412 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002413 audit_log_end(audit_buf);
2414}
2415EXPORT_SYMBOL_GPL(xfrm_audit_state_delete);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002416
2417void xfrm_audit_state_replay_overflow(struct xfrm_state *x,
2418 struct sk_buff *skb)
2419{
2420 struct audit_buffer *audit_buf;
2421 u32 spi;
2422
2423 audit_buf = xfrm_audit_start("SA-replay-overflow");
2424 if (audit_buf == NULL)
2425 return;
2426 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2427 /* don't record the sequence number because it's inherent in this kind
2428 * of audit message */
2429 spi = ntohl(x->id.spi);
2430 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
2431 audit_log_end(audit_buf);
2432}
2433EXPORT_SYMBOL_GPL(xfrm_audit_state_replay_overflow);
2434
Steffen Klassert9fdc4882011-03-08 00:08:32 +00002435void xfrm_audit_state_replay(struct xfrm_state *x,
Paul Mooreafeb14b2007-12-21 14:58:11 -08002436 struct sk_buff *skb, __be32 net_seq)
2437{
2438 struct audit_buffer *audit_buf;
2439 u32 spi;
2440
2441 audit_buf = xfrm_audit_start("SA-replayed-pkt");
2442 if (audit_buf == NULL)
2443 return;
2444 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2445 spi = ntohl(x->id.spi);
2446 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2447 spi, spi, ntohl(net_seq));
2448 audit_log_end(audit_buf);
2449}
Steffen Klassert9fdc4882011-03-08 00:08:32 +00002450EXPORT_SYMBOL_GPL(xfrm_audit_state_replay);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002451
2452void xfrm_audit_state_notfound_simple(struct sk_buff *skb, u16 family)
2453{
2454 struct audit_buffer *audit_buf;
2455
2456 audit_buf = xfrm_audit_start("SA-notfound");
2457 if (audit_buf == NULL)
2458 return;
2459 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2460 audit_log_end(audit_buf);
2461}
2462EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound_simple);
2463
2464void xfrm_audit_state_notfound(struct sk_buff *skb, u16 family,
2465 __be32 net_spi, __be32 net_seq)
2466{
2467 struct audit_buffer *audit_buf;
2468 u32 spi;
2469
2470 audit_buf = xfrm_audit_start("SA-notfound");
2471 if (audit_buf == NULL)
2472 return;
2473 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2474 spi = ntohl(net_spi);
2475 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2476 spi, spi, ntohl(net_seq));
2477 audit_log_end(audit_buf);
2478}
2479EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound);
2480
2481void xfrm_audit_state_icvfail(struct xfrm_state *x,
2482 struct sk_buff *skb, u8 proto)
2483{
2484 struct audit_buffer *audit_buf;
2485 __be32 net_spi;
2486 __be32 net_seq;
2487
2488 audit_buf = xfrm_audit_start("SA-icv-failure");
2489 if (audit_buf == NULL)
2490 return;
2491 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2492 if (xfrm_parse_spi(skb, proto, &net_spi, &net_seq) == 0) {
2493 u32 spi = ntohl(net_spi);
2494 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2495 spi, spi, ntohl(net_seq));
2496 }
2497 audit_log_end(audit_buf);
2498}
2499EXPORT_SYMBOL_GPL(xfrm_audit_state_icvfail);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002500#endif /* CONFIG_AUDITSYSCALL */