gre_offload.c 7.0 KB

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