xfrm4_mode_transport.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. #include <net/protocol.h>
  15. /* Add encapsulation header.
  16. *
  17. * The IP header will be moved forward to make space for the encapsulation
  18. * header.
  19. */
  20. static int xfrm4_transport_output(struct xfrm_state *x, struct sk_buff *skb)
  21. {
  22. struct iphdr *iph = ip_hdr(skb);
  23. int ihl = iph->ihl * 4;
  24. skb_set_inner_transport_header(skb, skb_transport_offset(skb));
  25. skb_set_network_header(skb, -x->props.header_len);
  26. skb->mac_header = skb->network_header +
  27. offsetof(struct iphdr, protocol);
  28. skb->transport_header = skb->network_header + ihl;
  29. __skb_pull(skb, ihl);
  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->transport_header != skb->network_header) {
  45. memmove(skb_transport_header(skb),
  46. skb_network_header(skb), ihl);
  47. skb->network_header = skb->transport_header;
  48. }
  49. ip_hdr(skb)->tot_len = htons(skb->len + ihl);
  50. skb_reset_transport_header(skb);
  51. return 0;
  52. }
  53. static struct sk_buff *xfrm4_transport_gso_segment(struct xfrm_state *x,
  54. struct sk_buff *skb,
  55. netdev_features_t features)
  56. {
  57. const struct net_offload *ops;
  58. struct sk_buff *segs = ERR_PTR(-EINVAL);
  59. struct xfrm_offload *xo = xfrm_offload(skb);
  60. skb->transport_header += x->props.header_len;
  61. ops = rcu_dereference(inet_offloads[xo->proto]);
  62. if (likely(ops && ops->callbacks.gso_segment))
  63. segs = ops->callbacks.gso_segment(skb, features);
  64. return segs;
  65. }
  66. static void xfrm4_transport_xmit(struct xfrm_state *x, struct sk_buff *skb)
  67. {
  68. struct xfrm_offload *xo = xfrm_offload(skb);
  69. skb_reset_mac_len(skb);
  70. pskb_pull(skb, skb->mac_len + sizeof(struct iphdr) + x->props.header_len);
  71. if (xo->flags & XFRM_GSO_SEGMENT) {
  72. skb_reset_transport_header(skb);
  73. skb->transport_header -= x->props.header_len;
  74. }
  75. }
  76. static struct xfrm_mode xfrm4_transport_mode = {
  77. .input = xfrm4_transport_input,
  78. .output = xfrm4_transport_output,
  79. .gso_segment = xfrm4_transport_gso_segment,
  80. .xmit = xfrm4_transport_xmit,
  81. .owner = THIS_MODULE,
  82. .encap = XFRM_MODE_TRANSPORT,
  83. };
  84. static int __init xfrm4_transport_init(void)
  85. {
  86. return xfrm_register_mode(&xfrm4_transport_mode, AF_INET);
  87. }
  88. static void __exit xfrm4_transport_exit(void)
  89. {
  90. int err;
  91. err = xfrm_unregister_mode(&xfrm4_transport_mode, AF_INET);
  92. BUG_ON(err);
  93. }
  94. module_init(xfrm4_transport_init);
  95. module_exit(xfrm4_transport_exit);
  96. MODULE_LICENSE("GPL");
  97. MODULE_ALIAS_XFRM_MODE(AF_INET, XFRM_MODE_TRANSPORT);