blob: 8a57a1ee538ca78eabdbaec23462cfbd670e2539 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * xfrm_state.c
3 *
4 * Changes:
5 * Mitsuru KANDA @USAGI
6 * Kazunori MIYAZAWA @USAGI
7 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
8 * IPv6 support
9 * YOSHIFUJI Hideaki @USAGI
10 * Split up af-specific functions
11 * Derek Atkins <derek@ihtfp.com>
12 * Add UDP Encapsulation
Trent Jaegerdf718372005-12-13 23:12:27 -080013 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 */
15
16#include <linux/workqueue.h>
17#include <net/xfrm.h>
18#include <linux/pfkeyv2.h>
19#include <linux/ipsec.h>
20#include <linux/module.h>
David S. Millerf034b5d2006-08-24 03:08:07 -070021#include <linux/cache.h>
Paul Moore68277ac2007-12-20 20:49:33 -080022#include <linux/audit.h>
Jesper Juhlb5890d82007-08-10 15:20:21 -070023#include <asm/uaccess.h>
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -080024#include <linux/ktime.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -080026#include <linux/interrupt.h>
27#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
David S. Miller44e36b42006-08-24 04:50:50 -070029#include "xfrm_hash.h"
30
Linus Torvalds1da177e2005-04-16 15:20:36 -070031/* Each xfrm_state may be linked to two tables:
32
33 1. Hash table by (spi,daddr,ah/esp) to find SA by SPI. (input,ctl)
David S. Millera624c102006-08-24 03:24:33 -070034 2. Hash table by (daddr,family,reqid) to find what SAs exist for given
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 destination/tunnel endpoint. (output)
36 */
37
38static DEFINE_SPINLOCK(xfrm_state_lock);
39
David S. Millerf034b5d2006-08-24 03:08:07 -070040static unsigned int xfrm_state_hashmax __read_mostly = 1 * 1024 * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Herbert Xu17c2a422007-10-17 21:33:12 -070042static struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family);
43static void xfrm_state_put_afinfo(struct xfrm_state_afinfo *afinfo);
44
Paul Mooreafeb14b2007-12-21 14:58:11 -080045#ifdef CONFIG_AUDITSYSCALL
46static void xfrm_audit_state_replay(struct xfrm_state *x,
47 struct sk_buff *skb, __be32 net_seq);
48#else
49#define xfrm_audit_state_replay(x, s, sq) do { ; } while (0)
50#endif /* CONFIG_AUDITSYSCALL */
51
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -080052static inline unsigned int xfrm_dst_hash(struct net *net,
David S. Miller2ab38502011-02-24 01:47:16 -050053 const xfrm_address_t *daddr,
54 const xfrm_address_t *saddr,
David S. Millerc1969f22006-08-24 04:00:03 -070055 u32 reqid,
David S. Millera624c102006-08-24 03:24:33 -070056 unsigned short family)
57{
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -080058 return __xfrm_dst_hash(daddr, saddr, reqid, family, net->xfrm.state_hmask);
David S. Millera624c102006-08-24 03:24:33 -070059}
60
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -080061static inline unsigned int xfrm_src_hash(struct net *net,
David S. Miller2ab38502011-02-24 01:47:16 -050062 const xfrm_address_t *daddr,
63 const xfrm_address_t *saddr,
David S. Miller44e36b42006-08-24 04:50:50 -070064 unsigned short family)
David S. Millerf034b5d2006-08-24 03:08:07 -070065{
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -080066 return __xfrm_src_hash(daddr, saddr, family, net->xfrm.state_hmask);
David S. Millerf034b5d2006-08-24 03:08:07 -070067}
68
David S. Miller2575b652006-08-24 03:26:44 -070069static inline unsigned int
David S. Miller2ab38502011-02-24 01:47:16 -050070xfrm_spi_hash(struct net *net, const xfrm_address_t *daddr,
71 __be32 spi, u8 proto, unsigned short family)
David S. Millerf034b5d2006-08-24 03:08:07 -070072{
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -080073 return __xfrm_spi_hash(daddr, spi, proto, family, net->xfrm.state_hmask);
David S. Millerf034b5d2006-08-24 03:08:07 -070074}
75
David S. Millerf034b5d2006-08-24 03:08:07 -070076static void xfrm_hash_transfer(struct hlist_head *list,
77 struct hlist_head *ndsttable,
78 struct hlist_head *nsrctable,
79 struct hlist_head *nspitable,
80 unsigned int nhashmask)
81{
82 struct hlist_node *entry, *tmp;
83 struct xfrm_state *x;
84
85 hlist_for_each_entry_safe(x, entry, tmp, list, bydst) {
86 unsigned int h;
87
David S. Millerc1969f22006-08-24 04:00:03 -070088 h = __xfrm_dst_hash(&x->id.daddr, &x->props.saddr,
89 x->props.reqid, x->props.family,
90 nhashmask);
David S. Millerf034b5d2006-08-24 03:08:07 -070091 hlist_add_head(&x->bydst, ndsttable+h);
92
Masahide NAKAMURA667bbcb2006-10-03 15:56:09 -070093 h = __xfrm_src_hash(&x->id.daddr, &x->props.saddr,
94 x->props.family,
David S. Millerf034b5d2006-08-24 03:08:07 -070095 nhashmask);
96 hlist_add_head(&x->bysrc, nsrctable+h);
97
Masahide NAKAMURA7b4dc3602006-09-27 22:21:52 -070098 if (x->id.spi) {
99 h = __xfrm_spi_hash(&x->id.daddr, x->id.spi,
100 x->id.proto, x->props.family,
101 nhashmask);
102 hlist_add_head(&x->byspi, nspitable+h);
103 }
David S. Millerf034b5d2006-08-24 03:08:07 -0700104 }
105}
106
Alexey Dobriyan63082732008-11-25 17:19:07 -0800107static unsigned long xfrm_hash_new_size(unsigned int state_hmask)
David S. Millerf034b5d2006-08-24 03:08:07 -0700108{
Alexey Dobriyan63082732008-11-25 17:19:07 -0800109 return ((state_hmask + 1) << 1) * sizeof(struct hlist_head);
David S. Millerf034b5d2006-08-24 03:08:07 -0700110}
111
112static DEFINE_MUTEX(hash_resize_mutex);
113
Alexey Dobriyan63082732008-11-25 17:19:07 -0800114static void xfrm_hash_resize(struct work_struct *work)
David S. Millerf034b5d2006-08-24 03:08:07 -0700115{
Alexey Dobriyan63082732008-11-25 17:19:07 -0800116 struct net *net = container_of(work, struct net, xfrm.state_hash_work);
David S. Millerf034b5d2006-08-24 03:08:07 -0700117 struct hlist_head *ndst, *nsrc, *nspi, *odst, *osrc, *ospi;
118 unsigned long nsize, osize;
119 unsigned int nhashmask, ohashmask;
120 int i;
121
122 mutex_lock(&hash_resize_mutex);
123
Alexey Dobriyan63082732008-11-25 17:19:07 -0800124 nsize = xfrm_hash_new_size(net->xfrm.state_hmask);
David S. Miller44e36b42006-08-24 04:50:50 -0700125 ndst = xfrm_hash_alloc(nsize);
David S. Millerf034b5d2006-08-24 03:08:07 -0700126 if (!ndst)
127 goto out_unlock;
David S. Miller44e36b42006-08-24 04:50:50 -0700128 nsrc = xfrm_hash_alloc(nsize);
David S. Millerf034b5d2006-08-24 03:08:07 -0700129 if (!nsrc) {
David S. Miller44e36b42006-08-24 04:50:50 -0700130 xfrm_hash_free(ndst, nsize);
David S. Millerf034b5d2006-08-24 03:08:07 -0700131 goto out_unlock;
132 }
David S. Miller44e36b42006-08-24 04:50:50 -0700133 nspi = xfrm_hash_alloc(nsize);
David S. Millerf034b5d2006-08-24 03:08:07 -0700134 if (!nspi) {
David S. Miller44e36b42006-08-24 04:50:50 -0700135 xfrm_hash_free(ndst, nsize);
136 xfrm_hash_free(nsrc, nsize);
David S. Millerf034b5d2006-08-24 03:08:07 -0700137 goto out_unlock;
138 }
139
140 spin_lock_bh(&xfrm_state_lock);
141
142 nhashmask = (nsize / sizeof(struct hlist_head)) - 1U;
Alexey Dobriyan63082732008-11-25 17:19:07 -0800143 for (i = net->xfrm.state_hmask; i >= 0; i--)
144 xfrm_hash_transfer(net->xfrm.state_bydst+i, ndst, nsrc, nspi,
David S. Millerf034b5d2006-08-24 03:08:07 -0700145 nhashmask);
146
Alexey Dobriyan63082732008-11-25 17:19:07 -0800147 odst = net->xfrm.state_bydst;
148 osrc = net->xfrm.state_bysrc;
149 ospi = net->xfrm.state_byspi;
150 ohashmask = net->xfrm.state_hmask;
David S. Millerf034b5d2006-08-24 03:08:07 -0700151
Alexey Dobriyan63082732008-11-25 17:19:07 -0800152 net->xfrm.state_bydst = ndst;
153 net->xfrm.state_bysrc = nsrc;
154 net->xfrm.state_byspi = nspi;
155 net->xfrm.state_hmask = nhashmask;
David S. Millerf034b5d2006-08-24 03:08:07 -0700156
157 spin_unlock_bh(&xfrm_state_lock);
158
159 osize = (ohashmask + 1) * sizeof(struct hlist_head);
David S. Miller44e36b42006-08-24 04:50:50 -0700160 xfrm_hash_free(odst, osize);
161 xfrm_hash_free(osrc, osize);
162 xfrm_hash_free(ospi, osize);
David S. Millerf034b5d2006-08-24 03:08:07 -0700163
164out_unlock:
165 mutex_unlock(&hash_resize_mutex);
166}
167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168static DEFINE_RWLOCK(xfrm_state_afinfo_lock);
169static struct xfrm_state_afinfo *xfrm_state_afinfo[NPROTO];
170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171static DEFINE_SPINLOCK(xfrm_state_gc_lock);
172
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -0800173int __xfrm_state_delete(struct xfrm_state *x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -0800175int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol);
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -0800176void km_state_expired(struct xfrm_state *x, int hard, u32 pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700178static struct xfrm_state_afinfo *xfrm_state_lock_afinfo(unsigned int family)
179{
180 struct xfrm_state_afinfo *afinfo;
181 if (unlikely(family >= NPROTO))
182 return NULL;
183 write_lock_bh(&xfrm_state_afinfo_lock);
184 afinfo = xfrm_state_afinfo[family];
185 if (unlikely(!afinfo))
186 write_unlock_bh(&xfrm_state_afinfo_lock);
187 return afinfo;
188}
189
190static void xfrm_state_unlock_afinfo(struct xfrm_state_afinfo *afinfo)
Eric Dumazet9a429c42008-01-01 21:58:02 -0800191 __releases(xfrm_state_afinfo_lock)
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700192{
193 write_unlock_bh(&xfrm_state_afinfo_lock);
194}
195
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800196int xfrm_register_type(const struct xfrm_type *type, unsigned short family)
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700197{
198 struct xfrm_state_afinfo *afinfo = xfrm_state_lock_afinfo(family);
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800199 const struct xfrm_type **typemap;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700200 int err = 0;
201
202 if (unlikely(afinfo == NULL))
203 return -EAFNOSUPPORT;
204 typemap = afinfo->type_map;
205
206 if (likely(typemap[type->proto] == NULL))
207 typemap[type->proto] = type;
208 else
209 err = -EEXIST;
210 xfrm_state_unlock_afinfo(afinfo);
211 return err;
212}
213EXPORT_SYMBOL(xfrm_register_type);
214
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800215int xfrm_unregister_type(const struct xfrm_type *type, unsigned short family)
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700216{
217 struct xfrm_state_afinfo *afinfo = xfrm_state_lock_afinfo(family);
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800218 const struct xfrm_type **typemap;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700219 int err = 0;
220
221 if (unlikely(afinfo == NULL))
222 return -EAFNOSUPPORT;
223 typemap = afinfo->type_map;
224
225 if (unlikely(typemap[type->proto] != type))
226 err = -ENOENT;
227 else
228 typemap[type->proto] = NULL;
229 xfrm_state_unlock_afinfo(afinfo);
230 return err;
231}
232EXPORT_SYMBOL(xfrm_unregister_type);
233
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800234static const struct xfrm_type *xfrm_get_type(u8 proto, unsigned short family)
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700235{
236 struct xfrm_state_afinfo *afinfo;
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800237 const struct xfrm_type **typemap;
238 const struct xfrm_type *type;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700239 int modload_attempted = 0;
240
241retry:
242 afinfo = xfrm_state_get_afinfo(family);
243 if (unlikely(afinfo == NULL))
244 return NULL;
245 typemap = afinfo->type_map;
246
247 type = typemap[proto];
248 if (unlikely(type && !try_module_get(type->owner)))
249 type = NULL;
250 if (!type && !modload_attempted) {
251 xfrm_state_put_afinfo(afinfo);
252 request_module("xfrm-type-%d-%d", family, proto);
253 modload_attempted = 1;
254 goto retry;
255 }
256
257 xfrm_state_put_afinfo(afinfo);
258 return type;
259}
260
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800261static void xfrm_put_type(const struct xfrm_type *type)
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700262{
263 module_put(type->owner);
264}
265
266int xfrm_register_mode(struct xfrm_mode *mode, int family)
267{
268 struct xfrm_state_afinfo *afinfo;
269 struct xfrm_mode **modemap;
270 int err;
271
272 if (unlikely(mode->encap >= XFRM_MODE_MAX))
273 return -EINVAL;
274
275 afinfo = xfrm_state_lock_afinfo(family);
276 if (unlikely(afinfo == NULL))
277 return -EAFNOSUPPORT;
278
279 err = -EEXIST;
280 modemap = afinfo->mode_map;
Herbert Xu17c2a422007-10-17 21:33:12 -0700281 if (modemap[mode->encap])
282 goto out;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700283
Herbert Xu17c2a422007-10-17 21:33:12 -0700284 err = -ENOENT;
285 if (!try_module_get(afinfo->owner))
286 goto out;
287
288 mode->afinfo = afinfo;
289 modemap[mode->encap] = mode;
290 err = 0;
291
292out:
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700293 xfrm_state_unlock_afinfo(afinfo);
294 return err;
295}
296EXPORT_SYMBOL(xfrm_register_mode);
297
298int xfrm_unregister_mode(struct xfrm_mode *mode, int family)
299{
300 struct xfrm_state_afinfo *afinfo;
301 struct xfrm_mode **modemap;
302 int err;
303
304 if (unlikely(mode->encap >= XFRM_MODE_MAX))
305 return -EINVAL;
306
307 afinfo = xfrm_state_lock_afinfo(family);
308 if (unlikely(afinfo == NULL))
309 return -EAFNOSUPPORT;
310
311 err = -ENOENT;
312 modemap = afinfo->mode_map;
313 if (likely(modemap[mode->encap] == mode)) {
314 modemap[mode->encap] = NULL;
Herbert Xu17c2a422007-10-17 21:33:12 -0700315 module_put(mode->afinfo->owner);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700316 err = 0;
317 }
318
319 xfrm_state_unlock_afinfo(afinfo);
320 return err;
321}
322EXPORT_SYMBOL(xfrm_unregister_mode);
323
324static struct xfrm_mode *xfrm_get_mode(unsigned int encap, int family)
325{
326 struct xfrm_state_afinfo *afinfo;
327 struct xfrm_mode *mode;
328 int modload_attempted = 0;
329
330 if (unlikely(encap >= XFRM_MODE_MAX))
331 return NULL;
332
333retry:
334 afinfo = xfrm_state_get_afinfo(family);
335 if (unlikely(afinfo == NULL))
336 return NULL;
337
338 mode = afinfo->mode_map[encap];
339 if (unlikely(mode && !try_module_get(mode->owner)))
340 mode = NULL;
341 if (!mode && !modload_attempted) {
342 xfrm_state_put_afinfo(afinfo);
343 request_module("xfrm-mode-%d-%d", family, encap);
344 modload_attempted = 1;
345 goto retry;
346 }
347
348 xfrm_state_put_afinfo(afinfo);
349 return mode;
350}
351
352static void xfrm_put_mode(struct xfrm_mode *mode)
353{
354 module_put(mode->owner);
355}
356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357static void xfrm_state_gc_destroy(struct xfrm_state *x)
358{
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800359 tasklet_hrtimer_cancel(&x->mtimer);
David S. Millera47f0ce2006-08-24 03:54:22 -0700360 del_timer_sync(&x->rtimer);
Jesper Juhla51482b2005-11-08 09:41:34 -0800361 kfree(x->aalg);
362 kfree(x->ealg);
363 kfree(x->calg);
364 kfree(x->encap);
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700365 kfree(x->coaddr);
Herbert Xu13996372007-10-17 21:35:51 -0700366 if (x->inner_mode)
367 xfrm_put_mode(x->inner_mode);
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700368 if (x->inner_mode_iaf)
369 xfrm_put_mode(x->inner_mode_iaf);
Herbert Xu13996372007-10-17 21:35:51 -0700370 if (x->outer_mode)
371 xfrm_put_mode(x->outer_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 if (x->type) {
373 x->type->destructor(x);
374 xfrm_put_type(x->type);
375 }
Trent Jaegerdf718372005-12-13 23:12:27 -0800376 security_xfrm_state_free(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 kfree(x);
378}
379
Alexey Dobriyanc7837142008-11-25 17:20:36 -0800380static void xfrm_state_gc_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
Alexey Dobriyanc7837142008-11-25 17:20:36 -0800382 struct net *net = container_of(work, struct net, xfrm.state_gc_work);
Herbert Xu12a169e2008-10-01 07:03:24 -0700383 struct xfrm_state *x;
384 struct hlist_node *entry, *tmp;
385 struct hlist_head gc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 spin_lock_bh(&xfrm_state_gc_lock);
Alexey Dobriyanc7837142008-11-25 17:20:36 -0800388 hlist_move_list(&net->xfrm.state_gc_list, &gc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 spin_unlock_bh(&xfrm_state_gc_lock);
390
Herbert Xu12a169e2008-10-01 07:03:24 -0700391 hlist_for_each_entry_safe(x, entry, tmp, &gc_list, gclist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 xfrm_state_gc_destroy(x);
David S. Miller8f126e32006-08-24 02:45:07 -0700393
Alexey Dobriyan50a30652008-11-25 17:21:01 -0800394 wake_up(&net->xfrm.km_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395}
396
397static inline unsigned long make_jiffies(long secs)
398{
399 if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
400 return MAX_SCHEDULE_TIMEOUT-1;
401 else
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900402 return secs*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403}
404
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800405static enum hrtimer_restart xfrm_timer_handler(struct hrtimer * me)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406{
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800407 struct tasklet_hrtimer *thr = container_of(me, struct tasklet_hrtimer, timer);
408 struct xfrm_state *x = container_of(thr, struct xfrm_state, mtimer);
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800409 struct net *net = xs_net(x);
James Morris9d729f72007-03-04 16:12:44 -0800410 unsigned long now = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 long next = LONG_MAX;
412 int warn = 0;
Joy Latten161a09e2006-11-27 13:11:54 -0600413 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
415 spin_lock(&x->lock);
416 if (x->km.state == XFRM_STATE_DEAD)
417 goto out;
418 if (x->km.state == XFRM_STATE_EXPIRED)
419 goto expired;
420 if (x->lft.hard_add_expires_seconds) {
421 long tmo = x->lft.hard_add_expires_seconds +
422 x->curlft.add_time - now;
423 if (tmo <= 0)
424 goto expired;
425 if (tmo < next)
426 next = tmo;
427 }
428 if (x->lft.hard_use_expires_seconds) {
429 long tmo = x->lft.hard_use_expires_seconds +
430 (x->curlft.use_time ? : now) - now;
431 if (tmo <= 0)
432 goto expired;
433 if (tmo < next)
434 next = tmo;
435 }
436 if (x->km.dying)
437 goto resched;
438 if (x->lft.soft_add_expires_seconds) {
439 long tmo = x->lft.soft_add_expires_seconds +
440 x->curlft.add_time - now;
441 if (tmo <= 0)
442 warn = 1;
443 else if (tmo < next)
444 next = tmo;
445 }
446 if (x->lft.soft_use_expires_seconds) {
447 long tmo = x->lft.soft_use_expires_seconds +
448 (x->curlft.use_time ? : now) - now;
449 if (tmo <= 0)
450 warn = 1;
451 else if (tmo < next)
452 next = tmo;
453 }
454
Herbert Xu4666faa2005-06-18 22:43:22 -0700455 x->km.dying = warn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 if (warn)
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -0800457 km_state_expired(x, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458resched:
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800459 if (next != LONG_MAX){
460 tasklet_hrtimer_start(&x->mtimer, ktime_set(next, 0), HRTIMER_MODE_REL);
461 }
David S. Millera47f0ce2006-08-24 03:54:22 -0700462
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 goto out;
464
465expired:
466 if (x->km.state == XFRM_STATE_ACQ && x->id.spi == 0) {
467 x->km.state = XFRM_STATE_EXPIRED;
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800468 wake_up(&net->xfrm.km_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 next = 2;
470 goto resched;
471 }
Joy Latten161a09e2006-11-27 13:11:54 -0600472
473 err = __xfrm_state_delete(x);
474 if (!err && x->id.spi)
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -0800475 km_state_expired(x, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Joy Lattenab5f5e82007-09-17 11:51:22 -0700477 xfrm_audit_state_delete(x, err ? 0 : 1,
Eric Paris25323862008-04-18 10:09:25 -0400478 audit_get_loginuid(current),
479 audit_get_sessionid(current), 0);
Joy Latten161a09e2006-11-27 13:11:54 -0600480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481out:
482 spin_unlock(&x->lock);
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800483 return HRTIMER_NORESTART;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484}
485
David S. Miller0ac84752006-03-20 19:18:23 -0800486static void xfrm_replay_timer_handler(unsigned long data);
487
Alexey Dobriyan673c09b2008-11-25 17:15:16 -0800488struct xfrm_state *xfrm_state_alloc(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
490 struct xfrm_state *x;
491
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700492 x = kzalloc(sizeof(struct xfrm_state), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494 if (x) {
Alexey Dobriyan673c09b2008-11-25 17:15:16 -0800495 write_pnet(&x->xs_net, net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 atomic_set(&x->refcnt, 1);
497 atomic_set(&x->tunnel_users, 0);
Herbert Xu12a169e2008-10-01 07:03:24 -0700498 INIT_LIST_HEAD(&x->km.all);
David S. Miller8f126e32006-08-24 02:45:07 -0700499 INIT_HLIST_NODE(&x->bydst);
500 INIT_HLIST_NODE(&x->bysrc);
501 INIT_HLIST_NODE(&x->byspi);
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800502 tasklet_hrtimer_init(&x->mtimer, xfrm_timer_handler, CLOCK_REALTIME, HRTIMER_MODE_ABS);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800503 setup_timer(&x->rtimer, xfrm_replay_timer_handler,
504 (unsigned long)x);
James Morris9d729f72007-03-04 16:12:44 -0800505 x->curlft.add_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 x->lft.soft_byte_limit = XFRM_INF;
507 x->lft.soft_packet_limit = XFRM_INF;
508 x->lft.hard_byte_limit = XFRM_INF;
509 x->lft.hard_packet_limit = XFRM_INF;
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -0800510 x->replay_maxage = 0;
511 x->replay_maxdiff = 0;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700512 x->inner_mode = NULL;
513 x->inner_mode_iaf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 spin_lock_init(&x->lock);
515 }
516 return x;
517}
518EXPORT_SYMBOL(xfrm_state_alloc);
519
520void __xfrm_state_destroy(struct xfrm_state *x)
521{
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800522 struct net *net = xs_net(x);
523
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700524 WARN_ON(x->km.state != XFRM_STATE_DEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
526 spin_lock_bh(&xfrm_state_gc_lock);
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800527 hlist_add_head(&x->gclist, &net->xfrm.state_gc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 spin_unlock_bh(&xfrm_state_gc_lock);
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800529 schedule_work(&net->xfrm.state_gc_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530}
531EXPORT_SYMBOL(__xfrm_state_destroy);
532
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -0800533int __xfrm_state_delete(struct xfrm_state *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534{
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800535 struct net *net = xs_net(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700536 int err = -ESRCH;
537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 if (x->km.state != XFRM_STATE_DEAD) {
539 x->km.state = XFRM_STATE_DEAD;
540 spin_lock(&xfrm_state_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -0700541 list_del(&x->km.all);
David S. Miller8f126e32006-08-24 02:45:07 -0700542 hlist_del(&x->bydst);
David S. Miller8f126e32006-08-24 02:45:07 -0700543 hlist_del(&x->bysrc);
David S. Millera47f0ce2006-08-24 03:54:22 -0700544 if (x->id.spi)
David S. Miller8f126e32006-08-24 02:45:07 -0700545 hlist_del(&x->byspi);
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800546 net->xfrm.state_num--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 spin_unlock(&xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 /* All xfrm_state objects are created by xfrm_state_alloc.
550 * The xfrm_state_alloc call gives a reference, and that
551 * is what we are dropping here.
552 */
Patrick McHardy5dba4792007-11-27 11:10:07 +0800553 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700554 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700556
557 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558}
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -0800559EXPORT_SYMBOL(__xfrm_state_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700561int xfrm_state_delete(struct xfrm_state *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700563 int err;
564
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 spin_lock_bh(&x->lock);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700566 err = __xfrm_state_delete(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 spin_unlock_bh(&x->lock);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700568
569 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570}
571EXPORT_SYMBOL(xfrm_state_delete);
572
Joy Latten4aa2e622007-06-04 19:05:57 -0400573#ifdef CONFIG_SECURITY_NETWORK_XFRM
574static inline int
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800575xfrm_state_flush_secctx_check(struct net *net, u8 proto, struct xfrm_audit *audit_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576{
Joy Latten4aa2e622007-06-04 19:05:57 -0400577 int i, err = 0;
578
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800579 for (i = 0; i <= net->xfrm.state_hmask; i++) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400580 struct hlist_node *entry;
581 struct xfrm_state *x;
582
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800583 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+i, bydst) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400584 if (xfrm_id_proto_match(x->id.proto, proto) &&
585 (err = security_xfrm_state_delete(x)) != 0) {
Joy Lattenab5f5e82007-09-17 11:51:22 -0700586 xfrm_audit_state_delete(x, 0,
587 audit_info->loginuid,
Eric Paris25323862008-04-18 10:09:25 -0400588 audit_info->sessionid,
Joy Lattenab5f5e82007-09-17 11:51:22 -0700589 audit_info->secid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400590 return err;
591 }
592 }
593 }
594
595 return err;
596}
597#else
598static inline int
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800599xfrm_state_flush_secctx_check(struct net *net, u8 proto, struct xfrm_audit *audit_info)
Joy Latten4aa2e622007-06-04 19:05:57 -0400600{
601 return 0;
602}
603#endif
604
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800605int xfrm_state_flush(struct net *net, u8 proto, struct xfrm_audit *audit_info)
Joy Latten4aa2e622007-06-04 19:05:57 -0400606{
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000607 int i, err = 0, cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
609 spin_lock_bh(&xfrm_state_lock);
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800610 err = xfrm_state_flush_secctx_check(net, proto, audit_info);
Joy Latten4aa2e622007-06-04 19:05:57 -0400611 if (err)
612 goto out;
613
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000614 err = -ESRCH;
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800615 for (i = 0; i <= net->xfrm.state_hmask; i++) {
David S. Miller8f126e32006-08-24 02:45:07 -0700616 struct hlist_node *entry;
617 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618restart:
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800619 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+i, bydst) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 if (!xfrm_state_kern(x) &&
Masahide NAKAMURA57947082006-09-22 15:06:24 -0700621 xfrm_id_proto_match(x->id.proto, proto)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 xfrm_state_hold(x);
623 spin_unlock_bh(&xfrm_state_lock);
624
Joy Latten161a09e2006-11-27 13:11:54 -0600625 err = xfrm_state_delete(x);
Joy Lattenab5f5e82007-09-17 11:51:22 -0700626 xfrm_audit_state_delete(x, err ? 0 : 1,
627 audit_info->loginuid,
Eric Paris25323862008-04-18 10:09:25 -0400628 audit_info->sessionid,
Joy Lattenab5f5e82007-09-17 11:51:22 -0700629 audit_info->secid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 xfrm_state_put(x);
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000631 if (!err)
632 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
634 spin_lock_bh(&xfrm_state_lock);
635 goto restart;
636 }
637 }
638 }
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000639 if (cnt)
640 err = 0;
Joy Latten4aa2e622007-06-04 19:05:57 -0400641
642out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 spin_unlock_bh(&xfrm_state_lock);
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800644 wake_up(&net->xfrm.km_waitq);
Joy Latten4aa2e622007-06-04 19:05:57 -0400645 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646}
647EXPORT_SYMBOL(xfrm_state_flush);
648
Alexey Dobriyane0710412010-01-23 13:37:10 +0000649void xfrm_sad_getinfo(struct net *net, struct xfrmk_sadinfo *si)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700650{
651 spin_lock_bh(&xfrm_state_lock);
Alexey Dobriyane0710412010-01-23 13:37:10 +0000652 si->sadcnt = net->xfrm.state_num;
653 si->sadhcnt = net->xfrm.state_hmask;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700654 si->sadhmcnt = xfrm_state_hashmax;
655 spin_unlock_bh(&xfrm_state_lock);
656}
657EXPORT_SYMBOL(xfrm_sad_getinfo);
658
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659static int
David S. Miller1a898592011-02-22 18:22:34 -0800660xfrm_init_tempstate(struct xfrm_state *x, const struct flowi *fl,
David S. Miller04686012011-02-24 01:50:12 -0500661 const struct xfrm_tmpl *tmpl,
Thomas Egerer8444cf72010-09-20 11:11:38 -0700662 xfrm_address_t *daddr, xfrm_address_t *saddr,
663 unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664{
665 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
666 if (!afinfo)
667 return -1;
Thomas Egerer8444cf72010-09-20 11:11:38 -0700668 afinfo->init_tempsel(&x->sel, fl);
669
670 if (family != tmpl->encap_family) {
671 xfrm_state_put_afinfo(afinfo);
672 afinfo = xfrm_state_get_afinfo(tmpl->encap_family);
673 if (!afinfo)
674 return -1;
675 }
676 afinfo->init_temprop(x, tmpl, daddr, saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 xfrm_state_put_afinfo(afinfo);
678 return 0;
679}
680
David S. Miller9aa60082011-02-24 01:51:36 -0500681static struct xfrm_state *__xfrm_state_lookup(struct net *net, u32 mark,
682 const xfrm_address_t *daddr,
683 __be32 spi, u8 proto,
684 unsigned short family)
David S. Milleredcd5822006-08-24 00:42:45 -0700685{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800686 unsigned int h = xfrm_spi_hash(net, daddr, spi, proto, family);
David S. Milleredcd5822006-08-24 00:42:45 -0700687 struct xfrm_state *x;
David S. Miller8f126e32006-08-24 02:45:07 -0700688 struct hlist_node *entry;
David S. Milleredcd5822006-08-24 00:42:45 -0700689
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800690 hlist_for_each_entry(x, entry, net->xfrm.state_byspi+h, byspi) {
David S. Milleredcd5822006-08-24 00:42:45 -0700691 if (x->props.family != family ||
692 x->id.spi != spi ||
Wei Yongjun18025712009-06-28 18:42:53 +0000693 x->id.proto != proto ||
694 xfrm_addr_cmp(&x->id.daddr, daddr, family))
David S. Milleredcd5822006-08-24 00:42:45 -0700695 continue;
696
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000697 if ((mark & x->mark.m) != x->mark.v)
698 continue;
David S. Milleredcd5822006-08-24 00:42:45 -0700699 xfrm_state_hold(x);
700 return x;
701 }
702
703 return NULL;
704}
705
David S. Miller9aa60082011-02-24 01:51:36 -0500706static struct xfrm_state *__xfrm_state_lookup_byaddr(struct net *net, u32 mark,
707 const xfrm_address_t *daddr,
708 const xfrm_address_t *saddr,
709 u8 proto, unsigned short family)
David S. Milleredcd5822006-08-24 00:42:45 -0700710{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800711 unsigned int h = xfrm_src_hash(net, daddr, saddr, family);
David S. Milleredcd5822006-08-24 00:42:45 -0700712 struct xfrm_state *x;
David S. Miller8f126e32006-08-24 02:45:07 -0700713 struct hlist_node *entry;
David S. Milleredcd5822006-08-24 00:42:45 -0700714
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800715 hlist_for_each_entry(x, entry, net->xfrm.state_bysrc+h, bysrc) {
David S. Milleredcd5822006-08-24 00:42:45 -0700716 if (x->props.family != family ||
Wei Yongjun18025712009-06-28 18:42:53 +0000717 x->id.proto != proto ||
718 xfrm_addr_cmp(&x->id.daddr, daddr, family) ||
719 xfrm_addr_cmp(&x->props.saddr, saddr, family))
David S. Milleredcd5822006-08-24 00:42:45 -0700720 continue;
721
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000722 if ((mark & x->mark.m) != x->mark.v)
723 continue;
David S. Milleredcd5822006-08-24 00:42:45 -0700724 xfrm_state_hold(x);
725 return x;
726 }
727
728 return NULL;
729}
730
731static inline struct xfrm_state *
732__xfrm_state_locate(struct xfrm_state *x, int use_spi, int family)
733{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800734 struct net *net = xs_net(x);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800735 u32 mark = x->mark.v & x->mark.m;
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800736
David S. Milleredcd5822006-08-24 00:42:45 -0700737 if (use_spi)
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800738 return __xfrm_state_lookup(net, mark, &x->id.daddr,
739 x->id.spi, x->id.proto, family);
David S. Milleredcd5822006-08-24 00:42:45 -0700740 else
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800741 return __xfrm_state_lookup_byaddr(net, mark,
742 &x->id.daddr,
David S. Milleredcd5822006-08-24 00:42:45 -0700743 &x->props.saddr,
744 x->id.proto, family);
745}
746
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800747static void xfrm_hash_grow_check(struct net *net, int have_hash_collision)
Patrick McHardy2fab22f2006-10-24 15:34:00 -0700748{
749 if (have_hash_collision &&
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800750 (net->xfrm.state_hmask + 1) < xfrm_state_hashmax &&
751 net->xfrm.state_num > net->xfrm.state_hmask)
752 schedule_work(&net->xfrm.state_hash_work);
Patrick McHardy2fab22f2006-10-24 15:34:00 -0700753}
754
David S. Miller08ec9af2009-03-13 14:22:40 -0700755static void xfrm_state_look_at(struct xfrm_policy *pol, struct xfrm_state *x,
David S. Miller4a08ab02011-02-22 18:21:31 -0800756 const struct flowi *fl, unsigned short family,
David S. Miller08ec9af2009-03-13 14:22:40 -0700757 xfrm_address_t *daddr, xfrm_address_t *saddr,
758 struct xfrm_state **best, int *acq_in_progress,
759 int *error)
760{
761 /* Resolution logic:
762 * 1. There is a valid state with matching selector. Done.
763 * 2. Valid state with inappropriate selector. Skip.
764 *
765 * Entering area of "sysdeps".
766 *
767 * 3. If state is not valid, selector is temporary, it selects
768 * only session which triggered previous resolution. Key
769 * manager will do something to install a state with proper
770 * selector.
771 */
772 if (x->km.state == XFRM_STATE_VALID) {
773 if ((x->sel.family &&
774 !xfrm_selector_match(&x->sel, fl, x->sel.family)) ||
775 !security_xfrm_state_pol_flow_match(x, pol, fl))
776 return;
777
778 if (!*best ||
779 (*best)->km.dying > x->km.dying ||
780 ((*best)->km.dying == x->km.dying &&
781 (*best)->curlft.add_time < x->curlft.add_time))
782 *best = x;
783 } else if (x->km.state == XFRM_STATE_ACQ) {
784 *acq_in_progress = 1;
785 } else if (x->km.state == XFRM_STATE_ERROR ||
786 x->km.state == XFRM_STATE_EXPIRED) {
787 if (xfrm_selector_match(&x->sel, fl, x->sel.family) &&
788 security_xfrm_state_pol_flow_match(x, pol, fl))
789 *error = -ESRCH;
790 }
791}
792
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793struct xfrm_state *
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900794xfrm_state_find(xfrm_address_t *daddr, xfrm_address_t *saddr,
David S. Millerb520e9f2011-02-22 18:24:19 -0800795 const struct flowi *fl, struct xfrm_tmpl *tmpl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 struct xfrm_policy *pol, int *err,
797 unsigned short family)
798{
David S. Miller08ec9af2009-03-13 14:22:40 -0700799 static xfrm_address_t saddr_wildcard = { };
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800800 struct net *net = xp_net(pol);
Nicolas Dichtel6a783c92009-04-27 02:58:59 -0700801 unsigned int h, h_wildcard;
David S. Miller8f126e32006-08-24 02:45:07 -0700802 struct hlist_node *entry;
David S. Miller37b08e32008-09-02 20:14:15 -0700803 struct xfrm_state *x, *x0, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 int acquire_in_progress = 0;
805 int error = 0;
806 struct xfrm_state *best = NULL;
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800807 u32 mark = pol->mark.v & pol->mark.m;
Thomas Egerer8444cf72010-09-20 11:11:38 -0700808 unsigned short encap_family = tmpl->encap_family;
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900809
David S. Miller37b08e32008-09-02 20:14:15 -0700810 to_put = NULL;
811
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 spin_lock_bh(&xfrm_state_lock);
Thomas Egerer8444cf72010-09-20 11:11:38 -0700813 h = xfrm_dst_hash(net, daddr, saddr, tmpl->reqid, encap_family);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800814 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+h, bydst) {
Thomas Egerer8444cf72010-09-20 11:11:38 -0700815 if (x->props.family == encap_family &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 x->props.reqid == tmpl->reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000817 (mark & x->mark.m) == x->mark.v &&
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -0700818 !(x->props.flags & XFRM_STATE_WILDRECV) &&
Thomas Egerer8444cf72010-09-20 11:11:38 -0700819 xfrm_state_addr_check(x, daddr, saddr, encap_family) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 tmpl->mode == x->props.mode &&
821 tmpl->id.proto == x->id.proto &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700822 (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
Thomas Egerer8444cf72010-09-20 11:11:38 -0700823 xfrm_state_look_at(pol, x, fl, encap_family, daddr, saddr,
David S. Miller08ec9af2009-03-13 14:22:40 -0700824 &best, &acquire_in_progress, &error);
825 }
826 if (best)
827 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
Thomas Egerer8444cf72010-09-20 11:11:38 -0700829 h_wildcard = xfrm_dst_hash(net, daddr, &saddr_wildcard, tmpl->reqid, encap_family);
Nicolas Dichtel6a783c92009-04-27 02:58:59 -0700830 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+h_wildcard, bydst) {
Thomas Egerer8444cf72010-09-20 11:11:38 -0700831 if (x->props.family == encap_family &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700832 x->props.reqid == tmpl->reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000833 (mark & x->mark.m) == x->mark.v &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700834 !(x->props.flags & XFRM_STATE_WILDRECV) &&
Thomas Egerer8444cf72010-09-20 11:11:38 -0700835 xfrm_state_addr_check(x, daddr, saddr, encap_family) &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700836 tmpl->mode == x->props.mode &&
837 tmpl->id.proto == x->id.proto &&
838 (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
Thomas Egerer8444cf72010-09-20 11:11:38 -0700839 xfrm_state_look_at(pol, x, fl, encap_family, daddr, saddr,
David S. Miller08ec9af2009-03-13 14:22:40 -0700840 &best, &acquire_in_progress, &error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 }
842
David S. Miller08ec9af2009-03-13 14:22:40 -0700843found:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 x = best;
845 if (!x && !error && !acquire_in_progress) {
Patrick McHardy5c5d2812005-04-21 20:12:32 -0700846 if (tmpl->id.spi &&
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800847 (x0 = __xfrm_state_lookup(net, mark, daddr, tmpl->id.spi,
Thomas Egerer8444cf72010-09-20 11:11:38 -0700848 tmpl->id.proto, encap_family)) != NULL) {
David S. Miller37b08e32008-09-02 20:14:15 -0700849 to_put = x0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 error = -EEXIST;
851 goto out;
852 }
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800853 x = xfrm_state_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 if (x == NULL) {
855 error = -ENOMEM;
856 goto out;
857 }
Thomas Egerer8444cf72010-09-20 11:11:38 -0700858 /* Initialize temporary state matching only
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 * to current session. */
Thomas Egerer8444cf72010-09-20 11:11:38 -0700860 xfrm_init_tempstate(x, fl, tmpl, daddr, saddr, family);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800861 memcpy(&x->mark, &pol->mark, sizeof(x->mark));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -0700863 error = security_xfrm_state_alloc_acquire(x, pol->security, fl->secid);
864 if (error) {
865 x->km.state = XFRM_STATE_DEAD;
David S. Miller37b08e32008-09-02 20:14:15 -0700866 to_put = x;
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -0700867 x = NULL;
868 goto out;
869 }
870
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 if (km_query(x, tmpl, pol) == 0) {
872 x->km.state = XFRM_STATE_ACQ;
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800873 list_add(&x->km.all, &net->xfrm.state_all);
874 hlist_add_head(&x->bydst, net->xfrm.state_bydst+h);
Thomas Egerer8444cf72010-09-20 11:11:38 -0700875 h = xfrm_src_hash(net, daddr, saddr, encap_family);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800876 hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 if (x->id.spi) {
Thomas Egerer8444cf72010-09-20 11:11:38 -0700878 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, encap_family);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800879 hlist_add_head(&x->byspi, net->xfrm.state_byspi+h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 }
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800881 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800882 tasklet_hrtimer_start(&x->mtimer, ktime_set(net->xfrm.sysctl_acq_expires, 0), HRTIMER_MODE_REL);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800883 net->xfrm.state_num++;
884 xfrm_hash_grow_check(net, x->bydst.next != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 } else {
886 x->km.state = XFRM_STATE_DEAD;
David S. Miller37b08e32008-09-02 20:14:15 -0700887 to_put = x;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 x = NULL;
889 error = -ESRCH;
890 }
891 }
892out:
893 if (x)
894 xfrm_state_hold(x);
895 else
896 *err = acquire_in_progress ? -EAGAIN : error;
897 spin_unlock_bh(&xfrm_state_lock);
David S. Miller37b08e32008-09-02 20:14:15 -0700898 if (to_put)
899 xfrm_state_put(to_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 return x;
901}
902
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700903struct xfrm_state *
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800904xfrm_stateonly_find(struct net *net, u32 mark,
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800905 xfrm_address_t *daddr, xfrm_address_t *saddr,
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700906 unsigned short family, u8 mode, u8 proto, u32 reqid)
907{
Pavel Emelyanov4bda4f22007-12-14 11:38:04 -0800908 unsigned int h;
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700909 struct xfrm_state *rx = NULL, *x = NULL;
910 struct hlist_node *entry;
911
912 spin_lock(&xfrm_state_lock);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800913 h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
914 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+h, bydst) {
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700915 if (x->props.family == family &&
916 x->props.reqid == reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000917 (mark & x->mark.m) == x->mark.v &&
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700918 !(x->props.flags & XFRM_STATE_WILDRECV) &&
919 xfrm_state_addr_check(x, daddr, saddr, family) &&
920 mode == x->props.mode &&
921 proto == x->id.proto &&
922 x->km.state == XFRM_STATE_VALID) {
923 rx = x;
924 break;
925 }
926 }
927
928 if (rx)
929 xfrm_state_hold(rx);
930 spin_unlock(&xfrm_state_lock);
931
932
933 return rx;
934}
935EXPORT_SYMBOL(xfrm_stateonly_find);
936
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937static void __xfrm_state_insert(struct xfrm_state *x)
938{
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800939 struct net *net = xs_net(x);
David S. Millera624c102006-08-24 03:24:33 -0700940 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800942 list_add(&x->km.all, &net->xfrm.state_all);
Timo Teras4c563f72008-02-28 21:31:08 -0800943
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800944 h = xfrm_dst_hash(net, &x->id.daddr, &x->props.saddr,
David S. Millerc1969f22006-08-24 04:00:03 -0700945 x->props.reqid, x->props.family);
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800946 hlist_add_head(&x->bydst, net->xfrm.state_bydst+h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800948 h = xfrm_src_hash(net, &x->id.daddr, &x->props.saddr, x->props.family);
949 hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
Masahide NAKAMURA7b4dc3602006-09-27 22:21:52 -0700951 if (x->id.spi) {
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800952 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto,
Masahide NAKAMURA6c44e6b2006-08-23 17:53:57 -0700953 x->props.family);
954
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800955 hlist_add_head(&x->byspi, net->xfrm.state_byspi+h);
Masahide NAKAMURA6c44e6b2006-08-23 17:53:57 -0700956 }
957
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800958 tasklet_hrtimer_start(&x->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL);
David S. Millera47f0ce2006-08-24 03:54:22 -0700959 if (x->replay_maxage)
960 mod_timer(&x->rtimer, jiffies + x->replay_maxage);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -0800961
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800962 wake_up(&net->xfrm.km_waitq);
David S. Millerf034b5d2006-08-24 03:08:07 -0700963
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800964 net->xfrm.state_num++;
David S. Millerf034b5d2006-08-24 03:08:07 -0700965
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800966 xfrm_hash_grow_check(net, x->bydst.next != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967}
968
David S. Millerc7f5ea32006-08-24 03:29:04 -0700969/* xfrm_state_lock is held */
970static void __xfrm_state_bump_genids(struct xfrm_state *xnew)
971{
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800972 struct net *net = xs_net(xnew);
David S. Millerc7f5ea32006-08-24 03:29:04 -0700973 unsigned short family = xnew->props.family;
974 u32 reqid = xnew->props.reqid;
975 struct xfrm_state *x;
976 struct hlist_node *entry;
977 unsigned int h;
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000978 u32 mark = xnew->mark.v & xnew->mark.m;
David S. Millerc7f5ea32006-08-24 03:29:04 -0700979
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800980 h = xfrm_dst_hash(net, &xnew->id.daddr, &xnew->props.saddr, reqid, family);
981 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+h, bydst) {
David S. Millerc7f5ea32006-08-24 03:29:04 -0700982 if (x->props.family == family &&
983 x->props.reqid == reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000984 (mark & x->mark.m) == x->mark.v &&
David S. Millerc1969f22006-08-24 04:00:03 -0700985 !xfrm_addr_cmp(&x->id.daddr, &xnew->id.daddr, family) &&
986 !xfrm_addr_cmp(&x->props.saddr, &xnew->props.saddr, family))
Herbert Xu34996cb2010-03-31 01:19:49 +0000987 x->genid++;
David S. Millerc7f5ea32006-08-24 03:29:04 -0700988 }
989}
990
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991void xfrm_state_insert(struct xfrm_state *x)
992{
993 spin_lock_bh(&xfrm_state_lock);
David S. Millerc7f5ea32006-08-24 03:29:04 -0700994 __xfrm_state_bump_genids(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 __xfrm_state_insert(x);
996 spin_unlock_bh(&xfrm_state_lock);
997}
998EXPORT_SYMBOL(xfrm_state_insert);
999
David S. Miller27708342006-08-24 00:13:10 -07001000/* xfrm_state_lock is held */
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001001static struct xfrm_state *__find_acq_core(struct net *net, struct xfrm_mark *m, unsigned short family, u8 mode, u32 reqid, u8 proto, xfrm_address_t *daddr, xfrm_address_t *saddr, int create)
David S. Miller27708342006-08-24 00:13:10 -07001002{
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001003 unsigned int h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
David S. Miller8f126e32006-08-24 02:45:07 -07001004 struct hlist_node *entry;
David S. Miller27708342006-08-24 00:13:10 -07001005 struct xfrm_state *x;
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001006 u32 mark = m->v & m->m;
David S. Miller27708342006-08-24 00:13:10 -07001007
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001008 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+h, bydst) {
David S. Miller27708342006-08-24 00:13:10 -07001009 if (x->props.reqid != reqid ||
1010 x->props.mode != mode ||
1011 x->props.family != family ||
1012 x->km.state != XFRM_STATE_ACQ ||
Joy Latten75e252d2007-03-12 17:14:07 -07001013 x->id.spi != 0 ||
Wei Yongjun18025712009-06-28 18:42:53 +00001014 x->id.proto != proto ||
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001015 (mark & x->mark.m) != x->mark.v ||
Wei Yongjun18025712009-06-28 18:42:53 +00001016 xfrm_addr_cmp(&x->id.daddr, daddr, family) ||
1017 xfrm_addr_cmp(&x->props.saddr, saddr, family))
David S. Miller27708342006-08-24 00:13:10 -07001018 continue;
1019
David S. Miller27708342006-08-24 00:13:10 -07001020 xfrm_state_hold(x);
1021 return x;
1022 }
1023
1024 if (!create)
1025 return NULL;
1026
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001027 x = xfrm_state_alloc(net);
David S. Miller27708342006-08-24 00:13:10 -07001028 if (likely(x)) {
1029 switch (family) {
1030 case AF_INET:
1031 x->sel.daddr.a4 = daddr->a4;
1032 x->sel.saddr.a4 = saddr->a4;
1033 x->sel.prefixlen_d = 32;
1034 x->sel.prefixlen_s = 32;
1035 x->props.saddr.a4 = saddr->a4;
1036 x->id.daddr.a4 = daddr->a4;
1037 break;
1038
1039 case AF_INET6:
1040 ipv6_addr_copy((struct in6_addr *)x->sel.daddr.a6,
1041 (struct in6_addr *)daddr);
1042 ipv6_addr_copy((struct in6_addr *)x->sel.saddr.a6,
1043 (struct in6_addr *)saddr);
1044 x->sel.prefixlen_d = 128;
1045 x->sel.prefixlen_s = 128;
1046 ipv6_addr_copy((struct in6_addr *)x->props.saddr.a6,
1047 (struct in6_addr *)saddr);
1048 ipv6_addr_copy((struct in6_addr *)x->id.daddr.a6,
1049 (struct in6_addr *)daddr);
1050 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001051 }
David S. Miller27708342006-08-24 00:13:10 -07001052
1053 x->km.state = XFRM_STATE_ACQ;
1054 x->id.proto = proto;
1055 x->props.family = family;
1056 x->props.mode = mode;
1057 x->props.reqid = reqid;
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001058 x->mark.v = m->v;
1059 x->mark.m = m->m;
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08001060 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
David S. Miller27708342006-08-24 00:13:10 -07001061 xfrm_state_hold(x);
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -08001062 tasklet_hrtimer_start(&x->mtimer, ktime_set(net->xfrm.sysctl_acq_expires, 0), HRTIMER_MODE_REL);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001063 list_add(&x->km.all, &net->xfrm.state_all);
1064 hlist_add_head(&x->bydst, net->xfrm.state_bydst+h);
1065 h = xfrm_src_hash(net, daddr, saddr, family);
1066 hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h);
David S. Miller918049f2006-10-12 22:03:24 -07001067
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001068 net->xfrm.state_num++;
David S. Miller918049f2006-10-12 22:03:24 -07001069
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001070 xfrm_hash_grow_check(net, x->bydst.next != NULL);
David S. Miller27708342006-08-24 00:13:10 -07001071 }
1072
1073 return x;
1074}
1075
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001076static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
1078int xfrm_state_add(struct xfrm_state *x)
1079{
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001080 struct net *net = xs_net(x);
David S. Miller37b08e32008-09-02 20:14:15 -07001081 struct xfrm_state *x1, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 int family;
1083 int err;
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001084 u32 mark = x->mark.v & x->mark.m;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001085 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
1087 family = x->props.family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
David S. Miller37b08e32008-09-02 20:14:15 -07001089 to_put = NULL;
1090
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 spin_lock_bh(&xfrm_state_lock);
1092
David S. Milleredcd5822006-08-24 00:42:45 -07001093 x1 = __xfrm_state_locate(x, use_spi, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 if (x1) {
David S. Miller37b08e32008-09-02 20:14:15 -07001095 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 x1 = NULL;
1097 err = -EEXIST;
1098 goto out;
1099 }
1100
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001101 if (use_spi && x->km.seq) {
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001102 x1 = __xfrm_find_acq_byseq(net, mark, x->km.seq);
Joy Latten75e252d2007-03-12 17:14:07 -07001103 if (x1 && ((x1->id.proto != x->id.proto) ||
1104 xfrm_addr_cmp(&x1->id.daddr, &x->id.daddr, family))) {
David S. Miller37b08e32008-09-02 20:14:15 -07001105 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 x1 = NULL;
1107 }
1108 }
1109
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001110 if (use_spi && !x1)
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001111 x1 = __find_acq_core(net, &x->mark, family, x->props.mode,
1112 x->props.reqid, x->id.proto,
David S. Miller27708342006-08-24 00:13:10 -07001113 &x->id.daddr, &x->props.saddr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
David S. Millerc7f5ea32006-08-24 03:29:04 -07001115 __xfrm_state_bump_genids(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 __xfrm_state_insert(x);
1117 err = 0;
1118
1119out:
1120 spin_unlock_bh(&xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
1122 if (x1) {
1123 xfrm_state_delete(x1);
1124 xfrm_state_put(x1);
1125 }
1126
David S. Miller37b08e32008-09-02 20:14:15 -07001127 if (to_put)
1128 xfrm_state_put(to_put);
1129
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 return err;
1131}
1132EXPORT_SYMBOL(xfrm_state_add);
1133
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001134#ifdef CONFIG_XFRM_MIGRATE
Eric Dumazet66663512008-01-08 01:35:52 -08001135static struct xfrm_state *xfrm_state_clone(struct xfrm_state *orig, int *errp)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001136{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001137 struct net *net = xs_net(orig);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001138 int err = -ENOMEM;
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001139 struct xfrm_state *x = xfrm_state_alloc(net);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001140 if (!x)
Herbert Xu553f9112010-02-15 20:00:51 +00001141 goto out;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001142
1143 memcpy(&x->id, &orig->id, sizeof(x->id));
1144 memcpy(&x->sel, &orig->sel, sizeof(x->sel));
1145 memcpy(&x->lft, &orig->lft, sizeof(x->lft));
1146 x->props.mode = orig->props.mode;
1147 x->props.replay_window = orig->props.replay_window;
1148 x->props.reqid = orig->props.reqid;
1149 x->props.family = orig->props.family;
1150 x->props.saddr = orig->props.saddr;
1151
1152 if (orig->aalg) {
Martin Willi4447bb32009-11-25 00:29:52 +00001153 x->aalg = xfrm_algo_auth_clone(orig->aalg);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001154 if (!x->aalg)
1155 goto error;
1156 }
1157 x->props.aalgo = orig->props.aalgo;
1158
1159 if (orig->ealg) {
1160 x->ealg = xfrm_algo_clone(orig->ealg);
1161 if (!x->ealg)
1162 goto error;
1163 }
1164 x->props.ealgo = orig->props.ealgo;
1165
1166 if (orig->calg) {
1167 x->calg = xfrm_algo_clone(orig->calg);
1168 if (!x->calg)
1169 goto error;
1170 }
1171 x->props.calgo = orig->props.calgo;
1172
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001173 if (orig->encap) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001174 x->encap = kmemdup(orig->encap, sizeof(*x->encap), GFP_KERNEL);
1175 if (!x->encap)
1176 goto error;
1177 }
1178
1179 if (orig->coaddr) {
1180 x->coaddr = kmemdup(orig->coaddr, sizeof(*x->coaddr),
1181 GFP_KERNEL);
1182 if (!x->coaddr)
1183 goto error;
1184 }
1185
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001186 memcpy(&x->mark, &orig->mark, sizeof(x->mark));
1187
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001188 err = xfrm_init_state(x);
1189 if (err)
1190 goto error;
1191
1192 x->props.flags = orig->props.flags;
1193
1194 x->curlft.add_time = orig->curlft.add_time;
1195 x->km.state = orig->km.state;
1196 x->km.seq = orig->km.seq;
1197
1198 return x;
1199
1200 error:
Herbert Xu553f9112010-02-15 20:00:51 +00001201 xfrm_state_put(x);
1202out:
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001203 if (errp)
1204 *errp = err;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001205 return NULL;
1206}
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001207
1208/* xfrm_state_lock is held */
1209struct xfrm_state * xfrm_migrate_state_find(struct xfrm_migrate *m)
1210{
1211 unsigned int h;
1212 struct xfrm_state *x;
1213 struct hlist_node *entry;
1214
1215 if (m->reqid) {
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -08001216 h = xfrm_dst_hash(&init_net, &m->old_daddr, &m->old_saddr,
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001217 m->reqid, m->old_family);
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08001218 hlist_for_each_entry(x, entry, init_net.xfrm.state_bydst+h, bydst) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001219 if (x->props.mode != m->mode ||
1220 x->id.proto != m->proto)
1221 continue;
1222 if (m->reqid && x->props.reqid != m->reqid)
1223 continue;
1224 if (xfrm_addr_cmp(&x->id.daddr, &m->old_daddr,
1225 m->old_family) ||
1226 xfrm_addr_cmp(&x->props.saddr, &m->old_saddr,
1227 m->old_family))
1228 continue;
1229 xfrm_state_hold(x);
1230 return x;
1231 }
1232 } else {
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -08001233 h = xfrm_src_hash(&init_net, &m->old_daddr, &m->old_saddr,
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001234 m->old_family);
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08001235 hlist_for_each_entry(x, entry, init_net.xfrm.state_bysrc+h, bysrc) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001236 if (x->props.mode != m->mode ||
1237 x->id.proto != m->proto)
1238 continue;
1239 if (xfrm_addr_cmp(&x->id.daddr, &m->old_daddr,
1240 m->old_family) ||
1241 xfrm_addr_cmp(&x->props.saddr, &m->old_saddr,
1242 m->old_family))
1243 continue;
1244 xfrm_state_hold(x);
1245 return x;
1246 }
1247 }
1248
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001249 return NULL;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001250}
1251EXPORT_SYMBOL(xfrm_migrate_state_find);
1252
1253struct xfrm_state * xfrm_state_migrate(struct xfrm_state *x,
1254 struct xfrm_migrate *m)
1255{
1256 struct xfrm_state *xc;
1257 int err;
1258
1259 xc = xfrm_state_clone(x, &err);
1260 if (!xc)
1261 return NULL;
1262
1263 memcpy(&xc->id.daddr, &m->new_daddr, sizeof(xc->id.daddr));
1264 memcpy(&xc->props.saddr, &m->new_saddr, sizeof(xc->props.saddr));
1265
1266 /* add state */
1267 if (!xfrm_addr_cmp(&x->id.daddr, &m->new_daddr, m->new_family)) {
1268 /* a care is needed when the destination address of the
1269 state is to be updated as it is a part of triplet */
1270 xfrm_state_insert(xc);
1271 } else {
1272 if ((err = xfrm_state_add(xc)) < 0)
1273 goto error;
1274 }
1275
1276 return xc;
1277error:
Thomas Egerer78347c82010-12-06 23:28:56 +00001278 xfrm_state_put(xc);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001279 return NULL;
1280}
1281EXPORT_SYMBOL(xfrm_state_migrate);
1282#endif
1283
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284int xfrm_state_update(struct xfrm_state *x)
1285{
David S. Miller37b08e32008-09-02 20:14:15 -07001286 struct xfrm_state *x1, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 int err;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001288 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
David S. Miller37b08e32008-09-02 20:14:15 -07001290 to_put = NULL;
1291
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 spin_lock_bh(&xfrm_state_lock);
David S. Milleredcd5822006-08-24 00:42:45 -07001293 x1 = __xfrm_state_locate(x, use_spi, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
1295 err = -ESRCH;
1296 if (!x1)
1297 goto out;
1298
1299 if (xfrm_state_kern(x1)) {
David S. Miller37b08e32008-09-02 20:14:15 -07001300 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 err = -EEXIST;
1302 goto out;
1303 }
1304
1305 if (x1->km.state == XFRM_STATE_ACQ) {
1306 __xfrm_state_insert(x);
1307 x = NULL;
1308 }
1309 err = 0;
1310
1311out:
1312 spin_unlock_bh(&xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
David S. Miller37b08e32008-09-02 20:14:15 -07001314 if (to_put)
1315 xfrm_state_put(to_put);
1316
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 if (err)
1318 return err;
1319
1320 if (!x) {
1321 xfrm_state_delete(x1);
1322 xfrm_state_put(x1);
1323 return 0;
1324 }
1325
1326 err = -EINVAL;
1327 spin_lock_bh(&x1->lock);
1328 if (likely(x1->km.state == XFRM_STATE_VALID)) {
1329 if (x->encap && x1->encap)
1330 memcpy(x1->encap, x->encap, sizeof(*x1->encap));
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -07001331 if (x->coaddr && x1->coaddr) {
1332 memcpy(x1->coaddr, x->coaddr, sizeof(*x1->coaddr));
1333 }
1334 if (!use_spi && memcmp(&x1->sel, &x->sel, sizeof(x1->sel)))
1335 memcpy(&x1->sel, &x->sel, sizeof(x1->sel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 memcpy(&x1->lft, &x->lft, sizeof(x1->lft));
1337 x1->km.dying = 0;
1338
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -08001339 tasklet_hrtimer_start(&x1->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 if (x1->curlft.use_time)
1341 xfrm_state_check_expire(x1);
1342
1343 err = 0;
1344 }
1345 spin_unlock_bh(&x1->lock);
1346
1347 xfrm_state_put(x1);
1348
1349 return err;
1350}
1351EXPORT_SYMBOL(xfrm_state_update);
1352
1353int xfrm_state_check_expire(struct xfrm_state *x)
1354{
1355 if (!x->curlft.use_time)
James Morris9d729f72007-03-04 16:12:44 -08001356 x->curlft.use_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
1358 if (x->km.state != XFRM_STATE_VALID)
1359 return -EINVAL;
1360
1361 if (x->curlft.bytes >= x->lft.hard_byte_limit ||
1362 x->curlft.packets >= x->lft.hard_packet_limit) {
Herbert Xu4666faa2005-06-18 22:43:22 -07001363 x->km.state = XFRM_STATE_EXPIRED;
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -08001364 tasklet_hrtimer_start(&x->mtimer, ktime_set(0,0), HRTIMER_MODE_REL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 return -EINVAL;
1366 }
1367
1368 if (!x->km.dying &&
1369 (x->curlft.bytes >= x->lft.soft_byte_limit ||
Herbert Xu4666faa2005-06-18 22:43:22 -07001370 x->curlft.packets >= x->lft.soft_packet_limit)) {
1371 x->km.dying = 1;
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -08001372 km_state_expired(x, 0, 0);
Herbert Xu4666faa2005-06-18 22:43:22 -07001373 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 return 0;
1375}
1376EXPORT_SYMBOL(xfrm_state_check_expire);
1377
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378struct xfrm_state *
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001379xfrm_state_lookup(struct net *net, u32 mark, xfrm_address_t *daddr, __be32 spi,
1380 u8 proto, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381{
1382 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
1384 spin_lock_bh(&xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001385 x = __xfrm_state_lookup(net, mark, daddr, spi, proto, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 spin_unlock_bh(&xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 return x;
1388}
1389EXPORT_SYMBOL(xfrm_state_lookup);
1390
1391struct xfrm_state *
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001392xfrm_state_lookup_byaddr(struct net *net, u32 mark,
Alexey Dobriyan221df1e2008-11-25 17:30:50 -08001393 xfrm_address_t *daddr, xfrm_address_t *saddr,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001394 u8 proto, unsigned short family)
1395{
1396 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001397
1398 spin_lock_bh(&xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001399 x = __xfrm_state_lookup_byaddr(net, mark, daddr, saddr, proto, family);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001400 spin_unlock_bh(&xfrm_state_lock);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001401 return x;
1402}
1403EXPORT_SYMBOL(xfrm_state_lookup_byaddr);
1404
1405struct xfrm_state *
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001406xfrm_find_acq(struct net *net, struct xfrm_mark *mark, u8 mode, u32 reqid, u8 proto,
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001407 xfrm_address_t *daddr, xfrm_address_t *saddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 int create, unsigned short family)
1409{
1410 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
1412 spin_lock_bh(&xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001413 x = __find_acq_core(net, mark, family, mode, reqid, proto, daddr, saddr, create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 spin_unlock_bh(&xfrm_state_lock);
David S. Miller27708342006-08-24 00:13:10 -07001415
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 return x;
1417}
1418EXPORT_SYMBOL(xfrm_find_acq);
1419
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001420#ifdef CONFIG_XFRM_SUB_POLICY
1421int
1422xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n,
1423 unsigned short family)
1424{
1425 int err = 0;
1426 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
1427 if (!afinfo)
1428 return -EAFNOSUPPORT;
1429
1430 spin_lock_bh(&xfrm_state_lock);
1431 if (afinfo->tmpl_sort)
1432 err = afinfo->tmpl_sort(dst, src, n);
1433 spin_unlock_bh(&xfrm_state_lock);
1434 xfrm_state_put_afinfo(afinfo);
1435 return err;
1436}
1437EXPORT_SYMBOL(xfrm_tmpl_sort);
1438
1439int
1440xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src, int n,
1441 unsigned short family)
1442{
1443 int err = 0;
1444 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
1445 if (!afinfo)
1446 return -EAFNOSUPPORT;
1447
1448 spin_lock_bh(&xfrm_state_lock);
1449 if (afinfo->state_sort)
1450 err = afinfo->state_sort(dst, src, n);
1451 spin_unlock_bh(&xfrm_state_lock);
1452 xfrm_state_put_afinfo(afinfo);
1453 return err;
1454}
1455EXPORT_SYMBOL(xfrm_state_sort);
1456#endif
1457
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458/* Silly enough, but I'm lazy to build resolution list */
1459
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001460static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461{
1462 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001464 for (i = 0; i <= net->xfrm.state_hmask; i++) {
David S. Miller8f126e32006-08-24 02:45:07 -07001465 struct hlist_node *entry;
1466 struct xfrm_state *x;
1467
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001468 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+i, bydst) {
David S. Miller8f126e32006-08-24 02:45:07 -07001469 if (x->km.seq == seq &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001470 (mark & x->mark.m) == x->mark.v &&
David S. Miller8f126e32006-08-24 02:45:07 -07001471 x->km.state == XFRM_STATE_ACQ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 xfrm_state_hold(x);
1473 return x;
1474 }
1475 }
1476 }
1477 return NULL;
1478}
1479
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001480struct xfrm_state *xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481{
1482 struct xfrm_state *x;
1483
1484 spin_lock_bh(&xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001485 x = __xfrm_find_acq_byseq(net, mark, seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 spin_unlock_bh(&xfrm_state_lock);
1487 return x;
1488}
1489EXPORT_SYMBOL(xfrm_find_acq_byseq);
1490
1491u32 xfrm_get_acqseq(void)
1492{
1493 u32 res;
jamal6836b9b2010-02-16 02:01:22 +00001494 static atomic_t acqseq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
jamal6836b9b2010-02-16 02:01:22 +00001496 do {
1497 res = atomic_inc_return(&acqseq);
1498 } while (!res);
1499
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 return res;
1501}
1502EXPORT_SYMBOL(xfrm_get_acqseq);
1503
Herbert Xu658b2192007-10-09 13:29:52 -07001504int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -08001506 struct net *net = xs_net(x);
David S. Millerf034b5d2006-08-24 03:08:07 -07001507 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 struct xfrm_state *x0;
Herbert Xu658b2192007-10-09 13:29:52 -07001509 int err = -ENOENT;
1510 __be32 minspi = htonl(low);
1511 __be32 maxspi = htonl(high);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001512 u32 mark = x->mark.v & x->mark.m;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
Herbert Xu658b2192007-10-09 13:29:52 -07001514 spin_lock_bh(&x->lock);
1515 if (x->km.state == XFRM_STATE_DEAD)
1516 goto unlock;
1517
1518 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 if (x->id.spi)
Herbert Xu658b2192007-10-09 13:29:52 -07001520 goto unlock;
1521
1522 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
1524 if (minspi == maxspi) {
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001525 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, minspi, x->id.proto, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 if (x0) {
1527 xfrm_state_put(x0);
Herbert Xu658b2192007-10-09 13:29:52 -07001528 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 }
1530 x->id.spi = minspi;
1531 } else {
1532 u32 spi = 0;
Al Viro26977b42006-09-27 18:47:05 -07001533 for (h=0; h<high-low+1; h++) {
1534 spi = low + net_random()%(high-low+1);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001535 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, htonl(spi), x->id.proto, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 if (x0 == NULL) {
1537 x->id.spi = htonl(spi);
1538 break;
1539 }
1540 xfrm_state_put(x0);
1541 }
1542 }
1543 if (x->id.spi) {
1544 spin_lock_bh(&xfrm_state_lock);
Alexey Dobriyan12604d82008-11-25 17:31:18 -08001545 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, x->props.family);
1546 hlist_add_head(&x->byspi, net->xfrm.state_byspi+h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 spin_unlock_bh(&xfrm_state_lock);
Herbert Xu658b2192007-10-09 13:29:52 -07001548
1549 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 }
Herbert Xu658b2192007-10-09 13:29:52 -07001551
1552unlock:
1553 spin_unlock_bh(&x->lock);
1554
1555 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556}
1557EXPORT_SYMBOL(xfrm_alloc_spi);
1558
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08001559int xfrm_state_walk(struct net *net, struct xfrm_state_walk *walk,
Timo Teras4c563f72008-02-28 21:31:08 -08001560 int (*func)(struct xfrm_state *, int, void*),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 void *data)
1562{
Herbert Xu12a169e2008-10-01 07:03:24 -07001563 struct xfrm_state *state;
1564 struct xfrm_state_walk *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 int err = 0;
1566
Herbert Xu12a169e2008-10-01 07:03:24 -07001567 if (walk->seq != 0 && list_empty(&walk->all))
Timo Teras4c563f72008-02-28 21:31:08 -08001568 return 0;
1569
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 spin_lock_bh(&xfrm_state_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001571 if (list_empty(&walk->all))
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08001572 x = list_first_entry(&net->xfrm.state_all, struct xfrm_state_walk, all);
Herbert Xu12a169e2008-10-01 07:03:24 -07001573 else
1574 x = list_entry(&walk->all, struct xfrm_state_walk, all);
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08001575 list_for_each_entry_from(x, &net->xfrm.state_all, all) {
Herbert Xu12a169e2008-10-01 07:03:24 -07001576 if (x->state == XFRM_STATE_DEAD)
Timo Teras4c563f72008-02-28 21:31:08 -08001577 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07001578 state = container_of(x, struct xfrm_state, km);
1579 if (!xfrm_id_proto_match(state->id.proto, walk->proto))
Timo Teras4c563f72008-02-28 21:31:08 -08001580 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07001581 err = func(state, walk->seq, data);
1582 if (err) {
1583 list_move_tail(&walk->all, &x->all);
1584 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001586 walk->seq++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001588 if (walk->seq == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 err = -ENOENT;
1590 goto out;
1591 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001592 list_del_init(&walk->all);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593out:
1594 spin_unlock_bh(&xfrm_state_lock);
1595 return err;
1596}
1597EXPORT_SYMBOL(xfrm_state_walk);
1598
Herbert Xu5c182452008-09-22 19:48:19 -07001599void xfrm_state_walk_init(struct xfrm_state_walk *walk, u8 proto)
1600{
Herbert Xu12a169e2008-10-01 07:03:24 -07001601 INIT_LIST_HEAD(&walk->all);
Herbert Xu5c182452008-09-22 19:48:19 -07001602 walk->proto = proto;
Herbert Xu12a169e2008-10-01 07:03:24 -07001603 walk->state = XFRM_STATE_DEAD;
1604 walk->seq = 0;
Herbert Xu5c182452008-09-22 19:48:19 -07001605}
1606EXPORT_SYMBOL(xfrm_state_walk_init);
1607
Herbert Xuabb81c42008-09-09 19:58:29 -07001608void xfrm_state_walk_done(struct xfrm_state_walk *walk)
1609{
Herbert Xu12a169e2008-10-01 07:03:24 -07001610 if (list_empty(&walk->all))
Herbert Xu5c182452008-09-22 19:48:19 -07001611 return;
Herbert Xu5c182452008-09-22 19:48:19 -07001612
Herbert Xu12a169e2008-10-01 07:03:24 -07001613 spin_lock_bh(&xfrm_state_lock);
1614 list_del(&walk->all);
Chuck Ebbert7d0b5912009-03-27 00:22:01 -07001615 spin_unlock_bh(&xfrm_state_lock);
Herbert Xuabb81c42008-09-09 19:58:29 -07001616}
1617EXPORT_SYMBOL(xfrm_state_walk_done);
1618
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001619
1620void xfrm_replay_notify(struct xfrm_state *x, int event)
1621{
1622 struct km_event c;
1623 /* we send notify messages in case
1624 * 1. we updated on of the sequence numbers, and the seqno difference
1625 * is at least x->replay_maxdiff, in this case we also update the
1626 * timeout of our timer function
1627 * 2. if x->replay_maxage has elapsed since last update,
1628 * and there were changes
1629 *
1630 * The state structure must be locked!
1631 */
1632
1633 switch (event) {
1634 case XFRM_REPLAY_UPDATE:
1635 if (x->replay_maxdiff &&
1636 (x->replay.seq - x->preplay.seq < x->replay_maxdiff) &&
Jamal Hadi Salim27170962006-04-14 15:03:05 -07001637 (x->replay.oseq - x->preplay.oseq < x->replay_maxdiff)) {
1638 if (x->xflags & XFRM_TIME_DEFER)
1639 event = XFRM_REPLAY_TIMEOUT;
1640 else
1641 return;
1642 }
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001643
1644 break;
1645
1646 case XFRM_REPLAY_TIMEOUT:
1647 if ((x->replay.seq == x->preplay.seq) &&
1648 (x->replay.bitmap == x->preplay.bitmap) &&
Jamal Hadi Salim27170962006-04-14 15:03:05 -07001649 (x->replay.oseq == x->preplay.oseq)) {
1650 x->xflags |= XFRM_TIME_DEFER;
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001651 return;
Jamal Hadi Salim27170962006-04-14 15:03:05 -07001652 }
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001653
1654 break;
1655 }
1656
1657 memcpy(&x->preplay, &x->replay, sizeof(struct xfrm_replay_state));
1658 c.event = XFRM_MSG_NEWAE;
1659 c.data.aevent = event;
1660 km_state_notify(x, &c);
1661
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001662 if (x->replay_maxage &&
David S. Millera47f0ce2006-08-24 03:54:22 -07001663 !mod_timer(&x->rtimer, jiffies + x->replay_maxage))
Jamal Hadi Salim27170962006-04-14 15:03:05 -07001664 x->xflags &= ~XFRM_TIME_DEFER;
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001665}
1666
1667static void xfrm_replay_timer_handler(unsigned long data)
1668{
1669 struct xfrm_state *x = (struct xfrm_state*)data;
1670
1671 spin_lock(&x->lock);
1672
Jamal Hadi Salim27170962006-04-14 15:03:05 -07001673 if (x->km.state == XFRM_STATE_VALID) {
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001674 if (xfrm_aevent_is_on(xs_net(x)))
Jamal Hadi Salim27170962006-04-14 15:03:05 -07001675 xfrm_replay_notify(x, XFRM_REPLAY_TIMEOUT);
1676 else
1677 x->xflags |= XFRM_TIME_DEFER;
1678 }
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001679
1680 spin_unlock(&x->lock);
1681}
1682
Paul Mooreafeb14b2007-12-21 14:58:11 -08001683int xfrm_replay_check(struct xfrm_state *x,
1684 struct sk_buff *skb, __be32 net_seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685{
1686 u32 diff;
Al Viroa252cc22006-09-27 18:48:18 -07001687 u32 seq = ntohl(net_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
1689 if (unlikely(seq == 0))
Paul Mooreafeb14b2007-12-21 14:58:11 -08001690 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
1692 if (likely(seq > x->replay.seq))
1693 return 0;
1694
1695 diff = x->replay.seq - seq;
Herbert Xu4c4d51a72007-04-05 00:07:39 -07001696 if (diff >= min_t(unsigned int, x->props.replay_window,
1697 sizeof(x->replay.bitmap) * 8)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 x->stats.replay_window++;
Paul Mooreafeb14b2007-12-21 14:58:11 -08001699 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 }
1701
1702 if (x->replay.bitmap & (1U << diff)) {
1703 x->stats.replay++;
Paul Mooreafeb14b2007-12-21 14:58:11 -08001704 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 }
1706 return 0;
Paul Mooreafeb14b2007-12-21 14:58:11 -08001707
1708err:
1709 xfrm_audit_state_replay(x, skb, net_seq);
1710 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712
Al Viro61f46272006-09-27 18:48:33 -07001713void xfrm_replay_advance(struct xfrm_state *x, __be32 net_seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714{
1715 u32 diff;
Al Viro61f46272006-09-27 18:48:33 -07001716 u32 seq = ntohl(net_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
1718 if (seq > x->replay.seq) {
1719 diff = seq - x->replay.seq;
1720 if (diff < x->props.replay_window)
1721 x->replay.bitmap = ((x->replay.bitmap) << diff) | 1;
1722 else
1723 x->replay.bitmap = 1;
1724 x->replay.seq = seq;
1725 } else {
1726 diff = x->replay.seq - seq;
1727 x->replay.bitmap |= (1U << diff);
1728 }
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001729
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001730 if (xfrm_aevent_is_on(xs_net(x)))
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001731 xfrm_replay_notify(x, XFRM_REPLAY_UPDATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
Denis Chengdf018122007-12-07 00:51:11 -08001734static LIST_HEAD(xfrm_km_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735static DEFINE_RWLOCK(xfrm_km_lock);
1736
David S. Miller214e0052011-02-24 00:02:38 -05001737void km_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738{
1739 struct xfrm_mgr *km;
1740
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001741 read_lock(&xfrm_km_lock);
1742 list_for_each_entry(km, &xfrm_km_list, list)
1743 if (km->notify_policy)
1744 km->notify_policy(xp, dir, c);
1745 read_unlock(&xfrm_km_lock);
1746}
1747
David S. Miller214e0052011-02-24 00:02:38 -05001748void km_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001749{
1750 struct xfrm_mgr *km;
1751 read_lock(&xfrm_km_lock);
1752 list_for_each_entry(km, &xfrm_km_list, list)
1753 if (km->notify)
1754 km->notify(x, c);
1755 read_unlock(&xfrm_km_lock);
1756}
1757
1758EXPORT_SYMBOL(km_policy_notify);
1759EXPORT_SYMBOL(km_state_notify);
1760
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -08001761void km_state_expired(struct xfrm_state *x, int hard, u32 pid)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001762{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001763 struct net *net = xs_net(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001764 struct km_event c;
1765
Herbert Xubf08867f92005-06-18 22:44:00 -07001766 c.data.hard = hard;
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -08001767 c.pid = pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001768 c.event = XFRM_MSG_EXPIRE;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001769 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
1771 if (hard)
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001772 wake_up(&net->xfrm.km_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773}
1774
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -08001775EXPORT_SYMBOL(km_state_expired);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001776/*
1777 * We send to all registered managers regardless of failure
1778 * We are happy with one success
1779*/
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001780int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001782 int err = -EINVAL, acqret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 struct xfrm_mgr *km;
1784
1785 read_lock(&xfrm_km_lock);
1786 list_for_each_entry(km, &xfrm_km_list, list) {
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001787 acqret = km->acquire(x, t, pol, XFRM_POLICY_OUT);
1788 if (!acqret)
1789 err = acqret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 }
1791 read_unlock(&xfrm_km_lock);
1792 return err;
1793}
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001794EXPORT_SYMBOL(km_query);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
Al Viro5d36b182006-11-08 00:24:06 -08001796int km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797{
1798 int err = -EINVAL;
1799 struct xfrm_mgr *km;
1800
1801 read_lock(&xfrm_km_lock);
1802 list_for_each_entry(km, &xfrm_km_list, list) {
1803 if (km->new_mapping)
1804 err = km->new_mapping(x, ipaddr, sport);
1805 if (!err)
1806 break;
1807 }
1808 read_unlock(&xfrm_km_lock);
1809 return err;
1810}
1811EXPORT_SYMBOL(km_new_mapping);
1812
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001813void km_policy_expired(struct xfrm_policy *pol, int dir, int hard, u32 pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001815 struct net *net = xp_net(pol);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001816 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817
Herbert Xubf08867f92005-06-18 22:44:00 -07001818 c.data.hard = hard;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001819 c.pid = pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001820 c.event = XFRM_MSG_POLEXPIRE;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001821 km_policy_notify(pol, dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822
1823 if (hard)
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001824 wake_up(&net->xfrm.km_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825}
David S. Millera70fcb02006-03-20 19:18:52 -08001826EXPORT_SYMBOL(km_policy_expired);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827
Eric Dumazet2d60abc2008-01-03 20:43:21 -08001828#ifdef CONFIG_XFRM_MIGRATE
David S. Miller183cad12011-02-24 00:28:01 -05001829int km_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
1830 const struct xfrm_migrate *m, int num_migrate,
1831 const struct xfrm_kmaddress *k)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001832{
1833 int err = -EINVAL;
1834 int ret;
1835 struct xfrm_mgr *km;
1836
1837 read_lock(&xfrm_km_lock);
1838 list_for_each_entry(km, &xfrm_km_list, list) {
1839 if (km->migrate) {
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001840 ret = km->migrate(sel, dir, type, m, num_migrate, k);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001841 if (!ret)
1842 err = ret;
1843 }
1844 }
1845 read_unlock(&xfrm_km_lock);
1846 return err;
1847}
1848EXPORT_SYMBOL(km_migrate);
Eric Dumazet2d60abc2008-01-03 20:43:21 -08001849#endif
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001850
Alexey Dobriyandb983c12008-11-25 17:51:01 -08001851int km_report(struct net *net, u8 proto, struct xfrm_selector *sel, xfrm_address_t *addr)
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001852{
1853 int err = -EINVAL;
1854 int ret;
1855 struct xfrm_mgr *km;
1856
1857 read_lock(&xfrm_km_lock);
1858 list_for_each_entry(km, &xfrm_km_list, list) {
1859 if (km->report) {
Alexey Dobriyandb983c12008-11-25 17:51:01 -08001860 ret = km->report(net, proto, sel, addr);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001861 if (!ret)
1862 err = ret;
1863 }
1864 }
1865 read_unlock(&xfrm_km_lock);
1866 return err;
1867}
1868EXPORT_SYMBOL(km_report);
1869
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870int xfrm_user_policy(struct sock *sk, int optname, u8 __user *optval, int optlen)
1871{
1872 int err;
1873 u8 *data;
1874 struct xfrm_mgr *km;
1875 struct xfrm_policy *pol = NULL;
1876
1877 if (optlen <= 0 || optlen > PAGE_SIZE)
1878 return -EMSGSIZE;
1879
1880 data = kmalloc(optlen, GFP_KERNEL);
1881 if (!data)
1882 return -ENOMEM;
1883
1884 err = -EFAULT;
1885 if (copy_from_user(data, optval, optlen))
1886 goto out;
1887
1888 err = -EINVAL;
1889 read_lock(&xfrm_km_lock);
1890 list_for_each_entry(km, &xfrm_km_list, list) {
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07001891 pol = km->compile_policy(sk, optname, data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 optlen, &err);
1893 if (err >= 0)
1894 break;
1895 }
1896 read_unlock(&xfrm_km_lock);
1897
1898 if (err >= 0) {
1899 xfrm_sk_policy_insert(sk, err, pol);
1900 xfrm_pol_put(pol);
1901 err = 0;
1902 }
1903
1904out:
1905 kfree(data);
1906 return err;
1907}
1908EXPORT_SYMBOL(xfrm_user_policy);
1909
1910int xfrm_register_km(struct xfrm_mgr *km)
1911{
1912 write_lock_bh(&xfrm_km_lock);
1913 list_add_tail(&km->list, &xfrm_km_list);
1914 write_unlock_bh(&xfrm_km_lock);
1915 return 0;
1916}
1917EXPORT_SYMBOL(xfrm_register_km);
1918
1919int xfrm_unregister_km(struct xfrm_mgr *km)
1920{
1921 write_lock_bh(&xfrm_km_lock);
1922 list_del(&km->list);
1923 write_unlock_bh(&xfrm_km_lock);
1924 return 0;
1925}
1926EXPORT_SYMBOL(xfrm_unregister_km);
1927
1928int xfrm_state_register_afinfo(struct xfrm_state_afinfo *afinfo)
1929{
1930 int err = 0;
1931 if (unlikely(afinfo == NULL))
1932 return -EINVAL;
1933 if (unlikely(afinfo->family >= NPROTO))
1934 return -EAFNOSUPPORT;
Ingo Molnarf3111502006-04-28 15:30:03 -07001935 write_lock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 if (unlikely(xfrm_state_afinfo[afinfo->family] != NULL))
1937 err = -ENOBUFS;
David S. Milleredcd5822006-08-24 00:42:45 -07001938 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 xfrm_state_afinfo[afinfo->family] = afinfo;
Ingo Molnarf3111502006-04-28 15:30:03 -07001940 write_unlock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 return err;
1942}
1943EXPORT_SYMBOL(xfrm_state_register_afinfo);
1944
1945int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo)
1946{
1947 int err = 0;
1948 if (unlikely(afinfo == NULL))
1949 return -EINVAL;
1950 if (unlikely(afinfo->family >= NPROTO))
1951 return -EAFNOSUPPORT;
Ingo Molnarf3111502006-04-28 15:30:03 -07001952 write_lock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 if (likely(xfrm_state_afinfo[afinfo->family] != NULL)) {
1954 if (unlikely(xfrm_state_afinfo[afinfo->family] != afinfo))
1955 err = -EINVAL;
David S. Milleredcd5822006-08-24 00:42:45 -07001956 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 xfrm_state_afinfo[afinfo->family] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 }
Ingo Molnarf3111502006-04-28 15:30:03 -07001959 write_unlock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 return err;
1961}
1962EXPORT_SYMBOL(xfrm_state_unregister_afinfo);
1963
Herbert Xu17c2a422007-10-17 21:33:12 -07001964static struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965{
1966 struct xfrm_state_afinfo *afinfo;
1967 if (unlikely(family >= NPROTO))
1968 return NULL;
1969 read_lock(&xfrm_state_afinfo_lock);
1970 afinfo = xfrm_state_afinfo[family];
Herbert Xu546be242006-05-27 23:03:58 -07001971 if (unlikely(!afinfo))
1972 read_unlock(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 return afinfo;
1974}
1975
Herbert Xu17c2a422007-10-17 21:33:12 -07001976static void xfrm_state_put_afinfo(struct xfrm_state_afinfo *afinfo)
Eric Dumazet9a429c42008-01-01 21:58:02 -08001977 __releases(xfrm_state_afinfo_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978{
Herbert Xu546be242006-05-27 23:03:58 -07001979 read_unlock(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980}
1981
1982/* Temporarily located here until net/xfrm/xfrm_tunnel.c is created */
1983void xfrm_state_delete_tunnel(struct xfrm_state *x)
1984{
1985 if (x->tunnel) {
1986 struct xfrm_state *t = x->tunnel;
1987
1988 if (atomic_read(&t->tunnel_users) == 2)
1989 xfrm_state_delete(t);
1990 atomic_dec(&t->tunnel_users);
1991 xfrm_state_put(t);
1992 x->tunnel = NULL;
1993 }
1994}
1995EXPORT_SYMBOL(xfrm_state_delete_tunnel);
1996
1997int xfrm_state_mtu(struct xfrm_state *x, int mtu)
1998{
Patrick McHardyc5c25232007-04-09 11:47:18 -07001999 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000
Patrick McHardyc5c25232007-04-09 11:47:18 -07002001 spin_lock_bh(&x->lock);
2002 if (x->km.state == XFRM_STATE_VALID &&
2003 x->type && x->type->get_mtu)
2004 res = x->type->get_mtu(x, mtu);
2005 else
Patrick McHardy28121612007-06-18 22:30:15 -07002006 res = mtu - x->props.header_len;
Patrick McHardyc5c25232007-04-09 11:47:18 -07002007 spin_unlock_bh(&x->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 return res;
2009}
2010
Herbert Xu72cb6962005-06-20 13:18:08 -07002011int xfrm_init_state(struct xfrm_state *x)
2012{
Herbert Xud094cd82005-06-20 13:19:41 -07002013 struct xfrm_state_afinfo *afinfo;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002014 struct xfrm_mode *inner_mode;
Herbert Xud094cd82005-06-20 13:19:41 -07002015 int family = x->props.family;
Herbert Xu72cb6962005-06-20 13:18:08 -07002016 int err;
2017
Herbert Xud094cd82005-06-20 13:19:41 -07002018 err = -EAFNOSUPPORT;
2019 afinfo = xfrm_state_get_afinfo(family);
2020 if (!afinfo)
2021 goto error;
2022
2023 err = 0;
2024 if (afinfo->init_flags)
2025 err = afinfo->init_flags(x);
2026
2027 xfrm_state_put_afinfo(afinfo);
2028
2029 if (err)
2030 goto error;
2031
2032 err = -EPROTONOSUPPORT;
Herbert Xu13996372007-10-17 21:35:51 -07002033
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002034 if (x->sel.family != AF_UNSPEC) {
2035 inner_mode = xfrm_get_mode(x->props.mode, x->sel.family);
2036 if (inner_mode == NULL)
2037 goto error;
2038
2039 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL) &&
2040 family != x->sel.family) {
2041 xfrm_put_mode(inner_mode);
2042 goto error;
2043 }
2044
2045 x->inner_mode = inner_mode;
2046 } else {
2047 struct xfrm_mode *inner_mode_iaf;
Martin Willid81d2282008-12-03 15:38:07 -08002048 int iafamily = AF_INET;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002049
Martin Willid81d2282008-12-03 15:38:07 -08002050 inner_mode = xfrm_get_mode(x->props.mode, x->props.family);
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002051 if (inner_mode == NULL)
2052 goto error;
2053
2054 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL)) {
2055 xfrm_put_mode(inner_mode);
2056 goto error;
2057 }
Martin Willid81d2282008-12-03 15:38:07 -08002058 x->inner_mode = inner_mode;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002059
Martin Willid81d2282008-12-03 15:38:07 -08002060 if (x->props.family == AF_INET)
2061 iafamily = AF_INET6;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002062
Martin Willid81d2282008-12-03 15:38:07 -08002063 inner_mode_iaf = xfrm_get_mode(x->props.mode, iafamily);
2064 if (inner_mode_iaf) {
2065 if (inner_mode_iaf->flags & XFRM_MODE_FLAG_TUNNEL)
2066 x->inner_mode_iaf = inner_mode_iaf;
2067 else
2068 xfrm_put_mode(inner_mode_iaf);
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002069 }
2070 }
Herbert Xu13996372007-10-17 21:35:51 -07002071
Herbert Xud094cd82005-06-20 13:19:41 -07002072 x->type = xfrm_get_type(x->id.proto, family);
Herbert Xu72cb6962005-06-20 13:18:08 -07002073 if (x->type == NULL)
2074 goto error;
2075
2076 err = x->type->init_state(x);
2077 if (err)
2078 goto error;
2079
Herbert Xu13996372007-10-17 21:35:51 -07002080 x->outer_mode = xfrm_get_mode(x->props.mode, family);
2081 if (x->outer_mode == NULL)
Herbert Xub59f45d2006-05-27 23:05:54 -07002082 goto error;
2083
Herbert Xu72cb6962005-06-20 13:18:08 -07002084 x->km.state = XFRM_STATE_VALID;
2085
2086error:
2087 return err;
2088}
2089
2090EXPORT_SYMBOL(xfrm_init_state);
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09002091
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002092int __net_init xfrm_state_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093{
David S. Millerf034b5d2006-08-24 03:08:07 -07002094 unsigned int sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095
Alexey Dobriyan9d4139c2008-11-25 17:16:11 -08002096 INIT_LIST_HEAD(&net->xfrm.state_all);
2097
David S. Millerf034b5d2006-08-24 03:08:07 -07002098 sz = sizeof(struct hlist_head) * 8;
2099
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002100 net->xfrm.state_bydst = xfrm_hash_alloc(sz);
2101 if (!net->xfrm.state_bydst)
2102 goto out_bydst;
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002103 net->xfrm.state_bysrc = xfrm_hash_alloc(sz);
2104 if (!net->xfrm.state_bysrc)
2105 goto out_bysrc;
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002106 net->xfrm.state_byspi = xfrm_hash_alloc(sz);
2107 if (!net->xfrm.state_byspi)
2108 goto out_byspi;
Alexey Dobriyan529983e2008-11-25 17:18:12 -08002109 net->xfrm.state_hmask = ((sz / sizeof(struct hlist_head)) - 1);
David S. Millerf034b5d2006-08-24 03:08:07 -07002110
Alexey Dobriyan0bf7c5b2008-11-25 17:18:39 -08002111 net->xfrm.state_num = 0;
Alexey Dobriyan63082732008-11-25 17:19:07 -08002112 INIT_WORK(&net->xfrm.state_hash_work, xfrm_hash_resize);
Alexey Dobriyanb8a0ae22008-11-25 17:20:11 -08002113 INIT_HLIST_HEAD(&net->xfrm.state_gc_list);
Alexey Dobriyanc7837142008-11-25 17:20:36 -08002114 INIT_WORK(&net->xfrm.state_gc_work, xfrm_state_gc_task);
Alexey Dobriyan50a30652008-11-25 17:21:01 -08002115 init_waitqueue_head(&net->xfrm.km_waitq);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002116 return 0;
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002117
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002118out_byspi:
2119 xfrm_hash_free(net->xfrm.state_bysrc, sz);
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002120out_bysrc:
2121 xfrm_hash_free(net->xfrm.state_bydst, sz);
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002122out_bydst:
2123 return -ENOMEM;
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002124}
2125
2126void xfrm_state_fini(struct net *net)
2127{
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002128 struct xfrm_audit audit_info;
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002129 unsigned int sz;
2130
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002131 flush_work(&net->xfrm.state_hash_work);
2132 audit_info.loginuid = -1;
2133 audit_info.sessionid = -1;
2134 audit_info.secid = 0;
2135 xfrm_state_flush(net, IPSEC_PROTO_ANY, &audit_info);
2136 flush_work(&net->xfrm.state_gc_work);
2137
Alexey Dobriyan9d4139c2008-11-25 17:16:11 -08002138 WARN_ON(!list_empty(&net->xfrm.state_all));
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002139
Alexey Dobriyan529983e2008-11-25 17:18:12 -08002140 sz = (net->xfrm.state_hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002141 WARN_ON(!hlist_empty(net->xfrm.state_byspi));
2142 xfrm_hash_free(net->xfrm.state_byspi, sz);
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002143 WARN_ON(!hlist_empty(net->xfrm.state_bysrc));
2144 xfrm_hash_free(net->xfrm.state_bysrc, sz);
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002145 WARN_ON(!hlist_empty(net->xfrm.state_bydst));
2146 xfrm_hash_free(net->xfrm.state_bydst, sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147}
2148
Joy Lattenab5f5e82007-09-17 11:51:22 -07002149#ifdef CONFIG_AUDITSYSCALL
Ilpo Järvinencf35f432008-01-05 23:13:20 -08002150static void xfrm_audit_helper_sainfo(struct xfrm_state *x,
2151 struct audit_buffer *audit_buf)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002152{
Paul Moore68277ac2007-12-20 20:49:33 -08002153 struct xfrm_sec_ctx *ctx = x->security;
2154 u32 spi = ntohl(x->id.spi);
2155
2156 if (ctx)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002157 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
Paul Moore68277ac2007-12-20 20:49:33 -08002158 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002159
2160 switch(x->props.family) {
2161 case AF_INET:
Harvey Harrison21454aa2008-10-31 00:54:56 -07002162 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2163 &x->props.saddr.a4, &x->id.daddr.a4);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002164 break;
2165 case AF_INET6:
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002166 audit_log_format(audit_buf, " src=%pI6 dst=%pI6",
Harvey Harrisonfdb46ee2008-10-28 16:10:17 -07002167 x->props.saddr.a6, x->id.daddr.a6);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002168 break;
2169 }
Paul Moore68277ac2007-12-20 20:49:33 -08002170
2171 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002172}
2173
Ilpo Järvinencf35f432008-01-05 23:13:20 -08002174static void xfrm_audit_helper_pktinfo(struct sk_buff *skb, u16 family,
2175 struct audit_buffer *audit_buf)
Paul Mooreafeb14b2007-12-21 14:58:11 -08002176{
2177 struct iphdr *iph4;
2178 struct ipv6hdr *iph6;
2179
2180 switch (family) {
2181 case AF_INET:
2182 iph4 = ip_hdr(skb);
Harvey Harrison21454aa2008-10-31 00:54:56 -07002183 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2184 &iph4->saddr, &iph4->daddr);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002185 break;
2186 case AF_INET6:
2187 iph6 = ipv6_hdr(skb);
2188 audit_log_format(audit_buf,
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002189 " src=%pI6 dst=%pI6 flowlbl=0x%x%02x%02x",
Harvey Harrisonfdb46ee2008-10-28 16:10:17 -07002190 &iph6->saddr,&iph6->daddr,
Paul Mooreafeb14b2007-12-21 14:58:11 -08002191 iph6->flow_lbl[0] & 0x0f,
2192 iph6->flow_lbl[1],
2193 iph6->flow_lbl[2]);
2194 break;
2195 }
2196}
2197
Paul Moore68277ac2007-12-20 20:49:33 -08002198void xfrm_audit_state_add(struct xfrm_state *x, int result,
Eric Paris25323862008-04-18 10:09:25 -04002199 uid_t auid, u32 sessionid, u32 secid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002200{
2201 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002202
Paul Mooreafeb14b2007-12-21 14:58:11 -08002203 audit_buf = xfrm_audit_start("SAD-add");
Joy Lattenab5f5e82007-09-17 11:51:22 -07002204 if (audit_buf == NULL)
2205 return;
Eric Paris25323862008-04-18 10:09:25 -04002206 xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002207 xfrm_audit_helper_sainfo(x, audit_buf);
2208 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002209 audit_log_end(audit_buf);
2210}
2211EXPORT_SYMBOL_GPL(xfrm_audit_state_add);
2212
Paul Moore68277ac2007-12-20 20:49:33 -08002213void xfrm_audit_state_delete(struct xfrm_state *x, int result,
Eric Paris25323862008-04-18 10:09:25 -04002214 uid_t auid, u32 sessionid, u32 secid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002215{
2216 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002217
Paul Mooreafeb14b2007-12-21 14:58:11 -08002218 audit_buf = xfrm_audit_start("SAD-delete");
Joy Lattenab5f5e82007-09-17 11:51:22 -07002219 if (audit_buf == NULL)
2220 return;
Eric Paris25323862008-04-18 10:09:25 -04002221 xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002222 xfrm_audit_helper_sainfo(x, audit_buf);
2223 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002224 audit_log_end(audit_buf);
2225}
2226EXPORT_SYMBOL_GPL(xfrm_audit_state_delete);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002227
2228void xfrm_audit_state_replay_overflow(struct xfrm_state *x,
2229 struct sk_buff *skb)
2230{
2231 struct audit_buffer *audit_buf;
2232 u32 spi;
2233
2234 audit_buf = xfrm_audit_start("SA-replay-overflow");
2235 if (audit_buf == NULL)
2236 return;
2237 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2238 /* don't record the sequence number because it's inherent in this kind
2239 * of audit message */
2240 spi = ntohl(x->id.spi);
2241 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
2242 audit_log_end(audit_buf);
2243}
2244EXPORT_SYMBOL_GPL(xfrm_audit_state_replay_overflow);
2245
2246static void xfrm_audit_state_replay(struct xfrm_state *x,
2247 struct sk_buff *skb, __be32 net_seq)
2248{
2249 struct audit_buffer *audit_buf;
2250 u32 spi;
2251
2252 audit_buf = xfrm_audit_start("SA-replayed-pkt");
2253 if (audit_buf == NULL)
2254 return;
2255 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2256 spi = ntohl(x->id.spi);
2257 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2258 spi, spi, ntohl(net_seq));
2259 audit_log_end(audit_buf);
2260}
2261
2262void xfrm_audit_state_notfound_simple(struct sk_buff *skb, u16 family)
2263{
2264 struct audit_buffer *audit_buf;
2265
2266 audit_buf = xfrm_audit_start("SA-notfound");
2267 if (audit_buf == NULL)
2268 return;
2269 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2270 audit_log_end(audit_buf);
2271}
2272EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound_simple);
2273
2274void xfrm_audit_state_notfound(struct sk_buff *skb, u16 family,
2275 __be32 net_spi, __be32 net_seq)
2276{
2277 struct audit_buffer *audit_buf;
2278 u32 spi;
2279
2280 audit_buf = xfrm_audit_start("SA-notfound");
2281 if (audit_buf == NULL)
2282 return;
2283 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2284 spi = ntohl(net_spi);
2285 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2286 spi, spi, ntohl(net_seq));
2287 audit_log_end(audit_buf);
2288}
2289EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound);
2290
2291void xfrm_audit_state_icvfail(struct xfrm_state *x,
2292 struct sk_buff *skb, u8 proto)
2293{
2294 struct audit_buffer *audit_buf;
2295 __be32 net_spi;
2296 __be32 net_seq;
2297
2298 audit_buf = xfrm_audit_start("SA-icv-failure");
2299 if (audit_buf == NULL)
2300 return;
2301 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2302 if (xfrm_parse_spi(skb, proto, &net_spi, &net_seq) == 0) {
2303 u32 spi = ntohl(net_spi);
2304 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2305 spi, spi, ntohl(net_seq));
2306 }
2307 audit_log_end(audit_buf);
2308}
2309EXPORT_SYMBOL_GPL(xfrm_audit_state_icvfail);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002310#endif /* CONFIG_AUDITSYSCALL */