gre_offload.c 7.2 KB

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