gre_offload.c 7.1 KB

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