ALSA: usb-audio: Fix an out-of-bound read in create_composite_quirks
[linux/fpc-iii.git] / net / xfrm / xfrm_state.c
blob13f261feb75c4a91b937733533d23b72c2110819
1 /*
2 * xfrm_state.c
4 * Changes:
5 * Mitsuru KANDA @USAGI
6 * Kazunori MIYAZAWA @USAGI
7 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
8 * IPv6 support
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>
17 #include <net/xfrm.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,
43 u32 reqid,
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;
71 struct xfrm_state *x;
73 hlist_for_each_entry_safe(x, tmp, list, bydst) {
74 unsigned int h;
76 h = __xfrm_dst_hash(&x->id.daddr, &x->props.saddr,
77 x->props.reqid, x->props.family,
78 nhashmask);
79 hlist_add_head(&x->bydst, ndsttable+h);
81 h = __xfrm_src_hash(&x->id.daddr, &x->props.saddr,
82 x->props.family,
83 nhashmask);
84 hlist_add_head(&x->bysrc, nsrctable+h);
86 if (x->id.spi) {
87 h = __xfrm_spi_hash(&x->id.daddr, x->id.spi,
88 x->id.proto, x->props.family,
89 nhashmask);
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;
106 int i;
108 nsize = xfrm_hash_new_size(net->xfrm.state_hmask);
109 ndst = xfrm_hash_alloc(nsize);
110 if (!ndst)
111 return;
112 nsrc = xfrm_hash_alloc(nsize);
113 if (!nsrc) {
114 xfrm_hash_free(ndst, nsize);
115 return;
117 nspi = xfrm_hash_alloc(nsize);
118 if (!nspi) {
119 xfrm_hash_free(ndst, nsize);
120 xfrm_hash_free(nsrc, nsize);
121 return;
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,
129 nhashmask);
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;
165 int err = 0;
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;
174 else
175 err = -EEXIST;
176 spin_unlock_bh(&xfrm_type_lock);
177 xfrm_state_put_afinfo(afinfo);
178 return err;
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;
186 int err = 0;
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))
194 err = -ENOENT;
195 else
196 typemap[type->proto] = NULL;
197 spin_unlock_bh(&xfrm_type_lock);
198 xfrm_state_put_afinfo(afinfo);
199 return err;
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;
210 retry:
211 afinfo = xfrm_state_get_afinfo(family);
212 if (unlikely(afinfo == NULL))
213 return NULL;
214 typemap = afinfo->type_map;
216 type = typemap[proto];
217 if (unlikely(type && !try_module_get(type->owner)))
218 type = NULL;
219 if (!type && !modload_attempted) {
220 xfrm_state_put_afinfo(afinfo);
221 request_module("xfrm-type-%d-%d", family, proto);
222 modload_attempted = 1;
223 goto retry;
226 xfrm_state_put_afinfo(afinfo);
227 return type;
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;
240 int err;
242 if (unlikely(mode->encap >= XFRM_MODE_MAX))
243 return -EINVAL;
245 afinfo = xfrm_state_get_afinfo(family);
246 if (unlikely(afinfo == NULL))
247 return -EAFNOSUPPORT;
249 err = -EEXIST;
250 modemap = afinfo->mode_map;
251 spin_lock_bh(&xfrm_mode_lock);
252 if (modemap[mode->encap])
253 goto out;
255 err = -ENOENT;
256 if (!try_module_get(afinfo->owner))
257 goto out;
259 mode->afinfo = afinfo;
260 modemap[mode->encap] = mode;
261 err = 0;
263 out:
264 spin_unlock_bh(&xfrm_mode_lock);
265 xfrm_state_put_afinfo(afinfo);
266 return err;
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;
274 int err;
276 if (unlikely(mode->encap >= XFRM_MODE_MAX))
277 return -EINVAL;
279 afinfo = xfrm_state_get_afinfo(family);
280 if (unlikely(afinfo == NULL))
281 return -EAFNOSUPPORT;
283 err = -ENOENT;
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);
289 err = 0;
292 spin_unlock_bh(&xfrm_mode_lock);
293 xfrm_state_put_afinfo(afinfo);
294 return err;
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))
305 return NULL;
307 retry:
308 afinfo = xfrm_state_get_afinfo(family);
309 if (unlikely(afinfo == NULL))
310 return NULL;
312 mode = afinfo->mode_map[encap];
313 if (unlikely(mode && !try_module_get(mode->owner)))
314 mode = NULL;
315 if (!mode && !modload_attempted) {
316 xfrm_state_put_afinfo(afinfo);
317 request_module("xfrm-mode-%d-%d", family, encap);
318 modload_attempted = 1;
319 goto retry;
322 xfrm_state_put_afinfo(afinfo);
323 return mode;
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);
335 kfree(x->aalg);
336 kfree(x->ealg);
337 kfree(x->calg);
338 kfree(x->encap);
339 kfree(x->coaddr);
340 kfree(x->replay_esn);
341 kfree(x->preplay_esn);
342 if (x->inner_mode)
343 xfrm_put_mode(x->inner_mode);
344 if (x->inner_mode_iaf)
345 xfrm_put_mode(x->inner_mode_iaf);
346 if (x->outer_mode)
347 xfrm_put_mode(x->outer_mode);
348 if (x->type) {
349 x->type->destructor(x);
350 xfrm_put_type(x->type);
352 security_xfrm_state_free(x);
353 kfree(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;
375 else
376 return secs*HZ;
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;
385 int warn = 0;
386 int err = 0;
388 spin_lock(&x->lock);
389 if (x->km.state == XFRM_STATE_DEAD)
390 goto out;
391 if (x->km.state == XFRM_STATE_EXPIRED)
392 goto expired;
393 if (x->lft.hard_add_expires_seconds) {
394 long tmo = x->lft.hard_add_expires_seconds +
395 x->curlft.add_time - now;
396 if (tmo <= 0) {
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;
404 } else
405 goto expired;
407 if (tmo < next)
408 next = 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;
413 if (tmo <= 0)
414 goto expired;
415 if (tmo < next)
416 next = tmo;
418 if (x->km.dying)
419 goto resched;
420 if (x->lft.soft_add_expires_seconds) {
421 long tmo = x->lft.soft_add_expires_seconds +
422 x->curlft.add_time - now;
423 if (tmo <= 0) {
424 warn = 1;
425 x->xflags &= ~XFRM_SOFT_EXPIRE;
426 } else if (tmo < next) {
427 next = tmo;
428 x->xflags |= XFRM_SOFT_EXPIRE;
429 x->saved_tmo = tmo;
432 if (x->lft.soft_use_expires_seconds) {
433 long tmo = x->lft.soft_use_expires_seconds +
434 (x->curlft.use_time ? : now) - now;
435 if (tmo <= 0)
436 warn = 1;
437 else if (tmo < next)
438 next = tmo;
441 x->km.dying = warn;
442 if (warn)
443 km_state_expired(x, 0, 0);
444 resched:
445 if (next != LONG_MAX) {
446 tasklet_hrtimer_start(&x->mtimer, ktime_set(next, 0), HRTIMER_MODE_REL);
449 goto out;
451 expired:
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);
456 if (!err)
457 km_state_expired(x, 1, 0);
459 xfrm_audit_state_delete(x, err ? 0 : 1, true);
461 out:
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);
474 if (x) {
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,
485 (unsigned long)x);
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);
497 return x;
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);
517 int err = -ESRCH;
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);
525 if (x->id.spi)
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.
534 xfrm_state_put(x);
535 err = 0;
538 return err;
540 EXPORT_SYMBOL(__xfrm_state_delete);
542 int xfrm_state_delete(struct xfrm_state *x)
544 int err;
546 spin_lock_bh(&x->lock);
547 err = __xfrm_state_delete(x);
548 spin_unlock_bh(&x->lock);
550 return err;
552 EXPORT_SYMBOL(xfrm_state_delete);
554 #ifdef CONFIG_SECURITY_NETWORK_XFRM
555 static inline int
556 xfrm_state_flush_secctx_check(struct net *net, u8 proto, bool task_valid)
558 int i, err = 0;
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);
567 return err;
572 return err;
574 #else
575 static inline int
576 xfrm_state_flush_secctx_check(struct net *net, u8 proto, bool task_valid)
578 return 0;
580 #endif
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);
588 if (err)
589 goto out;
591 err = -ESRCH;
592 for (i = 0; i <= net->xfrm.state_hmask; i++) {
593 struct xfrm_state *x;
594 restart:
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)) {
598 xfrm_state_hold(x);
599 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
601 err = xfrm_state_delete(x);
602 xfrm_audit_state_delete(x, err ? 0 : 1,
603 task_valid);
604 xfrm_state_put(x);
605 if (!err)
606 cnt++;
608 spin_lock_bh(&net->xfrm.xfrm_state_lock);
609 goto restart;
613 if (cnt)
614 err = 0;
616 out:
617 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
618 return err;
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 + 1;
627 si->sadhmcnt = xfrm_state_hashmax;
628 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
630 EXPORT_SYMBOL(xfrm_sad_getinfo);
632 static int
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);
639 if (!afinfo)
640 return -1;
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);
646 if (!afinfo)
647 return -1;
649 afinfo->init_temprop(x, tmpl, daddr, saddr);
650 xfrm_state_put_afinfo(afinfo);
651 return 0;
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 ||
664 x->id.spi != spi ||
665 x->id.proto != proto ||
666 !xfrm_addr_equal(&x->id.daddr, daddr, family))
667 continue;
669 if ((mark & x->mark.m) != x->mark.v)
670 continue;
671 xfrm_state_hold(x);
672 return x;
675 return NULL;
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))
691 continue;
693 if ((mark & x->mark.m) != x->mark.v)
694 continue;
695 xfrm_state_hold(x);
696 return x;
699 return NULL;
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;
708 if (use_spi)
709 return __xfrm_state_lookup(net, mark, &x->id.daddr,
710 x->id.spi, x->id.proto, family);
711 else
712 return __xfrm_state_lookup_byaddr(net, mark,
713 &x->id.daddr,
714 &x->props.saddr,
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,
729 int *error)
731 /* Resolution logic:
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
740 * selector.
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))
746 return;
748 if (!*best ||
749 (*best)->km.dying > x->km.dying ||
750 ((*best)->km.dying == x->km.dying &&
751 (*best)->curlft.add_time < x->curlft.add_time))
752 *best = x;
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))
759 *error = -ESRCH;
763 struct xfrm_state *
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;
774 int error = 0;
775 struct xfrm_state *best = NULL;
776 u32 mark = pol->mark.v & pol->mark.m;
777 unsigned short encap_family = tmpl->encap_family;
778 struct km_event c;
780 to_put = NULL;
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)
797 goto found;
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);
813 found:
814 x = best;
815 if (!x && !error && !acquire_in_progress) {
816 if (tmpl->id.spi &&
817 (x0 = __xfrm_state_lookup(net, mark, daddr, tmpl->id.spi,
818 tmpl->id.proto, encap_family)) != NULL) {
819 to_put = x0;
820 error = -EEXIST;
821 goto out;
824 c.net = net;
825 /* If the KMs have no listeners (yet...), avoid allocating an SA
826 * for each and every packet - garbage collection might not
827 * handle the flood.
829 if (!km_is_alive(&c)) {
830 error = -ESRCH;
831 goto out;
834 x = xfrm_state_alloc(net);
835 if (x == NULL) {
836 error = -ENOMEM;
837 goto out;
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);
845 if (error) {
846 x->km.state = XFRM_STATE_DEAD;
847 to_put = x;
848 x = NULL;
849 goto out;
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);
858 if (x->id.spi) {
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);
866 } else {
867 x->km.state = XFRM_STATE_DEAD;
868 to_put = x;
869 x = NULL;
870 error = -ESRCH;
873 out:
874 if (x)
875 xfrm_state_hold(x);
876 else
877 *err = acquire_in_progress ? -EAGAIN : error;
878 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
879 if (to_put)
880 xfrm_state_put(to_put);
881 return x;
884 struct xfrm_state *
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)
889 unsigned int h;
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) {
903 rx = x;
904 break;
908 if (rx)
909 xfrm_state_hold(rx);
910 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
913 return rx;
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 ||
927 x->id.spi != spi)
928 continue;
930 xfrm_state_hold(x);
931 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
932 return x;
934 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
935 return NULL;
937 EXPORT_SYMBOL(xfrm_state_lookup_byspi);
939 static void __xfrm_state_insert(struct xfrm_state *x)
941 struct net *net = xs_net(x);
942 unsigned int h;
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);
953 if (x->id.spi) {
954 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto,
955 x->props.family);
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;
976 unsigned int h;
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))
986 x->genid++;
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,
1008 int create)
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 ||
1019 x->id.spi != 0 ||
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))
1024 continue;
1026 xfrm_state_hold(x);
1027 return x;
1030 if (!create)
1031 return NULL;
1033 x = xfrm_state_alloc(net);
1034 if (likely(x)) {
1035 switch (family) {
1036 case AF_INET:
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;
1043 break;
1045 case AF_INET6:
1046 x->sel.daddr.in6 = daddr->in6;
1047 x->sel.saddr.in6 = saddr->in6;
1048 x->sel.prefixlen_d = 128;
1049 x->sel.prefixlen_s = 128;
1050 x->props.saddr.in6 = saddr->in6;
1051 x->id.daddr.in6 = daddr->in6;
1052 break;
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;
1060 x->mark.v = m->v;
1061 x->mark.m = m->m;
1062 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
1063 xfrm_state_hold(x);
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);
1075 return x;
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;
1084 int family;
1085 int err;
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;
1091 to_put = NULL;
1093 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1095 x1 = __xfrm_state_locate(x, use_spi, family);
1096 if (x1) {
1097 to_put = x1;
1098 x1 = NULL;
1099 err = -EEXIST;
1100 goto out;
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))) {
1107 to_put = x1;
1108 x1 = NULL;
1112 if (use_spi && !x1)
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);
1119 err = 0;
1121 out:
1122 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1124 if (x1) {
1125 xfrm_state_delete(x1);
1126 xfrm_state_put(x1);
1129 if (to_put)
1130 xfrm_state_put(to_put);
1132 return err;
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);
1141 if (!x)
1142 goto out;
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;
1153 if (orig->aalg) {
1154 x->aalg = xfrm_algo_auth_clone(orig->aalg);
1155 if (!x->aalg)
1156 goto error;
1158 x->props.aalgo = orig->props.aalgo;
1160 if (orig->aead) {
1161 x->aead = xfrm_algo_aead_clone(orig->aead);
1162 x->geniv = orig->geniv;
1163 if (!x->aead)
1164 goto error;
1166 if (orig->ealg) {
1167 x->ealg = xfrm_algo_clone(orig->ealg);
1168 if (!x->ealg)
1169 goto error;
1171 x->props.ealgo = orig->props.ealgo;
1173 if (orig->calg) {
1174 x->calg = xfrm_algo_clone(orig->calg);
1175 if (!x->calg)
1176 goto error;
1178 x->props.calgo = orig->props.calgo;
1180 if (orig->encap) {
1181 x->encap = kmemdup(orig->encap, sizeof(*x->encap), GFP_KERNEL);
1182 if (!x->encap)
1183 goto error;
1186 if (orig->coaddr) {
1187 x->coaddr = kmemdup(orig->coaddr, sizeof(*x->coaddr),
1188 GFP_KERNEL);
1189 if (!x->coaddr)
1190 goto error;
1193 if (orig->replay_esn) {
1194 if (xfrm_replay_clone(x, orig))
1195 goto error;
1198 memcpy(&x->mark, &orig->mark, sizeof(x->mark));
1200 if (xfrm_init_state(x) < 0)
1201 goto error;
1203 x->props.flags = orig->props.flags;
1204 x->props.extra_flags = orig->props.extra_flags;
1206 x->tfcpad = orig->tfcpad;
1207 x->replay_maxdiff = orig->replay_maxdiff;
1208 x->replay_maxage = orig->replay_maxage;
1209 x->curlft.add_time = orig->curlft.add_time;
1210 x->km.state = orig->km.state;
1211 x->km.seq = orig->km.seq;
1212 x->replay = orig->replay;
1213 x->preplay = orig->preplay;
1215 return x;
1217 error:
1218 xfrm_state_put(x);
1219 out:
1220 return NULL;
1223 struct xfrm_state *xfrm_migrate_state_find(struct xfrm_migrate *m, struct net *net)
1225 unsigned int h;
1226 struct xfrm_state *x = NULL;
1228 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1230 if (m->reqid) {
1231 h = xfrm_dst_hash(net, &m->old_daddr, &m->old_saddr,
1232 m->reqid, m->old_family);
1233 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
1234 if (x->props.mode != m->mode ||
1235 x->id.proto != m->proto)
1236 continue;
1237 if (m->reqid && x->props.reqid != m->reqid)
1238 continue;
1239 if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr,
1240 m->old_family) ||
1241 !xfrm_addr_equal(&x->props.saddr, &m->old_saddr,
1242 m->old_family))
1243 continue;
1244 xfrm_state_hold(x);
1245 break;
1247 } else {
1248 h = xfrm_src_hash(net, &m->old_daddr, &m->old_saddr,
1249 m->old_family);
1250 hlist_for_each_entry(x, net->xfrm.state_bysrc+h, bysrc) {
1251 if (x->props.mode != m->mode ||
1252 x->id.proto != m->proto)
1253 continue;
1254 if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr,
1255 m->old_family) ||
1256 !xfrm_addr_equal(&x->props.saddr, &m->old_saddr,
1257 m->old_family))
1258 continue;
1259 xfrm_state_hold(x);
1260 break;
1264 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1266 return x;
1268 EXPORT_SYMBOL(xfrm_migrate_state_find);
1270 struct xfrm_state *xfrm_state_migrate(struct xfrm_state *x,
1271 struct xfrm_migrate *m)
1273 struct xfrm_state *xc;
1275 xc = xfrm_state_clone(x);
1276 if (!xc)
1277 return NULL;
1279 memcpy(&xc->id.daddr, &m->new_daddr, sizeof(xc->id.daddr));
1280 memcpy(&xc->props.saddr, &m->new_saddr, sizeof(xc->props.saddr));
1282 /* add state */
1283 if (xfrm_addr_equal(&x->id.daddr, &m->new_daddr, m->new_family)) {
1284 /* a care is needed when the destination address of the
1285 state is to be updated as it is a part of triplet */
1286 xfrm_state_insert(xc);
1287 } else {
1288 if (xfrm_state_add(xc) < 0)
1289 goto error;
1292 return xc;
1293 error:
1294 xfrm_state_put(xc);
1295 return NULL;
1297 EXPORT_SYMBOL(xfrm_state_migrate);
1298 #endif
1300 int xfrm_state_update(struct xfrm_state *x)
1302 struct xfrm_state *x1, *to_put;
1303 int err;
1304 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
1305 struct net *net = xs_net(x);
1307 to_put = NULL;
1309 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1310 x1 = __xfrm_state_locate(x, use_spi, x->props.family);
1312 err = -ESRCH;
1313 if (!x1)
1314 goto out;
1316 if (xfrm_state_kern(x1)) {
1317 to_put = x1;
1318 err = -EEXIST;
1319 goto out;
1322 if (x1->km.state == XFRM_STATE_ACQ) {
1323 __xfrm_state_insert(x);
1324 x = NULL;
1326 err = 0;
1328 out:
1329 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1331 if (to_put)
1332 xfrm_state_put(to_put);
1334 if (err)
1335 return err;
1337 if (!x) {
1338 xfrm_state_delete(x1);
1339 xfrm_state_put(x1);
1340 return 0;
1343 err = -EINVAL;
1344 spin_lock_bh(&x1->lock);
1345 if (likely(x1->km.state == XFRM_STATE_VALID)) {
1346 if (x->encap && x1->encap)
1347 memcpy(x1->encap, x->encap, sizeof(*x1->encap));
1348 if (x->coaddr && x1->coaddr) {
1349 memcpy(x1->coaddr, x->coaddr, sizeof(*x1->coaddr));
1351 if (!use_spi && memcmp(&x1->sel, &x->sel, sizeof(x1->sel)))
1352 memcpy(&x1->sel, &x->sel, sizeof(x1->sel));
1353 memcpy(&x1->lft, &x->lft, sizeof(x1->lft));
1354 x1->km.dying = 0;
1356 tasklet_hrtimer_start(&x1->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL);
1357 if (x1->curlft.use_time)
1358 xfrm_state_check_expire(x1);
1360 err = 0;
1361 x->km.state = XFRM_STATE_DEAD;
1362 __xfrm_state_put(x);
1364 spin_unlock_bh(&x1->lock);
1366 xfrm_state_put(x1);
1368 return err;
1370 EXPORT_SYMBOL(xfrm_state_update);
1372 int xfrm_state_check_expire(struct xfrm_state *x)
1374 if (!x->curlft.use_time)
1375 x->curlft.use_time = get_seconds();
1377 if (x->curlft.bytes >= x->lft.hard_byte_limit ||
1378 x->curlft.packets >= x->lft.hard_packet_limit) {
1379 x->km.state = XFRM_STATE_EXPIRED;
1380 tasklet_hrtimer_start(&x->mtimer, ktime_set(0, 0), HRTIMER_MODE_REL);
1381 return -EINVAL;
1384 if (!x->km.dying &&
1385 (x->curlft.bytes >= x->lft.soft_byte_limit ||
1386 x->curlft.packets >= x->lft.soft_packet_limit)) {
1387 x->km.dying = 1;
1388 km_state_expired(x, 0, 0);
1390 return 0;
1392 EXPORT_SYMBOL(xfrm_state_check_expire);
1394 struct xfrm_state *
1395 xfrm_state_lookup(struct net *net, u32 mark, const xfrm_address_t *daddr, __be32 spi,
1396 u8 proto, unsigned short family)
1398 struct xfrm_state *x;
1400 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1401 x = __xfrm_state_lookup(net, mark, daddr, spi, proto, family);
1402 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1403 return x;
1405 EXPORT_SYMBOL(xfrm_state_lookup);
1407 struct xfrm_state *
1408 xfrm_state_lookup_byaddr(struct net *net, u32 mark,
1409 const xfrm_address_t *daddr, const xfrm_address_t *saddr,
1410 u8 proto, unsigned short family)
1412 struct xfrm_state *x;
1414 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1415 x = __xfrm_state_lookup_byaddr(net, mark, daddr, saddr, proto, family);
1416 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1417 return x;
1419 EXPORT_SYMBOL(xfrm_state_lookup_byaddr);
1421 struct xfrm_state *
1422 xfrm_find_acq(struct net *net, const struct xfrm_mark *mark, u8 mode, u32 reqid,
1423 u8 proto, const xfrm_address_t *daddr,
1424 const xfrm_address_t *saddr, int create, unsigned short family)
1426 struct xfrm_state *x;
1428 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1429 x = __find_acq_core(net, mark, family, mode, reqid, proto, daddr, saddr, create);
1430 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1432 return x;
1434 EXPORT_SYMBOL(xfrm_find_acq);
1436 #ifdef CONFIG_XFRM_SUB_POLICY
1438 xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n,
1439 unsigned short family, struct net *net)
1441 int err = 0;
1442 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
1443 if (!afinfo)
1444 return -EAFNOSUPPORT;
1446 spin_lock_bh(&net->xfrm.xfrm_state_lock); /*FIXME*/
1447 if (afinfo->tmpl_sort)
1448 err = afinfo->tmpl_sort(dst, src, n);
1449 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1450 xfrm_state_put_afinfo(afinfo);
1451 return err;
1453 EXPORT_SYMBOL(xfrm_tmpl_sort);
1456 xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src, int n,
1457 unsigned short family)
1459 int err = 0;
1460 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
1461 struct net *net = xs_net(*src);
1463 if (!afinfo)
1464 return -EAFNOSUPPORT;
1466 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1467 if (afinfo->state_sort)
1468 err = afinfo->state_sort(dst, src, n);
1469 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1470 xfrm_state_put_afinfo(afinfo);
1471 return err;
1473 EXPORT_SYMBOL(xfrm_state_sort);
1474 #endif
1476 /* Silly enough, but I'm lazy to build resolution list */
1478 static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
1480 int i;
1482 for (i = 0; i <= net->xfrm.state_hmask; i++) {
1483 struct xfrm_state *x;
1485 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
1486 if (x->km.seq == seq &&
1487 (mark & x->mark.m) == x->mark.v &&
1488 x->km.state == XFRM_STATE_ACQ) {
1489 xfrm_state_hold(x);
1490 return x;
1494 return NULL;
1497 struct xfrm_state *xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
1499 struct xfrm_state *x;
1501 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1502 x = __xfrm_find_acq_byseq(net, mark, seq);
1503 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1504 return x;
1506 EXPORT_SYMBOL(xfrm_find_acq_byseq);
1508 u32 xfrm_get_acqseq(void)
1510 u32 res;
1511 static atomic_t acqseq;
1513 do {
1514 res = atomic_inc_return(&acqseq);
1515 } while (!res);
1517 return res;
1519 EXPORT_SYMBOL(xfrm_get_acqseq);
1521 int verify_spi_info(u8 proto, u32 min, u32 max)
1523 switch (proto) {
1524 case IPPROTO_AH:
1525 case IPPROTO_ESP:
1526 break;
1528 case IPPROTO_COMP:
1529 /* IPCOMP spi is 16-bits. */
1530 if (max >= 0x10000)
1531 return -EINVAL;
1532 break;
1534 default:
1535 return -EINVAL;
1538 if (min > max)
1539 return -EINVAL;
1541 return 0;
1543 EXPORT_SYMBOL(verify_spi_info);
1545 int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high)
1547 struct net *net = xs_net(x);
1548 unsigned int h;
1549 struct xfrm_state *x0;
1550 int err = -ENOENT;
1551 __be32 minspi = htonl(low);
1552 __be32 maxspi = htonl(high);
1553 u32 mark = x->mark.v & x->mark.m;
1555 spin_lock_bh(&x->lock);
1556 if (x->km.state == XFRM_STATE_DEAD)
1557 goto unlock;
1559 err = 0;
1560 if (x->id.spi)
1561 goto unlock;
1563 err = -ENOENT;
1565 if (minspi == maxspi) {
1566 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, minspi, x->id.proto, x->props.family);
1567 if (x0) {
1568 xfrm_state_put(x0);
1569 goto unlock;
1571 x->id.spi = minspi;
1572 } else {
1573 u32 spi = 0;
1574 for (h = 0; h < high-low+1; h++) {
1575 spi = low + prandom_u32()%(high-low+1);
1576 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, htonl(spi), x->id.proto, x->props.family);
1577 if (x0 == NULL) {
1578 x->id.spi = htonl(spi);
1579 break;
1581 xfrm_state_put(x0);
1584 if (x->id.spi) {
1585 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1586 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, x->props.family);
1587 hlist_add_head(&x->byspi, net->xfrm.state_byspi+h);
1588 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1590 err = 0;
1593 unlock:
1594 spin_unlock_bh(&x->lock);
1596 return err;
1598 EXPORT_SYMBOL(xfrm_alloc_spi);
1600 static bool __xfrm_state_filter_match(struct xfrm_state *x,
1601 struct xfrm_address_filter *filter)
1603 if (filter) {
1604 if ((filter->family == AF_INET ||
1605 filter->family == AF_INET6) &&
1606 x->props.family != filter->family)
1607 return false;
1609 return addr_match(&x->props.saddr, &filter->saddr,
1610 filter->splen) &&
1611 addr_match(&x->id.daddr, &filter->daddr,
1612 filter->dplen);
1614 return true;
1617 int xfrm_state_walk(struct net *net, struct xfrm_state_walk *walk,
1618 int (*func)(struct xfrm_state *, int, void*),
1619 void *data)
1621 struct xfrm_state *state;
1622 struct xfrm_state_walk *x;
1623 int err = 0;
1625 if (walk->seq != 0 && list_empty(&walk->all))
1626 return 0;
1628 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1629 if (list_empty(&walk->all))
1630 x = list_first_entry(&net->xfrm.state_all, struct xfrm_state_walk, all);
1631 else
1632 x = list_first_entry(&walk->all, struct xfrm_state_walk, all);
1633 list_for_each_entry_from(x, &net->xfrm.state_all, all) {
1634 if (x->state == XFRM_STATE_DEAD)
1635 continue;
1636 state = container_of(x, struct xfrm_state, km);
1637 if (!xfrm_id_proto_match(state->id.proto, walk->proto))
1638 continue;
1639 if (!__xfrm_state_filter_match(state, walk->filter))
1640 continue;
1641 err = func(state, walk->seq, data);
1642 if (err) {
1643 list_move_tail(&walk->all, &x->all);
1644 goto out;
1646 walk->seq++;
1648 if (walk->seq == 0) {
1649 err = -ENOENT;
1650 goto out;
1652 list_del_init(&walk->all);
1653 out:
1654 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1655 return err;
1657 EXPORT_SYMBOL(xfrm_state_walk);
1659 void xfrm_state_walk_init(struct xfrm_state_walk *walk, u8 proto,
1660 struct xfrm_address_filter *filter)
1662 INIT_LIST_HEAD(&walk->all);
1663 walk->proto = proto;
1664 walk->state = XFRM_STATE_DEAD;
1665 walk->seq = 0;
1666 walk->filter = filter;
1668 EXPORT_SYMBOL(xfrm_state_walk_init);
1670 void xfrm_state_walk_done(struct xfrm_state_walk *walk, struct net *net)
1672 kfree(walk->filter);
1674 if (list_empty(&walk->all))
1675 return;
1677 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1678 list_del(&walk->all);
1679 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1681 EXPORT_SYMBOL(xfrm_state_walk_done);
1683 static void xfrm_replay_timer_handler(unsigned long data)
1685 struct xfrm_state *x = (struct xfrm_state *)data;
1687 spin_lock(&x->lock);
1689 if (x->km.state == XFRM_STATE_VALID) {
1690 if (xfrm_aevent_is_on(xs_net(x)))
1691 x->repl->notify(x, XFRM_REPLAY_TIMEOUT);
1692 else
1693 x->xflags |= XFRM_TIME_DEFER;
1696 spin_unlock(&x->lock);
1699 static LIST_HEAD(xfrm_km_list);
1701 void km_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
1703 struct xfrm_mgr *km;
1705 rcu_read_lock();
1706 list_for_each_entry_rcu(km, &xfrm_km_list, list)
1707 if (km->notify_policy)
1708 km->notify_policy(xp, dir, c);
1709 rcu_read_unlock();
1712 void km_state_notify(struct xfrm_state *x, const struct km_event *c)
1714 struct xfrm_mgr *km;
1715 rcu_read_lock();
1716 list_for_each_entry_rcu(km, &xfrm_km_list, list)
1717 if (km->notify)
1718 km->notify(x, c);
1719 rcu_read_unlock();
1722 EXPORT_SYMBOL(km_policy_notify);
1723 EXPORT_SYMBOL(km_state_notify);
1725 void km_state_expired(struct xfrm_state *x, int hard, u32 portid)
1727 struct km_event c;
1729 c.data.hard = hard;
1730 c.portid = portid;
1731 c.event = XFRM_MSG_EXPIRE;
1732 km_state_notify(x, &c);
1735 EXPORT_SYMBOL(km_state_expired);
1737 * We send to all registered managers regardless of failure
1738 * We are happy with one success
1740 int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol)
1742 int err = -EINVAL, acqret;
1743 struct xfrm_mgr *km;
1745 rcu_read_lock();
1746 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
1747 acqret = km->acquire(x, t, pol);
1748 if (!acqret)
1749 err = acqret;
1751 rcu_read_unlock();
1752 return err;
1754 EXPORT_SYMBOL(km_query);
1756 int km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
1758 int err = -EINVAL;
1759 struct xfrm_mgr *km;
1761 rcu_read_lock();
1762 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
1763 if (km->new_mapping)
1764 err = km->new_mapping(x, ipaddr, sport);
1765 if (!err)
1766 break;
1768 rcu_read_unlock();
1769 return err;
1771 EXPORT_SYMBOL(km_new_mapping);
1773 void km_policy_expired(struct xfrm_policy *pol, int dir, int hard, u32 portid)
1775 struct km_event c;
1777 c.data.hard = hard;
1778 c.portid = portid;
1779 c.event = XFRM_MSG_POLEXPIRE;
1780 km_policy_notify(pol, dir, &c);
1782 EXPORT_SYMBOL(km_policy_expired);
1784 #ifdef CONFIG_XFRM_MIGRATE
1785 int km_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
1786 const struct xfrm_migrate *m, int num_migrate,
1787 const struct xfrm_kmaddress *k)
1789 int err = -EINVAL;
1790 int ret;
1791 struct xfrm_mgr *km;
1793 rcu_read_lock();
1794 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
1795 if (km->migrate) {
1796 ret = km->migrate(sel, dir, type, m, num_migrate, k);
1797 if (!ret)
1798 err = ret;
1801 rcu_read_unlock();
1802 return err;
1804 EXPORT_SYMBOL(km_migrate);
1805 #endif
1807 int km_report(struct net *net, u8 proto, struct xfrm_selector *sel, xfrm_address_t *addr)
1809 int err = -EINVAL;
1810 int ret;
1811 struct xfrm_mgr *km;
1813 rcu_read_lock();
1814 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
1815 if (km->report) {
1816 ret = km->report(net, proto, sel, addr);
1817 if (!ret)
1818 err = ret;
1821 rcu_read_unlock();
1822 return err;
1824 EXPORT_SYMBOL(km_report);
1826 bool km_is_alive(const struct km_event *c)
1828 struct xfrm_mgr *km;
1829 bool is_alive = false;
1831 rcu_read_lock();
1832 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
1833 if (km->is_alive && km->is_alive(c)) {
1834 is_alive = true;
1835 break;
1838 rcu_read_unlock();
1840 return is_alive;
1842 EXPORT_SYMBOL(km_is_alive);
1844 int xfrm_user_policy(struct sock *sk, int optname, u8 __user *optval, int optlen)
1846 int err;
1847 u8 *data;
1848 struct xfrm_mgr *km;
1849 struct xfrm_policy *pol = NULL;
1851 #ifdef CONFIG_COMPAT
1852 if (is_compat_task())
1853 return -EOPNOTSUPP;
1854 #endif
1856 if (!optval && !optlen) {
1857 xfrm_sk_policy_insert(sk, XFRM_POLICY_IN, NULL);
1858 xfrm_sk_policy_insert(sk, XFRM_POLICY_OUT, NULL);
1859 __sk_dst_reset(sk);
1860 return 0;
1863 if (optlen <= 0 || optlen > PAGE_SIZE)
1864 return -EMSGSIZE;
1866 data = kmalloc(optlen, GFP_KERNEL);
1867 if (!data)
1868 return -ENOMEM;
1870 err = -EFAULT;
1871 if (copy_from_user(data, optval, optlen))
1872 goto out;
1874 err = -EINVAL;
1875 rcu_read_lock();
1876 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
1877 pol = km->compile_policy(sk, optname, data,
1878 optlen, &err);
1879 if (err >= 0)
1880 break;
1882 rcu_read_unlock();
1884 if (err >= 0) {
1885 xfrm_sk_policy_insert(sk, err, pol);
1886 xfrm_pol_put(pol);
1887 __sk_dst_reset(sk);
1888 err = 0;
1891 out:
1892 kfree(data);
1893 return err;
1895 EXPORT_SYMBOL(xfrm_user_policy);
1897 static DEFINE_SPINLOCK(xfrm_km_lock);
1899 int xfrm_register_km(struct xfrm_mgr *km)
1901 spin_lock_bh(&xfrm_km_lock);
1902 list_add_tail_rcu(&km->list, &xfrm_km_list);
1903 spin_unlock_bh(&xfrm_km_lock);
1904 return 0;
1906 EXPORT_SYMBOL(xfrm_register_km);
1908 int xfrm_unregister_km(struct xfrm_mgr *km)
1910 spin_lock_bh(&xfrm_km_lock);
1911 list_del_rcu(&km->list);
1912 spin_unlock_bh(&xfrm_km_lock);
1913 synchronize_rcu();
1914 return 0;
1916 EXPORT_SYMBOL(xfrm_unregister_km);
1918 int xfrm_state_register_afinfo(struct xfrm_state_afinfo *afinfo)
1920 int err = 0;
1921 if (unlikely(afinfo == NULL))
1922 return -EINVAL;
1923 if (unlikely(afinfo->family >= NPROTO))
1924 return -EAFNOSUPPORT;
1925 spin_lock_bh(&xfrm_state_afinfo_lock);
1926 if (unlikely(xfrm_state_afinfo[afinfo->family] != NULL))
1927 err = -EEXIST;
1928 else
1929 rcu_assign_pointer(xfrm_state_afinfo[afinfo->family], afinfo);
1930 spin_unlock_bh(&xfrm_state_afinfo_lock);
1931 return err;
1933 EXPORT_SYMBOL(xfrm_state_register_afinfo);
1935 int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo)
1937 int err = 0;
1938 if (unlikely(afinfo == NULL))
1939 return -EINVAL;
1940 if (unlikely(afinfo->family >= NPROTO))
1941 return -EAFNOSUPPORT;
1942 spin_lock_bh(&xfrm_state_afinfo_lock);
1943 if (likely(xfrm_state_afinfo[afinfo->family] != NULL)) {
1944 if (unlikely(xfrm_state_afinfo[afinfo->family] != afinfo))
1945 err = -EINVAL;
1946 else
1947 RCU_INIT_POINTER(xfrm_state_afinfo[afinfo->family], NULL);
1949 spin_unlock_bh(&xfrm_state_afinfo_lock);
1950 synchronize_rcu();
1951 return err;
1953 EXPORT_SYMBOL(xfrm_state_unregister_afinfo);
1955 struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family)
1957 struct xfrm_state_afinfo *afinfo;
1958 if (unlikely(family >= NPROTO))
1959 return NULL;
1960 rcu_read_lock();
1961 afinfo = rcu_dereference(xfrm_state_afinfo[family]);
1962 if (unlikely(!afinfo))
1963 rcu_read_unlock();
1964 return afinfo;
1967 void xfrm_state_put_afinfo(struct xfrm_state_afinfo *afinfo)
1969 rcu_read_unlock();
1972 /* Temporarily located here until net/xfrm/xfrm_tunnel.c is created */
1973 void xfrm_state_delete_tunnel(struct xfrm_state *x)
1975 if (x->tunnel) {
1976 struct xfrm_state *t = x->tunnel;
1978 if (atomic_read(&t->tunnel_users) == 2)
1979 xfrm_state_delete(t);
1980 atomic_dec(&t->tunnel_users);
1981 xfrm_state_put(t);
1982 x->tunnel = NULL;
1985 EXPORT_SYMBOL(xfrm_state_delete_tunnel);
1987 int xfrm_state_mtu(struct xfrm_state *x, int mtu)
1989 int res;
1991 spin_lock_bh(&x->lock);
1992 if (x->km.state == XFRM_STATE_VALID &&
1993 x->type && x->type->get_mtu)
1994 res = x->type->get_mtu(x, mtu);
1995 else
1996 res = mtu - x->props.header_len;
1997 spin_unlock_bh(&x->lock);
1998 return res;
2001 int __xfrm_init_state(struct xfrm_state *x, bool init_replay)
2003 struct xfrm_state_afinfo *afinfo;
2004 struct xfrm_mode *inner_mode;
2005 int family = x->props.family;
2006 int err;
2008 err = -EAFNOSUPPORT;
2009 afinfo = xfrm_state_get_afinfo(family);
2010 if (!afinfo)
2011 goto error;
2013 err = 0;
2014 if (afinfo->init_flags)
2015 err = afinfo->init_flags(x);
2017 xfrm_state_put_afinfo(afinfo);
2019 if (err)
2020 goto error;
2022 err = -EPROTONOSUPPORT;
2024 if (x->sel.family != AF_UNSPEC) {
2025 inner_mode = xfrm_get_mode(x->props.mode, x->sel.family);
2026 if (inner_mode == NULL)
2027 goto error;
2029 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL) &&
2030 family != x->sel.family) {
2031 xfrm_put_mode(inner_mode);
2032 goto error;
2035 x->inner_mode = inner_mode;
2036 } else {
2037 struct xfrm_mode *inner_mode_iaf;
2038 int iafamily = AF_INET;
2040 inner_mode = xfrm_get_mode(x->props.mode, x->props.family);
2041 if (inner_mode == NULL)
2042 goto error;
2044 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL)) {
2045 xfrm_put_mode(inner_mode);
2046 goto error;
2048 x->inner_mode = inner_mode;
2050 if (x->props.family == AF_INET)
2051 iafamily = AF_INET6;
2053 inner_mode_iaf = xfrm_get_mode(x->props.mode, iafamily);
2054 if (inner_mode_iaf) {
2055 if (inner_mode_iaf->flags & XFRM_MODE_FLAG_TUNNEL)
2056 x->inner_mode_iaf = inner_mode_iaf;
2057 else
2058 xfrm_put_mode(inner_mode_iaf);
2062 x->type = xfrm_get_type(x->id.proto, family);
2063 if (x->type == NULL)
2064 goto error;
2066 err = x->type->init_state(x);
2067 if (err)
2068 goto error;
2070 x->outer_mode = xfrm_get_mode(x->props.mode, family);
2071 if (x->outer_mode == NULL) {
2072 err = -EPROTONOSUPPORT;
2073 goto error;
2076 if (init_replay) {
2077 err = xfrm_init_replay(x);
2078 if (err)
2079 goto error;
2082 x->km.state = XFRM_STATE_VALID;
2084 error:
2085 return err;
2088 EXPORT_SYMBOL(__xfrm_init_state);
2090 int xfrm_init_state(struct xfrm_state *x)
2092 return __xfrm_init_state(x, true);
2095 EXPORT_SYMBOL(xfrm_init_state);
2097 int __net_init xfrm_state_init(struct net *net)
2099 unsigned int sz;
2101 INIT_LIST_HEAD(&net->xfrm.state_all);
2103 sz = sizeof(struct hlist_head) * 8;
2105 net->xfrm.state_bydst = xfrm_hash_alloc(sz);
2106 if (!net->xfrm.state_bydst)
2107 goto out_bydst;
2108 net->xfrm.state_bysrc = xfrm_hash_alloc(sz);
2109 if (!net->xfrm.state_bysrc)
2110 goto out_bysrc;
2111 net->xfrm.state_byspi = xfrm_hash_alloc(sz);
2112 if (!net->xfrm.state_byspi)
2113 goto out_byspi;
2114 net->xfrm.state_hmask = ((sz / sizeof(struct hlist_head)) - 1);
2116 net->xfrm.state_num = 0;
2117 INIT_WORK(&net->xfrm.state_hash_work, xfrm_hash_resize);
2118 INIT_HLIST_HEAD(&net->xfrm.state_gc_list);
2119 INIT_WORK(&net->xfrm.state_gc_work, xfrm_state_gc_task);
2120 spin_lock_init(&net->xfrm.xfrm_state_lock);
2121 return 0;
2123 out_byspi:
2124 xfrm_hash_free(net->xfrm.state_bysrc, sz);
2125 out_bysrc:
2126 xfrm_hash_free(net->xfrm.state_bydst, sz);
2127 out_bydst:
2128 return -ENOMEM;
2131 void xfrm_state_fini(struct net *net)
2133 unsigned int sz;
2135 flush_work(&net->xfrm.state_hash_work);
2136 xfrm_state_flush(net, IPSEC_PROTO_ANY, false);
2137 flush_work(&net->xfrm.state_gc_work);
2139 WARN_ON(!list_empty(&net->xfrm.state_all));
2141 sz = (net->xfrm.state_hmask + 1) * sizeof(struct hlist_head);
2142 WARN_ON(!hlist_empty(net->xfrm.state_byspi));
2143 xfrm_hash_free(net->xfrm.state_byspi, sz);
2144 WARN_ON(!hlist_empty(net->xfrm.state_bysrc));
2145 xfrm_hash_free(net->xfrm.state_bysrc, sz);
2146 WARN_ON(!hlist_empty(net->xfrm.state_bydst));
2147 xfrm_hash_free(net->xfrm.state_bydst, sz);
2150 #ifdef CONFIG_AUDITSYSCALL
2151 static void xfrm_audit_helper_sainfo(struct xfrm_state *x,
2152 struct audit_buffer *audit_buf)
2154 struct xfrm_sec_ctx *ctx = x->security;
2155 u32 spi = ntohl(x->id.spi);
2157 if (ctx)
2158 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
2159 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
2161 switch (x->props.family) {
2162 case AF_INET:
2163 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2164 &x->props.saddr.a4, &x->id.daddr.a4);
2165 break;
2166 case AF_INET6:
2167 audit_log_format(audit_buf, " src=%pI6 dst=%pI6",
2168 x->props.saddr.a6, x->id.daddr.a6);
2169 break;
2172 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
2175 static void xfrm_audit_helper_pktinfo(struct sk_buff *skb, u16 family,
2176 struct audit_buffer *audit_buf)
2178 const struct iphdr *iph4;
2179 const struct ipv6hdr *iph6;
2181 switch (family) {
2182 case AF_INET:
2183 iph4 = ip_hdr(skb);
2184 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2185 &iph4->saddr, &iph4->daddr);
2186 break;
2187 case AF_INET6:
2188 iph6 = ipv6_hdr(skb);
2189 audit_log_format(audit_buf,
2190 " src=%pI6 dst=%pI6 flowlbl=0x%x%02x%02x",
2191 &iph6->saddr, &iph6->daddr,
2192 iph6->flow_lbl[0] & 0x0f,
2193 iph6->flow_lbl[1],
2194 iph6->flow_lbl[2]);
2195 break;
2199 void xfrm_audit_state_add(struct xfrm_state *x, int result, bool task_valid)
2201 struct audit_buffer *audit_buf;
2203 audit_buf = xfrm_audit_start("SAD-add");
2204 if (audit_buf == NULL)
2205 return;
2206 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
2207 xfrm_audit_helper_sainfo(x, audit_buf);
2208 audit_log_format(audit_buf, " res=%u", result);
2209 audit_log_end(audit_buf);
2211 EXPORT_SYMBOL_GPL(xfrm_audit_state_add);
2213 void xfrm_audit_state_delete(struct xfrm_state *x, int result, bool task_valid)
2215 struct audit_buffer *audit_buf;
2217 audit_buf = xfrm_audit_start("SAD-delete");
2218 if (audit_buf == NULL)
2219 return;
2220 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
2221 xfrm_audit_helper_sainfo(x, audit_buf);
2222 audit_log_format(audit_buf, " res=%u", result);
2223 audit_log_end(audit_buf);
2225 EXPORT_SYMBOL_GPL(xfrm_audit_state_delete);
2227 void xfrm_audit_state_replay_overflow(struct xfrm_state *x,
2228 struct sk_buff *skb)
2230 struct audit_buffer *audit_buf;
2231 u32 spi;
2233 audit_buf = xfrm_audit_start("SA-replay-overflow");
2234 if (audit_buf == NULL)
2235 return;
2236 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2237 /* don't record the sequence number because it's inherent in this kind
2238 * of audit message */
2239 spi = ntohl(x->id.spi);
2240 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
2241 audit_log_end(audit_buf);
2243 EXPORT_SYMBOL_GPL(xfrm_audit_state_replay_overflow);
2245 void xfrm_audit_state_replay(struct xfrm_state *x,
2246 struct sk_buff *skb, __be32 net_seq)
2248 struct audit_buffer *audit_buf;
2249 u32 spi;
2251 audit_buf = xfrm_audit_start("SA-replayed-pkt");
2252 if (audit_buf == NULL)
2253 return;
2254 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2255 spi = ntohl(x->id.spi);
2256 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2257 spi, spi, ntohl(net_seq));
2258 audit_log_end(audit_buf);
2260 EXPORT_SYMBOL_GPL(xfrm_audit_state_replay);
2262 void xfrm_audit_state_notfound_simple(struct sk_buff *skb, u16 family)
2264 struct audit_buffer *audit_buf;
2266 audit_buf = xfrm_audit_start("SA-notfound");
2267 if (audit_buf == NULL)
2268 return;
2269 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2270 audit_log_end(audit_buf);
2272 EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound_simple);
2274 void xfrm_audit_state_notfound(struct sk_buff *skb, u16 family,
2275 __be32 net_spi, __be32 net_seq)
2277 struct audit_buffer *audit_buf;
2278 u32 spi;
2280 audit_buf = xfrm_audit_start("SA-notfound");
2281 if (audit_buf == NULL)
2282 return;
2283 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2284 spi = ntohl(net_spi);
2285 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2286 spi, spi, ntohl(net_seq));
2287 audit_log_end(audit_buf);
2289 EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound);
2291 void xfrm_audit_state_icvfail(struct xfrm_state *x,
2292 struct sk_buff *skb, u8 proto)
2294 struct audit_buffer *audit_buf;
2295 __be32 net_spi;
2296 __be32 net_seq;
2298 audit_buf = xfrm_audit_start("SA-icv-failure");
2299 if (audit_buf == NULL)
2300 return;
2301 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2302 if (xfrm_parse_spi(skb, proto, &net_spi, &net_seq) == 0) {
2303 u32 spi = ntohl(net_spi);
2304 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2305 spi, spi, ntohl(net_seq));
2307 audit_log_end(audit_buf);
2309 EXPORT_SYMBOL_GPL(xfrm_audit_state_icvfail);
2310 #endif /* CONFIG_AUDITSYSCALL */