Revert "x86: disable IOMMUs on kernel crash"
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / xfrm / xfrm_state.c
blobf445ea1c5f52b46392a2860c4381d626da893ff3
1 /*
2 * xfrm_state.c
4 * Changes:
5 * Mitsuru KANDA @USAGI
6 * Kazunori MIYAZAWA @USAGI
7 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
8 * IPv6 support
9 * YOSHIFUJI Hideaki @USAGI
10 * Split up af-specific functions
11 * Derek Atkins <derek@ihtfp.com>
12 * Add UDP Encapsulation
16 #include <linux/workqueue.h>
17 #include <net/xfrm.h>
18 #include <linux/pfkeyv2.h>
19 #include <linux/ipsec.h>
20 #include <linux/module.h>
21 #include <linux/cache.h>
22 #include <linux/audit.h>
23 #include <asm/uaccess.h>
24 #include <linux/ktime.h>
25 #include <linux/interrupt.h>
26 #include <linux/kernel.h>
28 #include "xfrm_hash.h"
30 /* Each xfrm_state may be linked to two tables:
32 1. Hash table by (spi,daddr,ah/esp) to find SA by SPI. (input,ctl)
33 2. Hash table by (daddr,family,reqid) to find what SAs exist for given
34 destination/tunnel endpoint. (output)
37 static DEFINE_SPINLOCK(xfrm_state_lock);
39 static unsigned int xfrm_state_hashmax __read_mostly = 1 * 1024 * 1024;
40 static unsigned int xfrm_state_genid;
42 static struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family);
43 static void xfrm_state_put_afinfo(struct xfrm_state_afinfo *afinfo);
45 #ifdef CONFIG_AUDITSYSCALL
46 static void xfrm_audit_state_replay(struct xfrm_state *x,
47 struct sk_buff *skb, __be32 net_seq);
48 #else
49 #define xfrm_audit_state_replay(x, s, sq) do { ; } while (0)
50 #endif /* CONFIG_AUDITSYSCALL */
52 static inline unsigned int xfrm_dst_hash(struct net *net,
53 xfrm_address_t *daddr,
54 xfrm_address_t *saddr,
55 u32 reqid,
56 unsigned short family)
58 return __xfrm_dst_hash(daddr, saddr, reqid, family, net->xfrm.state_hmask);
61 static inline unsigned int xfrm_src_hash(struct net *net,
62 xfrm_address_t *daddr,
63 xfrm_address_t *saddr,
64 unsigned short family)
66 return __xfrm_src_hash(daddr, saddr, family, net->xfrm.state_hmask);
69 static inline unsigned int
70 xfrm_spi_hash(struct net *net, xfrm_address_t *daddr, __be32 spi, u8 proto, unsigned short family)
72 return __xfrm_spi_hash(daddr, spi, proto, family, net->xfrm.state_hmask);
75 static void xfrm_hash_transfer(struct hlist_head *list,
76 struct hlist_head *ndsttable,
77 struct hlist_head *nsrctable,
78 struct hlist_head *nspitable,
79 unsigned int nhashmask)
81 struct hlist_node *entry, *tmp;
82 struct xfrm_state *x;
84 hlist_for_each_entry_safe(x, entry, tmp, list, bydst) {
85 unsigned int h;
87 h = __xfrm_dst_hash(&x->id.daddr, &x->props.saddr,
88 x->props.reqid, x->props.family,
89 nhashmask);
90 hlist_add_head(&x->bydst, ndsttable+h);
92 h = __xfrm_src_hash(&x->id.daddr, &x->props.saddr,
93 x->props.family,
94 nhashmask);
95 hlist_add_head(&x->bysrc, nsrctable+h);
97 if (x->id.spi) {
98 h = __xfrm_spi_hash(&x->id.daddr, x->id.spi,
99 x->id.proto, x->props.family,
100 nhashmask);
101 hlist_add_head(&x->byspi, nspitable+h);
106 static unsigned long xfrm_hash_new_size(unsigned int state_hmask)
108 return ((state_hmask + 1) << 1) * sizeof(struct hlist_head);
111 static DEFINE_MUTEX(hash_resize_mutex);
113 static void xfrm_hash_resize(struct work_struct *work)
115 struct net *net = container_of(work, struct net, xfrm.state_hash_work);
116 struct hlist_head *ndst, *nsrc, *nspi, *odst, *osrc, *ospi;
117 unsigned long nsize, osize;
118 unsigned int nhashmask, ohashmask;
119 int i;
121 mutex_lock(&hash_resize_mutex);
123 nsize = xfrm_hash_new_size(net->xfrm.state_hmask);
124 ndst = xfrm_hash_alloc(nsize);
125 if (!ndst)
126 goto out_unlock;
127 nsrc = xfrm_hash_alloc(nsize);
128 if (!nsrc) {
129 xfrm_hash_free(ndst, nsize);
130 goto out_unlock;
132 nspi = xfrm_hash_alloc(nsize);
133 if (!nspi) {
134 xfrm_hash_free(ndst, nsize);
135 xfrm_hash_free(nsrc, nsize);
136 goto out_unlock;
139 spin_lock_bh(&xfrm_state_lock);
141 nhashmask = (nsize / sizeof(struct hlist_head)) - 1U;
142 for (i = net->xfrm.state_hmask; i >= 0; i--)
143 xfrm_hash_transfer(net->xfrm.state_bydst+i, ndst, nsrc, nspi,
144 nhashmask);
146 odst = net->xfrm.state_bydst;
147 osrc = net->xfrm.state_bysrc;
148 ospi = net->xfrm.state_byspi;
149 ohashmask = net->xfrm.state_hmask;
151 net->xfrm.state_bydst = ndst;
152 net->xfrm.state_bysrc = nsrc;
153 net->xfrm.state_byspi = nspi;
154 net->xfrm.state_hmask = nhashmask;
156 spin_unlock_bh(&xfrm_state_lock);
158 osize = (ohashmask + 1) * sizeof(struct hlist_head);
159 xfrm_hash_free(odst, osize);
160 xfrm_hash_free(osrc, osize);
161 xfrm_hash_free(ospi, osize);
163 out_unlock:
164 mutex_unlock(&hash_resize_mutex);
167 static DEFINE_RWLOCK(xfrm_state_afinfo_lock);
168 static struct xfrm_state_afinfo *xfrm_state_afinfo[NPROTO];
170 static DEFINE_SPINLOCK(xfrm_state_gc_lock);
172 int __xfrm_state_delete(struct xfrm_state *x);
174 int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol);
175 void km_state_expired(struct xfrm_state *x, int hard, u32 pid);
177 static struct xfrm_state_afinfo *xfrm_state_lock_afinfo(unsigned int family)
179 struct xfrm_state_afinfo *afinfo;
180 if (unlikely(family >= NPROTO))
181 return NULL;
182 write_lock_bh(&xfrm_state_afinfo_lock);
183 afinfo = xfrm_state_afinfo[family];
184 if (unlikely(!afinfo))
185 write_unlock_bh(&xfrm_state_afinfo_lock);
186 return afinfo;
189 static void xfrm_state_unlock_afinfo(struct xfrm_state_afinfo *afinfo)
190 __releases(xfrm_state_afinfo_lock)
192 write_unlock_bh(&xfrm_state_afinfo_lock);
195 int xfrm_register_type(const struct xfrm_type *type, unsigned short family)
197 struct xfrm_state_afinfo *afinfo = xfrm_state_lock_afinfo(family);
198 const struct xfrm_type **typemap;
199 int err = 0;
201 if (unlikely(afinfo == NULL))
202 return -EAFNOSUPPORT;
203 typemap = afinfo->type_map;
205 if (likely(typemap[type->proto] == NULL))
206 typemap[type->proto] = type;
207 else
208 err = -EEXIST;
209 xfrm_state_unlock_afinfo(afinfo);
210 return err;
212 EXPORT_SYMBOL(xfrm_register_type);
214 int xfrm_unregister_type(const struct xfrm_type *type, unsigned short family)
216 struct xfrm_state_afinfo *afinfo = xfrm_state_lock_afinfo(family);
217 const struct xfrm_type **typemap;
218 int err = 0;
220 if (unlikely(afinfo == NULL))
221 return -EAFNOSUPPORT;
222 typemap = afinfo->type_map;
224 if (unlikely(typemap[type->proto] != type))
225 err = -ENOENT;
226 else
227 typemap[type->proto] = NULL;
228 xfrm_state_unlock_afinfo(afinfo);
229 return err;
231 EXPORT_SYMBOL(xfrm_unregister_type);
233 static const struct xfrm_type *xfrm_get_type(u8 proto, unsigned short family)
235 struct xfrm_state_afinfo *afinfo;
236 const struct xfrm_type **typemap;
237 const struct xfrm_type *type;
238 int modload_attempted = 0;
240 retry:
241 afinfo = xfrm_state_get_afinfo(family);
242 if (unlikely(afinfo == NULL))
243 return NULL;
244 typemap = afinfo->type_map;
246 type = typemap[proto];
247 if (unlikely(type && !try_module_get(type->owner)))
248 type = NULL;
249 if (!type && !modload_attempted) {
250 xfrm_state_put_afinfo(afinfo);
251 request_module("xfrm-type-%d-%d", family, proto);
252 modload_attempted = 1;
253 goto retry;
256 xfrm_state_put_afinfo(afinfo);
257 return type;
260 static void xfrm_put_type(const struct xfrm_type *type)
262 module_put(type->owner);
265 int xfrm_register_mode(struct xfrm_mode *mode, int family)
267 struct xfrm_state_afinfo *afinfo;
268 struct xfrm_mode **modemap;
269 int err;
271 if (unlikely(mode->encap >= XFRM_MODE_MAX))
272 return -EINVAL;
274 afinfo = xfrm_state_lock_afinfo(family);
275 if (unlikely(afinfo == NULL))
276 return -EAFNOSUPPORT;
278 err = -EEXIST;
279 modemap = afinfo->mode_map;
280 if (modemap[mode->encap])
281 goto out;
283 err = -ENOENT;
284 if (!try_module_get(afinfo->owner))
285 goto out;
287 mode->afinfo = afinfo;
288 modemap[mode->encap] = mode;
289 err = 0;
291 out:
292 xfrm_state_unlock_afinfo(afinfo);
293 return err;
295 EXPORT_SYMBOL(xfrm_register_mode);
297 int xfrm_unregister_mode(struct xfrm_mode *mode, int family)
299 struct xfrm_state_afinfo *afinfo;
300 struct xfrm_mode **modemap;
301 int err;
303 if (unlikely(mode->encap >= XFRM_MODE_MAX))
304 return -EINVAL;
306 afinfo = xfrm_state_lock_afinfo(family);
307 if (unlikely(afinfo == NULL))
308 return -EAFNOSUPPORT;
310 err = -ENOENT;
311 modemap = afinfo->mode_map;
312 if (likely(modemap[mode->encap] == mode)) {
313 modemap[mode->encap] = NULL;
314 module_put(mode->afinfo->owner);
315 err = 0;
318 xfrm_state_unlock_afinfo(afinfo);
319 return err;
321 EXPORT_SYMBOL(xfrm_unregister_mode);
323 static struct xfrm_mode *xfrm_get_mode(unsigned int encap, int family)
325 struct xfrm_state_afinfo *afinfo;
326 struct xfrm_mode *mode;
327 int modload_attempted = 0;
329 if (unlikely(encap >= XFRM_MODE_MAX))
330 return NULL;
332 retry:
333 afinfo = xfrm_state_get_afinfo(family);
334 if (unlikely(afinfo == NULL))
335 return NULL;
337 mode = afinfo->mode_map[encap];
338 if (unlikely(mode && !try_module_get(mode->owner)))
339 mode = NULL;
340 if (!mode && !modload_attempted) {
341 xfrm_state_put_afinfo(afinfo);
342 request_module("xfrm-mode-%d-%d", family, encap);
343 modload_attempted = 1;
344 goto retry;
347 xfrm_state_put_afinfo(afinfo);
348 return mode;
351 static void xfrm_put_mode(struct xfrm_mode *mode)
353 module_put(mode->owner);
356 static void xfrm_state_gc_destroy(struct xfrm_state *x)
358 tasklet_hrtimer_cancel(&x->mtimer);
359 del_timer_sync(&x->rtimer);
360 kfree(x->aalg);
361 kfree(x->ealg);
362 kfree(x->calg);
363 kfree(x->encap);
364 kfree(x->coaddr);
365 if (x->inner_mode)
366 xfrm_put_mode(x->inner_mode);
367 if (x->inner_mode_iaf)
368 xfrm_put_mode(x->inner_mode_iaf);
369 if (x->outer_mode)
370 xfrm_put_mode(x->outer_mode);
371 if (x->type) {
372 x->type->destructor(x);
373 xfrm_put_type(x->type);
375 security_xfrm_state_free(x);
376 kfree(x);
379 static void xfrm_state_gc_task(struct work_struct *work)
381 struct net *net = container_of(work, struct net, xfrm.state_gc_work);
382 struct xfrm_state *x;
383 struct hlist_node *entry, *tmp;
384 struct hlist_head gc_list;
386 spin_lock_bh(&xfrm_state_gc_lock);
387 hlist_move_list(&net->xfrm.state_gc_list, &gc_list);
388 spin_unlock_bh(&xfrm_state_gc_lock);
390 hlist_for_each_entry_safe(x, entry, tmp, &gc_list, gclist)
391 xfrm_state_gc_destroy(x);
393 wake_up(&net->xfrm.km_waitq);
396 static inline unsigned long make_jiffies(long secs)
398 if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
399 return MAX_SCHEDULE_TIMEOUT-1;
400 else
401 return secs*HZ;
404 static enum hrtimer_restart xfrm_timer_handler(struct hrtimer * me)
406 struct tasklet_hrtimer *thr = container_of(me, struct tasklet_hrtimer, timer);
407 struct xfrm_state *x = container_of(thr, struct xfrm_state, mtimer);
408 struct net *net = xs_net(x);
409 unsigned long now = get_seconds();
410 long next = LONG_MAX;
411 int warn = 0;
412 int err = 0;
414 spin_lock(&x->lock);
415 if (x->km.state == XFRM_STATE_DEAD)
416 goto out;
417 if (x->km.state == XFRM_STATE_EXPIRED)
418 goto expired;
419 if (x->lft.hard_add_expires_seconds) {
420 long tmo = x->lft.hard_add_expires_seconds +
421 x->curlft.add_time - now;
422 if (tmo <= 0)
423 goto expired;
424 if (tmo < next)
425 next = tmo;
427 if (x->lft.hard_use_expires_seconds) {
428 long tmo = x->lft.hard_use_expires_seconds +
429 (x->curlft.use_time ? : now) - now;
430 if (tmo <= 0)
431 goto expired;
432 if (tmo < next)
433 next = tmo;
435 if (x->km.dying)
436 goto resched;
437 if (x->lft.soft_add_expires_seconds) {
438 long tmo = x->lft.soft_add_expires_seconds +
439 x->curlft.add_time - now;
440 if (tmo <= 0)
441 warn = 1;
442 else if (tmo < next)
443 next = tmo;
445 if (x->lft.soft_use_expires_seconds) {
446 long tmo = x->lft.soft_use_expires_seconds +
447 (x->curlft.use_time ? : now) - now;
448 if (tmo <= 0)
449 warn = 1;
450 else if (tmo < next)
451 next = tmo;
454 x->km.dying = warn;
455 if (warn)
456 km_state_expired(x, 0, 0);
457 resched:
458 if (next != LONG_MAX){
459 tasklet_hrtimer_start(&x->mtimer, ktime_set(next, 0), HRTIMER_MODE_REL);
462 goto out;
464 expired:
465 if (x->km.state == XFRM_STATE_ACQ && x->id.spi == 0) {
466 x->km.state = XFRM_STATE_EXPIRED;
467 wake_up(&net->xfrm.km_waitq);
468 next = 2;
469 goto resched;
472 err = __xfrm_state_delete(x);
473 if (!err && x->id.spi)
474 km_state_expired(x, 1, 0);
476 xfrm_audit_state_delete(x, err ? 0 : 1,
477 audit_get_loginuid(current),
478 audit_get_sessionid(current), 0);
480 out:
481 spin_unlock(&x->lock);
482 return HRTIMER_NORESTART;
485 static void xfrm_replay_timer_handler(unsigned long data);
487 struct xfrm_state *xfrm_state_alloc(struct net *net)
489 struct xfrm_state *x;
491 x = kzalloc(sizeof(struct xfrm_state), GFP_ATOMIC);
493 if (x) {
494 write_pnet(&x->xs_net, net);
495 atomic_set(&x->refcnt, 1);
496 atomic_set(&x->tunnel_users, 0);
497 INIT_LIST_HEAD(&x->km.all);
498 INIT_HLIST_NODE(&x->bydst);
499 INIT_HLIST_NODE(&x->bysrc);
500 INIT_HLIST_NODE(&x->byspi);
501 tasklet_hrtimer_init(&x->mtimer, xfrm_timer_handler, CLOCK_REALTIME, HRTIMER_MODE_ABS);
502 setup_timer(&x->rtimer, xfrm_replay_timer_handler,
503 (unsigned long)x);
504 x->curlft.add_time = get_seconds();
505 x->lft.soft_byte_limit = XFRM_INF;
506 x->lft.soft_packet_limit = XFRM_INF;
507 x->lft.hard_byte_limit = XFRM_INF;
508 x->lft.hard_packet_limit = XFRM_INF;
509 x->replay_maxage = 0;
510 x->replay_maxdiff = 0;
511 x->inner_mode = NULL;
512 x->inner_mode_iaf = NULL;
513 spin_lock_init(&x->lock);
515 return x;
517 EXPORT_SYMBOL(xfrm_state_alloc);
519 void __xfrm_state_destroy(struct xfrm_state *x)
521 struct net *net = xs_net(x);
523 WARN_ON(x->km.state != XFRM_STATE_DEAD);
525 spin_lock_bh(&xfrm_state_gc_lock);
526 hlist_add_head(&x->gclist, &net->xfrm.state_gc_list);
527 spin_unlock_bh(&xfrm_state_gc_lock);
528 schedule_work(&net->xfrm.state_gc_work);
530 EXPORT_SYMBOL(__xfrm_state_destroy);
532 int __xfrm_state_delete(struct xfrm_state *x)
534 struct net *net = xs_net(x);
535 int err = -ESRCH;
537 if (x->km.state != XFRM_STATE_DEAD) {
538 x->km.state = XFRM_STATE_DEAD;
539 spin_lock(&xfrm_state_lock);
540 list_del(&x->km.all);
541 hlist_del(&x->bydst);
542 hlist_del(&x->bysrc);
543 if (x->id.spi)
544 hlist_del(&x->byspi);
545 net->xfrm.state_num--;
546 spin_unlock(&xfrm_state_lock);
548 /* All xfrm_state objects are created by xfrm_state_alloc.
549 * The xfrm_state_alloc call gives a reference, and that
550 * is what we are dropping here.
552 xfrm_state_put(x);
553 err = 0;
556 return err;
558 EXPORT_SYMBOL(__xfrm_state_delete);
560 int xfrm_state_delete(struct xfrm_state *x)
562 int err;
564 spin_lock_bh(&x->lock);
565 err = __xfrm_state_delete(x);
566 spin_unlock_bh(&x->lock);
568 return err;
570 EXPORT_SYMBOL(xfrm_state_delete);
572 #ifdef CONFIG_SECURITY_NETWORK_XFRM
573 static inline int
574 xfrm_state_flush_secctx_check(struct net *net, u8 proto, struct xfrm_audit *audit_info)
576 int i, err = 0;
578 for (i = 0; i <= net->xfrm.state_hmask; i++) {
579 struct hlist_node *entry;
580 struct xfrm_state *x;
582 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+i, bydst) {
583 if (xfrm_id_proto_match(x->id.proto, proto) &&
584 (err = security_xfrm_state_delete(x)) != 0) {
585 xfrm_audit_state_delete(x, 0,
586 audit_info->loginuid,
587 audit_info->sessionid,
588 audit_info->secid);
589 return err;
594 return err;
596 #else
597 static inline int
598 xfrm_state_flush_secctx_check(struct net *net, u8 proto, struct xfrm_audit *audit_info)
600 return 0;
602 #endif
604 int xfrm_state_flush(struct net *net, u8 proto, struct xfrm_audit *audit_info)
606 int i, err = 0;
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 for (i = 0; i <= net->xfrm.state_hmask; i++) {
614 struct hlist_node *entry;
615 struct xfrm_state *x;
616 restart:
617 hlist_for_each_entry(x, entry, 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);
630 spin_lock_bh(&xfrm_state_lock);
631 goto restart;
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_tempsel(struct xfrm_state *x, struct flowi *fl,
656 struct xfrm_tmpl *tmpl,
657 xfrm_address_t *daddr, 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, fl, tmpl, daddr, saddr);
664 xfrm_state_put_afinfo(afinfo);
665 return 0;
668 static struct xfrm_state *__xfrm_state_lookup(struct net *net, xfrm_address_t *daddr, __be32 spi, u8 proto, unsigned short family)
670 unsigned int h = xfrm_spi_hash(net, daddr, spi, proto, family);
671 struct xfrm_state *x;
672 struct hlist_node *entry;
674 hlist_for_each_entry(x, entry, net->xfrm.state_byspi+h, byspi) {
675 if (x->props.family != family ||
676 x->id.spi != spi ||
677 x->id.proto != proto ||
678 xfrm_addr_cmp(&x->id.daddr, daddr, family))
679 continue;
681 xfrm_state_hold(x);
682 return x;
685 return NULL;
688 static struct xfrm_state *__xfrm_state_lookup_byaddr(struct net *net, xfrm_address_t *daddr, xfrm_address_t *saddr, u8 proto, unsigned short family)
690 unsigned int h = xfrm_src_hash(net, daddr, saddr, family);
691 struct xfrm_state *x;
692 struct hlist_node *entry;
694 hlist_for_each_entry(x, entry, net->xfrm.state_bysrc+h, bysrc) {
695 if (x->props.family != family ||
696 x->id.proto != proto ||
697 xfrm_addr_cmp(&x->id.daddr, daddr, family) ||
698 xfrm_addr_cmp(&x->props.saddr, saddr, family))
699 continue;
701 xfrm_state_hold(x);
702 return x;
705 return NULL;
708 static inline struct xfrm_state *
709 __xfrm_state_locate(struct xfrm_state *x, int use_spi, int family)
711 struct net *net = xs_net(x);
713 if (use_spi)
714 return __xfrm_state_lookup(net, &x->id.daddr, x->id.spi,
715 x->id.proto, family);
716 else
717 return __xfrm_state_lookup_byaddr(net, &x->id.daddr,
718 &x->props.saddr,
719 x->id.proto, family);
722 static void xfrm_hash_grow_check(struct net *net, int have_hash_collision)
724 if (have_hash_collision &&
725 (net->xfrm.state_hmask + 1) < xfrm_state_hashmax &&
726 net->xfrm.state_num > net->xfrm.state_hmask)
727 schedule_work(&net->xfrm.state_hash_work);
730 static void xfrm_state_look_at(struct xfrm_policy *pol, struct xfrm_state *x,
731 struct flowi *fl, unsigned short family,
732 xfrm_address_t *daddr, xfrm_address_t *saddr,
733 struct xfrm_state **best, int *acq_in_progress,
734 int *error)
736 /* Resolution logic:
737 * 1. There is a valid state with matching selector. Done.
738 * 2. Valid state with inappropriate selector. Skip.
740 * Entering area of "sysdeps".
742 * 3. If state is not valid, selector is temporary, it selects
743 * only session which triggered previous resolution. Key
744 * manager will do something to install a state with proper
745 * selector.
747 if (x->km.state == XFRM_STATE_VALID) {
748 if ((x->sel.family &&
749 !xfrm_selector_match(&x->sel, fl, x->sel.family)) ||
750 !security_xfrm_state_pol_flow_match(x, pol, fl))
751 return;
753 if (!*best ||
754 (*best)->km.dying > x->km.dying ||
755 ((*best)->km.dying == x->km.dying &&
756 (*best)->curlft.add_time < x->curlft.add_time))
757 *best = x;
758 } else if (x->km.state == XFRM_STATE_ACQ) {
759 *acq_in_progress = 1;
760 } else if (x->km.state == XFRM_STATE_ERROR ||
761 x->km.state == XFRM_STATE_EXPIRED) {
762 if (xfrm_selector_match(&x->sel, fl, x->sel.family) &&
763 security_xfrm_state_pol_flow_match(x, pol, fl))
764 *error = -ESRCH;
768 struct xfrm_state *
769 xfrm_state_find(xfrm_address_t *daddr, xfrm_address_t *saddr,
770 struct flowi *fl, struct xfrm_tmpl *tmpl,
771 struct xfrm_policy *pol, int *err,
772 unsigned short family)
774 static xfrm_address_t saddr_wildcard = { };
775 struct net *net = xp_net(pol);
776 unsigned int h, h_wildcard;
777 struct hlist_node *entry;
778 struct xfrm_state *x, *x0, *to_put;
779 int acquire_in_progress = 0;
780 int error = 0;
781 struct xfrm_state *best = NULL;
783 to_put = NULL;
785 spin_lock_bh(&xfrm_state_lock);
786 h = xfrm_dst_hash(net, daddr, saddr, tmpl->reqid, family);
787 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+h, bydst) {
788 if (x->props.family == family &&
789 x->props.reqid == tmpl->reqid &&
790 !(x->props.flags & XFRM_STATE_WILDRECV) &&
791 xfrm_state_addr_check(x, daddr, saddr, family) &&
792 tmpl->mode == x->props.mode &&
793 tmpl->id.proto == x->id.proto &&
794 (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
795 xfrm_state_look_at(pol, x, fl, family, daddr, saddr,
796 &best, &acquire_in_progress, &error);
798 if (best)
799 goto found;
801 h_wildcard = xfrm_dst_hash(net, daddr, &saddr_wildcard, tmpl->reqid, family);
802 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+h_wildcard, bydst) {
803 if (x->props.family == family &&
804 x->props.reqid == tmpl->reqid &&
805 !(x->props.flags & XFRM_STATE_WILDRECV) &&
806 xfrm_state_addr_check(x, daddr, saddr, family) &&
807 tmpl->mode == x->props.mode &&
808 tmpl->id.proto == x->id.proto &&
809 (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
810 xfrm_state_look_at(pol, x, fl, family, daddr, saddr,
811 &best, &acquire_in_progress, &error);
814 found:
815 x = best;
816 if (!x && !error && !acquire_in_progress) {
817 if (tmpl->id.spi &&
818 (x0 = __xfrm_state_lookup(net, daddr, tmpl->id.spi,
819 tmpl->id.proto, family)) != NULL) {
820 to_put = x0;
821 error = -EEXIST;
822 goto out;
824 x = xfrm_state_alloc(net);
825 if (x == NULL) {
826 error = -ENOMEM;
827 goto out;
829 /* Initialize temporary selector matching only
830 * to current session. */
831 xfrm_init_tempsel(x, fl, tmpl, daddr, saddr, family);
833 error = security_xfrm_state_alloc_acquire(x, pol->security, fl->secid);
834 if (error) {
835 x->km.state = XFRM_STATE_DEAD;
836 to_put = x;
837 x = NULL;
838 goto out;
841 if (km_query(x, tmpl, pol) == 0) {
842 x->km.state = XFRM_STATE_ACQ;
843 list_add(&x->km.all, &net->xfrm.state_all);
844 hlist_add_head(&x->bydst, net->xfrm.state_bydst+h);
845 h = xfrm_src_hash(net, daddr, saddr, family);
846 hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h);
847 if (x->id.spi) {
848 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, family);
849 hlist_add_head(&x->byspi, net->xfrm.state_byspi+h);
851 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
852 tasklet_hrtimer_start(&x->mtimer, ktime_set(net->xfrm.sysctl_acq_expires, 0), HRTIMER_MODE_REL);
853 net->xfrm.state_num++;
854 xfrm_hash_grow_check(net, x->bydst.next != NULL);
855 } else {
856 x->km.state = XFRM_STATE_DEAD;
857 to_put = x;
858 x = NULL;
859 error = -ESRCH;
862 out:
863 if (x)
864 xfrm_state_hold(x);
865 else
866 *err = acquire_in_progress ? -EAGAIN : error;
867 spin_unlock_bh(&xfrm_state_lock);
868 if (to_put)
869 xfrm_state_put(to_put);
870 return x;
873 struct xfrm_state *
874 xfrm_stateonly_find(struct net *net,
875 xfrm_address_t *daddr, xfrm_address_t *saddr,
876 unsigned short family, u8 mode, u8 proto, u32 reqid)
878 unsigned int h;
879 struct xfrm_state *rx = NULL, *x = NULL;
880 struct hlist_node *entry;
882 spin_lock(&xfrm_state_lock);
883 h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
884 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+h, bydst) {
885 if (x->props.family == family &&
886 x->props.reqid == reqid &&
887 !(x->props.flags & XFRM_STATE_WILDRECV) &&
888 xfrm_state_addr_check(x, daddr, saddr, family) &&
889 mode == x->props.mode &&
890 proto == x->id.proto &&
891 x->km.state == XFRM_STATE_VALID) {
892 rx = x;
893 break;
897 if (rx)
898 xfrm_state_hold(rx);
899 spin_unlock(&xfrm_state_lock);
902 return rx;
904 EXPORT_SYMBOL(xfrm_stateonly_find);
906 static void __xfrm_state_insert(struct xfrm_state *x)
908 struct net *net = xs_net(x);
909 unsigned int h;
911 x->genid = ++xfrm_state_genid;
913 list_add(&x->km.all, &net->xfrm.state_all);
915 h = xfrm_dst_hash(net, &x->id.daddr, &x->props.saddr,
916 x->props.reqid, x->props.family);
917 hlist_add_head(&x->bydst, net->xfrm.state_bydst+h);
919 h = xfrm_src_hash(net, &x->id.daddr, &x->props.saddr, x->props.family);
920 hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h);
922 if (x->id.spi) {
923 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto,
924 x->props.family);
926 hlist_add_head(&x->byspi, net->xfrm.state_byspi+h);
929 tasklet_hrtimer_start(&x->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL);
930 if (x->replay_maxage)
931 mod_timer(&x->rtimer, jiffies + x->replay_maxage);
933 wake_up(&net->xfrm.km_waitq);
935 net->xfrm.state_num++;
937 xfrm_hash_grow_check(net, x->bydst.next != NULL);
940 /* xfrm_state_lock is held */
941 static void __xfrm_state_bump_genids(struct xfrm_state *xnew)
943 struct net *net = xs_net(xnew);
944 unsigned short family = xnew->props.family;
945 u32 reqid = xnew->props.reqid;
946 struct xfrm_state *x;
947 struct hlist_node *entry;
948 unsigned int h;
950 h = xfrm_dst_hash(net, &xnew->id.daddr, &xnew->props.saddr, reqid, family);
951 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+h, bydst) {
952 if (x->props.family == family &&
953 x->props.reqid == reqid &&
954 !xfrm_addr_cmp(&x->id.daddr, &xnew->id.daddr, family) &&
955 !xfrm_addr_cmp(&x->props.saddr, &xnew->props.saddr, family))
956 x->genid = xfrm_state_genid;
960 void xfrm_state_insert(struct xfrm_state *x)
962 spin_lock_bh(&xfrm_state_lock);
963 __xfrm_state_bump_genids(x);
964 __xfrm_state_insert(x);
965 spin_unlock_bh(&xfrm_state_lock);
967 EXPORT_SYMBOL(xfrm_state_insert);
969 /* xfrm_state_lock is held */
970 static struct xfrm_state *__find_acq_core(struct net *net, unsigned short family, u8 mode, u32 reqid, u8 proto, xfrm_address_t *daddr, xfrm_address_t *saddr, int create)
972 unsigned int h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
973 struct hlist_node *entry;
974 struct xfrm_state *x;
976 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+h, bydst) {
977 if (x->props.reqid != reqid ||
978 x->props.mode != mode ||
979 x->props.family != family ||
980 x->km.state != XFRM_STATE_ACQ ||
981 x->id.spi != 0 ||
982 x->id.proto != proto ||
983 xfrm_addr_cmp(&x->id.daddr, daddr, family) ||
984 xfrm_addr_cmp(&x->props.saddr, saddr, family))
985 continue;
987 xfrm_state_hold(x);
988 return x;
991 if (!create)
992 return NULL;
994 x = xfrm_state_alloc(net);
995 if (likely(x)) {
996 switch (family) {
997 case AF_INET:
998 x->sel.daddr.a4 = daddr->a4;
999 x->sel.saddr.a4 = saddr->a4;
1000 x->sel.prefixlen_d = 32;
1001 x->sel.prefixlen_s = 32;
1002 x->props.saddr.a4 = saddr->a4;
1003 x->id.daddr.a4 = daddr->a4;
1004 break;
1006 case AF_INET6:
1007 ipv6_addr_copy((struct in6_addr *)x->sel.daddr.a6,
1008 (struct in6_addr *)daddr);
1009 ipv6_addr_copy((struct in6_addr *)x->sel.saddr.a6,
1010 (struct in6_addr *)saddr);
1011 x->sel.prefixlen_d = 128;
1012 x->sel.prefixlen_s = 128;
1013 ipv6_addr_copy((struct in6_addr *)x->props.saddr.a6,
1014 (struct in6_addr *)saddr);
1015 ipv6_addr_copy((struct in6_addr *)x->id.daddr.a6,
1016 (struct in6_addr *)daddr);
1017 break;
1020 x->km.state = XFRM_STATE_ACQ;
1021 x->id.proto = proto;
1022 x->props.family = family;
1023 x->props.mode = mode;
1024 x->props.reqid = reqid;
1025 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
1026 xfrm_state_hold(x);
1027 tasklet_hrtimer_start(&x->mtimer, ktime_set(net->xfrm.sysctl_acq_expires, 0), HRTIMER_MODE_REL);
1028 list_add(&x->km.all, &net->xfrm.state_all);
1029 hlist_add_head(&x->bydst, net->xfrm.state_bydst+h);
1030 h = xfrm_src_hash(net, daddr, saddr, family);
1031 hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h);
1033 net->xfrm.state_num++;
1035 xfrm_hash_grow_check(net, x->bydst.next != NULL);
1038 return x;
1041 static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 seq);
1043 int xfrm_state_add(struct xfrm_state *x)
1045 struct net *net = xs_net(x);
1046 struct xfrm_state *x1, *to_put;
1047 int family;
1048 int err;
1049 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
1051 family = x->props.family;
1053 to_put = NULL;
1055 spin_lock_bh(&xfrm_state_lock);
1057 x1 = __xfrm_state_locate(x, use_spi, family);
1058 if (x1) {
1059 to_put = x1;
1060 x1 = NULL;
1061 err = -EEXIST;
1062 goto out;
1065 if (use_spi && x->km.seq) {
1066 x1 = __xfrm_find_acq_byseq(net, x->km.seq);
1067 if (x1 && ((x1->id.proto != x->id.proto) ||
1068 xfrm_addr_cmp(&x1->id.daddr, &x->id.daddr, family))) {
1069 to_put = x1;
1070 x1 = NULL;
1074 if (use_spi && !x1)
1075 x1 = __find_acq_core(net, family, x->props.mode, x->props.reqid,
1076 x->id.proto,
1077 &x->id.daddr, &x->props.saddr, 0);
1079 __xfrm_state_bump_genids(x);
1080 __xfrm_state_insert(x);
1081 err = 0;
1083 out:
1084 spin_unlock_bh(&xfrm_state_lock);
1086 if (x1) {
1087 xfrm_state_delete(x1);
1088 xfrm_state_put(x1);
1091 if (to_put)
1092 xfrm_state_put(to_put);
1094 return err;
1096 EXPORT_SYMBOL(xfrm_state_add);
1098 #ifdef CONFIG_XFRM_MIGRATE
1099 static struct xfrm_state *xfrm_state_clone(struct xfrm_state *orig, int *errp)
1101 struct net *net = xs_net(orig);
1102 int err = -ENOMEM;
1103 struct xfrm_state *x = xfrm_state_alloc(net);
1104 if (!x)
1105 goto out;
1107 memcpy(&x->id, &orig->id, sizeof(x->id));
1108 memcpy(&x->sel, &orig->sel, sizeof(x->sel));
1109 memcpy(&x->lft, &orig->lft, sizeof(x->lft));
1110 x->props.mode = orig->props.mode;
1111 x->props.replay_window = orig->props.replay_window;
1112 x->props.reqid = orig->props.reqid;
1113 x->props.family = orig->props.family;
1114 x->props.saddr = orig->props.saddr;
1116 if (orig->aalg) {
1117 x->aalg = xfrm_algo_auth_clone(orig->aalg);
1118 if (!x->aalg)
1119 goto error;
1121 x->props.aalgo = orig->props.aalgo;
1123 if (orig->ealg) {
1124 x->ealg = xfrm_algo_clone(orig->ealg);
1125 if (!x->ealg)
1126 goto error;
1128 x->props.ealgo = orig->props.ealgo;
1130 if (orig->calg) {
1131 x->calg = xfrm_algo_clone(orig->calg);
1132 if (!x->calg)
1133 goto error;
1135 x->props.calgo = orig->props.calgo;
1137 if (orig->encap) {
1138 x->encap = kmemdup(orig->encap, sizeof(*x->encap), GFP_KERNEL);
1139 if (!x->encap)
1140 goto error;
1143 if (orig->coaddr) {
1144 x->coaddr = kmemdup(orig->coaddr, sizeof(*x->coaddr),
1145 GFP_KERNEL);
1146 if (!x->coaddr)
1147 goto error;
1150 err = xfrm_init_state(x);
1151 if (err)
1152 goto error;
1154 x->props.flags = orig->props.flags;
1156 x->curlft.add_time = orig->curlft.add_time;
1157 x->km.state = orig->km.state;
1158 x->km.seq = orig->km.seq;
1160 return x;
1162 error:
1163 xfrm_state_put(x);
1164 out:
1165 if (errp)
1166 *errp = err;
1167 return NULL;
1170 /* xfrm_state_lock is held */
1171 struct xfrm_state * xfrm_migrate_state_find(struct xfrm_migrate *m)
1173 unsigned int h;
1174 struct xfrm_state *x;
1175 struct hlist_node *entry;
1177 if (m->reqid) {
1178 h = xfrm_dst_hash(&init_net, &m->old_daddr, &m->old_saddr,
1179 m->reqid, m->old_family);
1180 hlist_for_each_entry(x, entry, init_net.xfrm.state_bydst+h, bydst) {
1181 if (x->props.mode != m->mode ||
1182 x->id.proto != m->proto)
1183 continue;
1184 if (m->reqid && x->props.reqid != m->reqid)
1185 continue;
1186 if (xfrm_addr_cmp(&x->id.daddr, &m->old_daddr,
1187 m->old_family) ||
1188 xfrm_addr_cmp(&x->props.saddr, &m->old_saddr,
1189 m->old_family))
1190 continue;
1191 xfrm_state_hold(x);
1192 return x;
1194 } else {
1195 h = xfrm_src_hash(&init_net, &m->old_daddr, &m->old_saddr,
1196 m->old_family);
1197 hlist_for_each_entry(x, entry, init_net.xfrm.state_bysrc+h, bysrc) {
1198 if (x->props.mode != m->mode ||
1199 x->id.proto != m->proto)
1200 continue;
1201 if (xfrm_addr_cmp(&x->id.daddr, &m->old_daddr,
1202 m->old_family) ||
1203 xfrm_addr_cmp(&x->props.saddr, &m->old_saddr,
1204 m->old_family))
1205 continue;
1206 xfrm_state_hold(x);
1207 return x;
1211 return NULL;
1213 EXPORT_SYMBOL(xfrm_migrate_state_find);
1215 struct xfrm_state * xfrm_state_migrate(struct xfrm_state *x,
1216 struct xfrm_migrate *m)
1218 struct xfrm_state *xc;
1219 int err;
1221 xc = xfrm_state_clone(x, &err);
1222 if (!xc)
1223 return NULL;
1225 memcpy(&xc->id.daddr, &m->new_daddr, sizeof(xc->id.daddr));
1226 memcpy(&xc->props.saddr, &m->new_saddr, sizeof(xc->props.saddr));
1228 /* add state */
1229 if (!xfrm_addr_cmp(&x->id.daddr, &m->new_daddr, m->new_family)) {
1230 /* a care is needed when the destination address of the
1231 state is to be updated as it is a part of triplet */
1232 xfrm_state_insert(xc);
1233 } else {
1234 if ((err = xfrm_state_add(xc)) < 0)
1235 goto error;
1238 return xc;
1239 error:
1240 kfree(xc);
1241 return NULL;
1243 EXPORT_SYMBOL(xfrm_state_migrate);
1244 #endif
1246 int xfrm_state_update(struct xfrm_state *x)
1248 struct xfrm_state *x1, *to_put;
1249 int err;
1250 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
1252 to_put = NULL;
1254 spin_lock_bh(&xfrm_state_lock);
1255 x1 = __xfrm_state_locate(x, use_spi, x->props.family);
1257 err = -ESRCH;
1258 if (!x1)
1259 goto out;
1261 if (xfrm_state_kern(x1)) {
1262 to_put = x1;
1263 err = -EEXIST;
1264 goto out;
1267 if (x1->km.state == XFRM_STATE_ACQ) {
1268 __xfrm_state_insert(x);
1269 x = NULL;
1271 err = 0;
1273 out:
1274 spin_unlock_bh(&xfrm_state_lock);
1276 if (to_put)
1277 xfrm_state_put(to_put);
1279 if (err)
1280 return err;
1282 if (!x) {
1283 xfrm_state_delete(x1);
1284 xfrm_state_put(x1);
1285 return 0;
1288 err = -EINVAL;
1289 spin_lock_bh(&x1->lock);
1290 if (likely(x1->km.state == XFRM_STATE_VALID)) {
1291 if (x->encap && x1->encap)
1292 memcpy(x1->encap, x->encap, sizeof(*x1->encap));
1293 if (x->coaddr && x1->coaddr) {
1294 memcpy(x1->coaddr, x->coaddr, sizeof(*x1->coaddr));
1296 if (!use_spi && memcmp(&x1->sel, &x->sel, sizeof(x1->sel)))
1297 memcpy(&x1->sel, &x->sel, sizeof(x1->sel));
1298 memcpy(&x1->lft, &x->lft, sizeof(x1->lft));
1299 x1->km.dying = 0;
1301 tasklet_hrtimer_start(&x1->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL);
1302 if (x1->curlft.use_time)
1303 xfrm_state_check_expire(x1);
1305 err = 0;
1307 spin_unlock_bh(&x1->lock);
1309 xfrm_state_put(x1);
1311 return err;
1313 EXPORT_SYMBOL(xfrm_state_update);
1315 int xfrm_state_check_expire(struct xfrm_state *x)
1317 if (!x->curlft.use_time)
1318 x->curlft.use_time = get_seconds();
1320 if (x->km.state != XFRM_STATE_VALID)
1321 return -EINVAL;
1323 if (x->curlft.bytes >= x->lft.hard_byte_limit ||
1324 x->curlft.packets >= x->lft.hard_packet_limit) {
1325 x->km.state = XFRM_STATE_EXPIRED;
1326 tasklet_hrtimer_start(&x->mtimer, ktime_set(0,0), HRTIMER_MODE_REL);
1327 return -EINVAL;
1330 if (!x->km.dying &&
1331 (x->curlft.bytes >= x->lft.soft_byte_limit ||
1332 x->curlft.packets >= x->lft.soft_packet_limit)) {
1333 x->km.dying = 1;
1334 km_state_expired(x, 0, 0);
1336 return 0;
1338 EXPORT_SYMBOL(xfrm_state_check_expire);
1340 struct xfrm_state *
1341 xfrm_state_lookup(struct net *net, xfrm_address_t *daddr, __be32 spi, u8 proto,
1342 unsigned short family)
1344 struct xfrm_state *x;
1346 spin_lock_bh(&xfrm_state_lock);
1347 x = __xfrm_state_lookup(net, daddr, spi, proto, family);
1348 spin_unlock_bh(&xfrm_state_lock);
1349 return x;
1351 EXPORT_SYMBOL(xfrm_state_lookup);
1353 struct xfrm_state *
1354 xfrm_state_lookup_byaddr(struct net *net,
1355 xfrm_address_t *daddr, xfrm_address_t *saddr,
1356 u8 proto, unsigned short family)
1358 struct xfrm_state *x;
1360 spin_lock_bh(&xfrm_state_lock);
1361 x = __xfrm_state_lookup_byaddr(net, daddr, saddr, proto, family);
1362 spin_unlock_bh(&xfrm_state_lock);
1363 return x;
1365 EXPORT_SYMBOL(xfrm_state_lookup_byaddr);
1367 struct xfrm_state *
1368 xfrm_find_acq(struct net *net, u8 mode, u32 reqid, u8 proto,
1369 xfrm_address_t *daddr, xfrm_address_t *saddr,
1370 int create, unsigned short family)
1372 struct xfrm_state *x;
1374 spin_lock_bh(&xfrm_state_lock);
1375 x = __find_acq_core(net, family, mode, reqid, proto, daddr, saddr, create);
1376 spin_unlock_bh(&xfrm_state_lock);
1378 return x;
1380 EXPORT_SYMBOL(xfrm_find_acq);
1382 #ifdef CONFIG_XFRM_SUB_POLICY
1384 xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n,
1385 unsigned short family)
1387 int err = 0;
1388 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
1389 if (!afinfo)
1390 return -EAFNOSUPPORT;
1392 spin_lock_bh(&xfrm_state_lock);
1393 if (afinfo->tmpl_sort)
1394 err = afinfo->tmpl_sort(dst, src, n);
1395 spin_unlock_bh(&xfrm_state_lock);
1396 xfrm_state_put_afinfo(afinfo);
1397 return err;
1399 EXPORT_SYMBOL(xfrm_tmpl_sort);
1402 xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src, int n,
1403 unsigned short family)
1405 int err = 0;
1406 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
1407 if (!afinfo)
1408 return -EAFNOSUPPORT;
1410 spin_lock_bh(&xfrm_state_lock);
1411 if (afinfo->state_sort)
1412 err = afinfo->state_sort(dst, src, n);
1413 spin_unlock_bh(&xfrm_state_lock);
1414 xfrm_state_put_afinfo(afinfo);
1415 return err;
1417 EXPORT_SYMBOL(xfrm_state_sort);
1418 #endif
1420 /* Silly enough, but I'm lazy to build resolution list */
1422 static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 seq)
1424 int i;
1426 for (i = 0; i <= net->xfrm.state_hmask; i++) {
1427 struct hlist_node *entry;
1428 struct xfrm_state *x;
1430 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+i, bydst) {
1431 if (x->km.seq == seq &&
1432 x->km.state == XFRM_STATE_ACQ) {
1433 xfrm_state_hold(x);
1434 return x;
1438 return NULL;
1441 struct xfrm_state *xfrm_find_acq_byseq(struct net *net, u32 seq)
1443 struct xfrm_state *x;
1445 spin_lock_bh(&xfrm_state_lock);
1446 x = __xfrm_find_acq_byseq(net, seq);
1447 spin_unlock_bh(&xfrm_state_lock);
1448 return x;
1450 EXPORT_SYMBOL(xfrm_find_acq_byseq);
1452 u32 xfrm_get_acqseq(void)
1454 u32 res;
1455 static u32 acqseq;
1456 static DEFINE_SPINLOCK(acqseq_lock);
1458 spin_lock_bh(&acqseq_lock);
1459 res = (++acqseq ? : ++acqseq);
1460 spin_unlock_bh(&acqseq_lock);
1461 return res;
1463 EXPORT_SYMBOL(xfrm_get_acqseq);
1465 int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high)
1467 struct net *net = xs_net(x);
1468 unsigned int h;
1469 struct xfrm_state *x0;
1470 int err = -ENOENT;
1471 __be32 minspi = htonl(low);
1472 __be32 maxspi = htonl(high);
1474 spin_lock_bh(&x->lock);
1475 if (x->km.state == XFRM_STATE_DEAD)
1476 goto unlock;
1478 err = 0;
1479 if (x->id.spi)
1480 goto unlock;
1482 err = -ENOENT;
1484 if (minspi == maxspi) {
1485 x0 = xfrm_state_lookup(net, &x->id.daddr, minspi, x->id.proto, x->props.family);
1486 if (x0) {
1487 xfrm_state_put(x0);
1488 goto unlock;
1490 x->id.spi = minspi;
1491 } else {
1492 u32 spi = 0;
1493 for (h=0; h<high-low+1; h++) {
1494 spi = low + net_random()%(high-low+1);
1495 x0 = xfrm_state_lookup(net, &x->id.daddr, htonl(spi), x->id.proto, x->props.family);
1496 if (x0 == NULL) {
1497 x->id.spi = htonl(spi);
1498 break;
1500 xfrm_state_put(x0);
1503 if (x->id.spi) {
1504 spin_lock_bh(&xfrm_state_lock);
1505 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, x->props.family);
1506 hlist_add_head(&x->byspi, net->xfrm.state_byspi+h);
1507 spin_unlock_bh(&xfrm_state_lock);
1509 err = 0;
1512 unlock:
1513 spin_unlock_bh(&x->lock);
1515 return err;
1517 EXPORT_SYMBOL(xfrm_alloc_spi);
1519 int xfrm_state_walk(struct net *net, struct xfrm_state_walk *walk,
1520 int (*func)(struct xfrm_state *, int, void*),
1521 void *data)
1523 struct xfrm_state *state;
1524 struct xfrm_state_walk *x;
1525 int err = 0;
1527 if (walk->seq != 0 && list_empty(&walk->all))
1528 return 0;
1530 spin_lock_bh(&xfrm_state_lock);
1531 if (list_empty(&walk->all))
1532 x = list_first_entry(&net->xfrm.state_all, struct xfrm_state_walk, all);
1533 else
1534 x = list_entry(&walk->all, struct xfrm_state_walk, all);
1535 list_for_each_entry_from(x, &net->xfrm.state_all, all) {
1536 if (x->state == XFRM_STATE_DEAD)
1537 continue;
1538 state = container_of(x, struct xfrm_state, km);
1539 if (!xfrm_id_proto_match(state->id.proto, walk->proto))
1540 continue;
1541 err = func(state, walk->seq, data);
1542 if (err) {
1543 list_move_tail(&walk->all, &x->all);
1544 goto out;
1546 walk->seq++;
1548 if (walk->seq == 0) {
1549 err = -ENOENT;
1550 goto out;
1552 list_del_init(&walk->all);
1553 out:
1554 spin_unlock_bh(&xfrm_state_lock);
1555 return err;
1557 EXPORT_SYMBOL(xfrm_state_walk);
1559 void xfrm_state_walk_init(struct xfrm_state_walk *walk, u8 proto)
1561 INIT_LIST_HEAD(&walk->all);
1562 walk->proto = proto;
1563 walk->state = XFRM_STATE_DEAD;
1564 walk->seq = 0;
1566 EXPORT_SYMBOL(xfrm_state_walk_init);
1568 void xfrm_state_walk_done(struct xfrm_state_walk *walk)
1570 if (list_empty(&walk->all))
1571 return;
1573 spin_lock_bh(&xfrm_state_lock);
1574 list_del(&walk->all);
1575 spin_unlock_bh(&xfrm_state_lock);
1577 EXPORT_SYMBOL(xfrm_state_walk_done);
1580 void xfrm_replay_notify(struct xfrm_state *x, int event)
1582 struct km_event c;
1583 /* we send notify messages in case
1584 * 1. we updated on of the sequence numbers, and the seqno difference
1585 * is at least x->replay_maxdiff, in this case we also update the
1586 * timeout of our timer function
1587 * 2. if x->replay_maxage has elapsed since last update,
1588 * and there were changes
1590 * The state structure must be locked!
1593 switch (event) {
1594 case XFRM_REPLAY_UPDATE:
1595 if (x->replay_maxdiff &&
1596 (x->replay.seq - x->preplay.seq < x->replay_maxdiff) &&
1597 (x->replay.oseq - x->preplay.oseq < x->replay_maxdiff)) {
1598 if (x->xflags & XFRM_TIME_DEFER)
1599 event = XFRM_REPLAY_TIMEOUT;
1600 else
1601 return;
1604 break;
1606 case XFRM_REPLAY_TIMEOUT:
1607 if ((x->replay.seq == x->preplay.seq) &&
1608 (x->replay.bitmap == x->preplay.bitmap) &&
1609 (x->replay.oseq == x->preplay.oseq)) {
1610 x->xflags |= XFRM_TIME_DEFER;
1611 return;
1614 break;
1617 memcpy(&x->preplay, &x->replay, sizeof(struct xfrm_replay_state));
1618 c.event = XFRM_MSG_NEWAE;
1619 c.data.aevent = event;
1620 km_state_notify(x, &c);
1622 if (x->replay_maxage &&
1623 !mod_timer(&x->rtimer, jiffies + x->replay_maxage))
1624 x->xflags &= ~XFRM_TIME_DEFER;
1627 static void xfrm_replay_timer_handler(unsigned long data)
1629 struct xfrm_state *x = (struct xfrm_state*)data;
1631 spin_lock(&x->lock);
1633 if (x->km.state == XFRM_STATE_VALID) {
1634 if (xfrm_aevent_is_on(xs_net(x)))
1635 xfrm_replay_notify(x, XFRM_REPLAY_TIMEOUT);
1636 else
1637 x->xflags |= XFRM_TIME_DEFER;
1640 spin_unlock(&x->lock);
1643 int xfrm_replay_check(struct xfrm_state *x,
1644 struct sk_buff *skb, __be32 net_seq)
1646 u32 diff;
1647 u32 seq = ntohl(net_seq);
1649 if (unlikely(seq == 0))
1650 goto err;
1652 if (likely(seq > x->replay.seq))
1653 return 0;
1655 diff = x->replay.seq - seq;
1656 if (diff >= min_t(unsigned int, x->props.replay_window,
1657 sizeof(x->replay.bitmap) * 8)) {
1658 x->stats.replay_window++;
1659 goto err;
1662 if (x->replay.bitmap & (1U << diff)) {
1663 x->stats.replay++;
1664 goto err;
1666 return 0;
1668 err:
1669 xfrm_audit_state_replay(x, skb, net_seq);
1670 return -EINVAL;
1673 void xfrm_replay_advance(struct xfrm_state *x, __be32 net_seq)
1675 u32 diff;
1676 u32 seq = ntohl(net_seq);
1678 if (seq > x->replay.seq) {
1679 diff = seq - x->replay.seq;
1680 if (diff < x->props.replay_window)
1681 x->replay.bitmap = ((x->replay.bitmap) << diff) | 1;
1682 else
1683 x->replay.bitmap = 1;
1684 x->replay.seq = seq;
1685 } else {
1686 diff = x->replay.seq - seq;
1687 x->replay.bitmap |= (1U << diff);
1690 if (xfrm_aevent_is_on(xs_net(x)))
1691 xfrm_replay_notify(x, XFRM_REPLAY_UPDATE);
1694 static LIST_HEAD(xfrm_km_list);
1695 static DEFINE_RWLOCK(xfrm_km_lock);
1697 void km_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
1699 struct xfrm_mgr *km;
1701 read_lock(&xfrm_km_lock);
1702 list_for_each_entry(km, &xfrm_km_list, list)
1703 if (km->notify_policy)
1704 km->notify_policy(xp, dir, c);
1705 read_unlock(&xfrm_km_lock);
1708 void km_state_notify(struct xfrm_state *x, struct km_event *c)
1710 struct xfrm_mgr *km;
1711 read_lock(&xfrm_km_lock);
1712 list_for_each_entry(km, &xfrm_km_list, list)
1713 if (km->notify)
1714 km->notify(x, c);
1715 read_unlock(&xfrm_km_lock);
1718 EXPORT_SYMBOL(km_policy_notify);
1719 EXPORT_SYMBOL(km_state_notify);
1721 void km_state_expired(struct xfrm_state *x, int hard, u32 pid)
1723 struct net *net = xs_net(x);
1724 struct km_event c;
1726 c.data.hard = hard;
1727 c.pid = pid;
1728 c.event = XFRM_MSG_EXPIRE;
1729 km_state_notify(x, &c);
1731 if (hard)
1732 wake_up(&net->xfrm.km_waitq);
1735 EXPORT_SYMBOL(km_state_expired);
1737 * We send to all registered managers regardless of failure
1738 * We are happy with one success
1740 int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol)
1742 int err = -EINVAL, acqret;
1743 struct xfrm_mgr *km;
1745 read_lock(&xfrm_km_lock);
1746 list_for_each_entry(km, &xfrm_km_list, list) {
1747 acqret = km->acquire(x, t, pol, XFRM_POLICY_OUT);
1748 if (!acqret)
1749 err = acqret;
1751 read_unlock(&xfrm_km_lock);
1752 return err;
1754 EXPORT_SYMBOL(km_query);
1756 int km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
1758 int err = -EINVAL;
1759 struct xfrm_mgr *km;
1761 read_lock(&xfrm_km_lock);
1762 list_for_each_entry(km, &xfrm_km_list, list) {
1763 if (km->new_mapping)
1764 err = km->new_mapping(x, ipaddr, sport);
1765 if (!err)
1766 break;
1768 read_unlock(&xfrm_km_lock);
1769 return err;
1771 EXPORT_SYMBOL(km_new_mapping);
1773 void km_policy_expired(struct xfrm_policy *pol, int dir, int hard, u32 pid)
1775 struct net *net = xp_net(pol);
1776 struct km_event c;
1778 c.data.hard = hard;
1779 c.pid = pid;
1780 c.event = XFRM_MSG_POLEXPIRE;
1781 km_policy_notify(pol, dir, &c);
1783 if (hard)
1784 wake_up(&net->xfrm.km_waitq);
1786 EXPORT_SYMBOL(km_policy_expired);
1788 #ifdef CONFIG_XFRM_MIGRATE
1789 int km_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
1790 struct xfrm_migrate *m, int num_migrate,
1791 struct xfrm_kmaddress *k)
1793 int err = -EINVAL;
1794 int ret;
1795 struct xfrm_mgr *km;
1797 read_lock(&xfrm_km_lock);
1798 list_for_each_entry(km, &xfrm_km_list, list) {
1799 if (km->migrate) {
1800 ret = km->migrate(sel, dir, type, m, num_migrate, k);
1801 if (!ret)
1802 err = ret;
1805 read_unlock(&xfrm_km_lock);
1806 return err;
1808 EXPORT_SYMBOL(km_migrate);
1809 #endif
1811 int km_report(struct net *net, u8 proto, struct xfrm_selector *sel, xfrm_address_t *addr)
1813 int err = -EINVAL;
1814 int ret;
1815 struct xfrm_mgr *km;
1817 read_lock(&xfrm_km_lock);
1818 list_for_each_entry(km, &xfrm_km_list, list) {
1819 if (km->report) {
1820 ret = km->report(net, proto, sel, addr);
1821 if (!ret)
1822 err = ret;
1825 read_unlock(&xfrm_km_lock);
1826 return err;
1828 EXPORT_SYMBOL(km_report);
1830 int xfrm_user_policy(struct sock *sk, int optname, u8 __user *optval, int optlen)
1832 int err;
1833 u8 *data;
1834 struct xfrm_mgr *km;
1835 struct xfrm_policy *pol = NULL;
1837 if (optlen <= 0 || optlen > PAGE_SIZE)
1838 return -EMSGSIZE;
1840 data = kmalloc(optlen, GFP_KERNEL);
1841 if (!data)
1842 return -ENOMEM;
1844 err = -EFAULT;
1845 if (copy_from_user(data, optval, optlen))
1846 goto out;
1848 err = -EINVAL;
1849 read_lock(&xfrm_km_lock);
1850 list_for_each_entry(km, &xfrm_km_list, list) {
1851 pol = km->compile_policy(sk, optname, data,
1852 optlen, &err);
1853 if (err >= 0)
1854 break;
1856 read_unlock(&xfrm_km_lock);
1858 if (err >= 0) {
1859 xfrm_sk_policy_insert(sk, err, pol);
1860 xfrm_pol_put(pol);
1861 err = 0;
1864 out:
1865 kfree(data);
1866 return err;
1868 EXPORT_SYMBOL(xfrm_user_policy);
1870 int xfrm_register_km(struct xfrm_mgr *km)
1872 write_lock_bh(&xfrm_km_lock);
1873 list_add_tail(&km->list, &xfrm_km_list);
1874 write_unlock_bh(&xfrm_km_lock);
1875 return 0;
1877 EXPORT_SYMBOL(xfrm_register_km);
1879 int xfrm_unregister_km(struct xfrm_mgr *km)
1881 write_lock_bh(&xfrm_km_lock);
1882 list_del(&km->list);
1883 write_unlock_bh(&xfrm_km_lock);
1884 return 0;
1886 EXPORT_SYMBOL(xfrm_unregister_km);
1888 int xfrm_state_register_afinfo(struct xfrm_state_afinfo *afinfo)
1890 int err = 0;
1891 if (unlikely(afinfo == NULL))
1892 return -EINVAL;
1893 if (unlikely(afinfo->family >= NPROTO))
1894 return -EAFNOSUPPORT;
1895 write_lock_bh(&xfrm_state_afinfo_lock);
1896 if (unlikely(xfrm_state_afinfo[afinfo->family] != NULL))
1897 err = -ENOBUFS;
1898 else
1899 xfrm_state_afinfo[afinfo->family] = afinfo;
1900 write_unlock_bh(&xfrm_state_afinfo_lock);
1901 return err;
1903 EXPORT_SYMBOL(xfrm_state_register_afinfo);
1905 int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo)
1907 int err = 0;
1908 if (unlikely(afinfo == NULL))
1909 return -EINVAL;
1910 if (unlikely(afinfo->family >= NPROTO))
1911 return -EAFNOSUPPORT;
1912 write_lock_bh(&xfrm_state_afinfo_lock);
1913 if (likely(xfrm_state_afinfo[afinfo->family] != NULL)) {
1914 if (unlikely(xfrm_state_afinfo[afinfo->family] != afinfo))
1915 err = -EINVAL;
1916 else
1917 xfrm_state_afinfo[afinfo->family] = NULL;
1919 write_unlock_bh(&xfrm_state_afinfo_lock);
1920 return err;
1922 EXPORT_SYMBOL(xfrm_state_unregister_afinfo);
1924 static struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family)
1926 struct xfrm_state_afinfo *afinfo;
1927 if (unlikely(family >= NPROTO))
1928 return NULL;
1929 read_lock(&xfrm_state_afinfo_lock);
1930 afinfo = xfrm_state_afinfo[family];
1931 if (unlikely(!afinfo))
1932 read_unlock(&xfrm_state_afinfo_lock);
1933 return afinfo;
1936 static void xfrm_state_put_afinfo(struct xfrm_state_afinfo *afinfo)
1937 __releases(xfrm_state_afinfo_lock)
1939 read_unlock(&xfrm_state_afinfo_lock);
1942 /* Temporarily located here until net/xfrm/xfrm_tunnel.c is created */
1943 void xfrm_state_delete_tunnel(struct xfrm_state *x)
1945 if (x->tunnel) {
1946 struct xfrm_state *t = x->tunnel;
1948 if (atomic_read(&t->tunnel_users) == 2)
1949 xfrm_state_delete(t);
1950 atomic_dec(&t->tunnel_users);
1951 xfrm_state_put(t);
1952 x->tunnel = NULL;
1955 EXPORT_SYMBOL(xfrm_state_delete_tunnel);
1957 int xfrm_state_mtu(struct xfrm_state *x, int mtu)
1959 int res;
1961 spin_lock_bh(&x->lock);
1962 if (x->km.state == XFRM_STATE_VALID &&
1963 x->type && x->type->get_mtu)
1964 res = x->type->get_mtu(x, mtu);
1965 else
1966 res = mtu - x->props.header_len;
1967 spin_unlock_bh(&x->lock);
1968 return res;
1971 int xfrm_init_state(struct xfrm_state *x)
1973 struct xfrm_state_afinfo *afinfo;
1974 struct xfrm_mode *inner_mode;
1975 int family = x->props.family;
1976 int err;
1978 err = -EAFNOSUPPORT;
1979 afinfo = xfrm_state_get_afinfo(family);
1980 if (!afinfo)
1981 goto error;
1983 err = 0;
1984 if (afinfo->init_flags)
1985 err = afinfo->init_flags(x);
1987 xfrm_state_put_afinfo(afinfo);
1989 if (err)
1990 goto error;
1992 err = -EPROTONOSUPPORT;
1994 if (x->sel.family != AF_UNSPEC) {
1995 inner_mode = xfrm_get_mode(x->props.mode, x->sel.family);
1996 if (inner_mode == NULL)
1997 goto error;
1999 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL) &&
2000 family != x->sel.family) {
2001 xfrm_put_mode(inner_mode);
2002 goto error;
2005 x->inner_mode = inner_mode;
2006 } else {
2007 struct xfrm_mode *inner_mode_iaf;
2008 int iafamily = AF_INET;
2010 inner_mode = xfrm_get_mode(x->props.mode, x->props.family);
2011 if (inner_mode == NULL)
2012 goto error;
2014 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL)) {
2015 xfrm_put_mode(inner_mode);
2016 goto error;
2018 x->inner_mode = inner_mode;
2020 if (x->props.family == AF_INET)
2021 iafamily = AF_INET6;
2023 inner_mode_iaf = xfrm_get_mode(x->props.mode, iafamily);
2024 if (inner_mode_iaf) {
2025 if (inner_mode_iaf->flags & XFRM_MODE_FLAG_TUNNEL)
2026 x->inner_mode_iaf = inner_mode_iaf;
2027 else
2028 xfrm_put_mode(inner_mode_iaf);
2032 x->type = xfrm_get_type(x->id.proto, family);
2033 if (x->type == NULL)
2034 goto error;
2036 err = x->type->init_state(x);
2037 if (err)
2038 goto error;
2040 x->outer_mode = xfrm_get_mode(x->props.mode, family);
2041 if (x->outer_mode == NULL)
2042 goto error;
2044 x->km.state = XFRM_STATE_VALID;
2046 error:
2047 return err;
2050 EXPORT_SYMBOL(xfrm_init_state);
2052 int __net_init xfrm_state_init(struct net *net)
2054 unsigned int sz;
2056 INIT_LIST_HEAD(&net->xfrm.state_all);
2058 sz = sizeof(struct hlist_head) * 8;
2060 net->xfrm.state_bydst = xfrm_hash_alloc(sz);
2061 if (!net->xfrm.state_bydst)
2062 goto out_bydst;
2063 net->xfrm.state_bysrc = xfrm_hash_alloc(sz);
2064 if (!net->xfrm.state_bysrc)
2065 goto out_bysrc;
2066 net->xfrm.state_byspi = xfrm_hash_alloc(sz);
2067 if (!net->xfrm.state_byspi)
2068 goto out_byspi;
2069 net->xfrm.state_hmask = ((sz / sizeof(struct hlist_head)) - 1);
2071 net->xfrm.state_num = 0;
2072 INIT_WORK(&net->xfrm.state_hash_work, xfrm_hash_resize);
2073 INIT_HLIST_HEAD(&net->xfrm.state_gc_list);
2074 INIT_WORK(&net->xfrm.state_gc_work, xfrm_state_gc_task);
2075 init_waitqueue_head(&net->xfrm.km_waitq);
2076 return 0;
2078 out_byspi:
2079 xfrm_hash_free(net->xfrm.state_bysrc, sz);
2080 out_bysrc:
2081 xfrm_hash_free(net->xfrm.state_bydst, sz);
2082 out_bydst:
2083 return -ENOMEM;
2086 void xfrm_state_fini(struct net *net)
2088 struct xfrm_audit audit_info;
2089 unsigned int sz;
2091 flush_work(&net->xfrm.state_hash_work);
2092 audit_info.loginuid = -1;
2093 audit_info.sessionid = -1;
2094 audit_info.secid = 0;
2095 xfrm_state_flush(net, IPSEC_PROTO_ANY, &audit_info);
2096 flush_work(&net->xfrm.state_gc_work);
2098 WARN_ON(!list_empty(&net->xfrm.state_all));
2100 sz = (net->xfrm.state_hmask + 1) * sizeof(struct hlist_head);
2101 WARN_ON(!hlist_empty(net->xfrm.state_byspi));
2102 xfrm_hash_free(net->xfrm.state_byspi, sz);
2103 WARN_ON(!hlist_empty(net->xfrm.state_bysrc));
2104 xfrm_hash_free(net->xfrm.state_bysrc, sz);
2105 WARN_ON(!hlist_empty(net->xfrm.state_bydst));
2106 xfrm_hash_free(net->xfrm.state_bydst, sz);
2109 #ifdef CONFIG_AUDITSYSCALL
2110 static void xfrm_audit_helper_sainfo(struct xfrm_state *x,
2111 struct audit_buffer *audit_buf)
2113 struct xfrm_sec_ctx *ctx = x->security;
2114 u32 spi = ntohl(x->id.spi);
2116 if (ctx)
2117 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
2118 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
2120 switch(x->props.family) {
2121 case AF_INET:
2122 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2123 &x->props.saddr.a4, &x->id.daddr.a4);
2124 break;
2125 case AF_INET6:
2126 audit_log_format(audit_buf, " src=%pI6 dst=%pI6",
2127 x->props.saddr.a6, x->id.daddr.a6);
2128 break;
2131 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
2134 static void xfrm_audit_helper_pktinfo(struct sk_buff *skb, u16 family,
2135 struct audit_buffer *audit_buf)
2137 struct iphdr *iph4;
2138 struct ipv6hdr *iph6;
2140 switch (family) {
2141 case AF_INET:
2142 iph4 = ip_hdr(skb);
2143 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2144 &iph4->saddr, &iph4->daddr);
2145 break;
2146 case AF_INET6:
2147 iph6 = ipv6_hdr(skb);
2148 audit_log_format(audit_buf,
2149 " src=%pI6 dst=%pI6 flowlbl=0x%x%02x%02x",
2150 &iph6->saddr,&iph6->daddr,
2151 iph6->flow_lbl[0] & 0x0f,
2152 iph6->flow_lbl[1],
2153 iph6->flow_lbl[2]);
2154 break;
2158 void xfrm_audit_state_add(struct xfrm_state *x, int result,
2159 uid_t auid, u32 sessionid, u32 secid)
2161 struct audit_buffer *audit_buf;
2163 audit_buf = xfrm_audit_start("SAD-add");
2164 if (audit_buf == NULL)
2165 return;
2166 xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf);
2167 xfrm_audit_helper_sainfo(x, audit_buf);
2168 audit_log_format(audit_buf, " res=%u", result);
2169 audit_log_end(audit_buf);
2171 EXPORT_SYMBOL_GPL(xfrm_audit_state_add);
2173 void xfrm_audit_state_delete(struct xfrm_state *x, int result,
2174 uid_t auid, u32 sessionid, u32 secid)
2176 struct audit_buffer *audit_buf;
2178 audit_buf = xfrm_audit_start("SAD-delete");
2179 if (audit_buf == NULL)
2180 return;
2181 xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf);
2182 xfrm_audit_helper_sainfo(x, audit_buf);
2183 audit_log_format(audit_buf, " res=%u", result);
2184 audit_log_end(audit_buf);
2186 EXPORT_SYMBOL_GPL(xfrm_audit_state_delete);
2188 void xfrm_audit_state_replay_overflow(struct xfrm_state *x,
2189 struct sk_buff *skb)
2191 struct audit_buffer *audit_buf;
2192 u32 spi;
2194 audit_buf = xfrm_audit_start("SA-replay-overflow");
2195 if (audit_buf == NULL)
2196 return;
2197 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2198 /* don't record the sequence number because it's inherent in this kind
2199 * of audit message */
2200 spi = ntohl(x->id.spi);
2201 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
2202 audit_log_end(audit_buf);
2204 EXPORT_SYMBOL_GPL(xfrm_audit_state_replay_overflow);
2206 static void xfrm_audit_state_replay(struct xfrm_state *x,
2207 struct sk_buff *skb, __be32 net_seq)
2209 struct audit_buffer *audit_buf;
2210 u32 spi;
2212 audit_buf = xfrm_audit_start("SA-replayed-pkt");
2213 if (audit_buf == NULL)
2214 return;
2215 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2216 spi = ntohl(x->id.spi);
2217 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2218 spi, spi, ntohl(net_seq));
2219 audit_log_end(audit_buf);
2222 void xfrm_audit_state_notfound_simple(struct sk_buff *skb, u16 family)
2224 struct audit_buffer *audit_buf;
2226 audit_buf = xfrm_audit_start("SA-notfound");
2227 if (audit_buf == NULL)
2228 return;
2229 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2230 audit_log_end(audit_buf);
2232 EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound_simple);
2234 void xfrm_audit_state_notfound(struct sk_buff *skb, u16 family,
2235 __be32 net_spi, __be32 net_seq)
2237 struct audit_buffer *audit_buf;
2238 u32 spi;
2240 audit_buf = xfrm_audit_start("SA-notfound");
2241 if (audit_buf == NULL)
2242 return;
2243 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2244 spi = ntohl(net_spi);
2245 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2246 spi, spi, ntohl(net_seq));
2247 audit_log_end(audit_buf);
2249 EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound);
2251 void xfrm_audit_state_icvfail(struct xfrm_state *x,
2252 struct sk_buff *skb, u8 proto)
2254 struct audit_buffer *audit_buf;
2255 __be32 net_spi;
2256 __be32 net_seq;
2258 audit_buf = xfrm_audit_start("SA-icv-failure");
2259 if (audit_buf == NULL)
2260 return;
2261 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2262 if (xfrm_parse_spi(skb, proto, &net_spi, &net_seq) == 0) {
2263 u32 spi = ntohl(net_spi);
2264 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2265 spi, spi, ntohl(net_seq));
2267 audit_log_end(audit_buf);
2269 EXPORT_SYMBOL_GPL(xfrm_audit_state_icvfail);
2270 #endif /* CONFIG_AUDITSYSCALL */