xfrm4_mode_transport.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * xfrm4_mode_transport.c - Transport mode encapsulation for IPv4.
  3. *
  4. * Copyright (c) 2004-2006 Herbert Xu <herbert@gondor.apana.org.au>
  5. */
  6. #include <linux/init.h>
  7. #include <linux/kernel.h>
  8. #include <linux/module.h>
  9. #include <linux/skbuff.h>
  10. #include <linux/stringify.h>
  11. #include <net/dst.h>
  12. #include <net/ip.h>
  13. #include <net/xfrm.h>
  14. /* Add encapsulation header.
  15. *
  16. * The IP header will be moved forward to make space for the encapsulation
  17. * header.
  18. *
  19. * On exit, skb->h will be set to the start of the payload to be processed
  20. * by x->type->output and skb->nh will be set to the top IP header.
  21. */
  22. static int xfrm4_transport_output(struct xfrm_state *x, struct sk_buff *skb)
  23. {
  24. struct iphdr *iph;
  25. int ihl;
  26. iph = skb->nh.iph;
  27. skb->h.ipiph = iph;
  28. ihl = iph->ihl * 4;
  29. skb->h.raw += ihl;
  30. skb_push(skb, x->props.header_len);
  31. skb_reset_network_header(skb);
  32. memmove(skb_network_header(skb), iph, ihl);
  33. return 0;
  34. }
  35. /* Remove encapsulation header.
  36. *
  37. * The IP header will be moved over the top of the encapsulation header.
  38. *
  39. * On entry, skb->h shall point to where the IP header should be and skb->nh
  40. * shall be set to where the IP header currently is. skb->data shall point
  41. * to the start of the payload.
  42. */
  43. static int xfrm4_transport_input(struct xfrm_state *x, struct sk_buff *skb)
  44. {
  45. int ihl = skb->data - skb->h.raw;
  46. if (skb->h.raw != skb->nh.raw) {
  47. memmove(skb->h.raw, skb_network_header(skb), ihl);
  48. skb->nh.raw = skb->h.raw;
  49. }
  50. skb->nh.iph->tot_len = htons(skb->len + ihl);
  51. skb->h.raw = skb->data;
  52. return 0;
  53. }
  54. static struct xfrm_mode xfrm4_transport_mode = {
  55. .input = xfrm4_transport_input,
  56. .output = xfrm4_transport_output,
  57. .owner = THIS_MODULE,
  58. .encap = XFRM_MODE_TRANSPORT,
  59. };
  60. static int __init xfrm4_transport_init(void)
  61. {
  62. return xfrm_register_mode(&xfrm4_transport_mode, AF_INET);
  63. }
  64. static void __exit xfrm4_transport_exit(void)
  65. {
  66. int err;
  67. err = xfrm_unregister_mode(&xfrm4_transport_mode, AF_INET);
  68. BUG_ON(err);
  69. }
  70. module_init(xfrm4_transport_init);
  71. module_exit(xfrm4_transport_exit);
  72. MODULE_LICENSE("GPL");
  73. MODULE_ALIAS_XFRM_MODE(AF_INET, XFRM_MODE_TRANSPORT);