xfrm6_mode_transport.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. #include <net/protocol.h>
  16. /* Add encapsulation header.
  17. *
  18. * The IP header and mutable extension headers will be moved forward to make
  19. * space for the encapsulation header.
  20. */
  21. static int xfrm6_transport_output(struct xfrm_state *x, struct sk_buff *skb)
  22. {
  23. struct ipv6hdr *iph;
  24. u8 *prevhdr;
  25. int hdr_len;
  26. iph = ipv6_hdr(skb);
  27. skb_set_inner_transport_header(skb, skb_transport_offset(skb));
  28. hdr_len = x->type->hdr_offset(x, skb, &prevhdr);
  29. if (hdr_len < 0)
  30. return hdr_len;
  31. skb_set_mac_header(skb, (prevhdr - x->props.header_len) - skb->data);
  32. skb_set_network_header(skb, -x->props.header_len);
  33. skb->transport_header = skb->network_header + hdr_len;
  34. __skb_pull(skb, hdr_len);
  35. memmove(ipv6_hdr(skb), iph, hdr_len);
  36. return 0;
  37. }
  38. /* Remove encapsulation header.
  39. *
  40. * The IP header will be moved over the top of the encapsulation header.
  41. *
  42. * On entry, skb->h shall point to where the IP header should be and skb->nh
  43. * shall be set to where the IP header currently is. skb->data shall point
  44. * to the start of the payload.
  45. */
  46. static int xfrm6_transport_input(struct xfrm_state *x, struct sk_buff *skb)
  47. {
  48. int ihl = skb->data - skb_transport_header(skb);
  49. struct xfrm_offload *xo = xfrm_offload(skb);
  50. if (skb->transport_header != skb->network_header) {
  51. memmove(skb_transport_header(skb),
  52. skb_network_header(skb), ihl);
  53. skb->network_header = skb->transport_header;
  54. }
  55. ipv6_hdr(skb)->payload_len = htons(skb->len + ihl -
  56. sizeof(struct ipv6hdr));
  57. if (!xo || !(xo->flags & XFRM_GRO))
  58. skb_reset_transport_header(skb);
  59. return 0;
  60. }
  61. static struct sk_buff *xfrm4_transport_gso_segment(struct xfrm_state *x,
  62. struct sk_buff *skb,
  63. netdev_features_t features)
  64. {
  65. const struct net_offload *ops;
  66. struct sk_buff *segs = ERR_PTR(-EINVAL);
  67. struct xfrm_offload *xo = xfrm_offload(skb);
  68. skb->transport_header += x->props.header_len;
  69. ops = rcu_dereference(inet6_offloads[xo->proto]);
  70. if (likely(ops && ops->callbacks.gso_segment))
  71. segs = ops->callbacks.gso_segment(skb, features);
  72. return segs;
  73. }
  74. static void xfrm6_transport_xmit(struct xfrm_state *x, struct sk_buff *skb)
  75. {
  76. struct xfrm_offload *xo = xfrm_offload(skb);
  77. skb_reset_mac_len(skb);
  78. pskb_pull(skb, skb->mac_len + sizeof(struct ipv6hdr) + x->props.header_len);
  79. if (xo->flags & XFRM_GSO_SEGMENT) {
  80. skb_reset_transport_header(skb);
  81. skb->transport_header -= x->props.header_len;
  82. }
  83. }
  84. static struct xfrm_mode xfrm6_transport_mode = {
  85. .input = xfrm6_transport_input,
  86. .output = xfrm6_transport_output,
  87. .gso_segment = xfrm4_transport_gso_segment,
  88. .xmit = xfrm6_transport_xmit,
  89. .owner = THIS_MODULE,
  90. .encap = XFRM_MODE_TRANSPORT,
  91. };
  92. static int __init xfrm6_transport_init(void)
  93. {
  94. return xfrm_register_mode(&xfrm6_transport_mode, AF_INET6);
  95. }
  96. static void __exit xfrm6_transport_exit(void)
  97. {
  98. int err;
  99. err = xfrm_unregister_mode(&xfrm6_transport_mode, AF_INET6);
  100. BUG_ON(err);
  101. }
  102. module_init(xfrm6_transport_init);
  103. module_exit(xfrm6_transport_exit);
  104. MODULE_LICENSE("GPL");
  105. MODULE_ALIAS_XFRM_MODE(AF_INET6, XFRM_MODE_TRANSPORT);