blob: 53e7867f9254479b18792207368d363f58118d7a [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070034/* Each xfrm_state may be linked to two tables:
35
36 1. Hash table by (spi,daddr,ah/esp) to find SA by SPI. (input,ctl)
David S. Millera624c102006-08-24 03:24:33 -070037 2. Hash table by (daddr,family,reqid) to find what SAs exist for given
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 destination/tunnel endpoint. (output)
39 */
40
David S. Millerf034b5d2006-08-24 03:08:07 -070041static unsigned int xfrm_state_hashmax __read_mostly = 1 * 1024 * 1024;
Florian Westphalb65e3d72016-08-09 12:16:07 +020042static __read_mostly seqcount_t xfrm_state_hash_generation = SEQCNT_ZERO(xfrm_state_hash_generation);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Florian Westphal02efdff2016-08-09 12:16:05 +020044static inline bool xfrm_state_hold_rcu(struct xfrm_state __rcu *x)
45{
46 return atomic_inc_not_zero(&x->refcnt);
47}
48
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -080049static inline unsigned int xfrm_dst_hash(struct net *net,
David S. Miller2ab38502011-02-24 01:47:16 -050050 const xfrm_address_t *daddr,
51 const xfrm_address_t *saddr,
David S. Millerc1969f22006-08-24 04:00:03 -070052 u32 reqid,
David S. Millera624c102006-08-24 03:24:33 -070053 unsigned short family)
54{
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -080055 return __xfrm_dst_hash(daddr, saddr, reqid, family, net->xfrm.state_hmask);
David S. Millera624c102006-08-24 03:24:33 -070056}
57
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -080058static inline unsigned int xfrm_src_hash(struct net *net,
David S. Miller2ab38502011-02-24 01:47:16 -050059 const xfrm_address_t *daddr,
60 const xfrm_address_t *saddr,
David S. Miller44e36b42006-08-24 04:50:50 -070061 unsigned short family)
David S. Millerf034b5d2006-08-24 03:08:07 -070062{
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -080063 return __xfrm_src_hash(daddr, saddr, family, net->xfrm.state_hmask);
David S. Millerf034b5d2006-08-24 03:08:07 -070064}
65
David S. Miller2575b652006-08-24 03:26:44 -070066static inline unsigned int
David S. Miller2ab38502011-02-24 01:47:16 -050067xfrm_spi_hash(struct net *net, const xfrm_address_t *daddr,
68 __be32 spi, u8 proto, unsigned short family)
David S. Millerf034b5d2006-08-24 03:08:07 -070069{
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -080070 return __xfrm_spi_hash(daddr, spi, proto, family, net->xfrm.state_hmask);
David S. Millerf034b5d2006-08-24 03:08:07 -070071}
72
David S. Millerf034b5d2006-08-24 03:08:07 -070073static void xfrm_hash_transfer(struct hlist_head *list,
74 struct hlist_head *ndsttable,
75 struct hlist_head *nsrctable,
76 struct hlist_head *nspitable,
77 unsigned int nhashmask)
78{
Sasha Levinb67bfe02013-02-27 17:06:00 -080079 struct hlist_node *tmp;
David S. Millerf034b5d2006-08-24 03:08:07 -070080 struct xfrm_state *x;
81
Sasha Levinb67bfe02013-02-27 17:06:00 -080082 hlist_for_each_entry_safe(x, tmp, list, bydst) {
David S. Millerf034b5d2006-08-24 03:08:07 -070083 unsigned int h;
84
David S. Millerc1969f22006-08-24 04:00:03 -070085 h = __xfrm_dst_hash(&x->id.daddr, &x->props.saddr,
86 x->props.reqid, x->props.family,
87 nhashmask);
Florian Westphalae3fb6d2016-08-09 12:16:04 +020088 hlist_add_head_rcu(&x->bydst, ndsttable + h);
David S. Millerf034b5d2006-08-24 03:08:07 -070089
Masahide NAKAMURA667bbcb2006-10-03 15:56:09 -070090 h = __xfrm_src_hash(&x->id.daddr, &x->props.saddr,
91 x->props.family,
David S. Millerf034b5d2006-08-24 03:08:07 -070092 nhashmask);
Florian Westphalae3fb6d2016-08-09 12:16:04 +020093 hlist_add_head_rcu(&x->bysrc, nsrctable + h);
David S. Millerf034b5d2006-08-24 03:08:07 -070094
Masahide NAKAMURA7b4dc3602006-09-27 22:21:52 -070095 if (x->id.spi) {
96 h = __xfrm_spi_hash(&x->id.daddr, x->id.spi,
97 x->id.proto, x->props.family,
98 nhashmask);
Florian Westphalae3fb6d2016-08-09 12:16:04 +020099 hlist_add_head_rcu(&x->byspi, nspitable + h);
Masahide NAKAMURA7b4dc3602006-09-27 22:21:52 -0700100 }
David S. Millerf034b5d2006-08-24 03:08:07 -0700101 }
102}
103
Alexey Dobriyan63082732008-11-25 17:19:07 -0800104static unsigned long xfrm_hash_new_size(unsigned int state_hmask)
David S. Millerf034b5d2006-08-24 03:08:07 -0700105{
Alexey Dobriyan63082732008-11-25 17:19:07 -0800106 return ((state_hmask + 1) << 1) * sizeof(struct hlist_head);
David S. Millerf034b5d2006-08-24 03:08:07 -0700107}
108
Alexey Dobriyan63082732008-11-25 17:19:07 -0800109static void xfrm_hash_resize(struct work_struct *work)
David S. Millerf034b5d2006-08-24 03:08:07 -0700110{
Alexey Dobriyan63082732008-11-25 17:19:07 -0800111 struct net *net = container_of(work, struct net, xfrm.state_hash_work);
David S. Millerf034b5d2006-08-24 03:08:07 -0700112 struct hlist_head *ndst, *nsrc, *nspi, *odst, *osrc, *ospi;
113 unsigned long nsize, osize;
114 unsigned int nhashmask, ohashmask;
115 int i;
116
Alexey Dobriyan63082732008-11-25 17:19:07 -0800117 nsize = xfrm_hash_new_size(net->xfrm.state_hmask);
David S. Miller44e36b42006-08-24 04:50:50 -0700118 ndst = xfrm_hash_alloc(nsize);
David S. Millerf034b5d2006-08-24 03:08:07 -0700119 if (!ndst)
Ying Xue02447902014-08-29 17:09:07 +0800120 return;
David S. Miller44e36b42006-08-24 04:50:50 -0700121 nsrc = xfrm_hash_alloc(nsize);
David S. Millerf034b5d2006-08-24 03:08:07 -0700122 if (!nsrc) {
David S. Miller44e36b42006-08-24 04:50:50 -0700123 xfrm_hash_free(ndst, nsize);
Ying Xue02447902014-08-29 17:09:07 +0800124 return;
David S. Millerf034b5d2006-08-24 03:08:07 -0700125 }
David S. Miller44e36b42006-08-24 04:50:50 -0700126 nspi = xfrm_hash_alloc(nsize);
David S. Millerf034b5d2006-08-24 03:08:07 -0700127 if (!nspi) {
David S. Miller44e36b42006-08-24 04:50:50 -0700128 xfrm_hash_free(ndst, nsize);
129 xfrm_hash_free(nsrc, nsize);
Ying Xue02447902014-08-29 17:09:07 +0800130 return;
David S. Millerf034b5d2006-08-24 03:08:07 -0700131 }
132
Fan Du283bc9f2013-11-07 17:47:50 +0800133 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Florian Westphalb65e3d72016-08-09 12:16:07 +0200134 write_seqcount_begin(&xfrm_state_hash_generation);
David S. Millerf034b5d2006-08-24 03:08:07 -0700135
136 nhashmask = (nsize / sizeof(struct hlist_head)) - 1U;
Florian Westphalc8406992016-08-09 12:16:08 +0200137 odst = xfrm_state_deref_prot(net->xfrm.state_bydst, net);
Alexey Dobriyan63082732008-11-25 17:19:07 -0800138 for (i = net->xfrm.state_hmask; i >= 0; i--)
Florian Westphalc8406992016-08-09 12:16:08 +0200139 xfrm_hash_transfer(odst + i, ndst, nsrc, nspi, nhashmask);
David S. Millerf034b5d2006-08-24 03:08:07 -0700140
Florian Westphalc8406992016-08-09 12:16:08 +0200141 osrc = xfrm_state_deref_prot(net->xfrm.state_bysrc, net);
142 ospi = xfrm_state_deref_prot(net->xfrm.state_byspi, net);
Alexey Dobriyan63082732008-11-25 17:19:07 -0800143 ohashmask = net->xfrm.state_hmask;
David S. Millerf034b5d2006-08-24 03:08:07 -0700144
Florian Westphalc8406992016-08-09 12:16:08 +0200145 rcu_assign_pointer(net->xfrm.state_bydst, ndst);
146 rcu_assign_pointer(net->xfrm.state_bysrc, nsrc);
147 rcu_assign_pointer(net->xfrm.state_byspi, nspi);
Alexey Dobriyan63082732008-11-25 17:19:07 -0800148 net->xfrm.state_hmask = nhashmask;
David S. Millerf034b5d2006-08-24 03:08:07 -0700149
Florian Westphalb65e3d72016-08-09 12:16:07 +0200150 write_seqcount_end(&xfrm_state_hash_generation);
Fan Du283bc9f2013-11-07 17:47:50 +0800151 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
David S. Millerf034b5d2006-08-24 03:08:07 -0700152
153 osize = (ohashmask + 1) * sizeof(struct hlist_head);
Florian Westphaldf7274e2016-08-09 12:16:06 +0200154
155 synchronize_rcu();
156
David S. Miller44e36b42006-08-24 04:50:50 -0700157 xfrm_hash_free(odst, osize);
158 xfrm_hash_free(osrc, osize);
159 xfrm_hash_free(ospi, osize);
David S. Millerf034b5d2006-08-24 03:08:07 -0700160}
161
Cong Wang44abdc32013-01-16 16:05:05 +0800162static DEFINE_SPINLOCK(xfrm_state_afinfo_lock);
163static struct xfrm_state_afinfo __rcu *xfrm_state_afinfo[NPROTO];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165static DEFINE_SPINLOCK(xfrm_state_gc_lock);
166
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -0800167int __xfrm_state_delete(struct xfrm_state *x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -0800169int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol);
Horia Geanta0f245582014-02-12 16:20:06 +0200170bool km_is_alive(const struct km_event *c);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000171void km_state_expired(struct xfrm_state *x, int hard, u32 portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Cong Wang7a9885b2013-01-17 16:34:11 +0800173static DEFINE_SPINLOCK(xfrm_type_lock);
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800174int xfrm_register_type(const struct xfrm_type *type, unsigned short family)
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700175{
Cong Wang7a9885b2013-01-17 16:34:11 +0800176 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800177 const struct xfrm_type **typemap;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700178 int err = 0;
179
180 if (unlikely(afinfo == NULL))
181 return -EAFNOSUPPORT;
182 typemap = afinfo->type_map;
Cong Wang7a9885b2013-01-17 16:34:11 +0800183 spin_lock_bh(&xfrm_type_lock);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700184
185 if (likely(typemap[type->proto] == NULL))
186 typemap[type->proto] = type;
187 else
188 err = -EEXIST;
Cong Wang7a9885b2013-01-17 16:34:11 +0800189 spin_unlock_bh(&xfrm_type_lock);
190 xfrm_state_put_afinfo(afinfo);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700191 return err;
192}
193EXPORT_SYMBOL(xfrm_register_type);
194
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800195int xfrm_unregister_type(const struct xfrm_type *type, unsigned short family)
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700196{
Cong Wang7a9885b2013-01-17 16:34:11 +0800197 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800198 const struct xfrm_type **typemap;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700199 int err = 0;
200
201 if (unlikely(afinfo == NULL))
202 return -EAFNOSUPPORT;
203 typemap = afinfo->type_map;
Cong Wang7a9885b2013-01-17 16:34:11 +0800204 spin_lock_bh(&xfrm_type_lock);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700205
206 if (unlikely(typemap[type->proto] != type))
207 err = -ENOENT;
208 else
209 typemap[type->proto] = NULL;
Cong Wang7a9885b2013-01-17 16:34:11 +0800210 spin_unlock_bh(&xfrm_type_lock);
211 xfrm_state_put_afinfo(afinfo);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700212 return err;
213}
214EXPORT_SYMBOL(xfrm_unregister_type);
215
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800216static const struct xfrm_type *xfrm_get_type(u8 proto, unsigned short family)
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700217{
218 struct xfrm_state_afinfo *afinfo;
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800219 const struct xfrm_type **typemap;
220 const struct xfrm_type *type;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700221 int modload_attempted = 0;
222
223retry:
224 afinfo = xfrm_state_get_afinfo(family);
225 if (unlikely(afinfo == NULL))
226 return NULL;
227 typemap = afinfo->type_map;
228
229 type = typemap[proto];
230 if (unlikely(type && !try_module_get(type->owner)))
231 type = NULL;
232 if (!type && !modload_attempted) {
233 xfrm_state_put_afinfo(afinfo);
234 request_module("xfrm-type-%d-%d", family, proto);
235 modload_attempted = 1;
236 goto retry;
237 }
238
239 xfrm_state_put_afinfo(afinfo);
240 return type;
241}
242
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800243static void xfrm_put_type(const struct xfrm_type *type)
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700244{
245 module_put(type->owner);
246}
247
Cong Wang7a9885b2013-01-17 16:34:11 +0800248static DEFINE_SPINLOCK(xfrm_mode_lock);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700249int xfrm_register_mode(struct xfrm_mode *mode, int family)
250{
251 struct xfrm_state_afinfo *afinfo;
252 struct xfrm_mode **modemap;
253 int err;
254
255 if (unlikely(mode->encap >= XFRM_MODE_MAX))
256 return -EINVAL;
257
Cong Wang7a9885b2013-01-17 16:34:11 +0800258 afinfo = xfrm_state_get_afinfo(family);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700259 if (unlikely(afinfo == NULL))
260 return -EAFNOSUPPORT;
261
262 err = -EEXIST;
263 modemap = afinfo->mode_map;
Cong Wang7a9885b2013-01-17 16:34:11 +0800264 spin_lock_bh(&xfrm_mode_lock);
Herbert Xu17c2a422007-10-17 21:33:12 -0700265 if (modemap[mode->encap])
266 goto out;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700267
Herbert Xu17c2a422007-10-17 21:33:12 -0700268 err = -ENOENT;
269 if (!try_module_get(afinfo->owner))
270 goto out;
271
272 mode->afinfo = afinfo;
273 modemap[mode->encap] = mode;
274 err = 0;
275
276out:
Cong Wang7a9885b2013-01-17 16:34:11 +0800277 spin_unlock_bh(&xfrm_mode_lock);
278 xfrm_state_put_afinfo(afinfo);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700279 return err;
280}
281EXPORT_SYMBOL(xfrm_register_mode);
282
283int xfrm_unregister_mode(struct xfrm_mode *mode, int family)
284{
285 struct xfrm_state_afinfo *afinfo;
286 struct xfrm_mode **modemap;
287 int err;
288
289 if (unlikely(mode->encap >= XFRM_MODE_MAX))
290 return -EINVAL;
291
Cong Wang7a9885b2013-01-17 16:34:11 +0800292 afinfo = xfrm_state_get_afinfo(family);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700293 if (unlikely(afinfo == NULL))
294 return -EAFNOSUPPORT;
295
296 err = -ENOENT;
297 modemap = afinfo->mode_map;
Cong Wang7a9885b2013-01-17 16:34:11 +0800298 spin_lock_bh(&xfrm_mode_lock);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700299 if (likely(modemap[mode->encap] == mode)) {
300 modemap[mode->encap] = NULL;
Herbert Xu17c2a422007-10-17 21:33:12 -0700301 module_put(mode->afinfo->owner);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700302 err = 0;
303 }
304
Cong Wang7a9885b2013-01-17 16:34:11 +0800305 spin_unlock_bh(&xfrm_mode_lock);
306 xfrm_state_put_afinfo(afinfo);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700307 return err;
308}
309EXPORT_SYMBOL(xfrm_unregister_mode);
310
311static struct xfrm_mode *xfrm_get_mode(unsigned int encap, int family)
312{
313 struct xfrm_state_afinfo *afinfo;
314 struct xfrm_mode *mode;
315 int modload_attempted = 0;
316
317 if (unlikely(encap >= XFRM_MODE_MAX))
318 return NULL;
319
320retry:
321 afinfo = xfrm_state_get_afinfo(family);
322 if (unlikely(afinfo == NULL))
323 return NULL;
324
325 mode = afinfo->mode_map[encap];
326 if (unlikely(mode && !try_module_get(mode->owner)))
327 mode = NULL;
328 if (!mode && !modload_attempted) {
329 xfrm_state_put_afinfo(afinfo);
330 request_module("xfrm-mode-%d-%d", family, encap);
331 modload_attempted = 1;
332 goto retry;
333 }
334
335 xfrm_state_put_afinfo(afinfo);
336 return mode;
337}
338
339static void xfrm_put_mode(struct xfrm_mode *mode)
340{
341 module_put(mode->owner);
342}
343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344static void xfrm_state_gc_destroy(struct xfrm_state *x)
345{
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800346 tasklet_hrtimer_cancel(&x->mtimer);
David S. Millera47f0ce2006-08-24 03:54:22 -0700347 del_timer_sync(&x->rtimer);
Jesper Juhla51482b2005-11-08 09:41:34 -0800348 kfree(x->aalg);
349 kfree(x->ealg);
350 kfree(x->calg);
351 kfree(x->encap);
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700352 kfree(x->coaddr);
Steffen Klassertd8647b72011-03-08 00:10:27 +0000353 kfree(x->replay_esn);
354 kfree(x->preplay_esn);
Herbert Xu13996372007-10-17 21:35:51 -0700355 if (x->inner_mode)
356 xfrm_put_mode(x->inner_mode);
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700357 if (x->inner_mode_iaf)
358 xfrm_put_mode(x->inner_mode_iaf);
Herbert Xu13996372007-10-17 21:35:51 -0700359 if (x->outer_mode)
360 xfrm_put_mode(x->outer_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 if (x->type) {
362 x->type->destructor(x);
363 xfrm_put_type(x->type);
364 }
Trent Jaegerdf718372005-12-13 23:12:27 -0800365 security_xfrm_state_free(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 kfree(x);
367}
368
Alexey Dobriyanc7837142008-11-25 17:20:36 -0800369static void xfrm_state_gc_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370{
Alexey Dobriyanc7837142008-11-25 17:20:36 -0800371 struct net *net = container_of(work, struct net, xfrm.state_gc_work);
Herbert Xu12a169e2008-10-01 07:03:24 -0700372 struct xfrm_state *x;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800373 struct hlist_node *tmp;
Herbert Xu12a169e2008-10-01 07:03:24 -0700374 struct hlist_head gc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 spin_lock_bh(&xfrm_state_gc_lock);
Alexey Dobriyanc7837142008-11-25 17:20:36 -0800377 hlist_move_list(&net->xfrm.state_gc_list, &gc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 spin_unlock_bh(&xfrm_state_gc_lock);
379
Florian Westphaldf7274e2016-08-09 12:16:06 +0200380 synchronize_rcu();
381
Sasha Levinb67bfe02013-02-27 17:06:00 -0800382 hlist_for_each_entry_safe(x, tmp, &gc_list, gclist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 xfrm_state_gc_destroy(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384}
385
386static inline unsigned long make_jiffies(long secs)
387{
388 if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
389 return MAX_SCHEDULE_TIMEOUT-1;
390 else
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900391 return secs*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392}
393
Weilong Chen3e94c2d2013-12-24 09:43:47 +0800394static enum hrtimer_restart xfrm_timer_handler(struct hrtimer *me)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395{
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800396 struct tasklet_hrtimer *thr = container_of(me, struct tasklet_hrtimer, timer);
397 struct xfrm_state *x = container_of(thr, struct xfrm_state, mtimer);
James Morris9d729f72007-03-04 16:12:44 -0800398 unsigned long now = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 long next = LONG_MAX;
400 int warn = 0;
Joy Latten161a09e2006-11-27 13:11:54 -0600401 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
403 spin_lock(&x->lock);
404 if (x->km.state == XFRM_STATE_DEAD)
405 goto out;
406 if (x->km.state == XFRM_STATE_EXPIRED)
407 goto expired;
408 if (x->lft.hard_add_expires_seconds) {
409 long tmo = x->lft.hard_add_expires_seconds +
410 x->curlft.add_time - now;
Fan Due3c0d042012-07-30 21:43:54 +0000411 if (tmo <= 0) {
412 if (x->xflags & XFRM_SOFT_EXPIRE) {
413 /* enter hard expire without soft expire first?!
414 * setting a new date could trigger this.
415 * workarbound: fix x->curflt.add_time by below:
416 */
417 x->curlft.add_time = now - x->saved_tmo - 1;
418 tmo = x->lft.hard_add_expires_seconds - x->saved_tmo;
419 } else
420 goto expired;
421 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 if (tmo < next)
423 next = tmo;
424 }
425 if (x->lft.hard_use_expires_seconds) {
426 long tmo = x->lft.hard_use_expires_seconds +
427 (x->curlft.use_time ? : now) - now;
428 if (tmo <= 0)
429 goto expired;
430 if (tmo < next)
431 next = tmo;
432 }
433 if (x->km.dying)
434 goto resched;
435 if (x->lft.soft_add_expires_seconds) {
436 long tmo = x->lft.soft_add_expires_seconds +
437 x->curlft.add_time - now;
Fan Due3c0d042012-07-30 21:43:54 +0000438 if (tmo <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 warn = 1;
Fan Due3c0d042012-07-30 21:43:54 +0000440 x->xflags &= ~XFRM_SOFT_EXPIRE;
441 } else if (tmo < next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 next = tmo;
Fan Due3c0d042012-07-30 21:43:54 +0000443 x->xflags |= XFRM_SOFT_EXPIRE;
444 x->saved_tmo = tmo;
445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 }
447 if (x->lft.soft_use_expires_seconds) {
448 long tmo = x->lft.soft_use_expires_seconds +
449 (x->curlft.use_time ? : now) - now;
450 if (tmo <= 0)
451 warn = 1;
452 else if (tmo < next)
453 next = tmo;
454 }
455
Herbert Xu4666faa2005-06-18 22:43:22 -0700456 x->km.dying = warn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 if (warn)
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -0800458 km_state_expired(x, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459resched:
Weilong Chen9b7a7872013-12-24 09:43:46 +0800460 if (next != LONG_MAX) {
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800461 tasklet_hrtimer_start(&x->mtimer, ktime_set(next, 0), HRTIMER_MODE_REL);
462 }
David S. Millera47f0ce2006-08-24 03:54:22 -0700463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 goto out;
465
466expired:
Steffen Klassert5b8ef342013-08-27 13:43:30 +0200467 if (x->km.state == XFRM_STATE_ACQ && x->id.spi == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 x->km.state = XFRM_STATE_EXPIRED;
Joy Latten161a09e2006-11-27 13:11:54 -0600469
470 err = __xfrm_state_delete(x);
Nicolas Dichtel0806ae42013-08-23 15:46:08 +0200471 if (!err)
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -0800472 km_state_expired(x, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Tetsuo Handa2e710292014-04-22 21:48:30 +0900474 xfrm_audit_state_delete(x, err ? 0 : 1, true);
Joy Latten161a09e2006-11-27 13:11:54 -0600475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476out:
477 spin_unlock(&x->lock);
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800478 return HRTIMER_NORESTART;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479}
480
David S. Miller0ac84752006-03-20 19:18:23 -0800481static void xfrm_replay_timer_handler(unsigned long data);
482
Alexey Dobriyan673c09b2008-11-25 17:15:16 -0800483struct xfrm_state *xfrm_state_alloc(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
485 struct xfrm_state *x;
486
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700487 x = kzalloc(sizeof(struct xfrm_state), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
489 if (x) {
Alexey Dobriyan673c09b2008-11-25 17:15:16 -0800490 write_pnet(&x->xs_net, net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 atomic_set(&x->refcnt, 1);
492 atomic_set(&x->tunnel_users, 0);
Herbert Xu12a169e2008-10-01 07:03:24 -0700493 INIT_LIST_HEAD(&x->km.all);
David S. Miller8f126e32006-08-24 02:45:07 -0700494 INIT_HLIST_NODE(&x->bydst);
495 INIT_HLIST_NODE(&x->bysrc);
496 INIT_HLIST_NODE(&x->byspi);
Fan Du99565a6c2013-08-15 15:49:06 +0800497 tasklet_hrtimer_init(&x->mtimer, xfrm_timer_handler,
498 CLOCK_BOOTTIME, HRTIMER_MODE_ABS);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800499 setup_timer(&x->rtimer, xfrm_replay_timer_handler,
500 (unsigned long)x);
James Morris9d729f72007-03-04 16:12:44 -0800501 x->curlft.add_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 x->lft.soft_byte_limit = XFRM_INF;
503 x->lft.soft_packet_limit = XFRM_INF;
504 x->lft.hard_byte_limit = XFRM_INF;
505 x->lft.hard_packet_limit = XFRM_INF;
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -0800506 x->replay_maxage = 0;
507 x->replay_maxdiff = 0;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700508 x->inner_mode = NULL;
509 x->inner_mode_iaf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 spin_lock_init(&x->lock);
511 }
512 return x;
513}
514EXPORT_SYMBOL(xfrm_state_alloc);
515
516void __xfrm_state_destroy(struct xfrm_state *x)
517{
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800518 struct net *net = xs_net(x);
519
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700520 WARN_ON(x->km.state != XFRM_STATE_DEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
522 spin_lock_bh(&xfrm_state_gc_lock);
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800523 hlist_add_head(&x->gclist, &net->xfrm.state_gc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 spin_unlock_bh(&xfrm_state_gc_lock);
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800525 schedule_work(&net->xfrm.state_gc_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526}
527EXPORT_SYMBOL(__xfrm_state_destroy);
528
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -0800529int __xfrm_state_delete(struct xfrm_state *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530{
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800531 struct net *net = xs_net(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700532 int err = -ESRCH;
533
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 if (x->km.state != XFRM_STATE_DEAD) {
535 x->km.state = XFRM_STATE_DEAD;
Fan Du283bc9f2013-11-07 17:47:50 +0800536 spin_lock(&net->xfrm.xfrm_state_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -0700537 list_del(&x->km.all);
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200538 hlist_del_rcu(&x->bydst);
539 hlist_del_rcu(&x->bysrc);
David S. Millera47f0ce2006-08-24 03:54:22 -0700540 if (x->id.spi)
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200541 hlist_del_rcu(&x->byspi);
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800542 net->xfrm.state_num--;
Fan Du283bc9f2013-11-07 17:47:50 +0800543 spin_unlock(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 /* All xfrm_state objects are created by xfrm_state_alloc.
546 * The xfrm_state_alloc call gives a reference, and that
547 * is what we are dropping here.
548 */
Patrick McHardy5dba4792007-11-27 11:10:07 +0800549 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700550 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700552
553 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554}
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -0800555EXPORT_SYMBOL(__xfrm_state_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700557int xfrm_state_delete(struct xfrm_state *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700559 int err;
560
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 spin_lock_bh(&x->lock);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700562 err = __xfrm_state_delete(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 spin_unlock_bh(&x->lock);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700564
565 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566}
567EXPORT_SYMBOL(xfrm_state_delete);
568
Joy Latten4aa2e622007-06-04 19:05:57 -0400569#ifdef CONFIG_SECURITY_NETWORK_XFRM
570static inline int
Tetsuo Handa2e710292014-04-22 21:48:30 +0900571xfrm_state_flush_secctx_check(struct net *net, u8 proto, bool task_valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572{
Joy Latten4aa2e622007-06-04 19:05:57 -0400573 int i, err = 0;
574
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800575 for (i = 0; i <= net->xfrm.state_hmask; i++) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400576 struct xfrm_state *x;
577
Sasha Levinb67bfe02013-02-27 17:06:00 -0800578 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400579 if (xfrm_id_proto_match(x->id.proto, proto) &&
580 (err = security_xfrm_state_delete(x)) != 0) {
Tetsuo Handa2e710292014-04-22 21:48:30 +0900581 xfrm_audit_state_delete(x, 0, task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400582 return err;
583 }
584 }
585 }
586
587 return err;
588}
589#else
590static inline int
Tetsuo Handa2e710292014-04-22 21:48:30 +0900591xfrm_state_flush_secctx_check(struct net *net, u8 proto, bool task_valid)
Joy Latten4aa2e622007-06-04 19:05:57 -0400592{
593 return 0;
594}
595#endif
596
Tetsuo Handa2e710292014-04-22 21:48:30 +0900597int xfrm_state_flush(struct net *net, u8 proto, bool task_valid)
Joy Latten4aa2e622007-06-04 19:05:57 -0400598{
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000599 int i, err = 0, cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
Fan Du283bc9f2013-11-07 17:47:50 +0800601 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Tetsuo Handa2e710292014-04-22 21:48:30 +0900602 err = xfrm_state_flush_secctx_check(net, proto, task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400603 if (err)
604 goto out;
605
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000606 err = -ESRCH;
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800607 for (i = 0; i <= net->xfrm.state_hmask; i++) {
David S. Miller8f126e32006-08-24 02:45:07 -0700608 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609restart:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800610 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 if (!xfrm_state_kern(x) &&
Masahide NAKAMURA57947082006-09-22 15:06:24 -0700612 xfrm_id_proto_match(x->id.proto, proto)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 xfrm_state_hold(x);
Fan Du283bc9f2013-11-07 17:47:50 +0800614 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Joy Latten161a09e2006-11-27 13:11:54 -0600616 err = xfrm_state_delete(x);
Joy Lattenab5f5e82007-09-17 11:51:22 -0700617 xfrm_audit_state_delete(x, err ? 0 : 1,
Tetsuo Handa2e710292014-04-22 21:48:30 +0900618 task_valid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 xfrm_state_put(x);
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000620 if (!err)
621 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Fan Du283bc9f2013-11-07 17:47:50 +0800623 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 goto restart;
625 }
626 }
627 }
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000628 if (cnt)
629 err = 0;
Joy Latten4aa2e622007-06-04 19:05:57 -0400630
631out:
Fan Du283bc9f2013-11-07 17:47:50 +0800632 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -0400633 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634}
635EXPORT_SYMBOL(xfrm_state_flush);
636
Alexey Dobriyane0710412010-01-23 13:37:10 +0000637void xfrm_sad_getinfo(struct net *net, struct xfrmk_sadinfo *si)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700638{
Fan Du283bc9f2013-11-07 17:47:50 +0800639 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Alexey Dobriyane0710412010-01-23 13:37:10 +0000640 si->sadcnt = net->xfrm.state_num;
641 si->sadhcnt = net->xfrm.state_hmask;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700642 si->sadhmcnt = xfrm_state_hashmax;
Fan Du283bc9f2013-11-07 17:47:50 +0800643 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700644}
645EXPORT_SYMBOL(xfrm_sad_getinfo);
646
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647static int
David S. Miller1a898592011-02-22 18:22:34 -0800648xfrm_init_tempstate(struct xfrm_state *x, const struct flowi *fl,
David S. Miller04686012011-02-24 01:50:12 -0500649 const struct xfrm_tmpl *tmpl,
David S. Miller33765d02011-02-24 01:55:45 -0500650 const xfrm_address_t *daddr, const xfrm_address_t *saddr,
Thomas Egerer8444cf72010-09-20 11:11:38 -0700651 unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652{
653 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
654 if (!afinfo)
655 return -1;
Thomas Egerer8444cf72010-09-20 11:11:38 -0700656 afinfo->init_tempsel(&x->sel, fl);
657
658 if (family != tmpl->encap_family) {
659 xfrm_state_put_afinfo(afinfo);
660 afinfo = xfrm_state_get_afinfo(tmpl->encap_family);
661 if (!afinfo)
662 return -1;
663 }
664 afinfo->init_temprop(x, tmpl, daddr, saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 xfrm_state_put_afinfo(afinfo);
666 return 0;
667}
668
David S. Miller9aa60082011-02-24 01:51:36 -0500669static struct xfrm_state *__xfrm_state_lookup(struct net *net, u32 mark,
670 const xfrm_address_t *daddr,
671 __be32 spi, u8 proto,
672 unsigned short family)
David S. Milleredcd5822006-08-24 00:42:45 -0700673{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800674 unsigned int h = xfrm_spi_hash(net, daddr, spi, proto, family);
David S. Milleredcd5822006-08-24 00:42:45 -0700675 struct xfrm_state *x;
676
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200677 hlist_for_each_entry_rcu(x, net->xfrm.state_byspi + h, byspi) {
David S. Milleredcd5822006-08-24 00:42:45 -0700678 if (x->props.family != family ||
679 x->id.spi != spi ||
Wei Yongjun18025712009-06-28 18:42:53 +0000680 x->id.proto != proto ||
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +0000681 !xfrm_addr_equal(&x->id.daddr, daddr, family))
David S. Milleredcd5822006-08-24 00:42:45 -0700682 continue;
683
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000684 if ((mark & x->mark.m) != x->mark.v)
685 continue;
Florian Westphal02efdff2016-08-09 12:16:05 +0200686 if (!xfrm_state_hold_rcu(x))
687 continue;
David S. Milleredcd5822006-08-24 00:42:45 -0700688 return x;
689 }
690
691 return NULL;
692}
693
David S. Miller9aa60082011-02-24 01:51:36 -0500694static struct xfrm_state *__xfrm_state_lookup_byaddr(struct net *net, u32 mark,
695 const xfrm_address_t *daddr,
696 const xfrm_address_t *saddr,
697 u8 proto, unsigned short family)
David S. Milleredcd5822006-08-24 00:42:45 -0700698{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800699 unsigned int h = xfrm_src_hash(net, daddr, saddr, family);
David S. Milleredcd5822006-08-24 00:42:45 -0700700 struct xfrm_state *x;
701
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200702 hlist_for_each_entry_rcu(x, net->xfrm.state_bysrc + h, bysrc) {
David S. Milleredcd5822006-08-24 00:42:45 -0700703 if (x->props.family != family ||
Wei Yongjun18025712009-06-28 18:42:53 +0000704 x->id.proto != proto ||
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +0000705 !xfrm_addr_equal(&x->id.daddr, daddr, family) ||
706 !xfrm_addr_equal(&x->props.saddr, saddr, family))
David S. Milleredcd5822006-08-24 00:42:45 -0700707 continue;
708
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000709 if ((mark & x->mark.m) != x->mark.v)
710 continue;
Florian Westphal02efdff2016-08-09 12:16:05 +0200711 if (!xfrm_state_hold_rcu(x))
712 continue;
David S. Milleredcd5822006-08-24 00:42:45 -0700713 return x;
714 }
715
716 return NULL;
717}
718
719static inline struct xfrm_state *
720__xfrm_state_locate(struct xfrm_state *x, int use_spi, int family)
721{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800722 struct net *net = xs_net(x);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800723 u32 mark = x->mark.v & x->mark.m;
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800724
David S. Milleredcd5822006-08-24 00:42:45 -0700725 if (use_spi)
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800726 return __xfrm_state_lookup(net, mark, &x->id.daddr,
727 x->id.spi, x->id.proto, family);
David S. Milleredcd5822006-08-24 00:42:45 -0700728 else
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800729 return __xfrm_state_lookup_byaddr(net, mark,
730 &x->id.daddr,
David S. Milleredcd5822006-08-24 00:42:45 -0700731 &x->props.saddr,
732 x->id.proto, family);
733}
734
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800735static void xfrm_hash_grow_check(struct net *net, int have_hash_collision)
Patrick McHardy2fab22f2006-10-24 15:34:00 -0700736{
737 if (have_hash_collision &&
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800738 (net->xfrm.state_hmask + 1) < xfrm_state_hashmax &&
739 net->xfrm.state_num > net->xfrm.state_hmask)
740 schedule_work(&net->xfrm.state_hash_work);
Patrick McHardy2fab22f2006-10-24 15:34:00 -0700741}
742
David S. Miller08ec9af2009-03-13 14:22:40 -0700743static void xfrm_state_look_at(struct xfrm_policy *pol, struct xfrm_state *x,
David S. Miller4a08ab02011-02-22 18:21:31 -0800744 const struct flowi *fl, unsigned short family,
David S. Miller08ec9af2009-03-13 14:22:40 -0700745 struct xfrm_state **best, int *acq_in_progress,
746 int *error)
747{
748 /* Resolution logic:
749 * 1. There is a valid state with matching selector. Done.
750 * 2. Valid state with inappropriate selector. Skip.
751 *
752 * Entering area of "sysdeps".
753 *
754 * 3. If state is not valid, selector is temporary, it selects
755 * only session which triggered previous resolution. Key
756 * manager will do something to install a state with proper
757 * selector.
758 */
759 if (x->km.state == XFRM_STATE_VALID) {
760 if ((x->sel.family &&
761 !xfrm_selector_match(&x->sel, fl, x->sel.family)) ||
762 !security_xfrm_state_pol_flow_match(x, pol, fl))
763 return;
764
765 if (!*best ||
766 (*best)->km.dying > x->km.dying ||
767 ((*best)->km.dying == x->km.dying &&
768 (*best)->curlft.add_time < x->curlft.add_time))
769 *best = x;
770 } else if (x->km.state == XFRM_STATE_ACQ) {
771 *acq_in_progress = 1;
772 } else if (x->km.state == XFRM_STATE_ERROR ||
773 x->km.state == XFRM_STATE_EXPIRED) {
774 if (xfrm_selector_match(&x->sel, fl, x->sel.family) &&
775 security_xfrm_state_pol_flow_match(x, pol, fl))
776 *error = -ESRCH;
777 }
778}
779
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780struct xfrm_state *
David S. Miller33765d02011-02-24 01:55:45 -0500781xfrm_state_find(const xfrm_address_t *daddr, const xfrm_address_t *saddr,
David S. Millerb520e9f2011-02-22 18:24:19 -0800782 const struct flowi *fl, struct xfrm_tmpl *tmpl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 struct xfrm_policy *pol, int *err,
784 unsigned short family)
785{
David S. Miller08ec9af2009-03-13 14:22:40 -0700786 static xfrm_address_t saddr_wildcard = { };
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800787 struct net *net = xp_net(pol);
Nicolas Dichtel6a783c92009-04-27 02:58:59 -0700788 unsigned int h, h_wildcard;
David S. Miller37b08e32008-09-02 20:14:15 -0700789 struct xfrm_state *x, *x0, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 int acquire_in_progress = 0;
791 int error = 0;
792 struct xfrm_state *best = NULL;
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800793 u32 mark = pol->mark.v & pol->mark.m;
Thomas Egerer8444cf72010-09-20 11:11:38 -0700794 unsigned short encap_family = tmpl->encap_family;
Florian Westphalb65e3d72016-08-09 12:16:07 +0200795 unsigned int sequence;
Horia Geanta0f245582014-02-12 16:20:06 +0200796 struct km_event c;
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900797
David S. Miller37b08e32008-09-02 20:14:15 -0700798 to_put = NULL;
799
Florian Westphalb65e3d72016-08-09 12:16:07 +0200800 sequence = read_seqcount_begin(&xfrm_state_hash_generation);
801
Fan Du283bc9f2013-11-07 17:47:50 +0800802 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Thomas Egerer8444cf72010-09-20 11:11:38 -0700803 h = xfrm_dst_hash(net, daddr, saddr, tmpl->reqid, encap_family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200804 hlist_for_each_entry_rcu(x, net->xfrm.state_bydst + h, bydst) {
Thomas Egerer8444cf72010-09-20 11:11:38 -0700805 if (x->props.family == encap_family &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 x->props.reqid == tmpl->reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000807 (mark & x->mark.m) == x->mark.v &&
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -0700808 !(x->props.flags & XFRM_STATE_WILDRECV) &&
Thomas Egerer8444cf72010-09-20 11:11:38 -0700809 xfrm_state_addr_check(x, daddr, saddr, encap_family) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 tmpl->mode == x->props.mode &&
811 tmpl->id.proto == x->id.proto &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700812 (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
David S. Miller1f673c52011-02-24 01:53:13 -0500813 xfrm_state_look_at(pol, x, fl, encap_family,
David S. Miller08ec9af2009-03-13 14:22:40 -0700814 &best, &acquire_in_progress, &error);
815 }
Fan Du6f115632013-09-23 17:18:25 +0800816 if (best || acquire_in_progress)
David S. Miller08ec9af2009-03-13 14:22:40 -0700817 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
Thomas Egerer8444cf72010-09-20 11:11:38 -0700819 h_wildcard = xfrm_dst_hash(net, daddr, &saddr_wildcard, tmpl->reqid, encap_family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200820 hlist_for_each_entry_rcu(x, net->xfrm.state_bydst + h_wildcard, bydst) {
Thomas Egerer8444cf72010-09-20 11:11:38 -0700821 if (x->props.family == encap_family &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700822 x->props.reqid == tmpl->reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000823 (mark & x->mark.m) == x->mark.v &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700824 !(x->props.flags & XFRM_STATE_WILDRECV) &&
Fan Duf59bbdf2013-09-27 16:32:50 +0800825 xfrm_addr_equal(&x->id.daddr, daddr, encap_family) &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700826 tmpl->mode == x->props.mode &&
827 tmpl->id.proto == x->id.proto &&
828 (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
David S. Miller1f673c52011-02-24 01:53:13 -0500829 xfrm_state_look_at(pol, x, fl, encap_family,
David S. Miller08ec9af2009-03-13 14:22:40 -0700830 &best, &acquire_in_progress, &error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 }
832
David S. Miller08ec9af2009-03-13 14:22:40 -0700833found:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 x = best;
835 if (!x && !error && !acquire_in_progress) {
Patrick McHardy5c5d2812005-04-21 20:12:32 -0700836 if (tmpl->id.spi &&
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800837 (x0 = __xfrm_state_lookup(net, mark, daddr, tmpl->id.spi,
Thomas Egerer8444cf72010-09-20 11:11:38 -0700838 tmpl->id.proto, encap_family)) != NULL) {
David S. Miller37b08e32008-09-02 20:14:15 -0700839 to_put = x0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 error = -EEXIST;
841 goto out;
842 }
Horia Geanta0f245582014-02-12 16:20:06 +0200843
844 c.net = net;
845 /* If the KMs have no listeners (yet...), avoid allocating an SA
846 * for each and every packet - garbage collection might not
847 * handle the flood.
848 */
849 if (!km_is_alive(&c)) {
850 error = -ESRCH;
851 goto out;
852 }
853
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800854 x = xfrm_state_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 if (x == NULL) {
856 error = -ENOMEM;
857 goto out;
858 }
Thomas Egerer8444cf72010-09-20 11:11:38 -0700859 /* Initialize temporary state matching only
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 * to current session. */
Thomas Egerer8444cf72010-09-20 11:11:38 -0700861 xfrm_init_tempstate(x, fl, tmpl, daddr, saddr, family);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800862 memcpy(&x->mark, &pol->mark, sizeof(x->mark));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
David S. Miller1d28f422011-03-12 00:29:39 -0500864 error = security_xfrm_state_alloc_acquire(x, pol->security, fl->flowi_secid);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -0700865 if (error) {
866 x->km.state = XFRM_STATE_DEAD;
David S. Miller37b08e32008-09-02 20:14:15 -0700867 to_put = x;
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -0700868 x = NULL;
869 goto out;
870 }
871
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 if (km_query(x, tmpl, pol) == 0) {
873 x->km.state = XFRM_STATE_ACQ;
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800874 list_add(&x->km.all, &net->xfrm.state_all);
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200875 hlist_add_head_rcu(&x->bydst, net->xfrm.state_bydst + h);
Thomas Egerer8444cf72010-09-20 11:11:38 -0700876 h = xfrm_src_hash(net, daddr, saddr, encap_family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200877 hlist_add_head_rcu(&x->bysrc, net->xfrm.state_bysrc + h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 if (x->id.spi) {
Thomas Egerer8444cf72010-09-20 11:11:38 -0700879 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, encap_family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200880 hlist_add_head_rcu(&x->byspi, net->xfrm.state_byspi + h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 }
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800882 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800883 tasklet_hrtimer_start(&x->mtimer, ktime_set(net->xfrm.sysctl_acq_expires, 0), HRTIMER_MODE_REL);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800884 net->xfrm.state_num++;
885 xfrm_hash_grow_check(net, x->bydst.next != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 } else {
887 x->km.state = XFRM_STATE_DEAD;
David S. Miller37b08e32008-09-02 20:14:15 -0700888 to_put = x;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 x = NULL;
890 error = -ESRCH;
891 }
892 }
893out:
Florian Westphal02efdff2016-08-09 12:16:05 +0200894 if (x) {
895 if (!xfrm_state_hold_rcu(x)) {
896 *err = -EAGAIN;
897 x = NULL;
898 }
899 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 *err = acquire_in_progress ? -EAGAIN : error;
Florian Westphal02efdff2016-08-09 12:16:05 +0200901 }
Fan Du283bc9f2013-11-07 17:47:50 +0800902 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
David S. Miller37b08e32008-09-02 20:14:15 -0700903 if (to_put)
904 xfrm_state_put(to_put);
Florian Westphalb65e3d72016-08-09 12:16:07 +0200905
906 if (read_seqcount_retry(&xfrm_state_hash_generation, sequence)) {
907 *err = -EAGAIN;
908 if (x) {
909 xfrm_state_put(x);
910 x = NULL;
911 }
912 }
913
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 return x;
915}
916
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700917struct xfrm_state *
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800918xfrm_stateonly_find(struct net *net, u32 mark,
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800919 xfrm_address_t *daddr, xfrm_address_t *saddr,
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700920 unsigned short family, u8 mode, u8 proto, u32 reqid)
921{
Pavel Emelyanov4bda4f22007-12-14 11:38:04 -0800922 unsigned int h;
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700923 struct xfrm_state *rx = NULL, *x = NULL;
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700924
Fan Du4ae770b2014-01-03 11:18:29 +0800925 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800926 h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800927 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700928 if (x->props.family == family &&
929 x->props.reqid == reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000930 (mark & x->mark.m) == x->mark.v &&
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700931 !(x->props.flags & XFRM_STATE_WILDRECV) &&
932 xfrm_state_addr_check(x, daddr, saddr, family) &&
933 mode == x->props.mode &&
934 proto == x->id.proto &&
935 x->km.state == XFRM_STATE_VALID) {
936 rx = x;
937 break;
938 }
939 }
940
941 if (rx)
942 xfrm_state_hold(rx);
Fan Du4ae770b2014-01-03 11:18:29 +0800943 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700944
945
946 return rx;
947}
948EXPORT_SYMBOL(xfrm_stateonly_find);
949
Fan Duc4549972014-01-03 11:18:32 +0800950struct xfrm_state *xfrm_state_lookup_byspi(struct net *net, __be32 spi,
951 unsigned short family)
952{
953 struct xfrm_state *x;
954 struct xfrm_state_walk *w;
955
956 spin_lock_bh(&net->xfrm.xfrm_state_lock);
957 list_for_each_entry(w, &net->xfrm.state_all, all) {
958 x = container_of(w, struct xfrm_state, km);
959 if (x->props.family != family ||
960 x->id.spi != spi)
961 continue;
962
Fan Duc4549972014-01-03 11:18:32 +0800963 xfrm_state_hold(x);
Li RongQingbdddbf62015-04-29 08:42:44 +0800964 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Fan Duc4549972014-01-03 11:18:32 +0800965 return x;
966 }
967 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
968 return NULL;
969}
970EXPORT_SYMBOL(xfrm_state_lookup_byspi);
971
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972static void __xfrm_state_insert(struct xfrm_state *x)
973{
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800974 struct net *net = xs_net(x);
David S. Millera624c102006-08-24 03:24:33 -0700975 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800977 list_add(&x->km.all, &net->xfrm.state_all);
Timo Teras4c563f72008-02-28 21:31:08 -0800978
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800979 h = xfrm_dst_hash(net, &x->id.daddr, &x->props.saddr,
David S. Millerc1969f22006-08-24 04:00:03 -0700980 x->props.reqid, x->props.family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200981 hlist_add_head_rcu(&x->bydst, net->xfrm.state_bydst + h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800983 h = xfrm_src_hash(net, &x->id.daddr, &x->props.saddr, x->props.family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200984 hlist_add_head_rcu(&x->bysrc, net->xfrm.state_bysrc + h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
Masahide NAKAMURA7b4dc3602006-09-27 22:21:52 -0700986 if (x->id.spi) {
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800987 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto,
Masahide NAKAMURA6c44e6b2006-08-23 17:53:57 -0700988 x->props.family);
989
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200990 hlist_add_head_rcu(&x->byspi, net->xfrm.state_byspi + h);
Masahide NAKAMURA6c44e6b2006-08-23 17:53:57 -0700991 }
992
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800993 tasklet_hrtimer_start(&x->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL);
David S. Millera47f0ce2006-08-24 03:54:22 -0700994 if (x->replay_maxage)
995 mod_timer(&x->rtimer, jiffies + x->replay_maxage);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -0800996
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800997 net->xfrm.state_num++;
David S. Millerf034b5d2006-08-24 03:08:07 -0700998
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800999 xfrm_hash_grow_check(net, x->bydst.next != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000}
1001
Fan Du283bc9f2013-11-07 17:47:50 +08001002/* net->xfrm.xfrm_state_lock is held */
David S. Millerc7f5ea32006-08-24 03:29:04 -07001003static void __xfrm_state_bump_genids(struct xfrm_state *xnew)
1004{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001005 struct net *net = xs_net(xnew);
David S. Millerc7f5ea32006-08-24 03:29:04 -07001006 unsigned short family = xnew->props.family;
1007 u32 reqid = xnew->props.reqid;
1008 struct xfrm_state *x;
David S. Millerc7f5ea32006-08-24 03:29:04 -07001009 unsigned int h;
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001010 u32 mark = xnew->mark.v & xnew->mark.m;
David S. Millerc7f5ea32006-08-24 03:29:04 -07001011
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001012 h = xfrm_dst_hash(net, &xnew->id.daddr, &xnew->props.saddr, reqid, family);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001013 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
David S. Millerc7f5ea32006-08-24 03:29:04 -07001014 if (x->props.family == family &&
1015 x->props.reqid == reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001016 (mark & x->mark.m) == x->mark.v &&
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001017 xfrm_addr_equal(&x->id.daddr, &xnew->id.daddr, family) &&
1018 xfrm_addr_equal(&x->props.saddr, &xnew->props.saddr, family))
Herbert Xu34996cb2010-03-31 01:19:49 +00001019 x->genid++;
David S. Millerc7f5ea32006-08-24 03:29:04 -07001020 }
1021}
1022
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023void xfrm_state_insert(struct xfrm_state *x)
1024{
Fan Du283bc9f2013-11-07 17:47:50 +08001025 struct net *net = xs_net(x);
1026
1027 spin_lock_bh(&net->xfrm.xfrm_state_lock);
David S. Millerc7f5ea32006-08-24 03:29:04 -07001028 __xfrm_state_bump_genids(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 __xfrm_state_insert(x);
Fan Du283bc9f2013-11-07 17:47:50 +08001030 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031}
1032EXPORT_SYMBOL(xfrm_state_insert);
1033
Fan Du283bc9f2013-11-07 17:47:50 +08001034/* net->xfrm.xfrm_state_lock is held */
Mathias Krausee473fcb2013-06-26 23:56:58 +02001035static struct xfrm_state *__find_acq_core(struct net *net,
1036 const struct xfrm_mark *m,
David S. Millera70486f2011-02-27 23:17:24 -08001037 unsigned short family, u8 mode,
1038 u32 reqid, u8 proto,
1039 const xfrm_address_t *daddr,
Mathias Krausee473fcb2013-06-26 23:56:58 +02001040 const xfrm_address_t *saddr,
1041 int create)
David S. Miller27708342006-08-24 00:13:10 -07001042{
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001043 unsigned int h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
David S. Miller27708342006-08-24 00:13:10 -07001044 struct xfrm_state *x;
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001045 u32 mark = m->v & m->m;
David S. Miller27708342006-08-24 00:13:10 -07001046
Sasha Levinb67bfe02013-02-27 17:06:00 -08001047 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
David S. Miller27708342006-08-24 00:13:10 -07001048 if (x->props.reqid != reqid ||
1049 x->props.mode != mode ||
1050 x->props.family != family ||
1051 x->km.state != XFRM_STATE_ACQ ||
Joy Latten75e252d2007-03-12 17:14:07 -07001052 x->id.spi != 0 ||
Wei Yongjun18025712009-06-28 18:42:53 +00001053 x->id.proto != proto ||
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001054 (mark & x->mark.m) != x->mark.v ||
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001055 !xfrm_addr_equal(&x->id.daddr, daddr, family) ||
1056 !xfrm_addr_equal(&x->props.saddr, saddr, family))
David S. Miller27708342006-08-24 00:13:10 -07001057 continue;
1058
David S. Miller27708342006-08-24 00:13:10 -07001059 xfrm_state_hold(x);
1060 return x;
1061 }
1062
1063 if (!create)
1064 return NULL;
1065
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001066 x = xfrm_state_alloc(net);
David S. Miller27708342006-08-24 00:13:10 -07001067 if (likely(x)) {
1068 switch (family) {
1069 case AF_INET:
1070 x->sel.daddr.a4 = daddr->a4;
1071 x->sel.saddr.a4 = saddr->a4;
1072 x->sel.prefixlen_d = 32;
1073 x->sel.prefixlen_s = 32;
1074 x->props.saddr.a4 = saddr->a4;
1075 x->id.daddr.a4 = daddr->a4;
1076 break;
1077
1078 case AF_INET6:
Jiri Benc15e318b2015-03-29 16:59:24 +02001079 x->sel.daddr.in6 = daddr->in6;
1080 x->sel.saddr.in6 = saddr->in6;
David S. Miller27708342006-08-24 00:13:10 -07001081 x->sel.prefixlen_d = 128;
1082 x->sel.prefixlen_s = 128;
Jiri Benc15e318b2015-03-29 16:59:24 +02001083 x->props.saddr.in6 = saddr->in6;
1084 x->id.daddr.in6 = daddr->in6;
David S. Miller27708342006-08-24 00:13:10 -07001085 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001086 }
David S. Miller27708342006-08-24 00:13:10 -07001087
1088 x->km.state = XFRM_STATE_ACQ;
1089 x->id.proto = proto;
1090 x->props.family = family;
1091 x->props.mode = mode;
1092 x->props.reqid = reqid;
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001093 x->mark.v = m->v;
1094 x->mark.m = m->m;
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08001095 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
David S. Miller27708342006-08-24 00:13:10 -07001096 xfrm_state_hold(x);
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -08001097 tasklet_hrtimer_start(&x->mtimer, ktime_set(net->xfrm.sysctl_acq_expires, 0), HRTIMER_MODE_REL);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001098 list_add(&x->km.all, &net->xfrm.state_all);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001099 hlist_add_head_rcu(&x->bydst, net->xfrm.state_bydst + h);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001100 h = xfrm_src_hash(net, daddr, saddr, family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001101 hlist_add_head_rcu(&x->bysrc, net->xfrm.state_bysrc + h);
David S. Miller918049f2006-10-12 22:03:24 -07001102
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001103 net->xfrm.state_num++;
David S. Miller918049f2006-10-12 22:03:24 -07001104
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001105 xfrm_hash_grow_check(net, x->bydst.next != NULL);
David S. Miller27708342006-08-24 00:13:10 -07001106 }
1107
1108 return x;
1109}
1110
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001111static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
1113int xfrm_state_add(struct xfrm_state *x)
1114{
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001115 struct net *net = xs_net(x);
David S. Miller37b08e32008-09-02 20:14:15 -07001116 struct xfrm_state *x1, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 int family;
1118 int err;
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001119 u32 mark = x->mark.v & x->mark.m;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001120 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
1122 family = x->props.family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
David S. Miller37b08e32008-09-02 20:14:15 -07001124 to_put = NULL;
1125
Fan Du283bc9f2013-11-07 17:47:50 +08001126 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
David S. Milleredcd5822006-08-24 00:42:45 -07001128 x1 = __xfrm_state_locate(x, use_spi, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 if (x1) {
David S. Miller37b08e32008-09-02 20:14:15 -07001130 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 x1 = NULL;
1132 err = -EEXIST;
1133 goto out;
1134 }
1135
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001136 if (use_spi && x->km.seq) {
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001137 x1 = __xfrm_find_acq_byseq(net, mark, x->km.seq);
Joy Latten75e252d2007-03-12 17:14:07 -07001138 if (x1 && ((x1->id.proto != x->id.proto) ||
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001139 !xfrm_addr_equal(&x1->id.daddr, &x->id.daddr, family))) {
David S. Miller37b08e32008-09-02 20:14:15 -07001140 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 x1 = NULL;
1142 }
1143 }
1144
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001145 if (use_spi && !x1)
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001146 x1 = __find_acq_core(net, &x->mark, family, x->props.mode,
1147 x->props.reqid, x->id.proto,
David S. Miller27708342006-08-24 00:13:10 -07001148 &x->id.daddr, &x->props.saddr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
David S. Millerc7f5ea32006-08-24 03:29:04 -07001150 __xfrm_state_bump_genids(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 __xfrm_state_insert(x);
1152 err = 0;
1153
1154out:
Fan Du283bc9f2013-11-07 17:47:50 +08001155 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
1157 if (x1) {
1158 xfrm_state_delete(x1);
1159 xfrm_state_put(x1);
1160 }
1161
David S. Miller37b08e32008-09-02 20:14:15 -07001162 if (to_put)
1163 xfrm_state_put(to_put);
1164
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 return err;
1166}
1167EXPORT_SYMBOL(xfrm_state_add);
1168
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001169#ifdef CONFIG_XFRM_MIGRATE
Steffen Klassertcc9ab602014-02-19 13:33:24 +01001170static struct xfrm_state *xfrm_state_clone(struct xfrm_state *orig)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001171{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001172 struct net *net = xs_net(orig);
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001173 struct xfrm_state *x = xfrm_state_alloc(net);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001174 if (!x)
Herbert Xu553f9112010-02-15 20:00:51 +00001175 goto out;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001176
1177 memcpy(&x->id, &orig->id, sizeof(x->id));
1178 memcpy(&x->sel, &orig->sel, sizeof(x->sel));
1179 memcpy(&x->lft, &orig->lft, sizeof(x->lft));
1180 x->props.mode = orig->props.mode;
1181 x->props.replay_window = orig->props.replay_window;
1182 x->props.reqid = orig->props.reqid;
1183 x->props.family = orig->props.family;
1184 x->props.saddr = orig->props.saddr;
1185
1186 if (orig->aalg) {
Martin Willi4447bb32009-11-25 00:29:52 +00001187 x->aalg = xfrm_algo_auth_clone(orig->aalg);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001188 if (!x->aalg)
1189 goto error;
1190 }
1191 x->props.aalgo = orig->props.aalgo;
1192
Steffen Klassertee5c2312014-02-19 13:33:24 +01001193 if (orig->aead) {
1194 x->aead = xfrm_algo_aead_clone(orig->aead);
1195 if (!x->aead)
1196 goto error;
1197 }
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001198 if (orig->ealg) {
1199 x->ealg = xfrm_algo_clone(orig->ealg);
1200 if (!x->ealg)
1201 goto error;
1202 }
1203 x->props.ealgo = orig->props.ealgo;
1204
1205 if (orig->calg) {
1206 x->calg = xfrm_algo_clone(orig->calg);
1207 if (!x->calg)
1208 goto error;
1209 }
1210 x->props.calgo = orig->props.calgo;
1211
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001212 if (orig->encap) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001213 x->encap = kmemdup(orig->encap, sizeof(*x->encap), GFP_KERNEL);
1214 if (!x->encap)
1215 goto error;
1216 }
1217
1218 if (orig->coaddr) {
1219 x->coaddr = kmemdup(orig->coaddr, sizeof(*x->coaddr),
1220 GFP_KERNEL);
1221 if (!x->coaddr)
1222 goto error;
1223 }
1224
Steffen Klassertaf2f4642011-03-28 19:46:39 +00001225 if (orig->replay_esn) {
Steffen Klassertcc9ab602014-02-19 13:33:24 +01001226 if (xfrm_replay_clone(x, orig))
Steffen Klassertaf2f4642011-03-28 19:46:39 +00001227 goto error;
1228 }
1229
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001230 memcpy(&x->mark, &orig->mark, sizeof(x->mark));
1231
Steffen Klassertcc9ab602014-02-19 13:33:24 +01001232 if (xfrm_init_state(x) < 0)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001233 goto error;
1234
1235 x->props.flags = orig->props.flags;
Nicolas Dichtela947b0a2013-02-22 10:54:54 +01001236 x->props.extra_flags = orig->props.extra_flags;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001237
Steffen Klassertee5c2312014-02-19 13:33:24 +01001238 x->tfcpad = orig->tfcpad;
1239 x->replay_maxdiff = orig->replay_maxdiff;
1240 x->replay_maxage = orig->replay_maxage;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001241 x->curlft.add_time = orig->curlft.add_time;
1242 x->km.state = orig->km.state;
1243 x->km.seq = orig->km.seq;
1244
1245 return x;
1246
1247 error:
Herbert Xu553f9112010-02-15 20:00:51 +00001248 xfrm_state_put(x);
1249out:
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001250 return NULL;
1251}
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001252
Fan Du283bc9f2013-11-07 17:47:50 +08001253struct xfrm_state *xfrm_migrate_state_find(struct xfrm_migrate *m, struct net *net)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001254{
1255 unsigned int h;
Steffen Klassert8c0cba22014-02-19 13:33:24 +01001256 struct xfrm_state *x = NULL;
1257
1258 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001259
1260 if (m->reqid) {
Fan Du283bc9f2013-11-07 17:47:50 +08001261 h = xfrm_dst_hash(net, &m->old_daddr, &m->old_saddr,
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001262 m->reqid, m->old_family);
Fan Du283bc9f2013-11-07 17:47:50 +08001263 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001264 if (x->props.mode != m->mode ||
1265 x->id.proto != m->proto)
1266 continue;
1267 if (m->reqid && x->props.reqid != m->reqid)
1268 continue;
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001269 if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr,
1270 m->old_family) ||
1271 !xfrm_addr_equal(&x->props.saddr, &m->old_saddr,
1272 m->old_family))
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001273 continue;
1274 xfrm_state_hold(x);
Steffen Klassert8c0cba22014-02-19 13:33:24 +01001275 break;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001276 }
1277 } else {
Fan Du283bc9f2013-11-07 17:47:50 +08001278 h = xfrm_src_hash(net, &m->old_daddr, &m->old_saddr,
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001279 m->old_family);
Fan Du283bc9f2013-11-07 17:47:50 +08001280 hlist_for_each_entry(x, net->xfrm.state_bysrc+h, bysrc) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001281 if (x->props.mode != m->mode ||
1282 x->id.proto != m->proto)
1283 continue;
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001284 if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr,
1285 m->old_family) ||
1286 !xfrm_addr_equal(&x->props.saddr, &m->old_saddr,
1287 m->old_family))
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001288 continue;
1289 xfrm_state_hold(x);
Steffen Klassert8c0cba22014-02-19 13:33:24 +01001290 break;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001291 }
1292 }
1293
Steffen Klassert8c0cba22014-02-19 13:33:24 +01001294 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1295
1296 return x;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001297}
1298EXPORT_SYMBOL(xfrm_migrate_state_find);
1299
Weilong Chen3e94c2d2013-12-24 09:43:47 +08001300struct xfrm_state *xfrm_state_migrate(struct xfrm_state *x,
1301 struct xfrm_migrate *m)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001302{
1303 struct xfrm_state *xc;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001304
Steffen Klassertcc9ab602014-02-19 13:33:24 +01001305 xc = xfrm_state_clone(x);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001306 if (!xc)
1307 return NULL;
1308
1309 memcpy(&xc->id.daddr, &m->new_daddr, sizeof(xc->id.daddr));
1310 memcpy(&xc->props.saddr, &m->new_saddr, sizeof(xc->props.saddr));
1311
1312 /* add state */
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001313 if (xfrm_addr_equal(&x->id.daddr, &m->new_daddr, m->new_family)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001314 /* a care is needed when the destination address of the
1315 state is to be updated as it is a part of triplet */
1316 xfrm_state_insert(xc);
1317 } else {
Steffen Klassertcc9ab602014-02-19 13:33:24 +01001318 if (xfrm_state_add(xc) < 0)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001319 goto error;
1320 }
1321
1322 return xc;
1323error:
Thomas Egerer78347c82010-12-06 23:28:56 +00001324 xfrm_state_put(xc);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001325 return NULL;
1326}
1327EXPORT_SYMBOL(xfrm_state_migrate);
1328#endif
1329
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330int xfrm_state_update(struct xfrm_state *x)
1331{
David S. Miller37b08e32008-09-02 20:14:15 -07001332 struct xfrm_state *x1, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 int err;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001334 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
Fan Du283bc9f2013-11-07 17:47:50 +08001335 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
David S. Miller37b08e32008-09-02 20:14:15 -07001337 to_put = NULL;
1338
Fan Du283bc9f2013-11-07 17:47:50 +08001339 spin_lock_bh(&net->xfrm.xfrm_state_lock);
David S. Milleredcd5822006-08-24 00:42:45 -07001340 x1 = __xfrm_state_locate(x, use_spi, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
1342 err = -ESRCH;
1343 if (!x1)
1344 goto out;
1345
1346 if (xfrm_state_kern(x1)) {
David S. Miller37b08e32008-09-02 20:14:15 -07001347 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 err = -EEXIST;
1349 goto out;
1350 }
1351
1352 if (x1->km.state == XFRM_STATE_ACQ) {
1353 __xfrm_state_insert(x);
1354 x = NULL;
1355 }
1356 err = 0;
1357
1358out:
Fan Du283bc9f2013-11-07 17:47:50 +08001359 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
David S. Miller37b08e32008-09-02 20:14:15 -07001361 if (to_put)
1362 xfrm_state_put(to_put);
1363
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 if (err)
1365 return err;
1366
1367 if (!x) {
1368 xfrm_state_delete(x1);
1369 xfrm_state_put(x1);
1370 return 0;
1371 }
1372
1373 err = -EINVAL;
1374 spin_lock_bh(&x1->lock);
1375 if (likely(x1->km.state == XFRM_STATE_VALID)) {
1376 if (x->encap && x1->encap)
1377 memcpy(x1->encap, x->encap, sizeof(*x1->encap));
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -07001378 if (x->coaddr && x1->coaddr) {
1379 memcpy(x1->coaddr, x->coaddr, sizeof(*x1->coaddr));
1380 }
1381 if (!use_spi && memcmp(&x1->sel, &x->sel, sizeof(x1->sel)))
1382 memcpy(&x1->sel, &x->sel, sizeof(x1->sel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 memcpy(&x1->lft, &x->lft, sizeof(x1->lft));
1384 x1->km.dying = 0;
1385
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -08001386 tasklet_hrtimer_start(&x1->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 if (x1->curlft.use_time)
1388 xfrm_state_check_expire(x1);
1389
1390 err = 0;
Tushar Gohad8fcbc632011-07-07 15:38:52 +00001391 x->km.state = XFRM_STATE_DEAD;
1392 __xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 }
1394 spin_unlock_bh(&x1->lock);
1395
1396 xfrm_state_put(x1);
1397
1398 return err;
1399}
1400EXPORT_SYMBOL(xfrm_state_update);
1401
1402int xfrm_state_check_expire(struct xfrm_state *x)
1403{
1404 if (!x->curlft.use_time)
James Morris9d729f72007-03-04 16:12:44 -08001405 x->curlft.use_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 if (x->curlft.bytes >= x->lft.hard_byte_limit ||
1408 x->curlft.packets >= x->lft.hard_packet_limit) {
Herbert Xu4666faa2005-06-18 22:43:22 -07001409 x->km.state = XFRM_STATE_EXPIRED;
Weilong Chen9b7a7872013-12-24 09:43:46 +08001410 tasklet_hrtimer_start(&x->mtimer, ktime_set(0, 0), HRTIMER_MODE_REL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 return -EINVAL;
1412 }
1413
1414 if (!x->km.dying &&
1415 (x->curlft.bytes >= x->lft.soft_byte_limit ||
Herbert Xu4666faa2005-06-18 22:43:22 -07001416 x->curlft.packets >= x->lft.soft_packet_limit)) {
1417 x->km.dying = 1;
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -08001418 km_state_expired(x, 0, 0);
Herbert Xu4666faa2005-06-18 22:43:22 -07001419 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 return 0;
1421}
1422EXPORT_SYMBOL(xfrm_state_check_expire);
1423
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424struct xfrm_state *
David S. Millera70486f2011-02-27 23:17:24 -08001425xfrm_state_lookup(struct net *net, u32 mark, const xfrm_address_t *daddr, __be32 spi,
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001426 u8 proto, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427{
1428 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
Fan Du283bc9f2013-11-07 17:47:50 +08001430 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001431 x = __xfrm_state_lookup(net, mark, daddr, spi, proto, family);
Fan Du283bc9f2013-11-07 17:47:50 +08001432 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 return x;
1434}
1435EXPORT_SYMBOL(xfrm_state_lookup);
1436
1437struct xfrm_state *
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001438xfrm_state_lookup_byaddr(struct net *net, u32 mark,
David S. Millera70486f2011-02-27 23:17:24 -08001439 const xfrm_address_t *daddr, const xfrm_address_t *saddr,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001440 u8 proto, unsigned short family)
1441{
1442 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001443
Fan Du283bc9f2013-11-07 17:47:50 +08001444 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001445 x = __xfrm_state_lookup_byaddr(net, mark, daddr, saddr, proto, family);
Fan Du283bc9f2013-11-07 17:47:50 +08001446 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001447 return x;
1448}
1449EXPORT_SYMBOL(xfrm_state_lookup_byaddr);
1450
1451struct xfrm_state *
Mathias Krausee473fcb2013-06-26 23:56:58 +02001452xfrm_find_acq(struct net *net, const struct xfrm_mark *mark, u8 mode, u32 reqid,
1453 u8 proto, const xfrm_address_t *daddr,
1454 const xfrm_address_t *saddr, int create, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455{
1456 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
Fan Du283bc9f2013-11-07 17:47:50 +08001458 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001459 x = __find_acq_core(net, mark, family, mode, reqid, proto, daddr, saddr, create);
Fan Du283bc9f2013-11-07 17:47:50 +08001460 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
David S. Miller27708342006-08-24 00:13:10 -07001461
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 return x;
1463}
1464EXPORT_SYMBOL(xfrm_find_acq);
1465
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001466#ifdef CONFIG_XFRM_SUB_POLICY
1467int
1468xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n,
Fan Du283bc9f2013-11-07 17:47:50 +08001469 unsigned short family, struct net *net)
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001470{
1471 int err = 0;
1472 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
1473 if (!afinfo)
1474 return -EAFNOSUPPORT;
1475
Fan Du283bc9f2013-11-07 17:47:50 +08001476 spin_lock_bh(&net->xfrm.xfrm_state_lock); /*FIXME*/
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001477 if (afinfo->tmpl_sort)
1478 err = afinfo->tmpl_sort(dst, src, n);
Fan Du283bc9f2013-11-07 17:47:50 +08001479 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001480 xfrm_state_put_afinfo(afinfo);
1481 return err;
1482}
1483EXPORT_SYMBOL(xfrm_tmpl_sort);
1484
1485int
1486xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src, int n,
1487 unsigned short family)
1488{
1489 int err = 0;
1490 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
Steffen Klassert35ea790d2014-02-19 13:33:23 +01001491 struct net *net = xs_net(*src);
Fan Du283bc9f2013-11-07 17:47:50 +08001492
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001493 if (!afinfo)
1494 return -EAFNOSUPPORT;
1495
Fan Du283bc9f2013-11-07 17:47:50 +08001496 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001497 if (afinfo->state_sort)
1498 err = afinfo->state_sort(dst, src, n);
Fan Du283bc9f2013-11-07 17:47:50 +08001499 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001500 xfrm_state_put_afinfo(afinfo);
1501 return err;
1502}
1503EXPORT_SYMBOL(xfrm_state_sort);
1504#endif
1505
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506/* Silly enough, but I'm lazy to build resolution list */
1507
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001508static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509{
1510 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001512 for (i = 0; i <= net->xfrm.state_hmask; i++) {
David S. Miller8f126e32006-08-24 02:45:07 -07001513 struct xfrm_state *x;
1514
Sasha Levinb67bfe02013-02-27 17:06:00 -08001515 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
David S. Miller8f126e32006-08-24 02:45:07 -07001516 if (x->km.seq == seq &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001517 (mark & x->mark.m) == x->mark.v &&
David S. Miller8f126e32006-08-24 02:45:07 -07001518 x->km.state == XFRM_STATE_ACQ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 xfrm_state_hold(x);
1520 return x;
1521 }
1522 }
1523 }
1524 return NULL;
1525}
1526
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001527struct xfrm_state *xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528{
1529 struct xfrm_state *x;
1530
Fan Du283bc9f2013-11-07 17:47:50 +08001531 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001532 x = __xfrm_find_acq_byseq(net, mark, seq);
Fan Du283bc9f2013-11-07 17:47:50 +08001533 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 return x;
1535}
1536EXPORT_SYMBOL(xfrm_find_acq_byseq);
1537
1538u32 xfrm_get_acqseq(void)
1539{
1540 u32 res;
jamal6836b9b2010-02-16 02:01:22 +00001541 static atomic_t acqseq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542
jamal6836b9b2010-02-16 02:01:22 +00001543 do {
1544 res = atomic_inc_return(&acqseq);
1545 } while (!res);
1546
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 return res;
1548}
1549EXPORT_SYMBOL(xfrm_get_acqseq);
1550
Fan Du776e9dd2013-12-16 18:47:49 +08001551int verify_spi_info(u8 proto, u32 min, u32 max)
1552{
1553 switch (proto) {
1554 case IPPROTO_AH:
1555 case IPPROTO_ESP:
1556 break;
1557
1558 case IPPROTO_COMP:
1559 /* IPCOMP spi is 16-bits. */
1560 if (max >= 0x10000)
1561 return -EINVAL;
1562 break;
1563
1564 default:
1565 return -EINVAL;
1566 }
1567
1568 if (min > max)
1569 return -EINVAL;
1570
1571 return 0;
1572}
1573EXPORT_SYMBOL(verify_spi_info);
1574
Herbert Xu658b2192007-10-09 13:29:52 -07001575int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -08001577 struct net *net = xs_net(x);
David S. Millerf034b5d2006-08-24 03:08:07 -07001578 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 struct xfrm_state *x0;
Herbert Xu658b2192007-10-09 13:29:52 -07001580 int err = -ENOENT;
1581 __be32 minspi = htonl(low);
1582 __be32 maxspi = htonl(high);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001583 u32 mark = x->mark.v & x->mark.m;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
Herbert Xu658b2192007-10-09 13:29:52 -07001585 spin_lock_bh(&x->lock);
1586 if (x->km.state == XFRM_STATE_DEAD)
1587 goto unlock;
1588
1589 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 if (x->id.spi)
Herbert Xu658b2192007-10-09 13:29:52 -07001591 goto unlock;
1592
1593 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
1595 if (minspi == maxspi) {
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001596 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, minspi, x->id.proto, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 if (x0) {
1598 xfrm_state_put(x0);
Herbert Xu658b2192007-10-09 13:29:52 -07001599 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 }
1601 x->id.spi = minspi;
1602 } else {
1603 u32 spi = 0;
Weilong Chen9b7a7872013-12-24 09:43:46 +08001604 for (h = 0; h < high-low+1; h++) {
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05001605 spi = low + prandom_u32()%(high-low+1);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001606 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, htonl(spi), x->id.proto, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 if (x0 == NULL) {
1608 x->id.spi = htonl(spi);
1609 break;
1610 }
1611 xfrm_state_put(x0);
1612 }
1613 }
1614 if (x->id.spi) {
Fan Du283bc9f2013-11-07 17:47:50 +08001615 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Alexey Dobriyan12604d82008-11-25 17:31:18 -08001616 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, x->props.family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001617 hlist_add_head_rcu(&x->byspi, net->xfrm.state_byspi + h);
Fan Du283bc9f2013-11-07 17:47:50 +08001618 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Herbert Xu658b2192007-10-09 13:29:52 -07001619
1620 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 }
Herbert Xu658b2192007-10-09 13:29:52 -07001622
1623unlock:
1624 spin_unlock_bh(&x->lock);
1625
1626 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627}
1628EXPORT_SYMBOL(xfrm_alloc_spi);
1629
Nicolas Dichteld3623092014-02-14 15:30:36 +01001630static bool __xfrm_state_filter_match(struct xfrm_state *x,
Nicolas Dichtel870a2df2014-03-06 18:24:29 +01001631 struct xfrm_address_filter *filter)
Nicolas Dichteld3623092014-02-14 15:30:36 +01001632{
1633 if (filter) {
1634 if ((filter->family == AF_INET ||
1635 filter->family == AF_INET6) &&
1636 x->props.family != filter->family)
1637 return false;
1638
1639 return addr_match(&x->props.saddr, &filter->saddr,
1640 filter->splen) &&
1641 addr_match(&x->id.daddr, &filter->daddr,
1642 filter->dplen);
1643 }
1644 return true;
1645}
1646
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08001647int xfrm_state_walk(struct net *net, struct xfrm_state_walk *walk,
Timo Teras4c563f72008-02-28 21:31:08 -08001648 int (*func)(struct xfrm_state *, int, void*),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 void *data)
1650{
Herbert Xu12a169e2008-10-01 07:03:24 -07001651 struct xfrm_state *state;
1652 struct xfrm_state_walk *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 int err = 0;
1654
Herbert Xu12a169e2008-10-01 07:03:24 -07001655 if (walk->seq != 0 && list_empty(&walk->all))
Timo Teras4c563f72008-02-28 21:31:08 -08001656 return 0;
1657
Fan Du283bc9f2013-11-07 17:47:50 +08001658 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001659 if (list_empty(&walk->all))
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08001660 x = list_first_entry(&net->xfrm.state_all, struct xfrm_state_walk, all);
Herbert Xu12a169e2008-10-01 07:03:24 -07001661 else
Li RongQing80077702015-04-22 17:09:54 +08001662 x = list_first_entry(&walk->all, struct xfrm_state_walk, all);
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08001663 list_for_each_entry_from(x, &net->xfrm.state_all, all) {
Herbert Xu12a169e2008-10-01 07:03:24 -07001664 if (x->state == XFRM_STATE_DEAD)
Timo Teras4c563f72008-02-28 21:31:08 -08001665 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07001666 state = container_of(x, struct xfrm_state, km);
1667 if (!xfrm_id_proto_match(state->id.proto, walk->proto))
Timo Teras4c563f72008-02-28 21:31:08 -08001668 continue;
Nicolas Dichteld3623092014-02-14 15:30:36 +01001669 if (!__xfrm_state_filter_match(state, walk->filter))
1670 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07001671 err = func(state, walk->seq, data);
1672 if (err) {
1673 list_move_tail(&walk->all, &x->all);
1674 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001676 walk->seq++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001678 if (walk->seq == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 err = -ENOENT;
1680 goto out;
1681 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001682 list_del_init(&walk->all);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683out:
Fan Du283bc9f2013-11-07 17:47:50 +08001684 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 return err;
1686}
1687EXPORT_SYMBOL(xfrm_state_walk);
1688
Nicolas Dichteld3623092014-02-14 15:30:36 +01001689void xfrm_state_walk_init(struct xfrm_state_walk *walk, u8 proto,
Nicolas Dichtel870a2df2014-03-06 18:24:29 +01001690 struct xfrm_address_filter *filter)
Herbert Xu5c182452008-09-22 19:48:19 -07001691{
Herbert Xu12a169e2008-10-01 07:03:24 -07001692 INIT_LIST_HEAD(&walk->all);
Herbert Xu5c182452008-09-22 19:48:19 -07001693 walk->proto = proto;
Herbert Xu12a169e2008-10-01 07:03:24 -07001694 walk->state = XFRM_STATE_DEAD;
1695 walk->seq = 0;
Nicolas Dichteld3623092014-02-14 15:30:36 +01001696 walk->filter = filter;
Herbert Xu5c182452008-09-22 19:48:19 -07001697}
1698EXPORT_SYMBOL(xfrm_state_walk_init);
1699
Fan Du283bc9f2013-11-07 17:47:50 +08001700void xfrm_state_walk_done(struct xfrm_state_walk *walk, struct net *net)
Herbert Xuabb81c42008-09-09 19:58:29 -07001701{
Nicolas Dichteld3623092014-02-14 15:30:36 +01001702 kfree(walk->filter);
1703
Herbert Xu12a169e2008-10-01 07:03:24 -07001704 if (list_empty(&walk->all))
Herbert Xu5c182452008-09-22 19:48:19 -07001705 return;
Herbert Xu5c182452008-09-22 19:48:19 -07001706
Fan Du283bc9f2013-11-07 17:47:50 +08001707 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001708 list_del(&walk->all);
Fan Du283bc9f2013-11-07 17:47:50 +08001709 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Herbert Xuabb81c42008-09-09 19:58:29 -07001710}
1711EXPORT_SYMBOL(xfrm_state_walk_done);
1712
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001713static void xfrm_replay_timer_handler(unsigned long data)
1714{
Weilong Chen3e94c2d2013-12-24 09:43:47 +08001715 struct xfrm_state *x = (struct xfrm_state *)data;
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001716
1717 spin_lock(&x->lock);
1718
Jamal Hadi Salim27170962006-04-14 15:03:05 -07001719 if (x->km.state == XFRM_STATE_VALID) {
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001720 if (xfrm_aevent_is_on(xs_net(x)))
Steffen Klassert9fdc4882011-03-08 00:08:32 +00001721 x->repl->notify(x, XFRM_REPLAY_TIMEOUT);
Jamal Hadi Salim27170962006-04-14 15:03:05 -07001722 else
1723 x->xflags |= XFRM_TIME_DEFER;
1724 }
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001725
1726 spin_unlock(&x->lock);
1727}
1728
Denis Chengdf018122007-12-07 00:51:11 -08001729static LIST_HEAD(xfrm_km_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
David S. Miller214e0052011-02-24 00:02:38 -05001731void km_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732{
1733 struct xfrm_mgr *km;
1734
Cong Wang85168c02013-01-16 16:05:06 +08001735 rcu_read_lock();
1736 list_for_each_entry_rcu(km, &xfrm_km_list, list)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001737 if (km->notify_policy)
1738 km->notify_policy(xp, dir, c);
Cong Wang85168c02013-01-16 16:05:06 +08001739 rcu_read_unlock();
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001740}
1741
David S. Miller214e0052011-02-24 00:02:38 -05001742void km_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001743{
1744 struct xfrm_mgr *km;
Cong Wang85168c02013-01-16 16:05:06 +08001745 rcu_read_lock();
1746 list_for_each_entry_rcu(km, &xfrm_km_list, list)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001747 if (km->notify)
1748 km->notify(x, c);
Cong Wang85168c02013-01-16 16:05:06 +08001749 rcu_read_unlock();
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001750}
1751
1752EXPORT_SYMBOL(km_policy_notify);
1753EXPORT_SYMBOL(km_state_notify);
1754
Eric W. Biederman15e47302012-09-07 20:12:54 +00001755void km_state_expired(struct xfrm_state *x, int hard, u32 portid)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001756{
1757 struct km_event c;
1758
Herbert Xubf08867f92005-06-18 22:44:00 -07001759 c.data.hard = hard;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001760 c.portid = portid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001761 c.event = XFRM_MSG_EXPIRE;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001762 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763}
1764
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -08001765EXPORT_SYMBOL(km_state_expired);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001766/*
1767 * We send to all registered managers regardless of failure
1768 * We are happy with one success
1769*/
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001770int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001772 int err = -EINVAL, acqret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 struct xfrm_mgr *km;
1774
Cong Wang85168c02013-01-16 16:05:06 +08001775 rcu_read_lock();
1776 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Fan Du65e07362012-08-15 10:13:47 +08001777 acqret = km->acquire(x, t, pol);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001778 if (!acqret)
1779 err = acqret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 }
Cong Wang85168c02013-01-16 16:05:06 +08001781 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 return err;
1783}
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001784EXPORT_SYMBOL(km_query);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785
Al Viro5d36b182006-11-08 00:24:06 -08001786int km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787{
1788 int err = -EINVAL;
1789 struct xfrm_mgr *km;
1790
Cong Wang85168c02013-01-16 16:05:06 +08001791 rcu_read_lock();
1792 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 if (km->new_mapping)
1794 err = km->new_mapping(x, ipaddr, sport);
1795 if (!err)
1796 break;
1797 }
Cong Wang85168c02013-01-16 16:05:06 +08001798 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 return err;
1800}
1801EXPORT_SYMBOL(km_new_mapping);
1802
Eric W. Biederman15e47302012-09-07 20:12:54 +00001803void km_policy_expired(struct xfrm_policy *pol, int dir, int hard, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001805 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806
Herbert Xubf08867f92005-06-18 22:44:00 -07001807 c.data.hard = hard;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001808 c.portid = portid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001809 c.event = XFRM_MSG_POLEXPIRE;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001810 km_policy_notify(pol, dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811}
David S. Millera70fcb02006-03-20 19:18:52 -08001812EXPORT_SYMBOL(km_policy_expired);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813
Eric Dumazet2d60abc2008-01-03 20:43:21 -08001814#ifdef CONFIG_XFRM_MIGRATE
David S. Miller183cad12011-02-24 00:28:01 -05001815int km_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
1816 const struct xfrm_migrate *m, int num_migrate,
1817 const struct xfrm_kmaddress *k)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001818{
1819 int err = -EINVAL;
1820 int ret;
1821 struct xfrm_mgr *km;
1822
Cong Wang85168c02013-01-16 16:05:06 +08001823 rcu_read_lock();
1824 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001825 if (km->migrate) {
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001826 ret = km->migrate(sel, dir, type, m, num_migrate, k);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001827 if (!ret)
1828 err = ret;
1829 }
1830 }
Cong Wang85168c02013-01-16 16:05:06 +08001831 rcu_read_unlock();
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001832 return err;
1833}
1834EXPORT_SYMBOL(km_migrate);
Eric Dumazet2d60abc2008-01-03 20:43:21 -08001835#endif
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001836
Alexey Dobriyandb983c12008-11-25 17:51:01 -08001837int km_report(struct net *net, u8 proto, struct xfrm_selector *sel, xfrm_address_t *addr)
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001838{
1839 int err = -EINVAL;
1840 int ret;
1841 struct xfrm_mgr *km;
1842
Cong Wang85168c02013-01-16 16:05:06 +08001843 rcu_read_lock();
1844 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001845 if (km->report) {
Alexey Dobriyandb983c12008-11-25 17:51:01 -08001846 ret = km->report(net, proto, sel, addr);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001847 if (!ret)
1848 err = ret;
1849 }
1850 }
Cong Wang85168c02013-01-16 16:05:06 +08001851 rcu_read_unlock();
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001852 return err;
1853}
1854EXPORT_SYMBOL(km_report);
1855
Horia Geanta0f245582014-02-12 16:20:06 +02001856bool km_is_alive(const struct km_event *c)
1857{
1858 struct xfrm_mgr *km;
1859 bool is_alive = false;
1860
1861 rcu_read_lock();
1862 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
1863 if (km->is_alive && km->is_alive(c)) {
1864 is_alive = true;
1865 break;
1866 }
1867 }
1868 rcu_read_unlock();
1869
1870 return is_alive;
1871}
1872EXPORT_SYMBOL(km_is_alive);
1873
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874int xfrm_user_policy(struct sock *sk, int optname, u8 __user *optval, int optlen)
1875{
1876 int err;
1877 u8 *data;
1878 struct xfrm_mgr *km;
1879 struct xfrm_policy *pol = NULL;
1880
1881 if (optlen <= 0 || optlen > PAGE_SIZE)
1882 return -EMSGSIZE;
1883
1884 data = kmalloc(optlen, GFP_KERNEL);
1885 if (!data)
1886 return -ENOMEM;
1887
1888 err = -EFAULT;
1889 if (copy_from_user(data, optval, optlen))
1890 goto out;
1891
1892 err = -EINVAL;
Cong Wang85168c02013-01-16 16:05:06 +08001893 rcu_read_lock();
1894 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07001895 pol = km->compile_policy(sk, optname, data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 optlen, &err);
1897 if (err >= 0)
1898 break;
1899 }
Cong Wang85168c02013-01-16 16:05:06 +08001900 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901
1902 if (err >= 0) {
1903 xfrm_sk_policy_insert(sk, err, pol);
1904 xfrm_pol_put(pol);
1905 err = 0;
1906 }
1907
1908out:
1909 kfree(data);
1910 return err;
1911}
1912EXPORT_SYMBOL(xfrm_user_policy);
1913
Cong Wang85168c02013-01-16 16:05:06 +08001914static DEFINE_SPINLOCK(xfrm_km_lock);
1915
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916int xfrm_register_km(struct xfrm_mgr *km)
1917{
Cong Wang85168c02013-01-16 16:05:06 +08001918 spin_lock_bh(&xfrm_km_lock);
1919 list_add_tail_rcu(&km->list, &xfrm_km_list);
1920 spin_unlock_bh(&xfrm_km_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 return 0;
1922}
1923EXPORT_SYMBOL(xfrm_register_km);
1924
1925int xfrm_unregister_km(struct xfrm_mgr *km)
1926{
Cong Wang85168c02013-01-16 16:05:06 +08001927 spin_lock_bh(&xfrm_km_lock);
1928 list_del_rcu(&km->list);
1929 spin_unlock_bh(&xfrm_km_lock);
1930 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 return 0;
1932}
1933EXPORT_SYMBOL(xfrm_unregister_km);
1934
1935int xfrm_state_register_afinfo(struct xfrm_state_afinfo *afinfo)
1936{
1937 int err = 0;
1938 if (unlikely(afinfo == NULL))
1939 return -EINVAL;
1940 if (unlikely(afinfo->family >= NPROTO))
1941 return -EAFNOSUPPORT;
Cong Wang44abdc32013-01-16 16:05:05 +08001942 spin_lock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 if (unlikely(xfrm_state_afinfo[afinfo->family] != NULL))
Li RongQingf31e8d4f2015-04-23 11:06:53 +08001944 err = -EEXIST;
David S. Milleredcd5822006-08-24 00:42:45 -07001945 else
Cong Wang44abdc32013-01-16 16:05:05 +08001946 rcu_assign_pointer(xfrm_state_afinfo[afinfo->family], afinfo);
1947 spin_unlock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 return err;
1949}
1950EXPORT_SYMBOL(xfrm_state_register_afinfo);
1951
1952int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo)
1953{
1954 int err = 0;
1955 if (unlikely(afinfo == NULL))
1956 return -EINVAL;
1957 if (unlikely(afinfo->family >= NPROTO))
1958 return -EAFNOSUPPORT;
Cong Wang44abdc32013-01-16 16:05:05 +08001959 spin_lock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 if (likely(xfrm_state_afinfo[afinfo->family] != NULL)) {
1961 if (unlikely(xfrm_state_afinfo[afinfo->family] != afinfo))
1962 err = -EINVAL;
David S. Milleredcd5822006-08-24 00:42:45 -07001963 else
Cong Wang44abdc32013-01-16 16:05:05 +08001964 RCU_INIT_POINTER(xfrm_state_afinfo[afinfo->family], NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 }
Cong Wang44abdc32013-01-16 16:05:05 +08001966 spin_unlock_bh(&xfrm_state_afinfo_lock);
1967 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 return err;
1969}
1970EXPORT_SYMBOL(xfrm_state_unregister_afinfo);
1971
Hannes Frederic Sowa628e3412013-08-14 13:05:23 +02001972struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973{
1974 struct xfrm_state_afinfo *afinfo;
1975 if (unlikely(family >= NPROTO))
1976 return NULL;
Cong Wang44abdc32013-01-16 16:05:05 +08001977 rcu_read_lock();
1978 afinfo = rcu_dereference(xfrm_state_afinfo[family]);
Herbert Xu546be242006-05-27 23:03:58 -07001979 if (unlikely(!afinfo))
Cong Wang44abdc32013-01-16 16:05:05 +08001980 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 return afinfo;
1982}
1983
Hannes Frederic Sowa628e3412013-08-14 13:05:23 +02001984void xfrm_state_put_afinfo(struct xfrm_state_afinfo *afinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985{
Cong Wang44abdc32013-01-16 16:05:05 +08001986 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987}
1988
1989/* Temporarily located here until net/xfrm/xfrm_tunnel.c is created */
1990void xfrm_state_delete_tunnel(struct xfrm_state *x)
1991{
1992 if (x->tunnel) {
1993 struct xfrm_state *t = x->tunnel;
1994
1995 if (atomic_read(&t->tunnel_users) == 2)
1996 xfrm_state_delete(t);
1997 atomic_dec(&t->tunnel_users);
1998 xfrm_state_put(t);
1999 x->tunnel = NULL;
2000 }
2001}
2002EXPORT_SYMBOL(xfrm_state_delete_tunnel);
2003
2004int xfrm_state_mtu(struct xfrm_state *x, int mtu)
2005{
Patrick McHardyc5c25232007-04-09 11:47:18 -07002006 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007
Patrick McHardyc5c25232007-04-09 11:47:18 -07002008 spin_lock_bh(&x->lock);
2009 if (x->km.state == XFRM_STATE_VALID &&
2010 x->type && x->type->get_mtu)
2011 res = x->type->get_mtu(x, mtu);
2012 else
Patrick McHardy28121612007-06-18 22:30:15 -07002013 res = mtu - x->props.header_len;
Patrick McHardyc5c25232007-04-09 11:47:18 -07002014 spin_unlock_bh(&x->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 return res;
2016}
2017
Wei Yongjuna454f0c2011-03-21 18:08:28 -07002018int __xfrm_init_state(struct xfrm_state *x, bool init_replay)
Herbert Xu72cb6962005-06-20 13:18:08 -07002019{
Herbert Xud094cd82005-06-20 13:19:41 -07002020 struct xfrm_state_afinfo *afinfo;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002021 struct xfrm_mode *inner_mode;
Herbert Xud094cd82005-06-20 13:19:41 -07002022 int family = x->props.family;
Herbert Xu72cb6962005-06-20 13:18:08 -07002023 int err;
2024
Herbert Xud094cd82005-06-20 13:19:41 -07002025 err = -EAFNOSUPPORT;
2026 afinfo = xfrm_state_get_afinfo(family);
2027 if (!afinfo)
2028 goto error;
2029
2030 err = 0;
2031 if (afinfo->init_flags)
2032 err = afinfo->init_flags(x);
2033
2034 xfrm_state_put_afinfo(afinfo);
2035
2036 if (err)
2037 goto error;
2038
2039 err = -EPROTONOSUPPORT;
Herbert Xu13996372007-10-17 21:35:51 -07002040
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002041 if (x->sel.family != AF_UNSPEC) {
2042 inner_mode = xfrm_get_mode(x->props.mode, x->sel.family);
2043 if (inner_mode == NULL)
2044 goto error;
2045
2046 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL) &&
2047 family != x->sel.family) {
2048 xfrm_put_mode(inner_mode);
2049 goto error;
2050 }
2051
2052 x->inner_mode = inner_mode;
2053 } else {
2054 struct xfrm_mode *inner_mode_iaf;
Martin Willid81d2282008-12-03 15:38:07 -08002055 int iafamily = AF_INET;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002056
Martin Willid81d2282008-12-03 15:38:07 -08002057 inner_mode = xfrm_get_mode(x->props.mode, x->props.family);
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002058 if (inner_mode == NULL)
2059 goto error;
2060
2061 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL)) {
2062 xfrm_put_mode(inner_mode);
2063 goto error;
2064 }
Martin Willid81d2282008-12-03 15:38:07 -08002065 x->inner_mode = inner_mode;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002066
Martin Willid81d2282008-12-03 15:38:07 -08002067 if (x->props.family == AF_INET)
2068 iafamily = AF_INET6;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002069
Martin Willid81d2282008-12-03 15:38:07 -08002070 inner_mode_iaf = xfrm_get_mode(x->props.mode, iafamily);
2071 if (inner_mode_iaf) {
2072 if (inner_mode_iaf->flags & XFRM_MODE_FLAG_TUNNEL)
2073 x->inner_mode_iaf = inner_mode_iaf;
2074 else
2075 xfrm_put_mode(inner_mode_iaf);
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002076 }
2077 }
Herbert Xu13996372007-10-17 21:35:51 -07002078
Herbert Xud094cd82005-06-20 13:19:41 -07002079 x->type = xfrm_get_type(x->id.proto, family);
Herbert Xu72cb6962005-06-20 13:18:08 -07002080 if (x->type == NULL)
2081 goto error;
2082
2083 err = x->type->init_state(x);
2084 if (err)
2085 goto error;
2086
Herbert Xu13996372007-10-17 21:35:51 -07002087 x->outer_mode = xfrm_get_mode(x->props.mode, family);
Julia Lawall599901c2012-08-29 06:49:15 +00002088 if (x->outer_mode == NULL) {
2089 err = -EPROTONOSUPPORT;
Herbert Xub59f45d2006-05-27 23:05:54 -07002090 goto error;
Julia Lawall599901c2012-08-29 06:49:15 +00002091 }
Herbert Xub59f45d2006-05-27 23:05:54 -07002092
Wei Yongjuna454f0c2011-03-21 18:08:28 -07002093 if (init_replay) {
2094 err = xfrm_init_replay(x);
2095 if (err)
2096 goto error;
2097 }
2098
Herbert Xu72cb6962005-06-20 13:18:08 -07002099 x->km.state = XFRM_STATE_VALID;
2100
2101error:
2102 return err;
2103}
2104
Wei Yongjuna454f0c2011-03-21 18:08:28 -07002105EXPORT_SYMBOL(__xfrm_init_state);
2106
2107int xfrm_init_state(struct xfrm_state *x)
2108{
2109 return __xfrm_init_state(x, true);
2110}
2111
Herbert Xu72cb6962005-06-20 13:18:08 -07002112EXPORT_SYMBOL(xfrm_init_state);
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09002113
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002114int __net_init xfrm_state_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115{
David S. Millerf034b5d2006-08-24 03:08:07 -07002116 unsigned int sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117
Alexey Dobriyan9d4139c2008-11-25 17:16:11 -08002118 INIT_LIST_HEAD(&net->xfrm.state_all);
2119
David S. Millerf034b5d2006-08-24 03:08:07 -07002120 sz = sizeof(struct hlist_head) * 8;
2121
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002122 net->xfrm.state_bydst = xfrm_hash_alloc(sz);
2123 if (!net->xfrm.state_bydst)
2124 goto out_bydst;
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002125 net->xfrm.state_bysrc = xfrm_hash_alloc(sz);
2126 if (!net->xfrm.state_bysrc)
2127 goto out_bysrc;
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002128 net->xfrm.state_byspi = xfrm_hash_alloc(sz);
2129 if (!net->xfrm.state_byspi)
2130 goto out_byspi;
Alexey Dobriyan529983e2008-11-25 17:18:12 -08002131 net->xfrm.state_hmask = ((sz / sizeof(struct hlist_head)) - 1);
David S. Millerf034b5d2006-08-24 03:08:07 -07002132
Alexey Dobriyan0bf7c5b2008-11-25 17:18:39 -08002133 net->xfrm.state_num = 0;
Alexey Dobriyan63082732008-11-25 17:19:07 -08002134 INIT_WORK(&net->xfrm.state_hash_work, xfrm_hash_resize);
Alexey Dobriyanb8a0ae22008-11-25 17:20:11 -08002135 INIT_HLIST_HEAD(&net->xfrm.state_gc_list);
Alexey Dobriyanc7837142008-11-25 17:20:36 -08002136 INIT_WORK(&net->xfrm.state_gc_work, xfrm_state_gc_task);
Fan Du283bc9f2013-11-07 17:47:50 +08002137 spin_lock_init(&net->xfrm.xfrm_state_lock);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002138 return 0;
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002139
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002140out_byspi:
2141 xfrm_hash_free(net->xfrm.state_bysrc, sz);
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002142out_bysrc:
2143 xfrm_hash_free(net->xfrm.state_bydst, sz);
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002144out_bydst:
2145 return -ENOMEM;
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002146}
2147
2148void xfrm_state_fini(struct net *net)
2149{
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002150 unsigned int sz;
2151
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002152 flush_work(&net->xfrm.state_hash_work);
Tetsuo Handa2e710292014-04-22 21:48:30 +09002153 xfrm_state_flush(net, IPSEC_PROTO_ANY, false);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002154 flush_work(&net->xfrm.state_gc_work);
2155
Alexey Dobriyan9d4139c2008-11-25 17:16:11 -08002156 WARN_ON(!list_empty(&net->xfrm.state_all));
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002157
Alexey Dobriyan529983e2008-11-25 17:18:12 -08002158 sz = (net->xfrm.state_hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002159 WARN_ON(!hlist_empty(net->xfrm.state_byspi));
2160 xfrm_hash_free(net->xfrm.state_byspi, sz);
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002161 WARN_ON(!hlist_empty(net->xfrm.state_bysrc));
2162 xfrm_hash_free(net->xfrm.state_bysrc, sz);
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002163 WARN_ON(!hlist_empty(net->xfrm.state_bydst));
2164 xfrm_hash_free(net->xfrm.state_bydst, sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165}
2166
Joy Lattenab5f5e82007-09-17 11:51:22 -07002167#ifdef CONFIG_AUDITSYSCALL
Ilpo Järvinencf35f432008-01-05 23:13:20 -08002168static void xfrm_audit_helper_sainfo(struct xfrm_state *x,
2169 struct audit_buffer *audit_buf)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002170{
Paul Moore68277ac2007-12-20 20:49:33 -08002171 struct xfrm_sec_ctx *ctx = x->security;
2172 u32 spi = ntohl(x->id.spi);
2173
2174 if (ctx)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002175 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
Paul Moore68277ac2007-12-20 20:49:33 -08002176 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002177
Weilong Chen9b7a7872013-12-24 09:43:46 +08002178 switch (x->props.family) {
Joy Lattenab5f5e82007-09-17 11:51:22 -07002179 case AF_INET:
Harvey Harrison21454aa2008-10-31 00:54:56 -07002180 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2181 &x->props.saddr.a4, &x->id.daddr.a4);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002182 break;
2183 case AF_INET6:
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002184 audit_log_format(audit_buf, " src=%pI6 dst=%pI6",
Harvey Harrisonfdb46ee2008-10-28 16:10:17 -07002185 x->props.saddr.a6, x->id.daddr.a6);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002186 break;
2187 }
Paul Moore68277ac2007-12-20 20:49:33 -08002188
2189 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002190}
2191
Ilpo Järvinencf35f432008-01-05 23:13:20 -08002192static void xfrm_audit_helper_pktinfo(struct sk_buff *skb, u16 family,
2193 struct audit_buffer *audit_buf)
Paul Mooreafeb14b2007-12-21 14:58:11 -08002194{
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002195 const struct iphdr *iph4;
2196 const struct ipv6hdr *iph6;
Paul Mooreafeb14b2007-12-21 14:58:11 -08002197
2198 switch (family) {
2199 case AF_INET:
2200 iph4 = ip_hdr(skb);
Harvey Harrison21454aa2008-10-31 00:54:56 -07002201 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2202 &iph4->saddr, &iph4->daddr);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002203 break;
2204 case AF_INET6:
2205 iph6 = ipv6_hdr(skb);
2206 audit_log_format(audit_buf,
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002207 " src=%pI6 dst=%pI6 flowlbl=0x%x%02x%02x",
Weilong Chen9b7a7872013-12-24 09:43:46 +08002208 &iph6->saddr, &iph6->daddr,
Paul Mooreafeb14b2007-12-21 14:58:11 -08002209 iph6->flow_lbl[0] & 0x0f,
2210 iph6->flow_lbl[1],
2211 iph6->flow_lbl[2]);
2212 break;
2213 }
2214}
2215
Tetsuo Handa2e710292014-04-22 21:48:30 +09002216void xfrm_audit_state_add(struct xfrm_state *x, int result, bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002217{
2218 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002219
Paul Mooreafeb14b2007-12-21 14:58:11 -08002220 audit_buf = xfrm_audit_start("SAD-add");
Joy Lattenab5f5e82007-09-17 11:51:22 -07002221 if (audit_buf == NULL)
2222 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09002223 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002224 xfrm_audit_helper_sainfo(x, audit_buf);
2225 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002226 audit_log_end(audit_buf);
2227}
2228EXPORT_SYMBOL_GPL(xfrm_audit_state_add);
2229
Tetsuo Handa2e710292014-04-22 21:48:30 +09002230void xfrm_audit_state_delete(struct xfrm_state *x, int result, bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002231{
2232 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002233
Paul Mooreafeb14b2007-12-21 14:58:11 -08002234 audit_buf = xfrm_audit_start("SAD-delete");
Joy Lattenab5f5e82007-09-17 11:51:22 -07002235 if (audit_buf == NULL)
2236 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09002237 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002238 xfrm_audit_helper_sainfo(x, audit_buf);
2239 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002240 audit_log_end(audit_buf);
2241}
2242EXPORT_SYMBOL_GPL(xfrm_audit_state_delete);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002243
2244void xfrm_audit_state_replay_overflow(struct xfrm_state *x,
2245 struct sk_buff *skb)
2246{
2247 struct audit_buffer *audit_buf;
2248 u32 spi;
2249
2250 audit_buf = xfrm_audit_start("SA-replay-overflow");
2251 if (audit_buf == NULL)
2252 return;
2253 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2254 /* don't record the sequence number because it's inherent in this kind
2255 * of audit message */
2256 spi = ntohl(x->id.spi);
2257 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
2258 audit_log_end(audit_buf);
2259}
2260EXPORT_SYMBOL_GPL(xfrm_audit_state_replay_overflow);
2261
Steffen Klassert9fdc4882011-03-08 00:08:32 +00002262void xfrm_audit_state_replay(struct xfrm_state *x,
Paul Mooreafeb14b2007-12-21 14:58:11 -08002263 struct sk_buff *skb, __be32 net_seq)
2264{
2265 struct audit_buffer *audit_buf;
2266 u32 spi;
2267
2268 audit_buf = xfrm_audit_start("SA-replayed-pkt");
2269 if (audit_buf == NULL)
2270 return;
2271 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2272 spi = ntohl(x->id.spi);
2273 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2274 spi, spi, ntohl(net_seq));
2275 audit_log_end(audit_buf);
2276}
Steffen Klassert9fdc4882011-03-08 00:08:32 +00002277EXPORT_SYMBOL_GPL(xfrm_audit_state_replay);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002278
2279void xfrm_audit_state_notfound_simple(struct sk_buff *skb, u16 family)
2280{
2281 struct audit_buffer *audit_buf;
2282
2283 audit_buf = xfrm_audit_start("SA-notfound");
2284 if (audit_buf == NULL)
2285 return;
2286 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2287 audit_log_end(audit_buf);
2288}
2289EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound_simple);
2290
2291void xfrm_audit_state_notfound(struct sk_buff *skb, u16 family,
2292 __be32 net_spi, __be32 net_seq)
2293{
2294 struct audit_buffer *audit_buf;
2295 u32 spi;
2296
2297 audit_buf = xfrm_audit_start("SA-notfound");
2298 if (audit_buf == NULL)
2299 return;
2300 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2301 spi = ntohl(net_spi);
2302 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2303 spi, spi, ntohl(net_seq));
2304 audit_log_end(audit_buf);
2305}
2306EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound);
2307
2308void xfrm_audit_state_icvfail(struct xfrm_state *x,
2309 struct sk_buff *skb, u8 proto)
2310{
2311 struct audit_buffer *audit_buf;
2312 __be32 net_spi;
2313 __be32 net_seq;
2314
2315 audit_buf = xfrm_audit_start("SA-icv-failure");
2316 if (audit_buf == NULL)
2317 return;
2318 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2319 if (xfrm_parse_spi(skb, proto, &net_spi, &net_seq) == 0) {
2320 u32 spi = ntohl(net_spi);
2321 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2322 spi, spi, ntohl(net_seq));
2323 }
2324 audit_log_end(audit_buf);
2325}
2326EXPORT_SYMBOL_GPL(xfrm_audit_state_icvfail);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002327#endif /* CONFIG_AUDITSYSCALL */