6 * Kazunori MIYAZAWA @USAGI
7 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
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>
18 #include <linux/pfkeyv2.h>
19 #include <linux/ipsec.h>
20 #include <linux/module.h>
21 #include <linux/cache.h>
22 #include <linux/audit.h>
23 #include <asm/uaccess.h>
25 #include "xfrm_hash.h"
28 EXPORT_SYMBOL(xfrm_nl);
30 u32 sysctl_xfrm_aevent_etime __read_mostly = XFRM_AE_ETIME;
31 EXPORT_SYMBOL(sysctl_xfrm_aevent_etime);
33 u32 sysctl_xfrm_aevent_rseqth __read_mostly = XFRM_AE_SEQT_SIZE;
34 EXPORT_SYMBOL(sysctl_xfrm_aevent_rseqth);
36 u32 sysctl_xfrm_acq_expires __read_mostly = 30;
38 /* Each xfrm_state may be linked to two tables:
40 1. Hash table by (spi,daddr,ah/esp) to find SA by SPI. (input,ctl)
41 2. Hash table by (daddr,family,reqid) to find what SAs exist for given
42 destination/tunnel endpoint. (output)
45 static DEFINE_SPINLOCK(xfrm_state_lock);
47 /* Hash table to find appropriate SA towards given target (endpoint
48 * of tunnel or destination of transport mode) allowed by selector.
50 * Main use is finding SA after policy selected tunnel or transport mode.
51 * Also, it can be used by ah/esp icmp error handler to find offending SA.
53 static LIST_HEAD(xfrm_state_all);
54 static struct hlist_head *xfrm_state_bydst __read_mostly;
55 static struct hlist_head *xfrm_state_bysrc __read_mostly;
56 static struct hlist_head *xfrm_state_byspi __read_mostly;
57 static unsigned int xfrm_state_hmask __read_mostly;
58 static unsigned int xfrm_state_hashmax __read_mostly = 1 * 1024 * 1024;
59 static unsigned int xfrm_state_num;
60 static unsigned int xfrm_state_genid;
62 static struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family);
63 static void xfrm_state_put_afinfo(struct xfrm_state_afinfo *afinfo);
65 #ifdef CONFIG_AUDITSYSCALL
66 static void xfrm_audit_state_replay(struct xfrm_state *x,
67 struct sk_buff *skb, __be32 net_seq);
69 #define xfrm_audit_state_replay(x, s, sq) do { ; } while (0)
70 #endif /* CONFIG_AUDITSYSCALL */
72 static inline unsigned int xfrm_dst_hash(xfrm_address_t *daddr,
73 xfrm_address_t *saddr,
75 unsigned short family)
77 return __xfrm_dst_hash(daddr, saddr, reqid, family, xfrm_state_hmask);
80 static inline unsigned int xfrm_src_hash(xfrm_address_t *daddr,
81 xfrm_address_t *saddr,
82 unsigned short family)
84 return __xfrm_src_hash(daddr, saddr, family, xfrm_state_hmask);
87 static inline unsigned int
88 xfrm_spi_hash(xfrm_address_t *daddr, __be32 spi, u8 proto, unsigned short family)
90 return __xfrm_spi_hash(daddr, spi, proto, family, xfrm_state_hmask);
93 static void xfrm_hash_transfer(struct hlist_head *list,
94 struct hlist_head *ndsttable,
95 struct hlist_head *nsrctable,
96 struct hlist_head *nspitable,
97 unsigned int nhashmask)
99 struct hlist_node *entry, *tmp;
100 struct xfrm_state *x;
102 hlist_for_each_entry_safe(x, entry, tmp, list, bydst) {
105 h = __xfrm_dst_hash(&x->id.daddr, &x->props.saddr,
106 x->props.reqid, x->props.family,
108 hlist_add_head(&x->bydst, ndsttable+h);
110 h = __xfrm_src_hash(&x->id.daddr, &x->props.saddr,
113 hlist_add_head(&x->bysrc, nsrctable+h);
116 h = __xfrm_spi_hash(&x->id.daddr, x->id.spi,
117 x->id.proto, x->props.family,
119 hlist_add_head(&x->byspi, nspitable+h);
124 static unsigned long xfrm_hash_new_size(void)
126 return ((xfrm_state_hmask + 1) << 1) *
127 sizeof(struct hlist_head);
130 static DEFINE_MUTEX(hash_resize_mutex);
132 static void xfrm_hash_resize(struct work_struct *__unused)
134 struct hlist_head *ndst, *nsrc, *nspi, *odst, *osrc, *ospi;
135 unsigned long nsize, osize;
136 unsigned int nhashmask, ohashmask;
139 mutex_lock(&hash_resize_mutex);
141 nsize = xfrm_hash_new_size();
142 ndst = xfrm_hash_alloc(nsize);
145 nsrc = xfrm_hash_alloc(nsize);
147 xfrm_hash_free(ndst, nsize);
150 nspi = xfrm_hash_alloc(nsize);
152 xfrm_hash_free(ndst, nsize);
153 xfrm_hash_free(nsrc, nsize);
157 spin_lock_bh(&xfrm_state_lock);
159 nhashmask = (nsize / sizeof(struct hlist_head)) - 1U;
160 for (i = xfrm_state_hmask; i >= 0; i--)
161 xfrm_hash_transfer(xfrm_state_bydst+i, ndst, nsrc, nspi,
164 odst = xfrm_state_bydst;
165 osrc = xfrm_state_bysrc;
166 ospi = xfrm_state_byspi;
167 ohashmask = xfrm_state_hmask;
169 xfrm_state_bydst = ndst;
170 xfrm_state_bysrc = nsrc;
171 xfrm_state_byspi = nspi;
172 xfrm_state_hmask = nhashmask;
174 spin_unlock_bh(&xfrm_state_lock);
176 osize = (ohashmask + 1) * sizeof(struct hlist_head);
177 xfrm_hash_free(odst, osize);
178 xfrm_hash_free(osrc, osize);
179 xfrm_hash_free(ospi, osize);
182 mutex_unlock(&hash_resize_mutex);
185 static DECLARE_WORK(xfrm_hash_work, xfrm_hash_resize);
187 DECLARE_WAIT_QUEUE_HEAD(km_waitq);
188 EXPORT_SYMBOL(km_waitq);
190 static DEFINE_RWLOCK(xfrm_state_afinfo_lock);
191 static struct xfrm_state_afinfo *xfrm_state_afinfo[NPROTO];
193 static struct work_struct xfrm_state_gc_work;
194 static HLIST_HEAD(xfrm_state_gc_list);
195 static DEFINE_SPINLOCK(xfrm_state_gc_lock);
197 int __xfrm_state_delete(struct xfrm_state *x);
199 int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol);
200 void km_state_expired(struct xfrm_state *x, int hard, u32 pid);
202 static struct xfrm_state_afinfo *xfrm_state_lock_afinfo(unsigned int family)
204 struct xfrm_state_afinfo *afinfo;
205 if (unlikely(family >= NPROTO))
207 write_lock_bh(&xfrm_state_afinfo_lock);
208 afinfo = xfrm_state_afinfo[family];
209 if (unlikely(!afinfo))
210 write_unlock_bh(&xfrm_state_afinfo_lock);
214 static void xfrm_state_unlock_afinfo(struct xfrm_state_afinfo *afinfo)
215 __releases(xfrm_state_afinfo_lock)
217 write_unlock_bh(&xfrm_state_afinfo_lock);
220 int xfrm_register_type(const struct xfrm_type *type, unsigned short family)
222 struct xfrm_state_afinfo *afinfo = xfrm_state_lock_afinfo(family);
223 const struct xfrm_type **typemap;
226 if (unlikely(afinfo == NULL))
227 return -EAFNOSUPPORT;
228 typemap = afinfo->type_map;
230 if (likely(typemap[type->proto] == NULL))
231 typemap[type->proto] = type;
234 xfrm_state_unlock_afinfo(afinfo);
237 EXPORT_SYMBOL(xfrm_register_type);
239 int xfrm_unregister_type(const struct xfrm_type *type, unsigned short family)
241 struct xfrm_state_afinfo *afinfo = xfrm_state_lock_afinfo(family);
242 const struct xfrm_type **typemap;
245 if (unlikely(afinfo == NULL))
246 return -EAFNOSUPPORT;
247 typemap = afinfo->type_map;
249 if (unlikely(typemap[type->proto] != type))
252 typemap[type->proto] = NULL;
253 xfrm_state_unlock_afinfo(afinfo);
256 EXPORT_SYMBOL(xfrm_unregister_type);
258 static const struct xfrm_type *xfrm_get_type(u8 proto, unsigned short family)
260 struct xfrm_state_afinfo *afinfo;
261 const struct xfrm_type **typemap;
262 const struct xfrm_type *type;
263 int modload_attempted = 0;
266 afinfo = xfrm_state_get_afinfo(family);
267 if (unlikely(afinfo == NULL))
269 typemap = afinfo->type_map;
271 type = typemap[proto];
272 if (unlikely(type && !try_module_get(type->owner)))
274 if (!type && !modload_attempted) {
275 xfrm_state_put_afinfo(afinfo);
276 request_module("xfrm-type-%d-%d", family, proto);
277 modload_attempted = 1;
281 xfrm_state_put_afinfo(afinfo);
285 static void xfrm_put_type(const struct xfrm_type *type)
287 module_put(type->owner);
290 int xfrm_register_mode(struct xfrm_mode *mode, int family)
292 struct xfrm_state_afinfo *afinfo;
293 struct xfrm_mode **modemap;
296 if (unlikely(mode->encap >= XFRM_MODE_MAX))
299 afinfo = xfrm_state_lock_afinfo(family);
300 if (unlikely(afinfo == NULL))
301 return -EAFNOSUPPORT;
304 modemap = afinfo->mode_map;
305 if (modemap[mode->encap])
309 if (!try_module_get(afinfo->owner))
312 mode->afinfo = afinfo;
313 modemap[mode->encap] = mode;
317 xfrm_state_unlock_afinfo(afinfo);
320 EXPORT_SYMBOL(xfrm_register_mode);
322 int xfrm_unregister_mode(struct xfrm_mode *mode, int family)
324 struct xfrm_state_afinfo *afinfo;
325 struct xfrm_mode **modemap;
328 if (unlikely(mode->encap >= XFRM_MODE_MAX))
331 afinfo = xfrm_state_lock_afinfo(family);
332 if (unlikely(afinfo == NULL))
333 return -EAFNOSUPPORT;
336 modemap = afinfo->mode_map;
337 if (likely(modemap[mode->encap] == mode)) {
338 modemap[mode->encap] = NULL;
339 module_put(mode->afinfo->owner);
343 xfrm_state_unlock_afinfo(afinfo);
346 EXPORT_SYMBOL(xfrm_unregister_mode);
348 static struct xfrm_mode *xfrm_get_mode(unsigned int encap, int family)
350 struct xfrm_state_afinfo *afinfo;
351 struct xfrm_mode *mode;
352 int modload_attempted = 0;
354 if (unlikely(encap >= XFRM_MODE_MAX))
358 afinfo = xfrm_state_get_afinfo(family);
359 if (unlikely(afinfo == NULL))
362 mode = afinfo->mode_map[encap];
363 if (unlikely(mode && !try_module_get(mode->owner)))
365 if (!mode && !modload_attempted) {
366 xfrm_state_put_afinfo(afinfo);
367 request_module("xfrm-mode-%d-%d", family, encap);
368 modload_attempted = 1;
372 xfrm_state_put_afinfo(afinfo);
376 static void xfrm_put_mode(struct xfrm_mode *mode)
378 module_put(mode->owner);
381 static void xfrm_state_gc_destroy(struct xfrm_state *x)
383 del_timer_sync(&x->timer);
384 del_timer_sync(&x->rtimer);
391 xfrm_put_mode(x->inner_mode);
392 if (x->inner_mode_iaf)
393 xfrm_put_mode(x->inner_mode_iaf);
395 xfrm_put_mode(x->outer_mode);
397 x->type->destructor(x);
398 xfrm_put_type(x->type);
400 security_xfrm_state_free(x);
404 static void xfrm_state_gc_task(struct work_struct *data)
406 struct xfrm_state *x;
407 struct hlist_node *entry, *tmp;
408 struct hlist_head gc_list;
410 spin_lock_bh(&xfrm_state_gc_lock);
411 gc_list.first = xfrm_state_gc_list.first;
412 INIT_HLIST_HEAD(&xfrm_state_gc_list);
413 spin_unlock_bh(&xfrm_state_gc_lock);
415 hlist_for_each_entry_safe(x, entry, tmp, &gc_list, bydst)
416 xfrm_state_gc_destroy(x);
421 static inline unsigned long make_jiffies(long secs)
423 if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
424 return MAX_SCHEDULE_TIMEOUT-1;
429 static void xfrm_timer_handler(unsigned long data)
431 struct xfrm_state *x = (struct xfrm_state*)data;
432 unsigned long now = get_seconds();
433 long next = LONG_MAX;
438 if (x->km.state == XFRM_STATE_DEAD)
440 if (x->km.state == XFRM_STATE_EXPIRED)
442 if (x->lft.hard_add_expires_seconds) {
443 long tmo = x->lft.hard_add_expires_seconds +
444 x->curlft.add_time - now;
450 if (x->lft.hard_use_expires_seconds) {
451 long tmo = x->lft.hard_use_expires_seconds +
452 (x->curlft.use_time ? : now) - now;
460 if (x->lft.soft_add_expires_seconds) {
461 long tmo = x->lft.soft_add_expires_seconds +
462 x->curlft.add_time - now;
468 if (x->lft.soft_use_expires_seconds) {
469 long tmo = x->lft.soft_use_expires_seconds +
470 (x->curlft.use_time ? : now) - now;
479 km_state_expired(x, 0, 0);
481 if (next != LONG_MAX)
482 mod_timer(&x->timer, jiffies + make_jiffies(next));
487 if (x->km.state == XFRM_STATE_ACQ && x->id.spi == 0) {
488 x->km.state = XFRM_STATE_EXPIRED;
494 err = __xfrm_state_delete(x);
495 if (!err && x->id.spi)
496 km_state_expired(x, 1, 0);
498 xfrm_audit_state_delete(x, err ? 0 : 1,
499 audit_get_loginuid(current), 0);
502 spin_unlock(&x->lock);
505 static void xfrm_replay_timer_handler(unsigned long data);
507 struct xfrm_state *xfrm_state_alloc(void)
509 struct xfrm_state *x;
511 x = kzalloc(sizeof(struct xfrm_state), GFP_ATOMIC);
514 atomic_set(&x->refcnt, 1);
515 atomic_set(&x->tunnel_users, 0);
516 INIT_LIST_HEAD(&x->all);
517 INIT_HLIST_NODE(&x->bydst);
518 INIT_HLIST_NODE(&x->bysrc);
519 INIT_HLIST_NODE(&x->byspi);
520 setup_timer(&x->timer, xfrm_timer_handler, (unsigned long)x);
521 setup_timer(&x->rtimer, xfrm_replay_timer_handler,
523 x->curlft.add_time = get_seconds();
524 x->lft.soft_byte_limit = XFRM_INF;
525 x->lft.soft_packet_limit = XFRM_INF;
526 x->lft.hard_byte_limit = XFRM_INF;
527 x->lft.hard_packet_limit = XFRM_INF;
528 x->replay_maxage = 0;
529 x->replay_maxdiff = 0;
530 x->inner_mode = NULL;
531 x->inner_mode_iaf = NULL;
532 spin_lock_init(&x->lock);
536 EXPORT_SYMBOL(xfrm_state_alloc);
538 void __xfrm_state_destroy(struct xfrm_state *x)
540 BUG_TRAP(x->km.state == XFRM_STATE_DEAD);
542 spin_lock_bh(&xfrm_state_lock);
544 spin_unlock_bh(&xfrm_state_lock);
546 spin_lock_bh(&xfrm_state_gc_lock);
547 hlist_add_head(&x->bydst, &xfrm_state_gc_list);
548 spin_unlock_bh(&xfrm_state_gc_lock);
549 schedule_work(&xfrm_state_gc_work);
551 EXPORT_SYMBOL(__xfrm_state_destroy);
553 int __xfrm_state_delete(struct xfrm_state *x)
557 if (x->km.state != XFRM_STATE_DEAD) {
558 x->km.state = XFRM_STATE_DEAD;
559 spin_lock(&xfrm_state_lock);
560 hlist_del(&x->bydst);
561 hlist_del(&x->bysrc);
563 hlist_del(&x->byspi);
565 spin_unlock(&xfrm_state_lock);
567 /* All xfrm_state objects are created by xfrm_state_alloc.
568 * The xfrm_state_alloc call gives a reference, and that
569 * is what we are dropping here.
577 EXPORT_SYMBOL(__xfrm_state_delete);
579 int xfrm_state_delete(struct xfrm_state *x)
583 spin_lock_bh(&x->lock);
584 err = __xfrm_state_delete(x);
585 spin_unlock_bh(&x->lock);
589 EXPORT_SYMBOL(xfrm_state_delete);
591 #ifdef CONFIG_SECURITY_NETWORK_XFRM
593 xfrm_state_flush_secctx_check(u8 proto, struct xfrm_audit *audit_info)
597 for (i = 0; i <= xfrm_state_hmask; i++) {
598 struct hlist_node *entry;
599 struct xfrm_state *x;
601 hlist_for_each_entry(x, entry, xfrm_state_bydst+i, bydst) {
602 if (xfrm_id_proto_match(x->id.proto, proto) &&
603 (err = security_xfrm_state_delete(x)) != 0) {
604 xfrm_audit_state_delete(x, 0,
605 audit_info->loginuid,
616 xfrm_state_flush_secctx_check(u8 proto, struct xfrm_audit *audit_info)
622 int xfrm_state_flush(u8 proto, struct xfrm_audit *audit_info)
626 spin_lock_bh(&xfrm_state_lock);
627 err = xfrm_state_flush_secctx_check(proto, audit_info);
631 for (i = 0; i <= xfrm_state_hmask; i++) {
632 struct hlist_node *entry;
633 struct xfrm_state *x;
635 hlist_for_each_entry(x, entry, xfrm_state_bydst+i, bydst) {
636 if (!xfrm_state_kern(x) &&
637 xfrm_id_proto_match(x->id.proto, proto)) {
639 spin_unlock_bh(&xfrm_state_lock);
641 err = xfrm_state_delete(x);
642 xfrm_audit_state_delete(x, err ? 0 : 1,
643 audit_info->loginuid,
647 spin_lock_bh(&xfrm_state_lock);
655 spin_unlock_bh(&xfrm_state_lock);
659 EXPORT_SYMBOL(xfrm_state_flush);
661 void xfrm_sad_getinfo(struct xfrmk_sadinfo *si)
663 spin_lock_bh(&xfrm_state_lock);
664 si->sadcnt = xfrm_state_num;
665 si->sadhcnt = xfrm_state_hmask;
666 si->sadhmcnt = xfrm_state_hashmax;
667 spin_unlock_bh(&xfrm_state_lock);
669 EXPORT_SYMBOL(xfrm_sad_getinfo);
672 xfrm_init_tempsel(struct xfrm_state *x, struct flowi *fl,
673 struct xfrm_tmpl *tmpl,
674 xfrm_address_t *daddr, xfrm_address_t *saddr,
675 unsigned short family)
677 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
680 afinfo->init_tempsel(x, fl, tmpl, daddr, saddr);
681 xfrm_state_put_afinfo(afinfo);
685 static struct xfrm_state *__xfrm_state_lookup(xfrm_address_t *daddr, __be32 spi, u8 proto, unsigned short family)
687 unsigned int h = xfrm_spi_hash(daddr, spi, proto, family);
688 struct xfrm_state *x;
689 struct hlist_node *entry;
691 hlist_for_each_entry(x, entry, xfrm_state_byspi+h, byspi) {
692 if (x->props.family != family ||
694 x->id.proto != proto)
699 if (x->id.daddr.a4 != daddr->a4)
703 if (!ipv6_addr_equal((struct in6_addr *)daddr,
717 static struct xfrm_state *__xfrm_state_lookup_byaddr(xfrm_address_t *daddr, xfrm_address_t *saddr, u8 proto, unsigned short family)
719 unsigned int h = xfrm_src_hash(daddr, saddr, family);
720 struct xfrm_state *x;
721 struct hlist_node *entry;
723 hlist_for_each_entry(x, entry, xfrm_state_bysrc+h, bysrc) {
724 if (x->props.family != family ||
725 x->id.proto != proto)
730 if (x->id.daddr.a4 != daddr->a4 ||
731 x->props.saddr.a4 != saddr->a4)
735 if (!ipv6_addr_equal((struct in6_addr *)daddr,
738 !ipv6_addr_equal((struct in6_addr *)saddr,
752 static inline struct xfrm_state *
753 __xfrm_state_locate(struct xfrm_state *x, int use_spi, int family)
756 return __xfrm_state_lookup(&x->id.daddr, x->id.spi,
757 x->id.proto, family);
759 return __xfrm_state_lookup_byaddr(&x->id.daddr,
761 x->id.proto, family);
764 static void xfrm_hash_grow_check(int have_hash_collision)
766 if (have_hash_collision &&
767 (xfrm_state_hmask + 1) < xfrm_state_hashmax &&
768 xfrm_state_num > xfrm_state_hmask)
769 schedule_work(&xfrm_hash_work);
773 xfrm_state_find(xfrm_address_t *daddr, xfrm_address_t *saddr,
774 struct flowi *fl, struct xfrm_tmpl *tmpl,
775 struct xfrm_policy *pol, int *err,
776 unsigned short family)
779 struct hlist_node *entry;
780 struct xfrm_state *x, *x0;
781 int acquire_in_progress = 0;
783 struct xfrm_state *best = NULL;
785 spin_lock_bh(&xfrm_state_lock);
786 h = xfrm_dst_hash(daddr, saddr, tmpl->reqid, family);
787 hlist_for_each_entry(x, entry, 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)) {
796 1. There is a valid state with matching selector.
798 2. Valid state with inappropriate selector. Skip.
800 Entering area of "sysdeps".
802 3. If state is not valid, selector is temporary,
803 it selects only session which triggered
804 previous resolution. Key manager will do
805 something to install a state with proper
808 if (x->km.state == XFRM_STATE_VALID) {
809 if ((x->sel.family && !xfrm_selector_match(&x->sel, fl, x->sel.family)) ||
810 !security_xfrm_state_pol_flow_match(x, pol, fl))
813 best->km.dying > x->km.dying ||
814 (best->km.dying == x->km.dying &&
815 best->curlft.add_time < x->curlft.add_time))
817 } else if (x->km.state == XFRM_STATE_ACQ) {
818 acquire_in_progress = 1;
819 } else if (x->km.state == XFRM_STATE_ERROR ||
820 x->km.state == XFRM_STATE_EXPIRED) {
821 if (xfrm_selector_match(&x->sel, fl, x->sel.family) &&
822 security_xfrm_state_pol_flow_match(x, pol, fl))
829 if (!x && !error && !acquire_in_progress) {
831 (x0 = __xfrm_state_lookup(daddr, tmpl->id.spi,
832 tmpl->id.proto, family)) != NULL) {
837 x = xfrm_state_alloc();
842 /* Initialize temporary selector matching only
843 * to current session. */
844 xfrm_init_tempsel(x, fl, tmpl, daddr, saddr, family);
846 error = security_xfrm_state_alloc_acquire(x, pol->security, fl->secid);
848 x->km.state = XFRM_STATE_DEAD;
854 if (km_query(x, tmpl, pol) == 0) {
855 x->km.state = XFRM_STATE_ACQ;
856 hlist_add_head(&x->bydst, xfrm_state_bydst+h);
857 h = xfrm_src_hash(daddr, saddr, family);
858 hlist_add_head(&x->bysrc, xfrm_state_bysrc+h);
860 h = xfrm_spi_hash(&x->id.daddr, x->id.spi, x->id.proto, family);
861 hlist_add_head(&x->byspi, xfrm_state_byspi+h);
863 x->lft.hard_add_expires_seconds = sysctl_xfrm_acq_expires;
864 x->timer.expires = jiffies + sysctl_xfrm_acq_expires*HZ;
865 add_timer(&x->timer);
867 xfrm_hash_grow_check(x->bydst.next != NULL);
869 x->km.state = XFRM_STATE_DEAD;
879 *err = acquire_in_progress ? -EAGAIN : error;
880 spin_unlock_bh(&xfrm_state_lock);
885 xfrm_stateonly_find(xfrm_address_t *daddr, xfrm_address_t *saddr,
886 unsigned short family, u8 mode, u8 proto, u32 reqid)
889 struct xfrm_state *rx = NULL, *x = NULL;
890 struct hlist_node *entry;
892 spin_lock(&xfrm_state_lock);
893 h = xfrm_dst_hash(daddr, saddr, reqid, family);
894 hlist_for_each_entry(x, entry, xfrm_state_bydst+h, bydst) {
895 if (x->props.family == family &&
896 x->props.reqid == reqid &&
897 !(x->props.flags & XFRM_STATE_WILDRECV) &&
898 xfrm_state_addr_check(x, daddr, saddr, family) &&
899 mode == x->props.mode &&
900 proto == x->id.proto &&
901 x->km.state == XFRM_STATE_VALID) {
909 spin_unlock(&xfrm_state_lock);
914 EXPORT_SYMBOL(xfrm_stateonly_find);
916 static void __xfrm_state_insert(struct xfrm_state *x)
920 x->genid = ++xfrm_state_genid;
922 list_add_tail(&x->all, &xfrm_state_all);
924 h = xfrm_dst_hash(&x->id.daddr, &x->props.saddr,
925 x->props.reqid, x->props.family);
926 hlist_add_head(&x->bydst, xfrm_state_bydst+h);
928 h = xfrm_src_hash(&x->id.daddr, &x->props.saddr, x->props.family);
929 hlist_add_head(&x->bysrc, xfrm_state_bysrc+h);
932 h = xfrm_spi_hash(&x->id.daddr, x->id.spi, x->id.proto,
935 hlist_add_head(&x->byspi, xfrm_state_byspi+h);
938 mod_timer(&x->timer, jiffies + HZ);
939 if (x->replay_maxage)
940 mod_timer(&x->rtimer, jiffies + x->replay_maxage);
946 xfrm_hash_grow_check(x->bydst.next != NULL);
949 /* xfrm_state_lock is held */
950 static void __xfrm_state_bump_genids(struct xfrm_state *xnew)
952 unsigned short family = xnew->props.family;
953 u32 reqid = xnew->props.reqid;
954 struct xfrm_state *x;
955 struct hlist_node *entry;
958 h = xfrm_dst_hash(&xnew->id.daddr, &xnew->props.saddr, reqid, family);
959 hlist_for_each_entry(x, entry, xfrm_state_bydst+h, bydst) {
960 if (x->props.family == family &&
961 x->props.reqid == reqid &&
962 !xfrm_addr_cmp(&x->id.daddr, &xnew->id.daddr, family) &&
963 !xfrm_addr_cmp(&x->props.saddr, &xnew->props.saddr, family))
964 x->genid = xfrm_state_genid;
968 void xfrm_state_insert(struct xfrm_state *x)
970 spin_lock_bh(&xfrm_state_lock);
971 __xfrm_state_bump_genids(x);
972 __xfrm_state_insert(x);
973 spin_unlock_bh(&xfrm_state_lock);
975 EXPORT_SYMBOL(xfrm_state_insert);
977 /* xfrm_state_lock is held */
978 static struct xfrm_state *__find_acq_core(unsigned short family, u8 mode, u32 reqid, u8 proto, xfrm_address_t *daddr, xfrm_address_t *saddr, int create)
980 unsigned int h = xfrm_dst_hash(daddr, saddr, reqid, family);
981 struct hlist_node *entry;
982 struct xfrm_state *x;
984 hlist_for_each_entry(x, entry, xfrm_state_bydst+h, bydst) {
985 if (x->props.reqid != reqid ||
986 x->props.mode != mode ||
987 x->props.family != family ||
988 x->km.state != XFRM_STATE_ACQ ||
990 x->id.proto != proto)
995 if (x->id.daddr.a4 != daddr->a4 ||
996 x->props.saddr.a4 != saddr->a4)
1000 if (!ipv6_addr_equal((struct in6_addr *)x->id.daddr.a6,
1001 (struct in6_addr *)daddr) ||
1002 !ipv6_addr_equal((struct in6_addr *)
1004 (struct in6_addr *)saddr))
1016 x = xfrm_state_alloc();
1020 x->sel.daddr.a4 = daddr->a4;
1021 x->sel.saddr.a4 = saddr->a4;
1022 x->sel.prefixlen_d = 32;
1023 x->sel.prefixlen_s = 32;
1024 x->props.saddr.a4 = saddr->a4;
1025 x->id.daddr.a4 = daddr->a4;
1029 ipv6_addr_copy((struct in6_addr *)x->sel.daddr.a6,
1030 (struct in6_addr *)daddr);
1031 ipv6_addr_copy((struct in6_addr *)x->sel.saddr.a6,
1032 (struct in6_addr *)saddr);
1033 x->sel.prefixlen_d = 128;
1034 x->sel.prefixlen_s = 128;
1035 ipv6_addr_copy((struct in6_addr *)x->props.saddr.a6,
1036 (struct in6_addr *)saddr);
1037 ipv6_addr_copy((struct in6_addr *)x->id.daddr.a6,
1038 (struct in6_addr *)daddr);
1042 x->km.state = XFRM_STATE_ACQ;
1043 x->id.proto = proto;
1044 x->props.family = family;
1045 x->props.mode = mode;
1046 x->props.reqid = reqid;
1047 x->lft.hard_add_expires_seconds = sysctl_xfrm_acq_expires;
1049 x->timer.expires = jiffies + sysctl_xfrm_acq_expires*HZ;
1050 add_timer(&x->timer);
1051 hlist_add_head(&x->bydst, xfrm_state_bydst+h);
1052 h = xfrm_src_hash(daddr, saddr, family);
1053 hlist_add_head(&x->bysrc, xfrm_state_bysrc+h);
1057 xfrm_hash_grow_check(x->bydst.next != NULL);
1063 static struct xfrm_state *__xfrm_find_acq_byseq(u32 seq);
1065 int xfrm_state_add(struct xfrm_state *x)
1067 struct xfrm_state *x1;
1070 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
1072 family = x->props.family;
1074 spin_lock_bh(&xfrm_state_lock);
1076 x1 = __xfrm_state_locate(x, use_spi, family);
1084 if (use_spi && x->km.seq) {
1085 x1 = __xfrm_find_acq_byseq(x->km.seq);
1086 if (x1 && ((x1->id.proto != x->id.proto) ||
1087 xfrm_addr_cmp(&x1->id.daddr, &x->id.daddr, family))) {
1094 x1 = __find_acq_core(family, x->props.mode, x->props.reqid,
1096 &x->id.daddr, &x->props.saddr, 0);
1098 __xfrm_state_bump_genids(x);
1099 __xfrm_state_insert(x);
1103 spin_unlock_bh(&xfrm_state_lock);
1106 xfrm_state_delete(x1);
1112 EXPORT_SYMBOL(xfrm_state_add);
1114 #ifdef CONFIG_XFRM_MIGRATE
1115 static struct xfrm_state *xfrm_state_clone(struct xfrm_state *orig, int *errp)
1118 struct xfrm_state *x = xfrm_state_alloc();
1122 memcpy(&x->id, &orig->id, sizeof(x->id));
1123 memcpy(&x->sel, &orig->sel, sizeof(x->sel));
1124 memcpy(&x->lft, &orig->lft, sizeof(x->lft));
1125 x->props.mode = orig->props.mode;
1126 x->props.replay_window = orig->props.replay_window;
1127 x->props.reqid = orig->props.reqid;
1128 x->props.family = orig->props.family;
1129 x->props.saddr = orig->props.saddr;
1132 x->aalg = xfrm_algo_clone(orig->aalg);
1136 x->props.aalgo = orig->props.aalgo;
1139 x->ealg = xfrm_algo_clone(orig->ealg);
1143 x->props.ealgo = orig->props.ealgo;
1146 x->calg = xfrm_algo_clone(orig->calg);
1150 x->props.calgo = orig->props.calgo;
1153 x->encap = kmemdup(orig->encap, sizeof(*x->encap), GFP_KERNEL);
1159 x->coaddr = kmemdup(orig->coaddr, sizeof(*x->coaddr),
1165 err = xfrm_init_state(x);
1169 x->props.flags = orig->props.flags;
1171 x->curlft.add_time = orig->curlft.add_time;
1172 x->km.state = orig->km.state;
1173 x->km.seq = orig->km.seq;
1191 /* xfrm_state_lock is held */
1192 struct xfrm_state * xfrm_migrate_state_find(struct xfrm_migrate *m)
1195 struct xfrm_state *x;
1196 struct hlist_node *entry;
1199 h = xfrm_dst_hash(&m->old_daddr, &m->old_saddr,
1200 m->reqid, m->old_family);
1201 hlist_for_each_entry(x, entry, xfrm_state_bydst+h, bydst) {
1202 if (x->props.mode != m->mode ||
1203 x->id.proto != m->proto)
1205 if (m->reqid && x->props.reqid != m->reqid)
1207 if (xfrm_addr_cmp(&x->id.daddr, &m->old_daddr,
1209 xfrm_addr_cmp(&x->props.saddr, &m->old_saddr,
1216 h = xfrm_src_hash(&m->old_daddr, &m->old_saddr,
1218 hlist_for_each_entry(x, entry, xfrm_state_bysrc+h, bysrc) {
1219 if (x->props.mode != m->mode ||
1220 x->id.proto != m->proto)
1222 if (xfrm_addr_cmp(&x->id.daddr, &m->old_daddr,
1224 xfrm_addr_cmp(&x->props.saddr, &m->old_saddr,
1234 EXPORT_SYMBOL(xfrm_migrate_state_find);
1236 struct xfrm_state * xfrm_state_migrate(struct xfrm_state *x,
1237 struct xfrm_migrate *m)
1239 struct xfrm_state *xc;
1242 xc = xfrm_state_clone(x, &err);
1246 memcpy(&xc->id.daddr, &m->new_daddr, sizeof(xc->id.daddr));
1247 memcpy(&xc->props.saddr, &m->new_saddr, sizeof(xc->props.saddr));
1250 if (!xfrm_addr_cmp(&x->id.daddr, &m->new_daddr, m->new_family)) {
1251 /* a care is needed when the destination address of the
1252 state is to be updated as it is a part of triplet */
1253 xfrm_state_insert(xc);
1255 if ((err = xfrm_state_add(xc)) < 0)
1264 EXPORT_SYMBOL(xfrm_state_migrate);
1267 int xfrm_state_update(struct xfrm_state *x)
1269 struct xfrm_state *x1;
1271 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
1273 spin_lock_bh(&xfrm_state_lock);
1274 x1 = __xfrm_state_locate(x, use_spi, x->props.family);
1280 if (xfrm_state_kern(x1)) {
1286 if (x1->km.state == XFRM_STATE_ACQ) {
1287 __xfrm_state_insert(x);
1293 spin_unlock_bh(&xfrm_state_lock);
1299 xfrm_state_delete(x1);
1305 spin_lock_bh(&x1->lock);
1306 if (likely(x1->km.state == XFRM_STATE_VALID)) {
1307 if (x->encap && x1->encap)
1308 memcpy(x1->encap, x->encap, sizeof(*x1->encap));
1309 if (x->coaddr && x1->coaddr) {
1310 memcpy(x1->coaddr, x->coaddr, sizeof(*x1->coaddr));
1312 if (!use_spi && memcmp(&x1->sel, &x->sel, sizeof(x1->sel)))
1313 memcpy(&x1->sel, &x->sel, sizeof(x1->sel));
1314 memcpy(&x1->lft, &x->lft, sizeof(x1->lft));
1317 mod_timer(&x1->timer, jiffies + HZ);
1318 if (x1->curlft.use_time)
1319 xfrm_state_check_expire(x1);
1323 spin_unlock_bh(&x1->lock);
1329 EXPORT_SYMBOL(xfrm_state_update);
1331 int xfrm_state_check_expire(struct xfrm_state *x)
1333 if (!x->curlft.use_time)
1334 x->curlft.use_time = get_seconds();
1336 if (x->km.state != XFRM_STATE_VALID)
1339 if (x->curlft.bytes >= x->lft.hard_byte_limit ||
1340 x->curlft.packets >= x->lft.hard_packet_limit) {
1341 x->km.state = XFRM_STATE_EXPIRED;
1342 mod_timer(&x->timer, jiffies);
1347 (x->curlft.bytes >= x->lft.soft_byte_limit ||
1348 x->curlft.packets >= x->lft.soft_packet_limit)) {
1350 km_state_expired(x, 0, 0);
1354 EXPORT_SYMBOL(xfrm_state_check_expire);
1357 xfrm_state_lookup(xfrm_address_t *daddr, __be32 spi, u8 proto,
1358 unsigned short family)
1360 struct xfrm_state *x;
1362 spin_lock_bh(&xfrm_state_lock);
1363 x = __xfrm_state_lookup(daddr, spi, proto, family);
1364 spin_unlock_bh(&xfrm_state_lock);
1367 EXPORT_SYMBOL(xfrm_state_lookup);
1370 xfrm_state_lookup_byaddr(xfrm_address_t *daddr, xfrm_address_t *saddr,
1371 u8 proto, unsigned short family)
1373 struct xfrm_state *x;
1375 spin_lock_bh(&xfrm_state_lock);
1376 x = __xfrm_state_lookup_byaddr(daddr, saddr, proto, family);
1377 spin_unlock_bh(&xfrm_state_lock);
1380 EXPORT_SYMBOL(xfrm_state_lookup_byaddr);
1383 xfrm_find_acq(u8 mode, u32 reqid, u8 proto,
1384 xfrm_address_t *daddr, xfrm_address_t *saddr,
1385 int create, unsigned short family)
1387 struct xfrm_state *x;
1389 spin_lock_bh(&xfrm_state_lock);
1390 x = __find_acq_core(family, mode, reqid, proto, daddr, saddr, create);
1391 spin_unlock_bh(&xfrm_state_lock);
1395 EXPORT_SYMBOL(xfrm_find_acq);
1397 #ifdef CONFIG_XFRM_SUB_POLICY
1399 xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n,
1400 unsigned short family)
1403 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
1405 return -EAFNOSUPPORT;
1407 spin_lock_bh(&xfrm_state_lock);
1408 if (afinfo->tmpl_sort)
1409 err = afinfo->tmpl_sort(dst, src, n);
1410 spin_unlock_bh(&xfrm_state_lock);
1411 xfrm_state_put_afinfo(afinfo);
1414 EXPORT_SYMBOL(xfrm_tmpl_sort);
1417 xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src, int n,
1418 unsigned short family)
1421 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
1423 return -EAFNOSUPPORT;
1425 spin_lock_bh(&xfrm_state_lock);
1426 if (afinfo->state_sort)
1427 err = afinfo->state_sort(dst, src, n);
1428 spin_unlock_bh(&xfrm_state_lock);
1429 xfrm_state_put_afinfo(afinfo);
1432 EXPORT_SYMBOL(xfrm_state_sort);
1435 /* Silly enough, but I'm lazy to build resolution list */
1437 static struct xfrm_state *__xfrm_find_acq_byseq(u32 seq)
1441 for (i = 0; i <= xfrm_state_hmask; i++) {
1442 struct hlist_node *entry;
1443 struct xfrm_state *x;
1445 hlist_for_each_entry(x, entry, xfrm_state_bydst+i, bydst) {
1446 if (x->km.seq == seq &&
1447 x->km.state == XFRM_STATE_ACQ) {
1456 struct xfrm_state *xfrm_find_acq_byseq(u32 seq)
1458 struct xfrm_state *x;
1460 spin_lock_bh(&xfrm_state_lock);
1461 x = __xfrm_find_acq_byseq(seq);
1462 spin_unlock_bh(&xfrm_state_lock);
1465 EXPORT_SYMBOL(xfrm_find_acq_byseq);
1467 u32 xfrm_get_acqseq(void)
1471 static DEFINE_SPINLOCK(acqseq_lock);
1473 spin_lock_bh(&acqseq_lock);
1474 res = (++acqseq ? : ++acqseq);
1475 spin_unlock_bh(&acqseq_lock);
1478 EXPORT_SYMBOL(xfrm_get_acqseq);
1480 int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high)
1483 struct xfrm_state *x0;
1485 __be32 minspi = htonl(low);
1486 __be32 maxspi = htonl(high);
1488 spin_lock_bh(&x->lock);
1489 if (x->km.state == XFRM_STATE_DEAD)
1498 if (minspi == maxspi) {
1499 x0 = xfrm_state_lookup(&x->id.daddr, minspi, x->id.proto, x->props.family);
1507 for (h=0; h<high-low+1; h++) {
1508 spi = low + net_random()%(high-low+1);
1509 x0 = xfrm_state_lookup(&x->id.daddr, htonl(spi), x->id.proto, x->props.family);
1511 x->id.spi = htonl(spi);
1518 spin_lock_bh(&xfrm_state_lock);
1519 h = xfrm_spi_hash(&x->id.daddr, x->id.spi, x->id.proto, x->props.family);
1520 hlist_add_head(&x->byspi, xfrm_state_byspi+h);
1521 spin_unlock_bh(&xfrm_state_lock);
1527 spin_unlock_bh(&x->lock);
1531 EXPORT_SYMBOL(xfrm_alloc_spi);
1533 int xfrm_state_walk(struct xfrm_state_walk *walk,
1534 int (*func)(struct xfrm_state *, int, void*),
1537 struct xfrm_state *old, *x, *last = NULL;
1540 if (walk->state == NULL && walk->count != 0)
1543 old = x = walk->state;
1545 spin_lock_bh(&xfrm_state_lock);
1547 x = list_first_entry(&xfrm_state_all, struct xfrm_state, all);
1548 list_for_each_entry_from(x, &xfrm_state_all, all) {
1549 if (x->km.state == XFRM_STATE_DEAD)
1551 if (!xfrm_id_proto_match(x->id.proto, walk->proto))
1554 err = func(last, walk->count, data);
1556 xfrm_state_hold(last);
1564 if (walk->count == 0) {
1569 err = func(last, 0, data);
1571 spin_unlock_bh(&xfrm_state_lock);
1573 xfrm_state_put(old);
1576 EXPORT_SYMBOL(xfrm_state_walk);
1579 void xfrm_replay_notify(struct xfrm_state *x, int event)
1582 /* we send notify messages in case
1583 * 1. we updated on of the sequence numbers, and the seqno difference
1584 * is at least x->replay_maxdiff, in this case we also update the
1585 * timeout of our timer function
1586 * 2. if x->replay_maxage has elapsed since last update,
1587 * and there were changes
1589 * The state structure must be locked!
1593 case XFRM_REPLAY_UPDATE:
1594 if (x->replay_maxdiff &&
1595 (x->replay.seq - x->preplay.seq < x->replay_maxdiff) &&
1596 (x->replay.oseq - x->preplay.oseq < x->replay_maxdiff)) {
1597 if (x->xflags & XFRM_TIME_DEFER)
1598 event = XFRM_REPLAY_TIMEOUT;
1605 case XFRM_REPLAY_TIMEOUT:
1606 if ((x->replay.seq == x->preplay.seq) &&
1607 (x->replay.bitmap == x->preplay.bitmap) &&
1608 (x->replay.oseq == x->preplay.oseq)) {
1609 x->xflags |= XFRM_TIME_DEFER;
1616 memcpy(&x->preplay, &x->replay, sizeof(struct xfrm_replay_state));
1617 c.event = XFRM_MSG_NEWAE;
1618 c.data.aevent = event;
1619 km_state_notify(x, &c);
1621 if (x->replay_maxage &&
1622 !mod_timer(&x->rtimer, jiffies + x->replay_maxage))
1623 x->xflags &= ~XFRM_TIME_DEFER;
1626 static void xfrm_replay_timer_handler(unsigned long data)
1628 struct xfrm_state *x = (struct xfrm_state*)data;
1630 spin_lock(&x->lock);
1632 if (x->km.state == XFRM_STATE_VALID) {
1633 if (xfrm_aevent_is_on())
1634 xfrm_replay_notify(x, XFRM_REPLAY_TIMEOUT);
1636 x->xflags |= XFRM_TIME_DEFER;
1639 spin_unlock(&x->lock);
1642 int xfrm_replay_check(struct xfrm_state *x,
1643 struct sk_buff *skb, __be32 net_seq)
1646 u32 seq = ntohl(net_seq);
1648 if (unlikely(seq == 0))
1651 if (likely(seq > x->replay.seq))
1654 diff = x->replay.seq - seq;
1655 if (diff >= min_t(unsigned int, x->props.replay_window,
1656 sizeof(x->replay.bitmap) * 8)) {
1657 x->stats.replay_window++;
1661 if (x->replay.bitmap & (1U << diff)) {
1668 xfrm_audit_state_replay(x, skb, net_seq);
1672 void xfrm_replay_advance(struct xfrm_state *x, __be32 net_seq)
1675 u32 seq = ntohl(net_seq);
1677 if (seq > x->replay.seq) {
1678 diff = seq - x->replay.seq;
1679 if (diff < x->props.replay_window)
1680 x->replay.bitmap = ((x->replay.bitmap) << diff) | 1;
1682 x->replay.bitmap = 1;
1683 x->replay.seq = seq;
1685 diff = x->replay.seq - seq;
1686 x->replay.bitmap |= (1U << diff);
1689 if (xfrm_aevent_is_on())
1690 xfrm_replay_notify(x, XFRM_REPLAY_UPDATE);
1693 static LIST_HEAD(xfrm_km_list);
1694 static DEFINE_RWLOCK(xfrm_km_lock);
1696 void km_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
1698 struct xfrm_mgr *km;
1700 read_lock(&xfrm_km_lock);
1701 list_for_each_entry(km, &xfrm_km_list, list)
1702 if (km->notify_policy)
1703 km->notify_policy(xp, dir, c);
1704 read_unlock(&xfrm_km_lock);
1707 void km_state_notify(struct xfrm_state *x, struct km_event *c)
1709 struct xfrm_mgr *km;
1710 read_lock(&xfrm_km_lock);
1711 list_for_each_entry(km, &xfrm_km_list, list)
1714 read_unlock(&xfrm_km_lock);
1717 EXPORT_SYMBOL(km_policy_notify);
1718 EXPORT_SYMBOL(km_state_notify);
1720 void km_state_expired(struct xfrm_state *x, int hard, u32 pid)
1726 c.event = XFRM_MSG_EXPIRE;
1727 km_state_notify(x, &c);
1733 EXPORT_SYMBOL(km_state_expired);
1735 * We send to all registered managers regardless of failure
1736 * We are happy with one success
1738 int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol)
1740 int err = -EINVAL, acqret;
1741 struct xfrm_mgr *km;
1743 read_lock(&xfrm_km_lock);
1744 list_for_each_entry(km, &xfrm_km_list, list) {
1745 acqret = km->acquire(x, t, pol, XFRM_POLICY_OUT);
1749 read_unlock(&xfrm_km_lock);
1752 EXPORT_SYMBOL(km_query);
1754 int km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
1757 struct xfrm_mgr *km;
1759 read_lock(&xfrm_km_lock);
1760 list_for_each_entry(km, &xfrm_km_list, list) {
1761 if (km->new_mapping)
1762 err = km->new_mapping(x, ipaddr, sport);
1766 read_unlock(&xfrm_km_lock);
1769 EXPORT_SYMBOL(km_new_mapping);
1771 void km_policy_expired(struct xfrm_policy *pol, int dir, int hard, u32 pid)
1777 c.event = XFRM_MSG_POLEXPIRE;
1778 km_policy_notify(pol, dir, &c);
1783 EXPORT_SYMBOL(km_policy_expired);
1785 #ifdef CONFIG_XFRM_MIGRATE
1786 int km_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
1787 struct xfrm_migrate *m, int num_migrate)
1791 struct xfrm_mgr *km;
1793 read_lock(&xfrm_km_lock);
1794 list_for_each_entry(km, &xfrm_km_list, list) {
1796 ret = km->migrate(sel, dir, type, m, num_migrate);
1801 read_unlock(&xfrm_km_lock);
1804 EXPORT_SYMBOL(km_migrate);
1807 int km_report(u8 proto, struct xfrm_selector *sel, xfrm_address_t *addr)
1811 struct xfrm_mgr *km;
1813 read_lock(&xfrm_km_lock);
1814 list_for_each_entry(km, &xfrm_km_list, list) {
1816 ret = km->report(proto, sel, addr);
1821 read_unlock(&xfrm_km_lock);
1824 EXPORT_SYMBOL(km_report);
1826 int xfrm_user_policy(struct sock *sk, int optname, u8 __user *optval, int optlen)
1830 struct xfrm_mgr *km;
1831 struct xfrm_policy *pol = NULL;
1833 if (optlen <= 0 || optlen > PAGE_SIZE)
1836 data = kmalloc(optlen, GFP_KERNEL);
1841 if (copy_from_user(data, optval, optlen))
1845 read_lock(&xfrm_km_lock);
1846 list_for_each_entry(km, &xfrm_km_list, list) {
1847 pol = km->compile_policy(sk, optname, data,
1852 read_unlock(&xfrm_km_lock);
1855 xfrm_sk_policy_insert(sk, err, pol);
1864 EXPORT_SYMBOL(xfrm_user_policy);
1866 int xfrm_register_km(struct xfrm_mgr *km)
1868 write_lock_bh(&xfrm_km_lock);
1869 list_add_tail(&km->list, &xfrm_km_list);
1870 write_unlock_bh(&xfrm_km_lock);
1873 EXPORT_SYMBOL(xfrm_register_km);
1875 int xfrm_unregister_km(struct xfrm_mgr *km)
1877 write_lock_bh(&xfrm_km_lock);
1878 list_del(&km->list);
1879 write_unlock_bh(&xfrm_km_lock);
1882 EXPORT_SYMBOL(xfrm_unregister_km);
1884 int xfrm_state_register_afinfo(struct xfrm_state_afinfo *afinfo)
1887 if (unlikely(afinfo == NULL))
1889 if (unlikely(afinfo->family >= NPROTO))
1890 return -EAFNOSUPPORT;
1891 write_lock_bh(&xfrm_state_afinfo_lock);
1892 if (unlikely(xfrm_state_afinfo[afinfo->family] != NULL))
1895 xfrm_state_afinfo[afinfo->family] = afinfo;
1896 write_unlock_bh(&xfrm_state_afinfo_lock);
1899 EXPORT_SYMBOL(xfrm_state_register_afinfo);
1901 int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo)
1904 if (unlikely(afinfo == NULL))
1906 if (unlikely(afinfo->family >= NPROTO))
1907 return -EAFNOSUPPORT;
1908 write_lock_bh(&xfrm_state_afinfo_lock);
1909 if (likely(xfrm_state_afinfo[afinfo->family] != NULL)) {
1910 if (unlikely(xfrm_state_afinfo[afinfo->family] != afinfo))
1913 xfrm_state_afinfo[afinfo->family] = NULL;
1915 write_unlock_bh(&xfrm_state_afinfo_lock);
1918 EXPORT_SYMBOL(xfrm_state_unregister_afinfo);
1920 static struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family)
1922 struct xfrm_state_afinfo *afinfo;
1923 if (unlikely(family >= NPROTO))
1925 read_lock(&xfrm_state_afinfo_lock);
1926 afinfo = xfrm_state_afinfo[family];
1927 if (unlikely(!afinfo))
1928 read_unlock(&xfrm_state_afinfo_lock);
1932 static void xfrm_state_put_afinfo(struct xfrm_state_afinfo *afinfo)
1933 __releases(xfrm_state_afinfo_lock)
1935 read_unlock(&xfrm_state_afinfo_lock);
1938 /* Temporarily located here until net/xfrm/xfrm_tunnel.c is created */
1939 void xfrm_state_delete_tunnel(struct xfrm_state *x)
1942 struct xfrm_state *t = x->tunnel;
1944 if (atomic_read(&t->tunnel_users) == 2)
1945 xfrm_state_delete(t);
1946 atomic_dec(&t->tunnel_users);
1951 EXPORT_SYMBOL(xfrm_state_delete_tunnel);
1953 int xfrm_state_mtu(struct xfrm_state *x, int mtu)
1957 spin_lock_bh(&x->lock);
1958 if (x->km.state == XFRM_STATE_VALID &&
1959 x->type && x->type->get_mtu)
1960 res = x->type->get_mtu(x, mtu);
1962 res = mtu - x->props.header_len;
1963 spin_unlock_bh(&x->lock);
1967 int xfrm_init_state(struct xfrm_state *x)
1969 struct xfrm_state_afinfo *afinfo;
1970 struct xfrm_mode *inner_mode;
1971 int family = x->props.family;
1974 err = -EAFNOSUPPORT;
1975 afinfo = xfrm_state_get_afinfo(family);
1980 if (afinfo->init_flags)
1981 err = afinfo->init_flags(x);
1983 xfrm_state_put_afinfo(afinfo);
1988 err = -EPROTONOSUPPORT;
1990 if (x->sel.family != AF_UNSPEC) {
1991 inner_mode = xfrm_get_mode(x->props.mode, x->sel.family);
1992 if (inner_mode == NULL)
1995 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL) &&
1996 family != x->sel.family) {
1997 xfrm_put_mode(inner_mode);
2001 x->inner_mode = inner_mode;
2003 struct xfrm_mode *inner_mode_iaf;
2005 inner_mode = xfrm_get_mode(x->props.mode, AF_INET);
2006 if (inner_mode == NULL)
2009 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL)) {
2010 xfrm_put_mode(inner_mode);
2014 inner_mode_iaf = xfrm_get_mode(x->props.mode, AF_INET6);
2015 if (inner_mode_iaf == NULL)
2018 if (!(inner_mode_iaf->flags & XFRM_MODE_FLAG_TUNNEL)) {
2019 xfrm_put_mode(inner_mode_iaf);
2023 if (x->props.family == AF_INET) {
2024 x->inner_mode = inner_mode;
2025 x->inner_mode_iaf = inner_mode_iaf;
2027 x->inner_mode = inner_mode_iaf;
2028 x->inner_mode_iaf = inner_mode;
2032 x->type = xfrm_get_type(x->id.proto, family);
2033 if (x->type == NULL)
2036 err = x->type->init_state(x);
2040 x->outer_mode = xfrm_get_mode(x->props.mode, family);
2041 if (x->outer_mode == NULL)
2044 x->km.state = XFRM_STATE_VALID;
2050 EXPORT_SYMBOL(xfrm_init_state);
2052 void __init xfrm_state_init(void)
2056 sz = sizeof(struct hlist_head) * 8;
2058 xfrm_state_bydst = xfrm_hash_alloc(sz);
2059 xfrm_state_bysrc = xfrm_hash_alloc(sz);
2060 xfrm_state_byspi = xfrm_hash_alloc(sz);
2061 if (!xfrm_state_bydst || !xfrm_state_bysrc || !xfrm_state_byspi)
2062 panic("XFRM: Cannot allocate bydst/bysrc/byspi hashes.");
2063 xfrm_state_hmask = ((sz / sizeof(struct hlist_head)) - 1);
2065 INIT_WORK(&xfrm_state_gc_work, xfrm_state_gc_task);
2068 #ifdef CONFIG_AUDITSYSCALL
2069 static void xfrm_audit_helper_sainfo(struct xfrm_state *x,
2070 struct audit_buffer *audit_buf)
2072 struct xfrm_sec_ctx *ctx = x->security;
2073 u32 spi = ntohl(x->id.spi);
2076 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
2077 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
2079 switch(x->props.family) {
2081 audit_log_format(audit_buf,
2082 " src=" NIPQUAD_FMT " dst=" NIPQUAD_FMT,
2083 NIPQUAD(x->props.saddr.a4),
2084 NIPQUAD(x->id.daddr.a4));
2087 audit_log_format(audit_buf,
2088 " src=" NIP6_FMT " dst=" NIP6_FMT,
2089 NIP6(*(struct in6_addr *)x->props.saddr.a6),
2090 NIP6(*(struct in6_addr *)x->id.daddr.a6));
2094 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
2097 static void xfrm_audit_helper_pktinfo(struct sk_buff *skb, u16 family,
2098 struct audit_buffer *audit_buf)
2101 struct ipv6hdr *iph6;
2106 audit_log_format(audit_buf,
2107 " src=" NIPQUAD_FMT " dst=" NIPQUAD_FMT,
2108 NIPQUAD(iph4->saddr),
2109 NIPQUAD(iph4->daddr));
2112 iph6 = ipv6_hdr(skb);
2113 audit_log_format(audit_buf,
2114 " src=" NIP6_FMT " dst=" NIP6_FMT
2115 " flowlbl=0x%x%x%x",
2118 iph6->flow_lbl[0] & 0x0f,
2125 void xfrm_audit_state_add(struct xfrm_state *x, int result,
2126 u32 auid, u32 secid)
2128 struct audit_buffer *audit_buf;
2130 audit_buf = xfrm_audit_start("SAD-add");
2131 if (audit_buf == NULL)
2133 xfrm_audit_helper_usrinfo(auid, 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_add);
2140 void xfrm_audit_state_delete(struct xfrm_state *x, int result,
2141 u32 auid, u32 secid)
2143 struct audit_buffer *audit_buf;
2145 audit_buf = xfrm_audit_start("SAD-delete");
2146 if (audit_buf == NULL)
2148 xfrm_audit_helper_usrinfo(auid, secid, audit_buf);
2149 xfrm_audit_helper_sainfo(x, audit_buf);
2150 audit_log_format(audit_buf, " res=%u", result);
2151 audit_log_end(audit_buf);
2153 EXPORT_SYMBOL_GPL(xfrm_audit_state_delete);
2155 void xfrm_audit_state_replay_overflow(struct xfrm_state *x,
2156 struct sk_buff *skb)
2158 struct audit_buffer *audit_buf;
2161 audit_buf = xfrm_audit_start("SA-replay-overflow");
2162 if (audit_buf == NULL)
2164 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2165 /* don't record the sequence number because it's inherent in this kind
2166 * of audit message */
2167 spi = ntohl(x->id.spi);
2168 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
2169 audit_log_end(audit_buf);
2171 EXPORT_SYMBOL_GPL(xfrm_audit_state_replay_overflow);
2173 static void xfrm_audit_state_replay(struct xfrm_state *x,
2174 struct sk_buff *skb, __be32 net_seq)
2176 struct audit_buffer *audit_buf;
2179 audit_buf = xfrm_audit_start("SA-replayed-pkt");
2180 if (audit_buf == NULL)
2182 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2183 spi = ntohl(x->id.spi);
2184 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2185 spi, spi, ntohl(net_seq));
2186 audit_log_end(audit_buf);
2189 void xfrm_audit_state_notfound_simple(struct sk_buff *skb, u16 family)
2191 struct audit_buffer *audit_buf;
2193 audit_buf = xfrm_audit_start("SA-notfound");
2194 if (audit_buf == NULL)
2196 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2197 audit_log_end(audit_buf);
2199 EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound_simple);
2201 void xfrm_audit_state_notfound(struct sk_buff *skb, u16 family,
2202 __be32 net_spi, __be32 net_seq)
2204 struct audit_buffer *audit_buf;
2207 audit_buf = xfrm_audit_start("SA-notfound");
2208 if (audit_buf == NULL)
2210 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2211 spi = ntohl(net_spi);
2212 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2213 spi, spi, ntohl(net_seq));
2214 audit_log_end(audit_buf);
2216 EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound);
2218 void xfrm_audit_state_icvfail(struct xfrm_state *x,
2219 struct sk_buff *skb, u8 proto)
2221 struct audit_buffer *audit_buf;
2225 audit_buf = xfrm_audit_start("SA-icv-failure");
2226 if (audit_buf == NULL)
2228 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2229 if (xfrm_parse_spi(skb, proto, &net_spi, &net_seq) == 0) {
2230 u32 spi = ntohl(net_spi);
2231 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2232 spi, spi, ntohl(net_seq));
2234 audit_log_end(audit_buf);
2236 EXPORT_SYMBOL_GPL(xfrm_audit_state_icvfail);
2237 #endif /* CONFIG_AUDITSYSCALL */