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 <asm/uaccess.h>
24 EXPORT_SYMBOL(xfrm_nl);
26 u32 sysctl_xfrm_aevent_etime = XFRM_AE_ETIME;
27 EXPORT_SYMBOL(sysctl_xfrm_aevent_etime);
29 u32 sysctl_xfrm_aevent_rseqth = XFRM_AE_SEQT_SIZE;
30 EXPORT_SYMBOL(sysctl_xfrm_aevent_rseqth);
32 /* Each xfrm_state may be linked to two tables:
34 1. Hash table by (spi,daddr,ah/esp) to find SA by SPI. (input,ctl)
35 2. Hash table by daddr to find what SAs exist for given
36 destination/tunnel endpoint. (output)
39 static DEFINE_SPINLOCK(xfrm_state_lock);
41 #define XFRM_DST_HSIZE 1024
43 /* Hash table to find appropriate SA towards given target (endpoint
44 * of tunnel or destination of transport mode) allowed by selector.
46 * Main use is finding SA after policy selected tunnel or transport mode.
47 * Also, it can be used by ah/esp icmp error handler to find offending SA.
49 static struct hlist_head xfrm_state_bydst[XFRM_DST_HSIZE];
50 static struct hlist_head xfrm_state_bysrc[XFRM_DST_HSIZE];
51 static struct hlist_head xfrm_state_byspi[XFRM_DST_HSIZE];
54 unsigned __xfrm4_dst_hash(xfrm_address_t *addr)
58 h = (h ^ (h>>16)) % XFRM_DST_HSIZE;
63 unsigned __xfrm6_dst_hash(xfrm_address_t *addr)
66 h = ntohl(addr->a6[2]^addr->a6[3]);
67 h = (h ^ (h>>16)) % XFRM_DST_HSIZE;
72 unsigned __xfrm4_src_hash(xfrm_address_t *addr)
74 return __xfrm4_dst_hash(addr);
78 unsigned __xfrm6_src_hash(xfrm_address_t *addr)
80 return __xfrm6_dst_hash(addr);
84 unsigned xfrm_src_hash(xfrm_address_t *addr, unsigned short family)
88 return __xfrm4_src_hash(addr);
90 return __xfrm6_src_hash(addr);
96 unsigned xfrm_dst_hash(xfrm_address_t *addr, unsigned short family)
100 return __xfrm4_dst_hash(addr);
102 return __xfrm6_dst_hash(addr);
108 unsigned __xfrm4_spi_hash(xfrm_address_t *addr, u32 spi, u8 proto)
111 h = ntohl(addr->a4^spi^proto);
112 h = (h ^ (h>>10) ^ (h>>20)) % XFRM_DST_HSIZE;
117 unsigned __xfrm6_spi_hash(xfrm_address_t *addr, u32 spi, u8 proto)
120 h = ntohl(addr->a6[2]^addr->a6[3]^spi^proto);
121 h = (h ^ (h>>10) ^ (h>>20)) % XFRM_DST_HSIZE;
126 unsigned xfrm_spi_hash(xfrm_address_t *addr, u32 spi, u8 proto, unsigned short family)
130 return __xfrm4_spi_hash(addr, spi, proto);
132 return __xfrm6_spi_hash(addr, spi, proto);
137 DECLARE_WAIT_QUEUE_HEAD(km_waitq);
138 EXPORT_SYMBOL(km_waitq);
140 static DEFINE_RWLOCK(xfrm_state_afinfo_lock);
141 static struct xfrm_state_afinfo *xfrm_state_afinfo[NPROTO];
143 static struct work_struct xfrm_state_gc_work;
144 static HLIST_HEAD(xfrm_state_gc_list);
145 static DEFINE_SPINLOCK(xfrm_state_gc_lock);
147 static int xfrm_state_gc_flush_bundles;
149 int __xfrm_state_delete(struct xfrm_state *x);
151 static struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned short family);
152 static void xfrm_state_put_afinfo(struct xfrm_state_afinfo *afinfo);
154 int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol);
155 void km_state_expired(struct xfrm_state *x, int hard, u32 pid);
157 static void xfrm_state_gc_destroy(struct xfrm_state *x)
159 if (del_timer(&x->timer))
161 if (del_timer(&x->rtimer))
169 xfrm_put_mode(x->mode);
171 x->type->destructor(x);
172 xfrm_put_type(x->type);
174 security_xfrm_state_free(x);
178 static void xfrm_state_gc_task(void *data)
180 struct xfrm_state *x;
181 struct hlist_node *entry, *tmp;
182 struct hlist_head gc_list;
184 if (xfrm_state_gc_flush_bundles) {
185 xfrm_state_gc_flush_bundles = 0;
186 xfrm_flush_bundles();
189 spin_lock_bh(&xfrm_state_gc_lock);
190 gc_list.first = xfrm_state_gc_list.first;
191 INIT_HLIST_HEAD(&xfrm_state_gc_list);
192 spin_unlock_bh(&xfrm_state_gc_lock);
194 hlist_for_each_entry_safe(x, entry, tmp, &gc_list, bydst)
195 xfrm_state_gc_destroy(x);
200 static inline unsigned long make_jiffies(long secs)
202 if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
203 return MAX_SCHEDULE_TIMEOUT-1;
208 static void xfrm_timer_handler(unsigned long data)
210 struct xfrm_state *x = (struct xfrm_state*)data;
211 unsigned long now = (unsigned long)xtime.tv_sec;
212 long next = LONG_MAX;
216 if (x->km.state == XFRM_STATE_DEAD)
218 if (x->km.state == XFRM_STATE_EXPIRED)
220 if (x->lft.hard_add_expires_seconds) {
221 long tmo = x->lft.hard_add_expires_seconds +
222 x->curlft.add_time - now;
228 if (x->lft.hard_use_expires_seconds) {
229 long tmo = x->lft.hard_use_expires_seconds +
230 (x->curlft.use_time ? : now) - now;
238 if (x->lft.soft_add_expires_seconds) {
239 long tmo = x->lft.soft_add_expires_seconds +
240 x->curlft.add_time - now;
246 if (x->lft.soft_use_expires_seconds) {
247 long tmo = x->lft.soft_use_expires_seconds +
248 (x->curlft.use_time ? : now) - now;
257 km_state_expired(x, 0, 0);
259 if (next != LONG_MAX &&
260 !mod_timer(&x->timer, jiffies + make_jiffies(next)))
265 if (x->km.state == XFRM_STATE_ACQ && x->id.spi == 0) {
266 x->km.state = XFRM_STATE_EXPIRED;
271 if (!__xfrm_state_delete(x) && x->id.spi)
272 km_state_expired(x, 1, 0);
275 spin_unlock(&x->lock);
279 static void xfrm_replay_timer_handler(unsigned long data);
281 struct xfrm_state *xfrm_state_alloc(void)
283 struct xfrm_state *x;
285 x = kzalloc(sizeof(struct xfrm_state), GFP_ATOMIC);
288 atomic_set(&x->refcnt, 1);
289 atomic_set(&x->tunnel_users, 0);
290 INIT_HLIST_NODE(&x->bydst);
291 INIT_HLIST_NODE(&x->bysrc);
292 INIT_HLIST_NODE(&x->byspi);
293 init_timer(&x->timer);
294 x->timer.function = xfrm_timer_handler;
295 x->timer.data = (unsigned long)x;
296 init_timer(&x->rtimer);
297 x->rtimer.function = xfrm_replay_timer_handler;
298 x->rtimer.data = (unsigned long)x;
299 x->curlft.add_time = (unsigned long)xtime.tv_sec;
300 x->lft.soft_byte_limit = XFRM_INF;
301 x->lft.soft_packet_limit = XFRM_INF;
302 x->lft.hard_byte_limit = XFRM_INF;
303 x->lft.hard_packet_limit = XFRM_INF;
304 x->replay_maxage = 0;
305 x->replay_maxdiff = 0;
306 spin_lock_init(&x->lock);
310 EXPORT_SYMBOL(xfrm_state_alloc);
312 void __xfrm_state_destroy(struct xfrm_state *x)
314 BUG_TRAP(x->km.state == XFRM_STATE_DEAD);
316 spin_lock_bh(&xfrm_state_gc_lock);
317 hlist_add_head(&x->bydst, &xfrm_state_gc_list);
318 spin_unlock_bh(&xfrm_state_gc_lock);
319 schedule_work(&xfrm_state_gc_work);
321 EXPORT_SYMBOL(__xfrm_state_destroy);
323 int __xfrm_state_delete(struct xfrm_state *x)
327 if (x->km.state != XFRM_STATE_DEAD) {
328 x->km.state = XFRM_STATE_DEAD;
329 spin_lock(&xfrm_state_lock);
330 hlist_del(&x->bydst);
332 hlist_del(&x->bysrc);
335 hlist_del(&x->byspi);
338 spin_unlock(&xfrm_state_lock);
339 if (del_timer(&x->timer))
341 if (del_timer(&x->rtimer))
344 /* The number two in this test is the reference
345 * mentioned in the comment below plus the reference
346 * our caller holds. A larger value means that
347 * there are DSTs attached to this xfrm_state.
349 if (atomic_read(&x->refcnt) > 2) {
350 xfrm_state_gc_flush_bundles = 1;
351 schedule_work(&xfrm_state_gc_work);
354 /* All xfrm_state objects are created by xfrm_state_alloc.
355 * The xfrm_state_alloc call gives a reference, and that
356 * is what we are dropping here.
364 EXPORT_SYMBOL(__xfrm_state_delete);
366 int xfrm_state_delete(struct xfrm_state *x)
370 spin_lock_bh(&x->lock);
371 err = __xfrm_state_delete(x);
372 spin_unlock_bh(&x->lock);
376 EXPORT_SYMBOL(xfrm_state_delete);
378 void xfrm_state_flush(u8 proto)
382 spin_lock_bh(&xfrm_state_lock);
383 for (i = 0; i < XFRM_DST_HSIZE; i++) {
384 struct hlist_node *entry;
385 struct xfrm_state *x;
387 hlist_for_each_entry(x, entry, xfrm_state_bydst+i, bydst) {
388 if (!xfrm_state_kern(x) &&
389 xfrm_id_proto_match(x->id.proto, proto)) {
391 spin_unlock_bh(&xfrm_state_lock);
393 xfrm_state_delete(x);
396 spin_lock_bh(&xfrm_state_lock);
401 spin_unlock_bh(&xfrm_state_lock);
404 EXPORT_SYMBOL(xfrm_state_flush);
407 xfrm_init_tempsel(struct xfrm_state *x, struct flowi *fl,
408 struct xfrm_tmpl *tmpl,
409 xfrm_address_t *daddr, xfrm_address_t *saddr,
410 unsigned short family)
412 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
415 afinfo->init_tempsel(x, fl, tmpl, daddr, saddr);
416 xfrm_state_put_afinfo(afinfo);
420 static struct xfrm_state *__xfrm_state_lookup(xfrm_address_t *daddr, u32 spi, u8 proto, unsigned short family)
422 unsigned int h = xfrm_spi_hash(daddr, spi, proto, family);
423 struct xfrm_state *x;
424 struct hlist_node *entry;
426 hlist_for_each_entry(x, entry, xfrm_state_byspi+h, byspi) {
427 if (x->props.family != family ||
429 x->id.proto != proto)
434 if (x->id.daddr.a4 != daddr->a4)
438 if (!ipv6_addr_equal((struct in6_addr *)daddr,
452 static struct xfrm_state *__xfrm_state_lookup_byaddr(xfrm_address_t *daddr, xfrm_address_t *saddr, u8 proto, unsigned short family)
454 unsigned int h = xfrm_src_hash(saddr, family);
455 struct xfrm_state *x;
456 struct hlist_node *entry;
458 hlist_for_each_entry(x, entry, xfrm_state_bysrc+h, bysrc) {
459 if (x->props.family != family ||
460 x->id.proto != proto)
465 if (x->id.daddr.a4 != daddr->a4 ||
466 x->props.saddr.a4 != saddr->a4)
470 if (!ipv6_addr_equal((struct in6_addr *)daddr,
473 !ipv6_addr_equal((struct in6_addr *)saddr,
487 static inline struct xfrm_state *
488 __xfrm_state_locate(struct xfrm_state *x, int use_spi, int family)
491 return __xfrm_state_lookup(&x->id.daddr, x->id.spi,
492 x->id.proto, family);
494 return __xfrm_state_lookup_byaddr(&x->id.daddr,
496 x->id.proto, family);
500 xfrm_state_find(xfrm_address_t *daddr, xfrm_address_t *saddr,
501 struct flowi *fl, struct xfrm_tmpl *tmpl,
502 struct xfrm_policy *pol, int *err,
503 unsigned short family)
505 unsigned int h = xfrm_dst_hash(daddr, family);
506 struct hlist_node *entry;
507 struct xfrm_state *x, *x0;
508 int acquire_in_progress = 0;
510 struct xfrm_state *best = NULL;
512 spin_lock_bh(&xfrm_state_lock);
513 hlist_for_each_entry(x, entry, xfrm_state_bydst+h, bydst) {
514 if (x->props.family == family &&
515 x->props.reqid == tmpl->reqid &&
516 !(x->props.flags & XFRM_STATE_WILDRECV) &&
517 xfrm_state_addr_check(x, daddr, saddr, family) &&
518 tmpl->mode == x->props.mode &&
519 tmpl->id.proto == x->id.proto &&
520 (tmpl->id.spi == x->id.spi || !tmpl->id.spi)) {
522 1. There is a valid state with matching selector.
524 2. Valid state with inappropriate selector. Skip.
526 Entering area of "sysdeps".
528 3. If state is not valid, selector is temporary,
529 it selects only session which triggered
530 previous resolution. Key manager will do
531 something to install a state with proper
534 if (x->km.state == XFRM_STATE_VALID) {
535 if (!xfrm_selector_match(&x->sel, fl, family) ||
536 !security_xfrm_state_pol_flow_match(x, pol, fl))
539 best->km.dying > x->km.dying ||
540 (best->km.dying == x->km.dying &&
541 best->curlft.add_time < x->curlft.add_time))
543 } else if (x->km.state == XFRM_STATE_ACQ) {
544 acquire_in_progress = 1;
545 } else if (x->km.state == XFRM_STATE_ERROR ||
546 x->km.state == XFRM_STATE_EXPIRED) {
547 if (xfrm_selector_match(&x->sel, fl, family) &&
548 security_xfrm_state_pol_flow_match(x, pol, fl))
555 if (!x && !error && !acquire_in_progress) {
557 (x0 = __xfrm_state_lookup(daddr, tmpl->id.spi,
558 tmpl->id.proto, family)) != NULL) {
563 x = xfrm_state_alloc();
568 /* Initialize temporary selector matching only
569 * to current session. */
570 xfrm_init_tempsel(x, fl, tmpl, daddr, saddr, family);
572 error = security_xfrm_state_alloc_acquire(x, pol->security, fl->secid);
574 x->km.state = XFRM_STATE_DEAD;
580 if (km_query(x, tmpl, pol) == 0) {
581 x->km.state = XFRM_STATE_ACQ;
582 hlist_add_head(&x->bydst, xfrm_state_bydst+h);
584 h = xfrm_src_hash(saddr, family);
585 hlist_add_head(&x->bysrc, xfrm_state_bysrc+h);
588 h = xfrm_spi_hash(&x->id.daddr, x->id.spi, x->id.proto, family);
589 hlist_add_head(&x->byspi, xfrm_state_byspi+h);
592 x->lft.hard_add_expires_seconds = XFRM_ACQ_EXPIRES;
594 x->timer.expires = jiffies + XFRM_ACQ_EXPIRES*HZ;
595 add_timer(&x->timer);
597 x->km.state = XFRM_STATE_DEAD;
607 *err = acquire_in_progress ? -EAGAIN : error;
608 spin_unlock_bh(&xfrm_state_lock);
612 static void __xfrm_state_insert(struct xfrm_state *x)
614 unsigned h = xfrm_dst_hash(&x->id.daddr, x->props.family);
616 hlist_add_head(&x->bydst, xfrm_state_bydst+h);
619 h = xfrm_src_hash(&x->props.saddr, x->props.family);
621 hlist_add_head(&x->bysrc, xfrm_state_bysrc+h);
624 if (xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY)) {
625 h = xfrm_spi_hash(&x->id.daddr, x->id.spi, x->id.proto,
628 hlist_add_head(&x->byspi, xfrm_state_byspi+h);
632 if (!mod_timer(&x->timer, jiffies + HZ))
635 if (x->replay_maxage &&
636 !mod_timer(&x->rtimer, jiffies + x->replay_maxage))
642 void xfrm_state_insert(struct xfrm_state *x)
644 spin_lock_bh(&xfrm_state_lock);
645 __xfrm_state_insert(x);
646 spin_unlock_bh(&xfrm_state_lock);
648 xfrm_flush_all_bundles();
650 EXPORT_SYMBOL(xfrm_state_insert);
652 /* xfrm_state_lock is held */
653 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)
655 unsigned int h = xfrm_dst_hash(daddr, family);
656 struct hlist_node *entry;
657 struct xfrm_state *x;
659 hlist_for_each_entry(x, entry, xfrm_state_bydst+h, bydst) {
660 if (x->props.reqid != reqid ||
661 x->props.mode != mode ||
662 x->props.family != family ||
663 x->km.state != XFRM_STATE_ACQ ||
669 if (x->id.daddr.a4 != daddr->a4 ||
670 x->props.saddr.a4 != saddr->a4)
674 if (!ipv6_addr_equal((struct in6_addr *)x->id.daddr.a6,
675 (struct in6_addr *)daddr) ||
676 !ipv6_addr_equal((struct in6_addr *)
678 (struct in6_addr *)saddr))
690 x = xfrm_state_alloc();
694 x->sel.daddr.a4 = daddr->a4;
695 x->sel.saddr.a4 = saddr->a4;
696 x->sel.prefixlen_d = 32;
697 x->sel.prefixlen_s = 32;
698 x->props.saddr.a4 = saddr->a4;
699 x->id.daddr.a4 = daddr->a4;
703 ipv6_addr_copy((struct in6_addr *)x->sel.daddr.a6,
704 (struct in6_addr *)daddr);
705 ipv6_addr_copy((struct in6_addr *)x->sel.saddr.a6,
706 (struct in6_addr *)saddr);
707 x->sel.prefixlen_d = 128;
708 x->sel.prefixlen_s = 128;
709 ipv6_addr_copy((struct in6_addr *)x->props.saddr.a6,
710 (struct in6_addr *)saddr);
711 ipv6_addr_copy((struct in6_addr *)x->id.daddr.a6,
712 (struct in6_addr *)daddr);
716 x->km.state = XFRM_STATE_ACQ;
718 x->props.family = family;
719 x->props.mode = mode;
720 x->props.reqid = reqid;
721 x->lft.hard_add_expires_seconds = XFRM_ACQ_EXPIRES;
723 x->timer.expires = jiffies + XFRM_ACQ_EXPIRES*HZ;
724 add_timer(&x->timer);
726 hlist_add_head(&x->bydst, xfrm_state_bydst+h);
727 h = xfrm_src_hash(saddr, family);
729 hlist_add_head(&x->bysrc, xfrm_state_bysrc+h);
736 static struct xfrm_state *__xfrm_find_acq_byseq(u32 seq);
738 int xfrm_state_add(struct xfrm_state *x)
740 struct xfrm_state *x1;
743 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
745 family = x->props.family;
747 spin_lock_bh(&xfrm_state_lock);
749 x1 = __xfrm_state_locate(x, use_spi, family);
757 if (use_spi && x->km.seq) {
758 x1 = __xfrm_find_acq_byseq(x->km.seq);
759 if (x1 && xfrm_addr_cmp(&x1->id.daddr, &x->id.daddr, family)) {
766 x1 = __find_acq_core(family, x->props.mode, x->props.reqid,
768 &x->id.daddr, &x->props.saddr, 0);
770 __xfrm_state_insert(x);
774 spin_unlock_bh(&xfrm_state_lock);
777 xfrm_flush_all_bundles();
780 xfrm_state_delete(x1);
786 EXPORT_SYMBOL(xfrm_state_add);
788 int xfrm_state_update(struct xfrm_state *x)
790 struct xfrm_state *x1;
792 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
794 spin_lock_bh(&xfrm_state_lock);
795 x1 = __xfrm_state_locate(x, use_spi, x->props.family);
801 if (xfrm_state_kern(x1)) {
807 if (x1->km.state == XFRM_STATE_ACQ) {
808 __xfrm_state_insert(x);
814 spin_unlock_bh(&xfrm_state_lock);
820 xfrm_state_delete(x1);
826 spin_lock_bh(&x1->lock);
827 if (likely(x1->km.state == XFRM_STATE_VALID)) {
828 if (x->encap && x1->encap)
829 memcpy(x1->encap, x->encap, sizeof(*x1->encap));
830 if (x->coaddr && x1->coaddr) {
831 memcpy(x1->coaddr, x->coaddr, sizeof(*x1->coaddr));
833 if (!use_spi && memcmp(&x1->sel, &x->sel, sizeof(x1->sel)))
834 memcpy(&x1->sel, &x->sel, sizeof(x1->sel));
835 memcpy(&x1->lft, &x->lft, sizeof(x1->lft));
838 if (!mod_timer(&x1->timer, jiffies + HZ))
840 if (x1->curlft.use_time)
841 xfrm_state_check_expire(x1);
845 spin_unlock_bh(&x1->lock);
851 EXPORT_SYMBOL(xfrm_state_update);
853 int xfrm_state_check_expire(struct xfrm_state *x)
855 if (!x->curlft.use_time)
856 x->curlft.use_time = (unsigned long)xtime.tv_sec;
858 if (x->km.state != XFRM_STATE_VALID)
861 if (x->curlft.bytes >= x->lft.hard_byte_limit ||
862 x->curlft.packets >= x->lft.hard_packet_limit) {
863 x->km.state = XFRM_STATE_EXPIRED;
864 if (!mod_timer(&x->timer, jiffies))
870 (x->curlft.bytes >= x->lft.soft_byte_limit ||
871 x->curlft.packets >= x->lft.soft_packet_limit)) {
873 km_state_expired(x, 0, 0);
877 EXPORT_SYMBOL(xfrm_state_check_expire);
879 static int xfrm_state_check_space(struct xfrm_state *x, struct sk_buff *skb)
881 int nhead = x->props.header_len + LL_RESERVED_SPACE(skb->dst->dev)
885 return pskb_expand_head(skb, nhead, 0, GFP_ATOMIC);
887 /* Check tail too... */
891 int xfrm_state_check(struct xfrm_state *x, struct sk_buff *skb)
893 int err = xfrm_state_check_expire(x);
896 err = xfrm_state_check_space(x, skb);
900 EXPORT_SYMBOL(xfrm_state_check);
903 xfrm_state_lookup(xfrm_address_t *daddr, u32 spi, u8 proto,
904 unsigned short family)
906 struct xfrm_state *x;
908 spin_lock_bh(&xfrm_state_lock);
909 x = __xfrm_state_lookup(daddr, spi, proto, family);
910 spin_unlock_bh(&xfrm_state_lock);
913 EXPORT_SYMBOL(xfrm_state_lookup);
916 xfrm_state_lookup_byaddr(xfrm_address_t *daddr, xfrm_address_t *saddr,
917 u8 proto, unsigned short family)
919 struct xfrm_state *x;
921 spin_lock_bh(&xfrm_state_lock);
922 x = __xfrm_state_lookup_byaddr(daddr, saddr, proto, family);
923 spin_unlock_bh(&xfrm_state_lock);
926 EXPORT_SYMBOL(xfrm_state_lookup_byaddr);
929 xfrm_find_acq(u8 mode, u32 reqid, u8 proto,
930 xfrm_address_t *daddr, xfrm_address_t *saddr,
931 int create, unsigned short family)
933 struct xfrm_state *x;
935 spin_lock_bh(&xfrm_state_lock);
936 x = __find_acq_core(family, mode, reqid, proto, daddr, saddr, create);
937 spin_unlock_bh(&xfrm_state_lock);
941 EXPORT_SYMBOL(xfrm_find_acq);
943 #ifdef CONFIG_XFRM_SUB_POLICY
945 xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n,
946 unsigned short family)
949 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
951 return -EAFNOSUPPORT;
953 spin_lock_bh(&xfrm_state_lock);
954 if (afinfo->tmpl_sort)
955 err = afinfo->tmpl_sort(dst, src, n);
956 spin_unlock_bh(&xfrm_state_lock);
957 xfrm_state_put_afinfo(afinfo);
960 EXPORT_SYMBOL(xfrm_tmpl_sort);
963 xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src, int n,
964 unsigned short family)
967 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
969 return -EAFNOSUPPORT;
971 spin_lock_bh(&xfrm_state_lock);
972 if (afinfo->state_sort)
973 err = afinfo->state_sort(dst, src, n);
974 spin_unlock_bh(&xfrm_state_lock);
975 xfrm_state_put_afinfo(afinfo);
978 EXPORT_SYMBOL(xfrm_state_sort);
981 /* Silly enough, but I'm lazy to build resolution list */
983 static struct xfrm_state *__xfrm_find_acq_byseq(u32 seq)
987 for (i = 0; i < XFRM_DST_HSIZE; i++) {
988 struct hlist_node *entry;
989 struct xfrm_state *x;
991 hlist_for_each_entry(x, entry, xfrm_state_bydst+i, bydst) {
992 if (x->km.seq == seq &&
993 x->km.state == XFRM_STATE_ACQ) {
1002 struct xfrm_state *xfrm_find_acq_byseq(u32 seq)
1004 struct xfrm_state *x;
1006 spin_lock_bh(&xfrm_state_lock);
1007 x = __xfrm_find_acq_byseq(seq);
1008 spin_unlock_bh(&xfrm_state_lock);
1011 EXPORT_SYMBOL(xfrm_find_acq_byseq);
1013 u32 xfrm_get_acqseq(void)
1017 static DEFINE_SPINLOCK(acqseq_lock);
1019 spin_lock_bh(&acqseq_lock);
1020 res = (++acqseq ? : ++acqseq);
1021 spin_unlock_bh(&acqseq_lock);
1024 EXPORT_SYMBOL(xfrm_get_acqseq);
1027 xfrm_alloc_spi(struct xfrm_state *x, u32 minspi, u32 maxspi)
1030 struct xfrm_state *x0;
1035 if (minspi == maxspi) {
1036 x0 = xfrm_state_lookup(&x->id.daddr, minspi, x->id.proto, x->props.family);
1044 minspi = ntohl(minspi);
1045 maxspi = ntohl(maxspi);
1046 for (h=0; h<maxspi-minspi+1; h++) {
1047 spi = minspi + net_random()%(maxspi-minspi+1);
1048 x0 = xfrm_state_lookup(&x->id.daddr, htonl(spi), x->id.proto, x->props.family);
1050 x->id.spi = htonl(spi);
1057 spin_lock_bh(&xfrm_state_lock);
1058 h = xfrm_spi_hash(&x->id.daddr, x->id.spi, x->id.proto, x->props.family);
1059 hlist_add_head(&x->byspi, xfrm_state_byspi+h);
1061 spin_unlock_bh(&xfrm_state_lock);
1065 EXPORT_SYMBOL(xfrm_alloc_spi);
1067 int xfrm_state_walk(u8 proto, int (*func)(struct xfrm_state *, int, void*),
1071 struct xfrm_state *x;
1072 struct hlist_node *entry;
1076 spin_lock_bh(&xfrm_state_lock);
1077 for (i = 0; i < XFRM_DST_HSIZE; i++) {
1078 hlist_for_each_entry(x, entry, xfrm_state_bydst+i, bydst) {
1079 if (xfrm_id_proto_match(x->id.proto, proto))
1088 for (i = 0; i < XFRM_DST_HSIZE; i++) {
1089 hlist_for_each_entry(x, entry, xfrm_state_bydst+i, bydst) {
1090 if (!xfrm_id_proto_match(x->id.proto, proto))
1092 err = func(x, --count, data);
1098 spin_unlock_bh(&xfrm_state_lock);
1101 EXPORT_SYMBOL(xfrm_state_walk);
1104 void xfrm_replay_notify(struct xfrm_state *x, int event)
1107 /* we send notify messages in case
1108 * 1. we updated on of the sequence numbers, and the seqno difference
1109 * is at least x->replay_maxdiff, in this case we also update the
1110 * timeout of our timer function
1111 * 2. if x->replay_maxage has elapsed since last update,
1112 * and there were changes
1114 * The state structure must be locked!
1118 case XFRM_REPLAY_UPDATE:
1119 if (x->replay_maxdiff &&
1120 (x->replay.seq - x->preplay.seq < x->replay_maxdiff) &&
1121 (x->replay.oseq - x->preplay.oseq < x->replay_maxdiff)) {
1122 if (x->xflags & XFRM_TIME_DEFER)
1123 event = XFRM_REPLAY_TIMEOUT;
1130 case XFRM_REPLAY_TIMEOUT:
1131 if ((x->replay.seq == x->preplay.seq) &&
1132 (x->replay.bitmap == x->preplay.bitmap) &&
1133 (x->replay.oseq == x->preplay.oseq)) {
1134 x->xflags |= XFRM_TIME_DEFER;
1141 memcpy(&x->preplay, &x->replay, sizeof(struct xfrm_replay_state));
1142 c.event = XFRM_MSG_NEWAE;
1143 c.data.aevent = event;
1144 km_state_notify(x, &c);
1146 if (x->replay_maxage &&
1147 !mod_timer(&x->rtimer, jiffies + x->replay_maxage)) {
1149 x->xflags &= ~XFRM_TIME_DEFER;
1152 EXPORT_SYMBOL(xfrm_replay_notify);
1154 static void xfrm_replay_timer_handler(unsigned long data)
1156 struct xfrm_state *x = (struct xfrm_state*)data;
1158 spin_lock(&x->lock);
1160 if (x->km.state == XFRM_STATE_VALID) {
1161 if (xfrm_aevent_is_on())
1162 xfrm_replay_notify(x, XFRM_REPLAY_TIMEOUT);
1164 x->xflags |= XFRM_TIME_DEFER;
1167 spin_unlock(&x->lock);
1171 int xfrm_replay_check(struct xfrm_state *x, u32 seq)
1177 if (unlikely(seq == 0))
1180 if (likely(seq > x->replay.seq))
1183 diff = x->replay.seq - seq;
1184 if (diff >= x->props.replay_window) {
1185 x->stats.replay_window++;
1189 if (x->replay.bitmap & (1U << diff)) {
1195 EXPORT_SYMBOL(xfrm_replay_check);
1197 void xfrm_replay_advance(struct xfrm_state *x, u32 seq)
1203 if (seq > x->replay.seq) {
1204 diff = seq - x->replay.seq;
1205 if (diff < x->props.replay_window)
1206 x->replay.bitmap = ((x->replay.bitmap) << diff) | 1;
1208 x->replay.bitmap = 1;
1209 x->replay.seq = seq;
1211 diff = x->replay.seq - seq;
1212 x->replay.bitmap |= (1U << diff);
1215 if (xfrm_aevent_is_on())
1216 xfrm_replay_notify(x, XFRM_REPLAY_UPDATE);
1218 EXPORT_SYMBOL(xfrm_replay_advance);
1220 static struct list_head xfrm_km_list = LIST_HEAD_INIT(xfrm_km_list);
1221 static DEFINE_RWLOCK(xfrm_km_lock);
1223 void km_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
1225 struct xfrm_mgr *km;
1227 read_lock(&xfrm_km_lock);
1228 list_for_each_entry(km, &xfrm_km_list, list)
1229 if (km->notify_policy)
1230 km->notify_policy(xp, dir, c);
1231 read_unlock(&xfrm_km_lock);
1234 void km_state_notify(struct xfrm_state *x, struct km_event *c)
1236 struct xfrm_mgr *km;
1237 read_lock(&xfrm_km_lock);
1238 list_for_each_entry(km, &xfrm_km_list, list)
1241 read_unlock(&xfrm_km_lock);
1244 EXPORT_SYMBOL(km_policy_notify);
1245 EXPORT_SYMBOL(km_state_notify);
1247 void km_state_expired(struct xfrm_state *x, int hard, u32 pid)
1253 c.event = XFRM_MSG_EXPIRE;
1254 km_state_notify(x, &c);
1260 EXPORT_SYMBOL(km_state_expired);
1262 * We send to all registered managers regardless of failure
1263 * We are happy with one success
1265 int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol)
1267 int err = -EINVAL, acqret;
1268 struct xfrm_mgr *km;
1270 read_lock(&xfrm_km_lock);
1271 list_for_each_entry(km, &xfrm_km_list, list) {
1272 acqret = km->acquire(x, t, pol, XFRM_POLICY_OUT);
1276 read_unlock(&xfrm_km_lock);
1279 EXPORT_SYMBOL(km_query);
1281 int km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, u16 sport)
1284 struct xfrm_mgr *km;
1286 read_lock(&xfrm_km_lock);
1287 list_for_each_entry(km, &xfrm_km_list, list) {
1288 if (km->new_mapping)
1289 err = km->new_mapping(x, ipaddr, sport);
1293 read_unlock(&xfrm_km_lock);
1296 EXPORT_SYMBOL(km_new_mapping);
1298 void km_policy_expired(struct xfrm_policy *pol, int dir, int hard, u32 pid)
1304 c.event = XFRM_MSG_POLEXPIRE;
1305 km_policy_notify(pol, dir, &c);
1310 EXPORT_SYMBOL(km_policy_expired);
1312 int km_report(u8 proto, struct xfrm_selector *sel, xfrm_address_t *addr)
1316 struct xfrm_mgr *km;
1318 read_lock(&xfrm_km_lock);
1319 list_for_each_entry(km, &xfrm_km_list, list) {
1321 ret = km->report(proto, sel, addr);
1326 read_unlock(&xfrm_km_lock);
1329 EXPORT_SYMBOL(km_report);
1331 int xfrm_user_policy(struct sock *sk, int optname, u8 __user *optval, int optlen)
1335 struct xfrm_mgr *km;
1336 struct xfrm_policy *pol = NULL;
1338 if (optlen <= 0 || optlen > PAGE_SIZE)
1341 data = kmalloc(optlen, GFP_KERNEL);
1346 if (copy_from_user(data, optval, optlen))
1350 read_lock(&xfrm_km_lock);
1351 list_for_each_entry(km, &xfrm_km_list, list) {
1352 pol = km->compile_policy(sk, optname, data,
1357 read_unlock(&xfrm_km_lock);
1360 xfrm_sk_policy_insert(sk, err, pol);
1369 EXPORT_SYMBOL(xfrm_user_policy);
1371 int xfrm_register_km(struct xfrm_mgr *km)
1373 write_lock_bh(&xfrm_km_lock);
1374 list_add_tail(&km->list, &xfrm_km_list);
1375 write_unlock_bh(&xfrm_km_lock);
1378 EXPORT_SYMBOL(xfrm_register_km);
1380 int xfrm_unregister_km(struct xfrm_mgr *km)
1382 write_lock_bh(&xfrm_km_lock);
1383 list_del(&km->list);
1384 write_unlock_bh(&xfrm_km_lock);
1387 EXPORT_SYMBOL(xfrm_unregister_km);
1389 int xfrm_state_register_afinfo(struct xfrm_state_afinfo *afinfo)
1392 if (unlikely(afinfo == NULL))
1394 if (unlikely(afinfo->family >= NPROTO))
1395 return -EAFNOSUPPORT;
1396 write_lock_bh(&xfrm_state_afinfo_lock);
1397 if (unlikely(xfrm_state_afinfo[afinfo->family] != NULL))
1400 xfrm_state_afinfo[afinfo->family] = afinfo;
1401 write_unlock_bh(&xfrm_state_afinfo_lock);
1404 EXPORT_SYMBOL(xfrm_state_register_afinfo);
1406 int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo)
1409 if (unlikely(afinfo == NULL))
1411 if (unlikely(afinfo->family >= NPROTO))
1412 return -EAFNOSUPPORT;
1413 write_lock_bh(&xfrm_state_afinfo_lock);
1414 if (likely(xfrm_state_afinfo[afinfo->family] != NULL)) {
1415 if (unlikely(xfrm_state_afinfo[afinfo->family] != afinfo))
1418 xfrm_state_afinfo[afinfo->family] = NULL;
1420 write_unlock_bh(&xfrm_state_afinfo_lock);
1423 EXPORT_SYMBOL(xfrm_state_unregister_afinfo);
1425 static struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned short family)
1427 struct xfrm_state_afinfo *afinfo;
1428 if (unlikely(family >= NPROTO))
1430 read_lock(&xfrm_state_afinfo_lock);
1431 afinfo = xfrm_state_afinfo[family];
1432 if (unlikely(!afinfo))
1433 read_unlock(&xfrm_state_afinfo_lock);
1437 static void xfrm_state_put_afinfo(struct xfrm_state_afinfo *afinfo)
1439 read_unlock(&xfrm_state_afinfo_lock);
1442 /* Temporarily located here until net/xfrm/xfrm_tunnel.c is created */
1443 void xfrm_state_delete_tunnel(struct xfrm_state *x)
1446 struct xfrm_state *t = x->tunnel;
1448 if (atomic_read(&t->tunnel_users) == 2)
1449 xfrm_state_delete(t);
1450 atomic_dec(&t->tunnel_users);
1455 EXPORT_SYMBOL(xfrm_state_delete_tunnel);
1458 * This function is NOT optimal. For example, with ESP it will give an
1459 * MTU that's usually two bytes short of being optimal. However, it will
1460 * usually give an answer that's a multiple of 4 provided the input is
1461 * also a multiple of 4.
1463 int xfrm_state_mtu(struct xfrm_state *x, int mtu)
1467 res -= x->props.header_len;
1475 spin_lock_bh(&x->lock);
1476 if (x->km.state == XFRM_STATE_VALID &&
1477 x->type && x->type->get_max_size)
1478 m = x->type->get_max_size(x, m);
1480 m += x->props.header_len;
1481 spin_unlock_bh(&x->lock);
1491 int xfrm_init_state(struct xfrm_state *x)
1493 struct xfrm_state_afinfo *afinfo;
1494 int family = x->props.family;
1497 err = -EAFNOSUPPORT;
1498 afinfo = xfrm_state_get_afinfo(family);
1503 if (afinfo->init_flags)
1504 err = afinfo->init_flags(x);
1506 xfrm_state_put_afinfo(afinfo);
1511 err = -EPROTONOSUPPORT;
1512 x->type = xfrm_get_type(x->id.proto, family);
1513 if (x->type == NULL)
1516 err = x->type->init_state(x);
1520 x->mode = xfrm_get_mode(x->props.mode, family);
1521 if (x->mode == NULL)
1524 x->km.state = XFRM_STATE_VALID;
1530 EXPORT_SYMBOL(xfrm_init_state);
1532 void __init xfrm_state_init(void)
1536 for (i=0; i<XFRM_DST_HSIZE; i++) {
1537 INIT_HLIST_HEAD(&xfrm_state_bydst[i]);
1538 INIT_HLIST_HEAD(&xfrm_state_bysrc[i]);
1539 INIT_HLIST_HEAD(&xfrm_state_byspi[i]);
1541 INIT_WORK(&xfrm_state_gc_work, xfrm_state_gc_task, NULL);