blob: 220ebc05c7afc6b602785f6e402d44098cc0bd8d [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070031/* Each xfrm_state may be linked to two tables:
32
33 1. Hash table by (spi,daddr,ah/esp) to find SA by SPI. (input,ctl)
David S. Millera624c102006-08-24 03:24:33 -070034 2. Hash table by (daddr,family,reqid) to find what SAs exist for given
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 destination/tunnel endpoint. (output)
36 */
37
38static DEFINE_SPINLOCK(xfrm_state_lock);
39
David S. Millerf034b5d2006-08-24 03:08:07 -070040static unsigned int xfrm_state_hashmax __read_mostly = 1 * 1024 * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Herbert Xu17c2a422007-10-17 21:33:12 -070042static struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family);
43static void xfrm_state_put_afinfo(struct xfrm_state_afinfo *afinfo);
44
Paul Mooreafeb14b2007-12-21 14:58:11 -080045#ifdef CONFIG_AUDITSYSCALL
46static void xfrm_audit_state_replay(struct xfrm_state *x,
47 struct sk_buff *skb, __be32 net_seq);
48#else
49#define xfrm_audit_state_replay(x, s, sq) do { ; } while (0)
50#endif /* CONFIG_AUDITSYSCALL */
51
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -080052static inline unsigned int xfrm_dst_hash(struct net *net,
53 xfrm_address_t *daddr,
David S. Millerc1969f22006-08-24 04:00:03 -070054 xfrm_address_t *saddr,
55 u32 reqid,
David S. Millera624c102006-08-24 03:24:33 -070056 unsigned short family)
57{
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -080058 return __xfrm_dst_hash(daddr, saddr, reqid, family, net->xfrm.state_hmask);
David S. Millera624c102006-08-24 03:24:33 -070059}
60
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -080061static inline unsigned int xfrm_src_hash(struct net *net,
62 xfrm_address_t *daddr,
Masahide NAKAMURA667bbcb2006-10-03 15:56:09 -070063 xfrm_address_t *saddr,
David S. Miller44e36b42006-08-24 04:50:50 -070064 unsigned short family)
David S. Millerf034b5d2006-08-24 03:08:07 -070065{
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -080066 return __xfrm_src_hash(daddr, saddr, family, net->xfrm.state_hmask);
David S. Millerf034b5d2006-08-24 03:08:07 -070067}
68
David S. Miller2575b652006-08-24 03:26:44 -070069static inline unsigned int
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -080070xfrm_spi_hash(struct net *net, xfrm_address_t *daddr, __be32 spi, u8 proto, unsigned short family)
David S. Millerf034b5d2006-08-24 03:08:07 -070071{
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -080072 return __xfrm_spi_hash(daddr, spi, proto, family, net->xfrm.state_hmask);
David S. Millerf034b5d2006-08-24 03:08:07 -070073}
74
David S. Millerf034b5d2006-08-24 03:08:07 -070075static void xfrm_hash_transfer(struct hlist_head *list,
76 struct hlist_head *ndsttable,
77 struct hlist_head *nsrctable,
78 struct hlist_head *nspitable,
79 unsigned int nhashmask)
80{
81 struct hlist_node *entry, *tmp;
82 struct xfrm_state *x;
83
84 hlist_for_each_entry_safe(x, entry, tmp, list, bydst) {
85 unsigned int h;
86
David S. Millerc1969f22006-08-24 04:00:03 -070087 h = __xfrm_dst_hash(&x->id.daddr, &x->props.saddr,
88 x->props.reqid, x->props.family,
89 nhashmask);
David S. Millerf034b5d2006-08-24 03:08:07 -070090 hlist_add_head(&x->bydst, ndsttable+h);
91
Masahide NAKAMURA667bbcb2006-10-03 15:56:09 -070092 h = __xfrm_src_hash(&x->id.daddr, &x->props.saddr,
93 x->props.family,
David S. Millerf034b5d2006-08-24 03:08:07 -070094 nhashmask);
95 hlist_add_head(&x->bysrc, nsrctable+h);
96
Masahide NAKAMURA7b4dc3602006-09-27 22:21:52 -070097 if (x->id.spi) {
98 h = __xfrm_spi_hash(&x->id.daddr, x->id.spi,
99 x->id.proto, x->props.family,
100 nhashmask);
101 hlist_add_head(&x->byspi, nspitable+h);
102 }
David S. Millerf034b5d2006-08-24 03:08:07 -0700103 }
104}
105
Alexey Dobriyan63082732008-11-25 17:19:07 -0800106static unsigned long xfrm_hash_new_size(unsigned int state_hmask)
David S. Millerf034b5d2006-08-24 03:08:07 -0700107{
Alexey Dobriyan63082732008-11-25 17:19:07 -0800108 return ((state_hmask + 1) << 1) * sizeof(struct hlist_head);
David S. Millerf034b5d2006-08-24 03:08:07 -0700109}
110
111static DEFINE_MUTEX(hash_resize_mutex);
112
Alexey Dobriyan63082732008-11-25 17:19:07 -0800113static void xfrm_hash_resize(struct work_struct *work)
David S. Millerf034b5d2006-08-24 03:08:07 -0700114{
Alexey Dobriyan63082732008-11-25 17:19:07 -0800115 struct net *net = container_of(work, struct net, xfrm.state_hash_work);
David S. Millerf034b5d2006-08-24 03:08:07 -0700116 struct hlist_head *ndst, *nsrc, *nspi, *odst, *osrc, *ospi;
117 unsigned long nsize, osize;
118 unsigned int nhashmask, ohashmask;
119 int i;
120
121 mutex_lock(&hash_resize_mutex);
122
Alexey Dobriyan63082732008-11-25 17:19:07 -0800123 nsize = xfrm_hash_new_size(net->xfrm.state_hmask);
David S. Miller44e36b42006-08-24 04:50:50 -0700124 ndst = xfrm_hash_alloc(nsize);
David S. Millerf034b5d2006-08-24 03:08:07 -0700125 if (!ndst)
126 goto out_unlock;
David S. Miller44e36b42006-08-24 04:50:50 -0700127 nsrc = xfrm_hash_alloc(nsize);
David S. Millerf034b5d2006-08-24 03:08:07 -0700128 if (!nsrc) {
David S. Miller44e36b42006-08-24 04:50:50 -0700129 xfrm_hash_free(ndst, nsize);
David S. Millerf034b5d2006-08-24 03:08:07 -0700130 goto out_unlock;
131 }
David S. Miller44e36b42006-08-24 04:50:50 -0700132 nspi = xfrm_hash_alloc(nsize);
David S. Millerf034b5d2006-08-24 03:08:07 -0700133 if (!nspi) {
David S. Miller44e36b42006-08-24 04:50:50 -0700134 xfrm_hash_free(ndst, nsize);
135 xfrm_hash_free(nsrc, nsize);
David S. Millerf034b5d2006-08-24 03:08:07 -0700136 goto out_unlock;
137 }
138
139 spin_lock_bh(&xfrm_state_lock);
140
141 nhashmask = (nsize / sizeof(struct hlist_head)) - 1U;
Alexey Dobriyan63082732008-11-25 17:19:07 -0800142 for (i = net->xfrm.state_hmask; i >= 0; i--)
143 xfrm_hash_transfer(net->xfrm.state_bydst+i, ndst, nsrc, nspi,
David S. Millerf034b5d2006-08-24 03:08:07 -0700144 nhashmask);
145
Alexey Dobriyan63082732008-11-25 17:19:07 -0800146 odst = net->xfrm.state_bydst;
147 osrc = net->xfrm.state_bysrc;
148 ospi = net->xfrm.state_byspi;
149 ohashmask = net->xfrm.state_hmask;
David S. Millerf034b5d2006-08-24 03:08:07 -0700150
Alexey Dobriyan63082732008-11-25 17:19:07 -0800151 net->xfrm.state_bydst = ndst;
152 net->xfrm.state_bysrc = nsrc;
153 net->xfrm.state_byspi = nspi;
154 net->xfrm.state_hmask = nhashmask;
David S. Millerf034b5d2006-08-24 03:08:07 -0700155
156 spin_unlock_bh(&xfrm_state_lock);
157
158 osize = (ohashmask + 1) * sizeof(struct hlist_head);
David S. Miller44e36b42006-08-24 04:50:50 -0700159 xfrm_hash_free(odst, osize);
160 xfrm_hash_free(osrc, osize);
161 xfrm_hash_free(ospi, osize);
David S. Millerf034b5d2006-08-24 03:08:07 -0700162
163out_unlock:
164 mutex_unlock(&hash_resize_mutex);
165}
166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167static DEFINE_RWLOCK(xfrm_state_afinfo_lock);
168static struct xfrm_state_afinfo *xfrm_state_afinfo[NPROTO];
169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170static DEFINE_SPINLOCK(xfrm_state_gc_lock);
171
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -0800172int __xfrm_state_delete(struct xfrm_state *x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -0800174int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -0800175void km_state_expired(struct xfrm_state *x, int hard, u32 pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700177static struct xfrm_state_afinfo *xfrm_state_lock_afinfo(unsigned int family)
178{
179 struct xfrm_state_afinfo *afinfo;
180 if (unlikely(family >= NPROTO))
181 return NULL;
182 write_lock_bh(&xfrm_state_afinfo_lock);
183 afinfo = xfrm_state_afinfo[family];
184 if (unlikely(!afinfo))
185 write_unlock_bh(&xfrm_state_afinfo_lock);
186 return afinfo;
187}
188
189static void xfrm_state_unlock_afinfo(struct xfrm_state_afinfo *afinfo)
Eric Dumazet9a429c42008-01-01 21:58:02 -0800190 __releases(xfrm_state_afinfo_lock)
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700191{
192 write_unlock_bh(&xfrm_state_afinfo_lock);
193}
194
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800195int xfrm_register_type(const struct xfrm_type *type, unsigned short family)
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700196{
197 struct xfrm_state_afinfo *afinfo = xfrm_state_lock_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;
204
205 if (likely(typemap[type->proto] == NULL))
206 typemap[type->proto] = type;
207 else
208 err = -EEXIST;
209 xfrm_state_unlock_afinfo(afinfo);
210 return err;
211}
212EXPORT_SYMBOL(xfrm_register_type);
213
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800214int xfrm_unregister_type(const struct xfrm_type *type, unsigned short family)
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700215{
216 struct xfrm_state_afinfo *afinfo = xfrm_state_lock_afinfo(family);
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800217 const struct xfrm_type **typemap;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700218 int err = 0;
219
220 if (unlikely(afinfo == NULL))
221 return -EAFNOSUPPORT;
222 typemap = afinfo->type_map;
223
224 if (unlikely(typemap[type->proto] != type))
225 err = -ENOENT;
226 else
227 typemap[type->proto] = NULL;
228 xfrm_state_unlock_afinfo(afinfo);
229 return err;
230}
231EXPORT_SYMBOL(xfrm_unregister_type);
232
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800233static const struct xfrm_type *xfrm_get_type(u8 proto, unsigned short family)
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700234{
235 struct xfrm_state_afinfo *afinfo;
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800236 const struct xfrm_type **typemap;
237 const struct xfrm_type *type;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700238 int modload_attempted = 0;
239
240retry:
241 afinfo = xfrm_state_get_afinfo(family);
242 if (unlikely(afinfo == NULL))
243 return NULL;
244 typemap = afinfo->type_map;
245
246 type = typemap[proto];
247 if (unlikely(type && !try_module_get(type->owner)))
248 type = NULL;
249 if (!type && !modload_attempted) {
250 xfrm_state_put_afinfo(afinfo);
251 request_module("xfrm-type-%d-%d", family, proto);
252 modload_attempted = 1;
253 goto retry;
254 }
255
256 xfrm_state_put_afinfo(afinfo);
257 return type;
258}
259
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800260static void xfrm_put_type(const struct xfrm_type *type)
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700261{
262 module_put(type->owner);
263}
264
265int xfrm_register_mode(struct xfrm_mode *mode, int family)
266{
267 struct xfrm_state_afinfo *afinfo;
268 struct xfrm_mode **modemap;
269 int err;
270
271 if (unlikely(mode->encap >= XFRM_MODE_MAX))
272 return -EINVAL;
273
274 afinfo = xfrm_state_lock_afinfo(family);
275 if (unlikely(afinfo == NULL))
276 return -EAFNOSUPPORT;
277
278 err = -EEXIST;
279 modemap = afinfo->mode_map;
Herbert Xu17c2a422007-10-17 21:33:12 -0700280 if (modemap[mode->encap])
281 goto out;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700282
Herbert Xu17c2a422007-10-17 21:33:12 -0700283 err = -ENOENT;
284 if (!try_module_get(afinfo->owner))
285 goto out;
286
287 mode->afinfo = afinfo;
288 modemap[mode->encap] = mode;
289 err = 0;
290
291out:
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700292 xfrm_state_unlock_afinfo(afinfo);
293 return err;
294}
295EXPORT_SYMBOL(xfrm_register_mode);
296
297int xfrm_unregister_mode(struct xfrm_mode *mode, int family)
298{
299 struct xfrm_state_afinfo *afinfo;
300 struct xfrm_mode **modemap;
301 int err;
302
303 if (unlikely(mode->encap >= XFRM_MODE_MAX))
304 return -EINVAL;
305
306 afinfo = xfrm_state_lock_afinfo(family);
307 if (unlikely(afinfo == NULL))
308 return -EAFNOSUPPORT;
309
310 err = -ENOENT;
311 modemap = afinfo->mode_map;
312 if (likely(modemap[mode->encap] == mode)) {
313 modemap[mode->encap] = NULL;
Herbert Xu17c2a422007-10-17 21:33:12 -0700314 module_put(mode->afinfo->owner);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700315 err = 0;
316 }
317
318 xfrm_state_unlock_afinfo(afinfo);
319 return err;
320}
321EXPORT_SYMBOL(xfrm_unregister_mode);
322
323static struct xfrm_mode *xfrm_get_mode(unsigned int encap, int family)
324{
325 struct xfrm_state_afinfo *afinfo;
326 struct xfrm_mode *mode;
327 int modload_attempted = 0;
328
329 if (unlikely(encap >= XFRM_MODE_MAX))
330 return NULL;
331
332retry:
333 afinfo = xfrm_state_get_afinfo(family);
334 if (unlikely(afinfo == NULL))
335 return NULL;
336
337 mode = afinfo->mode_map[encap];
338 if (unlikely(mode && !try_module_get(mode->owner)))
339 mode = NULL;
340 if (!mode && !modload_attempted) {
341 xfrm_state_put_afinfo(afinfo);
342 request_module("xfrm-mode-%d-%d", family, encap);
343 modload_attempted = 1;
344 goto retry;
345 }
346
347 xfrm_state_put_afinfo(afinfo);
348 return mode;
349}
350
351static void xfrm_put_mode(struct xfrm_mode *mode)
352{
353 module_put(mode->owner);
354}
355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356static void xfrm_state_gc_destroy(struct xfrm_state *x)
357{
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800358 tasklet_hrtimer_cancel(&x->mtimer);
David S. Millera47f0ce2006-08-24 03:54:22 -0700359 del_timer_sync(&x->rtimer);
Jesper Juhla51482b2005-11-08 09:41:34 -0800360 kfree(x->aalg);
361 kfree(x->ealg);
362 kfree(x->calg);
363 kfree(x->encap);
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700364 kfree(x->coaddr);
Herbert Xu13996372007-10-17 21:35:51 -0700365 if (x->inner_mode)
366 xfrm_put_mode(x->inner_mode);
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700367 if (x->inner_mode_iaf)
368 xfrm_put_mode(x->inner_mode_iaf);
Herbert Xu13996372007-10-17 21:35:51 -0700369 if (x->outer_mode)
370 xfrm_put_mode(x->outer_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 if (x->type) {
372 x->type->destructor(x);
373 xfrm_put_type(x->type);
374 }
Trent Jaegerdf718372005-12-13 23:12:27 -0800375 security_xfrm_state_free(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 kfree(x);
377}
378
Alexey Dobriyanc7837142008-11-25 17:20:36 -0800379static void xfrm_state_gc_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
Alexey Dobriyanc7837142008-11-25 17:20:36 -0800381 struct net *net = container_of(work, struct net, xfrm.state_gc_work);
Herbert Xu12a169e2008-10-01 07:03:24 -0700382 struct xfrm_state *x;
383 struct hlist_node *entry, *tmp;
384 struct hlist_head gc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 spin_lock_bh(&xfrm_state_gc_lock);
Alexey Dobriyanc7837142008-11-25 17:20:36 -0800387 hlist_move_list(&net->xfrm.state_gc_list, &gc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 spin_unlock_bh(&xfrm_state_gc_lock);
389
Herbert Xu12a169e2008-10-01 07:03:24 -0700390 hlist_for_each_entry_safe(x, entry, tmp, &gc_list, gclist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 xfrm_state_gc_destroy(x);
David S. Miller8f126e32006-08-24 02:45:07 -0700392
Alexey Dobriyan50a30652008-11-25 17:21:01 -0800393 wake_up(&net->xfrm.km_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394}
395
396static inline unsigned long make_jiffies(long secs)
397{
398 if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
399 return MAX_SCHEDULE_TIMEOUT-1;
400 else
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900401 return secs*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402}
403
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800404static enum hrtimer_restart xfrm_timer_handler(struct hrtimer * me)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800406 struct tasklet_hrtimer *thr = container_of(me, struct tasklet_hrtimer, timer);
407 struct xfrm_state *x = container_of(thr, struct xfrm_state, mtimer);
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800408 struct net *net = xs_net(x);
James Morris9d729f72007-03-04 16:12:44 -0800409 unsigned long now = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 long next = LONG_MAX;
411 int warn = 0;
Joy Latten161a09e2006-11-27 13:11:54 -0600412 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
414 spin_lock(&x->lock);
415 if (x->km.state == XFRM_STATE_DEAD)
416 goto out;
417 if (x->km.state == XFRM_STATE_EXPIRED)
418 goto expired;
419 if (x->lft.hard_add_expires_seconds) {
420 long tmo = x->lft.hard_add_expires_seconds +
421 x->curlft.add_time - now;
422 if (tmo <= 0)
423 goto expired;
424 if (tmo < next)
425 next = tmo;
426 }
427 if (x->lft.hard_use_expires_seconds) {
428 long tmo = x->lft.hard_use_expires_seconds +
429 (x->curlft.use_time ? : now) - now;
430 if (tmo <= 0)
431 goto expired;
432 if (tmo < next)
433 next = tmo;
434 }
435 if (x->km.dying)
436 goto resched;
437 if (x->lft.soft_add_expires_seconds) {
438 long tmo = x->lft.soft_add_expires_seconds +
439 x->curlft.add_time - now;
440 if (tmo <= 0)
441 warn = 1;
442 else if (tmo < next)
443 next = tmo;
444 }
445 if (x->lft.soft_use_expires_seconds) {
446 long tmo = x->lft.soft_use_expires_seconds +
447 (x->curlft.use_time ? : now) - now;
448 if (tmo <= 0)
449 warn = 1;
450 else if (tmo < next)
451 next = tmo;
452 }
453
Herbert Xu4666faa2005-06-18 22:43:22 -0700454 x->km.dying = warn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 if (warn)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -0800456 km_state_expired(x, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457resched:
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800458 if (next != LONG_MAX){
459 tasklet_hrtimer_start(&x->mtimer, ktime_set(next, 0), HRTIMER_MODE_REL);
460 }
David S. Millera47f0ce2006-08-24 03:54:22 -0700461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 goto out;
463
464expired:
465 if (x->km.state == XFRM_STATE_ACQ && x->id.spi == 0) {
466 x->km.state = XFRM_STATE_EXPIRED;
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800467 wake_up(&net->xfrm.km_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 next = 2;
469 goto resched;
470 }
Joy Latten161a09e2006-11-27 13:11:54 -0600471
472 err = __xfrm_state_delete(x);
473 if (!err && x->id.spi)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -0800474 km_state_expired(x, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Joy Lattenab5f5e82007-09-17 11:51:22 -0700476 xfrm_audit_state_delete(x, err ? 0 : 1,
Eric Paris25323862008-04-18 10:09:25 -0400477 audit_get_loginuid(current),
478 audit_get_sessionid(current), 0);
Joy Latten161a09e2006-11-27 13:11:54 -0600479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480out:
481 spin_unlock(&x->lock);
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800482 return HRTIMER_NORESTART;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483}
484
David S. Miller0ac84752006-03-20 19:18:23 -0800485static void xfrm_replay_timer_handler(unsigned long data);
486
Alexey Dobriyan673c09b2008-11-25 17:15:16 -0800487struct xfrm_state *xfrm_state_alloc(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
489 struct xfrm_state *x;
490
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700491 x = kzalloc(sizeof(struct xfrm_state), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
493 if (x) {
Alexey Dobriyan673c09b2008-11-25 17:15:16 -0800494 write_pnet(&x->xs_net, net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 atomic_set(&x->refcnt, 1);
496 atomic_set(&x->tunnel_users, 0);
Herbert Xu12a169e2008-10-01 07:03:24 -0700497 INIT_LIST_HEAD(&x->km.all);
David S. Miller8f126e32006-08-24 02:45:07 -0700498 INIT_HLIST_NODE(&x->bydst);
499 INIT_HLIST_NODE(&x->bysrc);
500 INIT_HLIST_NODE(&x->byspi);
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800501 tasklet_hrtimer_init(&x->mtimer, xfrm_timer_handler, CLOCK_REALTIME, HRTIMER_MODE_ABS);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800502 setup_timer(&x->rtimer, xfrm_replay_timer_handler,
503 (unsigned long)x);
James Morris9d729f72007-03-04 16:12:44 -0800504 x->curlft.add_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 x->lft.soft_byte_limit = XFRM_INF;
506 x->lft.soft_packet_limit = XFRM_INF;
507 x->lft.hard_byte_limit = XFRM_INF;
508 x->lft.hard_packet_limit = XFRM_INF;
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -0800509 x->replay_maxage = 0;
510 x->replay_maxdiff = 0;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700511 x->inner_mode = NULL;
512 x->inner_mode_iaf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 spin_lock_init(&x->lock);
514 }
515 return x;
516}
517EXPORT_SYMBOL(xfrm_state_alloc);
518
519void __xfrm_state_destroy(struct xfrm_state *x)
520{
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800521 struct net *net = xs_net(x);
522
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700523 WARN_ON(x->km.state != XFRM_STATE_DEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
525 spin_lock_bh(&xfrm_state_gc_lock);
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800526 hlist_add_head(&x->gclist, &net->xfrm.state_gc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 spin_unlock_bh(&xfrm_state_gc_lock);
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800528 schedule_work(&net->xfrm.state_gc_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529}
530EXPORT_SYMBOL(__xfrm_state_destroy);
531
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -0800532int __xfrm_state_delete(struct xfrm_state *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533{
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800534 struct net *net = xs_net(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700535 int err = -ESRCH;
536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 if (x->km.state != XFRM_STATE_DEAD) {
538 x->km.state = XFRM_STATE_DEAD;
539 spin_lock(&xfrm_state_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -0700540 list_del(&x->km.all);
David S. Miller8f126e32006-08-24 02:45:07 -0700541 hlist_del(&x->bydst);
David S. Miller8f126e32006-08-24 02:45:07 -0700542 hlist_del(&x->bysrc);
David S. Millera47f0ce2006-08-24 03:54:22 -0700543 if (x->id.spi)
David S. Miller8f126e32006-08-24 02:45:07 -0700544 hlist_del(&x->byspi);
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800545 net->xfrm.state_num--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 spin_unlock(&xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 /* All xfrm_state objects are created by xfrm_state_alloc.
549 * The xfrm_state_alloc call gives a reference, and that
550 * is what we are dropping here.
551 */
Patrick McHardy5dba4792007-11-27 11:10:07 +0800552 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700553 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700555
556 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557}
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -0800558EXPORT_SYMBOL(__xfrm_state_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700560int xfrm_state_delete(struct xfrm_state *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700562 int err;
563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 spin_lock_bh(&x->lock);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700565 err = __xfrm_state_delete(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 spin_unlock_bh(&x->lock);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700567
568 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569}
570EXPORT_SYMBOL(xfrm_state_delete);
571
Joy Latten4aa2e622007-06-04 19:05:57 -0400572#ifdef CONFIG_SECURITY_NETWORK_XFRM
573static inline int
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800574xfrm_state_flush_secctx_check(struct net *net, u8 proto, struct xfrm_audit *audit_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575{
Joy Latten4aa2e622007-06-04 19:05:57 -0400576 int i, err = 0;
577
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800578 for (i = 0; i <= net->xfrm.state_hmask; i++) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400579 struct hlist_node *entry;
580 struct xfrm_state *x;
581
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800582 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+i, bydst) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400583 if (xfrm_id_proto_match(x->id.proto, proto) &&
584 (err = security_xfrm_state_delete(x)) != 0) {
Joy Lattenab5f5e82007-09-17 11:51:22 -0700585 xfrm_audit_state_delete(x, 0,
586 audit_info->loginuid,
Eric Paris25323862008-04-18 10:09:25 -0400587 audit_info->sessionid,
Joy Lattenab5f5e82007-09-17 11:51:22 -0700588 audit_info->secid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400589 return err;
590 }
591 }
592 }
593
594 return err;
595}
596#else
597static inline int
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800598xfrm_state_flush_secctx_check(struct net *net, u8 proto, struct xfrm_audit *audit_info)
Joy Latten4aa2e622007-06-04 19:05:57 -0400599{
600 return 0;
601}
602#endif
603
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800604int xfrm_state_flush(struct net *net, u8 proto, struct xfrm_audit *audit_info)
Joy Latten4aa2e622007-06-04 19:05:57 -0400605{
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000606 int i, err = 0, cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
608 spin_lock_bh(&xfrm_state_lock);
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800609 err = xfrm_state_flush_secctx_check(net, proto, audit_info);
Joy Latten4aa2e622007-06-04 19:05:57 -0400610 if (err)
611 goto out;
612
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000613 err = -ESRCH;
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800614 for (i = 0; i <= net->xfrm.state_hmask; i++) {
David S. Miller8f126e32006-08-24 02:45:07 -0700615 struct hlist_node *entry;
616 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617restart:
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800618 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+i, bydst) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 if (!xfrm_state_kern(x) &&
Masahide NAKAMURA57947082006-09-22 15:06:24 -0700620 xfrm_id_proto_match(x->id.proto, proto)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 xfrm_state_hold(x);
622 spin_unlock_bh(&xfrm_state_lock);
623
Joy Latten161a09e2006-11-27 13:11:54 -0600624 err = xfrm_state_delete(x);
Joy Lattenab5f5e82007-09-17 11:51:22 -0700625 xfrm_audit_state_delete(x, err ? 0 : 1,
626 audit_info->loginuid,
Eric Paris25323862008-04-18 10:09:25 -0400627 audit_info->sessionid,
Joy Lattenab5f5e82007-09-17 11:51:22 -0700628 audit_info->secid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 xfrm_state_put(x);
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000630 if (!err)
631 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
633 spin_lock_bh(&xfrm_state_lock);
634 goto restart;
635 }
636 }
637 }
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000638 if (cnt)
639 err = 0;
Joy Latten4aa2e622007-06-04 19:05:57 -0400640
641out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 spin_unlock_bh(&xfrm_state_lock);
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800643 wake_up(&net->xfrm.km_waitq);
Joy Latten4aa2e622007-06-04 19:05:57 -0400644 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645}
646EXPORT_SYMBOL(xfrm_state_flush);
647
Alexey Dobriyane0710412010-01-23 13:37:10 +0000648void xfrm_sad_getinfo(struct net *net, struct xfrmk_sadinfo *si)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700649{
650 spin_lock_bh(&xfrm_state_lock);
Alexey Dobriyane0710412010-01-23 13:37:10 +0000651 si->sadcnt = net->xfrm.state_num;
652 si->sadhcnt = net->xfrm.state_hmask;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700653 si->sadhmcnt = xfrm_state_hashmax;
654 spin_unlock_bh(&xfrm_state_lock);
655}
656EXPORT_SYMBOL(xfrm_sad_getinfo);
657
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658static int
Thomas Egerer8444cf72010-09-20 11:11:38 -0700659xfrm_init_tempstate(struct xfrm_state *x, struct flowi *fl,
660 struct xfrm_tmpl *tmpl,
661 xfrm_address_t *daddr, xfrm_address_t *saddr,
662 unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663{
664 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
665 if (!afinfo)
666 return -1;
Thomas Egerer8444cf72010-09-20 11:11:38 -0700667 afinfo->init_tempsel(&x->sel, fl);
668
669 if (family != tmpl->encap_family) {
670 xfrm_state_put_afinfo(afinfo);
671 afinfo = xfrm_state_get_afinfo(tmpl->encap_family);
672 if (!afinfo)
673 return -1;
674 }
675 afinfo->init_temprop(x, tmpl, daddr, saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 xfrm_state_put_afinfo(afinfo);
677 return 0;
678}
679
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800680static struct xfrm_state *__xfrm_state_lookup(struct net *net, u32 mark, xfrm_address_t *daddr, __be32 spi, u8 proto, unsigned short family)
David S. Milleredcd5822006-08-24 00:42:45 -0700681{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800682 unsigned int h = xfrm_spi_hash(net, daddr, spi, proto, family);
David S. Milleredcd5822006-08-24 00:42:45 -0700683 struct xfrm_state *x;
David S. Miller8f126e32006-08-24 02:45:07 -0700684 struct hlist_node *entry;
David S. Milleredcd5822006-08-24 00:42:45 -0700685
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800686 hlist_for_each_entry(x, entry, net->xfrm.state_byspi+h, byspi) {
David S. Milleredcd5822006-08-24 00:42:45 -0700687 if (x->props.family != family ||
688 x->id.spi != spi ||
Wei Yongjun18025712009-06-28 18:42:53 +0000689 x->id.proto != proto ||
690 xfrm_addr_cmp(&x->id.daddr, daddr, family))
David S. Milleredcd5822006-08-24 00:42:45 -0700691 continue;
692
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000693 if ((mark & x->mark.m) != x->mark.v)
694 continue;
David S. Milleredcd5822006-08-24 00:42:45 -0700695 xfrm_state_hold(x);
696 return x;
697 }
698
699 return NULL;
700}
701
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800702static struct xfrm_state *__xfrm_state_lookup_byaddr(struct net *net, u32 mark, xfrm_address_t *daddr, xfrm_address_t *saddr, u8 proto, unsigned short family)
David S. Milleredcd5822006-08-24 00:42:45 -0700703{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800704 unsigned int h = xfrm_src_hash(net, daddr, saddr, family);
David S. Milleredcd5822006-08-24 00:42:45 -0700705 struct xfrm_state *x;
David S. Miller8f126e32006-08-24 02:45:07 -0700706 struct hlist_node *entry;
David S. Milleredcd5822006-08-24 00:42:45 -0700707
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800708 hlist_for_each_entry(x, entry, net->xfrm.state_bysrc+h, bysrc) {
David S. Milleredcd5822006-08-24 00:42:45 -0700709 if (x->props.family != family ||
Wei Yongjun18025712009-06-28 18:42:53 +0000710 x->id.proto != proto ||
711 xfrm_addr_cmp(&x->id.daddr, daddr, family) ||
712 xfrm_addr_cmp(&x->props.saddr, saddr, family))
David S. Milleredcd5822006-08-24 00:42:45 -0700713 continue;
714
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000715 if ((mark & x->mark.m) != x->mark.v)
716 continue;
David S. Milleredcd5822006-08-24 00:42:45 -0700717 xfrm_state_hold(x);
718 return x;
719 }
720
721 return NULL;
722}
723
724static inline struct xfrm_state *
725__xfrm_state_locate(struct xfrm_state *x, int use_spi, int family)
726{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800727 struct net *net = xs_net(x);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800728 u32 mark = x->mark.v & x->mark.m;
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800729
David S. Milleredcd5822006-08-24 00:42:45 -0700730 if (use_spi)
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800731 return __xfrm_state_lookup(net, mark, &x->id.daddr,
732 x->id.spi, x->id.proto, family);
David S. Milleredcd5822006-08-24 00:42:45 -0700733 else
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800734 return __xfrm_state_lookup_byaddr(net, mark,
735 &x->id.daddr,
David S. Milleredcd5822006-08-24 00:42:45 -0700736 &x->props.saddr,
737 x->id.proto, family);
738}
739
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800740static void xfrm_hash_grow_check(struct net *net, int have_hash_collision)
Patrick McHardy2fab22f2006-10-24 15:34:00 -0700741{
742 if (have_hash_collision &&
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800743 (net->xfrm.state_hmask + 1) < xfrm_state_hashmax &&
744 net->xfrm.state_num > net->xfrm.state_hmask)
745 schedule_work(&net->xfrm.state_hash_work);
Patrick McHardy2fab22f2006-10-24 15:34:00 -0700746}
747
David S. Miller08ec9af2009-03-13 14:22:40 -0700748static void xfrm_state_look_at(struct xfrm_policy *pol, struct xfrm_state *x,
749 struct flowi *fl, unsigned short family,
750 xfrm_address_t *daddr, xfrm_address_t *saddr,
751 struct xfrm_state **best, int *acq_in_progress,
752 int *error)
753{
754 /* Resolution logic:
755 * 1. There is a valid state with matching selector. Done.
756 * 2. Valid state with inappropriate selector. Skip.
757 *
758 * Entering area of "sysdeps".
759 *
760 * 3. If state is not valid, selector is temporary, it selects
761 * only session which triggered previous resolution. Key
762 * manager will do something to install a state with proper
763 * selector.
764 */
765 if (x->km.state == XFRM_STATE_VALID) {
766 if ((x->sel.family &&
767 !xfrm_selector_match(&x->sel, fl, x->sel.family)) ||
768 !security_xfrm_state_pol_flow_match(x, pol, fl))
769 return;
770
771 if (!*best ||
772 (*best)->km.dying > x->km.dying ||
773 ((*best)->km.dying == x->km.dying &&
774 (*best)->curlft.add_time < x->curlft.add_time))
775 *best = x;
776 } else if (x->km.state == XFRM_STATE_ACQ) {
777 *acq_in_progress = 1;
778 } else if (x->km.state == XFRM_STATE_ERROR ||
779 x->km.state == XFRM_STATE_EXPIRED) {
780 if (xfrm_selector_match(&x->sel, fl, x->sel.family) &&
781 security_xfrm_state_pol_flow_match(x, pol, fl))
782 *error = -ESRCH;
783 }
784}
785
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786struct xfrm_state *
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900787xfrm_state_find(xfrm_address_t *daddr, xfrm_address_t *saddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 struct flowi *fl, struct xfrm_tmpl *tmpl,
789 struct xfrm_policy *pol, int *err,
790 unsigned short family)
791{
David S. Miller08ec9af2009-03-13 14:22:40 -0700792 static xfrm_address_t saddr_wildcard = { };
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800793 struct net *net = xp_net(pol);
Nicolas Dichtel6a783c92009-04-27 02:58:59 -0700794 unsigned int h, h_wildcard;
David S. Miller8f126e32006-08-24 02:45:07 -0700795 struct hlist_node *entry;
David S. Miller37b08e32008-09-02 20:14:15 -0700796 struct xfrm_state *x, *x0, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 int acquire_in_progress = 0;
798 int error = 0;
799 struct xfrm_state *best = NULL;
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800800 u32 mark = pol->mark.v & pol->mark.m;
Thomas Egerer8444cf72010-09-20 11:11:38 -0700801 unsigned short encap_family = tmpl->encap_family;
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900802
David S. Miller37b08e32008-09-02 20:14:15 -0700803 to_put = NULL;
804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 spin_lock_bh(&xfrm_state_lock);
Thomas Egerer8444cf72010-09-20 11:11:38 -0700806 h = xfrm_dst_hash(net, daddr, saddr, tmpl->reqid, encap_family);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800807 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+h, bydst) {
Thomas Egerer8444cf72010-09-20 11:11:38 -0700808 if (x->props.family == encap_family &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 x->props.reqid == tmpl->reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000810 (mark & x->mark.m) == x->mark.v &&
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -0700811 !(x->props.flags & XFRM_STATE_WILDRECV) &&
Thomas Egerer8444cf72010-09-20 11:11:38 -0700812 xfrm_state_addr_check(x, daddr, saddr, encap_family) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 tmpl->mode == x->props.mode &&
814 tmpl->id.proto == x->id.proto &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700815 (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
Thomas Egerer8444cf72010-09-20 11:11:38 -0700816 xfrm_state_look_at(pol, x, fl, encap_family, daddr, saddr,
David S. Miller08ec9af2009-03-13 14:22:40 -0700817 &best, &acquire_in_progress, &error);
818 }
819 if (best)
820 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Thomas Egerer8444cf72010-09-20 11:11:38 -0700822 h_wildcard = xfrm_dst_hash(net, daddr, &saddr_wildcard, tmpl->reqid, encap_family);
Nicolas Dichtel6a783c92009-04-27 02:58:59 -0700823 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+h_wildcard, bydst) {
Thomas Egerer8444cf72010-09-20 11:11:38 -0700824 if (x->props.family == encap_family &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700825 x->props.reqid == tmpl->reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000826 (mark & x->mark.m) == x->mark.v &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700827 !(x->props.flags & XFRM_STATE_WILDRECV) &&
Thomas Egerer8444cf72010-09-20 11:11:38 -0700828 xfrm_state_addr_check(x, daddr, saddr, encap_family) &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700829 tmpl->mode == x->props.mode &&
830 tmpl->id.proto == x->id.proto &&
831 (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
Thomas Egerer8444cf72010-09-20 11:11:38 -0700832 xfrm_state_look_at(pol, x, fl, encap_family, daddr, saddr,
David S. Miller08ec9af2009-03-13 14:22:40 -0700833 &best, &acquire_in_progress, &error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 }
835
David S. Miller08ec9af2009-03-13 14:22:40 -0700836found:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 x = best;
838 if (!x && !error && !acquire_in_progress) {
Patrick McHardy5c5d2812005-04-21 20:12:32 -0700839 if (tmpl->id.spi &&
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800840 (x0 = __xfrm_state_lookup(net, mark, daddr, tmpl->id.spi,
Thomas Egerer8444cf72010-09-20 11:11:38 -0700841 tmpl->id.proto, encap_family)) != NULL) {
David S. Miller37b08e32008-09-02 20:14:15 -0700842 to_put = x0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 error = -EEXIST;
844 goto out;
845 }
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800846 x = xfrm_state_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 if (x == NULL) {
848 error = -ENOMEM;
849 goto out;
850 }
Thomas Egerer8444cf72010-09-20 11:11:38 -0700851 /* Initialize temporary state matching only
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 * to current session. */
Thomas Egerer8444cf72010-09-20 11:11:38 -0700853 xfrm_init_tempstate(x, fl, tmpl, daddr, saddr, family);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800854 memcpy(&x->mark, &pol->mark, sizeof(x->mark));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -0700856 error = security_xfrm_state_alloc_acquire(x, pol->security, fl->secid);
857 if (error) {
858 x->km.state = XFRM_STATE_DEAD;
David S. Miller37b08e32008-09-02 20:14:15 -0700859 to_put = x;
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -0700860 x = NULL;
861 goto out;
862 }
863
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 if (km_query(x, tmpl, pol) == 0) {
865 x->km.state = XFRM_STATE_ACQ;
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800866 list_add(&x->km.all, &net->xfrm.state_all);
867 hlist_add_head(&x->bydst, net->xfrm.state_bydst+h);
Thomas Egerer8444cf72010-09-20 11:11:38 -0700868 h = xfrm_src_hash(net, daddr, saddr, encap_family);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800869 hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 if (x->id.spi) {
Thomas Egerer8444cf72010-09-20 11:11:38 -0700871 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, encap_family);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800872 hlist_add_head(&x->byspi, net->xfrm.state_byspi+h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 }
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800874 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800875 tasklet_hrtimer_start(&x->mtimer, ktime_set(net->xfrm.sysctl_acq_expires, 0), HRTIMER_MODE_REL);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800876 net->xfrm.state_num++;
877 xfrm_hash_grow_check(net, x->bydst.next != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 } else {
879 x->km.state = XFRM_STATE_DEAD;
David S. Miller37b08e32008-09-02 20:14:15 -0700880 to_put = x;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 x = NULL;
882 error = -ESRCH;
883 }
884 }
885out:
886 if (x)
887 xfrm_state_hold(x);
888 else
889 *err = acquire_in_progress ? -EAGAIN : error;
890 spin_unlock_bh(&xfrm_state_lock);
David S. Miller37b08e32008-09-02 20:14:15 -0700891 if (to_put)
892 xfrm_state_put(to_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 return x;
894}
895
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700896struct xfrm_state *
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800897xfrm_stateonly_find(struct net *net, u32 mark,
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800898 xfrm_address_t *daddr, xfrm_address_t *saddr,
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700899 unsigned short family, u8 mode, u8 proto, u32 reqid)
900{
Pavel Emelyanov4bda4f22007-12-14 11:38:04 -0800901 unsigned int h;
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700902 struct xfrm_state *rx = NULL, *x = NULL;
903 struct hlist_node *entry;
904
905 spin_lock(&xfrm_state_lock);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800906 h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
907 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+h, bydst) {
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700908 if (x->props.family == family &&
909 x->props.reqid == reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000910 (mark & x->mark.m) == x->mark.v &&
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700911 !(x->props.flags & XFRM_STATE_WILDRECV) &&
912 xfrm_state_addr_check(x, daddr, saddr, family) &&
913 mode == x->props.mode &&
914 proto == x->id.proto &&
915 x->km.state == XFRM_STATE_VALID) {
916 rx = x;
917 break;
918 }
919 }
920
921 if (rx)
922 xfrm_state_hold(rx);
923 spin_unlock(&xfrm_state_lock);
924
925
926 return rx;
927}
928EXPORT_SYMBOL(xfrm_stateonly_find);
929
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930static void __xfrm_state_insert(struct xfrm_state *x)
931{
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800932 struct net *net = xs_net(x);
David S. Millera624c102006-08-24 03:24:33 -0700933 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800935 list_add(&x->km.all, &net->xfrm.state_all);
Timo Teras4c563f72008-02-28 21:31:08 -0800936
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800937 h = xfrm_dst_hash(net, &x->id.daddr, &x->props.saddr,
David S. Millerc1969f22006-08-24 04:00:03 -0700938 x->props.reqid, x->props.family);
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800939 hlist_add_head(&x->bydst, net->xfrm.state_bydst+h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800941 h = xfrm_src_hash(net, &x->id.daddr, &x->props.saddr, x->props.family);
942 hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
Masahide NAKAMURA7b4dc3602006-09-27 22:21:52 -0700944 if (x->id.spi) {
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800945 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto,
Masahide NAKAMURA6c44e6b2006-08-23 17:53:57 -0700946 x->props.family);
947
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800948 hlist_add_head(&x->byspi, net->xfrm.state_byspi+h);
Masahide NAKAMURA6c44e6b2006-08-23 17:53:57 -0700949 }
950
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800951 tasklet_hrtimer_start(&x->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL);
David S. Millera47f0ce2006-08-24 03:54:22 -0700952 if (x->replay_maxage)
953 mod_timer(&x->rtimer, jiffies + x->replay_maxage);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -0800954
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800955 wake_up(&net->xfrm.km_waitq);
David S. Millerf034b5d2006-08-24 03:08:07 -0700956
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800957 net->xfrm.state_num++;
David S. Millerf034b5d2006-08-24 03:08:07 -0700958
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800959 xfrm_hash_grow_check(net, x->bydst.next != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960}
961
David S. Millerc7f5ea32006-08-24 03:29:04 -0700962/* xfrm_state_lock is held */
963static void __xfrm_state_bump_genids(struct xfrm_state *xnew)
964{
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800965 struct net *net = xs_net(xnew);
David S. Millerc7f5ea32006-08-24 03:29:04 -0700966 unsigned short family = xnew->props.family;
967 u32 reqid = xnew->props.reqid;
968 struct xfrm_state *x;
969 struct hlist_node *entry;
970 unsigned int h;
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000971 u32 mark = xnew->mark.v & xnew->mark.m;
David S. Millerc7f5ea32006-08-24 03:29:04 -0700972
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800973 h = xfrm_dst_hash(net, &xnew->id.daddr, &xnew->props.saddr, reqid, family);
974 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+h, bydst) {
David S. Millerc7f5ea32006-08-24 03:29:04 -0700975 if (x->props.family == family &&
976 x->props.reqid == reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000977 (mark & x->mark.m) == x->mark.v &&
David S. Millerc1969f22006-08-24 04:00:03 -0700978 !xfrm_addr_cmp(&x->id.daddr, &xnew->id.daddr, family) &&
979 !xfrm_addr_cmp(&x->props.saddr, &xnew->props.saddr, family))
Herbert Xu34996cb2010-03-31 01:19:49 +0000980 x->genid++;
David S. Millerc7f5ea32006-08-24 03:29:04 -0700981 }
982}
983
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984void xfrm_state_insert(struct xfrm_state *x)
985{
986 spin_lock_bh(&xfrm_state_lock);
David S. Millerc7f5ea32006-08-24 03:29:04 -0700987 __xfrm_state_bump_genids(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 __xfrm_state_insert(x);
989 spin_unlock_bh(&xfrm_state_lock);
990}
991EXPORT_SYMBOL(xfrm_state_insert);
992
David S. Miller27708342006-08-24 00:13:10 -0700993/* xfrm_state_lock is held */
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800994static struct xfrm_state *__find_acq_core(struct net *net, struct xfrm_mark *m, unsigned short family, u8 mode, u32 reqid, u8 proto, xfrm_address_t *daddr, xfrm_address_t *saddr, int create)
David S. Miller27708342006-08-24 00:13:10 -0700995{
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800996 unsigned int h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
David S. Miller8f126e32006-08-24 02:45:07 -0700997 struct hlist_node *entry;
David S. Miller27708342006-08-24 00:13:10 -0700998 struct xfrm_state *x;
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000999 u32 mark = m->v & m->m;
David S. Miller27708342006-08-24 00:13:10 -07001000
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001001 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+h, bydst) {
David S. Miller27708342006-08-24 00:13:10 -07001002 if (x->props.reqid != reqid ||
1003 x->props.mode != mode ||
1004 x->props.family != family ||
1005 x->km.state != XFRM_STATE_ACQ ||
Joy Latten75e252d2007-03-12 17:14:07 -07001006 x->id.spi != 0 ||
Wei Yongjun18025712009-06-28 18:42:53 +00001007 x->id.proto != proto ||
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001008 (mark & x->mark.m) != x->mark.v ||
Wei Yongjun18025712009-06-28 18:42:53 +00001009 xfrm_addr_cmp(&x->id.daddr, daddr, family) ||
1010 xfrm_addr_cmp(&x->props.saddr, saddr, family))
David S. Miller27708342006-08-24 00:13:10 -07001011 continue;
1012
David S. Miller27708342006-08-24 00:13:10 -07001013 xfrm_state_hold(x);
1014 return x;
1015 }
1016
1017 if (!create)
1018 return NULL;
1019
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001020 x = xfrm_state_alloc(net);
David S. Miller27708342006-08-24 00:13:10 -07001021 if (likely(x)) {
1022 switch (family) {
1023 case AF_INET:
1024 x->sel.daddr.a4 = daddr->a4;
1025 x->sel.saddr.a4 = saddr->a4;
1026 x->sel.prefixlen_d = 32;
1027 x->sel.prefixlen_s = 32;
1028 x->props.saddr.a4 = saddr->a4;
1029 x->id.daddr.a4 = daddr->a4;
1030 break;
1031
1032 case AF_INET6:
1033 ipv6_addr_copy((struct in6_addr *)x->sel.daddr.a6,
1034 (struct in6_addr *)daddr);
1035 ipv6_addr_copy((struct in6_addr *)x->sel.saddr.a6,
1036 (struct in6_addr *)saddr);
1037 x->sel.prefixlen_d = 128;
1038 x->sel.prefixlen_s = 128;
1039 ipv6_addr_copy((struct in6_addr *)x->props.saddr.a6,
1040 (struct in6_addr *)saddr);
1041 ipv6_addr_copy((struct in6_addr *)x->id.daddr.a6,
1042 (struct in6_addr *)daddr);
1043 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001044 }
David S. Miller27708342006-08-24 00:13:10 -07001045
1046 x->km.state = XFRM_STATE_ACQ;
1047 x->id.proto = proto;
1048 x->props.family = family;
1049 x->props.mode = mode;
1050 x->props.reqid = reqid;
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001051 x->mark.v = m->v;
1052 x->mark.m = m->m;
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08001053 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
David S. Miller27708342006-08-24 00:13:10 -07001054 xfrm_state_hold(x);
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -08001055 tasklet_hrtimer_start(&x->mtimer, ktime_set(net->xfrm.sysctl_acq_expires, 0), HRTIMER_MODE_REL);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001056 list_add(&x->km.all, &net->xfrm.state_all);
1057 hlist_add_head(&x->bydst, net->xfrm.state_bydst+h);
1058 h = xfrm_src_hash(net, daddr, saddr, family);
1059 hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h);
David S. Miller918049f2006-10-12 22:03:24 -07001060
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001061 net->xfrm.state_num++;
David S. Miller918049f2006-10-12 22:03:24 -07001062
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001063 xfrm_hash_grow_check(net, x->bydst.next != NULL);
David S. Miller27708342006-08-24 00:13:10 -07001064 }
1065
1066 return x;
1067}
1068
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001069static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
1071int xfrm_state_add(struct xfrm_state *x)
1072{
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001073 struct net *net = xs_net(x);
David S. Miller37b08e32008-09-02 20:14:15 -07001074 struct xfrm_state *x1, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 int family;
1076 int err;
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001077 u32 mark = x->mark.v & x->mark.m;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001078 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
1080 family = x->props.family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
David S. Miller37b08e32008-09-02 20:14:15 -07001082 to_put = NULL;
1083
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 spin_lock_bh(&xfrm_state_lock);
1085
David S. Milleredcd5822006-08-24 00:42:45 -07001086 x1 = __xfrm_state_locate(x, use_spi, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 if (x1) {
David S. Miller37b08e32008-09-02 20:14:15 -07001088 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 x1 = NULL;
1090 err = -EEXIST;
1091 goto out;
1092 }
1093
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001094 if (use_spi && x->km.seq) {
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001095 x1 = __xfrm_find_acq_byseq(net, mark, x->km.seq);
Joy Latten75e252d2007-03-12 17:14:07 -07001096 if (x1 && ((x1->id.proto != x->id.proto) ||
1097 xfrm_addr_cmp(&x1->id.daddr, &x->id.daddr, family))) {
David S. Miller37b08e32008-09-02 20:14:15 -07001098 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 x1 = NULL;
1100 }
1101 }
1102
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001103 if (use_spi && !x1)
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001104 x1 = __find_acq_core(net, &x->mark, family, x->props.mode,
1105 x->props.reqid, x->id.proto,
David S. Miller27708342006-08-24 00:13:10 -07001106 &x->id.daddr, &x->props.saddr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
David S. Millerc7f5ea32006-08-24 03:29:04 -07001108 __xfrm_state_bump_genids(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 __xfrm_state_insert(x);
1110 err = 0;
1111
1112out:
1113 spin_unlock_bh(&xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
1115 if (x1) {
1116 xfrm_state_delete(x1);
1117 xfrm_state_put(x1);
1118 }
1119
David S. Miller37b08e32008-09-02 20:14:15 -07001120 if (to_put)
1121 xfrm_state_put(to_put);
1122
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 return err;
1124}
1125EXPORT_SYMBOL(xfrm_state_add);
1126
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001127#ifdef CONFIG_XFRM_MIGRATE
Eric Dumazet66663512008-01-08 01:35:52 -08001128static struct xfrm_state *xfrm_state_clone(struct xfrm_state *orig, int *errp)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001129{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001130 struct net *net = xs_net(orig);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001131 int err = -ENOMEM;
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001132 struct xfrm_state *x = xfrm_state_alloc(net);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001133 if (!x)
Herbert Xu553f9112010-02-15 20:00:51 +00001134 goto out;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001135
1136 memcpy(&x->id, &orig->id, sizeof(x->id));
1137 memcpy(&x->sel, &orig->sel, sizeof(x->sel));
1138 memcpy(&x->lft, &orig->lft, sizeof(x->lft));
1139 x->props.mode = orig->props.mode;
1140 x->props.replay_window = orig->props.replay_window;
1141 x->props.reqid = orig->props.reqid;
1142 x->props.family = orig->props.family;
1143 x->props.saddr = orig->props.saddr;
1144
1145 if (orig->aalg) {
Martin Willi4447bb32009-11-25 00:29:52 +00001146 x->aalg = xfrm_algo_auth_clone(orig->aalg);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001147 if (!x->aalg)
1148 goto error;
1149 }
1150 x->props.aalgo = orig->props.aalgo;
1151
1152 if (orig->ealg) {
1153 x->ealg = xfrm_algo_clone(orig->ealg);
1154 if (!x->ealg)
1155 goto error;
1156 }
1157 x->props.ealgo = orig->props.ealgo;
1158
1159 if (orig->calg) {
1160 x->calg = xfrm_algo_clone(orig->calg);
1161 if (!x->calg)
1162 goto error;
1163 }
1164 x->props.calgo = orig->props.calgo;
1165
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001166 if (orig->encap) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001167 x->encap = kmemdup(orig->encap, sizeof(*x->encap), GFP_KERNEL);
1168 if (!x->encap)
1169 goto error;
1170 }
1171
1172 if (orig->coaddr) {
1173 x->coaddr = kmemdup(orig->coaddr, sizeof(*x->coaddr),
1174 GFP_KERNEL);
1175 if (!x->coaddr)
1176 goto error;
1177 }
1178
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001179 memcpy(&x->mark, &orig->mark, sizeof(x->mark));
1180
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001181 err = xfrm_init_state(x);
1182 if (err)
1183 goto error;
1184
1185 x->props.flags = orig->props.flags;
1186
1187 x->curlft.add_time = orig->curlft.add_time;
1188 x->km.state = orig->km.state;
1189 x->km.seq = orig->km.seq;
1190
1191 return x;
1192
1193 error:
Herbert Xu553f9112010-02-15 20:00:51 +00001194 xfrm_state_put(x);
1195out:
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001196 if (errp)
1197 *errp = err;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001198 return NULL;
1199}
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001200
1201/* xfrm_state_lock is held */
1202struct xfrm_state * xfrm_migrate_state_find(struct xfrm_migrate *m)
1203{
1204 unsigned int h;
1205 struct xfrm_state *x;
1206 struct hlist_node *entry;
1207
1208 if (m->reqid) {
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -08001209 h = xfrm_dst_hash(&init_net, &m->old_daddr, &m->old_saddr,
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001210 m->reqid, m->old_family);
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08001211 hlist_for_each_entry(x, entry, init_net.xfrm.state_bydst+h, bydst) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001212 if (x->props.mode != m->mode ||
1213 x->id.proto != m->proto)
1214 continue;
1215 if (m->reqid && x->props.reqid != m->reqid)
1216 continue;
1217 if (xfrm_addr_cmp(&x->id.daddr, &m->old_daddr,
1218 m->old_family) ||
1219 xfrm_addr_cmp(&x->props.saddr, &m->old_saddr,
1220 m->old_family))
1221 continue;
1222 xfrm_state_hold(x);
1223 return x;
1224 }
1225 } else {
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -08001226 h = xfrm_src_hash(&init_net, &m->old_daddr, &m->old_saddr,
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001227 m->old_family);
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08001228 hlist_for_each_entry(x, entry, init_net.xfrm.state_bysrc+h, bysrc) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001229 if (x->props.mode != m->mode ||
1230 x->id.proto != m->proto)
1231 continue;
1232 if (xfrm_addr_cmp(&x->id.daddr, &m->old_daddr,
1233 m->old_family) ||
1234 xfrm_addr_cmp(&x->props.saddr, &m->old_saddr,
1235 m->old_family))
1236 continue;
1237 xfrm_state_hold(x);
1238 return x;
1239 }
1240 }
1241
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001242 return NULL;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001243}
1244EXPORT_SYMBOL(xfrm_migrate_state_find);
1245
1246struct xfrm_state * xfrm_state_migrate(struct xfrm_state *x,
1247 struct xfrm_migrate *m)
1248{
1249 struct xfrm_state *xc;
1250 int err;
1251
1252 xc = xfrm_state_clone(x, &err);
1253 if (!xc)
1254 return NULL;
1255
1256 memcpy(&xc->id.daddr, &m->new_daddr, sizeof(xc->id.daddr));
1257 memcpy(&xc->props.saddr, &m->new_saddr, sizeof(xc->props.saddr));
1258
1259 /* add state */
1260 if (!xfrm_addr_cmp(&x->id.daddr, &m->new_daddr, m->new_family)) {
1261 /* a care is needed when the destination address of the
1262 state is to be updated as it is a part of triplet */
1263 xfrm_state_insert(xc);
1264 } else {
1265 if ((err = xfrm_state_add(xc)) < 0)
1266 goto error;
1267 }
1268
1269 return xc;
1270error:
Thomas Egerer78347c82010-12-06 23:28:56 +00001271 xfrm_state_put(xc);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001272 return NULL;
1273}
1274EXPORT_SYMBOL(xfrm_state_migrate);
1275#endif
1276
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277int xfrm_state_update(struct xfrm_state *x)
1278{
David S. Miller37b08e32008-09-02 20:14:15 -07001279 struct xfrm_state *x1, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 int err;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001281 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
David S. Miller37b08e32008-09-02 20:14:15 -07001283 to_put = NULL;
1284
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 spin_lock_bh(&xfrm_state_lock);
David S. Milleredcd5822006-08-24 00:42:45 -07001286 x1 = __xfrm_state_locate(x, use_spi, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
1288 err = -ESRCH;
1289 if (!x1)
1290 goto out;
1291
1292 if (xfrm_state_kern(x1)) {
David S. Miller37b08e32008-09-02 20:14:15 -07001293 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 err = -EEXIST;
1295 goto out;
1296 }
1297
1298 if (x1->km.state == XFRM_STATE_ACQ) {
1299 __xfrm_state_insert(x);
1300 x = NULL;
1301 }
1302 err = 0;
1303
1304out:
1305 spin_unlock_bh(&xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
David S. Miller37b08e32008-09-02 20:14:15 -07001307 if (to_put)
1308 xfrm_state_put(to_put);
1309
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 if (err)
1311 return err;
1312
1313 if (!x) {
1314 xfrm_state_delete(x1);
1315 xfrm_state_put(x1);
1316 return 0;
1317 }
1318
1319 err = -EINVAL;
1320 spin_lock_bh(&x1->lock);
1321 if (likely(x1->km.state == XFRM_STATE_VALID)) {
1322 if (x->encap && x1->encap)
1323 memcpy(x1->encap, x->encap, sizeof(*x1->encap));
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -07001324 if (x->coaddr && x1->coaddr) {
1325 memcpy(x1->coaddr, x->coaddr, sizeof(*x1->coaddr));
1326 }
1327 if (!use_spi && memcmp(&x1->sel, &x->sel, sizeof(x1->sel)))
1328 memcpy(&x1->sel, &x->sel, sizeof(x1->sel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 memcpy(&x1->lft, &x->lft, sizeof(x1->lft));
1330 x1->km.dying = 0;
1331
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -08001332 tasklet_hrtimer_start(&x1->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 if (x1->curlft.use_time)
1334 xfrm_state_check_expire(x1);
1335
1336 err = 0;
1337 }
1338 spin_unlock_bh(&x1->lock);
1339
1340 xfrm_state_put(x1);
1341
1342 return err;
1343}
1344EXPORT_SYMBOL(xfrm_state_update);
1345
1346int xfrm_state_check_expire(struct xfrm_state *x)
1347{
1348 if (!x->curlft.use_time)
James Morris9d729f72007-03-04 16:12:44 -08001349 x->curlft.use_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
1351 if (x->km.state != XFRM_STATE_VALID)
1352 return -EINVAL;
1353
1354 if (x->curlft.bytes >= x->lft.hard_byte_limit ||
1355 x->curlft.packets >= x->lft.hard_packet_limit) {
Herbert Xu4666faa2005-06-18 22:43:22 -07001356 x->km.state = XFRM_STATE_EXPIRED;
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -08001357 tasklet_hrtimer_start(&x->mtimer, ktime_set(0,0), HRTIMER_MODE_REL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 return -EINVAL;
1359 }
1360
1361 if (!x->km.dying &&
1362 (x->curlft.bytes >= x->lft.soft_byte_limit ||
Herbert Xu4666faa2005-06-18 22:43:22 -07001363 x->curlft.packets >= x->lft.soft_packet_limit)) {
1364 x->km.dying = 1;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001365 km_state_expired(x, 0, 0);
Herbert Xu4666faa2005-06-18 22:43:22 -07001366 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 return 0;
1368}
1369EXPORT_SYMBOL(xfrm_state_check_expire);
1370
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371struct xfrm_state *
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001372xfrm_state_lookup(struct net *net, u32 mark, xfrm_address_t *daddr, __be32 spi,
1373 u8 proto, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374{
1375 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
1377 spin_lock_bh(&xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001378 x = __xfrm_state_lookup(net, mark, daddr, spi, proto, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 spin_unlock_bh(&xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 return x;
1381}
1382EXPORT_SYMBOL(xfrm_state_lookup);
1383
1384struct xfrm_state *
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001385xfrm_state_lookup_byaddr(struct net *net, u32 mark,
Alexey Dobriyan221df1e2008-11-25 17:30:50 -08001386 xfrm_address_t *daddr, xfrm_address_t *saddr,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001387 u8 proto, unsigned short family)
1388{
1389 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001390
1391 spin_lock_bh(&xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001392 x = __xfrm_state_lookup_byaddr(net, mark, daddr, saddr, proto, family);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001393 spin_unlock_bh(&xfrm_state_lock);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001394 return x;
1395}
1396EXPORT_SYMBOL(xfrm_state_lookup_byaddr);
1397
1398struct xfrm_state *
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001399xfrm_find_acq(struct net *net, struct xfrm_mark *mark, u8 mode, u32 reqid, u8 proto,
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001400 xfrm_address_t *daddr, xfrm_address_t *saddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 int create, unsigned short family)
1402{
1403 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
1405 spin_lock_bh(&xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001406 x = __find_acq_core(net, mark, family, mode, reqid, proto, daddr, saddr, create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 spin_unlock_bh(&xfrm_state_lock);
David S. Miller27708342006-08-24 00:13:10 -07001408
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 return x;
1410}
1411EXPORT_SYMBOL(xfrm_find_acq);
1412
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001413#ifdef CONFIG_XFRM_SUB_POLICY
1414int
1415xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n,
1416 unsigned short family)
1417{
1418 int err = 0;
1419 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
1420 if (!afinfo)
1421 return -EAFNOSUPPORT;
1422
1423 spin_lock_bh(&xfrm_state_lock);
1424 if (afinfo->tmpl_sort)
1425 err = afinfo->tmpl_sort(dst, src, n);
1426 spin_unlock_bh(&xfrm_state_lock);
1427 xfrm_state_put_afinfo(afinfo);
1428 return err;
1429}
1430EXPORT_SYMBOL(xfrm_tmpl_sort);
1431
1432int
1433xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src, int n,
1434 unsigned short family)
1435{
1436 int err = 0;
1437 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
1438 if (!afinfo)
1439 return -EAFNOSUPPORT;
1440
1441 spin_lock_bh(&xfrm_state_lock);
1442 if (afinfo->state_sort)
1443 err = afinfo->state_sort(dst, src, n);
1444 spin_unlock_bh(&xfrm_state_lock);
1445 xfrm_state_put_afinfo(afinfo);
1446 return err;
1447}
1448EXPORT_SYMBOL(xfrm_state_sort);
1449#endif
1450
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451/* Silly enough, but I'm lazy to build resolution list */
1452
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001453static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454{
1455 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001457 for (i = 0; i <= net->xfrm.state_hmask; i++) {
David S. Miller8f126e32006-08-24 02:45:07 -07001458 struct hlist_node *entry;
1459 struct xfrm_state *x;
1460
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001461 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+i, bydst) {
David S. Miller8f126e32006-08-24 02:45:07 -07001462 if (x->km.seq == seq &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001463 (mark & x->mark.m) == x->mark.v &&
David S. Miller8f126e32006-08-24 02:45:07 -07001464 x->km.state == XFRM_STATE_ACQ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 xfrm_state_hold(x);
1466 return x;
1467 }
1468 }
1469 }
1470 return NULL;
1471}
1472
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001473struct xfrm_state *xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474{
1475 struct xfrm_state *x;
1476
1477 spin_lock_bh(&xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001478 x = __xfrm_find_acq_byseq(net, mark, seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 spin_unlock_bh(&xfrm_state_lock);
1480 return x;
1481}
1482EXPORT_SYMBOL(xfrm_find_acq_byseq);
1483
1484u32 xfrm_get_acqseq(void)
1485{
1486 u32 res;
jamal6836b9b2010-02-16 02:01:22 +00001487 static atomic_t acqseq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
jamal6836b9b2010-02-16 02:01:22 +00001489 do {
1490 res = atomic_inc_return(&acqseq);
1491 } while (!res);
1492
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 return res;
1494}
1495EXPORT_SYMBOL(xfrm_get_acqseq);
1496
Herbert Xu658b2192007-10-09 13:29:52 -07001497int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -08001499 struct net *net = xs_net(x);
David S. Millerf034b5d2006-08-24 03:08:07 -07001500 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 struct xfrm_state *x0;
Herbert Xu658b2192007-10-09 13:29:52 -07001502 int err = -ENOENT;
1503 __be32 minspi = htonl(low);
1504 __be32 maxspi = htonl(high);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001505 u32 mark = x->mark.v & x->mark.m;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
Herbert Xu658b2192007-10-09 13:29:52 -07001507 spin_lock_bh(&x->lock);
1508 if (x->km.state == XFRM_STATE_DEAD)
1509 goto unlock;
1510
1511 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 if (x->id.spi)
Herbert Xu658b2192007-10-09 13:29:52 -07001513 goto unlock;
1514
1515 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516
1517 if (minspi == maxspi) {
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001518 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, minspi, x->id.proto, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 if (x0) {
1520 xfrm_state_put(x0);
Herbert Xu658b2192007-10-09 13:29:52 -07001521 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 }
1523 x->id.spi = minspi;
1524 } else {
1525 u32 spi = 0;
Al Viro26977b42006-09-27 18:47:05 -07001526 for (h=0; h<high-low+1; h++) {
1527 spi = low + net_random()%(high-low+1);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001528 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, htonl(spi), x->id.proto, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 if (x0 == NULL) {
1530 x->id.spi = htonl(spi);
1531 break;
1532 }
1533 xfrm_state_put(x0);
1534 }
1535 }
1536 if (x->id.spi) {
1537 spin_lock_bh(&xfrm_state_lock);
Alexey Dobriyan12604d82008-11-25 17:31:18 -08001538 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, x->props.family);
1539 hlist_add_head(&x->byspi, net->xfrm.state_byspi+h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 spin_unlock_bh(&xfrm_state_lock);
Herbert Xu658b2192007-10-09 13:29:52 -07001541
1542 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 }
Herbert Xu658b2192007-10-09 13:29:52 -07001544
1545unlock:
1546 spin_unlock_bh(&x->lock);
1547
1548 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549}
1550EXPORT_SYMBOL(xfrm_alloc_spi);
1551
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08001552int xfrm_state_walk(struct net *net, struct xfrm_state_walk *walk,
Timo Teras4c563f72008-02-28 21:31:08 -08001553 int (*func)(struct xfrm_state *, int, void*),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 void *data)
1555{
Herbert Xu12a169e2008-10-01 07:03:24 -07001556 struct xfrm_state *state;
1557 struct xfrm_state_walk *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 int err = 0;
1559
Herbert Xu12a169e2008-10-01 07:03:24 -07001560 if (walk->seq != 0 && list_empty(&walk->all))
Timo Teras4c563f72008-02-28 21:31:08 -08001561 return 0;
1562
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 spin_lock_bh(&xfrm_state_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001564 if (list_empty(&walk->all))
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08001565 x = list_first_entry(&net->xfrm.state_all, struct xfrm_state_walk, all);
Herbert Xu12a169e2008-10-01 07:03:24 -07001566 else
1567 x = list_entry(&walk->all, struct xfrm_state_walk, all);
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08001568 list_for_each_entry_from(x, &net->xfrm.state_all, all) {
Herbert Xu12a169e2008-10-01 07:03:24 -07001569 if (x->state == XFRM_STATE_DEAD)
Timo Teras4c563f72008-02-28 21:31:08 -08001570 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07001571 state = container_of(x, struct xfrm_state, km);
1572 if (!xfrm_id_proto_match(state->id.proto, walk->proto))
Timo Teras4c563f72008-02-28 21:31:08 -08001573 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07001574 err = func(state, walk->seq, data);
1575 if (err) {
1576 list_move_tail(&walk->all, &x->all);
1577 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001579 walk->seq++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001581 if (walk->seq == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 err = -ENOENT;
1583 goto out;
1584 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001585 list_del_init(&walk->all);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586out:
1587 spin_unlock_bh(&xfrm_state_lock);
1588 return err;
1589}
1590EXPORT_SYMBOL(xfrm_state_walk);
1591
Herbert Xu5c182452008-09-22 19:48:19 -07001592void xfrm_state_walk_init(struct xfrm_state_walk *walk, u8 proto)
1593{
Herbert Xu12a169e2008-10-01 07:03:24 -07001594 INIT_LIST_HEAD(&walk->all);
Herbert Xu5c182452008-09-22 19:48:19 -07001595 walk->proto = proto;
Herbert Xu12a169e2008-10-01 07:03:24 -07001596 walk->state = XFRM_STATE_DEAD;
1597 walk->seq = 0;
Herbert Xu5c182452008-09-22 19:48:19 -07001598}
1599EXPORT_SYMBOL(xfrm_state_walk_init);
1600
Herbert Xuabb81c42008-09-09 19:58:29 -07001601void xfrm_state_walk_done(struct xfrm_state_walk *walk)
1602{
Herbert Xu12a169e2008-10-01 07:03:24 -07001603 if (list_empty(&walk->all))
Herbert Xu5c182452008-09-22 19:48:19 -07001604 return;
Herbert Xu5c182452008-09-22 19:48:19 -07001605
Herbert Xu12a169e2008-10-01 07:03:24 -07001606 spin_lock_bh(&xfrm_state_lock);
1607 list_del(&walk->all);
Chuck Ebbert7d0b5912009-03-27 00:22:01 -07001608 spin_unlock_bh(&xfrm_state_lock);
Herbert Xuabb81c42008-09-09 19:58:29 -07001609}
1610EXPORT_SYMBOL(xfrm_state_walk_done);
1611
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001612
1613void xfrm_replay_notify(struct xfrm_state *x, int event)
1614{
1615 struct km_event c;
1616 /* we send notify messages in case
1617 * 1. we updated on of the sequence numbers, and the seqno difference
1618 * is at least x->replay_maxdiff, in this case we also update the
1619 * timeout of our timer function
1620 * 2. if x->replay_maxage has elapsed since last update,
1621 * and there were changes
1622 *
1623 * The state structure must be locked!
1624 */
1625
1626 switch (event) {
1627 case XFRM_REPLAY_UPDATE:
1628 if (x->replay_maxdiff &&
1629 (x->replay.seq - x->preplay.seq < x->replay_maxdiff) &&
Jamal Hadi Salim27170962006-04-14 15:03:05 -07001630 (x->replay.oseq - x->preplay.oseq < x->replay_maxdiff)) {
1631 if (x->xflags & XFRM_TIME_DEFER)
1632 event = XFRM_REPLAY_TIMEOUT;
1633 else
1634 return;
1635 }
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001636
1637 break;
1638
1639 case XFRM_REPLAY_TIMEOUT:
1640 if ((x->replay.seq == x->preplay.seq) &&
1641 (x->replay.bitmap == x->preplay.bitmap) &&
Jamal Hadi Salim27170962006-04-14 15:03:05 -07001642 (x->replay.oseq == x->preplay.oseq)) {
1643 x->xflags |= XFRM_TIME_DEFER;
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001644 return;
Jamal Hadi Salim27170962006-04-14 15:03:05 -07001645 }
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001646
1647 break;
1648 }
1649
1650 memcpy(&x->preplay, &x->replay, sizeof(struct xfrm_replay_state));
1651 c.event = XFRM_MSG_NEWAE;
1652 c.data.aevent = event;
1653 km_state_notify(x, &c);
1654
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001655 if (x->replay_maxage &&
David S. Millera47f0ce2006-08-24 03:54:22 -07001656 !mod_timer(&x->rtimer, jiffies + x->replay_maxage))
Jamal Hadi Salim27170962006-04-14 15:03:05 -07001657 x->xflags &= ~XFRM_TIME_DEFER;
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001658}
1659
1660static void xfrm_replay_timer_handler(unsigned long data)
1661{
1662 struct xfrm_state *x = (struct xfrm_state*)data;
1663
1664 spin_lock(&x->lock);
1665
Jamal Hadi Salim27170962006-04-14 15:03:05 -07001666 if (x->km.state == XFRM_STATE_VALID) {
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001667 if (xfrm_aevent_is_on(xs_net(x)))
Jamal Hadi Salim27170962006-04-14 15:03:05 -07001668 xfrm_replay_notify(x, XFRM_REPLAY_TIMEOUT);
1669 else
1670 x->xflags |= XFRM_TIME_DEFER;
1671 }
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001672
1673 spin_unlock(&x->lock);
1674}
1675
Paul Mooreafeb14b2007-12-21 14:58:11 -08001676int xfrm_replay_check(struct xfrm_state *x,
1677 struct sk_buff *skb, __be32 net_seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678{
1679 u32 diff;
Al Viroa252cc22006-09-27 18:48:18 -07001680 u32 seq = ntohl(net_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
1682 if (unlikely(seq == 0))
Paul Mooreafeb14b2007-12-21 14:58:11 -08001683 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684
1685 if (likely(seq > x->replay.seq))
1686 return 0;
1687
1688 diff = x->replay.seq - seq;
Herbert Xu4c4d51a72007-04-05 00:07:39 -07001689 if (diff >= min_t(unsigned int, x->props.replay_window,
1690 sizeof(x->replay.bitmap) * 8)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 x->stats.replay_window++;
Paul Mooreafeb14b2007-12-21 14:58:11 -08001692 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 }
1694
1695 if (x->replay.bitmap & (1U << diff)) {
1696 x->stats.replay++;
Paul Mooreafeb14b2007-12-21 14:58:11 -08001697 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 }
1699 return 0;
Paul Mooreafeb14b2007-12-21 14:58:11 -08001700
1701err:
1702 xfrm_audit_state_replay(x, skb, net_seq);
1703 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705
Al Viro61f46272006-09-27 18:48:33 -07001706void xfrm_replay_advance(struct xfrm_state *x, __be32 net_seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707{
1708 u32 diff;
Al Viro61f46272006-09-27 18:48:33 -07001709 u32 seq = ntohl(net_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710
1711 if (seq > x->replay.seq) {
1712 diff = seq - x->replay.seq;
1713 if (diff < x->props.replay_window)
1714 x->replay.bitmap = ((x->replay.bitmap) << diff) | 1;
1715 else
1716 x->replay.bitmap = 1;
1717 x->replay.seq = seq;
1718 } else {
1719 diff = x->replay.seq - seq;
1720 x->replay.bitmap |= (1U << diff);
1721 }
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001722
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001723 if (xfrm_aevent_is_on(xs_net(x)))
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001724 xfrm_replay_notify(x, XFRM_REPLAY_UPDATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
Denis Chengdf018122007-12-07 00:51:11 -08001727static LIST_HEAD(xfrm_km_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728static DEFINE_RWLOCK(xfrm_km_lock);
1729
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001730void km_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731{
1732 struct xfrm_mgr *km;
1733
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001734 read_lock(&xfrm_km_lock);
1735 list_for_each_entry(km, &xfrm_km_list, list)
1736 if (km->notify_policy)
1737 km->notify_policy(xp, dir, c);
1738 read_unlock(&xfrm_km_lock);
1739}
1740
1741void km_state_notify(struct xfrm_state *x, struct km_event *c)
1742{
1743 struct xfrm_mgr *km;
1744 read_lock(&xfrm_km_lock);
1745 list_for_each_entry(km, &xfrm_km_list, list)
1746 if (km->notify)
1747 km->notify(x, c);
1748 read_unlock(&xfrm_km_lock);
1749}
1750
1751EXPORT_SYMBOL(km_policy_notify);
1752EXPORT_SYMBOL(km_state_notify);
1753
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001754void km_state_expired(struct xfrm_state *x, int hard, u32 pid)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001755{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001756 struct net *net = xs_net(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001757 struct km_event c;
1758
Herbert Xubf088672005-06-18 22:44:00 -07001759 c.data.hard = hard;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001760 c.pid = pid;
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 if (hard)
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001765 wake_up(&net->xfrm.km_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766}
1767
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001768EXPORT_SYMBOL(km_state_expired);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001769/*
1770 * We send to all registered managers regardless of failure
1771 * We are happy with one success
1772*/
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001773int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001775 int err = -EINVAL, acqret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 struct xfrm_mgr *km;
1777
1778 read_lock(&xfrm_km_lock);
1779 list_for_each_entry(km, &xfrm_km_list, list) {
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001780 acqret = km->acquire(x, t, pol, XFRM_POLICY_OUT);
1781 if (!acqret)
1782 err = acqret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 }
1784 read_unlock(&xfrm_km_lock);
1785 return err;
1786}
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001787EXPORT_SYMBOL(km_query);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
Al Viro5d36b182006-11-08 00:24:06 -08001789int km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790{
1791 int err = -EINVAL;
1792 struct xfrm_mgr *km;
1793
1794 read_lock(&xfrm_km_lock);
1795 list_for_each_entry(km, &xfrm_km_list, list) {
1796 if (km->new_mapping)
1797 err = km->new_mapping(x, ipaddr, sport);
1798 if (!err)
1799 break;
1800 }
1801 read_unlock(&xfrm_km_lock);
1802 return err;
1803}
1804EXPORT_SYMBOL(km_new_mapping);
1805
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001806void km_policy_expired(struct xfrm_policy *pol, int dir, int hard, u32 pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001808 struct net *net = xp_net(pol);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001809 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810
Herbert Xubf088672005-06-18 22:44:00 -07001811 c.data.hard = hard;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001812 c.pid = pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001813 c.event = XFRM_MSG_POLEXPIRE;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001814 km_policy_notify(pol, dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815
1816 if (hard)
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001817 wake_up(&net->xfrm.km_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818}
David S. Millera70fcb02006-03-20 19:18:52 -08001819EXPORT_SYMBOL(km_policy_expired);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820
Eric Dumazet2d60abc2008-01-03 20:43:21 -08001821#ifdef CONFIG_XFRM_MIGRATE
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001822int km_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001823 struct xfrm_migrate *m, int num_migrate,
1824 struct xfrm_kmaddress *k)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001825{
1826 int err = -EINVAL;
1827 int ret;
1828 struct xfrm_mgr *km;
1829
1830 read_lock(&xfrm_km_lock);
1831 list_for_each_entry(km, &xfrm_km_list, list) {
1832 if (km->migrate) {
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001833 ret = km->migrate(sel, dir, type, m, num_migrate, k);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001834 if (!ret)
1835 err = ret;
1836 }
1837 }
1838 read_unlock(&xfrm_km_lock);
1839 return err;
1840}
1841EXPORT_SYMBOL(km_migrate);
Eric Dumazet2d60abc2008-01-03 20:43:21 -08001842#endif
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001843
Alexey Dobriyandb983c12008-11-25 17:51:01 -08001844int km_report(struct net *net, u8 proto, struct xfrm_selector *sel, xfrm_address_t *addr)
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001845{
1846 int err = -EINVAL;
1847 int ret;
1848 struct xfrm_mgr *km;
1849
1850 read_lock(&xfrm_km_lock);
1851 list_for_each_entry(km, &xfrm_km_list, list) {
1852 if (km->report) {
Alexey Dobriyandb983c12008-11-25 17:51:01 -08001853 ret = km->report(net, proto, sel, addr);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001854 if (!ret)
1855 err = ret;
1856 }
1857 }
1858 read_unlock(&xfrm_km_lock);
1859 return err;
1860}
1861EXPORT_SYMBOL(km_report);
1862
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863int xfrm_user_policy(struct sock *sk, int optname, u8 __user *optval, int optlen)
1864{
1865 int err;
1866 u8 *data;
1867 struct xfrm_mgr *km;
1868 struct xfrm_policy *pol = NULL;
1869
1870 if (optlen <= 0 || optlen > PAGE_SIZE)
1871 return -EMSGSIZE;
1872
1873 data = kmalloc(optlen, GFP_KERNEL);
1874 if (!data)
1875 return -ENOMEM;
1876
1877 err = -EFAULT;
1878 if (copy_from_user(data, optval, optlen))
1879 goto out;
1880
1881 err = -EINVAL;
1882 read_lock(&xfrm_km_lock);
1883 list_for_each_entry(km, &xfrm_km_list, list) {
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07001884 pol = km->compile_policy(sk, optname, data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 optlen, &err);
1886 if (err >= 0)
1887 break;
1888 }
1889 read_unlock(&xfrm_km_lock);
1890
1891 if (err >= 0) {
1892 xfrm_sk_policy_insert(sk, err, pol);
1893 xfrm_pol_put(pol);
1894 err = 0;
1895 }
1896
1897out:
1898 kfree(data);
1899 return err;
1900}
1901EXPORT_SYMBOL(xfrm_user_policy);
1902
1903int xfrm_register_km(struct xfrm_mgr *km)
1904{
1905 write_lock_bh(&xfrm_km_lock);
1906 list_add_tail(&km->list, &xfrm_km_list);
1907 write_unlock_bh(&xfrm_km_lock);
1908 return 0;
1909}
1910EXPORT_SYMBOL(xfrm_register_km);
1911
1912int xfrm_unregister_km(struct xfrm_mgr *km)
1913{
1914 write_lock_bh(&xfrm_km_lock);
1915 list_del(&km->list);
1916 write_unlock_bh(&xfrm_km_lock);
1917 return 0;
1918}
1919EXPORT_SYMBOL(xfrm_unregister_km);
1920
1921int xfrm_state_register_afinfo(struct xfrm_state_afinfo *afinfo)
1922{
1923 int err = 0;
1924 if (unlikely(afinfo == NULL))
1925 return -EINVAL;
1926 if (unlikely(afinfo->family >= NPROTO))
1927 return -EAFNOSUPPORT;
Ingo Molnarf3111502006-04-28 15:30:03 -07001928 write_lock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 if (unlikely(xfrm_state_afinfo[afinfo->family] != NULL))
1930 err = -ENOBUFS;
David S. Milleredcd5822006-08-24 00:42:45 -07001931 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 xfrm_state_afinfo[afinfo->family] = afinfo;
Ingo Molnarf3111502006-04-28 15:30:03 -07001933 write_unlock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 return err;
1935}
1936EXPORT_SYMBOL(xfrm_state_register_afinfo);
1937
1938int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo)
1939{
1940 int err = 0;
1941 if (unlikely(afinfo == NULL))
1942 return -EINVAL;
1943 if (unlikely(afinfo->family >= NPROTO))
1944 return -EAFNOSUPPORT;
Ingo Molnarf3111502006-04-28 15:30:03 -07001945 write_lock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 if (likely(xfrm_state_afinfo[afinfo->family] != NULL)) {
1947 if (unlikely(xfrm_state_afinfo[afinfo->family] != afinfo))
1948 err = -EINVAL;
David S. Milleredcd5822006-08-24 00:42:45 -07001949 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 xfrm_state_afinfo[afinfo->family] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 }
Ingo Molnarf3111502006-04-28 15:30:03 -07001952 write_unlock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 return err;
1954}
1955EXPORT_SYMBOL(xfrm_state_unregister_afinfo);
1956
Herbert Xu17c2a422007-10-17 21:33:12 -07001957static struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958{
1959 struct xfrm_state_afinfo *afinfo;
1960 if (unlikely(family >= NPROTO))
1961 return NULL;
1962 read_lock(&xfrm_state_afinfo_lock);
1963 afinfo = xfrm_state_afinfo[family];
Herbert Xu546be242006-05-27 23:03:58 -07001964 if (unlikely(!afinfo))
1965 read_unlock(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 return afinfo;
1967}
1968
Herbert Xu17c2a422007-10-17 21:33:12 -07001969static void xfrm_state_put_afinfo(struct xfrm_state_afinfo *afinfo)
Eric Dumazet9a429c42008-01-01 21:58:02 -08001970 __releases(xfrm_state_afinfo_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971{
Herbert Xu546be242006-05-27 23:03:58 -07001972 read_unlock(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973}
1974
1975/* Temporarily located here until net/xfrm/xfrm_tunnel.c is created */
1976void xfrm_state_delete_tunnel(struct xfrm_state *x)
1977{
1978 if (x->tunnel) {
1979 struct xfrm_state *t = x->tunnel;
1980
1981 if (atomic_read(&t->tunnel_users) == 2)
1982 xfrm_state_delete(t);
1983 atomic_dec(&t->tunnel_users);
1984 xfrm_state_put(t);
1985 x->tunnel = NULL;
1986 }
1987}
1988EXPORT_SYMBOL(xfrm_state_delete_tunnel);
1989
1990int xfrm_state_mtu(struct xfrm_state *x, int mtu)
1991{
Patrick McHardyc5c25232007-04-09 11:47:18 -07001992 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993
Patrick McHardyc5c25232007-04-09 11:47:18 -07001994 spin_lock_bh(&x->lock);
1995 if (x->km.state == XFRM_STATE_VALID &&
1996 x->type && x->type->get_mtu)
1997 res = x->type->get_mtu(x, mtu);
1998 else
Patrick McHardy28121612007-06-18 22:30:15 -07001999 res = mtu - x->props.header_len;
Patrick McHardyc5c25232007-04-09 11:47:18 -07002000 spin_unlock_bh(&x->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 return res;
2002}
2003
Herbert Xu72cb6962005-06-20 13:18:08 -07002004int xfrm_init_state(struct xfrm_state *x)
2005{
Herbert Xud094cd82005-06-20 13:19:41 -07002006 struct xfrm_state_afinfo *afinfo;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002007 struct xfrm_mode *inner_mode;
Herbert Xud094cd82005-06-20 13:19:41 -07002008 int family = x->props.family;
Herbert Xu72cb6962005-06-20 13:18:08 -07002009 int err;
2010
Herbert Xud094cd82005-06-20 13:19:41 -07002011 err = -EAFNOSUPPORT;
2012 afinfo = xfrm_state_get_afinfo(family);
2013 if (!afinfo)
2014 goto error;
2015
2016 err = 0;
2017 if (afinfo->init_flags)
2018 err = afinfo->init_flags(x);
2019
2020 xfrm_state_put_afinfo(afinfo);
2021
2022 if (err)
2023 goto error;
2024
2025 err = -EPROTONOSUPPORT;
Herbert Xu13996372007-10-17 21:35:51 -07002026
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002027 if (x->sel.family != AF_UNSPEC) {
2028 inner_mode = xfrm_get_mode(x->props.mode, x->sel.family);
2029 if (inner_mode == NULL)
2030 goto error;
2031
2032 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL) &&
2033 family != x->sel.family) {
2034 xfrm_put_mode(inner_mode);
2035 goto error;
2036 }
2037
2038 x->inner_mode = inner_mode;
2039 } else {
2040 struct xfrm_mode *inner_mode_iaf;
Martin Willid81d2282008-12-03 15:38:07 -08002041 int iafamily = AF_INET;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002042
Martin Willid81d2282008-12-03 15:38:07 -08002043 inner_mode = xfrm_get_mode(x->props.mode, x->props.family);
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002044 if (inner_mode == NULL)
2045 goto error;
2046
2047 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL)) {
2048 xfrm_put_mode(inner_mode);
2049 goto error;
2050 }
Martin Willid81d2282008-12-03 15:38:07 -08002051 x->inner_mode = inner_mode;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002052
Martin Willid81d2282008-12-03 15:38:07 -08002053 if (x->props.family == AF_INET)
2054 iafamily = AF_INET6;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002055
Martin Willid81d2282008-12-03 15:38:07 -08002056 inner_mode_iaf = xfrm_get_mode(x->props.mode, iafamily);
2057 if (inner_mode_iaf) {
2058 if (inner_mode_iaf->flags & XFRM_MODE_FLAG_TUNNEL)
2059 x->inner_mode_iaf = inner_mode_iaf;
2060 else
2061 xfrm_put_mode(inner_mode_iaf);
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002062 }
2063 }
Herbert Xu13996372007-10-17 21:35:51 -07002064
Herbert Xud094cd82005-06-20 13:19:41 -07002065 x->type = xfrm_get_type(x->id.proto, family);
Herbert Xu72cb6962005-06-20 13:18:08 -07002066 if (x->type == NULL)
2067 goto error;
2068
2069 err = x->type->init_state(x);
2070 if (err)
2071 goto error;
2072
Herbert Xu13996372007-10-17 21:35:51 -07002073 x->outer_mode = xfrm_get_mode(x->props.mode, family);
2074 if (x->outer_mode == NULL)
Herbert Xub59f45d2006-05-27 23:05:54 -07002075 goto error;
2076
Herbert Xu72cb6962005-06-20 13:18:08 -07002077 x->km.state = XFRM_STATE_VALID;
2078
2079error:
2080 return err;
2081}
2082
2083EXPORT_SYMBOL(xfrm_init_state);
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09002084
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002085int __net_init xfrm_state_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086{
David S. Millerf034b5d2006-08-24 03:08:07 -07002087 unsigned int sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088
Alexey Dobriyan9d4139c2008-11-25 17:16:11 -08002089 INIT_LIST_HEAD(&net->xfrm.state_all);
2090
David S. Millerf034b5d2006-08-24 03:08:07 -07002091 sz = sizeof(struct hlist_head) * 8;
2092
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002093 net->xfrm.state_bydst = xfrm_hash_alloc(sz);
2094 if (!net->xfrm.state_bydst)
2095 goto out_bydst;
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002096 net->xfrm.state_bysrc = xfrm_hash_alloc(sz);
2097 if (!net->xfrm.state_bysrc)
2098 goto out_bysrc;
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002099 net->xfrm.state_byspi = xfrm_hash_alloc(sz);
2100 if (!net->xfrm.state_byspi)
2101 goto out_byspi;
Alexey Dobriyan529983e2008-11-25 17:18:12 -08002102 net->xfrm.state_hmask = ((sz / sizeof(struct hlist_head)) - 1);
David S. Millerf034b5d2006-08-24 03:08:07 -07002103
Alexey Dobriyan0bf7c5b2008-11-25 17:18:39 -08002104 net->xfrm.state_num = 0;
Alexey Dobriyan63082732008-11-25 17:19:07 -08002105 INIT_WORK(&net->xfrm.state_hash_work, xfrm_hash_resize);
Alexey Dobriyanb8a0ae22008-11-25 17:20:11 -08002106 INIT_HLIST_HEAD(&net->xfrm.state_gc_list);
Alexey Dobriyanc7837142008-11-25 17:20:36 -08002107 INIT_WORK(&net->xfrm.state_gc_work, xfrm_state_gc_task);
Alexey Dobriyan50a30652008-11-25 17:21:01 -08002108 init_waitqueue_head(&net->xfrm.km_waitq);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002109 return 0;
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002110
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002111out_byspi:
2112 xfrm_hash_free(net->xfrm.state_bysrc, sz);
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002113out_bysrc:
2114 xfrm_hash_free(net->xfrm.state_bydst, sz);
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002115out_bydst:
2116 return -ENOMEM;
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002117}
2118
2119void xfrm_state_fini(struct net *net)
2120{
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002121 struct xfrm_audit audit_info;
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002122 unsigned int sz;
2123
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002124 flush_work(&net->xfrm.state_hash_work);
2125 audit_info.loginuid = -1;
2126 audit_info.sessionid = -1;
2127 audit_info.secid = 0;
2128 xfrm_state_flush(net, IPSEC_PROTO_ANY, &audit_info);
2129 flush_work(&net->xfrm.state_gc_work);
2130
Alexey Dobriyan9d4139c2008-11-25 17:16:11 -08002131 WARN_ON(!list_empty(&net->xfrm.state_all));
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002132
Alexey Dobriyan529983e2008-11-25 17:18:12 -08002133 sz = (net->xfrm.state_hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002134 WARN_ON(!hlist_empty(net->xfrm.state_byspi));
2135 xfrm_hash_free(net->xfrm.state_byspi, sz);
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002136 WARN_ON(!hlist_empty(net->xfrm.state_bysrc));
2137 xfrm_hash_free(net->xfrm.state_bysrc, sz);
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002138 WARN_ON(!hlist_empty(net->xfrm.state_bydst));
2139 xfrm_hash_free(net->xfrm.state_bydst, sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140}
2141
Joy Lattenab5f5e82007-09-17 11:51:22 -07002142#ifdef CONFIG_AUDITSYSCALL
Ilpo Järvinencf35f432008-01-05 23:13:20 -08002143static void xfrm_audit_helper_sainfo(struct xfrm_state *x,
2144 struct audit_buffer *audit_buf)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002145{
Paul Moore68277ac2007-12-20 20:49:33 -08002146 struct xfrm_sec_ctx *ctx = x->security;
2147 u32 spi = ntohl(x->id.spi);
2148
2149 if (ctx)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002150 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
Paul Moore68277ac2007-12-20 20:49:33 -08002151 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002152
2153 switch(x->props.family) {
2154 case AF_INET:
Harvey Harrison21454aa2008-10-31 00:54:56 -07002155 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2156 &x->props.saddr.a4, &x->id.daddr.a4);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002157 break;
2158 case AF_INET6:
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002159 audit_log_format(audit_buf, " src=%pI6 dst=%pI6",
Harvey Harrisonfdb46ee2008-10-28 16:10:17 -07002160 x->props.saddr.a6, x->id.daddr.a6);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002161 break;
2162 }
Paul Moore68277ac2007-12-20 20:49:33 -08002163
2164 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002165}
2166
Ilpo Järvinencf35f432008-01-05 23:13:20 -08002167static void xfrm_audit_helper_pktinfo(struct sk_buff *skb, u16 family,
2168 struct audit_buffer *audit_buf)
Paul Mooreafeb14b2007-12-21 14:58:11 -08002169{
2170 struct iphdr *iph4;
2171 struct ipv6hdr *iph6;
2172
2173 switch (family) {
2174 case AF_INET:
2175 iph4 = ip_hdr(skb);
Harvey Harrison21454aa2008-10-31 00:54:56 -07002176 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2177 &iph4->saddr, &iph4->daddr);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002178 break;
2179 case AF_INET6:
2180 iph6 = ipv6_hdr(skb);
2181 audit_log_format(audit_buf,
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002182 " src=%pI6 dst=%pI6 flowlbl=0x%x%02x%02x",
Harvey Harrisonfdb46ee2008-10-28 16:10:17 -07002183 &iph6->saddr,&iph6->daddr,
Paul Mooreafeb14b2007-12-21 14:58:11 -08002184 iph6->flow_lbl[0] & 0x0f,
2185 iph6->flow_lbl[1],
2186 iph6->flow_lbl[2]);
2187 break;
2188 }
2189}
2190
Paul Moore68277ac2007-12-20 20:49:33 -08002191void xfrm_audit_state_add(struct xfrm_state *x, int result,
Eric Paris25323862008-04-18 10:09:25 -04002192 uid_t auid, u32 sessionid, u32 secid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002193{
2194 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002195
Paul Mooreafeb14b2007-12-21 14:58:11 -08002196 audit_buf = xfrm_audit_start("SAD-add");
Joy Lattenab5f5e82007-09-17 11:51:22 -07002197 if (audit_buf == NULL)
2198 return;
Eric Paris25323862008-04-18 10:09:25 -04002199 xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002200 xfrm_audit_helper_sainfo(x, audit_buf);
2201 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002202 audit_log_end(audit_buf);
2203}
2204EXPORT_SYMBOL_GPL(xfrm_audit_state_add);
2205
Paul Moore68277ac2007-12-20 20:49:33 -08002206void xfrm_audit_state_delete(struct xfrm_state *x, int result,
Eric Paris25323862008-04-18 10:09:25 -04002207 uid_t auid, u32 sessionid, u32 secid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002208{
2209 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002210
Paul Mooreafeb14b2007-12-21 14:58:11 -08002211 audit_buf = xfrm_audit_start("SAD-delete");
Joy Lattenab5f5e82007-09-17 11:51:22 -07002212 if (audit_buf == NULL)
2213 return;
Eric Paris25323862008-04-18 10:09:25 -04002214 xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002215 xfrm_audit_helper_sainfo(x, audit_buf);
2216 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002217 audit_log_end(audit_buf);
2218}
2219EXPORT_SYMBOL_GPL(xfrm_audit_state_delete);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002220
2221void xfrm_audit_state_replay_overflow(struct xfrm_state *x,
2222 struct sk_buff *skb)
2223{
2224 struct audit_buffer *audit_buf;
2225 u32 spi;
2226
2227 audit_buf = xfrm_audit_start("SA-replay-overflow");
2228 if (audit_buf == NULL)
2229 return;
2230 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2231 /* don't record the sequence number because it's inherent in this kind
2232 * of audit message */
2233 spi = ntohl(x->id.spi);
2234 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
2235 audit_log_end(audit_buf);
2236}
2237EXPORT_SYMBOL_GPL(xfrm_audit_state_replay_overflow);
2238
2239static void xfrm_audit_state_replay(struct xfrm_state *x,
2240 struct sk_buff *skb, __be32 net_seq)
2241{
2242 struct audit_buffer *audit_buf;
2243 u32 spi;
2244
2245 audit_buf = xfrm_audit_start("SA-replayed-pkt");
2246 if (audit_buf == NULL)
2247 return;
2248 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2249 spi = ntohl(x->id.spi);
2250 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2251 spi, spi, ntohl(net_seq));
2252 audit_log_end(audit_buf);
2253}
2254
2255void xfrm_audit_state_notfound_simple(struct sk_buff *skb, u16 family)
2256{
2257 struct audit_buffer *audit_buf;
2258
2259 audit_buf = xfrm_audit_start("SA-notfound");
2260 if (audit_buf == NULL)
2261 return;
2262 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2263 audit_log_end(audit_buf);
2264}
2265EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound_simple);
2266
2267void xfrm_audit_state_notfound(struct sk_buff *skb, u16 family,
2268 __be32 net_spi, __be32 net_seq)
2269{
2270 struct audit_buffer *audit_buf;
2271 u32 spi;
2272
2273 audit_buf = xfrm_audit_start("SA-notfound");
2274 if (audit_buf == NULL)
2275 return;
2276 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2277 spi = ntohl(net_spi);
2278 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2279 spi, spi, ntohl(net_seq));
2280 audit_log_end(audit_buf);
2281}
2282EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound);
2283
2284void xfrm_audit_state_icvfail(struct xfrm_state *x,
2285 struct sk_buff *skb, u8 proto)
2286{
2287 struct audit_buffer *audit_buf;
2288 __be32 net_spi;
2289 __be32 net_seq;
2290
2291 audit_buf = xfrm_audit_start("SA-icv-failure");
2292 if (audit_buf == NULL)
2293 return;
2294 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2295 if (xfrm_parse_spi(skb, proto, &net_spi, &net_seq) == 0) {
2296 u32 spi = ntohl(net_spi);
2297 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2298 spi, spi, ntohl(net_seq));
2299 }
2300 audit_log_end(audit_buf);
2301}
2302EXPORT_SYMBOL_GPL(xfrm_audit_state_icvfail);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002303#endif /* CONFIG_AUDITSYSCALL */