tcpv6_offload.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * IPV6 GSO/GRO offload support
  4. * Linux INET6 implementation
  5. *
  6. * TCPv6 GSO/GRO support
  7. */
  8. #include <linux/indirect_call_wrapper.h>
  9. #include <linux/skbuff.h>
  10. #include <net/inet6_hashtables.h>
  11. #include <net/gro.h>
  12. #include <net/protocol.h>
  13. #include <net/tcp.h>
  14. #include <net/ip6_checksum.h>
  15. #include "ip6_offload.h"
  16. static void tcp6_check_fraglist_gro(struct list_head *head, struct sk_buff *skb,
  17. struct tcphdr *th)
  18. {
  19. #if IS_ENABLED(CONFIG_IPV6)
  20. const struct ipv6hdr *hdr;
  21. struct sk_buff *p;
  22. struct sock *sk;
  23. struct net *net;
  24. int iif, sdif;
  25. if (likely(!(skb->dev->features & NETIF_F_GRO_FRAGLIST)))
  26. return;
  27. p = tcp_gro_lookup(head, th);
  28. if (p) {
  29. NAPI_GRO_CB(skb)->is_flist = NAPI_GRO_CB(p)->is_flist;
  30. return;
  31. }
  32. inet6_get_iif_sdif(skb, &iif, &sdif);
  33. hdr = skb_gro_network_header(skb);
  34. net = dev_net(skb->dev);
  35. sk = __inet6_lookup_established(net, net->ipv4.tcp_death_row.hashinfo,
  36. &hdr->saddr, th->source,
  37. &hdr->daddr, ntohs(th->dest),
  38. iif, sdif);
  39. NAPI_GRO_CB(skb)->is_flist = !sk;
  40. if (sk)
  41. sock_gen_put(sk);
  42. #endif /* IS_ENABLED(CONFIG_IPV6) */
  43. }
  44. INDIRECT_CALLABLE_SCOPE
  45. struct sk_buff *tcp6_gro_receive(struct list_head *head, struct sk_buff *skb)
  46. {
  47. struct tcphdr *th;
  48. /* Don't bother verifying checksum if we're going to flush anyway. */
  49. if (!NAPI_GRO_CB(skb)->flush &&
  50. skb_gro_checksum_validate(skb, IPPROTO_TCP,
  51. ip6_gro_compute_pseudo))
  52. goto flush;
  53. th = tcp_gro_pull_header(skb);
  54. if (!th)
  55. goto flush;
  56. tcp6_check_fraglist_gro(head, skb, th);
  57. return tcp_gro_receive(head, skb, th);
  58. flush:
  59. NAPI_GRO_CB(skb)->flush = 1;
  60. return NULL;
  61. }
  62. INDIRECT_CALLABLE_SCOPE int tcp6_gro_complete(struct sk_buff *skb, int thoff)
  63. {
  64. const u16 offset = NAPI_GRO_CB(skb)->network_offsets[skb->encapsulation];
  65. const struct ipv6hdr *iph = (struct ipv6hdr *)(skb->data + offset);
  66. struct tcphdr *th = tcp_hdr(skb);
  67. if (unlikely(NAPI_GRO_CB(skb)->is_flist)) {
  68. skb_shinfo(skb)->gso_type |= SKB_GSO_FRAGLIST | SKB_GSO_TCPV6;
  69. skb_shinfo(skb)->gso_segs = NAPI_GRO_CB(skb)->count;
  70. __skb_incr_checksum_unnecessary(skb);
  71. return 0;
  72. }
  73. th->check = ~tcp_v6_check(skb->len - thoff, &iph->saddr,
  74. &iph->daddr, 0);
  75. skb_shinfo(skb)->gso_type |= SKB_GSO_TCPV6;
  76. tcp_gro_complete(skb);
  77. return 0;
  78. }
  79. static void __tcpv6_gso_segment_csum(struct sk_buff *seg,
  80. struct in6_addr *oldip,
  81. const struct in6_addr *newip,
  82. __be16 *oldport, __be16 newport)
  83. {
  84. struct tcphdr *th = tcp_hdr(seg);
  85. if (!ipv6_addr_equal(oldip, newip)) {
  86. inet_proto_csum_replace16(&th->check, seg,
  87. oldip->s6_addr32,
  88. newip->s6_addr32,
  89. true);
  90. *oldip = *newip;
  91. }
  92. if (*oldport == newport)
  93. return;
  94. inet_proto_csum_replace2(&th->check, seg, *oldport, newport, false);
  95. *oldport = newport;
  96. }
  97. static struct sk_buff *__tcpv6_gso_segment_list_csum(struct sk_buff *segs)
  98. {
  99. const struct tcphdr *th;
  100. const struct ipv6hdr *iph;
  101. struct sk_buff *seg;
  102. struct tcphdr *th2;
  103. struct ipv6hdr *iph2;
  104. seg = segs;
  105. th = tcp_hdr(seg);
  106. iph = ipv6_hdr(seg);
  107. th2 = tcp_hdr(seg->next);
  108. iph2 = ipv6_hdr(seg->next);
  109. if (!(*(const u32 *)&th->source ^ *(const u32 *)&th2->source) &&
  110. ipv6_addr_equal(&iph->saddr, &iph2->saddr) &&
  111. ipv6_addr_equal(&iph->daddr, &iph2->daddr))
  112. return segs;
  113. while ((seg = seg->next)) {
  114. th2 = tcp_hdr(seg);
  115. iph2 = ipv6_hdr(seg);
  116. __tcpv6_gso_segment_csum(seg, &iph2->saddr, &iph->saddr,
  117. &th2->source, th->source);
  118. __tcpv6_gso_segment_csum(seg, &iph2->daddr, &iph->daddr,
  119. &th2->dest, th->dest);
  120. }
  121. return segs;
  122. }
  123. static struct sk_buff *__tcp6_gso_segment_list(struct sk_buff *skb,
  124. netdev_features_t features)
  125. {
  126. skb = skb_segment_list(skb, features, skb_mac_header_len(skb));
  127. if (IS_ERR(skb))
  128. return skb;
  129. return __tcpv6_gso_segment_list_csum(skb);
  130. }
  131. static struct sk_buff *tcp6_gso_segment(struct sk_buff *skb,
  132. netdev_features_t features)
  133. {
  134. struct tcphdr *th;
  135. if (!(skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6))
  136. return ERR_PTR(-EINVAL);
  137. if (!pskb_may_pull(skb, sizeof(*th)))
  138. return ERR_PTR(-EINVAL);
  139. if (skb_shinfo(skb)->gso_type & SKB_GSO_FRAGLIST) {
  140. struct tcphdr *th = tcp_hdr(skb);
  141. if (skb_pagelen(skb) - th->doff * 4 == skb_shinfo(skb)->gso_size)
  142. return __tcp6_gso_segment_list(skb, features);
  143. skb->ip_summed = CHECKSUM_NONE;
  144. }
  145. if (unlikely(skb->ip_summed != CHECKSUM_PARTIAL)) {
  146. const struct ipv6hdr *ipv6h = ipv6_hdr(skb);
  147. struct tcphdr *th = tcp_hdr(skb);
  148. /* Set up pseudo header, usually expect stack to have done
  149. * this.
  150. */
  151. th->check = 0;
  152. skb->ip_summed = CHECKSUM_PARTIAL;
  153. __tcp_v6_send_check(skb, &ipv6h->saddr, &ipv6h->daddr);
  154. }
  155. return tcp_gso_segment(skb, features);
  156. }
  157. int __init tcpv6_offload_init(void)
  158. {
  159. net_hotdata.tcpv6_offload = (struct net_offload) {
  160. .callbacks = {
  161. .gso_segment = tcp6_gso_segment,
  162. .gro_receive = tcp6_gro_receive,
  163. .gro_complete = tcp6_gro_complete,
  164. },
  165. };
  166. return inet6_add_offload(&net_hotdata.tcpv6_offload, IPPROTO_TCP);
  167. }