xfrm6_mode_transport.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. if (skb->transport_header != skb->network_header) {
  50. memmove(skb_transport_header(skb),
  51. skb_network_header(skb), ihl);
  52. skb->network_header = skb->transport_header;
  53. }
  54. ipv6_hdr(skb)->payload_len = htons(skb->len + ihl -
  55. sizeof(struct ipv6hdr));
  56. skb_reset_transport_header(skb);
  57. return 0;
  58. }
  59. static struct sk_buff *xfrm4_transport_gso_segment(struct xfrm_state *x,
  60. struct sk_buff *skb,
  61. netdev_features_t features)
  62. {
  63. const struct net_offload *ops;
  64. struct sk_buff *segs = ERR_PTR(-EINVAL);
  65. struct xfrm_offload *xo = xfrm_offload(skb);
  66. skb->transport_header += x->props.header_len;
  67. ops = rcu_dereference(inet6_offloads[xo->proto]);
  68. if (likely(ops && ops->callbacks.gso_segment))
  69. segs = ops->callbacks.gso_segment(skb, features);
  70. return segs;
  71. }
  72. static void xfrm6_transport_xmit(struct xfrm_state *x, struct sk_buff *skb)
  73. {
  74. struct xfrm_offload *xo = xfrm_offload(skb);
  75. skb_reset_mac_len(skb);
  76. pskb_pull(skb, skb->mac_len + sizeof(struct ipv6hdr) + x->props.header_len);
  77. if (xo->flags & XFRM_GSO_SEGMENT) {
  78. skb_reset_transport_header(skb);
  79. skb->transport_header -= x->props.header_len;
  80. }
  81. }
  82. static struct xfrm_mode xfrm6_transport_mode = {
  83. .input = xfrm6_transport_input,
  84. .output = xfrm6_transport_output,
  85. .gso_segment = xfrm4_transport_gso_segment,
  86. .xmit = xfrm6_transport_xmit,
  87. .owner = THIS_MODULE,
  88. .encap = XFRM_MODE_TRANSPORT,
  89. };
  90. static int __init xfrm6_transport_init(void)
  91. {
  92. return xfrm_register_mode(&xfrm6_transport_mode, AF_INET6);
  93. }
  94. static void __exit xfrm6_transport_exit(void)
  95. {
  96. int err;
  97. err = xfrm_unregister_mode(&xfrm6_transport_mode, AF_INET6);
  98. BUG_ON(err);
  99. }
  100. module_init(xfrm6_transport_init);
  101. module_exit(xfrm6_transport_exit);
  102. MODULE_LICENSE("GPL");
  103. MODULE_ALIAS_XFRM_MODE(AF_INET6, XFRM_MODE_TRANSPORT);