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"
27 /* Each xfrm_state may be linked to two tables:
29 1. Hash table by (spi,daddr,ah/esp) to find SA by SPI. (input,ctl)
30 2. Hash table by (daddr,family,reqid) to find what SAs exist for given
31 destination/tunnel endpoint. (output)
34 static DEFINE_SPINLOCK(xfrm_state_lock);
36 static unsigned int xfrm_state_hashmax __read_mostly = 1 * 1024 * 1024;
37 static unsigned int xfrm_state_genid;
39 static struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family);
40 static void xfrm_state_put_afinfo(struct xfrm_state_afinfo *afinfo);
42 #ifdef CONFIG_AUDITSYSCALL
43 static void xfrm_audit_state_replay(struct xfrm_state *x,
44 struct sk_buff *skb, __be32 net_seq);
46 #define xfrm_audit_state_replay(x, s, sq) do { ; } while (0)
47 #endif /* CONFIG_AUDITSYSCALL */
49 static inline unsigned int xfrm_dst_hash(struct net *net,
50 xfrm_address_t *daddr,
51 xfrm_address_t *saddr,
53 unsigned short family)
55 return __xfrm_dst_hash(daddr, saddr, reqid, family, net->xfrm.state_hmask);
58 static inline unsigned int xfrm_src_hash(struct net *net,
59 xfrm_address_t *daddr,
60 xfrm_address_t *saddr,
61 unsigned short family)
63 return __xfrm_src_hash(daddr, saddr, family, net->xfrm.state_hmask);
66 static inline unsigned int
67 xfrm_spi_hash(struct net *net, xfrm_address_t *daddr, __be32 spi, u8 proto, unsigned short family)
69 return __xfrm_spi_hash(daddr, spi, proto, family, net->xfrm.state_hmask);
72 static void xfrm_hash_transfer(struct hlist_head *list,
73 struct hlist_head *ndsttable,
74 struct hlist_head *nsrctable,
75 struct hlist_head *nspitable,
76 unsigned int nhashmask)
78 struct hlist_node *entry, *tmp;
81 hlist_for_each_entry_safe(x, entry, tmp, list, bydst) {
84 h = __xfrm_dst_hash(&x->id.daddr, &x->props.saddr,
85 x->props.reqid, x->props.family,
87 hlist_add_head(&x->bydst, ndsttable+h);
89 h = __xfrm_src_hash(&x->id.daddr, &x->props.saddr,
92 hlist_add_head(&x->bysrc, nsrctable+h);
95 h = __xfrm_spi_hash(&x->id.daddr, x->id.spi,
96 x->id.proto, x->props.family,
98 hlist_add_head(&x->byspi, nspitable+h);
103 static unsigned long xfrm_hash_new_size(unsigned int state_hmask)
105 return ((state_hmask + 1) << 1) * sizeof(struct hlist_head);
108 static DEFINE_MUTEX(hash_resize_mutex);
110 static void xfrm_hash_resize(struct work_struct *work)
112 struct net *net = container_of(work, struct net, xfrm.state_hash_work);
113 struct hlist_head *ndst, *nsrc, *nspi, *odst, *osrc, *ospi;
114 unsigned long nsize, osize;
115 unsigned int nhashmask, ohashmask;
118 mutex_lock(&hash_resize_mutex);
120 nsize = xfrm_hash_new_size(net->xfrm.state_hmask);
121 ndst = xfrm_hash_alloc(nsize);
124 nsrc = xfrm_hash_alloc(nsize);
126 xfrm_hash_free(ndst, nsize);
129 nspi = xfrm_hash_alloc(nsize);
131 xfrm_hash_free(ndst, nsize);
132 xfrm_hash_free(nsrc, nsize);
136 spin_lock_bh(&xfrm_state_lock);
138 nhashmask = (nsize / sizeof(struct hlist_head)) - 1U;
139 for (i = net->xfrm.state_hmask; i >= 0; i--)
140 xfrm_hash_transfer(net->xfrm.state_bydst+i, ndst, nsrc, nspi,
143 odst = net->xfrm.state_bydst;
144 osrc = net->xfrm.state_bysrc;
145 ospi = net->xfrm.state_byspi;
146 ohashmask = net->xfrm.state_hmask;
148 net->xfrm.state_bydst = ndst;
149 net->xfrm.state_bysrc = nsrc;
150 net->xfrm.state_byspi = nspi;
151 net->xfrm.state_hmask = nhashmask;
153 spin_unlock_bh(&xfrm_state_lock);
155 osize = (ohashmask + 1) * sizeof(struct hlist_head);
156 xfrm_hash_free(odst, osize);
157 xfrm_hash_free(osrc, osize);
158 xfrm_hash_free(ospi, osize);
161 mutex_unlock(&hash_resize_mutex);
164 static DEFINE_RWLOCK(xfrm_state_afinfo_lock);
165 static struct xfrm_state_afinfo *xfrm_state_afinfo[NPROTO];
167 static DEFINE_SPINLOCK(xfrm_state_gc_lock);
169 int __xfrm_state_delete(struct xfrm_state *x);
171 int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol);
172 void km_state_expired(struct xfrm_state *x, int hard, u32 pid);
174 static struct xfrm_state_afinfo *xfrm_state_lock_afinfo(unsigned int family)
176 struct xfrm_state_afinfo *afinfo;
177 if (unlikely(family >= NPROTO))
179 write_lock_bh(&xfrm_state_afinfo_lock);
180 afinfo = xfrm_state_afinfo[family];
181 if (unlikely(!afinfo))
182 write_unlock_bh(&xfrm_state_afinfo_lock);
186 static void xfrm_state_unlock_afinfo(struct xfrm_state_afinfo *afinfo)
187 __releases(xfrm_state_afinfo_lock)
189 write_unlock_bh(&xfrm_state_afinfo_lock);
192 int xfrm_register_type(const struct xfrm_type *type, unsigned short family)
194 struct xfrm_state_afinfo *afinfo = xfrm_state_lock_afinfo(family);
195 const struct xfrm_type **typemap;
198 if (unlikely(afinfo == NULL))
199 return -EAFNOSUPPORT;
200 typemap = afinfo->type_map;
202 if (likely(typemap[type->proto] == NULL))
203 typemap[type->proto] = type;
206 xfrm_state_unlock_afinfo(afinfo);
209 EXPORT_SYMBOL(xfrm_register_type);
211 int xfrm_unregister_type(const struct xfrm_type *type, unsigned short family)
213 struct xfrm_state_afinfo *afinfo = xfrm_state_lock_afinfo(family);
214 const struct xfrm_type **typemap;
217 if (unlikely(afinfo == NULL))
218 return -EAFNOSUPPORT;
219 typemap = afinfo->type_map;
221 if (unlikely(typemap[type->proto] != type))
224 typemap[type->proto] = NULL;
225 xfrm_state_unlock_afinfo(afinfo);
228 EXPORT_SYMBOL(xfrm_unregister_type);
230 static const struct xfrm_type *xfrm_get_type(u8 proto, unsigned short family)
232 struct xfrm_state_afinfo *afinfo;
233 const struct xfrm_type **typemap;
234 const struct xfrm_type *type;
235 int modload_attempted = 0;
238 afinfo = xfrm_state_get_afinfo(family);
239 if (unlikely(afinfo == NULL))
241 typemap = afinfo->type_map;
243 type = typemap[proto];
244 if (unlikely(type && !try_module_get(type->owner)))
246 if (!type && !modload_attempted) {
247 xfrm_state_put_afinfo(afinfo);
248 request_module("xfrm-type-%d-%d", family, proto);
249 modload_attempted = 1;
253 xfrm_state_put_afinfo(afinfo);
257 static void xfrm_put_type(const struct xfrm_type *type)
259 module_put(type->owner);
262 int xfrm_register_mode(struct xfrm_mode *mode, int family)
264 struct xfrm_state_afinfo *afinfo;
265 struct xfrm_mode **modemap;
268 if (unlikely(mode->encap >= XFRM_MODE_MAX))
271 afinfo = xfrm_state_lock_afinfo(family);
272 if (unlikely(afinfo == NULL))
273 return -EAFNOSUPPORT;
276 modemap = afinfo->mode_map;
277 if (modemap[mode->encap])
281 if (!try_module_get(afinfo->owner))
284 mode->afinfo = afinfo;
285 modemap[mode->encap] = mode;
289 xfrm_state_unlock_afinfo(afinfo);
292 EXPORT_SYMBOL(xfrm_register_mode);
294 int xfrm_unregister_mode(struct xfrm_mode *mode, int family)
296 struct xfrm_state_afinfo *afinfo;
297 struct xfrm_mode **modemap;
300 if (unlikely(mode->encap >= XFRM_MODE_MAX))
303 afinfo = xfrm_state_lock_afinfo(family);
304 if (unlikely(afinfo == NULL))
305 return -EAFNOSUPPORT;
308 modemap = afinfo->mode_map;
309 if (likely(modemap[mode->encap] == mode)) {
310 modemap[mode->encap] = NULL;
311 module_put(mode->afinfo->owner);
315 xfrm_state_unlock_afinfo(afinfo);
318 EXPORT_SYMBOL(xfrm_unregister_mode);
320 static struct xfrm_mode *xfrm_get_mode(unsigned int encap, int family)
322 struct xfrm_state_afinfo *afinfo;
323 struct xfrm_mode *mode;
324 int modload_attempted = 0;
326 if (unlikely(encap >= XFRM_MODE_MAX))
330 afinfo = xfrm_state_get_afinfo(family);
331 if (unlikely(afinfo == NULL))
334 mode = afinfo->mode_map[encap];
335 if (unlikely(mode && !try_module_get(mode->owner)))
337 if (!mode && !modload_attempted) {
338 xfrm_state_put_afinfo(afinfo);
339 request_module("xfrm-mode-%d-%d", family, encap);
340 modload_attempted = 1;
344 xfrm_state_put_afinfo(afinfo);
348 static void xfrm_put_mode(struct xfrm_mode *mode)
350 module_put(mode->owner);
353 static void xfrm_state_gc_destroy(struct xfrm_state *x)
355 del_timer_sync(&x->timer);
356 del_timer_sync(&x->rtimer);
363 xfrm_put_mode(x->inner_mode);
364 if (x->inner_mode_iaf)
365 xfrm_put_mode(x->inner_mode_iaf);
367 xfrm_put_mode(x->outer_mode);
369 x->type->destructor(x);
370 xfrm_put_type(x->type);
372 security_xfrm_state_free(x);
376 static void xfrm_state_gc_task(struct work_struct *work)
378 struct net *net = container_of(work, struct net, xfrm.state_gc_work);
379 struct xfrm_state *x;
380 struct hlist_node *entry, *tmp;
381 struct hlist_head gc_list;
383 spin_lock_bh(&xfrm_state_gc_lock);
384 hlist_move_list(&net->xfrm.state_gc_list, &gc_list);
385 spin_unlock_bh(&xfrm_state_gc_lock);
387 hlist_for_each_entry_safe(x, entry, tmp, &gc_list, gclist)
388 xfrm_state_gc_destroy(x);
390 wake_up(&net->xfrm.km_waitq);
393 static inline unsigned long make_jiffies(long secs)
395 if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
396 return MAX_SCHEDULE_TIMEOUT-1;
401 static void xfrm_timer_handler(unsigned long data)
403 struct xfrm_state *x = (struct xfrm_state*)data;
404 struct net *net = xs_net(x);
405 unsigned long now = get_seconds();
406 long next = LONG_MAX;
411 if (x->km.state == XFRM_STATE_DEAD)
413 if (x->km.state == XFRM_STATE_EXPIRED)
415 if (x->lft.hard_add_expires_seconds) {
416 long tmo = x->lft.hard_add_expires_seconds +
417 x->curlft.add_time - now;
423 if (x->lft.hard_use_expires_seconds) {
424 long tmo = x->lft.hard_use_expires_seconds +
425 (x->curlft.use_time ? : now) - now;
433 if (x->lft.soft_add_expires_seconds) {
434 long tmo = x->lft.soft_add_expires_seconds +
435 x->curlft.add_time - now;
441 if (x->lft.soft_use_expires_seconds) {
442 long tmo = x->lft.soft_use_expires_seconds +
443 (x->curlft.use_time ? : now) - now;
452 km_state_expired(x, 0, 0);
454 if (next != LONG_MAX)
455 mod_timer(&x->timer, jiffies + make_jiffies(next));
460 if (x->km.state == XFRM_STATE_ACQ && x->id.spi == 0) {
461 x->km.state = XFRM_STATE_EXPIRED;
462 wake_up(&net->xfrm.km_waitq);
467 err = __xfrm_state_delete(x);
468 if (!err && x->id.spi)
469 km_state_expired(x, 1, 0);
471 xfrm_audit_state_delete(x, err ? 0 : 1,
472 audit_get_loginuid(current),
473 audit_get_sessionid(current), 0);
476 spin_unlock(&x->lock);
479 static void xfrm_replay_timer_handler(unsigned long data);
481 struct xfrm_state *xfrm_state_alloc(struct net *net)
483 struct xfrm_state *x;
485 x = kzalloc(sizeof(struct xfrm_state), GFP_ATOMIC);
488 write_pnet(&x->xs_net, net);
489 atomic_set(&x->refcnt, 1);
490 atomic_set(&x->tunnel_users, 0);
491 INIT_LIST_HEAD(&x->km.all);
492 INIT_HLIST_NODE(&x->bydst);
493 INIT_HLIST_NODE(&x->bysrc);
494 INIT_HLIST_NODE(&x->byspi);
495 setup_timer(&x->timer, xfrm_timer_handler, (unsigned long)x);
496 setup_timer(&x->rtimer, xfrm_replay_timer_handler,
498 x->curlft.add_time = get_seconds();
499 x->lft.soft_byte_limit = XFRM_INF;
500 x->lft.soft_packet_limit = XFRM_INF;
501 x->lft.hard_byte_limit = XFRM_INF;
502 x->lft.hard_packet_limit = XFRM_INF;
503 x->replay_maxage = 0;
504 x->replay_maxdiff = 0;
505 x->inner_mode = NULL;
506 x->inner_mode_iaf = NULL;
507 spin_lock_init(&x->lock);
511 EXPORT_SYMBOL(xfrm_state_alloc);
513 void __xfrm_state_destroy(struct xfrm_state *x)
515 struct net *net = xs_net(x);
517 WARN_ON(x->km.state != XFRM_STATE_DEAD);
519 spin_lock_bh(&xfrm_state_gc_lock);
520 hlist_add_head(&x->gclist, &net->xfrm.state_gc_list);
521 spin_unlock_bh(&xfrm_state_gc_lock);
522 schedule_work(&net->xfrm.state_gc_work);
524 EXPORT_SYMBOL(__xfrm_state_destroy);
526 int __xfrm_state_delete(struct xfrm_state *x)
528 struct net *net = xs_net(x);
531 if (x->km.state != XFRM_STATE_DEAD) {
532 x->km.state = XFRM_STATE_DEAD;
533 spin_lock(&xfrm_state_lock);
534 list_del(&x->km.all);
535 hlist_del(&x->bydst);
536 hlist_del(&x->bysrc);
538 hlist_del(&x->byspi);
539 net->xfrm.state_num--;
540 spin_unlock(&xfrm_state_lock);
542 /* All xfrm_state objects are created by xfrm_state_alloc.
543 * The xfrm_state_alloc call gives a reference, and that
544 * is what we are dropping here.
552 EXPORT_SYMBOL(__xfrm_state_delete);
554 int xfrm_state_delete(struct xfrm_state *x)
558 spin_lock_bh(&x->lock);
559 err = __xfrm_state_delete(x);
560 spin_unlock_bh(&x->lock);
564 EXPORT_SYMBOL(xfrm_state_delete);
566 #ifdef CONFIG_SECURITY_NETWORK_XFRM
568 xfrm_state_flush_secctx_check(struct net *net, u8 proto, struct xfrm_audit *audit_info)
572 for (i = 0; i <= net->xfrm.state_hmask; i++) {
573 struct hlist_node *entry;
574 struct xfrm_state *x;
576 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+i, bydst) {
577 if (xfrm_id_proto_match(x->id.proto, proto) &&
578 (err = security_xfrm_state_delete(x)) != 0) {
579 xfrm_audit_state_delete(x, 0,
580 audit_info->loginuid,
581 audit_info->sessionid,
592 xfrm_state_flush_secctx_check(struct net *net, u8 proto, struct xfrm_audit *audit_info)
598 int xfrm_state_flush(struct net *net, u8 proto, struct xfrm_audit *audit_info)
602 spin_lock_bh(&xfrm_state_lock);
603 err = xfrm_state_flush_secctx_check(net, proto, audit_info);
607 for (i = 0; i <= net->xfrm.state_hmask; i++) {
608 struct hlist_node *entry;
609 struct xfrm_state *x;
611 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+i, bydst) {
612 if (!xfrm_state_kern(x) &&
613 xfrm_id_proto_match(x->id.proto, proto)) {
615 spin_unlock_bh(&xfrm_state_lock);
617 err = xfrm_state_delete(x);
618 xfrm_audit_state_delete(x, err ? 0 : 1,
619 audit_info->loginuid,
620 audit_info->sessionid,
624 spin_lock_bh(&xfrm_state_lock);
632 spin_unlock_bh(&xfrm_state_lock);
633 wake_up(&net->xfrm.km_waitq);
636 EXPORT_SYMBOL(xfrm_state_flush);
638 void xfrm_sad_getinfo(struct xfrmk_sadinfo *si)
640 spin_lock_bh(&xfrm_state_lock);
641 si->sadcnt = init_net.xfrm.state_num;
642 si->sadhcnt = init_net.xfrm.state_hmask;
643 si->sadhmcnt = xfrm_state_hashmax;
644 spin_unlock_bh(&xfrm_state_lock);
646 EXPORT_SYMBOL(xfrm_sad_getinfo);
649 xfrm_init_tempsel(struct xfrm_state *x, struct flowi *fl,
650 struct xfrm_tmpl *tmpl,
651 xfrm_address_t *daddr, xfrm_address_t *saddr,
652 unsigned short family)
654 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
657 afinfo->init_tempsel(x, fl, tmpl, daddr, saddr);
658 xfrm_state_put_afinfo(afinfo);
662 static struct xfrm_state *__xfrm_state_lookup(struct net *net, xfrm_address_t *daddr, __be32 spi, u8 proto, unsigned short family)
664 unsigned int h = xfrm_spi_hash(net, daddr, spi, proto, family);
665 struct xfrm_state *x;
666 struct hlist_node *entry;
668 hlist_for_each_entry(x, entry, net->xfrm.state_byspi+h, byspi) {
669 if (x->props.family != family ||
671 x->id.proto != proto)
676 if (x->id.daddr.a4 != daddr->a4)
680 if (!ipv6_addr_equal((struct in6_addr *)daddr,
694 static struct xfrm_state *__xfrm_state_lookup_byaddr(struct net *net, xfrm_address_t *daddr, xfrm_address_t *saddr, u8 proto, unsigned short family)
696 unsigned int h = xfrm_src_hash(net, daddr, saddr, family);
697 struct xfrm_state *x;
698 struct hlist_node *entry;
700 hlist_for_each_entry(x, entry, net->xfrm.state_bysrc+h, bysrc) {
701 if (x->props.family != family ||
702 x->id.proto != proto)
707 if (x->id.daddr.a4 != daddr->a4 ||
708 x->props.saddr.a4 != saddr->a4)
712 if (!ipv6_addr_equal((struct in6_addr *)daddr,
715 !ipv6_addr_equal((struct in6_addr *)saddr,
729 static inline struct xfrm_state *
730 __xfrm_state_locate(struct xfrm_state *x, int use_spi, int family)
732 struct net *net = xs_net(x);
735 return __xfrm_state_lookup(net, &x->id.daddr, x->id.spi,
736 x->id.proto, family);
738 return __xfrm_state_lookup_byaddr(net, &x->id.daddr,
740 x->id.proto, family);
743 static void xfrm_hash_grow_check(struct net *net, int have_hash_collision)
745 if (have_hash_collision &&
746 (net->xfrm.state_hmask + 1) < xfrm_state_hashmax &&
747 net->xfrm.state_num > net->xfrm.state_hmask)
748 schedule_work(&net->xfrm.state_hash_work);
751 static void xfrm_state_look_at(struct xfrm_policy *pol, struct xfrm_state *x,
752 struct flowi *fl, unsigned short family,
753 xfrm_address_t *daddr, xfrm_address_t *saddr,
754 struct xfrm_state **best, int *acq_in_progress,
758 * 1. There is a valid state with matching selector. Done.
759 * 2. Valid state with inappropriate selector. Skip.
761 * Entering area of "sysdeps".
763 * 3. If state is not valid, selector is temporary, it selects
764 * only session which triggered previous resolution. Key
765 * manager will do something to install a state with proper
768 if (x->km.state == XFRM_STATE_VALID) {
769 if ((x->sel.family &&
770 !xfrm_selector_match(&x->sel, fl, x->sel.family)) ||
771 !security_xfrm_state_pol_flow_match(x, pol, fl))
775 (*best)->km.dying > x->km.dying ||
776 ((*best)->km.dying == x->km.dying &&
777 (*best)->curlft.add_time < x->curlft.add_time))
779 } else if (x->km.state == XFRM_STATE_ACQ) {
780 *acq_in_progress = 1;
781 } else if (x->km.state == XFRM_STATE_ERROR ||
782 x->km.state == XFRM_STATE_EXPIRED) {
783 if (xfrm_selector_match(&x->sel, fl, x->sel.family) &&
784 security_xfrm_state_pol_flow_match(x, pol, fl))
790 xfrm_state_find(xfrm_address_t *daddr, xfrm_address_t *saddr,
791 struct flowi *fl, struct xfrm_tmpl *tmpl,
792 struct xfrm_policy *pol, int *err,
793 unsigned short family)
795 static xfrm_address_t saddr_wildcard = { };
796 struct net *net = xp_net(pol);
798 struct hlist_node *entry;
799 struct xfrm_state *x, *x0, *to_put;
800 int acquire_in_progress = 0;
802 struct xfrm_state *best = NULL;
806 spin_lock_bh(&xfrm_state_lock);
807 h = xfrm_dst_hash(net, daddr, saddr, tmpl->reqid, family);
808 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+h, bydst) {
809 if (x->props.family == family &&
810 x->props.reqid == tmpl->reqid &&
811 !(x->props.flags & XFRM_STATE_WILDRECV) &&
812 xfrm_state_addr_check(x, daddr, saddr, family) &&
813 tmpl->mode == x->props.mode &&
814 tmpl->id.proto == x->id.proto &&
815 (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
816 xfrm_state_look_at(pol, x, fl, family, daddr, saddr,
817 &best, &acquire_in_progress, &error);
822 h = xfrm_dst_hash(net, daddr, &saddr_wildcard, tmpl->reqid, family);
823 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+h, bydst) {
824 if (x->props.family == family &&
825 x->props.reqid == tmpl->reqid &&
826 !(x->props.flags & XFRM_STATE_WILDRECV) &&
827 xfrm_state_addr_check(x, daddr, saddr, family) &&
828 tmpl->mode == x->props.mode &&
829 tmpl->id.proto == x->id.proto &&
830 (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
831 xfrm_state_look_at(pol, x, fl, family, daddr, saddr,
832 &best, &acquire_in_progress, &error);
837 if (!x && !error && !acquire_in_progress) {
839 (x0 = __xfrm_state_lookup(net, daddr, tmpl->id.spi,
840 tmpl->id.proto, family)) != NULL) {
845 x = xfrm_state_alloc(net);
850 /* Initialize temporary selector matching only
851 * to current session. */
852 xfrm_init_tempsel(x, fl, tmpl, daddr, saddr, family);
854 error = security_xfrm_state_alloc_acquire(x, pol->security, fl->secid);
856 x->km.state = XFRM_STATE_DEAD;
862 if (km_query(x, tmpl, pol) == 0) {
863 x->km.state = XFRM_STATE_ACQ;
864 list_add(&x->km.all, &net->xfrm.state_all);
865 hlist_add_head(&x->bydst, net->xfrm.state_bydst+h);
866 h = xfrm_src_hash(net, daddr, saddr, family);
867 hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h);
869 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, family);
870 hlist_add_head(&x->byspi, net->xfrm.state_byspi+h);
872 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
873 x->timer.expires = jiffies + net->xfrm.sysctl_acq_expires*HZ;
874 add_timer(&x->timer);
875 net->xfrm.state_num++;
876 xfrm_hash_grow_check(net, x->bydst.next != NULL);
878 x->km.state = XFRM_STATE_DEAD;
888 *err = acquire_in_progress ? -EAGAIN : error;
889 spin_unlock_bh(&xfrm_state_lock);
891 xfrm_state_put(to_put);
896 xfrm_stateonly_find(struct net *net,
897 xfrm_address_t *daddr, xfrm_address_t *saddr,
898 unsigned short family, u8 mode, u8 proto, u32 reqid)
901 struct xfrm_state *rx = NULL, *x = NULL;
902 struct hlist_node *entry;
904 spin_lock(&xfrm_state_lock);
905 h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
906 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+h, bydst) {
907 if (x->props.family == family &&
908 x->props.reqid == reqid &&
909 !(x->props.flags & XFRM_STATE_WILDRECV) &&
910 xfrm_state_addr_check(x, daddr, saddr, family) &&
911 mode == x->props.mode &&
912 proto == x->id.proto &&
913 x->km.state == XFRM_STATE_VALID) {
921 spin_unlock(&xfrm_state_lock);
926 EXPORT_SYMBOL(xfrm_stateonly_find);
928 static void __xfrm_state_insert(struct xfrm_state *x)
930 struct net *net = xs_net(x);
933 x->genid = ++xfrm_state_genid;
935 list_add(&x->km.all, &net->xfrm.state_all);
937 h = xfrm_dst_hash(net, &x->id.daddr, &x->props.saddr,
938 x->props.reqid, x->props.family);
939 hlist_add_head(&x->bydst, net->xfrm.state_bydst+h);
941 h = xfrm_src_hash(net, &x->id.daddr, &x->props.saddr, x->props.family);
942 hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h);
945 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto,
948 hlist_add_head(&x->byspi, net->xfrm.state_byspi+h);
951 mod_timer(&x->timer, jiffies + HZ);
952 if (x->replay_maxage)
953 mod_timer(&x->rtimer, jiffies + x->replay_maxage);
955 wake_up(&net->xfrm.km_waitq);
957 net->xfrm.state_num++;
959 xfrm_hash_grow_check(net, x->bydst.next != NULL);
962 /* xfrm_state_lock is held */
963 static void __xfrm_state_bump_genids(struct xfrm_state *xnew)
965 struct net *net = xs_net(xnew);
966 unsigned short family = xnew->props.family;
967 u32 reqid = xnew->props.reqid;
968 struct xfrm_state *x;
969 struct hlist_node *entry;
972 h = xfrm_dst_hash(net, &xnew->id.daddr, &xnew->props.saddr, reqid, family);
973 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+h, bydst) {
974 if (x->props.family == family &&
975 x->props.reqid == reqid &&
976 !xfrm_addr_cmp(&x->id.daddr, &xnew->id.daddr, family) &&
977 !xfrm_addr_cmp(&x->props.saddr, &xnew->props.saddr, family))
978 x->genid = xfrm_state_genid;
982 void xfrm_state_insert(struct xfrm_state *x)
984 spin_lock_bh(&xfrm_state_lock);
985 __xfrm_state_bump_genids(x);
986 __xfrm_state_insert(x);
987 spin_unlock_bh(&xfrm_state_lock);
989 EXPORT_SYMBOL(xfrm_state_insert);
991 /* xfrm_state_lock is held */
992 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)
994 unsigned int h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
995 struct hlist_node *entry;
996 struct xfrm_state *x;
998 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+h, bydst) {
999 if (x->props.reqid != reqid ||
1000 x->props.mode != mode ||
1001 x->props.family != family ||
1002 x->km.state != XFRM_STATE_ACQ ||
1004 x->id.proto != proto)
1009 if (x->id.daddr.a4 != daddr->a4 ||
1010 x->props.saddr.a4 != saddr->a4)
1014 if (!ipv6_addr_equal((struct in6_addr *)x->id.daddr.a6,
1015 (struct in6_addr *)daddr) ||
1016 !ipv6_addr_equal((struct in6_addr *)
1018 (struct in6_addr *)saddr))
1030 x = xfrm_state_alloc(net);
1034 x->sel.daddr.a4 = daddr->a4;
1035 x->sel.saddr.a4 = saddr->a4;
1036 x->sel.prefixlen_d = 32;
1037 x->sel.prefixlen_s = 32;
1038 x->props.saddr.a4 = saddr->a4;
1039 x->id.daddr.a4 = daddr->a4;
1043 ipv6_addr_copy((struct in6_addr *)x->sel.daddr.a6,
1044 (struct in6_addr *)daddr);
1045 ipv6_addr_copy((struct in6_addr *)x->sel.saddr.a6,
1046 (struct in6_addr *)saddr);
1047 x->sel.prefixlen_d = 128;
1048 x->sel.prefixlen_s = 128;
1049 ipv6_addr_copy((struct in6_addr *)x->props.saddr.a6,
1050 (struct in6_addr *)saddr);
1051 ipv6_addr_copy((struct in6_addr *)x->id.daddr.a6,
1052 (struct in6_addr *)daddr);
1056 x->km.state = XFRM_STATE_ACQ;
1057 x->id.proto = proto;
1058 x->props.family = family;
1059 x->props.mode = mode;
1060 x->props.reqid = reqid;
1061 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
1063 x->timer.expires = jiffies + net->xfrm.sysctl_acq_expires*HZ;
1064 add_timer(&x->timer);
1065 list_add(&x->km.all, &net->xfrm.state_all);
1066 hlist_add_head(&x->bydst, net->xfrm.state_bydst+h);
1067 h = xfrm_src_hash(net, daddr, saddr, family);
1068 hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h);
1070 net->xfrm.state_num++;
1072 xfrm_hash_grow_check(net, x->bydst.next != NULL);
1078 static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 seq);
1080 int xfrm_state_add(struct xfrm_state *x)
1082 struct net *net = xs_net(x);
1083 struct xfrm_state *x1, *to_put;
1086 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
1088 family = x->props.family;
1092 spin_lock_bh(&xfrm_state_lock);
1094 x1 = __xfrm_state_locate(x, use_spi, family);
1102 if (use_spi && x->km.seq) {
1103 x1 = __xfrm_find_acq_byseq(net, x->km.seq);
1104 if (x1 && ((x1->id.proto != x->id.proto) ||
1105 xfrm_addr_cmp(&x1->id.daddr, &x->id.daddr, family))) {
1112 x1 = __find_acq_core(net, family, x->props.mode, x->props.reqid,
1114 &x->id.daddr, &x->props.saddr, 0);
1116 __xfrm_state_bump_genids(x);
1117 __xfrm_state_insert(x);
1121 spin_unlock_bh(&xfrm_state_lock);
1124 xfrm_state_delete(x1);
1129 xfrm_state_put(to_put);
1133 EXPORT_SYMBOL(xfrm_state_add);
1135 #ifdef CONFIG_XFRM_MIGRATE
1136 static struct xfrm_state *xfrm_state_clone(struct xfrm_state *orig, int *errp)
1138 struct net *net = xs_net(orig);
1140 struct xfrm_state *x = xfrm_state_alloc(net);
1144 memcpy(&x->id, &orig->id, sizeof(x->id));
1145 memcpy(&x->sel, &orig->sel, sizeof(x->sel));
1146 memcpy(&x->lft, &orig->lft, sizeof(x->lft));
1147 x->props.mode = orig->props.mode;
1148 x->props.replay_window = orig->props.replay_window;
1149 x->props.reqid = orig->props.reqid;
1150 x->props.family = orig->props.family;
1151 x->props.saddr = orig->props.saddr;
1154 x->aalg = xfrm_algo_clone(orig->aalg);
1158 x->props.aalgo = orig->props.aalgo;
1161 x->ealg = xfrm_algo_clone(orig->ealg);
1165 x->props.ealgo = orig->props.ealgo;
1168 x->calg = xfrm_algo_clone(orig->calg);
1172 x->props.calgo = orig->props.calgo;
1175 x->encap = kmemdup(orig->encap, sizeof(*x->encap), GFP_KERNEL);
1181 x->coaddr = kmemdup(orig->coaddr, sizeof(*x->coaddr),
1187 err = xfrm_init_state(x);
1191 x->props.flags = orig->props.flags;
1193 x->curlft.add_time = orig->curlft.add_time;
1194 x->km.state = orig->km.state;
1195 x->km.seq = orig->km.seq;
1213 /* xfrm_state_lock is held */
1214 struct xfrm_state * xfrm_migrate_state_find(struct xfrm_migrate *m)
1217 struct xfrm_state *x;
1218 struct hlist_node *entry;
1221 h = xfrm_dst_hash(&init_net, &m->old_daddr, &m->old_saddr,
1222 m->reqid, m->old_family);
1223 hlist_for_each_entry(x, entry, init_net.xfrm.state_bydst+h, bydst) {
1224 if (x->props.mode != m->mode ||
1225 x->id.proto != m->proto)
1227 if (m->reqid && x->props.reqid != m->reqid)
1229 if (xfrm_addr_cmp(&x->id.daddr, &m->old_daddr,
1231 xfrm_addr_cmp(&x->props.saddr, &m->old_saddr,
1238 h = xfrm_src_hash(&init_net, &m->old_daddr, &m->old_saddr,
1240 hlist_for_each_entry(x, entry, init_net.xfrm.state_bysrc+h, bysrc) {
1241 if (x->props.mode != m->mode ||
1242 x->id.proto != m->proto)
1244 if (xfrm_addr_cmp(&x->id.daddr, &m->old_daddr,
1246 xfrm_addr_cmp(&x->props.saddr, &m->old_saddr,
1256 EXPORT_SYMBOL(xfrm_migrate_state_find);
1258 struct xfrm_state * xfrm_state_migrate(struct xfrm_state *x,
1259 struct xfrm_migrate *m)
1261 struct xfrm_state *xc;
1264 xc = xfrm_state_clone(x, &err);
1268 memcpy(&xc->id.daddr, &m->new_daddr, sizeof(xc->id.daddr));
1269 memcpy(&xc->props.saddr, &m->new_saddr, sizeof(xc->props.saddr));
1272 if (!xfrm_addr_cmp(&x->id.daddr, &m->new_daddr, m->new_family)) {
1273 /* a care is needed when the destination address of the
1274 state is to be updated as it is a part of triplet */
1275 xfrm_state_insert(xc);
1277 if ((err = xfrm_state_add(xc)) < 0)
1286 EXPORT_SYMBOL(xfrm_state_migrate);
1289 int xfrm_state_update(struct xfrm_state *x)
1291 struct xfrm_state *x1, *to_put;
1293 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
1297 spin_lock_bh(&xfrm_state_lock);
1298 x1 = __xfrm_state_locate(x, use_spi, x->props.family);
1304 if (xfrm_state_kern(x1)) {
1310 if (x1->km.state == XFRM_STATE_ACQ) {
1311 __xfrm_state_insert(x);
1317 spin_unlock_bh(&xfrm_state_lock);
1320 xfrm_state_put(to_put);
1326 xfrm_state_delete(x1);
1332 spin_lock_bh(&x1->lock);
1333 if (likely(x1->km.state == XFRM_STATE_VALID)) {
1334 if (x->encap && x1->encap)
1335 memcpy(x1->encap, x->encap, sizeof(*x1->encap));
1336 if (x->coaddr && x1->coaddr) {
1337 memcpy(x1->coaddr, x->coaddr, sizeof(*x1->coaddr));
1339 if (!use_spi && memcmp(&x1->sel, &x->sel, sizeof(x1->sel)))
1340 memcpy(&x1->sel, &x->sel, sizeof(x1->sel));
1341 memcpy(&x1->lft, &x->lft, sizeof(x1->lft));
1344 mod_timer(&x1->timer, jiffies + HZ);
1345 if (x1->curlft.use_time)
1346 xfrm_state_check_expire(x1);
1350 spin_unlock_bh(&x1->lock);
1356 EXPORT_SYMBOL(xfrm_state_update);
1358 int xfrm_state_check_expire(struct xfrm_state *x)
1360 if (!x->curlft.use_time)
1361 x->curlft.use_time = get_seconds();
1363 if (x->km.state != XFRM_STATE_VALID)
1366 if (x->curlft.bytes >= x->lft.hard_byte_limit ||
1367 x->curlft.packets >= x->lft.hard_packet_limit) {
1368 x->km.state = XFRM_STATE_EXPIRED;
1369 mod_timer(&x->timer, jiffies);
1374 (x->curlft.bytes >= x->lft.soft_byte_limit ||
1375 x->curlft.packets >= x->lft.soft_packet_limit)) {
1377 km_state_expired(x, 0, 0);
1381 EXPORT_SYMBOL(xfrm_state_check_expire);
1384 xfrm_state_lookup(struct net *net, xfrm_address_t *daddr, __be32 spi, u8 proto,
1385 unsigned short family)
1387 struct xfrm_state *x;
1389 spin_lock_bh(&xfrm_state_lock);
1390 x = __xfrm_state_lookup(net, daddr, spi, proto, family);
1391 spin_unlock_bh(&xfrm_state_lock);
1394 EXPORT_SYMBOL(xfrm_state_lookup);
1397 xfrm_state_lookup_byaddr(struct net *net,
1398 xfrm_address_t *daddr, xfrm_address_t *saddr,
1399 u8 proto, unsigned short family)
1401 struct xfrm_state *x;
1403 spin_lock_bh(&xfrm_state_lock);
1404 x = __xfrm_state_lookup_byaddr(net, daddr, saddr, proto, family);
1405 spin_unlock_bh(&xfrm_state_lock);
1408 EXPORT_SYMBOL(xfrm_state_lookup_byaddr);
1411 xfrm_find_acq(struct net *net, u8 mode, u32 reqid, u8 proto,
1412 xfrm_address_t *daddr, xfrm_address_t *saddr,
1413 int create, unsigned short family)
1415 struct xfrm_state *x;
1417 spin_lock_bh(&xfrm_state_lock);
1418 x = __find_acq_core(net, family, mode, reqid, proto, daddr, saddr, create);
1419 spin_unlock_bh(&xfrm_state_lock);
1423 EXPORT_SYMBOL(xfrm_find_acq);
1425 #ifdef CONFIG_XFRM_SUB_POLICY
1427 xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n,
1428 unsigned short family)
1431 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
1433 return -EAFNOSUPPORT;
1435 spin_lock_bh(&xfrm_state_lock);
1436 if (afinfo->tmpl_sort)
1437 err = afinfo->tmpl_sort(dst, src, n);
1438 spin_unlock_bh(&xfrm_state_lock);
1439 xfrm_state_put_afinfo(afinfo);
1442 EXPORT_SYMBOL(xfrm_tmpl_sort);
1445 xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src, int n,
1446 unsigned short family)
1449 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
1451 return -EAFNOSUPPORT;
1453 spin_lock_bh(&xfrm_state_lock);
1454 if (afinfo->state_sort)
1455 err = afinfo->state_sort(dst, src, n);
1456 spin_unlock_bh(&xfrm_state_lock);
1457 xfrm_state_put_afinfo(afinfo);
1460 EXPORT_SYMBOL(xfrm_state_sort);
1463 /* Silly enough, but I'm lazy to build resolution list */
1465 static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 seq)
1469 for (i = 0; i <= net->xfrm.state_hmask; i++) {
1470 struct hlist_node *entry;
1471 struct xfrm_state *x;
1473 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+i, bydst) {
1474 if (x->km.seq == seq &&
1475 x->km.state == XFRM_STATE_ACQ) {
1484 struct xfrm_state *xfrm_find_acq_byseq(struct net *net, u32 seq)
1486 struct xfrm_state *x;
1488 spin_lock_bh(&xfrm_state_lock);
1489 x = __xfrm_find_acq_byseq(net, seq);
1490 spin_unlock_bh(&xfrm_state_lock);
1493 EXPORT_SYMBOL(xfrm_find_acq_byseq);
1495 u32 xfrm_get_acqseq(void)
1499 static DEFINE_SPINLOCK(acqseq_lock);
1501 spin_lock_bh(&acqseq_lock);
1502 res = (++acqseq ? : ++acqseq);
1503 spin_unlock_bh(&acqseq_lock);
1506 EXPORT_SYMBOL(xfrm_get_acqseq);
1508 int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high)
1510 struct net *net = xs_net(x);
1512 struct xfrm_state *x0;
1514 __be32 minspi = htonl(low);
1515 __be32 maxspi = htonl(high);
1517 spin_lock_bh(&x->lock);
1518 if (x->km.state == XFRM_STATE_DEAD)
1527 if (minspi == maxspi) {
1528 x0 = xfrm_state_lookup(net, &x->id.daddr, minspi, x->id.proto, x->props.family);
1536 for (h=0; h<high-low+1; h++) {
1537 spi = low + net_random()%(high-low+1);
1538 x0 = xfrm_state_lookup(net, &x->id.daddr, htonl(spi), x->id.proto, x->props.family);
1540 x->id.spi = htonl(spi);
1547 spin_lock_bh(&xfrm_state_lock);
1548 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, x->props.family);
1549 hlist_add_head(&x->byspi, net->xfrm.state_byspi+h);
1550 spin_unlock_bh(&xfrm_state_lock);
1556 spin_unlock_bh(&x->lock);
1560 EXPORT_SYMBOL(xfrm_alloc_spi);
1562 int xfrm_state_walk(struct net *net, struct xfrm_state_walk *walk,
1563 int (*func)(struct xfrm_state *, int, void*),
1566 struct xfrm_state *state;
1567 struct xfrm_state_walk *x;
1570 if (walk->seq != 0 && list_empty(&walk->all))
1573 spin_lock_bh(&xfrm_state_lock);
1574 if (list_empty(&walk->all))
1575 x = list_first_entry(&net->xfrm.state_all, struct xfrm_state_walk, all);
1577 x = list_entry(&walk->all, struct xfrm_state_walk, all);
1578 list_for_each_entry_from(x, &net->xfrm.state_all, all) {
1579 if (x->state == XFRM_STATE_DEAD)
1581 state = container_of(x, struct xfrm_state, km);
1582 if (!xfrm_id_proto_match(state->id.proto, walk->proto))
1584 err = func(state, walk->seq, data);
1586 list_move_tail(&walk->all, &x->all);
1591 if (walk->seq == 0) {
1595 list_del_init(&walk->all);
1597 spin_unlock_bh(&xfrm_state_lock);
1600 EXPORT_SYMBOL(xfrm_state_walk);
1602 void xfrm_state_walk_init(struct xfrm_state_walk *walk, u8 proto)
1604 INIT_LIST_HEAD(&walk->all);
1605 walk->proto = proto;
1606 walk->state = XFRM_STATE_DEAD;
1609 EXPORT_SYMBOL(xfrm_state_walk_init);
1611 void xfrm_state_walk_done(struct xfrm_state_walk *walk)
1613 if (list_empty(&walk->all))
1616 spin_lock_bh(&xfrm_state_lock);
1617 list_del(&walk->all);
1618 spin_unlock_bh(&xfrm_state_lock);
1620 EXPORT_SYMBOL(xfrm_state_walk_done);
1623 void xfrm_replay_notify(struct xfrm_state *x, int event)
1626 /* we send notify messages in case
1627 * 1. we updated on of the sequence numbers, and the seqno difference
1628 * is at least x->replay_maxdiff, in this case we also update the
1629 * timeout of our timer function
1630 * 2. if x->replay_maxage has elapsed since last update,
1631 * and there were changes
1633 * The state structure must be locked!
1637 case XFRM_REPLAY_UPDATE:
1638 if (x->replay_maxdiff &&
1639 (x->replay.seq - x->preplay.seq < x->replay_maxdiff) &&
1640 (x->replay.oseq - x->preplay.oseq < x->replay_maxdiff)) {
1641 if (x->xflags & XFRM_TIME_DEFER)
1642 event = XFRM_REPLAY_TIMEOUT;
1649 case XFRM_REPLAY_TIMEOUT:
1650 if ((x->replay.seq == x->preplay.seq) &&
1651 (x->replay.bitmap == x->preplay.bitmap) &&
1652 (x->replay.oseq == x->preplay.oseq)) {
1653 x->xflags |= XFRM_TIME_DEFER;
1660 memcpy(&x->preplay, &x->replay, sizeof(struct xfrm_replay_state));
1661 c.event = XFRM_MSG_NEWAE;
1662 c.data.aevent = event;
1663 km_state_notify(x, &c);
1665 if (x->replay_maxage &&
1666 !mod_timer(&x->rtimer, jiffies + x->replay_maxage))
1667 x->xflags &= ~XFRM_TIME_DEFER;
1670 static void xfrm_replay_timer_handler(unsigned long data)
1672 struct xfrm_state *x = (struct xfrm_state*)data;
1674 spin_lock(&x->lock);
1676 if (x->km.state == XFRM_STATE_VALID) {
1677 if (xfrm_aevent_is_on(xs_net(x)))
1678 xfrm_replay_notify(x, XFRM_REPLAY_TIMEOUT);
1680 x->xflags |= XFRM_TIME_DEFER;
1683 spin_unlock(&x->lock);
1686 int xfrm_replay_check(struct xfrm_state *x,
1687 struct sk_buff *skb, __be32 net_seq)
1690 u32 seq = ntohl(net_seq);
1692 if (unlikely(seq == 0))
1695 if (likely(seq > x->replay.seq))
1698 diff = x->replay.seq - seq;
1699 if (diff >= min_t(unsigned int, x->props.replay_window,
1700 sizeof(x->replay.bitmap) * 8)) {
1701 x->stats.replay_window++;
1705 if (x->replay.bitmap & (1U << diff)) {
1712 xfrm_audit_state_replay(x, skb, net_seq);
1716 void xfrm_replay_advance(struct xfrm_state *x, __be32 net_seq)
1719 u32 seq = ntohl(net_seq);
1721 if (seq > x->replay.seq) {
1722 diff = seq - x->replay.seq;
1723 if (diff < x->props.replay_window)
1724 x->replay.bitmap = ((x->replay.bitmap) << diff) | 1;
1726 x->replay.bitmap = 1;
1727 x->replay.seq = seq;
1729 diff = x->replay.seq - seq;
1730 x->replay.bitmap |= (1U << diff);
1733 if (xfrm_aevent_is_on(xs_net(x)))
1734 xfrm_replay_notify(x, XFRM_REPLAY_UPDATE);
1737 static LIST_HEAD(xfrm_km_list);
1738 static DEFINE_RWLOCK(xfrm_km_lock);
1740 void km_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
1742 struct xfrm_mgr *km;
1744 read_lock(&xfrm_km_lock);
1745 list_for_each_entry(km, &xfrm_km_list, list)
1746 if (km->notify_policy)
1747 km->notify_policy(xp, dir, c);
1748 read_unlock(&xfrm_km_lock);
1751 void km_state_notify(struct xfrm_state *x, struct km_event *c)
1753 struct xfrm_mgr *km;
1754 read_lock(&xfrm_km_lock);
1755 list_for_each_entry(km, &xfrm_km_list, list)
1758 read_unlock(&xfrm_km_lock);
1761 EXPORT_SYMBOL(km_policy_notify);
1762 EXPORT_SYMBOL(km_state_notify);
1764 void km_state_expired(struct xfrm_state *x, int hard, u32 pid)
1766 struct net *net = xs_net(x);
1771 c.event = XFRM_MSG_EXPIRE;
1772 km_state_notify(x, &c);
1775 wake_up(&net->xfrm.km_waitq);
1778 EXPORT_SYMBOL(km_state_expired);
1780 * We send to all registered managers regardless of failure
1781 * We are happy with one success
1783 int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol)
1785 int err = -EINVAL, acqret;
1786 struct xfrm_mgr *km;
1788 read_lock(&xfrm_km_lock);
1789 list_for_each_entry(km, &xfrm_km_list, list) {
1790 acqret = km->acquire(x, t, pol, XFRM_POLICY_OUT);
1794 read_unlock(&xfrm_km_lock);
1797 EXPORT_SYMBOL(km_query);
1799 int km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
1802 struct xfrm_mgr *km;
1804 read_lock(&xfrm_km_lock);
1805 list_for_each_entry(km, &xfrm_km_list, list) {
1806 if (km->new_mapping)
1807 err = km->new_mapping(x, ipaddr, sport);
1811 read_unlock(&xfrm_km_lock);
1814 EXPORT_SYMBOL(km_new_mapping);
1816 void km_policy_expired(struct xfrm_policy *pol, int dir, int hard, u32 pid)
1818 struct net *net = xp_net(pol);
1823 c.event = XFRM_MSG_POLEXPIRE;
1824 km_policy_notify(pol, dir, &c);
1827 wake_up(&net->xfrm.km_waitq);
1829 EXPORT_SYMBOL(km_policy_expired);
1831 #ifdef CONFIG_XFRM_MIGRATE
1832 int km_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
1833 struct xfrm_migrate *m, int num_migrate,
1834 struct xfrm_kmaddress *k)
1838 struct xfrm_mgr *km;
1840 read_lock(&xfrm_km_lock);
1841 list_for_each_entry(km, &xfrm_km_list, list) {
1843 ret = km->migrate(sel, dir, type, m, num_migrate, k);
1848 read_unlock(&xfrm_km_lock);
1851 EXPORT_SYMBOL(km_migrate);
1854 int km_report(struct net *net, u8 proto, struct xfrm_selector *sel, xfrm_address_t *addr)
1858 struct xfrm_mgr *km;
1860 read_lock(&xfrm_km_lock);
1861 list_for_each_entry(km, &xfrm_km_list, list) {
1863 ret = km->report(net, proto, sel, addr);
1868 read_unlock(&xfrm_km_lock);
1871 EXPORT_SYMBOL(km_report);
1873 int xfrm_user_policy(struct sock *sk, int optname, u8 __user *optval, int optlen)
1877 struct xfrm_mgr *km;
1878 struct xfrm_policy *pol = NULL;
1880 if (optlen <= 0 || optlen > PAGE_SIZE)
1883 data = kmalloc(optlen, GFP_KERNEL);
1888 if (copy_from_user(data, optval, optlen))
1892 read_lock(&xfrm_km_lock);
1893 list_for_each_entry(km, &xfrm_km_list, list) {
1894 pol = km->compile_policy(sk, optname, data,
1899 read_unlock(&xfrm_km_lock);
1902 xfrm_sk_policy_insert(sk, err, pol);
1911 EXPORT_SYMBOL(xfrm_user_policy);
1913 int xfrm_register_km(struct xfrm_mgr *km)
1915 write_lock_bh(&xfrm_km_lock);
1916 list_add_tail(&km->list, &xfrm_km_list);
1917 write_unlock_bh(&xfrm_km_lock);
1920 EXPORT_SYMBOL(xfrm_register_km);
1922 int xfrm_unregister_km(struct xfrm_mgr *km)
1924 write_lock_bh(&xfrm_km_lock);
1925 list_del(&km->list);
1926 write_unlock_bh(&xfrm_km_lock);
1929 EXPORT_SYMBOL(xfrm_unregister_km);
1931 int xfrm_state_register_afinfo(struct xfrm_state_afinfo *afinfo)
1934 if (unlikely(afinfo == NULL))
1936 if (unlikely(afinfo->family >= NPROTO))
1937 return -EAFNOSUPPORT;
1938 write_lock_bh(&xfrm_state_afinfo_lock);
1939 if (unlikely(xfrm_state_afinfo[afinfo->family] != NULL))
1942 xfrm_state_afinfo[afinfo->family] = afinfo;
1943 write_unlock_bh(&xfrm_state_afinfo_lock);
1946 EXPORT_SYMBOL(xfrm_state_register_afinfo);
1948 int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo)
1951 if (unlikely(afinfo == NULL))
1953 if (unlikely(afinfo->family >= NPROTO))
1954 return -EAFNOSUPPORT;
1955 write_lock_bh(&xfrm_state_afinfo_lock);
1956 if (likely(xfrm_state_afinfo[afinfo->family] != NULL)) {
1957 if (unlikely(xfrm_state_afinfo[afinfo->family] != afinfo))
1960 xfrm_state_afinfo[afinfo->family] = NULL;
1962 write_unlock_bh(&xfrm_state_afinfo_lock);
1965 EXPORT_SYMBOL(xfrm_state_unregister_afinfo);
1967 static struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family)
1969 struct xfrm_state_afinfo *afinfo;
1970 if (unlikely(family >= NPROTO))
1972 read_lock(&xfrm_state_afinfo_lock);
1973 afinfo = xfrm_state_afinfo[family];
1974 if (unlikely(!afinfo))
1975 read_unlock(&xfrm_state_afinfo_lock);
1979 static void xfrm_state_put_afinfo(struct xfrm_state_afinfo *afinfo)
1980 __releases(xfrm_state_afinfo_lock)
1982 read_unlock(&xfrm_state_afinfo_lock);
1985 /* Temporarily located here until net/xfrm/xfrm_tunnel.c is created */
1986 void xfrm_state_delete_tunnel(struct xfrm_state *x)
1989 struct xfrm_state *t = x->tunnel;
1991 if (atomic_read(&t->tunnel_users) == 2)
1992 xfrm_state_delete(t);
1993 atomic_dec(&t->tunnel_users);
1998 EXPORT_SYMBOL(xfrm_state_delete_tunnel);
2000 int xfrm_state_mtu(struct xfrm_state *x, int mtu)
2004 spin_lock_bh(&x->lock);
2005 if (x->km.state == XFRM_STATE_VALID &&
2006 x->type && x->type->get_mtu)
2007 res = x->type->get_mtu(x, mtu);
2009 res = mtu - x->props.header_len;
2010 spin_unlock_bh(&x->lock);
2014 int xfrm_init_state(struct xfrm_state *x)
2016 struct xfrm_state_afinfo *afinfo;
2017 struct xfrm_mode *inner_mode;
2018 int family = x->props.family;
2021 err = -EAFNOSUPPORT;
2022 afinfo = xfrm_state_get_afinfo(family);
2027 if (afinfo->init_flags)
2028 err = afinfo->init_flags(x);
2030 xfrm_state_put_afinfo(afinfo);
2035 err = -EPROTONOSUPPORT;
2037 if (x->sel.family != AF_UNSPEC) {
2038 inner_mode = xfrm_get_mode(x->props.mode, x->sel.family);
2039 if (inner_mode == NULL)
2042 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL) &&
2043 family != x->sel.family) {
2044 xfrm_put_mode(inner_mode);
2048 x->inner_mode = inner_mode;
2050 struct xfrm_mode *inner_mode_iaf;
2051 int iafamily = AF_INET;
2053 inner_mode = xfrm_get_mode(x->props.mode, x->props.family);
2054 if (inner_mode == NULL)
2057 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL)) {
2058 xfrm_put_mode(inner_mode);
2061 x->inner_mode = inner_mode;
2063 if (x->props.family == AF_INET)
2064 iafamily = AF_INET6;
2066 inner_mode_iaf = xfrm_get_mode(x->props.mode, iafamily);
2067 if (inner_mode_iaf) {
2068 if (inner_mode_iaf->flags & XFRM_MODE_FLAG_TUNNEL)
2069 x->inner_mode_iaf = inner_mode_iaf;
2071 xfrm_put_mode(inner_mode_iaf);
2075 x->type = xfrm_get_type(x->id.proto, family);
2076 if (x->type == NULL)
2079 err = x->type->init_state(x);
2083 x->outer_mode = xfrm_get_mode(x->props.mode, family);
2084 if (x->outer_mode == NULL)
2087 x->km.state = XFRM_STATE_VALID;
2093 EXPORT_SYMBOL(xfrm_init_state);
2095 int __net_init xfrm_state_init(struct net *net)
2099 INIT_LIST_HEAD(&net->xfrm.state_all);
2101 sz = sizeof(struct hlist_head) * 8;
2103 net->xfrm.state_bydst = xfrm_hash_alloc(sz);
2104 if (!net->xfrm.state_bydst)
2106 net->xfrm.state_bysrc = xfrm_hash_alloc(sz);
2107 if (!net->xfrm.state_bysrc)
2109 net->xfrm.state_byspi = xfrm_hash_alloc(sz);
2110 if (!net->xfrm.state_byspi)
2112 net->xfrm.state_hmask = ((sz / sizeof(struct hlist_head)) - 1);
2114 net->xfrm.state_num = 0;
2115 INIT_WORK(&net->xfrm.state_hash_work, xfrm_hash_resize);
2116 INIT_HLIST_HEAD(&net->xfrm.state_gc_list);
2117 INIT_WORK(&net->xfrm.state_gc_work, xfrm_state_gc_task);
2118 init_waitqueue_head(&net->xfrm.km_waitq);
2122 xfrm_hash_free(net->xfrm.state_bysrc, sz);
2124 xfrm_hash_free(net->xfrm.state_bydst, sz);
2129 void xfrm_state_fini(struct net *net)
2131 struct xfrm_audit audit_info;
2134 flush_work(&net->xfrm.state_hash_work);
2135 audit_info.loginuid = -1;
2136 audit_info.sessionid = -1;
2137 audit_info.secid = 0;
2138 xfrm_state_flush(net, IPSEC_PROTO_ANY, &audit_info);
2139 flush_work(&net->xfrm.state_gc_work);
2141 WARN_ON(!list_empty(&net->xfrm.state_all));
2143 sz = (net->xfrm.state_hmask + 1) * sizeof(struct hlist_head);
2144 WARN_ON(!hlist_empty(net->xfrm.state_byspi));
2145 xfrm_hash_free(net->xfrm.state_byspi, sz);
2146 WARN_ON(!hlist_empty(net->xfrm.state_bysrc));
2147 xfrm_hash_free(net->xfrm.state_bysrc, sz);
2148 WARN_ON(!hlist_empty(net->xfrm.state_bydst));
2149 xfrm_hash_free(net->xfrm.state_bydst, sz);
2152 #ifdef CONFIG_AUDITSYSCALL
2153 static void xfrm_audit_helper_sainfo(struct xfrm_state *x,
2154 struct audit_buffer *audit_buf)
2156 struct xfrm_sec_ctx *ctx = x->security;
2157 u32 spi = ntohl(x->id.spi);
2160 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
2161 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
2163 switch(x->props.family) {
2165 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2166 &x->props.saddr.a4, &x->id.daddr.a4);
2169 audit_log_format(audit_buf, " src=%pI6 dst=%pI6",
2170 x->props.saddr.a6, x->id.daddr.a6);
2174 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
2177 static void xfrm_audit_helper_pktinfo(struct sk_buff *skb, u16 family,
2178 struct audit_buffer *audit_buf)
2181 struct ipv6hdr *iph6;
2186 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2187 &iph4->saddr, &iph4->daddr);
2190 iph6 = ipv6_hdr(skb);
2191 audit_log_format(audit_buf,
2192 " src=%pI6 dst=%pI6 flowlbl=0x%x%02x%02x",
2193 &iph6->saddr,&iph6->daddr,
2194 iph6->flow_lbl[0] & 0x0f,
2201 void xfrm_audit_state_add(struct xfrm_state *x, int result,
2202 uid_t auid, u32 sessionid, u32 secid)
2204 struct audit_buffer *audit_buf;
2206 audit_buf = xfrm_audit_start("SAD-add");
2207 if (audit_buf == NULL)
2209 xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf);
2210 xfrm_audit_helper_sainfo(x, audit_buf);
2211 audit_log_format(audit_buf, " res=%u", result);
2212 audit_log_end(audit_buf);
2214 EXPORT_SYMBOL_GPL(xfrm_audit_state_add);
2216 void xfrm_audit_state_delete(struct xfrm_state *x, int result,
2217 uid_t auid, u32 sessionid, u32 secid)
2219 struct audit_buffer *audit_buf;
2221 audit_buf = xfrm_audit_start("SAD-delete");
2222 if (audit_buf == NULL)
2224 xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf);
2225 xfrm_audit_helper_sainfo(x, audit_buf);
2226 audit_log_format(audit_buf, " res=%u", result);
2227 audit_log_end(audit_buf);
2229 EXPORT_SYMBOL_GPL(xfrm_audit_state_delete);
2231 void xfrm_audit_state_replay_overflow(struct xfrm_state *x,
2232 struct sk_buff *skb)
2234 struct audit_buffer *audit_buf;
2237 audit_buf = xfrm_audit_start("SA-replay-overflow");
2238 if (audit_buf == NULL)
2240 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2241 /* don't record the sequence number because it's inherent in this kind
2242 * of audit message */
2243 spi = ntohl(x->id.spi);
2244 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
2245 audit_log_end(audit_buf);
2247 EXPORT_SYMBOL_GPL(xfrm_audit_state_replay_overflow);
2249 static void xfrm_audit_state_replay(struct xfrm_state *x,
2250 struct sk_buff *skb, __be32 net_seq)
2252 struct audit_buffer *audit_buf;
2255 audit_buf = xfrm_audit_start("SA-replayed-pkt");
2256 if (audit_buf == NULL)
2258 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2259 spi = ntohl(x->id.spi);
2260 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2261 spi, spi, ntohl(net_seq));
2262 audit_log_end(audit_buf);
2265 void xfrm_audit_state_notfound_simple(struct sk_buff *skb, u16 family)
2267 struct audit_buffer *audit_buf;
2269 audit_buf = xfrm_audit_start("SA-notfound");
2270 if (audit_buf == NULL)
2272 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2273 audit_log_end(audit_buf);
2275 EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound_simple);
2277 void xfrm_audit_state_notfound(struct sk_buff *skb, u16 family,
2278 __be32 net_spi, __be32 net_seq)
2280 struct audit_buffer *audit_buf;
2283 audit_buf = xfrm_audit_start("SA-notfound");
2284 if (audit_buf == NULL)
2286 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2287 spi = ntohl(net_spi);
2288 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2289 spi, spi, ntohl(net_seq));
2290 audit_log_end(audit_buf);
2292 EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound);
2294 void xfrm_audit_state_icvfail(struct xfrm_state *x,
2295 struct sk_buff *skb, u8 proto)
2297 struct audit_buffer *audit_buf;
2301 audit_buf = xfrm_audit_start("SA-icv-failure");
2302 if (audit_buf == NULL)
2304 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2305 if (xfrm_parse_spi(skb, proto, &net_spi, &net_seq) == 0) {
2306 u32 spi = ntohl(net_spi);
2307 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2308 spi, spi, ntohl(net_seq));
2310 audit_log_end(audit_buf);
2312 EXPORT_SYMBOL_GPL(xfrm_audit_state_icvfail);
2313 #endif /* CONFIG_AUDITSYSCALL */