gre_offload.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. int tnl_hlen = skb_inner_mac_header(skb) - skb_transport_header(skb);
  20. struct sk_buff *segs = ERR_PTR(-EINVAL);
  21. u16 mac_offset = skb->mac_header;
  22. __be16 protocol = skb->protocol;
  23. u16 mac_len = skb->mac_len;
  24. int gre_offset, outer_hlen;
  25. bool need_csum, gso_partial;
  26. if (!skb->encapsulation)
  27. goto out;
  28. if (unlikely(tnl_hlen < sizeof(struct gre_base_hdr)))
  29. goto out;
  30. if (unlikely(!pskb_may_pull(skb, tnl_hlen)))
  31. goto out;
  32. /* setup inner skb. */
  33. skb->encapsulation = 0;
  34. SKB_GSO_CB(skb)->encap_level = 0;
  35. __skb_pull(skb, tnl_hlen);
  36. skb_reset_mac_header(skb);
  37. skb_set_network_header(skb, skb_inner_network_offset(skb));
  38. skb->mac_len = skb_inner_network_offset(skb);
  39. skb->protocol = skb->inner_protocol;
  40. need_csum = !!(skb_shinfo(skb)->gso_type & SKB_GSO_GRE_CSUM);
  41. skb->encap_hdr_csum = need_csum;
  42. features &= skb->dev->hw_enc_features;
  43. /* segment inner packet. */
  44. segs = skb_mac_gso_segment(skb, features);
  45. if (IS_ERR_OR_NULL(segs)) {
  46. skb_gso_error_unwind(skb, protocol, tnl_hlen, mac_offset,
  47. mac_len);
  48. goto out;
  49. }
  50. gso_partial = !!(skb_shinfo(segs)->gso_type & SKB_GSO_PARTIAL);
  51. outer_hlen = skb_tnl_header_len(skb);
  52. gre_offset = outer_hlen - tnl_hlen;
  53. skb = segs;
  54. do {
  55. struct gre_base_hdr *greh;
  56. __sum16 *pcsum;
  57. /* Set up inner headers if we are offloading inner checksum */
  58. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  59. skb_reset_inner_headers(skb);
  60. skb->encapsulation = 1;
  61. }
  62. skb->mac_len = mac_len;
  63. skb->protocol = protocol;
  64. __skb_push(skb, outer_hlen);
  65. skb_reset_mac_header(skb);
  66. skb_set_network_header(skb, mac_len);
  67. skb_set_transport_header(skb, gre_offset);
  68. if (!need_csum)
  69. continue;
  70. greh = (struct gre_base_hdr *)skb_transport_header(skb);
  71. pcsum = (__sum16 *)(greh + 1);
  72. if (gso_partial && skb_is_gso(skb)) {
  73. unsigned int partial_adj;
  74. /* Adjust checksum to account for the fact that
  75. * the partial checksum is based on actual size
  76. * whereas headers should be based on MSS size.
  77. */
  78. partial_adj = skb->len + skb_headroom(skb) -
  79. SKB_GSO_CB(skb)->data_offset -
  80. skb_shinfo(skb)->gso_size;
  81. *pcsum = ~csum_fold((__force __wsum)htonl(partial_adj));
  82. } else {
  83. *pcsum = 0;
  84. }
  85. *(pcsum + 1) = 0;
  86. *pcsum = gso_make_checksum(skb, 0);
  87. } while ((skb = skb->next));
  88. out:
  89. return segs;
  90. }
  91. static struct sk_buff *gre_gro_receive(struct list_head *head,
  92. struct sk_buff *skb)
  93. {
  94. struct sk_buff *pp = NULL;
  95. struct sk_buff *p;
  96. const struct gre_base_hdr *greh;
  97. unsigned int hlen, grehlen;
  98. unsigned int off;
  99. int flush = 1;
  100. struct packet_offload *ptype;
  101. __be16 type;
  102. if (NAPI_GRO_CB(skb)->encap_mark)
  103. goto out;
  104. NAPI_GRO_CB(skb)->encap_mark = 1;
  105. off = skb_gro_offset(skb);
  106. hlen = off + sizeof(*greh);
  107. greh = skb_gro_header_fast(skb, off);
  108. if (skb_gro_header_hard(skb, hlen)) {
  109. greh = skb_gro_header_slow(skb, hlen, off);
  110. if (unlikely(!greh))
  111. goto out;
  112. }
  113. /* Only support version 0 and K (key), C (csum) flags. Note that
  114. * although the support for the S (seq#) flag can be added easily
  115. * for GRO, this is problematic for GSO hence can not be enabled
  116. * here because a GRO pkt may end up in the forwarding path, thus
  117. * requiring GSO support to break it up correctly.
  118. */
  119. if ((greh->flags & ~(GRE_KEY|GRE_CSUM)) != 0)
  120. goto out;
  121. /* We can only support GRE_CSUM if we can track the location of
  122. * the GRE header. In the case of FOU/GUE we cannot because the
  123. * outer UDP header displaces the GRE header leaving us in a state
  124. * of limbo.
  125. */
  126. if ((greh->flags & GRE_CSUM) && NAPI_GRO_CB(skb)->is_fou)
  127. goto out;
  128. type = greh->protocol;
  129. rcu_read_lock();
  130. ptype = gro_find_receive_by_type(type);
  131. if (!ptype)
  132. goto out_unlock;
  133. grehlen = GRE_HEADER_SECTION;
  134. if (greh->flags & GRE_KEY)
  135. grehlen += GRE_HEADER_SECTION;
  136. if (greh->flags & GRE_CSUM)
  137. grehlen += GRE_HEADER_SECTION;
  138. hlen = off + grehlen;
  139. if (skb_gro_header_hard(skb, hlen)) {
  140. greh = skb_gro_header_slow(skb, hlen, off);
  141. if (unlikely(!greh))
  142. goto out_unlock;
  143. }
  144. /* Don't bother verifying checksum if we're going to flush anyway. */
  145. if ((greh->flags & GRE_CSUM) && !NAPI_GRO_CB(skb)->flush) {
  146. if (skb_gro_checksum_simple_validate(skb))
  147. goto out_unlock;
  148. skb_gro_checksum_try_convert(skb, IPPROTO_GRE, 0,
  149. null_compute_pseudo);
  150. }
  151. list_for_each_entry(p, head, list) {
  152. const struct gre_base_hdr *greh2;
  153. if (!NAPI_GRO_CB(p)->same_flow)
  154. continue;
  155. /* The following checks are needed to ensure only pkts
  156. * from the same tunnel are considered for aggregation.
  157. * The criteria for "the same tunnel" includes:
  158. * 1) same version (we only support version 0 here)
  159. * 2) same protocol (we only support ETH_P_IP for now)
  160. * 3) same set of flags
  161. * 4) same key if the key field is present.
  162. */
  163. greh2 = (struct gre_base_hdr *)(p->data + off);
  164. if (greh2->flags != greh->flags ||
  165. greh2->protocol != greh->protocol) {
  166. NAPI_GRO_CB(p)->same_flow = 0;
  167. continue;
  168. }
  169. if (greh->flags & GRE_KEY) {
  170. /* compare keys */
  171. if (*(__be32 *)(greh2+1) != *(__be32 *)(greh+1)) {
  172. NAPI_GRO_CB(p)->same_flow = 0;
  173. continue;
  174. }
  175. }
  176. }
  177. skb_gro_pull(skb, grehlen);
  178. /* Adjusted NAPI_GRO_CB(skb)->csum after skb_gro_pull()*/
  179. skb_gro_postpull_rcsum(skb, greh, grehlen);
  180. pp = call_gro_receive(ptype->callbacks.gro_receive, head, skb);
  181. flush = 0;
  182. out_unlock:
  183. rcu_read_unlock();
  184. out:
  185. skb_gro_flush_final(skb, pp, flush);
  186. return pp;
  187. }
  188. static int gre_gro_complete(struct sk_buff *skb, int nhoff)
  189. {
  190. struct gre_base_hdr *greh = (struct gre_base_hdr *)(skb->data + nhoff);
  191. struct packet_offload *ptype;
  192. unsigned int grehlen = sizeof(*greh);
  193. int err = -ENOENT;
  194. __be16 type;
  195. skb->encapsulation = 1;
  196. skb_shinfo(skb)->gso_type = SKB_GSO_GRE;
  197. type = greh->protocol;
  198. if (greh->flags & GRE_KEY)
  199. grehlen += GRE_HEADER_SECTION;
  200. if (greh->flags & GRE_CSUM)
  201. grehlen += GRE_HEADER_SECTION;
  202. rcu_read_lock();
  203. ptype = gro_find_complete_by_type(type);
  204. if (ptype)
  205. err = ptype->callbacks.gro_complete(skb, nhoff + grehlen);
  206. rcu_read_unlock();
  207. skb_set_inner_mac_header(skb, nhoff + grehlen);
  208. return err;
  209. }
  210. static const struct net_offload gre_offload = {
  211. .callbacks = {
  212. .gso_segment = gre_gso_segment,
  213. .gro_receive = gre_gro_receive,
  214. .gro_complete = gre_gro_complete,
  215. },
  216. };
  217. static int __init gre_offload_init(void)
  218. {
  219. int err;
  220. err = inet_add_offload(&gre_offload, IPPROTO_GRE);
  221. #if IS_ENABLED(CONFIG_IPV6)
  222. if (err)
  223. return err;
  224. err = inet6_add_offload(&gre_offload, IPPROTO_GRE);
  225. if (err)
  226. inet_del_offload(&gre_offload, IPPROTO_GRE);
  227. #endif
  228. return err;
  229. }
  230. device_initcall(gre_offload_init);