blob: 419bf5d463bde118274e9f4dc1fffba08f75f258 [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;
1249
1250 return x;
1251
1252 error:
Herbert Xu553f9112010-02-15 20:00:51 +00001253 xfrm_state_put(x);
1254out:
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001255 return NULL;
1256}
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001257
Fan Du283bc9f2013-11-07 17:47:50 +08001258struct xfrm_state *xfrm_migrate_state_find(struct xfrm_migrate *m, struct net *net)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001259{
1260 unsigned int h;
Steffen Klassert8c0cba22014-02-19 13:33:24 +01001261 struct xfrm_state *x = NULL;
1262
1263 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001264
1265 if (m->reqid) {
Fan Du283bc9f2013-11-07 17:47:50 +08001266 h = xfrm_dst_hash(net, &m->old_daddr, &m->old_saddr,
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001267 m->reqid, m->old_family);
Fan Du283bc9f2013-11-07 17:47:50 +08001268 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001269 if (x->props.mode != m->mode ||
1270 x->id.proto != m->proto)
1271 continue;
1272 if (m->reqid && x->props.reqid != m->reqid)
1273 continue;
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001274 if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr,
1275 m->old_family) ||
1276 !xfrm_addr_equal(&x->props.saddr, &m->old_saddr,
1277 m->old_family))
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001278 continue;
1279 xfrm_state_hold(x);
Steffen Klassert8c0cba22014-02-19 13:33:24 +01001280 break;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001281 }
1282 } else {
Fan Du283bc9f2013-11-07 17:47:50 +08001283 h = xfrm_src_hash(net, &m->old_daddr, &m->old_saddr,
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001284 m->old_family);
Fan Du283bc9f2013-11-07 17:47:50 +08001285 hlist_for_each_entry(x, net->xfrm.state_bysrc+h, bysrc) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001286 if (x->props.mode != m->mode ||
1287 x->id.proto != m->proto)
1288 continue;
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001289 if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr,
1290 m->old_family) ||
1291 !xfrm_addr_equal(&x->props.saddr, &m->old_saddr,
1292 m->old_family))
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001293 continue;
1294 xfrm_state_hold(x);
Steffen Klassert8c0cba22014-02-19 13:33:24 +01001295 break;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001296 }
1297 }
1298
Steffen Klassert8c0cba22014-02-19 13:33:24 +01001299 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1300
1301 return x;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001302}
1303EXPORT_SYMBOL(xfrm_migrate_state_find);
1304
Weilong Chen3e94c2d2013-12-24 09:43:47 +08001305struct xfrm_state *xfrm_state_migrate(struct xfrm_state *x,
1306 struct xfrm_migrate *m)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001307{
1308 struct xfrm_state *xc;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001309
Steffen Klassertcc9ab602014-02-19 13:33:24 +01001310 xc = xfrm_state_clone(x);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001311 if (!xc)
1312 return NULL;
1313
1314 memcpy(&xc->id.daddr, &m->new_daddr, sizeof(xc->id.daddr));
1315 memcpy(&xc->props.saddr, &m->new_saddr, sizeof(xc->props.saddr));
1316
1317 /* add state */
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001318 if (xfrm_addr_equal(&x->id.daddr, &m->new_daddr, m->new_family)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001319 /* a care is needed when the destination address of the
1320 state is to be updated as it is a part of triplet */
1321 xfrm_state_insert(xc);
1322 } else {
Steffen Klassertcc9ab602014-02-19 13:33:24 +01001323 if (xfrm_state_add(xc) < 0)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001324 goto error;
1325 }
1326
1327 return xc;
1328error:
Thomas Egerer78347c82010-12-06 23:28:56 +00001329 xfrm_state_put(xc);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001330 return NULL;
1331}
1332EXPORT_SYMBOL(xfrm_state_migrate);
1333#endif
1334
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335int xfrm_state_update(struct xfrm_state *x)
1336{
David S. Miller37b08e32008-09-02 20:14:15 -07001337 struct xfrm_state *x1, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 int err;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001339 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
Fan Du283bc9f2013-11-07 17:47:50 +08001340 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
David S. Miller37b08e32008-09-02 20:14:15 -07001342 to_put = NULL;
1343
Fan Du283bc9f2013-11-07 17:47:50 +08001344 spin_lock_bh(&net->xfrm.xfrm_state_lock);
David S. Milleredcd5822006-08-24 00:42:45 -07001345 x1 = __xfrm_state_locate(x, use_spi, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
1347 err = -ESRCH;
1348 if (!x1)
1349 goto out;
1350
1351 if (xfrm_state_kern(x1)) {
David S. Miller37b08e32008-09-02 20:14:15 -07001352 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 err = -EEXIST;
1354 goto out;
1355 }
1356
1357 if (x1->km.state == XFRM_STATE_ACQ) {
1358 __xfrm_state_insert(x);
1359 x = NULL;
1360 }
1361 err = 0;
1362
1363out:
Fan Du283bc9f2013-11-07 17:47:50 +08001364 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
David S. Miller37b08e32008-09-02 20:14:15 -07001366 if (to_put)
1367 xfrm_state_put(to_put);
1368
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 if (err)
1370 return err;
1371
1372 if (!x) {
1373 xfrm_state_delete(x1);
1374 xfrm_state_put(x1);
1375 return 0;
1376 }
1377
1378 err = -EINVAL;
1379 spin_lock_bh(&x1->lock);
1380 if (likely(x1->km.state == XFRM_STATE_VALID)) {
1381 if (x->encap && x1->encap)
1382 memcpy(x1->encap, x->encap, sizeof(*x1->encap));
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -07001383 if (x->coaddr && x1->coaddr) {
1384 memcpy(x1->coaddr, x->coaddr, sizeof(*x1->coaddr));
1385 }
1386 if (!use_spi && memcmp(&x1->sel, &x->sel, sizeof(x1->sel)))
1387 memcpy(&x1->sel, &x->sel, sizeof(x1->sel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 memcpy(&x1->lft, &x->lft, sizeof(x1->lft));
1389 x1->km.dying = 0;
1390
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -08001391 tasklet_hrtimer_start(&x1->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 if (x1->curlft.use_time)
1393 xfrm_state_check_expire(x1);
1394
1395 err = 0;
Tushar Gohad8fcbc632011-07-07 15:38:52 +00001396 x->km.state = XFRM_STATE_DEAD;
1397 __xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 }
1399 spin_unlock_bh(&x1->lock);
1400
1401 xfrm_state_put(x1);
1402
1403 return err;
1404}
1405EXPORT_SYMBOL(xfrm_state_update);
1406
1407int xfrm_state_check_expire(struct xfrm_state *x)
1408{
1409 if (!x->curlft.use_time)
James Morris9d729f72007-03-04 16:12:44 -08001410 x->curlft.use_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 if (x->curlft.bytes >= x->lft.hard_byte_limit ||
1413 x->curlft.packets >= x->lft.hard_packet_limit) {
Herbert Xu4666faa2005-06-18 22:43:22 -07001414 x->km.state = XFRM_STATE_EXPIRED;
Weilong Chen9b7a7872013-12-24 09:43:46 +08001415 tasklet_hrtimer_start(&x->mtimer, ktime_set(0, 0), HRTIMER_MODE_REL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 return -EINVAL;
1417 }
1418
1419 if (!x->km.dying &&
1420 (x->curlft.bytes >= x->lft.soft_byte_limit ||
Herbert Xu4666faa2005-06-18 22:43:22 -07001421 x->curlft.packets >= x->lft.soft_packet_limit)) {
1422 x->km.dying = 1;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001423 km_state_expired(x, 0, 0);
Herbert Xu4666faa2005-06-18 22:43:22 -07001424 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 return 0;
1426}
1427EXPORT_SYMBOL(xfrm_state_check_expire);
1428
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429struct xfrm_state *
David S. Millera70486f2011-02-27 23:17:24 -08001430xfrm_state_lookup(struct net *net, u32 mark, const xfrm_address_t *daddr, __be32 spi,
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001431 u8 proto, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432{
1433 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
Florian Westphalc2f672f2016-09-20 15:45:26 +02001435 rcu_read_lock();
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001436 x = __xfrm_state_lookup(net, mark, daddr, spi, proto, family);
Florian Westphalc2f672f2016-09-20 15:45:26 +02001437 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 return x;
1439}
1440EXPORT_SYMBOL(xfrm_state_lookup);
1441
1442struct xfrm_state *
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001443xfrm_state_lookup_byaddr(struct net *net, u32 mark,
David S. Millera70486f2011-02-27 23:17:24 -08001444 const xfrm_address_t *daddr, const xfrm_address_t *saddr,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001445 u8 proto, unsigned short family)
1446{
1447 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001448
Fan Du283bc9f2013-11-07 17:47:50 +08001449 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001450 x = __xfrm_state_lookup_byaddr(net, mark, daddr, saddr, proto, family);
Fan Du283bc9f2013-11-07 17:47:50 +08001451 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001452 return x;
1453}
1454EXPORT_SYMBOL(xfrm_state_lookup_byaddr);
1455
1456struct xfrm_state *
Mathias Krausee473fcb2013-06-26 23:56:58 +02001457xfrm_find_acq(struct net *net, const struct xfrm_mark *mark, u8 mode, u32 reqid,
1458 u8 proto, const xfrm_address_t *daddr,
1459 const xfrm_address_t *saddr, int create, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460{
1461 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
Fan Du283bc9f2013-11-07 17:47:50 +08001463 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001464 x = __find_acq_core(net, mark, family, mode, reqid, proto, daddr, saddr, create);
Fan Du283bc9f2013-11-07 17:47:50 +08001465 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
David S. Miller27708342006-08-24 00:13:10 -07001466
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 return x;
1468}
1469EXPORT_SYMBOL(xfrm_find_acq);
1470
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001471#ifdef CONFIG_XFRM_SUB_POLICY
1472int
1473xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n,
Fan Du283bc9f2013-11-07 17:47:50 +08001474 unsigned short family, struct net *net)
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001475{
1476 int err = 0;
1477 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
1478 if (!afinfo)
1479 return -EAFNOSUPPORT;
1480
Fan Du283bc9f2013-11-07 17:47:50 +08001481 spin_lock_bh(&net->xfrm.xfrm_state_lock); /*FIXME*/
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001482 if (afinfo->tmpl_sort)
1483 err = afinfo->tmpl_sort(dst, src, n);
Fan Du283bc9f2013-11-07 17:47:50 +08001484 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001485 xfrm_state_put_afinfo(afinfo);
1486 return err;
1487}
1488EXPORT_SYMBOL(xfrm_tmpl_sort);
1489
1490int
1491xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src, int n,
1492 unsigned short family)
1493{
1494 int err = 0;
1495 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
Steffen Klassert35ea790d2014-02-19 13:33:23 +01001496 struct net *net = xs_net(*src);
Fan Du283bc9f2013-11-07 17:47:50 +08001497
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001498 if (!afinfo)
1499 return -EAFNOSUPPORT;
1500
Fan Du283bc9f2013-11-07 17:47:50 +08001501 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001502 if (afinfo->state_sort)
1503 err = afinfo->state_sort(dst, src, n);
Fan Du283bc9f2013-11-07 17:47:50 +08001504 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001505 xfrm_state_put_afinfo(afinfo);
1506 return err;
1507}
1508EXPORT_SYMBOL(xfrm_state_sort);
1509#endif
1510
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511/* Silly enough, but I'm lazy to build resolution list */
1512
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001513static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514{
1515 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001517 for (i = 0; i <= net->xfrm.state_hmask; i++) {
David S. Miller8f126e32006-08-24 02:45:07 -07001518 struct xfrm_state *x;
1519
Sasha Levinb67bfe02013-02-27 17:06:00 -08001520 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
David S. Miller8f126e32006-08-24 02:45:07 -07001521 if (x->km.seq == seq &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001522 (mark & x->mark.m) == x->mark.v &&
David S. Miller8f126e32006-08-24 02:45:07 -07001523 x->km.state == XFRM_STATE_ACQ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 xfrm_state_hold(x);
1525 return x;
1526 }
1527 }
1528 }
1529 return NULL;
1530}
1531
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001532struct xfrm_state *xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533{
1534 struct xfrm_state *x;
1535
Fan Du283bc9f2013-11-07 17:47:50 +08001536 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001537 x = __xfrm_find_acq_byseq(net, mark, seq);
Fan Du283bc9f2013-11-07 17:47:50 +08001538 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 return x;
1540}
1541EXPORT_SYMBOL(xfrm_find_acq_byseq);
1542
1543u32 xfrm_get_acqseq(void)
1544{
1545 u32 res;
jamal6836b9b2010-02-16 02:01:22 +00001546 static atomic_t acqseq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547
jamal6836b9b2010-02-16 02:01:22 +00001548 do {
1549 res = atomic_inc_return(&acqseq);
1550 } while (!res);
1551
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 return res;
1553}
1554EXPORT_SYMBOL(xfrm_get_acqseq);
1555
Fan Du776e9dd2013-12-16 18:47:49 +08001556int verify_spi_info(u8 proto, u32 min, u32 max)
1557{
1558 switch (proto) {
1559 case IPPROTO_AH:
1560 case IPPROTO_ESP:
1561 break;
1562
1563 case IPPROTO_COMP:
1564 /* IPCOMP spi is 16-bits. */
1565 if (max >= 0x10000)
1566 return -EINVAL;
1567 break;
1568
1569 default:
1570 return -EINVAL;
1571 }
1572
1573 if (min > max)
1574 return -EINVAL;
1575
1576 return 0;
1577}
1578EXPORT_SYMBOL(verify_spi_info);
1579
Herbert Xu658b2192007-10-09 13:29:52 -07001580int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -08001582 struct net *net = xs_net(x);
David S. Millerf034b5d2006-08-24 03:08:07 -07001583 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 struct xfrm_state *x0;
Herbert Xu658b2192007-10-09 13:29:52 -07001585 int err = -ENOENT;
1586 __be32 minspi = htonl(low);
1587 __be32 maxspi = htonl(high);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001588 u32 mark = x->mark.v & x->mark.m;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589
Herbert Xu658b2192007-10-09 13:29:52 -07001590 spin_lock_bh(&x->lock);
1591 if (x->km.state == XFRM_STATE_DEAD)
1592 goto unlock;
1593
1594 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 if (x->id.spi)
Herbert Xu658b2192007-10-09 13:29:52 -07001596 goto unlock;
1597
1598 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
1600 if (minspi == maxspi) {
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001601 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, minspi, x->id.proto, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 if (x0) {
1603 xfrm_state_put(x0);
Herbert Xu658b2192007-10-09 13:29:52 -07001604 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 }
1606 x->id.spi = minspi;
1607 } else {
1608 u32 spi = 0;
Weilong Chen9b7a7872013-12-24 09:43:46 +08001609 for (h = 0; h < high-low+1; h++) {
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05001610 spi = low + prandom_u32()%(high-low+1);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001611 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, htonl(spi), x->id.proto, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 if (x0 == NULL) {
1613 x->id.spi = htonl(spi);
1614 break;
1615 }
1616 xfrm_state_put(x0);
1617 }
1618 }
1619 if (x->id.spi) {
Fan Du283bc9f2013-11-07 17:47:50 +08001620 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Alexey Dobriyan12604d82008-11-25 17:31:18 -08001621 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, x->props.family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001622 hlist_add_head_rcu(&x->byspi, net->xfrm.state_byspi + h);
Fan Du283bc9f2013-11-07 17:47:50 +08001623 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Herbert Xu658b2192007-10-09 13:29:52 -07001624
1625 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 }
Herbert Xu658b2192007-10-09 13:29:52 -07001627
1628unlock:
1629 spin_unlock_bh(&x->lock);
1630
1631 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632}
1633EXPORT_SYMBOL(xfrm_alloc_spi);
1634
Nicolas Dichteld3623092014-02-14 15:30:36 +01001635static bool __xfrm_state_filter_match(struct xfrm_state *x,
Nicolas Dichtel870a2df2014-03-06 18:24:29 +01001636 struct xfrm_address_filter *filter)
Nicolas Dichteld3623092014-02-14 15:30:36 +01001637{
1638 if (filter) {
1639 if ((filter->family == AF_INET ||
1640 filter->family == AF_INET6) &&
1641 x->props.family != filter->family)
1642 return false;
1643
1644 return addr_match(&x->props.saddr, &filter->saddr,
1645 filter->splen) &&
1646 addr_match(&x->id.daddr, &filter->daddr,
1647 filter->dplen);
1648 }
1649 return true;
1650}
1651
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08001652int xfrm_state_walk(struct net *net, struct xfrm_state_walk *walk,
Timo Teras4c563f72008-02-28 21:31:08 -08001653 int (*func)(struct xfrm_state *, int, void*),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 void *data)
1655{
Herbert Xu12a169e2008-10-01 07:03:24 -07001656 struct xfrm_state *state;
1657 struct xfrm_state_walk *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 int err = 0;
1659
Herbert Xu12a169e2008-10-01 07:03:24 -07001660 if (walk->seq != 0 && list_empty(&walk->all))
Timo Teras4c563f72008-02-28 21:31:08 -08001661 return 0;
1662
Fan Du283bc9f2013-11-07 17:47:50 +08001663 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001664 if (list_empty(&walk->all))
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08001665 x = list_first_entry(&net->xfrm.state_all, struct xfrm_state_walk, all);
Herbert Xu12a169e2008-10-01 07:03:24 -07001666 else
Li RongQing80077702015-04-22 17:09:54 +08001667 x = list_first_entry(&walk->all, struct xfrm_state_walk, all);
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08001668 list_for_each_entry_from(x, &net->xfrm.state_all, all) {
Herbert Xu12a169e2008-10-01 07:03:24 -07001669 if (x->state == XFRM_STATE_DEAD)
Timo Teras4c563f72008-02-28 21:31:08 -08001670 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07001671 state = container_of(x, struct xfrm_state, km);
1672 if (!xfrm_id_proto_match(state->id.proto, walk->proto))
Timo Teras4c563f72008-02-28 21:31:08 -08001673 continue;
Nicolas Dichteld3623092014-02-14 15:30:36 +01001674 if (!__xfrm_state_filter_match(state, walk->filter))
1675 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07001676 err = func(state, walk->seq, data);
1677 if (err) {
1678 list_move_tail(&walk->all, &x->all);
1679 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001681 walk->seq++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001683 if (walk->seq == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 err = -ENOENT;
1685 goto out;
1686 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001687 list_del_init(&walk->all);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688out:
Fan Du283bc9f2013-11-07 17:47:50 +08001689 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 return err;
1691}
1692EXPORT_SYMBOL(xfrm_state_walk);
1693
Nicolas Dichteld3623092014-02-14 15:30:36 +01001694void xfrm_state_walk_init(struct xfrm_state_walk *walk, u8 proto,
Nicolas Dichtel870a2df2014-03-06 18:24:29 +01001695 struct xfrm_address_filter *filter)
Herbert Xu5c182452008-09-22 19:48:19 -07001696{
Herbert Xu12a169e2008-10-01 07:03:24 -07001697 INIT_LIST_HEAD(&walk->all);
Herbert Xu5c182452008-09-22 19:48:19 -07001698 walk->proto = proto;
Herbert Xu12a169e2008-10-01 07:03:24 -07001699 walk->state = XFRM_STATE_DEAD;
1700 walk->seq = 0;
Nicolas Dichteld3623092014-02-14 15:30:36 +01001701 walk->filter = filter;
Herbert Xu5c182452008-09-22 19:48:19 -07001702}
1703EXPORT_SYMBOL(xfrm_state_walk_init);
1704
Fan Du283bc9f2013-11-07 17:47:50 +08001705void xfrm_state_walk_done(struct xfrm_state_walk *walk, struct net *net)
Herbert Xuabb81c42008-09-09 19:58:29 -07001706{
Nicolas Dichteld3623092014-02-14 15:30:36 +01001707 kfree(walk->filter);
1708
Herbert Xu12a169e2008-10-01 07:03:24 -07001709 if (list_empty(&walk->all))
Herbert Xu5c182452008-09-22 19:48:19 -07001710 return;
Herbert Xu5c182452008-09-22 19:48:19 -07001711
Fan Du283bc9f2013-11-07 17:47:50 +08001712 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001713 list_del(&walk->all);
Fan Du283bc9f2013-11-07 17:47:50 +08001714 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Herbert Xuabb81c42008-09-09 19:58:29 -07001715}
1716EXPORT_SYMBOL(xfrm_state_walk_done);
1717
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001718static void xfrm_replay_timer_handler(unsigned long data)
1719{
Weilong Chen3e94c2d2013-12-24 09:43:47 +08001720 struct xfrm_state *x = (struct xfrm_state *)data;
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001721
1722 spin_lock(&x->lock);
1723
Jamal Hadi Salim27170962006-04-14 15:03:05 -07001724 if (x->km.state == XFRM_STATE_VALID) {
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001725 if (xfrm_aevent_is_on(xs_net(x)))
Steffen Klassert9fdc4882011-03-08 00:08:32 +00001726 x->repl->notify(x, XFRM_REPLAY_TIMEOUT);
Jamal Hadi Salim27170962006-04-14 15:03:05 -07001727 else
1728 x->xflags |= XFRM_TIME_DEFER;
1729 }
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001730
1731 spin_unlock(&x->lock);
1732}
1733
Denis Chengdf018122007-12-07 00:51:11 -08001734static LIST_HEAD(xfrm_km_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
David S. Miller214e0052011-02-24 00:02:38 -05001736void km_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737{
1738 struct xfrm_mgr *km;
1739
Cong Wang85168c02013-01-16 16:05:06 +08001740 rcu_read_lock();
1741 list_for_each_entry_rcu(km, &xfrm_km_list, list)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001742 if (km->notify_policy)
1743 km->notify_policy(xp, dir, c);
Cong Wang85168c02013-01-16 16:05:06 +08001744 rcu_read_unlock();
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001745}
1746
David S. Miller214e0052011-02-24 00:02:38 -05001747void km_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001748{
1749 struct xfrm_mgr *km;
Cong Wang85168c02013-01-16 16:05:06 +08001750 rcu_read_lock();
1751 list_for_each_entry_rcu(km, &xfrm_km_list, list)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001752 if (km->notify)
1753 km->notify(x, c);
Cong Wang85168c02013-01-16 16:05:06 +08001754 rcu_read_unlock();
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001755}
1756
1757EXPORT_SYMBOL(km_policy_notify);
1758EXPORT_SYMBOL(km_state_notify);
1759
Eric W. Biederman15e47302012-09-07 20:12:54 +00001760void km_state_expired(struct xfrm_state *x, int hard, u32 portid)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001761{
1762 struct km_event c;
1763
Herbert Xubf088672005-06-18 22:44:00 -07001764 c.data.hard = hard;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001765 c.portid = portid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001766 c.event = XFRM_MSG_EXPIRE;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001767 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768}
1769
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001770EXPORT_SYMBOL(km_state_expired);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001771/*
1772 * We send to all registered managers regardless of failure
1773 * We are happy with one success
1774*/
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001775int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001777 int err = -EINVAL, acqret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 struct xfrm_mgr *km;
1779
Cong Wang85168c02013-01-16 16:05:06 +08001780 rcu_read_lock();
1781 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Fan Du65e07362012-08-15 10:13:47 +08001782 acqret = km->acquire(x, t, pol);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001783 if (!acqret)
1784 err = acqret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 }
Cong Wang85168c02013-01-16 16:05:06 +08001786 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 return err;
1788}
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001789EXPORT_SYMBOL(km_query);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790
Al Viro5d36b182006-11-08 00:24:06 -08001791int km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792{
1793 int err = -EINVAL;
1794 struct xfrm_mgr *km;
1795
Cong Wang85168c02013-01-16 16:05:06 +08001796 rcu_read_lock();
1797 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 if (km->new_mapping)
1799 err = km->new_mapping(x, ipaddr, sport);
1800 if (!err)
1801 break;
1802 }
Cong Wang85168c02013-01-16 16:05:06 +08001803 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 return err;
1805}
1806EXPORT_SYMBOL(km_new_mapping);
1807
Eric W. Biederman15e47302012-09-07 20:12:54 +00001808void km_policy_expired(struct xfrm_policy *pol, int dir, int hard, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001810 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811
Herbert Xubf088672005-06-18 22:44:00 -07001812 c.data.hard = hard;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001813 c.portid = portid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001814 c.event = XFRM_MSG_POLEXPIRE;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001815 km_policy_notify(pol, dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816}
David S. Millera70fcb02006-03-20 19:18:52 -08001817EXPORT_SYMBOL(km_policy_expired);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818
Eric Dumazet2d60abc2008-01-03 20:43:21 -08001819#ifdef CONFIG_XFRM_MIGRATE
David S. Miller183cad12011-02-24 00:28:01 -05001820int km_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
1821 const struct xfrm_migrate *m, int num_migrate,
1822 const struct xfrm_kmaddress *k)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001823{
1824 int err = -EINVAL;
1825 int ret;
1826 struct xfrm_mgr *km;
1827
Cong Wang85168c02013-01-16 16:05:06 +08001828 rcu_read_lock();
1829 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001830 if (km->migrate) {
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001831 ret = km->migrate(sel, dir, type, m, num_migrate, k);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001832 if (!ret)
1833 err = ret;
1834 }
1835 }
Cong Wang85168c02013-01-16 16:05:06 +08001836 rcu_read_unlock();
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001837 return err;
1838}
1839EXPORT_SYMBOL(km_migrate);
Eric Dumazet2d60abc2008-01-03 20:43:21 -08001840#endif
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001841
Alexey Dobriyandb983c12008-11-25 17:51:01 -08001842int km_report(struct net *net, u8 proto, struct xfrm_selector *sel, xfrm_address_t *addr)
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001843{
1844 int err = -EINVAL;
1845 int ret;
1846 struct xfrm_mgr *km;
1847
Cong Wang85168c02013-01-16 16:05:06 +08001848 rcu_read_lock();
1849 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001850 if (km->report) {
Alexey Dobriyandb983c12008-11-25 17:51:01 -08001851 ret = km->report(net, proto, sel, addr);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001852 if (!ret)
1853 err = ret;
1854 }
1855 }
Cong Wang85168c02013-01-16 16:05:06 +08001856 rcu_read_unlock();
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001857 return err;
1858}
1859EXPORT_SYMBOL(km_report);
1860
Horia Geanta0f245582014-02-12 16:20:06 +02001861bool km_is_alive(const struct km_event *c)
1862{
1863 struct xfrm_mgr *km;
1864 bool is_alive = false;
1865
1866 rcu_read_lock();
1867 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
1868 if (km->is_alive && km->is_alive(c)) {
1869 is_alive = true;
1870 break;
1871 }
1872 }
1873 rcu_read_unlock();
1874
1875 return is_alive;
1876}
1877EXPORT_SYMBOL(km_is_alive);
1878
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879int xfrm_user_policy(struct sock *sk, int optname, u8 __user *optval, int optlen)
1880{
1881 int err;
1882 u8 *data;
1883 struct xfrm_mgr *km;
1884 struct xfrm_policy *pol = NULL;
1885
1886 if (optlen <= 0 || optlen > PAGE_SIZE)
1887 return -EMSGSIZE;
1888
1889 data = kmalloc(optlen, GFP_KERNEL);
1890 if (!data)
1891 return -ENOMEM;
1892
1893 err = -EFAULT;
1894 if (copy_from_user(data, optval, optlen))
1895 goto out;
1896
1897 err = -EINVAL;
Cong Wang85168c02013-01-16 16:05:06 +08001898 rcu_read_lock();
1899 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07001900 pol = km->compile_policy(sk, optname, data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 optlen, &err);
1902 if (err >= 0)
1903 break;
1904 }
Cong Wang85168c02013-01-16 16:05:06 +08001905 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906
1907 if (err >= 0) {
1908 xfrm_sk_policy_insert(sk, err, pol);
1909 xfrm_pol_put(pol);
1910 err = 0;
1911 }
1912
1913out:
1914 kfree(data);
1915 return err;
1916}
1917EXPORT_SYMBOL(xfrm_user_policy);
1918
Cong Wang85168c02013-01-16 16:05:06 +08001919static DEFINE_SPINLOCK(xfrm_km_lock);
1920
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921int xfrm_register_km(struct xfrm_mgr *km)
1922{
Cong Wang85168c02013-01-16 16:05:06 +08001923 spin_lock_bh(&xfrm_km_lock);
1924 list_add_tail_rcu(&km->list, &xfrm_km_list);
1925 spin_unlock_bh(&xfrm_km_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 return 0;
1927}
1928EXPORT_SYMBOL(xfrm_register_km);
1929
1930int xfrm_unregister_km(struct xfrm_mgr *km)
1931{
Cong Wang85168c02013-01-16 16:05:06 +08001932 spin_lock_bh(&xfrm_km_lock);
1933 list_del_rcu(&km->list);
1934 spin_unlock_bh(&xfrm_km_lock);
1935 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 return 0;
1937}
1938EXPORT_SYMBOL(xfrm_unregister_km);
1939
1940int xfrm_state_register_afinfo(struct xfrm_state_afinfo *afinfo)
1941{
1942 int err = 0;
1943 if (unlikely(afinfo == NULL))
1944 return -EINVAL;
1945 if (unlikely(afinfo->family >= NPROTO))
1946 return -EAFNOSUPPORT;
Cong Wang44abdc32013-01-16 16:05:05 +08001947 spin_lock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 if (unlikely(xfrm_state_afinfo[afinfo->family] != NULL))
Li RongQingf31e8d4f2015-04-23 11:06:53 +08001949 err = -EEXIST;
David S. Milleredcd5822006-08-24 00:42:45 -07001950 else
Cong Wang44abdc32013-01-16 16:05:05 +08001951 rcu_assign_pointer(xfrm_state_afinfo[afinfo->family], afinfo);
1952 spin_unlock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 return err;
1954}
1955EXPORT_SYMBOL(xfrm_state_register_afinfo);
1956
1957int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo)
1958{
1959 int err = 0;
1960 if (unlikely(afinfo == NULL))
1961 return -EINVAL;
1962 if (unlikely(afinfo->family >= NPROTO))
1963 return -EAFNOSUPPORT;
Cong Wang44abdc32013-01-16 16:05:05 +08001964 spin_lock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 if (likely(xfrm_state_afinfo[afinfo->family] != NULL)) {
1966 if (unlikely(xfrm_state_afinfo[afinfo->family] != afinfo))
1967 err = -EINVAL;
David S. Milleredcd5822006-08-24 00:42:45 -07001968 else
Cong Wang44abdc32013-01-16 16:05:05 +08001969 RCU_INIT_POINTER(xfrm_state_afinfo[afinfo->family], NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 }
Cong Wang44abdc32013-01-16 16:05:05 +08001971 spin_unlock_bh(&xfrm_state_afinfo_lock);
1972 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 return err;
1974}
1975EXPORT_SYMBOL(xfrm_state_unregister_afinfo);
1976
Hannes Frederic Sowa628e3412013-08-14 13:05:23 +02001977struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978{
1979 struct xfrm_state_afinfo *afinfo;
1980 if (unlikely(family >= NPROTO))
1981 return NULL;
Cong Wang44abdc32013-01-16 16:05:05 +08001982 rcu_read_lock();
1983 afinfo = rcu_dereference(xfrm_state_afinfo[family]);
Herbert Xu546be242006-05-27 23:03:58 -07001984 if (unlikely(!afinfo))
Cong Wang44abdc32013-01-16 16:05:05 +08001985 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 return afinfo;
1987}
1988
Hannes Frederic Sowa628e3412013-08-14 13:05:23 +02001989void xfrm_state_put_afinfo(struct xfrm_state_afinfo *afinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990{
Cong Wang44abdc32013-01-16 16:05:05 +08001991 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992}
1993
1994/* Temporarily located here until net/xfrm/xfrm_tunnel.c is created */
1995void xfrm_state_delete_tunnel(struct xfrm_state *x)
1996{
1997 if (x->tunnel) {
1998 struct xfrm_state *t = x->tunnel;
1999
2000 if (atomic_read(&t->tunnel_users) == 2)
2001 xfrm_state_delete(t);
2002 atomic_dec(&t->tunnel_users);
2003 xfrm_state_put(t);
2004 x->tunnel = NULL;
2005 }
2006}
2007EXPORT_SYMBOL(xfrm_state_delete_tunnel);
2008
2009int xfrm_state_mtu(struct xfrm_state *x, int mtu)
2010{
Patrick McHardyc5c25232007-04-09 11:47:18 -07002011 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012
Patrick McHardyc5c25232007-04-09 11:47:18 -07002013 spin_lock_bh(&x->lock);
2014 if (x->km.state == XFRM_STATE_VALID &&
2015 x->type && x->type->get_mtu)
2016 res = x->type->get_mtu(x, mtu);
2017 else
Patrick McHardy28121612007-06-18 22:30:15 -07002018 res = mtu - x->props.header_len;
Patrick McHardyc5c25232007-04-09 11:47:18 -07002019 spin_unlock_bh(&x->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 return res;
2021}
2022
Wei Yongjuna454f0c2011-03-21 18:08:28 -07002023int __xfrm_init_state(struct xfrm_state *x, bool init_replay)
Herbert Xu72cb6962005-06-20 13:18:08 -07002024{
Herbert Xud094cd82005-06-20 13:19:41 -07002025 struct xfrm_state_afinfo *afinfo;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002026 struct xfrm_mode *inner_mode;
Herbert Xud094cd82005-06-20 13:19:41 -07002027 int family = x->props.family;
Herbert Xu72cb6962005-06-20 13:18:08 -07002028 int err;
2029
Herbert Xud094cd82005-06-20 13:19:41 -07002030 err = -EAFNOSUPPORT;
2031 afinfo = xfrm_state_get_afinfo(family);
2032 if (!afinfo)
2033 goto error;
2034
2035 err = 0;
2036 if (afinfo->init_flags)
2037 err = afinfo->init_flags(x);
2038
2039 xfrm_state_put_afinfo(afinfo);
2040
2041 if (err)
2042 goto error;
2043
2044 err = -EPROTONOSUPPORT;
Herbert Xu13996372007-10-17 21:35:51 -07002045
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002046 if (x->sel.family != AF_UNSPEC) {
2047 inner_mode = xfrm_get_mode(x->props.mode, x->sel.family);
2048 if (inner_mode == NULL)
2049 goto error;
2050
2051 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL) &&
2052 family != x->sel.family) {
2053 xfrm_put_mode(inner_mode);
2054 goto error;
2055 }
2056
2057 x->inner_mode = inner_mode;
2058 } else {
2059 struct xfrm_mode *inner_mode_iaf;
Martin Willid81d2282008-12-03 15:38:07 -08002060 int iafamily = AF_INET;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002061
Martin Willid81d2282008-12-03 15:38:07 -08002062 inner_mode = xfrm_get_mode(x->props.mode, x->props.family);
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002063 if (inner_mode == NULL)
2064 goto error;
2065
2066 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL)) {
2067 xfrm_put_mode(inner_mode);
2068 goto error;
2069 }
Martin Willid81d2282008-12-03 15:38:07 -08002070 x->inner_mode = inner_mode;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002071
Martin Willid81d2282008-12-03 15:38:07 -08002072 if (x->props.family == AF_INET)
2073 iafamily = AF_INET6;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002074
Martin Willid81d2282008-12-03 15:38:07 -08002075 inner_mode_iaf = xfrm_get_mode(x->props.mode, iafamily);
2076 if (inner_mode_iaf) {
2077 if (inner_mode_iaf->flags & XFRM_MODE_FLAG_TUNNEL)
2078 x->inner_mode_iaf = inner_mode_iaf;
2079 else
2080 xfrm_put_mode(inner_mode_iaf);
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002081 }
2082 }
Herbert Xu13996372007-10-17 21:35:51 -07002083
Herbert Xud094cd82005-06-20 13:19:41 -07002084 x->type = xfrm_get_type(x->id.proto, family);
Herbert Xu72cb6962005-06-20 13:18:08 -07002085 if (x->type == NULL)
2086 goto error;
2087
2088 err = x->type->init_state(x);
2089 if (err)
2090 goto error;
2091
Herbert Xu13996372007-10-17 21:35:51 -07002092 x->outer_mode = xfrm_get_mode(x->props.mode, family);
Julia Lawall599901c2012-08-29 06:49:15 +00002093 if (x->outer_mode == NULL) {
2094 err = -EPROTONOSUPPORT;
Herbert Xub59f45d2006-05-27 23:05:54 -07002095 goto error;
Julia Lawall599901c2012-08-29 06:49:15 +00002096 }
Herbert Xub59f45d2006-05-27 23:05:54 -07002097
Wei Yongjuna454f0c2011-03-21 18:08:28 -07002098 if (init_replay) {
2099 err = xfrm_init_replay(x);
2100 if (err)
2101 goto error;
2102 }
2103
Herbert Xu72cb6962005-06-20 13:18:08 -07002104 x->km.state = XFRM_STATE_VALID;
2105
2106error:
2107 return err;
2108}
2109
Wei Yongjuna454f0c2011-03-21 18:08:28 -07002110EXPORT_SYMBOL(__xfrm_init_state);
2111
2112int xfrm_init_state(struct xfrm_state *x)
2113{
2114 return __xfrm_init_state(x, true);
2115}
2116
Herbert Xu72cb6962005-06-20 13:18:08 -07002117EXPORT_SYMBOL(xfrm_init_state);
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09002118
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002119int __net_init xfrm_state_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120{
David S. Millerf034b5d2006-08-24 03:08:07 -07002121 unsigned int sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122
Alexey Dobriyan9d4139c2008-11-25 17:16:11 -08002123 INIT_LIST_HEAD(&net->xfrm.state_all);
2124
David S. Millerf034b5d2006-08-24 03:08:07 -07002125 sz = sizeof(struct hlist_head) * 8;
2126
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002127 net->xfrm.state_bydst = xfrm_hash_alloc(sz);
2128 if (!net->xfrm.state_bydst)
2129 goto out_bydst;
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002130 net->xfrm.state_bysrc = xfrm_hash_alloc(sz);
2131 if (!net->xfrm.state_bysrc)
2132 goto out_bysrc;
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002133 net->xfrm.state_byspi = xfrm_hash_alloc(sz);
2134 if (!net->xfrm.state_byspi)
2135 goto out_byspi;
Alexey Dobriyan529983e2008-11-25 17:18:12 -08002136 net->xfrm.state_hmask = ((sz / sizeof(struct hlist_head)) - 1);
David S. Millerf034b5d2006-08-24 03:08:07 -07002137
Alexey Dobriyan0bf7c5b2008-11-25 17:18:39 -08002138 net->xfrm.state_num = 0;
Alexey Dobriyan63082732008-11-25 17:19:07 -08002139 INIT_WORK(&net->xfrm.state_hash_work, xfrm_hash_resize);
Fan Du283bc9f2013-11-07 17:47:50 +08002140 spin_lock_init(&net->xfrm.xfrm_state_lock);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002141 return 0;
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002142
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002143out_byspi:
2144 xfrm_hash_free(net->xfrm.state_bysrc, sz);
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002145out_bysrc:
2146 xfrm_hash_free(net->xfrm.state_bydst, sz);
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002147out_bydst:
2148 return -ENOMEM;
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002149}
2150
2151void xfrm_state_fini(struct net *net)
2152{
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002153 unsigned int sz;
2154
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002155 flush_work(&net->xfrm.state_hash_work);
Tetsuo Handa2e710292014-04-22 21:48:30 +09002156 xfrm_state_flush(net, IPSEC_PROTO_ANY, false);
Florian Westphal35db57bb2016-08-23 16:00:12 +02002157 flush_work(&xfrm_state_gc_work);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002158
Alexey Dobriyan9d4139c2008-11-25 17:16:11 -08002159 WARN_ON(!list_empty(&net->xfrm.state_all));
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002160
Alexey Dobriyan529983e2008-11-25 17:18:12 -08002161 sz = (net->xfrm.state_hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002162 WARN_ON(!hlist_empty(net->xfrm.state_byspi));
2163 xfrm_hash_free(net->xfrm.state_byspi, sz);
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002164 WARN_ON(!hlist_empty(net->xfrm.state_bysrc));
2165 xfrm_hash_free(net->xfrm.state_bysrc, sz);
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002166 WARN_ON(!hlist_empty(net->xfrm.state_bydst));
2167 xfrm_hash_free(net->xfrm.state_bydst, sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168}
2169
Joy Lattenab5f5e82007-09-17 11:51:22 -07002170#ifdef CONFIG_AUDITSYSCALL
Ilpo Järvinencf35f432008-01-05 23:13:20 -08002171static void xfrm_audit_helper_sainfo(struct xfrm_state *x,
2172 struct audit_buffer *audit_buf)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002173{
Paul Moore68277ac2007-12-20 20:49:33 -08002174 struct xfrm_sec_ctx *ctx = x->security;
2175 u32 spi = ntohl(x->id.spi);
2176
2177 if (ctx)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002178 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
Paul Moore68277ac2007-12-20 20:49:33 -08002179 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002180
Weilong Chen9b7a7872013-12-24 09:43:46 +08002181 switch (x->props.family) {
Joy Lattenab5f5e82007-09-17 11:51:22 -07002182 case AF_INET:
Harvey Harrison21454aa2008-10-31 00:54:56 -07002183 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2184 &x->props.saddr.a4, &x->id.daddr.a4);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002185 break;
2186 case AF_INET6:
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002187 audit_log_format(audit_buf, " src=%pI6 dst=%pI6",
Harvey Harrisonfdb46ee2008-10-28 16:10:17 -07002188 x->props.saddr.a6, x->id.daddr.a6);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002189 break;
2190 }
Paul Moore68277ac2007-12-20 20:49:33 -08002191
2192 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002193}
2194
Ilpo Järvinencf35f432008-01-05 23:13:20 -08002195static void xfrm_audit_helper_pktinfo(struct sk_buff *skb, u16 family,
2196 struct audit_buffer *audit_buf)
Paul Mooreafeb14b2007-12-21 14:58:11 -08002197{
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002198 const struct iphdr *iph4;
2199 const struct ipv6hdr *iph6;
Paul Mooreafeb14b2007-12-21 14:58:11 -08002200
2201 switch (family) {
2202 case AF_INET:
2203 iph4 = ip_hdr(skb);
Harvey Harrison21454aa2008-10-31 00:54:56 -07002204 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2205 &iph4->saddr, &iph4->daddr);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002206 break;
2207 case AF_INET6:
2208 iph6 = ipv6_hdr(skb);
2209 audit_log_format(audit_buf,
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002210 " src=%pI6 dst=%pI6 flowlbl=0x%x%02x%02x",
Weilong Chen9b7a7872013-12-24 09:43:46 +08002211 &iph6->saddr, &iph6->daddr,
Paul Mooreafeb14b2007-12-21 14:58:11 -08002212 iph6->flow_lbl[0] & 0x0f,
2213 iph6->flow_lbl[1],
2214 iph6->flow_lbl[2]);
2215 break;
2216 }
2217}
2218
Tetsuo Handa2e710292014-04-22 21:48:30 +09002219void xfrm_audit_state_add(struct xfrm_state *x, int result, bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002220{
2221 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002222
Paul Mooreafeb14b2007-12-21 14:58:11 -08002223 audit_buf = xfrm_audit_start("SAD-add");
Joy Lattenab5f5e82007-09-17 11:51:22 -07002224 if (audit_buf == NULL)
2225 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09002226 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002227 xfrm_audit_helper_sainfo(x, audit_buf);
2228 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002229 audit_log_end(audit_buf);
2230}
2231EXPORT_SYMBOL_GPL(xfrm_audit_state_add);
2232
Tetsuo Handa2e710292014-04-22 21:48:30 +09002233void xfrm_audit_state_delete(struct xfrm_state *x, int result, bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002234{
2235 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002236
Paul Mooreafeb14b2007-12-21 14:58:11 -08002237 audit_buf = xfrm_audit_start("SAD-delete");
Joy Lattenab5f5e82007-09-17 11:51:22 -07002238 if (audit_buf == NULL)
2239 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09002240 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002241 xfrm_audit_helper_sainfo(x, audit_buf);
2242 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002243 audit_log_end(audit_buf);
2244}
2245EXPORT_SYMBOL_GPL(xfrm_audit_state_delete);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002246
2247void xfrm_audit_state_replay_overflow(struct xfrm_state *x,
2248 struct sk_buff *skb)
2249{
2250 struct audit_buffer *audit_buf;
2251 u32 spi;
2252
2253 audit_buf = xfrm_audit_start("SA-replay-overflow");
2254 if (audit_buf == NULL)
2255 return;
2256 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2257 /* don't record the sequence number because it's inherent in this kind
2258 * of audit message */
2259 spi = ntohl(x->id.spi);
2260 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
2261 audit_log_end(audit_buf);
2262}
2263EXPORT_SYMBOL_GPL(xfrm_audit_state_replay_overflow);
2264
Steffen Klassert9fdc4882011-03-08 00:08:32 +00002265void xfrm_audit_state_replay(struct xfrm_state *x,
Paul Mooreafeb14b2007-12-21 14:58:11 -08002266 struct sk_buff *skb, __be32 net_seq)
2267{
2268 struct audit_buffer *audit_buf;
2269 u32 spi;
2270
2271 audit_buf = xfrm_audit_start("SA-replayed-pkt");
2272 if (audit_buf == NULL)
2273 return;
2274 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2275 spi = ntohl(x->id.spi);
2276 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2277 spi, spi, ntohl(net_seq));
2278 audit_log_end(audit_buf);
2279}
Steffen Klassert9fdc4882011-03-08 00:08:32 +00002280EXPORT_SYMBOL_GPL(xfrm_audit_state_replay);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002281
2282void xfrm_audit_state_notfound_simple(struct sk_buff *skb, u16 family)
2283{
2284 struct audit_buffer *audit_buf;
2285
2286 audit_buf = xfrm_audit_start("SA-notfound");
2287 if (audit_buf == NULL)
2288 return;
2289 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2290 audit_log_end(audit_buf);
2291}
2292EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound_simple);
2293
2294void xfrm_audit_state_notfound(struct sk_buff *skb, u16 family,
2295 __be32 net_spi, __be32 net_seq)
2296{
2297 struct audit_buffer *audit_buf;
2298 u32 spi;
2299
2300 audit_buf = xfrm_audit_start("SA-notfound");
2301 if (audit_buf == NULL)
2302 return;
2303 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2304 spi = ntohl(net_spi);
2305 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2306 spi, spi, ntohl(net_seq));
2307 audit_log_end(audit_buf);
2308}
2309EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound);
2310
2311void xfrm_audit_state_icvfail(struct xfrm_state *x,
2312 struct sk_buff *skb, u8 proto)
2313{
2314 struct audit_buffer *audit_buf;
2315 __be32 net_spi;
2316 __be32 net_seq;
2317
2318 audit_buf = xfrm_audit_start("SA-icv-failure");
2319 if (audit_buf == NULL)
2320 return;
2321 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2322 if (xfrm_parse_spi(skb, proto, &net_spi, &net_seq) == 0) {
2323 u32 spi = ntohl(net_spi);
2324 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2325 spi, spi, ntohl(net_seq));
2326 }
2327 audit_log_end(audit_buf);
2328}
2329EXPORT_SYMBOL_GPL(xfrm_audit_state_icvfail);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002330#endif /* CONFIG_AUDITSYSCALL */