xfrm_input.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * xfrm_input.c
  3. *
  4. * Changes:
  5. * YOSHIFUJI Hideaki @USAGI
  6. * Split up af-specific portion
  7. *
  8. */
  9. #include <linux/slab.h>
  10. #include <linux/module.h>
  11. #include <net/ip.h>
  12. #include <net/xfrm.h>
  13. static struct kmem_cache *secpath_cachep __read_mostly;
  14. void __secpath_destroy(struct sec_path *sp)
  15. {
  16. int i;
  17. for (i = 0; i < sp->len; i++)
  18. xfrm_state_put(sp->xvec[i]);
  19. kmem_cache_free(secpath_cachep, sp);
  20. }
  21. EXPORT_SYMBOL(__secpath_destroy);
  22. struct sec_path *secpath_dup(struct sec_path *src)
  23. {
  24. struct sec_path *sp;
  25. sp = kmem_cache_alloc(secpath_cachep, GFP_ATOMIC);
  26. if (!sp)
  27. return NULL;
  28. sp->len = 0;
  29. if (src) {
  30. int i;
  31. memcpy(sp, src, sizeof(*sp));
  32. for (i = 0; i < sp->len; i++)
  33. xfrm_state_hold(sp->xvec[i]);
  34. }
  35. atomic_set(&sp->refcnt, 1);
  36. return sp;
  37. }
  38. EXPORT_SYMBOL(secpath_dup);
  39. /* Fetch spi and seq from ipsec header */
  40. int xfrm_parse_spi(struct sk_buff *skb, u8 nexthdr, __be32 *spi, __be32 *seq)
  41. {
  42. int offset, offset_seq;
  43. switch (nexthdr) {
  44. case IPPROTO_AH:
  45. offset = offsetof(struct ip_auth_hdr, spi);
  46. offset_seq = offsetof(struct ip_auth_hdr, seq_no);
  47. break;
  48. case IPPROTO_ESP:
  49. offset = offsetof(struct ip_esp_hdr, spi);
  50. offset_seq = offsetof(struct ip_esp_hdr, seq_no);
  51. break;
  52. case IPPROTO_COMP:
  53. if (!pskb_may_pull(skb, sizeof(struct ip_comp_hdr)))
  54. return -EINVAL;
  55. *spi = htonl(ntohs(*(__be16*)(skb_transport_header(skb) + 2)));
  56. *seq = 0;
  57. return 0;
  58. default:
  59. return 1;
  60. }
  61. if (!pskb_may_pull(skb, 16))
  62. return -EINVAL;
  63. *spi = *(__be32*)(skb_transport_header(skb) + offset);
  64. *seq = *(__be32*)(skb_transport_header(skb) + offset_seq);
  65. return 0;
  66. }
  67. EXPORT_SYMBOL(xfrm_parse_spi);
  68. void __init xfrm_input_init(void)
  69. {
  70. secpath_cachep = kmem_cache_create("secpath_cache",
  71. sizeof(struct sec_path),
  72. 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
  73. NULL);
  74. }