tcp_offload.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * IPV4 GSO/GRO offload support
  4. * Linux INET implementation
  5. *
  6. * TCPv4 GSO/GRO support
  7. */
  8. #include <linux/indirect_call_wrapper.h>
  9. #include <linux/skbuff.h>
  10. #include <net/gro.h>
  11. #include <net/gso.h>
  12. #include <net/tcp.h>
  13. #include <net/protocol.h>
  14. static void tcp_gso_tstamp(struct sk_buff *skb, struct sk_buff *gso_skb,
  15. unsigned int seq, unsigned int mss)
  16. {
  17. u32 flags = skb_shinfo(gso_skb)->tx_flags & SKBTX_ANY_TSTAMP;
  18. u32 ts_seq = skb_shinfo(gso_skb)->tskey;
  19. while (skb) {
  20. if (before(ts_seq, seq + mss)) {
  21. skb_shinfo(skb)->tx_flags |= flags;
  22. skb_shinfo(skb)->tskey = ts_seq;
  23. return;
  24. }
  25. skb = skb->next;
  26. seq += mss;
  27. }
  28. }
  29. static void __tcpv4_gso_segment_csum(struct sk_buff *seg,
  30. __be32 *oldip, __be32 newip,
  31. __be16 *oldport, __be16 newport)
  32. {
  33. struct tcphdr *th;
  34. struct iphdr *iph;
  35. if (*oldip == newip && *oldport == newport)
  36. return;
  37. th = tcp_hdr(seg);
  38. iph = ip_hdr(seg);
  39. inet_proto_csum_replace4(&th->check, seg, *oldip, newip, true);
  40. inet_proto_csum_replace2(&th->check, seg, *oldport, newport, false);
  41. *oldport = newport;
  42. csum_replace4(&iph->check, *oldip, newip);
  43. *oldip = newip;
  44. }
  45. static struct sk_buff *__tcpv4_gso_segment_list_csum(struct sk_buff *segs)
  46. {
  47. const struct tcphdr *th;
  48. const struct iphdr *iph;
  49. struct sk_buff *seg;
  50. struct tcphdr *th2;
  51. struct iphdr *iph2;
  52. seg = segs;
  53. th = tcp_hdr(seg);
  54. iph = ip_hdr(seg);
  55. th2 = tcp_hdr(seg->next);
  56. iph2 = ip_hdr(seg->next);
  57. if (!(*(const u32 *)&th->source ^ *(const u32 *)&th2->source) &&
  58. iph->daddr == iph2->daddr && iph->saddr == iph2->saddr)
  59. return segs;
  60. while ((seg = seg->next)) {
  61. th2 = tcp_hdr(seg);
  62. iph2 = ip_hdr(seg);
  63. __tcpv4_gso_segment_csum(seg,
  64. &iph2->saddr, iph->saddr,
  65. &th2->source, th->source);
  66. __tcpv4_gso_segment_csum(seg,
  67. &iph2->daddr, iph->daddr,
  68. &th2->dest, th->dest);
  69. }
  70. return segs;
  71. }
  72. static struct sk_buff *__tcp4_gso_segment_list(struct sk_buff *skb,
  73. netdev_features_t features)
  74. {
  75. skb = skb_segment_list(skb, features, skb_mac_header_len(skb));
  76. if (IS_ERR(skb))
  77. return skb;
  78. return __tcpv4_gso_segment_list_csum(skb);
  79. }
  80. static struct sk_buff *tcp4_gso_segment(struct sk_buff *skb,
  81. netdev_features_t features)
  82. {
  83. if (!(skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4))
  84. return ERR_PTR(-EINVAL);
  85. if (!pskb_may_pull(skb, sizeof(struct tcphdr)))
  86. return ERR_PTR(-EINVAL);
  87. if (skb_shinfo(skb)->gso_type & SKB_GSO_FRAGLIST) {
  88. struct tcphdr *th = tcp_hdr(skb);
  89. if ((skb_pagelen(skb) - th->doff * 4 == skb_shinfo(skb)->gso_size) &&
  90. !(skb_shinfo(skb)->gso_type & SKB_GSO_DODGY))
  91. return __tcp4_gso_segment_list(skb, features);
  92. skb->ip_summed = CHECKSUM_NONE;
  93. }
  94. if (unlikely(skb->ip_summed != CHECKSUM_PARTIAL)) {
  95. const struct iphdr *iph = ip_hdr(skb);
  96. struct tcphdr *th = tcp_hdr(skb);
  97. /* Set up checksum pseudo header, usually expect stack to
  98. * have done this already.
  99. */
  100. th->check = 0;
  101. skb->ip_summed = CHECKSUM_PARTIAL;
  102. __tcp_v4_send_check(skb, iph->saddr, iph->daddr);
  103. }
  104. return tcp_gso_segment(skb, features);
  105. }
  106. struct sk_buff *tcp_gso_segment(struct sk_buff *skb,
  107. netdev_features_t features)
  108. {
  109. struct sk_buff *segs = ERR_PTR(-EINVAL);
  110. unsigned int sum_truesize = 0;
  111. struct tcphdr *th;
  112. unsigned int thlen;
  113. unsigned int seq;
  114. unsigned int oldlen;
  115. unsigned int mss;
  116. struct sk_buff *gso_skb = skb;
  117. __sum16 newcheck;
  118. bool ooo_okay, copy_destructor;
  119. bool ecn_cwr_mask;
  120. __wsum delta;
  121. th = tcp_hdr(skb);
  122. thlen = th->doff * 4;
  123. if (thlen < sizeof(*th))
  124. goto out;
  125. if (unlikely(skb_checksum_start(skb) != skb_transport_header(skb)))
  126. goto out;
  127. if (!pskb_may_pull(skb, thlen))
  128. goto out;
  129. oldlen = ~skb->len;
  130. __skb_pull(skb, thlen);
  131. mss = skb_shinfo(skb)->gso_size;
  132. if (unlikely(skb->len <= mss))
  133. goto out;
  134. if (skb_gso_ok(skb, features | NETIF_F_GSO_ROBUST)) {
  135. /* Packet is from an untrusted source, reset gso_segs. */
  136. skb_shinfo(skb)->gso_segs = DIV_ROUND_UP(skb->len, mss);
  137. segs = NULL;
  138. goto out;
  139. }
  140. copy_destructor = gso_skb->destructor == tcp_wfree;
  141. ooo_okay = gso_skb->ooo_okay;
  142. /* All segments but the first should have ooo_okay cleared */
  143. skb->ooo_okay = 0;
  144. segs = skb_segment(skb, features);
  145. if (IS_ERR(segs))
  146. goto out;
  147. /* Only first segment might have ooo_okay set */
  148. segs->ooo_okay = ooo_okay;
  149. /* GSO partial and frag_list segmentation only requires splitting
  150. * the frame into an MSS multiple and possibly a remainder, both
  151. * cases return a GSO skb. So update the mss now.
  152. */
  153. if (skb_is_gso(segs))
  154. mss *= skb_shinfo(segs)->gso_segs;
  155. delta = (__force __wsum)htonl(oldlen + thlen + mss);
  156. skb = segs;
  157. th = tcp_hdr(skb);
  158. seq = ntohl(th->seq);
  159. if (unlikely(skb_shinfo(gso_skb)->tx_flags & SKBTX_ANY_TSTAMP))
  160. tcp_gso_tstamp(segs, gso_skb, seq, mss);
  161. newcheck = ~csum_fold(csum_add(csum_unfold(th->check), delta));
  162. ecn_cwr_mask = !!(skb_shinfo(gso_skb)->gso_type & SKB_GSO_TCP_ACCECN);
  163. while (skb->next) {
  164. th->fin = th->psh = 0;
  165. th->check = newcheck;
  166. if (skb->ip_summed == CHECKSUM_PARTIAL)
  167. gso_reset_checksum(skb, ~th->check);
  168. else
  169. th->check = gso_make_checksum(skb, ~th->check);
  170. seq += mss;
  171. if (copy_destructor) {
  172. skb->destructor = gso_skb->destructor;
  173. skb->sk = gso_skb->sk;
  174. sum_truesize += skb->truesize;
  175. }
  176. skb = skb->next;
  177. th = tcp_hdr(skb);
  178. th->seq = htonl(seq);
  179. th->cwr &= ecn_cwr_mask;
  180. }
  181. /* Following permits TCP Small Queues to work well with GSO :
  182. * The callback to TCP stack will be called at the time last frag
  183. * is freed at TX completion, and not right now when gso_skb
  184. * is freed by GSO engine
  185. */
  186. if (copy_destructor) {
  187. int delta;
  188. swap(gso_skb->sk, skb->sk);
  189. swap(gso_skb->destructor, skb->destructor);
  190. sum_truesize += skb->truesize;
  191. delta = sum_truesize - gso_skb->truesize;
  192. /* In some pathological cases, delta can be negative.
  193. * We need to either use refcount_add() or refcount_sub_and_test()
  194. */
  195. if (likely(delta >= 0))
  196. refcount_add(delta, &skb->sk->sk_wmem_alloc);
  197. else
  198. WARN_ON_ONCE(refcount_sub_and_test(-delta, &skb->sk->sk_wmem_alloc));
  199. }
  200. delta = (__force __wsum)htonl(oldlen +
  201. (skb_tail_pointer(skb) -
  202. skb_transport_header(skb)) +
  203. skb->data_len);
  204. th->check = ~csum_fold(csum_add(csum_unfold(th->check), delta));
  205. if (skb->ip_summed == CHECKSUM_PARTIAL)
  206. gso_reset_checksum(skb, ~th->check);
  207. else
  208. th->check = gso_make_checksum(skb, ~th->check);
  209. out:
  210. return segs;
  211. }
  212. struct sk_buff *tcp_gro_lookup(struct list_head *head, struct tcphdr *th)
  213. {
  214. struct tcphdr *th2;
  215. struct sk_buff *p;
  216. list_for_each_entry(p, head, list) {
  217. if (!NAPI_GRO_CB(p)->same_flow)
  218. continue;
  219. th2 = tcp_hdr(p);
  220. if (*(u32 *)&th->source ^ *(u32 *)&th2->source) {
  221. NAPI_GRO_CB(p)->same_flow = 0;
  222. continue;
  223. }
  224. return p;
  225. }
  226. return NULL;
  227. }
  228. struct sk_buff *tcp_gro_receive(struct list_head *head, struct sk_buff *skb,
  229. struct tcphdr *th)
  230. {
  231. unsigned int thlen = th->doff * 4;
  232. struct sk_buff *pp = NULL;
  233. struct sk_buff *p;
  234. struct tcphdr *th2;
  235. unsigned int len;
  236. __be32 flags;
  237. unsigned int mss = 1;
  238. int flush = 1;
  239. int i;
  240. len = skb_gro_len(skb);
  241. flags = tcp_flag_word(th);
  242. p = tcp_gro_lookup(head, th);
  243. if (!p)
  244. goto out_check_final;
  245. th2 = tcp_hdr(p);
  246. flush = (__force int)((flags ^ tcp_flag_word(th2)) &
  247. ~(TCP_FLAG_FIN | TCP_FLAG_PSH));
  248. flush |= (__force int)(th->ack_seq ^ th2->ack_seq);
  249. for (i = sizeof(*th); i < thlen; i += 4)
  250. flush |= *(u32 *)((u8 *)th + i) ^
  251. *(u32 *)((u8 *)th2 + i);
  252. flush |= gro_receive_network_flush(th, th2, p);
  253. mss = skb_shinfo(p)->gso_size;
  254. /* If skb is a GRO packet, make sure its gso_size matches prior packet mss.
  255. * If it is a single frame, do not aggregate it if its length
  256. * is bigger than our mss.
  257. */
  258. if (unlikely(skb_is_gso(skb)))
  259. flush |= (mss != skb_shinfo(skb)->gso_size);
  260. else
  261. flush |= (len - 1) >= mss;
  262. flush |= (ntohl(th2->seq) + skb_gro_len(p)) ^ ntohl(th->seq);
  263. flush |= skb_cmp_decrypted(p, skb);
  264. if (unlikely(NAPI_GRO_CB(p)->is_flist)) {
  265. flush |= (__force int)(flags ^ tcp_flag_word(th2));
  266. flush |= skb->ip_summed != p->ip_summed;
  267. flush |= skb->csum_level != p->csum_level;
  268. flush |= NAPI_GRO_CB(p)->count >= 64;
  269. skb_set_network_header(skb, skb_gro_receive_network_offset(skb));
  270. if (flush || skb_gro_receive_list(p, skb))
  271. mss = 1;
  272. goto out_check_final;
  273. }
  274. if (flush || skb_gro_receive(p, skb)) {
  275. mss = 1;
  276. goto out_check_final;
  277. }
  278. tcp_flag_word(th2) |= flags & (TCP_FLAG_FIN | TCP_FLAG_PSH);
  279. out_check_final:
  280. /* Force a flush if last segment is smaller than mss. */
  281. if (unlikely(skb_is_gso(skb)))
  282. flush = len != NAPI_GRO_CB(skb)->count * skb_shinfo(skb)->gso_size;
  283. else
  284. flush = len < mss;
  285. flush |= (__force int)(flags & (TCP_FLAG_URG | TCP_FLAG_PSH |
  286. TCP_FLAG_RST | TCP_FLAG_SYN |
  287. TCP_FLAG_FIN));
  288. if (p && (!NAPI_GRO_CB(skb)->same_flow || flush))
  289. pp = p;
  290. NAPI_GRO_CB(skb)->flush |= (flush != 0);
  291. return pp;
  292. }
  293. void tcp_gro_complete(struct sk_buff *skb)
  294. {
  295. struct tcphdr *th = tcp_hdr(skb);
  296. struct skb_shared_info *shinfo;
  297. if (skb->encapsulation)
  298. skb->inner_transport_header = skb->transport_header;
  299. skb->csum_start = (unsigned char *)th - skb->head;
  300. skb->csum_offset = offsetof(struct tcphdr, check);
  301. skb->ip_summed = CHECKSUM_PARTIAL;
  302. shinfo = skb_shinfo(skb);
  303. shinfo->gso_segs = NAPI_GRO_CB(skb)->count;
  304. if (th->cwr)
  305. shinfo->gso_type |= SKB_GSO_TCP_ACCECN;
  306. }
  307. EXPORT_SYMBOL(tcp_gro_complete);
  308. static void tcp4_check_fraglist_gro(struct list_head *head, struct sk_buff *skb,
  309. struct tcphdr *th)
  310. {
  311. const struct iphdr *iph;
  312. struct sk_buff *p;
  313. struct sock *sk;
  314. struct net *net;
  315. int iif, sdif;
  316. if (likely(!(skb->dev->features & NETIF_F_GRO_FRAGLIST)))
  317. return;
  318. p = tcp_gro_lookup(head, th);
  319. if (p) {
  320. NAPI_GRO_CB(skb)->is_flist = NAPI_GRO_CB(p)->is_flist;
  321. return;
  322. }
  323. inet_get_iif_sdif(skb, &iif, &sdif);
  324. iph = skb_gro_network_header(skb);
  325. net = dev_net_rcu(skb->dev);
  326. sk = __inet_lookup_established(net, iph->saddr, th->source,
  327. iph->daddr, ntohs(th->dest),
  328. iif, sdif);
  329. NAPI_GRO_CB(skb)->is_flist = !sk;
  330. if (sk)
  331. sock_gen_put(sk);
  332. }
  333. INDIRECT_CALLABLE_SCOPE
  334. struct sk_buff *tcp4_gro_receive(struct list_head *head, struct sk_buff *skb)
  335. {
  336. struct tcphdr *th;
  337. /* Don't bother verifying checksum if we're going to flush anyway. */
  338. if (!NAPI_GRO_CB(skb)->flush &&
  339. skb_gro_checksum_validate(skb, IPPROTO_TCP,
  340. inet_gro_compute_pseudo))
  341. goto flush;
  342. th = tcp_gro_pull_header(skb);
  343. if (!th)
  344. goto flush;
  345. tcp4_check_fraglist_gro(head, skb, th);
  346. return tcp_gro_receive(head, skb, th);
  347. flush:
  348. NAPI_GRO_CB(skb)->flush = 1;
  349. return NULL;
  350. }
  351. INDIRECT_CALLABLE_SCOPE int tcp4_gro_complete(struct sk_buff *skb, int thoff)
  352. {
  353. const u16 offset = NAPI_GRO_CB(skb)->network_offsets[skb->encapsulation];
  354. const struct iphdr *iph = (struct iphdr *)(skb->data + offset);
  355. struct tcphdr *th = tcp_hdr(skb);
  356. if (unlikely(NAPI_GRO_CB(skb)->is_flist)) {
  357. skb_shinfo(skb)->gso_type |= SKB_GSO_FRAGLIST | SKB_GSO_TCPV4;
  358. skb_shinfo(skb)->gso_segs = NAPI_GRO_CB(skb)->count;
  359. __skb_incr_checksum_unnecessary(skb);
  360. return 0;
  361. }
  362. th->check = ~tcp_v4_check(skb->len - thoff, iph->saddr,
  363. iph->daddr, 0);
  364. BUILD_BUG_ON(SKB_GSO_TCP_FIXEDID << 1 != SKB_GSO_TCP_FIXEDID_INNER);
  365. skb_shinfo(skb)->gso_type |= SKB_GSO_TCPV4 |
  366. (NAPI_GRO_CB(skb)->ip_fixedid * SKB_GSO_TCP_FIXEDID);
  367. tcp_gro_complete(skb);
  368. return 0;
  369. }
  370. int __init tcpv4_offload_init(void)
  371. {
  372. net_hotdata.tcpv4_offload = (struct net_offload) {
  373. .callbacks = {
  374. .gso_segment = tcp4_gso_segment,
  375. .gro_receive = tcp4_gro_receive,
  376. .gro_complete = tcp4_gro_complete,
  377. },
  378. };
  379. return inet_add_offload(&net_hotdata.tcpv4_offload, IPPROTO_TCP);
  380. }