blob: d869b1d927071a329dd9ffee91a402f649778668 [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);
1200 if (!x->aead)
1201 goto error;
1202 }
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001203 if (orig->ealg) {
1204 x->ealg = xfrm_algo_clone(orig->ealg);
1205 if (!x->ealg)
1206 goto error;
1207 }
1208 x->props.ealgo = orig->props.ealgo;
1209
1210 if (orig->calg) {
1211 x->calg = xfrm_algo_clone(orig->calg);
1212 if (!x->calg)
1213 goto error;
1214 }
1215 x->props.calgo = orig->props.calgo;
1216
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001217 if (orig->encap) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001218 x->encap = kmemdup(orig->encap, sizeof(*x->encap), GFP_KERNEL);
1219 if (!x->encap)
1220 goto error;
1221 }
1222
1223 if (orig->coaddr) {
1224 x->coaddr = kmemdup(orig->coaddr, sizeof(*x->coaddr),
1225 GFP_KERNEL);
1226 if (!x->coaddr)
1227 goto error;
1228 }
1229
Steffen Klassertaf2f4642011-03-28 19:46:39 +00001230 if (orig->replay_esn) {
Steffen Klassertcc9ab602014-02-19 13:33:24 +01001231 if (xfrm_replay_clone(x, orig))
Steffen Klassertaf2f4642011-03-28 19:46:39 +00001232 goto error;
1233 }
1234
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001235 memcpy(&x->mark, &orig->mark, sizeof(x->mark));
1236
Steffen Klassertcc9ab602014-02-19 13:33:24 +01001237 if (xfrm_init_state(x) < 0)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001238 goto error;
1239
1240 x->props.flags = orig->props.flags;
Nicolas Dichtela947b0a2013-02-22 10:54:54 +01001241 x->props.extra_flags = orig->props.extra_flags;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001242
Steffen Klassertee5c2312014-02-19 13:33:24 +01001243 x->tfcpad = orig->tfcpad;
1244 x->replay_maxdiff = orig->replay_maxdiff;
1245 x->replay_maxage = orig->replay_maxage;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001246 x->curlft.add_time = orig->curlft.add_time;
1247 x->km.state = orig->km.state;
1248 x->km.seq = orig->km.seq;
Antony Antonyc4c32032017-05-19 12:47:00 +02001249 x->replay = orig->replay;
1250 x->preplay = orig->preplay;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001251
1252 return x;
1253
1254 error:
Herbert Xu553f9112010-02-15 20:00:51 +00001255 xfrm_state_put(x);
1256out:
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001257 return NULL;
1258}
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001259
Fan Du283bc9f2013-11-07 17:47:50 +08001260struct xfrm_state *xfrm_migrate_state_find(struct xfrm_migrate *m, struct net *net)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001261{
1262 unsigned int h;
Steffen Klassert8c0cba22014-02-19 13:33:24 +01001263 struct xfrm_state *x = NULL;
1264
1265 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001266
1267 if (m->reqid) {
Fan Du283bc9f2013-11-07 17:47:50 +08001268 h = xfrm_dst_hash(net, &m->old_daddr, &m->old_saddr,
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001269 m->reqid, m->old_family);
Fan Du283bc9f2013-11-07 17:47:50 +08001270 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001271 if (x->props.mode != m->mode ||
1272 x->id.proto != m->proto)
1273 continue;
1274 if (m->reqid && x->props.reqid != m->reqid)
1275 continue;
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001276 if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr,
1277 m->old_family) ||
1278 !xfrm_addr_equal(&x->props.saddr, &m->old_saddr,
1279 m->old_family))
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001280 continue;
1281 xfrm_state_hold(x);
Steffen Klassert8c0cba22014-02-19 13:33:24 +01001282 break;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001283 }
1284 } else {
Fan Du283bc9f2013-11-07 17:47:50 +08001285 h = xfrm_src_hash(net, &m->old_daddr, &m->old_saddr,
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001286 m->old_family);
Fan Du283bc9f2013-11-07 17:47:50 +08001287 hlist_for_each_entry(x, net->xfrm.state_bysrc+h, bysrc) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001288 if (x->props.mode != m->mode ||
1289 x->id.proto != m->proto)
1290 continue;
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001291 if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr,
1292 m->old_family) ||
1293 !xfrm_addr_equal(&x->props.saddr, &m->old_saddr,
1294 m->old_family))
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001295 continue;
1296 xfrm_state_hold(x);
Steffen Klassert8c0cba22014-02-19 13:33:24 +01001297 break;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001298 }
1299 }
1300
Steffen Klassert8c0cba22014-02-19 13:33:24 +01001301 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1302
1303 return x;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001304}
1305EXPORT_SYMBOL(xfrm_migrate_state_find);
1306
Weilong Chen3e94c2d2013-12-24 09:43:47 +08001307struct xfrm_state *xfrm_state_migrate(struct xfrm_state *x,
1308 struct xfrm_migrate *m)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001309{
1310 struct xfrm_state *xc;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001311
Steffen Klassertcc9ab602014-02-19 13:33:24 +01001312 xc = xfrm_state_clone(x);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001313 if (!xc)
1314 return NULL;
1315
1316 memcpy(&xc->id.daddr, &m->new_daddr, sizeof(xc->id.daddr));
1317 memcpy(&xc->props.saddr, &m->new_saddr, sizeof(xc->props.saddr));
1318
1319 /* add state */
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001320 if (xfrm_addr_equal(&x->id.daddr, &m->new_daddr, m->new_family)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001321 /* a care is needed when the destination address of the
1322 state is to be updated as it is a part of triplet */
1323 xfrm_state_insert(xc);
1324 } else {
Steffen Klassertcc9ab602014-02-19 13:33:24 +01001325 if (xfrm_state_add(xc) < 0)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001326 goto error;
1327 }
1328
1329 return xc;
1330error:
Thomas Egerer78347c82010-12-06 23:28:56 +00001331 xfrm_state_put(xc);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001332 return NULL;
1333}
1334EXPORT_SYMBOL(xfrm_state_migrate);
1335#endif
1336
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337int xfrm_state_update(struct xfrm_state *x)
1338{
David S. Miller37b08e32008-09-02 20:14:15 -07001339 struct xfrm_state *x1, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 int err;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001341 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
Fan Du283bc9f2013-11-07 17:47:50 +08001342 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
David S. Miller37b08e32008-09-02 20:14:15 -07001344 to_put = NULL;
1345
Fan Du283bc9f2013-11-07 17:47:50 +08001346 spin_lock_bh(&net->xfrm.xfrm_state_lock);
David S. Milleredcd5822006-08-24 00:42:45 -07001347 x1 = __xfrm_state_locate(x, use_spi, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
1349 err = -ESRCH;
1350 if (!x1)
1351 goto out;
1352
1353 if (xfrm_state_kern(x1)) {
David S. Miller37b08e32008-09-02 20:14:15 -07001354 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 err = -EEXIST;
1356 goto out;
1357 }
1358
1359 if (x1->km.state == XFRM_STATE_ACQ) {
1360 __xfrm_state_insert(x);
1361 x = NULL;
1362 }
1363 err = 0;
1364
1365out:
Fan Du283bc9f2013-11-07 17:47:50 +08001366 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
David S. Miller37b08e32008-09-02 20:14:15 -07001368 if (to_put)
1369 xfrm_state_put(to_put);
1370
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 if (err)
1372 return err;
1373
1374 if (!x) {
1375 xfrm_state_delete(x1);
1376 xfrm_state_put(x1);
1377 return 0;
1378 }
1379
1380 err = -EINVAL;
1381 spin_lock_bh(&x1->lock);
1382 if (likely(x1->km.state == XFRM_STATE_VALID)) {
1383 if (x->encap && x1->encap)
1384 memcpy(x1->encap, x->encap, sizeof(*x1->encap));
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -07001385 if (x->coaddr && x1->coaddr) {
1386 memcpy(x1->coaddr, x->coaddr, sizeof(*x1->coaddr));
1387 }
1388 if (!use_spi && memcmp(&x1->sel, &x->sel, sizeof(x1->sel)))
1389 memcpy(&x1->sel, &x->sel, sizeof(x1->sel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 memcpy(&x1->lft, &x->lft, sizeof(x1->lft));
1391 x1->km.dying = 0;
1392
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -08001393 tasklet_hrtimer_start(&x1->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 if (x1->curlft.use_time)
1395 xfrm_state_check_expire(x1);
1396
1397 err = 0;
Tushar Gohad8fcbc632011-07-07 15:38:52 +00001398 x->km.state = XFRM_STATE_DEAD;
1399 __xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 }
1401 spin_unlock_bh(&x1->lock);
1402
1403 xfrm_state_put(x1);
1404
1405 return err;
1406}
1407EXPORT_SYMBOL(xfrm_state_update);
1408
1409int xfrm_state_check_expire(struct xfrm_state *x)
1410{
1411 if (!x->curlft.use_time)
James Morris9d729f72007-03-04 16:12:44 -08001412 x->curlft.use_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 if (x->curlft.bytes >= x->lft.hard_byte_limit ||
1415 x->curlft.packets >= x->lft.hard_packet_limit) {
Herbert Xu4666faa2005-06-18 22:43:22 -07001416 x->km.state = XFRM_STATE_EXPIRED;
Weilong Chen9b7a7872013-12-24 09:43:46 +08001417 tasklet_hrtimer_start(&x->mtimer, ktime_set(0, 0), HRTIMER_MODE_REL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 return -EINVAL;
1419 }
1420
1421 if (!x->km.dying &&
1422 (x->curlft.bytes >= x->lft.soft_byte_limit ||
Herbert Xu4666faa2005-06-18 22:43:22 -07001423 x->curlft.packets >= x->lft.soft_packet_limit)) {
1424 x->km.dying = 1;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001425 km_state_expired(x, 0, 0);
Herbert Xu4666faa2005-06-18 22:43:22 -07001426 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 return 0;
1428}
1429EXPORT_SYMBOL(xfrm_state_check_expire);
1430
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431struct xfrm_state *
David S. Millera70486f2011-02-27 23:17:24 -08001432xfrm_state_lookup(struct net *net, u32 mark, const xfrm_address_t *daddr, __be32 spi,
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001433 u8 proto, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434{
1435 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
Florian Westphalc2f672f2016-09-20 15:45:26 +02001437 rcu_read_lock();
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001438 x = __xfrm_state_lookup(net, mark, daddr, spi, proto, family);
Florian Westphalc2f672f2016-09-20 15:45:26 +02001439 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 return x;
1441}
1442EXPORT_SYMBOL(xfrm_state_lookup);
1443
1444struct xfrm_state *
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001445xfrm_state_lookup_byaddr(struct net *net, u32 mark,
David S. Millera70486f2011-02-27 23:17:24 -08001446 const xfrm_address_t *daddr, const xfrm_address_t *saddr,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001447 u8 proto, unsigned short family)
1448{
1449 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001450
Fan Du283bc9f2013-11-07 17:47:50 +08001451 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001452 x = __xfrm_state_lookup_byaddr(net, mark, daddr, saddr, proto, family);
Fan Du283bc9f2013-11-07 17:47:50 +08001453 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001454 return x;
1455}
1456EXPORT_SYMBOL(xfrm_state_lookup_byaddr);
1457
1458struct xfrm_state *
Mathias Krausee473fcb2013-06-26 23:56:58 +02001459xfrm_find_acq(struct net *net, const struct xfrm_mark *mark, u8 mode, u32 reqid,
1460 u8 proto, const xfrm_address_t *daddr,
1461 const xfrm_address_t *saddr, int create, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462{
1463 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
Fan Du283bc9f2013-11-07 17:47:50 +08001465 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001466 x = __find_acq_core(net, mark, family, mode, reqid, proto, daddr, saddr, create);
Fan Du283bc9f2013-11-07 17:47:50 +08001467 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
David S. Miller27708342006-08-24 00:13:10 -07001468
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 return x;
1470}
1471EXPORT_SYMBOL(xfrm_find_acq);
1472
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001473#ifdef CONFIG_XFRM_SUB_POLICY
1474int
1475xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n,
Fan Du283bc9f2013-11-07 17:47:50 +08001476 unsigned short family, struct net *net)
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001477{
1478 int err = 0;
1479 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
1480 if (!afinfo)
1481 return -EAFNOSUPPORT;
1482
Fan Du283bc9f2013-11-07 17:47:50 +08001483 spin_lock_bh(&net->xfrm.xfrm_state_lock); /*FIXME*/
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001484 if (afinfo->tmpl_sort)
1485 err = afinfo->tmpl_sort(dst, src, n);
Fan Du283bc9f2013-11-07 17:47:50 +08001486 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001487 xfrm_state_put_afinfo(afinfo);
1488 return err;
1489}
1490EXPORT_SYMBOL(xfrm_tmpl_sort);
1491
1492int
1493xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src, int n,
1494 unsigned short family)
1495{
1496 int err = 0;
1497 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
Steffen Klassert35ea790d2014-02-19 13:33:23 +01001498 struct net *net = xs_net(*src);
Fan Du283bc9f2013-11-07 17:47:50 +08001499
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001500 if (!afinfo)
1501 return -EAFNOSUPPORT;
1502
Fan Du283bc9f2013-11-07 17:47:50 +08001503 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001504 if (afinfo->state_sort)
1505 err = afinfo->state_sort(dst, src, n);
Fan Du283bc9f2013-11-07 17:47:50 +08001506 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001507 xfrm_state_put_afinfo(afinfo);
1508 return err;
1509}
1510EXPORT_SYMBOL(xfrm_state_sort);
1511#endif
1512
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513/* Silly enough, but I'm lazy to build resolution list */
1514
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001515static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516{
1517 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001519 for (i = 0; i <= net->xfrm.state_hmask; i++) {
David S. Miller8f126e32006-08-24 02:45:07 -07001520 struct xfrm_state *x;
1521
Sasha Levinb67bfe02013-02-27 17:06:00 -08001522 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
David S. Miller8f126e32006-08-24 02:45:07 -07001523 if (x->km.seq == seq &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001524 (mark & x->mark.m) == x->mark.v &&
David S. Miller8f126e32006-08-24 02:45:07 -07001525 x->km.state == XFRM_STATE_ACQ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 xfrm_state_hold(x);
1527 return x;
1528 }
1529 }
1530 }
1531 return NULL;
1532}
1533
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001534struct xfrm_state *xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535{
1536 struct xfrm_state *x;
1537
Fan Du283bc9f2013-11-07 17:47:50 +08001538 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001539 x = __xfrm_find_acq_byseq(net, mark, seq);
Fan Du283bc9f2013-11-07 17:47:50 +08001540 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 return x;
1542}
1543EXPORT_SYMBOL(xfrm_find_acq_byseq);
1544
1545u32 xfrm_get_acqseq(void)
1546{
1547 u32 res;
jamal6836b9b2010-02-16 02:01:22 +00001548 static atomic_t acqseq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
jamal6836b9b2010-02-16 02:01:22 +00001550 do {
1551 res = atomic_inc_return(&acqseq);
1552 } while (!res);
1553
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 return res;
1555}
1556EXPORT_SYMBOL(xfrm_get_acqseq);
1557
Fan Du776e9dd2013-12-16 18:47:49 +08001558int verify_spi_info(u8 proto, u32 min, u32 max)
1559{
1560 switch (proto) {
1561 case IPPROTO_AH:
1562 case IPPROTO_ESP:
1563 break;
1564
1565 case IPPROTO_COMP:
1566 /* IPCOMP spi is 16-bits. */
1567 if (max >= 0x10000)
1568 return -EINVAL;
1569 break;
1570
1571 default:
1572 return -EINVAL;
1573 }
1574
1575 if (min > max)
1576 return -EINVAL;
1577
1578 return 0;
1579}
1580EXPORT_SYMBOL(verify_spi_info);
1581
Herbert Xu658b2192007-10-09 13:29:52 -07001582int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -08001584 struct net *net = xs_net(x);
David S. Millerf034b5d2006-08-24 03:08:07 -07001585 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 struct xfrm_state *x0;
Herbert Xu658b2192007-10-09 13:29:52 -07001587 int err = -ENOENT;
1588 __be32 minspi = htonl(low);
1589 __be32 maxspi = htonl(high);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001590 u32 mark = x->mark.v & x->mark.m;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
Herbert Xu658b2192007-10-09 13:29:52 -07001592 spin_lock_bh(&x->lock);
1593 if (x->km.state == XFRM_STATE_DEAD)
1594 goto unlock;
1595
1596 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 if (x->id.spi)
Herbert Xu658b2192007-10-09 13:29:52 -07001598 goto unlock;
1599
1600 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601
1602 if (minspi == maxspi) {
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001603 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, minspi, x->id.proto, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 if (x0) {
1605 xfrm_state_put(x0);
Herbert Xu658b2192007-10-09 13:29:52 -07001606 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 }
1608 x->id.spi = minspi;
1609 } else {
1610 u32 spi = 0;
Weilong Chen9b7a7872013-12-24 09:43:46 +08001611 for (h = 0; h < high-low+1; h++) {
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05001612 spi = low + prandom_u32()%(high-low+1);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001613 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, htonl(spi), x->id.proto, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 if (x0 == NULL) {
1615 x->id.spi = htonl(spi);
1616 break;
1617 }
1618 xfrm_state_put(x0);
1619 }
1620 }
1621 if (x->id.spi) {
Fan Du283bc9f2013-11-07 17:47:50 +08001622 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Alexey Dobriyan12604d82008-11-25 17:31:18 -08001623 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, x->props.family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001624 hlist_add_head_rcu(&x->byspi, net->xfrm.state_byspi + h);
Fan Du283bc9f2013-11-07 17:47:50 +08001625 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Herbert Xu658b2192007-10-09 13:29:52 -07001626
1627 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 }
Herbert Xu658b2192007-10-09 13:29:52 -07001629
1630unlock:
1631 spin_unlock_bh(&x->lock);
1632
1633 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634}
1635EXPORT_SYMBOL(xfrm_alloc_spi);
1636
Nicolas Dichteld3623092014-02-14 15:30:36 +01001637static bool __xfrm_state_filter_match(struct xfrm_state *x,
Nicolas Dichtel870a2df2014-03-06 18:24:29 +01001638 struct xfrm_address_filter *filter)
Nicolas Dichteld3623092014-02-14 15:30:36 +01001639{
1640 if (filter) {
1641 if ((filter->family == AF_INET ||
1642 filter->family == AF_INET6) &&
1643 x->props.family != filter->family)
1644 return false;
1645
1646 return addr_match(&x->props.saddr, &filter->saddr,
1647 filter->splen) &&
1648 addr_match(&x->id.daddr, &filter->daddr,
1649 filter->dplen);
1650 }
1651 return true;
1652}
1653
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08001654int xfrm_state_walk(struct net *net, struct xfrm_state_walk *walk,
Timo Teras4c563f72008-02-28 21:31:08 -08001655 int (*func)(struct xfrm_state *, int, void*),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 void *data)
1657{
Herbert Xu12a169e2008-10-01 07:03:24 -07001658 struct xfrm_state *state;
1659 struct xfrm_state_walk *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 int err = 0;
1661
Herbert Xu12a169e2008-10-01 07:03:24 -07001662 if (walk->seq != 0 && list_empty(&walk->all))
Timo Teras4c563f72008-02-28 21:31:08 -08001663 return 0;
1664
Fan Du283bc9f2013-11-07 17:47:50 +08001665 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001666 if (list_empty(&walk->all))
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08001667 x = list_first_entry(&net->xfrm.state_all, struct xfrm_state_walk, all);
Herbert Xu12a169e2008-10-01 07:03:24 -07001668 else
Li RongQing80077702015-04-22 17:09:54 +08001669 x = list_first_entry(&walk->all, struct xfrm_state_walk, all);
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08001670 list_for_each_entry_from(x, &net->xfrm.state_all, all) {
Herbert Xu12a169e2008-10-01 07:03:24 -07001671 if (x->state == XFRM_STATE_DEAD)
Timo Teras4c563f72008-02-28 21:31:08 -08001672 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07001673 state = container_of(x, struct xfrm_state, km);
1674 if (!xfrm_id_proto_match(state->id.proto, walk->proto))
Timo Teras4c563f72008-02-28 21:31:08 -08001675 continue;
Nicolas Dichteld3623092014-02-14 15:30:36 +01001676 if (!__xfrm_state_filter_match(state, walk->filter))
1677 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07001678 err = func(state, walk->seq, data);
1679 if (err) {
1680 list_move_tail(&walk->all, &x->all);
1681 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001683 walk->seq++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001685 if (walk->seq == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 err = -ENOENT;
1687 goto out;
1688 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001689 list_del_init(&walk->all);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690out:
Fan Du283bc9f2013-11-07 17:47:50 +08001691 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 return err;
1693}
1694EXPORT_SYMBOL(xfrm_state_walk);
1695
Nicolas Dichteld3623092014-02-14 15:30:36 +01001696void xfrm_state_walk_init(struct xfrm_state_walk *walk, u8 proto,
Nicolas Dichtel870a2df2014-03-06 18:24:29 +01001697 struct xfrm_address_filter *filter)
Herbert Xu5c182452008-09-22 19:48:19 -07001698{
Herbert Xu12a169e2008-10-01 07:03:24 -07001699 INIT_LIST_HEAD(&walk->all);
Herbert Xu5c182452008-09-22 19:48:19 -07001700 walk->proto = proto;
Herbert Xu12a169e2008-10-01 07:03:24 -07001701 walk->state = XFRM_STATE_DEAD;
1702 walk->seq = 0;
Nicolas Dichteld3623092014-02-14 15:30:36 +01001703 walk->filter = filter;
Herbert Xu5c182452008-09-22 19:48:19 -07001704}
1705EXPORT_SYMBOL(xfrm_state_walk_init);
1706
Fan Du283bc9f2013-11-07 17:47:50 +08001707void xfrm_state_walk_done(struct xfrm_state_walk *walk, struct net *net)
Herbert Xuabb81c42008-09-09 19:58:29 -07001708{
Nicolas Dichteld3623092014-02-14 15:30:36 +01001709 kfree(walk->filter);
1710
Herbert Xu12a169e2008-10-01 07:03:24 -07001711 if (list_empty(&walk->all))
Herbert Xu5c182452008-09-22 19:48:19 -07001712 return;
Herbert Xu5c182452008-09-22 19:48:19 -07001713
Fan Du283bc9f2013-11-07 17:47:50 +08001714 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001715 list_del(&walk->all);
Fan Du283bc9f2013-11-07 17:47:50 +08001716 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Herbert Xuabb81c42008-09-09 19:58:29 -07001717}
1718EXPORT_SYMBOL(xfrm_state_walk_done);
1719
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001720static void xfrm_replay_timer_handler(unsigned long data)
1721{
Weilong Chen3e94c2d2013-12-24 09:43:47 +08001722 struct xfrm_state *x = (struct xfrm_state *)data;
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001723
1724 spin_lock(&x->lock);
1725
Jamal Hadi Salim27170962006-04-14 15:03:05 -07001726 if (x->km.state == XFRM_STATE_VALID) {
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001727 if (xfrm_aevent_is_on(xs_net(x)))
Steffen Klassert9fdc4882011-03-08 00:08:32 +00001728 x->repl->notify(x, XFRM_REPLAY_TIMEOUT);
Jamal Hadi Salim27170962006-04-14 15:03:05 -07001729 else
1730 x->xflags |= XFRM_TIME_DEFER;
1731 }
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001732
1733 spin_unlock(&x->lock);
1734}
1735
Denis Chengdf018122007-12-07 00:51:11 -08001736static LIST_HEAD(xfrm_km_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737
David S. Miller214e0052011-02-24 00:02:38 -05001738void km_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739{
1740 struct xfrm_mgr *km;
1741
Cong Wang85168c02013-01-16 16:05:06 +08001742 rcu_read_lock();
1743 list_for_each_entry_rcu(km, &xfrm_km_list, list)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001744 if (km->notify_policy)
1745 km->notify_policy(xp, dir, c);
Cong Wang85168c02013-01-16 16:05:06 +08001746 rcu_read_unlock();
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001747}
1748
David S. Miller214e0052011-02-24 00:02:38 -05001749void km_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001750{
1751 struct xfrm_mgr *km;
Cong Wang85168c02013-01-16 16:05:06 +08001752 rcu_read_lock();
1753 list_for_each_entry_rcu(km, &xfrm_km_list, list)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001754 if (km->notify)
1755 km->notify(x, c);
Cong Wang85168c02013-01-16 16:05:06 +08001756 rcu_read_unlock();
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001757}
1758
1759EXPORT_SYMBOL(km_policy_notify);
1760EXPORT_SYMBOL(km_state_notify);
1761
Eric W. Biederman15e47302012-09-07 20:12:54 +00001762void km_state_expired(struct xfrm_state *x, int hard, u32 portid)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001763{
1764 struct km_event c;
1765
Herbert Xubf088672005-06-18 22:44:00 -07001766 c.data.hard = hard;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001767 c.portid = portid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001768 c.event = XFRM_MSG_EXPIRE;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001769 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770}
1771
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001772EXPORT_SYMBOL(km_state_expired);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001773/*
1774 * We send to all registered managers regardless of failure
1775 * We are happy with one success
1776*/
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001777int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001779 int err = -EINVAL, acqret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 struct xfrm_mgr *km;
1781
Cong Wang85168c02013-01-16 16:05:06 +08001782 rcu_read_lock();
1783 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Fan Du65e07362012-08-15 10:13:47 +08001784 acqret = km->acquire(x, t, pol);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001785 if (!acqret)
1786 err = acqret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 }
Cong Wang85168c02013-01-16 16:05:06 +08001788 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 return err;
1790}
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001791EXPORT_SYMBOL(km_query);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
Al Viro5d36b182006-11-08 00:24:06 -08001793int km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794{
1795 int err = -EINVAL;
1796 struct xfrm_mgr *km;
1797
Cong Wang85168c02013-01-16 16:05:06 +08001798 rcu_read_lock();
1799 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 if (km->new_mapping)
1801 err = km->new_mapping(x, ipaddr, sport);
1802 if (!err)
1803 break;
1804 }
Cong Wang85168c02013-01-16 16:05:06 +08001805 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 return err;
1807}
1808EXPORT_SYMBOL(km_new_mapping);
1809
Eric W. Biederman15e47302012-09-07 20:12:54 +00001810void km_policy_expired(struct xfrm_policy *pol, int dir, int hard, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001812 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813
Herbert Xubf088672005-06-18 22:44:00 -07001814 c.data.hard = hard;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001815 c.portid = portid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001816 c.event = XFRM_MSG_POLEXPIRE;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001817 km_policy_notify(pol, dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818}
David S. Millera70fcb02006-03-20 19:18:52 -08001819EXPORT_SYMBOL(km_policy_expired);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820
Eric Dumazet2d60abc2008-01-03 20:43:21 -08001821#ifdef CONFIG_XFRM_MIGRATE
David S. Miller183cad12011-02-24 00:28:01 -05001822int km_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
1823 const struct xfrm_migrate *m, int num_migrate,
1824 const struct xfrm_kmaddress *k)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001825{
1826 int err = -EINVAL;
1827 int ret;
1828 struct xfrm_mgr *km;
1829
Cong Wang85168c02013-01-16 16:05:06 +08001830 rcu_read_lock();
1831 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001832 if (km->migrate) {
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001833 ret = km->migrate(sel, dir, type, m, num_migrate, k);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001834 if (!ret)
1835 err = ret;
1836 }
1837 }
Cong Wang85168c02013-01-16 16:05:06 +08001838 rcu_read_unlock();
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001839 return err;
1840}
1841EXPORT_SYMBOL(km_migrate);
Eric Dumazet2d60abc2008-01-03 20:43:21 -08001842#endif
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001843
Alexey Dobriyandb983c12008-11-25 17:51:01 -08001844int km_report(struct net *net, u8 proto, struct xfrm_selector *sel, xfrm_address_t *addr)
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001845{
1846 int err = -EINVAL;
1847 int ret;
1848 struct xfrm_mgr *km;
1849
Cong Wang85168c02013-01-16 16:05:06 +08001850 rcu_read_lock();
1851 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001852 if (km->report) {
Alexey Dobriyandb983c12008-11-25 17:51:01 -08001853 ret = km->report(net, proto, sel, addr);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001854 if (!ret)
1855 err = ret;
1856 }
1857 }
Cong Wang85168c02013-01-16 16:05:06 +08001858 rcu_read_unlock();
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001859 return err;
1860}
1861EXPORT_SYMBOL(km_report);
1862
Horia Geanta0f245582014-02-12 16:20:06 +02001863bool km_is_alive(const struct km_event *c)
1864{
1865 struct xfrm_mgr *km;
1866 bool is_alive = false;
1867
1868 rcu_read_lock();
1869 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
1870 if (km->is_alive && km->is_alive(c)) {
1871 is_alive = true;
1872 break;
1873 }
1874 }
1875 rcu_read_unlock();
1876
1877 return is_alive;
1878}
1879EXPORT_SYMBOL(km_is_alive);
1880
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881int xfrm_user_policy(struct sock *sk, int optname, u8 __user *optval, int optlen)
1882{
1883 int err;
1884 u8 *data;
1885 struct xfrm_mgr *km;
1886 struct xfrm_policy *pol = NULL;
1887
Steffen Klassert02a5b412018-02-01 08:49:23 +01001888#ifdef CONFIG_COMPAT
1889 if (in_compat_syscall())
1890 return -EOPNOTSUPP;
1891#endif
1892
Lorenzo Colitti38884cc2017-11-20 19:26:02 +09001893 if (!optval && !optlen) {
1894 xfrm_sk_policy_insert(sk, XFRM_POLICY_IN, NULL);
1895 xfrm_sk_policy_insert(sk, XFRM_POLICY_OUT, NULL);
1896 __sk_dst_reset(sk);
1897 return 0;
1898 }
1899
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 if (optlen <= 0 || optlen > PAGE_SIZE)
1901 return -EMSGSIZE;
1902
1903 data = kmalloc(optlen, GFP_KERNEL);
1904 if (!data)
1905 return -ENOMEM;
1906
1907 err = -EFAULT;
1908 if (copy_from_user(data, optval, optlen))
1909 goto out;
1910
1911 err = -EINVAL;
Cong Wang85168c02013-01-16 16:05:06 +08001912 rcu_read_lock();
1913 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07001914 pol = km->compile_policy(sk, optname, data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 optlen, &err);
1916 if (err >= 0)
1917 break;
1918 }
Cong Wang85168c02013-01-16 16:05:06 +08001919 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920
1921 if (err >= 0) {
1922 xfrm_sk_policy_insert(sk, err, pol);
1923 xfrm_pol_put(pol);
Jonathan Basserib0240c42017-10-25 09:52:27 -07001924 __sk_dst_reset(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 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 */