gre_offload.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. struct sk_buff *segs = ERR_PTR(-EINVAL);
  20. netdev_features_t enc_features;
  21. int ghl;
  22. struct gre_base_hdr *greh;
  23. u16 mac_offset = skb->mac_header;
  24. int mac_len = skb->mac_len;
  25. __be16 protocol = skb->protocol;
  26. int tnl_hlen;
  27. bool csum;
  28. if (unlikely(skb_shinfo(skb)->gso_type &
  29. ~(SKB_GSO_TCPV4 |
  30. SKB_GSO_TCPV6 |
  31. SKB_GSO_UDP |
  32. SKB_GSO_DODGY |
  33. SKB_GSO_TCP_ECN |
  34. SKB_GSO_GRE |
  35. SKB_GSO_GRE_CSUM |
  36. SKB_GSO_IPIP |
  37. SKB_GSO_SIT)))
  38. goto out;
  39. if (!skb->encapsulation)
  40. goto out;
  41. if (unlikely(!pskb_may_pull(skb, sizeof(*greh))))
  42. goto out;
  43. greh = (struct gre_base_hdr *)skb_transport_header(skb);
  44. ghl = skb_inner_mac_header(skb) - skb_transport_header(skb);
  45. if (unlikely(ghl < sizeof(*greh)))
  46. goto out;
  47. csum = !!(greh->flags & GRE_CSUM);
  48. if (csum)
  49. skb->encap_hdr_csum = 1;
  50. /* setup inner skb. */
  51. skb->protocol = greh->protocol;
  52. skb->encapsulation = 0;
  53. if (unlikely(!pskb_may_pull(skb, ghl)))
  54. goto out;
  55. __skb_pull(skb, ghl);
  56. skb_reset_mac_header(skb);
  57. skb_set_network_header(skb, skb_inner_network_offset(skb));
  58. skb->mac_len = skb_inner_network_offset(skb);
  59. /* segment inner packet. */
  60. enc_features = skb->dev->hw_enc_features & features;
  61. segs = skb_mac_gso_segment(skb, enc_features);
  62. if (IS_ERR_OR_NULL(segs)) {
  63. skb_gso_error_unwind(skb, protocol, ghl, mac_offset, mac_len);
  64. goto out;
  65. }
  66. skb = segs;
  67. tnl_hlen = skb_tnl_header_len(skb);
  68. do {
  69. __skb_push(skb, ghl);
  70. if (csum) {
  71. __be32 *pcsum;
  72. if (skb_has_shared_frag(skb)) {
  73. int err;
  74. err = __skb_linearize(skb);
  75. if (err) {
  76. kfree_skb_list(segs);
  77. segs = ERR_PTR(err);
  78. goto out;
  79. }
  80. }
  81. skb_reset_transport_header(skb);
  82. greh = (struct gre_base_hdr *)
  83. skb_transport_header(skb);
  84. pcsum = (__be32 *)(greh + 1);
  85. *pcsum = 0;
  86. *(__sum16 *)pcsum = gso_make_checksum(skb, 0);
  87. }
  88. __skb_push(skb, tnl_hlen - ghl);
  89. skb_reset_inner_headers(skb);
  90. skb->encapsulation = 1;
  91. skb_reset_mac_header(skb);
  92. skb_set_network_header(skb, mac_len);
  93. skb->mac_len = mac_len;
  94. skb->protocol = protocol;
  95. } while ((skb = skb->next));
  96. out:
  97. return segs;
  98. }
  99. static struct sk_buff **gre_gro_receive(struct sk_buff **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. type = greh->protocol;
  130. rcu_read_lock();
  131. ptype = gro_find_receive_by_type(type);
  132. if (!ptype)
  133. goto out_unlock;
  134. grehlen = GRE_HEADER_SECTION;
  135. if (greh->flags & GRE_KEY)
  136. grehlen += GRE_HEADER_SECTION;
  137. if (greh->flags & GRE_CSUM)
  138. grehlen += GRE_HEADER_SECTION;
  139. hlen = off + grehlen;
  140. if (skb_gro_header_hard(skb, hlen)) {
  141. greh = skb_gro_header_slow(skb, hlen, off);
  142. if (unlikely(!greh))
  143. goto out_unlock;
  144. }
  145. /* Don't bother verifying checksum if we're going to flush anyway. */
  146. if ((greh->flags & GRE_CSUM) && !NAPI_GRO_CB(skb)->flush) {
  147. if (skb_gro_checksum_simple_validate(skb))
  148. goto out_unlock;
  149. skb_gro_checksum_try_convert(skb, IPPROTO_GRE, 0,
  150. null_compute_pseudo);
  151. }
  152. flush = 0;
  153. for (p = *head; p; p = p->next) {
  154. const struct gre_base_hdr *greh2;
  155. if (!NAPI_GRO_CB(p)->same_flow)
  156. continue;
  157. /* The following checks are needed to ensure only pkts
  158. * from the same tunnel are considered for aggregation.
  159. * The criteria for "the same tunnel" includes:
  160. * 1) same version (we only support version 0 here)
  161. * 2) same protocol (we only support ETH_P_IP for now)
  162. * 3) same set of flags
  163. * 4) same key if the key field is present.
  164. */
  165. greh2 = (struct gre_base_hdr *)(p->data + off);
  166. if (greh2->flags != greh->flags ||
  167. greh2->protocol != greh->protocol) {
  168. NAPI_GRO_CB(p)->same_flow = 0;
  169. continue;
  170. }
  171. if (greh->flags & GRE_KEY) {
  172. /* compare keys */
  173. if (*(__be32 *)(greh2+1) != *(__be32 *)(greh+1)) {
  174. NAPI_GRO_CB(p)->same_flow = 0;
  175. continue;
  176. }
  177. }
  178. }
  179. skb_gro_pull(skb, grehlen);
  180. /* Adjusted NAPI_GRO_CB(skb)->csum after skb_gro_pull()*/
  181. skb_gro_postpull_rcsum(skb, greh, grehlen);
  182. pp = call_gro_receive(ptype->callbacks.gro_receive, head, skb);
  183. out_unlock:
  184. rcu_read_unlock();
  185. out:
  186. NAPI_GRO_CB(skb)->flush |= flush;
  187. return pp;
  188. }
  189. static int gre_gro_complete(struct sk_buff *skb, int nhoff)
  190. {
  191. struct gre_base_hdr *greh = (struct gre_base_hdr *)(skb->data + nhoff);
  192. struct packet_offload *ptype;
  193. unsigned int grehlen = sizeof(*greh);
  194. int err = -ENOENT;
  195. __be16 type;
  196. skb->encapsulation = 1;
  197. skb_shinfo(skb)->gso_type = SKB_GSO_GRE;
  198. type = greh->protocol;
  199. if (greh->flags & GRE_KEY)
  200. grehlen += GRE_HEADER_SECTION;
  201. if (greh->flags & GRE_CSUM)
  202. grehlen += GRE_HEADER_SECTION;
  203. rcu_read_lock();
  204. ptype = gro_find_complete_by_type(type);
  205. if (ptype)
  206. err = ptype->callbacks.gro_complete(skb, nhoff + grehlen);
  207. rcu_read_unlock();
  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. return inet_add_offload(&gre_offload, IPPROTO_GRE);
  221. }
  222. device_initcall(gre_offload_init);