gre_offload.c 7.2 KB

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