tcp_offload.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. * TCPv4 GSO/GRO support
  11. */
  12. #include <linux/skbuff.h>
  13. #include <net/tcp.h>
  14. #include <net/protocol.h>
  15. struct sk_buff *tcp_gso_segment(struct sk_buff *skb,
  16. netdev_features_t features)
  17. {
  18. struct sk_buff *segs = ERR_PTR(-EINVAL);
  19. unsigned int sum_truesize = 0;
  20. struct tcphdr *th;
  21. unsigned int thlen;
  22. unsigned int seq;
  23. __be32 delta;
  24. unsigned int oldlen;
  25. unsigned int mss;
  26. struct sk_buff *gso_skb = skb;
  27. __sum16 newcheck;
  28. bool ooo_okay, copy_destructor;
  29. if (!pskb_may_pull(skb, sizeof(*th)))
  30. goto out;
  31. th = tcp_hdr(skb);
  32. thlen = th->doff * 4;
  33. if (thlen < sizeof(*th))
  34. goto out;
  35. if (!pskb_may_pull(skb, thlen))
  36. goto out;
  37. oldlen = (u16)~skb->len;
  38. __skb_pull(skb, thlen);
  39. mss = tcp_skb_mss(skb);
  40. if (unlikely(skb->len <= mss))
  41. goto out;
  42. if (skb_gso_ok(skb, features | NETIF_F_GSO_ROBUST)) {
  43. /* Packet is from an untrusted source, reset gso_segs. */
  44. int type = skb_shinfo(skb)->gso_type;
  45. if (unlikely(type &
  46. ~(SKB_GSO_TCPV4 |
  47. SKB_GSO_DODGY |
  48. SKB_GSO_TCP_ECN |
  49. SKB_GSO_TCPV6 |
  50. SKB_GSO_GRE |
  51. SKB_GSO_GRE_CSUM |
  52. SKB_GSO_IPIP |
  53. SKB_GSO_SIT |
  54. SKB_GSO_MPLS |
  55. SKB_GSO_UDP_TUNNEL |
  56. SKB_GSO_UDP_TUNNEL_CSUM |
  57. 0) ||
  58. !(type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))))
  59. goto out;
  60. skb_shinfo(skb)->gso_segs = DIV_ROUND_UP(skb->len, mss);
  61. segs = NULL;
  62. goto out;
  63. }
  64. copy_destructor = gso_skb->destructor == tcp_wfree;
  65. ooo_okay = gso_skb->ooo_okay;
  66. /* All segments but the first should have ooo_okay cleared */
  67. skb->ooo_okay = 0;
  68. segs = skb_segment(skb, features);
  69. if (IS_ERR(segs))
  70. goto out;
  71. /* Only first segment might have ooo_okay set */
  72. segs->ooo_okay = ooo_okay;
  73. delta = htonl(oldlen + (thlen + mss));
  74. skb = segs;
  75. th = tcp_hdr(skb);
  76. seq = ntohl(th->seq);
  77. newcheck = ~csum_fold((__force __wsum)((__force u32)th->check +
  78. (__force u32)delta));
  79. do {
  80. th->fin = th->psh = 0;
  81. th->check = newcheck;
  82. if (skb->ip_summed != CHECKSUM_PARTIAL)
  83. th->check = gso_make_checksum(skb, ~th->check);
  84. seq += mss;
  85. if (copy_destructor) {
  86. skb->destructor = gso_skb->destructor;
  87. skb->sk = gso_skb->sk;
  88. sum_truesize += skb->truesize;
  89. }
  90. skb = skb->next;
  91. th = tcp_hdr(skb);
  92. th->seq = htonl(seq);
  93. th->cwr = 0;
  94. } while (skb->next);
  95. /* Following permits TCP Small Queues to work well with GSO :
  96. * The callback to TCP stack will be called at the time last frag
  97. * is freed at TX completion, and not right now when gso_skb
  98. * is freed by GSO engine
  99. */
  100. if (copy_destructor) {
  101. swap(gso_skb->sk, skb->sk);
  102. swap(gso_skb->destructor, skb->destructor);
  103. sum_truesize += skb->truesize;
  104. atomic_add(sum_truesize - gso_skb->truesize,
  105. &skb->sk->sk_wmem_alloc);
  106. }
  107. delta = htonl(oldlen + (skb_tail_pointer(skb) -
  108. skb_transport_header(skb)) +
  109. skb->data_len);
  110. th->check = ~csum_fold((__force __wsum)((__force u32)th->check +
  111. (__force u32)delta));
  112. if (skb->ip_summed != CHECKSUM_PARTIAL)
  113. th->check = gso_make_checksum(skb, ~th->check);
  114. out:
  115. return segs;
  116. }
  117. struct sk_buff **tcp_gro_receive(struct sk_buff **head, struct sk_buff *skb)
  118. {
  119. struct sk_buff **pp = NULL;
  120. struct sk_buff *p;
  121. struct tcphdr *th;
  122. struct tcphdr *th2;
  123. unsigned int len;
  124. unsigned int thlen;
  125. __be32 flags;
  126. unsigned int mss = 1;
  127. unsigned int hlen;
  128. unsigned int off;
  129. int flush = 1;
  130. int i;
  131. off = skb_gro_offset(skb);
  132. hlen = off + sizeof(*th);
  133. th = skb_gro_header_fast(skb, off);
  134. if (skb_gro_header_hard(skb, hlen)) {
  135. th = skb_gro_header_slow(skb, hlen, off);
  136. if (unlikely(!th))
  137. goto out;
  138. }
  139. thlen = th->doff * 4;
  140. if (thlen < sizeof(*th))
  141. goto out;
  142. hlen = off + thlen;
  143. if (skb_gro_header_hard(skb, hlen)) {
  144. th = skb_gro_header_slow(skb, hlen, off);
  145. if (unlikely(!th))
  146. goto out;
  147. }
  148. skb_gro_pull(skb, thlen);
  149. len = skb_gro_len(skb);
  150. flags = tcp_flag_word(th);
  151. for (; (p = *head); head = &p->next) {
  152. if (!NAPI_GRO_CB(p)->same_flow)
  153. continue;
  154. th2 = tcp_hdr(p);
  155. if (*(u32 *)&th->source ^ *(u32 *)&th2->source) {
  156. NAPI_GRO_CB(p)->same_flow = 0;
  157. continue;
  158. }
  159. goto found;
  160. }
  161. goto out_check_final;
  162. found:
  163. /* Include the IP ID check below from the inner most IP hdr */
  164. flush = NAPI_GRO_CB(p)->flush | NAPI_GRO_CB(p)->flush_id;
  165. flush |= (__force int)(flags & TCP_FLAG_CWR);
  166. flush |= (__force int)((flags ^ tcp_flag_word(th2)) &
  167. ~(TCP_FLAG_CWR | TCP_FLAG_FIN | TCP_FLAG_PSH));
  168. flush |= (__force int)(th->ack_seq ^ th2->ack_seq);
  169. for (i = sizeof(*th); i < thlen; i += 4)
  170. flush |= *(u32 *)((u8 *)th + i) ^
  171. *(u32 *)((u8 *)th2 + i);
  172. mss = tcp_skb_mss(p);
  173. flush |= (len - 1) >= mss;
  174. flush |= (ntohl(th2->seq) + skb_gro_len(p)) ^ ntohl(th->seq);
  175. if (flush || skb_gro_receive(head, skb)) {
  176. mss = 1;
  177. goto out_check_final;
  178. }
  179. p = *head;
  180. th2 = tcp_hdr(p);
  181. tcp_flag_word(th2) |= flags & (TCP_FLAG_FIN | TCP_FLAG_PSH);
  182. out_check_final:
  183. flush = len < mss;
  184. flush |= (__force int)(flags & (TCP_FLAG_URG | TCP_FLAG_PSH |
  185. TCP_FLAG_RST | TCP_FLAG_SYN |
  186. TCP_FLAG_FIN));
  187. if (p && (!NAPI_GRO_CB(skb)->same_flow || flush))
  188. pp = head;
  189. out:
  190. NAPI_GRO_CB(skb)->flush |= (flush != 0);
  191. return pp;
  192. }
  193. int tcp_gro_complete(struct sk_buff *skb)
  194. {
  195. struct tcphdr *th = tcp_hdr(skb);
  196. skb->csum_start = (unsigned char *)th - skb->head;
  197. skb->csum_offset = offsetof(struct tcphdr, check);
  198. skb->ip_summed = CHECKSUM_PARTIAL;
  199. skb_shinfo(skb)->gso_segs = NAPI_GRO_CB(skb)->count;
  200. if (th->cwr)
  201. skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
  202. return 0;
  203. }
  204. EXPORT_SYMBOL(tcp_gro_complete);
  205. static int tcp_v4_gso_send_check(struct sk_buff *skb)
  206. {
  207. const struct iphdr *iph;
  208. struct tcphdr *th;
  209. if (!pskb_may_pull(skb, sizeof(*th)))
  210. return -EINVAL;
  211. iph = ip_hdr(skb);
  212. th = tcp_hdr(skb);
  213. th->check = 0;
  214. skb->ip_summed = CHECKSUM_PARTIAL;
  215. __tcp_v4_send_check(skb, iph->saddr, iph->daddr);
  216. return 0;
  217. }
  218. static struct sk_buff **tcp4_gro_receive(struct sk_buff **head, struct sk_buff *skb)
  219. {
  220. /* Use the IP hdr immediately proceeding for this transport */
  221. const struct iphdr *iph = skb_gro_network_header(skb);
  222. __wsum wsum;
  223. /* Don't bother verifying checksum if we're going to flush anyway. */
  224. if (NAPI_GRO_CB(skb)->flush)
  225. goto skip_csum;
  226. wsum = NAPI_GRO_CB(skb)->csum;
  227. switch (skb->ip_summed) {
  228. case CHECKSUM_NONE:
  229. wsum = skb_checksum(skb, skb_gro_offset(skb), skb_gro_len(skb),
  230. 0);
  231. /* fall through */
  232. case CHECKSUM_COMPLETE:
  233. if (!tcp_v4_check(skb_gro_len(skb), iph->saddr, iph->daddr,
  234. wsum)) {
  235. skb->ip_summed = CHECKSUM_UNNECESSARY;
  236. break;
  237. }
  238. NAPI_GRO_CB(skb)->flush = 1;
  239. return NULL;
  240. }
  241. skip_csum:
  242. return tcp_gro_receive(head, skb);
  243. }
  244. static int tcp4_gro_complete(struct sk_buff *skb, int thoff)
  245. {
  246. const struct iphdr *iph = ip_hdr(skb);
  247. struct tcphdr *th = tcp_hdr(skb);
  248. th->check = ~tcp_v4_check(skb->len - thoff, iph->saddr,
  249. iph->daddr, 0);
  250. skb_shinfo(skb)->gso_type |= SKB_GSO_TCPV4;
  251. return tcp_gro_complete(skb);
  252. }
  253. static const struct net_offload tcpv4_offload = {
  254. .callbacks = {
  255. .gso_send_check = tcp_v4_gso_send_check,
  256. .gso_segment = tcp_gso_segment,
  257. .gro_receive = tcp4_gro_receive,
  258. .gro_complete = tcp4_gro_complete,
  259. },
  260. };
  261. int __init tcpv4_offload_init(void)
  262. {
  263. return inet_add_offload(&tcpv4_offload, IPPROTO_TCP);
  264. }