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>
24 #include <linux/ktime.h>
25 #include <linux/slab.h>
26 #include <linux/interrupt.h>
27 #include <linux/kernel.h>
29 #include "xfrm_hash.h"
31 /* Each xfrm_state may be linked to two tables:
33 1. Hash table by (spi,daddr,ah/esp) to find SA by SPI. (input,ctl)
34 2. Hash table by (daddr,family,reqid) to find what SAs exist for given
35 destination/tunnel endpoint. (output)
38 static unsigned int xfrm_state_hashmax __read_mostly = 1 * 1024 * 1024;
40 static inline unsigned int xfrm_dst_hash(struct net *net,
41 const xfrm_address_t *daddr,
42 const xfrm_address_t *saddr,
44 unsigned short family)
46 return __xfrm_dst_hash(daddr, saddr, reqid, family, net->xfrm.state_hmask);
49 static inline unsigned int xfrm_src_hash(struct net *net,
50 const xfrm_address_t *daddr,
51 const xfrm_address_t *saddr,
52 unsigned short family)
54 return __xfrm_src_hash(daddr, saddr, family, net->xfrm.state_hmask);
57 static inline unsigned int
58 xfrm_spi_hash(struct net *net, const xfrm_address_t *daddr,
59 __be32 spi, u8 proto, unsigned short family)
61 return __xfrm_spi_hash(daddr, spi, proto, family, net->xfrm.state_hmask);
64 static void xfrm_hash_transfer(struct hlist_head *list,
65 struct hlist_head *ndsttable,
66 struct hlist_head *nsrctable,
67 struct hlist_head *nspitable,
68 unsigned int nhashmask)
70 struct hlist_node *tmp;
73 hlist_for_each_entry_safe(x, tmp, list, bydst) {
76 h = __xfrm_dst_hash(&x->id.daddr, &x->props.saddr,
77 x->props.reqid, x->props.family,
79 hlist_add_head(&x->bydst, ndsttable+h);
81 h = __xfrm_src_hash(&x->id.daddr, &x->props.saddr,
84 hlist_add_head(&x->bysrc, nsrctable+h);
87 h = __xfrm_spi_hash(&x->id.daddr, x->id.spi,
88 x->id.proto, x->props.family,
90 hlist_add_head(&x->byspi, nspitable+h);
95 static unsigned long xfrm_hash_new_size(unsigned int state_hmask)
97 return ((state_hmask + 1) << 1) * sizeof(struct hlist_head);
100 static DEFINE_MUTEX(hash_resize_mutex);
102 static void xfrm_hash_resize(struct work_struct *work)
104 struct net *net = container_of(work, struct net, xfrm.state_hash_work);
105 struct hlist_head *ndst, *nsrc, *nspi, *odst, *osrc, *ospi;
106 unsigned long nsize, osize;
107 unsigned int nhashmask, ohashmask;
110 mutex_lock(&hash_resize_mutex);
112 nsize = xfrm_hash_new_size(net->xfrm.state_hmask);
113 ndst = xfrm_hash_alloc(nsize);
116 nsrc = xfrm_hash_alloc(nsize);
118 xfrm_hash_free(ndst, nsize);
121 nspi = xfrm_hash_alloc(nsize);
123 xfrm_hash_free(ndst, nsize);
124 xfrm_hash_free(nsrc, nsize);
128 spin_lock_bh(&net->xfrm.xfrm_state_lock);
130 nhashmask = (nsize / sizeof(struct hlist_head)) - 1U;
131 for (i = net->xfrm.state_hmask; i >= 0; i--)
132 xfrm_hash_transfer(net->xfrm.state_bydst+i, ndst, nsrc, nspi,
135 odst = net->xfrm.state_bydst;
136 osrc = net->xfrm.state_bysrc;
137 ospi = net->xfrm.state_byspi;
138 ohashmask = net->xfrm.state_hmask;
140 net->xfrm.state_bydst = ndst;
141 net->xfrm.state_bysrc = nsrc;
142 net->xfrm.state_byspi = nspi;
143 net->xfrm.state_hmask = nhashmask;
145 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
147 osize = (ohashmask + 1) * sizeof(struct hlist_head);
148 xfrm_hash_free(odst, osize);
149 xfrm_hash_free(osrc, osize);
150 xfrm_hash_free(ospi, osize);
153 mutex_unlock(&hash_resize_mutex);
156 static DEFINE_SPINLOCK(xfrm_state_afinfo_lock);
157 static struct xfrm_state_afinfo __rcu *xfrm_state_afinfo[NPROTO];
159 static DEFINE_SPINLOCK(xfrm_state_gc_lock);
161 int __xfrm_state_delete(struct xfrm_state *x);
163 int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol);
164 void km_state_expired(struct xfrm_state *x, int hard, u32 portid);
166 static DEFINE_SPINLOCK(xfrm_type_lock);
167 int xfrm_register_type(const struct xfrm_type *type, unsigned short family)
169 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
170 const struct xfrm_type **typemap;
173 if (unlikely(afinfo == NULL))
174 return -EAFNOSUPPORT;
175 typemap = afinfo->type_map;
176 spin_lock_bh(&xfrm_type_lock);
178 if (likely(typemap[type->proto] == NULL))
179 typemap[type->proto] = type;
182 spin_unlock_bh(&xfrm_type_lock);
183 xfrm_state_put_afinfo(afinfo);
186 EXPORT_SYMBOL(xfrm_register_type);
188 int xfrm_unregister_type(const struct xfrm_type *type, unsigned short family)
190 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
191 const struct xfrm_type **typemap;
194 if (unlikely(afinfo == NULL))
195 return -EAFNOSUPPORT;
196 typemap = afinfo->type_map;
197 spin_lock_bh(&xfrm_type_lock);
199 if (unlikely(typemap[type->proto] != type))
202 typemap[type->proto] = NULL;
203 spin_unlock_bh(&xfrm_type_lock);
204 xfrm_state_put_afinfo(afinfo);
207 EXPORT_SYMBOL(xfrm_unregister_type);
209 static const struct xfrm_type *xfrm_get_type(u8 proto, unsigned short family)
211 struct xfrm_state_afinfo *afinfo;
212 const struct xfrm_type **typemap;
213 const struct xfrm_type *type;
214 int modload_attempted = 0;
217 afinfo = xfrm_state_get_afinfo(family);
218 if (unlikely(afinfo == NULL))
220 typemap = afinfo->type_map;
222 type = typemap[proto];
223 if (unlikely(type && !try_module_get(type->owner)))
225 if (!type && !modload_attempted) {
226 xfrm_state_put_afinfo(afinfo);
227 request_module("xfrm-type-%d-%d", family, proto);
228 modload_attempted = 1;
232 xfrm_state_put_afinfo(afinfo);
236 static void xfrm_put_type(const struct xfrm_type *type)
238 module_put(type->owner);
241 static DEFINE_SPINLOCK(xfrm_mode_lock);
242 int xfrm_register_mode(struct xfrm_mode *mode, int family)
244 struct xfrm_state_afinfo *afinfo;
245 struct xfrm_mode **modemap;
248 if (unlikely(mode->encap >= XFRM_MODE_MAX))
251 afinfo = xfrm_state_get_afinfo(family);
252 if (unlikely(afinfo == NULL))
253 return -EAFNOSUPPORT;
256 modemap = afinfo->mode_map;
257 spin_lock_bh(&xfrm_mode_lock);
258 if (modemap[mode->encap])
262 if (!try_module_get(afinfo->owner))
265 mode->afinfo = afinfo;
266 modemap[mode->encap] = mode;
270 spin_unlock_bh(&xfrm_mode_lock);
271 xfrm_state_put_afinfo(afinfo);
274 EXPORT_SYMBOL(xfrm_register_mode);
276 int xfrm_unregister_mode(struct xfrm_mode *mode, int family)
278 struct xfrm_state_afinfo *afinfo;
279 struct xfrm_mode **modemap;
282 if (unlikely(mode->encap >= XFRM_MODE_MAX))
285 afinfo = xfrm_state_get_afinfo(family);
286 if (unlikely(afinfo == NULL))
287 return -EAFNOSUPPORT;
290 modemap = afinfo->mode_map;
291 spin_lock_bh(&xfrm_mode_lock);
292 if (likely(modemap[mode->encap] == mode)) {
293 modemap[mode->encap] = NULL;
294 module_put(mode->afinfo->owner);
298 spin_unlock_bh(&xfrm_mode_lock);
299 xfrm_state_put_afinfo(afinfo);
302 EXPORT_SYMBOL(xfrm_unregister_mode);
304 static struct xfrm_mode *xfrm_get_mode(unsigned int encap, int family)
306 struct xfrm_state_afinfo *afinfo;
307 struct xfrm_mode *mode;
308 int modload_attempted = 0;
310 if (unlikely(encap >= XFRM_MODE_MAX))
314 afinfo = xfrm_state_get_afinfo(family);
315 if (unlikely(afinfo == NULL))
318 mode = afinfo->mode_map[encap];
319 if (unlikely(mode && !try_module_get(mode->owner)))
321 if (!mode && !modload_attempted) {
322 xfrm_state_put_afinfo(afinfo);
323 request_module("xfrm-mode-%d-%d", family, encap);
324 modload_attempted = 1;
328 xfrm_state_put_afinfo(afinfo);
332 static void xfrm_put_mode(struct xfrm_mode *mode)
334 module_put(mode->owner);
337 static void xfrm_state_gc_destroy(struct xfrm_state *x)
339 tasklet_hrtimer_cancel(&x->mtimer);
340 del_timer_sync(&x->rtimer);
346 kfree(x->replay_esn);
347 kfree(x->preplay_esn);
349 xfrm_put_mode(x->inner_mode);
350 if (x->inner_mode_iaf)
351 xfrm_put_mode(x->inner_mode_iaf);
353 xfrm_put_mode(x->outer_mode);
355 x->type->destructor(x);
356 xfrm_put_type(x->type);
358 security_xfrm_state_free(x);
362 static void xfrm_state_gc_task(struct work_struct *work)
364 struct net *net = container_of(work, struct net, xfrm.state_gc_work);
365 struct xfrm_state *x;
366 struct hlist_node *tmp;
367 struct hlist_head gc_list;
369 spin_lock_bh(&xfrm_state_gc_lock);
370 hlist_move_list(&net->xfrm.state_gc_list, &gc_list);
371 spin_unlock_bh(&xfrm_state_gc_lock);
373 hlist_for_each_entry_safe(x, tmp, &gc_list, gclist)
374 xfrm_state_gc_destroy(x);
377 static inline unsigned long make_jiffies(long secs)
379 if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
380 return MAX_SCHEDULE_TIMEOUT-1;
385 static enum hrtimer_restart xfrm_timer_handler(struct hrtimer *me)
387 struct tasklet_hrtimer *thr = container_of(me, struct tasklet_hrtimer, timer);
388 struct xfrm_state *x = container_of(thr, struct xfrm_state, mtimer);
389 unsigned long now = get_seconds();
390 long next = LONG_MAX;
395 if (x->km.state == XFRM_STATE_DEAD)
397 if (x->km.state == XFRM_STATE_EXPIRED)
399 if (x->lft.hard_add_expires_seconds) {
400 long tmo = x->lft.hard_add_expires_seconds +
401 x->curlft.add_time - now;
403 if (x->xflags & XFRM_SOFT_EXPIRE) {
404 /* enter hard expire without soft expire first?!
405 * setting a new date could trigger this.
406 * workarbound: fix x->curflt.add_time by below:
408 x->curlft.add_time = now - x->saved_tmo - 1;
409 tmo = x->lft.hard_add_expires_seconds - x->saved_tmo;
416 if (x->lft.hard_use_expires_seconds) {
417 long tmo = x->lft.hard_use_expires_seconds +
418 (x->curlft.use_time ? : now) - now;
426 if (x->lft.soft_add_expires_seconds) {
427 long tmo = x->lft.soft_add_expires_seconds +
428 x->curlft.add_time - now;
431 x->xflags &= ~XFRM_SOFT_EXPIRE;
432 } else if (tmo < next) {
434 x->xflags |= XFRM_SOFT_EXPIRE;
438 if (x->lft.soft_use_expires_seconds) {
439 long tmo = x->lft.soft_use_expires_seconds +
440 (x->curlft.use_time ? : now) - now;
449 km_state_expired(x, 0, 0);
451 if (next != LONG_MAX) {
452 tasklet_hrtimer_start(&x->mtimer, ktime_set(next, 0), HRTIMER_MODE_REL);
458 if (x->km.state == XFRM_STATE_ACQ && x->id.spi == 0)
459 x->km.state = XFRM_STATE_EXPIRED;
461 err = __xfrm_state_delete(x);
463 km_state_expired(x, 1, 0);
465 xfrm_audit_state_delete(x, err ? 0 : 1,
466 audit_get_loginuid(current),
467 audit_get_sessionid(current), 0);
470 spin_unlock(&x->lock);
471 return HRTIMER_NORESTART;
474 static void xfrm_replay_timer_handler(unsigned long data);
476 struct xfrm_state *xfrm_state_alloc(struct net *net)
478 struct xfrm_state *x;
480 x = kzalloc(sizeof(struct xfrm_state), GFP_ATOMIC);
483 write_pnet(&x->xs_net, net);
484 atomic_set(&x->refcnt, 1);
485 atomic_set(&x->tunnel_users, 0);
486 INIT_LIST_HEAD(&x->km.all);
487 INIT_HLIST_NODE(&x->bydst);
488 INIT_HLIST_NODE(&x->bysrc);
489 INIT_HLIST_NODE(&x->byspi);
490 tasklet_hrtimer_init(&x->mtimer, xfrm_timer_handler,
491 CLOCK_BOOTTIME, HRTIMER_MODE_ABS);
492 setup_timer(&x->rtimer, xfrm_replay_timer_handler,
494 x->curlft.add_time = get_seconds();
495 x->lft.soft_byte_limit = XFRM_INF;
496 x->lft.soft_packet_limit = XFRM_INF;
497 x->lft.hard_byte_limit = XFRM_INF;
498 x->lft.hard_packet_limit = XFRM_INF;
499 x->replay_maxage = 0;
500 x->replay_maxdiff = 0;
501 x->inner_mode = NULL;
502 x->inner_mode_iaf = NULL;
503 spin_lock_init(&x->lock);
507 EXPORT_SYMBOL(xfrm_state_alloc);
509 void __xfrm_state_destroy(struct xfrm_state *x)
511 struct net *net = xs_net(x);
513 WARN_ON(x->km.state != XFRM_STATE_DEAD);
515 spin_lock_bh(&xfrm_state_gc_lock);
516 hlist_add_head(&x->gclist, &net->xfrm.state_gc_list);
517 spin_unlock_bh(&xfrm_state_gc_lock);
518 schedule_work(&net->xfrm.state_gc_work);
520 EXPORT_SYMBOL(__xfrm_state_destroy);
522 int __xfrm_state_delete(struct xfrm_state *x)
524 struct net *net = xs_net(x);
527 if (x->km.state != XFRM_STATE_DEAD) {
528 x->km.state = XFRM_STATE_DEAD;
529 spin_lock(&net->xfrm.xfrm_state_lock);
530 list_del(&x->km.all);
531 hlist_del(&x->bydst);
532 hlist_del(&x->bysrc);
534 hlist_del(&x->byspi);
535 net->xfrm.state_num--;
536 spin_unlock(&net->xfrm.xfrm_state_lock);
538 /* All xfrm_state objects are created by xfrm_state_alloc.
539 * The xfrm_state_alloc call gives a reference, and that
540 * is what we are dropping here.
548 EXPORT_SYMBOL(__xfrm_state_delete);
550 int xfrm_state_delete(struct xfrm_state *x)
554 spin_lock_bh(&x->lock);
555 err = __xfrm_state_delete(x);
556 spin_unlock_bh(&x->lock);
560 EXPORT_SYMBOL(xfrm_state_delete);
562 #ifdef CONFIG_SECURITY_NETWORK_XFRM
564 xfrm_state_flush_secctx_check(struct net *net, u8 proto, struct xfrm_audit *audit_info)
568 for (i = 0; i <= net->xfrm.state_hmask; i++) {
569 struct xfrm_state *x;
571 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
572 if (xfrm_id_proto_match(x->id.proto, proto) &&
573 (err = security_xfrm_state_delete(x)) != 0) {
574 xfrm_audit_state_delete(x, 0,
575 audit_info->loginuid,
576 audit_info->sessionid,
587 xfrm_state_flush_secctx_check(struct net *net, u8 proto, struct xfrm_audit *audit_info)
593 int xfrm_state_flush(struct net *net, u8 proto, struct xfrm_audit *audit_info)
595 int i, err = 0, cnt = 0;
597 spin_lock_bh(&net->xfrm.xfrm_state_lock);
598 err = xfrm_state_flush_secctx_check(net, proto, audit_info);
603 for (i = 0; i <= net->xfrm.state_hmask; i++) {
604 struct xfrm_state *x;
606 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
607 if (!xfrm_state_kern(x) &&
608 xfrm_id_proto_match(x->id.proto, proto)) {
610 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
612 err = xfrm_state_delete(x);
613 xfrm_audit_state_delete(x, err ? 0 : 1,
614 audit_info->loginuid,
615 audit_info->sessionid,
621 spin_lock_bh(&net->xfrm.xfrm_state_lock);
630 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
633 EXPORT_SYMBOL(xfrm_state_flush);
635 void xfrm_sad_getinfo(struct net *net, struct xfrmk_sadinfo *si)
637 spin_lock_bh(&net->xfrm.xfrm_state_lock);
638 si->sadcnt = net->xfrm.state_num;
639 si->sadhcnt = net->xfrm.state_hmask;
640 si->sadhmcnt = xfrm_state_hashmax;
641 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
643 EXPORT_SYMBOL(xfrm_sad_getinfo);
646 xfrm_init_tempstate(struct xfrm_state *x, const struct flowi *fl,
647 const struct xfrm_tmpl *tmpl,
648 const xfrm_address_t *daddr, const xfrm_address_t *saddr,
649 unsigned short family)
651 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
654 afinfo->init_tempsel(&x->sel, fl);
656 if (family != tmpl->encap_family) {
657 xfrm_state_put_afinfo(afinfo);
658 afinfo = xfrm_state_get_afinfo(tmpl->encap_family);
662 afinfo->init_temprop(x, tmpl, daddr, saddr);
663 xfrm_state_put_afinfo(afinfo);
667 static struct xfrm_state *__xfrm_state_lookup(struct net *net, u32 mark,
668 const xfrm_address_t *daddr,
669 __be32 spi, u8 proto,
670 unsigned short family)
672 unsigned int h = xfrm_spi_hash(net, daddr, spi, proto, family);
673 struct xfrm_state *x;
675 hlist_for_each_entry(x, net->xfrm.state_byspi+h, byspi) {
676 if (x->props.family != family ||
678 x->id.proto != proto ||
679 !xfrm_addr_equal(&x->id.daddr, daddr, family))
682 if ((mark & x->mark.m) != x->mark.v)
691 static struct xfrm_state *__xfrm_state_lookup_byaddr(struct net *net, u32 mark,
692 const xfrm_address_t *daddr,
693 const xfrm_address_t *saddr,
694 u8 proto, unsigned short family)
696 unsigned int h = xfrm_src_hash(net, daddr, saddr, family);
697 struct xfrm_state *x;
699 hlist_for_each_entry(x, net->xfrm.state_bysrc+h, bysrc) {
700 if (x->props.family != family ||
701 x->id.proto != proto ||
702 !xfrm_addr_equal(&x->id.daddr, daddr, family) ||
703 !xfrm_addr_equal(&x->props.saddr, saddr, family))
706 if ((mark & x->mark.m) != x->mark.v)
715 static inline struct xfrm_state *
716 __xfrm_state_locate(struct xfrm_state *x, int use_spi, int family)
718 struct net *net = xs_net(x);
719 u32 mark = x->mark.v & x->mark.m;
722 return __xfrm_state_lookup(net, mark, &x->id.daddr,
723 x->id.spi, x->id.proto, family);
725 return __xfrm_state_lookup_byaddr(net, mark,
728 x->id.proto, family);
731 static void xfrm_hash_grow_check(struct net *net, int have_hash_collision)
733 if (have_hash_collision &&
734 (net->xfrm.state_hmask + 1) < xfrm_state_hashmax &&
735 net->xfrm.state_num > net->xfrm.state_hmask)
736 schedule_work(&net->xfrm.state_hash_work);
739 static void xfrm_state_look_at(struct xfrm_policy *pol, struct xfrm_state *x,
740 const struct flowi *fl, unsigned short family,
741 struct xfrm_state **best, int *acq_in_progress,
745 * 1. There is a valid state with matching selector. Done.
746 * 2. Valid state with inappropriate selector. Skip.
748 * Entering area of "sysdeps".
750 * 3. If state is not valid, selector is temporary, it selects
751 * only session which triggered previous resolution. Key
752 * manager will do something to install a state with proper
755 if (x->km.state == XFRM_STATE_VALID) {
756 if ((x->sel.family &&
757 !xfrm_selector_match(&x->sel, fl, x->sel.family)) ||
758 !security_xfrm_state_pol_flow_match(x, pol, fl))
762 (*best)->km.dying > x->km.dying ||
763 ((*best)->km.dying == x->km.dying &&
764 (*best)->curlft.add_time < x->curlft.add_time))
766 } else if (x->km.state == XFRM_STATE_ACQ) {
767 *acq_in_progress = 1;
768 } else if (x->km.state == XFRM_STATE_ERROR ||
769 x->km.state == XFRM_STATE_EXPIRED) {
770 if (xfrm_selector_match(&x->sel, fl, x->sel.family) &&
771 security_xfrm_state_pol_flow_match(x, pol, fl))
777 xfrm_state_find(const xfrm_address_t *daddr, const xfrm_address_t *saddr,
778 const struct flowi *fl, struct xfrm_tmpl *tmpl,
779 struct xfrm_policy *pol, int *err,
780 unsigned short family)
782 static xfrm_address_t saddr_wildcard = { };
783 struct net *net = xp_net(pol);
784 unsigned int h, h_wildcard;
785 struct xfrm_state *x, *x0, *to_put;
786 int acquire_in_progress = 0;
788 struct xfrm_state *best = NULL;
789 u32 mark = pol->mark.v & pol->mark.m;
790 unsigned short encap_family = tmpl->encap_family;
794 spin_lock_bh(&net->xfrm.xfrm_state_lock);
795 h = xfrm_dst_hash(net, daddr, saddr, tmpl->reqid, encap_family);
796 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
797 if (x->props.family == encap_family &&
798 x->props.reqid == tmpl->reqid &&
799 (mark & x->mark.m) == x->mark.v &&
800 !(x->props.flags & XFRM_STATE_WILDRECV) &&
801 xfrm_state_addr_check(x, daddr, saddr, encap_family) &&
802 tmpl->mode == x->props.mode &&
803 tmpl->id.proto == x->id.proto &&
804 (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
805 xfrm_state_look_at(pol, x, fl, encap_family,
806 &best, &acquire_in_progress, &error);
808 if (best || acquire_in_progress)
811 h_wildcard = xfrm_dst_hash(net, daddr, &saddr_wildcard, tmpl->reqid, encap_family);
812 hlist_for_each_entry(x, net->xfrm.state_bydst+h_wildcard, bydst) {
813 if (x->props.family == encap_family &&
814 x->props.reqid == tmpl->reqid &&
815 (mark & x->mark.m) == x->mark.v &&
816 !(x->props.flags & XFRM_STATE_WILDRECV) &&
817 xfrm_addr_equal(&x->id.daddr, daddr, encap_family) &&
818 tmpl->mode == x->props.mode &&
819 tmpl->id.proto == x->id.proto &&
820 (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
821 xfrm_state_look_at(pol, x, fl, encap_family,
822 &best, &acquire_in_progress, &error);
827 if (!x && !error && !acquire_in_progress) {
829 (x0 = __xfrm_state_lookup(net, mark, daddr, tmpl->id.spi,
830 tmpl->id.proto, encap_family)) != NULL) {
835 x = xfrm_state_alloc(net);
840 /* Initialize temporary state matching only
841 * to current session. */
842 xfrm_init_tempstate(x, fl, tmpl, daddr, saddr, family);
843 memcpy(&x->mark, &pol->mark, sizeof(x->mark));
845 error = security_xfrm_state_alloc_acquire(x, pol->security, fl->flowi_secid);
847 x->km.state = XFRM_STATE_DEAD;
853 if (km_query(x, tmpl, pol) == 0) {
854 x->km.state = XFRM_STATE_ACQ;
855 list_add(&x->km.all, &net->xfrm.state_all);
856 hlist_add_head(&x->bydst, net->xfrm.state_bydst+h);
857 h = xfrm_src_hash(net, daddr, saddr, encap_family);
858 hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h);
860 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, encap_family);
861 hlist_add_head(&x->byspi, net->xfrm.state_byspi+h);
863 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
864 tasklet_hrtimer_start(&x->mtimer, ktime_set(net->xfrm.sysctl_acq_expires, 0), HRTIMER_MODE_REL);
865 net->xfrm.state_num++;
866 xfrm_hash_grow_check(net, x->bydst.next != NULL);
868 x->km.state = XFRM_STATE_DEAD;
878 *err = acquire_in_progress ? -EAGAIN : error;
879 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
881 xfrm_state_put(to_put);
886 xfrm_stateonly_find(struct net *net, u32 mark,
887 xfrm_address_t *daddr, xfrm_address_t *saddr,
888 unsigned short family, u8 mode, u8 proto, u32 reqid)
891 struct xfrm_state *rx = NULL, *x = NULL;
893 spin_lock_bh(&net->xfrm.xfrm_state_lock);
894 h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
895 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
896 if (x->props.family == family &&
897 x->props.reqid == reqid &&
898 (mark & x->mark.m) == x->mark.v &&
899 !(x->props.flags & XFRM_STATE_WILDRECV) &&
900 xfrm_state_addr_check(x, daddr, saddr, family) &&
901 mode == x->props.mode &&
902 proto == x->id.proto &&
903 x->km.state == XFRM_STATE_VALID) {
911 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
916 EXPORT_SYMBOL(xfrm_stateonly_find);
918 struct xfrm_state *xfrm_state_lookup_byspi(struct net *net, __be32 spi,
919 unsigned short family)
921 struct xfrm_state *x;
922 struct xfrm_state_walk *w;
924 spin_lock_bh(&net->xfrm.xfrm_state_lock);
925 list_for_each_entry(w, &net->xfrm.state_all, all) {
926 x = container_of(w, struct xfrm_state, km);
927 if (x->props.family != family ||
931 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
935 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
938 EXPORT_SYMBOL(xfrm_state_lookup_byspi);
940 static void __xfrm_state_insert(struct xfrm_state *x)
942 struct net *net = xs_net(x);
945 list_add(&x->km.all, &net->xfrm.state_all);
947 h = xfrm_dst_hash(net, &x->id.daddr, &x->props.saddr,
948 x->props.reqid, x->props.family);
949 hlist_add_head(&x->bydst, net->xfrm.state_bydst+h);
951 h = xfrm_src_hash(net, &x->id.daddr, &x->props.saddr, x->props.family);
952 hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h);
955 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto,
958 hlist_add_head(&x->byspi, net->xfrm.state_byspi+h);
961 tasklet_hrtimer_start(&x->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL);
962 if (x->replay_maxage)
963 mod_timer(&x->rtimer, jiffies + x->replay_maxage);
965 net->xfrm.state_num++;
967 xfrm_hash_grow_check(net, x->bydst.next != NULL);
970 /* net->xfrm.xfrm_state_lock is held */
971 static void __xfrm_state_bump_genids(struct xfrm_state *xnew)
973 struct net *net = xs_net(xnew);
974 unsigned short family = xnew->props.family;
975 u32 reqid = xnew->props.reqid;
976 struct xfrm_state *x;
978 u32 mark = xnew->mark.v & xnew->mark.m;
980 h = xfrm_dst_hash(net, &xnew->id.daddr, &xnew->props.saddr, reqid, family);
981 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
982 if (x->props.family == family &&
983 x->props.reqid == reqid &&
984 (mark & x->mark.m) == x->mark.v &&
985 xfrm_addr_equal(&x->id.daddr, &xnew->id.daddr, family) &&
986 xfrm_addr_equal(&x->props.saddr, &xnew->props.saddr, family))
991 void xfrm_state_insert(struct xfrm_state *x)
993 struct net *net = xs_net(x);
995 spin_lock_bh(&net->xfrm.xfrm_state_lock);
996 __xfrm_state_bump_genids(x);
997 __xfrm_state_insert(x);
998 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1000 EXPORT_SYMBOL(xfrm_state_insert);
1002 /* net->xfrm.xfrm_state_lock is held */
1003 static struct xfrm_state *__find_acq_core(struct net *net,
1004 const struct xfrm_mark *m,
1005 unsigned short family, u8 mode,
1006 u32 reqid, u8 proto,
1007 const xfrm_address_t *daddr,
1008 const xfrm_address_t *saddr,
1011 unsigned int h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
1012 struct xfrm_state *x;
1013 u32 mark = m->v & m->m;
1015 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
1016 if (x->props.reqid != reqid ||
1017 x->props.mode != mode ||
1018 x->props.family != family ||
1019 x->km.state != XFRM_STATE_ACQ ||
1021 x->id.proto != proto ||
1022 (mark & x->mark.m) != x->mark.v ||
1023 !xfrm_addr_equal(&x->id.daddr, daddr, family) ||
1024 !xfrm_addr_equal(&x->props.saddr, saddr, family))
1034 x = xfrm_state_alloc(net);
1038 x->sel.daddr.a4 = daddr->a4;
1039 x->sel.saddr.a4 = saddr->a4;
1040 x->sel.prefixlen_d = 32;
1041 x->sel.prefixlen_s = 32;
1042 x->props.saddr.a4 = saddr->a4;
1043 x->id.daddr.a4 = daddr->a4;
1047 *(struct in6_addr *)x->sel.daddr.a6 = *(struct in6_addr *)daddr;
1048 *(struct in6_addr *)x->sel.saddr.a6 = *(struct in6_addr *)saddr;
1049 x->sel.prefixlen_d = 128;
1050 x->sel.prefixlen_s = 128;
1051 *(struct in6_addr *)x->props.saddr.a6 = *(struct in6_addr *)saddr;
1052 *(struct in6_addr *)x->id.daddr.a6 = *(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;
1063 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
1065 tasklet_hrtimer_start(&x->mtimer, ktime_set(net->xfrm.sysctl_acq_expires, 0), HRTIMER_MODE_REL);
1066 list_add(&x->km.all, &net->xfrm.state_all);
1067 hlist_add_head(&x->bydst, net->xfrm.state_bydst+h);
1068 h = xfrm_src_hash(net, daddr, saddr, family);
1069 hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h);
1071 net->xfrm.state_num++;
1073 xfrm_hash_grow_check(net, x->bydst.next != NULL);
1079 static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq);
1081 int xfrm_state_add(struct xfrm_state *x)
1083 struct net *net = xs_net(x);
1084 struct xfrm_state *x1, *to_put;
1087 u32 mark = x->mark.v & x->mark.m;
1088 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
1090 family = x->props.family;
1094 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1096 x1 = __xfrm_state_locate(x, use_spi, family);
1104 if (use_spi && x->km.seq) {
1105 x1 = __xfrm_find_acq_byseq(net, mark, x->km.seq);
1106 if (x1 && ((x1->id.proto != x->id.proto) ||
1107 !xfrm_addr_equal(&x1->id.daddr, &x->id.daddr, family))) {
1114 x1 = __find_acq_core(net, &x->mark, family, x->props.mode,
1115 x->props.reqid, x->id.proto,
1116 &x->id.daddr, &x->props.saddr, 0);
1118 __xfrm_state_bump_genids(x);
1119 __xfrm_state_insert(x);
1123 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1126 xfrm_state_delete(x1);
1131 xfrm_state_put(to_put);
1135 EXPORT_SYMBOL(xfrm_state_add);
1137 #ifdef CONFIG_XFRM_MIGRATE
1138 static struct xfrm_state *xfrm_state_clone(struct xfrm_state *orig, int *errp)
1140 struct net *net = xs_net(orig);
1142 struct xfrm_state *x = xfrm_state_alloc(net);
1146 memcpy(&x->id, &orig->id, sizeof(x->id));
1147 memcpy(&x->sel, &orig->sel, sizeof(x->sel));
1148 memcpy(&x->lft, &orig->lft, sizeof(x->lft));
1149 x->props.mode = orig->props.mode;
1150 x->props.replay_window = orig->props.replay_window;
1151 x->props.reqid = orig->props.reqid;
1152 x->props.family = orig->props.family;
1153 x->props.saddr = orig->props.saddr;
1156 x->aalg = xfrm_algo_auth_clone(orig->aalg);
1160 x->props.aalgo = orig->props.aalgo;
1163 x->ealg = xfrm_algo_clone(orig->ealg);
1167 x->props.ealgo = orig->props.ealgo;
1170 x->calg = xfrm_algo_clone(orig->calg);
1174 x->props.calgo = orig->props.calgo;
1177 x->encap = kmemdup(orig->encap, sizeof(*x->encap), GFP_KERNEL);
1183 x->coaddr = kmemdup(orig->coaddr, sizeof(*x->coaddr),
1189 if (orig->replay_esn) {
1190 err = xfrm_replay_clone(x, orig);
1195 memcpy(&x->mark, &orig->mark, sizeof(x->mark));
1197 err = xfrm_init_state(x);
1201 x->props.flags = orig->props.flags;
1202 x->props.extra_flags = orig->props.extra_flags;
1204 x->curlft.add_time = orig->curlft.add_time;
1205 x->km.state = orig->km.state;
1206 x->km.seq = orig->km.seq;
1218 struct xfrm_state *xfrm_migrate_state_find(struct xfrm_migrate *m, struct net *net)
1221 struct xfrm_state *x = NULL;
1223 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1226 h = xfrm_dst_hash(net, &m->old_daddr, &m->old_saddr,
1227 m->reqid, m->old_family);
1228 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
1229 if (x->props.mode != m->mode ||
1230 x->id.proto != m->proto)
1232 if (m->reqid && x->props.reqid != m->reqid)
1234 if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr,
1236 !xfrm_addr_equal(&x->props.saddr, &m->old_saddr,
1243 h = xfrm_src_hash(net, &m->old_daddr, &m->old_saddr,
1245 hlist_for_each_entry(x, net->xfrm.state_bysrc+h, bysrc) {
1246 if (x->props.mode != m->mode ||
1247 x->id.proto != m->proto)
1249 if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr,
1251 !xfrm_addr_equal(&x->props.saddr, &m->old_saddr,
1259 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1263 EXPORT_SYMBOL(xfrm_migrate_state_find);
1265 struct xfrm_state *xfrm_state_migrate(struct xfrm_state *x,
1266 struct xfrm_migrate *m)
1268 struct xfrm_state *xc;
1271 xc = xfrm_state_clone(x, &err);
1275 memcpy(&xc->id.daddr, &m->new_daddr, sizeof(xc->id.daddr));
1276 memcpy(&xc->props.saddr, &m->new_saddr, sizeof(xc->props.saddr));
1279 if (xfrm_addr_equal(&x->id.daddr, &m->new_daddr, m->new_family)) {
1280 /* a care is needed when the destination address of the
1281 state is to be updated as it is a part of triplet */
1282 xfrm_state_insert(xc);
1284 if ((err = xfrm_state_add(xc)) < 0)
1293 EXPORT_SYMBOL(xfrm_state_migrate);
1296 int xfrm_state_update(struct xfrm_state *x)
1298 struct xfrm_state *x1, *to_put;
1300 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
1301 struct net *net = xs_net(x);
1305 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1306 x1 = __xfrm_state_locate(x, use_spi, x->props.family);
1312 if (xfrm_state_kern(x1)) {
1318 if (x1->km.state == XFRM_STATE_ACQ) {
1319 __xfrm_state_insert(x);
1325 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1328 xfrm_state_put(to_put);
1334 xfrm_state_delete(x1);
1340 spin_lock_bh(&x1->lock);
1341 if (likely(x1->km.state == XFRM_STATE_VALID)) {
1342 if (x->encap && x1->encap)
1343 memcpy(x1->encap, x->encap, sizeof(*x1->encap));
1344 if (x->coaddr && x1->coaddr) {
1345 memcpy(x1->coaddr, x->coaddr, sizeof(*x1->coaddr));
1347 if (!use_spi && memcmp(&x1->sel, &x->sel, sizeof(x1->sel)))
1348 memcpy(&x1->sel, &x->sel, sizeof(x1->sel));
1349 memcpy(&x1->lft, &x->lft, sizeof(x1->lft));
1352 tasklet_hrtimer_start(&x1->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL);
1353 if (x1->curlft.use_time)
1354 xfrm_state_check_expire(x1);
1357 x->km.state = XFRM_STATE_DEAD;
1358 __xfrm_state_put(x);
1360 spin_unlock_bh(&x1->lock);
1366 EXPORT_SYMBOL(xfrm_state_update);
1368 int xfrm_state_check_expire(struct xfrm_state *x)
1370 if (!x->curlft.use_time)
1371 x->curlft.use_time = get_seconds();
1373 if (x->curlft.bytes >= x->lft.hard_byte_limit ||
1374 x->curlft.packets >= x->lft.hard_packet_limit) {
1375 x->km.state = XFRM_STATE_EXPIRED;
1376 tasklet_hrtimer_start(&x->mtimer, ktime_set(0, 0), HRTIMER_MODE_REL);
1381 (x->curlft.bytes >= x->lft.soft_byte_limit ||
1382 x->curlft.packets >= x->lft.soft_packet_limit)) {
1384 km_state_expired(x, 0, 0);
1388 EXPORT_SYMBOL(xfrm_state_check_expire);
1391 xfrm_state_lookup(struct net *net, u32 mark, const xfrm_address_t *daddr, __be32 spi,
1392 u8 proto, unsigned short family)
1394 struct xfrm_state *x;
1396 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1397 x = __xfrm_state_lookup(net, mark, daddr, spi, proto, family);
1398 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1401 EXPORT_SYMBOL(xfrm_state_lookup);
1404 xfrm_state_lookup_byaddr(struct net *net, u32 mark,
1405 const xfrm_address_t *daddr, const xfrm_address_t *saddr,
1406 u8 proto, unsigned short family)
1408 struct xfrm_state *x;
1410 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1411 x = __xfrm_state_lookup_byaddr(net, mark, daddr, saddr, proto, family);
1412 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1415 EXPORT_SYMBOL(xfrm_state_lookup_byaddr);
1418 xfrm_find_acq(struct net *net, const struct xfrm_mark *mark, u8 mode, u32 reqid,
1419 u8 proto, const xfrm_address_t *daddr,
1420 const xfrm_address_t *saddr, int create, unsigned short family)
1422 struct xfrm_state *x;
1424 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1425 x = __find_acq_core(net, mark, family, mode, reqid, proto, daddr, saddr, create);
1426 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1430 EXPORT_SYMBOL(xfrm_find_acq);
1432 #ifdef CONFIG_XFRM_SUB_POLICY
1434 xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n,
1435 unsigned short family, struct net *net)
1438 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
1440 return -EAFNOSUPPORT;
1442 spin_lock_bh(&net->xfrm.xfrm_state_lock); /*FIXME*/
1443 if (afinfo->tmpl_sort)
1444 err = afinfo->tmpl_sort(dst, src, n);
1445 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1446 xfrm_state_put_afinfo(afinfo);
1449 EXPORT_SYMBOL(xfrm_tmpl_sort);
1452 xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src, int n,
1453 unsigned short family)
1456 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
1457 struct net *net = xs_net(*src);
1460 return -EAFNOSUPPORT;
1462 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1463 if (afinfo->state_sort)
1464 err = afinfo->state_sort(dst, src, n);
1465 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1466 xfrm_state_put_afinfo(afinfo);
1469 EXPORT_SYMBOL(xfrm_state_sort);
1472 /* Silly enough, but I'm lazy to build resolution list */
1474 static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
1478 for (i = 0; i <= net->xfrm.state_hmask; i++) {
1479 struct xfrm_state *x;
1481 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
1482 if (x->km.seq == seq &&
1483 (mark & x->mark.m) == x->mark.v &&
1484 x->km.state == XFRM_STATE_ACQ) {
1493 struct xfrm_state *xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
1495 struct xfrm_state *x;
1497 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1498 x = __xfrm_find_acq_byseq(net, mark, seq);
1499 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1502 EXPORT_SYMBOL(xfrm_find_acq_byseq);
1504 u32 xfrm_get_acqseq(void)
1507 static atomic_t acqseq;
1510 res = atomic_inc_return(&acqseq);
1515 EXPORT_SYMBOL(xfrm_get_acqseq);
1517 int verify_spi_info(u8 proto, u32 min, u32 max)
1525 /* IPCOMP spi is 16-bits. */
1539 EXPORT_SYMBOL(verify_spi_info);
1541 int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high)
1543 struct net *net = xs_net(x);
1545 struct xfrm_state *x0;
1547 __be32 minspi = htonl(low);
1548 __be32 maxspi = htonl(high);
1549 u32 mark = x->mark.v & x->mark.m;
1551 spin_lock_bh(&x->lock);
1552 if (x->km.state == XFRM_STATE_DEAD)
1561 if (minspi == maxspi) {
1562 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, minspi, x->id.proto, x->props.family);
1570 for (h = 0; h < high-low+1; h++) {
1571 spi = low + prandom_u32()%(high-low+1);
1572 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, htonl(spi), x->id.proto, x->props.family);
1574 x->id.spi = htonl(spi);
1581 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1582 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, x->props.family);
1583 hlist_add_head(&x->byspi, net->xfrm.state_byspi+h);
1584 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1590 spin_unlock_bh(&x->lock);
1594 EXPORT_SYMBOL(xfrm_alloc_spi);
1596 int xfrm_state_walk(struct net *net, struct xfrm_state_walk *walk,
1597 int (*func)(struct xfrm_state *, int, void*),
1600 struct xfrm_state *state;
1601 struct xfrm_state_walk *x;
1604 if (walk->seq != 0 && list_empty(&walk->all))
1607 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1608 if (list_empty(&walk->all))
1609 x = list_first_entry(&net->xfrm.state_all, struct xfrm_state_walk, all);
1611 x = list_entry(&walk->all, struct xfrm_state_walk, all);
1612 list_for_each_entry_from(x, &net->xfrm.state_all, all) {
1613 if (x->state == XFRM_STATE_DEAD)
1615 state = container_of(x, struct xfrm_state, km);
1616 if (!xfrm_id_proto_match(state->id.proto, walk->proto))
1618 err = func(state, walk->seq, data);
1620 list_move_tail(&walk->all, &x->all);
1625 if (walk->seq == 0) {
1629 list_del_init(&walk->all);
1631 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1634 EXPORT_SYMBOL(xfrm_state_walk);
1636 void xfrm_state_walk_init(struct xfrm_state_walk *walk, u8 proto)
1638 INIT_LIST_HEAD(&walk->all);
1639 walk->proto = proto;
1640 walk->state = XFRM_STATE_DEAD;
1643 EXPORT_SYMBOL(xfrm_state_walk_init);
1645 void xfrm_state_walk_done(struct xfrm_state_walk *walk, struct net *net)
1647 if (list_empty(&walk->all))
1650 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1651 list_del(&walk->all);
1652 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1654 EXPORT_SYMBOL(xfrm_state_walk_done);
1656 static void xfrm_replay_timer_handler(unsigned long data)
1658 struct xfrm_state *x = (struct xfrm_state *)data;
1660 spin_lock(&x->lock);
1662 if (x->km.state == XFRM_STATE_VALID) {
1663 if (xfrm_aevent_is_on(xs_net(x)))
1664 x->repl->notify(x, XFRM_REPLAY_TIMEOUT);
1666 x->xflags |= XFRM_TIME_DEFER;
1669 spin_unlock(&x->lock);
1672 static LIST_HEAD(xfrm_km_list);
1674 void km_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
1676 struct xfrm_mgr *km;
1679 list_for_each_entry_rcu(km, &xfrm_km_list, list)
1680 if (km->notify_policy)
1681 km->notify_policy(xp, dir, c);
1685 void km_state_notify(struct xfrm_state *x, const struct km_event *c)
1687 struct xfrm_mgr *km;
1689 list_for_each_entry_rcu(km, &xfrm_km_list, list)
1695 EXPORT_SYMBOL(km_policy_notify);
1696 EXPORT_SYMBOL(km_state_notify);
1698 void km_state_expired(struct xfrm_state *x, int hard, u32 portid)
1704 c.event = XFRM_MSG_EXPIRE;
1705 km_state_notify(x, &c);
1708 EXPORT_SYMBOL(km_state_expired);
1710 * We send to all registered managers regardless of failure
1711 * We are happy with one success
1713 int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol)
1715 int err = -EINVAL, acqret;
1716 struct xfrm_mgr *km;
1719 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
1720 acqret = km->acquire(x, t, pol);
1727 EXPORT_SYMBOL(km_query);
1729 int km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
1732 struct xfrm_mgr *km;
1735 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
1736 if (km->new_mapping)
1737 err = km->new_mapping(x, ipaddr, sport);
1744 EXPORT_SYMBOL(km_new_mapping);
1746 void km_policy_expired(struct xfrm_policy *pol, int dir, int hard, u32 portid)
1752 c.event = XFRM_MSG_POLEXPIRE;
1753 km_policy_notify(pol, dir, &c);
1755 EXPORT_SYMBOL(km_policy_expired);
1757 #ifdef CONFIG_XFRM_MIGRATE
1758 int km_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
1759 const struct xfrm_migrate *m, int num_migrate,
1760 const struct xfrm_kmaddress *k)
1764 struct xfrm_mgr *km;
1767 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
1769 ret = km->migrate(sel, dir, type, m, num_migrate, k);
1777 EXPORT_SYMBOL(km_migrate);
1780 int km_report(struct net *net, u8 proto, struct xfrm_selector *sel, xfrm_address_t *addr)
1784 struct xfrm_mgr *km;
1787 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
1789 ret = km->report(net, proto, sel, addr);
1797 EXPORT_SYMBOL(km_report);
1799 int xfrm_user_policy(struct sock *sk, int optname, u8 __user *optval, int optlen)
1803 struct xfrm_mgr *km;
1804 struct xfrm_policy *pol = NULL;
1806 if (optlen <= 0 || optlen > PAGE_SIZE)
1809 data = kmalloc(optlen, GFP_KERNEL);
1814 if (copy_from_user(data, optval, optlen))
1819 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
1820 pol = km->compile_policy(sk, optname, data,
1828 xfrm_sk_policy_insert(sk, err, pol);
1837 EXPORT_SYMBOL(xfrm_user_policy);
1839 static DEFINE_SPINLOCK(xfrm_km_lock);
1841 int xfrm_register_km(struct xfrm_mgr *km)
1843 spin_lock_bh(&xfrm_km_lock);
1844 list_add_tail_rcu(&km->list, &xfrm_km_list);
1845 spin_unlock_bh(&xfrm_km_lock);
1848 EXPORT_SYMBOL(xfrm_register_km);
1850 int xfrm_unregister_km(struct xfrm_mgr *km)
1852 spin_lock_bh(&xfrm_km_lock);
1853 list_del_rcu(&km->list);
1854 spin_unlock_bh(&xfrm_km_lock);
1858 EXPORT_SYMBOL(xfrm_unregister_km);
1860 int xfrm_state_register_afinfo(struct xfrm_state_afinfo *afinfo)
1863 if (unlikely(afinfo == NULL))
1865 if (unlikely(afinfo->family >= NPROTO))
1866 return -EAFNOSUPPORT;
1867 spin_lock_bh(&xfrm_state_afinfo_lock);
1868 if (unlikely(xfrm_state_afinfo[afinfo->family] != NULL))
1871 rcu_assign_pointer(xfrm_state_afinfo[afinfo->family], afinfo);
1872 spin_unlock_bh(&xfrm_state_afinfo_lock);
1875 EXPORT_SYMBOL(xfrm_state_register_afinfo);
1877 int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo)
1880 if (unlikely(afinfo == NULL))
1882 if (unlikely(afinfo->family >= NPROTO))
1883 return -EAFNOSUPPORT;
1884 spin_lock_bh(&xfrm_state_afinfo_lock);
1885 if (likely(xfrm_state_afinfo[afinfo->family] != NULL)) {
1886 if (unlikely(xfrm_state_afinfo[afinfo->family] != afinfo))
1889 RCU_INIT_POINTER(xfrm_state_afinfo[afinfo->family], NULL);
1891 spin_unlock_bh(&xfrm_state_afinfo_lock);
1895 EXPORT_SYMBOL(xfrm_state_unregister_afinfo);
1897 struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family)
1899 struct xfrm_state_afinfo *afinfo;
1900 if (unlikely(family >= NPROTO))
1903 afinfo = rcu_dereference(xfrm_state_afinfo[family]);
1904 if (unlikely(!afinfo))
1909 void xfrm_state_put_afinfo(struct xfrm_state_afinfo *afinfo)
1914 /* Temporarily located here until net/xfrm/xfrm_tunnel.c is created */
1915 void xfrm_state_delete_tunnel(struct xfrm_state *x)
1918 struct xfrm_state *t = x->tunnel;
1920 if (atomic_read(&t->tunnel_users) == 2)
1921 xfrm_state_delete(t);
1922 atomic_dec(&t->tunnel_users);
1927 EXPORT_SYMBOL(xfrm_state_delete_tunnel);
1929 int xfrm_state_mtu(struct xfrm_state *x, int mtu)
1933 spin_lock_bh(&x->lock);
1934 if (x->km.state == XFRM_STATE_VALID &&
1935 x->type && x->type->get_mtu)
1936 res = x->type->get_mtu(x, mtu);
1938 res = mtu - x->props.header_len;
1939 spin_unlock_bh(&x->lock);
1943 int __xfrm_init_state(struct xfrm_state *x, bool init_replay)
1945 struct xfrm_state_afinfo *afinfo;
1946 struct xfrm_mode *inner_mode;
1947 int family = x->props.family;
1950 err = -EAFNOSUPPORT;
1951 afinfo = xfrm_state_get_afinfo(family);
1956 if (afinfo->init_flags)
1957 err = afinfo->init_flags(x);
1959 xfrm_state_put_afinfo(afinfo);
1964 err = -EPROTONOSUPPORT;
1966 if (x->sel.family != AF_UNSPEC) {
1967 inner_mode = xfrm_get_mode(x->props.mode, x->sel.family);
1968 if (inner_mode == NULL)
1971 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL) &&
1972 family != x->sel.family) {
1973 xfrm_put_mode(inner_mode);
1977 x->inner_mode = inner_mode;
1979 struct xfrm_mode *inner_mode_iaf;
1980 int iafamily = AF_INET;
1982 inner_mode = xfrm_get_mode(x->props.mode, x->props.family);
1983 if (inner_mode == NULL)
1986 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL)) {
1987 xfrm_put_mode(inner_mode);
1990 x->inner_mode = inner_mode;
1992 if (x->props.family == AF_INET)
1993 iafamily = AF_INET6;
1995 inner_mode_iaf = xfrm_get_mode(x->props.mode, iafamily);
1996 if (inner_mode_iaf) {
1997 if (inner_mode_iaf->flags & XFRM_MODE_FLAG_TUNNEL)
1998 x->inner_mode_iaf = inner_mode_iaf;
2000 xfrm_put_mode(inner_mode_iaf);
2004 x->type = xfrm_get_type(x->id.proto, family);
2005 if (x->type == NULL)
2008 err = x->type->init_state(x);
2012 x->outer_mode = xfrm_get_mode(x->props.mode, family);
2013 if (x->outer_mode == NULL) {
2014 err = -EPROTONOSUPPORT;
2019 err = xfrm_init_replay(x);
2024 x->km.state = XFRM_STATE_VALID;
2030 EXPORT_SYMBOL(__xfrm_init_state);
2032 int xfrm_init_state(struct xfrm_state *x)
2034 return __xfrm_init_state(x, true);
2037 EXPORT_SYMBOL(xfrm_init_state);
2039 int __net_init xfrm_state_init(struct net *net)
2043 INIT_LIST_HEAD(&net->xfrm.state_all);
2045 sz = sizeof(struct hlist_head) * 8;
2047 net->xfrm.state_bydst = xfrm_hash_alloc(sz);
2048 if (!net->xfrm.state_bydst)
2050 net->xfrm.state_bysrc = xfrm_hash_alloc(sz);
2051 if (!net->xfrm.state_bysrc)
2053 net->xfrm.state_byspi = xfrm_hash_alloc(sz);
2054 if (!net->xfrm.state_byspi)
2056 net->xfrm.state_hmask = ((sz / sizeof(struct hlist_head)) - 1);
2058 net->xfrm.state_num = 0;
2059 INIT_WORK(&net->xfrm.state_hash_work, xfrm_hash_resize);
2060 INIT_HLIST_HEAD(&net->xfrm.state_gc_list);
2061 INIT_WORK(&net->xfrm.state_gc_work, xfrm_state_gc_task);
2062 spin_lock_init(&net->xfrm.xfrm_state_lock);
2066 xfrm_hash_free(net->xfrm.state_bysrc, sz);
2068 xfrm_hash_free(net->xfrm.state_bydst, sz);
2073 void xfrm_state_fini(struct net *net)
2075 struct xfrm_audit audit_info;
2078 flush_work(&net->xfrm.state_hash_work);
2079 audit_info.loginuid = INVALID_UID;
2080 audit_info.sessionid = (unsigned int)-1;
2081 audit_info.secid = 0;
2082 xfrm_state_flush(net, IPSEC_PROTO_ANY, &audit_info);
2083 flush_work(&net->xfrm.state_gc_work);
2085 WARN_ON(!list_empty(&net->xfrm.state_all));
2087 sz = (net->xfrm.state_hmask + 1) * sizeof(struct hlist_head);
2088 WARN_ON(!hlist_empty(net->xfrm.state_byspi));
2089 xfrm_hash_free(net->xfrm.state_byspi, sz);
2090 WARN_ON(!hlist_empty(net->xfrm.state_bysrc));
2091 xfrm_hash_free(net->xfrm.state_bysrc, sz);
2092 WARN_ON(!hlist_empty(net->xfrm.state_bydst));
2093 xfrm_hash_free(net->xfrm.state_bydst, sz);
2096 #ifdef CONFIG_AUDITSYSCALL
2097 static void xfrm_audit_helper_sainfo(struct xfrm_state *x,
2098 struct audit_buffer *audit_buf)
2100 struct xfrm_sec_ctx *ctx = x->security;
2101 u32 spi = ntohl(x->id.spi);
2104 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
2105 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
2107 switch (x->props.family) {
2109 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2110 &x->props.saddr.a4, &x->id.daddr.a4);
2113 audit_log_format(audit_buf, " src=%pI6 dst=%pI6",
2114 x->props.saddr.a6, x->id.daddr.a6);
2118 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
2121 static void xfrm_audit_helper_pktinfo(struct sk_buff *skb, u16 family,
2122 struct audit_buffer *audit_buf)
2124 const struct iphdr *iph4;
2125 const struct ipv6hdr *iph6;
2130 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2131 &iph4->saddr, &iph4->daddr);
2134 iph6 = ipv6_hdr(skb);
2135 audit_log_format(audit_buf,
2136 " src=%pI6 dst=%pI6 flowlbl=0x%x%02x%02x",
2137 &iph6->saddr, &iph6->daddr,
2138 iph6->flow_lbl[0] & 0x0f,
2145 void xfrm_audit_state_add(struct xfrm_state *x, int result,
2146 kuid_t auid, unsigned int sessionid, u32 secid)
2148 struct audit_buffer *audit_buf;
2150 audit_buf = xfrm_audit_start("SAD-add");
2151 if (audit_buf == NULL)
2153 xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf);
2154 xfrm_audit_helper_sainfo(x, audit_buf);
2155 audit_log_format(audit_buf, " res=%u", result);
2156 audit_log_end(audit_buf);
2158 EXPORT_SYMBOL_GPL(xfrm_audit_state_add);
2160 void xfrm_audit_state_delete(struct xfrm_state *x, int result,
2161 kuid_t auid, unsigned int sessionid, u32 secid)
2163 struct audit_buffer *audit_buf;
2165 audit_buf = xfrm_audit_start("SAD-delete");
2166 if (audit_buf == NULL)
2168 xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf);
2169 xfrm_audit_helper_sainfo(x, audit_buf);
2170 audit_log_format(audit_buf, " res=%u", result);
2171 audit_log_end(audit_buf);
2173 EXPORT_SYMBOL_GPL(xfrm_audit_state_delete);
2175 void xfrm_audit_state_replay_overflow(struct xfrm_state *x,
2176 struct sk_buff *skb)
2178 struct audit_buffer *audit_buf;
2181 audit_buf = xfrm_audit_start("SA-replay-overflow");
2182 if (audit_buf == NULL)
2184 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2185 /* don't record the sequence number because it's inherent in this kind
2186 * of audit message */
2187 spi = ntohl(x->id.spi);
2188 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
2189 audit_log_end(audit_buf);
2191 EXPORT_SYMBOL_GPL(xfrm_audit_state_replay_overflow);
2193 void xfrm_audit_state_replay(struct xfrm_state *x,
2194 struct sk_buff *skb, __be32 net_seq)
2196 struct audit_buffer *audit_buf;
2199 audit_buf = xfrm_audit_start("SA-replayed-pkt");
2200 if (audit_buf == NULL)
2202 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2203 spi = ntohl(x->id.spi);
2204 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2205 spi, spi, ntohl(net_seq));
2206 audit_log_end(audit_buf);
2208 EXPORT_SYMBOL_GPL(xfrm_audit_state_replay);
2210 void xfrm_audit_state_notfound_simple(struct sk_buff *skb, u16 family)
2212 struct audit_buffer *audit_buf;
2214 audit_buf = xfrm_audit_start("SA-notfound");
2215 if (audit_buf == NULL)
2217 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2218 audit_log_end(audit_buf);
2220 EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound_simple);
2222 void xfrm_audit_state_notfound(struct sk_buff *skb, u16 family,
2223 __be32 net_spi, __be32 net_seq)
2225 struct audit_buffer *audit_buf;
2228 audit_buf = xfrm_audit_start("SA-notfound");
2229 if (audit_buf == NULL)
2231 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2232 spi = ntohl(net_spi);
2233 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2234 spi, spi, ntohl(net_seq));
2235 audit_log_end(audit_buf);
2237 EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound);
2239 void xfrm_audit_state_icvfail(struct xfrm_state *x,
2240 struct sk_buff *skb, u8 proto)
2242 struct audit_buffer *audit_buf;
2246 audit_buf = xfrm_audit_start("SA-icv-failure");
2247 if (audit_buf == NULL)
2249 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2250 if (xfrm_parse_spi(skb, proto, &net_spi, &net_seq) == 0) {
2251 u32 spi = ntohl(net_spi);
2252 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2253 spi, spi, ntohl(net_seq));
2255 audit_log_end(audit_buf);
2257 EXPORT_SYMBOL_GPL(xfrm_audit_state_icvfail);
2258 #endif /* CONFIG_AUDITSYSCALL */