FRV: Use generic show_interrupts()
[cris-mirror.git] / net / xfrm / xfrm_state.c
blobf83a3d1da81b90ec382918e4b5b193a150b99211
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/slab.h>
26 #include <linux/interrupt.h>
27 #include <linux/kernel.h>
29 #include "xfrm_hash.h"
31 /* Each xfrm_state may be linked to two tables:
33 1. Hash table by (spi,daddr,ah/esp) to find SA by SPI. (input,ctl)
34 2. Hash table by (daddr,family,reqid) to find what SAs exist for given
35 destination/tunnel endpoint. (output)
38 static DEFINE_SPINLOCK(xfrm_state_lock);
40 static unsigned int xfrm_state_hashmax __read_mostly = 1 * 1024 * 1024;
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 static inline unsigned int xfrm_dst_hash(struct net *net,
46 const xfrm_address_t *daddr,
47 const xfrm_address_t *saddr,
48 u32 reqid,
49 unsigned short family)
51 return __xfrm_dst_hash(daddr, saddr, reqid, family, net->xfrm.state_hmask);
54 static inline unsigned int xfrm_src_hash(struct net *net,
55 const xfrm_address_t *daddr,
56 const xfrm_address_t *saddr,
57 unsigned short family)
59 return __xfrm_src_hash(daddr, saddr, family, net->xfrm.state_hmask);
62 static inline unsigned int
63 xfrm_spi_hash(struct net *net, const xfrm_address_t *daddr,
64 __be32 spi, u8 proto, unsigned short family)
66 return __xfrm_spi_hash(daddr, spi, proto, family, net->xfrm.state_hmask);
69 static void xfrm_hash_transfer(struct hlist_head *list,
70 struct hlist_head *ndsttable,
71 struct hlist_head *nsrctable,
72 struct hlist_head *nspitable,
73 unsigned int nhashmask)
75 struct hlist_node *entry, *tmp;
76 struct xfrm_state *x;
78 hlist_for_each_entry_safe(x, entry, tmp, list, bydst) {
79 unsigned int h;
81 h = __xfrm_dst_hash(&x->id.daddr, &x->props.saddr,
82 x->props.reqid, x->props.family,
83 nhashmask);
84 hlist_add_head(&x->bydst, ndsttable+h);
86 h = __xfrm_src_hash(&x->id.daddr, &x->props.saddr,
87 x->props.family,
88 nhashmask);
89 hlist_add_head(&x->bysrc, nsrctable+h);
91 if (x->id.spi) {
92 h = __xfrm_spi_hash(&x->id.daddr, x->id.spi,
93 x->id.proto, x->props.family,
94 nhashmask);
95 hlist_add_head(&x->byspi, nspitable+h);
100 static unsigned long xfrm_hash_new_size(unsigned int state_hmask)
102 return ((state_hmask + 1) << 1) * sizeof(struct hlist_head);
105 static DEFINE_MUTEX(hash_resize_mutex);
107 static void xfrm_hash_resize(struct work_struct *work)
109 struct net *net = container_of(work, struct net, xfrm.state_hash_work);
110 struct hlist_head *ndst, *nsrc, *nspi, *odst, *osrc, *ospi;
111 unsigned long nsize, osize;
112 unsigned int nhashmask, ohashmask;
113 int i;
115 mutex_lock(&hash_resize_mutex);
117 nsize = xfrm_hash_new_size(net->xfrm.state_hmask);
118 ndst = xfrm_hash_alloc(nsize);
119 if (!ndst)
120 goto out_unlock;
121 nsrc = xfrm_hash_alloc(nsize);
122 if (!nsrc) {
123 xfrm_hash_free(ndst, nsize);
124 goto out_unlock;
126 nspi = xfrm_hash_alloc(nsize);
127 if (!nspi) {
128 xfrm_hash_free(ndst, nsize);
129 xfrm_hash_free(nsrc, nsize);
130 goto out_unlock;
133 spin_lock_bh(&xfrm_state_lock);
135 nhashmask = (nsize / sizeof(struct hlist_head)) - 1U;
136 for (i = net->xfrm.state_hmask; i >= 0; i--)
137 xfrm_hash_transfer(net->xfrm.state_bydst+i, ndst, nsrc, nspi,
138 nhashmask);
140 odst = net->xfrm.state_bydst;
141 osrc = net->xfrm.state_bysrc;
142 ospi = net->xfrm.state_byspi;
143 ohashmask = net->xfrm.state_hmask;
145 net->xfrm.state_bydst = ndst;
146 net->xfrm.state_bysrc = nsrc;
147 net->xfrm.state_byspi = nspi;
148 net->xfrm.state_hmask = nhashmask;
150 spin_unlock_bh(&xfrm_state_lock);
152 osize = (ohashmask + 1) * sizeof(struct hlist_head);
153 xfrm_hash_free(odst, osize);
154 xfrm_hash_free(osrc, osize);
155 xfrm_hash_free(ospi, osize);
157 out_unlock:
158 mutex_unlock(&hash_resize_mutex);
161 static DEFINE_RWLOCK(xfrm_state_afinfo_lock);
162 static struct xfrm_state_afinfo *xfrm_state_afinfo[NPROTO];
164 static DEFINE_SPINLOCK(xfrm_state_gc_lock);
166 int __xfrm_state_delete(struct xfrm_state *x);
168 int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol);
169 void km_state_expired(struct xfrm_state *x, int hard, u32 pid);
171 static struct xfrm_state_afinfo *xfrm_state_lock_afinfo(unsigned int family)
173 struct xfrm_state_afinfo *afinfo;
174 if (unlikely(family >= NPROTO))
175 return NULL;
176 write_lock_bh(&xfrm_state_afinfo_lock);
177 afinfo = xfrm_state_afinfo[family];
178 if (unlikely(!afinfo))
179 write_unlock_bh(&xfrm_state_afinfo_lock);
180 return afinfo;
183 static void xfrm_state_unlock_afinfo(struct xfrm_state_afinfo *afinfo)
184 __releases(xfrm_state_afinfo_lock)
186 write_unlock_bh(&xfrm_state_afinfo_lock);
189 int xfrm_register_type(const struct xfrm_type *type, unsigned short family)
191 struct xfrm_state_afinfo *afinfo = xfrm_state_lock_afinfo(family);
192 const struct xfrm_type **typemap;
193 int err = 0;
195 if (unlikely(afinfo == NULL))
196 return -EAFNOSUPPORT;
197 typemap = afinfo->type_map;
199 if (likely(typemap[type->proto] == NULL))
200 typemap[type->proto] = type;
201 else
202 err = -EEXIST;
203 xfrm_state_unlock_afinfo(afinfo);
204 return err;
206 EXPORT_SYMBOL(xfrm_register_type);
208 int xfrm_unregister_type(const struct xfrm_type *type, unsigned short family)
210 struct xfrm_state_afinfo *afinfo = xfrm_state_lock_afinfo(family);
211 const struct xfrm_type **typemap;
212 int err = 0;
214 if (unlikely(afinfo == NULL))
215 return -EAFNOSUPPORT;
216 typemap = afinfo->type_map;
218 if (unlikely(typemap[type->proto] != type))
219 err = -ENOENT;
220 else
221 typemap[type->proto] = NULL;
222 xfrm_state_unlock_afinfo(afinfo);
223 return err;
225 EXPORT_SYMBOL(xfrm_unregister_type);
227 static const struct xfrm_type *xfrm_get_type(u8 proto, unsigned short family)
229 struct xfrm_state_afinfo *afinfo;
230 const struct xfrm_type **typemap;
231 const struct xfrm_type *type;
232 int modload_attempted = 0;
234 retry:
235 afinfo = xfrm_state_get_afinfo(family);
236 if (unlikely(afinfo == NULL))
237 return NULL;
238 typemap = afinfo->type_map;
240 type = typemap[proto];
241 if (unlikely(type && !try_module_get(type->owner)))
242 type = NULL;
243 if (!type && !modload_attempted) {
244 xfrm_state_put_afinfo(afinfo);
245 request_module("xfrm-type-%d-%d", family, proto);
246 modload_attempted = 1;
247 goto retry;
250 xfrm_state_put_afinfo(afinfo);
251 return type;
254 static void xfrm_put_type(const struct xfrm_type *type)
256 module_put(type->owner);
259 int xfrm_register_mode(struct xfrm_mode *mode, int family)
261 struct xfrm_state_afinfo *afinfo;
262 struct xfrm_mode **modemap;
263 int err;
265 if (unlikely(mode->encap >= XFRM_MODE_MAX))
266 return -EINVAL;
268 afinfo = xfrm_state_lock_afinfo(family);
269 if (unlikely(afinfo == NULL))
270 return -EAFNOSUPPORT;
272 err = -EEXIST;
273 modemap = afinfo->mode_map;
274 if (modemap[mode->encap])
275 goto out;
277 err = -ENOENT;
278 if (!try_module_get(afinfo->owner))
279 goto out;
281 mode->afinfo = afinfo;
282 modemap[mode->encap] = mode;
283 err = 0;
285 out:
286 xfrm_state_unlock_afinfo(afinfo);
287 return err;
289 EXPORT_SYMBOL(xfrm_register_mode);
291 int xfrm_unregister_mode(struct xfrm_mode *mode, int family)
293 struct xfrm_state_afinfo *afinfo;
294 struct xfrm_mode **modemap;
295 int err;
297 if (unlikely(mode->encap >= XFRM_MODE_MAX))
298 return -EINVAL;
300 afinfo = xfrm_state_lock_afinfo(family);
301 if (unlikely(afinfo == NULL))
302 return -EAFNOSUPPORT;
304 err = -ENOENT;
305 modemap = afinfo->mode_map;
306 if (likely(modemap[mode->encap] == mode)) {
307 modemap[mode->encap] = NULL;
308 module_put(mode->afinfo->owner);
309 err = 0;
312 xfrm_state_unlock_afinfo(afinfo);
313 return err;
315 EXPORT_SYMBOL(xfrm_unregister_mode);
317 static struct xfrm_mode *xfrm_get_mode(unsigned int encap, int family)
319 struct xfrm_state_afinfo *afinfo;
320 struct xfrm_mode *mode;
321 int modload_attempted = 0;
323 if (unlikely(encap >= XFRM_MODE_MAX))
324 return NULL;
326 retry:
327 afinfo = xfrm_state_get_afinfo(family);
328 if (unlikely(afinfo == NULL))
329 return NULL;
331 mode = afinfo->mode_map[encap];
332 if (unlikely(mode && !try_module_get(mode->owner)))
333 mode = NULL;
334 if (!mode && !modload_attempted) {
335 xfrm_state_put_afinfo(afinfo);
336 request_module("xfrm-mode-%d-%d", family, encap);
337 modload_attempted = 1;
338 goto retry;
341 xfrm_state_put_afinfo(afinfo);
342 return mode;
345 static void xfrm_put_mode(struct xfrm_mode *mode)
347 module_put(mode->owner);
350 static void xfrm_state_gc_destroy(struct xfrm_state *x)
352 tasklet_hrtimer_cancel(&x->mtimer);
353 del_timer_sync(&x->rtimer);
354 kfree(x->aalg);
355 kfree(x->ealg);
356 kfree(x->calg);
357 kfree(x->encap);
358 kfree(x->coaddr);
359 kfree(x->replay_esn);
360 kfree(x->preplay_esn);
361 if (x->inner_mode)
362 xfrm_put_mode(x->inner_mode);
363 if (x->inner_mode_iaf)
364 xfrm_put_mode(x->inner_mode_iaf);
365 if (x->outer_mode)
366 xfrm_put_mode(x->outer_mode);
367 if (x->type) {
368 x->type->destructor(x);
369 xfrm_put_type(x->type);
371 security_xfrm_state_free(x);
372 kfree(x);
375 static void xfrm_state_gc_task(struct work_struct *work)
377 struct net *net = container_of(work, struct net, xfrm.state_gc_work);
378 struct xfrm_state *x;
379 struct hlist_node *entry, *tmp;
380 struct hlist_head gc_list;
382 spin_lock_bh(&xfrm_state_gc_lock);
383 hlist_move_list(&net->xfrm.state_gc_list, &gc_list);
384 spin_unlock_bh(&xfrm_state_gc_lock);
386 hlist_for_each_entry_safe(x, entry, tmp, &gc_list, gclist)
387 xfrm_state_gc_destroy(x);
389 wake_up(&net->xfrm.km_waitq);
392 static inline unsigned long make_jiffies(long secs)
394 if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
395 return MAX_SCHEDULE_TIMEOUT-1;
396 else
397 return secs*HZ;
400 static enum hrtimer_restart xfrm_timer_handler(struct hrtimer * me)
402 struct tasklet_hrtimer *thr = container_of(me, struct tasklet_hrtimer, timer);
403 struct xfrm_state *x = container_of(thr, struct xfrm_state, mtimer);
404 struct net *net = xs_net(x);
405 unsigned long now = get_seconds();
406 long next = LONG_MAX;
407 int warn = 0;
408 int err = 0;
410 spin_lock(&x->lock);
411 if (x->km.state == XFRM_STATE_DEAD)
412 goto out;
413 if (x->km.state == XFRM_STATE_EXPIRED)
414 goto expired;
415 if (x->lft.hard_add_expires_seconds) {
416 long tmo = x->lft.hard_add_expires_seconds +
417 x->curlft.add_time - now;
418 if (tmo <= 0)
419 goto expired;
420 if (tmo < next)
421 next = tmo;
423 if (x->lft.hard_use_expires_seconds) {
424 long tmo = x->lft.hard_use_expires_seconds +
425 (x->curlft.use_time ? : now) - now;
426 if (tmo <= 0)
427 goto expired;
428 if (tmo < next)
429 next = tmo;
431 if (x->km.dying)
432 goto resched;
433 if (x->lft.soft_add_expires_seconds) {
434 long tmo = x->lft.soft_add_expires_seconds +
435 x->curlft.add_time - now;
436 if (tmo <= 0)
437 warn = 1;
438 else if (tmo < next)
439 next = tmo;
441 if (x->lft.soft_use_expires_seconds) {
442 long tmo = x->lft.soft_use_expires_seconds +
443 (x->curlft.use_time ? : now) - now;
444 if (tmo <= 0)
445 warn = 1;
446 else if (tmo < next)
447 next = tmo;
450 x->km.dying = warn;
451 if (warn)
452 km_state_expired(x, 0, 0);
453 resched:
454 if (next != LONG_MAX){
455 tasklet_hrtimer_start(&x->mtimer, ktime_set(next, 0), HRTIMER_MODE_REL);
458 goto out;
460 expired:
461 if (x->km.state == XFRM_STATE_ACQ && x->id.spi == 0) {
462 x->km.state = XFRM_STATE_EXPIRED;
463 wake_up(&net->xfrm.km_waitq);
464 next = 2;
465 goto resched;
468 err = __xfrm_state_delete(x);
469 if (!err && x->id.spi)
470 km_state_expired(x, 1, 0);
472 xfrm_audit_state_delete(x, err ? 0 : 1,
473 audit_get_loginuid(current),
474 audit_get_sessionid(current), 0);
476 out:
477 spin_unlock(&x->lock);
478 return HRTIMER_NORESTART;
481 static void xfrm_replay_timer_handler(unsigned long data);
483 struct xfrm_state *xfrm_state_alloc(struct net *net)
485 struct xfrm_state *x;
487 x = kzalloc(sizeof(struct xfrm_state), GFP_ATOMIC);
489 if (x) {
490 write_pnet(&x->xs_net, net);
491 atomic_set(&x->refcnt, 1);
492 atomic_set(&x->tunnel_users, 0);
493 INIT_LIST_HEAD(&x->km.all);
494 INIT_HLIST_NODE(&x->bydst);
495 INIT_HLIST_NODE(&x->bysrc);
496 INIT_HLIST_NODE(&x->byspi);
497 tasklet_hrtimer_init(&x->mtimer, xfrm_timer_handler, CLOCK_REALTIME, HRTIMER_MODE_ABS);
498 setup_timer(&x->rtimer, xfrm_replay_timer_handler,
499 (unsigned long)x);
500 x->curlft.add_time = get_seconds();
501 x->lft.soft_byte_limit = XFRM_INF;
502 x->lft.soft_packet_limit = XFRM_INF;
503 x->lft.hard_byte_limit = XFRM_INF;
504 x->lft.hard_packet_limit = XFRM_INF;
505 x->replay_maxage = 0;
506 x->replay_maxdiff = 0;
507 x->inner_mode = NULL;
508 x->inner_mode_iaf = NULL;
509 spin_lock_init(&x->lock);
511 return x;
513 EXPORT_SYMBOL(xfrm_state_alloc);
515 void __xfrm_state_destroy(struct xfrm_state *x)
517 struct net *net = xs_net(x);
519 WARN_ON(x->km.state != XFRM_STATE_DEAD);
521 spin_lock_bh(&xfrm_state_gc_lock);
522 hlist_add_head(&x->gclist, &net->xfrm.state_gc_list);
523 spin_unlock_bh(&xfrm_state_gc_lock);
524 schedule_work(&net->xfrm.state_gc_work);
526 EXPORT_SYMBOL(__xfrm_state_destroy);
528 int __xfrm_state_delete(struct xfrm_state *x)
530 struct net *net = xs_net(x);
531 int err = -ESRCH;
533 if (x->km.state != XFRM_STATE_DEAD) {
534 x->km.state = XFRM_STATE_DEAD;
535 spin_lock(&xfrm_state_lock);
536 list_del(&x->km.all);
537 hlist_del(&x->bydst);
538 hlist_del(&x->bysrc);
539 if (x->id.spi)
540 hlist_del(&x->byspi);
541 net->xfrm.state_num--;
542 spin_unlock(&xfrm_state_lock);
544 /* All xfrm_state objects are created by xfrm_state_alloc.
545 * The xfrm_state_alloc call gives a reference, and that
546 * is what we are dropping here.
548 xfrm_state_put(x);
549 err = 0;
552 return err;
554 EXPORT_SYMBOL(__xfrm_state_delete);
556 int xfrm_state_delete(struct xfrm_state *x)
558 int err;
560 spin_lock_bh(&x->lock);
561 err = __xfrm_state_delete(x);
562 spin_unlock_bh(&x->lock);
564 return err;
566 EXPORT_SYMBOL(xfrm_state_delete);
568 #ifdef CONFIG_SECURITY_NETWORK_XFRM
569 static inline int
570 xfrm_state_flush_secctx_check(struct net *net, u8 proto, struct xfrm_audit *audit_info)
572 int i, err = 0;
574 for (i = 0; i <= net->xfrm.state_hmask; i++) {
575 struct hlist_node *entry;
576 struct xfrm_state *x;
578 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+i, bydst) {
579 if (xfrm_id_proto_match(x->id.proto, proto) &&
580 (err = security_xfrm_state_delete(x)) != 0) {
581 xfrm_audit_state_delete(x, 0,
582 audit_info->loginuid,
583 audit_info->sessionid,
584 audit_info->secid);
585 return err;
590 return err;
592 #else
593 static inline int
594 xfrm_state_flush_secctx_check(struct net *net, u8 proto, struct xfrm_audit *audit_info)
596 return 0;
598 #endif
600 int xfrm_state_flush(struct net *net, u8 proto, struct xfrm_audit *audit_info)
602 int i, err = 0, cnt = 0;
604 spin_lock_bh(&xfrm_state_lock);
605 err = xfrm_state_flush_secctx_check(net, proto, audit_info);
606 if (err)
607 goto out;
609 err = -ESRCH;
610 for (i = 0; i <= net->xfrm.state_hmask; i++) {
611 struct hlist_node *entry;
612 struct xfrm_state *x;
613 restart:
614 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+i, bydst) {
615 if (!xfrm_state_kern(x) &&
616 xfrm_id_proto_match(x->id.proto, proto)) {
617 xfrm_state_hold(x);
618 spin_unlock_bh(&xfrm_state_lock);
620 err = xfrm_state_delete(x);
621 xfrm_audit_state_delete(x, err ? 0 : 1,
622 audit_info->loginuid,
623 audit_info->sessionid,
624 audit_info->secid);
625 xfrm_state_put(x);
626 if (!err)
627 cnt++;
629 spin_lock_bh(&xfrm_state_lock);
630 goto restart;
634 if (cnt)
635 err = 0;
637 out:
638 spin_unlock_bh(&xfrm_state_lock);
639 wake_up(&net->xfrm.km_waitq);
640 return err;
642 EXPORT_SYMBOL(xfrm_state_flush);
644 void xfrm_sad_getinfo(struct net *net, struct xfrmk_sadinfo *si)
646 spin_lock_bh(&xfrm_state_lock);
647 si->sadcnt = net->xfrm.state_num;
648 si->sadhcnt = net->xfrm.state_hmask;
649 si->sadhmcnt = xfrm_state_hashmax;
650 spin_unlock_bh(&xfrm_state_lock);
652 EXPORT_SYMBOL(xfrm_sad_getinfo);
654 static int
655 xfrm_init_tempstate(struct xfrm_state *x, const struct flowi *fl,
656 const struct xfrm_tmpl *tmpl,
657 const xfrm_address_t *daddr, const xfrm_address_t *saddr,
658 unsigned short family)
660 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
661 if (!afinfo)
662 return -1;
663 afinfo->init_tempsel(&x->sel, fl);
665 if (family != tmpl->encap_family) {
666 xfrm_state_put_afinfo(afinfo);
667 afinfo = xfrm_state_get_afinfo(tmpl->encap_family);
668 if (!afinfo)
669 return -1;
671 afinfo->init_temprop(x, tmpl, daddr, saddr);
672 xfrm_state_put_afinfo(afinfo);
673 return 0;
676 static struct xfrm_state *__xfrm_state_lookup(struct net *net, u32 mark,
677 const xfrm_address_t *daddr,
678 __be32 spi, u8 proto,
679 unsigned short family)
681 unsigned int h = xfrm_spi_hash(net, daddr, spi, proto, family);
682 struct xfrm_state *x;
683 struct hlist_node *entry;
685 hlist_for_each_entry(x, entry, net->xfrm.state_byspi+h, byspi) {
686 if (x->props.family != family ||
687 x->id.spi != spi ||
688 x->id.proto != proto ||
689 xfrm_addr_cmp(&x->id.daddr, daddr, family))
690 continue;
692 if ((mark & x->mark.m) != x->mark.v)
693 continue;
694 xfrm_state_hold(x);
695 return x;
698 return NULL;
701 static struct xfrm_state *__xfrm_state_lookup_byaddr(struct net *net, u32 mark,
702 const xfrm_address_t *daddr,
703 const xfrm_address_t *saddr,
704 u8 proto, unsigned short family)
706 unsigned int h = xfrm_src_hash(net, daddr, saddr, family);
707 struct xfrm_state *x;
708 struct hlist_node *entry;
710 hlist_for_each_entry(x, entry, net->xfrm.state_bysrc+h, bysrc) {
711 if (x->props.family != family ||
712 x->id.proto != proto ||
713 xfrm_addr_cmp(&x->id.daddr, daddr, family) ||
714 xfrm_addr_cmp(&x->props.saddr, saddr, family))
715 continue;
717 if ((mark & x->mark.m) != x->mark.v)
718 continue;
719 xfrm_state_hold(x);
720 return x;
723 return NULL;
726 static inline struct xfrm_state *
727 __xfrm_state_locate(struct xfrm_state *x, int use_spi, int family)
729 struct net *net = xs_net(x);
730 u32 mark = x->mark.v & x->mark.m;
732 if (use_spi)
733 return __xfrm_state_lookup(net, mark, &x->id.daddr,
734 x->id.spi, x->id.proto, family);
735 else
736 return __xfrm_state_lookup_byaddr(net, mark,
737 &x->id.daddr,
738 &x->props.saddr,
739 x->id.proto, family);
742 static void xfrm_hash_grow_check(struct net *net, int have_hash_collision)
744 if (have_hash_collision &&
745 (net->xfrm.state_hmask + 1) < xfrm_state_hashmax &&
746 net->xfrm.state_num > net->xfrm.state_hmask)
747 schedule_work(&net->xfrm.state_hash_work);
750 static void xfrm_state_look_at(struct xfrm_policy *pol, struct xfrm_state *x,
751 const struct flowi *fl, unsigned short family,
752 struct xfrm_state **best, int *acq_in_progress,
753 int *error)
755 /* Resolution logic:
756 * 1. There is a valid state with matching selector. Done.
757 * 2. Valid state with inappropriate selector. Skip.
759 * Entering area of "sysdeps".
761 * 3. If state is not valid, selector is temporary, it selects
762 * only session which triggered previous resolution. Key
763 * manager will do something to install a state with proper
764 * selector.
766 if (x->km.state == XFRM_STATE_VALID) {
767 if ((x->sel.family &&
768 !xfrm_selector_match(&x->sel, fl, x->sel.family)) ||
769 !security_xfrm_state_pol_flow_match(x, pol, fl))
770 return;
772 if (!*best ||
773 (*best)->km.dying > x->km.dying ||
774 ((*best)->km.dying == x->km.dying &&
775 (*best)->curlft.add_time < x->curlft.add_time))
776 *best = x;
777 } else if (x->km.state == XFRM_STATE_ACQ) {
778 *acq_in_progress = 1;
779 } else if (x->km.state == XFRM_STATE_ERROR ||
780 x->km.state == XFRM_STATE_EXPIRED) {
781 if (xfrm_selector_match(&x->sel, fl, x->sel.family) &&
782 security_xfrm_state_pol_flow_match(x, pol, fl))
783 *error = -ESRCH;
787 struct xfrm_state *
788 xfrm_state_find(const xfrm_address_t *daddr, const xfrm_address_t *saddr,
789 const struct flowi *fl, struct xfrm_tmpl *tmpl,
790 struct xfrm_policy *pol, int *err,
791 unsigned short family)
793 static xfrm_address_t saddr_wildcard = { };
794 struct net *net = xp_net(pol);
795 unsigned int h, h_wildcard;
796 struct hlist_node *entry;
797 struct xfrm_state *x, *x0, *to_put;
798 int acquire_in_progress = 0;
799 int error = 0;
800 struct xfrm_state *best = NULL;
801 u32 mark = pol->mark.v & pol->mark.m;
802 unsigned short encap_family = tmpl->encap_family;
804 to_put = NULL;
806 spin_lock_bh(&xfrm_state_lock);
807 h = xfrm_dst_hash(net, daddr, saddr, tmpl->reqid, encap_family);
808 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+h, bydst) {
809 if (x->props.family == encap_family &&
810 x->props.reqid == tmpl->reqid &&
811 (mark & x->mark.m) == x->mark.v &&
812 !(x->props.flags & XFRM_STATE_WILDRECV) &&
813 xfrm_state_addr_check(x, daddr, saddr, encap_family) &&
814 tmpl->mode == x->props.mode &&
815 tmpl->id.proto == x->id.proto &&
816 (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
817 xfrm_state_look_at(pol, x, fl, encap_family,
818 &best, &acquire_in_progress, &error);
820 if (best)
821 goto found;
823 h_wildcard = xfrm_dst_hash(net, daddr, &saddr_wildcard, tmpl->reqid, encap_family);
824 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+h_wildcard, bydst) {
825 if (x->props.family == encap_family &&
826 x->props.reqid == tmpl->reqid &&
827 (mark & x->mark.m) == x->mark.v &&
828 !(x->props.flags & XFRM_STATE_WILDRECV) &&
829 xfrm_state_addr_check(x, daddr, saddr, encap_family) &&
830 tmpl->mode == x->props.mode &&
831 tmpl->id.proto == x->id.proto &&
832 (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
833 xfrm_state_look_at(pol, x, fl, encap_family,
834 &best, &acquire_in_progress, &error);
837 found:
838 x = best;
839 if (!x && !error && !acquire_in_progress) {
840 if (tmpl->id.spi &&
841 (x0 = __xfrm_state_lookup(net, mark, daddr, tmpl->id.spi,
842 tmpl->id.proto, encap_family)) != NULL) {
843 to_put = x0;
844 error = -EEXIST;
845 goto out;
847 x = xfrm_state_alloc(net);
848 if (x == NULL) {
849 error = -ENOMEM;
850 goto out;
852 /* Initialize temporary state matching only
853 * to current session. */
854 xfrm_init_tempstate(x, fl, tmpl, daddr, saddr, family);
855 memcpy(&x->mark, &pol->mark, sizeof(x->mark));
857 error = security_xfrm_state_alloc_acquire(x, pol->security, fl->flowi_secid);
858 if (error) {
859 x->km.state = XFRM_STATE_DEAD;
860 to_put = x;
861 x = NULL;
862 goto out;
865 if (km_query(x, tmpl, pol) == 0) {
866 x->km.state = XFRM_STATE_ACQ;
867 list_add(&x->km.all, &net->xfrm.state_all);
868 hlist_add_head(&x->bydst, net->xfrm.state_bydst+h);
869 h = xfrm_src_hash(net, daddr, saddr, encap_family);
870 hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h);
871 if (x->id.spi) {
872 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, encap_family);
873 hlist_add_head(&x->byspi, net->xfrm.state_byspi+h);
875 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
876 tasklet_hrtimer_start(&x->mtimer, ktime_set(net->xfrm.sysctl_acq_expires, 0), HRTIMER_MODE_REL);
877 net->xfrm.state_num++;
878 xfrm_hash_grow_check(net, x->bydst.next != NULL);
879 } else {
880 x->km.state = XFRM_STATE_DEAD;
881 to_put = x;
882 x = NULL;
883 error = -ESRCH;
886 out:
887 if (x)
888 xfrm_state_hold(x);
889 else
890 *err = acquire_in_progress ? -EAGAIN : error;
891 spin_unlock_bh(&xfrm_state_lock);
892 if (to_put)
893 xfrm_state_put(to_put);
894 return x;
897 struct xfrm_state *
898 xfrm_stateonly_find(struct net *net, u32 mark,
899 xfrm_address_t *daddr, xfrm_address_t *saddr,
900 unsigned short family, u8 mode, u8 proto, u32 reqid)
902 unsigned int h;
903 struct xfrm_state *rx = NULL, *x = NULL;
904 struct hlist_node *entry;
906 spin_lock(&xfrm_state_lock);
907 h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
908 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+h, bydst) {
909 if (x->props.family == family &&
910 x->props.reqid == reqid &&
911 (mark & x->mark.m) == x->mark.v &&
912 !(x->props.flags & XFRM_STATE_WILDRECV) &&
913 xfrm_state_addr_check(x, daddr, saddr, family) &&
914 mode == x->props.mode &&
915 proto == x->id.proto &&
916 x->km.state == XFRM_STATE_VALID) {
917 rx = x;
918 break;
922 if (rx)
923 xfrm_state_hold(rx);
924 spin_unlock(&xfrm_state_lock);
927 return rx;
929 EXPORT_SYMBOL(xfrm_stateonly_find);
931 static void __xfrm_state_insert(struct xfrm_state *x)
933 struct net *net = xs_net(x);
934 unsigned int h;
936 list_add(&x->km.all, &net->xfrm.state_all);
938 h = xfrm_dst_hash(net, &x->id.daddr, &x->props.saddr,
939 x->props.reqid, x->props.family);
940 hlist_add_head(&x->bydst, net->xfrm.state_bydst+h);
942 h = xfrm_src_hash(net, &x->id.daddr, &x->props.saddr, x->props.family);
943 hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h);
945 if (x->id.spi) {
946 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto,
947 x->props.family);
949 hlist_add_head(&x->byspi, net->xfrm.state_byspi+h);
952 tasklet_hrtimer_start(&x->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL);
953 if (x->replay_maxage)
954 mod_timer(&x->rtimer, jiffies + x->replay_maxage);
956 wake_up(&net->xfrm.km_waitq);
958 net->xfrm.state_num++;
960 xfrm_hash_grow_check(net, x->bydst.next != NULL);
963 /* xfrm_state_lock is held */
964 static void __xfrm_state_bump_genids(struct xfrm_state *xnew)
966 struct net *net = xs_net(xnew);
967 unsigned short family = xnew->props.family;
968 u32 reqid = xnew->props.reqid;
969 struct xfrm_state *x;
970 struct hlist_node *entry;
971 unsigned int h;
972 u32 mark = xnew->mark.v & xnew->mark.m;
974 h = xfrm_dst_hash(net, &xnew->id.daddr, &xnew->props.saddr, reqid, family);
975 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+h, bydst) {
976 if (x->props.family == family &&
977 x->props.reqid == reqid &&
978 (mark & x->mark.m) == x->mark.v &&
979 !xfrm_addr_cmp(&x->id.daddr, &xnew->id.daddr, family) &&
980 !xfrm_addr_cmp(&x->props.saddr, &xnew->props.saddr, family))
981 x->genid++;
985 void xfrm_state_insert(struct xfrm_state *x)
987 spin_lock_bh(&xfrm_state_lock);
988 __xfrm_state_bump_genids(x);
989 __xfrm_state_insert(x);
990 spin_unlock_bh(&xfrm_state_lock);
992 EXPORT_SYMBOL(xfrm_state_insert);
994 /* xfrm_state_lock is held */
995 static struct xfrm_state *__find_acq_core(struct net *net, struct xfrm_mark *m,
996 unsigned short family, u8 mode,
997 u32 reqid, u8 proto,
998 const xfrm_address_t *daddr,
999 const xfrm_address_t *saddr, int create)
1001 unsigned int h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
1002 struct hlist_node *entry;
1003 struct xfrm_state *x;
1004 u32 mark = m->v & m->m;
1006 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+h, bydst) {
1007 if (x->props.reqid != reqid ||
1008 x->props.mode != mode ||
1009 x->props.family != family ||
1010 x->km.state != XFRM_STATE_ACQ ||
1011 x->id.spi != 0 ||
1012 x->id.proto != proto ||
1013 (mark & x->mark.m) != x->mark.v ||
1014 xfrm_addr_cmp(&x->id.daddr, daddr, family) ||
1015 xfrm_addr_cmp(&x->props.saddr, saddr, family))
1016 continue;
1018 xfrm_state_hold(x);
1019 return x;
1022 if (!create)
1023 return NULL;
1025 x = xfrm_state_alloc(net);
1026 if (likely(x)) {
1027 switch (family) {
1028 case AF_INET:
1029 x->sel.daddr.a4 = daddr->a4;
1030 x->sel.saddr.a4 = saddr->a4;
1031 x->sel.prefixlen_d = 32;
1032 x->sel.prefixlen_s = 32;
1033 x->props.saddr.a4 = saddr->a4;
1034 x->id.daddr.a4 = daddr->a4;
1035 break;
1037 case AF_INET6:
1038 ipv6_addr_copy((struct in6_addr *)x->sel.daddr.a6,
1039 (struct in6_addr *)daddr);
1040 ipv6_addr_copy((struct in6_addr *)x->sel.saddr.a6,
1041 (struct in6_addr *)saddr);
1042 x->sel.prefixlen_d = 128;
1043 x->sel.prefixlen_s = 128;
1044 ipv6_addr_copy((struct in6_addr *)x->props.saddr.a6,
1045 (struct in6_addr *)saddr);
1046 ipv6_addr_copy((struct in6_addr *)x->id.daddr.a6,
1047 (struct in6_addr *)daddr);
1048 break;
1051 x->km.state = XFRM_STATE_ACQ;
1052 x->id.proto = proto;
1053 x->props.family = family;
1054 x->props.mode = mode;
1055 x->props.reqid = reqid;
1056 x->mark.v = m->v;
1057 x->mark.m = m->m;
1058 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
1059 xfrm_state_hold(x);
1060 tasklet_hrtimer_start(&x->mtimer, ktime_set(net->xfrm.sysctl_acq_expires, 0), HRTIMER_MODE_REL);
1061 list_add(&x->km.all, &net->xfrm.state_all);
1062 hlist_add_head(&x->bydst, net->xfrm.state_bydst+h);
1063 h = xfrm_src_hash(net, daddr, saddr, family);
1064 hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h);
1066 net->xfrm.state_num++;
1068 xfrm_hash_grow_check(net, x->bydst.next != NULL);
1071 return x;
1074 static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq);
1076 int xfrm_state_add(struct xfrm_state *x)
1078 struct net *net = xs_net(x);
1079 struct xfrm_state *x1, *to_put;
1080 int family;
1081 int err;
1082 u32 mark = x->mark.v & x->mark.m;
1083 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
1085 family = x->props.family;
1087 to_put = NULL;
1089 spin_lock_bh(&xfrm_state_lock);
1091 x1 = __xfrm_state_locate(x, use_spi, family);
1092 if (x1) {
1093 to_put = x1;
1094 x1 = NULL;
1095 err = -EEXIST;
1096 goto out;
1099 if (use_spi && x->km.seq) {
1100 x1 = __xfrm_find_acq_byseq(net, mark, x->km.seq);
1101 if (x1 && ((x1->id.proto != x->id.proto) ||
1102 xfrm_addr_cmp(&x1->id.daddr, &x->id.daddr, family))) {
1103 to_put = x1;
1104 x1 = NULL;
1108 if (use_spi && !x1)
1109 x1 = __find_acq_core(net, &x->mark, family, x->props.mode,
1110 x->props.reqid, x->id.proto,
1111 &x->id.daddr, &x->props.saddr, 0);
1113 __xfrm_state_bump_genids(x);
1114 __xfrm_state_insert(x);
1115 err = 0;
1117 out:
1118 spin_unlock_bh(&xfrm_state_lock);
1120 if (x1) {
1121 xfrm_state_delete(x1);
1122 xfrm_state_put(x1);
1125 if (to_put)
1126 xfrm_state_put(to_put);
1128 return err;
1130 EXPORT_SYMBOL(xfrm_state_add);
1132 #ifdef CONFIG_XFRM_MIGRATE
1133 static struct xfrm_state *xfrm_state_clone(struct xfrm_state *orig, int *errp)
1135 struct net *net = xs_net(orig);
1136 int err = -ENOMEM;
1137 struct xfrm_state *x = xfrm_state_alloc(net);
1138 if (!x)
1139 goto out;
1141 memcpy(&x->id, &orig->id, sizeof(x->id));
1142 memcpy(&x->sel, &orig->sel, sizeof(x->sel));
1143 memcpy(&x->lft, &orig->lft, sizeof(x->lft));
1144 x->props.mode = orig->props.mode;
1145 x->props.replay_window = orig->props.replay_window;
1146 x->props.reqid = orig->props.reqid;
1147 x->props.family = orig->props.family;
1148 x->props.saddr = orig->props.saddr;
1150 if (orig->aalg) {
1151 x->aalg = xfrm_algo_auth_clone(orig->aalg);
1152 if (!x->aalg)
1153 goto error;
1155 x->props.aalgo = orig->props.aalgo;
1157 if (orig->ealg) {
1158 x->ealg = xfrm_algo_clone(orig->ealg);
1159 if (!x->ealg)
1160 goto error;
1162 x->props.ealgo = orig->props.ealgo;
1164 if (orig->calg) {
1165 x->calg = xfrm_algo_clone(orig->calg);
1166 if (!x->calg)
1167 goto error;
1169 x->props.calgo = orig->props.calgo;
1171 if (orig->encap) {
1172 x->encap = kmemdup(orig->encap, sizeof(*x->encap), GFP_KERNEL);
1173 if (!x->encap)
1174 goto error;
1177 if (orig->coaddr) {
1178 x->coaddr = kmemdup(orig->coaddr, sizeof(*x->coaddr),
1179 GFP_KERNEL);
1180 if (!x->coaddr)
1181 goto error;
1184 memcpy(&x->mark, &orig->mark, sizeof(x->mark));
1186 err = xfrm_init_state(x);
1187 if (err)
1188 goto error;
1190 x->props.flags = orig->props.flags;
1192 x->curlft.add_time = orig->curlft.add_time;
1193 x->km.state = orig->km.state;
1194 x->km.seq = orig->km.seq;
1196 return x;
1198 error:
1199 xfrm_state_put(x);
1200 out:
1201 if (errp)
1202 *errp = err;
1203 return NULL;
1206 /* xfrm_state_lock is held */
1207 struct xfrm_state * xfrm_migrate_state_find(struct xfrm_migrate *m)
1209 unsigned int h;
1210 struct xfrm_state *x;
1211 struct hlist_node *entry;
1213 if (m->reqid) {
1214 h = xfrm_dst_hash(&init_net, &m->old_daddr, &m->old_saddr,
1215 m->reqid, m->old_family);
1216 hlist_for_each_entry(x, entry, init_net.xfrm.state_bydst+h, bydst) {
1217 if (x->props.mode != m->mode ||
1218 x->id.proto != m->proto)
1219 continue;
1220 if (m->reqid && x->props.reqid != m->reqid)
1221 continue;
1222 if (xfrm_addr_cmp(&x->id.daddr, &m->old_daddr,
1223 m->old_family) ||
1224 xfrm_addr_cmp(&x->props.saddr, &m->old_saddr,
1225 m->old_family))
1226 continue;
1227 xfrm_state_hold(x);
1228 return x;
1230 } else {
1231 h = xfrm_src_hash(&init_net, &m->old_daddr, &m->old_saddr,
1232 m->old_family);
1233 hlist_for_each_entry(x, entry, init_net.xfrm.state_bysrc+h, bysrc) {
1234 if (x->props.mode != m->mode ||
1235 x->id.proto != m->proto)
1236 continue;
1237 if (xfrm_addr_cmp(&x->id.daddr, &m->old_daddr,
1238 m->old_family) ||
1239 xfrm_addr_cmp(&x->props.saddr, &m->old_saddr,
1240 m->old_family))
1241 continue;
1242 xfrm_state_hold(x);
1243 return x;
1247 return NULL;
1249 EXPORT_SYMBOL(xfrm_migrate_state_find);
1251 struct xfrm_state * xfrm_state_migrate(struct xfrm_state *x,
1252 struct xfrm_migrate *m)
1254 struct xfrm_state *xc;
1255 int err;
1257 xc = xfrm_state_clone(x, &err);
1258 if (!xc)
1259 return NULL;
1261 memcpy(&xc->id.daddr, &m->new_daddr, sizeof(xc->id.daddr));
1262 memcpy(&xc->props.saddr, &m->new_saddr, sizeof(xc->props.saddr));
1264 /* add state */
1265 if (!xfrm_addr_cmp(&x->id.daddr, &m->new_daddr, m->new_family)) {
1266 /* a care is needed when the destination address of the
1267 state is to be updated as it is a part of triplet */
1268 xfrm_state_insert(xc);
1269 } else {
1270 if ((err = xfrm_state_add(xc)) < 0)
1271 goto error;
1274 return xc;
1275 error:
1276 xfrm_state_put(xc);
1277 return NULL;
1279 EXPORT_SYMBOL(xfrm_state_migrate);
1280 #endif
1282 int xfrm_state_update(struct xfrm_state *x)
1284 struct xfrm_state *x1, *to_put;
1285 int err;
1286 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
1288 to_put = NULL;
1290 spin_lock_bh(&xfrm_state_lock);
1291 x1 = __xfrm_state_locate(x, use_spi, x->props.family);
1293 err = -ESRCH;
1294 if (!x1)
1295 goto out;
1297 if (xfrm_state_kern(x1)) {
1298 to_put = x1;
1299 err = -EEXIST;
1300 goto out;
1303 if (x1->km.state == XFRM_STATE_ACQ) {
1304 __xfrm_state_insert(x);
1305 x = NULL;
1307 err = 0;
1309 out:
1310 spin_unlock_bh(&xfrm_state_lock);
1312 if (to_put)
1313 xfrm_state_put(to_put);
1315 if (err)
1316 return err;
1318 if (!x) {
1319 xfrm_state_delete(x1);
1320 xfrm_state_put(x1);
1321 return 0;
1324 err = -EINVAL;
1325 spin_lock_bh(&x1->lock);
1326 if (likely(x1->km.state == XFRM_STATE_VALID)) {
1327 if (x->encap && x1->encap)
1328 memcpy(x1->encap, x->encap, sizeof(*x1->encap));
1329 if (x->coaddr && x1->coaddr) {
1330 memcpy(x1->coaddr, x->coaddr, sizeof(*x1->coaddr));
1332 if (!use_spi && memcmp(&x1->sel, &x->sel, sizeof(x1->sel)))
1333 memcpy(&x1->sel, &x->sel, sizeof(x1->sel));
1334 memcpy(&x1->lft, &x->lft, sizeof(x1->lft));
1335 x1->km.dying = 0;
1337 tasklet_hrtimer_start(&x1->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL);
1338 if (x1->curlft.use_time)
1339 xfrm_state_check_expire(x1);
1341 err = 0;
1343 spin_unlock_bh(&x1->lock);
1345 xfrm_state_put(x1);
1347 return err;
1349 EXPORT_SYMBOL(xfrm_state_update);
1351 int xfrm_state_check_expire(struct xfrm_state *x)
1353 if (!x->curlft.use_time)
1354 x->curlft.use_time = get_seconds();
1356 if (x->km.state != XFRM_STATE_VALID)
1357 return -EINVAL;
1359 if (x->curlft.bytes >= x->lft.hard_byte_limit ||
1360 x->curlft.packets >= x->lft.hard_packet_limit) {
1361 x->km.state = XFRM_STATE_EXPIRED;
1362 tasklet_hrtimer_start(&x->mtimer, ktime_set(0,0), HRTIMER_MODE_REL);
1363 return -EINVAL;
1366 if (!x->km.dying &&
1367 (x->curlft.bytes >= x->lft.soft_byte_limit ||
1368 x->curlft.packets >= x->lft.soft_packet_limit)) {
1369 x->km.dying = 1;
1370 km_state_expired(x, 0, 0);
1372 return 0;
1374 EXPORT_SYMBOL(xfrm_state_check_expire);
1376 struct xfrm_state *
1377 xfrm_state_lookup(struct net *net, u32 mark, const xfrm_address_t *daddr, __be32 spi,
1378 u8 proto, unsigned short family)
1380 struct xfrm_state *x;
1382 spin_lock_bh(&xfrm_state_lock);
1383 x = __xfrm_state_lookup(net, mark, daddr, spi, proto, family);
1384 spin_unlock_bh(&xfrm_state_lock);
1385 return x;
1387 EXPORT_SYMBOL(xfrm_state_lookup);
1389 struct xfrm_state *
1390 xfrm_state_lookup_byaddr(struct net *net, u32 mark,
1391 const xfrm_address_t *daddr, const xfrm_address_t *saddr,
1392 u8 proto, unsigned short family)
1394 struct xfrm_state *x;
1396 spin_lock_bh(&xfrm_state_lock);
1397 x = __xfrm_state_lookup_byaddr(net, mark, daddr, saddr, proto, family);
1398 spin_unlock_bh(&xfrm_state_lock);
1399 return x;
1401 EXPORT_SYMBOL(xfrm_state_lookup_byaddr);
1403 struct xfrm_state *
1404 xfrm_find_acq(struct net *net, struct xfrm_mark *mark, u8 mode, u32 reqid, u8 proto,
1405 const xfrm_address_t *daddr, const xfrm_address_t *saddr,
1406 int create, unsigned short family)
1408 struct xfrm_state *x;
1410 spin_lock_bh(&xfrm_state_lock);
1411 x = __find_acq_core(net, mark, family, mode, reqid, proto, daddr, saddr, create);
1412 spin_unlock_bh(&xfrm_state_lock);
1414 return x;
1416 EXPORT_SYMBOL(xfrm_find_acq);
1418 #ifdef CONFIG_XFRM_SUB_POLICY
1420 xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n,
1421 unsigned short family)
1423 int err = 0;
1424 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
1425 if (!afinfo)
1426 return -EAFNOSUPPORT;
1428 spin_lock_bh(&xfrm_state_lock);
1429 if (afinfo->tmpl_sort)
1430 err = afinfo->tmpl_sort(dst, src, n);
1431 spin_unlock_bh(&xfrm_state_lock);
1432 xfrm_state_put_afinfo(afinfo);
1433 return err;
1435 EXPORT_SYMBOL(xfrm_tmpl_sort);
1438 xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src, int n,
1439 unsigned short family)
1441 int err = 0;
1442 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
1443 if (!afinfo)
1444 return -EAFNOSUPPORT;
1446 spin_lock_bh(&xfrm_state_lock);
1447 if (afinfo->state_sort)
1448 err = afinfo->state_sort(dst, src, n);
1449 spin_unlock_bh(&xfrm_state_lock);
1450 xfrm_state_put_afinfo(afinfo);
1451 return err;
1453 EXPORT_SYMBOL(xfrm_state_sort);
1454 #endif
1456 /* Silly enough, but I'm lazy to build resolution list */
1458 static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
1460 int i;
1462 for (i = 0; i <= net->xfrm.state_hmask; i++) {
1463 struct hlist_node *entry;
1464 struct xfrm_state *x;
1466 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+i, bydst) {
1467 if (x->km.seq == seq &&
1468 (mark & x->mark.m) == x->mark.v &&
1469 x->km.state == XFRM_STATE_ACQ) {
1470 xfrm_state_hold(x);
1471 return x;
1475 return NULL;
1478 struct xfrm_state *xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
1480 struct xfrm_state *x;
1482 spin_lock_bh(&xfrm_state_lock);
1483 x = __xfrm_find_acq_byseq(net, mark, seq);
1484 spin_unlock_bh(&xfrm_state_lock);
1485 return x;
1487 EXPORT_SYMBOL(xfrm_find_acq_byseq);
1489 u32 xfrm_get_acqseq(void)
1491 u32 res;
1492 static atomic_t acqseq;
1494 do {
1495 res = atomic_inc_return(&acqseq);
1496 } while (!res);
1498 return res;
1500 EXPORT_SYMBOL(xfrm_get_acqseq);
1502 int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high)
1504 struct net *net = xs_net(x);
1505 unsigned int h;
1506 struct xfrm_state *x0;
1507 int err = -ENOENT;
1508 __be32 minspi = htonl(low);
1509 __be32 maxspi = htonl(high);
1510 u32 mark = x->mark.v & x->mark.m;
1512 spin_lock_bh(&x->lock);
1513 if (x->km.state == XFRM_STATE_DEAD)
1514 goto unlock;
1516 err = 0;
1517 if (x->id.spi)
1518 goto unlock;
1520 err = -ENOENT;
1522 if (minspi == maxspi) {
1523 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, minspi, x->id.proto, x->props.family);
1524 if (x0) {
1525 xfrm_state_put(x0);
1526 goto unlock;
1528 x->id.spi = minspi;
1529 } else {
1530 u32 spi = 0;
1531 for (h=0; h<high-low+1; h++) {
1532 spi = low + net_random()%(high-low+1);
1533 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, htonl(spi), x->id.proto, x->props.family);
1534 if (x0 == NULL) {
1535 x->id.spi = htonl(spi);
1536 break;
1538 xfrm_state_put(x0);
1541 if (x->id.spi) {
1542 spin_lock_bh(&xfrm_state_lock);
1543 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, x->props.family);
1544 hlist_add_head(&x->byspi, net->xfrm.state_byspi+h);
1545 spin_unlock_bh(&xfrm_state_lock);
1547 err = 0;
1550 unlock:
1551 spin_unlock_bh(&x->lock);
1553 return err;
1555 EXPORT_SYMBOL(xfrm_alloc_spi);
1557 int xfrm_state_walk(struct net *net, struct xfrm_state_walk *walk,
1558 int (*func)(struct xfrm_state *, int, void*),
1559 void *data)
1561 struct xfrm_state *state;
1562 struct xfrm_state_walk *x;
1563 int err = 0;
1565 if (walk->seq != 0 && list_empty(&walk->all))
1566 return 0;
1568 spin_lock_bh(&xfrm_state_lock);
1569 if (list_empty(&walk->all))
1570 x = list_first_entry(&net->xfrm.state_all, struct xfrm_state_walk, all);
1571 else
1572 x = list_entry(&walk->all, struct xfrm_state_walk, all);
1573 list_for_each_entry_from(x, &net->xfrm.state_all, all) {
1574 if (x->state == XFRM_STATE_DEAD)
1575 continue;
1576 state = container_of(x, struct xfrm_state, km);
1577 if (!xfrm_id_proto_match(state->id.proto, walk->proto))
1578 continue;
1579 err = func(state, walk->seq, data);
1580 if (err) {
1581 list_move_tail(&walk->all, &x->all);
1582 goto out;
1584 walk->seq++;
1586 if (walk->seq == 0) {
1587 err = -ENOENT;
1588 goto out;
1590 list_del_init(&walk->all);
1591 out:
1592 spin_unlock_bh(&xfrm_state_lock);
1593 return err;
1595 EXPORT_SYMBOL(xfrm_state_walk);
1597 void xfrm_state_walk_init(struct xfrm_state_walk *walk, u8 proto)
1599 INIT_LIST_HEAD(&walk->all);
1600 walk->proto = proto;
1601 walk->state = XFRM_STATE_DEAD;
1602 walk->seq = 0;
1604 EXPORT_SYMBOL(xfrm_state_walk_init);
1606 void xfrm_state_walk_done(struct xfrm_state_walk *walk)
1608 if (list_empty(&walk->all))
1609 return;
1611 spin_lock_bh(&xfrm_state_lock);
1612 list_del(&walk->all);
1613 spin_unlock_bh(&xfrm_state_lock);
1615 EXPORT_SYMBOL(xfrm_state_walk_done);
1617 static void xfrm_replay_timer_handler(unsigned long data)
1619 struct xfrm_state *x = (struct xfrm_state*)data;
1621 spin_lock(&x->lock);
1623 if (x->km.state == XFRM_STATE_VALID) {
1624 if (xfrm_aevent_is_on(xs_net(x)))
1625 x->repl->notify(x, XFRM_REPLAY_TIMEOUT);
1626 else
1627 x->xflags |= XFRM_TIME_DEFER;
1630 spin_unlock(&x->lock);
1633 static LIST_HEAD(xfrm_km_list);
1634 static DEFINE_RWLOCK(xfrm_km_lock);
1636 void km_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
1638 struct xfrm_mgr *km;
1640 read_lock(&xfrm_km_lock);
1641 list_for_each_entry(km, &xfrm_km_list, list)
1642 if (km->notify_policy)
1643 km->notify_policy(xp, dir, c);
1644 read_unlock(&xfrm_km_lock);
1647 void km_state_notify(struct xfrm_state *x, const struct km_event *c)
1649 struct xfrm_mgr *km;
1650 read_lock(&xfrm_km_lock);
1651 list_for_each_entry(km, &xfrm_km_list, list)
1652 if (km->notify)
1653 km->notify(x, c);
1654 read_unlock(&xfrm_km_lock);
1657 EXPORT_SYMBOL(km_policy_notify);
1658 EXPORT_SYMBOL(km_state_notify);
1660 void km_state_expired(struct xfrm_state *x, int hard, u32 pid)
1662 struct net *net = xs_net(x);
1663 struct km_event c;
1665 c.data.hard = hard;
1666 c.pid = pid;
1667 c.event = XFRM_MSG_EXPIRE;
1668 km_state_notify(x, &c);
1670 if (hard)
1671 wake_up(&net->xfrm.km_waitq);
1674 EXPORT_SYMBOL(km_state_expired);
1676 * We send to all registered managers regardless of failure
1677 * We are happy with one success
1679 int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol)
1681 int err = -EINVAL, acqret;
1682 struct xfrm_mgr *km;
1684 read_lock(&xfrm_km_lock);
1685 list_for_each_entry(km, &xfrm_km_list, list) {
1686 acqret = km->acquire(x, t, pol, XFRM_POLICY_OUT);
1687 if (!acqret)
1688 err = acqret;
1690 read_unlock(&xfrm_km_lock);
1691 return err;
1693 EXPORT_SYMBOL(km_query);
1695 int km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
1697 int err = -EINVAL;
1698 struct xfrm_mgr *km;
1700 read_lock(&xfrm_km_lock);
1701 list_for_each_entry(km, &xfrm_km_list, list) {
1702 if (km->new_mapping)
1703 err = km->new_mapping(x, ipaddr, sport);
1704 if (!err)
1705 break;
1707 read_unlock(&xfrm_km_lock);
1708 return err;
1710 EXPORT_SYMBOL(km_new_mapping);
1712 void km_policy_expired(struct xfrm_policy *pol, int dir, int hard, u32 pid)
1714 struct net *net = xp_net(pol);
1715 struct km_event c;
1717 c.data.hard = hard;
1718 c.pid = pid;
1719 c.event = XFRM_MSG_POLEXPIRE;
1720 km_policy_notify(pol, dir, &c);
1722 if (hard)
1723 wake_up(&net->xfrm.km_waitq);
1725 EXPORT_SYMBOL(km_policy_expired);
1727 #ifdef CONFIG_XFRM_MIGRATE
1728 int km_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
1729 const struct xfrm_migrate *m, int num_migrate,
1730 const struct xfrm_kmaddress *k)
1732 int err = -EINVAL;
1733 int ret;
1734 struct xfrm_mgr *km;
1736 read_lock(&xfrm_km_lock);
1737 list_for_each_entry(km, &xfrm_km_list, list) {
1738 if (km->migrate) {
1739 ret = km->migrate(sel, dir, type, m, num_migrate, k);
1740 if (!ret)
1741 err = ret;
1744 read_unlock(&xfrm_km_lock);
1745 return err;
1747 EXPORT_SYMBOL(km_migrate);
1748 #endif
1750 int km_report(struct net *net, u8 proto, struct xfrm_selector *sel, xfrm_address_t *addr)
1752 int err = -EINVAL;
1753 int ret;
1754 struct xfrm_mgr *km;
1756 read_lock(&xfrm_km_lock);
1757 list_for_each_entry(km, &xfrm_km_list, list) {
1758 if (km->report) {
1759 ret = km->report(net, proto, sel, addr);
1760 if (!ret)
1761 err = ret;
1764 read_unlock(&xfrm_km_lock);
1765 return err;
1767 EXPORT_SYMBOL(km_report);
1769 int xfrm_user_policy(struct sock *sk, int optname, u8 __user *optval, int optlen)
1771 int err;
1772 u8 *data;
1773 struct xfrm_mgr *km;
1774 struct xfrm_policy *pol = NULL;
1776 if (optlen <= 0 || optlen > PAGE_SIZE)
1777 return -EMSGSIZE;
1779 data = kmalloc(optlen, GFP_KERNEL);
1780 if (!data)
1781 return -ENOMEM;
1783 err = -EFAULT;
1784 if (copy_from_user(data, optval, optlen))
1785 goto out;
1787 err = -EINVAL;
1788 read_lock(&xfrm_km_lock);
1789 list_for_each_entry(km, &xfrm_km_list, list) {
1790 pol = km->compile_policy(sk, optname, data,
1791 optlen, &err);
1792 if (err >= 0)
1793 break;
1795 read_unlock(&xfrm_km_lock);
1797 if (err >= 0) {
1798 xfrm_sk_policy_insert(sk, err, pol);
1799 xfrm_pol_put(pol);
1800 err = 0;
1803 out:
1804 kfree(data);
1805 return err;
1807 EXPORT_SYMBOL(xfrm_user_policy);
1809 int xfrm_register_km(struct xfrm_mgr *km)
1811 write_lock_bh(&xfrm_km_lock);
1812 list_add_tail(&km->list, &xfrm_km_list);
1813 write_unlock_bh(&xfrm_km_lock);
1814 return 0;
1816 EXPORT_SYMBOL(xfrm_register_km);
1818 int xfrm_unregister_km(struct xfrm_mgr *km)
1820 write_lock_bh(&xfrm_km_lock);
1821 list_del(&km->list);
1822 write_unlock_bh(&xfrm_km_lock);
1823 return 0;
1825 EXPORT_SYMBOL(xfrm_unregister_km);
1827 int xfrm_state_register_afinfo(struct xfrm_state_afinfo *afinfo)
1829 int err = 0;
1830 if (unlikely(afinfo == NULL))
1831 return -EINVAL;
1832 if (unlikely(afinfo->family >= NPROTO))
1833 return -EAFNOSUPPORT;
1834 write_lock_bh(&xfrm_state_afinfo_lock);
1835 if (unlikely(xfrm_state_afinfo[afinfo->family] != NULL))
1836 err = -ENOBUFS;
1837 else
1838 xfrm_state_afinfo[afinfo->family] = afinfo;
1839 write_unlock_bh(&xfrm_state_afinfo_lock);
1840 return err;
1842 EXPORT_SYMBOL(xfrm_state_register_afinfo);
1844 int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo)
1846 int err = 0;
1847 if (unlikely(afinfo == NULL))
1848 return -EINVAL;
1849 if (unlikely(afinfo->family >= NPROTO))
1850 return -EAFNOSUPPORT;
1851 write_lock_bh(&xfrm_state_afinfo_lock);
1852 if (likely(xfrm_state_afinfo[afinfo->family] != NULL)) {
1853 if (unlikely(xfrm_state_afinfo[afinfo->family] != afinfo))
1854 err = -EINVAL;
1855 else
1856 xfrm_state_afinfo[afinfo->family] = NULL;
1858 write_unlock_bh(&xfrm_state_afinfo_lock);
1859 return err;
1861 EXPORT_SYMBOL(xfrm_state_unregister_afinfo);
1863 static struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family)
1865 struct xfrm_state_afinfo *afinfo;
1866 if (unlikely(family >= NPROTO))
1867 return NULL;
1868 read_lock(&xfrm_state_afinfo_lock);
1869 afinfo = xfrm_state_afinfo[family];
1870 if (unlikely(!afinfo))
1871 read_unlock(&xfrm_state_afinfo_lock);
1872 return afinfo;
1875 static void xfrm_state_put_afinfo(struct xfrm_state_afinfo *afinfo)
1876 __releases(xfrm_state_afinfo_lock)
1878 read_unlock(&xfrm_state_afinfo_lock);
1881 /* Temporarily located here until net/xfrm/xfrm_tunnel.c is created */
1882 void xfrm_state_delete_tunnel(struct xfrm_state *x)
1884 if (x->tunnel) {
1885 struct xfrm_state *t = x->tunnel;
1887 if (atomic_read(&t->tunnel_users) == 2)
1888 xfrm_state_delete(t);
1889 atomic_dec(&t->tunnel_users);
1890 xfrm_state_put(t);
1891 x->tunnel = NULL;
1894 EXPORT_SYMBOL(xfrm_state_delete_tunnel);
1896 int xfrm_state_mtu(struct xfrm_state *x, int mtu)
1898 int res;
1900 spin_lock_bh(&x->lock);
1901 if (x->km.state == XFRM_STATE_VALID &&
1902 x->type && x->type->get_mtu)
1903 res = x->type->get_mtu(x, mtu);
1904 else
1905 res = mtu - x->props.header_len;
1906 spin_unlock_bh(&x->lock);
1907 return res;
1910 int __xfrm_init_state(struct xfrm_state *x, bool init_replay)
1912 struct xfrm_state_afinfo *afinfo;
1913 struct xfrm_mode *inner_mode;
1914 int family = x->props.family;
1915 int err;
1917 err = -EAFNOSUPPORT;
1918 afinfo = xfrm_state_get_afinfo(family);
1919 if (!afinfo)
1920 goto error;
1922 err = 0;
1923 if (afinfo->init_flags)
1924 err = afinfo->init_flags(x);
1926 xfrm_state_put_afinfo(afinfo);
1928 if (err)
1929 goto error;
1931 err = -EPROTONOSUPPORT;
1933 if (x->sel.family != AF_UNSPEC) {
1934 inner_mode = xfrm_get_mode(x->props.mode, x->sel.family);
1935 if (inner_mode == NULL)
1936 goto error;
1938 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL) &&
1939 family != x->sel.family) {
1940 xfrm_put_mode(inner_mode);
1941 goto error;
1944 x->inner_mode = inner_mode;
1945 } else {
1946 struct xfrm_mode *inner_mode_iaf;
1947 int iafamily = AF_INET;
1949 inner_mode = xfrm_get_mode(x->props.mode, x->props.family);
1950 if (inner_mode == NULL)
1951 goto error;
1953 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL)) {
1954 xfrm_put_mode(inner_mode);
1955 goto error;
1957 x->inner_mode = inner_mode;
1959 if (x->props.family == AF_INET)
1960 iafamily = AF_INET6;
1962 inner_mode_iaf = xfrm_get_mode(x->props.mode, iafamily);
1963 if (inner_mode_iaf) {
1964 if (inner_mode_iaf->flags & XFRM_MODE_FLAG_TUNNEL)
1965 x->inner_mode_iaf = inner_mode_iaf;
1966 else
1967 xfrm_put_mode(inner_mode_iaf);
1971 x->type = xfrm_get_type(x->id.proto, family);
1972 if (x->type == NULL)
1973 goto error;
1975 err = x->type->init_state(x);
1976 if (err)
1977 goto error;
1979 x->outer_mode = xfrm_get_mode(x->props.mode, family);
1980 if (x->outer_mode == NULL)
1981 goto error;
1983 if (init_replay) {
1984 err = xfrm_init_replay(x);
1985 if (err)
1986 goto error;
1989 x->km.state = XFRM_STATE_VALID;
1991 error:
1992 return err;
1995 EXPORT_SYMBOL(__xfrm_init_state);
1997 int xfrm_init_state(struct xfrm_state *x)
1999 return __xfrm_init_state(x, true);
2002 EXPORT_SYMBOL(xfrm_init_state);
2004 int __net_init xfrm_state_init(struct net *net)
2006 unsigned int sz;
2008 INIT_LIST_HEAD(&net->xfrm.state_all);
2010 sz = sizeof(struct hlist_head) * 8;
2012 net->xfrm.state_bydst = xfrm_hash_alloc(sz);
2013 if (!net->xfrm.state_bydst)
2014 goto out_bydst;
2015 net->xfrm.state_bysrc = xfrm_hash_alloc(sz);
2016 if (!net->xfrm.state_bysrc)
2017 goto out_bysrc;
2018 net->xfrm.state_byspi = xfrm_hash_alloc(sz);
2019 if (!net->xfrm.state_byspi)
2020 goto out_byspi;
2021 net->xfrm.state_hmask = ((sz / sizeof(struct hlist_head)) - 1);
2023 net->xfrm.state_num = 0;
2024 INIT_WORK(&net->xfrm.state_hash_work, xfrm_hash_resize);
2025 INIT_HLIST_HEAD(&net->xfrm.state_gc_list);
2026 INIT_WORK(&net->xfrm.state_gc_work, xfrm_state_gc_task);
2027 init_waitqueue_head(&net->xfrm.km_waitq);
2028 return 0;
2030 out_byspi:
2031 xfrm_hash_free(net->xfrm.state_bysrc, sz);
2032 out_bysrc:
2033 xfrm_hash_free(net->xfrm.state_bydst, sz);
2034 out_bydst:
2035 return -ENOMEM;
2038 void xfrm_state_fini(struct net *net)
2040 struct xfrm_audit audit_info;
2041 unsigned int sz;
2043 flush_work(&net->xfrm.state_hash_work);
2044 audit_info.loginuid = -1;
2045 audit_info.sessionid = -1;
2046 audit_info.secid = 0;
2047 xfrm_state_flush(net, IPSEC_PROTO_ANY, &audit_info);
2048 flush_work(&net->xfrm.state_gc_work);
2050 WARN_ON(!list_empty(&net->xfrm.state_all));
2052 sz = (net->xfrm.state_hmask + 1) * sizeof(struct hlist_head);
2053 WARN_ON(!hlist_empty(net->xfrm.state_byspi));
2054 xfrm_hash_free(net->xfrm.state_byspi, sz);
2055 WARN_ON(!hlist_empty(net->xfrm.state_bysrc));
2056 xfrm_hash_free(net->xfrm.state_bysrc, sz);
2057 WARN_ON(!hlist_empty(net->xfrm.state_bydst));
2058 xfrm_hash_free(net->xfrm.state_bydst, sz);
2061 #ifdef CONFIG_AUDITSYSCALL
2062 static void xfrm_audit_helper_sainfo(struct xfrm_state *x,
2063 struct audit_buffer *audit_buf)
2065 struct xfrm_sec_ctx *ctx = x->security;
2066 u32 spi = ntohl(x->id.spi);
2068 if (ctx)
2069 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
2070 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
2072 switch(x->props.family) {
2073 case AF_INET:
2074 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2075 &x->props.saddr.a4, &x->id.daddr.a4);
2076 break;
2077 case AF_INET6:
2078 audit_log_format(audit_buf, " src=%pI6 dst=%pI6",
2079 x->props.saddr.a6, x->id.daddr.a6);
2080 break;
2083 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
2086 static void xfrm_audit_helper_pktinfo(struct sk_buff *skb, u16 family,
2087 struct audit_buffer *audit_buf)
2089 struct iphdr *iph4;
2090 struct ipv6hdr *iph6;
2092 switch (family) {
2093 case AF_INET:
2094 iph4 = ip_hdr(skb);
2095 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2096 &iph4->saddr, &iph4->daddr);
2097 break;
2098 case AF_INET6:
2099 iph6 = ipv6_hdr(skb);
2100 audit_log_format(audit_buf,
2101 " src=%pI6 dst=%pI6 flowlbl=0x%x%02x%02x",
2102 &iph6->saddr,&iph6->daddr,
2103 iph6->flow_lbl[0] & 0x0f,
2104 iph6->flow_lbl[1],
2105 iph6->flow_lbl[2]);
2106 break;
2110 void xfrm_audit_state_add(struct xfrm_state *x, int result,
2111 uid_t auid, u32 sessionid, u32 secid)
2113 struct audit_buffer *audit_buf;
2115 audit_buf = xfrm_audit_start("SAD-add");
2116 if (audit_buf == NULL)
2117 return;
2118 xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf);
2119 xfrm_audit_helper_sainfo(x, audit_buf);
2120 audit_log_format(audit_buf, " res=%u", result);
2121 audit_log_end(audit_buf);
2123 EXPORT_SYMBOL_GPL(xfrm_audit_state_add);
2125 void xfrm_audit_state_delete(struct xfrm_state *x, int result,
2126 uid_t auid, u32 sessionid, u32 secid)
2128 struct audit_buffer *audit_buf;
2130 audit_buf = xfrm_audit_start("SAD-delete");
2131 if (audit_buf == NULL)
2132 return;
2133 xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf);
2134 xfrm_audit_helper_sainfo(x, audit_buf);
2135 audit_log_format(audit_buf, " res=%u", result);
2136 audit_log_end(audit_buf);
2138 EXPORT_SYMBOL_GPL(xfrm_audit_state_delete);
2140 void xfrm_audit_state_replay_overflow(struct xfrm_state *x,
2141 struct sk_buff *skb)
2143 struct audit_buffer *audit_buf;
2144 u32 spi;
2146 audit_buf = xfrm_audit_start("SA-replay-overflow");
2147 if (audit_buf == NULL)
2148 return;
2149 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2150 /* don't record the sequence number because it's inherent in this kind
2151 * of audit message */
2152 spi = ntohl(x->id.spi);
2153 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
2154 audit_log_end(audit_buf);
2156 EXPORT_SYMBOL_GPL(xfrm_audit_state_replay_overflow);
2158 void xfrm_audit_state_replay(struct xfrm_state *x,
2159 struct sk_buff *skb, __be32 net_seq)
2161 struct audit_buffer *audit_buf;
2162 u32 spi;
2164 audit_buf = xfrm_audit_start("SA-replayed-pkt");
2165 if (audit_buf == NULL)
2166 return;
2167 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2168 spi = ntohl(x->id.spi);
2169 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2170 spi, spi, ntohl(net_seq));
2171 audit_log_end(audit_buf);
2173 EXPORT_SYMBOL_GPL(xfrm_audit_state_replay);
2175 void xfrm_audit_state_notfound_simple(struct sk_buff *skb, u16 family)
2177 struct audit_buffer *audit_buf;
2179 audit_buf = xfrm_audit_start("SA-notfound");
2180 if (audit_buf == NULL)
2181 return;
2182 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2183 audit_log_end(audit_buf);
2185 EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound_simple);
2187 void xfrm_audit_state_notfound(struct sk_buff *skb, u16 family,
2188 __be32 net_spi, __be32 net_seq)
2190 struct audit_buffer *audit_buf;
2191 u32 spi;
2193 audit_buf = xfrm_audit_start("SA-notfound");
2194 if (audit_buf == NULL)
2195 return;
2196 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2197 spi = ntohl(net_spi);
2198 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2199 spi, spi, ntohl(net_seq));
2200 audit_log_end(audit_buf);
2202 EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound);
2204 void xfrm_audit_state_icvfail(struct xfrm_state *x,
2205 struct sk_buff *skb, u8 proto)
2207 struct audit_buffer *audit_buf;
2208 __be32 net_spi;
2209 __be32 net_seq;
2211 audit_buf = xfrm_audit_start("SA-icv-failure");
2212 if (audit_buf == NULL)
2213 return;
2214 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2215 if (xfrm_parse_spi(skb, proto, &net_spi, &net_seq) == 0) {
2216 u32 spi = ntohl(net_spi);
2217 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2218 spi, spi, ntohl(net_seq));
2220 audit_log_end(audit_buf);
2222 EXPORT_SYMBOL_GPL(xfrm_audit_state_icvfail);
2223 #endif /* CONFIG_AUDITSYSCALL */