lib/string.c: simplify stricmp()
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / xfrm / xfrm_state.c
blob17d5b96f2fc8b6d8b7f290abbd67906526393a53
1 /*
2 * xfrm_state.c
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
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>
21 #include <linux/cache.h>
22 #include <linux/audit.h>
23 #include <asm/uaccess.h>
24 #include <linux/ktime.h>
25 #include <linux/interrupt.h>
26 #include <linux/kernel.h>
28 #include "xfrm_hash.h"
30 /* Each xfrm_state may be linked to two tables:
32 1. Hash table by (spi,daddr,ah/esp) to find SA by SPI. (input,ctl)
33 2. Hash table by (daddr,family,reqid) to find what SAs exist for given
34 destination/tunnel endpoint. (output)
37 static DEFINE_SPINLOCK(xfrm_state_lock);
39 static unsigned int xfrm_state_hashmax __read_mostly = 1 * 1024 * 1024;
40 static unsigned int xfrm_state_genid;
42 static struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family);
43 static void xfrm_state_put_afinfo(struct xfrm_state_afinfo *afinfo);
45 #ifdef CONFIG_AUDITSYSCALL
46 static 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 */
52 static inline unsigned int xfrm_dst_hash(struct net *net,
53 xfrm_address_t *daddr,
54 xfrm_address_t *saddr,
55 u32 reqid,
56 unsigned short family)
58 return __xfrm_dst_hash(daddr, saddr, reqid, family, net->xfrm.state_hmask);
61 static inline unsigned int xfrm_src_hash(struct net *net,
62 xfrm_address_t *daddr,
63 xfrm_address_t *saddr,
64 unsigned short family)
66 return __xfrm_src_hash(daddr, saddr, family, net->xfrm.state_hmask);
69 static inline unsigned int
70 xfrm_spi_hash(struct net *net, xfrm_address_t *daddr, __be32 spi, u8 proto, unsigned short family)
72 return __xfrm_spi_hash(daddr, spi, proto, family, net->xfrm.state_hmask);
75 static void xfrm_hash_transfer(struct hlist_head *list,
76 struct hlist_head *ndsttable,
77 struct hlist_head *nsrctable,
78 struct hlist_head *nspitable,
79 unsigned int nhashmask)
81 struct hlist_node *entry, *tmp;
82 struct xfrm_state *x;
84 hlist_for_each_entry_safe(x, entry, tmp, list, bydst) {
85 unsigned int h;
87 h = __xfrm_dst_hash(&x->id.daddr, &x->props.saddr,
88 x->props.reqid, x->props.family,
89 nhashmask);
90 hlist_add_head(&x->bydst, ndsttable+h);
92 h = __xfrm_src_hash(&x->id.daddr, &x->props.saddr,
93 x->props.family,
94 nhashmask);
95 hlist_add_head(&x->bysrc, nsrctable+h);
97 if (x->id.spi) {
98 h = __xfrm_spi_hash(&x->id.daddr, x->id.spi,
99 x->id.proto, x->props.family,
100 nhashmask);
101 hlist_add_head(&x->byspi, nspitable+h);
106 static unsigned long xfrm_hash_new_size(unsigned int state_hmask)
108 return ((state_hmask + 1) << 1) * sizeof(struct hlist_head);
111 static DEFINE_MUTEX(hash_resize_mutex);
113 static void xfrm_hash_resize(struct work_struct *work)
115 struct net *net = container_of(work, struct net, xfrm.state_hash_work);
116 struct hlist_head *ndst, *nsrc, *nspi, *odst, *osrc, *ospi;
117 unsigned long nsize, osize;
118 unsigned int nhashmask, ohashmask;
119 int i;
121 mutex_lock(&hash_resize_mutex);
123 nsize = xfrm_hash_new_size(net->xfrm.state_hmask);
124 ndst = xfrm_hash_alloc(nsize);
125 if (!ndst)
126 goto out_unlock;
127 nsrc = xfrm_hash_alloc(nsize);
128 if (!nsrc) {
129 xfrm_hash_free(ndst, nsize);
130 goto out_unlock;
132 nspi = xfrm_hash_alloc(nsize);
133 if (!nspi) {
134 xfrm_hash_free(ndst, nsize);
135 xfrm_hash_free(nsrc, nsize);
136 goto out_unlock;
139 spin_lock_bh(&xfrm_state_lock);
141 nhashmask = (nsize / sizeof(struct hlist_head)) - 1U;
142 for (i = net->xfrm.state_hmask; i >= 0; i--)
143 xfrm_hash_transfer(net->xfrm.state_bydst+i, ndst, nsrc, nspi,
144 nhashmask);
146 odst = net->xfrm.state_bydst;
147 osrc = net->xfrm.state_bysrc;
148 ospi = net->xfrm.state_byspi;
149 ohashmask = net->xfrm.state_hmask;
151 net->xfrm.state_bydst = ndst;
152 net->xfrm.state_bysrc = nsrc;
153 net->xfrm.state_byspi = nspi;
154 net->xfrm.state_hmask = nhashmask;
156 spin_unlock_bh(&xfrm_state_lock);
158 osize = (ohashmask + 1) * sizeof(struct hlist_head);
159 xfrm_hash_free(odst, osize);
160 xfrm_hash_free(osrc, osize);
161 xfrm_hash_free(ospi, osize);
163 out_unlock:
164 mutex_unlock(&hash_resize_mutex);
167 static DEFINE_RWLOCK(xfrm_state_afinfo_lock);
168 static struct xfrm_state_afinfo *xfrm_state_afinfo[NPROTO];
170 static DEFINE_SPINLOCK(xfrm_state_gc_lock);
172 int __xfrm_state_delete(struct xfrm_state *x);
174 int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol);
175 void km_state_expired(struct xfrm_state *x, int hard, u32 pid);
177 static struct xfrm_state_afinfo *xfrm_state_lock_afinfo(unsigned int family)
179 struct xfrm_state_afinfo *afinfo;
180 if (unlikely(family >= NPROTO))
181 return NULL;
182 write_lock_bh(&xfrm_state_afinfo_lock);
183 afinfo = xfrm_state_afinfo[family];
184 if (unlikely(!afinfo))
185 write_unlock_bh(&xfrm_state_afinfo_lock);
186 return afinfo;
189 static void xfrm_state_unlock_afinfo(struct xfrm_state_afinfo *afinfo)
190 __releases(xfrm_state_afinfo_lock)
192 write_unlock_bh(&xfrm_state_afinfo_lock);
195 int xfrm_register_type(const struct xfrm_type *type, unsigned short family)
197 struct xfrm_state_afinfo *afinfo = xfrm_state_lock_afinfo(family);
198 const struct xfrm_type **typemap;
199 int err = 0;
201 if (unlikely(afinfo == NULL))
202 return -EAFNOSUPPORT;
203 typemap = afinfo->type_map;
205 if (likely(typemap[type->proto] == NULL))
206 typemap[type->proto] = type;
207 else
208 err = -EEXIST;
209 xfrm_state_unlock_afinfo(afinfo);
210 return err;
212 EXPORT_SYMBOL(xfrm_register_type);
214 int xfrm_unregister_type(const struct xfrm_type *type, unsigned short family)
216 struct xfrm_state_afinfo *afinfo = xfrm_state_lock_afinfo(family);
217 const struct xfrm_type **typemap;
218 int err = 0;
220 if (unlikely(afinfo == NULL))
221 return -EAFNOSUPPORT;
222 typemap = afinfo->type_map;
224 if (unlikely(typemap[type->proto] != type))
225 err = -ENOENT;
226 else
227 typemap[type->proto] = NULL;
228 xfrm_state_unlock_afinfo(afinfo);
229 return err;
231 EXPORT_SYMBOL(xfrm_unregister_type);
233 static const struct xfrm_type *xfrm_get_type(u8 proto, unsigned short family)
235 struct xfrm_state_afinfo *afinfo;
236 const struct xfrm_type **typemap;
237 const struct xfrm_type *type;
238 int modload_attempted = 0;
240 retry:
241 afinfo = xfrm_state_get_afinfo(family);
242 if (unlikely(afinfo == NULL))
243 return NULL;
244 typemap = afinfo->type_map;
246 type = typemap[proto];
247 if (unlikely(type && !try_module_get(type->owner)))
248 type = NULL;
249 if (!type && !modload_attempted) {
250 xfrm_state_put_afinfo(afinfo);
251 request_module("xfrm-type-%d-%d", family, proto);
252 modload_attempted = 1;
253 goto retry;
256 xfrm_state_put_afinfo(afinfo);
257 return type;
260 static void xfrm_put_type(const struct xfrm_type *type)
262 module_put(type->owner);
265 int xfrm_register_mode(struct xfrm_mode *mode, int family)
267 struct xfrm_state_afinfo *afinfo;
268 struct xfrm_mode **modemap;
269 int err;
271 if (unlikely(mode->encap >= XFRM_MODE_MAX))
272 return -EINVAL;
274 afinfo = xfrm_state_lock_afinfo(family);
275 if (unlikely(afinfo == NULL))
276 return -EAFNOSUPPORT;
278 err = -EEXIST;
279 modemap = afinfo->mode_map;
280 if (modemap[mode->encap])
281 goto out;
283 err = -ENOENT;
284 if (!try_module_get(afinfo->owner))
285 goto out;
287 mode->afinfo = afinfo;
288 modemap[mode->encap] = mode;
289 err = 0;
291 out:
292 xfrm_state_unlock_afinfo(afinfo);
293 return err;
295 EXPORT_SYMBOL(xfrm_register_mode);
297 int xfrm_unregister_mode(struct xfrm_mode *mode, int family)
299 struct xfrm_state_afinfo *afinfo;
300 struct xfrm_mode **modemap;
301 int err;
303 if (unlikely(mode->encap >= XFRM_MODE_MAX))
304 return -EINVAL;
306 afinfo = xfrm_state_lock_afinfo(family);
307 if (unlikely(afinfo == NULL))
308 return -EAFNOSUPPORT;
310 err = -ENOENT;
311 modemap = afinfo->mode_map;
312 if (likely(modemap[mode->encap] == mode)) {
313 modemap[mode->encap] = NULL;
314 module_put(mode->afinfo->owner);
315 err = 0;
318 xfrm_state_unlock_afinfo(afinfo);
319 return err;
321 EXPORT_SYMBOL(xfrm_unregister_mode);
323 static struct xfrm_mode *xfrm_get_mode(unsigned int encap, int family)
325 struct xfrm_state_afinfo *afinfo;
326 struct xfrm_mode *mode;
327 int modload_attempted = 0;
329 if (unlikely(encap >= XFRM_MODE_MAX))
330 return NULL;
332 retry:
333 afinfo = xfrm_state_get_afinfo(family);
334 if (unlikely(afinfo == NULL))
335 return NULL;
337 mode = afinfo->mode_map[encap];
338 if (unlikely(mode && !try_module_get(mode->owner)))
339 mode = NULL;
340 if (!mode && !modload_attempted) {
341 xfrm_state_put_afinfo(afinfo);
342 request_module("xfrm-mode-%d-%d", family, encap);
343 modload_attempted = 1;
344 goto retry;
347 xfrm_state_put_afinfo(afinfo);
348 return mode;
351 static void xfrm_put_mode(struct xfrm_mode *mode)
353 module_put(mode->owner);
356 static void xfrm_state_gc_destroy(struct xfrm_state *x)
358 tasklet_hrtimer_cancel(&x->mtimer);
359 del_timer_sync(&x->rtimer);
360 kfree(x->aalg);
361 kfree(x->ealg);
362 kfree(x->calg);
363 kfree(x->encap);
364 kfree(x->coaddr);
365 if (x->inner_mode)
366 xfrm_put_mode(x->inner_mode);
367 if (x->inner_mode_iaf)
368 xfrm_put_mode(x->inner_mode_iaf);
369 if (x->outer_mode)
370 xfrm_put_mode(x->outer_mode);
371 if (x->type) {
372 x->type->destructor(x);
373 xfrm_put_type(x->type);
375 security_xfrm_state_free(x);
376 kfree(x);
379 static void xfrm_state_gc_task(struct work_struct *work)
381 struct net *net = container_of(work, struct net, xfrm.state_gc_work);
382 struct xfrm_state *x;
383 struct hlist_node *entry, *tmp;
384 struct hlist_head gc_list;
386 spin_lock_bh(&xfrm_state_gc_lock);
387 hlist_move_list(&net->xfrm.state_gc_list, &gc_list);
388 spin_unlock_bh(&xfrm_state_gc_lock);
390 hlist_for_each_entry_safe(x, entry, tmp, &gc_list, gclist)
391 xfrm_state_gc_destroy(x);
393 wake_up(&net->xfrm.km_waitq);
396 static inline unsigned long make_jiffies(long secs)
398 if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
399 return MAX_SCHEDULE_TIMEOUT-1;
400 else
401 return secs*HZ;
404 static enum hrtimer_restart xfrm_timer_handler(struct hrtimer * me)
406 struct tasklet_hrtimer *thr = container_of(me, struct tasklet_hrtimer, timer);
407 struct xfrm_state *x = container_of(thr, struct xfrm_state, mtimer);
408 struct net *net = xs_net(x);
409 unsigned long now = get_seconds();
410 long next = LONG_MAX;
411 int warn = 0;
412 int err = 0;
414 spin_lock(&x->lock);
415 if (x->km.state == XFRM_STATE_DEAD)
416 goto out;
417 if (x->km.state == XFRM_STATE_EXPIRED)
418 goto expired;
419 if (x->lft.hard_add_expires_seconds) {
420 long tmo = x->lft.hard_add_expires_seconds +
421 x->curlft.add_time - now;
422 if (tmo <= 0)
423 goto expired;
424 if (tmo < next)
425 next = tmo;
427 if (x->lft.hard_use_expires_seconds) {
428 long tmo = x->lft.hard_use_expires_seconds +
429 (x->curlft.use_time ? : now) - now;
430 if (tmo <= 0)
431 goto expired;
432 if (tmo < next)
433 next = tmo;
435 if (x->km.dying)
436 goto resched;
437 if (x->lft.soft_add_expires_seconds) {
438 long tmo = x->lft.soft_add_expires_seconds +
439 x->curlft.add_time - now;
440 if (tmo <= 0)
441 warn = 1;
442 else if (tmo < next)
443 next = tmo;
445 if (x->lft.soft_use_expires_seconds) {
446 long tmo = x->lft.soft_use_expires_seconds +
447 (x->curlft.use_time ? : now) - now;
448 if (tmo <= 0)
449 warn = 1;
450 else if (tmo < next)
451 next = tmo;
454 x->km.dying = warn;
455 if (warn)
456 km_state_expired(x, 0, 0);
457 resched:
458 if (next != LONG_MAX){
459 tasklet_hrtimer_start(&x->mtimer, ktime_set(next, 0), HRTIMER_MODE_REL);
462 goto out;
464 expired:
465 if (x->km.state == XFRM_STATE_ACQ && x->id.spi == 0) {
466 x->km.state = XFRM_STATE_EXPIRED;
467 wake_up(&net->xfrm.km_waitq);
468 next = 2;
469 goto resched;
472 err = __xfrm_state_delete(x);
473 if (!err && x->id.spi)
474 km_state_expired(x, 1, 0);
476 xfrm_audit_state_delete(x, err ? 0 : 1,
477 audit_get_loginuid(current),
478 audit_get_sessionid(current), 0);
480 out:
481 spin_unlock(&x->lock);
482 return HRTIMER_NORESTART;
485 static void xfrm_replay_timer_handler(unsigned long data);
487 struct xfrm_state *xfrm_state_alloc(struct net *net)
489 struct xfrm_state *x;
491 x = kzalloc(sizeof(struct xfrm_state), GFP_ATOMIC);
493 if (x) {
494 write_pnet(&x->xs_net, net);
495 atomic_set(&x->refcnt, 1);
496 atomic_set(&x->tunnel_users, 0);
497 INIT_LIST_HEAD(&x->km.all);
498 INIT_HLIST_NODE(&x->bydst);
499 INIT_HLIST_NODE(&x->bysrc);
500 INIT_HLIST_NODE(&x->byspi);
501 tasklet_hrtimer_init(&x->mtimer, xfrm_timer_handler, CLOCK_REALTIME, HRTIMER_MODE_ABS);
502 setup_timer(&x->rtimer, xfrm_replay_timer_handler,
503 (unsigned long)x);
504 x->curlft.add_time = get_seconds();
505 x->lft.soft_byte_limit = XFRM_INF;
506 x->lft.soft_packet_limit = XFRM_INF;
507 x->lft.hard_byte_limit = XFRM_INF;
508 x->lft.hard_packet_limit = XFRM_INF;
509 x->replay_maxage = 0;
510 x->replay_maxdiff = 0;
511 x->inner_mode = NULL;
512 x->inner_mode_iaf = NULL;
513 spin_lock_init(&x->lock);
515 return x;
517 EXPORT_SYMBOL(xfrm_state_alloc);
519 void __xfrm_state_destroy(struct xfrm_state *x)
521 struct net *net = xs_net(x);
523 WARN_ON(x->km.state != XFRM_STATE_DEAD);
525 spin_lock_bh(&xfrm_state_gc_lock);
526 hlist_add_head(&x->gclist, &net->xfrm.state_gc_list);
527 spin_unlock_bh(&xfrm_state_gc_lock);
528 schedule_work(&net->xfrm.state_gc_work);
530 EXPORT_SYMBOL(__xfrm_state_destroy);
532 int __xfrm_state_delete(struct xfrm_state *x)
534 struct net *net = xs_net(x);
535 int err = -ESRCH;
537 if (x->km.state != XFRM_STATE_DEAD) {
538 x->km.state = XFRM_STATE_DEAD;
539 spin_lock(&xfrm_state_lock);
540 list_del(&x->km.all);
541 hlist_del(&x->bydst);
542 hlist_del(&x->bysrc);
543 if (x->id.spi)
544 hlist_del(&x->byspi);
545 net->xfrm.state_num--;
546 spin_unlock(&xfrm_state_lock);
548 /* All xfrm_state objects are created by xfrm_state_alloc.
549 * The xfrm_state_alloc call gives a reference, and that
550 * is what we are dropping here.
552 xfrm_state_put(x);
553 err = 0;
556 return err;
558 EXPORT_SYMBOL(__xfrm_state_delete);
560 int xfrm_state_delete(struct xfrm_state *x)
562 int err;
564 spin_lock_bh(&x->lock);
565 err = __xfrm_state_delete(x);
566 spin_unlock_bh(&x->lock);
568 return err;
570 EXPORT_SYMBOL(xfrm_state_delete);
572 #ifdef CONFIG_SECURITY_NETWORK_XFRM
573 static inline int
574 xfrm_state_flush_secctx_check(struct net *net, u8 proto, struct xfrm_audit *audit_info)
576 int i, err = 0;
578 for (i = 0; i <= net->xfrm.state_hmask; i++) {
579 struct hlist_node *entry;
580 struct xfrm_state *x;
582 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+i, bydst) {
583 if (xfrm_id_proto_match(x->id.proto, proto) &&
584 (err = security_xfrm_state_delete(x)) != 0) {
585 xfrm_audit_state_delete(x, 0,
586 audit_info->loginuid,
587 audit_info->sessionid,
588 audit_info->secid);
589 return err;
594 return err;
596 #else
597 static inline int
598 xfrm_state_flush_secctx_check(struct net *net, u8 proto, struct xfrm_audit *audit_info)
600 return 0;
602 #endif
604 int xfrm_state_flush(struct net *net, u8 proto, struct xfrm_audit *audit_info)
606 int i, err = 0, cnt = 0;
608 spin_lock_bh(&xfrm_state_lock);
609 err = xfrm_state_flush_secctx_check(net, proto, audit_info);
610 if (err)
611 goto out;
613 err = -ESRCH;
614 for (i = 0; i <= net->xfrm.state_hmask; i++) {
615 struct hlist_node *entry;
616 struct xfrm_state *x;
617 restart:
618 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+i, bydst) {
619 if (!xfrm_state_kern(x) &&
620 xfrm_id_proto_match(x->id.proto, proto)) {
621 xfrm_state_hold(x);
622 spin_unlock_bh(&xfrm_state_lock);
624 err = xfrm_state_delete(x);
625 xfrm_audit_state_delete(x, err ? 0 : 1,
626 audit_info->loginuid,
627 audit_info->sessionid,
628 audit_info->secid);
629 xfrm_state_put(x);
630 if (!err)
631 cnt++;
633 spin_lock_bh(&xfrm_state_lock);
634 goto restart;
638 if (cnt)
639 err = 0;
641 out:
642 spin_unlock_bh(&xfrm_state_lock);
643 wake_up(&net->xfrm.km_waitq);
644 return err;
646 EXPORT_SYMBOL(xfrm_state_flush);
648 void xfrm_sad_getinfo(struct net *net, struct xfrmk_sadinfo *si)
650 spin_lock_bh(&xfrm_state_lock);
651 si->sadcnt = net->xfrm.state_num;
652 si->sadhcnt = net->xfrm.state_hmask;
653 si->sadhmcnt = xfrm_state_hashmax;
654 spin_unlock_bh(&xfrm_state_lock);
656 EXPORT_SYMBOL(xfrm_sad_getinfo);
658 static int
659 xfrm_init_tempsel(struct xfrm_state *x, struct flowi *fl,
660 struct xfrm_tmpl *tmpl,
661 xfrm_address_t *daddr, xfrm_address_t *saddr,
662 unsigned short family)
664 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
665 if (!afinfo)
666 return -1;
667 afinfo->init_tempsel(x, fl, tmpl, daddr, saddr);
668 xfrm_state_put_afinfo(afinfo);
669 return 0;
672 static struct xfrm_state *__xfrm_state_lookup(struct net *net, u32 mark, xfrm_address_t *daddr, __be32 spi, u8 proto, unsigned short family)
674 unsigned int h = xfrm_spi_hash(net, daddr, spi, proto, family);
675 struct xfrm_state *x;
676 struct hlist_node *entry;
678 hlist_for_each_entry(x, entry, net->xfrm.state_byspi+h, byspi) {
679 if (x->props.family != family ||
680 x->id.spi != spi ||
681 x->id.proto != proto ||
682 xfrm_addr_cmp(&x->id.daddr, daddr, family))
683 continue;
685 if ((mark & x->mark.m) != x->mark.v)
686 continue;
687 xfrm_state_hold(x);
688 return x;
691 return NULL;
694 static struct xfrm_state *__xfrm_state_lookup_byaddr(struct net *net, u32 mark, xfrm_address_t *daddr, xfrm_address_t *saddr, u8 proto, unsigned short family)
696 unsigned int h = xfrm_src_hash(net, daddr, saddr, family);
697 struct xfrm_state *x;
698 struct hlist_node *entry;
700 hlist_for_each_entry(x, entry, net->xfrm.state_bysrc+h, bysrc) {
701 if (x->props.family != family ||
702 x->id.proto != proto ||
703 xfrm_addr_cmp(&x->id.daddr, daddr, family) ||
704 xfrm_addr_cmp(&x->props.saddr, saddr, family))
705 continue;
707 if ((mark & x->mark.m) != x->mark.v)
708 continue;
709 xfrm_state_hold(x);
710 return x;
713 return NULL;
716 static inline struct xfrm_state *
717 __xfrm_state_locate(struct xfrm_state *x, int use_spi, int family)
719 struct net *net = xs_net(x);
720 u32 mark = x->mark.v & x->mark.m;
722 if (use_spi)
723 return __xfrm_state_lookup(net, mark, &x->id.daddr,
724 x->id.spi, x->id.proto, family);
725 else
726 return __xfrm_state_lookup_byaddr(net, mark,
727 &x->id.daddr,
728 &x->props.saddr,
729 x->id.proto, family);
732 static void xfrm_hash_grow_check(struct net *net, int have_hash_collision)
734 if (have_hash_collision &&
735 (net->xfrm.state_hmask + 1) < xfrm_state_hashmax &&
736 net->xfrm.state_num > net->xfrm.state_hmask)
737 schedule_work(&net->xfrm.state_hash_work);
740 static void xfrm_state_look_at(struct xfrm_policy *pol, struct xfrm_state *x,
741 struct flowi *fl, unsigned short family,
742 xfrm_address_t *daddr, xfrm_address_t *saddr,
743 struct xfrm_state **best, int *acq_in_progress,
744 int *error)
746 /* Resolution logic:
747 * 1. There is a valid state with matching selector. Done.
748 * 2. Valid state with inappropriate selector. Skip.
750 * Entering area of "sysdeps".
752 * 3. If state is not valid, selector is temporary, it selects
753 * only session which triggered previous resolution. Key
754 * manager will do something to install a state with proper
755 * selector.
757 if (x->km.state == XFRM_STATE_VALID) {
758 if ((x->sel.family &&
759 !xfrm_selector_match(&x->sel, fl, x->sel.family)) ||
760 !security_xfrm_state_pol_flow_match(x, pol, fl))
761 return;
763 if (!*best ||
764 (*best)->km.dying > x->km.dying ||
765 ((*best)->km.dying == x->km.dying &&
766 (*best)->curlft.add_time < x->curlft.add_time))
767 *best = x;
768 } else if (x->km.state == XFRM_STATE_ACQ) {
769 *acq_in_progress = 1;
770 } else if (x->km.state == XFRM_STATE_ERROR ||
771 x->km.state == XFRM_STATE_EXPIRED) {
772 if (xfrm_selector_match(&x->sel, fl, x->sel.family) &&
773 security_xfrm_state_pol_flow_match(x, pol, fl))
774 *error = -ESRCH;
778 struct xfrm_state *
779 xfrm_state_find(xfrm_address_t *daddr, xfrm_address_t *saddr,
780 struct flowi *fl, struct xfrm_tmpl *tmpl,
781 struct xfrm_policy *pol, int *err,
782 unsigned short family)
784 static xfrm_address_t saddr_wildcard = { };
785 struct net *net = xp_net(pol);
786 unsigned int h, h_wildcard;
787 struct hlist_node *entry;
788 struct xfrm_state *x, *x0, *to_put;
789 int acquire_in_progress = 0;
790 int error = 0;
791 struct xfrm_state *best = NULL;
792 u32 mark = pol->mark.v & pol->mark.m;
794 to_put = NULL;
796 spin_lock_bh(&xfrm_state_lock);
797 h = xfrm_dst_hash(net, daddr, saddr, tmpl->reqid, family);
798 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+h, bydst) {
799 if (x->props.family == family &&
800 x->props.reqid == tmpl->reqid &&
801 (mark & x->mark.m) == x->mark.v &&
802 !(x->props.flags & XFRM_STATE_WILDRECV) &&
803 xfrm_state_addr_check(x, daddr, saddr, family) &&
804 tmpl->mode == x->props.mode &&
805 tmpl->id.proto == x->id.proto &&
806 (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
807 xfrm_state_look_at(pol, x, fl, family, daddr, saddr,
808 &best, &acquire_in_progress, &error);
810 if (best)
811 goto found;
813 h_wildcard = xfrm_dst_hash(net, daddr, &saddr_wildcard, tmpl->reqid, family);
814 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+h_wildcard, bydst) {
815 if (x->props.family == family &&
816 x->props.reqid == tmpl->reqid &&
817 (mark & x->mark.m) == x->mark.v &&
818 !(x->props.flags & XFRM_STATE_WILDRECV) &&
819 xfrm_state_addr_check(x, daddr, saddr, family) &&
820 tmpl->mode == x->props.mode &&
821 tmpl->id.proto == x->id.proto &&
822 (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
823 xfrm_state_look_at(pol, x, fl, family, daddr, saddr,
824 &best, &acquire_in_progress, &error);
827 found:
828 x = best;
829 if (!x && !error && !acquire_in_progress) {
830 if (tmpl->id.spi &&
831 (x0 = __xfrm_state_lookup(net, mark, daddr, tmpl->id.spi,
832 tmpl->id.proto, family)) != NULL) {
833 to_put = x0;
834 error = -EEXIST;
835 goto out;
837 x = xfrm_state_alloc(net);
838 if (x == NULL) {
839 error = -ENOMEM;
840 goto out;
842 /* Initialize temporary selector matching only
843 * to current session. */
844 xfrm_init_tempsel(x, fl, tmpl, daddr, saddr, family);
845 memcpy(&x->mark, &pol->mark, sizeof(x->mark));
847 error = security_xfrm_state_alloc_acquire(x, pol->security, fl->secid);
848 if (error) {
849 x->km.state = XFRM_STATE_DEAD;
850 to_put = x;
851 x = NULL;
852 goto out;
855 if (km_query(x, tmpl, pol) == 0) {
856 x->km.state = XFRM_STATE_ACQ;
857 list_add(&x->km.all, &net->xfrm.state_all);
858 hlist_add_head(&x->bydst, net->xfrm.state_bydst+h);
859 h = xfrm_src_hash(net, daddr, saddr, family);
860 hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h);
861 if (x->id.spi) {
862 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, family);
863 hlist_add_head(&x->byspi, net->xfrm.state_byspi+h);
865 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
866 tasklet_hrtimer_start(&x->mtimer, ktime_set(net->xfrm.sysctl_acq_expires, 0), HRTIMER_MODE_REL);
867 net->xfrm.state_num++;
868 xfrm_hash_grow_check(net, x->bydst.next != NULL);
869 } else {
870 x->km.state = XFRM_STATE_DEAD;
871 to_put = x;
872 x = NULL;
873 error = -ESRCH;
876 out:
877 if (x)
878 xfrm_state_hold(x);
879 else
880 *err = acquire_in_progress ? -EAGAIN : error;
881 spin_unlock_bh(&xfrm_state_lock);
882 if (to_put)
883 xfrm_state_put(to_put);
884 return x;
887 struct xfrm_state *
888 xfrm_stateonly_find(struct net *net, u32 mark,
889 xfrm_address_t *daddr, xfrm_address_t *saddr,
890 unsigned short family, u8 mode, u8 proto, u32 reqid)
892 unsigned int h;
893 struct xfrm_state *rx = NULL, *x = NULL;
894 struct hlist_node *entry;
896 spin_lock(&xfrm_state_lock);
897 h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
898 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+h, bydst) {
899 if (x->props.family == family &&
900 x->props.reqid == reqid &&
901 (mark & x->mark.m) == x->mark.v &&
902 !(x->props.flags & XFRM_STATE_WILDRECV) &&
903 xfrm_state_addr_check(x, daddr, saddr, family) &&
904 mode == x->props.mode &&
905 proto == x->id.proto &&
906 x->km.state == XFRM_STATE_VALID) {
907 rx = x;
908 break;
912 if (rx)
913 xfrm_state_hold(rx);
914 spin_unlock(&xfrm_state_lock);
917 return rx;
919 EXPORT_SYMBOL(xfrm_stateonly_find);
921 static void __xfrm_state_insert(struct xfrm_state *x)
923 struct net *net = xs_net(x);
924 unsigned int h;
926 x->genid = ++xfrm_state_genid;
928 list_add(&x->km.all, &net->xfrm.state_all);
930 h = xfrm_dst_hash(net, &x->id.daddr, &x->props.saddr,
931 x->props.reqid, x->props.family);
932 hlist_add_head(&x->bydst, net->xfrm.state_bydst+h);
934 h = xfrm_src_hash(net, &x->id.daddr, &x->props.saddr, x->props.family);
935 hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h);
937 if (x->id.spi) {
938 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto,
939 x->props.family);
941 hlist_add_head(&x->byspi, net->xfrm.state_byspi+h);
944 tasklet_hrtimer_start(&x->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL);
945 if (x->replay_maxage)
946 mod_timer(&x->rtimer, jiffies + x->replay_maxage);
948 wake_up(&net->xfrm.km_waitq);
950 net->xfrm.state_num++;
952 xfrm_hash_grow_check(net, x->bydst.next != NULL);
955 /* xfrm_state_lock is held */
956 static void __xfrm_state_bump_genids(struct xfrm_state *xnew)
958 struct net *net = xs_net(xnew);
959 unsigned short family = xnew->props.family;
960 u32 reqid = xnew->props.reqid;
961 struct xfrm_state *x;
962 struct hlist_node *entry;
963 unsigned int h;
964 u32 mark = xnew->mark.v & xnew->mark.m;
966 h = xfrm_dst_hash(net, &xnew->id.daddr, &xnew->props.saddr, reqid, family);
967 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+h, bydst) {
968 if (x->props.family == family &&
969 x->props.reqid == reqid &&
970 (mark & x->mark.m) == x->mark.v &&
971 !xfrm_addr_cmp(&x->id.daddr, &xnew->id.daddr, family) &&
972 !xfrm_addr_cmp(&x->props.saddr, &xnew->props.saddr, family))
973 x->genid = xfrm_state_genid;
977 void xfrm_state_insert(struct xfrm_state *x)
979 spin_lock_bh(&xfrm_state_lock);
980 __xfrm_state_bump_genids(x);
981 __xfrm_state_insert(x);
982 spin_unlock_bh(&xfrm_state_lock);
984 EXPORT_SYMBOL(xfrm_state_insert);
986 /* xfrm_state_lock is held */
987 static 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)
989 unsigned int h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
990 struct hlist_node *entry;
991 struct xfrm_state *x;
992 u32 mark = m->v & m->m;
994 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+h, bydst) {
995 if (x->props.reqid != reqid ||
996 x->props.mode != mode ||
997 x->props.family != family ||
998 x->km.state != XFRM_STATE_ACQ ||
999 x->id.spi != 0 ||
1000 x->id.proto != proto ||
1001 (mark & x->mark.m) != x->mark.v ||
1002 xfrm_addr_cmp(&x->id.daddr, daddr, family) ||
1003 xfrm_addr_cmp(&x->props.saddr, saddr, family))
1004 continue;
1006 xfrm_state_hold(x);
1007 return x;
1010 if (!create)
1011 return NULL;
1013 x = xfrm_state_alloc(net);
1014 if (likely(x)) {
1015 switch (family) {
1016 case AF_INET:
1017 x->sel.daddr.a4 = daddr->a4;
1018 x->sel.saddr.a4 = saddr->a4;
1019 x->sel.prefixlen_d = 32;
1020 x->sel.prefixlen_s = 32;
1021 x->props.saddr.a4 = saddr->a4;
1022 x->id.daddr.a4 = daddr->a4;
1023 break;
1025 case AF_INET6:
1026 ipv6_addr_copy((struct in6_addr *)x->sel.daddr.a6,
1027 (struct in6_addr *)daddr);
1028 ipv6_addr_copy((struct in6_addr *)x->sel.saddr.a6,
1029 (struct in6_addr *)saddr);
1030 x->sel.prefixlen_d = 128;
1031 x->sel.prefixlen_s = 128;
1032 ipv6_addr_copy((struct in6_addr *)x->props.saddr.a6,
1033 (struct in6_addr *)saddr);
1034 ipv6_addr_copy((struct in6_addr *)x->id.daddr.a6,
1035 (struct in6_addr *)daddr);
1036 break;
1039 x->km.state = XFRM_STATE_ACQ;
1040 x->id.proto = proto;
1041 x->props.family = family;
1042 x->props.mode = mode;
1043 x->props.reqid = reqid;
1044 x->mark.v = m->v;
1045 x->mark.m = m->m;
1046 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
1047 xfrm_state_hold(x);
1048 tasklet_hrtimer_start(&x->mtimer, ktime_set(net->xfrm.sysctl_acq_expires, 0), HRTIMER_MODE_REL);
1049 list_add(&x->km.all, &net->xfrm.state_all);
1050 hlist_add_head(&x->bydst, net->xfrm.state_bydst+h);
1051 h = xfrm_src_hash(net, daddr, saddr, family);
1052 hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h);
1054 net->xfrm.state_num++;
1056 xfrm_hash_grow_check(net, x->bydst.next != NULL);
1059 return x;
1062 static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq);
1064 int xfrm_state_add(struct xfrm_state *x)
1066 struct net *net = xs_net(x);
1067 struct xfrm_state *x1, *to_put;
1068 int family;
1069 int err;
1070 u32 mark = x->mark.v & x->mark.m;
1071 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
1073 family = x->props.family;
1075 to_put = NULL;
1077 spin_lock_bh(&xfrm_state_lock);
1079 x1 = __xfrm_state_locate(x, use_spi, family);
1080 if (x1) {
1081 to_put = x1;
1082 x1 = NULL;
1083 err = -EEXIST;
1084 goto out;
1087 if (use_spi && x->km.seq) {
1088 x1 = __xfrm_find_acq_byseq(net, mark, x->km.seq);
1089 if (x1 && ((x1->id.proto != x->id.proto) ||
1090 xfrm_addr_cmp(&x1->id.daddr, &x->id.daddr, family))) {
1091 to_put = x1;
1092 x1 = NULL;
1096 if (use_spi && !x1)
1097 x1 = __find_acq_core(net, &x->mark, family, x->props.mode,
1098 x->props.reqid, x->id.proto,
1099 &x->id.daddr, &x->props.saddr, 0);
1101 __xfrm_state_bump_genids(x);
1102 __xfrm_state_insert(x);
1103 err = 0;
1105 out:
1106 spin_unlock_bh(&xfrm_state_lock);
1108 if (x1) {
1109 xfrm_state_delete(x1);
1110 xfrm_state_put(x1);
1113 if (to_put)
1114 xfrm_state_put(to_put);
1116 return err;
1118 EXPORT_SYMBOL(xfrm_state_add);
1120 #ifdef CONFIG_XFRM_MIGRATE
1121 static struct xfrm_state *xfrm_state_clone(struct xfrm_state *orig, int *errp)
1123 struct net *net = xs_net(orig);
1124 int err = -ENOMEM;
1125 struct xfrm_state *x = xfrm_state_alloc(net);
1126 if (!x)
1127 goto out;
1129 memcpy(&x->id, &orig->id, sizeof(x->id));
1130 memcpy(&x->sel, &orig->sel, sizeof(x->sel));
1131 memcpy(&x->lft, &orig->lft, sizeof(x->lft));
1132 x->props.mode = orig->props.mode;
1133 x->props.replay_window = orig->props.replay_window;
1134 x->props.reqid = orig->props.reqid;
1135 x->props.family = orig->props.family;
1136 x->props.saddr = orig->props.saddr;
1138 if (orig->aalg) {
1139 x->aalg = xfrm_algo_auth_clone(orig->aalg);
1140 if (!x->aalg)
1141 goto error;
1143 x->props.aalgo = orig->props.aalgo;
1145 if (orig->ealg) {
1146 x->ealg = xfrm_algo_clone(orig->ealg);
1147 if (!x->ealg)
1148 goto error;
1150 x->props.ealgo = orig->props.ealgo;
1152 if (orig->calg) {
1153 x->calg = xfrm_algo_clone(orig->calg);
1154 if (!x->calg)
1155 goto error;
1157 x->props.calgo = orig->props.calgo;
1159 if (orig->encap) {
1160 x->encap = kmemdup(orig->encap, sizeof(*x->encap), GFP_KERNEL);
1161 if (!x->encap)
1162 goto error;
1165 if (orig->coaddr) {
1166 x->coaddr = kmemdup(orig->coaddr, sizeof(*x->coaddr),
1167 GFP_KERNEL);
1168 if (!x->coaddr)
1169 goto error;
1172 memcpy(&x->mark, &orig->mark, sizeof(x->mark));
1174 err = xfrm_init_state(x);
1175 if (err)
1176 goto error;
1178 x->props.flags = orig->props.flags;
1180 x->curlft.add_time = orig->curlft.add_time;
1181 x->km.state = orig->km.state;
1182 x->km.seq = orig->km.seq;
1184 return x;
1186 error:
1187 xfrm_state_put(x);
1188 out:
1189 if (errp)
1190 *errp = err;
1191 return NULL;
1194 /* xfrm_state_lock is held */
1195 struct xfrm_state * xfrm_migrate_state_find(struct xfrm_migrate *m)
1197 unsigned int h;
1198 struct xfrm_state *x;
1199 struct hlist_node *entry;
1201 if (m->reqid) {
1202 h = xfrm_dst_hash(&init_net, &m->old_daddr, &m->old_saddr,
1203 m->reqid, m->old_family);
1204 hlist_for_each_entry(x, entry, init_net.xfrm.state_bydst+h, bydst) {
1205 if (x->props.mode != m->mode ||
1206 x->id.proto != m->proto)
1207 continue;
1208 if (m->reqid && x->props.reqid != m->reqid)
1209 continue;
1210 if (xfrm_addr_cmp(&x->id.daddr, &m->old_daddr,
1211 m->old_family) ||
1212 xfrm_addr_cmp(&x->props.saddr, &m->old_saddr,
1213 m->old_family))
1214 continue;
1215 xfrm_state_hold(x);
1216 return x;
1218 } else {
1219 h = xfrm_src_hash(&init_net, &m->old_daddr, &m->old_saddr,
1220 m->old_family);
1221 hlist_for_each_entry(x, entry, init_net.xfrm.state_bysrc+h, bysrc) {
1222 if (x->props.mode != m->mode ||
1223 x->id.proto != m->proto)
1224 continue;
1225 if (xfrm_addr_cmp(&x->id.daddr, &m->old_daddr,
1226 m->old_family) ||
1227 xfrm_addr_cmp(&x->props.saddr, &m->old_saddr,
1228 m->old_family))
1229 continue;
1230 xfrm_state_hold(x);
1231 return x;
1235 return NULL;
1237 EXPORT_SYMBOL(xfrm_migrate_state_find);
1239 struct xfrm_state * xfrm_state_migrate(struct xfrm_state *x,
1240 struct xfrm_migrate *m)
1242 struct xfrm_state *xc;
1243 int err;
1245 xc = xfrm_state_clone(x, &err);
1246 if (!xc)
1247 return NULL;
1249 memcpy(&xc->id.daddr, &m->new_daddr, sizeof(xc->id.daddr));
1250 memcpy(&xc->props.saddr, &m->new_saddr, sizeof(xc->props.saddr));
1252 /* add state */
1253 if (!xfrm_addr_cmp(&x->id.daddr, &m->new_daddr, m->new_family)) {
1254 /* a care is needed when the destination address of the
1255 state is to be updated as it is a part of triplet */
1256 xfrm_state_insert(xc);
1257 } else {
1258 if ((err = xfrm_state_add(xc)) < 0)
1259 goto error;
1262 return xc;
1263 error:
1264 kfree(xc);
1265 return NULL;
1267 EXPORT_SYMBOL(xfrm_state_migrate);
1268 #endif
1270 int xfrm_state_update(struct xfrm_state *x)
1272 struct xfrm_state *x1, *to_put;
1273 int err;
1274 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
1276 to_put = NULL;
1278 spin_lock_bh(&xfrm_state_lock);
1279 x1 = __xfrm_state_locate(x, use_spi, x->props.family);
1281 err = -ESRCH;
1282 if (!x1)
1283 goto out;
1285 if (xfrm_state_kern(x1)) {
1286 to_put = x1;
1287 err = -EEXIST;
1288 goto out;
1291 if (x1->km.state == XFRM_STATE_ACQ) {
1292 __xfrm_state_insert(x);
1293 x = NULL;
1295 err = 0;
1297 out:
1298 spin_unlock_bh(&xfrm_state_lock);
1300 if (to_put)
1301 xfrm_state_put(to_put);
1303 if (err)
1304 return err;
1306 if (!x) {
1307 xfrm_state_delete(x1);
1308 xfrm_state_put(x1);
1309 return 0;
1312 err = -EINVAL;
1313 spin_lock_bh(&x1->lock);
1314 if (likely(x1->km.state == XFRM_STATE_VALID)) {
1315 if (x->encap && x1->encap)
1316 memcpy(x1->encap, x->encap, sizeof(*x1->encap));
1317 if (x->coaddr && x1->coaddr) {
1318 memcpy(x1->coaddr, x->coaddr, sizeof(*x1->coaddr));
1320 if (!use_spi && memcmp(&x1->sel, &x->sel, sizeof(x1->sel)))
1321 memcpy(&x1->sel, &x->sel, sizeof(x1->sel));
1322 memcpy(&x1->lft, &x->lft, sizeof(x1->lft));
1323 x1->km.dying = 0;
1325 tasklet_hrtimer_start(&x1->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL);
1326 if (x1->curlft.use_time)
1327 xfrm_state_check_expire(x1);
1329 err = 0;
1331 spin_unlock_bh(&x1->lock);
1333 xfrm_state_put(x1);
1335 return err;
1337 EXPORT_SYMBOL(xfrm_state_update);
1339 int xfrm_state_check_expire(struct xfrm_state *x)
1341 if (!x->curlft.use_time)
1342 x->curlft.use_time = get_seconds();
1344 if (x->km.state != XFRM_STATE_VALID)
1345 return -EINVAL;
1347 if (x->curlft.bytes >= x->lft.hard_byte_limit ||
1348 x->curlft.packets >= x->lft.hard_packet_limit) {
1349 x->km.state = XFRM_STATE_EXPIRED;
1350 tasklet_hrtimer_start(&x->mtimer, ktime_set(0,0), HRTIMER_MODE_REL);
1351 return -EINVAL;
1354 if (!x->km.dying &&
1355 (x->curlft.bytes >= x->lft.soft_byte_limit ||
1356 x->curlft.packets >= x->lft.soft_packet_limit)) {
1357 x->km.dying = 1;
1358 km_state_expired(x, 0, 0);
1360 return 0;
1362 EXPORT_SYMBOL(xfrm_state_check_expire);
1364 struct xfrm_state *
1365 xfrm_state_lookup(struct net *net, u32 mark, xfrm_address_t *daddr, __be32 spi,
1366 u8 proto, unsigned short family)
1368 struct xfrm_state *x;
1370 spin_lock_bh(&xfrm_state_lock);
1371 x = __xfrm_state_lookup(net, mark, daddr, spi, proto, family);
1372 spin_unlock_bh(&xfrm_state_lock);
1373 return x;
1375 EXPORT_SYMBOL(xfrm_state_lookup);
1377 struct xfrm_state *
1378 xfrm_state_lookup_byaddr(struct net *net, u32 mark,
1379 xfrm_address_t *daddr, xfrm_address_t *saddr,
1380 u8 proto, unsigned short family)
1382 struct xfrm_state *x;
1384 spin_lock_bh(&xfrm_state_lock);
1385 x = __xfrm_state_lookup_byaddr(net, mark, daddr, saddr, proto, family);
1386 spin_unlock_bh(&xfrm_state_lock);
1387 return x;
1389 EXPORT_SYMBOL(xfrm_state_lookup_byaddr);
1391 struct xfrm_state *
1392 xfrm_find_acq(struct net *net, struct xfrm_mark *mark, u8 mode, u32 reqid, u8 proto,
1393 xfrm_address_t *daddr, xfrm_address_t *saddr,
1394 int create, unsigned short family)
1396 struct xfrm_state *x;
1398 spin_lock_bh(&xfrm_state_lock);
1399 x = __find_acq_core(net, mark, family, mode, reqid, proto, daddr, saddr, create);
1400 spin_unlock_bh(&xfrm_state_lock);
1402 return x;
1404 EXPORT_SYMBOL(xfrm_find_acq);
1406 #ifdef CONFIG_XFRM_SUB_POLICY
1408 xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n,
1409 unsigned short family)
1411 int err = 0;
1412 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
1413 if (!afinfo)
1414 return -EAFNOSUPPORT;
1416 spin_lock_bh(&xfrm_state_lock);
1417 if (afinfo->tmpl_sort)
1418 err = afinfo->tmpl_sort(dst, src, n);
1419 spin_unlock_bh(&xfrm_state_lock);
1420 xfrm_state_put_afinfo(afinfo);
1421 return err;
1423 EXPORT_SYMBOL(xfrm_tmpl_sort);
1426 xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src, int n,
1427 unsigned short family)
1429 int err = 0;
1430 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
1431 if (!afinfo)
1432 return -EAFNOSUPPORT;
1434 spin_lock_bh(&xfrm_state_lock);
1435 if (afinfo->state_sort)
1436 err = afinfo->state_sort(dst, src, n);
1437 spin_unlock_bh(&xfrm_state_lock);
1438 xfrm_state_put_afinfo(afinfo);
1439 return err;
1441 EXPORT_SYMBOL(xfrm_state_sort);
1442 #endif
1444 /* Silly enough, but I'm lazy to build resolution list */
1446 static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
1448 int i;
1450 for (i = 0; i <= net->xfrm.state_hmask; i++) {
1451 struct hlist_node *entry;
1452 struct xfrm_state *x;
1454 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+i, bydst) {
1455 if (x->km.seq == seq &&
1456 (mark & x->mark.m) == x->mark.v &&
1457 x->km.state == XFRM_STATE_ACQ) {
1458 xfrm_state_hold(x);
1459 return x;
1463 return NULL;
1466 struct xfrm_state *xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
1468 struct xfrm_state *x;
1470 spin_lock_bh(&xfrm_state_lock);
1471 x = __xfrm_find_acq_byseq(net, mark, seq);
1472 spin_unlock_bh(&xfrm_state_lock);
1473 return x;
1475 EXPORT_SYMBOL(xfrm_find_acq_byseq);
1477 u32 xfrm_get_acqseq(void)
1479 u32 res;
1480 static atomic_t acqseq;
1482 do {
1483 res = atomic_inc_return(&acqseq);
1484 } while (!res);
1486 return res;
1488 EXPORT_SYMBOL(xfrm_get_acqseq);
1490 int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high)
1492 struct net *net = xs_net(x);
1493 unsigned int h;
1494 struct xfrm_state *x0;
1495 int err = -ENOENT;
1496 __be32 minspi = htonl(low);
1497 __be32 maxspi = htonl(high);
1498 u32 mark = x->mark.v & x->mark.m;
1500 spin_lock_bh(&x->lock);
1501 if (x->km.state == XFRM_STATE_DEAD)
1502 goto unlock;
1504 err = 0;
1505 if (x->id.spi)
1506 goto unlock;
1508 err = -ENOENT;
1510 if (minspi == maxspi) {
1511 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, minspi, x->id.proto, x->props.family);
1512 if (x0) {
1513 xfrm_state_put(x0);
1514 goto unlock;
1516 x->id.spi = minspi;
1517 } else {
1518 u32 spi = 0;
1519 for (h=0; h<high-low+1; h++) {
1520 spi = low + net_random()%(high-low+1);
1521 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, htonl(spi), x->id.proto, x->props.family);
1522 if (x0 == NULL) {
1523 x->id.spi = htonl(spi);
1524 break;
1526 xfrm_state_put(x0);
1529 if (x->id.spi) {
1530 spin_lock_bh(&xfrm_state_lock);
1531 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, x->props.family);
1532 hlist_add_head(&x->byspi, net->xfrm.state_byspi+h);
1533 spin_unlock_bh(&xfrm_state_lock);
1535 err = 0;
1538 unlock:
1539 spin_unlock_bh(&x->lock);
1541 return err;
1543 EXPORT_SYMBOL(xfrm_alloc_spi);
1545 int xfrm_state_walk(struct net *net, struct xfrm_state_walk *walk,
1546 int (*func)(struct xfrm_state *, int, void*),
1547 void *data)
1549 struct xfrm_state *state;
1550 struct xfrm_state_walk *x;
1551 int err = 0;
1553 if (walk->seq != 0 && list_empty(&walk->all))
1554 return 0;
1556 spin_lock_bh(&xfrm_state_lock);
1557 if (list_empty(&walk->all))
1558 x = list_first_entry(&net->xfrm.state_all, struct xfrm_state_walk, all);
1559 else
1560 x = list_entry(&walk->all, struct xfrm_state_walk, all);
1561 list_for_each_entry_from(x, &net->xfrm.state_all, all) {
1562 if (x->state == XFRM_STATE_DEAD)
1563 continue;
1564 state = container_of(x, struct xfrm_state, km);
1565 if (!xfrm_id_proto_match(state->id.proto, walk->proto))
1566 continue;
1567 err = func(state, walk->seq, data);
1568 if (err) {
1569 list_move_tail(&walk->all, &x->all);
1570 goto out;
1572 walk->seq++;
1574 if (walk->seq == 0) {
1575 err = -ENOENT;
1576 goto out;
1578 list_del_init(&walk->all);
1579 out:
1580 spin_unlock_bh(&xfrm_state_lock);
1581 return err;
1583 EXPORT_SYMBOL(xfrm_state_walk);
1585 void xfrm_state_walk_init(struct xfrm_state_walk *walk, u8 proto)
1587 INIT_LIST_HEAD(&walk->all);
1588 walk->proto = proto;
1589 walk->state = XFRM_STATE_DEAD;
1590 walk->seq = 0;
1592 EXPORT_SYMBOL(xfrm_state_walk_init);
1594 void xfrm_state_walk_done(struct xfrm_state_walk *walk)
1596 if (list_empty(&walk->all))
1597 return;
1599 spin_lock_bh(&xfrm_state_lock);
1600 list_del(&walk->all);
1601 spin_unlock_bh(&xfrm_state_lock);
1603 EXPORT_SYMBOL(xfrm_state_walk_done);
1606 void xfrm_replay_notify(struct xfrm_state *x, int event)
1608 struct km_event c;
1609 /* we send notify messages in case
1610 * 1. we updated on of the sequence numbers, and the seqno difference
1611 * is at least x->replay_maxdiff, in this case we also update the
1612 * timeout of our timer function
1613 * 2. if x->replay_maxage has elapsed since last update,
1614 * and there were changes
1616 * The state structure must be locked!
1619 switch (event) {
1620 case XFRM_REPLAY_UPDATE:
1621 if (x->replay_maxdiff &&
1622 (x->replay.seq - x->preplay.seq < x->replay_maxdiff) &&
1623 (x->replay.oseq - x->preplay.oseq < x->replay_maxdiff)) {
1624 if (x->xflags & XFRM_TIME_DEFER)
1625 event = XFRM_REPLAY_TIMEOUT;
1626 else
1627 return;
1630 break;
1632 case XFRM_REPLAY_TIMEOUT:
1633 if ((x->replay.seq == x->preplay.seq) &&
1634 (x->replay.bitmap == x->preplay.bitmap) &&
1635 (x->replay.oseq == x->preplay.oseq)) {
1636 x->xflags |= XFRM_TIME_DEFER;
1637 return;
1640 break;
1643 memcpy(&x->preplay, &x->replay, sizeof(struct xfrm_replay_state));
1644 c.event = XFRM_MSG_NEWAE;
1645 c.data.aevent = event;
1646 km_state_notify(x, &c);
1648 if (x->replay_maxage &&
1649 !mod_timer(&x->rtimer, jiffies + x->replay_maxage))
1650 x->xflags &= ~XFRM_TIME_DEFER;
1653 static void xfrm_replay_timer_handler(unsigned long data)
1655 struct xfrm_state *x = (struct xfrm_state*)data;
1657 spin_lock(&x->lock);
1659 if (x->km.state == XFRM_STATE_VALID) {
1660 if (xfrm_aevent_is_on(xs_net(x)))
1661 xfrm_replay_notify(x, XFRM_REPLAY_TIMEOUT);
1662 else
1663 x->xflags |= XFRM_TIME_DEFER;
1666 spin_unlock(&x->lock);
1669 int xfrm_replay_check(struct xfrm_state *x,
1670 struct sk_buff *skb, __be32 net_seq)
1672 u32 diff;
1673 u32 seq = ntohl(net_seq);
1675 if (unlikely(seq == 0))
1676 goto err;
1678 if (likely(seq > x->replay.seq))
1679 return 0;
1681 diff = x->replay.seq - seq;
1682 if (diff >= min_t(unsigned int, x->props.replay_window,
1683 sizeof(x->replay.bitmap) * 8)) {
1684 x->stats.replay_window++;
1685 goto err;
1688 if (x->replay.bitmap & (1U << diff)) {
1689 x->stats.replay++;
1690 goto err;
1692 return 0;
1694 err:
1695 xfrm_audit_state_replay(x, skb, net_seq);
1696 return -EINVAL;
1699 void xfrm_replay_advance(struct xfrm_state *x, __be32 net_seq)
1701 u32 diff;
1702 u32 seq = ntohl(net_seq);
1704 if (seq > x->replay.seq) {
1705 diff = seq - x->replay.seq;
1706 if (diff < x->props.replay_window)
1707 x->replay.bitmap = ((x->replay.bitmap) << diff) | 1;
1708 else
1709 x->replay.bitmap = 1;
1710 x->replay.seq = seq;
1711 } else {
1712 diff = x->replay.seq - seq;
1713 x->replay.bitmap |= (1U << diff);
1716 if (xfrm_aevent_is_on(xs_net(x)))
1717 xfrm_replay_notify(x, XFRM_REPLAY_UPDATE);
1720 static LIST_HEAD(xfrm_km_list);
1721 static DEFINE_RWLOCK(xfrm_km_lock);
1723 void km_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
1725 struct xfrm_mgr *km;
1727 read_lock(&xfrm_km_lock);
1728 list_for_each_entry(km, &xfrm_km_list, list)
1729 if (km->notify_policy)
1730 km->notify_policy(xp, dir, c);
1731 read_unlock(&xfrm_km_lock);
1734 void km_state_notify(struct xfrm_state *x, struct km_event *c)
1736 struct xfrm_mgr *km;
1737 read_lock(&xfrm_km_lock);
1738 list_for_each_entry(km, &xfrm_km_list, list)
1739 if (km->notify)
1740 km->notify(x, c);
1741 read_unlock(&xfrm_km_lock);
1744 EXPORT_SYMBOL(km_policy_notify);
1745 EXPORT_SYMBOL(km_state_notify);
1747 void km_state_expired(struct xfrm_state *x, int hard, u32 pid)
1749 struct net *net = xs_net(x);
1750 struct km_event c;
1752 c.data.hard = hard;
1753 c.pid = pid;
1754 c.event = XFRM_MSG_EXPIRE;
1755 km_state_notify(x, &c);
1757 if (hard)
1758 wake_up(&net->xfrm.km_waitq);
1761 EXPORT_SYMBOL(km_state_expired);
1763 * We send to all registered managers regardless of failure
1764 * We are happy with one success
1766 int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol)
1768 int err = -EINVAL, acqret;
1769 struct xfrm_mgr *km;
1771 read_lock(&xfrm_km_lock);
1772 list_for_each_entry(km, &xfrm_km_list, list) {
1773 acqret = km->acquire(x, t, pol, XFRM_POLICY_OUT);
1774 if (!acqret)
1775 err = acqret;
1777 read_unlock(&xfrm_km_lock);
1778 return err;
1780 EXPORT_SYMBOL(km_query);
1782 int km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
1784 int err = -EINVAL;
1785 struct xfrm_mgr *km;
1787 read_lock(&xfrm_km_lock);
1788 list_for_each_entry(km, &xfrm_km_list, list) {
1789 if (km->new_mapping)
1790 err = km->new_mapping(x, ipaddr, sport);
1791 if (!err)
1792 break;
1794 read_unlock(&xfrm_km_lock);
1795 return err;
1797 EXPORT_SYMBOL(km_new_mapping);
1799 void km_policy_expired(struct xfrm_policy *pol, int dir, int hard, u32 pid)
1801 struct net *net = xp_net(pol);
1802 struct km_event c;
1804 c.data.hard = hard;
1805 c.pid = pid;
1806 c.event = XFRM_MSG_POLEXPIRE;
1807 km_policy_notify(pol, dir, &c);
1809 if (hard)
1810 wake_up(&net->xfrm.km_waitq);
1812 EXPORT_SYMBOL(km_policy_expired);
1814 #ifdef CONFIG_XFRM_MIGRATE
1815 int km_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
1816 struct xfrm_migrate *m, int num_migrate,
1817 struct xfrm_kmaddress *k)
1819 int err = -EINVAL;
1820 int ret;
1821 struct xfrm_mgr *km;
1823 read_lock(&xfrm_km_lock);
1824 list_for_each_entry(km, &xfrm_km_list, list) {
1825 if (km->migrate) {
1826 ret = km->migrate(sel, dir, type, m, num_migrate, k);
1827 if (!ret)
1828 err = ret;
1831 read_unlock(&xfrm_km_lock);
1832 return err;
1834 EXPORT_SYMBOL(km_migrate);
1835 #endif
1837 int km_report(struct net *net, u8 proto, struct xfrm_selector *sel, xfrm_address_t *addr)
1839 int err = -EINVAL;
1840 int ret;
1841 struct xfrm_mgr *km;
1843 read_lock(&xfrm_km_lock);
1844 list_for_each_entry(km, &xfrm_km_list, list) {
1845 if (km->report) {
1846 ret = km->report(net, proto, sel, addr);
1847 if (!ret)
1848 err = ret;
1851 read_unlock(&xfrm_km_lock);
1852 return err;
1854 EXPORT_SYMBOL(km_report);
1856 int xfrm_user_policy(struct sock *sk, int optname, u8 __user *optval, int optlen)
1858 int err;
1859 u8 *data;
1860 struct xfrm_mgr *km;
1861 struct xfrm_policy *pol = NULL;
1863 if (optlen <= 0 || optlen > PAGE_SIZE)
1864 return -EMSGSIZE;
1866 data = kmalloc(optlen, GFP_KERNEL);
1867 if (!data)
1868 return -ENOMEM;
1870 err = -EFAULT;
1871 if (copy_from_user(data, optval, optlen))
1872 goto out;
1874 err = -EINVAL;
1875 read_lock(&xfrm_km_lock);
1876 list_for_each_entry(km, &xfrm_km_list, list) {
1877 pol = km->compile_policy(sk, optname, data,
1878 optlen, &err);
1879 if (err >= 0)
1880 break;
1882 read_unlock(&xfrm_km_lock);
1884 if (err >= 0) {
1885 xfrm_sk_policy_insert(sk, err, pol);
1886 xfrm_pol_put(pol);
1887 err = 0;
1890 out:
1891 kfree(data);
1892 return err;
1894 EXPORT_SYMBOL(xfrm_user_policy);
1896 int xfrm_register_km(struct xfrm_mgr *km)
1898 write_lock_bh(&xfrm_km_lock);
1899 list_add_tail(&km->list, &xfrm_km_list);
1900 write_unlock_bh(&xfrm_km_lock);
1901 return 0;
1903 EXPORT_SYMBOL(xfrm_register_km);
1905 int xfrm_unregister_km(struct xfrm_mgr *km)
1907 write_lock_bh(&xfrm_km_lock);
1908 list_del(&km->list);
1909 write_unlock_bh(&xfrm_km_lock);
1910 return 0;
1912 EXPORT_SYMBOL(xfrm_unregister_km);
1914 int xfrm_state_register_afinfo(struct xfrm_state_afinfo *afinfo)
1916 int err = 0;
1917 if (unlikely(afinfo == NULL))
1918 return -EINVAL;
1919 if (unlikely(afinfo->family >= NPROTO))
1920 return -EAFNOSUPPORT;
1921 write_lock_bh(&xfrm_state_afinfo_lock);
1922 if (unlikely(xfrm_state_afinfo[afinfo->family] != NULL))
1923 err = -ENOBUFS;
1924 else
1925 xfrm_state_afinfo[afinfo->family] = afinfo;
1926 write_unlock_bh(&xfrm_state_afinfo_lock);
1927 return err;
1929 EXPORT_SYMBOL(xfrm_state_register_afinfo);
1931 int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo)
1933 int err = 0;
1934 if (unlikely(afinfo == NULL))
1935 return -EINVAL;
1936 if (unlikely(afinfo->family >= NPROTO))
1937 return -EAFNOSUPPORT;
1938 write_lock_bh(&xfrm_state_afinfo_lock);
1939 if (likely(xfrm_state_afinfo[afinfo->family] != NULL)) {
1940 if (unlikely(xfrm_state_afinfo[afinfo->family] != afinfo))
1941 err = -EINVAL;
1942 else
1943 xfrm_state_afinfo[afinfo->family] = NULL;
1945 write_unlock_bh(&xfrm_state_afinfo_lock);
1946 return err;
1948 EXPORT_SYMBOL(xfrm_state_unregister_afinfo);
1950 static struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family)
1952 struct xfrm_state_afinfo *afinfo;
1953 if (unlikely(family >= NPROTO))
1954 return NULL;
1955 read_lock(&xfrm_state_afinfo_lock);
1956 afinfo = xfrm_state_afinfo[family];
1957 if (unlikely(!afinfo))
1958 read_unlock(&xfrm_state_afinfo_lock);
1959 return afinfo;
1962 static void xfrm_state_put_afinfo(struct xfrm_state_afinfo *afinfo)
1963 __releases(xfrm_state_afinfo_lock)
1965 read_unlock(&xfrm_state_afinfo_lock);
1968 /* Temporarily located here until net/xfrm/xfrm_tunnel.c is created */
1969 void xfrm_state_delete_tunnel(struct xfrm_state *x)
1971 if (x->tunnel) {
1972 struct xfrm_state *t = x->tunnel;
1974 if (atomic_read(&t->tunnel_users) == 2)
1975 xfrm_state_delete(t);
1976 atomic_dec(&t->tunnel_users);
1977 xfrm_state_put(t);
1978 x->tunnel = NULL;
1981 EXPORT_SYMBOL(xfrm_state_delete_tunnel);
1983 int xfrm_state_mtu(struct xfrm_state *x, int mtu)
1985 int res;
1987 spin_lock_bh(&x->lock);
1988 if (x->km.state == XFRM_STATE_VALID &&
1989 x->type && x->type->get_mtu)
1990 res = x->type->get_mtu(x, mtu);
1991 else
1992 res = mtu - x->props.header_len;
1993 spin_unlock_bh(&x->lock);
1994 return res;
1997 int xfrm_init_state(struct xfrm_state *x)
1999 struct xfrm_state_afinfo *afinfo;
2000 struct xfrm_mode *inner_mode;
2001 int family = x->props.family;
2002 int err;
2004 err = -EAFNOSUPPORT;
2005 afinfo = xfrm_state_get_afinfo(family);
2006 if (!afinfo)
2007 goto error;
2009 err = 0;
2010 if (afinfo->init_flags)
2011 err = afinfo->init_flags(x);
2013 xfrm_state_put_afinfo(afinfo);
2015 if (err)
2016 goto error;
2018 err = -EPROTONOSUPPORT;
2020 if (x->sel.family != AF_UNSPEC) {
2021 inner_mode = xfrm_get_mode(x->props.mode, x->sel.family);
2022 if (inner_mode == NULL)
2023 goto error;
2025 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL) &&
2026 family != x->sel.family) {
2027 xfrm_put_mode(inner_mode);
2028 goto error;
2031 x->inner_mode = inner_mode;
2032 } else {
2033 struct xfrm_mode *inner_mode_iaf;
2034 int iafamily = AF_INET;
2036 inner_mode = xfrm_get_mode(x->props.mode, x->props.family);
2037 if (inner_mode == NULL)
2038 goto error;
2040 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL)) {
2041 xfrm_put_mode(inner_mode);
2042 goto error;
2044 x->inner_mode = inner_mode;
2046 if (x->props.family == AF_INET)
2047 iafamily = AF_INET6;
2049 inner_mode_iaf = xfrm_get_mode(x->props.mode, iafamily);
2050 if (inner_mode_iaf) {
2051 if (inner_mode_iaf->flags & XFRM_MODE_FLAG_TUNNEL)
2052 x->inner_mode_iaf = inner_mode_iaf;
2053 else
2054 xfrm_put_mode(inner_mode_iaf);
2058 x->type = xfrm_get_type(x->id.proto, family);
2059 if (x->type == NULL)
2060 goto error;
2062 err = x->type->init_state(x);
2063 if (err)
2064 goto error;
2066 x->outer_mode = xfrm_get_mode(x->props.mode, family);
2067 if (x->outer_mode == NULL)
2068 goto error;
2070 x->km.state = XFRM_STATE_VALID;
2072 error:
2073 return err;
2076 EXPORT_SYMBOL(xfrm_init_state);
2078 int __net_init xfrm_state_init(struct net *net)
2080 unsigned int sz;
2082 INIT_LIST_HEAD(&net->xfrm.state_all);
2084 sz = sizeof(struct hlist_head) * 8;
2086 net->xfrm.state_bydst = xfrm_hash_alloc(sz);
2087 if (!net->xfrm.state_bydst)
2088 goto out_bydst;
2089 net->xfrm.state_bysrc = xfrm_hash_alloc(sz);
2090 if (!net->xfrm.state_bysrc)
2091 goto out_bysrc;
2092 net->xfrm.state_byspi = xfrm_hash_alloc(sz);
2093 if (!net->xfrm.state_byspi)
2094 goto out_byspi;
2095 net->xfrm.state_hmask = ((sz / sizeof(struct hlist_head)) - 1);
2097 net->xfrm.state_num = 0;
2098 INIT_WORK(&net->xfrm.state_hash_work, xfrm_hash_resize);
2099 INIT_HLIST_HEAD(&net->xfrm.state_gc_list);
2100 INIT_WORK(&net->xfrm.state_gc_work, xfrm_state_gc_task);
2101 init_waitqueue_head(&net->xfrm.km_waitq);
2102 return 0;
2104 out_byspi:
2105 xfrm_hash_free(net->xfrm.state_bysrc, sz);
2106 out_bysrc:
2107 xfrm_hash_free(net->xfrm.state_bydst, sz);
2108 out_bydst:
2109 return -ENOMEM;
2112 void xfrm_state_fini(struct net *net)
2114 struct xfrm_audit audit_info;
2115 unsigned int sz;
2117 flush_work(&net->xfrm.state_hash_work);
2118 audit_info.loginuid = -1;
2119 audit_info.sessionid = -1;
2120 audit_info.secid = 0;
2121 xfrm_state_flush(net, IPSEC_PROTO_ANY, &audit_info);
2122 flush_work(&net->xfrm.state_gc_work);
2124 WARN_ON(!list_empty(&net->xfrm.state_all));
2126 sz = (net->xfrm.state_hmask + 1) * sizeof(struct hlist_head);
2127 WARN_ON(!hlist_empty(net->xfrm.state_byspi));
2128 xfrm_hash_free(net->xfrm.state_byspi, sz);
2129 WARN_ON(!hlist_empty(net->xfrm.state_bysrc));
2130 xfrm_hash_free(net->xfrm.state_bysrc, sz);
2131 WARN_ON(!hlist_empty(net->xfrm.state_bydst));
2132 xfrm_hash_free(net->xfrm.state_bydst, sz);
2135 #ifdef CONFIG_AUDITSYSCALL
2136 static void xfrm_audit_helper_sainfo(struct xfrm_state *x,
2137 struct audit_buffer *audit_buf)
2139 struct xfrm_sec_ctx *ctx = x->security;
2140 u32 spi = ntohl(x->id.spi);
2142 if (ctx)
2143 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
2144 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
2146 switch(x->props.family) {
2147 case AF_INET:
2148 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2149 &x->props.saddr.a4, &x->id.daddr.a4);
2150 break;
2151 case AF_INET6:
2152 audit_log_format(audit_buf, " src=%pI6 dst=%pI6",
2153 x->props.saddr.a6, x->id.daddr.a6);
2154 break;
2157 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
2160 static void xfrm_audit_helper_pktinfo(struct sk_buff *skb, u16 family,
2161 struct audit_buffer *audit_buf)
2163 struct iphdr *iph4;
2164 struct ipv6hdr *iph6;
2166 switch (family) {
2167 case AF_INET:
2168 iph4 = ip_hdr(skb);
2169 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2170 &iph4->saddr, &iph4->daddr);
2171 break;
2172 case AF_INET6:
2173 iph6 = ipv6_hdr(skb);
2174 audit_log_format(audit_buf,
2175 " src=%pI6 dst=%pI6 flowlbl=0x%x%02x%02x",
2176 &iph6->saddr,&iph6->daddr,
2177 iph6->flow_lbl[0] & 0x0f,
2178 iph6->flow_lbl[1],
2179 iph6->flow_lbl[2]);
2180 break;
2184 void xfrm_audit_state_add(struct xfrm_state *x, int result,
2185 uid_t auid, u32 sessionid, u32 secid)
2187 struct audit_buffer *audit_buf;
2189 audit_buf = xfrm_audit_start("SAD-add");
2190 if (audit_buf == NULL)
2191 return;
2192 xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf);
2193 xfrm_audit_helper_sainfo(x, audit_buf);
2194 audit_log_format(audit_buf, " res=%u", result);
2195 audit_log_end(audit_buf);
2197 EXPORT_SYMBOL_GPL(xfrm_audit_state_add);
2199 void xfrm_audit_state_delete(struct xfrm_state *x, int result,
2200 uid_t auid, u32 sessionid, u32 secid)
2202 struct audit_buffer *audit_buf;
2204 audit_buf = xfrm_audit_start("SAD-delete");
2205 if (audit_buf == NULL)
2206 return;
2207 xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf);
2208 xfrm_audit_helper_sainfo(x, audit_buf);
2209 audit_log_format(audit_buf, " res=%u", result);
2210 audit_log_end(audit_buf);
2212 EXPORT_SYMBOL_GPL(xfrm_audit_state_delete);
2214 void xfrm_audit_state_replay_overflow(struct xfrm_state *x,
2215 struct sk_buff *skb)
2217 struct audit_buffer *audit_buf;
2218 u32 spi;
2220 audit_buf = xfrm_audit_start("SA-replay-overflow");
2221 if (audit_buf == NULL)
2222 return;
2223 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2224 /* don't record the sequence number because it's inherent in this kind
2225 * of audit message */
2226 spi = ntohl(x->id.spi);
2227 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
2228 audit_log_end(audit_buf);
2230 EXPORT_SYMBOL_GPL(xfrm_audit_state_replay_overflow);
2232 static void xfrm_audit_state_replay(struct xfrm_state *x,
2233 struct sk_buff *skb, __be32 net_seq)
2235 struct audit_buffer *audit_buf;
2236 u32 spi;
2238 audit_buf = xfrm_audit_start("SA-replayed-pkt");
2239 if (audit_buf == NULL)
2240 return;
2241 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2242 spi = ntohl(x->id.spi);
2243 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2244 spi, spi, ntohl(net_seq));
2245 audit_log_end(audit_buf);
2248 void xfrm_audit_state_notfound_simple(struct sk_buff *skb, u16 family)
2250 struct audit_buffer *audit_buf;
2252 audit_buf = xfrm_audit_start("SA-notfound");
2253 if (audit_buf == NULL)
2254 return;
2255 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2256 audit_log_end(audit_buf);
2258 EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound_simple);
2260 void xfrm_audit_state_notfound(struct sk_buff *skb, u16 family,
2261 __be32 net_spi, __be32 net_seq)
2263 struct audit_buffer *audit_buf;
2264 u32 spi;
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 spi = ntohl(net_spi);
2271 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2272 spi, spi, ntohl(net_seq));
2273 audit_log_end(audit_buf);
2275 EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound);
2277 void xfrm_audit_state_icvfail(struct xfrm_state *x,
2278 struct sk_buff *skb, u8 proto)
2280 struct audit_buffer *audit_buf;
2281 __be32 net_spi;
2282 __be32 net_seq;
2284 audit_buf = xfrm_audit_start("SA-icv-failure");
2285 if (audit_buf == NULL)
2286 return;
2287 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2288 if (xfrm_parse_spi(skb, proto, &net_spi, &net_seq) == 0) {
2289 u32 spi = ntohl(net_spi);
2290 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2291 spi, spi, ntohl(net_seq));
2293 audit_log_end(audit_buf);
2295 EXPORT_SYMBOL_GPL(xfrm_audit_state_icvfail);
2296 #endif /* CONFIG_AUDITSYSCALL */