blob: f2f7c638083e321b8500e449f9a84c19bf903d97 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
David S. Miller44e36b42006-08-24 04:50:50 -070025#include "xfrm_hash.h"
26
Linus Torvalds1da177e2005-04-16 15:20:36 -070027/* Each xfrm_state may be linked to two tables:
28
29 1. Hash table by (spi,daddr,ah/esp) to find SA by SPI. (input,ctl)
David S. Millera624c102006-08-24 03:24:33 -070030 2. Hash table by (daddr,family,reqid) to find what SAs exist for given
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 destination/tunnel endpoint. (output)
32 */
33
34static DEFINE_SPINLOCK(xfrm_state_lock);
35
David S. Millerf034b5d2006-08-24 03:08:07 -070036static unsigned int xfrm_state_hashmax __read_mostly = 1 * 1024 * 1024;
David S. Miller9d4a7062006-08-24 03:18:09 -070037static unsigned int xfrm_state_genid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Herbert Xu17c2a422007-10-17 21:33:12 -070039static struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family);
40static void xfrm_state_put_afinfo(struct xfrm_state_afinfo *afinfo);
41
Paul Mooreafeb14b2007-12-21 14:58:11 -080042#ifdef CONFIG_AUDITSYSCALL
43static void xfrm_audit_state_replay(struct xfrm_state *x,
44 struct sk_buff *skb, __be32 net_seq);
45#else
46#define xfrm_audit_state_replay(x, s, sq) do { ; } while (0)
47#endif /* CONFIG_AUDITSYSCALL */
48
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -080049static inline unsigned int xfrm_dst_hash(struct net *net,
50 xfrm_address_t *daddr,
David S. Millerc1969f22006-08-24 04:00:03 -070051 xfrm_address_t *saddr,
52 u32 reqid,
David S. Millera624c102006-08-24 03:24:33 -070053 unsigned short family)
54{
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -080055 return __xfrm_dst_hash(daddr, saddr, reqid, family, net->xfrm.state_hmask);
David S. Millera624c102006-08-24 03:24:33 -070056}
57
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -080058static inline unsigned int xfrm_src_hash(struct net *net,
59 xfrm_address_t *daddr,
Masahide NAKAMURA667bbcb2006-10-03 15:56:09 -070060 xfrm_address_t *saddr,
David S. Miller44e36b42006-08-24 04:50:50 -070061 unsigned short family)
David S. Millerf034b5d2006-08-24 03:08:07 -070062{
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -080063 return __xfrm_src_hash(daddr, saddr, family, net->xfrm.state_hmask);
David S. Millerf034b5d2006-08-24 03:08:07 -070064}
65
David S. Miller2575b652006-08-24 03:26:44 -070066static inline unsigned int
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -080067xfrm_spi_hash(struct net *net, xfrm_address_t *daddr, __be32 spi, u8 proto, unsigned short family)
David S. Millerf034b5d2006-08-24 03:08:07 -070068{
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -080069 return __xfrm_spi_hash(daddr, spi, proto, family, net->xfrm.state_hmask);
David S. Millerf034b5d2006-08-24 03:08:07 -070070}
71
David S. Millerf034b5d2006-08-24 03:08:07 -070072static void xfrm_hash_transfer(struct hlist_head *list,
73 struct hlist_head *ndsttable,
74 struct hlist_head *nsrctable,
75 struct hlist_head *nspitable,
76 unsigned int nhashmask)
77{
78 struct hlist_node *entry, *tmp;
79 struct xfrm_state *x;
80
81 hlist_for_each_entry_safe(x, entry, tmp, list, bydst) {
82 unsigned int h;
83
David S. Millerc1969f22006-08-24 04:00:03 -070084 h = __xfrm_dst_hash(&x->id.daddr, &x->props.saddr,
85 x->props.reqid, x->props.family,
86 nhashmask);
David S. Millerf034b5d2006-08-24 03:08:07 -070087 hlist_add_head(&x->bydst, ndsttable+h);
88
Masahide NAKAMURA667bbcb2006-10-03 15:56:09 -070089 h = __xfrm_src_hash(&x->id.daddr, &x->props.saddr,
90 x->props.family,
David S. Millerf034b5d2006-08-24 03:08:07 -070091 nhashmask);
92 hlist_add_head(&x->bysrc, nsrctable+h);
93
Masahide NAKAMURA7b4dc3602006-09-27 22:21:52 -070094 if (x->id.spi) {
95 h = __xfrm_spi_hash(&x->id.daddr, x->id.spi,
96 x->id.proto, x->props.family,
97 nhashmask);
98 hlist_add_head(&x->byspi, nspitable+h);
99 }
David S. Millerf034b5d2006-08-24 03:08:07 -0700100 }
101}
102
Alexey Dobriyan63082732008-11-25 17:19:07 -0800103static unsigned long xfrm_hash_new_size(unsigned int state_hmask)
David S. Millerf034b5d2006-08-24 03:08:07 -0700104{
Alexey Dobriyan63082732008-11-25 17:19:07 -0800105 return ((state_hmask + 1) << 1) * sizeof(struct hlist_head);
David S. Millerf034b5d2006-08-24 03:08:07 -0700106}
107
108static DEFINE_MUTEX(hash_resize_mutex);
109
Alexey Dobriyan63082732008-11-25 17:19:07 -0800110static void xfrm_hash_resize(struct work_struct *work)
David S. Millerf034b5d2006-08-24 03:08:07 -0700111{
Alexey Dobriyan63082732008-11-25 17:19:07 -0800112 struct net *net = container_of(work, struct net, xfrm.state_hash_work);
David S. Millerf034b5d2006-08-24 03:08:07 -0700113 struct hlist_head *ndst, *nsrc, *nspi, *odst, *osrc, *ospi;
114 unsigned long nsize, osize;
115 unsigned int nhashmask, ohashmask;
116 int i;
117
118 mutex_lock(&hash_resize_mutex);
119
Alexey Dobriyan63082732008-11-25 17:19:07 -0800120 nsize = xfrm_hash_new_size(net->xfrm.state_hmask);
David S. Miller44e36b42006-08-24 04:50:50 -0700121 ndst = xfrm_hash_alloc(nsize);
David S. Millerf034b5d2006-08-24 03:08:07 -0700122 if (!ndst)
123 goto out_unlock;
David S. Miller44e36b42006-08-24 04:50:50 -0700124 nsrc = xfrm_hash_alloc(nsize);
David S. Millerf034b5d2006-08-24 03:08:07 -0700125 if (!nsrc) {
David S. Miller44e36b42006-08-24 04:50:50 -0700126 xfrm_hash_free(ndst, nsize);
David S. Millerf034b5d2006-08-24 03:08:07 -0700127 goto out_unlock;
128 }
David S. Miller44e36b42006-08-24 04:50:50 -0700129 nspi = xfrm_hash_alloc(nsize);
David S. Millerf034b5d2006-08-24 03:08:07 -0700130 if (!nspi) {
David S. Miller44e36b42006-08-24 04:50:50 -0700131 xfrm_hash_free(ndst, nsize);
132 xfrm_hash_free(nsrc, nsize);
David S. Millerf034b5d2006-08-24 03:08:07 -0700133 goto out_unlock;
134 }
135
136 spin_lock_bh(&xfrm_state_lock);
137
138 nhashmask = (nsize / sizeof(struct hlist_head)) - 1U;
Alexey Dobriyan63082732008-11-25 17:19:07 -0800139 for (i = net->xfrm.state_hmask; i >= 0; i--)
140 xfrm_hash_transfer(net->xfrm.state_bydst+i, ndst, nsrc, nspi,
David S. Millerf034b5d2006-08-24 03:08:07 -0700141 nhashmask);
142
Alexey Dobriyan63082732008-11-25 17:19:07 -0800143 odst = net->xfrm.state_bydst;
144 osrc = net->xfrm.state_bysrc;
145 ospi = net->xfrm.state_byspi;
146 ohashmask = net->xfrm.state_hmask;
David S. Millerf034b5d2006-08-24 03:08:07 -0700147
Alexey Dobriyan63082732008-11-25 17:19:07 -0800148 net->xfrm.state_bydst = ndst;
149 net->xfrm.state_bysrc = nsrc;
150 net->xfrm.state_byspi = nspi;
151 net->xfrm.state_hmask = nhashmask;
David S. Millerf034b5d2006-08-24 03:08:07 -0700152
153 spin_unlock_bh(&xfrm_state_lock);
154
155 osize = (ohashmask + 1) * sizeof(struct hlist_head);
David S. Miller44e36b42006-08-24 04:50:50 -0700156 xfrm_hash_free(odst, osize);
157 xfrm_hash_free(osrc, osize);
158 xfrm_hash_free(ospi, osize);
David S. Millerf034b5d2006-08-24 03:08:07 -0700159
160out_unlock:
161 mutex_unlock(&hash_resize_mutex);
162}
163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164static DEFINE_RWLOCK(xfrm_state_afinfo_lock);
165static struct xfrm_state_afinfo *xfrm_state_afinfo[NPROTO];
166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167static DEFINE_SPINLOCK(xfrm_state_gc_lock);
168
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -0800169int __xfrm_state_delete(struct xfrm_state *x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -0800171int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -0800172void km_state_expired(struct xfrm_state *x, int hard, u32 pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700174static struct xfrm_state_afinfo *xfrm_state_lock_afinfo(unsigned int family)
175{
176 struct xfrm_state_afinfo *afinfo;
177 if (unlikely(family >= NPROTO))
178 return NULL;
179 write_lock_bh(&xfrm_state_afinfo_lock);
180 afinfo = xfrm_state_afinfo[family];
181 if (unlikely(!afinfo))
182 write_unlock_bh(&xfrm_state_afinfo_lock);
183 return afinfo;
184}
185
186static void xfrm_state_unlock_afinfo(struct xfrm_state_afinfo *afinfo)
Eric Dumazet9a429c42008-01-01 21:58:02 -0800187 __releases(xfrm_state_afinfo_lock)
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700188{
189 write_unlock_bh(&xfrm_state_afinfo_lock);
190}
191
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800192int xfrm_register_type(const struct xfrm_type *type, unsigned short family)
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700193{
194 struct xfrm_state_afinfo *afinfo = xfrm_state_lock_afinfo(family);
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800195 const struct xfrm_type **typemap;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700196 int err = 0;
197
198 if (unlikely(afinfo == NULL))
199 return -EAFNOSUPPORT;
200 typemap = afinfo->type_map;
201
202 if (likely(typemap[type->proto] == NULL))
203 typemap[type->proto] = type;
204 else
205 err = -EEXIST;
206 xfrm_state_unlock_afinfo(afinfo);
207 return err;
208}
209EXPORT_SYMBOL(xfrm_register_type);
210
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800211int xfrm_unregister_type(const struct xfrm_type *type, unsigned short family)
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700212{
213 struct xfrm_state_afinfo *afinfo = xfrm_state_lock_afinfo(family);
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800214 const struct xfrm_type **typemap;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700215 int err = 0;
216
217 if (unlikely(afinfo == NULL))
218 return -EAFNOSUPPORT;
219 typemap = afinfo->type_map;
220
221 if (unlikely(typemap[type->proto] != type))
222 err = -ENOENT;
223 else
224 typemap[type->proto] = NULL;
225 xfrm_state_unlock_afinfo(afinfo);
226 return err;
227}
228EXPORT_SYMBOL(xfrm_unregister_type);
229
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800230static const struct xfrm_type *xfrm_get_type(u8 proto, unsigned short family)
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700231{
232 struct xfrm_state_afinfo *afinfo;
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800233 const struct xfrm_type **typemap;
234 const struct xfrm_type *type;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700235 int modload_attempted = 0;
236
237retry:
238 afinfo = xfrm_state_get_afinfo(family);
239 if (unlikely(afinfo == NULL))
240 return NULL;
241 typemap = afinfo->type_map;
242
243 type = typemap[proto];
244 if (unlikely(type && !try_module_get(type->owner)))
245 type = NULL;
246 if (!type && !modload_attempted) {
247 xfrm_state_put_afinfo(afinfo);
248 request_module("xfrm-type-%d-%d", family, proto);
249 modload_attempted = 1;
250 goto retry;
251 }
252
253 xfrm_state_put_afinfo(afinfo);
254 return type;
255}
256
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800257static void xfrm_put_type(const struct xfrm_type *type)
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700258{
259 module_put(type->owner);
260}
261
262int xfrm_register_mode(struct xfrm_mode *mode, int family)
263{
264 struct xfrm_state_afinfo *afinfo;
265 struct xfrm_mode **modemap;
266 int err;
267
268 if (unlikely(mode->encap >= XFRM_MODE_MAX))
269 return -EINVAL;
270
271 afinfo = xfrm_state_lock_afinfo(family);
272 if (unlikely(afinfo == NULL))
273 return -EAFNOSUPPORT;
274
275 err = -EEXIST;
276 modemap = afinfo->mode_map;
Herbert Xu17c2a422007-10-17 21:33:12 -0700277 if (modemap[mode->encap])
278 goto out;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700279
Herbert Xu17c2a422007-10-17 21:33:12 -0700280 err = -ENOENT;
281 if (!try_module_get(afinfo->owner))
282 goto out;
283
284 mode->afinfo = afinfo;
285 modemap[mode->encap] = mode;
286 err = 0;
287
288out:
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700289 xfrm_state_unlock_afinfo(afinfo);
290 return err;
291}
292EXPORT_SYMBOL(xfrm_register_mode);
293
294int xfrm_unregister_mode(struct xfrm_mode *mode, int family)
295{
296 struct xfrm_state_afinfo *afinfo;
297 struct xfrm_mode **modemap;
298 int err;
299
300 if (unlikely(mode->encap >= XFRM_MODE_MAX))
301 return -EINVAL;
302
303 afinfo = xfrm_state_lock_afinfo(family);
304 if (unlikely(afinfo == NULL))
305 return -EAFNOSUPPORT;
306
307 err = -ENOENT;
308 modemap = afinfo->mode_map;
309 if (likely(modemap[mode->encap] == mode)) {
310 modemap[mode->encap] = NULL;
Herbert Xu17c2a422007-10-17 21:33:12 -0700311 module_put(mode->afinfo->owner);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700312 err = 0;
313 }
314
315 xfrm_state_unlock_afinfo(afinfo);
316 return err;
317}
318EXPORT_SYMBOL(xfrm_unregister_mode);
319
320static struct xfrm_mode *xfrm_get_mode(unsigned int encap, int family)
321{
322 struct xfrm_state_afinfo *afinfo;
323 struct xfrm_mode *mode;
324 int modload_attempted = 0;
325
326 if (unlikely(encap >= XFRM_MODE_MAX))
327 return NULL;
328
329retry:
330 afinfo = xfrm_state_get_afinfo(family);
331 if (unlikely(afinfo == NULL))
332 return NULL;
333
334 mode = afinfo->mode_map[encap];
335 if (unlikely(mode && !try_module_get(mode->owner)))
336 mode = NULL;
337 if (!mode && !modload_attempted) {
338 xfrm_state_put_afinfo(afinfo);
339 request_module("xfrm-mode-%d-%d", family, encap);
340 modload_attempted = 1;
341 goto retry;
342 }
343
344 xfrm_state_put_afinfo(afinfo);
345 return mode;
346}
347
348static void xfrm_put_mode(struct xfrm_mode *mode)
349{
350 module_put(mode->owner);
351}
352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353static void xfrm_state_gc_destroy(struct xfrm_state *x)
354{
David S. Millera47f0ce2006-08-24 03:54:22 -0700355 del_timer_sync(&x->timer);
356 del_timer_sync(&x->rtimer);
Jesper Juhla51482b2005-11-08 09:41:34 -0800357 kfree(x->aalg);
358 kfree(x->ealg);
359 kfree(x->calg);
360 kfree(x->encap);
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700361 kfree(x->coaddr);
Herbert Xu13996372007-10-17 21:35:51 -0700362 if (x->inner_mode)
363 xfrm_put_mode(x->inner_mode);
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700364 if (x->inner_mode_iaf)
365 xfrm_put_mode(x->inner_mode_iaf);
Herbert Xu13996372007-10-17 21:35:51 -0700366 if (x->outer_mode)
367 xfrm_put_mode(x->outer_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 if (x->type) {
369 x->type->destructor(x);
370 xfrm_put_type(x->type);
371 }
Trent Jaegerdf718372005-12-13 23:12:27 -0800372 security_xfrm_state_free(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 kfree(x);
374}
375
Alexey Dobriyanc7837142008-11-25 17:20:36 -0800376static void xfrm_state_gc_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
Alexey Dobriyanc7837142008-11-25 17:20:36 -0800378 struct net *net = container_of(work, struct net, xfrm.state_gc_work);
Herbert Xu12a169e2008-10-01 07:03:24 -0700379 struct xfrm_state *x;
380 struct hlist_node *entry, *tmp;
381 struct hlist_head gc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 spin_lock_bh(&xfrm_state_gc_lock);
Alexey Dobriyanc7837142008-11-25 17:20:36 -0800384 hlist_move_list(&net->xfrm.state_gc_list, &gc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 spin_unlock_bh(&xfrm_state_gc_lock);
386
Herbert Xu12a169e2008-10-01 07:03:24 -0700387 hlist_for_each_entry_safe(x, entry, tmp, &gc_list, gclist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 xfrm_state_gc_destroy(x);
David S. Miller8f126e32006-08-24 02:45:07 -0700389
Alexey Dobriyan50a30652008-11-25 17:21:01 -0800390 wake_up(&net->xfrm.km_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391}
392
393static inline unsigned long make_jiffies(long secs)
394{
395 if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
396 return MAX_SCHEDULE_TIMEOUT-1;
397 else
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900398 return secs*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399}
400
401static void xfrm_timer_handler(unsigned long data)
402{
403 struct xfrm_state *x = (struct xfrm_state*)data;
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800404 struct net *net = xs_net(x);
James Morris9d729f72007-03-04 16:12:44 -0800405 unsigned long now = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 long next = LONG_MAX;
407 int warn = 0;
Joy Latten161a09e2006-11-27 13:11:54 -0600408 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
410 spin_lock(&x->lock);
411 if (x->km.state == XFRM_STATE_DEAD)
412 goto out;
413 if (x->km.state == XFRM_STATE_EXPIRED)
414 goto expired;
415 if (x->lft.hard_add_expires_seconds) {
416 long tmo = x->lft.hard_add_expires_seconds +
417 x->curlft.add_time - now;
418 if (tmo <= 0)
419 goto expired;
420 if (tmo < next)
421 next = tmo;
422 }
423 if (x->lft.hard_use_expires_seconds) {
424 long tmo = x->lft.hard_use_expires_seconds +
425 (x->curlft.use_time ? : now) - now;
426 if (tmo <= 0)
427 goto expired;
428 if (tmo < next)
429 next = tmo;
430 }
431 if (x->km.dying)
432 goto resched;
433 if (x->lft.soft_add_expires_seconds) {
434 long tmo = x->lft.soft_add_expires_seconds +
435 x->curlft.add_time - now;
436 if (tmo <= 0)
437 warn = 1;
438 else if (tmo < next)
439 next = tmo;
440 }
441 if (x->lft.soft_use_expires_seconds) {
442 long tmo = x->lft.soft_use_expires_seconds +
443 (x->curlft.use_time ? : now) - now;
444 if (tmo <= 0)
445 warn = 1;
446 else if (tmo < next)
447 next = tmo;
448 }
449
Herbert Xu4666faa2005-06-18 22:43:22 -0700450 x->km.dying = warn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 if (warn)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -0800452 km_state_expired(x, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453resched:
David S. Millera47f0ce2006-08-24 03:54:22 -0700454 if (next != LONG_MAX)
455 mod_timer(&x->timer, jiffies + make_jiffies(next));
456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 goto out;
458
459expired:
460 if (x->km.state == XFRM_STATE_ACQ && x->id.spi == 0) {
461 x->km.state = XFRM_STATE_EXPIRED;
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800462 wake_up(&net->xfrm.km_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 next = 2;
464 goto resched;
465 }
Joy Latten161a09e2006-11-27 13:11:54 -0600466
467 err = __xfrm_state_delete(x);
468 if (!err && x->id.spi)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -0800469 km_state_expired(x, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
Joy Lattenab5f5e82007-09-17 11:51:22 -0700471 xfrm_audit_state_delete(x, err ? 0 : 1,
Eric Paris25323862008-04-18 10:09:25 -0400472 audit_get_loginuid(current),
473 audit_get_sessionid(current), 0);
Joy Latten161a09e2006-11-27 13:11:54 -0600474
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475out:
476 spin_unlock(&x->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477}
478
David S. Miller0ac84752006-03-20 19:18:23 -0800479static void xfrm_replay_timer_handler(unsigned long data);
480
Alexey Dobriyan673c09b2008-11-25 17:15:16 -0800481struct xfrm_state *xfrm_state_alloc(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
483 struct xfrm_state *x;
484
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700485 x = kzalloc(sizeof(struct xfrm_state), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
487 if (x) {
Alexey Dobriyan673c09b2008-11-25 17:15:16 -0800488 write_pnet(&x->xs_net, net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 atomic_set(&x->refcnt, 1);
490 atomic_set(&x->tunnel_users, 0);
Herbert Xu12a169e2008-10-01 07:03:24 -0700491 INIT_LIST_HEAD(&x->km.all);
David S. Miller8f126e32006-08-24 02:45:07 -0700492 INIT_HLIST_NODE(&x->bydst);
493 INIT_HLIST_NODE(&x->bysrc);
494 INIT_HLIST_NODE(&x->byspi);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800495 setup_timer(&x->timer, xfrm_timer_handler, (unsigned long)x);
496 setup_timer(&x->rtimer, xfrm_replay_timer_handler,
497 (unsigned long)x);
James Morris9d729f72007-03-04 16:12:44 -0800498 x->curlft.add_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 x->lft.soft_byte_limit = XFRM_INF;
500 x->lft.soft_packet_limit = XFRM_INF;
501 x->lft.hard_byte_limit = XFRM_INF;
502 x->lft.hard_packet_limit = XFRM_INF;
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -0800503 x->replay_maxage = 0;
504 x->replay_maxdiff = 0;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700505 x->inner_mode = NULL;
506 x->inner_mode_iaf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 spin_lock_init(&x->lock);
508 }
509 return x;
510}
511EXPORT_SYMBOL(xfrm_state_alloc);
512
513void __xfrm_state_destroy(struct xfrm_state *x)
514{
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800515 struct net *net = xs_net(x);
516
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700517 WARN_ON(x->km.state != XFRM_STATE_DEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
519 spin_lock_bh(&xfrm_state_gc_lock);
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800520 hlist_add_head(&x->gclist, &net->xfrm.state_gc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 spin_unlock_bh(&xfrm_state_gc_lock);
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800522 schedule_work(&net->xfrm.state_gc_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523}
524EXPORT_SYMBOL(__xfrm_state_destroy);
525
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -0800526int __xfrm_state_delete(struct xfrm_state *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800528 struct net *net = xs_net(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700529 int err = -ESRCH;
530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 if (x->km.state != XFRM_STATE_DEAD) {
532 x->km.state = XFRM_STATE_DEAD;
533 spin_lock(&xfrm_state_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -0700534 list_del(&x->km.all);
David S. Miller8f126e32006-08-24 02:45:07 -0700535 hlist_del(&x->bydst);
David S. Miller8f126e32006-08-24 02:45:07 -0700536 hlist_del(&x->bysrc);
David S. Millera47f0ce2006-08-24 03:54:22 -0700537 if (x->id.spi)
David S. Miller8f126e32006-08-24 02:45:07 -0700538 hlist_del(&x->byspi);
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800539 net->xfrm.state_num--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 spin_unlock(&xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 /* All xfrm_state objects are created by xfrm_state_alloc.
543 * The xfrm_state_alloc call gives a reference, and that
544 * is what we are dropping here.
545 */
Patrick McHardy5dba4792007-11-27 11:10:07 +0800546 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700547 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700549
550 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551}
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -0800552EXPORT_SYMBOL(__xfrm_state_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700554int xfrm_state_delete(struct xfrm_state *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700556 int err;
557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 spin_lock_bh(&x->lock);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700559 err = __xfrm_state_delete(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 spin_unlock_bh(&x->lock);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700561
562 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563}
564EXPORT_SYMBOL(xfrm_state_delete);
565
Joy Latten4aa2e622007-06-04 19:05:57 -0400566#ifdef CONFIG_SECURITY_NETWORK_XFRM
567static inline int
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800568xfrm_state_flush_secctx_check(struct net *net, u8 proto, struct xfrm_audit *audit_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
Joy Latten4aa2e622007-06-04 19:05:57 -0400570 int i, err = 0;
571
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800572 for (i = 0; i <= net->xfrm.state_hmask; i++) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400573 struct hlist_node *entry;
574 struct xfrm_state *x;
575
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800576 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+i, bydst) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400577 if (xfrm_id_proto_match(x->id.proto, proto) &&
578 (err = security_xfrm_state_delete(x)) != 0) {
Joy Lattenab5f5e82007-09-17 11:51:22 -0700579 xfrm_audit_state_delete(x, 0,
580 audit_info->loginuid,
Eric Paris25323862008-04-18 10:09:25 -0400581 audit_info->sessionid,
Joy Lattenab5f5e82007-09-17 11:51:22 -0700582 audit_info->secid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400583 return err;
584 }
585 }
586 }
587
588 return err;
589}
590#else
591static inline int
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800592xfrm_state_flush_secctx_check(struct net *net, u8 proto, struct xfrm_audit *audit_info)
Joy Latten4aa2e622007-06-04 19:05:57 -0400593{
594 return 0;
595}
596#endif
597
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800598int xfrm_state_flush(struct net *net, u8 proto, struct xfrm_audit *audit_info)
Joy Latten4aa2e622007-06-04 19:05:57 -0400599{
600 int i, err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
602 spin_lock_bh(&xfrm_state_lock);
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800603 err = xfrm_state_flush_secctx_check(net, proto, audit_info);
Joy Latten4aa2e622007-06-04 19:05:57 -0400604 if (err)
605 goto out;
606
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800607 for (i = 0; i <= net->xfrm.state_hmask; i++) {
David S. Miller8f126e32006-08-24 02:45:07 -0700608 struct hlist_node *entry;
609 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610restart:
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800611 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+i, bydst) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 if (!xfrm_state_kern(x) &&
Masahide NAKAMURA57947082006-09-22 15:06:24 -0700613 xfrm_id_proto_match(x->id.proto, proto)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 xfrm_state_hold(x);
615 spin_unlock_bh(&xfrm_state_lock);
616
Joy Latten161a09e2006-11-27 13:11:54 -0600617 err = xfrm_state_delete(x);
Joy Lattenab5f5e82007-09-17 11:51:22 -0700618 xfrm_audit_state_delete(x, err ? 0 : 1,
619 audit_info->loginuid,
Eric Paris25323862008-04-18 10:09:25 -0400620 audit_info->sessionid,
Joy Lattenab5f5e82007-09-17 11:51:22 -0700621 audit_info->secid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 xfrm_state_put(x);
623
624 spin_lock_bh(&xfrm_state_lock);
625 goto restart;
626 }
627 }
628 }
Joy Latten4aa2e622007-06-04 19:05:57 -0400629 err = 0;
630
631out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 spin_unlock_bh(&xfrm_state_lock);
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800633 wake_up(&net->xfrm.km_waitq);
Joy Latten4aa2e622007-06-04 19:05:57 -0400634 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635}
636EXPORT_SYMBOL(xfrm_state_flush);
637
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -0700638void xfrm_sad_getinfo(struct xfrmk_sadinfo *si)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700639{
640 spin_lock_bh(&xfrm_state_lock);
Alexey Dobriyan0bf7c5b2008-11-25 17:18:39 -0800641 si->sadcnt = init_net.xfrm.state_num;
Alexey Dobriyan529983e2008-11-25 17:18:12 -0800642 si->sadhcnt = init_net.xfrm.state_hmask;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700643 si->sadhmcnt = xfrm_state_hashmax;
644 spin_unlock_bh(&xfrm_state_lock);
645}
646EXPORT_SYMBOL(xfrm_sad_getinfo);
647
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648static int
649xfrm_init_tempsel(struct xfrm_state *x, struct flowi *fl,
650 struct xfrm_tmpl *tmpl,
651 xfrm_address_t *daddr, xfrm_address_t *saddr,
652 unsigned short family)
653{
654 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
655 if (!afinfo)
656 return -1;
657 afinfo->init_tempsel(x, fl, tmpl, daddr, saddr);
658 xfrm_state_put_afinfo(afinfo);
659 return 0;
660}
661
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800662static 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 -0700663{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800664 unsigned int h = xfrm_spi_hash(net, daddr, spi, proto, family);
David S. Milleredcd5822006-08-24 00:42:45 -0700665 struct xfrm_state *x;
David S. Miller8f126e32006-08-24 02:45:07 -0700666 struct hlist_node *entry;
David S. Milleredcd5822006-08-24 00:42:45 -0700667
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800668 hlist_for_each_entry(x, entry, net->xfrm.state_byspi+h, byspi) {
David S. Milleredcd5822006-08-24 00:42:45 -0700669 if (x->props.family != family ||
670 x->id.spi != spi ||
Wei Yongjun18025712009-06-28 18:42:53 +0000671 x->id.proto != proto ||
672 xfrm_addr_cmp(&x->id.daddr, daddr, family))
David S. Milleredcd5822006-08-24 00:42:45 -0700673 continue;
674
David S. Milleredcd5822006-08-24 00:42:45 -0700675 xfrm_state_hold(x);
676 return x;
677 }
678
679 return NULL;
680}
681
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800682static 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 -0700683{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800684 unsigned int h = xfrm_src_hash(net, daddr, saddr, family);
David S. Milleredcd5822006-08-24 00:42:45 -0700685 struct xfrm_state *x;
David S. Miller8f126e32006-08-24 02:45:07 -0700686 struct hlist_node *entry;
David S. Milleredcd5822006-08-24 00:42:45 -0700687
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800688 hlist_for_each_entry(x, entry, net->xfrm.state_bysrc+h, bysrc) {
David S. Milleredcd5822006-08-24 00:42:45 -0700689 if (x->props.family != family ||
Wei Yongjun18025712009-06-28 18:42:53 +0000690 x->id.proto != proto ||
691 xfrm_addr_cmp(&x->id.daddr, daddr, family) ||
692 xfrm_addr_cmp(&x->props.saddr, saddr, family))
David S. Milleredcd5822006-08-24 00:42:45 -0700693 continue;
694
David S. Milleredcd5822006-08-24 00:42:45 -0700695 xfrm_state_hold(x);
696 return x;
697 }
698
699 return NULL;
700}
701
702static inline struct xfrm_state *
703__xfrm_state_locate(struct xfrm_state *x, int use_spi, int family)
704{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800705 struct net *net = xs_net(x);
706
David S. Milleredcd5822006-08-24 00:42:45 -0700707 if (use_spi)
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800708 return __xfrm_state_lookup(net, &x->id.daddr, x->id.spi,
David S. Milleredcd5822006-08-24 00:42:45 -0700709 x->id.proto, family);
710 else
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800711 return __xfrm_state_lookup_byaddr(net, &x->id.daddr,
David S. Milleredcd5822006-08-24 00:42:45 -0700712 &x->props.saddr,
713 x->id.proto, family);
714}
715
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800716static void xfrm_hash_grow_check(struct net *net, int have_hash_collision)
Patrick McHardy2fab22f2006-10-24 15:34:00 -0700717{
718 if (have_hash_collision &&
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800719 (net->xfrm.state_hmask + 1) < xfrm_state_hashmax &&
720 net->xfrm.state_num > net->xfrm.state_hmask)
721 schedule_work(&net->xfrm.state_hash_work);
Patrick McHardy2fab22f2006-10-24 15:34:00 -0700722}
723
David S. Miller08ec9af2009-03-13 14:22:40 -0700724static void xfrm_state_look_at(struct xfrm_policy *pol, struct xfrm_state *x,
725 struct flowi *fl, unsigned short family,
726 xfrm_address_t *daddr, xfrm_address_t *saddr,
727 struct xfrm_state **best, int *acq_in_progress,
728 int *error)
729{
730 /* Resolution logic:
731 * 1. There is a valid state with matching selector. Done.
732 * 2. Valid state with inappropriate selector. Skip.
733 *
734 * Entering area of "sysdeps".
735 *
736 * 3. If state is not valid, selector is temporary, it selects
737 * only session which triggered previous resolution. Key
738 * manager will do something to install a state with proper
739 * selector.
740 */
741 if (x->km.state == XFRM_STATE_VALID) {
742 if ((x->sel.family &&
743 !xfrm_selector_match(&x->sel, fl, x->sel.family)) ||
744 !security_xfrm_state_pol_flow_match(x, pol, fl))
745 return;
746
747 if (!*best ||
748 (*best)->km.dying > x->km.dying ||
749 ((*best)->km.dying == x->km.dying &&
750 (*best)->curlft.add_time < x->curlft.add_time))
751 *best = x;
752 } else if (x->km.state == XFRM_STATE_ACQ) {
753 *acq_in_progress = 1;
754 } else if (x->km.state == XFRM_STATE_ERROR ||
755 x->km.state == XFRM_STATE_EXPIRED) {
756 if (xfrm_selector_match(&x->sel, fl, x->sel.family) &&
757 security_xfrm_state_pol_flow_match(x, pol, fl))
758 *error = -ESRCH;
759 }
760}
761
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762struct xfrm_state *
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900763xfrm_state_find(xfrm_address_t *daddr, xfrm_address_t *saddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 struct flowi *fl, struct xfrm_tmpl *tmpl,
765 struct xfrm_policy *pol, int *err,
766 unsigned short family)
767{
David S. Miller08ec9af2009-03-13 14:22:40 -0700768 static xfrm_address_t saddr_wildcard = { };
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800769 struct net *net = xp_net(pol);
Nicolas Dichtel6a783c92009-04-27 02:58:59 -0700770 unsigned int h, h_wildcard;
David S. Miller8f126e32006-08-24 02:45:07 -0700771 struct hlist_node *entry;
David S. Miller37b08e32008-09-02 20:14:15 -0700772 struct xfrm_state *x, *x0, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 int acquire_in_progress = 0;
774 int error = 0;
775 struct xfrm_state *best = NULL;
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900776
David S. Miller37b08e32008-09-02 20:14:15 -0700777 to_put = NULL;
778
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 spin_lock_bh(&xfrm_state_lock);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800780 h = xfrm_dst_hash(net, daddr, saddr, tmpl->reqid, family);
781 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+h, bydst) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 if (x->props.family == family &&
783 x->props.reqid == tmpl->reqid &&
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -0700784 !(x->props.flags & XFRM_STATE_WILDRECV) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 xfrm_state_addr_check(x, daddr, saddr, family) &&
786 tmpl->mode == x->props.mode &&
787 tmpl->id.proto == x->id.proto &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700788 (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
789 xfrm_state_look_at(pol, x, fl, family, daddr, saddr,
790 &best, &acquire_in_progress, &error);
791 }
792 if (best)
793 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
Nicolas Dichtel6a783c92009-04-27 02:58:59 -0700795 h_wildcard = xfrm_dst_hash(net, daddr, &saddr_wildcard, tmpl->reqid, family);
796 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+h_wildcard, bydst) {
David S. Miller08ec9af2009-03-13 14:22:40 -0700797 if (x->props.family == family &&
798 x->props.reqid == tmpl->reqid &&
799 !(x->props.flags & XFRM_STATE_WILDRECV) &&
800 xfrm_state_addr_check(x, daddr, saddr, family) &&
801 tmpl->mode == x->props.mode &&
802 tmpl->id.proto == x->id.proto &&
803 (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
804 xfrm_state_look_at(pol, x, fl, family, daddr, saddr,
805 &best, &acquire_in_progress, &error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 }
807
David S. Miller08ec9af2009-03-13 14:22:40 -0700808found:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 x = best;
810 if (!x && !error && !acquire_in_progress) {
Patrick McHardy5c5d2812005-04-21 20:12:32 -0700811 if (tmpl->id.spi &&
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800812 (x0 = __xfrm_state_lookup(net, daddr, tmpl->id.spi,
David S. Milleredcd5822006-08-24 00:42:45 -0700813 tmpl->id.proto, family)) != NULL) {
David S. Miller37b08e32008-09-02 20:14:15 -0700814 to_put = x0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 error = -EEXIST;
816 goto out;
817 }
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800818 x = xfrm_state_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 if (x == NULL) {
820 error = -ENOMEM;
821 goto out;
822 }
823 /* Initialize temporary selector matching only
824 * to current session. */
825 xfrm_init_tempsel(x, fl, tmpl, daddr, saddr, family);
826
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -0700827 error = security_xfrm_state_alloc_acquire(x, pol->security, fl->secid);
828 if (error) {
829 x->km.state = XFRM_STATE_DEAD;
David S. Miller37b08e32008-09-02 20:14:15 -0700830 to_put = x;
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -0700831 x = NULL;
832 goto out;
833 }
834
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 if (km_query(x, tmpl, pol) == 0) {
836 x->km.state = XFRM_STATE_ACQ;
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800837 list_add(&x->km.all, &net->xfrm.state_all);
838 hlist_add_head(&x->bydst, net->xfrm.state_bydst+h);
839 h = xfrm_src_hash(net, daddr, saddr, family);
840 hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 if (x->id.spi) {
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800842 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, family);
843 hlist_add_head(&x->byspi, net->xfrm.state_byspi+h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 }
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800845 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
846 x->timer.expires = jiffies + net->xfrm.sysctl_acq_expires*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 add_timer(&x->timer);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800848 net->xfrm.state_num++;
849 xfrm_hash_grow_check(net, x->bydst.next != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 } else {
851 x->km.state = XFRM_STATE_DEAD;
David S. Miller37b08e32008-09-02 20:14:15 -0700852 to_put = x;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 x = NULL;
854 error = -ESRCH;
855 }
856 }
857out:
858 if (x)
859 xfrm_state_hold(x);
860 else
861 *err = acquire_in_progress ? -EAGAIN : error;
862 spin_unlock_bh(&xfrm_state_lock);
David S. Miller37b08e32008-09-02 20:14:15 -0700863 if (to_put)
864 xfrm_state_put(to_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 return x;
866}
867
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700868struct xfrm_state *
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800869xfrm_stateonly_find(struct net *net,
870 xfrm_address_t *daddr, xfrm_address_t *saddr,
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700871 unsigned short family, u8 mode, u8 proto, u32 reqid)
872{
Pavel Emelyanov4bda4f22007-12-14 11:38:04 -0800873 unsigned int h;
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700874 struct xfrm_state *rx = NULL, *x = NULL;
875 struct hlist_node *entry;
876
877 spin_lock(&xfrm_state_lock);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800878 h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
879 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+h, bydst) {
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700880 if (x->props.family == family &&
881 x->props.reqid == reqid &&
882 !(x->props.flags & XFRM_STATE_WILDRECV) &&
883 xfrm_state_addr_check(x, daddr, saddr, family) &&
884 mode == x->props.mode &&
885 proto == x->id.proto &&
886 x->km.state == XFRM_STATE_VALID) {
887 rx = x;
888 break;
889 }
890 }
891
892 if (rx)
893 xfrm_state_hold(rx);
894 spin_unlock(&xfrm_state_lock);
895
896
897 return rx;
898}
899EXPORT_SYMBOL(xfrm_stateonly_find);
900
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901static void __xfrm_state_insert(struct xfrm_state *x)
902{
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800903 struct net *net = xs_net(x);
David S. Millera624c102006-08-24 03:24:33 -0700904 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
David S. Miller9d4a7062006-08-24 03:18:09 -0700906 x->genid = ++xfrm_state_genid;
907
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800908 list_add(&x->km.all, &net->xfrm.state_all);
Timo Teras4c563f72008-02-28 21:31:08 -0800909
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800910 h = xfrm_dst_hash(net, &x->id.daddr, &x->props.saddr,
David S. Millerc1969f22006-08-24 04:00:03 -0700911 x->props.reqid, x->props.family);
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800912 hlist_add_head(&x->bydst, net->xfrm.state_bydst+h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800914 h = xfrm_src_hash(net, &x->id.daddr, &x->props.saddr, x->props.family);
915 hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
Masahide NAKAMURA7b4dc3602006-09-27 22:21:52 -0700917 if (x->id.spi) {
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800918 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto,
Masahide NAKAMURA6c44e6b2006-08-23 17:53:57 -0700919 x->props.family);
920
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800921 hlist_add_head(&x->byspi, net->xfrm.state_byspi+h);
Masahide NAKAMURA6c44e6b2006-08-23 17:53:57 -0700922 }
923
David S. Millera47f0ce2006-08-24 03:54:22 -0700924 mod_timer(&x->timer, jiffies + HZ);
925 if (x->replay_maxage)
926 mod_timer(&x->rtimer, jiffies + x->replay_maxage);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -0800927
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800928 wake_up(&net->xfrm.km_waitq);
David S. Millerf034b5d2006-08-24 03:08:07 -0700929
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800930 net->xfrm.state_num++;
David S. Millerf034b5d2006-08-24 03:08:07 -0700931
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800932 xfrm_hash_grow_check(net, x->bydst.next != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933}
934
David S. Millerc7f5ea32006-08-24 03:29:04 -0700935/* xfrm_state_lock is held */
936static void __xfrm_state_bump_genids(struct xfrm_state *xnew)
937{
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800938 struct net *net = xs_net(xnew);
David S. Millerc7f5ea32006-08-24 03:29:04 -0700939 unsigned short family = xnew->props.family;
940 u32 reqid = xnew->props.reqid;
941 struct xfrm_state *x;
942 struct hlist_node *entry;
943 unsigned int h;
944
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800945 h = xfrm_dst_hash(net, &xnew->id.daddr, &xnew->props.saddr, reqid, family);
946 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+h, bydst) {
David S. Millerc7f5ea32006-08-24 03:29:04 -0700947 if (x->props.family == family &&
948 x->props.reqid == reqid &&
David S. Millerc1969f22006-08-24 04:00:03 -0700949 !xfrm_addr_cmp(&x->id.daddr, &xnew->id.daddr, family) &&
950 !xfrm_addr_cmp(&x->props.saddr, &xnew->props.saddr, family))
David S. Millerc7f5ea32006-08-24 03:29:04 -0700951 x->genid = xfrm_state_genid;
952 }
953}
954
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955void xfrm_state_insert(struct xfrm_state *x)
956{
957 spin_lock_bh(&xfrm_state_lock);
David S. Millerc7f5ea32006-08-24 03:29:04 -0700958 __xfrm_state_bump_genids(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 __xfrm_state_insert(x);
960 spin_unlock_bh(&xfrm_state_lock);
961}
962EXPORT_SYMBOL(xfrm_state_insert);
963
David S. Miller27708342006-08-24 00:13:10 -0700964/* xfrm_state_lock is held */
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800965static 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 -0700966{
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800967 unsigned int h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
David S. Miller8f126e32006-08-24 02:45:07 -0700968 struct hlist_node *entry;
David S. Miller27708342006-08-24 00:13:10 -0700969 struct xfrm_state *x;
970
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800971 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+h, bydst) {
David S. Miller27708342006-08-24 00:13:10 -0700972 if (x->props.reqid != reqid ||
973 x->props.mode != mode ||
974 x->props.family != family ||
975 x->km.state != XFRM_STATE_ACQ ||
Joy Latten75e252d2007-03-12 17:14:07 -0700976 x->id.spi != 0 ||
Wei Yongjun18025712009-06-28 18:42:53 +0000977 x->id.proto != proto ||
978 xfrm_addr_cmp(&x->id.daddr, daddr, family) ||
979 xfrm_addr_cmp(&x->props.saddr, saddr, family))
David S. Miller27708342006-08-24 00:13:10 -0700980 continue;
981
David S. Miller27708342006-08-24 00:13:10 -0700982 xfrm_state_hold(x);
983 return x;
984 }
985
986 if (!create)
987 return NULL;
988
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800989 x = xfrm_state_alloc(net);
David S. Miller27708342006-08-24 00:13:10 -0700990 if (likely(x)) {
991 switch (family) {
992 case AF_INET:
993 x->sel.daddr.a4 = daddr->a4;
994 x->sel.saddr.a4 = saddr->a4;
995 x->sel.prefixlen_d = 32;
996 x->sel.prefixlen_s = 32;
997 x->props.saddr.a4 = saddr->a4;
998 x->id.daddr.a4 = daddr->a4;
999 break;
1000
1001 case AF_INET6:
1002 ipv6_addr_copy((struct in6_addr *)x->sel.daddr.a6,
1003 (struct in6_addr *)daddr);
1004 ipv6_addr_copy((struct in6_addr *)x->sel.saddr.a6,
1005 (struct in6_addr *)saddr);
1006 x->sel.prefixlen_d = 128;
1007 x->sel.prefixlen_s = 128;
1008 ipv6_addr_copy((struct in6_addr *)x->props.saddr.a6,
1009 (struct in6_addr *)saddr);
1010 ipv6_addr_copy((struct in6_addr *)x->id.daddr.a6,
1011 (struct in6_addr *)daddr);
1012 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001013 }
David S. Miller27708342006-08-24 00:13:10 -07001014
1015 x->km.state = XFRM_STATE_ACQ;
1016 x->id.proto = proto;
1017 x->props.family = family;
1018 x->props.mode = mode;
1019 x->props.reqid = reqid;
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08001020 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
David S. Miller27708342006-08-24 00:13:10 -07001021 xfrm_state_hold(x);
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08001022 x->timer.expires = jiffies + net->xfrm.sysctl_acq_expires*HZ;
David S. Miller27708342006-08-24 00:13:10 -07001023 add_timer(&x->timer);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001024 list_add(&x->km.all, &net->xfrm.state_all);
1025 hlist_add_head(&x->bydst, net->xfrm.state_bydst+h);
1026 h = xfrm_src_hash(net, daddr, saddr, family);
1027 hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h);
David S. Miller918049f2006-10-12 22:03:24 -07001028
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001029 net->xfrm.state_num++;
David S. Miller918049f2006-10-12 22:03:24 -07001030
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001031 xfrm_hash_grow_check(net, x->bydst.next != NULL);
David S. Miller27708342006-08-24 00:13:10 -07001032 }
1033
1034 return x;
1035}
1036
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001037static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
1039int xfrm_state_add(struct xfrm_state *x)
1040{
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001041 struct net *net = xs_net(x);
David S. Miller37b08e32008-09-02 20:14:15 -07001042 struct xfrm_state *x1, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 int family;
1044 int err;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001045 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
1047 family = x->props.family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
David S. Miller37b08e32008-09-02 20:14:15 -07001049 to_put = NULL;
1050
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 spin_lock_bh(&xfrm_state_lock);
1052
David S. Milleredcd5822006-08-24 00:42:45 -07001053 x1 = __xfrm_state_locate(x, use_spi, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 if (x1) {
David S. Miller37b08e32008-09-02 20:14:15 -07001055 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 x1 = NULL;
1057 err = -EEXIST;
1058 goto out;
1059 }
1060
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001061 if (use_spi && x->km.seq) {
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001062 x1 = __xfrm_find_acq_byseq(net, x->km.seq);
Joy Latten75e252d2007-03-12 17:14:07 -07001063 if (x1 && ((x1->id.proto != x->id.proto) ||
1064 xfrm_addr_cmp(&x1->id.daddr, &x->id.daddr, family))) {
David S. Miller37b08e32008-09-02 20:14:15 -07001065 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 x1 = NULL;
1067 }
1068 }
1069
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001070 if (use_spi && !x1)
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001071 x1 = __find_acq_core(net, family, x->props.mode, x->props.reqid,
David S. Miller27708342006-08-24 00:13:10 -07001072 x->id.proto,
1073 &x->id.daddr, &x->props.saddr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
David S. Millerc7f5ea32006-08-24 03:29:04 -07001075 __xfrm_state_bump_genids(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 __xfrm_state_insert(x);
1077 err = 0;
1078
1079out:
1080 spin_unlock_bh(&xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
1082 if (x1) {
1083 xfrm_state_delete(x1);
1084 xfrm_state_put(x1);
1085 }
1086
David S. Miller37b08e32008-09-02 20:14:15 -07001087 if (to_put)
1088 xfrm_state_put(to_put);
1089
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 return err;
1091}
1092EXPORT_SYMBOL(xfrm_state_add);
1093
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001094#ifdef CONFIG_XFRM_MIGRATE
Eric Dumazet66663512008-01-08 01:35:52 -08001095static struct xfrm_state *xfrm_state_clone(struct xfrm_state *orig, int *errp)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001096{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001097 struct net *net = xs_net(orig);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001098 int err = -ENOMEM;
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001099 struct xfrm_state *x = xfrm_state_alloc(net);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001100 if (!x)
1101 goto error;
1102
1103 memcpy(&x->id, &orig->id, sizeof(x->id));
1104 memcpy(&x->sel, &orig->sel, sizeof(x->sel));
1105 memcpy(&x->lft, &orig->lft, sizeof(x->lft));
1106 x->props.mode = orig->props.mode;
1107 x->props.replay_window = orig->props.replay_window;
1108 x->props.reqid = orig->props.reqid;
1109 x->props.family = orig->props.family;
1110 x->props.saddr = orig->props.saddr;
1111
1112 if (orig->aalg) {
1113 x->aalg = xfrm_algo_clone(orig->aalg);
1114 if (!x->aalg)
1115 goto error;
1116 }
1117 x->props.aalgo = orig->props.aalgo;
1118
1119 if (orig->ealg) {
1120 x->ealg = xfrm_algo_clone(orig->ealg);
1121 if (!x->ealg)
1122 goto error;
1123 }
1124 x->props.ealgo = orig->props.ealgo;
1125
1126 if (orig->calg) {
1127 x->calg = xfrm_algo_clone(orig->calg);
1128 if (!x->calg)
1129 goto error;
1130 }
1131 x->props.calgo = orig->props.calgo;
1132
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001133 if (orig->encap) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001134 x->encap = kmemdup(orig->encap, sizeof(*x->encap), GFP_KERNEL);
1135 if (!x->encap)
1136 goto error;
1137 }
1138
1139 if (orig->coaddr) {
1140 x->coaddr = kmemdup(orig->coaddr, sizeof(*x->coaddr),
1141 GFP_KERNEL);
1142 if (!x->coaddr)
1143 goto error;
1144 }
1145
1146 err = xfrm_init_state(x);
1147 if (err)
1148 goto error;
1149
1150 x->props.flags = orig->props.flags;
1151
1152 x->curlft.add_time = orig->curlft.add_time;
1153 x->km.state = orig->km.state;
1154 x->km.seq = orig->km.seq;
1155
1156 return x;
1157
1158 error:
1159 if (errp)
1160 *errp = err;
1161 if (x) {
1162 kfree(x->aalg);
1163 kfree(x->ealg);
1164 kfree(x->calg);
1165 kfree(x->encap);
1166 kfree(x->coaddr);
1167 }
1168 kfree(x);
1169 return NULL;
1170}
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001171
1172/* xfrm_state_lock is held */
1173struct xfrm_state * xfrm_migrate_state_find(struct xfrm_migrate *m)
1174{
1175 unsigned int h;
1176 struct xfrm_state *x;
1177 struct hlist_node *entry;
1178
1179 if (m->reqid) {
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -08001180 h = xfrm_dst_hash(&init_net, &m->old_daddr, &m->old_saddr,
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001181 m->reqid, m->old_family);
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08001182 hlist_for_each_entry(x, entry, init_net.xfrm.state_bydst+h, bydst) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001183 if (x->props.mode != m->mode ||
1184 x->id.proto != m->proto)
1185 continue;
1186 if (m->reqid && x->props.reqid != m->reqid)
1187 continue;
1188 if (xfrm_addr_cmp(&x->id.daddr, &m->old_daddr,
1189 m->old_family) ||
1190 xfrm_addr_cmp(&x->props.saddr, &m->old_saddr,
1191 m->old_family))
1192 continue;
1193 xfrm_state_hold(x);
1194 return x;
1195 }
1196 } else {
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -08001197 h = xfrm_src_hash(&init_net, &m->old_daddr, &m->old_saddr,
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001198 m->old_family);
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08001199 hlist_for_each_entry(x, entry, init_net.xfrm.state_bysrc+h, bysrc) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001200 if (x->props.mode != m->mode ||
1201 x->id.proto != m->proto)
1202 continue;
1203 if (xfrm_addr_cmp(&x->id.daddr, &m->old_daddr,
1204 m->old_family) ||
1205 xfrm_addr_cmp(&x->props.saddr, &m->old_saddr,
1206 m->old_family))
1207 continue;
1208 xfrm_state_hold(x);
1209 return x;
1210 }
1211 }
1212
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001213 return NULL;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001214}
1215EXPORT_SYMBOL(xfrm_migrate_state_find);
1216
1217struct xfrm_state * xfrm_state_migrate(struct xfrm_state *x,
1218 struct xfrm_migrate *m)
1219{
1220 struct xfrm_state *xc;
1221 int err;
1222
1223 xc = xfrm_state_clone(x, &err);
1224 if (!xc)
1225 return NULL;
1226
1227 memcpy(&xc->id.daddr, &m->new_daddr, sizeof(xc->id.daddr));
1228 memcpy(&xc->props.saddr, &m->new_saddr, sizeof(xc->props.saddr));
1229
1230 /* add state */
1231 if (!xfrm_addr_cmp(&x->id.daddr, &m->new_daddr, m->new_family)) {
1232 /* a care is needed when the destination address of the
1233 state is to be updated as it is a part of triplet */
1234 xfrm_state_insert(xc);
1235 } else {
1236 if ((err = xfrm_state_add(xc)) < 0)
1237 goto error;
1238 }
1239
1240 return xc;
1241error:
1242 kfree(xc);
1243 return NULL;
1244}
1245EXPORT_SYMBOL(xfrm_state_migrate);
1246#endif
1247
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248int xfrm_state_update(struct xfrm_state *x)
1249{
David S. Miller37b08e32008-09-02 20:14:15 -07001250 struct xfrm_state *x1, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 int err;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001252 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
David S. Miller37b08e32008-09-02 20:14:15 -07001254 to_put = NULL;
1255
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 spin_lock_bh(&xfrm_state_lock);
David S. Milleredcd5822006-08-24 00:42:45 -07001257 x1 = __xfrm_state_locate(x, use_spi, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
1259 err = -ESRCH;
1260 if (!x1)
1261 goto out;
1262
1263 if (xfrm_state_kern(x1)) {
David S. Miller37b08e32008-09-02 20:14:15 -07001264 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 err = -EEXIST;
1266 goto out;
1267 }
1268
1269 if (x1->km.state == XFRM_STATE_ACQ) {
1270 __xfrm_state_insert(x);
1271 x = NULL;
1272 }
1273 err = 0;
1274
1275out:
1276 spin_unlock_bh(&xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
David S. Miller37b08e32008-09-02 20:14:15 -07001278 if (to_put)
1279 xfrm_state_put(to_put);
1280
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 if (err)
1282 return err;
1283
1284 if (!x) {
1285 xfrm_state_delete(x1);
1286 xfrm_state_put(x1);
1287 return 0;
1288 }
1289
1290 err = -EINVAL;
1291 spin_lock_bh(&x1->lock);
1292 if (likely(x1->km.state == XFRM_STATE_VALID)) {
1293 if (x->encap && x1->encap)
1294 memcpy(x1->encap, x->encap, sizeof(*x1->encap));
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -07001295 if (x->coaddr && x1->coaddr) {
1296 memcpy(x1->coaddr, x->coaddr, sizeof(*x1->coaddr));
1297 }
1298 if (!use_spi && memcmp(&x1->sel, &x->sel, sizeof(x1->sel)))
1299 memcpy(&x1->sel, &x->sel, sizeof(x1->sel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 memcpy(&x1->lft, &x->lft, sizeof(x1->lft));
1301 x1->km.dying = 0;
1302
David S. Millera47f0ce2006-08-24 03:54:22 -07001303 mod_timer(&x1->timer, jiffies + HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 if (x1->curlft.use_time)
1305 xfrm_state_check_expire(x1);
1306
1307 err = 0;
1308 }
1309 spin_unlock_bh(&x1->lock);
1310
1311 xfrm_state_put(x1);
1312
1313 return err;
1314}
1315EXPORT_SYMBOL(xfrm_state_update);
1316
1317int xfrm_state_check_expire(struct xfrm_state *x)
1318{
1319 if (!x->curlft.use_time)
James Morris9d729f72007-03-04 16:12:44 -08001320 x->curlft.use_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
1322 if (x->km.state != XFRM_STATE_VALID)
1323 return -EINVAL;
1324
1325 if (x->curlft.bytes >= x->lft.hard_byte_limit ||
1326 x->curlft.packets >= x->lft.hard_packet_limit) {
Herbert Xu4666faa2005-06-18 22:43:22 -07001327 x->km.state = XFRM_STATE_EXPIRED;
David S. Millera47f0ce2006-08-24 03:54:22 -07001328 mod_timer(&x->timer, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 return -EINVAL;
1330 }
1331
1332 if (!x->km.dying &&
1333 (x->curlft.bytes >= x->lft.soft_byte_limit ||
Herbert Xu4666faa2005-06-18 22:43:22 -07001334 x->curlft.packets >= x->lft.soft_packet_limit)) {
1335 x->km.dying = 1;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001336 km_state_expired(x, 0, 0);
Herbert Xu4666faa2005-06-18 22:43:22 -07001337 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 return 0;
1339}
1340EXPORT_SYMBOL(xfrm_state_check_expire);
1341
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342struct xfrm_state *
Alexey Dobriyan221df1e2008-11-25 17:30:50 -08001343xfrm_state_lookup(struct net *net, xfrm_address_t *daddr, __be32 spi, u8 proto,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 unsigned short family)
1345{
1346 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
1348 spin_lock_bh(&xfrm_state_lock);
Alexey Dobriyan221df1e2008-11-25 17:30:50 -08001349 x = __xfrm_state_lookup(net, daddr, spi, proto, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 spin_unlock_bh(&xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 return x;
1352}
1353EXPORT_SYMBOL(xfrm_state_lookup);
1354
1355struct xfrm_state *
Alexey Dobriyan221df1e2008-11-25 17:30:50 -08001356xfrm_state_lookup_byaddr(struct net *net,
1357 xfrm_address_t *daddr, xfrm_address_t *saddr,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001358 u8 proto, unsigned short family)
1359{
1360 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001361
1362 spin_lock_bh(&xfrm_state_lock);
Alexey Dobriyan221df1e2008-11-25 17:30:50 -08001363 x = __xfrm_state_lookup_byaddr(net, daddr, saddr, proto, family);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001364 spin_unlock_bh(&xfrm_state_lock);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001365 return x;
1366}
1367EXPORT_SYMBOL(xfrm_state_lookup_byaddr);
1368
1369struct xfrm_state *
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001370xfrm_find_acq(struct net *net, u8 mode, u32 reqid, u8 proto,
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001371 xfrm_address_t *daddr, xfrm_address_t *saddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 int create, unsigned short family)
1373{
1374 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
1376 spin_lock_bh(&xfrm_state_lock);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001377 x = __find_acq_core(net, family, mode, reqid, proto, daddr, saddr, create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 spin_unlock_bh(&xfrm_state_lock);
David S. Miller27708342006-08-24 00:13:10 -07001379
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 return x;
1381}
1382EXPORT_SYMBOL(xfrm_find_acq);
1383
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001384#ifdef CONFIG_XFRM_SUB_POLICY
1385int
1386xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n,
1387 unsigned short family)
1388{
1389 int err = 0;
1390 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
1391 if (!afinfo)
1392 return -EAFNOSUPPORT;
1393
1394 spin_lock_bh(&xfrm_state_lock);
1395 if (afinfo->tmpl_sort)
1396 err = afinfo->tmpl_sort(dst, src, n);
1397 spin_unlock_bh(&xfrm_state_lock);
1398 xfrm_state_put_afinfo(afinfo);
1399 return err;
1400}
1401EXPORT_SYMBOL(xfrm_tmpl_sort);
1402
1403int
1404xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src, int n,
1405 unsigned short family)
1406{
1407 int err = 0;
1408 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
1409 if (!afinfo)
1410 return -EAFNOSUPPORT;
1411
1412 spin_lock_bh(&xfrm_state_lock);
1413 if (afinfo->state_sort)
1414 err = afinfo->state_sort(dst, src, n);
1415 spin_unlock_bh(&xfrm_state_lock);
1416 xfrm_state_put_afinfo(afinfo);
1417 return err;
1418}
1419EXPORT_SYMBOL(xfrm_state_sort);
1420#endif
1421
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422/* Silly enough, but I'm lazy to build resolution list */
1423
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001424static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425{
1426 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001428 for (i = 0; i <= net->xfrm.state_hmask; i++) {
David S. Miller8f126e32006-08-24 02:45:07 -07001429 struct hlist_node *entry;
1430 struct xfrm_state *x;
1431
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001432 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+i, bydst) {
David S. Miller8f126e32006-08-24 02:45:07 -07001433 if (x->km.seq == seq &&
1434 x->km.state == XFRM_STATE_ACQ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 xfrm_state_hold(x);
1436 return x;
1437 }
1438 }
1439 }
1440 return NULL;
1441}
1442
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001443struct xfrm_state *xfrm_find_acq_byseq(struct net *net, u32 seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444{
1445 struct xfrm_state *x;
1446
1447 spin_lock_bh(&xfrm_state_lock);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001448 x = __xfrm_find_acq_byseq(net, seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 spin_unlock_bh(&xfrm_state_lock);
1450 return x;
1451}
1452EXPORT_SYMBOL(xfrm_find_acq_byseq);
1453
1454u32 xfrm_get_acqseq(void)
1455{
1456 u32 res;
1457 static u32 acqseq;
1458 static DEFINE_SPINLOCK(acqseq_lock);
1459
1460 spin_lock_bh(&acqseq_lock);
1461 res = (++acqseq ? : ++acqseq);
1462 spin_unlock_bh(&acqseq_lock);
1463 return res;
1464}
1465EXPORT_SYMBOL(xfrm_get_acqseq);
1466
Herbert Xu658b2192007-10-09 13:29:52 -07001467int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -08001469 struct net *net = xs_net(x);
David S. Millerf034b5d2006-08-24 03:08:07 -07001470 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 struct xfrm_state *x0;
Herbert Xu658b2192007-10-09 13:29:52 -07001472 int err = -ENOENT;
1473 __be32 minspi = htonl(low);
1474 __be32 maxspi = htonl(high);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
Herbert Xu658b2192007-10-09 13:29:52 -07001476 spin_lock_bh(&x->lock);
1477 if (x->km.state == XFRM_STATE_DEAD)
1478 goto unlock;
1479
1480 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 if (x->id.spi)
Herbert Xu658b2192007-10-09 13:29:52 -07001482 goto unlock;
1483
1484 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
1486 if (minspi == maxspi) {
Alexey Dobriyan221df1e2008-11-25 17:30:50 -08001487 x0 = xfrm_state_lookup(net, &x->id.daddr, minspi, x->id.proto, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 if (x0) {
1489 xfrm_state_put(x0);
Herbert Xu658b2192007-10-09 13:29:52 -07001490 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 }
1492 x->id.spi = minspi;
1493 } else {
1494 u32 spi = 0;
Al Viro26977b42006-09-27 18:47:05 -07001495 for (h=0; h<high-low+1; h++) {
1496 spi = low + net_random()%(high-low+1);
Alexey Dobriyan221df1e2008-11-25 17:30:50 -08001497 x0 = xfrm_state_lookup(net, &x->id.daddr, htonl(spi), x->id.proto, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 if (x0 == NULL) {
1499 x->id.spi = htonl(spi);
1500 break;
1501 }
1502 xfrm_state_put(x0);
1503 }
1504 }
1505 if (x->id.spi) {
1506 spin_lock_bh(&xfrm_state_lock);
Alexey Dobriyan12604d82008-11-25 17:31:18 -08001507 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, x->props.family);
1508 hlist_add_head(&x->byspi, net->xfrm.state_byspi+h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 spin_unlock_bh(&xfrm_state_lock);
Herbert Xu658b2192007-10-09 13:29:52 -07001510
1511 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 }
Herbert Xu658b2192007-10-09 13:29:52 -07001513
1514unlock:
1515 spin_unlock_bh(&x->lock);
1516
1517 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518}
1519EXPORT_SYMBOL(xfrm_alloc_spi);
1520
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08001521int xfrm_state_walk(struct net *net, struct xfrm_state_walk *walk,
Timo Teras4c563f72008-02-28 21:31:08 -08001522 int (*func)(struct xfrm_state *, int, void*),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 void *data)
1524{
Herbert Xu12a169e2008-10-01 07:03:24 -07001525 struct xfrm_state *state;
1526 struct xfrm_state_walk *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 int err = 0;
1528
Herbert Xu12a169e2008-10-01 07:03:24 -07001529 if (walk->seq != 0 && list_empty(&walk->all))
Timo Teras4c563f72008-02-28 21:31:08 -08001530 return 0;
1531
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 spin_lock_bh(&xfrm_state_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001533 if (list_empty(&walk->all))
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08001534 x = list_first_entry(&net->xfrm.state_all, struct xfrm_state_walk, all);
Herbert Xu12a169e2008-10-01 07:03:24 -07001535 else
1536 x = list_entry(&walk->all, struct xfrm_state_walk, all);
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08001537 list_for_each_entry_from(x, &net->xfrm.state_all, all) {
Herbert Xu12a169e2008-10-01 07:03:24 -07001538 if (x->state == XFRM_STATE_DEAD)
Timo Teras4c563f72008-02-28 21:31:08 -08001539 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07001540 state = container_of(x, struct xfrm_state, km);
1541 if (!xfrm_id_proto_match(state->id.proto, walk->proto))
Timo Teras4c563f72008-02-28 21:31:08 -08001542 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07001543 err = func(state, walk->seq, data);
1544 if (err) {
1545 list_move_tail(&walk->all, &x->all);
1546 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001548 walk->seq++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001550 if (walk->seq == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 err = -ENOENT;
1552 goto out;
1553 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001554 list_del_init(&walk->all);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555out:
1556 spin_unlock_bh(&xfrm_state_lock);
1557 return err;
1558}
1559EXPORT_SYMBOL(xfrm_state_walk);
1560
Herbert Xu5c182452008-09-22 19:48:19 -07001561void xfrm_state_walk_init(struct xfrm_state_walk *walk, u8 proto)
1562{
Herbert Xu12a169e2008-10-01 07:03:24 -07001563 INIT_LIST_HEAD(&walk->all);
Herbert Xu5c182452008-09-22 19:48:19 -07001564 walk->proto = proto;
Herbert Xu12a169e2008-10-01 07:03:24 -07001565 walk->state = XFRM_STATE_DEAD;
1566 walk->seq = 0;
Herbert Xu5c182452008-09-22 19:48:19 -07001567}
1568EXPORT_SYMBOL(xfrm_state_walk_init);
1569
Herbert Xuabb81c42008-09-09 19:58:29 -07001570void xfrm_state_walk_done(struct xfrm_state_walk *walk)
1571{
Herbert Xu12a169e2008-10-01 07:03:24 -07001572 if (list_empty(&walk->all))
Herbert Xu5c182452008-09-22 19:48:19 -07001573 return;
Herbert Xu5c182452008-09-22 19:48:19 -07001574
Herbert Xu12a169e2008-10-01 07:03:24 -07001575 spin_lock_bh(&xfrm_state_lock);
1576 list_del(&walk->all);
Chuck Ebbert7d0b5912009-03-27 00:22:01 -07001577 spin_unlock_bh(&xfrm_state_lock);
Herbert Xuabb81c42008-09-09 19:58:29 -07001578}
1579EXPORT_SYMBOL(xfrm_state_walk_done);
1580
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001581
1582void xfrm_replay_notify(struct xfrm_state *x, int event)
1583{
1584 struct km_event c;
1585 /* we send notify messages in case
1586 * 1. we updated on of the sequence numbers, and the seqno difference
1587 * is at least x->replay_maxdiff, in this case we also update the
1588 * timeout of our timer function
1589 * 2. if x->replay_maxage has elapsed since last update,
1590 * and there were changes
1591 *
1592 * The state structure must be locked!
1593 */
1594
1595 switch (event) {
1596 case XFRM_REPLAY_UPDATE:
1597 if (x->replay_maxdiff &&
1598 (x->replay.seq - x->preplay.seq < x->replay_maxdiff) &&
Jamal Hadi Salim27170962006-04-14 15:03:05 -07001599 (x->replay.oseq - x->preplay.oseq < x->replay_maxdiff)) {
1600 if (x->xflags & XFRM_TIME_DEFER)
1601 event = XFRM_REPLAY_TIMEOUT;
1602 else
1603 return;
1604 }
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001605
1606 break;
1607
1608 case XFRM_REPLAY_TIMEOUT:
1609 if ((x->replay.seq == x->preplay.seq) &&
1610 (x->replay.bitmap == x->preplay.bitmap) &&
Jamal Hadi Salim27170962006-04-14 15:03:05 -07001611 (x->replay.oseq == x->preplay.oseq)) {
1612 x->xflags |= XFRM_TIME_DEFER;
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001613 return;
Jamal Hadi Salim27170962006-04-14 15:03:05 -07001614 }
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001615
1616 break;
1617 }
1618
1619 memcpy(&x->preplay, &x->replay, sizeof(struct xfrm_replay_state));
1620 c.event = XFRM_MSG_NEWAE;
1621 c.data.aevent = event;
1622 km_state_notify(x, &c);
1623
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001624 if (x->replay_maxage &&
David S. Millera47f0ce2006-08-24 03:54:22 -07001625 !mod_timer(&x->rtimer, jiffies + x->replay_maxage))
Jamal Hadi Salim27170962006-04-14 15:03:05 -07001626 x->xflags &= ~XFRM_TIME_DEFER;
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001627}
1628
1629static void xfrm_replay_timer_handler(unsigned long data)
1630{
1631 struct xfrm_state *x = (struct xfrm_state*)data;
1632
1633 spin_lock(&x->lock);
1634
Jamal Hadi Salim27170962006-04-14 15:03:05 -07001635 if (x->km.state == XFRM_STATE_VALID) {
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001636 if (xfrm_aevent_is_on(xs_net(x)))
Jamal Hadi Salim27170962006-04-14 15:03:05 -07001637 xfrm_replay_notify(x, XFRM_REPLAY_TIMEOUT);
1638 else
1639 x->xflags |= XFRM_TIME_DEFER;
1640 }
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001641
1642 spin_unlock(&x->lock);
1643}
1644
Paul Mooreafeb14b2007-12-21 14:58:11 -08001645int xfrm_replay_check(struct xfrm_state *x,
1646 struct sk_buff *skb, __be32 net_seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647{
1648 u32 diff;
Al Viroa252cc22006-09-27 18:48:18 -07001649 u32 seq = ntohl(net_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650
1651 if (unlikely(seq == 0))
Paul Mooreafeb14b2007-12-21 14:58:11 -08001652 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653
1654 if (likely(seq > x->replay.seq))
1655 return 0;
1656
1657 diff = x->replay.seq - seq;
Herbert Xu4c4d51a72007-04-05 00:07:39 -07001658 if (diff >= min_t(unsigned int, x->props.replay_window,
1659 sizeof(x->replay.bitmap) * 8)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 x->stats.replay_window++;
Paul Mooreafeb14b2007-12-21 14:58:11 -08001661 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 }
1663
1664 if (x->replay.bitmap & (1U << diff)) {
1665 x->stats.replay++;
Paul Mooreafeb14b2007-12-21 14:58:11 -08001666 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 }
1668 return 0;
Paul Mooreafeb14b2007-12-21 14:58:11 -08001669
1670err:
1671 xfrm_audit_state_replay(x, skb, net_seq);
1672 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
Al Viro61f46272006-09-27 18:48:33 -07001675void xfrm_replay_advance(struct xfrm_state *x, __be32 net_seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676{
1677 u32 diff;
Al Viro61f46272006-09-27 18:48:33 -07001678 u32 seq = ntohl(net_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
1680 if (seq > x->replay.seq) {
1681 diff = seq - x->replay.seq;
1682 if (diff < x->props.replay_window)
1683 x->replay.bitmap = ((x->replay.bitmap) << diff) | 1;
1684 else
1685 x->replay.bitmap = 1;
1686 x->replay.seq = seq;
1687 } else {
1688 diff = x->replay.seq - seq;
1689 x->replay.bitmap |= (1U << diff);
1690 }
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001691
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001692 if (xfrm_aevent_is_on(xs_net(x)))
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001693 xfrm_replay_notify(x, XFRM_REPLAY_UPDATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695
Denis Chengdf018122007-12-07 00:51:11 -08001696static LIST_HEAD(xfrm_km_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697static DEFINE_RWLOCK(xfrm_km_lock);
1698
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001699void km_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700{
1701 struct xfrm_mgr *km;
1702
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001703 read_lock(&xfrm_km_lock);
1704 list_for_each_entry(km, &xfrm_km_list, list)
1705 if (km->notify_policy)
1706 km->notify_policy(xp, dir, c);
1707 read_unlock(&xfrm_km_lock);
1708}
1709
1710void km_state_notify(struct xfrm_state *x, struct km_event *c)
1711{
1712 struct xfrm_mgr *km;
1713 read_lock(&xfrm_km_lock);
1714 list_for_each_entry(km, &xfrm_km_list, list)
1715 if (km->notify)
1716 km->notify(x, c);
1717 read_unlock(&xfrm_km_lock);
1718}
1719
1720EXPORT_SYMBOL(km_policy_notify);
1721EXPORT_SYMBOL(km_state_notify);
1722
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001723void km_state_expired(struct xfrm_state *x, int hard, u32 pid)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001724{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001725 struct net *net = xs_net(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001726 struct km_event c;
1727
Herbert Xubf088672005-06-18 22:44:00 -07001728 c.data.hard = hard;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001729 c.pid = pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001730 c.event = XFRM_MSG_EXPIRE;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001731 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
1733 if (hard)
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001734 wake_up(&net->xfrm.km_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735}
1736
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001737EXPORT_SYMBOL(km_state_expired);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001738/*
1739 * We send to all registered managers regardless of failure
1740 * We are happy with one success
1741*/
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001742int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001744 int err = -EINVAL, acqret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 struct xfrm_mgr *km;
1746
1747 read_lock(&xfrm_km_lock);
1748 list_for_each_entry(km, &xfrm_km_list, list) {
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001749 acqret = km->acquire(x, t, pol, XFRM_POLICY_OUT);
1750 if (!acqret)
1751 err = acqret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 }
1753 read_unlock(&xfrm_km_lock);
1754 return err;
1755}
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001756EXPORT_SYMBOL(km_query);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757
Al Viro5d36b182006-11-08 00:24:06 -08001758int km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759{
1760 int err = -EINVAL;
1761 struct xfrm_mgr *km;
1762
1763 read_lock(&xfrm_km_lock);
1764 list_for_each_entry(km, &xfrm_km_list, list) {
1765 if (km->new_mapping)
1766 err = km->new_mapping(x, ipaddr, sport);
1767 if (!err)
1768 break;
1769 }
1770 read_unlock(&xfrm_km_lock);
1771 return err;
1772}
1773EXPORT_SYMBOL(km_new_mapping);
1774
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001775void km_policy_expired(struct xfrm_policy *pol, int dir, int hard, u32 pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001777 struct net *net = xp_net(pol);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001778 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779
Herbert Xubf088672005-06-18 22:44:00 -07001780 c.data.hard = hard;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001781 c.pid = pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001782 c.event = XFRM_MSG_POLEXPIRE;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001783 km_policy_notify(pol, dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784
1785 if (hard)
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001786 wake_up(&net->xfrm.km_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787}
David S. Millera70fcb02006-03-20 19:18:52 -08001788EXPORT_SYMBOL(km_policy_expired);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
Eric Dumazet2d60abc2008-01-03 20:43:21 -08001790#ifdef CONFIG_XFRM_MIGRATE
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001791int km_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001792 struct xfrm_migrate *m, int num_migrate,
1793 struct xfrm_kmaddress *k)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001794{
1795 int err = -EINVAL;
1796 int ret;
1797 struct xfrm_mgr *km;
1798
1799 read_lock(&xfrm_km_lock);
1800 list_for_each_entry(km, &xfrm_km_list, list) {
1801 if (km->migrate) {
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001802 ret = km->migrate(sel, dir, type, m, num_migrate, k);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001803 if (!ret)
1804 err = ret;
1805 }
1806 }
1807 read_unlock(&xfrm_km_lock);
1808 return err;
1809}
1810EXPORT_SYMBOL(km_migrate);
Eric Dumazet2d60abc2008-01-03 20:43:21 -08001811#endif
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001812
Alexey Dobriyandb983c12008-11-25 17:51:01 -08001813int km_report(struct net *net, u8 proto, struct xfrm_selector *sel, xfrm_address_t *addr)
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001814{
1815 int err = -EINVAL;
1816 int ret;
1817 struct xfrm_mgr *km;
1818
1819 read_lock(&xfrm_km_lock);
1820 list_for_each_entry(km, &xfrm_km_list, list) {
1821 if (km->report) {
Alexey Dobriyandb983c12008-11-25 17:51:01 -08001822 ret = km->report(net, proto, sel, addr);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001823 if (!ret)
1824 err = ret;
1825 }
1826 }
1827 read_unlock(&xfrm_km_lock);
1828 return err;
1829}
1830EXPORT_SYMBOL(km_report);
1831
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832int xfrm_user_policy(struct sock *sk, int optname, u8 __user *optval, int optlen)
1833{
1834 int err;
1835 u8 *data;
1836 struct xfrm_mgr *km;
1837 struct xfrm_policy *pol = NULL;
1838
1839 if (optlen <= 0 || optlen > PAGE_SIZE)
1840 return -EMSGSIZE;
1841
1842 data = kmalloc(optlen, GFP_KERNEL);
1843 if (!data)
1844 return -ENOMEM;
1845
1846 err = -EFAULT;
1847 if (copy_from_user(data, optval, optlen))
1848 goto out;
1849
1850 err = -EINVAL;
1851 read_lock(&xfrm_km_lock);
1852 list_for_each_entry(km, &xfrm_km_list, list) {
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07001853 pol = km->compile_policy(sk, optname, data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 optlen, &err);
1855 if (err >= 0)
1856 break;
1857 }
1858 read_unlock(&xfrm_km_lock);
1859
1860 if (err >= 0) {
1861 xfrm_sk_policy_insert(sk, err, pol);
1862 xfrm_pol_put(pol);
1863 err = 0;
1864 }
1865
1866out:
1867 kfree(data);
1868 return err;
1869}
1870EXPORT_SYMBOL(xfrm_user_policy);
1871
1872int xfrm_register_km(struct xfrm_mgr *km)
1873{
1874 write_lock_bh(&xfrm_km_lock);
1875 list_add_tail(&km->list, &xfrm_km_list);
1876 write_unlock_bh(&xfrm_km_lock);
1877 return 0;
1878}
1879EXPORT_SYMBOL(xfrm_register_km);
1880
1881int xfrm_unregister_km(struct xfrm_mgr *km)
1882{
1883 write_lock_bh(&xfrm_km_lock);
1884 list_del(&km->list);
1885 write_unlock_bh(&xfrm_km_lock);
1886 return 0;
1887}
1888EXPORT_SYMBOL(xfrm_unregister_km);
1889
1890int xfrm_state_register_afinfo(struct xfrm_state_afinfo *afinfo)
1891{
1892 int err = 0;
1893 if (unlikely(afinfo == NULL))
1894 return -EINVAL;
1895 if (unlikely(afinfo->family >= NPROTO))
1896 return -EAFNOSUPPORT;
Ingo Molnarf3111502006-04-28 15:30:03 -07001897 write_lock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 if (unlikely(xfrm_state_afinfo[afinfo->family] != NULL))
1899 err = -ENOBUFS;
David S. Milleredcd5822006-08-24 00:42:45 -07001900 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 xfrm_state_afinfo[afinfo->family] = afinfo;
Ingo Molnarf3111502006-04-28 15:30:03 -07001902 write_unlock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 return err;
1904}
1905EXPORT_SYMBOL(xfrm_state_register_afinfo);
1906
1907int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo)
1908{
1909 int err = 0;
1910 if (unlikely(afinfo == NULL))
1911 return -EINVAL;
1912 if (unlikely(afinfo->family >= NPROTO))
1913 return -EAFNOSUPPORT;
Ingo Molnarf3111502006-04-28 15:30:03 -07001914 write_lock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 if (likely(xfrm_state_afinfo[afinfo->family] != NULL)) {
1916 if (unlikely(xfrm_state_afinfo[afinfo->family] != afinfo))
1917 err = -EINVAL;
David S. Milleredcd5822006-08-24 00:42:45 -07001918 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 xfrm_state_afinfo[afinfo->family] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 }
Ingo Molnarf3111502006-04-28 15:30:03 -07001921 write_unlock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 return err;
1923}
1924EXPORT_SYMBOL(xfrm_state_unregister_afinfo);
1925
Herbert Xu17c2a422007-10-17 21:33:12 -07001926static struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927{
1928 struct xfrm_state_afinfo *afinfo;
1929 if (unlikely(family >= NPROTO))
1930 return NULL;
1931 read_lock(&xfrm_state_afinfo_lock);
1932 afinfo = xfrm_state_afinfo[family];
Herbert Xu546be242006-05-27 23:03:58 -07001933 if (unlikely(!afinfo))
1934 read_unlock(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 return afinfo;
1936}
1937
Herbert Xu17c2a422007-10-17 21:33:12 -07001938static void xfrm_state_put_afinfo(struct xfrm_state_afinfo *afinfo)
Eric Dumazet9a429c42008-01-01 21:58:02 -08001939 __releases(xfrm_state_afinfo_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940{
Herbert Xu546be242006-05-27 23:03:58 -07001941 read_unlock(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942}
1943
1944/* Temporarily located here until net/xfrm/xfrm_tunnel.c is created */
1945void xfrm_state_delete_tunnel(struct xfrm_state *x)
1946{
1947 if (x->tunnel) {
1948 struct xfrm_state *t = x->tunnel;
1949
1950 if (atomic_read(&t->tunnel_users) == 2)
1951 xfrm_state_delete(t);
1952 atomic_dec(&t->tunnel_users);
1953 xfrm_state_put(t);
1954 x->tunnel = NULL;
1955 }
1956}
1957EXPORT_SYMBOL(xfrm_state_delete_tunnel);
1958
1959int xfrm_state_mtu(struct xfrm_state *x, int mtu)
1960{
Patrick McHardyc5c25232007-04-09 11:47:18 -07001961 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
Patrick McHardyc5c25232007-04-09 11:47:18 -07001963 spin_lock_bh(&x->lock);
1964 if (x->km.state == XFRM_STATE_VALID &&
1965 x->type && x->type->get_mtu)
1966 res = x->type->get_mtu(x, mtu);
1967 else
Patrick McHardy28121612007-06-18 22:30:15 -07001968 res = mtu - x->props.header_len;
Patrick McHardyc5c25232007-04-09 11:47:18 -07001969 spin_unlock_bh(&x->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 return res;
1971}
1972
Herbert Xu72cb6962005-06-20 13:18:08 -07001973int xfrm_init_state(struct xfrm_state *x)
1974{
Herbert Xud094cd82005-06-20 13:19:41 -07001975 struct xfrm_state_afinfo *afinfo;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07001976 struct xfrm_mode *inner_mode;
Herbert Xud094cd82005-06-20 13:19:41 -07001977 int family = x->props.family;
Herbert Xu72cb6962005-06-20 13:18:08 -07001978 int err;
1979
Herbert Xud094cd82005-06-20 13:19:41 -07001980 err = -EAFNOSUPPORT;
1981 afinfo = xfrm_state_get_afinfo(family);
1982 if (!afinfo)
1983 goto error;
1984
1985 err = 0;
1986 if (afinfo->init_flags)
1987 err = afinfo->init_flags(x);
1988
1989 xfrm_state_put_afinfo(afinfo);
1990
1991 if (err)
1992 goto error;
1993
1994 err = -EPROTONOSUPPORT;
Herbert Xu13996372007-10-17 21:35:51 -07001995
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07001996 if (x->sel.family != AF_UNSPEC) {
1997 inner_mode = xfrm_get_mode(x->props.mode, x->sel.family);
1998 if (inner_mode == NULL)
1999 goto error;
2000
2001 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL) &&
2002 family != x->sel.family) {
2003 xfrm_put_mode(inner_mode);
2004 goto error;
2005 }
2006
2007 x->inner_mode = inner_mode;
2008 } else {
2009 struct xfrm_mode *inner_mode_iaf;
Martin Willid81d2282008-12-03 15:38:07 -08002010 int iafamily = AF_INET;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002011
Martin Willid81d2282008-12-03 15:38:07 -08002012 inner_mode = xfrm_get_mode(x->props.mode, x->props.family);
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002013 if (inner_mode == NULL)
2014 goto error;
2015
2016 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL)) {
2017 xfrm_put_mode(inner_mode);
2018 goto error;
2019 }
Martin Willid81d2282008-12-03 15:38:07 -08002020 x->inner_mode = inner_mode;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002021
Martin Willid81d2282008-12-03 15:38:07 -08002022 if (x->props.family == AF_INET)
2023 iafamily = AF_INET6;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002024
Martin Willid81d2282008-12-03 15:38:07 -08002025 inner_mode_iaf = xfrm_get_mode(x->props.mode, iafamily);
2026 if (inner_mode_iaf) {
2027 if (inner_mode_iaf->flags & XFRM_MODE_FLAG_TUNNEL)
2028 x->inner_mode_iaf = inner_mode_iaf;
2029 else
2030 xfrm_put_mode(inner_mode_iaf);
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002031 }
2032 }
Herbert Xu13996372007-10-17 21:35:51 -07002033
Herbert Xud094cd82005-06-20 13:19:41 -07002034 x->type = xfrm_get_type(x->id.proto, family);
Herbert Xu72cb6962005-06-20 13:18:08 -07002035 if (x->type == NULL)
2036 goto error;
2037
2038 err = x->type->init_state(x);
2039 if (err)
2040 goto error;
2041
Herbert Xu13996372007-10-17 21:35:51 -07002042 x->outer_mode = xfrm_get_mode(x->props.mode, family);
2043 if (x->outer_mode == NULL)
Herbert Xub59f45d2006-05-27 23:05:54 -07002044 goto error;
2045
Herbert Xu72cb6962005-06-20 13:18:08 -07002046 x->km.state = XFRM_STATE_VALID;
2047
2048error:
2049 return err;
2050}
2051
2052EXPORT_SYMBOL(xfrm_init_state);
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09002053
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002054int __net_init xfrm_state_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055{
David S. Millerf034b5d2006-08-24 03:08:07 -07002056 unsigned int sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057
Alexey Dobriyan9d4139c2008-11-25 17:16:11 -08002058 INIT_LIST_HEAD(&net->xfrm.state_all);
2059
David S. Millerf034b5d2006-08-24 03:08:07 -07002060 sz = sizeof(struct hlist_head) * 8;
2061
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002062 net->xfrm.state_bydst = xfrm_hash_alloc(sz);
2063 if (!net->xfrm.state_bydst)
2064 goto out_bydst;
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002065 net->xfrm.state_bysrc = xfrm_hash_alloc(sz);
2066 if (!net->xfrm.state_bysrc)
2067 goto out_bysrc;
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002068 net->xfrm.state_byspi = xfrm_hash_alloc(sz);
2069 if (!net->xfrm.state_byspi)
2070 goto out_byspi;
Alexey Dobriyan529983e2008-11-25 17:18:12 -08002071 net->xfrm.state_hmask = ((sz / sizeof(struct hlist_head)) - 1);
David S. Millerf034b5d2006-08-24 03:08:07 -07002072
Alexey Dobriyan0bf7c5b2008-11-25 17:18:39 -08002073 net->xfrm.state_num = 0;
Alexey Dobriyan63082732008-11-25 17:19:07 -08002074 INIT_WORK(&net->xfrm.state_hash_work, xfrm_hash_resize);
Alexey Dobriyanb8a0ae22008-11-25 17:20:11 -08002075 INIT_HLIST_HEAD(&net->xfrm.state_gc_list);
Alexey Dobriyanc7837142008-11-25 17:20:36 -08002076 INIT_WORK(&net->xfrm.state_gc_work, xfrm_state_gc_task);
Alexey Dobriyan50a30652008-11-25 17:21:01 -08002077 init_waitqueue_head(&net->xfrm.km_waitq);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002078 return 0;
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002079
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002080out_byspi:
2081 xfrm_hash_free(net->xfrm.state_bysrc, sz);
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002082out_bysrc:
2083 xfrm_hash_free(net->xfrm.state_bydst, sz);
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002084out_bydst:
2085 return -ENOMEM;
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002086}
2087
2088void xfrm_state_fini(struct net *net)
2089{
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002090 struct xfrm_audit audit_info;
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002091 unsigned int sz;
2092
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002093 flush_work(&net->xfrm.state_hash_work);
2094 audit_info.loginuid = -1;
2095 audit_info.sessionid = -1;
2096 audit_info.secid = 0;
2097 xfrm_state_flush(net, IPSEC_PROTO_ANY, &audit_info);
2098 flush_work(&net->xfrm.state_gc_work);
2099
Alexey Dobriyan9d4139c2008-11-25 17:16:11 -08002100 WARN_ON(!list_empty(&net->xfrm.state_all));
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002101
Alexey Dobriyan529983e2008-11-25 17:18:12 -08002102 sz = (net->xfrm.state_hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002103 WARN_ON(!hlist_empty(net->xfrm.state_byspi));
2104 xfrm_hash_free(net->xfrm.state_byspi, sz);
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002105 WARN_ON(!hlist_empty(net->xfrm.state_bysrc));
2106 xfrm_hash_free(net->xfrm.state_bysrc, sz);
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002107 WARN_ON(!hlist_empty(net->xfrm.state_bydst));
2108 xfrm_hash_free(net->xfrm.state_bydst, sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109}
2110
Joy Lattenab5f5e82007-09-17 11:51:22 -07002111#ifdef CONFIG_AUDITSYSCALL
Ilpo Järvinencf35f432008-01-05 23:13:20 -08002112static void xfrm_audit_helper_sainfo(struct xfrm_state *x,
2113 struct audit_buffer *audit_buf)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002114{
Paul Moore68277ac2007-12-20 20:49:33 -08002115 struct xfrm_sec_ctx *ctx = x->security;
2116 u32 spi = ntohl(x->id.spi);
2117
2118 if (ctx)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002119 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
Paul Moore68277ac2007-12-20 20:49:33 -08002120 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002121
2122 switch(x->props.family) {
2123 case AF_INET:
Harvey Harrison21454aa2008-10-31 00:54:56 -07002124 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2125 &x->props.saddr.a4, &x->id.daddr.a4);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002126 break;
2127 case AF_INET6:
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002128 audit_log_format(audit_buf, " src=%pI6 dst=%pI6",
Harvey Harrisonfdb46ee2008-10-28 16:10:17 -07002129 x->props.saddr.a6, x->id.daddr.a6);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002130 break;
2131 }
Paul Moore68277ac2007-12-20 20:49:33 -08002132
2133 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002134}
2135
Ilpo Järvinencf35f432008-01-05 23:13:20 -08002136static void xfrm_audit_helper_pktinfo(struct sk_buff *skb, u16 family,
2137 struct audit_buffer *audit_buf)
Paul Mooreafeb14b2007-12-21 14:58:11 -08002138{
2139 struct iphdr *iph4;
2140 struct ipv6hdr *iph6;
2141
2142 switch (family) {
2143 case AF_INET:
2144 iph4 = ip_hdr(skb);
Harvey Harrison21454aa2008-10-31 00:54:56 -07002145 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2146 &iph4->saddr, &iph4->daddr);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002147 break;
2148 case AF_INET6:
2149 iph6 = ipv6_hdr(skb);
2150 audit_log_format(audit_buf,
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002151 " src=%pI6 dst=%pI6 flowlbl=0x%x%02x%02x",
Harvey Harrisonfdb46ee2008-10-28 16:10:17 -07002152 &iph6->saddr,&iph6->daddr,
Paul Mooreafeb14b2007-12-21 14:58:11 -08002153 iph6->flow_lbl[0] & 0x0f,
2154 iph6->flow_lbl[1],
2155 iph6->flow_lbl[2]);
2156 break;
2157 }
2158}
2159
Paul Moore68277ac2007-12-20 20:49:33 -08002160void xfrm_audit_state_add(struct xfrm_state *x, int result,
Eric Paris25323862008-04-18 10:09:25 -04002161 uid_t auid, u32 sessionid, u32 secid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002162{
2163 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002164
Paul Mooreafeb14b2007-12-21 14:58:11 -08002165 audit_buf = xfrm_audit_start("SAD-add");
Joy Lattenab5f5e82007-09-17 11:51:22 -07002166 if (audit_buf == NULL)
2167 return;
Eric Paris25323862008-04-18 10:09:25 -04002168 xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002169 xfrm_audit_helper_sainfo(x, audit_buf);
2170 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002171 audit_log_end(audit_buf);
2172}
2173EXPORT_SYMBOL_GPL(xfrm_audit_state_add);
2174
Paul Moore68277ac2007-12-20 20:49:33 -08002175void xfrm_audit_state_delete(struct xfrm_state *x, int result,
Eric Paris25323862008-04-18 10:09:25 -04002176 uid_t auid, u32 sessionid, u32 secid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002177{
2178 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002179
Paul Mooreafeb14b2007-12-21 14:58:11 -08002180 audit_buf = xfrm_audit_start("SAD-delete");
Joy Lattenab5f5e82007-09-17 11:51:22 -07002181 if (audit_buf == NULL)
2182 return;
Eric Paris25323862008-04-18 10:09:25 -04002183 xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002184 xfrm_audit_helper_sainfo(x, audit_buf);
2185 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002186 audit_log_end(audit_buf);
2187}
2188EXPORT_SYMBOL_GPL(xfrm_audit_state_delete);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002189
2190void xfrm_audit_state_replay_overflow(struct xfrm_state *x,
2191 struct sk_buff *skb)
2192{
2193 struct audit_buffer *audit_buf;
2194 u32 spi;
2195
2196 audit_buf = xfrm_audit_start("SA-replay-overflow");
2197 if (audit_buf == NULL)
2198 return;
2199 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2200 /* don't record the sequence number because it's inherent in this kind
2201 * of audit message */
2202 spi = ntohl(x->id.spi);
2203 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
2204 audit_log_end(audit_buf);
2205}
2206EXPORT_SYMBOL_GPL(xfrm_audit_state_replay_overflow);
2207
2208static void xfrm_audit_state_replay(struct xfrm_state *x,
2209 struct sk_buff *skb, __be32 net_seq)
2210{
2211 struct audit_buffer *audit_buf;
2212 u32 spi;
2213
2214 audit_buf = xfrm_audit_start("SA-replayed-pkt");
2215 if (audit_buf == NULL)
2216 return;
2217 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2218 spi = ntohl(x->id.spi);
2219 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2220 spi, spi, ntohl(net_seq));
2221 audit_log_end(audit_buf);
2222}
2223
2224void xfrm_audit_state_notfound_simple(struct sk_buff *skb, u16 family)
2225{
2226 struct audit_buffer *audit_buf;
2227
2228 audit_buf = xfrm_audit_start("SA-notfound");
2229 if (audit_buf == NULL)
2230 return;
2231 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2232 audit_log_end(audit_buf);
2233}
2234EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound_simple);
2235
2236void xfrm_audit_state_notfound(struct sk_buff *skb, u16 family,
2237 __be32 net_spi, __be32 net_seq)
2238{
2239 struct audit_buffer *audit_buf;
2240 u32 spi;
2241
2242 audit_buf = xfrm_audit_start("SA-notfound");
2243 if (audit_buf == NULL)
2244 return;
2245 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2246 spi = ntohl(net_spi);
2247 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2248 spi, spi, ntohl(net_seq));
2249 audit_log_end(audit_buf);
2250}
2251EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound);
2252
2253void xfrm_audit_state_icvfail(struct xfrm_state *x,
2254 struct sk_buff *skb, u8 proto)
2255{
2256 struct audit_buffer *audit_buf;
2257 __be32 net_spi;
2258 __be32 net_seq;
2259
2260 audit_buf = xfrm_audit_start("SA-icv-failure");
2261 if (audit_buf == NULL)
2262 return;
2263 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2264 if (xfrm_parse_spi(skb, proto, &net_spi, &net_seq) == 0) {
2265 u32 spi = ntohl(net_spi);
2266 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2267 spi, spi, ntohl(net_seq));
2268 }
2269 audit_log_end(audit_buf);
2270}
2271EXPORT_SYMBOL_GPL(xfrm_audit_state_icvfail);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002272#endif /* CONFIG_AUDITSYSCALL */