Committer: Michael Beasley <mike@snafu.setup>
[mikesnafu-overlay.git] / net / xfrm / xfrm_state.c
blob413885c7bd25bf3bb1333b9bdeba05db76b12655
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>
25 #include "xfrm_hash.h"
27 struct sock *xfrm_nl;
28 EXPORT_SYMBOL(xfrm_nl);
30 u32 sysctl_xfrm_aevent_etime __read_mostly = XFRM_AE_ETIME;
31 EXPORT_SYMBOL(sysctl_xfrm_aevent_etime);
33 u32 sysctl_xfrm_aevent_rseqth __read_mostly = XFRM_AE_SEQT_SIZE;
34 EXPORT_SYMBOL(sysctl_xfrm_aevent_rseqth);
36 u32 sysctl_xfrm_acq_expires __read_mostly = 30;
38 /* Each xfrm_state may be linked to two tables:
40 1. Hash table by (spi,daddr,ah/esp) to find SA by SPI. (input,ctl)
41 2. Hash table by (daddr,family,reqid) to find what SAs exist for given
42 destination/tunnel endpoint. (output)
45 static DEFINE_SPINLOCK(xfrm_state_lock);
47 /* Hash table to find appropriate SA towards given target (endpoint
48 * of tunnel or destination of transport mode) allowed by selector.
50 * Main use is finding SA after policy selected tunnel or transport mode.
51 * Also, it can be used by ah/esp icmp error handler to find offending SA.
53 static struct hlist_head *xfrm_state_bydst __read_mostly;
54 static struct hlist_head *xfrm_state_bysrc __read_mostly;
55 static struct hlist_head *xfrm_state_byspi __read_mostly;
56 static unsigned int xfrm_state_hmask __read_mostly;
57 static unsigned int xfrm_state_hashmax __read_mostly = 1 * 1024 * 1024;
58 static unsigned int xfrm_state_num;
59 static unsigned int xfrm_state_genid;
61 static struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family);
62 static void xfrm_state_put_afinfo(struct xfrm_state_afinfo *afinfo);
64 #ifdef CONFIG_AUDITSYSCALL
65 static void xfrm_audit_state_replay(struct xfrm_state *x,
66 struct sk_buff *skb, __be32 net_seq);
67 #else
68 #define xfrm_audit_state_replay(x, s, sq) do { ; } while (0)
69 #endif /* CONFIG_AUDITSYSCALL */
71 static inline unsigned int xfrm_dst_hash(xfrm_address_t *daddr,
72 xfrm_address_t *saddr,
73 u32 reqid,
74 unsigned short family)
76 return __xfrm_dst_hash(daddr, saddr, reqid, family, xfrm_state_hmask);
79 static inline unsigned int xfrm_src_hash(xfrm_address_t *daddr,
80 xfrm_address_t *saddr,
81 unsigned short family)
83 return __xfrm_src_hash(daddr, saddr, family, xfrm_state_hmask);
86 static inline unsigned int
87 xfrm_spi_hash(xfrm_address_t *daddr, __be32 spi, u8 proto, unsigned short family)
89 return __xfrm_spi_hash(daddr, spi, proto, family, xfrm_state_hmask);
92 static void xfrm_hash_transfer(struct hlist_head *list,
93 struct hlist_head *ndsttable,
94 struct hlist_head *nsrctable,
95 struct hlist_head *nspitable,
96 unsigned int nhashmask)
98 struct hlist_node *entry, *tmp;
99 struct xfrm_state *x;
101 hlist_for_each_entry_safe(x, entry, tmp, list, bydst) {
102 unsigned int h;
104 h = __xfrm_dst_hash(&x->id.daddr, &x->props.saddr,
105 x->props.reqid, x->props.family,
106 nhashmask);
107 hlist_add_head(&x->bydst, ndsttable+h);
109 h = __xfrm_src_hash(&x->id.daddr, &x->props.saddr,
110 x->props.family,
111 nhashmask);
112 hlist_add_head(&x->bysrc, nsrctable+h);
114 if (x->id.spi) {
115 h = __xfrm_spi_hash(&x->id.daddr, x->id.spi,
116 x->id.proto, x->props.family,
117 nhashmask);
118 hlist_add_head(&x->byspi, nspitable+h);
123 static unsigned long xfrm_hash_new_size(void)
125 return ((xfrm_state_hmask + 1) << 1) *
126 sizeof(struct hlist_head);
129 static DEFINE_MUTEX(hash_resize_mutex);
131 static void xfrm_hash_resize(struct work_struct *__unused)
133 struct hlist_head *ndst, *nsrc, *nspi, *odst, *osrc, *ospi;
134 unsigned long nsize, osize;
135 unsigned int nhashmask, ohashmask;
136 int i;
138 mutex_lock(&hash_resize_mutex);
140 nsize = xfrm_hash_new_size();
141 ndst = xfrm_hash_alloc(nsize);
142 if (!ndst)
143 goto out_unlock;
144 nsrc = xfrm_hash_alloc(nsize);
145 if (!nsrc) {
146 xfrm_hash_free(ndst, nsize);
147 goto out_unlock;
149 nspi = xfrm_hash_alloc(nsize);
150 if (!nspi) {
151 xfrm_hash_free(ndst, nsize);
152 xfrm_hash_free(nsrc, nsize);
153 goto out_unlock;
156 spin_lock_bh(&xfrm_state_lock);
158 nhashmask = (nsize / sizeof(struct hlist_head)) - 1U;
159 for (i = xfrm_state_hmask; i >= 0; i--)
160 xfrm_hash_transfer(xfrm_state_bydst+i, ndst, nsrc, nspi,
161 nhashmask);
163 odst = xfrm_state_bydst;
164 osrc = xfrm_state_bysrc;
165 ospi = xfrm_state_byspi;
166 ohashmask = xfrm_state_hmask;
168 xfrm_state_bydst = ndst;
169 xfrm_state_bysrc = nsrc;
170 xfrm_state_byspi = nspi;
171 xfrm_state_hmask = nhashmask;
173 spin_unlock_bh(&xfrm_state_lock);
175 osize = (ohashmask + 1) * sizeof(struct hlist_head);
176 xfrm_hash_free(odst, osize);
177 xfrm_hash_free(osrc, osize);
178 xfrm_hash_free(ospi, osize);
180 out_unlock:
181 mutex_unlock(&hash_resize_mutex);
184 static DECLARE_WORK(xfrm_hash_work, xfrm_hash_resize);
186 DECLARE_WAIT_QUEUE_HEAD(km_waitq);
187 EXPORT_SYMBOL(km_waitq);
189 static DEFINE_RWLOCK(xfrm_state_afinfo_lock);
190 static struct xfrm_state_afinfo *xfrm_state_afinfo[NPROTO];
192 static struct work_struct xfrm_state_gc_work;
193 static HLIST_HEAD(xfrm_state_gc_list);
194 static DEFINE_SPINLOCK(xfrm_state_gc_lock);
196 int __xfrm_state_delete(struct xfrm_state *x);
198 int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol);
199 void km_state_expired(struct xfrm_state *x, int hard, u32 pid);
201 static struct xfrm_state_afinfo *xfrm_state_lock_afinfo(unsigned int family)
203 struct xfrm_state_afinfo *afinfo;
204 if (unlikely(family >= NPROTO))
205 return NULL;
206 write_lock_bh(&xfrm_state_afinfo_lock);
207 afinfo = xfrm_state_afinfo[family];
208 if (unlikely(!afinfo))
209 write_unlock_bh(&xfrm_state_afinfo_lock);
210 return afinfo;
213 static void xfrm_state_unlock_afinfo(struct xfrm_state_afinfo *afinfo)
214 __releases(xfrm_state_afinfo_lock)
216 write_unlock_bh(&xfrm_state_afinfo_lock);
219 int xfrm_register_type(const struct xfrm_type *type, unsigned short family)
221 struct xfrm_state_afinfo *afinfo = xfrm_state_lock_afinfo(family);
222 const struct xfrm_type **typemap;
223 int err = 0;
225 if (unlikely(afinfo == NULL))
226 return -EAFNOSUPPORT;
227 typemap = afinfo->type_map;
229 if (likely(typemap[type->proto] == NULL))
230 typemap[type->proto] = type;
231 else
232 err = -EEXIST;
233 xfrm_state_unlock_afinfo(afinfo);
234 return err;
236 EXPORT_SYMBOL(xfrm_register_type);
238 int xfrm_unregister_type(const struct xfrm_type *type, unsigned short family)
240 struct xfrm_state_afinfo *afinfo = xfrm_state_lock_afinfo(family);
241 const struct xfrm_type **typemap;
242 int err = 0;
244 if (unlikely(afinfo == NULL))
245 return -EAFNOSUPPORT;
246 typemap = afinfo->type_map;
248 if (unlikely(typemap[type->proto] != type))
249 err = -ENOENT;
250 else
251 typemap[type->proto] = NULL;
252 xfrm_state_unlock_afinfo(afinfo);
253 return err;
255 EXPORT_SYMBOL(xfrm_unregister_type);
257 static const struct xfrm_type *xfrm_get_type(u8 proto, unsigned short family)
259 struct xfrm_state_afinfo *afinfo;
260 const struct xfrm_type **typemap;
261 const struct xfrm_type *type;
262 int modload_attempted = 0;
264 retry:
265 afinfo = xfrm_state_get_afinfo(family);
266 if (unlikely(afinfo == NULL))
267 return NULL;
268 typemap = afinfo->type_map;
270 type = typemap[proto];
271 if (unlikely(type && !try_module_get(type->owner)))
272 type = NULL;
273 if (!type && !modload_attempted) {
274 xfrm_state_put_afinfo(afinfo);
275 request_module("xfrm-type-%d-%d", family, proto);
276 modload_attempted = 1;
277 goto retry;
280 xfrm_state_put_afinfo(afinfo);
281 return type;
284 static void xfrm_put_type(const struct xfrm_type *type)
286 module_put(type->owner);
289 int xfrm_register_mode(struct xfrm_mode *mode, int family)
291 struct xfrm_state_afinfo *afinfo;
292 struct xfrm_mode **modemap;
293 int err;
295 if (unlikely(mode->encap >= XFRM_MODE_MAX))
296 return -EINVAL;
298 afinfo = xfrm_state_lock_afinfo(family);
299 if (unlikely(afinfo == NULL))
300 return -EAFNOSUPPORT;
302 err = -EEXIST;
303 modemap = afinfo->mode_map;
304 if (modemap[mode->encap])
305 goto out;
307 err = -ENOENT;
308 if (!try_module_get(afinfo->owner))
309 goto out;
311 mode->afinfo = afinfo;
312 modemap[mode->encap] = mode;
313 err = 0;
315 out:
316 xfrm_state_unlock_afinfo(afinfo);
317 return err;
319 EXPORT_SYMBOL(xfrm_register_mode);
321 int xfrm_unregister_mode(struct xfrm_mode *mode, int family)
323 struct xfrm_state_afinfo *afinfo;
324 struct xfrm_mode **modemap;
325 int err;
327 if (unlikely(mode->encap >= XFRM_MODE_MAX))
328 return -EINVAL;
330 afinfo = xfrm_state_lock_afinfo(family);
331 if (unlikely(afinfo == NULL))
332 return -EAFNOSUPPORT;
334 err = -ENOENT;
335 modemap = afinfo->mode_map;
336 if (likely(modemap[mode->encap] == mode)) {
337 modemap[mode->encap] = NULL;
338 module_put(mode->afinfo->owner);
339 err = 0;
342 xfrm_state_unlock_afinfo(afinfo);
343 return err;
345 EXPORT_SYMBOL(xfrm_unregister_mode);
347 static struct xfrm_mode *xfrm_get_mode(unsigned int encap, int family)
349 struct xfrm_state_afinfo *afinfo;
350 struct xfrm_mode *mode;
351 int modload_attempted = 0;
353 if (unlikely(encap >= XFRM_MODE_MAX))
354 return NULL;
356 retry:
357 afinfo = xfrm_state_get_afinfo(family);
358 if (unlikely(afinfo == NULL))
359 return NULL;
361 mode = afinfo->mode_map[encap];
362 if (unlikely(mode && !try_module_get(mode->owner)))
363 mode = NULL;
364 if (!mode && !modload_attempted) {
365 xfrm_state_put_afinfo(afinfo);
366 request_module("xfrm-mode-%d-%d", family, encap);
367 modload_attempted = 1;
368 goto retry;
371 xfrm_state_put_afinfo(afinfo);
372 return mode;
375 static void xfrm_put_mode(struct xfrm_mode *mode)
377 module_put(mode->owner);
380 static void xfrm_state_gc_destroy(struct xfrm_state *x)
382 del_timer_sync(&x->timer);
383 del_timer_sync(&x->rtimer);
384 kfree(x->aalg);
385 kfree(x->ealg);
386 kfree(x->calg);
387 kfree(x->encap);
388 kfree(x->coaddr);
389 if (x->inner_mode)
390 xfrm_put_mode(x->inner_mode);
391 if (x->inner_mode_iaf)
392 xfrm_put_mode(x->inner_mode_iaf);
393 if (x->outer_mode)
394 xfrm_put_mode(x->outer_mode);
395 if (x->type) {
396 x->type->destructor(x);
397 xfrm_put_type(x->type);
399 security_xfrm_state_free(x);
400 kfree(x);
403 static void xfrm_state_gc_task(struct work_struct *data)
405 struct xfrm_state *x;
406 struct hlist_node *entry, *tmp;
407 struct hlist_head gc_list;
409 spin_lock_bh(&xfrm_state_gc_lock);
410 gc_list.first = xfrm_state_gc_list.first;
411 INIT_HLIST_HEAD(&xfrm_state_gc_list);
412 spin_unlock_bh(&xfrm_state_gc_lock);
414 hlist_for_each_entry_safe(x, entry, tmp, &gc_list, bydst)
415 xfrm_state_gc_destroy(x);
417 wake_up(&km_waitq);
420 static inline unsigned long make_jiffies(long secs)
422 if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
423 return MAX_SCHEDULE_TIMEOUT-1;
424 else
425 return secs*HZ;
428 static void xfrm_timer_handler(unsigned long data)
430 struct xfrm_state *x = (struct xfrm_state*)data;
431 unsigned long now = get_seconds();
432 long next = LONG_MAX;
433 int warn = 0;
434 int err = 0;
436 spin_lock(&x->lock);
437 if (x->km.state == XFRM_STATE_DEAD)
438 goto out;
439 if (x->km.state == XFRM_STATE_EXPIRED)
440 goto expired;
441 if (x->lft.hard_add_expires_seconds) {
442 long tmo = x->lft.hard_add_expires_seconds +
443 x->curlft.add_time - now;
444 if (tmo <= 0)
445 goto expired;
446 if (tmo < next)
447 next = tmo;
449 if (x->lft.hard_use_expires_seconds) {
450 long tmo = x->lft.hard_use_expires_seconds +
451 (x->curlft.use_time ? : now) - now;
452 if (tmo <= 0)
453 goto expired;
454 if (tmo < next)
455 next = tmo;
457 if (x->km.dying)
458 goto resched;
459 if (x->lft.soft_add_expires_seconds) {
460 long tmo = x->lft.soft_add_expires_seconds +
461 x->curlft.add_time - now;
462 if (tmo <= 0)
463 warn = 1;
464 else if (tmo < next)
465 next = tmo;
467 if (x->lft.soft_use_expires_seconds) {
468 long tmo = x->lft.soft_use_expires_seconds +
469 (x->curlft.use_time ? : now) - now;
470 if (tmo <= 0)
471 warn = 1;
472 else if (tmo < next)
473 next = tmo;
476 x->km.dying = warn;
477 if (warn)
478 km_state_expired(x, 0, 0);
479 resched:
480 if (next != LONG_MAX)
481 mod_timer(&x->timer, jiffies + make_jiffies(next));
483 goto out;
485 expired:
486 if (x->km.state == XFRM_STATE_ACQ && x->id.spi == 0) {
487 x->km.state = XFRM_STATE_EXPIRED;
488 wake_up(&km_waitq);
489 next = 2;
490 goto resched;
493 err = __xfrm_state_delete(x);
494 if (!err && x->id.spi)
495 km_state_expired(x, 1, 0);
497 xfrm_audit_state_delete(x, err ? 0 : 1,
498 audit_get_loginuid(current), 0);
500 out:
501 spin_unlock(&x->lock);
504 static void xfrm_replay_timer_handler(unsigned long data);
506 struct xfrm_state *xfrm_state_alloc(void)
508 struct xfrm_state *x;
510 x = kzalloc(sizeof(struct xfrm_state), GFP_ATOMIC);
512 if (x) {
513 atomic_set(&x->refcnt, 1);
514 atomic_set(&x->tunnel_users, 0);
515 INIT_HLIST_NODE(&x->bydst);
516 INIT_HLIST_NODE(&x->bysrc);
517 INIT_HLIST_NODE(&x->byspi);
518 setup_timer(&x->timer, xfrm_timer_handler, (unsigned long)x);
519 setup_timer(&x->rtimer, xfrm_replay_timer_handler,
520 (unsigned long)x);
521 x->curlft.add_time = get_seconds();
522 x->lft.soft_byte_limit = XFRM_INF;
523 x->lft.soft_packet_limit = XFRM_INF;
524 x->lft.hard_byte_limit = XFRM_INF;
525 x->lft.hard_packet_limit = XFRM_INF;
526 x->replay_maxage = 0;
527 x->replay_maxdiff = 0;
528 x->inner_mode = NULL;
529 x->inner_mode_iaf = NULL;
530 spin_lock_init(&x->lock);
532 return x;
534 EXPORT_SYMBOL(xfrm_state_alloc);
536 void __xfrm_state_destroy(struct xfrm_state *x)
538 BUG_TRAP(x->km.state == XFRM_STATE_DEAD);
540 spin_lock_bh(&xfrm_state_gc_lock);
541 hlist_add_head(&x->bydst, &xfrm_state_gc_list);
542 spin_unlock_bh(&xfrm_state_gc_lock);
543 schedule_work(&xfrm_state_gc_work);
545 EXPORT_SYMBOL(__xfrm_state_destroy);
547 int __xfrm_state_delete(struct xfrm_state *x)
549 int err = -ESRCH;
551 if (x->km.state != XFRM_STATE_DEAD) {
552 x->km.state = XFRM_STATE_DEAD;
553 spin_lock(&xfrm_state_lock);
554 hlist_del(&x->bydst);
555 hlist_del(&x->bysrc);
556 if (x->id.spi)
557 hlist_del(&x->byspi);
558 xfrm_state_num--;
559 spin_unlock(&xfrm_state_lock);
561 /* All xfrm_state objects are created by xfrm_state_alloc.
562 * The xfrm_state_alloc call gives a reference, and that
563 * is what we are dropping here.
565 xfrm_state_put(x);
566 err = 0;
569 return err;
571 EXPORT_SYMBOL(__xfrm_state_delete);
573 int xfrm_state_delete(struct xfrm_state *x)
575 int err;
577 spin_lock_bh(&x->lock);
578 err = __xfrm_state_delete(x);
579 spin_unlock_bh(&x->lock);
581 return err;
583 EXPORT_SYMBOL(xfrm_state_delete);
585 #ifdef CONFIG_SECURITY_NETWORK_XFRM
586 static inline int
587 xfrm_state_flush_secctx_check(u8 proto, struct xfrm_audit *audit_info)
589 int i, err = 0;
591 for (i = 0; i <= xfrm_state_hmask; i++) {
592 struct hlist_node *entry;
593 struct xfrm_state *x;
595 hlist_for_each_entry(x, entry, xfrm_state_bydst+i, bydst) {
596 if (xfrm_id_proto_match(x->id.proto, proto) &&
597 (err = security_xfrm_state_delete(x)) != 0) {
598 xfrm_audit_state_delete(x, 0,
599 audit_info->loginuid,
600 audit_info->secid);
601 return err;
606 return err;
608 #else
609 static inline int
610 xfrm_state_flush_secctx_check(u8 proto, struct xfrm_audit *audit_info)
612 return 0;
614 #endif
616 int xfrm_state_flush(u8 proto, struct xfrm_audit *audit_info)
618 int i, err = 0;
620 spin_lock_bh(&xfrm_state_lock);
621 err = xfrm_state_flush_secctx_check(proto, audit_info);
622 if (err)
623 goto out;
625 for (i = 0; i <= xfrm_state_hmask; i++) {
626 struct hlist_node *entry;
627 struct xfrm_state *x;
628 restart:
629 hlist_for_each_entry(x, entry, xfrm_state_bydst+i, bydst) {
630 if (!xfrm_state_kern(x) &&
631 xfrm_id_proto_match(x->id.proto, proto)) {
632 xfrm_state_hold(x);
633 spin_unlock_bh(&xfrm_state_lock);
635 err = xfrm_state_delete(x);
636 xfrm_audit_state_delete(x, err ? 0 : 1,
637 audit_info->loginuid,
638 audit_info->secid);
639 xfrm_state_put(x);
641 spin_lock_bh(&xfrm_state_lock);
642 goto restart;
646 err = 0;
648 out:
649 spin_unlock_bh(&xfrm_state_lock);
650 wake_up(&km_waitq);
651 return err;
653 EXPORT_SYMBOL(xfrm_state_flush);
655 void xfrm_sad_getinfo(struct xfrmk_sadinfo *si)
657 spin_lock_bh(&xfrm_state_lock);
658 si->sadcnt = xfrm_state_num;
659 si->sadhcnt = xfrm_state_hmask;
660 si->sadhmcnt = xfrm_state_hashmax;
661 spin_unlock_bh(&xfrm_state_lock);
663 EXPORT_SYMBOL(xfrm_sad_getinfo);
665 static int
666 xfrm_init_tempsel(struct xfrm_state *x, struct flowi *fl,
667 struct xfrm_tmpl *tmpl,
668 xfrm_address_t *daddr, xfrm_address_t *saddr,
669 unsigned short family)
671 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
672 if (!afinfo)
673 return -1;
674 afinfo->init_tempsel(x, fl, tmpl, daddr, saddr);
675 xfrm_state_put_afinfo(afinfo);
676 return 0;
679 static struct xfrm_state *__xfrm_state_lookup(xfrm_address_t *daddr, __be32 spi, u8 proto, unsigned short family)
681 unsigned int h = xfrm_spi_hash(daddr, spi, proto, family);
682 struct xfrm_state *x;
683 struct hlist_node *entry;
685 hlist_for_each_entry(x, entry, xfrm_state_byspi+h, byspi) {
686 if (x->props.family != family ||
687 x->id.spi != spi ||
688 x->id.proto != proto)
689 continue;
691 switch (family) {
692 case AF_INET:
693 if (x->id.daddr.a4 != daddr->a4)
694 continue;
695 break;
696 case AF_INET6:
697 if (!ipv6_addr_equal((struct in6_addr *)daddr,
698 (struct in6_addr *)
699 x->id.daddr.a6))
700 continue;
701 break;
704 xfrm_state_hold(x);
705 return x;
708 return NULL;
711 static struct xfrm_state *__xfrm_state_lookup_byaddr(xfrm_address_t *daddr, xfrm_address_t *saddr, u8 proto, unsigned short family)
713 unsigned int h = xfrm_src_hash(daddr, saddr, family);
714 struct xfrm_state *x;
715 struct hlist_node *entry;
717 hlist_for_each_entry(x, entry, xfrm_state_bysrc+h, bysrc) {
718 if (x->props.family != family ||
719 x->id.proto != proto)
720 continue;
722 switch (family) {
723 case AF_INET:
724 if (x->id.daddr.a4 != daddr->a4 ||
725 x->props.saddr.a4 != saddr->a4)
726 continue;
727 break;
728 case AF_INET6:
729 if (!ipv6_addr_equal((struct in6_addr *)daddr,
730 (struct in6_addr *)
731 x->id.daddr.a6) ||
732 !ipv6_addr_equal((struct in6_addr *)saddr,
733 (struct in6_addr *)
734 x->props.saddr.a6))
735 continue;
736 break;
739 xfrm_state_hold(x);
740 return x;
743 return NULL;
746 static inline struct xfrm_state *
747 __xfrm_state_locate(struct xfrm_state *x, int use_spi, int family)
749 if (use_spi)
750 return __xfrm_state_lookup(&x->id.daddr, x->id.spi,
751 x->id.proto, family);
752 else
753 return __xfrm_state_lookup_byaddr(&x->id.daddr,
754 &x->props.saddr,
755 x->id.proto, family);
758 static void xfrm_hash_grow_check(int have_hash_collision)
760 if (have_hash_collision &&
761 (xfrm_state_hmask + 1) < xfrm_state_hashmax &&
762 xfrm_state_num > xfrm_state_hmask)
763 schedule_work(&xfrm_hash_work);
766 struct xfrm_state *
767 xfrm_state_find(xfrm_address_t *daddr, xfrm_address_t *saddr,
768 struct flowi *fl, struct xfrm_tmpl *tmpl,
769 struct xfrm_policy *pol, int *err,
770 unsigned short family)
772 unsigned int h;
773 struct hlist_node *entry;
774 struct xfrm_state *x, *x0;
775 int acquire_in_progress = 0;
776 int error = 0;
777 struct xfrm_state *best = NULL;
779 spin_lock_bh(&xfrm_state_lock);
780 h = xfrm_dst_hash(daddr, saddr, tmpl->reqid, family);
781 hlist_for_each_entry(x, entry, xfrm_state_bydst+h, bydst) {
782 if (x->props.family == family &&
783 x->props.reqid == tmpl->reqid &&
784 !(x->props.flags & XFRM_STATE_WILDRECV) &&
785 xfrm_state_addr_check(x, daddr, saddr, family) &&
786 tmpl->mode == x->props.mode &&
787 tmpl->id.proto == x->id.proto &&
788 (tmpl->id.spi == x->id.spi || !tmpl->id.spi)) {
789 /* Resolution logic:
790 1. There is a valid state with matching selector.
791 Done.
792 2. Valid state with inappropriate selector. Skip.
794 Entering area of "sysdeps".
796 3. If state is not valid, selector is temporary,
797 it selects only session which triggered
798 previous resolution. Key manager will do
799 something to install a state with proper
800 selector.
802 if (x->km.state == XFRM_STATE_VALID) {
803 if ((x->sel.family && !xfrm_selector_match(&x->sel, fl, x->sel.family)) ||
804 !security_xfrm_state_pol_flow_match(x, pol, fl))
805 continue;
806 if (!best ||
807 best->km.dying > x->km.dying ||
808 (best->km.dying == x->km.dying &&
809 best->curlft.add_time < x->curlft.add_time))
810 best = x;
811 } else if (x->km.state == XFRM_STATE_ACQ) {
812 acquire_in_progress = 1;
813 } else if (x->km.state == XFRM_STATE_ERROR ||
814 x->km.state == XFRM_STATE_EXPIRED) {
815 if (xfrm_selector_match(&x->sel, fl, x->sel.family) &&
816 security_xfrm_state_pol_flow_match(x, pol, fl))
817 error = -ESRCH;
822 x = best;
823 if (!x && !error && !acquire_in_progress) {
824 if (tmpl->id.spi &&
825 (x0 = __xfrm_state_lookup(daddr, tmpl->id.spi,
826 tmpl->id.proto, family)) != NULL) {
827 xfrm_state_put(x0);
828 error = -EEXIST;
829 goto out;
831 x = xfrm_state_alloc();
832 if (x == NULL) {
833 error = -ENOMEM;
834 goto out;
836 /* Initialize temporary selector matching only
837 * to current session. */
838 xfrm_init_tempsel(x, fl, tmpl, daddr, saddr, family);
840 error = security_xfrm_state_alloc_acquire(x, pol->security, fl->secid);
841 if (error) {
842 x->km.state = XFRM_STATE_DEAD;
843 xfrm_state_put(x);
844 x = NULL;
845 goto out;
848 if (km_query(x, tmpl, pol) == 0) {
849 x->km.state = XFRM_STATE_ACQ;
850 hlist_add_head(&x->bydst, xfrm_state_bydst+h);
851 h = xfrm_src_hash(daddr, saddr, family);
852 hlist_add_head(&x->bysrc, xfrm_state_bysrc+h);
853 if (x->id.spi) {
854 h = xfrm_spi_hash(&x->id.daddr, x->id.spi, x->id.proto, family);
855 hlist_add_head(&x->byspi, xfrm_state_byspi+h);
857 x->lft.hard_add_expires_seconds = sysctl_xfrm_acq_expires;
858 x->timer.expires = jiffies + sysctl_xfrm_acq_expires*HZ;
859 add_timer(&x->timer);
860 xfrm_state_num++;
861 xfrm_hash_grow_check(x->bydst.next != NULL);
862 } else {
863 x->km.state = XFRM_STATE_DEAD;
864 xfrm_state_put(x);
865 x = NULL;
866 error = -ESRCH;
869 out:
870 if (x)
871 xfrm_state_hold(x);
872 else
873 *err = acquire_in_progress ? -EAGAIN : error;
874 spin_unlock_bh(&xfrm_state_lock);
875 return x;
878 struct xfrm_state *
879 xfrm_stateonly_find(xfrm_address_t *daddr, xfrm_address_t *saddr,
880 unsigned short family, u8 mode, u8 proto, u32 reqid)
882 unsigned int h;
883 struct xfrm_state *rx = NULL, *x = NULL;
884 struct hlist_node *entry;
886 spin_lock(&xfrm_state_lock);
887 h = xfrm_dst_hash(daddr, saddr, reqid, family);
888 hlist_for_each_entry(x, entry, xfrm_state_bydst+h, bydst) {
889 if (x->props.family == family &&
890 x->props.reqid == reqid &&
891 !(x->props.flags & XFRM_STATE_WILDRECV) &&
892 xfrm_state_addr_check(x, daddr, saddr, family) &&
893 mode == x->props.mode &&
894 proto == x->id.proto &&
895 x->km.state == XFRM_STATE_VALID) {
896 rx = x;
897 break;
901 if (rx)
902 xfrm_state_hold(rx);
903 spin_unlock(&xfrm_state_lock);
906 return rx;
908 EXPORT_SYMBOL(xfrm_stateonly_find);
910 static void __xfrm_state_insert(struct xfrm_state *x)
912 unsigned int h;
914 x->genid = ++xfrm_state_genid;
916 h = xfrm_dst_hash(&x->id.daddr, &x->props.saddr,
917 x->props.reqid, x->props.family);
918 hlist_add_head(&x->bydst, xfrm_state_bydst+h);
920 h = xfrm_src_hash(&x->id.daddr, &x->props.saddr, x->props.family);
921 hlist_add_head(&x->bysrc, xfrm_state_bysrc+h);
923 if (x->id.spi) {
924 h = xfrm_spi_hash(&x->id.daddr, x->id.spi, x->id.proto,
925 x->props.family);
927 hlist_add_head(&x->byspi, xfrm_state_byspi+h);
930 mod_timer(&x->timer, jiffies + HZ);
931 if (x->replay_maxage)
932 mod_timer(&x->rtimer, jiffies + x->replay_maxage);
934 wake_up(&km_waitq);
936 xfrm_state_num++;
938 xfrm_hash_grow_check(x->bydst.next != NULL);
941 /* xfrm_state_lock is held */
942 static void __xfrm_state_bump_genids(struct xfrm_state *xnew)
944 unsigned short family = xnew->props.family;
945 u32 reqid = xnew->props.reqid;
946 struct xfrm_state *x;
947 struct hlist_node *entry;
948 unsigned int h;
950 h = xfrm_dst_hash(&xnew->id.daddr, &xnew->props.saddr, reqid, family);
951 hlist_for_each_entry(x, entry, xfrm_state_bydst+h, bydst) {
952 if (x->props.family == family &&
953 x->props.reqid == reqid &&
954 !xfrm_addr_cmp(&x->id.daddr, &xnew->id.daddr, family) &&
955 !xfrm_addr_cmp(&x->props.saddr, &xnew->props.saddr, family))
956 x->genid = xfrm_state_genid;
960 void xfrm_state_insert(struct xfrm_state *x)
962 spin_lock_bh(&xfrm_state_lock);
963 __xfrm_state_bump_genids(x);
964 __xfrm_state_insert(x);
965 spin_unlock_bh(&xfrm_state_lock);
967 EXPORT_SYMBOL(xfrm_state_insert);
969 /* xfrm_state_lock is held */
970 static struct xfrm_state *__find_acq_core(unsigned short family, u8 mode, u32 reqid, u8 proto, xfrm_address_t *daddr, xfrm_address_t *saddr, int create)
972 unsigned int h = xfrm_dst_hash(daddr, saddr, reqid, family);
973 struct hlist_node *entry;
974 struct xfrm_state *x;
976 hlist_for_each_entry(x, entry, xfrm_state_bydst+h, bydst) {
977 if (x->props.reqid != reqid ||
978 x->props.mode != mode ||
979 x->props.family != family ||
980 x->km.state != XFRM_STATE_ACQ ||
981 x->id.spi != 0 ||
982 x->id.proto != proto)
983 continue;
985 switch (family) {
986 case AF_INET:
987 if (x->id.daddr.a4 != daddr->a4 ||
988 x->props.saddr.a4 != saddr->a4)
989 continue;
990 break;
991 case AF_INET6:
992 if (!ipv6_addr_equal((struct in6_addr *)x->id.daddr.a6,
993 (struct in6_addr *)daddr) ||
994 !ipv6_addr_equal((struct in6_addr *)
995 x->props.saddr.a6,
996 (struct in6_addr *)saddr))
997 continue;
998 break;
1001 xfrm_state_hold(x);
1002 return x;
1005 if (!create)
1006 return NULL;
1008 x = xfrm_state_alloc();
1009 if (likely(x)) {
1010 switch (family) {
1011 case AF_INET:
1012 x->sel.daddr.a4 = daddr->a4;
1013 x->sel.saddr.a4 = saddr->a4;
1014 x->sel.prefixlen_d = 32;
1015 x->sel.prefixlen_s = 32;
1016 x->props.saddr.a4 = saddr->a4;
1017 x->id.daddr.a4 = daddr->a4;
1018 break;
1020 case AF_INET6:
1021 ipv6_addr_copy((struct in6_addr *)x->sel.daddr.a6,
1022 (struct in6_addr *)daddr);
1023 ipv6_addr_copy((struct in6_addr *)x->sel.saddr.a6,
1024 (struct in6_addr *)saddr);
1025 x->sel.prefixlen_d = 128;
1026 x->sel.prefixlen_s = 128;
1027 ipv6_addr_copy((struct in6_addr *)x->props.saddr.a6,
1028 (struct in6_addr *)saddr);
1029 ipv6_addr_copy((struct in6_addr *)x->id.daddr.a6,
1030 (struct in6_addr *)daddr);
1031 break;
1034 x->km.state = XFRM_STATE_ACQ;
1035 x->id.proto = proto;
1036 x->props.family = family;
1037 x->props.mode = mode;
1038 x->props.reqid = reqid;
1039 x->lft.hard_add_expires_seconds = sysctl_xfrm_acq_expires;
1040 xfrm_state_hold(x);
1041 x->timer.expires = jiffies + sysctl_xfrm_acq_expires*HZ;
1042 add_timer(&x->timer);
1043 hlist_add_head(&x->bydst, xfrm_state_bydst+h);
1044 h = xfrm_src_hash(daddr, saddr, family);
1045 hlist_add_head(&x->bysrc, xfrm_state_bysrc+h);
1047 xfrm_state_num++;
1049 xfrm_hash_grow_check(x->bydst.next != NULL);
1052 return x;
1055 static struct xfrm_state *__xfrm_find_acq_byseq(u32 seq);
1057 int xfrm_state_add(struct xfrm_state *x)
1059 struct xfrm_state *x1;
1060 int family;
1061 int err;
1062 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
1064 family = x->props.family;
1066 spin_lock_bh(&xfrm_state_lock);
1068 x1 = __xfrm_state_locate(x, use_spi, family);
1069 if (x1) {
1070 xfrm_state_put(x1);
1071 x1 = NULL;
1072 err = -EEXIST;
1073 goto out;
1076 if (use_spi && x->km.seq) {
1077 x1 = __xfrm_find_acq_byseq(x->km.seq);
1078 if (x1 && ((x1->id.proto != x->id.proto) ||
1079 xfrm_addr_cmp(&x1->id.daddr, &x->id.daddr, family))) {
1080 xfrm_state_put(x1);
1081 x1 = NULL;
1085 if (use_spi && !x1)
1086 x1 = __find_acq_core(family, x->props.mode, x->props.reqid,
1087 x->id.proto,
1088 &x->id.daddr, &x->props.saddr, 0);
1090 __xfrm_state_bump_genids(x);
1091 __xfrm_state_insert(x);
1092 err = 0;
1094 out:
1095 spin_unlock_bh(&xfrm_state_lock);
1097 if (x1) {
1098 xfrm_state_delete(x1);
1099 xfrm_state_put(x1);
1102 return err;
1104 EXPORT_SYMBOL(xfrm_state_add);
1106 #ifdef CONFIG_XFRM_MIGRATE
1107 static struct xfrm_state *xfrm_state_clone(struct xfrm_state *orig, int *errp)
1109 int err = -ENOMEM;
1110 struct xfrm_state *x = xfrm_state_alloc();
1111 if (!x)
1112 goto error;
1114 memcpy(&x->id, &orig->id, sizeof(x->id));
1115 memcpy(&x->sel, &orig->sel, sizeof(x->sel));
1116 memcpy(&x->lft, &orig->lft, sizeof(x->lft));
1117 x->props.mode = orig->props.mode;
1118 x->props.replay_window = orig->props.replay_window;
1119 x->props.reqid = orig->props.reqid;
1120 x->props.family = orig->props.family;
1121 x->props.saddr = orig->props.saddr;
1123 if (orig->aalg) {
1124 x->aalg = xfrm_algo_clone(orig->aalg);
1125 if (!x->aalg)
1126 goto error;
1128 x->props.aalgo = orig->props.aalgo;
1130 if (orig->ealg) {
1131 x->ealg = xfrm_algo_clone(orig->ealg);
1132 if (!x->ealg)
1133 goto error;
1135 x->props.ealgo = orig->props.ealgo;
1137 if (orig->calg) {
1138 x->calg = xfrm_algo_clone(orig->calg);
1139 if (!x->calg)
1140 goto error;
1142 x->props.calgo = orig->props.calgo;
1144 if (orig->encap) {
1145 x->encap = kmemdup(orig->encap, sizeof(*x->encap), GFP_KERNEL);
1146 if (!x->encap)
1147 goto error;
1150 if (orig->coaddr) {
1151 x->coaddr = kmemdup(orig->coaddr, sizeof(*x->coaddr),
1152 GFP_KERNEL);
1153 if (!x->coaddr)
1154 goto error;
1157 err = xfrm_init_state(x);
1158 if (err)
1159 goto error;
1161 x->props.flags = orig->props.flags;
1163 x->curlft.add_time = orig->curlft.add_time;
1164 x->km.state = orig->km.state;
1165 x->km.seq = orig->km.seq;
1167 return x;
1169 error:
1170 if (errp)
1171 *errp = err;
1172 if (x) {
1173 kfree(x->aalg);
1174 kfree(x->ealg);
1175 kfree(x->calg);
1176 kfree(x->encap);
1177 kfree(x->coaddr);
1179 kfree(x);
1180 return NULL;
1183 /* xfrm_state_lock is held */
1184 struct xfrm_state * xfrm_migrate_state_find(struct xfrm_migrate *m)
1186 unsigned int h;
1187 struct xfrm_state *x;
1188 struct hlist_node *entry;
1190 if (m->reqid) {
1191 h = xfrm_dst_hash(&m->old_daddr, &m->old_saddr,
1192 m->reqid, m->old_family);
1193 hlist_for_each_entry(x, entry, xfrm_state_bydst+h, bydst) {
1194 if (x->props.mode != m->mode ||
1195 x->id.proto != m->proto)
1196 continue;
1197 if (m->reqid && x->props.reqid != m->reqid)
1198 continue;
1199 if (xfrm_addr_cmp(&x->id.daddr, &m->old_daddr,
1200 m->old_family) ||
1201 xfrm_addr_cmp(&x->props.saddr, &m->old_saddr,
1202 m->old_family))
1203 continue;
1204 xfrm_state_hold(x);
1205 return x;
1207 } else {
1208 h = xfrm_src_hash(&m->old_daddr, &m->old_saddr,
1209 m->old_family);
1210 hlist_for_each_entry(x, entry, xfrm_state_bysrc+h, bysrc) {
1211 if (x->props.mode != m->mode ||
1212 x->id.proto != m->proto)
1213 continue;
1214 if (xfrm_addr_cmp(&x->id.daddr, &m->old_daddr,
1215 m->old_family) ||
1216 xfrm_addr_cmp(&x->props.saddr, &m->old_saddr,
1217 m->old_family))
1218 continue;
1219 xfrm_state_hold(x);
1220 return x;
1224 return NULL;
1226 EXPORT_SYMBOL(xfrm_migrate_state_find);
1228 struct xfrm_state * xfrm_state_migrate(struct xfrm_state *x,
1229 struct xfrm_migrate *m)
1231 struct xfrm_state *xc;
1232 int err;
1234 xc = xfrm_state_clone(x, &err);
1235 if (!xc)
1236 return NULL;
1238 memcpy(&xc->id.daddr, &m->new_daddr, sizeof(xc->id.daddr));
1239 memcpy(&xc->props.saddr, &m->new_saddr, sizeof(xc->props.saddr));
1241 /* add state */
1242 if (!xfrm_addr_cmp(&x->id.daddr, &m->new_daddr, m->new_family)) {
1243 /* a care is needed when the destination address of the
1244 state is to be updated as it is a part of triplet */
1245 xfrm_state_insert(xc);
1246 } else {
1247 if ((err = xfrm_state_add(xc)) < 0)
1248 goto error;
1251 return xc;
1252 error:
1253 kfree(xc);
1254 return NULL;
1256 EXPORT_SYMBOL(xfrm_state_migrate);
1257 #endif
1259 int xfrm_state_update(struct xfrm_state *x)
1261 struct xfrm_state *x1;
1262 int err;
1263 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
1265 spin_lock_bh(&xfrm_state_lock);
1266 x1 = __xfrm_state_locate(x, use_spi, x->props.family);
1268 err = -ESRCH;
1269 if (!x1)
1270 goto out;
1272 if (xfrm_state_kern(x1)) {
1273 xfrm_state_put(x1);
1274 err = -EEXIST;
1275 goto out;
1278 if (x1->km.state == XFRM_STATE_ACQ) {
1279 __xfrm_state_insert(x);
1280 x = NULL;
1282 err = 0;
1284 out:
1285 spin_unlock_bh(&xfrm_state_lock);
1287 if (err)
1288 return err;
1290 if (!x) {
1291 xfrm_state_delete(x1);
1292 xfrm_state_put(x1);
1293 return 0;
1296 err = -EINVAL;
1297 spin_lock_bh(&x1->lock);
1298 if (likely(x1->km.state == XFRM_STATE_VALID)) {
1299 if (x->encap && x1->encap)
1300 memcpy(x1->encap, x->encap, sizeof(*x1->encap));
1301 if (x->coaddr && x1->coaddr) {
1302 memcpy(x1->coaddr, x->coaddr, sizeof(*x1->coaddr));
1304 if (!use_spi && memcmp(&x1->sel, &x->sel, sizeof(x1->sel)))
1305 memcpy(&x1->sel, &x->sel, sizeof(x1->sel));
1306 memcpy(&x1->lft, &x->lft, sizeof(x1->lft));
1307 x1->km.dying = 0;
1309 mod_timer(&x1->timer, jiffies + HZ);
1310 if (x1->curlft.use_time)
1311 xfrm_state_check_expire(x1);
1313 err = 0;
1315 spin_unlock_bh(&x1->lock);
1317 xfrm_state_put(x1);
1319 return err;
1321 EXPORT_SYMBOL(xfrm_state_update);
1323 int xfrm_state_check_expire(struct xfrm_state *x)
1325 if (!x->curlft.use_time)
1326 x->curlft.use_time = get_seconds();
1328 if (x->km.state != XFRM_STATE_VALID)
1329 return -EINVAL;
1331 if (x->curlft.bytes >= x->lft.hard_byte_limit ||
1332 x->curlft.packets >= x->lft.hard_packet_limit) {
1333 x->km.state = XFRM_STATE_EXPIRED;
1334 mod_timer(&x->timer, jiffies);
1335 return -EINVAL;
1338 if (!x->km.dying &&
1339 (x->curlft.bytes >= x->lft.soft_byte_limit ||
1340 x->curlft.packets >= x->lft.soft_packet_limit)) {
1341 x->km.dying = 1;
1342 km_state_expired(x, 0, 0);
1344 return 0;
1346 EXPORT_SYMBOL(xfrm_state_check_expire);
1348 struct xfrm_state *
1349 xfrm_state_lookup(xfrm_address_t *daddr, __be32 spi, u8 proto,
1350 unsigned short family)
1352 struct xfrm_state *x;
1354 spin_lock_bh(&xfrm_state_lock);
1355 x = __xfrm_state_lookup(daddr, spi, proto, family);
1356 spin_unlock_bh(&xfrm_state_lock);
1357 return x;
1359 EXPORT_SYMBOL(xfrm_state_lookup);
1361 struct xfrm_state *
1362 xfrm_state_lookup_byaddr(xfrm_address_t *daddr, xfrm_address_t *saddr,
1363 u8 proto, unsigned short family)
1365 struct xfrm_state *x;
1367 spin_lock_bh(&xfrm_state_lock);
1368 x = __xfrm_state_lookup_byaddr(daddr, saddr, proto, family);
1369 spin_unlock_bh(&xfrm_state_lock);
1370 return x;
1372 EXPORT_SYMBOL(xfrm_state_lookup_byaddr);
1374 struct xfrm_state *
1375 xfrm_find_acq(u8 mode, u32 reqid, u8 proto,
1376 xfrm_address_t *daddr, xfrm_address_t *saddr,
1377 int create, unsigned short family)
1379 struct xfrm_state *x;
1381 spin_lock_bh(&xfrm_state_lock);
1382 x = __find_acq_core(family, mode, reqid, proto, daddr, saddr, create);
1383 spin_unlock_bh(&xfrm_state_lock);
1385 return x;
1387 EXPORT_SYMBOL(xfrm_find_acq);
1389 #ifdef CONFIG_XFRM_SUB_POLICY
1391 xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n,
1392 unsigned short family)
1394 int err = 0;
1395 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
1396 if (!afinfo)
1397 return -EAFNOSUPPORT;
1399 spin_lock_bh(&xfrm_state_lock);
1400 if (afinfo->tmpl_sort)
1401 err = afinfo->tmpl_sort(dst, src, n);
1402 spin_unlock_bh(&xfrm_state_lock);
1403 xfrm_state_put_afinfo(afinfo);
1404 return err;
1406 EXPORT_SYMBOL(xfrm_tmpl_sort);
1409 xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src, int n,
1410 unsigned short family)
1412 int err = 0;
1413 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
1414 if (!afinfo)
1415 return -EAFNOSUPPORT;
1417 spin_lock_bh(&xfrm_state_lock);
1418 if (afinfo->state_sort)
1419 err = afinfo->state_sort(dst, src, n);
1420 spin_unlock_bh(&xfrm_state_lock);
1421 xfrm_state_put_afinfo(afinfo);
1422 return err;
1424 EXPORT_SYMBOL(xfrm_state_sort);
1425 #endif
1427 /* Silly enough, but I'm lazy to build resolution list */
1429 static struct xfrm_state *__xfrm_find_acq_byseq(u32 seq)
1431 int i;
1433 for (i = 0; i <= xfrm_state_hmask; i++) {
1434 struct hlist_node *entry;
1435 struct xfrm_state *x;
1437 hlist_for_each_entry(x, entry, xfrm_state_bydst+i, bydst) {
1438 if (x->km.seq == seq &&
1439 x->km.state == XFRM_STATE_ACQ) {
1440 xfrm_state_hold(x);
1441 return x;
1445 return NULL;
1448 struct xfrm_state *xfrm_find_acq_byseq(u32 seq)
1450 struct xfrm_state *x;
1452 spin_lock_bh(&xfrm_state_lock);
1453 x = __xfrm_find_acq_byseq(seq);
1454 spin_unlock_bh(&xfrm_state_lock);
1455 return x;
1457 EXPORT_SYMBOL(xfrm_find_acq_byseq);
1459 u32 xfrm_get_acqseq(void)
1461 u32 res;
1462 static u32 acqseq;
1463 static DEFINE_SPINLOCK(acqseq_lock);
1465 spin_lock_bh(&acqseq_lock);
1466 res = (++acqseq ? : ++acqseq);
1467 spin_unlock_bh(&acqseq_lock);
1468 return res;
1470 EXPORT_SYMBOL(xfrm_get_acqseq);
1472 int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high)
1474 unsigned int h;
1475 struct xfrm_state *x0;
1476 int err = -ENOENT;
1477 __be32 minspi = htonl(low);
1478 __be32 maxspi = htonl(high);
1480 spin_lock_bh(&x->lock);
1481 if (x->km.state == XFRM_STATE_DEAD)
1482 goto unlock;
1484 err = 0;
1485 if (x->id.spi)
1486 goto unlock;
1488 err = -ENOENT;
1490 if (minspi == maxspi) {
1491 x0 = xfrm_state_lookup(&x->id.daddr, minspi, x->id.proto, x->props.family);
1492 if (x0) {
1493 xfrm_state_put(x0);
1494 goto unlock;
1496 x->id.spi = minspi;
1497 } else {
1498 u32 spi = 0;
1499 for (h=0; h<high-low+1; h++) {
1500 spi = low + net_random()%(high-low+1);
1501 x0 = xfrm_state_lookup(&x->id.daddr, htonl(spi), x->id.proto, x->props.family);
1502 if (x0 == NULL) {
1503 x->id.spi = htonl(spi);
1504 break;
1506 xfrm_state_put(x0);
1509 if (x->id.spi) {
1510 spin_lock_bh(&xfrm_state_lock);
1511 h = xfrm_spi_hash(&x->id.daddr, x->id.spi, x->id.proto, x->props.family);
1512 hlist_add_head(&x->byspi, xfrm_state_byspi+h);
1513 spin_unlock_bh(&xfrm_state_lock);
1515 err = 0;
1518 unlock:
1519 spin_unlock_bh(&x->lock);
1521 return err;
1523 EXPORT_SYMBOL(xfrm_alloc_spi);
1525 int xfrm_state_walk(u8 proto, int (*func)(struct xfrm_state *, int, void*),
1526 void *data)
1528 int i;
1529 struct xfrm_state *x, *last = NULL;
1530 struct hlist_node *entry;
1531 int count = 0;
1532 int err = 0;
1534 spin_lock_bh(&xfrm_state_lock);
1535 for (i = 0; i <= xfrm_state_hmask; i++) {
1536 hlist_for_each_entry(x, entry, xfrm_state_bydst+i, bydst) {
1537 if (!xfrm_id_proto_match(x->id.proto, proto))
1538 continue;
1539 if (last) {
1540 err = func(last, count, data);
1541 if (err)
1542 goto out;
1544 last = x;
1545 count++;
1548 if (count == 0) {
1549 err = -ENOENT;
1550 goto out;
1552 err = func(last, 0, data);
1553 out:
1554 spin_unlock_bh(&xfrm_state_lock);
1555 return err;
1557 EXPORT_SYMBOL(xfrm_state_walk);
1560 void xfrm_replay_notify(struct xfrm_state *x, int event)
1562 struct km_event c;
1563 /* we send notify messages in case
1564 * 1. we updated on of the sequence numbers, and the seqno difference
1565 * is at least x->replay_maxdiff, in this case we also update the
1566 * timeout of our timer function
1567 * 2. if x->replay_maxage has elapsed since last update,
1568 * and there were changes
1570 * The state structure must be locked!
1573 switch (event) {
1574 case XFRM_REPLAY_UPDATE:
1575 if (x->replay_maxdiff &&
1576 (x->replay.seq - x->preplay.seq < x->replay_maxdiff) &&
1577 (x->replay.oseq - x->preplay.oseq < x->replay_maxdiff)) {
1578 if (x->xflags & XFRM_TIME_DEFER)
1579 event = XFRM_REPLAY_TIMEOUT;
1580 else
1581 return;
1584 break;
1586 case XFRM_REPLAY_TIMEOUT:
1587 if ((x->replay.seq == x->preplay.seq) &&
1588 (x->replay.bitmap == x->preplay.bitmap) &&
1589 (x->replay.oseq == x->preplay.oseq)) {
1590 x->xflags |= XFRM_TIME_DEFER;
1591 return;
1594 break;
1597 memcpy(&x->preplay, &x->replay, sizeof(struct xfrm_replay_state));
1598 c.event = XFRM_MSG_NEWAE;
1599 c.data.aevent = event;
1600 km_state_notify(x, &c);
1602 if (x->replay_maxage &&
1603 !mod_timer(&x->rtimer, jiffies + x->replay_maxage))
1604 x->xflags &= ~XFRM_TIME_DEFER;
1607 static void xfrm_replay_timer_handler(unsigned long data)
1609 struct xfrm_state *x = (struct xfrm_state*)data;
1611 spin_lock(&x->lock);
1613 if (x->km.state == XFRM_STATE_VALID) {
1614 if (xfrm_aevent_is_on())
1615 xfrm_replay_notify(x, XFRM_REPLAY_TIMEOUT);
1616 else
1617 x->xflags |= XFRM_TIME_DEFER;
1620 spin_unlock(&x->lock);
1623 int xfrm_replay_check(struct xfrm_state *x,
1624 struct sk_buff *skb, __be32 net_seq)
1626 u32 diff;
1627 u32 seq = ntohl(net_seq);
1629 if (unlikely(seq == 0))
1630 goto err;
1632 if (likely(seq > x->replay.seq))
1633 return 0;
1635 diff = x->replay.seq - seq;
1636 if (diff >= min_t(unsigned int, x->props.replay_window,
1637 sizeof(x->replay.bitmap) * 8)) {
1638 x->stats.replay_window++;
1639 goto err;
1642 if (x->replay.bitmap & (1U << diff)) {
1643 x->stats.replay++;
1644 goto err;
1646 return 0;
1648 err:
1649 xfrm_audit_state_replay(x, skb, net_seq);
1650 return -EINVAL;
1653 void xfrm_replay_advance(struct xfrm_state *x, __be32 net_seq)
1655 u32 diff;
1656 u32 seq = ntohl(net_seq);
1658 if (seq > x->replay.seq) {
1659 diff = seq - x->replay.seq;
1660 if (diff < x->props.replay_window)
1661 x->replay.bitmap = ((x->replay.bitmap) << diff) | 1;
1662 else
1663 x->replay.bitmap = 1;
1664 x->replay.seq = seq;
1665 } else {
1666 diff = x->replay.seq - seq;
1667 x->replay.bitmap |= (1U << diff);
1670 if (xfrm_aevent_is_on())
1671 xfrm_replay_notify(x, XFRM_REPLAY_UPDATE);
1674 static LIST_HEAD(xfrm_km_list);
1675 static DEFINE_RWLOCK(xfrm_km_lock);
1677 void km_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
1679 struct xfrm_mgr *km;
1681 read_lock(&xfrm_km_lock);
1682 list_for_each_entry(km, &xfrm_km_list, list)
1683 if (km->notify_policy)
1684 km->notify_policy(xp, dir, c);
1685 read_unlock(&xfrm_km_lock);
1688 void km_state_notify(struct xfrm_state *x, struct km_event *c)
1690 struct xfrm_mgr *km;
1691 read_lock(&xfrm_km_lock);
1692 list_for_each_entry(km, &xfrm_km_list, list)
1693 if (km->notify)
1694 km->notify(x, c);
1695 read_unlock(&xfrm_km_lock);
1698 EXPORT_SYMBOL(km_policy_notify);
1699 EXPORT_SYMBOL(km_state_notify);
1701 void km_state_expired(struct xfrm_state *x, int hard, u32 pid)
1703 struct km_event c;
1705 c.data.hard = hard;
1706 c.pid = pid;
1707 c.event = XFRM_MSG_EXPIRE;
1708 km_state_notify(x, &c);
1710 if (hard)
1711 wake_up(&km_waitq);
1714 EXPORT_SYMBOL(km_state_expired);
1716 * We send to all registered managers regardless of failure
1717 * We are happy with one success
1719 int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol)
1721 int err = -EINVAL, acqret;
1722 struct xfrm_mgr *km;
1724 read_lock(&xfrm_km_lock);
1725 list_for_each_entry(km, &xfrm_km_list, list) {
1726 acqret = km->acquire(x, t, pol, XFRM_POLICY_OUT);
1727 if (!acqret)
1728 err = acqret;
1730 read_unlock(&xfrm_km_lock);
1731 return err;
1733 EXPORT_SYMBOL(km_query);
1735 int km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
1737 int err = -EINVAL;
1738 struct xfrm_mgr *km;
1740 read_lock(&xfrm_km_lock);
1741 list_for_each_entry(km, &xfrm_km_list, list) {
1742 if (km->new_mapping)
1743 err = km->new_mapping(x, ipaddr, sport);
1744 if (!err)
1745 break;
1747 read_unlock(&xfrm_km_lock);
1748 return err;
1750 EXPORT_SYMBOL(km_new_mapping);
1752 void km_policy_expired(struct xfrm_policy *pol, int dir, int hard, u32 pid)
1754 struct km_event c;
1756 c.data.hard = hard;
1757 c.pid = pid;
1758 c.event = XFRM_MSG_POLEXPIRE;
1759 km_policy_notify(pol, dir, &c);
1761 if (hard)
1762 wake_up(&km_waitq);
1764 EXPORT_SYMBOL(km_policy_expired);
1766 #ifdef CONFIG_XFRM_MIGRATE
1767 int km_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
1768 struct xfrm_migrate *m, int num_migrate)
1770 int err = -EINVAL;
1771 int ret;
1772 struct xfrm_mgr *km;
1774 read_lock(&xfrm_km_lock);
1775 list_for_each_entry(km, &xfrm_km_list, list) {
1776 if (km->migrate) {
1777 ret = km->migrate(sel, dir, type, m, num_migrate);
1778 if (!ret)
1779 err = ret;
1782 read_unlock(&xfrm_km_lock);
1783 return err;
1785 EXPORT_SYMBOL(km_migrate);
1786 #endif
1788 int km_report(u8 proto, struct xfrm_selector *sel, xfrm_address_t *addr)
1790 int err = -EINVAL;
1791 int ret;
1792 struct xfrm_mgr *km;
1794 read_lock(&xfrm_km_lock);
1795 list_for_each_entry(km, &xfrm_km_list, list) {
1796 if (km->report) {
1797 ret = km->report(proto, sel, addr);
1798 if (!ret)
1799 err = ret;
1802 read_unlock(&xfrm_km_lock);
1803 return err;
1805 EXPORT_SYMBOL(km_report);
1807 int xfrm_user_policy(struct sock *sk, int optname, u8 __user *optval, int optlen)
1809 int err;
1810 u8 *data;
1811 struct xfrm_mgr *km;
1812 struct xfrm_policy *pol = NULL;
1814 if (optlen <= 0 || optlen > PAGE_SIZE)
1815 return -EMSGSIZE;
1817 data = kmalloc(optlen, GFP_KERNEL);
1818 if (!data)
1819 return -ENOMEM;
1821 err = -EFAULT;
1822 if (copy_from_user(data, optval, optlen))
1823 goto out;
1825 err = -EINVAL;
1826 read_lock(&xfrm_km_lock);
1827 list_for_each_entry(km, &xfrm_km_list, list) {
1828 pol = km->compile_policy(sk, optname, data,
1829 optlen, &err);
1830 if (err >= 0)
1831 break;
1833 read_unlock(&xfrm_km_lock);
1835 if (err >= 0) {
1836 xfrm_sk_policy_insert(sk, err, pol);
1837 xfrm_pol_put(pol);
1838 err = 0;
1841 out:
1842 kfree(data);
1843 return err;
1845 EXPORT_SYMBOL(xfrm_user_policy);
1847 int xfrm_register_km(struct xfrm_mgr *km)
1849 write_lock_bh(&xfrm_km_lock);
1850 list_add_tail(&km->list, &xfrm_km_list);
1851 write_unlock_bh(&xfrm_km_lock);
1852 return 0;
1854 EXPORT_SYMBOL(xfrm_register_km);
1856 int xfrm_unregister_km(struct xfrm_mgr *km)
1858 write_lock_bh(&xfrm_km_lock);
1859 list_del(&km->list);
1860 write_unlock_bh(&xfrm_km_lock);
1861 return 0;
1863 EXPORT_SYMBOL(xfrm_unregister_km);
1865 int xfrm_state_register_afinfo(struct xfrm_state_afinfo *afinfo)
1867 int err = 0;
1868 if (unlikely(afinfo == NULL))
1869 return -EINVAL;
1870 if (unlikely(afinfo->family >= NPROTO))
1871 return -EAFNOSUPPORT;
1872 write_lock_bh(&xfrm_state_afinfo_lock);
1873 if (unlikely(xfrm_state_afinfo[afinfo->family] != NULL))
1874 err = -ENOBUFS;
1875 else
1876 xfrm_state_afinfo[afinfo->family] = afinfo;
1877 write_unlock_bh(&xfrm_state_afinfo_lock);
1878 return err;
1880 EXPORT_SYMBOL(xfrm_state_register_afinfo);
1882 int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo)
1884 int err = 0;
1885 if (unlikely(afinfo == NULL))
1886 return -EINVAL;
1887 if (unlikely(afinfo->family >= NPROTO))
1888 return -EAFNOSUPPORT;
1889 write_lock_bh(&xfrm_state_afinfo_lock);
1890 if (likely(xfrm_state_afinfo[afinfo->family] != NULL)) {
1891 if (unlikely(xfrm_state_afinfo[afinfo->family] != afinfo))
1892 err = -EINVAL;
1893 else
1894 xfrm_state_afinfo[afinfo->family] = NULL;
1896 write_unlock_bh(&xfrm_state_afinfo_lock);
1897 return err;
1899 EXPORT_SYMBOL(xfrm_state_unregister_afinfo);
1901 static struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family)
1903 struct xfrm_state_afinfo *afinfo;
1904 if (unlikely(family >= NPROTO))
1905 return NULL;
1906 read_lock(&xfrm_state_afinfo_lock);
1907 afinfo = xfrm_state_afinfo[family];
1908 if (unlikely(!afinfo))
1909 read_unlock(&xfrm_state_afinfo_lock);
1910 return afinfo;
1913 static void xfrm_state_put_afinfo(struct xfrm_state_afinfo *afinfo)
1914 __releases(xfrm_state_afinfo_lock)
1916 read_unlock(&xfrm_state_afinfo_lock);
1919 /* Temporarily located here until net/xfrm/xfrm_tunnel.c is created */
1920 void xfrm_state_delete_tunnel(struct xfrm_state *x)
1922 if (x->tunnel) {
1923 struct xfrm_state *t = x->tunnel;
1925 if (atomic_read(&t->tunnel_users) == 2)
1926 xfrm_state_delete(t);
1927 atomic_dec(&t->tunnel_users);
1928 xfrm_state_put(t);
1929 x->tunnel = NULL;
1932 EXPORT_SYMBOL(xfrm_state_delete_tunnel);
1934 int xfrm_state_mtu(struct xfrm_state *x, int mtu)
1936 int res;
1938 spin_lock_bh(&x->lock);
1939 if (x->km.state == XFRM_STATE_VALID &&
1940 x->type && x->type->get_mtu)
1941 res = x->type->get_mtu(x, mtu);
1942 else
1943 res = mtu - x->props.header_len;
1944 spin_unlock_bh(&x->lock);
1945 return res;
1948 int xfrm_init_state(struct xfrm_state *x)
1950 struct xfrm_state_afinfo *afinfo;
1951 struct xfrm_mode *inner_mode;
1952 int family = x->props.family;
1953 int err;
1955 err = -EAFNOSUPPORT;
1956 afinfo = xfrm_state_get_afinfo(family);
1957 if (!afinfo)
1958 goto error;
1960 err = 0;
1961 if (afinfo->init_flags)
1962 err = afinfo->init_flags(x);
1964 xfrm_state_put_afinfo(afinfo);
1966 if (err)
1967 goto error;
1969 err = -EPROTONOSUPPORT;
1971 if (x->sel.family != AF_UNSPEC) {
1972 inner_mode = xfrm_get_mode(x->props.mode, x->sel.family);
1973 if (inner_mode == NULL)
1974 goto error;
1976 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL) &&
1977 family != x->sel.family) {
1978 xfrm_put_mode(inner_mode);
1979 goto error;
1982 x->inner_mode = inner_mode;
1983 } else {
1984 struct xfrm_mode *inner_mode_iaf;
1986 inner_mode = xfrm_get_mode(x->props.mode, AF_INET);
1987 if (inner_mode == NULL)
1988 goto error;
1990 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL)) {
1991 xfrm_put_mode(inner_mode);
1992 goto error;
1995 inner_mode_iaf = xfrm_get_mode(x->props.mode, AF_INET6);
1996 if (inner_mode_iaf == NULL)
1997 goto error;
1999 if (!(inner_mode_iaf->flags & XFRM_MODE_FLAG_TUNNEL)) {
2000 xfrm_put_mode(inner_mode_iaf);
2001 goto error;
2004 if (x->props.family == AF_INET) {
2005 x->inner_mode = inner_mode;
2006 x->inner_mode_iaf = inner_mode_iaf;
2007 } else {
2008 x->inner_mode = inner_mode_iaf;
2009 x->inner_mode_iaf = inner_mode;
2013 x->type = xfrm_get_type(x->id.proto, family);
2014 if (x->type == NULL)
2015 goto error;
2017 err = x->type->init_state(x);
2018 if (err)
2019 goto error;
2021 x->outer_mode = xfrm_get_mode(x->props.mode, family);
2022 if (x->outer_mode == NULL)
2023 goto error;
2025 x->km.state = XFRM_STATE_VALID;
2027 error:
2028 return err;
2031 EXPORT_SYMBOL(xfrm_init_state);
2033 void __init xfrm_state_init(void)
2035 unsigned int sz;
2037 sz = sizeof(struct hlist_head) * 8;
2039 xfrm_state_bydst = xfrm_hash_alloc(sz);
2040 xfrm_state_bysrc = xfrm_hash_alloc(sz);
2041 xfrm_state_byspi = xfrm_hash_alloc(sz);
2042 if (!xfrm_state_bydst || !xfrm_state_bysrc || !xfrm_state_byspi)
2043 panic("XFRM: Cannot allocate bydst/bysrc/byspi hashes.");
2044 xfrm_state_hmask = ((sz / sizeof(struct hlist_head)) - 1);
2046 INIT_WORK(&xfrm_state_gc_work, xfrm_state_gc_task);
2049 #ifdef CONFIG_AUDITSYSCALL
2050 static void xfrm_audit_helper_sainfo(struct xfrm_state *x,
2051 struct audit_buffer *audit_buf)
2053 struct xfrm_sec_ctx *ctx = x->security;
2054 u32 spi = ntohl(x->id.spi);
2056 if (ctx)
2057 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
2058 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
2060 switch(x->props.family) {
2061 case AF_INET:
2062 audit_log_format(audit_buf,
2063 " src=" NIPQUAD_FMT " dst=" NIPQUAD_FMT,
2064 NIPQUAD(x->props.saddr.a4),
2065 NIPQUAD(x->id.daddr.a4));
2066 break;
2067 case AF_INET6:
2068 audit_log_format(audit_buf,
2069 " src=" NIP6_FMT " dst=" NIP6_FMT,
2070 NIP6(*(struct in6_addr *)x->props.saddr.a6),
2071 NIP6(*(struct in6_addr *)x->id.daddr.a6));
2072 break;
2075 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
2078 static void xfrm_audit_helper_pktinfo(struct sk_buff *skb, u16 family,
2079 struct audit_buffer *audit_buf)
2081 struct iphdr *iph4;
2082 struct ipv6hdr *iph6;
2084 switch (family) {
2085 case AF_INET:
2086 iph4 = ip_hdr(skb);
2087 audit_log_format(audit_buf,
2088 " src=" NIPQUAD_FMT " dst=" NIPQUAD_FMT,
2089 NIPQUAD(iph4->saddr),
2090 NIPQUAD(iph4->daddr));
2091 break;
2092 case AF_INET6:
2093 iph6 = ipv6_hdr(skb);
2094 audit_log_format(audit_buf,
2095 " src=" NIP6_FMT " dst=" NIP6_FMT
2096 " flowlbl=0x%x%02x%02x",
2097 NIP6(iph6->saddr),
2098 NIP6(iph6->daddr),
2099 iph6->flow_lbl[0] & 0x0f,
2100 iph6->flow_lbl[1],
2101 iph6->flow_lbl[2]);
2102 break;
2106 void xfrm_audit_state_add(struct xfrm_state *x, int result,
2107 u32 auid, u32 secid)
2109 struct audit_buffer *audit_buf;
2111 audit_buf = xfrm_audit_start("SAD-add");
2112 if (audit_buf == NULL)
2113 return;
2114 xfrm_audit_helper_usrinfo(auid, secid, audit_buf);
2115 xfrm_audit_helper_sainfo(x, audit_buf);
2116 audit_log_format(audit_buf, " res=%u", result);
2117 audit_log_end(audit_buf);
2119 EXPORT_SYMBOL_GPL(xfrm_audit_state_add);
2121 void xfrm_audit_state_delete(struct xfrm_state *x, int result,
2122 u32 auid, u32 secid)
2124 struct audit_buffer *audit_buf;
2126 audit_buf = xfrm_audit_start("SAD-delete");
2127 if (audit_buf == NULL)
2128 return;
2129 xfrm_audit_helper_usrinfo(auid, secid, audit_buf);
2130 xfrm_audit_helper_sainfo(x, audit_buf);
2131 audit_log_format(audit_buf, " res=%u", result);
2132 audit_log_end(audit_buf);
2134 EXPORT_SYMBOL_GPL(xfrm_audit_state_delete);
2136 void xfrm_audit_state_replay_overflow(struct xfrm_state *x,
2137 struct sk_buff *skb)
2139 struct audit_buffer *audit_buf;
2140 u32 spi;
2142 audit_buf = xfrm_audit_start("SA-replay-overflow");
2143 if (audit_buf == NULL)
2144 return;
2145 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2146 /* don't record the sequence number because it's inherent in this kind
2147 * of audit message */
2148 spi = ntohl(x->id.spi);
2149 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
2150 audit_log_end(audit_buf);
2152 EXPORT_SYMBOL_GPL(xfrm_audit_state_replay_overflow);
2154 static void xfrm_audit_state_replay(struct xfrm_state *x,
2155 struct sk_buff *skb, __be32 net_seq)
2157 struct audit_buffer *audit_buf;
2158 u32 spi;
2160 audit_buf = xfrm_audit_start("SA-replayed-pkt");
2161 if (audit_buf == NULL)
2162 return;
2163 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2164 spi = ntohl(x->id.spi);
2165 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2166 spi, spi, ntohl(net_seq));
2167 audit_log_end(audit_buf);
2170 void xfrm_audit_state_notfound_simple(struct sk_buff *skb, u16 family)
2172 struct audit_buffer *audit_buf;
2174 audit_buf = xfrm_audit_start("SA-notfound");
2175 if (audit_buf == NULL)
2176 return;
2177 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2178 audit_log_end(audit_buf);
2180 EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound_simple);
2182 void xfrm_audit_state_notfound(struct sk_buff *skb, u16 family,
2183 __be32 net_spi, __be32 net_seq)
2185 struct audit_buffer *audit_buf;
2186 u32 spi;
2188 audit_buf = xfrm_audit_start("SA-notfound");
2189 if (audit_buf == NULL)
2190 return;
2191 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2192 spi = ntohl(net_spi);
2193 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2194 spi, spi, ntohl(net_seq));
2195 audit_log_end(audit_buf);
2197 EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound);
2199 void xfrm_audit_state_icvfail(struct xfrm_state *x,
2200 struct sk_buff *skb, u8 proto)
2202 struct audit_buffer *audit_buf;
2203 __be32 net_spi;
2204 __be32 net_seq;
2206 audit_buf = xfrm_audit_start("SA-icv-failure");
2207 if (audit_buf == NULL)
2208 return;
2209 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2210 if (xfrm_parse_spi(skb, proto, &net_spi, &net_seq) == 0) {
2211 u32 spi = ntohl(net_spi);
2212 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2213 spi, spi, ntohl(net_seq));
2215 audit_log_end(audit_buf);
2217 EXPORT_SYMBOL_GPL(xfrm_audit_state_icvfail);
2218 #endif /* CONFIG_AUDITSYSCALL */