Merge git://git.infradead.org/users/willy/linux-nvme
[linux-2.6/cjktty.git] / net / xfrm / xfrm_state.c
blob2c341bdaf47cbabfe1b8801e65a3b4a2ea571a66
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 *tmp;
76 struct xfrm_state *x;
78 hlist_for_each_entry_safe(x, 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_SPINLOCK(xfrm_state_afinfo_lock);
162 static struct xfrm_state_afinfo __rcu *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 portid);
171 static DEFINE_SPINLOCK(xfrm_type_lock);
172 int xfrm_register_type(const struct xfrm_type *type, unsigned short family)
174 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
175 const struct xfrm_type **typemap;
176 int err = 0;
178 if (unlikely(afinfo == NULL))
179 return -EAFNOSUPPORT;
180 typemap = afinfo->type_map;
181 spin_lock_bh(&xfrm_type_lock);
183 if (likely(typemap[type->proto] == NULL))
184 typemap[type->proto] = type;
185 else
186 err = -EEXIST;
187 spin_unlock_bh(&xfrm_type_lock);
188 xfrm_state_put_afinfo(afinfo);
189 return err;
191 EXPORT_SYMBOL(xfrm_register_type);
193 int xfrm_unregister_type(const struct xfrm_type *type, unsigned short family)
195 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
196 const struct xfrm_type **typemap;
197 int err = 0;
199 if (unlikely(afinfo == NULL))
200 return -EAFNOSUPPORT;
201 typemap = afinfo->type_map;
202 spin_lock_bh(&xfrm_type_lock);
204 if (unlikely(typemap[type->proto] != type))
205 err = -ENOENT;
206 else
207 typemap[type->proto] = NULL;
208 spin_unlock_bh(&xfrm_type_lock);
209 xfrm_state_put_afinfo(afinfo);
210 return err;
212 EXPORT_SYMBOL(xfrm_unregister_type);
214 static const struct xfrm_type *xfrm_get_type(u8 proto, unsigned short family)
216 struct xfrm_state_afinfo *afinfo;
217 const struct xfrm_type **typemap;
218 const struct xfrm_type *type;
219 int modload_attempted = 0;
221 retry:
222 afinfo = xfrm_state_get_afinfo(family);
223 if (unlikely(afinfo == NULL))
224 return NULL;
225 typemap = afinfo->type_map;
227 type = typemap[proto];
228 if (unlikely(type && !try_module_get(type->owner)))
229 type = NULL;
230 if (!type && !modload_attempted) {
231 xfrm_state_put_afinfo(afinfo);
232 request_module("xfrm-type-%d-%d", family, proto);
233 modload_attempted = 1;
234 goto retry;
237 xfrm_state_put_afinfo(afinfo);
238 return type;
241 static void xfrm_put_type(const struct xfrm_type *type)
243 module_put(type->owner);
246 static DEFINE_SPINLOCK(xfrm_mode_lock);
247 int xfrm_register_mode(struct xfrm_mode *mode, int family)
249 struct xfrm_state_afinfo *afinfo;
250 struct xfrm_mode **modemap;
251 int err;
253 if (unlikely(mode->encap >= XFRM_MODE_MAX))
254 return -EINVAL;
256 afinfo = xfrm_state_get_afinfo(family);
257 if (unlikely(afinfo == NULL))
258 return -EAFNOSUPPORT;
260 err = -EEXIST;
261 modemap = afinfo->mode_map;
262 spin_lock_bh(&xfrm_mode_lock);
263 if (modemap[mode->encap])
264 goto out;
266 err = -ENOENT;
267 if (!try_module_get(afinfo->owner))
268 goto out;
270 mode->afinfo = afinfo;
271 modemap[mode->encap] = mode;
272 err = 0;
274 out:
275 spin_unlock_bh(&xfrm_mode_lock);
276 xfrm_state_put_afinfo(afinfo);
277 return err;
279 EXPORT_SYMBOL(xfrm_register_mode);
281 int xfrm_unregister_mode(struct xfrm_mode *mode, int family)
283 struct xfrm_state_afinfo *afinfo;
284 struct xfrm_mode **modemap;
285 int err;
287 if (unlikely(mode->encap >= XFRM_MODE_MAX))
288 return -EINVAL;
290 afinfo = xfrm_state_get_afinfo(family);
291 if (unlikely(afinfo == NULL))
292 return -EAFNOSUPPORT;
294 err = -ENOENT;
295 modemap = afinfo->mode_map;
296 spin_lock_bh(&xfrm_mode_lock);
297 if (likely(modemap[mode->encap] == mode)) {
298 modemap[mode->encap] = NULL;
299 module_put(mode->afinfo->owner);
300 err = 0;
303 spin_unlock_bh(&xfrm_mode_lock);
304 xfrm_state_put_afinfo(afinfo);
305 return err;
307 EXPORT_SYMBOL(xfrm_unregister_mode);
309 static struct xfrm_mode *xfrm_get_mode(unsigned int encap, int family)
311 struct xfrm_state_afinfo *afinfo;
312 struct xfrm_mode *mode;
313 int modload_attempted = 0;
315 if (unlikely(encap >= XFRM_MODE_MAX))
316 return NULL;
318 retry:
319 afinfo = xfrm_state_get_afinfo(family);
320 if (unlikely(afinfo == NULL))
321 return NULL;
323 mode = afinfo->mode_map[encap];
324 if (unlikely(mode && !try_module_get(mode->owner)))
325 mode = NULL;
326 if (!mode && !modload_attempted) {
327 xfrm_state_put_afinfo(afinfo);
328 request_module("xfrm-mode-%d-%d", family, encap);
329 modload_attempted = 1;
330 goto retry;
333 xfrm_state_put_afinfo(afinfo);
334 return mode;
337 static void xfrm_put_mode(struct xfrm_mode *mode)
339 module_put(mode->owner);
342 static void xfrm_state_gc_destroy(struct xfrm_state *x)
344 tasklet_hrtimer_cancel(&x->mtimer);
345 del_timer_sync(&x->rtimer);
346 kfree(x->aalg);
347 kfree(x->ealg);
348 kfree(x->calg);
349 kfree(x->encap);
350 kfree(x->coaddr);
351 kfree(x->replay_esn);
352 kfree(x->preplay_esn);
353 if (x->inner_mode)
354 xfrm_put_mode(x->inner_mode);
355 if (x->inner_mode_iaf)
356 xfrm_put_mode(x->inner_mode_iaf);
357 if (x->outer_mode)
358 xfrm_put_mode(x->outer_mode);
359 if (x->type) {
360 x->type->destructor(x);
361 xfrm_put_type(x->type);
363 security_xfrm_state_free(x);
364 kfree(x);
367 static void xfrm_state_gc_task(struct work_struct *work)
369 struct net *net = container_of(work, struct net, xfrm.state_gc_work);
370 struct xfrm_state *x;
371 struct hlist_node *tmp;
372 struct hlist_head gc_list;
374 spin_lock_bh(&xfrm_state_gc_lock);
375 hlist_move_list(&net->xfrm.state_gc_list, &gc_list);
376 spin_unlock_bh(&xfrm_state_gc_lock);
378 hlist_for_each_entry_safe(x, tmp, &gc_list, gclist)
379 xfrm_state_gc_destroy(x);
381 wake_up(&net->xfrm.km_waitq);
384 static inline unsigned long make_jiffies(long secs)
386 if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
387 return MAX_SCHEDULE_TIMEOUT-1;
388 else
389 return secs*HZ;
392 static enum hrtimer_restart xfrm_timer_handler(struct hrtimer * me)
394 struct tasklet_hrtimer *thr = container_of(me, struct tasklet_hrtimer, timer);
395 struct xfrm_state *x = container_of(thr, struct xfrm_state, mtimer);
396 struct net *net = xs_net(x);
397 unsigned long now = get_seconds();
398 long next = LONG_MAX;
399 int warn = 0;
400 int err = 0;
402 spin_lock(&x->lock);
403 if (x->km.state == XFRM_STATE_DEAD)
404 goto out;
405 if (x->km.state == XFRM_STATE_EXPIRED)
406 goto expired;
407 if (x->lft.hard_add_expires_seconds) {
408 long tmo = x->lft.hard_add_expires_seconds +
409 x->curlft.add_time - now;
410 if (tmo <= 0) {
411 if (x->xflags & XFRM_SOFT_EXPIRE) {
412 /* enter hard expire without soft expire first?!
413 * setting a new date could trigger this.
414 * workarbound: fix x->curflt.add_time by below:
416 x->curlft.add_time = now - x->saved_tmo - 1;
417 tmo = x->lft.hard_add_expires_seconds - x->saved_tmo;
418 } else
419 goto expired;
421 if (tmo < next)
422 next = tmo;
424 if (x->lft.hard_use_expires_seconds) {
425 long tmo = x->lft.hard_use_expires_seconds +
426 (x->curlft.use_time ? : now) - now;
427 if (tmo <= 0)
428 goto expired;
429 if (tmo < next)
430 next = tmo;
432 if (x->km.dying)
433 goto resched;
434 if (x->lft.soft_add_expires_seconds) {
435 long tmo = x->lft.soft_add_expires_seconds +
436 x->curlft.add_time - now;
437 if (tmo <= 0) {
438 warn = 1;
439 x->xflags &= ~XFRM_SOFT_EXPIRE;
440 } else if (tmo < next) {
441 next = tmo;
442 x->xflags |= XFRM_SOFT_EXPIRE;
443 x->saved_tmo = tmo;
446 if (x->lft.soft_use_expires_seconds) {
447 long tmo = x->lft.soft_use_expires_seconds +
448 (x->curlft.use_time ? : now) - now;
449 if (tmo <= 0)
450 warn = 1;
451 else if (tmo < next)
452 next = tmo;
455 x->km.dying = warn;
456 if (warn)
457 km_state_expired(x, 0, 0);
458 resched:
459 if (next != LONG_MAX){
460 tasklet_hrtimer_start(&x->mtimer, ktime_set(next, 0), HRTIMER_MODE_REL);
463 goto out;
465 expired:
466 if (x->km.state == XFRM_STATE_ACQ && x->id.spi == 0) {
467 x->km.state = XFRM_STATE_EXPIRED;
468 wake_up(&net->xfrm.km_waitq);
469 next = 2;
470 goto resched;
473 err = __xfrm_state_delete(x);
474 if (!err && x->id.spi)
475 km_state_expired(x, 1, 0);
477 xfrm_audit_state_delete(x, err ? 0 : 1,
478 audit_get_loginuid(current),
479 audit_get_sessionid(current), 0);
481 out:
482 spin_unlock(&x->lock);
483 return HRTIMER_NORESTART;
486 static void xfrm_replay_timer_handler(unsigned long data);
488 struct xfrm_state *xfrm_state_alloc(struct net *net)
490 struct xfrm_state *x;
492 x = kzalloc(sizeof(struct xfrm_state), GFP_ATOMIC);
494 if (x) {
495 write_pnet(&x->xs_net, net);
496 atomic_set(&x->refcnt, 1);
497 atomic_set(&x->tunnel_users, 0);
498 INIT_LIST_HEAD(&x->km.all);
499 INIT_HLIST_NODE(&x->bydst);
500 INIT_HLIST_NODE(&x->bysrc);
501 INIT_HLIST_NODE(&x->byspi);
502 tasklet_hrtimer_init(&x->mtimer, xfrm_timer_handler, CLOCK_REALTIME, HRTIMER_MODE_ABS);
503 setup_timer(&x->rtimer, xfrm_replay_timer_handler,
504 (unsigned long)x);
505 x->curlft.add_time = get_seconds();
506 x->lft.soft_byte_limit = XFRM_INF;
507 x->lft.soft_packet_limit = XFRM_INF;
508 x->lft.hard_byte_limit = XFRM_INF;
509 x->lft.hard_packet_limit = XFRM_INF;
510 x->replay_maxage = 0;
511 x->replay_maxdiff = 0;
512 x->inner_mode = NULL;
513 x->inner_mode_iaf = NULL;
514 spin_lock_init(&x->lock);
516 return x;
518 EXPORT_SYMBOL(xfrm_state_alloc);
520 void __xfrm_state_destroy(struct xfrm_state *x)
522 struct net *net = xs_net(x);
524 WARN_ON(x->km.state != XFRM_STATE_DEAD);
526 spin_lock_bh(&xfrm_state_gc_lock);
527 hlist_add_head(&x->gclist, &net->xfrm.state_gc_list);
528 spin_unlock_bh(&xfrm_state_gc_lock);
529 schedule_work(&net->xfrm.state_gc_work);
531 EXPORT_SYMBOL(__xfrm_state_destroy);
533 int __xfrm_state_delete(struct xfrm_state *x)
535 struct net *net = xs_net(x);
536 int err = -ESRCH;
538 if (x->km.state != XFRM_STATE_DEAD) {
539 x->km.state = XFRM_STATE_DEAD;
540 spin_lock(&xfrm_state_lock);
541 list_del(&x->km.all);
542 hlist_del(&x->bydst);
543 hlist_del(&x->bysrc);
544 if (x->id.spi)
545 hlist_del(&x->byspi);
546 net->xfrm.state_num--;
547 spin_unlock(&xfrm_state_lock);
549 /* All xfrm_state objects are created by xfrm_state_alloc.
550 * The xfrm_state_alloc call gives a reference, and that
551 * is what we are dropping here.
553 xfrm_state_put(x);
554 err = 0;
557 return err;
559 EXPORT_SYMBOL(__xfrm_state_delete);
561 int xfrm_state_delete(struct xfrm_state *x)
563 int err;
565 spin_lock_bh(&x->lock);
566 err = __xfrm_state_delete(x);
567 spin_unlock_bh(&x->lock);
569 return err;
571 EXPORT_SYMBOL(xfrm_state_delete);
573 #ifdef CONFIG_SECURITY_NETWORK_XFRM
574 static inline int
575 xfrm_state_flush_secctx_check(struct net *net, u8 proto, struct xfrm_audit *audit_info)
577 int i, err = 0;
579 for (i = 0; i <= net->xfrm.state_hmask; i++) {
580 struct xfrm_state *x;
582 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
583 if (xfrm_id_proto_match(x->id.proto, proto) &&
584 (err = security_xfrm_state_delete(x)) != 0) {
585 xfrm_audit_state_delete(x, 0,
586 audit_info->loginuid,
587 audit_info->sessionid,
588 audit_info->secid);
589 return err;
594 return err;
596 #else
597 static inline int
598 xfrm_state_flush_secctx_check(struct net *net, u8 proto, struct xfrm_audit *audit_info)
600 return 0;
602 #endif
604 int xfrm_state_flush(struct net *net, u8 proto, struct xfrm_audit *audit_info)
606 int i, err = 0, cnt = 0;
608 spin_lock_bh(&xfrm_state_lock);
609 err = xfrm_state_flush_secctx_check(net, proto, audit_info);
610 if (err)
611 goto out;
613 err = -ESRCH;
614 for (i = 0; i <= net->xfrm.state_hmask; i++) {
615 struct xfrm_state *x;
616 restart:
617 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
618 if (!xfrm_state_kern(x) &&
619 xfrm_id_proto_match(x->id.proto, proto)) {
620 xfrm_state_hold(x);
621 spin_unlock_bh(&xfrm_state_lock);
623 err = xfrm_state_delete(x);
624 xfrm_audit_state_delete(x, err ? 0 : 1,
625 audit_info->loginuid,
626 audit_info->sessionid,
627 audit_info->secid);
628 xfrm_state_put(x);
629 if (!err)
630 cnt++;
632 spin_lock_bh(&xfrm_state_lock);
633 goto restart;
637 if (cnt)
638 err = 0;
640 out:
641 spin_unlock_bh(&xfrm_state_lock);
642 wake_up(&net->xfrm.km_waitq);
643 return err;
645 EXPORT_SYMBOL(xfrm_state_flush);
647 void xfrm_sad_getinfo(struct net *net, struct xfrmk_sadinfo *si)
649 spin_lock_bh(&xfrm_state_lock);
650 si->sadcnt = net->xfrm.state_num;
651 si->sadhcnt = net->xfrm.state_hmask;
652 si->sadhmcnt = xfrm_state_hashmax;
653 spin_unlock_bh(&xfrm_state_lock);
655 EXPORT_SYMBOL(xfrm_sad_getinfo);
657 static int
658 xfrm_init_tempstate(struct xfrm_state *x, const struct flowi *fl,
659 const struct xfrm_tmpl *tmpl,
660 const xfrm_address_t *daddr, const xfrm_address_t *saddr,
661 unsigned short family)
663 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
664 if (!afinfo)
665 return -1;
666 afinfo->init_tempsel(&x->sel, fl);
668 if (family != tmpl->encap_family) {
669 xfrm_state_put_afinfo(afinfo);
670 afinfo = xfrm_state_get_afinfo(tmpl->encap_family);
671 if (!afinfo)
672 return -1;
674 afinfo->init_temprop(x, tmpl, daddr, saddr);
675 xfrm_state_put_afinfo(afinfo);
676 return 0;
679 static struct xfrm_state *__xfrm_state_lookup(struct net *net, u32 mark,
680 const xfrm_address_t *daddr,
681 __be32 spi, u8 proto,
682 unsigned short family)
684 unsigned int h = xfrm_spi_hash(net, daddr, spi, proto, family);
685 struct xfrm_state *x;
687 hlist_for_each_entry(x, net->xfrm.state_byspi+h, byspi) {
688 if (x->props.family != family ||
689 x->id.spi != spi ||
690 x->id.proto != proto ||
691 !xfrm_addr_equal(&x->id.daddr, daddr, family))
692 continue;
694 if ((mark & x->mark.m) != x->mark.v)
695 continue;
696 xfrm_state_hold(x);
697 return x;
700 return NULL;
703 static struct xfrm_state *__xfrm_state_lookup_byaddr(struct net *net, u32 mark,
704 const xfrm_address_t *daddr,
705 const xfrm_address_t *saddr,
706 u8 proto, unsigned short family)
708 unsigned int h = xfrm_src_hash(net, daddr, saddr, family);
709 struct xfrm_state *x;
711 hlist_for_each_entry(x, net->xfrm.state_bysrc+h, bysrc) {
712 if (x->props.family != family ||
713 x->id.proto != proto ||
714 !xfrm_addr_equal(&x->id.daddr, daddr, family) ||
715 !xfrm_addr_equal(&x->props.saddr, saddr, family))
716 continue;
718 if ((mark & x->mark.m) != x->mark.v)
719 continue;
720 xfrm_state_hold(x);
721 return x;
724 return NULL;
727 static inline struct xfrm_state *
728 __xfrm_state_locate(struct xfrm_state *x, int use_spi, int family)
730 struct net *net = xs_net(x);
731 u32 mark = x->mark.v & x->mark.m;
733 if (use_spi)
734 return __xfrm_state_lookup(net, mark, &x->id.daddr,
735 x->id.spi, x->id.proto, family);
736 else
737 return __xfrm_state_lookup_byaddr(net, mark,
738 &x->id.daddr,
739 &x->props.saddr,
740 x->id.proto, family);
743 static void xfrm_hash_grow_check(struct net *net, int have_hash_collision)
745 if (have_hash_collision &&
746 (net->xfrm.state_hmask + 1) < xfrm_state_hashmax &&
747 net->xfrm.state_num > net->xfrm.state_hmask)
748 schedule_work(&net->xfrm.state_hash_work);
751 static void xfrm_state_look_at(struct xfrm_policy *pol, struct xfrm_state *x,
752 const struct flowi *fl, unsigned short family,
753 struct xfrm_state **best, int *acq_in_progress,
754 int *error)
756 /* Resolution logic:
757 * 1. There is a valid state with matching selector. Done.
758 * 2. Valid state with inappropriate selector. Skip.
760 * Entering area of "sysdeps".
762 * 3. If state is not valid, selector is temporary, it selects
763 * only session which triggered previous resolution. Key
764 * manager will do something to install a state with proper
765 * selector.
767 if (x->km.state == XFRM_STATE_VALID) {
768 if ((x->sel.family &&
769 !xfrm_selector_match(&x->sel, fl, x->sel.family)) ||
770 !security_xfrm_state_pol_flow_match(x, pol, fl))
771 return;
773 if (!*best ||
774 (*best)->km.dying > x->km.dying ||
775 ((*best)->km.dying == x->km.dying &&
776 (*best)->curlft.add_time < x->curlft.add_time))
777 *best = x;
778 } else if (x->km.state == XFRM_STATE_ACQ) {
779 *acq_in_progress = 1;
780 } else if (x->km.state == XFRM_STATE_ERROR ||
781 x->km.state == XFRM_STATE_EXPIRED) {
782 if (xfrm_selector_match(&x->sel, fl, x->sel.family) &&
783 security_xfrm_state_pol_flow_match(x, pol, fl))
784 *error = -ESRCH;
788 struct xfrm_state *
789 xfrm_state_find(const xfrm_address_t *daddr, const xfrm_address_t *saddr,
790 const struct flowi *fl, struct xfrm_tmpl *tmpl,
791 struct xfrm_policy *pol, int *err,
792 unsigned short family)
794 static xfrm_address_t saddr_wildcard = { };
795 struct net *net = xp_net(pol);
796 unsigned int h, h_wildcard;
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, 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, 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;
905 spin_lock(&xfrm_state_lock);
906 h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
907 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
908 if (x->props.family == family &&
909 x->props.reqid == reqid &&
910 (mark & x->mark.m) == x->mark.v &&
911 !(x->props.flags & XFRM_STATE_WILDRECV) &&
912 xfrm_state_addr_check(x, daddr, saddr, family) &&
913 mode == x->props.mode &&
914 proto == x->id.proto &&
915 x->km.state == XFRM_STATE_VALID) {
916 rx = x;
917 break;
921 if (rx)
922 xfrm_state_hold(rx);
923 spin_unlock(&xfrm_state_lock);
926 return rx;
928 EXPORT_SYMBOL(xfrm_stateonly_find);
930 static void __xfrm_state_insert(struct xfrm_state *x)
932 struct net *net = xs_net(x);
933 unsigned int h;
935 list_add(&x->km.all, &net->xfrm.state_all);
937 h = xfrm_dst_hash(net, &x->id.daddr, &x->props.saddr,
938 x->props.reqid, x->props.family);
939 hlist_add_head(&x->bydst, net->xfrm.state_bydst+h);
941 h = xfrm_src_hash(net, &x->id.daddr, &x->props.saddr, x->props.family);
942 hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h);
944 if (x->id.spi) {
945 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto,
946 x->props.family);
948 hlist_add_head(&x->byspi, net->xfrm.state_byspi+h);
951 tasklet_hrtimer_start(&x->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL);
952 if (x->replay_maxage)
953 mod_timer(&x->rtimer, jiffies + x->replay_maxage);
955 wake_up(&net->xfrm.km_waitq);
957 net->xfrm.state_num++;
959 xfrm_hash_grow_check(net, x->bydst.next != NULL);
962 /* xfrm_state_lock is held */
963 static void __xfrm_state_bump_genids(struct xfrm_state *xnew)
965 struct net *net = xs_net(xnew);
966 unsigned short family = xnew->props.family;
967 u32 reqid = xnew->props.reqid;
968 struct xfrm_state *x;
969 unsigned int h;
970 u32 mark = xnew->mark.v & xnew->mark.m;
972 h = xfrm_dst_hash(net, &xnew->id.daddr, &xnew->props.saddr, reqid, family);
973 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
974 if (x->props.family == family &&
975 x->props.reqid == reqid &&
976 (mark & x->mark.m) == x->mark.v &&
977 xfrm_addr_equal(&x->id.daddr, &xnew->id.daddr, family) &&
978 xfrm_addr_equal(&x->props.saddr, &xnew->props.saddr, family))
979 x->genid++;
983 void xfrm_state_insert(struct xfrm_state *x)
985 spin_lock_bh(&xfrm_state_lock);
986 __xfrm_state_bump_genids(x);
987 __xfrm_state_insert(x);
988 spin_unlock_bh(&xfrm_state_lock);
990 EXPORT_SYMBOL(xfrm_state_insert);
992 /* xfrm_state_lock is held */
993 static struct xfrm_state *__find_acq_core(struct net *net, struct xfrm_mark *m,
994 unsigned short family, u8 mode,
995 u32 reqid, u8 proto,
996 const xfrm_address_t *daddr,
997 const xfrm_address_t *saddr, int create)
999 unsigned int h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
1000 struct xfrm_state *x;
1001 u32 mark = m->v & m->m;
1003 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
1004 if (x->props.reqid != reqid ||
1005 x->props.mode != mode ||
1006 x->props.family != family ||
1007 x->km.state != XFRM_STATE_ACQ ||
1008 x->id.spi != 0 ||
1009 x->id.proto != proto ||
1010 (mark & x->mark.m) != x->mark.v ||
1011 !xfrm_addr_equal(&x->id.daddr, daddr, family) ||
1012 !xfrm_addr_equal(&x->props.saddr, saddr, family))
1013 continue;
1015 xfrm_state_hold(x);
1016 return x;
1019 if (!create)
1020 return NULL;
1022 x = xfrm_state_alloc(net);
1023 if (likely(x)) {
1024 switch (family) {
1025 case AF_INET:
1026 x->sel.daddr.a4 = daddr->a4;
1027 x->sel.saddr.a4 = saddr->a4;
1028 x->sel.prefixlen_d = 32;
1029 x->sel.prefixlen_s = 32;
1030 x->props.saddr.a4 = saddr->a4;
1031 x->id.daddr.a4 = daddr->a4;
1032 break;
1034 case AF_INET6:
1035 *(struct in6_addr *)x->sel.daddr.a6 = *(struct in6_addr *)daddr;
1036 *(struct in6_addr *)x->sel.saddr.a6 = *(struct in6_addr *)saddr;
1037 x->sel.prefixlen_d = 128;
1038 x->sel.prefixlen_s = 128;
1039 *(struct in6_addr *)x->props.saddr.a6 = *(struct in6_addr *)saddr;
1040 *(struct in6_addr *)x->id.daddr.a6 = *(struct in6_addr *)daddr;
1041 break;
1044 x->km.state = XFRM_STATE_ACQ;
1045 x->id.proto = proto;
1046 x->props.family = family;
1047 x->props.mode = mode;
1048 x->props.reqid = reqid;
1049 x->mark.v = m->v;
1050 x->mark.m = m->m;
1051 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
1052 xfrm_state_hold(x);
1053 tasklet_hrtimer_start(&x->mtimer, ktime_set(net->xfrm.sysctl_acq_expires, 0), HRTIMER_MODE_REL);
1054 list_add(&x->km.all, &net->xfrm.state_all);
1055 hlist_add_head(&x->bydst, net->xfrm.state_bydst+h);
1056 h = xfrm_src_hash(net, daddr, saddr, family);
1057 hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h);
1059 net->xfrm.state_num++;
1061 xfrm_hash_grow_check(net, x->bydst.next != NULL);
1064 return x;
1067 static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq);
1069 int xfrm_state_add(struct xfrm_state *x)
1071 struct net *net = xs_net(x);
1072 struct xfrm_state *x1, *to_put;
1073 int family;
1074 int err;
1075 u32 mark = x->mark.v & x->mark.m;
1076 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
1078 family = x->props.family;
1080 to_put = NULL;
1082 spin_lock_bh(&xfrm_state_lock);
1084 x1 = __xfrm_state_locate(x, use_spi, family);
1085 if (x1) {
1086 to_put = x1;
1087 x1 = NULL;
1088 err = -EEXIST;
1089 goto out;
1092 if (use_spi && x->km.seq) {
1093 x1 = __xfrm_find_acq_byseq(net, mark, x->km.seq);
1094 if (x1 && ((x1->id.proto != x->id.proto) ||
1095 !xfrm_addr_equal(&x1->id.daddr, &x->id.daddr, family))) {
1096 to_put = x1;
1097 x1 = NULL;
1101 if (use_spi && !x1)
1102 x1 = __find_acq_core(net, &x->mark, family, x->props.mode,
1103 x->props.reqid, x->id.proto,
1104 &x->id.daddr, &x->props.saddr, 0);
1106 __xfrm_state_bump_genids(x);
1107 __xfrm_state_insert(x);
1108 err = 0;
1110 out:
1111 spin_unlock_bh(&xfrm_state_lock);
1113 if (x1) {
1114 xfrm_state_delete(x1);
1115 xfrm_state_put(x1);
1118 if (to_put)
1119 xfrm_state_put(to_put);
1121 return err;
1123 EXPORT_SYMBOL(xfrm_state_add);
1125 #ifdef CONFIG_XFRM_MIGRATE
1126 static struct xfrm_state *xfrm_state_clone(struct xfrm_state *orig, int *errp)
1128 struct net *net = xs_net(orig);
1129 int err = -ENOMEM;
1130 struct xfrm_state *x = xfrm_state_alloc(net);
1131 if (!x)
1132 goto out;
1134 memcpy(&x->id, &orig->id, sizeof(x->id));
1135 memcpy(&x->sel, &orig->sel, sizeof(x->sel));
1136 memcpy(&x->lft, &orig->lft, sizeof(x->lft));
1137 x->props.mode = orig->props.mode;
1138 x->props.replay_window = orig->props.replay_window;
1139 x->props.reqid = orig->props.reqid;
1140 x->props.family = orig->props.family;
1141 x->props.saddr = orig->props.saddr;
1143 if (orig->aalg) {
1144 x->aalg = xfrm_algo_auth_clone(orig->aalg);
1145 if (!x->aalg)
1146 goto error;
1148 x->props.aalgo = orig->props.aalgo;
1150 if (orig->ealg) {
1151 x->ealg = xfrm_algo_clone(orig->ealg);
1152 if (!x->ealg)
1153 goto error;
1155 x->props.ealgo = orig->props.ealgo;
1157 if (orig->calg) {
1158 x->calg = xfrm_algo_clone(orig->calg);
1159 if (!x->calg)
1160 goto error;
1162 x->props.calgo = orig->props.calgo;
1164 if (orig->encap) {
1165 x->encap = kmemdup(orig->encap, sizeof(*x->encap), GFP_KERNEL);
1166 if (!x->encap)
1167 goto error;
1170 if (orig->coaddr) {
1171 x->coaddr = kmemdup(orig->coaddr, sizeof(*x->coaddr),
1172 GFP_KERNEL);
1173 if (!x->coaddr)
1174 goto error;
1177 if (orig->replay_esn) {
1178 err = xfrm_replay_clone(x, orig);
1179 if (err)
1180 goto error;
1183 memcpy(&x->mark, &orig->mark, sizeof(x->mark));
1185 err = xfrm_init_state(x);
1186 if (err)
1187 goto error;
1189 x->props.flags = orig->props.flags;
1191 x->curlft.add_time = orig->curlft.add_time;
1192 x->km.state = orig->km.state;
1193 x->km.seq = orig->km.seq;
1195 return x;
1197 error:
1198 xfrm_state_put(x);
1199 out:
1200 if (errp)
1201 *errp = err;
1202 return NULL;
1205 /* xfrm_state_lock is held */
1206 struct xfrm_state * xfrm_migrate_state_find(struct xfrm_migrate *m)
1208 unsigned int h;
1209 struct xfrm_state *x;
1211 if (m->reqid) {
1212 h = xfrm_dst_hash(&init_net, &m->old_daddr, &m->old_saddr,
1213 m->reqid, m->old_family);
1214 hlist_for_each_entry(x, init_net.xfrm.state_bydst+h, bydst) {
1215 if (x->props.mode != m->mode ||
1216 x->id.proto != m->proto)
1217 continue;
1218 if (m->reqid && x->props.reqid != m->reqid)
1219 continue;
1220 if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr,
1221 m->old_family) ||
1222 !xfrm_addr_equal(&x->props.saddr, &m->old_saddr,
1223 m->old_family))
1224 continue;
1225 xfrm_state_hold(x);
1226 return x;
1228 } else {
1229 h = xfrm_src_hash(&init_net, &m->old_daddr, &m->old_saddr,
1230 m->old_family);
1231 hlist_for_each_entry(x, init_net.xfrm.state_bysrc+h, bysrc) {
1232 if (x->props.mode != m->mode ||
1233 x->id.proto != m->proto)
1234 continue;
1235 if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr,
1236 m->old_family) ||
1237 !xfrm_addr_equal(&x->props.saddr, &m->old_saddr,
1238 m->old_family))
1239 continue;
1240 xfrm_state_hold(x);
1241 return x;
1245 return NULL;
1247 EXPORT_SYMBOL(xfrm_migrate_state_find);
1249 struct xfrm_state * xfrm_state_migrate(struct xfrm_state *x,
1250 struct xfrm_migrate *m)
1252 struct xfrm_state *xc;
1253 int err;
1255 xc = xfrm_state_clone(x, &err);
1256 if (!xc)
1257 return NULL;
1259 memcpy(&xc->id.daddr, &m->new_daddr, sizeof(xc->id.daddr));
1260 memcpy(&xc->props.saddr, &m->new_saddr, sizeof(xc->props.saddr));
1262 /* add state */
1263 if (xfrm_addr_equal(&x->id.daddr, &m->new_daddr, m->new_family)) {
1264 /* a care is needed when the destination address of the
1265 state is to be updated as it is a part of triplet */
1266 xfrm_state_insert(xc);
1267 } else {
1268 if ((err = xfrm_state_add(xc)) < 0)
1269 goto error;
1272 return xc;
1273 error:
1274 xfrm_state_put(xc);
1275 return NULL;
1277 EXPORT_SYMBOL(xfrm_state_migrate);
1278 #endif
1280 int xfrm_state_update(struct xfrm_state *x)
1282 struct xfrm_state *x1, *to_put;
1283 int err;
1284 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
1286 to_put = NULL;
1288 spin_lock_bh(&xfrm_state_lock);
1289 x1 = __xfrm_state_locate(x, use_spi, x->props.family);
1291 err = -ESRCH;
1292 if (!x1)
1293 goto out;
1295 if (xfrm_state_kern(x1)) {
1296 to_put = x1;
1297 err = -EEXIST;
1298 goto out;
1301 if (x1->km.state == XFRM_STATE_ACQ) {
1302 __xfrm_state_insert(x);
1303 x = NULL;
1305 err = 0;
1307 out:
1308 spin_unlock_bh(&xfrm_state_lock);
1310 if (to_put)
1311 xfrm_state_put(to_put);
1313 if (err)
1314 return err;
1316 if (!x) {
1317 xfrm_state_delete(x1);
1318 xfrm_state_put(x1);
1319 return 0;
1322 err = -EINVAL;
1323 spin_lock_bh(&x1->lock);
1324 if (likely(x1->km.state == XFRM_STATE_VALID)) {
1325 if (x->encap && x1->encap)
1326 memcpy(x1->encap, x->encap, sizeof(*x1->encap));
1327 if (x->coaddr && x1->coaddr) {
1328 memcpy(x1->coaddr, x->coaddr, sizeof(*x1->coaddr));
1330 if (!use_spi && memcmp(&x1->sel, &x->sel, sizeof(x1->sel)))
1331 memcpy(&x1->sel, &x->sel, sizeof(x1->sel));
1332 memcpy(&x1->lft, &x->lft, sizeof(x1->lft));
1333 x1->km.dying = 0;
1335 tasklet_hrtimer_start(&x1->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL);
1336 if (x1->curlft.use_time)
1337 xfrm_state_check_expire(x1);
1339 err = 0;
1340 x->km.state = XFRM_STATE_DEAD;
1341 __xfrm_state_put(x);
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->curlft.bytes >= x->lft.hard_byte_limit ||
1357 x->curlft.packets >= x->lft.hard_packet_limit) {
1358 x->km.state = XFRM_STATE_EXPIRED;
1359 tasklet_hrtimer_start(&x->mtimer, ktime_set(0,0), HRTIMER_MODE_REL);
1360 return -EINVAL;
1363 if (!x->km.dying &&
1364 (x->curlft.bytes >= x->lft.soft_byte_limit ||
1365 x->curlft.packets >= x->lft.soft_packet_limit)) {
1366 x->km.dying = 1;
1367 km_state_expired(x, 0, 0);
1369 return 0;
1371 EXPORT_SYMBOL(xfrm_state_check_expire);
1373 struct xfrm_state *
1374 xfrm_state_lookup(struct net *net, u32 mark, const xfrm_address_t *daddr, __be32 spi,
1375 u8 proto, unsigned short family)
1377 struct xfrm_state *x;
1379 spin_lock_bh(&xfrm_state_lock);
1380 x = __xfrm_state_lookup(net, mark, daddr, spi, proto, family);
1381 spin_unlock_bh(&xfrm_state_lock);
1382 return x;
1384 EXPORT_SYMBOL(xfrm_state_lookup);
1386 struct xfrm_state *
1387 xfrm_state_lookup_byaddr(struct net *net, u32 mark,
1388 const xfrm_address_t *daddr, const xfrm_address_t *saddr,
1389 u8 proto, unsigned short family)
1391 struct xfrm_state *x;
1393 spin_lock_bh(&xfrm_state_lock);
1394 x = __xfrm_state_lookup_byaddr(net, mark, daddr, saddr, proto, family);
1395 spin_unlock_bh(&xfrm_state_lock);
1396 return x;
1398 EXPORT_SYMBOL(xfrm_state_lookup_byaddr);
1400 struct xfrm_state *
1401 xfrm_find_acq(struct net *net, struct xfrm_mark *mark, u8 mode, u32 reqid, u8 proto,
1402 const xfrm_address_t *daddr, const xfrm_address_t *saddr,
1403 int create, unsigned short family)
1405 struct xfrm_state *x;
1407 spin_lock_bh(&xfrm_state_lock);
1408 x = __find_acq_core(net, mark, family, mode, reqid, proto, daddr, saddr, create);
1409 spin_unlock_bh(&xfrm_state_lock);
1411 return x;
1413 EXPORT_SYMBOL(xfrm_find_acq);
1415 #ifdef CONFIG_XFRM_SUB_POLICY
1417 xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n,
1418 unsigned short family)
1420 int err = 0;
1421 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
1422 if (!afinfo)
1423 return -EAFNOSUPPORT;
1425 spin_lock_bh(&xfrm_state_lock);
1426 if (afinfo->tmpl_sort)
1427 err = afinfo->tmpl_sort(dst, src, n);
1428 spin_unlock_bh(&xfrm_state_lock);
1429 xfrm_state_put_afinfo(afinfo);
1430 return err;
1432 EXPORT_SYMBOL(xfrm_tmpl_sort);
1435 xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src, int n,
1436 unsigned short family)
1438 int err = 0;
1439 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
1440 if (!afinfo)
1441 return -EAFNOSUPPORT;
1443 spin_lock_bh(&xfrm_state_lock);
1444 if (afinfo->state_sort)
1445 err = afinfo->state_sort(dst, src, n);
1446 spin_unlock_bh(&xfrm_state_lock);
1447 xfrm_state_put_afinfo(afinfo);
1448 return err;
1450 EXPORT_SYMBOL(xfrm_state_sort);
1451 #endif
1453 /* Silly enough, but I'm lazy to build resolution list */
1455 static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
1457 int i;
1459 for (i = 0; i <= net->xfrm.state_hmask; i++) {
1460 struct xfrm_state *x;
1462 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
1463 if (x->km.seq == seq &&
1464 (mark & x->mark.m) == x->mark.v &&
1465 x->km.state == XFRM_STATE_ACQ) {
1466 xfrm_state_hold(x);
1467 return x;
1471 return NULL;
1474 struct xfrm_state *xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
1476 struct xfrm_state *x;
1478 spin_lock_bh(&xfrm_state_lock);
1479 x = __xfrm_find_acq_byseq(net, mark, seq);
1480 spin_unlock_bh(&xfrm_state_lock);
1481 return x;
1483 EXPORT_SYMBOL(xfrm_find_acq_byseq);
1485 u32 xfrm_get_acqseq(void)
1487 u32 res;
1488 static atomic_t acqseq;
1490 do {
1491 res = atomic_inc_return(&acqseq);
1492 } while (!res);
1494 return res;
1496 EXPORT_SYMBOL(xfrm_get_acqseq);
1498 int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high)
1500 struct net *net = xs_net(x);
1501 unsigned int h;
1502 struct xfrm_state *x0;
1503 int err = -ENOENT;
1504 __be32 minspi = htonl(low);
1505 __be32 maxspi = htonl(high);
1506 u32 mark = x->mark.v & x->mark.m;
1508 spin_lock_bh(&x->lock);
1509 if (x->km.state == XFRM_STATE_DEAD)
1510 goto unlock;
1512 err = 0;
1513 if (x->id.spi)
1514 goto unlock;
1516 err = -ENOENT;
1518 if (minspi == maxspi) {
1519 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, minspi, x->id.proto, x->props.family);
1520 if (x0) {
1521 xfrm_state_put(x0);
1522 goto unlock;
1524 x->id.spi = minspi;
1525 } else {
1526 u32 spi = 0;
1527 for (h=0; h<high-low+1; h++) {
1528 spi = low + net_random()%(high-low+1);
1529 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, htonl(spi), x->id.proto, x->props.family);
1530 if (x0 == NULL) {
1531 x->id.spi = htonl(spi);
1532 break;
1534 xfrm_state_put(x0);
1537 if (x->id.spi) {
1538 spin_lock_bh(&xfrm_state_lock);
1539 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, x->props.family);
1540 hlist_add_head(&x->byspi, net->xfrm.state_byspi+h);
1541 spin_unlock_bh(&xfrm_state_lock);
1543 err = 0;
1546 unlock:
1547 spin_unlock_bh(&x->lock);
1549 return err;
1551 EXPORT_SYMBOL(xfrm_alloc_spi);
1553 int xfrm_state_walk(struct net *net, struct xfrm_state_walk *walk,
1554 int (*func)(struct xfrm_state *, int, void*),
1555 void *data)
1557 struct xfrm_state *state;
1558 struct xfrm_state_walk *x;
1559 int err = 0;
1561 if (walk->seq != 0 && list_empty(&walk->all))
1562 return 0;
1564 spin_lock_bh(&xfrm_state_lock);
1565 if (list_empty(&walk->all))
1566 x = list_first_entry(&net->xfrm.state_all, struct xfrm_state_walk, all);
1567 else
1568 x = list_entry(&walk->all, struct xfrm_state_walk, all);
1569 list_for_each_entry_from(x, &net->xfrm.state_all, all) {
1570 if (x->state == XFRM_STATE_DEAD)
1571 continue;
1572 state = container_of(x, struct xfrm_state, km);
1573 if (!xfrm_id_proto_match(state->id.proto, walk->proto))
1574 continue;
1575 err = func(state, walk->seq, data);
1576 if (err) {
1577 list_move_tail(&walk->all, &x->all);
1578 goto out;
1580 walk->seq++;
1582 if (walk->seq == 0) {
1583 err = -ENOENT;
1584 goto out;
1586 list_del_init(&walk->all);
1587 out:
1588 spin_unlock_bh(&xfrm_state_lock);
1589 return err;
1591 EXPORT_SYMBOL(xfrm_state_walk);
1593 void xfrm_state_walk_init(struct xfrm_state_walk *walk, u8 proto)
1595 INIT_LIST_HEAD(&walk->all);
1596 walk->proto = proto;
1597 walk->state = XFRM_STATE_DEAD;
1598 walk->seq = 0;
1600 EXPORT_SYMBOL(xfrm_state_walk_init);
1602 void xfrm_state_walk_done(struct xfrm_state_walk *walk)
1604 if (list_empty(&walk->all))
1605 return;
1607 spin_lock_bh(&xfrm_state_lock);
1608 list_del(&walk->all);
1609 spin_unlock_bh(&xfrm_state_lock);
1611 EXPORT_SYMBOL(xfrm_state_walk_done);
1613 static void xfrm_replay_timer_handler(unsigned long data)
1615 struct xfrm_state *x = (struct xfrm_state*)data;
1617 spin_lock(&x->lock);
1619 if (x->km.state == XFRM_STATE_VALID) {
1620 if (xfrm_aevent_is_on(xs_net(x)))
1621 x->repl->notify(x, XFRM_REPLAY_TIMEOUT);
1622 else
1623 x->xflags |= XFRM_TIME_DEFER;
1626 spin_unlock(&x->lock);
1629 static LIST_HEAD(xfrm_km_list);
1631 void km_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
1633 struct xfrm_mgr *km;
1635 rcu_read_lock();
1636 list_for_each_entry_rcu(km, &xfrm_km_list, list)
1637 if (km->notify_policy)
1638 km->notify_policy(xp, dir, c);
1639 rcu_read_unlock();
1642 void km_state_notify(struct xfrm_state *x, const struct km_event *c)
1644 struct xfrm_mgr *km;
1645 rcu_read_lock();
1646 list_for_each_entry_rcu(km, &xfrm_km_list, list)
1647 if (km->notify)
1648 km->notify(x, c);
1649 rcu_read_unlock();
1652 EXPORT_SYMBOL(km_policy_notify);
1653 EXPORT_SYMBOL(km_state_notify);
1655 void km_state_expired(struct xfrm_state *x, int hard, u32 portid)
1657 struct net *net = xs_net(x);
1658 struct km_event c;
1660 c.data.hard = hard;
1661 c.portid = portid;
1662 c.event = XFRM_MSG_EXPIRE;
1663 km_state_notify(x, &c);
1665 if (hard)
1666 wake_up(&net->xfrm.km_waitq);
1669 EXPORT_SYMBOL(km_state_expired);
1671 * We send to all registered managers regardless of failure
1672 * We are happy with one success
1674 int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol)
1676 int err = -EINVAL, acqret;
1677 struct xfrm_mgr *km;
1679 rcu_read_lock();
1680 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
1681 acqret = km->acquire(x, t, pol);
1682 if (!acqret)
1683 err = acqret;
1685 rcu_read_unlock();
1686 return err;
1688 EXPORT_SYMBOL(km_query);
1690 int km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
1692 int err = -EINVAL;
1693 struct xfrm_mgr *km;
1695 rcu_read_lock();
1696 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
1697 if (km->new_mapping)
1698 err = km->new_mapping(x, ipaddr, sport);
1699 if (!err)
1700 break;
1702 rcu_read_unlock();
1703 return err;
1705 EXPORT_SYMBOL(km_new_mapping);
1707 void km_policy_expired(struct xfrm_policy *pol, int dir, int hard, u32 portid)
1709 struct net *net = xp_net(pol);
1710 struct km_event c;
1712 c.data.hard = hard;
1713 c.portid = portid;
1714 c.event = XFRM_MSG_POLEXPIRE;
1715 km_policy_notify(pol, dir, &c);
1717 if (hard)
1718 wake_up(&net->xfrm.km_waitq);
1720 EXPORT_SYMBOL(km_policy_expired);
1722 #ifdef CONFIG_XFRM_MIGRATE
1723 int km_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
1724 const struct xfrm_migrate *m, int num_migrate,
1725 const struct xfrm_kmaddress *k)
1727 int err = -EINVAL;
1728 int ret;
1729 struct xfrm_mgr *km;
1731 rcu_read_lock();
1732 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
1733 if (km->migrate) {
1734 ret = km->migrate(sel, dir, type, m, num_migrate, k);
1735 if (!ret)
1736 err = ret;
1739 rcu_read_unlock();
1740 return err;
1742 EXPORT_SYMBOL(km_migrate);
1743 #endif
1745 int km_report(struct net *net, u8 proto, struct xfrm_selector *sel, xfrm_address_t *addr)
1747 int err = -EINVAL;
1748 int ret;
1749 struct xfrm_mgr *km;
1751 rcu_read_lock();
1752 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
1753 if (km->report) {
1754 ret = km->report(net, proto, sel, addr);
1755 if (!ret)
1756 err = ret;
1759 rcu_read_unlock();
1760 return err;
1762 EXPORT_SYMBOL(km_report);
1764 int xfrm_user_policy(struct sock *sk, int optname, u8 __user *optval, int optlen)
1766 int err;
1767 u8 *data;
1768 struct xfrm_mgr *km;
1769 struct xfrm_policy *pol = NULL;
1771 if (optlen <= 0 || optlen > PAGE_SIZE)
1772 return -EMSGSIZE;
1774 data = kmalloc(optlen, GFP_KERNEL);
1775 if (!data)
1776 return -ENOMEM;
1778 err = -EFAULT;
1779 if (copy_from_user(data, optval, optlen))
1780 goto out;
1782 err = -EINVAL;
1783 rcu_read_lock();
1784 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
1785 pol = km->compile_policy(sk, optname, data,
1786 optlen, &err);
1787 if (err >= 0)
1788 break;
1790 rcu_read_unlock();
1792 if (err >= 0) {
1793 xfrm_sk_policy_insert(sk, err, pol);
1794 xfrm_pol_put(pol);
1795 err = 0;
1798 out:
1799 kfree(data);
1800 return err;
1802 EXPORT_SYMBOL(xfrm_user_policy);
1804 static DEFINE_SPINLOCK(xfrm_km_lock);
1806 int xfrm_register_km(struct xfrm_mgr *km)
1808 spin_lock_bh(&xfrm_km_lock);
1809 list_add_tail_rcu(&km->list, &xfrm_km_list);
1810 spin_unlock_bh(&xfrm_km_lock);
1811 return 0;
1813 EXPORT_SYMBOL(xfrm_register_km);
1815 int xfrm_unregister_km(struct xfrm_mgr *km)
1817 spin_lock_bh(&xfrm_km_lock);
1818 list_del_rcu(&km->list);
1819 spin_unlock_bh(&xfrm_km_lock);
1820 synchronize_rcu();
1821 return 0;
1823 EXPORT_SYMBOL(xfrm_unregister_km);
1825 int xfrm_state_register_afinfo(struct xfrm_state_afinfo *afinfo)
1827 int err = 0;
1828 if (unlikely(afinfo == NULL))
1829 return -EINVAL;
1830 if (unlikely(afinfo->family >= NPROTO))
1831 return -EAFNOSUPPORT;
1832 spin_lock_bh(&xfrm_state_afinfo_lock);
1833 if (unlikely(xfrm_state_afinfo[afinfo->family] != NULL))
1834 err = -ENOBUFS;
1835 else
1836 rcu_assign_pointer(xfrm_state_afinfo[afinfo->family], afinfo);
1837 spin_unlock_bh(&xfrm_state_afinfo_lock);
1838 return err;
1840 EXPORT_SYMBOL(xfrm_state_register_afinfo);
1842 int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo)
1844 int err = 0;
1845 if (unlikely(afinfo == NULL))
1846 return -EINVAL;
1847 if (unlikely(afinfo->family >= NPROTO))
1848 return -EAFNOSUPPORT;
1849 spin_lock_bh(&xfrm_state_afinfo_lock);
1850 if (likely(xfrm_state_afinfo[afinfo->family] != NULL)) {
1851 if (unlikely(xfrm_state_afinfo[afinfo->family] != afinfo))
1852 err = -EINVAL;
1853 else
1854 RCU_INIT_POINTER(xfrm_state_afinfo[afinfo->family], NULL);
1856 spin_unlock_bh(&xfrm_state_afinfo_lock);
1857 synchronize_rcu();
1858 return err;
1860 EXPORT_SYMBOL(xfrm_state_unregister_afinfo);
1862 static struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family)
1864 struct xfrm_state_afinfo *afinfo;
1865 if (unlikely(family >= NPROTO))
1866 return NULL;
1867 rcu_read_lock();
1868 afinfo = rcu_dereference(xfrm_state_afinfo[family]);
1869 if (unlikely(!afinfo))
1870 rcu_read_unlock();
1871 return afinfo;
1874 static void xfrm_state_put_afinfo(struct xfrm_state_afinfo *afinfo)
1876 rcu_read_unlock();
1879 /* Temporarily located here until net/xfrm/xfrm_tunnel.c is created */
1880 void xfrm_state_delete_tunnel(struct xfrm_state *x)
1882 if (x->tunnel) {
1883 struct xfrm_state *t = x->tunnel;
1885 if (atomic_read(&t->tunnel_users) == 2)
1886 xfrm_state_delete(t);
1887 atomic_dec(&t->tunnel_users);
1888 xfrm_state_put(t);
1889 x->tunnel = NULL;
1892 EXPORT_SYMBOL(xfrm_state_delete_tunnel);
1894 int xfrm_state_mtu(struct xfrm_state *x, int mtu)
1896 int res;
1898 spin_lock_bh(&x->lock);
1899 if (x->km.state == XFRM_STATE_VALID &&
1900 x->type && x->type->get_mtu)
1901 res = x->type->get_mtu(x, mtu);
1902 else
1903 res = mtu - x->props.header_len;
1904 spin_unlock_bh(&x->lock);
1905 return res;
1908 int __xfrm_init_state(struct xfrm_state *x, bool init_replay)
1910 struct xfrm_state_afinfo *afinfo;
1911 struct xfrm_mode *inner_mode;
1912 int family = x->props.family;
1913 int err;
1915 err = -EAFNOSUPPORT;
1916 afinfo = xfrm_state_get_afinfo(family);
1917 if (!afinfo)
1918 goto error;
1920 err = 0;
1921 if (afinfo->init_flags)
1922 err = afinfo->init_flags(x);
1924 xfrm_state_put_afinfo(afinfo);
1926 if (err)
1927 goto error;
1929 err = -EPROTONOSUPPORT;
1931 if (x->sel.family != AF_UNSPEC) {
1932 inner_mode = xfrm_get_mode(x->props.mode, x->sel.family);
1933 if (inner_mode == NULL)
1934 goto error;
1936 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL) &&
1937 family != x->sel.family) {
1938 xfrm_put_mode(inner_mode);
1939 goto error;
1942 x->inner_mode = inner_mode;
1943 } else {
1944 struct xfrm_mode *inner_mode_iaf;
1945 int iafamily = AF_INET;
1947 inner_mode = xfrm_get_mode(x->props.mode, x->props.family);
1948 if (inner_mode == NULL)
1949 goto error;
1951 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL)) {
1952 xfrm_put_mode(inner_mode);
1953 goto error;
1955 x->inner_mode = inner_mode;
1957 if (x->props.family == AF_INET)
1958 iafamily = AF_INET6;
1960 inner_mode_iaf = xfrm_get_mode(x->props.mode, iafamily);
1961 if (inner_mode_iaf) {
1962 if (inner_mode_iaf->flags & XFRM_MODE_FLAG_TUNNEL)
1963 x->inner_mode_iaf = inner_mode_iaf;
1964 else
1965 xfrm_put_mode(inner_mode_iaf);
1969 x->type = xfrm_get_type(x->id.proto, family);
1970 if (x->type == NULL)
1971 goto error;
1973 err = x->type->init_state(x);
1974 if (err)
1975 goto error;
1977 x->outer_mode = xfrm_get_mode(x->props.mode, family);
1978 if (x->outer_mode == NULL) {
1979 err = -EPROTONOSUPPORT;
1980 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 = INVALID_UID;
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 const struct iphdr *iph4;
2090 const 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 kuid_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 kuid_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 */