gre_offload.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. goto out;
  40. if (unlikely(!pskb_may_pull(skb, sizeof(*greh))))
  41. goto out;
  42. greh = (struct gre_base_hdr *)skb_transport_header(skb);
  43. if (greh->flags & GRE_KEY)
  44. ghl += GRE_HEADER_SECTION;
  45. if (greh->flags & GRE_SEQ)
  46. ghl += GRE_HEADER_SECTION;
  47. if (greh->flags & GRE_CSUM) {
  48. ghl += GRE_HEADER_SECTION;
  49. csum = true;
  50. } else
  51. csum = false;
  52. /* setup inner skb. */
  53. skb->protocol = greh->protocol;
  54. skb->encapsulation = 0;
  55. if (unlikely(!pskb_may_pull(skb, ghl)))
  56. goto out;
  57. __skb_pull(skb, ghl);
  58. skb_reset_mac_header(skb);
  59. skb_set_network_header(skb, skb_inner_network_offset(skb));
  60. skb->mac_len = skb_inner_network_offset(skb);
  61. /* segment inner packet. */
  62. enc_features = skb->dev->hw_enc_features & netif_skb_features(skb);
  63. segs = skb_mac_gso_segment(skb, enc_features);
  64. if (!segs || IS_ERR(segs))
  65. goto out;
  66. skb = segs;
  67. tnl_hlen = skb_tnl_header_len(skb);
  68. do {
  69. __skb_push(skb, ghl);
  70. if (csum) {
  71. __be32 *pcsum;
  72. if (skb_has_shared_frag(skb)) {
  73. int err;
  74. err = __skb_linearize(skb);
  75. if (err) {
  76. kfree_skb_list(segs);
  77. segs = ERR_PTR(err);
  78. goto out;
  79. }
  80. }
  81. greh = (struct gre_base_hdr *)(skb->data);
  82. pcsum = (__be32 *)(greh + 1);
  83. *pcsum = 0;
  84. *(__sum16 *)pcsum = csum_fold(skb_checksum(skb, 0, skb->len, 0));
  85. }
  86. __skb_push(skb, tnl_hlen - ghl);
  87. skb_reset_inner_headers(skb);
  88. skb->encapsulation = 1;
  89. skb_reset_mac_header(skb);
  90. skb_set_network_header(skb, mac_len);
  91. skb->mac_len = mac_len;
  92. skb->protocol = protocol;
  93. } while ((skb = skb->next));
  94. out:
  95. return segs;
  96. }
  97. static const struct net_offload gre_offload = {
  98. .callbacks = {
  99. .gso_send_check = gre_gso_send_check,
  100. .gso_segment = gre_gso_segment,
  101. },
  102. };
  103. int __init gre_offload_init(void)
  104. {
  105. return inet_add_offload(&gre_offload, IPPROTO_GRE);
  106. }
  107. void __exit gre_offload_exit(void)
  108. {
  109. inet_del_offload(&gre_offload, IPPROTO_GRE);
  110. }