xfrm4_mode_transport.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 = ip_hdr(skb);
  25. int ihl = iph->ihl * 4;
  26. skb->h.raw = skb->nh.raw;
  27. skb->h.raw += ihl;
  28. skb_push(skb, x->props.header_len);
  29. skb_reset_network_header(skb);
  30. memmove(skb_network_header(skb), iph, ihl);
  31. return 0;
  32. }
  33. /* Remove encapsulation header.
  34. *
  35. * The IP header will be moved over the top of the encapsulation header.
  36. *
  37. * On entry, skb->h shall point to where the IP header should be and skb->nh
  38. * shall be set to where the IP header currently is. skb->data shall point
  39. * to the start of the payload.
  40. */
  41. static int xfrm4_transport_input(struct xfrm_state *x, struct sk_buff *skb)
  42. {
  43. int ihl = skb->data - skb_transport_header(skb);
  44. if (skb->h.raw != skb->nh.raw) {
  45. memmove(skb_transport_header(skb),
  46. skb_network_header(skb), ihl);
  47. skb->nh.raw = skb->h.raw;
  48. }
  49. ip_hdr(skb)->tot_len = htons(skb->len + ihl);
  50. skb_reset_transport_header(skb);
  51. return 0;
  52. }
  53. static struct xfrm_mode xfrm4_transport_mode = {
  54. .input = xfrm4_transport_input,
  55. .output = xfrm4_transport_output,
  56. .owner = THIS_MODULE,
  57. .encap = XFRM_MODE_TRANSPORT,
  58. };
  59. static int __init xfrm4_transport_init(void)
  60. {
  61. return xfrm_register_mode(&xfrm4_transport_mode, AF_INET);
  62. }
  63. static void __exit xfrm4_transport_exit(void)
  64. {
  65. int err;
  66. err = xfrm_unregister_mode(&xfrm4_transport_mode, AF_INET);
  67. BUG_ON(err);
  68. }
  69. module_init(xfrm4_transport_init);
  70. module_exit(xfrm4_transport_exit);
  71. MODULE_LICENSE("GPL");
  72. MODULE_ALIAS_XFRM_MODE(AF_INET, XFRM_MODE_TRANSPORT);