blob: 9fa3322b2a7dda786922f472b6a49a369c40f1ac [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>
25#include <linux/interrupt.h>
26#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
David S. Miller44e36b42006-08-24 04:50:50 -070028#include "xfrm_hash.h"
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030/* Each xfrm_state may be linked to two tables:
31
32 1. Hash table by (spi,daddr,ah/esp) to find SA by SPI. (input,ctl)
David S. Millera624c102006-08-24 03:24:33 -070033 2. Hash table by (daddr,family,reqid) to find what SAs exist for given
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 destination/tunnel endpoint. (output)
35 */
36
37static DEFINE_SPINLOCK(xfrm_state_lock);
38
David S. Millerf034b5d2006-08-24 03:08:07 -070039static unsigned int xfrm_state_hashmax __read_mostly = 1 * 1024 * 1024;
David S. Miller9d4a7062006-08-24 03:18:09 -070040static unsigned int xfrm_state_genid;
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
659xfrm_init_tempsel(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)
663{
664 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
665 if (!afinfo)
666 return -1;
667 afinfo->init_tempsel(x, fl, tmpl, daddr, saddr);
668 xfrm_state_put_afinfo(afinfo);
669 return 0;
670}
671
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800672static struct xfrm_state *__xfrm_state_lookup(struct net *net, xfrm_address_t *daddr, __be32 spi, u8 proto, unsigned short family)
David S. Milleredcd5822006-08-24 00:42:45 -0700673{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800674 unsigned int h = xfrm_spi_hash(net, daddr, spi, proto, family);
David S. Milleredcd5822006-08-24 00:42:45 -0700675 struct xfrm_state *x;
David S. Miller8f126e32006-08-24 02:45:07 -0700676 struct hlist_node *entry;
David S. Milleredcd5822006-08-24 00:42:45 -0700677
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800678 hlist_for_each_entry(x, entry, net->xfrm.state_byspi+h, byspi) {
David S. Milleredcd5822006-08-24 00:42:45 -0700679 if (x->props.family != family ||
680 x->id.spi != spi ||
Wei Yongjun18025712009-06-28 18:42:53 +0000681 x->id.proto != proto ||
682 xfrm_addr_cmp(&x->id.daddr, daddr, family))
David S. Milleredcd5822006-08-24 00:42:45 -0700683 continue;
684
David S. Milleredcd5822006-08-24 00:42:45 -0700685 xfrm_state_hold(x);
686 return x;
687 }
688
689 return NULL;
690}
691
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800692static struct xfrm_state *__xfrm_state_lookup_byaddr(struct net *net, xfrm_address_t *daddr, xfrm_address_t *saddr, u8 proto, unsigned short family)
David S. Milleredcd5822006-08-24 00:42:45 -0700693{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800694 unsigned int h = xfrm_src_hash(net, daddr, saddr, family);
David S. Milleredcd5822006-08-24 00:42:45 -0700695 struct xfrm_state *x;
David S. Miller8f126e32006-08-24 02:45:07 -0700696 struct hlist_node *entry;
David S. Milleredcd5822006-08-24 00:42:45 -0700697
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800698 hlist_for_each_entry(x, entry, net->xfrm.state_bysrc+h, bysrc) {
David S. Milleredcd5822006-08-24 00:42:45 -0700699 if (x->props.family != family ||
Wei Yongjun18025712009-06-28 18:42:53 +0000700 x->id.proto != proto ||
701 xfrm_addr_cmp(&x->id.daddr, daddr, family) ||
702 xfrm_addr_cmp(&x->props.saddr, saddr, family))
David S. Milleredcd5822006-08-24 00:42:45 -0700703 continue;
704
David S. Milleredcd5822006-08-24 00:42:45 -0700705 xfrm_state_hold(x);
706 return x;
707 }
708
709 return NULL;
710}
711
712static inline struct xfrm_state *
713__xfrm_state_locate(struct xfrm_state *x, int use_spi, int family)
714{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800715 struct net *net = xs_net(x);
716
David S. Milleredcd5822006-08-24 00:42:45 -0700717 if (use_spi)
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800718 return __xfrm_state_lookup(net, &x->id.daddr, x->id.spi,
David S. Milleredcd5822006-08-24 00:42:45 -0700719 x->id.proto, family);
720 else
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800721 return __xfrm_state_lookup_byaddr(net, &x->id.daddr,
David S. Milleredcd5822006-08-24 00:42:45 -0700722 &x->props.saddr,
723 x->id.proto, family);
724}
725
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800726static void xfrm_hash_grow_check(struct net *net, int have_hash_collision)
Patrick McHardy2fab22f2006-10-24 15:34:00 -0700727{
728 if (have_hash_collision &&
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800729 (net->xfrm.state_hmask + 1) < xfrm_state_hashmax &&
730 net->xfrm.state_num > net->xfrm.state_hmask)
731 schedule_work(&net->xfrm.state_hash_work);
Patrick McHardy2fab22f2006-10-24 15:34:00 -0700732}
733
David S. Miller08ec9af2009-03-13 14:22:40 -0700734static void xfrm_state_look_at(struct xfrm_policy *pol, struct xfrm_state *x,
735 struct flowi *fl, unsigned short family,
736 xfrm_address_t *daddr, xfrm_address_t *saddr,
737 struct xfrm_state **best, int *acq_in_progress,
738 int *error)
739{
740 /* Resolution logic:
741 * 1. There is a valid state with matching selector. Done.
742 * 2. Valid state with inappropriate selector. Skip.
743 *
744 * Entering area of "sysdeps".
745 *
746 * 3. If state is not valid, selector is temporary, it selects
747 * only session which triggered previous resolution. Key
748 * manager will do something to install a state with proper
749 * selector.
750 */
751 if (x->km.state == XFRM_STATE_VALID) {
752 if ((x->sel.family &&
753 !xfrm_selector_match(&x->sel, fl, x->sel.family)) ||
754 !security_xfrm_state_pol_flow_match(x, pol, fl))
755 return;
756
757 if (!*best ||
758 (*best)->km.dying > x->km.dying ||
759 ((*best)->km.dying == x->km.dying &&
760 (*best)->curlft.add_time < x->curlft.add_time))
761 *best = x;
762 } else if (x->km.state == XFRM_STATE_ACQ) {
763 *acq_in_progress = 1;
764 } else if (x->km.state == XFRM_STATE_ERROR ||
765 x->km.state == XFRM_STATE_EXPIRED) {
766 if (xfrm_selector_match(&x->sel, fl, x->sel.family) &&
767 security_xfrm_state_pol_flow_match(x, pol, fl))
768 *error = -ESRCH;
769 }
770}
771
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772struct xfrm_state *
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900773xfrm_state_find(xfrm_address_t *daddr, xfrm_address_t *saddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 struct flowi *fl, struct xfrm_tmpl *tmpl,
775 struct xfrm_policy *pol, int *err,
776 unsigned short family)
777{
David S. Miller08ec9af2009-03-13 14:22:40 -0700778 static xfrm_address_t saddr_wildcard = { };
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800779 struct net *net = xp_net(pol);
Nicolas Dichtel6a783c92009-04-27 02:58:59 -0700780 unsigned int h, h_wildcard;
David S. Miller8f126e32006-08-24 02:45:07 -0700781 struct hlist_node *entry;
David S. Miller37b08e32008-09-02 20:14:15 -0700782 struct xfrm_state *x, *x0, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 int acquire_in_progress = 0;
784 int error = 0;
785 struct xfrm_state *best = NULL;
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900786
David S. Miller37b08e32008-09-02 20:14:15 -0700787 to_put = NULL;
788
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 spin_lock_bh(&xfrm_state_lock);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800790 h = xfrm_dst_hash(net, daddr, saddr, tmpl->reqid, family);
791 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+h, bydst) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 if (x->props.family == family &&
793 x->props.reqid == tmpl->reqid &&
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -0700794 !(x->props.flags & XFRM_STATE_WILDRECV) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 xfrm_state_addr_check(x, daddr, saddr, family) &&
796 tmpl->mode == x->props.mode &&
797 tmpl->id.proto == x->id.proto &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700798 (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
799 xfrm_state_look_at(pol, x, fl, family, daddr, saddr,
800 &best, &acquire_in_progress, &error);
801 }
802 if (best)
803 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Nicolas Dichtel6a783c92009-04-27 02:58:59 -0700805 h_wildcard = xfrm_dst_hash(net, daddr, &saddr_wildcard, tmpl->reqid, family);
806 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+h_wildcard, bydst) {
David S. Miller08ec9af2009-03-13 14:22:40 -0700807 if (x->props.family == family &&
808 x->props.reqid == tmpl->reqid &&
809 !(x->props.flags & XFRM_STATE_WILDRECV) &&
810 xfrm_state_addr_check(x, daddr, saddr, family) &&
811 tmpl->mode == x->props.mode &&
812 tmpl->id.proto == x->id.proto &&
813 (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
814 xfrm_state_look_at(pol, x, fl, family, daddr, saddr,
815 &best, &acquire_in_progress, &error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 }
817
David S. Miller08ec9af2009-03-13 14:22:40 -0700818found:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 x = best;
820 if (!x && !error && !acquire_in_progress) {
Patrick McHardy5c5d2812005-04-21 20:12:32 -0700821 if (tmpl->id.spi &&
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800822 (x0 = __xfrm_state_lookup(net, daddr, tmpl->id.spi,
David S. Milleredcd5822006-08-24 00:42:45 -0700823 tmpl->id.proto, family)) != NULL) {
David S. Miller37b08e32008-09-02 20:14:15 -0700824 to_put = x0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 error = -EEXIST;
826 goto out;
827 }
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800828 x = xfrm_state_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 if (x == NULL) {
830 error = -ENOMEM;
831 goto out;
832 }
833 /* Initialize temporary selector matching only
834 * to current session. */
835 xfrm_init_tempsel(x, fl, tmpl, daddr, saddr, family);
836
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -0700837 error = security_xfrm_state_alloc_acquire(x, pol->security, fl->secid);
838 if (error) {
839 x->km.state = XFRM_STATE_DEAD;
David S. Miller37b08e32008-09-02 20:14:15 -0700840 to_put = x;
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -0700841 x = NULL;
842 goto out;
843 }
844
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 if (km_query(x, tmpl, pol) == 0) {
846 x->km.state = XFRM_STATE_ACQ;
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800847 list_add(&x->km.all, &net->xfrm.state_all);
848 hlist_add_head(&x->bydst, net->xfrm.state_bydst+h);
849 h = xfrm_src_hash(net, daddr, saddr, family);
850 hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 if (x->id.spi) {
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800852 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, family);
853 hlist_add_head(&x->byspi, net->xfrm.state_byspi+h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 }
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800855 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800856 tasklet_hrtimer_start(&x->mtimer, ktime_set(net->xfrm.sysctl_acq_expires, 0), HRTIMER_MODE_REL);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800857 net->xfrm.state_num++;
858 xfrm_hash_grow_check(net, x->bydst.next != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 } else {
860 x->km.state = XFRM_STATE_DEAD;
David S. Miller37b08e32008-09-02 20:14:15 -0700861 to_put = x;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 x = NULL;
863 error = -ESRCH;
864 }
865 }
866out:
867 if (x)
868 xfrm_state_hold(x);
869 else
870 *err = acquire_in_progress ? -EAGAIN : error;
871 spin_unlock_bh(&xfrm_state_lock);
David S. Miller37b08e32008-09-02 20:14:15 -0700872 if (to_put)
873 xfrm_state_put(to_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 return x;
875}
876
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700877struct xfrm_state *
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800878xfrm_stateonly_find(struct net *net,
879 xfrm_address_t *daddr, xfrm_address_t *saddr,
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700880 unsigned short family, u8 mode, u8 proto, u32 reqid)
881{
Pavel Emelyanov4bda4f22007-12-14 11:38:04 -0800882 unsigned int h;
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700883 struct xfrm_state *rx = NULL, *x = NULL;
884 struct hlist_node *entry;
885
886 spin_lock(&xfrm_state_lock);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800887 h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
888 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+h, bydst) {
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700889 if (x->props.family == family &&
890 x->props.reqid == reqid &&
891 !(x->props.flags & XFRM_STATE_WILDRECV) &&
892 xfrm_state_addr_check(x, daddr, saddr, family) &&
893 mode == x->props.mode &&
894 proto == x->id.proto &&
895 x->km.state == XFRM_STATE_VALID) {
896 rx = x;
897 break;
898 }
899 }
900
901 if (rx)
902 xfrm_state_hold(rx);
903 spin_unlock(&xfrm_state_lock);
904
905
906 return rx;
907}
908EXPORT_SYMBOL(xfrm_stateonly_find);
909
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910static void __xfrm_state_insert(struct xfrm_state *x)
911{
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800912 struct net *net = xs_net(x);
David S. Millera624c102006-08-24 03:24:33 -0700913 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
David S. Miller9d4a7062006-08-24 03:18:09 -0700915 x->genid = ++xfrm_state_genid;
916
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800917 list_add(&x->km.all, &net->xfrm.state_all);
Timo Teras4c563f72008-02-28 21:31:08 -0800918
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800919 h = xfrm_dst_hash(net, &x->id.daddr, &x->props.saddr,
David S. Millerc1969f22006-08-24 04:00:03 -0700920 x->props.reqid, x->props.family);
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800921 hlist_add_head(&x->bydst, net->xfrm.state_bydst+h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800923 h = xfrm_src_hash(net, &x->id.daddr, &x->props.saddr, x->props.family);
924 hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
Masahide NAKAMURA7b4dc3602006-09-27 22:21:52 -0700926 if (x->id.spi) {
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800927 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto,
Masahide NAKAMURA6c44e6b2006-08-23 17:53:57 -0700928 x->props.family);
929
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800930 hlist_add_head(&x->byspi, net->xfrm.state_byspi+h);
Masahide NAKAMURA6c44e6b2006-08-23 17:53:57 -0700931 }
932
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800933 tasklet_hrtimer_start(&x->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL);
David S. Millera47f0ce2006-08-24 03:54:22 -0700934 if (x->replay_maxage)
935 mod_timer(&x->rtimer, jiffies + x->replay_maxage);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -0800936
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800937 wake_up(&net->xfrm.km_waitq);
David S. Millerf034b5d2006-08-24 03:08:07 -0700938
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800939 net->xfrm.state_num++;
David S. Millerf034b5d2006-08-24 03:08:07 -0700940
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800941 xfrm_hash_grow_check(net, x->bydst.next != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942}
943
David S. Millerc7f5ea32006-08-24 03:29:04 -0700944/* xfrm_state_lock is held */
945static void __xfrm_state_bump_genids(struct xfrm_state *xnew)
946{
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800947 struct net *net = xs_net(xnew);
David S. Millerc7f5ea32006-08-24 03:29:04 -0700948 unsigned short family = xnew->props.family;
949 u32 reqid = xnew->props.reqid;
950 struct xfrm_state *x;
951 struct hlist_node *entry;
952 unsigned int h;
953
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800954 h = xfrm_dst_hash(net, &xnew->id.daddr, &xnew->props.saddr, reqid, family);
955 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+h, bydst) {
David S. Millerc7f5ea32006-08-24 03:29:04 -0700956 if (x->props.family == family &&
957 x->props.reqid == reqid &&
David S. Millerc1969f22006-08-24 04:00:03 -0700958 !xfrm_addr_cmp(&x->id.daddr, &xnew->id.daddr, family) &&
959 !xfrm_addr_cmp(&x->props.saddr, &xnew->props.saddr, family))
David S. Millerc7f5ea32006-08-24 03:29:04 -0700960 x->genid = xfrm_state_genid;
961 }
962}
963
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964void xfrm_state_insert(struct xfrm_state *x)
965{
966 spin_lock_bh(&xfrm_state_lock);
David S. Millerc7f5ea32006-08-24 03:29:04 -0700967 __xfrm_state_bump_genids(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 __xfrm_state_insert(x);
969 spin_unlock_bh(&xfrm_state_lock);
970}
971EXPORT_SYMBOL(xfrm_state_insert);
972
David S. Miller27708342006-08-24 00:13:10 -0700973/* xfrm_state_lock is held */
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800974static struct xfrm_state *__find_acq_core(struct net *net, 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 -0700975{
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800976 unsigned int h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
David S. Miller8f126e32006-08-24 02:45:07 -0700977 struct hlist_node *entry;
David S. Miller27708342006-08-24 00:13:10 -0700978 struct xfrm_state *x;
979
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800980 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+h, bydst) {
David S. Miller27708342006-08-24 00:13:10 -0700981 if (x->props.reqid != reqid ||
982 x->props.mode != mode ||
983 x->props.family != family ||
984 x->km.state != XFRM_STATE_ACQ ||
Joy Latten75e252d2007-03-12 17:14:07 -0700985 x->id.spi != 0 ||
Wei Yongjun18025712009-06-28 18:42:53 +0000986 x->id.proto != proto ||
987 xfrm_addr_cmp(&x->id.daddr, daddr, family) ||
988 xfrm_addr_cmp(&x->props.saddr, saddr, family))
David S. Miller27708342006-08-24 00:13:10 -0700989 continue;
990
David S. Miller27708342006-08-24 00:13:10 -0700991 xfrm_state_hold(x);
992 return x;
993 }
994
995 if (!create)
996 return NULL;
997
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800998 x = xfrm_state_alloc(net);
David S. Miller27708342006-08-24 00:13:10 -0700999 if (likely(x)) {
1000 switch (family) {
1001 case AF_INET:
1002 x->sel.daddr.a4 = daddr->a4;
1003 x->sel.saddr.a4 = saddr->a4;
1004 x->sel.prefixlen_d = 32;
1005 x->sel.prefixlen_s = 32;
1006 x->props.saddr.a4 = saddr->a4;
1007 x->id.daddr.a4 = daddr->a4;
1008 break;
1009
1010 case AF_INET6:
1011 ipv6_addr_copy((struct in6_addr *)x->sel.daddr.a6,
1012 (struct in6_addr *)daddr);
1013 ipv6_addr_copy((struct in6_addr *)x->sel.saddr.a6,
1014 (struct in6_addr *)saddr);
1015 x->sel.prefixlen_d = 128;
1016 x->sel.prefixlen_s = 128;
1017 ipv6_addr_copy((struct in6_addr *)x->props.saddr.a6,
1018 (struct in6_addr *)saddr);
1019 ipv6_addr_copy((struct in6_addr *)x->id.daddr.a6,
1020 (struct in6_addr *)daddr);
1021 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001022 }
David S. Miller27708342006-08-24 00:13:10 -07001023
1024 x->km.state = XFRM_STATE_ACQ;
1025 x->id.proto = proto;
1026 x->props.family = family;
1027 x->props.mode = mode;
1028 x->props.reqid = reqid;
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08001029 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
David S. Miller27708342006-08-24 00:13:10 -07001030 xfrm_state_hold(x);
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -08001031 tasklet_hrtimer_start(&x->mtimer, ktime_set(net->xfrm.sysctl_acq_expires, 0), HRTIMER_MODE_REL);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001032 list_add(&x->km.all, &net->xfrm.state_all);
1033 hlist_add_head(&x->bydst, net->xfrm.state_bydst+h);
1034 h = xfrm_src_hash(net, daddr, saddr, family);
1035 hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h);
David S. Miller918049f2006-10-12 22:03:24 -07001036
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001037 net->xfrm.state_num++;
David S. Miller918049f2006-10-12 22:03:24 -07001038
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001039 xfrm_hash_grow_check(net, x->bydst.next != NULL);
David S. Miller27708342006-08-24 00:13:10 -07001040 }
1041
1042 return x;
1043}
1044
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001045static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
1047int xfrm_state_add(struct xfrm_state *x)
1048{
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001049 struct net *net = xs_net(x);
David S. Miller37b08e32008-09-02 20:14:15 -07001050 struct xfrm_state *x1, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 int family;
1052 int err;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001053 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
1055 family = x->props.family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
David S. Miller37b08e32008-09-02 20:14:15 -07001057 to_put = NULL;
1058
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 spin_lock_bh(&xfrm_state_lock);
1060
David S. Milleredcd5822006-08-24 00:42:45 -07001061 x1 = __xfrm_state_locate(x, use_spi, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 if (x1) {
David S. Miller37b08e32008-09-02 20:14:15 -07001063 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 x1 = NULL;
1065 err = -EEXIST;
1066 goto out;
1067 }
1068
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001069 if (use_spi && x->km.seq) {
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001070 x1 = __xfrm_find_acq_byseq(net, x->km.seq);
Joy Latten75e252d2007-03-12 17:14:07 -07001071 if (x1 && ((x1->id.proto != x->id.proto) ||
1072 xfrm_addr_cmp(&x1->id.daddr, &x->id.daddr, family))) {
David S. Miller37b08e32008-09-02 20:14:15 -07001073 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 x1 = NULL;
1075 }
1076 }
1077
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001078 if (use_spi && !x1)
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001079 x1 = __find_acq_core(net, family, x->props.mode, x->props.reqid,
David S. Miller27708342006-08-24 00:13:10 -07001080 x->id.proto,
1081 &x->id.daddr, &x->props.saddr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
David S. Millerc7f5ea32006-08-24 03:29:04 -07001083 __xfrm_state_bump_genids(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 __xfrm_state_insert(x);
1085 err = 0;
1086
1087out:
1088 spin_unlock_bh(&xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
1090 if (x1) {
1091 xfrm_state_delete(x1);
1092 xfrm_state_put(x1);
1093 }
1094
David S. Miller37b08e32008-09-02 20:14:15 -07001095 if (to_put)
1096 xfrm_state_put(to_put);
1097
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 return err;
1099}
1100EXPORT_SYMBOL(xfrm_state_add);
1101
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001102#ifdef CONFIG_XFRM_MIGRATE
Eric Dumazet66663512008-01-08 01:35:52 -08001103static struct xfrm_state *xfrm_state_clone(struct xfrm_state *orig, int *errp)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001104{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001105 struct net *net = xs_net(orig);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001106 int err = -ENOMEM;
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001107 struct xfrm_state *x = xfrm_state_alloc(net);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001108 if (!x)
Herbert Xu553f9112010-02-15 20:00:51 +00001109 goto out;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001110
1111 memcpy(&x->id, &orig->id, sizeof(x->id));
1112 memcpy(&x->sel, &orig->sel, sizeof(x->sel));
1113 memcpy(&x->lft, &orig->lft, sizeof(x->lft));
1114 x->props.mode = orig->props.mode;
1115 x->props.replay_window = orig->props.replay_window;
1116 x->props.reqid = orig->props.reqid;
1117 x->props.family = orig->props.family;
1118 x->props.saddr = orig->props.saddr;
1119
1120 if (orig->aalg) {
Martin Willi4447bb32009-11-25 00:29:52 +00001121 x->aalg = xfrm_algo_auth_clone(orig->aalg);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001122 if (!x->aalg)
1123 goto error;
1124 }
1125 x->props.aalgo = orig->props.aalgo;
1126
1127 if (orig->ealg) {
1128 x->ealg = xfrm_algo_clone(orig->ealg);
1129 if (!x->ealg)
1130 goto error;
1131 }
1132 x->props.ealgo = orig->props.ealgo;
1133
1134 if (orig->calg) {
1135 x->calg = xfrm_algo_clone(orig->calg);
1136 if (!x->calg)
1137 goto error;
1138 }
1139 x->props.calgo = orig->props.calgo;
1140
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001141 if (orig->encap) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001142 x->encap = kmemdup(orig->encap, sizeof(*x->encap), GFP_KERNEL);
1143 if (!x->encap)
1144 goto error;
1145 }
1146
1147 if (orig->coaddr) {
1148 x->coaddr = kmemdup(orig->coaddr, sizeof(*x->coaddr),
1149 GFP_KERNEL);
1150 if (!x->coaddr)
1151 goto error;
1152 }
1153
1154 err = xfrm_init_state(x);
1155 if (err)
1156 goto error;
1157
1158 x->props.flags = orig->props.flags;
1159
1160 x->curlft.add_time = orig->curlft.add_time;
1161 x->km.state = orig->km.state;
1162 x->km.seq = orig->km.seq;
1163
1164 return x;
1165
1166 error:
Herbert Xu553f9112010-02-15 20:00:51 +00001167 xfrm_state_put(x);
1168out:
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001169 if (errp)
1170 *errp = err;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001171 return NULL;
1172}
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001173
1174/* xfrm_state_lock is held */
1175struct xfrm_state * xfrm_migrate_state_find(struct xfrm_migrate *m)
1176{
1177 unsigned int h;
1178 struct xfrm_state *x;
1179 struct hlist_node *entry;
1180
1181 if (m->reqid) {
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -08001182 h = xfrm_dst_hash(&init_net, &m->old_daddr, &m->old_saddr,
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001183 m->reqid, m->old_family);
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08001184 hlist_for_each_entry(x, entry, init_net.xfrm.state_bydst+h, bydst) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001185 if (x->props.mode != m->mode ||
1186 x->id.proto != m->proto)
1187 continue;
1188 if (m->reqid && x->props.reqid != m->reqid)
1189 continue;
1190 if (xfrm_addr_cmp(&x->id.daddr, &m->old_daddr,
1191 m->old_family) ||
1192 xfrm_addr_cmp(&x->props.saddr, &m->old_saddr,
1193 m->old_family))
1194 continue;
1195 xfrm_state_hold(x);
1196 return x;
1197 }
1198 } else {
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -08001199 h = xfrm_src_hash(&init_net, &m->old_daddr, &m->old_saddr,
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001200 m->old_family);
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08001201 hlist_for_each_entry(x, entry, init_net.xfrm.state_bysrc+h, bysrc) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001202 if (x->props.mode != m->mode ||
1203 x->id.proto != m->proto)
1204 continue;
1205 if (xfrm_addr_cmp(&x->id.daddr, &m->old_daddr,
1206 m->old_family) ||
1207 xfrm_addr_cmp(&x->props.saddr, &m->old_saddr,
1208 m->old_family))
1209 continue;
1210 xfrm_state_hold(x);
1211 return x;
1212 }
1213 }
1214
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001215 return NULL;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001216}
1217EXPORT_SYMBOL(xfrm_migrate_state_find);
1218
1219struct xfrm_state * xfrm_state_migrate(struct xfrm_state *x,
1220 struct xfrm_migrate *m)
1221{
1222 struct xfrm_state *xc;
1223 int err;
1224
1225 xc = xfrm_state_clone(x, &err);
1226 if (!xc)
1227 return NULL;
1228
1229 memcpy(&xc->id.daddr, &m->new_daddr, sizeof(xc->id.daddr));
1230 memcpy(&xc->props.saddr, &m->new_saddr, sizeof(xc->props.saddr));
1231
1232 /* add state */
1233 if (!xfrm_addr_cmp(&x->id.daddr, &m->new_daddr, m->new_family)) {
1234 /* a care is needed when the destination address of the
1235 state is to be updated as it is a part of triplet */
1236 xfrm_state_insert(xc);
1237 } else {
1238 if ((err = xfrm_state_add(xc)) < 0)
1239 goto error;
1240 }
1241
1242 return xc;
1243error:
1244 kfree(xc);
1245 return NULL;
1246}
1247EXPORT_SYMBOL(xfrm_state_migrate);
1248#endif
1249
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250int xfrm_state_update(struct xfrm_state *x)
1251{
David S. Miller37b08e32008-09-02 20:14:15 -07001252 struct xfrm_state *x1, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 int err;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001254 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
David S. Miller37b08e32008-09-02 20:14:15 -07001256 to_put = NULL;
1257
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 spin_lock_bh(&xfrm_state_lock);
David S. Milleredcd5822006-08-24 00:42:45 -07001259 x1 = __xfrm_state_locate(x, use_spi, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
1261 err = -ESRCH;
1262 if (!x1)
1263 goto out;
1264
1265 if (xfrm_state_kern(x1)) {
David S. Miller37b08e32008-09-02 20:14:15 -07001266 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 err = -EEXIST;
1268 goto out;
1269 }
1270
1271 if (x1->km.state == XFRM_STATE_ACQ) {
1272 __xfrm_state_insert(x);
1273 x = NULL;
1274 }
1275 err = 0;
1276
1277out:
1278 spin_unlock_bh(&xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
David S. Miller37b08e32008-09-02 20:14:15 -07001280 if (to_put)
1281 xfrm_state_put(to_put);
1282
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 if (err)
1284 return err;
1285
1286 if (!x) {
1287 xfrm_state_delete(x1);
1288 xfrm_state_put(x1);
1289 return 0;
1290 }
1291
1292 err = -EINVAL;
1293 spin_lock_bh(&x1->lock);
1294 if (likely(x1->km.state == XFRM_STATE_VALID)) {
1295 if (x->encap && x1->encap)
1296 memcpy(x1->encap, x->encap, sizeof(*x1->encap));
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -07001297 if (x->coaddr && x1->coaddr) {
1298 memcpy(x1->coaddr, x->coaddr, sizeof(*x1->coaddr));
1299 }
1300 if (!use_spi && memcmp(&x1->sel, &x->sel, sizeof(x1->sel)))
1301 memcpy(&x1->sel, &x->sel, sizeof(x1->sel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 memcpy(&x1->lft, &x->lft, sizeof(x1->lft));
1303 x1->km.dying = 0;
1304
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -08001305 tasklet_hrtimer_start(&x1->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 if (x1->curlft.use_time)
1307 xfrm_state_check_expire(x1);
1308
1309 err = 0;
1310 }
1311 spin_unlock_bh(&x1->lock);
1312
1313 xfrm_state_put(x1);
1314
1315 return err;
1316}
1317EXPORT_SYMBOL(xfrm_state_update);
1318
1319int xfrm_state_check_expire(struct xfrm_state *x)
1320{
1321 if (!x->curlft.use_time)
James Morris9d729f72007-03-04 16:12:44 -08001322 x->curlft.use_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
1324 if (x->km.state != XFRM_STATE_VALID)
1325 return -EINVAL;
1326
1327 if (x->curlft.bytes >= x->lft.hard_byte_limit ||
1328 x->curlft.packets >= x->lft.hard_packet_limit) {
Herbert Xu4666faa2005-06-18 22:43:22 -07001329 x->km.state = XFRM_STATE_EXPIRED;
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -08001330 tasklet_hrtimer_start(&x->mtimer, ktime_set(0,0), HRTIMER_MODE_REL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 return -EINVAL;
1332 }
1333
1334 if (!x->km.dying &&
1335 (x->curlft.bytes >= x->lft.soft_byte_limit ||
Herbert Xu4666faa2005-06-18 22:43:22 -07001336 x->curlft.packets >= x->lft.soft_packet_limit)) {
1337 x->km.dying = 1;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001338 km_state_expired(x, 0, 0);
Herbert Xu4666faa2005-06-18 22:43:22 -07001339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 return 0;
1341}
1342EXPORT_SYMBOL(xfrm_state_check_expire);
1343
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344struct xfrm_state *
Alexey Dobriyan221df1e2008-11-25 17:30:50 -08001345xfrm_state_lookup(struct net *net, xfrm_address_t *daddr, __be32 spi, u8 proto,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 unsigned short family)
1347{
1348 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
1350 spin_lock_bh(&xfrm_state_lock);
Alexey Dobriyan221df1e2008-11-25 17:30:50 -08001351 x = __xfrm_state_lookup(net, daddr, spi, proto, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 spin_unlock_bh(&xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 return x;
1354}
1355EXPORT_SYMBOL(xfrm_state_lookup);
1356
1357struct xfrm_state *
Alexey Dobriyan221df1e2008-11-25 17:30:50 -08001358xfrm_state_lookup_byaddr(struct net *net,
1359 xfrm_address_t *daddr, xfrm_address_t *saddr,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001360 u8 proto, unsigned short family)
1361{
1362 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001363
1364 spin_lock_bh(&xfrm_state_lock);
Alexey Dobriyan221df1e2008-11-25 17:30:50 -08001365 x = __xfrm_state_lookup_byaddr(net, daddr, saddr, proto, family);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001366 spin_unlock_bh(&xfrm_state_lock);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001367 return x;
1368}
1369EXPORT_SYMBOL(xfrm_state_lookup_byaddr);
1370
1371struct xfrm_state *
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001372xfrm_find_acq(struct net *net, u8 mode, u32 reqid, u8 proto,
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001373 xfrm_address_t *daddr, xfrm_address_t *saddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 int create, unsigned short family)
1375{
1376 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
1378 spin_lock_bh(&xfrm_state_lock);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001379 x = __find_acq_core(net, family, mode, reqid, proto, daddr, saddr, create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 spin_unlock_bh(&xfrm_state_lock);
David S. Miller27708342006-08-24 00:13:10 -07001381
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 return x;
1383}
1384EXPORT_SYMBOL(xfrm_find_acq);
1385
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001386#ifdef CONFIG_XFRM_SUB_POLICY
1387int
1388xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n,
1389 unsigned short family)
1390{
1391 int err = 0;
1392 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
1393 if (!afinfo)
1394 return -EAFNOSUPPORT;
1395
1396 spin_lock_bh(&xfrm_state_lock);
1397 if (afinfo->tmpl_sort)
1398 err = afinfo->tmpl_sort(dst, src, n);
1399 spin_unlock_bh(&xfrm_state_lock);
1400 xfrm_state_put_afinfo(afinfo);
1401 return err;
1402}
1403EXPORT_SYMBOL(xfrm_tmpl_sort);
1404
1405int
1406xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src, int n,
1407 unsigned short family)
1408{
1409 int err = 0;
1410 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
1411 if (!afinfo)
1412 return -EAFNOSUPPORT;
1413
1414 spin_lock_bh(&xfrm_state_lock);
1415 if (afinfo->state_sort)
1416 err = afinfo->state_sort(dst, src, n);
1417 spin_unlock_bh(&xfrm_state_lock);
1418 xfrm_state_put_afinfo(afinfo);
1419 return err;
1420}
1421EXPORT_SYMBOL(xfrm_state_sort);
1422#endif
1423
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424/* Silly enough, but I'm lazy to build resolution list */
1425
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001426static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427{
1428 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001430 for (i = 0; i <= net->xfrm.state_hmask; i++) {
David S. Miller8f126e32006-08-24 02:45:07 -07001431 struct hlist_node *entry;
1432 struct xfrm_state *x;
1433
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001434 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+i, bydst) {
David S. Miller8f126e32006-08-24 02:45:07 -07001435 if (x->km.seq == seq &&
1436 x->km.state == XFRM_STATE_ACQ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 xfrm_state_hold(x);
1438 return x;
1439 }
1440 }
1441 }
1442 return NULL;
1443}
1444
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001445struct xfrm_state *xfrm_find_acq_byseq(struct net *net, u32 seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446{
1447 struct xfrm_state *x;
1448
1449 spin_lock_bh(&xfrm_state_lock);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001450 x = __xfrm_find_acq_byseq(net, seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 spin_unlock_bh(&xfrm_state_lock);
1452 return x;
1453}
1454EXPORT_SYMBOL(xfrm_find_acq_byseq);
1455
1456u32 xfrm_get_acqseq(void)
1457{
1458 u32 res;
jamal6836b9b2010-02-16 02:01:22 +00001459 static atomic_t acqseq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
jamal6836b9b2010-02-16 02:01:22 +00001461 do {
1462 res = atomic_inc_return(&acqseq);
1463 } while (!res);
1464
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 return res;
1466}
1467EXPORT_SYMBOL(xfrm_get_acqseq);
1468
Herbert Xu658b2192007-10-09 13:29:52 -07001469int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -08001471 struct net *net = xs_net(x);
David S. Millerf034b5d2006-08-24 03:08:07 -07001472 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 struct xfrm_state *x0;
Herbert Xu658b2192007-10-09 13:29:52 -07001474 int err = -ENOENT;
1475 __be32 minspi = htonl(low);
1476 __be32 maxspi = htonl(high);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
Herbert Xu658b2192007-10-09 13:29:52 -07001478 spin_lock_bh(&x->lock);
1479 if (x->km.state == XFRM_STATE_DEAD)
1480 goto unlock;
1481
1482 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 if (x->id.spi)
Herbert Xu658b2192007-10-09 13:29:52 -07001484 goto unlock;
1485
1486 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
1488 if (minspi == maxspi) {
Alexey Dobriyan221df1e2008-11-25 17:30:50 -08001489 x0 = xfrm_state_lookup(net, &x->id.daddr, minspi, x->id.proto, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 if (x0) {
1491 xfrm_state_put(x0);
Herbert Xu658b2192007-10-09 13:29:52 -07001492 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 }
1494 x->id.spi = minspi;
1495 } else {
1496 u32 spi = 0;
Al Viro26977b42006-09-27 18:47:05 -07001497 for (h=0; h<high-low+1; h++) {
1498 spi = low + net_random()%(high-low+1);
Alexey Dobriyan221df1e2008-11-25 17:30:50 -08001499 x0 = xfrm_state_lookup(net, &x->id.daddr, htonl(spi), x->id.proto, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 if (x0 == NULL) {
1501 x->id.spi = htonl(spi);
1502 break;
1503 }
1504 xfrm_state_put(x0);
1505 }
1506 }
1507 if (x->id.spi) {
1508 spin_lock_bh(&xfrm_state_lock);
Alexey Dobriyan12604d82008-11-25 17:31:18 -08001509 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, x->props.family);
1510 hlist_add_head(&x->byspi, net->xfrm.state_byspi+h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 spin_unlock_bh(&xfrm_state_lock);
Herbert Xu658b2192007-10-09 13:29:52 -07001512
1513 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 }
Herbert Xu658b2192007-10-09 13:29:52 -07001515
1516unlock:
1517 spin_unlock_bh(&x->lock);
1518
1519 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520}
1521EXPORT_SYMBOL(xfrm_alloc_spi);
1522
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08001523int xfrm_state_walk(struct net *net, struct xfrm_state_walk *walk,
Timo Teras4c563f72008-02-28 21:31:08 -08001524 int (*func)(struct xfrm_state *, int, void*),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 void *data)
1526{
Herbert Xu12a169e2008-10-01 07:03:24 -07001527 struct xfrm_state *state;
1528 struct xfrm_state_walk *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 int err = 0;
1530
Herbert Xu12a169e2008-10-01 07:03:24 -07001531 if (walk->seq != 0 && list_empty(&walk->all))
Timo Teras4c563f72008-02-28 21:31:08 -08001532 return 0;
1533
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 spin_lock_bh(&xfrm_state_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001535 if (list_empty(&walk->all))
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08001536 x = list_first_entry(&net->xfrm.state_all, struct xfrm_state_walk, all);
Herbert Xu12a169e2008-10-01 07:03:24 -07001537 else
1538 x = list_entry(&walk->all, struct xfrm_state_walk, all);
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08001539 list_for_each_entry_from(x, &net->xfrm.state_all, all) {
Herbert Xu12a169e2008-10-01 07:03:24 -07001540 if (x->state == XFRM_STATE_DEAD)
Timo Teras4c563f72008-02-28 21:31:08 -08001541 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07001542 state = container_of(x, struct xfrm_state, km);
1543 if (!xfrm_id_proto_match(state->id.proto, walk->proto))
Timo Teras4c563f72008-02-28 21:31:08 -08001544 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07001545 err = func(state, walk->seq, data);
1546 if (err) {
1547 list_move_tail(&walk->all, &x->all);
1548 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001550 walk->seq++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001552 if (walk->seq == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 err = -ENOENT;
1554 goto out;
1555 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001556 list_del_init(&walk->all);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557out:
1558 spin_unlock_bh(&xfrm_state_lock);
1559 return err;
1560}
1561EXPORT_SYMBOL(xfrm_state_walk);
1562
Herbert Xu5c182452008-09-22 19:48:19 -07001563void xfrm_state_walk_init(struct xfrm_state_walk *walk, u8 proto)
1564{
Herbert Xu12a169e2008-10-01 07:03:24 -07001565 INIT_LIST_HEAD(&walk->all);
Herbert Xu5c182452008-09-22 19:48:19 -07001566 walk->proto = proto;
Herbert Xu12a169e2008-10-01 07:03:24 -07001567 walk->state = XFRM_STATE_DEAD;
1568 walk->seq = 0;
Herbert Xu5c182452008-09-22 19:48:19 -07001569}
1570EXPORT_SYMBOL(xfrm_state_walk_init);
1571
Herbert Xuabb81c42008-09-09 19:58:29 -07001572void xfrm_state_walk_done(struct xfrm_state_walk *walk)
1573{
Herbert Xu12a169e2008-10-01 07:03:24 -07001574 if (list_empty(&walk->all))
Herbert Xu5c182452008-09-22 19:48:19 -07001575 return;
Herbert Xu5c182452008-09-22 19:48:19 -07001576
Herbert Xu12a169e2008-10-01 07:03:24 -07001577 spin_lock_bh(&xfrm_state_lock);
1578 list_del(&walk->all);
Chuck Ebbert7d0b5912009-03-27 00:22:01 -07001579 spin_unlock_bh(&xfrm_state_lock);
Herbert Xuabb81c42008-09-09 19:58:29 -07001580}
1581EXPORT_SYMBOL(xfrm_state_walk_done);
1582
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001583
1584void xfrm_replay_notify(struct xfrm_state *x, int event)
1585{
1586 struct km_event c;
1587 /* we send notify messages in case
1588 * 1. we updated on of the sequence numbers, and the seqno difference
1589 * is at least x->replay_maxdiff, in this case we also update the
1590 * timeout of our timer function
1591 * 2. if x->replay_maxage has elapsed since last update,
1592 * and there were changes
1593 *
1594 * The state structure must be locked!
1595 */
1596
1597 switch (event) {
1598 case XFRM_REPLAY_UPDATE:
1599 if (x->replay_maxdiff &&
1600 (x->replay.seq - x->preplay.seq < x->replay_maxdiff) &&
Jamal Hadi Salim27170962006-04-14 15:03:05 -07001601 (x->replay.oseq - x->preplay.oseq < x->replay_maxdiff)) {
1602 if (x->xflags & XFRM_TIME_DEFER)
1603 event = XFRM_REPLAY_TIMEOUT;
1604 else
1605 return;
1606 }
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001607
1608 break;
1609
1610 case XFRM_REPLAY_TIMEOUT:
1611 if ((x->replay.seq == x->preplay.seq) &&
1612 (x->replay.bitmap == x->preplay.bitmap) &&
Jamal Hadi Salim27170962006-04-14 15:03:05 -07001613 (x->replay.oseq == x->preplay.oseq)) {
1614 x->xflags |= XFRM_TIME_DEFER;
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001615 return;
Jamal Hadi Salim27170962006-04-14 15:03:05 -07001616 }
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001617
1618 break;
1619 }
1620
1621 memcpy(&x->preplay, &x->replay, sizeof(struct xfrm_replay_state));
1622 c.event = XFRM_MSG_NEWAE;
1623 c.data.aevent = event;
1624 km_state_notify(x, &c);
1625
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001626 if (x->replay_maxage &&
David S. Millera47f0ce2006-08-24 03:54:22 -07001627 !mod_timer(&x->rtimer, jiffies + x->replay_maxage))
Jamal Hadi Salim27170962006-04-14 15:03:05 -07001628 x->xflags &= ~XFRM_TIME_DEFER;
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001629}
1630
1631static void xfrm_replay_timer_handler(unsigned long data)
1632{
1633 struct xfrm_state *x = (struct xfrm_state*)data;
1634
1635 spin_lock(&x->lock);
1636
Jamal Hadi Salim27170962006-04-14 15:03:05 -07001637 if (x->km.state == XFRM_STATE_VALID) {
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001638 if (xfrm_aevent_is_on(xs_net(x)))
Jamal Hadi Salim27170962006-04-14 15:03:05 -07001639 xfrm_replay_notify(x, XFRM_REPLAY_TIMEOUT);
1640 else
1641 x->xflags |= XFRM_TIME_DEFER;
1642 }
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001643
1644 spin_unlock(&x->lock);
1645}
1646
Paul Mooreafeb14b2007-12-21 14:58:11 -08001647int xfrm_replay_check(struct xfrm_state *x,
1648 struct sk_buff *skb, __be32 net_seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649{
1650 u32 diff;
Al Viroa252cc22006-09-27 18:48:18 -07001651 u32 seq = ntohl(net_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
1653 if (unlikely(seq == 0))
Paul Mooreafeb14b2007-12-21 14:58:11 -08001654 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655
1656 if (likely(seq > x->replay.seq))
1657 return 0;
1658
1659 diff = x->replay.seq - seq;
Herbert Xu4c4d51a72007-04-05 00:07:39 -07001660 if (diff >= min_t(unsigned int, x->props.replay_window,
1661 sizeof(x->replay.bitmap) * 8)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 x->stats.replay_window++;
Paul Mooreafeb14b2007-12-21 14:58:11 -08001663 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 }
1665
1666 if (x->replay.bitmap & (1U << diff)) {
1667 x->stats.replay++;
Paul Mooreafeb14b2007-12-21 14:58:11 -08001668 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 }
1670 return 0;
Paul Mooreafeb14b2007-12-21 14:58:11 -08001671
1672err:
1673 xfrm_audit_state_replay(x, skb, net_seq);
1674 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
Al Viro61f46272006-09-27 18:48:33 -07001677void xfrm_replay_advance(struct xfrm_state *x, __be32 net_seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678{
1679 u32 diff;
Al Viro61f46272006-09-27 18:48:33 -07001680 u32 seq = ntohl(net_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
1682 if (seq > x->replay.seq) {
1683 diff = seq - x->replay.seq;
1684 if (diff < x->props.replay_window)
1685 x->replay.bitmap = ((x->replay.bitmap) << diff) | 1;
1686 else
1687 x->replay.bitmap = 1;
1688 x->replay.seq = seq;
1689 } else {
1690 diff = x->replay.seq - seq;
1691 x->replay.bitmap |= (1U << diff);
1692 }
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001693
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001694 if (xfrm_aevent_is_on(xs_net(x)))
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001695 xfrm_replay_notify(x, XFRM_REPLAY_UPDATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697
Denis Chengdf018122007-12-07 00:51:11 -08001698static LIST_HEAD(xfrm_km_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699static DEFINE_RWLOCK(xfrm_km_lock);
1700
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001701void km_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702{
1703 struct xfrm_mgr *km;
1704
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001705 read_lock(&xfrm_km_lock);
1706 list_for_each_entry(km, &xfrm_km_list, list)
1707 if (km->notify_policy)
1708 km->notify_policy(xp, dir, c);
1709 read_unlock(&xfrm_km_lock);
1710}
1711
1712void km_state_notify(struct xfrm_state *x, struct km_event *c)
1713{
1714 struct xfrm_mgr *km;
1715 read_lock(&xfrm_km_lock);
1716 list_for_each_entry(km, &xfrm_km_list, list)
1717 if (km->notify)
1718 km->notify(x, c);
1719 read_unlock(&xfrm_km_lock);
1720}
1721
1722EXPORT_SYMBOL(km_policy_notify);
1723EXPORT_SYMBOL(km_state_notify);
1724
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001725void km_state_expired(struct xfrm_state *x, int hard, u32 pid)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001726{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001727 struct net *net = xs_net(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001728 struct km_event c;
1729
Herbert Xubf088672005-06-18 22:44:00 -07001730 c.data.hard = hard;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001731 c.pid = pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001732 c.event = XFRM_MSG_EXPIRE;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001733 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734
1735 if (hard)
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001736 wake_up(&net->xfrm.km_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737}
1738
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001739EXPORT_SYMBOL(km_state_expired);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001740/*
1741 * We send to all registered managers regardless of failure
1742 * We are happy with one success
1743*/
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001744int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001746 int err = -EINVAL, acqret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 struct xfrm_mgr *km;
1748
1749 read_lock(&xfrm_km_lock);
1750 list_for_each_entry(km, &xfrm_km_list, list) {
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001751 acqret = km->acquire(x, t, pol, XFRM_POLICY_OUT);
1752 if (!acqret)
1753 err = acqret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 }
1755 read_unlock(&xfrm_km_lock);
1756 return err;
1757}
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001758EXPORT_SYMBOL(km_query);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759
Al Viro5d36b182006-11-08 00:24:06 -08001760int km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761{
1762 int err = -EINVAL;
1763 struct xfrm_mgr *km;
1764
1765 read_lock(&xfrm_km_lock);
1766 list_for_each_entry(km, &xfrm_km_list, list) {
1767 if (km->new_mapping)
1768 err = km->new_mapping(x, ipaddr, sport);
1769 if (!err)
1770 break;
1771 }
1772 read_unlock(&xfrm_km_lock);
1773 return err;
1774}
1775EXPORT_SYMBOL(km_new_mapping);
1776
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001777void km_policy_expired(struct xfrm_policy *pol, int dir, int hard, u32 pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001779 struct net *net = xp_net(pol);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001780 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
Herbert Xubf088672005-06-18 22:44:00 -07001782 c.data.hard = hard;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001783 c.pid = pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001784 c.event = XFRM_MSG_POLEXPIRE;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001785 km_policy_notify(pol, dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
1787 if (hard)
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001788 wake_up(&net->xfrm.km_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789}
David S. Millera70fcb02006-03-20 19:18:52 -08001790EXPORT_SYMBOL(km_policy_expired);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
Eric Dumazet2d60abc2008-01-03 20:43:21 -08001792#ifdef CONFIG_XFRM_MIGRATE
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001793int km_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001794 struct xfrm_migrate *m, int num_migrate,
1795 struct xfrm_kmaddress *k)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001796{
1797 int err = -EINVAL;
1798 int ret;
1799 struct xfrm_mgr *km;
1800
1801 read_lock(&xfrm_km_lock);
1802 list_for_each_entry(km, &xfrm_km_list, list) {
1803 if (km->migrate) {
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001804 ret = km->migrate(sel, dir, type, m, num_migrate, k);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001805 if (!ret)
1806 err = ret;
1807 }
1808 }
1809 read_unlock(&xfrm_km_lock);
1810 return err;
1811}
1812EXPORT_SYMBOL(km_migrate);
Eric Dumazet2d60abc2008-01-03 20:43:21 -08001813#endif
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001814
Alexey Dobriyandb983c12008-11-25 17:51:01 -08001815int km_report(struct net *net, u8 proto, struct xfrm_selector *sel, xfrm_address_t *addr)
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001816{
1817 int err = -EINVAL;
1818 int ret;
1819 struct xfrm_mgr *km;
1820
1821 read_lock(&xfrm_km_lock);
1822 list_for_each_entry(km, &xfrm_km_list, list) {
1823 if (km->report) {
Alexey Dobriyandb983c12008-11-25 17:51:01 -08001824 ret = km->report(net, proto, sel, addr);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001825 if (!ret)
1826 err = ret;
1827 }
1828 }
1829 read_unlock(&xfrm_km_lock);
1830 return err;
1831}
1832EXPORT_SYMBOL(km_report);
1833
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834int xfrm_user_policy(struct sock *sk, int optname, u8 __user *optval, int optlen)
1835{
1836 int err;
1837 u8 *data;
1838 struct xfrm_mgr *km;
1839 struct xfrm_policy *pol = NULL;
1840
1841 if (optlen <= 0 || optlen > PAGE_SIZE)
1842 return -EMSGSIZE;
1843
1844 data = kmalloc(optlen, GFP_KERNEL);
1845 if (!data)
1846 return -ENOMEM;
1847
1848 err = -EFAULT;
1849 if (copy_from_user(data, optval, optlen))
1850 goto out;
1851
1852 err = -EINVAL;
1853 read_lock(&xfrm_km_lock);
1854 list_for_each_entry(km, &xfrm_km_list, list) {
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07001855 pol = km->compile_policy(sk, optname, data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 optlen, &err);
1857 if (err >= 0)
1858 break;
1859 }
1860 read_unlock(&xfrm_km_lock);
1861
1862 if (err >= 0) {
1863 xfrm_sk_policy_insert(sk, err, pol);
1864 xfrm_pol_put(pol);
1865 err = 0;
1866 }
1867
1868out:
1869 kfree(data);
1870 return err;
1871}
1872EXPORT_SYMBOL(xfrm_user_policy);
1873
1874int xfrm_register_km(struct xfrm_mgr *km)
1875{
1876 write_lock_bh(&xfrm_km_lock);
1877 list_add_tail(&km->list, &xfrm_km_list);
1878 write_unlock_bh(&xfrm_km_lock);
1879 return 0;
1880}
1881EXPORT_SYMBOL(xfrm_register_km);
1882
1883int xfrm_unregister_km(struct xfrm_mgr *km)
1884{
1885 write_lock_bh(&xfrm_km_lock);
1886 list_del(&km->list);
1887 write_unlock_bh(&xfrm_km_lock);
1888 return 0;
1889}
1890EXPORT_SYMBOL(xfrm_unregister_km);
1891
1892int xfrm_state_register_afinfo(struct xfrm_state_afinfo *afinfo)
1893{
1894 int err = 0;
1895 if (unlikely(afinfo == NULL))
1896 return -EINVAL;
1897 if (unlikely(afinfo->family >= NPROTO))
1898 return -EAFNOSUPPORT;
Ingo Molnarf3111502006-04-28 15:30:03 -07001899 write_lock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 if (unlikely(xfrm_state_afinfo[afinfo->family] != NULL))
1901 err = -ENOBUFS;
David S. Milleredcd5822006-08-24 00:42:45 -07001902 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 xfrm_state_afinfo[afinfo->family] = afinfo;
Ingo Molnarf3111502006-04-28 15:30:03 -07001904 write_unlock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 return err;
1906}
1907EXPORT_SYMBOL(xfrm_state_register_afinfo);
1908
1909int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo)
1910{
1911 int err = 0;
1912 if (unlikely(afinfo == NULL))
1913 return -EINVAL;
1914 if (unlikely(afinfo->family >= NPROTO))
1915 return -EAFNOSUPPORT;
Ingo Molnarf3111502006-04-28 15:30:03 -07001916 write_lock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 if (likely(xfrm_state_afinfo[afinfo->family] != NULL)) {
1918 if (unlikely(xfrm_state_afinfo[afinfo->family] != afinfo))
1919 err = -EINVAL;
David S. Milleredcd5822006-08-24 00:42:45 -07001920 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 xfrm_state_afinfo[afinfo->family] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 }
Ingo Molnarf3111502006-04-28 15:30:03 -07001923 write_unlock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 return err;
1925}
1926EXPORT_SYMBOL(xfrm_state_unregister_afinfo);
1927
Herbert Xu17c2a422007-10-17 21:33:12 -07001928static struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929{
1930 struct xfrm_state_afinfo *afinfo;
1931 if (unlikely(family >= NPROTO))
1932 return NULL;
1933 read_lock(&xfrm_state_afinfo_lock);
1934 afinfo = xfrm_state_afinfo[family];
Herbert Xu546be242006-05-27 23:03:58 -07001935 if (unlikely(!afinfo))
1936 read_unlock(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 return afinfo;
1938}
1939
Herbert Xu17c2a422007-10-17 21:33:12 -07001940static void xfrm_state_put_afinfo(struct xfrm_state_afinfo *afinfo)
Eric Dumazet9a429c42008-01-01 21:58:02 -08001941 __releases(xfrm_state_afinfo_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942{
Herbert Xu546be242006-05-27 23:03:58 -07001943 read_unlock(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944}
1945
1946/* Temporarily located here until net/xfrm/xfrm_tunnel.c is created */
1947void xfrm_state_delete_tunnel(struct xfrm_state *x)
1948{
1949 if (x->tunnel) {
1950 struct xfrm_state *t = x->tunnel;
1951
1952 if (atomic_read(&t->tunnel_users) == 2)
1953 xfrm_state_delete(t);
1954 atomic_dec(&t->tunnel_users);
1955 xfrm_state_put(t);
1956 x->tunnel = NULL;
1957 }
1958}
1959EXPORT_SYMBOL(xfrm_state_delete_tunnel);
1960
1961int xfrm_state_mtu(struct xfrm_state *x, int mtu)
1962{
Patrick McHardyc5c25232007-04-09 11:47:18 -07001963 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964
Patrick McHardyc5c25232007-04-09 11:47:18 -07001965 spin_lock_bh(&x->lock);
1966 if (x->km.state == XFRM_STATE_VALID &&
1967 x->type && x->type->get_mtu)
1968 res = x->type->get_mtu(x, mtu);
1969 else
Patrick McHardy28121612007-06-18 22:30:15 -07001970 res = mtu - x->props.header_len;
Patrick McHardyc5c25232007-04-09 11:47:18 -07001971 spin_unlock_bh(&x->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 return res;
1973}
1974
Herbert Xu72cb6962005-06-20 13:18:08 -07001975int xfrm_init_state(struct xfrm_state *x)
1976{
Herbert Xud094cd82005-06-20 13:19:41 -07001977 struct xfrm_state_afinfo *afinfo;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07001978 struct xfrm_mode *inner_mode;
Herbert Xud094cd82005-06-20 13:19:41 -07001979 int family = x->props.family;
Herbert Xu72cb6962005-06-20 13:18:08 -07001980 int err;
1981
Herbert Xud094cd82005-06-20 13:19:41 -07001982 err = -EAFNOSUPPORT;
1983 afinfo = xfrm_state_get_afinfo(family);
1984 if (!afinfo)
1985 goto error;
1986
1987 err = 0;
1988 if (afinfo->init_flags)
1989 err = afinfo->init_flags(x);
1990
1991 xfrm_state_put_afinfo(afinfo);
1992
1993 if (err)
1994 goto error;
1995
1996 err = -EPROTONOSUPPORT;
Herbert Xu13996372007-10-17 21:35:51 -07001997
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07001998 if (x->sel.family != AF_UNSPEC) {
1999 inner_mode = xfrm_get_mode(x->props.mode, x->sel.family);
2000 if (inner_mode == NULL)
2001 goto error;
2002
2003 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL) &&
2004 family != x->sel.family) {
2005 xfrm_put_mode(inner_mode);
2006 goto error;
2007 }
2008
2009 x->inner_mode = inner_mode;
2010 } else {
2011 struct xfrm_mode *inner_mode_iaf;
Martin Willid81d2282008-12-03 15:38:07 -08002012 int iafamily = AF_INET;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002013
Martin Willid81d2282008-12-03 15:38:07 -08002014 inner_mode = xfrm_get_mode(x->props.mode, x->props.family);
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002015 if (inner_mode == NULL)
2016 goto error;
2017
2018 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL)) {
2019 xfrm_put_mode(inner_mode);
2020 goto error;
2021 }
Martin Willid81d2282008-12-03 15:38:07 -08002022 x->inner_mode = inner_mode;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002023
Martin Willid81d2282008-12-03 15:38:07 -08002024 if (x->props.family == AF_INET)
2025 iafamily = AF_INET6;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002026
Martin Willid81d2282008-12-03 15:38:07 -08002027 inner_mode_iaf = xfrm_get_mode(x->props.mode, iafamily);
2028 if (inner_mode_iaf) {
2029 if (inner_mode_iaf->flags & XFRM_MODE_FLAG_TUNNEL)
2030 x->inner_mode_iaf = inner_mode_iaf;
2031 else
2032 xfrm_put_mode(inner_mode_iaf);
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002033 }
2034 }
Herbert Xu13996372007-10-17 21:35:51 -07002035
Herbert Xud094cd82005-06-20 13:19:41 -07002036 x->type = xfrm_get_type(x->id.proto, family);
Herbert Xu72cb6962005-06-20 13:18:08 -07002037 if (x->type == NULL)
2038 goto error;
2039
2040 err = x->type->init_state(x);
2041 if (err)
2042 goto error;
2043
Herbert Xu13996372007-10-17 21:35:51 -07002044 x->outer_mode = xfrm_get_mode(x->props.mode, family);
2045 if (x->outer_mode == NULL)
Herbert Xub59f45d2006-05-27 23:05:54 -07002046 goto error;
2047
Herbert Xu72cb6962005-06-20 13:18:08 -07002048 x->km.state = XFRM_STATE_VALID;
2049
2050error:
2051 return err;
2052}
2053
2054EXPORT_SYMBOL(xfrm_init_state);
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09002055
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002056int __net_init xfrm_state_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057{
David S. Millerf034b5d2006-08-24 03:08:07 -07002058 unsigned int sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059
Alexey Dobriyan9d4139c2008-11-25 17:16:11 -08002060 INIT_LIST_HEAD(&net->xfrm.state_all);
2061
David S. Millerf034b5d2006-08-24 03:08:07 -07002062 sz = sizeof(struct hlist_head) * 8;
2063
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002064 net->xfrm.state_bydst = xfrm_hash_alloc(sz);
2065 if (!net->xfrm.state_bydst)
2066 goto out_bydst;
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002067 net->xfrm.state_bysrc = xfrm_hash_alloc(sz);
2068 if (!net->xfrm.state_bysrc)
2069 goto out_bysrc;
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002070 net->xfrm.state_byspi = xfrm_hash_alloc(sz);
2071 if (!net->xfrm.state_byspi)
2072 goto out_byspi;
Alexey Dobriyan529983e2008-11-25 17:18:12 -08002073 net->xfrm.state_hmask = ((sz / sizeof(struct hlist_head)) - 1);
David S. Millerf034b5d2006-08-24 03:08:07 -07002074
Alexey Dobriyan0bf7c5b2008-11-25 17:18:39 -08002075 net->xfrm.state_num = 0;
Alexey Dobriyan63082732008-11-25 17:19:07 -08002076 INIT_WORK(&net->xfrm.state_hash_work, xfrm_hash_resize);
Alexey Dobriyanb8a0ae22008-11-25 17:20:11 -08002077 INIT_HLIST_HEAD(&net->xfrm.state_gc_list);
Alexey Dobriyanc7837142008-11-25 17:20:36 -08002078 INIT_WORK(&net->xfrm.state_gc_work, xfrm_state_gc_task);
Alexey Dobriyan50a30652008-11-25 17:21:01 -08002079 init_waitqueue_head(&net->xfrm.km_waitq);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002080 return 0;
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002081
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002082out_byspi:
2083 xfrm_hash_free(net->xfrm.state_bysrc, sz);
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002084out_bysrc:
2085 xfrm_hash_free(net->xfrm.state_bydst, sz);
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002086out_bydst:
2087 return -ENOMEM;
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002088}
2089
2090void xfrm_state_fini(struct net *net)
2091{
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002092 struct xfrm_audit audit_info;
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002093 unsigned int sz;
2094
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002095 flush_work(&net->xfrm.state_hash_work);
2096 audit_info.loginuid = -1;
2097 audit_info.sessionid = -1;
2098 audit_info.secid = 0;
2099 xfrm_state_flush(net, IPSEC_PROTO_ANY, &audit_info);
2100 flush_work(&net->xfrm.state_gc_work);
2101
Alexey Dobriyan9d4139c2008-11-25 17:16:11 -08002102 WARN_ON(!list_empty(&net->xfrm.state_all));
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002103
Alexey Dobriyan529983e2008-11-25 17:18:12 -08002104 sz = (net->xfrm.state_hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002105 WARN_ON(!hlist_empty(net->xfrm.state_byspi));
2106 xfrm_hash_free(net->xfrm.state_byspi, sz);
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002107 WARN_ON(!hlist_empty(net->xfrm.state_bysrc));
2108 xfrm_hash_free(net->xfrm.state_bysrc, sz);
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002109 WARN_ON(!hlist_empty(net->xfrm.state_bydst));
2110 xfrm_hash_free(net->xfrm.state_bydst, sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111}
2112
Joy Lattenab5f5e82007-09-17 11:51:22 -07002113#ifdef CONFIG_AUDITSYSCALL
Ilpo Järvinencf35f432008-01-05 23:13:20 -08002114static void xfrm_audit_helper_sainfo(struct xfrm_state *x,
2115 struct audit_buffer *audit_buf)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002116{
Paul Moore68277ac2007-12-20 20:49:33 -08002117 struct xfrm_sec_ctx *ctx = x->security;
2118 u32 spi = ntohl(x->id.spi);
2119
2120 if (ctx)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002121 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
Paul Moore68277ac2007-12-20 20:49:33 -08002122 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002123
2124 switch(x->props.family) {
2125 case AF_INET:
Harvey Harrison21454aa2008-10-31 00:54:56 -07002126 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2127 &x->props.saddr.a4, &x->id.daddr.a4);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002128 break;
2129 case AF_INET6:
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002130 audit_log_format(audit_buf, " src=%pI6 dst=%pI6",
Harvey Harrisonfdb46ee2008-10-28 16:10:17 -07002131 x->props.saddr.a6, x->id.daddr.a6);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002132 break;
2133 }
Paul Moore68277ac2007-12-20 20:49:33 -08002134
2135 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002136}
2137
Ilpo Järvinencf35f432008-01-05 23:13:20 -08002138static void xfrm_audit_helper_pktinfo(struct sk_buff *skb, u16 family,
2139 struct audit_buffer *audit_buf)
Paul Mooreafeb14b2007-12-21 14:58:11 -08002140{
2141 struct iphdr *iph4;
2142 struct ipv6hdr *iph6;
2143
2144 switch (family) {
2145 case AF_INET:
2146 iph4 = ip_hdr(skb);
Harvey Harrison21454aa2008-10-31 00:54:56 -07002147 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2148 &iph4->saddr, &iph4->daddr);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002149 break;
2150 case AF_INET6:
2151 iph6 = ipv6_hdr(skb);
2152 audit_log_format(audit_buf,
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002153 " src=%pI6 dst=%pI6 flowlbl=0x%x%02x%02x",
Harvey Harrisonfdb46ee2008-10-28 16:10:17 -07002154 &iph6->saddr,&iph6->daddr,
Paul Mooreafeb14b2007-12-21 14:58:11 -08002155 iph6->flow_lbl[0] & 0x0f,
2156 iph6->flow_lbl[1],
2157 iph6->flow_lbl[2]);
2158 break;
2159 }
2160}
2161
Paul Moore68277ac2007-12-20 20:49:33 -08002162void xfrm_audit_state_add(struct xfrm_state *x, int result,
Eric Paris25323862008-04-18 10:09:25 -04002163 uid_t auid, u32 sessionid, u32 secid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002164{
2165 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002166
Paul Mooreafeb14b2007-12-21 14:58:11 -08002167 audit_buf = xfrm_audit_start("SAD-add");
Joy Lattenab5f5e82007-09-17 11:51:22 -07002168 if (audit_buf == NULL)
2169 return;
Eric Paris25323862008-04-18 10:09:25 -04002170 xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002171 xfrm_audit_helper_sainfo(x, audit_buf);
2172 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002173 audit_log_end(audit_buf);
2174}
2175EXPORT_SYMBOL_GPL(xfrm_audit_state_add);
2176
Paul Moore68277ac2007-12-20 20:49:33 -08002177void xfrm_audit_state_delete(struct xfrm_state *x, int result,
Eric Paris25323862008-04-18 10:09:25 -04002178 uid_t auid, u32 sessionid, u32 secid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002179{
2180 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002181
Paul Mooreafeb14b2007-12-21 14:58:11 -08002182 audit_buf = xfrm_audit_start("SAD-delete");
Joy Lattenab5f5e82007-09-17 11:51:22 -07002183 if (audit_buf == NULL)
2184 return;
Eric Paris25323862008-04-18 10:09:25 -04002185 xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002186 xfrm_audit_helper_sainfo(x, audit_buf);
2187 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002188 audit_log_end(audit_buf);
2189}
2190EXPORT_SYMBOL_GPL(xfrm_audit_state_delete);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002191
2192void xfrm_audit_state_replay_overflow(struct xfrm_state *x,
2193 struct sk_buff *skb)
2194{
2195 struct audit_buffer *audit_buf;
2196 u32 spi;
2197
2198 audit_buf = xfrm_audit_start("SA-replay-overflow");
2199 if (audit_buf == NULL)
2200 return;
2201 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2202 /* don't record the sequence number because it's inherent in this kind
2203 * of audit message */
2204 spi = ntohl(x->id.spi);
2205 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
2206 audit_log_end(audit_buf);
2207}
2208EXPORT_SYMBOL_GPL(xfrm_audit_state_replay_overflow);
2209
2210static void xfrm_audit_state_replay(struct xfrm_state *x,
2211 struct sk_buff *skb, __be32 net_seq)
2212{
2213 struct audit_buffer *audit_buf;
2214 u32 spi;
2215
2216 audit_buf = xfrm_audit_start("SA-replayed-pkt");
2217 if (audit_buf == NULL)
2218 return;
2219 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2220 spi = ntohl(x->id.spi);
2221 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2222 spi, spi, ntohl(net_seq));
2223 audit_log_end(audit_buf);
2224}
2225
2226void xfrm_audit_state_notfound_simple(struct sk_buff *skb, u16 family)
2227{
2228 struct audit_buffer *audit_buf;
2229
2230 audit_buf = xfrm_audit_start("SA-notfound");
2231 if (audit_buf == NULL)
2232 return;
2233 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2234 audit_log_end(audit_buf);
2235}
2236EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound_simple);
2237
2238void xfrm_audit_state_notfound(struct sk_buff *skb, u16 family,
2239 __be32 net_spi, __be32 net_seq)
2240{
2241 struct audit_buffer *audit_buf;
2242 u32 spi;
2243
2244 audit_buf = xfrm_audit_start("SA-notfound");
2245 if (audit_buf == NULL)
2246 return;
2247 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2248 spi = ntohl(net_spi);
2249 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2250 spi, spi, ntohl(net_seq));
2251 audit_log_end(audit_buf);
2252}
2253EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound);
2254
2255void xfrm_audit_state_icvfail(struct xfrm_state *x,
2256 struct sk_buff *skb, u8 proto)
2257{
2258 struct audit_buffer *audit_buf;
2259 __be32 net_spi;
2260 __be32 net_seq;
2261
2262 audit_buf = xfrm_audit_start("SA-icv-failure");
2263 if (audit_buf == NULL)
2264 return;
2265 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2266 if (xfrm_parse_spi(skb, proto, &net_spi, &net_seq) == 0) {
2267 u32 spi = ntohl(net_spi);
2268 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2269 spi, spi, ntohl(net_seq));
2270 }
2271 audit_log_end(audit_buf);
2272}
2273EXPORT_SYMBOL_GPL(xfrm_audit_state_icvfail);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002274#endif /* CONFIG_AUDITSYSCALL */