xfrm6_mode_transport.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * xfrm6_mode_transport.c - Transport mode encapsulation for IPv6.
  3. *
  4. * Copyright (C) 2002 USAGI/WIDE Project
  5. * Copyright (c) 2004-2006 Herbert Xu <herbert@gondor.apana.org.au>
  6. */
  7. #include <linux/init.h>
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/skbuff.h>
  11. #include <linux/stringify.h>
  12. #include <net/dst.h>
  13. #include <net/ipv6.h>
  14. #include <net/xfrm.h>
  15. /* Add encapsulation header.
  16. *
  17. * The IP header and mutable extension headers will be moved forward to make
  18. * space for the encapsulation header.
  19. *
  20. * On exit, skb->h will be set to the start of the encapsulation header to be
  21. * filled in by x->type->output and skb->nh will be set to the nextheader field
  22. * of the extension header directly preceding the encapsulation header, or in
  23. * its absence, that of the top IP header. The value of skb->data will always
  24. * point to the top IP header.
  25. */
  26. static int xfrm6_transport_output(struct xfrm_state *x, struct sk_buff *skb)
  27. {
  28. struct ipv6hdr *iph;
  29. u8 *prevhdr;
  30. int hdr_len;
  31. skb_push(skb, x->props.header_len);
  32. iph = skb->nh.ipv6h;
  33. hdr_len = x->type->hdr_offset(x, skb, &prevhdr);
  34. skb->nh.raw = prevhdr - x->props.header_len;
  35. skb->h.raw = skb->data + hdr_len;
  36. memmove(skb->data, iph, hdr_len);
  37. return 0;
  38. }
  39. /* Remove encapsulation header.
  40. *
  41. * The IP header will be moved over the top of the encapsulation header.
  42. *
  43. * On entry, skb->h shall point to where the IP header should be and skb->nh
  44. * shall be set to where the IP header currently is. skb->data shall point
  45. * to the start of the payload.
  46. */
  47. static int xfrm6_transport_input(struct xfrm_state *x, struct sk_buff *skb)
  48. {
  49. int ihl = skb->data - skb->h.raw;
  50. if (skb->h.raw != skb->nh.raw)
  51. skb->nh.raw = memmove(skb->h.raw, skb->nh.raw, ihl);
  52. skb->nh.ipv6h->payload_len = htons(skb->len + ihl -
  53. sizeof(struct ipv6hdr));
  54. skb->h.raw = skb->data;
  55. return 0;
  56. }
  57. static struct xfrm_mode xfrm6_transport_mode = {
  58. .input = xfrm6_transport_input,
  59. .output = xfrm6_transport_output,
  60. .owner = THIS_MODULE,
  61. .encap = XFRM_MODE_TRANSPORT,
  62. };
  63. static int __init xfrm6_transport_init(void)
  64. {
  65. return xfrm_register_mode(&xfrm6_transport_mode, AF_INET6);
  66. }
  67. static void __exit xfrm6_transport_exit(void)
  68. {
  69. int err;
  70. err = xfrm_unregister_mode(&xfrm6_transport_mode, AF_INET6);
  71. BUG_ON(err);
  72. }
  73. module_init(xfrm6_transport_init);
  74. module_exit(xfrm6_transport_exit);
  75. MODULE_LICENSE("GPL");
  76. MODULE_ALIAS_XFRM_MODE(AF_INET6, XFRM_MODE_TRANSPORT);