xfrm6_mode_transport.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. memmove(skb->h.raw, skb_network_header(skb), ihl);
  52. skb->nh.raw = skb->h.raw;
  53. }
  54. skb->nh.ipv6h->payload_len = htons(skb->len + ihl -
  55. sizeof(struct ipv6hdr));
  56. skb->h.raw = skb->data;
  57. return 0;
  58. }
  59. static struct xfrm_mode xfrm6_transport_mode = {
  60. .input = xfrm6_transport_input,
  61. .output = xfrm6_transport_output,
  62. .owner = THIS_MODULE,
  63. .encap = XFRM_MODE_TRANSPORT,
  64. };
  65. static int __init xfrm6_transport_init(void)
  66. {
  67. return xfrm_register_mode(&xfrm6_transport_mode, AF_INET6);
  68. }
  69. static void __exit xfrm6_transport_exit(void)
  70. {
  71. int err;
  72. err = xfrm_unregister_mode(&xfrm6_transport_mode, AF_INET6);
  73. BUG_ON(err);
  74. }
  75. module_init(xfrm6_transport_init);
  76. module_exit(xfrm6_transport_exit);
  77. MODULE_LICENSE("GPL");
  78. MODULE_ALIAS_XFRM_MODE(AF_INET6, XFRM_MODE_TRANSPORT);