gre_offload.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. goto out;
  38. if (!skb->encapsulation)
  39. goto out;
  40. if (unlikely(!pskb_may_pull(skb, sizeof(*greh))))
  41. goto out;
  42. greh = (struct gre_base_hdr *)skb_transport_header(skb);
  43. ghl = skb_inner_mac_header(skb) - skb_transport_header(skb);
  44. if (unlikely(ghl < sizeof(*greh)))
  45. goto out;
  46. csum = !!(greh->flags & GRE_CSUM);
  47. if (csum)
  48. skb->encap_hdr_csum = 1;
  49. /* setup inner skb. */
  50. skb->protocol = greh->protocol;
  51. skb->encapsulation = 0;
  52. if (unlikely(!pskb_may_pull(skb, ghl)))
  53. goto out;
  54. __skb_pull(skb, ghl);
  55. skb_reset_mac_header(skb);
  56. skb_set_network_header(skb, skb_inner_network_offset(skb));
  57. skb->mac_len = skb_inner_network_offset(skb);
  58. /* segment inner packet. */
  59. enc_features = skb->dev->hw_enc_features & features;
  60. segs = skb_mac_gso_segment(skb, enc_features);
  61. if (IS_ERR_OR_NULL(segs)) {
  62. skb_gso_error_unwind(skb, protocol, ghl, mac_offset, mac_len);
  63. goto out;
  64. }
  65. skb = segs;
  66. tnl_hlen = skb_tnl_header_len(skb);
  67. do {
  68. __skb_push(skb, ghl);
  69. if (csum) {
  70. __be32 *pcsum;
  71. if (skb_has_shared_frag(skb)) {
  72. int err;
  73. err = __skb_linearize(skb);
  74. if (err) {
  75. kfree_skb_list(segs);
  76. segs = ERR_PTR(err);
  77. goto out;
  78. }
  79. }
  80. skb_reset_transport_header(skb);
  81. greh = (struct gre_base_hdr *)
  82. skb_transport_header(skb);
  83. pcsum = (__be32 *)(greh + 1);
  84. *pcsum = 0;
  85. *(__sum16 *)pcsum = gso_make_checksum(skb, 0);
  86. }
  87. __skb_push(skb, tnl_hlen - ghl);
  88. skb_reset_inner_headers(skb);
  89. skb->encapsulation = 1;
  90. skb_reset_mac_header(skb);
  91. skb_set_network_header(skb, mac_len);
  92. skb->mac_len = mac_len;
  93. skb->protocol = protocol;
  94. } while ((skb = skb->next));
  95. out:
  96. return segs;
  97. }
  98. static struct sk_buff **gre_gro_receive(struct sk_buff **head,
  99. struct sk_buff *skb)
  100. {
  101. struct sk_buff **pp = NULL;
  102. struct sk_buff *p;
  103. const struct gre_base_hdr *greh;
  104. unsigned int hlen, grehlen;
  105. unsigned int off;
  106. int flush = 1;
  107. struct packet_offload *ptype;
  108. __be16 type;
  109. off = skb_gro_offset(skb);
  110. hlen = off + sizeof(*greh);
  111. greh = skb_gro_header_fast(skb, off);
  112. if (skb_gro_header_hard(skb, hlen)) {
  113. greh = skb_gro_header_slow(skb, hlen, off);
  114. if (unlikely(!greh))
  115. goto out;
  116. }
  117. /* Only support version 0 and K (key), C (csum) flags. Note that
  118. * although the support for the S (seq#) flag can be added easily
  119. * for GRO, this is problematic for GSO hence can not be enabled
  120. * here because a GRO pkt may end up in the forwarding path, thus
  121. * requiring GSO support to break it up correctly.
  122. */
  123. if ((greh->flags & ~(GRE_KEY|GRE_CSUM)) != 0)
  124. goto out;
  125. type = greh->protocol;
  126. rcu_read_lock();
  127. ptype = gro_find_receive_by_type(type);
  128. if (ptype == NULL)
  129. goto out_unlock;
  130. grehlen = GRE_HEADER_SECTION;
  131. if (greh->flags & GRE_KEY)
  132. grehlen += GRE_HEADER_SECTION;
  133. if (greh->flags & GRE_CSUM)
  134. grehlen += GRE_HEADER_SECTION;
  135. hlen = off + grehlen;
  136. if (skb_gro_header_hard(skb, hlen)) {
  137. greh = skb_gro_header_slow(skb, hlen, off);
  138. if (unlikely(!greh))
  139. goto out_unlock;
  140. }
  141. /* Don't bother verifying checksum if we're going to flush anyway. */
  142. if ((greh->flags & GRE_CSUM) && !NAPI_GRO_CB(skb)->flush) {
  143. if (skb_gro_checksum_simple_validate(skb))
  144. goto out_unlock;
  145. skb_gro_checksum_try_convert(skb, IPPROTO_GRE, 0,
  146. null_compute_pseudo);
  147. }
  148. flush = 0;
  149. for (p = *head; p; p = p->next) {
  150. const struct gre_base_hdr *greh2;
  151. if (!NAPI_GRO_CB(p)->same_flow)
  152. continue;
  153. /* The following checks are needed to ensure only pkts
  154. * from the same tunnel are considered for aggregation.
  155. * The criteria for "the same tunnel" includes:
  156. * 1) same version (we only support version 0 here)
  157. * 2) same protocol (we only support ETH_P_IP for now)
  158. * 3) same set of flags
  159. * 4) same key if the key field is present.
  160. */
  161. greh2 = (struct gre_base_hdr *)(p->data + off);
  162. if (greh2->flags != greh->flags ||
  163. greh2->protocol != greh->protocol) {
  164. NAPI_GRO_CB(p)->same_flow = 0;
  165. continue;
  166. }
  167. if (greh->flags & GRE_KEY) {
  168. /* compare keys */
  169. if (*(__be32 *)(greh2+1) != *(__be32 *)(greh+1)) {
  170. NAPI_GRO_CB(p)->same_flow = 0;
  171. continue;
  172. }
  173. }
  174. }
  175. skb_gro_pull(skb, grehlen);
  176. /* Adjusted NAPI_GRO_CB(skb)->csum after skb_gro_pull()*/
  177. skb_gro_postpull_rcsum(skb, greh, grehlen);
  178. pp = ptype->callbacks.gro_receive(head, skb);
  179. out_unlock:
  180. rcu_read_unlock();
  181. out:
  182. NAPI_GRO_CB(skb)->flush |= flush;
  183. return pp;
  184. }
  185. static int gre_gro_complete(struct sk_buff *skb, int nhoff)
  186. {
  187. struct gre_base_hdr *greh = (struct gre_base_hdr *)(skb->data + nhoff);
  188. struct packet_offload *ptype;
  189. unsigned int grehlen = sizeof(*greh);
  190. int err = -ENOENT;
  191. __be16 type;
  192. skb->encapsulation = 1;
  193. skb_shinfo(skb)->gso_type = SKB_GSO_GRE;
  194. type = greh->protocol;
  195. if (greh->flags & GRE_KEY)
  196. grehlen += GRE_HEADER_SECTION;
  197. if (greh->flags & GRE_CSUM)
  198. grehlen += GRE_HEADER_SECTION;
  199. rcu_read_lock();
  200. ptype = gro_find_complete_by_type(type);
  201. if (ptype != NULL)
  202. err = ptype->callbacks.gro_complete(skb, nhoff + grehlen);
  203. rcu_read_unlock();
  204. skb_set_inner_mac_header(skb, nhoff + grehlen);
  205. return err;
  206. }
  207. static const struct net_offload gre_offload = {
  208. .callbacks = {
  209. .gso_segment = gre_gso_segment,
  210. .gro_receive = gre_gro_receive,
  211. .gro_complete = gre_gro_complete,
  212. },
  213. };
  214. static int __init gre_offload_init(void)
  215. {
  216. return inet_add_offload(&gre_offload, IPPROTO_GRE);
  217. }
  218. device_initcall(gre_offload_init);