blob: 71a94e549301aebb5578ec81f4b8068ce3359ae1 [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>
Jesper Juhlb5890d82007-08-10 15:20:21 -070023#include <asm/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{
51 return atomic_inc_not_zero(&x->refcnt);
52}
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);
195 xfrm_state_put_afinfo(afinfo);
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);
216 xfrm_state_put_afinfo(afinfo);
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
234 type = typemap[proto];
235 if (unlikely(type && !try_module_get(type->owner)))
236 type = NULL;
237 if (!type && !modload_attempted) {
238 xfrm_state_put_afinfo(afinfo);
239 request_module("xfrm-type-%d-%d", family, proto);
240 modload_attempted = 1;
241 goto retry;
242 }
243
244 xfrm_state_put_afinfo(afinfo);
245 return type;
246}
247
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800248static void xfrm_put_type(const struct xfrm_type *type)
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700249{
250 module_put(type->owner);
251}
252
Cong Wang7a9885b2013-01-17 16:34:11 +0800253static DEFINE_SPINLOCK(xfrm_mode_lock);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700254int xfrm_register_mode(struct xfrm_mode *mode, int family)
255{
256 struct xfrm_state_afinfo *afinfo;
257 struct xfrm_mode **modemap;
258 int err;
259
260 if (unlikely(mode->encap >= XFRM_MODE_MAX))
261 return -EINVAL;
262
Cong Wang7a9885b2013-01-17 16:34:11 +0800263 afinfo = xfrm_state_get_afinfo(family);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700264 if (unlikely(afinfo == NULL))
265 return -EAFNOSUPPORT;
266
267 err = -EEXIST;
268 modemap = afinfo->mode_map;
Cong Wang7a9885b2013-01-17 16:34:11 +0800269 spin_lock_bh(&xfrm_mode_lock);
Herbert Xu17c2a422007-10-17 21:33:12 -0700270 if (modemap[mode->encap])
271 goto out;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700272
Herbert Xu17c2a422007-10-17 21:33:12 -0700273 err = -ENOENT;
274 if (!try_module_get(afinfo->owner))
275 goto out;
276
277 mode->afinfo = afinfo;
278 modemap[mode->encap] = mode;
279 err = 0;
280
281out:
Cong Wang7a9885b2013-01-17 16:34:11 +0800282 spin_unlock_bh(&xfrm_mode_lock);
283 xfrm_state_put_afinfo(afinfo);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700284 return err;
285}
286EXPORT_SYMBOL(xfrm_register_mode);
287
288int xfrm_unregister_mode(struct xfrm_mode *mode, int family)
289{
290 struct xfrm_state_afinfo *afinfo;
291 struct xfrm_mode **modemap;
292 int err;
293
294 if (unlikely(mode->encap >= XFRM_MODE_MAX))
295 return -EINVAL;
296
Cong Wang7a9885b2013-01-17 16:34:11 +0800297 afinfo = xfrm_state_get_afinfo(family);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700298 if (unlikely(afinfo == NULL))
299 return -EAFNOSUPPORT;
300
301 err = -ENOENT;
302 modemap = afinfo->mode_map;
Cong Wang7a9885b2013-01-17 16:34:11 +0800303 spin_lock_bh(&xfrm_mode_lock);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700304 if (likely(modemap[mode->encap] == mode)) {
305 modemap[mode->encap] = NULL;
Herbert Xu17c2a422007-10-17 21:33:12 -0700306 module_put(mode->afinfo->owner);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700307 err = 0;
308 }
309
Cong Wang7a9885b2013-01-17 16:34:11 +0800310 spin_unlock_bh(&xfrm_mode_lock);
311 xfrm_state_put_afinfo(afinfo);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700312 return err;
313}
314EXPORT_SYMBOL(xfrm_unregister_mode);
315
316static struct xfrm_mode *xfrm_get_mode(unsigned int encap, int family)
317{
318 struct xfrm_state_afinfo *afinfo;
319 struct xfrm_mode *mode;
320 int modload_attempted = 0;
321
322 if (unlikely(encap >= XFRM_MODE_MAX))
323 return NULL;
324
325retry:
326 afinfo = xfrm_state_get_afinfo(family);
327 if (unlikely(afinfo == NULL))
328 return NULL;
329
330 mode = afinfo->mode_map[encap];
331 if (unlikely(mode && !try_module_get(mode->owner)))
332 mode = NULL;
333 if (!mode && !modload_attempted) {
334 xfrm_state_put_afinfo(afinfo);
335 request_module("xfrm-mode-%d-%d", family, encap);
336 modload_attempted = 1;
337 goto retry;
338 }
339
340 xfrm_state_put_afinfo(afinfo);
341 return mode;
342}
343
344static void xfrm_put_mode(struct xfrm_mode *mode)
345{
346 module_put(mode->owner);
347}
348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349static void xfrm_state_gc_destroy(struct xfrm_state *x)
350{
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800351 tasklet_hrtimer_cancel(&x->mtimer);
David S. Millera47f0ce2006-08-24 03:54:22 -0700352 del_timer_sync(&x->rtimer);
Ilan Tayarib5884792016-09-18 07:42:53 +0000353 kfree(x->aead);
Jesper Juhla51482b2005-11-08 09:41:34 -0800354 kfree(x->aalg);
355 kfree(x->ealg);
356 kfree(x->calg);
357 kfree(x->encap);
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700358 kfree(x->coaddr);
Steffen Klassertd8647b72011-03-08 00:10:27 +0000359 kfree(x->replay_esn);
360 kfree(x->preplay_esn);
Herbert Xu13996372007-10-17 21:35:51 -0700361 if (x->inner_mode)
362 xfrm_put_mode(x->inner_mode);
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700363 if (x->inner_mode_iaf)
364 xfrm_put_mode(x->inner_mode_iaf);
Herbert Xu13996372007-10-17 21:35:51 -0700365 if (x->outer_mode)
366 xfrm_put_mode(x->outer_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 if (x->type) {
368 x->type->destructor(x);
369 xfrm_put_type(x->type);
370 }
Trent Jaegerdf718372005-12-13 23:12:27 -0800371 security_xfrm_state_free(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 kfree(x);
373}
374
Alexey Dobriyanc7837142008-11-25 17:20:36 -0800375static void xfrm_state_gc_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
Herbert Xu12a169e2008-10-01 07:03:24 -0700377 struct xfrm_state *x;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800378 struct hlist_node *tmp;
Herbert Xu12a169e2008-10-01 07:03:24 -0700379 struct hlist_head gc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 spin_lock_bh(&xfrm_state_gc_lock);
Florian Westphal35db57bb2016-08-23 16:00:12 +0200382 hlist_move_list(&xfrm_state_gc_list, &gc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 spin_unlock_bh(&xfrm_state_gc_lock);
384
Florian Westphaldf7274e2016-08-09 12:16:06 +0200385 synchronize_rcu();
386
Sasha Levinb67bfe02013-02-27 17:06:00 -0800387 hlist_for_each_entry_safe(x, tmp, &gc_list, gclist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 xfrm_state_gc_destroy(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389}
390
391static inline unsigned long make_jiffies(long secs)
392{
393 if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
394 return MAX_SCHEDULE_TIMEOUT-1;
395 else
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900396 return secs*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397}
398
Weilong Chen3e94c2d2013-12-24 09:43:47 +0800399static enum hrtimer_restart xfrm_timer_handler(struct hrtimer *me)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800401 struct tasklet_hrtimer *thr = container_of(me, struct tasklet_hrtimer, timer);
402 struct xfrm_state *x = container_of(thr, struct xfrm_state, mtimer);
James Morris9d729f72007-03-04 16:12:44 -0800403 unsigned long now = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 long next = LONG_MAX;
405 int warn = 0;
Joy Latten161a09e2006-11-27 13:11:54 -0600406 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
408 spin_lock(&x->lock);
409 if (x->km.state == XFRM_STATE_DEAD)
410 goto out;
411 if (x->km.state == XFRM_STATE_EXPIRED)
412 goto expired;
413 if (x->lft.hard_add_expires_seconds) {
414 long tmo = x->lft.hard_add_expires_seconds +
415 x->curlft.add_time - now;
Fan Due3c0d042012-07-30 21:43:54 +0000416 if (tmo <= 0) {
417 if (x->xflags & XFRM_SOFT_EXPIRE) {
418 /* enter hard expire without soft expire first?!
419 * setting a new date could trigger this.
420 * workarbound: fix x->curflt.add_time by below:
421 */
422 x->curlft.add_time = now - x->saved_tmo - 1;
423 tmo = x->lft.hard_add_expires_seconds - x->saved_tmo;
424 } else
425 goto expired;
426 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 if (tmo < next)
428 next = tmo;
429 }
430 if (x->lft.hard_use_expires_seconds) {
431 long tmo = x->lft.hard_use_expires_seconds +
432 (x->curlft.use_time ? : now) - now;
433 if (tmo <= 0)
434 goto expired;
435 if (tmo < next)
436 next = tmo;
437 }
438 if (x->km.dying)
439 goto resched;
440 if (x->lft.soft_add_expires_seconds) {
441 long tmo = x->lft.soft_add_expires_seconds +
442 x->curlft.add_time - now;
Fan Due3c0d042012-07-30 21:43:54 +0000443 if (tmo <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 warn = 1;
Fan Due3c0d042012-07-30 21:43:54 +0000445 x->xflags &= ~XFRM_SOFT_EXPIRE;
446 } else if (tmo < next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 next = tmo;
Fan Due3c0d042012-07-30 21:43:54 +0000448 x->xflags |= XFRM_SOFT_EXPIRE;
449 x->saved_tmo = tmo;
450 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 }
452 if (x->lft.soft_use_expires_seconds) {
453 long tmo = x->lft.soft_use_expires_seconds +
454 (x->curlft.use_time ? : now) - now;
455 if (tmo <= 0)
456 warn = 1;
457 else if (tmo < next)
458 next = tmo;
459 }
460
Herbert Xu4666faa2005-06-18 22:43:22 -0700461 x->km.dying = warn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 if (warn)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -0800463 km_state_expired(x, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464resched:
Weilong Chen9b7a7872013-12-24 09:43:46 +0800465 if (next != LONG_MAX) {
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800466 tasklet_hrtimer_start(&x->mtimer, ktime_set(next, 0), HRTIMER_MODE_REL);
467 }
David S. Millera47f0ce2006-08-24 03:54:22 -0700468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 goto out;
470
471expired:
Steffen Klassert5b8ef342013-08-27 13:43:30 +0200472 if (x->km.state == XFRM_STATE_ACQ && x->id.spi == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 x->km.state = XFRM_STATE_EXPIRED;
Joy Latten161a09e2006-11-27 13:11:54 -0600474
475 err = __xfrm_state_delete(x);
Nicolas Dichtel0806ae42013-08-23 15:46:08 +0200476 if (!err)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -0800477 km_state_expired(x, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
Tetsuo Handa2e710292014-04-22 21:48:30 +0900479 xfrm_audit_state_delete(x, err ? 0 : 1, true);
Joy Latten161a09e2006-11-27 13:11:54 -0600480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481out:
482 spin_unlock(&x->lock);
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800483 return HRTIMER_NORESTART;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484}
485
David S. Miller0ac84752006-03-20 19:18:23 -0800486static void xfrm_replay_timer_handler(unsigned long data);
487
Alexey Dobriyan673c09b2008-11-25 17:15:16 -0800488struct xfrm_state *xfrm_state_alloc(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
490 struct xfrm_state *x;
491
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700492 x = kzalloc(sizeof(struct xfrm_state), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494 if (x) {
Alexey Dobriyan673c09b2008-11-25 17:15:16 -0800495 write_pnet(&x->xs_net, net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 atomic_set(&x->refcnt, 1);
497 atomic_set(&x->tunnel_users, 0);
Herbert Xu12a169e2008-10-01 07:03:24 -0700498 INIT_LIST_HEAD(&x->km.all);
David S. Miller8f126e32006-08-24 02:45:07 -0700499 INIT_HLIST_NODE(&x->bydst);
500 INIT_HLIST_NODE(&x->bysrc);
501 INIT_HLIST_NODE(&x->byspi);
Fan Du99565a6c2013-08-15 15:49:06 +0800502 tasklet_hrtimer_init(&x->mtimer, xfrm_timer_handler,
503 CLOCK_BOOTTIME, HRTIMER_MODE_ABS);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800504 setup_timer(&x->rtimer, xfrm_replay_timer_handler,
505 (unsigned long)x);
James Morris9d729f72007-03-04 16:12:44 -0800506 x->curlft.add_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 x->lft.soft_byte_limit = XFRM_INF;
508 x->lft.soft_packet_limit = XFRM_INF;
509 x->lft.hard_byte_limit = XFRM_INF;
510 x->lft.hard_packet_limit = XFRM_INF;
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -0800511 x->replay_maxage = 0;
512 x->replay_maxdiff = 0;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700513 x->inner_mode = NULL;
514 x->inner_mode_iaf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 spin_lock_init(&x->lock);
516 }
517 return x;
518}
519EXPORT_SYMBOL(xfrm_state_alloc);
520
521void __xfrm_state_destroy(struct xfrm_state *x)
522{
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700523 WARN_ON(x->km.state != XFRM_STATE_DEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
525 spin_lock_bh(&xfrm_state_gc_lock);
Florian Westphal35db57bb2016-08-23 16:00:12 +0200526 hlist_add_head(&x->gclist, &xfrm_state_gc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 spin_unlock_bh(&xfrm_state_gc_lock);
Florian Westphal35db57bb2016-08-23 16:00:12 +0200528 schedule_work(&xfrm_state_gc_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529}
530EXPORT_SYMBOL(__xfrm_state_destroy);
531
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -0800532int __xfrm_state_delete(struct xfrm_state *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533{
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800534 struct net *net = xs_net(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700535 int err = -ESRCH;
536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 if (x->km.state != XFRM_STATE_DEAD) {
538 x->km.state = XFRM_STATE_DEAD;
Fan Du283bc9f2013-11-07 17:47:50 +0800539 spin_lock(&net->xfrm.xfrm_state_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -0700540 list_del(&x->km.all);
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200541 hlist_del_rcu(&x->bydst);
542 hlist_del_rcu(&x->bysrc);
David S. Millera47f0ce2006-08-24 03:54:22 -0700543 if (x->id.spi)
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200544 hlist_del_rcu(&x->byspi);
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800545 net->xfrm.state_num--;
Fan Du283bc9f2013-11-07 17:47:50 +0800546 spin_unlock(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 /* All xfrm_state objects are created by xfrm_state_alloc.
549 * The xfrm_state_alloc call gives a reference, and that
550 * is what we are dropping here.
551 */
Patrick McHardy5dba4792007-11-27 11:10:07 +0800552 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700553 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700555
556 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557}
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -0800558EXPORT_SYMBOL(__xfrm_state_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700560int xfrm_state_delete(struct xfrm_state *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700562 int err;
563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 spin_lock_bh(&x->lock);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700565 err = __xfrm_state_delete(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 spin_unlock_bh(&x->lock);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700567
568 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569}
570EXPORT_SYMBOL(xfrm_state_delete);
571
Joy Latten4aa2e622007-06-04 19:05:57 -0400572#ifdef CONFIG_SECURITY_NETWORK_XFRM
573static inline int
Tetsuo Handa2e710292014-04-22 21:48:30 +0900574xfrm_state_flush_secctx_check(struct net *net, u8 proto, bool task_valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575{
Joy Latten4aa2e622007-06-04 19:05:57 -0400576 int i, err = 0;
577
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800578 for (i = 0; i <= net->xfrm.state_hmask; i++) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400579 struct xfrm_state *x;
580
Sasha Levinb67bfe02013-02-27 17:06:00 -0800581 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400582 if (xfrm_id_proto_match(x->id.proto, proto) &&
583 (err = security_xfrm_state_delete(x)) != 0) {
Tetsuo Handa2e710292014-04-22 21:48:30 +0900584 xfrm_audit_state_delete(x, 0, task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400585 return err;
586 }
587 }
588 }
589
590 return err;
591}
592#else
593static inline int
Tetsuo Handa2e710292014-04-22 21:48:30 +0900594xfrm_state_flush_secctx_check(struct net *net, u8 proto, bool task_valid)
Joy Latten4aa2e622007-06-04 19:05:57 -0400595{
596 return 0;
597}
598#endif
599
Tetsuo Handa2e710292014-04-22 21:48:30 +0900600int xfrm_state_flush(struct net *net, u8 proto, bool task_valid)
Joy Latten4aa2e622007-06-04 19:05:57 -0400601{
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000602 int i, err = 0, cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Fan Du283bc9f2013-11-07 17:47:50 +0800604 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Tetsuo Handa2e710292014-04-22 21:48:30 +0900605 err = xfrm_state_flush_secctx_check(net, proto, task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400606 if (err)
607 goto out;
608
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000609 err = -ESRCH;
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800610 for (i = 0; i <= net->xfrm.state_hmask; i++) {
David S. Miller8f126e32006-08-24 02:45:07 -0700611 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612restart:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800613 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 if (!xfrm_state_kern(x) &&
Masahide NAKAMURA57947082006-09-22 15:06:24 -0700615 xfrm_id_proto_match(x->id.proto, proto)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 xfrm_state_hold(x);
Fan Du283bc9f2013-11-07 17:47:50 +0800617 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Joy Latten161a09e2006-11-27 13:11:54 -0600619 err = xfrm_state_delete(x);
Joy Lattenab5f5e82007-09-17 11:51:22 -0700620 xfrm_audit_state_delete(x, err ? 0 : 1,
Tetsuo Handa2e710292014-04-22 21:48:30 +0900621 task_valid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 xfrm_state_put(x);
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000623 if (!err)
624 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Fan Du283bc9f2013-11-07 17:47:50 +0800626 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 goto restart;
628 }
629 }
630 }
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000631 if (cnt)
632 err = 0;
Joy Latten4aa2e622007-06-04 19:05:57 -0400633
634out:
Fan Du283bc9f2013-11-07 17:47:50 +0800635 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -0400636 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637}
638EXPORT_SYMBOL(xfrm_state_flush);
639
Alexey Dobriyane0710412010-01-23 13:37:10 +0000640void xfrm_sad_getinfo(struct net *net, struct xfrmk_sadinfo *si)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700641{
Fan Du283bc9f2013-11-07 17:47:50 +0800642 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Alexey Dobriyane0710412010-01-23 13:37:10 +0000643 si->sadcnt = net->xfrm.state_num;
644 si->sadhcnt = net->xfrm.state_hmask;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700645 si->sadhmcnt = xfrm_state_hashmax;
Fan Du283bc9f2013-11-07 17:47:50 +0800646 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700647}
648EXPORT_SYMBOL(xfrm_sad_getinfo);
649
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650static int
David S. Miller1a898592011-02-22 18:22:34 -0800651xfrm_init_tempstate(struct xfrm_state *x, const struct flowi *fl,
David S. Miller04686012011-02-24 01:50:12 -0500652 const struct xfrm_tmpl *tmpl,
David S. Miller33765d02011-02-24 01:55:45 -0500653 const xfrm_address_t *daddr, const xfrm_address_t *saddr,
Thomas Egerer8444cf72010-09-20 11:11:38 -0700654 unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655{
656 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
657 if (!afinfo)
658 return -1;
Thomas Egerer8444cf72010-09-20 11:11:38 -0700659 afinfo->init_tempsel(&x->sel, fl);
660
661 if (family != tmpl->encap_family) {
662 xfrm_state_put_afinfo(afinfo);
663 afinfo = xfrm_state_get_afinfo(tmpl->encap_family);
664 if (!afinfo)
665 return -1;
666 }
667 afinfo->init_temprop(x, tmpl, daddr, saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 xfrm_state_put_afinfo(afinfo);
669 return 0;
670}
671
David S. Miller9aa60082011-02-24 01:51:36 -0500672static struct xfrm_state *__xfrm_state_lookup(struct net *net, u32 mark,
673 const xfrm_address_t *daddr,
674 __be32 spi, u8 proto,
675 unsigned short family)
David S. Milleredcd5822006-08-24 00:42:45 -0700676{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800677 unsigned int h = xfrm_spi_hash(net, daddr, spi, proto, family);
David S. Milleredcd5822006-08-24 00:42:45 -0700678 struct xfrm_state *x;
679
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200680 hlist_for_each_entry_rcu(x, net->xfrm.state_byspi + h, byspi) {
David S. Milleredcd5822006-08-24 00:42:45 -0700681 if (x->props.family != family ||
682 x->id.spi != spi ||
Wei Yongjun18025712009-06-28 18:42:53 +0000683 x->id.proto != proto ||
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +0000684 !xfrm_addr_equal(&x->id.daddr, daddr, family))
David S. Milleredcd5822006-08-24 00:42:45 -0700685 continue;
686
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000687 if ((mark & x->mark.m) != x->mark.v)
688 continue;
Florian Westphal02efdff2016-08-09 12:16:05 +0200689 if (!xfrm_state_hold_rcu(x))
690 continue;
David S. Milleredcd5822006-08-24 00:42:45 -0700691 return x;
692 }
693
694 return NULL;
695}
696
David S. Miller9aa60082011-02-24 01:51:36 -0500697static struct xfrm_state *__xfrm_state_lookup_byaddr(struct net *net, u32 mark,
698 const xfrm_address_t *daddr,
699 const xfrm_address_t *saddr,
700 u8 proto, unsigned short family)
David S. Milleredcd5822006-08-24 00:42:45 -0700701{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800702 unsigned int h = xfrm_src_hash(net, daddr, saddr, family);
David S. Milleredcd5822006-08-24 00:42:45 -0700703 struct xfrm_state *x;
704
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200705 hlist_for_each_entry_rcu(x, net->xfrm.state_bysrc + h, bysrc) {
David S. Milleredcd5822006-08-24 00:42:45 -0700706 if (x->props.family != family ||
Wei Yongjun18025712009-06-28 18:42:53 +0000707 x->id.proto != proto ||
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +0000708 !xfrm_addr_equal(&x->id.daddr, daddr, family) ||
709 !xfrm_addr_equal(&x->props.saddr, saddr, family))
David S. Milleredcd5822006-08-24 00:42:45 -0700710 continue;
711
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000712 if ((mark & x->mark.m) != x->mark.v)
713 continue;
Florian Westphal02efdff2016-08-09 12:16:05 +0200714 if (!xfrm_state_hold_rcu(x))
715 continue;
David S. Milleredcd5822006-08-24 00:42:45 -0700716 return x;
717 }
718
719 return NULL;
720}
721
722static inline struct xfrm_state *
723__xfrm_state_locate(struct xfrm_state *x, int use_spi, int family)
724{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800725 struct net *net = xs_net(x);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800726 u32 mark = x->mark.v & x->mark.m;
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800727
David S. Milleredcd5822006-08-24 00:42:45 -0700728 if (use_spi)
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800729 return __xfrm_state_lookup(net, mark, &x->id.daddr,
730 x->id.spi, x->id.proto, family);
David S. Milleredcd5822006-08-24 00:42:45 -0700731 else
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800732 return __xfrm_state_lookup_byaddr(net, mark,
733 &x->id.daddr,
David S. Milleredcd5822006-08-24 00:42:45 -0700734 &x->props.saddr,
735 x->id.proto, family);
736}
737
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800738static void xfrm_hash_grow_check(struct net *net, int have_hash_collision)
Patrick McHardy2fab22f2006-10-24 15:34:00 -0700739{
740 if (have_hash_collision &&
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800741 (net->xfrm.state_hmask + 1) < xfrm_state_hashmax &&
742 net->xfrm.state_num > net->xfrm.state_hmask)
743 schedule_work(&net->xfrm.state_hash_work);
Patrick McHardy2fab22f2006-10-24 15:34:00 -0700744}
745
David S. Miller08ec9af2009-03-13 14:22:40 -0700746static void xfrm_state_look_at(struct xfrm_policy *pol, struct xfrm_state *x,
David S. Miller4a08ab02011-02-22 18:21:31 -0800747 const struct flowi *fl, unsigned short family,
David S. Miller08ec9af2009-03-13 14:22:40 -0700748 struct xfrm_state **best, int *acq_in_progress,
749 int *error)
750{
751 /* Resolution logic:
752 * 1. There is a valid state with matching selector. Done.
753 * 2. Valid state with inappropriate selector. Skip.
754 *
755 * Entering area of "sysdeps".
756 *
757 * 3. If state is not valid, selector is temporary, it selects
758 * only session which triggered previous resolution. Key
759 * manager will do something to install a state with proper
760 * selector.
761 */
762 if (x->km.state == XFRM_STATE_VALID) {
763 if ((x->sel.family &&
764 !xfrm_selector_match(&x->sel, fl, x->sel.family)) ||
765 !security_xfrm_state_pol_flow_match(x, pol, fl))
766 return;
767
768 if (!*best ||
769 (*best)->km.dying > x->km.dying ||
770 ((*best)->km.dying == x->km.dying &&
771 (*best)->curlft.add_time < x->curlft.add_time))
772 *best = x;
773 } else if (x->km.state == XFRM_STATE_ACQ) {
774 *acq_in_progress = 1;
775 } else if (x->km.state == XFRM_STATE_ERROR ||
776 x->km.state == XFRM_STATE_EXPIRED) {
777 if (xfrm_selector_match(&x->sel, fl, x->sel.family) &&
778 security_xfrm_state_pol_flow_match(x, pol, fl))
779 *error = -ESRCH;
780 }
781}
782
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783struct xfrm_state *
David S. Miller33765d02011-02-24 01:55:45 -0500784xfrm_state_find(const xfrm_address_t *daddr, const xfrm_address_t *saddr,
David S. Millerb520e9f2011-02-22 18:24:19 -0800785 const struct flowi *fl, struct xfrm_tmpl *tmpl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 struct xfrm_policy *pol, int *err,
787 unsigned short family)
788{
David S. Miller08ec9af2009-03-13 14:22:40 -0700789 static xfrm_address_t saddr_wildcard = { };
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800790 struct net *net = xp_net(pol);
Nicolas Dichtel6a783c92009-04-27 02:58:59 -0700791 unsigned int h, h_wildcard;
David S. Miller37b08e32008-09-02 20:14:15 -0700792 struct xfrm_state *x, *x0, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 int acquire_in_progress = 0;
794 int error = 0;
795 struct xfrm_state *best = NULL;
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800796 u32 mark = pol->mark.v & pol->mark.m;
Thomas Egerer8444cf72010-09-20 11:11:38 -0700797 unsigned short encap_family = tmpl->encap_family;
Florian Westphalb65e3d72016-08-09 12:16:07 +0200798 unsigned int sequence;
Horia Geanta0f245582014-02-12 16:20:06 +0200799 struct km_event c;
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900800
David S. Miller37b08e32008-09-02 20:14:15 -0700801 to_put = NULL;
802
Florian Westphalb65e3d72016-08-09 12:16:07 +0200803 sequence = read_seqcount_begin(&xfrm_state_hash_generation);
804
Florian Westphald737a582016-08-09 12:16:09 +0200805 rcu_read_lock();
Thomas Egerer8444cf72010-09-20 11:11:38 -0700806 h = xfrm_dst_hash(net, daddr, saddr, tmpl->reqid, encap_family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200807 hlist_for_each_entry_rcu(x, net->xfrm.state_bydst + h, bydst) {
Thomas Egerer8444cf72010-09-20 11:11:38 -0700808 if (x->props.family == encap_family &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 x->props.reqid == tmpl->reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000810 (mark & x->mark.m) == x->mark.v &&
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -0700811 !(x->props.flags & XFRM_STATE_WILDRECV) &&
Thomas Egerer8444cf72010-09-20 11:11:38 -0700812 xfrm_state_addr_check(x, daddr, saddr, encap_family) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 tmpl->mode == x->props.mode &&
814 tmpl->id.proto == x->id.proto &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700815 (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
David S. Miller1f673c52011-02-24 01:53:13 -0500816 xfrm_state_look_at(pol, x, fl, encap_family,
David S. Miller08ec9af2009-03-13 14:22:40 -0700817 &best, &acquire_in_progress, &error);
818 }
Fan Du6f115632013-09-23 17:18:25 +0800819 if (best || acquire_in_progress)
David S. Miller08ec9af2009-03-13 14:22:40 -0700820 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Thomas Egerer8444cf72010-09-20 11:11:38 -0700822 h_wildcard = xfrm_dst_hash(net, daddr, &saddr_wildcard, tmpl->reqid, encap_family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200823 hlist_for_each_entry_rcu(x, net->xfrm.state_bydst + h_wildcard, bydst) {
Thomas Egerer8444cf72010-09-20 11:11:38 -0700824 if (x->props.family == encap_family &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700825 x->props.reqid == tmpl->reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000826 (mark & x->mark.m) == x->mark.v &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700827 !(x->props.flags & XFRM_STATE_WILDRECV) &&
Fan Duf59bbdf2013-09-27 16:32:50 +0800828 xfrm_addr_equal(&x->id.daddr, daddr, encap_family) &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700829 tmpl->mode == x->props.mode &&
830 tmpl->id.proto == x->id.proto &&
831 (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
David S. Miller1f673c52011-02-24 01:53:13 -0500832 xfrm_state_look_at(pol, x, fl, encap_family,
David S. Miller08ec9af2009-03-13 14:22:40 -0700833 &best, &acquire_in_progress, &error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 }
835
David S. Miller08ec9af2009-03-13 14:22:40 -0700836found:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 x = best;
838 if (!x && !error && !acquire_in_progress) {
Patrick McHardy5c5d2812005-04-21 20:12:32 -0700839 if (tmpl->id.spi &&
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800840 (x0 = __xfrm_state_lookup(net, mark, daddr, tmpl->id.spi,
Thomas Egerer8444cf72010-09-20 11:11:38 -0700841 tmpl->id.proto, encap_family)) != NULL) {
David S. Miller37b08e32008-09-02 20:14:15 -0700842 to_put = x0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 error = -EEXIST;
844 goto out;
845 }
Horia Geanta0f245582014-02-12 16:20:06 +0200846
847 c.net = net;
848 /* If the KMs have no listeners (yet...), avoid allocating an SA
849 * for each and every packet - garbage collection might not
850 * handle the flood.
851 */
852 if (!km_is_alive(&c)) {
853 error = -ESRCH;
854 goto out;
855 }
856
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800857 x = xfrm_state_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 if (x == NULL) {
859 error = -ENOMEM;
860 goto out;
861 }
Thomas Egerer8444cf72010-09-20 11:11:38 -0700862 /* Initialize temporary state matching only
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 * to current session. */
Thomas Egerer8444cf72010-09-20 11:11:38 -0700864 xfrm_init_tempstate(x, fl, tmpl, daddr, saddr, family);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800865 memcpy(&x->mark, &pol->mark, sizeof(x->mark));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
David S. Miller1d28f422011-03-12 00:29:39 -0500867 error = security_xfrm_state_alloc_acquire(x, pol->security, fl->flowi_secid);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -0700868 if (error) {
869 x->km.state = XFRM_STATE_DEAD;
David S. Miller37b08e32008-09-02 20:14:15 -0700870 to_put = x;
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -0700871 x = NULL;
872 goto out;
873 }
874
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 if (km_query(x, tmpl, pol) == 0) {
Florian Westphald737a582016-08-09 12:16:09 +0200876 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 x->km.state = XFRM_STATE_ACQ;
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800878 list_add(&x->km.all, &net->xfrm.state_all);
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200879 hlist_add_head_rcu(&x->bydst, net->xfrm.state_bydst + h);
Thomas Egerer8444cf72010-09-20 11:11:38 -0700880 h = xfrm_src_hash(net, daddr, saddr, encap_family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200881 hlist_add_head_rcu(&x->bysrc, net->xfrm.state_bysrc + h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 if (x->id.spi) {
Thomas Egerer8444cf72010-09-20 11:11:38 -0700883 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, encap_family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200884 hlist_add_head_rcu(&x->byspi, net->xfrm.state_byspi + h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 }
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800886 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800887 tasklet_hrtimer_start(&x->mtimer, ktime_set(net->xfrm.sysctl_acq_expires, 0), HRTIMER_MODE_REL);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800888 net->xfrm.state_num++;
889 xfrm_hash_grow_check(net, x->bydst.next != NULL);
Florian Westphald737a582016-08-09 12:16:09 +0200890 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 } else {
892 x->km.state = XFRM_STATE_DEAD;
David S. Miller37b08e32008-09-02 20:14:15 -0700893 to_put = x;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 x = NULL;
895 error = -ESRCH;
896 }
897 }
898out:
Florian Westphal02efdff2016-08-09 12:16:05 +0200899 if (x) {
900 if (!xfrm_state_hold_rcu(x)) {
901 *err = -EAGAIN;
902 x = NULL;
903 }
904 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 *err = acquire_in_progress ? -EAGAIN : error;
Florian Westphal02efdff2016-08-09 12:16:05 +0200906 }
Florian Westphald737a582016-08-09 12:16:09 +0200907 rcu_read_unlock();
David S. Miller37b08e32008-09-02 20:14:15 -0700908 if (to_put)
909 xfrm_state_put(to_put);
Florian Westphalb65e3d72016-08-09 12:16:07 +0200910
911 if (read_seqcount_retry(&xfrm_state_hash_generation, sequence)) {
912 *err = -EAGAIN;
913 if (x) {
914 xfrm_state_put(x);
915 x = NULL;
916 }
917 }
918
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 return x;
920}
921
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700922struct xfrm_state *
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800923xfrm_stateonly_find(struct net *net, u32 mark,
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800924 xfrm_address_t *daddr, xfrm_address_t *saddr,
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700925 unsigned short family, u8 mode, u8 proto, u32 reqid)
926{
Pavel Emelyanov4bda4f22007-12-14 11:38:04 -0800927 unsigned int h;
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700928 struct xfrm_state *rx = NULL, *x = NULL;
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700929
Fan Du4ae770b2014-01-03 11:18:29 +0800930 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800931 h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800932 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700933 if (x->props.family == family &&
934 x->props.reqid == reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000935 (mark & x->mark.m) == x->mark.v &&
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700936 !(x->props.flags & XFRM_STATE_WILDRECV) &&
937 xfrm_state_addr_check(x, daddr, saddr, family) &&
938 mode == x->props.mode &&
939 proto == x->id.proto &&
940 x->km.state == XFRM_STATE_VALID) {
941 rx = x;
942 break;
943 }
944 }
945
946 if (rx)
947 xfrm_state_hold(rx);
Fan Du4ae770b2014-01-03 11:18:29 +0800948 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700949
950
951 return rx;
952}
953EXPORT_SYMBOL(xfrm_stateonly_find);
954
Fan Duc4549972014-01-03 11:18:32 +0800955struct xfrm_state *xfrm_state_lookup_byspi(struct net *net, __be32 spi,
956 unsigned short family)
957{
958 struct xfrm_state *x;
959 struct xfrm_state_walk *w;
960
961 spin_lock_bh(&net->xfrm.xfrm_state_lock);
962 list_for_each_entry(w, &net->xfrm.state_all, all) {
963 x = container_of(w, struct xfrm_state, km);
964 if (x->props.family != family ||
965 x->id.spi != spi)
966 continue;
967
Fan Duc4549972014-01-03 11:18:32 +0800968 xfrm_state_hold(x);
Li RongQingbdddbf62015-04-29 08:42:44 +0800969 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Fan Duc4549972014-01-03 11:18:32 +0800970 return x;
971 }
972 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
973 return NULL;
974}
975EXPORT_SYMBOL(xfrm_state_lookup_byspi);
976
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977static void __xfrm_state_insert(struct xfrm_state *x)
978{
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800979 struct net *net = xs_net(x);
David S. Millera624c102006-08-24 03:24:33 -0700980 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800982 list_add(&x->km.all, &net->xfrm.state_all);
Timo Teras4c563f72008-02-28 21:31:08 -0800983
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800984 h = xfrm_dst_hash(net, &x->id.daddr, &x->props.saddr,
David S. Millerc1969f22006-08-24 04:00:03 -0700985 x->props.reqid, x->props.family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200986 hlist_add_head_rcu(&x->bydst, net->xfrm.state_bydst + h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800988 h = xfrm_src_hash(net, &x->id.daddr, &x->props.saddr, x->props.family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200989 hlist_add_head_rcu(&x->bysrc, net->xfrm.state_bysrc + h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
Masahide NAKAMURA7b4dc3602006-09-27 22:21:52 -0700991 if (x->id.spi) {
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800992 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto,
Masahide NAKAMURA6c44e6b2006-08-23 17:53:57 -0700993 x->props.family);
994
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200995 hlist_add_head_rcu(&x->byspi, net->xfrm.state_byspi + h);
Masahide NAKAMURA6c44e6b2006-08-23 17:53:57 -0700996 }
997
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800998 tasklet_hrtimer_start(&x->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL);
David S. Millera47f0ce2006-08-24 03:54:22 -0700999 if (x->replay_maxage)
1000 mod_timer(&x->rtimer, jiffies + x->replay_maxage);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001001
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001002 net->xfrm.state_num++;
David S. Millerf034b5d2006-08-24 03:08:07 -07001003
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001004 xfrm_hash_grow_check(net, x->bydst.next != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005}
1006
Fan Du283bc9f2013-11-07 17:47:50 +08001007/* net->xfrm.xfrm_state_lock is held */
David S. Millerc7f5ea32006-08-24 03:29:04 -07001008static void __xfrm_state_bump_genids(struct xfrm_state *xnew)
1009{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001010 struct net *net = xs_net(xnew);
David S. Millerc7f5ea32006-08-24 03:29:04 -07001011 unsigned short family = xnew->props.family;
1012 u32 reqid = xnew->props.reqid;
1013 struct xfrm_state *x;
David S. Millerc7f5ea32006-08-24 03:29:04 -07001014 unsigned int h;
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001015 u32 mark = xnew->mark.v & xnew->mark.m;
David S. Millerc7f5ea32006-08-24 03:29:04 -07001016
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001017 h = xfrm_dst_hash(net, &xnew->id.daddr, &xnew->props.saddr, reqid, family);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001018 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
David S. Millerc7f5ea32006-08-24 03:29:04 -07001019 if (x->props.family == family &&
1020 x->props.reqid == reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001021 (mark & x->mark.m) == x->mark.v &&
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001022 xfrm_addr_equal(&x->id.daddr, &xnew->id.daddr, family) &&
1023 xfrm_addr_equal(&x->props.saddr, &xnew->props.saddr, family))
Herbert Xu34996cb2010-03-31 01:19:49 +00001024 x->genid++;
David S. Millerc7f5ea32006-08-24 03:29:04 -07001025 }
1026}
1027
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028void xfrm_state_insert(struct xfrm_state *x)
1029{
Fan Du283bc9f2013-11-07 17:47:50 +08001030 struct net *net = xs_net(x);
1031
1032 spin_lock_bh(&net->xfrm.xfrm_state_lock);
David S. Millerc7f5ea32006-08-24 03:29:04 -07001033 __xfrm_state_bump_genids(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 __xfrm_state_insert(x);
Fan Du283bc9f2013-11-07 17:47:50 +08001035 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036}
1037EXPORT_SYMBOL(xfrm_state_insert);
1038
Fan Du283bc9f2013-11-07 17:47:50 +08001039/* net->xfrm.xfrm_state_lock is held */
Mathias Krausee473fcb2013-06-26 23:56:58 +02001040static struct xfrm_state *__find_acq_core(struct net *net,
1041 const struct xfrm_mark *m,
David S. Millera70486f2011-02-27 23:17:24 -08001042 unsigned short family, u8 mode,
1043 u32 reqid, u8 proto,
1044 const xfrm_address_t *daddr,
Mathias Krausee473fcb2013-06-26 23:56:58 +02001045 const xfrm_address_t *saddr,
1046 int create)
David S. Miller27708342006-08-24 00:13:10 -07001047{
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001048 unsigned int h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
David S. Miller27708342006-08-24 00:13:10 -07001049 struct xfrm_state *x;
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001050 u32 mark = m->v & m->m;
David S. Miller27708342006-08-24 00:13:10 -07001051
Sasha Levinb67bfe02013-02-27 17:06:00 -08001052 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
David S. Miller27708342006-08-24 00:13:10 -07001053 if (x->props.reqid != reqid ||
1054 x->props.mode != mode ||
1055 x->props.family != family ||
1056 x->km.state != XFRM_STATE_ACQ ||
Joy Latten75e252d2007-03-12 17:14:07 -07001057 x->id.spi != 0 ||
Wei Yongjun18025712009-06-28 18:42:53 +00001058 x->id.proto != proto ||
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001059 (mark & x->mark.m) != x->mark.v ||
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001060 !xfrm_addr_equal(&x->id.daddr, daddr, family) ||
1061 !xfrm_addr_equal(&x->props.saddr, saddr, family))
David S. Miller27708342006-08-24 00:13:10 -07001062 continue;
1063
David S. Miller27708342006-08-24 00:13:10 -07001064 xfrm_state_hold(x);
1065 return x;
1066 }
1067
1068 if (!create)
1069 return NULL;
1070
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001071 x = xfrm_state_alloc(net);
David S. Miller27708342006-08-24 00:13:10 -07001072 if (likely(x)) {
1073 switch (family) {
1074 case AF_INET:
1075 x->sel.daddr.a4 = daddr->a4;
1076 x->sel.saddr.a4 = saddr->a4;
1077 x->sel.prefixlen_d = 32;
1078 x->sel.prefixlen_s = 32;
1079 x->props.saddr.a4 = saddr->a4;
1080 x->id.daddr.a4 = daddr->a4;
1081 break;
1082
1083 case AF_INET6:
Jiri Benc15e318b2015-03-29 16:59:24 +02001084 x->sel.daddr.in6 = daddr->in6;
1085 x->sel.saddr.in6 = saddr->in6;
David S. Miller27708342006-08-24 00:13:10 -07001086 x->sel.prefixlen_d = 128;
1087 x->sel.prefixlen_s = 128;
Jiri Benc15e318b2015-03-29 16:59:24 +02001088 x->props.saddr.in6 = saddr->in6;
1089 x->id.daddr.in6 = daddr->in6;
David S. Miller27708342006-08-24 00:13:10 -07001090 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001091 }
David S. Miller27708342006-08-24 00:13:10 -07001092
1093 x->km.state = XFRM_STATE_ACQ;
1094 x->id.proto = proto;
1095 x->props.family = family;
1096 x->props.mode = mode;
1097 x->props.reqid = reqid;
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001098 x->mark.v = m->v;
1099 x->mark.m = m->m;
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08001100 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
David S. Miller27708342006-08-24 00:13:10 -07001101 xfrm_state_hold(x);
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -08001102 tasklet_hrtimer_start(&x->mtimer, ktime_set(net->xfrm.sysctl_acq_expires, 0), HRTIMER_MODE_REL);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001103 list_add(&x->km.all, &net->xfrm.state_all);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001104 hlist_add_head_rcu(&x->bydst, net->xfrm.state_bydst + h);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001105 h = xfrm_src_hash(net, daddr, saddr, family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001106 hlist_add_head_rcu(&x->bysrc, net->xfrm.state_bysrc + h);
David S. Miller918049f2006-10-12 22:03:24 -07001107
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001108 net->xfrm.state_num++;
David S. Miller918049f2006-10-12 22:03:24 -07001109
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001110 xfrm_hash_grow_check(net, x->bydst.next != NULL);
David S. Miller27708342006-08-24 00:13:10 -07001111 }
1112
1113 return x;
1114}
1115
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001116static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
1118int xfrm_state_add(struct xfrm_state *x)
1119{
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001120 struct net *net = xs_net(x);
David S. Miller37b08e32008-09-02 20:14:15 -07001121 struct xfrm_state *x1, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 int family;
1123 int err;
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001124 u32 mark = x->mark.v & x->mark.m;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001125 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
1127 family = x->props.family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
David S. Miller37b08e32008-09-02 20:14:15 -07001129 to_put = NULL;
1130
Fan Du283bc9f2013-11-07 17:47:50 +08001131 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
David S. Milleredcd5822006-08-24 00:42:45 -07001133 x1 = __xfrm_state_locate(x, use_spi, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 if (x1) {
David S. Miller37b08e32008-09-02 20:14:15 -07001135 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 x1 = NULL;
1137 err = -EEXIST;
1138 goto out;
1139 }
1140
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001141 if (use_spi && x->km.seq) {
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001142 x1 = __xfrm_find_acq_byseq(net, mark, x->km.seq);
Joy Latten75e252d2007-03-12 17:14:07 -07001143 if (x1 && ((x1->id.proto != x->id.proto) ||
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001144 !xfrm_addr_equal(&x1->id.daddr, &x->id.daddr, family))) {
David S. Miller37b08e32008-09-02 20:14:15 -07001145 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 x1 = NULL;
1147 }
1148 }
1149
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001150 if (use_spi && !x1)
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001151 x1 = __find_acq_core(net, &x->mark, family, x->props.mode,
1152 x->props.reqid, x->id.proto,
David S. Miller27708342006-08-24 00:13:10 -07001153 &x->id.daddr, &x->props.saddr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
David S. Millerc7f5ea32006-08-24 03:29:04 -07001155 __xfrm_state_bump_genids(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 __xfrm_state_insert(x);
1157 err = 0;
1158
1159out:
Fan Du283bc9f2013-11-07 17:47:50 +08001160 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
1162 if (x1) {
1163 xfrm_state_delete(x1);
1164 xfrm_state_put(x1);
1165 }
1166
David S. Miller37b08e32008-09-02 20:14:15 -07001167 if (to_put)
1168 xfrm_state_put(to_put);
1169
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 return err;
1171}
1172EXPORT_SYMBOL(xfrm_state_add);
1173
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001174#ifdef CONFIG_XFRM_MIGRATE
Steffen Klassertcc9ab602014-02-19 13:33:24 +01001175static struct xfrm_state *xfrm_state_clone(struct xfrm_state *orig)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001176{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001177 struct net *net = xs_net(orig);
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001178 struct xfrm_state *x = xfrm_state_alloc(net);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001179 if (!x)
Herbert Xu553f9112010-02-15 20:00:51 +00001180 goto out;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001181
1182 memcpy(&x->id, &orig->id, sizeof(x->id));
1183 memcpy(&x->sel, &orig->sel, sizeof(x->sel));
1184 memcpy(&x->lft, &orig->lft, sizeof(x->lft));
1185 x->props.mode = orig->props.mode;
1186 x->props.replay_window = orig->props.replay_window;
1187 x->props.reqid = orig->props.reqid;
1188 x->props.family = orig->props.family;
1189 x->props.saddr = orig->props.saddr;
1190
1191 if (orig->aalg) {
Martin Willi4447bb32009-11-25 00:29:52 +00001192 x->aalg = xfrm_algo_auth_clone(orig->aalg);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001193 if (!x->aalg)
1194 goto error;
1195 }
1196 x->props.aalgo = orig->props.aalgo;
1197
Steffen Klassertee5c2312014-02-19 13:33:24 +01001198 if (orig->aead) {
1199 x->aead = xfrm_algo_aead_clone(orig->aead);
Antony Antony3e54e1c2017-12-07 21:54:27 +01001200 x->geniv = orig->geniv;
Steffen Klassertee5c2312014-02-19 13:33:24 +01001201 if (!x->aead)
1202 goto error;
1203 }
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001204 if (orig->ealg) {
1205 x->ealg = xfrm_algo_clone(orig->ealg);
1206 if (!x->ealg)
1207 goto error;
1208 }
1209 x->props.ealgo = orig->props.ealgo;
1210
1211 if (orig->calg) {
1212 x->calg = xfrm_algo_clone(orig->calg);
1213 if (!x->calg)
1214 goto error;
1215 }
1216 x->props.calgo = orig->props.calgo;
1217
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001218 if (orig->encap) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001219 x->encap = kmemdup(orig->encap, sizeof(*x->encap), GFP_KERNEL);
1220 if (!x->encap)
1221 goto error;
1222 }
1223
1224 if (orig->coaddr) {
1225 x->coaddr = kmemdup(orig->coaddr, sizeof(*x->coaddr),
1226 GFP_KERNEL);
1227 if (!x->coaddr)
1228 goto error;
1229 }
1230
Steffen Klassertaf2f4642011-03-28 19:46:39 +00001231 if (orig->replay_esn) {
Steffen Klassertcc9ab602014-02-19 13:33:24 +01001232 if (xfrm_replay_clone(x, orig))
Steffen Klassertaf2f4642011-03-28 19:46:39 +00001233 goto error;
1234 }
1235
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001236 memcpy(&x->mark, &orig->mark, sizeof(x->mark));
1237
Steffen Klassertcc9ab602014-02-19 13:33:24 +01001238 if (xfrm_init_state(x) < 0)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001239 goto error;
1240
1241 x->props.flags = orig->props.flags;
Nicolas Dichtela947b0a2013-02-22 10:54:54 +01001242 x->props.extra_flags = orig->props.extra_flags;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001243
Steffen Klassertee5c2312014-02-19 13:33:24 +01001244 x->tfcpad = orig->tfcpad;
1245 x->replay_maxdiff = orig->replay_maxdiff;
1246 x->replay_maxage = orig->replay_maxage;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001247 x->curlft.add_time = orig->curlft.add_time;
1248 x->km.state = orig->km.state;
1249 x->km.seq = orig->km.seq;
Antony Antonyc4c32032017-05-19 12:47:00 +02001250 x->replay = orig->replay;
1251 x->preplay = orig->preplay;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001252
1253 return x;
1254
1255 error:
Herbert Xu553f9112010-02-15 20:00:51 +00001256 xfrm_state_put(x);
1257out:
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001258 return NULL;
1259}
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001260
Fan Du283bc9f2013-11-07 17:47:50 +08001261struct xfrm_state *xfrm_migrate_state_find(struct xfrm_migrate *m, struct net *net)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001262{
1263 unsigned int h;
Steffen Klassert8c0cba22014-02-19 13:33:24 +01001264 struct xfrm_state *x = NULL;
1265
1266 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001267
1268 if (m->reqid) {
Fan Du283bc9f2013-11-07 17:47:50 +08001269 h = xfrm_dst_hash(net, &m->old_daddr, &m->old_saddr,
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001270 m->reqid, m->old_family);
Fan Du283bc9f2013-11-07 17:47:50 +08001271 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001272 if (x->props.mode != m->mode ||
1273 x->id.proto != m->proto)
1274 continue;
1275 if (m->reqid && x->props.reqid != m->reqid)
1276 continue;
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001277 if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr,
1278 m->old_family) ||
1279 !xfrm_addr_equal(&x->props.saddr, &m->old_saddr,
1280 m->old_family))
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001281 continue;
1282 xfrm_state_hold(x);
Steffen Klassert8c0cba22014-02-19 13:33:24 +01001283 break;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001284 }
1285 } else {
Fan Du283bc9f2013-11-07 17:47:50 +08001286 h = xfrm_src_hash(net, &m->old_daddr, &m->old_saddr,
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001287 m->old_family);
Fan Du283bc9f2013-11-07 17:47:50 +08001288 hlist_for_each_entry(x, net->xfrm.state_bysrc+h, bysrc) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001289 if (x->props.mode != m->mode ||
1290 x->id.proto != m->proto)
1291 continue;
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001292 if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr,
1293 m->old_family) ||
1294 !xfrm_addr_equal(&x->props.saddr, &m->old_saddr,
1295 m->old_family))
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001296 continue;
1297 xfrm_state_hold(x);
Steffen Klassert8c0cba22014-02-19 13:33:24 +01001298 break;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001299 }
1300 }
1301
Steffen Klassert8c0cba22014-02-19 13:33:24 +01001302 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1303
1304 return x;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001305}
1306EXPORT_SYMBOL(xfrm_migrate_state_find);
1307
Weilong Chen3e94c2d2013-12-24 09:43:47 +08001308struct xfrm_state *xfrm_state_migrate(struct xfrm_state *x,
1309 struct xfrm_migrate *m)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001310{
1311 struct xfrm_state *xc;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001312
Steffen Klassertcc9ab602014-02-19 13:33:24 +01001313 xc = xfrm_state_clone(x);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001314 if (!xc)
1315 return NULL;
1316
1317 memcpy(&xc->id.daddr, &m->new_daddr, sizeof(xc->id.daddr));
1318 memcpy(&xc->props.saddr, &m->new_saddr, sizeof(xc->props.saddr));
1319
1320 /* add state */
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001321 if (xfrm_addr_equal(&x->id.daddr, &m->new_daddr, m->new_family)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001322 /* a care is needed when the destination address of the
1323 state is to be updated as it is a part of triplet */
1324 xfrm_state_insert(xc);
1325 } else {
Steffen Klassertcc9ab602014-02-19 13:33:24 +01001326 if (xfrm_state_add(xc) < 0)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001327 goto error;
1328 }
1329
1330 return xc;
1331error:
Thomas Egerer78347c82010-12-06 23:28:56 +00001332 xfrm_state_put(xc);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001333 return NULL;
1334}
1335EXPORT_SYMBOL(xfrm_state_migrate);
1336#endif
1337
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338int xfrm_state_update(struct xfrm_state *x)
1339{
David S. Miller37b08e32008-09-02 20:14:15 -07001340 struct xfrm_state *x1, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 int err;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001342 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
Fan Du283bc9f2013-11-07 17:47:50 +08001343 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
David S. Miller37b08e32008-09-02 20:14:15 -07001345 to_put = NULL;
1346
Fan Du283bc9f2013-11-07 17:47:50 +08001347 spin_lock_bh(&net->xfrm.xfrm_state_lock);
David S. Milleredcd5822006-08-24 00:42:45 -07001348 x1 = __xfrm_state_locate(x, use_spi, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
1350 err = -ESRCH;
1351 if (!x1)
1352 goto out;
1353
1354 if (xfrm_state_kern(x1)) {
David S. Miller37b08e32008-09-02 20:14:15 -07001355 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 err = -EEXIST;
1357 goto out;
1358 }
1359
1360 if (x1->km.state == XFRM_STATE_ACQ) {
1361 __xfrm_state_insert(x);
1362 x = NULL;
1363 }
1364 err = 0;
1365
1366out:
Fan Du283bc9f2013-11-07 17:47:50 +08001367 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
David S. Miller37b08e32008-09-02 20:14:15 -07001369 if (to_put)
1370 xfrm_state_put(to_put);
1371
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 if (err)
1373 return err;
1374
1375 if (!x) {
1376 xfrm_state_delete(x1);
1377 xfrm_state_put(x1);
1378 return 0;
1379 }
1380
1381 err = -EINVAL;
1382 spin_lock_bh(&x1->lock);
1383 if (likely(x1->km.state == XFRM_STATE_VALID)) {
1384 if (x->encap && x1->encap)
1385 memcpy(x1->encap, x->encap, sizeof(*x1->encap));
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -07001386 if (x->coaddr && x1->coaddr) {
1387 memcpy(x1->coaddr, x->coaddr, sizeof(*x1->coaddr));
1388 }
1389 if (!use_spi && memcmp(&x1->sel, &x->sel, sizeof(x1->sel)))
1390 memcpy(&x1->sel, &x->sel, sizeof(x1->sel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 memcpy(&x1->lft, &x->lft, sizeof(x1->lft));
1392 x1->km.dying = 0;
1393
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -08001394 tasklet_hrtimer_start(&x1->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 if (x1->curlft.use_time)
1396 xfrm_state_check_expire(x1);
1397
1398 err = 0;
Tushar Gohad8fcbc632011-07-07 15:38:52 +00001399 x->km.state = XFRM_STATE_DEAD;
1400 __xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 }
1402 spin_unlock_bh(&x1->lock);
1403
1404 xfrm_state_put(x1);
1405
1406 return err;
1407}
1408EXPORT_SYMBOL(xfrm_state_update);
1409
1410int xfrm_state_check_expire(struct xfrm_state *x)
1411{
1412 if (!x->curlft.use_time)
James Morris9d729f72007-03-04 16:12:44 -08001413 x->curlft.use_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 if (x->curlft.bytes >= x->lft.hard_byte_limit ||
1416 x->curlft.packets >= x->lft.hard_packet_limit) {
Herbert Xu4666faa2005-06-18 22:43:22 -07001417 x->km.state = XFRM_STATE_EXPIRED;
Weilong Chen9b7a7872013-12-24 09:43:46 +08001418 tasklet_hrtimer_start(&x->mtimer, ktime_set(0, 0), HRTIMER_MODE_REL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 return -EINVAL;
1420 }
1421
1422 if (!x->km.dying &&
1423 (x->curlft.bytes >= x->lft.soft_byte_limit ||
Herbert Xu4666faa2005-06-18 22:43:22 -07001424 x->curlft.packets >= x->lft.soft_packet_limit)) {
1425 x->km.dying = 1;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001426 km_state_expired(x, 0, 0);
Herbert Xu4666faa2005-06-18 22:43:22 -07001427 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 return 0;
1429}
1430EXPORT_SYMBOL(xfrm_state_check_expire);
1431
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432struct xfrm_state *
David S. Millera70486f2011-02-27 23:17:24 -08001433xfrm_state_lookup(struct net *net, u32 mark, const xfrm_address_t *daddr, __be32 spi,
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001434 u8 proto, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435{
1436 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
Florian Westphalc2f672f2016-09-20 15:45:26 +02001438 rcu_read_lock();
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001439 x = __xfrm_state_lookup(net, mark, daddr, spi, proto, family);
Florian Westphalc2f672f2016-09-20 15:45:26 +02001440 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 return x;
1442}
1443EXPORT_SYMBOL(xfrm_state_lookup);
1444
1445struct xfrm_state *
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001446xfrm_state_lookup_byaddr(struct net *net, u32 mark,
David S. Millera70486f2011-02-27 23:17:24 -08001447 const xfrm_address_t *daddr, const xfrm_address_t *saddr,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001448 u8 proto, unsigned short family)
1449{
1450 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001451
Fan Du283bc9f2013-11-07 17:47:50 +08001452 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001453 x = __xfrm_state_lookup_byaddr(net, mark, daddr, saddr, proto, family);
Fan Du283bc9f2013-11-07 17:47:50 +08001454 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001455 return x;
1456}
1457EXPORT_SYMBOL(xfrm_state_lookup_byaddr);
1458
1459struct xfrm_state *
Mathias Krausee473fcb2013-06-26 23:56:58 +02001460xfrm_find_acq(struct net *net, const struct xfrm_mark *mark, u8 mode, u32 reqid,
1461 u8 proto, const xfrm_address_t *daddr,
1462 const xfrm_address_t *saddr, int create, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463{
1464 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
Fan Du283bc9f2013-11-07 17:47:50 +08001466 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001467 x = __find_acq_core(net, mark, family, mode, reqid, proto, daddr, saddr, create);
Fan Du283bc9f2013-11-07 17:47:50 +08001468 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
David S. Miller27708342006-08-24 00:13:10 -07001469
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 return x;
1471}
1472EXPORT_SYMBOL(xfrm_find_acq);
1473
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001474#ifdef CONFIG_XFRM_SUB_POLICY
1475int
1476xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n,
Fan Du283bc9f2013-11-07 17:47:50 +08001477 unsigned short family, struct net *net)
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001478{
1479 int err = 0;
1480 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
1481 if (!afinfo)
1482 return -EAFNOSUPPORT;
1483
Fan Du283bc9f2013-11-07 17:47:50 +08001484 spin_lock_bh(&net->xfrm.xfrm_state_lock); /*FIXME*/
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001485 if (afinfo->tmpl_sort)
1486 err = afinfo->tmpl_sort(dst, src, n);
Fan Du283bc9f2013-11-07 17:47:50 +08001487 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001488 xfrm_state_put_afinfo(afinfo);
1489 return err;
1490}
1491EXPORT_SYMBOL(xfrm_tmpl_sort);
1492
1493int
1494xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src, int n,
1495 unsigned short family)
1496{
1497 int err = 0;
1498 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
Steffen Klassert35ea790d2014-02-19 13:33:23 +01001499 struct net *net = xs_net(*src);
Fan Du283bc9f2013-11-07 17:47:50 +08001500
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001501 if (!afinfo)
1502 return -EAFNOSUPPORT;
1503
Fan Du283bc9f2013-11-07 17:47:50 +08001504 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001505 if (afinfo->state_sort)
1506 err = afinfo->state_sort(dst, src, n);
Fan Du283bc9f2013-11-07 17:47:50 +08001507 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001508 xfrm_state_put_afinfo(afinfo);
1509 return err;
1510}
1511EXPORT_SYMBOL(xfrm_state_sort);
1512#endif
1513
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514/* Silly enough, but I'm lazy to build resolution list */
1515
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001516static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517{
1518 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001520 for (i = 0; i <= net->xfrm.state_hmask; i++) {
David S. Miller8f126e32006-08-24 02:45:07 -07001521 struct xfrm_state *x;
1522
Sasha Levinb67bfe02013-02-27 17:06:00 -08001523 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
David S. Miller8f126e32006-08-24 02:45:07 -07001524 if (x->km.seq == seq &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001525 (mark & x->mark.m) == x->mark.v &&
David S. Miller8f126e32006-08-24 02:45:07 -07001526 x->km.state == XFRM_STATE_ACQ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 xfrm_state_hold(x);
1528 return x;
1529 }
1530 }
1531 }
1532 return NULL;
1533}
1534
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001535struct xfrm_state *xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536{
1537 struct xfrm_state *x;
1538
Fan Du283bc9f2013-11-07 17:47:50 +08001539 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001540 x = __xfrm_find_acq_byseq(net, mark, seq);
Fan Du283bc9f2013-11-07 17:47:50 +08001541 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 return x;
1543}
1544EXPORT_SYMBOL(xfrm_find_acq_byseq);
1545
1546u32 xfrm_get_acqseq(void)
1547{
1548 u32 res;
jamal6836b9b2010-02-16 02:01:22 +00001549 static atomic_t acqseq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
jamal6836b9b2010-02-16 02:01:22 +00001551 do {
1552 res = atomic_inc_return(&acqseq);
1553 } while (!res);
1554
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 return res;
1556}
1557EXPORT_SYMBOL(xfrm_get_acqseq);
1558
Fan Du776e9dd2013-12-16 18:47:49 +08001559int verify_spi_info(u8 proto, u32 min, u32 max)
1560{
1561 switch (proto) {
1562 case IPPROTO_AH:
1563 case IPPROTO_ESP:
1564 break;
1565
1566 case IPPROTO_COMP:
1567 /* IPCOMP spi is 16-bits. */
1568 if (max >= 0x10000)
1569 return -EINVAL;
1570 break;
1571
1572 default:
1573 return -EINVAL;
1574 }
1575
1576 if (min > max)
1577 return -EINVAL;
1578
1579 return 0;
1580}
1581EXPORT_SYMBOL(verify_spi_info);
1582
Herbert Xu658b2192007-10-09 13:29:52 -07001583int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -08001585 struct net *net = xs_net(x);
David S. Millerf034b5d2006-08-24 03:08:07 -07001586 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 struct xfrm_state *x0;
Herbert Xu658b2192007-10-09 13:29:52 -07001588 int err = -ENOENT;
1589 __be32 minspi = htonl(low);
1590 __be32 maxspi = htonl(high);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001591 u32 mark = x->mark.v & x->mark.m;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592
Herbert Xu658b2192007-10-09 13:29:52 -07001593 spin_lock_bh(&x->lock);
1594 if (x->km.state == XFRM_STATE_DEAD)
1595 goto unlock;
1596
1597 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 if (x->id.spi)
Herbert Xu658b2192007-10-09 13:29:52 -07001599 goto unlock;
1600
1601 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602
1603 if (minspi == maxspi) {
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001604 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, minspi, x->id.proto, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 if (x0) {
1606 xfrm_state_put(x0);
Herbert Xu658b2192007-10-09 13:29:52 -07001607 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 }
1609 x->id.spi = minspi;
1610 } else {
1611 u32 spi = 0;
Weilong Chen9b7a7872013-12-24 09:43:46 +08001612 for (h = 0; h < high-low+1; h++) {
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05001613 spi = low + prandom_u32()%(high-low+1);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001614 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, htonl(spi), x->id.proto, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 if (x0 == NULL) {
1616 x->id.spi = htonl(spi);
1617 break;
1618 }
1619 xfrm_state_put(x0);
1620 }
1621 }
1622 if (x->id.spi) {
Fan Du283bc9f2013-11-07 17:47:50 +08001623 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Alexey Dobriyan12604d82008-11-25 17:31:18 -08001624 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, x->props.family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001625 hlist_add_head_rcu(&x->byspi, net->xfrm.state_byspi + h);
Fan Du283bc9f2013-11-07 17:47:50 +08001626 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Herbert Xu658b2192007-10-09 13:29:52 -07001627
1628 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 }
Herbert Xu658b2192007-10-09 13:29:52 -07001630
1631unlock:
1632 spin_unlock_bh(&x->lock);
1633
1634 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635}
1636EXPORT_SYMBOL(xfrm_alloc_spi);
1637
Nicolas Dichteld3623092014-02-14 15:30:36 +01001638static bool __xfrm_state_filter_match(struct xfrm_state *x,
Nicolas Dichtel870a2df2014-03-06 18:24:29 +01001639 struct xfrm_address_filter *filter)
Nicolas Dichteld3623092014-02-14 15:30:36 +01001640{
1641 if (filter) {
1642 if ((filter->family == AF_INET ||
1643 filter->family == AF_INET6) &&
1644 x->props.family != filter->family)
1645 return false;
1646
1647 return addr_match(&x->props.saddr, &filter->saddr,
1648 filter->splen) &&
1649 addr_match(&x->id.daddr, &filter->daddr,
1650 filter->dplen);
1651 }
1652 return true;
1653}
1654
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08001655int xfrm_state_walk(struct net *net, struct xfrm_state_walk *walk,
Timo Teras4c563f72008-02-28 21:31:08 -08001656 int (*func)(struct xfrm_state *, int, void*),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 void *data)
1658{
Herbert Xu12a169e2008-10-01 07:03:24 -07001659 struct xfrm_state *state;
1660 struct xfrm_state_walk *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 int err = 0;
1662
Herbert Xu12a169e2008-10-01 07:03:24 -07001663 if (walk->seq != 0 && list_empty(&walk->all))
Timo Teras4c563f72008-02-28 21:31:08 -08001664 return 0;
1665
Fan Du283bc9f2013-11-07 17:47:50 +08001666 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001667 if (list_empty(&walk->all))
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08001668 x = list_first_entry(&net->xfrm.state_all, struct xfrm_state_walk, all);
Herbert Xu12a169e2008-10-01 07:03:24 -07001669 else
Li RongQing80077702015-04-22 17:09:54 +08001670 x = list_first_entry(&walk->all, struct xfrm_state_walk, all);
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08001671 list_for_each_entry_from(x, &net->xfrm.state_all, all) {
Herbert Xu12a169e2008-10-01 07:03:24 -07001672 if (x->state == XFRM_STATE_DEAD)
Timo Teras4c563f72008-02-28 21:31:08 -08001673 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07001674 state = container_of(x, struct xfrm_state, km);
1675 if (!xfrm_id_proto_match(state->id.proto, walk->proto))
Timo Teras4c563f72008-02-28 21:31:08 -08001676 continue;
Nicolas Dichteld3623092014-02-14 15:30:36 +01001677 if (!__xfrm_state_filter_match(state, walk->filter))
1678 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07001679 err = func(state, walk->seq, data);
1680 if (err) {
1681 list_move_tail(&walk->all, &x->all);
1682 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001684 walk->seq++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001686 if (walk->seq == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 err = -ENOENT;
1688 goto out;
1689 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001690 list_del_init(&walk->all);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691out:
Fan Du283bc9f2013-11-07 17:47:50 +08001692 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 return err;
1694}
1695EXPORT_SYMBOL(xfrm_state_walk);
1696
Nicolas Dichteld3623092014-02-14 15:30:36 +01001697void xfrm_state_walk_init(struct xfrm_state_walk *walk, u8 proto,
Nicolas Dichtel870a2df2014-03-06 18:24:29 +01001698 struct xfrm_address_filter *filter)
Herbert Xu5c182452008-09-22 19:48:19 -07001699{
Herbert Xu12a169e2008-10-01 07:03:24 -07001700 INIT_LIST_HEAD(&walk->all);
Herbert Xu5c182452008-09-22 19:48:19 -07001701 walk->proto = proto;
Herbert Xu12a169e2008-10-01 07:03:24 -07001702 walk->state = XFRM_STATE_DEAD;
1703 walk->seq = 0;
Nicolas Dichteld3623092014-02-14 15:30:36 +01001704 walk->filter = filter;
Herbert Xu5c182452008-09-22 19:48:19 -07001705}
1706EXPORT_SYMBOL(xfrm_state_walk_init);
1707
Fan Du283bc9f2013-11-07 17:47:50 +08001708void xfrm_state_walk_done(struct xfrm_state_walk *walk, struct net *net)
Herbert Xuabb81c42008-09-09 19:58:29 -07001709{
Nicolas Dichteld3623092014-02-14 15:30:36 +01001710 kfree(walk->filter);
1711
Herbert Xu12a169e2008-10-01 07:03:24 -07001712 if (list_empty(&walk->all))
Herbert Xu5c182452008-09-22 19:48:19 -07001713 return;
Herbert Xu5c182452008-09-22 19:48:19 -07001714
Fan Du283bc9f2013-11-07 17:47:50 +08001715 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001716 list_del(&walk->all);
Fan Du283bc9f2013-11-07 17:47:50 +08001717 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Herbert Xuabb81c42008-09-09 19:58:29 -07001718}
1719EXPORT_SYMBOL(xfrm_state_walk_done);
1720
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001721static void xfrm_replay_timer_handler(unsigned long data)
1722{
Weilong Chen3e94c2d2013-12-24 09:43:47 +08001723 struct xfrm_state *x = (struct xfrm_state *)data;
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001724
1725 spin_lock(&x->lock);
1726
Jamal Hadi Salim27170962006-04-14 15:03:05 -07001727 if (x->km.state == XFRM_STATE_VALID) {
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001728 if (xfrm_aevent_is_on(xs_net(x)))
Steffen Klassert9fdc4882011-03-08 00:08:32 +00001729 x->repl->notify(x, XFRM_REPLAY_TIMEOUT);
Jamal Hadi Salim27170962006-04-14 15:03:05 -07001730 else
1731 x->xflags |= XFRM_TIME_DEFER;
1732 }
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001733
1734 spin_unlock(&x->lock);
1735}
1736
Denis Chengdf018122007-12-07 00:51:11 -08001737static LIST_HEAD(xfrm_km_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738
David S. Miller214e0052011-02-24 00:02:38 -05001739void km_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740{
1741 struct xfrm_mgr *km;
1742
Cong Wang85168c02013-01-16 16:05:06 +08001743 rcu_read_lock();
1744 list_for_each_entry_rcu(km, &xfrm_km_list, list)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001745 if (km->notify_policy)
1746 km->notify_policy(xp, dir, c);
Cong Wang85168c02013-01-16 16:05:06 +08001747 rcu_read_unlock();
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001748}
1749
David S. Miller214e0052011-02-24 00:02:38 -05001750void km_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001751{
1752 struct xfrm_mgr *km;
Cong Wang85168c02013-01-16 16:05:06 +08001753 rcu_read_lock();
1754 list_for_each_entry_rcu(km, &xfrm_km_list, list)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001755 if (km->notify)
1756 km->notify(x, c);
Cong Wang85168c02013-01-16 16:05:06 +08001757 rcu_read_unlock();
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001758}
1759
1760EXPORT_SYMBOL(km_policy_notify);
1761EXPORT_SYMBOL(km_state_notify);
1762
Eric W. Biederman15e47302012-09-07 20:12:54 +00001763void km_state_expired(struct xfrm_state *x, int hard, u32 portid)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001764{
1765 struct km_event c;
1766
Herbert Xubf088672005-06-18 22:44:00 -07001767 c.data.hard = hard;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001768 c.portid = portid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001769 c.event = XFRM_MSG_EXPIRE;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001770 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771}
1772
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001773EXPORT_SYMBOL(km_state_expired);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001774/*
1775 * We send to all registered managers regardless of failure
1776 * We are happy with one success
1777*/
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001778int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001780 int err = -EINVAL, acqret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 struct xfrm_mgr *km;
1782
Cong Wang85168c02013-01-16 16:05:06 +08001783 rcu_read_lock();
1784 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Fan Du65e07362012-08-15 10:13:47 +08001785 acqret = km->acquire(x, t, pol);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001786 if (!acqret)
1787 err = acqret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 }
Cong Wang85168c02013-01-16 16:05:06 +08001789 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 return err;
1791}
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001792EXPORT_SYMBOL(km_query);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
Al Viro5d36b182006-11-08 00:24:06 -08001794int km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795{
1796 int err = -EINVAL;
1797 struct xfrm_mgr *km;
1798
Cong Wang85168c02013-01-16 16:05:06 +08001799 rcu_read_lock();
1800 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 if (km->new_mapping)
1802 err = km->new_mapping(x, ipaddr, sport);
1803 if (!err)
1804 break;
1805 }
Cong Wang85168c02013-01-16 16:05:06 +08001806 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 return err;
1808}
1809EXPORT_SYMBOL(km_new_mapping);
1810
Eric W. Biederman15e47302012-09-07 20:12:54 +00001811void km_policy_expired(struct xfrm_policy *pol, int dir, int hard, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001813 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
Herbert Xubf088672005-06-18 22:44:00 -07001815 c.data.hard = hard;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001816 c.portid = portid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001817 c.event = XFRM_MSG_POLEXPIRE;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001818 km_policy_notify(pol, dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819}
David S. Millera70fcb02006-03-20 19:18:52 -08001820EXPORT_SYMBOL(km_policy_expired);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821
Eric Dumazet2d60abc2008-01-03 20:43:21 -08001822#ifdef CONFIG_XFRM_MIGRATE
David S. Miller183cad12011-02-24 00:28:01 -05001823int km_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
1824 const struct xfrm_migrate *m, int num_migrate,
1825 const struct xfrm_kmaddress *k)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001826{
1827 int err = -EINVAL;
1828 int ret;
1829 struct xfrm_mgr *km;
1830
Cong Wang85168c02013-01-16 16:05:06 +08001831 rcu_read_lock();
1832 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001833 if (km->migrate) {
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001834 ret = km->migrate(sel, dir, type, m, num_migrate, k);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001835 if (!ret)
1836 err = ret;
1837 }
1838 }
Cong Wang85168c02013-01-16 16:05:06 +08001839 rcu_read_unlock();
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001840 return err;
1841}
1842EXPORT_SYMBOL(km_migrate);
Eric Dumazet2d60abc2008-01-03 20:43:21 -08001843#endif
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001844
Alexey Dobriyandb983c12008-11-25 17:51:01 -08001845int km_report(struct net *net, u8 proto, struct xfrm_selector *sel, xfrm_address_t *addr)
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001846{
1847 int err = -EINVAL;
1848 int ret;
1849 struct xfrm_mgr *km;
1850
Cong Wang85168c02013-01-16 16:05:06 +08001851 rcu_read_lock();
1852 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001853 if (km->report) {
Alexey Dobriyandb983c12008-11-25 17:51:01 -08001854 ret = km->report(net, proto, sel, addr);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001855 if (!ret)
1856 err = ret;
1857 }
1858 }
Cong Wang85168c02013-01-16 16:05:06 +08001859 rcu_read_unlock();
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001860 return err;
1861}
1862EXPORT_SYMBOL(km_report);
1863
Horia Geanta0f245582014-02-12 16:20:06 +02001864bool km_is_alive(const struct km_event *c)
1865{
1866 struct xfrm_mgr *km;
1867 bool is_alive = false;
1868
1869 rcu_read_lock();
1870 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
1871 if (km->is_alive && km->is_alive(c)) {
1872 is_alive = true;
1873 break;
1874 }
1875 }
1876 rcu_read_unlock();
1877
1878 return is_alive;
1879}
1880EXPORT_SYMBOL(km_is_alive);
1881
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882int xfrm_user_policy(struct sock *sk, int optname, u8 __user *optval, int optlen)
1883{
1884 int err;
1885 u8 *data;
1886 struct xfrm_mgr *km;
1887 struct xfrm_policy *pol = NULL;
1888
Steffen Klassert02a5b412018-02-01 08:49:23 +01001889#ifdef CONFIG_COMPAT
1890 if (in_compat_syscall())
1891 return -EOPNOTSUPP;
1892#endif
1893
Lorenzo Colitti5eef9b52017-11-20 19:26:02 +09001894 if (!optval && !optlen) {
1895 xfrm_sk_policy_insert(sk, XFRM_POLICY_IN, NULL);
1896 xfrm_sk_policy_insert(sk, XFRM_POLICY_OUT, NULL);
1897 __sk_dst_reset(sk);
1898 return 0;
1899 }
1900
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 if (optlen <= 0 || optlen > PAGE_SIZE)
1902 return -EMSGSIZE;
1903
1904 data = kmalloc(optlen, GFP_KERNEL);
1905 if (!data)
1906 return -ENOMEM;
1907
1908 err = -EFAULT;
1909 if (copy_from_user(data, optval, optlen))
1910 goto out;
1911
1912 err = -EINVAL;
Cong Wang85168c02013-01-16 16:05:06 +08001913 rcu_read_lock();
1914 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07001915 pol = km->compile_policy(sk, optname, data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 optlen, &err);
1917 if (err >= 0)
1918 break;
1919 }
Cong Wang85168c02013-01-16 16:05:06 +08001920 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921
1922 if (err >= 0) {
1923 xfrm_sk_policy_insert(sk, err, pol);
1924 xfrm_pol_put(pol);
1925 err = 0;
1926 }
1927
1928out:
1929 kfree(data);
1930 return err;
1931}
1932EXPORT_SYMBOL(xfrm_user_policy);
1933
Cong Wang85168c02013-01-16 16:05:06 +08001934static DEFINE_SPINLOCK(xfrm_km_lock);
1935
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936int xfrm_register_km(struct xfrm_mgr *km)
1937{
Cong Wang85168c02013-01-16 16:05:06 +08001938 spin_lock_bh(&xfrm_km_lock);
1939 list_add_tail_rcu(&km->list, &xfrm_km_list);
1940 spin_unlock_bh(&xfrm_km_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 return 0;
1942}
1943EXPORT_SYMBOL(xfrm_register_km);
1944
1945int xfrm_unregister_km(struct xfrm_mgr *km)
1946{
Cong Wang85168c02013-01-16 16:05:06 +08001947 spin_lock_bh(&xfrm_km_lock);
1948 list_del_rcu(&km->list);
1949 spin_unlock_bh(&xfrm_km_lock);
1950 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 return 0;
1952}
1953EXPORT_SYMBOL(xfrm_unregister_km);
1954
1955int xfrm_state_register_afinfo(struct xfrm_state_afinfo *afinfo)
1956{
1957 int err = 0;
1958 if (unlikely(afinfo == NULL))
1959 return -EINVAL;
1960 if (unlikely(afinfo->family >= NPROTO))
1961 return -EAFNOSUPPORT;
Cong Wang44abdc32013-01-16 16:05:05 +08001962 spin_lock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 if (unlikely(xfrm_state_afinfo[afinfo->family] != NULL))
Li RongQingf31e8d4f2015-04-23 11:06:53 +08001964 err = -EEXIST;
David S. Milleredcd5822006-08-24 00:42:45 -07001965 else
Cong Wang44abdc32013-01-16 16:05:05 +08001966 rcu_assign_pointer(xfrm_state_afinfo[afinfo->family], afinfo);
1967 spin_unlock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 return err;
1969}
1970EXPORT_SYMBOL(xfrm_state_register_afinfo);
1971
1972int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo)
1973{
1974 int err = 0;
1975 if (unlikely(afinfo == NULL))
1976 return -EINVAL;
1977 if (unlikely(afinfo->family >= NPROTO))
1978 return -EAFNOSUPPORT;
Cong Wang44abdc32013-01-16 16:05:05 +08001979 spin_lock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 if (likely(xfrm_state_afinfo[afinfo->family] != NULL)) {
1981 if (unlikely(xfrm_state_afinfo[afinfo->family] != afinfo))
1982 err = -EINVAL;
David S. Milleredcd5822006-08-24 00:42:45 -07001983 else
Cong Wang44abdc32013-01-16 16:05:05 +08001984 RCU_INIT_POINTER(xfrm_state_afinfo[afinfo->family], NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 }
Cong Wang44abdc32013-01-16 16:05:05 +08001986 spin_unlock_bh(&xfrm_state_afinfo_lock);
1987 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 return err;
1989}
1990EXPORT_SYMBOL(xfrm_state_unregister_afinfo);
1991
Hannes Frederic Sowa628e3412013-08-14 13:05:23 +02001992struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993{
1994 struct xfrm_state_afinfo *afinfo;
1995 if (unlikely(family >= NPROTO))
1996 return NULL;
Cong Wang44abdc32013-01-16 16:05:05 +08001997 rcu_read_lock();
1998 afinfo = rcu_dereference(xfrm_state_afinfo[family]);
Herbert Xu546be242006-05-27 23:03:58 -07001999 if (unlikely(!afinfo))
Cong Wang44abdc32013-01-16 16:05:05 +08002000 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 return afinfo;
2002}
2003
Hannes Frederic Sowa628e3412013-08-14 13:05:23 +02002004void xfrm_state_put_afinfo(struct xfrm_state_afinfo *afinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005{
Cong Wang44abdc32013-01-16 16:05:05 +08002006 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007}
2008
2009/* Temporarily located here until net/xfrm/xfrm_tunnel.c is created */
2010void xfrm_state_delete_tunnel(struct xfrm_state *x)
2011{
2012 if (x->tunnel) {
2013 struct xfrm_state *t = x->tunnel;
2014
2015 if (atomic_read(&t->tunnel_users) == 2)
2016 xfrm_state_delete(t);
2017 atomic_dec(&t->tunnel_users);
2018 xfrm_state_put(t);
2019 x->tunnel = NULL;
2020 }
2021}
2022EXPORT_SYMBOL(xfrm_state_delete_tunnel);
2023
2024int xfrm_state_mtu(struct xfrm_state *x, int mtu)
2025{
Patrick McHardyc5c25232007-04-09 11:47:18 -07002026 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027
Patrick McHardyc5c25232007-04-09 11:47:18 -07002028 spin_lock_bh(&x->lock);
2029 if (x->km.state == XFRM_STATE_VALID &&
2030 x->type && x->type->get_mtu)
2031 res = x->type->get_mtu(x, mtu);
2032 else
Patrick McHardy28121612007-06-18 22:30:15 -07002033 res = mtu - x->props.header_len;
Patrick McHardyc5c25232007-04-09 11:47:18 -07002034 spin_unlock_bh(&x->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 return res;
2036}
2037
Wei Yongjuna454f0c2011-03-21 18:08:28 -07002038int __xfrm_init_state(struct xfrm_state *x, bool init_replay)
Herbert Xu72cb6962005-06-20 13:18:08 -07002039{
Herbert Xud094cd82005-06-20 13:19:41 -07002040 struct xfrm_state_afinfo *afinfo;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002041 struct xfrm_mode *inner_mode;
Herbert Xud094cd82005-06-20 13:19:41 -07002042 int family = x->props.family;
Herbert Xu72cb6962005-06-20 13:18:08 -07002043 int err;
2044
Herbert Xud094cd82005-06-20 13:19:41 -07002045 err = -EAFNOSUPPORT;
2046 afinfo = xfrm_state_get_afinfo(family);
2047 if (!afinfo)
2048 goto error;
2049
2050 err = 0;
2051 if (afinfo->init_flags)
2052 err = afinfo->init_flags(x);
2053
2054 xfrm_state_put_afinfo(afinfo);
2055
2056 if (err)
2057 goto error;
2058
2059 err = -EPROTONOSUPPORT;
Herbert Xu13996372007-10-17 21:35:51 -07002060
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002061 if (x->sel.family != AF_UNSPEC) {
2062 inner_mode = xfrm_get_mode(x->props.mode, x->sel.family);
2063 if (inner_mode == NULL)
2064 goto error;
2065
2066 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL) &&
2067 family != x->sel.family) {
2068 xfrm_put_mode(inner_mode);
2069 goto error;
2070 }
2071
2072 x->inner_mode = inner_mode;
2073 } else {
2074 struct xfrm_mode *inner_mode_iaf;
Martin Willid81d2282008-12-03 15:38:07 -08002075 int iafamily = AF_INET;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002076
Martin Willid81d2282008-12-03 15:38:07 -08002077 inner_mode = xfrm_get_mode(x->props.mode, x->props.family);
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002078 if (inner_mode == NULL)
2079 goto error;
2080
2081 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL)) {
2082 xfrm_put_mode(inner_mode);
2083 goto error;
2084 }
Martin Willid81d2282008-12-03 15:38:07 -08002085 x->inner_mode = inner_mode;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002086
Martin Willid81d2282008-12-03 15:38:07 -08002087 if (x->props.family == AF_INET)
2088 iafamily = AF_INET6;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002089
Martin Willid81d2282008-12-03 15:38:07 -08002090 inner_mode_iaf = xfrm_get_mode(x->props.mode, iafamily);
2091 if (inner_mode_iaf) {
2092 if (inner_mode_iaf->flags & XFRM_MODE_FLAG_TUNNEL)
2093 x->inner_mode_iaf = inner_mode_iaf;
2094 else
2095 xfrm_put_mode(inner_mode_iaf);
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002096 }
2097 }
Herbert Xu13996372007-10-17 21:35:51 -07002098
Herbert Xud094cd82005-06-20 13:19:41 -07002099 x->type = xfrm_get_type(x->id.proto, family);
Herbert Xu72cb6962005-06-20 13:18:08 -07002100 if (x->type == NULL)
2101 goto error;
2102
2103 err = x->type->init_state(x);
2104 if (err)
2105 goto error;
2106
Herbert Xu13996372007-10-17 21:35:51 -07002107 x->outer_mode = xfrm_get_mode(x->props.mode, family);
Julia Lawall599901c2012-08-29 06:49:15 +00002108 if (x->outer_mode == NULL) {
2109 err = -EPROTONOSUPPORT;
Herbert Xub59f45d2006-05-27 23:05:54 -07002110 goto error;
Julia Lawall599901c2012-08-29 06:49:15 +00002111 }
Herbert Xub59f45d2006-05-27 23:05:54 -07002112
Wei Yongjuna454f0c2011-03-21 18:08:28 -07002113 if (init_replay) {
2114 err = xfrm_init_replay(x);
2115 if (err)
2116 goto error;
2117 }
2118
Herbert Xu72cb6962005-06-20 13:18:08 -07002119 x->km.state = XFRM_STATE_VALID;
2120
2121error:
2122 return err;
2123}
2124
Wei Yongjuna454f0c2011-03-21 18:08:28 -07002125EXPORT_SYMBOL(__xfrm_init_state);
2126
2127int xfrm_init_state(struct xfrm_state *x)
2128{
2129 return __xfrm_init_state(x, true);
2130}
2131
Herbert Xu72cb6962005-06-20 13:18:08 -07002132EXPORT_SYMBOL(xfrm_init_state);
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09002133
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002134int __net_init xfrm_state_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135{
David S. Millerf034b5d2006-08-24 03:08:07 -07002136 unsigned int sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137
Alexey Dobriyan9d4139c2008-11-25 17:16:11 -08002138 INIT_LIST_HEAD(&net->xfrm.state_all);
2139
David S. Millerf034b5d2006-08-24 03:08:07 -07002140 sz = sizeof(struct hlist_head) * 8;
2141
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002142 net->xfrm.state_bydst = xfrm_hash_alloc(sz);
2143 if (!net->xfrm.state_bydst)
2144 goto out_bydst;
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002145 net->xfrm.state_bysrc = xfrm_hash_alloc(sz);
2146 if (!net->xfrm.state_bysrc)
2147 goto out_bysrc;
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002148 net->xfrm.state_byspi = xfrm_hash_alloc(sz);
2149 if (!net->xfrm.state_byspi)
2150 goto out_byspi;
Alexey Dobriyan529983e2008-11-25 17:18:12 -08002151 net->xfrm.state_hmask = ((sz / sizeof(struct hlist_head)) - 1);
David S. Millerf034b5d2006-08-24 03:08:07 -07002152
Alexey Dobriyan0bf7c5b2008-11-25 17:18:39 -08002153 net->xfrm.state_num = 0;
Alexey Dobriyan63082732008-11-25 17:19:07 -08002154 INIT_WORK(&net->xfrm.state_hash_work, xfrm_hash_resize);
Fan Du283bc9f2013-11-07 17:47:50 +08002155 spin_lock_init(&net->xfrm.xfrm_state_lock);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002156 return 0;
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002157
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002158out_byspi:
2159 xfrm_hash_free(net->xfrm.state_bysrc, sz);
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002160out_bysrc:
2161 xfrm_hash_free(net->xfrm.state_bydst, sz);
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002162out_bydst:
2163 return -ENOMEM;
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002164}
2165
2166void xfrm_state_fini(struct net *net)
2167{
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002168 unsigned int sz;
2169
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002170 flush_work(&net->xfrm.state_hash_work);
Tetsuo Handa2e710292014-04-22 21:48:30 +09002171 xfrm_state_flush(net, IPSEC_PROTO_ANY, false);
Florian Westphal35db57bb2016-08-23 16:00:12 +02002172 flush_work(&xfrm_state_gc_work);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002173
Alexey Dobriyan9d4139c2008-11-25 17:16:11 -08002174 WARN_ON(!list_empty(&net->xfrm.state_all));
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002175
Alexey Dobriyan529983e2008-11-25 17:18:12 -08002176 sz = (net->xfrm.state_hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002177 WARN_ON(!hlist_empty(net->xfrm.state_byspi));
2178 xfrm_hash_free(net->xfrm.state_byspi, sz);
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002179 WARN_ON(!hlist_empty(net->xfrm.state_bysrc));
2180 xfrm_hash_free(net->xfrm.state_bysrc, sz);
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002181 WARN_ON(!hlist_empty(net->xfrm.state_bydst));
2182 xfrm_hash_free(net->xfrm.state_bydst, sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183}
2184
Joy Lattenab5f5e82007-09-17 11:51:22 -07002185#ifdef CONFIG_AUDITSYSCALL
Ilpo Järvinencf35f432008-01-05 23:13:20 -08002186static void xfrm_audit_helper_sainfo(struct xfrm_state *x,
2187 struct audit_buffer *audit_buf)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002188{
Paul Moore68277ac2007-12-20 20:49:33 -08002189 struct xfrm_sec_ctx *ctx = x->security;
2190 u32 spi = ntohl(x->id.spi);
2191
2192 if (ctx)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002193 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
Paul Moore68277ac2007-12-20 20:49:33 -08002194 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002195
Weilong Chen9b7a7872013-12-24 09:43:46 +08002196 switch (x->props.family) {
Joy Lattenab5f5e82007-09-17 11:51:22 -07002197 case AF_INET:
Harvey Harrison21454aa2008-10-31 00:54:56 -07002198 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2199 &x->props.saddr.a4, &x->id.daddr.a4);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002200 break;
2201 case AF_INET6:
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002202 audit_log_format(audit_buf, " src=%pI6 dst=%pI6",
Harvey Harrisonfdb46ee2008-10-28 16:10:17 -07002203 x->props.saddr.a6, x->id.daddr.a6);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002204 break;
2205 }
Paul Moore68277ac2007-12-20 20:49:33 -08002206
2207 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002208}
2209
Ilpo Järvinencf35f432008-01-05 23:13:20 -08002210static void xfrm_audit_helper_pktinfo(struct sk_buff *skb, u16 family,
2211 struct audit_buffer *audit_buf)
Paul Mooreafeb14b2007-12-21 14:58:11 -08002212{
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002213 const struct iphdr *iph4;
2214 const struct ipv6hdr *iph6;
Paul Mooreafeb14b2007-12-21 14:58:11 -08002215
2216 switch (family) {
2217 case AF_INET:
2218 iph4 = ip_hdr(skb);
Harvey Harrison21454aa2008-10-31 00:54:56 -07002219 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2220 &iph4->saddr, &iph4->daddr);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002221 break;
2222 case AF_INET6:
2223 iph6 = ipv6_hdr(skb);
2224 audit_log_format(audit_buf,
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002225 " src=%pI6 dst=%pI6 flowlbl=0x%x%02x%02x",
Weilong Chen9b7a7872013-12-24 09:43:46 +08002226 &iph6->saddr, &iph6->daddr,
Paul Mooreafeb14b2007-12-21 14:58:11 -08002227 iph6->flow_lbl[0] & 0x0f,
2228 iph6->flow_lbl[1],
2229 iph6->flow_lbl[2]);
2230 break;
2231 }
2232}
2233
Tetsuo Handa2e710292014-04-22 21:48:30 +09002234void xfrm_audit_state_add(struct xfrm_state *x, int result, bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002235{
2236 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002237
Paul Mooreafeb14b2007-12-21 14:58:11 -08002238 audit_buf = xfrm_audit_start("SAD-add");
Joy Lattenab5f5e82007-09-17 11:51:22 -07002239 if (audit_buf == NULL)
2240 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09002241 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002242 xfrm_audit_helper_sainfo(x, audit_buf);
2243 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002244 audit_log_end(audit_buf);
2245}
2246EXPORT_SYMBOL_GPL(xfrm_audit_state_add);
2247
Tetsuo Handa2e710292014-04-22 21:48:30 +09002248void xfrm_audit_state_delete(struct xfrm_state *x, int result, bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002249{
2250 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002251
Paul Mooreafeb14b2007-12-21 14:58:11 -08002252 audit_buf = xfrm_audit_start("SAD-delete");
Joy Lattenab5f5e82007-09-17 11:51:22 -07002253 if (audit_buf == NULL)
2254 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09002255 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002256 xfrm_audit_helper_sainfo(x, audit_buf);
2257 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002258 audit_log_end(audit_buf);
2259}
2260EXPORT_SYMBOL_GPL(xfrm_audit_state_delete);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002261
2262void xfrm_audit_state_replay_overflow(struct xfrm_state *x,
2263 struct sk_buff *skb)
2264{
2265 struct audit_buffer *audit_buf;
2266 u32 spi;
2267
2268 audit_buf = xfrm_audit_start("SA-replay-overflow");
2269 if (audit_buf == NULL)
2270 return;
2271 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2272 /* don't record the sequence number because it's inherent in this kind
2273 * of audit message */
2274 spi = ntohl(x->id.spi);
2275 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
2276 audit_log_end(audit_buf);
2277}
2278EXPORT_SYMBOL_GPL(xfrm_audit_state_replay_overflow);
2279
Steffen Klassert9fdc4882011-03-08 00:08:32 +00002280void xfrm_audit_state_replay(struct xfrm_state *x,
Paul Mooreafeb14b2007-12-21 14:58:11 -08002281 struct sk_buff *skb, __be32 net_seq)
2282{
2283 struct audit_buffer *audit_buf;
2284 u32 spi;
2285
2286 audit_buf = xfrm_audit_start("SA-replayed-pkt");
2287 if (audit_buf == NULL)
2288 return;
2289 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2290 spi = ntohl(x->id.spi);
2291 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2292 spi, spi, ntohl(net_seq));
2293 audit_log_end(audit_buf);
2294}
Steffen Klassert9fdc4882011-03-08 00:08:32 +00002295EXPORT_SYMBOL_GPL(xfrm_audit_state_replay);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002296
2297void xfrm_audit_state_notfound_simple(struct sk_buff *skb, u16 family)
2298{
2299 struct audit_buffer *audit_buf;
2300
2301 audit_buf = xfrm_audit_start("SA-notfound");
2302 if (audit_buf == NULL)
2303 return;
2304 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2305 audit_log_end(audit_buf);
2306}
2307EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound_simple);
2308
2309void xfrm_audit_state_notfound(struct sk_buff *skb, u16 family,
2310 __be32 net_spi, __be32 net_seq)
2311{
2312 struct audit_buffer *audit_buf;
2313 u32 spi;
2314
2315 audit_buf = xfrm_audit_start("SA-notfound");
2316 if (audit_buf == NULL)
2317 return;
2318 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2319 spi = ntohl(net_spi);
2320 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2321 spi, spi, ntohl(net_seq));
2322 audit_log_end(audit_buf);
2323}
2324EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound);
2325
2326void xfrm_audit_state_icvfail(struct xfrm_state *x,
2327 struct sk_buff *skb, u8 proto)
2328{
2329 struct audit_buffer *audit_buf;
2330 __be32 net_spi;
2331 __be32 net_seq;
2332
2333 audit_buf = xfrm_audit_start("SA-icv-failure");
2334 if (audit_buf == NULL)
2335 return;
2336 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2337 if (xfrm_parse_spi(skb, proto, &net_spi, &net_seq) == 0) {
2338 u32 spi = ntohl(net_spi);
2339 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2340 spi, spi, ntohl(net_seq));
2341 }
2342 audit_log_end(audit_buf);
2343}
2344EXPORT_SYMBOL_GPL(xfrm_audit_state_icvfail);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002345#endif /* CONFIG_AUDITSYSCALL */