xfrm_input.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. int hlen;
  44. switch (nexthdr) {
  45. case IPPROTO_AH:
  46. hlen = sizeof(struct ip_auth_hdr);
  47. offset = offsetof(struct ip_auth_hdr, spi);
  48. offset_seq = offsetof(struct ip_auth_hdr, seq_no);
  49. break;
  50. case IPPROTO_ESP:
  51. hlen = sizeof(struct ip_esp_hdr);
  52. offset = offsetof(struct ip_esp_hdr, spi);
  53. offset_seq = offsetof(struct ip_esp_hdr, seq_no);
  54. break;
  55. case IPPROTO_COMP:
  56. if (!pskb_may_pull(skb, sizeof(struct ip_comp_hdr)))
  57. return -EINVAL;
  58. *spi = htonl(ntohs(*(__be16*)(skb_transport_header(skb) + 2)));
  59. *seq = 0;
  60. return 0;
  61. default:
  62. return 1;
  63. }
  64. if (!pskb_may_pull(skb, hlen))
  65. return -EINVAL;
  66. *spi = *(__be32*)(skb_transport_header(skb) + offset);
  67. *seq = *(__be32*)(skb_transport_header(skb) + offset_seq);
  68. return 0;
  69. }
  70. EXPORT_SYMBOL(xfrm_parse_spi);
  71. void __init xfrm_input_init(void)
  72. {
  73. secpath_cachep = kmem_cache_create("secpath_cache",
  74. sizeof(struct sec_path),
  75. 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
  76. NULL);
  77. }