xfrm_input.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 kmem_cache_t *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->x[i].xvec);
  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, SLAB_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->x[i].xvec);
  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, u32 *spi, u32 *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 = ntohl(ntohs(*(u16*)(skb->h.raw + 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 = *(u32*)(skb->h.raw + offset);
  64. *seq = *(u32*)(skb->h.raw + 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,
  73. NULL, NULL);
  74. if (!secpath_cachep)
  75. panic("XFRM: failed to allocate secpath_cache\n");
  76. }