gre_offload.c 7.3 KB

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