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 void xfrm_hash_resize(struct work_struct *work)
102 struct net *net = container_of(work, struct net, xfrm.state_hash_work);
103 struct hlist_head *ndst, *nsrc, *nspi, *odst, *osrc, *ospi;
104 unsigned long nsize, osize;
105 unsigned int nhashmask, ohashmask;
108 nsize = xfrm_hash_new_size(net->xfrm.state_hmask);
109 ndst = xfrm_hash_alloc(nsize);
112 nsrc = xfrm_hash_alloc(nsize);
114 xfrm_hash_free(ndst, nsize);
117 nspi = xfrm_hash_alloc(nsize);
119 xfrm_hash_free(ndst, nsize);
120 xfrm_hash_free(nsrc, nsize);
124 spin_lock_bh(&net->xfrm.xfrm_state_lock);
126 nhashmask = (nsize / sizeof(struct hlist_head)) - 1U;
127 for (i = net->xfrm.state_hmask; i >= 0; i--)
128 xfrm_hash_transfer(net->xfrm.state_bydst+i, ndst, nsrc, nspi,
131 odst = net->xfrm.state_bydst;
132 osrc = net->xfrm.state_bysrc;
133 ospi = net->xfrm.state_byspi;
134 ohashmask = net->xfrm.state_hmask;
136 net->xfrm.state_bydst = ndst;
137 net->xfrm.state_bysrc = nsrc;
138 net->xfrm.state_byspi = nspi;
139 net->xfrm.state_hmask = nhashmask;
141 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
143 osize = (ohashmask + 1) * sizeof(struct hlist_head);
144 xfrm_hash_free(odst, osize);
145 xfrm_hash_free(osrc, osize);
146 xfrm_hash_free(ospi, osize);
149 static DEFINE_SPINLOCK(xfrm_state_afinfo_lock);
150 static struct xfrm_state_afinfo __rcu *xfrm_state_afinfo[NPROTO];
152 static DEFINE_SPINLOCK(xfrm_state_gc_lock);
154 int __xfrm_state_delete(struct xfrm_state *x);
156 int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol);
157 bool km_is_alive(const struct km_event *c);
158 void km_state_expired(struct xfrm_state *x, int hard, u32 portid);
160 static DEFINE_SPINLOCK(xfrm_type_lock);
161 int xfrm_register_type(const struct xfrm_type *type, unsigned short family)
163 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
164 const struct xfrm_type **typemap;
167 if (unlikely(afinfo == NULL))
168 return -EAFNOSUPPORT;
169 typemap = afinfo->type_map;
170 spin_lock_bh(&xfrm_type_lock);
172 if (likely(typemap[type->proto] == NULL))
173 typemap[type->proto] = type;
176 spin_unlock_bh(&xfrm_type_lock);
177 xfrm_state_put_afinfo(afinfo);
180 EXPORT_SYMBOL(xfrm_register_type);
182 int xfrm_unregister_type(const struct xfrm_type *type, unsigned short family)
184 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
185 const struct xfrm_type **typemap;
188 if (unlikely(afinfo == NULL))
189 return -EAFNOSUPPORT;
190 typemap = afinfo->type_map;
191 spin_lock_bh(&xfrm_type_lock);
193 if (unlikely(typemap[type->proto] != type))
196 typemap[type->proto] = NULL;
197 spin_unlock_bh(&xfrm_type_lock);
198 xfrm_state_put_afinfo(afinfo);
201 EXPORT_SYMBOL(xfrm_unregister_type);
203 static const struct xfrm_type *xfrm_get_type(u8 proto, unsigned short family)
205 struct xfrm_state_afinfo *afinfo;
206 const struct xfrm_type **typemap;
207 const struct xfrm_type *type;
208 int modload_attempted = 0;
211 afinfo = xfrm_state_get_afinfo(family);
212 if (unlikely(afinfo == NULL))
214 typemap = afinfo->type_map;
216 type = typemap[proto];
217 if (unlikely(type && !try_module_get(type->owner)))
219 if (!type && !modload_attempted) {
220 xfrm_state_put_afinfo(afinfo);
221 request_module("xfrm-type-%d-%d", family, proto);
222 modload_attempted = 1;
226 xfrm_state_put_afinfo(afinfo);
230 static void xfrm_put_type(const struct xfrm_type *type)
232 module_put(type->owner);
235 static DEFINE_SPINLOCK(xfrm_mode_lock);
236 int xfrm_register_mode(struct xfrm_mode *mode, int family)
238 struct xfrm_state_afinfo *afinfo;
239 struct xfrm_mode **modemap;
242 if (unlikely(mode->encap >= XFRM_MODE_MAX))
245 afinfo = xfrm_state_get_afinfo(family);
246 if (unlikely(afinfo == NULL))
247 return -EAFNOSUPPORT;
250 modemap = afinfo->mode_map;
251 spin_lock_bh(&xfrm_mode_lock);
252 if (modemap[mode->encap])
256 if (!try_module_get(afinfo->owner))
259 mode->afinfo = afinfo;
260 modemap[mode->encap] = mode;
264 spin_unlock_bh(&xfrm_mode_lock);
265 xfrm_state_put_afinfo(afinfo);
268 EXPORT_SYMBOL(xfrm_register_mode);
270 int xfrm_unregister_mode(struct xfrm_mode *mode, int family)
272 struct xfrm_state_afinfo *afinfo;
273 struct xfrm_mode **modemap;
276 if (unlikely(mode->encap >= XFRM_MODE_MAX))
279 afinfo = xfrm_state_get_afinfo(family);
280 if (unlikely(afinfo == NULL))
281 return -EAFNOSUPPORT;
284 modemap = afinfo->mode_map;
285 spin_lock_bh(&xfrm_mode_lock);
286 if (likely(modemap[mode->encap] == mode)) {
287 modemap[mode->encap] = NULL;
288 module_put(mode->afinfo->owner);
292 spin_unlock_bh(&xfrm_mode_lock);
293 xfrm_state_put_afinfo(afinfo);
296 EXPORT_SYMBOL(xfrm_unregister_mode);
298 static struct xfrm_mode *xfrm_get_mode(unsigned int encap, int family)
300 struct xfrm_state_afinfo *afinfo;
301 struct xfrm_mode *mode;
302 int modload_attempted = 0;
304 if (unlikely(encap >= XFRM_MODE_MAX))
308 afinfo = xfrm_state_get_afinfo(family);
309 if (unlikely(afinfo == NULL))
312 mode = afinfo->mode_map[encap];
313 if (unlikely(mode && !try_module_get(mode->owner)))
315 if (!mode && !modload_attempted) {
316 xfrm_state_put_afinfo(afinfo);
317 request_module("xfrm-mode-%d-%d", family, encap);
318 modload_attempted = 1;
322 xfrm_state_put_afinfo(afinfo);
326 static void xfrm_put_mode(struct xfrm_mode *mode)
328 module_put(mode->owner);
331 static void xfrm_state_gc_destroy(struct xfrm_state *x)
333 tasklet_hrtimer_cancel(&x->mtimer);
334 del_timer_sync(&x->rtimer);
340 kfree(x->replay_esn);
341 kfree(x->preplay_esn);
343 xfrm_put_mode(x->inner_mode);
344 if (x->inner_mode_iaf)
345 xfrm_put_mode(x->inner_mode_iaf);
347 xfrm_put_mode(x->outer_mode);
349 x->type->destructor(x);
350 xfrm_put_type(x->type);
352 security_xfrm_state_free(x);
356 static void xfrm_state_gc_task(struct work_struct *work)
358 struct net *net = container_of(work, struct net, xfrm.state_gc_work);
359 struct xfrm_state *x;
360 struct hlist_node *tmp;
361 struct hlist_head gc_list;
363 spin_lock_bh(&xfrm_state_gc_lock);
364 hlist_move_list(&net->xfrm.state_gc_list, &gc_list);
365 spin_unlock_bh(&xfrm_state_gc_lock);
367 hlist_for_each_entry_safe(x, tmp, &gc_list, gclist)
368 xfrm_state_gc_destroy(x);
371 static inline unsigned long make_jiffies(long secs)
373 if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
374 return MAX_SCHEDULE_TIMEOUT-1;
379 static enum hrtimer_restart xfrm_timer_handler(struct hrtimer *me)
381 struct tasklet_hrtimer *thr = container_of(me, struct tasklet_hrtimer, timer);
382 struct xfrm_state *x = container_of(thr, struct xfrm_state, mtimer);
383 unsigned long now = get_seconds();
384 long next = LONG_MAX;
389 if (x->km.state == XFRM_STATE_DEAD)
391 if (x->km.state == XFRM_STATE_EXPIRED)
393 if (x->lft.hard_add_expires_seconds) {
394 long tmo = x->lft.hard_add_expires_seconds +
395 x->curlft.add_time - now;
397 if (x->xflags & XFRM_SOFT_EXPIRE) {
398 /* enter hard expire without soft expire first?!
399 * setting a new date could trigger this.
400 * workarbound: fix x->curflt.add_time by below:
402 x->curlft.add_time = now - x->saved_tmo - 1;
403 tmo = x->lft.hard_add_expires_seconds - x->saved_tmo;
410 if (x->lft.hard_use_expires_seconds) {
411 long tmo = x->lft.hard_use_expires_seconds +
412 (x->curlft.use_time ? : now) - now;
420 if (x->lft.soft_add_expires_seconds) {
421 long tmo = x->lft.soft_add_expires_seconds +
422 x->curlft.add_time - now;
425 x->xflags &= ~XFRM_SOFT_EXPIRE;
426 } else if (tmo < next) {
428 x->xflags |= XFRM_SOFT_EXPIRE;
432 if (x->lft.soft_use_expires_seconds) {
433 long tmo = x->lft.soft_use_expires_seconds +
434 (x->curlft.use_time ? : now) - now;
443 km_state_expired(x, 0, 0);
445 if (next != LONG_MAX) {
446 tasklet_hrtimer_start(&x->mtimer, ktime_set(next, 0), HRTIMER_MODE_REL);
452 if (x->km.state == XFRM_STATE_ACQ && x->id.spi == 0)
453 x->km.state = XFRM_STATE_EXPIRED;
455 err = __xfrm_state_delete(x);
457 km_state_expired(x, 1, 0);
459 xfrm_audit_state_delete(x, err ? 0 : 1, true);
462 spin_unlock(&x->lock);
463 return HRTIMER_NORESTART;
466 static void xfrm_replay_timer_handler(unsigned long data);
468 struct xfrm_state *xfrm_state_alloc(struct net *net)
470 struct xfrm_state *x;
472 x = kzalloc(sizeof(struct xfrm_state), GFP_ATOMIC);
475 write_pnet(&x->xs_net, net);
476 atomic_set(&x->refcnt, 1);
477 atomic_set(&x->tunnel_users, 0);
478 INIT_LIST_HEAD(&x->km.all);
479 INIT_HLIST_NODE(&x->bydst);
480 INIT_HLIST_NODE(&x->bysrc);
481 INIT_HLIST_NODE(&x->byspi);
482 tasklet_hrtimer_init(&x->mtimer, xfrm_timer_handler,
483 CLOCK_BOOTTIME, HRTIMER_MODE_ABS);
484 setup_timer(&x->rtimer, xfrm_replay_timer_handler,
486 x->curlft.add_time = get_seconds();
487 x->lft.soft_byte_limit = XFRM_INF;
488 x->lft.soft_packet_limit = XFRM_INF;
489 x->lft.hard_byte_limit = XFRM_INF;
490 x->lft.hard_packet_limit = XFRM_INF;
491 x->replay_maxage = 0;
492 x->replay_maxdiff = 0;
493 x->inner_mode = NULL;
494 x->inner_mode_iaf = NULL;
495 spin_lock_init(&x->lock);
499 EXPORT_SYMBOL(xfrm_state_alloc);
501 void __xfrm_state_destroy(struct xfrm_state *x)
503 struct net *net = xs_net(x);
505 WARN_ON(x->km.state != XFRM_STATE_DEAD);
507 spin_lock_bh(&xfrm_state_gc_lock);
508 hlist_add_head(&x->gclist, &net->xfrm.state_gc_list);
509 spin_unlock_bh(&xfrm_state_gc_lock);
510 schedule_work(&net->xfrm.state_gc_work);
512 EXPORT_SYMBOL(__xfrm_state_destroy);
514 int __xfrm_state_delete(struct xfrm_state *x)
516 struct net *net = xs_net(x);
519 if (x->km.state != XFRM_STATE_DEAD) {
520 x->km.state = XFRM_STATE_DEAD;
521 spin_lock(&net->xfrm.xfrm_state_lock);
522 list_del(&x->km.all);
523 hlist_del(&x->bydst);
524 hlist_del(&x->bysrc);
526 hlist_del(&x->byspi);
527 net->xfrm.state_num--;
528 spin_unlock(&net->xfrm.xfrm_state_lock);
530 /* All xfrm_state objects are created by xfrm_state_alloc.
531 * The xfrm_state_alloc call gives a reference, and that
532 * is what we are dropping here.
540 EXPORT_SYMBOL(__xfrm_state_delete);
542 int xfrm_state_delete(struct xfrm_state *x)
546 spin_lock_bh(&x->lock);
547 err = __xfrm_state_delete(x);
548 spin_unlock_bh(&x->lock);
552 EXPORT_SYMBOL(xfrm_state_delete);
554 #ifdef CONFIG_SECURITY_NETWORK_XFRM
556 xfrm_state_flush_secctx_check(struct net *net, u8 proto, bool task_valid)
560 for (i = 0; i <= net->xfrm.state_hmask; i++) {
561 struct xfrm_state *x;
563 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
564 if (xfrm_id_proto_match(x->id.proto, proto) &&
565 (err = security_xfrm_state_delete(x)) != 0) {
566 xfrm_audit_state_delete(x, 0, task_valid);
576 xfrm_state_flush_secctx_check(struct net *net, u8 proto, bool task_valid)
582 int xfrm_state_flush(struct net *net, u8 proto, bool task_valid)
584 int i, err = 0, cnt = 0;
586 spin_lock_bh(&net->xfrm.xfrm_state_lock);
587 err = xfrm_state_flush_secctx_check(net, proto, task_valid);
592 for (i = 0; i <= net->xfrm.state_hmask; i++) {
593 struct xfrm_state *x;
595 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
596 if (!xfrm_state_kern(x) &&
597 xfrm_id_proto_match(x->id.proto, proto)) {
599 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
601 err = xfrm_state_delete(x);
602 xfrm_audit_state_delete(x, err ? 0 : 1,
608 spin_lock_bh(&net->xfrm.xfrm_state_lock);
617 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
620 EXPORT_SYMBOL(xfrm_state_flush);
622 void xfrm_sad_getinfo(struct net *net, struct xfrmk_sadinfo *si)
624 spin_lock_bh(&net->xfrm.xfrm_state_lock);
625 si->sadcnt = net->xfrm.state_num;
626 si->sadhcnt = net->xfrm.state_hmask;
627 si->sadhmcnt = xfrm_state_hashmax;
628 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
630 EXPORT_SYMBOL(xfrm_sad_getinfo);
633 xfrm_init_tempstate(struct xfrm_state *x, const struct flowi *fl,
634 const struct xfrm_tmpl *tmpl,
635 const xfrm_address_t *daddr, const xfrm_address_t *saddr,
636 unsigned short family)
638 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
641 afinfo->init_tempsel(&x->sel, fl);
643 if (family != tmpl->encap_family) {
644 xfrm_state_put_afinfo(afinfo);
645 afinfo = xfrm_state_get_afinfo(tmpl->encap_family);
649 afinfo->init_temprop(x, tmpl, daddr, saddr);
650 xfrm_state_put_afinfo(afinfo);
654 static struct xfrm_state *__xfrm_state_lookup(struct net *net, u32 mark,
655 const xfrm_address_t *daddr,
656 __be32 spi, u8 proto,
657 unsigned short family)
659 unsigned int h = xfrm_spi_hash(net, daddr, spi, proto, family);
660 struct xfrm_state *x;
662 hlist_for_each_entry(x, net->xfrm.state_byspi+h, byspi) {
663 if (x->props.family != family ||
665 x->id.proto != proto ||
666 !xfrm_addr_equal(&x->id.daddr, daddr, family))
669 if ((mark & x->mark.m) != x->mark.v)
678 static struct xfrm_state *__xfrm_state_lookup_byaddr(struct net *net, u32 mark,
679 const xfrm_address_t *daddr,
680 const xfrm_address_t *saddr,
681 u8 proto, unsigned short family)
683 unsigned int h = xfrm_src_hash(net, daddr, saddr, family);
684 struct xfrm_state *x;
686 hlist_for_each_entry(x, net->xfrm.state_bysrc+h, bysrc) {
687 if (x->props.family != family ||
688 x->id.proto != proto ||
689 !xfrm_addr_equal(&x->id.daddr, daddr, family) ||
690 !xfrm_addr_equal(&x->props.saddr, saddr, family))
693 if ((mark & x->mark.m) != x->mark.v)
702 static inline struct xfrm_state *
703 __xfrm_state_locate(struct xfrm_state *x, int use_spi, int family)
705 struct net *net = xs_net(x);
706 u32 mark = x->mark.v & x->mark.m;
709 return __xfrm_state_lookup(net, mark, &x->id.daddr,
710 x->id.spi, x->id.proto, family);
712 return __xfrm_state_lookup_byaddr(net, mark,
715 x->id.proto, family);
718 static void xfrm_hash_grow_check(struct net *net, int have_hash_collision)
720 if (have_hash_collision &&
721 (net->xfrm.state_hmask + 1) < xfrm_state_hashmax &&
722 net->xfrm.state_num > net->xfrm.state_hmask)
723 schedule_work(&net->xfrm.state_hash_work);
726 static void xfrm_state_look_at(struct xfrm_policy *pol, struct xfrm_state *x,
727 const struct flowi *fl, unsigned short family,
728 struct xfrm_state **best, int *acq_in_progress,
732 * 1. There is a valid state with matching selector. Done.
733 * 2. Valid state with inappropriate selector. Skip.
735 * Entering area of "sysdeps".
737 * 3. If state is not valid, selector is temporary, it selects
738 * only session which triggered previous resolution. Key
739 * manager will do something to install a state with proper
742 if (x->km.state == XFRM_STATE_VALID) {
743 if ((x->sel.family &&
744 !xfrm_selector_match(&x->sel, fl, x->sel.family)) ||
745 !security_xfrm_state_pol_flow_match(x, pol, fl))
749 (*best)->km.dying > x->km.dying ||
750 ((*best)->km.dying == x->km.dying &&
751 (*best)->curlft.add_time < x->curlft.add_time))
753 } else if (x->km.state == XFRM_STATE_ACQ) {
754 *acq_in_progress = 1;
755 } else if (x->km.state == XFRM_STATE_ERROR ||
756 x->km.state == XFRM_STATE_EXPIRED) {
757 if (xfrm_selector_match(&x->sel, fl, x->sel.family) &&
758 security_xfrm_state_pol_flow_match(x, pol, fl))
764 xfrm_state_find(const xfrm_address_t *daddr, const xfrm_address_t *saddr,
765 const struct flowi *fl, struct xfrm_tmpl *tmpl,
766 struct xfrm_policy *pol, int *err,
767 unsigned short family)
769 static xfrm_address_t saddr_wildcard = { };
770 struct net *net = xp_net(pol);
771 unsigned int h, h_wildcard;
772 struct xfrm_state *x, *x0, *to_put;
773 int acquire_in_progress = 0;
775 struct xfrm_state *best = NULL;
776 u32 mark = pol->mark.v & pol->mark.m;
777 unsigned short encap_family = tmpl->encap_family;
782 spin_lock_bh(&net->xfrm.xfrm_state_lock);
783 h = xfrm_dst_hash(net, daddr, saddr, tmpl->reqid, encap_family);
784 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
785 if (x->props.family == encap_family &&
786 x->props.reqid == tmpl->reqid &&
787 (mark & x->mark.m) == x->mark.v &&
788 !(x->props.flags & XFRM_STATE_WILDRECV) &&
789 xfrm_state_addr_check(x, daddr, saddr, encap_family) &&
790 tmpl->mode == x->props.mode &&
791 tmpl->id.proto == x->id.proto &&
792 (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
793 xfrm_state_look_at(pol, x, fl, encap_family,
794 &best, &acquire_in_progress, &error);
796 if (best || acquire_in_progress)
799 h_wildcard = xfrm_dst_hash(net, daddr, &saddr_wildcard, tmpl->reqid, encap_family);
800 hlist_for_each_entry(x, net->xfrm.state_bydst+h_wildcard, bydst) {
801 if (x->props.family == encap_family &&
802 x->props.reqid == tmpl->reqid &&
803 (mark & x->mark.m) == x->mark.v &&
804 !(x->props.flags & XFRM_STATE_WILDRECV) &&
805 xfrm_addr_equal(&x->id.daddr, daddr, encap_family) &&
806 tmpl->mode == x->props.mode &&
807 tmpl->id.proto == x->id.proto &&
808 (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
809 xfrm_state_look_at(pol, x, fl, encap_family,
810 &best, &acquire_in_progress, &error);
815 if (!x && !error && !acquire_in_progress) {
817 (x0 = __xfrm_state_lookup(net, mark, daddr, tmpl->id.spi,
818 tmpl->id.proto, encap_family)) != NULL) {
825 /* If the KMs have no listeners (yet...), avoid allocating an SA
826 * for each and every packet - garbage collection might not
829 if (!km_is_alive(&c)) {
834 x = xfrm_state_alloc(net);
839 /* Initialize temporary state matching only
840 * to current session. */
841 xfrm_init_tempstate(x, fl, tmpl, daddr, saddr, family);
842 memcpy(&x->mark, &pol->mark, sizeof(x->mark));
844 error = security_xfrm_state_alloc_acquire(x, pol->security, fl->flowi_secid);
846 x->km.state = XFRM_STATE_DEAD;
852 if (km_query(x, tmpl, pol) == 0) {
853 x->km.state = XFRM_STATE_ACQ;
854 list_add(&x->km.all, &net->xfrm.state_all);
855 hlist_add_head(&x->bydst, net->xfrm.state_bydst+h);
856 h = xfrm_src_hash(net, daddr, saddr, encap_family);
857 hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h);
859 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, encap_family);
860 hlist_add_head(&x->byspi, net->xfrm.state_byspi+h);
862 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
863 tasklet_hrtimer_start(&x->mtimer, ktime_set(net->xfrm.sysctl_acq_expires, 0), HRTIMER_MODE_REL);
864 net->xfrm.state_num++;
865 xfrm_hash_grow_check(net, x->bydst.next != NULL);
867 x->km.state = XFRM_STATE_DEAD;
877 *err = acquire_in_progress ? -EAGAIN : error;
878 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
880 xfrm_state_put(to_put);
885 xfrm_stateonly_find(struct net *net, u32 mark,
886 xfrm_address_t *daddr, xfrm_address_t *saddr,
887 unsigned short family, u8 mode, u8 proto, u32 reqid)
890 struct xfrm_state *rx = NULL, *x = NULL;
892 spin_lock_bh(&net->xfrm.xfrm_state_lock);
893 h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
894 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
895 if (x->props.family == family &&
896 x->props.reqid == reqid &&
897 (mark & x->mark.m) == x->mark.v &&
898 !(x->props.flags & XFRM_STATE_WILDRECV) &&
899 xfrm_state_addr_check(x, daddr, saddr, family) &&
900 mode == x->props.mode &&
901 proto == x->id.proto &&
902 x->km.state == XFRM_STATE_VALID) {
910 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
915 EXPORT_SYMBOL(xfrm_stateonly_find);
917 struct xfrm_state *xfrm_state_lookup_byspi(struct net *net, __be32 spi,
918 unsigned short family)
920 struct xfrm_state *x;
921 struct xfrm_state_walk *w;
923 spin_lock_bh(&net->xfrm.xfrm_state_lock);
924 list_for_each_entry(w, &net->xfrm.state_all, all) {
925 x = container_of(w, struct xfrm_state, km);
926 if (x->props.family != family ||
930 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
934 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
937 EXPORT_SYMBOL(xfrm_state_lookup_byspi);
939 static void __xfrm_state_insert(struct xfrm_state *x)
941 struct net *net = xs_net(x);
944 list_add(&x->km.all, &net->xfrm.state_all);
946 h = xfrm_dst_hash(net, &x->id.daddr, &x->props.saddr,
947 x->props.reqid, x->props.family);
948 hlist_add_head(&x->bydst, net->xfrm.state_bydst+h);
950 h = xfrm_src_hash(net, &x->id.daddr, &x->props.saddr, x->props.family);
951 hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h);
954 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto,
957 hlist_add_head(&x->byspi, net->xfrm.state_byspi+h);
960 tasklet_hrtimer_start(&x->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL);
961 if (x->replay_maxage)
962 mod_timer(&x->rtimer, jiffies + x->replay_maxage);
964 net->xfrm.state_num++;
966 xfrm_hash_grow_check(net, x->bydst.next != NULL);
969 /* net->xfrm.xfrm_state_lock is held */
970 static void __xfrm_state_bump_genids(struct xfrm_state *xnew)
972 struct net *net = xs_net(xnew);
973 unsigned short family = xnew->props.family;
974 u32 reqid = xnew->props.reqid;
975 struct xfrm_state *x;
977 u32 mark = xnew->mark.v & xnew->mark.m;
979 h = xfrm_dst_hash(net, &xnew->id.daddr, &xnew->props.saddr, reqid, family);
980 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
981 if (x->props.family == family &&
982 x->props.reqid == reqid &&
983 (mark & x->mark.m) == x->mark.v &&
984 xfrm_addr_equal(&x->id.daddr, &xnew->id.daddr, family) &&
985 xfrm_addr_equal(&x->props.saddr, &xnew->props.saddr, family))
990 void xfrm_state_insert(struct xfrm_state *x)
992 struct net *net = xs_net(x);
994 spin_lock_bh(&net->xfrm.xfrm_state_lock);
995 __xfrm_state_bump_genids(x);
996 __xfrm_state_insert(x);
997 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
999 EXPORT_SYMBOL(xfrm_state_insert);
1001 /* net->xfrm.xfrm_state_lock is held */
1002 static struct xfrm_state *__find_acq_core(struct net *net,
1003 const struct xfrm_mark *m,
1004 unsigned short family, u8 mode,
1005 u32 reqid, u8 proto,
1006 const xfrm_address_t *daddr,
1007 const xfrm_address_t *saddr,
1010 unsigned int h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
1011 struct xfrm_state *x;
1012 u32 mark = m->v & m->m;
1014 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
1015 if (x->props.reqid != reqid ||
1016 x->props.mode != mode ||
1017 x->props.family != family ||
1018 x->km.state != XFRM_STATE_ACQ ||
1020 x->id.proto != proto ||
1021 (mark & x->mark.m) != x->mark.v ||
1022 !xfrm_addr_equal(&x->id.daddr, daddr, family) ||
1023 !xfrm_addr_equal(&x->props.saddr, saddr, family))
1033 x = xfrm_state_alloc(net);
1037 x->sel.daddr.a4 = daddr->a4;
1038 x->sel.saddr.a4 = saddr->a4;
1039 x->sel.prefixlen_d = 32;
1040 x->sel.prefixlen_s = 32;
1041 x->props.saddr.a4 = saddr->a4;
1042 x->id.daddr.a4 = daddr->a4;
1046 *(struct in6_addr *)x->sel.daddr.a6 = *(struct in6_addr *)daddr;
1047 *(struct in6_addr *)x->sel.saddr.a6 = *(struct in6_addr *)saddr;
1048 x->sel.prefixlen_d = 128;
1049 x->sel.prefixlen_s = 128;
1050 *(struct in6_addr *)x->props.saddr.a6 = *(struct in6_addr *)saddr;
1051 *(struct in6_addr *)x->id.daddr.a6 = *(struct in6_addr *)daddr;
1055 x->km.state = XFRM_STATE_ACQ;
1056 x->id.proto = proto;
1057 x->props.family = family;
1058 x->props.mode = mode;
1059 x->props.reqid = reqid;
1062 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
1064 tasklet_hrtimer_start(&x->mtimer, ktime_set(net->xfrm.sysctl_acq_expires, 0), HRTIMER_MODE_REL);
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 mark, 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 u32 mark = x->mark.v & x->mark.m;
1087 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
1089 family = x->props.family;
1093 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1095 x1 = __xfrm_state_locate(x, use_spi, family);
1103 if (use_spi && x->km.seq) {
1104 x1 = __xfrm_find_acq_byseq(net, mark, x->km.seq);
1105 if (x1 && ((x1->id.proto != x->id.proto) ||
1106 !xfrm_addr_equal(&x1->id.daddr, &x->id.daddr, family))) {
1113 x1 = __find_acq_core(net, &x->mark, family, x->props.mode,
1114 x->props.reqid, x->id.proto,
1115 &x->id.daddr, &x->props.saddr, 0);
1117 __xfrm_state_bump_genids(x);
1118 __xfrm_state_insert(x);
1122 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1125 xfrm_state_delete(x1);
1130 xfrm_state_put(to_put);
1134 EXPORT_SYMBOL(xfrm_state_add);
1136 #ifdef CONFIG_XFRM_MIGRATE
1137 static struct xfrm_state *xfrm_state_clone(struct xfrm_state *orig)
1139 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_auth_clone(orig->aalg);
1158 x->props.aalgo = orig->props.aalgo;
1161 x->aead = xfrm_algo_aead_clone(orig->aead);
1166 x->ealg = xfrm_algo_clone(orig->ealg);
1170 x->props.ealgo = orig->props.ealgo;
1173 x->calg = xfrm_algo_clone(orig->calg);
1177 x->props.calgo = orig->props.calgo;
1180 x->encap = kmemdup(orig->encap, sizeof(*x->encap), GFP_KERNEL);
1186 x->coaddr = kmemdup(orig->coaddr, sizeof(*x->coaddr),
1192 if (orig->replay_esn) {
1193 if (xfrm_replay_clone(x, orig))
1197 memcpy(&x->mark, &orig->mark, sizeof(x->mark));
1199 if (xfrm_init_state(x) < 0)
1202 x->props.flags = orig->props.flags;
1203 x->props.extra_flags = orig->props.extra_flags;
1205 x->tfcpad = orig->tfcpad;
1206 x->replay_maxdiff = orig->replay_maxdiff;
1207 x->replay_maxage = orig->replay_maxage;
1208 x->curlft.add_time = orig->curlft.add_time;
1209 x->km.state = orig->km.state;
1210 x->km.seq = orig->km.seq;
1220 struct xfrm_state *xfrm_migrate_state_find(struct xfrm_migrate *m, struct net *net)
1223 struct xfrm_state *x = NULL;
1225 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1228 h = xfrm_dst_hash(net, &m->old_daddr, &m->old_saddr,
1229 m->reqid, m->old_family);
1230 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
1231 if (x->props.mode != m->mode ||
1232 x->id.proto != m->proto)
1234 if (m->reqid && x->props.reqid != m->reqid)
1236 if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr,
1238 !xfrm_addr_equal(&x->props.saddr, &m->old_saddr,
1245 h = xfrm_src_hash(net, &m->old_daddr, &m->old_saddr,
1247 hlist_for_each_entry(x, net->xfrm.state_bysrc+h, bysrc) {
1248 if (x->props.mode != m->mode ||
1249 x->id.proto != m->proto)
1251 if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr,
1253 !xfrm_addr_equal(&x->props.saddr, &m->old_saddr,
1261 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1265 EXPORT_SYMBOL(xfrm_migrate_state_find);
1267 struct xfrm_state *xfrm_state_migrate(struct xfrm_state *x,
1268 struct xfrm_migrate *m)
1270 struct xfrm_state *xc;
1272 xc = xfrm_state_clone(x);
1276 memcpy(&xc->id.daddr, &m->new_daddr, sizeof(xc->id.daddr));
1277 memcpy(&xc->props.saddr, &m->new_saddr, sizeof(xc->props.saddr));
1280 if (xfrm_addr_equal(&x->id.daddr, &m->new_daddr, m->new_family)) {
1281 /* a care is needed when the destination address of the
1282 state is to be updated as it is a part of triplet */
1283 xfrm_state_insert(xc);
1285 if (xfrm_state_add(xc) < 0)
1294 EXPORT_SYMBOL(xfrm_state_migrate);
1297 int xfrm_state_update(struct xfrm_state *x)
1299 struct xfrm_state *x1, *to_put;
1301 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
1302 struct net *net = xs_net(x);
1306 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1307 x1 = __xfrm_state_locate(x, use_spi, x->props.family);
1313 if (xfrm_state_kern(x1)) {
1319 if (x1->km.state == XFRM_STATE_ACQ) {
1320 __xfrm_state_insert(x);
1326 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1329 xfrm_state_put(to_put);
1335 xfrm_state_delete(x1);
1341 spin_lock_bh(&x1->lock);
1342 if (likely(x1->km.state == XFRM_STATE_VALID)) {
1343 if (x->encap && x1->encap)
1344 memcpy(x1->encap, x->encap, sizeof(*x1->encap));
1345 if (x->coaddr && x1->coaddr) {
1346 memcpy(x1->coaddr, x->coaddr, sizeof(*x1->coaddr));
1348 if (!use_spi && memcmp(&x1->sel, &x->sel, sizeof(x1->sel)))
1349 memcpy(&x1->sel, &x->sel, sizeof(x1->sel));
1350 memcpy(&x1->lft, &x->lft, sizeof(x1->lft));
1353 tasklet_hrtimer_start(&x1->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL);
1354 if (x1->curlft.use_time)
1355 xfrm_state_check_expire(x1);
1358 x->km.state = XFRM_STATE_DEAD;
1359 __xfrm_state_put(x);
1361 spin_unlock_bh(&x1->lock);
1367 EXPORT_SYMBOL(xfrm_state_update);
1369 int xfrm_state_check_expire(struct xfrm_state *x)
1371 if (!x->curlft.use_time)
1372 x->curlft.use_time = get_seconds();
1374 if (x->curlft.bytes >= x->lft.hard_byte_limit ||
1375 x->curlft.packets >= x->lft.hard_packet_limit) {
1376 x->km.state = XFRM_STATE_EXPIRED;
1377 tasklet_hrtimer_start(&x->mtimer, ktime_set(0, 0), HRTIMER_MODE_REL);
1382 (x->curlft.bytes >= x->lft.soft_byte_limit ||
1383 x->curlft.packets >= x->lft.soft_packet_limit)) {
1385 km_state_expired(x, 0, 0);
1389 EXPORT_SYMBOL(xfrm_state_check_expire);
1392 xfrm_state_lookup(struct net *net, u32 mark, const xfrm_address_t *daddr, __be32 spi,
1393 u8 proto, unsigned short family)
1395 struct xfrm_state *x;
1397 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1398 x = __xfrm_state_lookup(net, mark, daddr, spi, proto, family);
1399 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1402 EXPORT_SYMBOL(xfrm_state_lookup);
1405 xfrm_state_lookup_byaddr(struct net *net, u32 mark,
1406 const xfrm_address_t *daddr, const xfrm_address_t *saddr,
1407 u8 proto, unsigned short family)
1409 struct xfrm_state *x;
1411 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1412 x = __xfrm_state_lookup_byaddr(net, mark, daddr, saddr, proto, family);
1413 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1416 EXPORT_SYMBOL(xfrm_state_lookup_byaddr);
1419 xfrm_find_acq(struct net *net, const struct xfrm_mark *mark, u8 mode, u32 reqid,
1420 u8 proto, const xfrm_address_t *daddr,
1421 const xfrm_address_t *saddr, int create, unsigned short family)
1423 struct xfrm_state *x;
1425 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1426 x = __find_acq_core(net, mark, family, mode, reqid, proto, daddr, saddr, create);
1427 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1431 EXPORT_SYMBOL(xfrm_find_acq);
1433 #ifdef CONFIG_XFRM_SUB_POLICY
1435 xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n,
1436 unsigned short family, struct net *net)
1439 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
1441 return -EAFNOSUPPORT;
1443 spin_lock_bh(&net->xfrm.xfrm_state_lock); /*FIXME*/
1444 if (afinfo->tmpl_sort)
1445 err = afinfo->tmpl_sort(dst, src, n);
1446 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1447 xfrm_state_put_afinfo(afinfo);
1450 EXPORT_SYMBOL(xfrm_tmpl_sort);
1453 xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src, int n,
1454 unsigned short family)
1457 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
1458 struct net *net = xs_net(*src);
1461 return -EAFNOSUPPORT;
1463 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1464 if (afinfo->state_sort)
1465 err = afinfo->state_sort(dst, src, n);
1466 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1467 xfrm_state_put_afinfo(afinfo);
1470 EXPORT_SYMBOL(xfrm_state_sort);
1473 /* Silly enough, but I'm lazy to build resolution list */
1475 static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
1479 for (i = 0; i <= net->xfrm.state_hmask; i++) {
1480 struct xfrm_state *x;
1482 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
1483 if (x->km.seq == seq &&
1484 (mark & x->mark.m) == x->mark.v &&
1485 x->km.state == XFRM_STATE_ACQ) {
1494 struct xfrm_state *xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
1496 struct xfrm_state *x;
1498 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1499 x = __xfrm_find_acq_byseq(net, mark, seq);
1500 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1503 EXPORT_SYMBOL(xfrm_find_acq_byseq);
1505 u32 xfrm_get_acqseq(void)
1508 static atomic_t acqseq;
1511 res = atomic_inc_return(&acqseq);
1516 EXPORT_SYMBOL(xfrm_get_acqseq);
1518 int verify_spi_info(u8 proto, u32 min, u32 max)
1526 /* IPCOMP spi is 16-bits. */
1540 EXPORT_SYMBOL(verify_spi_info);
1542 int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high)
1544 struct net *net = xs_net(x);
1546 struct xfrm_state *x0;
1548 __be32 minspi = htonl(low);
1549 __be32 maxspi = htonl(high);
1550 u32 mark = x->mark.v & x->mark.m;
1552 spin_lock_bh(&x->lock);
1553 if (x->km.state == XFRM_STATE_DEAD)
1562 if (minspi == maxspi) {
1563 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, minspi, x->id.proto, x->props.family);
1571 for (h = 0; h < high-low+1; h++) {
1572 spi = low + prandom_u32()%(high-low+1);
1573 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, htonl(spi), x->id.proto, x->props.family);
1575 x->id.spi = htonl(spi);
1582 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1583 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, x->props.family);
1584 hlist_add_head(&x->byspi, net->xfrm.state_byspi+h);
1585 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1591 spin_unlock_bh(&x->lock);
1595 EXPORT_SYMBOL(xfrm_alloc_spi);
1597 static bool __xfrm_state_filter_match(struct xfrm_state *x,
1598 struct xfrm_address_filter *filter)
1601 if ((filter->family == AF_INET ||
1602 filter->family == AF_INET6) &&
1603 x->props.family != filter->family)
1606 return addr_match(&x->props.saddr, &filter->saddr,
1608 addr_match(&x->id.daddr, &filter->daddr,
1614 int xfrm_state_walk(struct net *net, struct xfrm_state_walk *walk,
1615 int (*func)(struct xfrm_state *, int, void*),
1618 struct xfrm_state *state;
1619 struct xfrm_state_walk *x;
1622 if (walk->seq != 0 && list_empty(&walk->all))
1625 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1626 if (list_empty(&walk->all))
1627 x = list_first_entry(&net->xfrm.state_all, struct xfrm_state_walk, all);
1629 x = list_entry(&walk->all, struct xfrm_state_walk, all);
1630 list_for_each_entry_from(x, &net->xfrm.state_all, all) {
1631 if (x->state == XFRM_STATE_DEAD)
1633 state = container_of(x, struct xfrm_state, km);
1634 if (!xfrm_id_proto_match(state->id.proto, walk->proto))
1636 if (!__xfrm_state_filter_match(state, walk->filter))
1638 err = func(state, walk->seq, data);
1640 list_move_tail(&walk->all, &x->all);
1645 if (walk->seq == 0) {
1649 list_del_init(&walk->all);
1651 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1654 EXPORT_SYMBOL(xfrm_state_walk);
1656 void xfrm_state_walk_init(struct xfrm_state_walk *walk, u8 proto,
1657 struct xfrm_address_filter *filter)
1659 INIT_LIST_HEAD(&walk->all);
1660 walk->proto = proto;
1661 walk->state = XFRM_STATE_DEAD;
1663 walk->filter = filter;
1665 EXPORT_SYMBOL(xfrm_state_walk_init);
1667 void xfrm_state_walk_done(struct xfrm_state_walk *walk, struct net *net)
1669 kfree(walk->filter);
1671 if (list_empty(&walk->all))
1674 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1675 list_del(&walk->all);
1676 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1678 EXPORT_SYMBOL(xfrm_state_walk_done);
1680 static void xfrm_replay_timer_handler(unsigned long data)
1682 struct xfrm_state *x = (struct xfrm_state *)data;
1684 spin_lock(&x->lock);
1686 if (x->km.state == XFRM_STATE_VALID) {
1687 if (xfrm_aevent_is_on(xs_net(x)))
1688 x->repl->notify(x, XFRM_REPLAY_TIMEOUT);
1690 x->xflags |= XFRM_TIME_DEFER;
1693 spin_unlock(&x->lock);
1696 static LIST_HEAD(xfrm_km_list);
1698 void km_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
1700 struct xfrm_mgr *km;
1703 list_for_each_entry_rcu(km, &xfrm_km_list, list)
1704 if (km->notify_policy)
1705 km->notify_policy(xp, dir, c);
1709 void km_state_notify(struct xfrm_state *x, const struct km_event *c)
1711 struct xfrm_mgr *km;
1713 list_for_each_entry_rcu(km, &xfrm_km_list, list)
1719 EXPORT_SYMBOL(km_policy_notify);
1720 EXPORT_SYMBOL(km_state_notify);
1722 void km_state_expired(struct xfrm_state *x, int hard, u32 portid)
1728 c.event = XFRM_MSG_EXPIRE;
1729 km_state_notify(x, &c);
1732 EXPORT_SYMBOL(km_state_expired);
1734 * We send to all registered managers regardless of failure
1735 * We are happy with one success
1737 int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol)
1739 int err = -EINVAL, acqret;
1740 struct xfrm_mgr *km;
1743 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
1744 acqret = km->acquire(x, t, pol);
1751 EXPORT_SYMBOL(km_query);
1753 int km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
1756 struct xfrm_mgr *km;
1759 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
1760 if (km->new_mapping)
1761 err = km->new_mapping(x, ipaddr, sport);
1768 EXPORT_SYMBOL(km_new_mapping);
1770 void km_policy_expired(struct xfrm_policy *pol, int dir, int hard, u32 portid)
1776 c.event = XFRM_MSG_POLEXPIRE;
1777 km_policy_notify(pol, dir, &c);
1779 EXPORT_SYMBOL(km_policy_expired);
1781 #ifdef CONFIG_XFRM_MIGRATE
1782 int km_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
1783 const struct xfrm_migrate *m, int num_migrate,
1784 const struct xfrm_kmaddress *k)
1788 struct xfrm_mgr *km;
1791 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
1793 ret = km->migrate(sel, dir, type, m, num_migrate, k);
1801 EXPORT_SYMBOL(km_migrate);
1804 int km_report(struct net *net, u8 proto, struct xfrm_selector *sel, xfrm_address_t *addr)
1808 struct xfrm_mgr *km;
1811 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
1813 ret = km->report(net, proto, sel, addr);
1821 EXPORT_SYMBOL(km_report);
1823 bool km_is_alive(const struct km_event *c)
1825 struct xfrm_mgr *km;
1826 bool is_alive = false;
1829 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
1830 if (km->is_alive && km->is_alive(c)) {
1839 EXPORT_SYMBOL(km_is_alive);
1841 int xfrm_user_policy(struct sock *sk, int optname, u8 __user *optval, int optlen)
1845 struct xfrm_mgr *km;
1846 struct xfrm_policy *pol = NULL;
1848 if (optlen <= 0 || optlen > PAGE_SIZE)
1851 data = kmalloc(optlen, GFP_KERNEL);
1856 if (copy_from_user(data, optval, optlen))
1861 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
1862 pol = km->compile_policy(sk, optname, data,
1870 xfrm_sk_policy_insert(sk, err, pol);
1879 EXPORT_SYMBOL(xfrm_user_policy);
1881 static DEFINE_SPINLOCK(xfrm_km_lock);
1883 int xfrm_register_km(struct xfrm_mgr *km)
1885 spin_lock_bh(&xfrm_km_lock);
1886 list_add_tail_rcu(&km->list, &xfrm_km_list);
1887 spin_unlock_bh(&xfrm_km_lock);
1890 EXPORT_SYMBOL(xfrm_register_km);
1892 int xfrm_unregister_km(struct xfrm_mgr *km)
1894 spin_lock_bh(&xfrm_km_lock);
1895 list_del_rcu(&km->list);
1896 spin_unlock_bh(&xfrm_km_lock);
1900 EXPORT_SYMBOL(xfrm_unregister_km);
1902 int xfrm_state_register_afinfo(struct xfrm_state_afinfo *afinfo)
1905 if (unlikely(afinfo == NULL))
1907 if (unlikely(afinfo->family >= NPROTO))
1908 return -EAFNOSUPPORT;
1909 spin_lock_bh(&xfrm_state_afinfo_lock);
1910 if (unlikely(xfrm_state_afinfo[afinfo->family] != NULL))
1913 rcu_assign_pointer(xfrm_state_afinfo[afinfo->family], afinfo);
1914 spin_unlock_bh(&xfrm_state_afinfo_lock);
1917 EXPORT_SYMBOL(xfrm_state_register_afinfo);
1919 int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo)
1922 if (unlikely(afinfo == NULL))
1924 if (unlikely(afinfo->family >= NPROTO))
1925 return -EAFNOSUPPORT;
1926 spin_lock_bh(&xfrm_state_afinfo_lock);
1927 if (likely(xfrm_state_afinfo[afinfo->family] != NULL)) {
1928 if (unlikely(xfrm_state_afinfo[afinfo->family] != afinfo))
1931 RCU_INIT_POINTER(xfrm_state_afinfo[afinfo->family], NULL);
1933 spin_unlock_bh(&xfrm_state_afinfo_lock);
1937 EXPORT_SYMBOL(xfrm_state_unregister_afinfo);
1939 struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family)
1941 struct xfrm_state_afinfo *afinfo;
1942 if (unlikely(family >= NPROTO))
1945 afinfo = rcu_dereference(xfrm_state_afinfo[family]);
1946 if (unlikely(!afinfo))
1951 void xfrm_state_put_afinfo(struct xfrm_state_afinfo *afinfo)
1956 /* Temporarily located here until net/xfrm/xfrm_tunnel.c is created */
1957 void xfrm_state_delete_tunnel(struct xfrm_state *x)
1960 struct xfrm_state *t = x->tunnel;
1962 if (atomic_read(&t->tunnel_users) == 2)
1963 xfrm_state_delete(t);
1964 atomic_dec(&t->tunnel_users);
1969 EXPORT_SYMBOL(xfrm_state_delete_tunnel);
1971 int xfrm_state_mtu(struct xfrm_state *x, int mtu)
1975 spin_lock_bh(&x->lock);
1976 if (x->km.state == XFRM_STATE_VALID &&
1977 x->type && x->type->get_mtu)
1978 res = x->type->get_mtu(x, mtu);
1980 res = mtu - x->props.header_len;
1981 spin_unlock_bh(&x->lock);
1985 int __xfrm_init_state(struct xfrm_state *x, bool init_replay)
1987 struct xfrm_state_afinfo *afinfo;
1988 struct xfrm_mode *inner_mode;
1989 int family = x->props.family;
1992 err = -EAFNOSUPPORT;
1993 afinfo = xfrm_state_get_afinfo(family);
1998 if (afinfo->init_flags)
1999 err = afinfo->init_flags(x);
2001 xfrm_state_put_afinfo(afinfo);
2006 err = -EPROTONOSUPPORT;
2008 if (x->sel.family != AF_UNSPEC) {
2009 inner_mode = xfrm_get_mode(x->props.mode, x->sel.family);
2010 if (inner_mode == NULL)
2013 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL) &&
2014 family != x->sel.family) {
2015 xfrm_put_mode(inner_mode);
2019 x->inner_mode = inner_mode;
2021 struct xfrm_mode *inner_mode_iaf;
2022 int iafamily = AF_INET;
2024 inner_mode = xfrm_get_mode(x->props.mode, x->props.family);
2025 if (inner_mode == NULL)
2028 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL)) {
2029 xfrm_put_mode(inner_mode);
2032 x->inner_mode = inner_mode;
2034 if (x->props.family == AF_INET)
2035 iafamily = AF_INET6;
2037 inner_mode_iaf = xfrm_get_mode(x->props.mode, iafamily);
2038 if (inner_mode_iaf) {
2039 if (inner_mode_iaf->flags & XFRM_MODE_FLAG_TUNNEL)
2040 x->inner_mode_iaf = inner_mode_iaf;
2042 xfrm_put_mode(inner_mode_iaf);
2046 x->type = xfrm_get_type(x->id.proto, family);
2047 if (x->type == NULL)
2050 err = x->type->init_state(x);
2054 x->outer_mode = xfrm_get_mode(x->props.mode, family);
2055 if (x->outer_mode == NULL) {
2056 err = -EPROTONOSUPPORT;
2061 err = xfrm_init_replay(x);
2066 x->km.state = XFRM_STATE_VALID;
2072 EXPORT_SYMBOL(__xfrm_init_state);
2074 int xfrm_init_state(struct xfrm_state *x)
2076 return __xfrm_init_state(x, true);
2079 EXPORT_SYMBOL(xfrm_init_state);
2081 int __net_init xfrm_state_init(struct net *net)
2085 INIT_LIST_HEAD(&net->xfrm.state_all);
2087 sz = sizeof(struct hlist_head) * 8;
2089 net->xfrm.state_bydst = xfrm_hash_alloc(sz);
2090 if (!net->xfrm.state_bydst)
2092 net->xfrm.state_bysrc = xfrm_hash_alloc(sz);
2093 if (!net->xfrm.state_bysrc)
2095 net->xfrm.state_byspi = xfrm_hash_alloc(sz);
2096 if (!net->xfrm.state_byspi)
2098 net->xfrm.state_hmask = ((sz / sizeof(struct hlist_head)) - 1);
2100 net->xfrm.state_num = 0;
2101 INIT_WORK(&net->xfrm.state_hash_work, xfrm_hash_resize);
2102 INIT_HLIST_HEAD(&net->xfrm.state_gc_list);
2103 INIT_WORK(&net->xfrm.state_gc_work, xfrm_state_gc_task);
2104 spin_lock_init(&net->xfrm.xfrm_state_lock);
2108 xfrm_hash_free(net->xfrm.state_bysrc, sz);
2110 xfrm_hash_free(net->xfrm.state_bydst, sz);
2115 void xfrm_state_fini(struct net *net)
2119 flush_work(&net->xfrm.state_hash_work);
2120 xfrm_state_flush(net, IPSEC_PROTO_ANY, false);
2121 flush_work(&net->xfrm.state_gc_work);
2123 WARN_ON(!list_empty(&net->xfrm.state_all));
2125 sz = (net->xfrm.state_hmask + 1) * sizeof(struct hlist_head);
2126 WARN_ON(!hlist_empty(net->xfrm.state_byspi));
2127 xfrm_hash_free(net->xfrm.state_byspi, sz);
2128 WARN_ON(!hlist_empty(net->xfrm.state_bysrc));
2129 xfrm_hash_free(net->xfrm.state_bysrc, sz);
2130 WARN_ON(!hlist_empty(net->xfrm.state_bydst));
2131 xfrm_hash_free(net->xfrm.state_bydst, sz);
2134 #ifdef CONFIG_AUDITSYSCALL
2135 static void xfrm_audit_helper_sainfo(struct xfrm_state *x,
2136 struct audit_buffer *audit_buf)
2138 struct xfrm_sec_ctx *ctx = x->security;
2139 u32 spi = ntohl(x->id.spi);
2142 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
2143 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
2145 switch (x->props.family) {
2147 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2148 &x->props.saddr.a4, &x->id.daddr.a4);
2151 audit_log_format(audit_buf, " src=%pI6 dst=%pI6",
2152 x->props.saddr.a6, x->id.daddr.a6);
2156 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
2159 static void xfrm_audit_helper_pktinfo(struct sk_buff *skb, u16 family,
2160 struct audit_buffer *audit_buf)
2162 const struct iphdr *iph4;
2163 const struct ipv6hdr *iph6;
2168 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2169 &iph4->saddr, &iph4->daddr);
2172 iph6 = ipv6_hdr(skb);
2173 audit_log_format(audit_buf,
2174 " src=%pI6 dst=%pI6 flowlbl=0x%x%02x%02x",
2175 &iph6->saddr, &iph6->daddr,
2176 iph6->flow_lbl[0] & 0x0f,
2183 void xfrm_audit_state_add(struct xfrm_state *x, int result, bool task_valid)
2185 struct audit_buffer *audit_buf;
2187 audit_buf = xfrm_audit_start("SAD-add");
2188 if (audit_buf == NULL)
2190 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
2191 xfrm_audit_helper_sainfo(x, audit_buf);
2192 audit_log_format(audit_buf, " res=%u", result);
2193 audit_log_end(audit_buf);
2195 EXPORT_SYMBOL_GPL(xfrm_audit_state_add);
2197 void xfrm_audit_state_delete(struct xfrm_state *x, int result, bool task_valid)
2199 struct audit_buffer *audit_buf;
2201 audit_buf = xfrm_audit_start("SAD-delete");
2202 if (audit_buf == NULL)
2204 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
2205 xfrm_audit_helper_sainfo(x, audit_buf);
2206 audit_log_format(audit_buf, " res=%u", result);
2207 audit_log_end(audit_buf);
2209 EXPORT_SYMBOL_GPL(xfrm_audit_state_delete);
2211 void xfrm_audit_state_replay_overflow(struct xfrm_state *x,
2212 struct sk_buff *skb)
2214 struct audit_buffer *audit_buf;
2217 audit_buf = xfrm_audit_start("SA-replay-overflow");
2218 if (audit_buf == NULL)
2220 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2221 /* don't record the sequence number because it's inherent in this kind
2222 * of audit message */
2223 spi = ntohl(x->id.spi);
2224 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
2225 audit_log_end(audit_buf);
2227 EXPORT_SYMBOL_GPL(xfrm_audit_state_replay_overflow);
2229 void xfrm_audit_state_replay(struct xfrm_state *x,
2230 struct sk_buff *skb, __be32 net_seq)
2232 struct audit_buffer *audit_buf;
2235 audit_buf = xfrm_audit_start("SA-replayed-pkt");
2236 if (audit_buf == NULL)
2238 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2239 spi = ntohl(x->id.spi);
2240 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2241 spi, spi, ntohl(net_seq));
2242 audit_log_end(audit_buf);
2244 EXPORT_SYMBOL_GPL(xfrm_audit_state_replay);
2246 void xfrm_audit_state_notfound_simple(struct sk_buff *skb, u16 family)
2248 struct audit_buffer *audit_buf;
2250 audit_buf = xfrm_audit_start("SA-notfound");
2251 if (audit_buf == NULL)
2253 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2254 audit_log_end(audit_buf);
2256 EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound_simple);
2258 void xfrm_audit_state_notfound(struct sk_buff *skb, u16 family,
2259 __be32 net_spi, __be32 net_seq)
2261 struct audit_buffer *audit_buf;
2264 audit_buf = xfrm_audit_start("SA-notfound");
2265 if (audit_buf == NULL)
2267 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2268 spi = ntohl(net_spi);
2269 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2270 spi, spi, ntohl(net_seq));
2271 audit_log_end(audit_buf);
2273 EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound);
2275 void xfrm_audit_state_icvfail(struct xfrm_state *x,
2276 struct sk_buff *skb, u8 proto)
2278 struct audit_buffer *audit_buf;
2282 audit_buf = xfrm_audit_start("SA-icv-failure");
2283 if (audit_buf == NULL)
2285 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2286 if (xfrm_parse_spi(skb, proto, &net_spi, &net_seq) == 0) {
2287 u32 spi = ntohl(net_spi);
2288 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2289 spi, spi, ntohl(net_seq));
2291 audit_log_end(audit_buf);
2293 EXPORT_SYMBOL_GPL(xfrm_audit_state_icvfail);
2294 #endif /* CONFIG_AUDITSYSCALL */