gre_offload.c 7.3 KB

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