gre_offload.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * IPV4 GSO/GRO offload support
  3. * Linux INET implementation
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version
  8. * 2 of the License, or (at your option) any later version.
  9. *
  10. * GRE GSO support
  11. */
  12. #include <linux/skbuff.h>
  13. #include <net/protocol.h>
  14. #include <net/gre.h>
  15. static int gre_gso_send_check(struct sk_buff *skb)
  16. {
  17. if (!skb->encapsulation)
  18. return -EINVAL;
  19. return 0;
  20. }
  21. static struct sk_buff *gre_gso_segment(struct sk_buff *skb,
  22. netdev_features_t features)
  23. {
  24. struct sk_buff *segs = ERR_PTR(-EINVAL);
  25. netdev_features_t enc_features;
  26. int ghl = GRE_HEADER_SECTION;
  27. struct gre_base_hdr *greh;
  28. int mac_len = skb->mac_len;
  29. __be16 protocol = skb->protocol;
  30. int tnl_hlen;
  31. bool csum;
  32. if (unlikely(skb_shinfo(skb)->gso_type &
  33. ~(SKB_GSO_TCPV4 |
  34. SKB_GSO_TCPV6 |
  35. SKB_GSO_UDP |
  36. SKB_GSO_DODGY |
  37. SKB_GSO_TCP_ECN |
  38. SKB_GSO_GRE |
  39. SKB_GSO_IPIP)))
  40. goto out;
  41. if (unlikely(!pskb_may_pull(skb, sizeof(*greh))))
  42. goto out;
  43. greh = (struct gre_base_hdr *)skb_transport_header(skb);
  44. if (greh->flags & GRE_KEY)
  45. ghl += GRE_HEADER_SECTION;
  46. if (greh->flags & GRE_SEQ)
  47. ghl += GRE_HEADER_SECTION;
  48. if (greh->flags & GRE_CSUM) {
  49. ghl += GRE_HEADER_SECTION;
  50. csum = true;
  51. } else
  52. csum = false;
  53. /* setup inner skb. */
  54. skb->protocol = greh->protocol;
  55. skb->encapsulation = 0;
  56. if (unlikely(!pskb_may_pull(skb, ghl)))
  57. goto out;
  58. __skb_pull(skb, ghl);
  59. skb_reset_mac_header(skb);
  60. skb_set_network_header(skb, skb_inner_network_offset(skb));
  61. skb->mac_len = skb_inner_network_offset(skb);
  62. /* segment inner packet. */
  63. enc_features = skb->dev->hw_enc_features & netif_skb_features(skb);
  64. segs = skb_mac_gso_segment(skb, enc_features);
  65. if (!segs || IS_ERR(segs))
  66. goto out;
  67. skb = segs;
  68. tnl_hlen = skb_tnl_header_len(skb);
  69. do {
  70. __skb_push(skb, ghl);
  71. if (csum) {
  72. __be32 *pcsum;
  73. if (skb_has_shared_frag(skb)) {
  74. int err;
  75. err = __skb_linearize(skb);
  76. if (err) {
  77. kfree_skb_list(segs);
  78. segs = ERR_PTR(err);
  79. goto out;
  80. }
  81. }
  82. greh = (struct gre_base_hdr *)(skb->data);
  83. pcsum = (__be32 *)(greh + 1);
  84. *pcsum = 0;
  85. *(__sum16 *)pcsum = csum_fold(skb_checksum(skb, 0, skb->len, 0));
  86. }
  87. __skb_push(skb, tnl_hlen - ghl);
  88. skb_reset_inner_headers(skb);
  89. skb->encapsulation = 1;
  90. skb_reset_mac_header(skb);
  91. skb_set_network_header(skb, mac_len);
  92. skb->mac_len = mac_len;
  93. skb->protocol = protocol;
  94. } while ((skb = skb->next));
  95. out:
  96. return segs;
  97. }
  98. static const struct net_offload gre_offload = {
  99. .callbacks = {
  100. .gso_send_check = gre_gso_send_check,
  101. .gso_segment = gre_gso_segment,
  102. },
  103. };
  104. int __init gre_offload_init(void)
  105. {
  106. return inet_add_offload(&gre_offload, IPPROTO_GRE);
  107. }
  108. void __exit gre_offload_exit(void)
  109. {
  110. inet_del_offload(&gre_offload, IPPROTO_GRE);
  111. }