xfrm6_mode_transport.c 2.3 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. static int xfrm6_transport_output(struct xfrm_state *x, struct sk_buff *skb)
  21. {
  22. struct ipv6hdr *iph;
  23. u8 *prevhdr;
  24. int hdr_len;
  25. iph = ipv6_hdr(skb);
  26. hdr_len = x->type->hdr_offset(x, skb, &prevhdr);
  27. if (hdr_len < 0)
  28. return hdr_len;
  29. skb_set_mac_header(skb, (prevhdr - x->props.header_len) - skb->data);
  30. skb_set_network_header(skb, -x->props.header_len);
  31. skb->transport_header = skb->network_header + hdr_len;
  32. __skb_pull(skb, hdr_len);
  33. memmove(ipv6_hdr(skb), iph, hdr_len);
  34. return 0;
  35. }
  36. /* Remove encapsulation header.
  37. *
  38. * The IP header will be moved over the top of the encapsulation header.
  39. *
  40. * On entry, skb->h shall point to where the IP header should be and skb->nh
  41. * shall be set to where the IP header currently is. skb->data shall point
  42. * to the start of the payload.
  43. */
  44. static int xfrm6_transport_input(struct xfrm_state *x, struct sk_buff *skb)
  45. {
  46. int ihl = skb->data - skb_transport_header(skb);
  47. if (skb->transport_header != skb->network_header) {
  48. memmove(skb_transport_header(skb),
  49. skb_network_header(skb), ihl);
  50. skb->network_header = skb->transport_header;
  51. }
  52. ipv6_hdr(skb)->payload_len = htons(skb->len + ihl -
  53. sizeof(struct ipv6hdr));
  54. skb_reset_transport_header(skb);
  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);