tcp_offload.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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. return __tcp4_gso_segment_list(skb, features);
  91. skb->ip_summed = CHECKSUM_NONE;
  92. }
  93. if (unlikely(skb->ip_summed != CHECKSUM_PARTIAL)) {
  94. const struct iphdr *iph = ip_hdr(skb);
  95. struct tcphdr *th = tcp_hdr(skb);
  96. /* Set up checksum pseudo header, usually expect stack to
  97. * have done this already.
  98. */
  99. th->check = 0;
  100. skb->ip_summed = CHECKSUM_PARTIAL;
  101. __tcp_v4_send_check(skb, iph->saddr, iph->daddr);
  102. }
  103. return tcp_gso_segment(skb, features);
  104. }
  105. struct sk_buff *tcp_gso_segment(struct sk_buff *skb,
  106. netdev_features_t features)
  107. {
  108. struct sk_buff *segs = ERR_PTR(-EINVAL);
  109. unsigned int sum_truesize = 0;
  110. struct tcphdr *th;
  111. unsigned int thlen;
  112. unsigned int seq;
  113. unsigned int oldlen;
  114. unsigned int mss;
  115. struct sk_buff *gso_skb = skb;
  116. __sum16 newcheck;
  117. bool ooo_okay, copy_destructor;
  118. __wsum delta;
  119. th = tcp_hdr(skb);
  120. thlen = th->doff * 4;
  121. if (thlen < sizeof(*th))
  122. goto out;
  123. if (unlikely(skb_checksum_start(skb) != skb_transport_header(skb)))
  124. goto out;
  125. if (!pskb_may_pull(skb, thlen))
  126. goto out;
  127. oldlen = ~skb->len;
  128. __skb_pull(skb, thlen);
  129. mss = skb_shinfo(skb)->gso_size;
  130. if (unlikely(skb->len <= mss))
  131. goto out;
  132. if (skb_gso_ok(skb, features | NETIF_F_GSO_ROBUST)) {
  133. /* Packet is from an untrusted source, reset gso_segs. */
  134. skb_shinfo(skb)->gso_segs = DIV_ROUND_UP(skb->len, mss);
  135. segs = NULL;
  136. goto out;
  137. }
  138. copy_destructor = gso_skb->destructor == tcp_wfree;
  139. ooo_okay = gso_skb->ooo_okay;
  140. /* All segments but the first should have ooo_okay cleared */
  141. skb->ooo_okay = 0;
  142. segs = skb_segment(skb, features);
  143. if (IS_ERR(segs))
  144. goto out;
  145. /* Only first segment might have ooo_okay set */
  146. segs->ooo_okay = ooo_okay;
  147. /* GSO partial and frag_list segmentation only requires splitting
  148. * the frame into an MSS multiple and possibly a remainder, both
  149. * cases return a GSO skb. So update the mss now.
  150. */
  151. if (skb_is_gso(segs))
  152. mss *= skb_shinfo(segs)->gso_segs;
  153. delta = (__force __wsum)htonl(oldlen + thlen + mss);
  154. skb = segs;
  155. th = tcp_hdr(skb);
  156. seq = ntohl(th->seq);
  157. if (unlikely(skb_shinfo(gso_skb)->tx_flags & SKBTX_ANY_TSTAMP))
  158. tcp_gso_tstamp(segs, gso_skb, seq, mss);
  159. newcheck = ~csum_fold(csum_add(csum_unfold(th->check), delta));
  160. while (skb->next) {
  161. th->fin = th->psh = 0;
  162. th->check = newcheck;
  163. if (skb->ip_summed == CHECKSUM_PARTIAL)
  164. gso_reset_checksum(skb, ~th->check);
  165. else
  166. th->check = gso_make_checksum(skb, ~th->check);
  167. seq += mss;
  168. if (copy_destructor) {
  169. skb->destructor = gso_skb->destructor;
  170. skb->sk = gso_skb->sk;
  171. sum_truesize += skb->truesize;
  172. }
  173. skb = skb->next;
  174. th = tcp_hdr(skb);
  175. th->seq = htonl(seq);
  176. th->cwr = 0;
  177. }
  178. /* Following permits TCP Small Queues to work well with GSO :
  179. * The callback to TCP stack will be called at the time last frag
  180. * is freed at TX completion, and not right now when gso_skb
  181. * is freed by GSO engine
  182. */
  183. if (copy_destructor) {
  184. int delta;
  185. swap(gso_skb->sk, skb->sk);
  186. swap(gso_skb->destructor, skb->destructor);
  187. sum_truesize += skb->truesize;
  188. delta = sum_truesize - gso_skb->truesize;
  189. /* In some pathological cases, delta can be negative.
  190. * We need to either use refcount_add() or refcount_sub_and_test()
  191. */
  192. if (likely(delta >= 0))
  193. refcount_add(delta, &skb->sk->sk_wmem_alloc);
  194. else
  195. WARN_ON_ONCE(refcount_sub_and_test(-delta, &skb->sk->sk_wmem_alloc));
  196. }
  197. delta = (__force __wsum)htonl(oldlen +
  198. (skb_tail_pointer(skb) -
  199. skb_transport_header(skb)) +
  200. skb->data_len);
  201. th->check = ~csum_fold(csum_add(csum_unfold(th->check), delta));
  202. if (skb->ip_summed == CHECKSUM_PARTIAL)
  203. gso_reset_checksum(skb, ~th->check);
  204. else
  205. th->check = gso_make_checksum(skb, ~th->check);
  206. out:
  207. return segs;
  208. }
  209. struct sk_buff *tcp_gro_lookup(struct list_head *head, struct tcphdr *th)
  210. {
  211. struct tcphdr *th2;
  212. struct sk_buff *p;
  213. list_for_each_entry(p, head, list) {
  214. if (!NAPI_GRO_CB(p)->same_flow)
  215. continue;
  216. th2 = tcp_hdr(p);
  217. if (*(u32 *)&th->source ^ *(u32 *)&th2->source) {
  218. NAPI_GRO_CB(p)->same_flow = 0;
  219. continue;
  220. }
  221. return p;
  222. }
  223. return NULL;
  224. }
  225. struct tcphdr *tcp_gro_pull_header(struct sk_buff *skb)
  226. {
  227. unsigned int thlen, hlen, off;
  228. struct tcphdr *th;
  229. off = skb_gro_offset(skb);
  230. hlen = off + sizeof(*th);
  231. th = skb_gro_header(skb, hlen, off);
  232. if (unlikely(!th))
  233. return NULL;
  234. thlen = th->doff * 4;
  235. if (thlen < sizeof(*th))
  236. return NULL;
  237. hlen = off + thlen;
  238. if (!skb_gro_may_pull(skb, hlen)) {
  239. th = skb_gro_header_slow(skb, hlen, off);
  240. if (unlikely(!th))
  241. return NULL;
  242. }
  243. skb_gro_pull(skb, thlen);
  244. return th;
  245. }
  246. struct sk_buff *tcp_gro_receive(struct list_head *head, struct sk_buff *skb,
  247. struct tcphdr *th)
  248. {
  249. unsigned int thlen = th->doff * 4;
  250. struct sk_buff *pp = NULL;
  251. struct sk_buff *p;
  252. struct tcphdr *th2;
  253. unsigned int len;
  254. __be32 flags;
  255. unsigned int mss = 1;
  256. int flush = 1;
  257. int i;
  258. len = skb_gro_len(skb);
  259. flags = tcp_flag_word(th);
  260. p = tcp_gro_lookup(head, th);
  261. if (!p)
  262. goto out_check_final;
  263. th2 = tcp_hdr(p);
  264. flush = (__force int)(flags & TCP_FLAG_CWR);
  265. flush |= (__force int)((flags ^ tcp_flag_word(th2)) &
  266. ~(TCP_FLAG_CWR | TCP_FLAG_FIN | TCP_FLAG_PSH));
  267. flush |= (__force int)(th->ack_seq ^ th2->ack_seq);
  268. for (i = sizeof(*th); i < thlen; i += 4)
  269. flush |= *(u32 *)((u8 *)th + i) ^
  270. *(u32 *)((u8 *)th2 + i);
  271. flush |= gro_receive_network_flush(th, th2, p);
  272. mss = skb_shinfo(p)->gso_size;
  273. /* If skb is a GRO packet, make sure its gso_size matches prior packet mss.
  274. * If it is a single frame, do not aggregate it if its length
  275. * is bigger than our mss.
  276. */
  277. if (unlikely(skb_is_gso(skb)))
  278. flush |= (mss != skb_shinfo(skb)->gso_size);
  279. else
  280. flush |= (len - 1) >= mss;
  281. flush |= (ntohl(th2->seq) + skb_gro_len(p)) ^ ntohl(th->seq);
  282. flush |= skb_cmp_decrypted(p, skb);
  283. if (unlikely(NAPI_GRO_CB(p)->is_flist)) {
  284. flush |= (__force int)(flags ^ tcp_flag_word(th2));
  285. flush |= skb->ip_summed != p->ip_summed;
  286. flush |= skb->csum_level != p->csum_level;
  287. flush |= NAPI_GRO_CB(p)->count >= 64;
  288. skb_set_network_header(skb, skb_gro_receive_network_offset(skb));
  289. if (flush || skb_gro_receive_list(p, skb))
  290. mss = 1;
  291. goto out_check_final;
  292. }
  293. if (flush || skb_gro_receive(p, skb)) {
  294. mss = 1;
  295. goto out_check_final;
  296. }
  297. tcp_flag_word(th2) |= flags & (TCP_FLAG_FIN | TCP_FLAG_PSH);
  298. out_check_final:
  299. /* Force a flush if last segment is smaller than mss. */
  300. if (unlikely(skb_is_gso(skb)))
  301. flush = len != NAPI_GRO_CB(skb)->count * skb_shinfo(skb)->gso_size;
  302. else
  303. flush = len < mss;
  304. flush |= (__force int)(flags & (TCP_FLAG_URG | TCP_FLAG_PSH |
  305. TCP_FLAG_RST | TCP_FLAG_SYN |
  306. TCP_FLAG_FIN));
  307. if (p && (!NAPI_GRO_CB(skb)->same_flow || flush))
  308. pp = p;
  309. NAPI_GRO_CB(skb)->flush |= (flush != 0);
  310. return pp;
  311. }
  312. void tcp_gro_complete(struct sk_buff *skb)
  313. {
  314. struct tcphdr *th = tcp_hdr(skb);
  315. struct skb_shared_info *shinfo;
  316. if (skb->encapsulation)
  317. skb->inner_transport_header = skb->transport_header;
  318. skb->csum_start = (unsigned char *)th - skb->head;
  319. skb->csum_offset = offsetof(struct tcphdr, check);
  320. skb->ip_summed = CHECKSUM_PARTIAL;
  321. shinfo = skb_shinfo(skb);
  322. shinfo->gso_segs = NAPI_GRO_CB(skb)->count;
  323. if (th->cwr)
  324. shinfo->gso_type |= SKB_GSO_TCP_ECN;
  325. }
  326. EXPORT_SYMBOL(tcp_gro_complete);
  327. static void tcp4_check_fraglist_gro(struct list_head *head, struct sk_buff *skb,
  328. struct tcphdr *th)
  329. {
  330. const struct iphdr *iph;
  331. struct sk_buff *p;
  332. struct sock *sk;
  333. struct net *net;
  334. int iif, sdif;
  335. if (likely(!(skb->dev->features & NETIF_F_GRO_FRAGLIST)))
  336. return;
  337. p = tcp_gro_lookup(head, th);
  338. if (p) {
  339. NAPI_GRO_CB(skb)->is_flist = NAPI_GRO_CB(p)->is_flist;
  340. return;
  341. }
  342. inet_get_iif_sdif(skb, &iif, &sdif);
  343. iph = skb_gro_network_header(skb);
  344. net = dev_net(skb->dev);
  345. sk = __inet_lookup_established(net, net->ipv4.tcp_death_row.hashinfo,
  346. iph->saddr, th->source,
  347. iph->daddr, ntohs(th->dest),
  348. iif, sdif);
  349. NAPI_GRO_CB(skb)->is_flist = !sk;
  350. if (sk)
  351. sock_gen_put(sk);
  352. }
  353. INDIRECT_CALLABLE_SCOPE
  354. struct sk_buff *tcp4_gro_receive(struct list_head *head, struct sk_buff *skb)
  355. {
  356. struct tcphdr *th;
  357. /* Don't bother verifying checksum if we're going to flush anyway. */
  358. if (!NAPI_GRO_CB(skb)->flush &&
  359. skb_gro_checksum_validate(skb, IPPROTO_TCP,
  360. inet_gro_compute_pseudo))
  361. goto flush;
  362. th = tcp_gro_pull_header(skb);
  363. if (!th)
  364. goto flush;
  365. tcp4_check_fraglist_gro(head, skb, th);
  366. return tcp_gro_receive(head, skb, th);
  367. flush:
  368. NAPI_GRO_CB(skb)->flush = 1;
  369. return NULL;
  370. }
  371. INDIRECT_CALLABLE_SCOPE int tcp4_gro_complete(struct sk_buff *skb, int thoff)
  372. {
  373. const u16 offset = NAPI_GRO_CB(skb)->network_offsets[skb->encapsulation];
  374. const struct iphdr *iph = (struct iphdr *)(skb->data + offset);
  375. struct tcphdr *th = tcp_hdr(skb);
  376. if (unlikely(NAPI_GRO_CB(skb)->is_flist)) {
  377. skb_shinfo(skb)->gso_type |= SKB_GSO_FRAGLIST | SKB_GSO_TCPV4;
  378. skb_shinfo(skb)->gso_segs = NAPI_GRO_CB(skb)->count;
  379. __skb_incr_checksum_unnecessary(skb);
  380. return 0;
  381. }
  382. th->check = ~tcp_v4_check(skb->len - thoff, iph->saddr,
  383. iph->daddr, 0);
  384. skb_shinfo(skb)->gso_type |= SKB_GSO_TCPV4 |
  385. (NAPI_GRO_CB(skb)->ip_fixedid * SKB_GSO_TCP_FIXEDID);
  386. tcp_gro_complete(skb);
  387. return 0;
  388. }
  389. int __init tcpv4_offload_init(void)
  390. {
  391. net_hotdata.tcpv4_offload = (struct net_offload) {
  392. .callbacks = {
  393. .gso_segment = tcp4_gso_segment,
  394. .gro_receive = tcp4_gro_receive,
  395. .gro_complete = tcp4_gro_complete,
  396. },
  397. };
  398. return inet_add_offload(&net_hotdata.tcpv4_offload, IPPROTO_TCP);
  399. }