gre_offload.c 7.1 KB

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