xfrm4_mode_transport.c 2.1 KB

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