xfrm6_mode_transport.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 sk_buff *skb)
  27. {
  28. struct xfrm_state *x = skb->dst->xfrm;
  29. struct ipv6hdr *iph;
  30. u8 *prevhdr;
  31. int hdr_len;
  32. skb_push(skb, x->props.header_len);
  33. iph = skb->nh.ipv6h;
  34. hdr_len = ip6_find_1stfragopt(skb, &prevhdr);
  35. skb->nh.raw = prevhdr - x->props.header_len;
  36. skb->h.raw = skb->data + hdr_len;
  37. memmove(skb->data, iph, hdr_len);
  38. return 0;
  39. }
  40. /* Remove encapsulation header.
  41. *
  42. * The IP header will be moved over the top of the encapsulation header.
  43. *
  44. * On entry, skb->h shall point to where the IP header should be and skb->nh
  45. * shall be set to where the IP header currently is. skb->data shall point
  46. * to the start of the payload.
  47. */
  48. static int xfrm6_transport_input(struct xfrm_state *x, struct sk_buff *skb)
  49. {
  50. int ihl = skb->data - skb->h.raw;
  51. if (skb->h.raw != skb->nh.raw)
  52. skb->nh.raw = memmove(skb->h.raw, skb->nh.raw, ihl);
  53. skb->nh.ipv6h->payload_len = htons(skb->len + ihl -
  54. sizeof(struct ipv6hdr));
  55. skb->h.raw = skb->data;
  56. return 0;
  57. }
  58. static struct xfrm_mode xfrm6_transport_mode = {
  59. .input = xfrm6_transport_input,
  60. .output = xfrm6_transport_output,
  61. .owner = THIS_MODULE,
  62. .encap = XFRM_MODE_TRANSPORT,
  63. };
  64. static int __init xfrm6_transport_init(void)
  65. {
  66. return xfrm_register_mode(&xfrm6_transport_mode, AF_INET6);
  67. }
  68. static void __exit xfrm6_transport_exit(void)
  69. {
  70. int err;
  71. err = xfrm_unregister_mode(&xfrm6_transport_mode, AF_INET6);
  72. BUG_ON(err);
  73. }
  74. module_init(xfrm6_transport_init);
  75. module_exit(xfrm6_transport_exit);
  76. MODULE_LICENSE("GPL");
  77. MODULE_ALIAS_XFRM_MODE(AF_INET6, XFRM_MODE_TRANSPORT);