xfrm6_mode_transport.c 2.3 KB

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