gre_offload.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 int gre_gso_send_check(struct sk_buff *skb)
  17. {
  18. if (!skb->encapsulation)
  19. return -EINVAL;
  20. return 0;
  21. }
  22. static struct sk_buff *gre_gso_segment(struct sk_buff *skb,
  23. netdev_features_t features)
  24. {
  25. struct sk_buff *segs = ERR_PTR(-EINVAL);
  26. netdev_features_t enc_features;
  27. int ghl;
  28. struct gre_base_hdr *greh;
  29. u16 mac_offset = skb->mac_header;
  30. int mac_len = skb->mac_len;
  31. __be16 protocol = skb->protocol;
  32. int tnl_hlen;
  33. bool csum;
  34. if (unlikely(skb_shinfo(skb)->gso_type &
  35. ~(SKB_GSO_TCPV4 |
  36. SKB_GSO_TCPV6 |
  37. SKB_GSO_UDP |
  38. SKB_GSO_DODGY |
  39. SKB_GSO_TCP_ECN |
  40. SKB_GSO_GRE |
  41. SKB_GSO_IPIP)))
  42. goto out;
  43. if (unlikely(!pskb_may_pull(skb, sizeof(*greh))))
  44. goto out;
  45. greh = (struct gre_base_hdr *)skb_transport_header(skb);
  46. ghl = skb_inner_network_header(skb) - skb_transport_header(skb);
  47. if (unlikely(ghl < sizeof(*greh)))
  48. goto out;
  49. csum = !!(greh->flags & GRE_CSUM);
  50. if (unlikely(!pskb_may_pull(skb, ghl)))
  51. goto out;
  52. /* setup inner skb. */
  53. skb->protocol = greh->protocol;
  54. skb->encapsulation = 0;
  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 & netif_skb_features(skb);
  61. segs = skb_mac_gso_segment(skb, enc_features);
  62. if (!segs || IS_ERR(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. greh = (struct gre_base_hdr *)(skb->data);
  82. pcsum = (__be32 *)(greh + 1);
  83. *pcsum = 0;
  84. *(__sum16 *)pcsum = csum_fold(skb_checksum(skb, 0, skb->len, 0));
  85. }
  86. __skb_push(skb, tnl_hlen - ghl);
  87. skb_reset_inner_headers(skb);
  88. skb->encapsulation = 1;
  89. skb_reset_mac_header(skb);
  90. skb_set_network_header(skb, mac_len);
  91. skb->mac_len = mac_len;
  92. skb->protocol = protocol;
  93. } while ((skb = skb->next));
  94. out:
  95. return segs;
  96. }
  97. /* Compute the whole skb csum in s/w and store it, then verify GRO csum
  98. * starting from gro_offset.
  99. */
  100. static __sum16 gro_skb_checksum(struct sk_buff *skb)
  101. {
  102. __sum16 sum;
  103. skb->csum = skb_checksum(skb, 0, skb->len, 0);
  104. NAPI_GRO_CB(skb)->csum = csum_sub(skb->csum,
  105. csum_partial(skb->data, skb_gro_offset(skb), 0));
  106. sum = csum_fold(NAPI_GRO_CB(skb)->csum);
  107. if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE)) {
  108. if (unlikely(!sum))
  109. netdev_rx_csum_fault(skb->dev);
  110. } else
  111. skb->ip_summed = CHECKSUM_COMPLETE;
  112. return sum;
  113. }
  114. static struct sk_buff **gre_gro_receive(struct sk_buff **head,
  115. struct sk_buff *skb)
  116. {
  117. struct sk_buff **pp = NULL;
  118. struct sk_buff *p;
  119. const struct gre_base_hdr *greh;
  120. unsigned int hlen, grehlen;
  121. unsigned int off;
  122. int flush = 1;
  123. struct packet_offload *ptype;
  124. __be16 type;
  125. off = skb_gro_offset(skb);
  126. hlen = off + sizeof(*greh);
  127. greh = skb_gro_header_fast(skb, off);
  128. if (skb_gro_header_hard(skb, hlen)) {
  129. greh = skb_gro_header_slow(skb, hlen, off);
  130. if (unlikely(!greh))
  131. goto out;
  132. }
  133. /* Only support version 0 and K (key), C (csum) flags. Note that
  134. * although the support for the S (seq#) flag can be added easily
  135. * for GRO, this is problematic for GSO hence can not be enabled
  136. * here because a GRO pkt may end up in the forwarding path, thus
  137. * requiring GSO support to break it up correctly.
  138. */
  139. if ((greh->flags & ~(GRE_KEY|GRE_CSUM)) != 0)
  140. goto out;
  141. type = greh->protocol;
  142. rcu_read_lock();
  143. ptype = gro_find_receive_by_type(type);
  144. if (ptype == NULL)
  145. goto out_unlock;
  146. grehlen = GRE_HEADER_SECTION;
  147. if (greh->flags & GRE_KEY)
  148. grehlen += GRE_HEADER_SECTION;
  149. if (greh->flags & GRE_CSUM)
  150. grehlen += GRE_HEADER_SECTION;
  151. hlen = off + grehlen;
  152. if (skb_gro_header_hard(skb, hlen)) {
  153. greh = skb_gro_header_slow(skb, hlen, off);
  154. if (unlikely(!greh))
  155. goto out_unlock;
  156. }
  157. if (greh->flags & GRE_CSUM) { /* Need to verify GRE csum first */
  158. __sum16 csum = 0;
  159. if (skb->ip_summed == CHECKSUM_COMPLETE)
  160. csum = csum_fold(NAPI_GRO_CB(skb)->csum);
  161. /* Don't trust csum error calculated/reported by h/w */
  162. if (skb->ip_summed == CHECKSUM_NONE || csum != 0)
  163. csum = gro_skb_checksum(skb);
  164. /* GRE CSUM is the 1's complement of the 1's complement sum
  165. * of the GRE hdr plus payload so it should add up to 0xffff
  166. * (and 0 after csum_fold()) just like the IPv4 hdr csum.
  167. */
  168. if (csum)
  169. goto out_unlock;
  170. }
  171. flush = 0;
  172. for (p = *head; p; p = p->next) {
  173. const struct gre_base_hdr *greh2;
  174. if (!NAPI_GRO_CB(p)->same_flow)
  175. continue;
  176. /* The following checks are needed to ensure only pkts
  177. * from the same tunnel are considered for aggregation.
  178. * The criteria for "the same tunnel" includes:
  179. * 1) same version (we only support version 0 here)
  180. * 2) same protocol (we only support ETH_P_IP for now)
  181. * 3) same set of flags
  182. * 4) same key if the key field is present.
  183. */
  184. greh2 = (struct gre_base_hdr *)(p->data + off);
  185. if (greh2->flags != greh->flags ||
  186. greh2->protocol != greh->protocol) {
  187. NAPI_GRO_CB(p)->same_flow = 0;
  188. continue;
  189. }
  190. if (greh->flags & GRE_KEY) {
  191. /* compare keys */
  192. if (*(__be32 *)(greh2+1) != *(__be32 *)(greh+1)) {
  193. NAPI_GRO_CB(p)->same_flow = 0;
  194. continue;
  195. }
  196. }
  197. }
  198. skb_gro_pull(skb, grehlen);
  199. /* Adjusted NAPI_GRO_CB(skb)->csum after skb_gro_pull()*/
  200. skb_gro_postpull_rcsum(skb, greh, grehlen);
  201. pp = ptype->callbacks.gro_receive(head, skb);
  202. out_unlock:
  203. rcu_read_unlock();
  204. out:
  205. NAPI_GRO_CB(skb)->flush |= flush;
  206. return pp;
  207. }
  208. static int gre_gro_complete(struct sk_buff *skb, int nhoff)
  209. {
  210. struct gre_base_hdr *greh = (struct gre_base_hdr *)(skb->data + nhoff);
  211. struct packet_offload *ptype;
  212. unsigned int grehlen = sizeof(*greh);
  213. int err = -ENOENT;
  214. __be16 type;
  215. type = greh->protocol;
  216. if (greh->flags & GRE_KEY)
  217. grehlen += GRE_HEADER_SECTION;
  218. if (greh->flags & GRE_CSUM)
  219. grehlen += GRE_HEADER_SECTION;
  220. rcu_read_lock();
  221. ptype = gro_find_complete_by_type(type);
  222. if (ptype != NULL)
  223. err = ptype->callbacks.gro_complete(skb, nhoff + grehlen);
  224. rcu_read_unlock();
  225. return err;
  226. }
  227. static const struct net_offload gre_offload = {
  228. .callbacks = {
  229. .gso_send_check = gre_gso_send_check,
  230. .gso_segment = gre_gso_segment,
  231. .gro_receive = gre_gro_receive,
  232. .gro_complete = gre_gro_complete,
  233. },
  234. };
  235. static int __init gre_offload_init(void)
  236. {
  237. return inet_add_offload(&gre_offload, IPPROTO_GRE);
  238. }
  239. device_initcall(gre_offload_init);