Merge tag 'pm-6.16-rc5' of git://git.kernel.org/pub/scm/linux/kernel/git/rafael/linux-pm
[linux-2.6-block.git] / net / ipv6 / tcpv6_offload.c
CommitLineData
2874c5fd 1// SPDX-License-Identifier: GPL-2.0-or-later
8663e02a
VY
2/*
3 * IPV6 GSO/GRO offload support
4 * Linux INET6 implementation
5 *
8663e02a
VY
6 * TCPv6 GSO/GRO support
7 */
028e0a47 8#include <linux/indirect_call_wrapper.h>
8663e02a 9#include <linux/skbuff.h>
c9d1d23e 10#include <net/inet6_hashtables.h>
4721031c 11#include <net/gro.h>
8663e02a
VY
12#include <net/protocol.h>
13#include <net/tcp.h>
14#include <net/ip6_checksum.h>
15#include "ip6_offload.h"
16
c9d1d23e
FF
17static void tcp6_check_fraglist_gro(struct list_head *head, struct sk_buff *skb,
18 struct tcphdr *th)
19{
20#if IS_ENABLED(CONFIG_IPV6)
21 const struct ipv6hdr *hdr;
22 struct sk_buff *p;
23 struct sock *sk;
24 struct net *net;
25 int iif, sdif;
26
27 if (likely(!(skb->dev->features & NETIF_F_GRO_FRAGLIST)))
28 return;
29
30 p = tcp_gro_lookup(head, th);
31 if (p) {
32 NAPI_GRO_CB(skb)->is_flist = NAPI_GRO_CB(p)->is_flist;
33 return;
34 }
35
36 inet6_get_iif_sdif(skb, &iif, &sdif);
37 hdr = skb_gro_network_header(skb);
9b49f57c 38 net = dev_net_rcu(skb->dev);
c9d1d23e
FF
39 sk = __inet6_lookup_established(net, net->ipv4.tcp_death_row.hashinfo,
40 &hdr->saddr, th->source,
41 &hdr->daddr, ntohs(th->dest),
42 iif, sdif);
43 NAPI_GRO_CB(skb)->is_flist = !sk;
44 if (sk)
f920436a 45 sock_gen_put(sk);
c9d1d23e
FF
46#endif /* IS_ENABLED(CONFIG_IPV6) */
47}
48
028e0a47
PA
49INDIRECT_CALLABLE_SCOPE
50struct sk_buff *tcp6_gro_receive(struct list_head *head, struct sk_buff *skb)
8663e02a 51{
7516b27c
FF
52 struct tcphdr *th;
53
cc5c00bb 54 /* Don't bother verifying checksum if we're going to flush anyway. */
149d0774
TH
55 if (!NAPI_GRO_CB(skb)->flush &&
56 skb_gro_checksum_validate(skb, IPPROTO_TCP,
7516b27c
FF
57 ip6_gro_compute_pseudo))
58 goto flush;
59
60 th = tcp_gro_pull_header(skb);
61 if (!th)
62 goto flush;
63
c9d1d23e
FF
64 tcp6_check_fraglist_gro(head, skb, th);
65
7516b27c 66 return tcp_gro_receive(head, skb, th);
8663e02a 67
7516b27c
FF
68flush:
69 NAPI_GRO_CB(skb)->flush = 1;
70 return NULL;
8663e02a
VY
71}
72
028e0a47 73INDIRECT_CALLABLE_SCOPE int tcp6_gro_complete(struct sk_buff *skb, int thoff)
8663e02a 74{
186b1ea7
RG
75 const u16 offset = NAPI_GRO_CB(skb)->network_offsets[skb->encapsulation];
76 const struct ipv6hdr *iph = (struct ipv6hdr *)(skb->data + offset);
8663e02a
VY
77 struct tcphdr *th = tcp_hdr(skb);
78
8d95dc47
FF
79 if (unlikely(NAPI_GRO_CB(skb)->is_flist)) {
80 skb_shinfo(skb)->gso_type |= SKB_GSO_FRAGLIST | SKB_GSO_TCPV6;
81 skb_shinfo(skb)->gso_segs = NAPI_GRO_CB(skb)->count;
82
83 __skb_incr_checksum_unnecessary(skb);
84
85 return 0;
86 }
87
299603e8
JC
88 th->check = ~tcp_v6_check(skb->len - thoff, &iph->saddr,
89 &iph->daddr, 0);
c3caf119 90 skb_shinfo(skb)->gso_type |= SKB_GSO_TCPV6;
8663e02a 91
b1f2abcf
PP
92 tcp_gro_complete(skb);
93 return 0;
8663e02a
VY
94}
95
bee88cd5 96static void __tcpv6_gso_segment_csum(struct sk_buff *seg,
daa624d3
FF
97 struct in6_addr *oldip,
98 const struct in6_addr *newip,
bee88cd5
FF
99 __be16 *oldport, __be16 newport)
100{
daa624d3
FF
101 struct tcphdr *th = tcp_hdr(seg);
102
103 if (!ipv6_addr_equal(oldip, newip)) {
104 inet_proto_csum_replace16(&th->check, seg,
105 oldip->s6_addr32,
106 newip->s6_addr32,
107 true);
108 *oldip = *newip;
109 }
bee88cd5
FF
110
111 if (*oldport == newport)
112 return;
113
bee88cd5
FF
114 inet_proto_csum_replace2(&th->check, seg, *oldport, newport, false);
115 *oldport = newport;
116}
117
118static struct sk_buff *__tcpv6_gso_segment_list_csum(struct sk_buff *segs)
119{
120 const struct tcphdr *th;
121 const struct ipv6hdr *iph;
122 struct sk_buff *seg;
123 struct tcphdr *th2;
124 struct ipv6hdr *iph2;
125
126 seg = segs;
127 th = tcp_hdr(seg);
128 iph = ipv6_hdr(seg);
129 th2 = tcp_hdr(seg->next);
130 iph2 = ipv6_hdr(seg->next);
131
132 if (!(*(const u32 *)&th->source ^ *(const u32 *)&th2->source) &&
133 ipv6_addr_equal(&iph->saddr, &iph2->saddr) &&
134 ipv6_addr_equal(&iph->daddr, &iph2->daddr))
135 return segs;
136
137 while ((seg = seg->next)) {
138 th2 = tcp_hdr(seg);
139 iph2 = ipv6_hdr(seg);
140
daa624d3
FF
141 __tcpv6_gso_segment_csum(seg, &iph2->saddr, &iph->saddr,
142 &th2->source, th->source);
143 __tcpv6_gso_segment_csum(seg, &iph2->daddr, &iph->daddr,
144 &th2->dest, th->dest);
bee88cd5
FF
145 }
146
147 return segs;
148}
149
150static struct sk_buff *__tcp6_gso_segment_list(struct sk_buff *skb,
151 netdev_features_t features)
152{
153 skb = skb_segment_list(skb, features, skb_mac_header_len(skb));
154 if (IS_ERR(skb))
155 return skb;
156
157 return __tcpv6_gso_segment_list_csum(skb);
158}
159
74abc20c
ED
160static struct sk_buff *tcp6_gso_segment(struct sk_buff *skb,
161 netdev_features_t features)
d020f8f7
TH
162{
163 struct tcphdr *th;
164
121d57af
WB
165 if (!(skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6))
166 return ERR_PTR(-EINVAL);
167
d020f8f7
TH
168 if (!pskb_may_pull(skb, sizeof(*th)))
169 return ERR_PTR(-EINVAL);
170
17bd3bd8
FF
171 if (skb_shinfo(skb)->gso_type & SKB_GSO_FRAGLIST) {
172 struct tcphdr *th = tcp_hdr(skb);
173
174 if (skb_pagelen(skb) - th->doff * 4 == skb_shinfo(skb)->gso_size)
175 return __tcp6_gso_segment_list(skb, features);
176
177 skb->ip_summed = CHECKSUM_NONE;
178 }
bee88cd5 179
d020f8f7
TH
180 if (unlikely(skb->ip_summed != CHECKSUM_PARTIAL)) {
181 const struct ipv6hdr *ipv6h = ipv6_hdr(skb);
182 struct tcphdr *th = tcp_hdr(skb);
183
184 /* Set up pseudo header, usually expect stack to have done
185 * this.
186 */
187
188 th->check = 0;
189 skb->ip_summed = CHECKSUM_PARTIAL;
190 __tcp_v6_send_check(skb, &ipv6h->saddr, &ipv6h->daddr);
191 }
192
193 return tcp_gso_segment(skb, features);
194}
8663e02a
VY
195
196int __init tcpv6_offload_init(void)
197{
0139806e
ED
198 net_hotdata.tcpv6_offload = (struct net_offload) {
199 .callbacks = {
200 .gso_segment = tcp6_gso_segment,
201 .gro_receive = tcp6_gro_receive,
202 .gro_complete = tcp6_gro_complete,
203 },
204 };
205 return inet6_add_offload(&net_hotdata.tcpv6_offload, IPPROTO_TCP);
8663e02a 206}