gre_offload.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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) && !skb->csum_complete_sw)
  114. netdev_rx_csum_fault(skb->dev);
  115. } else {
  116. skb->ip_summed = CHECKSUM_COMPLETE;
  117. skb->csum_complete_sw = 1;
  118. }
  119. return sum;
  120. }
  121. static struct sk_buff **gre_gro_receive(struct sk_buff **head,
  122. struct sk_buff *skb)
  123. {
  124. struct sk_buff **pp = NULL;
  125. struct sk_buff *p;
  126. const struct gre_base_hdr *greh;
  127. unsigned int hlen, grehlen;
  128. unsigned int off;
  129. int flush = 1;
  130. struct packet_offload *ptype;
  131. __be16 type;
  132. off = skb_gro_offset(skb);
  133. hlen = off + sizeof(*greh);
  134. greh = skb_gro_header_fast(skb, off);
  135. if (skb_gro_header_hard(skb, hlen)) {
  136. greh = skb_gro_header_slow(skb, hlen, off);
  137. if (unlikely(!greh))
  138. goto out;
  139. }
  140. /* Only support version 0 and K (key), C (csum) flags. Note that
  141. * although the support for the S (seq#) flag can be added easily
  142. * for GRO, this is problematic for GSO hence can not be enabled
  143. * here because a GRO pkt may end up in the forwarding path, thus
  144. * requiring GSO support to break it up correctly.
  145. */
  146. if ((greh->flags & ~(GRE_KEY|GRE_CSUM)) != 0)
  147. goto out;
  148. type = greh->protocol;
  149. rcu_read_lock();
  150. ptype = gro_find_receive_by_type(type);
  151. if (ptype == NULL)
  152. goto out_unlock;
  153. grehlen = GRE_HEADER_SECTION;
  154. if (greh->flags & GRE_KEY)
  155. grehlen += GRE_HEADER_SECTION;
  156. if (greh->flags & GRE_CSUM)
  157. grehlen += GRE_HEADER_SECTION;
  158. hlen = off + grehlen;
  159. if (skb_gro_header_hard(skb, hlen)) {
  160. greh = skb_gro_header_slow(skb, hlen, off);
  161. if (unlikely(!greh))
  162. goto out_unlock;
  163. }
  164. if (greh->flags & GRE_CSUM) { /* Need to verify GRE csum first */
  165. __sum16 csum = 0;
  166. if (skb->ip_summed == CHECKSUM_COMPLETE)
  167. csum = csum_fold(NAPI_GRO_CB(skb)->csum);
  168. /* Don't trust csum error calculated/reported by h/w */
  169. if (skb->ip_summed == CHECKSUM_NONE || csum != 0)
  170. csum = gro_skb_checksum(skb);
  171. /* GRE CSUM is the 1's complement of the 1's complement sum
  172. * of the GRE hdr plus payload so it should add up to 0xffff
  173. * (and 0 after csum_fold()) just like the IPv4 hdr csum.
  174. */
  175. if (csum)
  176. goto out_unlock;
  177. }
  178. flush = 0;
  179. for (p = *head; p; p = p->next) {
  180. const struct gre_base_hdr *greh2;
  181. if (!NAPI_GRO_CB(p)->same_flow)
  182. continue;
  183. /* The following checks are needed to ensure only pkts
  184. * from the same tunnel are considered for aggregation.
  185. * The criteria for "the same tunnel" includes:
  186. * 1) same version (we only support version 0 here)
  187. * 2) same protocol (we only support ETH_P_IP for now)
  188. * 3) same set of flags
  189. * 4) same key if the key field is present.
  190. */
  191. greh2 = (struct gre_base_hdr *)(p->data + off);
  192. if (greh2->flags != greh->flags ||
  193. greh2->protocol != greh->protocol) {
  194. NAPI_GRO_CB(p)->same_flow = 0;
  195. continue;
  196. }
  197. if (greh->flags & GRE_KEY) {
  198. /* compare keys */
  199. if (*(__be32 *)(greh2+1) != *(__be32 *)(greh+1)) {
  200. NAPI_GRO_CB(p)->same_flow = 0;
  201. continue;
  202. }
  203. }
  204. }
  205. skb_gro_pull(skb, grehlen);
  206. /* Adjusted NAPI_GRO_CB(skb)->csum after skb_gro_pull()*/
  207. skb_gro_postpull_rcsum(skb, greh, grehlen);
  208. pp = ptype->callbacks.gro_receive(head, skb);
  209. out_unlock:
  210. rcu_read_unlock();
  211. out:
  212. NAPI_GRO_CB(skb)->flush |= flush;
  213. return pp;
  214. }
  215. static int gre_gro_complete(struct sk_buff *skb, int nhoff)
  216. {
  217. struct gre_base_hdr *greh = (struct gre_base_hdr *)(skb->data + nhoff);
  218. struct packet_offload *ptype;
  219. unsigned int grehlen = sizeof(*greh);
  220. int err = -ENOENT;
  221. __be16 type;
  222. skb->encapsulation = 1;
  223. skb_shinfo(skb)->gso_type = SKB_GSO_GRE;
  224. type = greh->protocol;
  225. if (greh->flags & GRE_KEY)
  226. grehlen += GRE_HEADER_SECTION;
  227. if (greh->flags & GRE_CSUM)
  228. grehlen += GRE_HEADER_SECTION;
  229. rcu_read_lock();
  230. ptype = gro_find_complete_by_type(type);
  231. if (ptype != NULL)
  232. err = ptype->callbacks.gro_complete(skb, nhoff + grehlen);
  233. rcu_read_unlock();
  234. return err;
  235. }
  236. static const struct net_offload gre_offload = {
  237. .callbacks = {
  238. .gso_send_check = gre_gso_send_check,
  239. .gso_segment = gre_gso_segment,
  240. .gro_receive = gre_gro_receive,
  241. .gro_complete = gre_gro_complete,
  242. },
  243. };
  244. static int __init gre_offload_init(void)
  245. {
  246. return inet_add_offload(&gre_offload, IPPROTO_GRE);
  247. }
  248. device_initcall(gre_offload_init);