gre_offload.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 <linux/init.h>
  14. #include <net/protocol.h>
  15. #include <net/gre.h>
  16. static struct sk_buff *gre_gso_segment(struct sk_buff *skb,
  17. netdev_features_t features)
  18. {
  19. struct sk_buff *segs = ERR_PTR(-EINVAL);
  20. netdev_features_t enc_features;
  21. int ghl;
  22. struct gre_base_hdr *greh;
  23. u16 mac_offset = skb->mac_header;
  24. int mac_len = skb->mac_len;
  25. __be16 protocol = skb->protocol;
  26. int tnl_hlen;
  27. bool csum;
  28. if (unlikely(skb_shinfo(skb)->gso_type &
  29. ~(SKB_GSO_TCPV4 |
  30. SKB_GSO_TCPV6 |
  31. SKB_GSO_UDP |
  32. SKB_GSO_DODGY |
  33. SKB_GSO_TCP_ECN |
  34. SKB_GSO_GRE |
  35. SKB_GSO_GRE_CSUM |
  36. SKB_GSO_IPIP |
  37. SKB_GSO_SIT)))
  38. goto out;
  39. if (!skb->encapsulation)
  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. ghl = skb_inner_mac_header(skb) - skb_transport_header(skb);
  45. if (unlikely(ghl < sizeof(*greh)))
  46. goto out;
  47. csum = !!(greh->flags & GRE_CSUM);
  48. if (csum)
  49. skb->encap_hdr_csum = 1;
  50. /* setup inner skb. */
  51. skb->protocol = greh->protocol;
  52. skb->encapsulation = 0;
  53. if (unlikely(!pskb_may_pull(skb, ghl)))
  54. goto out;
  55. __skb_pull(skb, ghl);
  56. skb_reset_mac_header(skb);
  57. skb_set_network_header(skb, skb_inner_network_offset(skb));
  58. skb->mac_len = skb_inner_network_offset(skb);
  59. /* segment inner packet. */
  60. enc_features = skb->dev->hw_enc_features & features;
  61. segs = skb_mac_gso_segment(skb, enc_features);
  62. if (IS_ERR_OR_NULL(segs)) {
  63. skb_gso_error_unwind(skb, protocol, ghl, mac_offset, mac_len);
  64. goto out;
  65. }
  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. skb_reset_transport_header(skb);
  82. greh = (struct gre_base_hdr *)
  83. skb_transport_header(skb);
  84. pcsum = (__be32 *)(greh + 1);
  85. *pcsum = 0;
  86. *(__sum16 *)pcsum = gso_make_checksum(skb, 0);
  87. }
  88. __skb_push(skb, tnl_hlen - ghl);
  89. skb_reset_inner_headers(skb);
  90. skb->encapsulation = 1;
  91. skb_reset_mac_header(skb);
  92. skb_set_network_header(skb, mac_len);
  93. skb->mac_len = mac_len;
  94. skb->protocol = protocol;
  95. } while ((skb = skb->next));
  96. out:
  97. return segs;
  98. }
  99. static struct sk_buff **gre_gro_receive(struct sk_buff **head,
  100. struct sk_buff *skb)
  101. {
  102. struct sk_buff **pp = NULL;
  103. struct sk_buff *p;
  104. const struct gre_base_hdr *greh;
  105. unsigned int hlen, grehlen;
  106. unsigned int off;
  107. int flush = 1;
  108. struct packet_offload *ptype;
  109. __be16 type;
  110. off = skb_gro_offset(skb);
  111. hlen = off + sizeof(*greh);
  112. greh = skb_gro_header_fast(skb, off);
  113. if (skb_gro_header_hard(skb, hlen)) {
  114. greh = skb_gro_header_slow(skb, hlen, off);
  115. if (unlikely(!greh))
  116. goto out;
  117. }
  118. /* Only support version 0 and K (key), C (csum) flags. Note that
  119. * although the support for the S (seq#) flag can be added easily
  120. * for GRO, this is problematic for GSO hence can not be enabled
  121. * here because a GRO pkt may end up in the forwarding path, thus
  122. * requiring GSO support to break it up correctly.
  123. */
  124. if ((greh->flags & ~(GRE_KEY|GRE_CSUM)) != 0)
  125. goto out;
  126. type = greh->protocol;
  127. rcu_read_lock();
  128. ptype = gro_find_receive_by_type(type);
  129. if (!ptype)
  130. goto out_unlock;
  131. grehlen = GRE_HEADER_SECTION;
  132. if (greh->flags & GRE_KEY)
  133. grehlen += GRE_HEADER_SECTION;
  134. if (greh->flags & GRE_CSUM)
  135. grehlen += GRE_HEADER_SECTION;
  136. hlen = off + grehlen;
  137. if (skb_gro_header_hard(skb, hlen)) {
  138. greh = skb_gro_header_slow(skb, hlen, off);
  139. if (unlikely(!greh))
  140. goto out_unlock;
  141. }
  142. /* Don't bother verifying checksum if we're going to flush anyway. */
  143. if ((greh->flags & GRE_CSUM) && !NAPI_GRO_CB(skb)->flush) {
  144. if (skb_gro_checksum_simple_validate(skb))
  145. goto out_unlock;
  146. skb_gro_checksum_try_convert(skb, IPPROTO_GRE, 0,
  147. null_compute_pseudo);
  148. }
  149. flush = 0;
  150. for (p = *head; p; p = p->next) {
  151. const struct gre_base_hdr *greh2;
  152. if (!NAPI_GRO_CB(p)->same_flow)
  153. continue;
  154. /* The following checks are needed to ensure only pkts
  155. * from the same tunnel are considered for aggregation.
  156. * The criteria for "the same tunnel" includes:
  157. * 1) same version (we only support version 0 here)
  158. * 2) same protocol (we only support ETH_P_IP for now)
  159. * 3) same set of flags
  160. * 4) same key if the key field is present.
  161. */
  162. greh2 = (struct gre_base_hdr *)(p->data + off);
  163. if (greh2->flags != greh->flags ||
  164. greh2->protocol != greh->protocol) {
  165. NAPI_GRO_CB(p)->same_flow = 0;
  166. continue;
  167. }
  168. if (greh->flags & GRE_KEY) {
  169. /* compare keys */
  170. if (*(__be32 *)(greh2+1) != *(__be32 *)(greh+1)) {
  171. NAPI_GRO_CB(p)->same_flow = 0;
  172. continue;
  173. }
  174. }
  175. }
  176. skb_gro_pull(skb, grehlen);
  177. /* Adjusted NAPI_GRO_CB(skb)->csum after skb_gro_pull()*/
  178. skb_gro_postpull_rcsum(skb, greh, grehlen);
  179. pp = ptype->callbacks.gro_receive(head, skb);
  180. out_unlock:
  181. rcu_read_unlock();
  182. out:
  183. NAPI_GRO_CB(skb)->flush |= flush;
  184. return pp;
  185. }
  186. static int gre_gro_complete(struct sk_buff *skb, int nhoff)
  187. {
  188. struct gre_base_hdr *greh = (struct gre_base_hdr *)(skb->data + nhoff);
  189. struct packet_offload *ptype;
  190. unsigned int grehlen = sizeof(*greh);
  191. int err = -ENOENT;
  192. __be16 type;
  193. skb->encapsulation = 1;
  194. skb_shinfo(skb)->gso_type = SKB_GSO_GRE;
  195. type = greh->protocol;
  196. if (greh->flags & GRE_KEY)
  197. grehlen += GRE_HEADER_SECTION;
  198. if (greh->flags & GRE_CSUM)
  199. grehlen += GRE_HEADER_SECTION;
  200. rcu_read_lock();
  201. ptype = gro_find_complete_by_type(type);
  202. if (ptype)
  203. err = ptype->callbacks.gro_complete(skb, nhoff + grehlen);
  204. rcu_read_unlock();
  205. skb_set_inner_mac_header(skb, nhoff + grehlen);
  206. return err;
  207. }
  208. static const struct net_offload gre_offload = {
  209. .callbacks = {
  210. .gso_segment = gre_gso_segment,
  211. .gro_receive = gre_gro_receive,
  212. .gro_complete = gre_gro_complete,
  213. },
  214. };
  215. static int __init gre_offload_init(void)
  216. {
  217. return inet_add_offload(&gre_offload, IPPROTO_GRE);
  218. }
  219. device_initcall(gre_offload_init);