Blame view

include/net/tcp.h 71.8 KB
2874c5fd2   Thomas Gleixner   treewide: Replace...
1
  /* SPDX-License-Identifier: GPL-2.0-or-later */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2
3
4
5
6
7
8
9
10
  /*
   * INET		An implementation of the TCP/IP protocol suite for the LINUX
   *		operating system.  INET is implemented using the  BSD Socket
   *		interface as the means of communication with the user level.
   *
   *		Definitions for the TCP module.
   *
   * Version:	@(#)tcp.h	1.0.5	05/23/93
   *
02c30a84e   Jesper Juhl   [PATCH] update Ro...
11
   * Authors:	Ross Biro
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
12
   *		Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
13
14
15
   */
  #ifndef _TCP_H
  #define _TCP_H
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
16
  #define FASTRETRANS_DEBUG 1
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
17
18
  #include <linux/list.h>
  #include <linux/tcp.h>
187f1882b   Paul Gortmaker   BUG: headers with...
19
  #include <linux/bug.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
20
21
22
  #include <linux/slab.h>
  #include <linux/cache.h>
  #include <linux/percpu.h>
fb286bb29   Herbert Xu   [NET]: Detect har...
23
  #include <linux/skbuff.h>
435cf559f   William Allen Simpson   TCPCT part 1d: de...
24
  #include <linux/kref.h>
740b0f184   Eric Dumazet   tcp: switch rtt e...
25
  #include <linux/ktime.h>
05e22e839   Eric Dumazet   tcp: remove indir...
26
  #include <linux/indirect_call_wrapper.h>
3f421baa4   Arnaldo Carvalho de Melo   [NET]: Just move ...
27
28
  
  #include <net/inet_connection_sock.h>
295ff7edb   Arnaldo Carvalho de Melo   [TIMEWAIT]: Intro...
29
  #include <net/inet_timewait_sock.h>
77d8bf9c6   Arnaldo Carvalho de Melo   [INET]: Move the ...
30
  #include <net/inet_hashtables.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
31
  #include <net/checksum.h>
2e6599cb8   Arnaldo Carvalho de Melo   [NET] Generalise ...
32
  #include <net/request_sock.h>
40a1227ea   Martin KaFai Lau   tcp: Avoid TCP sy...
33
  #include <net/sock_reuseport.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
34
35
36
  #include <net/sock.h>
  #include <net/snmp.h>
  #include <net/ip.h>
c752f0739   Arnaldo Carvalho de Melo   [TCP]: Move the t...
37
  #include <net/tcp_states.h>
bdf1ee5d3   Ilpo Järvinen   [TCP]: Move code ...
38
  #include <net/inet_ecn.h>
0c266898b   Satoru SATOH   tcp: Fix tcp_preq...
39
  #include <net/dst.h>
857124841   Mat Martineau   tcp: coalesce/col...
40
  #include <net/mptcp.h>
c752f0739   Arnaldo Carvalho de Melo   [TCP]: Move the t...
41

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
42
  #include <linux/seq_file.h>
180d8cd94   Glauber Costa   foundations of pe...
43
  #include <linux/memcontrol.h>
40304b2a1   Lawrence Brakmo   bpf: BPF support ...
44
  #include <linux/bpf-cgroup.h>
438ac8800   Ard Biesheuvel   net: fastopen: ro...
45
  #include <linux/siphash.h>
40304b2a1   Lawrence Brakmo   bpf: BPF support ...
46

6e04e0216   Arnaldo Carvalho de Melo   [INET]: Move tcp_...
47
  extern struct inet_hashinfo tcp_hashinfo;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
48

dd24c0019   Eric Dumazet   net: Use a percpu...
49
  extern struct percpu_counter tcp_orphan_count;
5c9f30236   Joe Perches   tcp.h: Remove ext...
50
  void tcp_time_wait(struct sock *sk, int state, int timeo);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
51

9bacd256f   Eric Dumazet   tcp: cache line a...
52
  #define MAX_TCP_HEADER	L1_CACHE_ALIGN(128 + MAX_HEADER)
33ad798c9   Adam Langley   tcp: options clea...
53
  #define MAX_TCP_OPTION_SPACE 40
3b4929f65   Eric Dumazet   tcp: limit payloa...
54
55
  #define TCP_MIN_SND_MSS		48
  #define TCP_MIN_GSO_SIZE	(TCP_MIN_SND_MSS - MAX_TCP_OPTION_SPACE)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
56

105970f60   Kenjiro Nakayama   net: Remove trail...
57
  /*
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
58
   * Never offer a window over 32767 without using window scaling. Some
105970f60   Kenjiro Nakayama   net: Remove trail...
59
   * poor stacks do signed 16bit maths!
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
60
61
62
63
64
   */
  #define MAX_TCP_WINDOW		32767U
  
  /* Minimal accepted MSS. It is (60+60+8) - (20+20). */
  #define TCP_MIN_MSS		88U
1555e6fdf   Josh Hunt   tcp: Update TCP_B...
65
  /* The initial MTU to use for probing */
dcd8fb853   Fan Du   ipv4: Raise tcp P...
66
  #define TCP_BASE_MSS		1024
5d424d5a6   John Heffner   [TCP]: MTU probing
67

05cbc0db0   Fan Du   ipv4: Create prob...
68
69
  /* probing interval, default to 10 minutes as per RFC4821 */
  #define TCP_PROBE_INTERVAL	600
6b58e0a5f   Fan Du   ipv4: Use binary ...
70
71
  /* Specify interval when tcp mtu probing will stop */
  #define TCP_PROBE_THRESHOLD	8
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
72
73
  /* After receiving this amount of duplicate ACKs fast retransmit starts. */
  #define TCP_FASTRETRANS_THRESH 3
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
74
75
  /* Maximal number of ACKs sent quickly to accelerate slow-start. */
  #define TCP_MAX_QUICKACKS	16U
589c49cbf   Gao Feng   net: tcp: Define ...
76
77
  /* Maximal number of window scale according to RFC1323 */
  #define TCP_MAX_WSCALE		14U
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
  /* urg_data states */
  #define TCP_URG_VALID	0x0100
  #define TCP_URG_NOTYET	0x0200
  #define TCP_URG_READ	0x0400
  
  #define TCP_RETR1	3	/*
  				 * This is how many retries it does before it
  				 * tries to figure out if the gateway is
  				 * down. Minimal RFC value is 3; it corresponds
  				 * to ~3sec-8min depending on RTO.
  				 */
  
  #define TCP_RETR2	15	/*
  				 * This should take at least
  				 * 90 minutes to time out.
  				 * RFC1122 says that the limit is 100 sec.
  				 * 15 is ~13-30min depending on RTO.
  				 */
6c9ff979d   Alex Bergmann   tcp: Increase tim...
96
97
98
99
100
101
102
103
  #define TCP_SYN_RETRIES	 6	/* This is how many retries are done
  				 * when active opening a connection.
  				 * RFC1122 says the minimum retry MUST
  				 * be at least 180secs.  Nevertheless
  				 * this value is corresponding to
  				 * 63secs of retransmission with the
  				 * current initial RTO.
  				 */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
104

6c9ff979d   Alex Bergmann   tcp: Increase tim...
105
106
107
108
109
110
  #define TCP_SYNACK_RETRIES 5	/* This is how may retries are done
  				 * when passive opening a connection.
  				 * This is corresponding to 31secs of
  				 * retransmission with the current
  				 * initial RTO.
  				 */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
111

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
112
113
114
115
116
117
118
119
  #define TCP_TIMEWAIT_LEN (60*HZ) /* how long to wait to destroy TIME-WAIT
  				  * state, about 60 seconds	*/
  #define TCP_FIN_TIMEOUT	TCP_TIMEWAIT_LEN
                                   /* BSD style FIN_WAIT2 deadlock breaker.
  				  * It used to be 3min, new value is 60sec,
  				  * to combine FIN-WAIT-2 timeout with
  				  * TIME-WAIT timer.
  				  */
f0628c524   Cambda Zhu   net: Replace the ...
120
  #define TCP_FIN_TIMEOUT_MAX (120 * HZ) /* max TCP_LINGER2 value (two minutes) */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
121
122
123
124
125
126
127
128
129
130
131
  
  #define TCP_DELACK_MAX	((unsigned)(HZ/5))	/* maximal time to delay before sending an ACK */
  #if HZ >= 100
  #define TCP_DELACK_MIN	((unsigned)(HZ/25))	/* minimal time to delay before sending an ACK */
  #define TCP_ATO_MIN	((unsigned)(HZ/25))
  #else
  #define TCP_DELACK_MIN	4U
  #define TCP_ATO_MIN	4U
  #endif
  #define TCP_RTO_MAX	((unsigned)(120*HZ))
  #define TCP_RTO_MIN	((unsigned)(HZ/5))
bb4d991a2   Yuchung Cheng   tcp: adjust tail ...
132
  #define TCP_TIMEOUT_MIN	(2U) /* Min timeout for TCP timers in jiffies */
fd4f2cead   Eric Dumazet   tcp: RFC6298 supe...
133
  #define TCP_TIMEOUT_INIT ((unsigned)(1*HZ))	/* RFC6298 2.1 initial RTO value	*/
9ad7c049f   Jerry Chu   tcp: RFC2988bis +...
134
135
136
137
138
139
  #define TCP_TIMEOUT_FALLBACK ((unsigned)(3*HZ))	/* RFC 1122 initial RTO value, now
  						 * used as a fallback RTO for the
  						 * initial data transmission if no
  						 * valid RTT sample has been acquired,
  						 * most likely due to retrans in 3WHS.
  						 */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
140
141
142
143
  
  #define TCP_RESOURCE_PROBE_INTERVAL ((unsigned)(HZ/2U)) /* Maximal interval between probes
  					                 * for local resources.
  					                 */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
144
145
146
147
148
149
150
151
152
153
  #define TCP_KEEPALIVE_TIME	(120*60*HZ)	/* two hours */
  #define TCP_KEEPALIVE_PROBES	9		/* Max of 9 keepalive probes	*/
  #define TCP_KEEPALIVE_INTVL	(75*HZ)
  
  #define MAX_TCP_KEEPIDLE	32767
  #define MAX_TCP_KEEPINTVL	32767
  #define MAX_TCP_KEEPCNT		127
  #define MAX_TCP_SYNCNT		127
  
  #define TCP_SYNQ_INTERVAL	(HZ/5)	/* Period of SYNACK timer */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
154
155
156
157
158
159
160
161
162
163
164
165
  
  #define TCP_PAWS_24DAYS	(60 * 60 * 24 * 24)
  #define TCP_PAWS_MSL	60		/* Per-host timestamps are invalidated
  					 * after this time. It should be equal
  					 * (or greater than) TCP_TIMEWAIT_LEN
  					 * to provide reliability equal to one
  					 * provided by timewait state.
  					 */
  #define TCP_PAWS_WINDOW	1		/* Replay window for per-host
  					 * timestamps. It must be less than
  					 * minimal timewait lifetime.
  					 */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
166
167
168
  /*
   *	TCP option
   */
105970f60   Kenjiro Nakayama   net: Remove trail...
169

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
170
171
172
173
174
175
176
  #define TCPOPT_NOP		1	/* Padding */
  #define TCPOPT_EOL		0	/* End of options */
  #define TCPOPT_MSS		2	/* Segment size negotiating */
  #define TCPOPT_WINDOW		3	/* Window scaling */
  #define TCPOPT_SACK_PERM        4       /* SACK Permitted */
  #define TCPOPT_SACK             5       /* SACK Block */
  #define TCPOPT_TIMESTAMP	8	/* Better RTT estimations/PAWS */
cfb6eeb4c   YOSHIFUJI Hideaki   [TCP]: MD5 Signat...
177
  #define TCPOPT_MD5SIG		19	/* MD5 Signature (RFC2385) */
c74a39c86   Mat Martineau   tcp: Add MPTCP op...
178
  #define TCPOPT_MPTCP		30	/* Multipath TCP (RFC6824) */
7f9b838b7   Daniel Lee   tcp: RFC7413 opti...
179
  #define TCPOPT_FASTOPEN		34	/* Fast open (RFC7413) */
2100c8d2d   Yuchung Cheng   net-tcp: Fast Ope...
180
181
182
183
184
  #define TCPOPT_EXP		254	/* Experimental */
  /* Magic number to be after the option value for sharing TCP
   * experimental options. See draft-ietf-tcpm-experimental-options-00.txt
   */
  #define TCPOPT_FASTOPEN_MAGIC	0xF989
60e2a7780   Ursula Braun   tcp: TCP experime...
185
  #define TCPOPT_SMC_MAGIC	0xE2D4C3D9
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
186
187
188
189
190
191
192
193
194
  
  /*
   *     TCP option lengths
   */
  
  #define TCPOLEN_MSS            4
  #define TCPOLEN_WINDOW         3
  #define TCPOLEN_SACK_PERM      2
  #define TCPOLEN_TIMESTAMP      10
cfb6eeb4c   YOSHIFUJI Hideaki   [TCP]: MD5 Signat...
195
  #define TCPOLEN_MD5SIG         18
7f9b838b7   Daniel Lee   tcp: RFC7413 opti...
196
  #define TCPOLEN_FASTOPEN_BASE  2
2100c8d2d   Yuchung Cheng   net-tcp: Fast Ope...
197
  #define TCPOLEN_EXP_FASTOPEN_BASE  4
60e2a7780   Ursula Braun   tcp: TCP experime...
198
  #define TCPOLEN_EXP_SMC_BASE   6
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
199
200
201
202
203
204
205
206
  
  /* But this is what stacks really send out. */
  #define TCPOLEN_TSTAMP_ALIGNED		12
  #define TCPOLEN_WSCALE_ALIGNED		4
  #define TCPOLEN_SACKPERM_ALIGNED	4
  #define TCPOLEN_SACK_BASE		2
  #define TCPOLEN_SACK_BASE_ALIGNED	4
  #define TCPOLEN_SACK_PERBLOCK		8
cfb6eeb4c   YOSHIFUJI Hideaki   [TCP]: MD5 Signat...
207
  #define TCPOLEN_MD5SIG_ALIGNED		20
33ad798c9   Adam Langley   tcp: options clea...
208
  #define TCPOLEN_MSS_ALIGNED		4
60e2a7780   Ursula Braun   tcp: TCP experime...
209
  #define TCPOLEN_EXP_SMC_BASE_ALIGNED	8
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
210

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
211
212
213
  /* Flags in tp->nonagle */
  #define TCP_NAGLE_OFF		1	/* Nagle's algo is disabled */
  #define TCP_NAGLE_CORK		2	/* Socket is corked	    */
caa20d9ab   Stephen Hemminger   [TCP]: spelling f...
214
  #define TCP_NAGLE_PUSH		4	/* Cork is overridden for already queued data */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
215

36e31b0af   Andreas Petlund   net: TCP thin lin...
216
217
  /* TCP thin-stream limits */
  #define TCP_THIN_LINEAR_RETRIES 6       /* After 6 linear retries, do exp. backoff */
21603fc45   Jörg Thalheim   tcp: Change refer...
218
  /* TCP initial congestion window as per rfc6928 */
442b9635c   David S. Miller   tcp: Increase the...
219
  #define TCP_INIT_CWND		10
cf60af03c   Yuchung Cheng   net-tcp: Fast Ope...
220
221
  /* Bit Flags for sysctl_tcp_fastopen */
  #define	TFO_CLIENT_ENABLE	1
104671636   Jerry Chu   tcp: TCP Fast Ope...
222
  #define	TFO_SERVER_ENABLE	2
67da22d23   Yuchung Cheng   net-tcp: Fast Ope...
223
  #define	TFO_CLIENT_NO_COOKIE	4	/* Data in SYN w/o cookie option */
cf60af03c   Yuchung Cheng   net-tcp: Fast Ope...
224

104671636   Jerry Chu   tcp: TCP Fast Ope...
225
226
227
228
  /* Accept SYN data w/o any cookie option */
  #define	TFO_SERVER_COOKIE_NOT_REQD	0x200
  
  /* Force enable TFO on all listeners, i.e., not requiring the
cebc5cbab   Yuchung Cheng   net-tcp: retire T...
229
   * TCP_FASTOPEN socket option.
104671636   Jerry Chu   tcp: TCP Fast Ope...
230
231
   */
  #define	TFO_SERVER_WO_SOCKOPT1	0x400
104671636   Jerry Chu   tcp: TCP Fast Ope...
232

295ff7edb   Arnaldo Carvalho de Melo   [TIMEWAIT]: Intro...
233

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
234
  /* sysctl variables for tcp */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
235
  extern int sysctl_tcp_max_orphans;
a4fe34bf9   Eric W. Biederman   tcp_memcontrol: R...
236
  extern long sysctl_tcp_mem[3];
e20223f19   Eric Dumazet   tcp: Namespace-if...
237

a0370b3f3   Yuchung Cheng   tcp: enable RACK ...
238
  #define TCP_RACK_LOSS_DETECTION  0x1 /* Use RACK to detect losses */
1f2556916   Priyaranjan Jha   tcp: higher throu...
239
  #define TCP_RACK_STATIC_REO_WND  0x2 /* Use static RACK reo wnd */
20b654dfe   Yuchung Cheng   tcp: support DUPA...
240
  #define TCP_RACK_NO_DUPTHRESH    0x4 /* Do not use DUPACK threshold in RACK */
a0370b3f3   Yuchung Cheng   tcp: enable RACK ...
241

8d987e5c7   Eric Dumazet   net: avoid limits...
242
  extern atomic_long_t tcp_memory_allocated;
1748376b6   Eric Dumazet   net: Use a percpu...
243
  extern struct percpu_counter tcp_sockets_allocated;
060447511   Eric Dumazet   tcp: add TCPMemor...
244
  extern unsigned long tcp_memory_pressure;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
245

b8da51ebb   Eric Dumazet   tcp: introduce tc...
246
247
248
  /* optimized version of sk_under_memory_pressure() for TCP sockets */
  static inline bool tcp_under_memory_pressure(const struct sock *sk)
  {
baac50bbc   Johannes Weiner   net: tcp_memcontr...
249
250
  	if (mem_cgroup_sockets_enabled && sk->sk_memcg &&
  	    mem_cgroup_under_socket_pressure(sk->sk_memcg))
e805605c7   Johannes Weiner   net: tcp_memcontr...
251
  		return true;
b8da51ebb   Eric Dumazet   tcp: introduce tc...
252

1f142c17d   Eric Dumazet   tcp: annotate loc...
253
  	return READ_ONCE(tcp_memory_pressure);
b8da51ebb   Eric Dumazet   tcp: introduce tc...
254
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
255
  /*
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
256
257
258
   * The next routines deal with comparing 32 bit unsigned ints
   * and worry about wraparound (automatic with unsigned arithmetic).
   */
a2a385d62   Eric Dumazet   tcp: bool convers...
259
  static inline bool before(__u32 seq1, __u32 seq2)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
260
  {
0d630cc0a   Gerrit Renker   [TCP]: Use old de...
261
          return (__s32)(seq1-seq2) < 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
262
  }
9a036b9c3   Gerrit Renker   [TCP]: Fix ambigu...
263
  #define after(seq2, seq1) 	before(seq1, seq2)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
264
265
  
  /* is s2<=s1<=s3 ? */
a2a385d62   Eric Dumazet   tcp: bool convers...
266
  static inline bool between(__u32 seq1, __u32 seq2, __u32 seq3)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
267
268
269
  {
  	return seq3 - seq2 >= seq1 - seq2;
  }
efcdbf24f   Arun Sharma   net: Disambiguate...
270
271
272
273
274
275
276
  static inline bool tcp_out_of_memory(struct sock *sk)
  {
  	if (sk->sk_wmem_queued > SOCK_MIN_SNDBUF &&
  	    sk_memory_allocated(sk) > sk_prot_mem_limits(sk, 2))
  		return true;
  	return false;
  }
a6c5ea4cc   Eric Dumazet   tcp: rename sk_fo...
277
  void sk_forced_mem_schedule(struct sock *sk, int size);
ad1af0fed   David S. Miller   tcp: Combat per-c...
278
  static inline bool tcp_too_many_orphans(struct sock *sk, int shift)
e4fd5da39   Pavel Emelianov   [TCP]: Consolidat...
279
  {
ad1af0fed   David S. Miller   tcp: Combat per-c...
280
281
282
283
284
285
286
287
  	struct percpu_counter *ocp = sk->sk_prot->orphan_count;
  	int orphans = percpu_counter_read_positive(ocp);
  
  	if (orphans << shift > sysctl_tcp_max_orphans) {
  		orphans = percpu_counter_sum_positive(ocp);
  		if (orphans << shift > sysctl_tcp_max_orphans)
  			return true;
  	}
ad1af0fed   David S. Miller   tcp: Combat per-c...
288
  	return false;
e4fd5da39   Pavel Emelianov   [TCP]: Consolidat...
289
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
290

5c9f30236   Joe Perches   tcp.h: Remove ext...
291
  bool tcp_check_oom(struct sock *sk, int shift);
efcdbf24f   Arun Sharma   net: Disambiguate...
292

a0f82f64e   Florian Westphal   syncookies: remov...
293

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
294
  extern struct proto tcp_prot;
57ef42d59   Pavel Emelyanov   mib: put tcp stat...
295
  #define TCP_INC_STATS(net, field)	SNMP_INC_STATS((net)->mib.tcp_statistics, field)
13415e46c   Eric Dumazet   net: snmp: kill S...
296
  #define __TCP_INC_STATS(net, field)	__SNMP_INC_STATS((net)->mib.tcp_statistics, field)
57ef42d59   Pavel Emelyanov   mib: put tcp stat...
297
  #define TCP_DEC_STATS(net, field)	SNMP_DEC_STATS((net)->mib.tcp_statistics, field)
aa2ea0586   Tom Herbert   tcp: fix outsegs ...
298
  #define TCP_ADD_STATS(net, field, val)	SNMP_ADD_STATS((net)->mib.tcp_statistics, field, val)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
299

5c9f30236   Joe Perches   tcp.h: Remove ext...
300
  void tcp_tasklet_init(void);
32bbd8793   Stefano Brivio   net: Convert prot...
301
  int tcp_v4_err(struct sk_buff *skb, u32);
5c9f30236   Joe Perches   tcp.h: Remove ext...
302
303
  
  void tcp_shutdown(struct sock *sk, int how);
7487449c8   Paolo Abeni   IPv4: early demux...
304
  int tcp_v4_early_demux(struct sk_buff *skb);
5c9f30236   Joe Perches   tcp.h: Remove ext...
305
306
307
  int tcp_v4_rcv(struct sk_buff *skb);
  
  int tcp_v4_tw_remember_stamp(struct inet_timewait_sock *tw);
1b7841404   Ying Xue   net: Remove iocb ...
308
  int tcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t size);
306b13eb3   Tom Herbert   proto_ops: Add lo...
309
  int tcp_sendmsg_locked(struct sock *sk, struct msghdr *msg, size_t size);
5c9f30236   Joe Perches   tcp.h: Remove ext...
310
311
  int tcp_sendpage(struct sock *sk, struct page *page, int offset, size_t size,
  		 int flags);
306b13eb3   Tom Herbert   proto_ops: Add lo...
312
313
  int tcp_sendpage_locked(struct sock *sk, struct page *page, int offset,
  			size_t size, int flags);
e3b5616a3   Dave Watson   tcp: export do_tc...
314
315
  ssize_t do_tcp_sendpages(struct sock *sk, struct page *page, int offset,
  		 size_t size, int flags);
35b2c3211   Mat Martineau   tcp: Export TCP f...
316
317
318
  int tcp_send_mss(struct sock *sk, int *size_goal, int flags);
  void tcp_push(struct sock *sk, int flags, int mss_now, int nonagle,
  	      int size_goal);
5c9f30236   Joe Perches   tcp.h: Remove ext...
319
320
321
322
323
  void tcp_release_cb(struct sock *sk);
  void tcp_wfree(struct sk_buff *skb);
  void tcp_write_timer_handler(struct sock *sk);
  void tcp_delack_timer_handler(struct sock *sk);
  int tcp_ioctl(struct sock *sk, int cmd, unsigned long arg);
72ab4a86f   Eric Dumazet   tcp: remove tcp_r...
324
  int tcp_rcv_state_process(struct sock *sk, struct sk_buff *skb);
3d97d88e8   Yafang Shao   tcp: minor optimi...
325
  void tcp_rcv_established(struct sock *sk, struct sk_buff *skb);
5c9f30236   Joe Perches   tcp.h: Remove ext...
326
  void tcp_rcv_space_adjust(struct sock *sk);
5c9f30236   Joe Perches   tcp.h: Remove ext...
327
328
329
330
331
  int tcp_twsk_unique(struct sock *sk, struct sock *sktw, void *twp);
  void tcp_twsk_destructor(struct sock *sk);
  ssize_t tcp_splice_read(struct socket *sk, loff_t *ppos,
  			struct pipe_inode_info *pipe, size_t len,
  			unsigned int flags);
9c55e01c0   Jens Axboe   [TCP]: Splice rec...
332

a0496ef2c   Yuchung Cheng   tcp: do not delay...
333
  void tcp_enter_quickack_mode(struct sock *sk, unsigned int max_quickacks);
463c84b97   Arnaldo Carvalho de Melo   [NET]: Introduce ...
334
335
  static inline void tcp_dec_quickack_mode(struct sock *sk,
  					 const unsigned int pkts)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
336
  {
463c84b97   Arnaldo Carvalho de Melo   [NET]: Introduce ...
337
  	struct inet_connection_sock *icsk = inet_csk(sk);
fc6415bcb   David S. Miller   [TCP]: Fix quick-...
338

463c84b97   Arnaldo Carvalho de Melo   [NET]: Introduce ...
339
340
341
  	if (icsk->icsk_ack.quick) {
  		if (pkts >= icsk->icsk_ack.quick) {
  			icsk->icsk_ack.quick = 0;
fc6415bcb   David S. Miller   [TCP]: Fix quick-...
342
  			/* Leaving quickack mode we deflate ATO. */
463c84b97   Arnaldo Carvalho de Melo   [NET]: Introduce ...
343
  			icsk->icsk_ack.ato   = TCP_ATO_MIN;
fc6415bcb   David S. Miller   [TCP]: Fix quick-...
344
  		} else
463c84b97   Arnaldo Carvalho de Melo   [NET]: Introduce ...
345
  			icsk->icsk_ack.quick -= pkts;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
346
347
  	}
  }
bdf1ee5d3   Ilpo Järvinen   [TCP]: Move code ...
348
349
350
  #define	TCP_ECN_OK		1
  #define	TCP_ECN_QUEUE_CWR	2
  #define	TCP_ECN_DEMAND_CWR	4
7a269ffad   Eric Dumazet   tcp: ECN blackhol...
351
  #define	TCP_ECN_SEEN		8
bdf1ee5d3   Ilpo Järvinen   [TCP]: Move code ...
352

fd2c3ef76   Eric Dumazet   net: cleanup incl...
353
  enum tcp_tw_status {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
354
355
356
357
358
  	TCP_TW_SUCCESS = 0,
  	TCP_TW_RST = 1,
  	TCP_TW_ACK = 2,
  	TCP_TW_SYN = 3
  };
5c9f30236   Joe Perches   tcp.h: Remove ext...
359
360
361
362
  enum tcp_tw_status tcp_timewait_state_process(struct inet_timewait_sock *tw,
  					      struct sk_buff *skb,
  					      const struct tcphdr *th);
  struct sock *tcp_check_req(struct sock *sk, struct sk_buff *skb,
e0f9759f5   Eric Dumazet   tcp: try to keep ...
363
364
  			   struct request_sock *req, bool fastopen,
  			   bool *lost_race);
5c9f30236   Joe Perches   tcp.h: Remove ext...
365
366
  int tcp_child_process(struct sock *parent, struct sock *child,
  		      struct sk_buff *skb);
5ae344c94   Neal Cardwell   tcp: reduce spuri...
367
  void tcp_enter_loss(struct sock *sk);
57dde7f70   Yuchung Cheng   tcp: add reorderi...
368
  void tcp_cwnd_reduction(struct sock *sk, int newly_acked_sacked, int flag);
5c9f30236   Joe Perches   tcp.h: Remove ext...
369
370
371
372
  void tcp_clear_retrans(struct tcp_sock *tp);
  void tcp_update_metrics(struct sock *sk);
  void tcp_init_metrics(struct sock *sk);
  void tcp_metrics_init(void);
d82bae12d   Soheil Hassas Yeganeh   tcp: remove per-d...
373
  bool tcp_peer_is_proven(struct request_sock *req, struct dst_entry *dst);
5c9f30236   Joe Perches   tcp.h: Remove ext...
374
375
  void tcp_close(struct sock *sk, long timeout);
  void tcp_init_sock(struct sock *sk);
72be0fe6b   Martin KaFai Lau   bpf: tcp: Add bpf...
376
  void tcp_init_transfer(struct sock *sk, int bpf_op, struct sk_buff *skb);
a11e1d432   Linus Torvalds   Revert changes to...
377
378
  __poll_t tcp_poll(struct file *file, struct socket *sock,
  		      struct poll_table_struct *wait);
5c9f30236   Joe Perches   tcp.h: Remove ext...
379
380
  int tcp_getsockopt(struct sock *sk, int level, int optname,
  		   char __user *optval, int __user *optlen);
a7b75c5a8   Christoph Hellwig   net: pass a sockp...
381
382
  int tcp_setsockopt(struct sock *sk, int level, int optname, sockptr_t optval,
  		   unsigned int optlen);
5c9f30236   Joe Perches   tcp.h: Remove ext...
383
  void tcp_set_keepalive(struct sock *sk, int val);
42cb80a23   Eric Dumazet   inet: remove sk_l...
384
  void tcp_syn_ack_timeout(const struct request_sock *req);
1b7841404   Ying Xue   net: Remove iocb ...
385
386
  int tcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int nonblock,
  		int flags, int *addr_len);
d1361840f   Eric Dumazet   tcp: fix SO_RCVLO...
387
  int tcp_set_rcvlowat(struct sock *sk, int val);
03f45c883   Eric Dumazet   tcp: avoid extra ...
388
  void tcp_data_ready(struct sock *sk);
340a6f3d2   Yafang Shao   tcp: declare tcp_...
389
  #ifdef CONFIG_MMU
93ab6cc69   Eric Dumazet   tcp: implement mm...
390
391
  int tcp_mmap(struct file *file, struct socket *sock,
  	     struct vm_area_struct *vma);
340a6f3d2   Yafang Shao   tcp: declare tcp_...
392
  #endif
eed29f17f   Eric Dumazet   tcp: add a struct...
393
  void tcp_parse_options(const struct net *net, const struct sk_buff *skb,
5c9f30236   Joe Perches   tcp.h: Remove ext...
394
395
396
  		       struct tcp_options_received *opt_rx,
  		       int estab, struct tcp_fastopen_cookie *foc);
  const u8 *tcp_parse_md5sig_option(const struct tcphdr *th);
7d5d5525b   YOSHIFUJI Hideaki   tcp md5sig: Share...
397

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
398
  /*
9349d600f   Petar Penkov   tcp: add skb-less...
399
400
401
402
403
404
405
406
407
408
   *	BPF SKB-less helpers
   */
  u16 tcp_v4_get_syncookie(struct sock *sk, struct iphdr *iph,
  			 struct tcphdr *th, u32 *cookie);
  u16 tcp_v6_get_syncookie(struct sock *sk, struct ipv6hdr *iph,
  			 struct tcphdr *th, u32 *cookie);
  u16 tcp_get_syncookie_mss(struct request_sock_ops *rsk_ops,
  			  const struct tcp_request_sock_ops *af_ops,
  			  struct sock *sk, struct tcphdr *th);
  /*
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
409
410
   *	TCP v4 functions exported for the inet6 API
   */
5c9f30236   Joe Perches   tcp.h: Remove ext...
411
  void tcp_v4_send_check(struct sock *sk, struct sk_buff *skb);
4fab90719   Neal Cardwell   tcp: fix tcp_rele...
412
  void tcp_v4_mtu_reduced(struct sock *sk);
9cf749036   Eric Dumazet   tcp: do not drop ...
413
  void tcp_req_err(struct sock *sk, u32 seq, bool abort);
d29245692   Eric Dumazet   tcp: ipv6: suppor...
414
  void tcp_ld_RTO_revert(struct sock *sk, u32 seq);
5c9f30236   Joe Perches   tcp.h: Remove ext...
415
  int tcp_v4_conn_request(struct sock *sk, struct sk_buff *skb);
c28c6f045   Eric Dumazet   tcp: constify tcp...
416
  struct sock *tcp_create_openreq_child(const struct sock *sk,
5c9f30236   Joe Perches   tcp.h: Remove ext...
417
418
  				      struct request_sock *req,
  				      struct sk_buff *skb);
81164413a   Daniel Borkmann   net: tcp: add per...
419
  void tcp_ca_openreq_child(struct sock *sk, const struct dst_entry *dst);
0c27171e6   Eric Dumazet   tcp/dccp: constif...
420
  struct sock *tcp_v4_syn_recv_sock(const struct sock *sk, struct sk_buff *skb,
5c9f30236   Joe Perches   tcp.h: Remove ext...
421
  				  struct request_sock *req,
5e0724d02   Eric Dumazet   tcp/dccp: fix has...
422
423
424
  				  struct dst_entry *dst,
  				  struct request_sock *req_unhash,
  				  bool *own_req);
5c9f30236   Joe Perches   tcp.h: Remove ext...
425
426
427
  int tcp_v4_do_rcv(struct sock *sk, struct sk_buff *skb);
  int tcp_v4_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len);
  int tcp_connect(struct sock *sk);
b3d051477   Eric Dumazet   tcp: do not mess ...
428
429
430
431
432
  enum tcp_synack_type {
  	TCP_SYNACK_NORMAL,
  	TCP_SYNACK_FASTOPEN,
  	TCP_SYNACK_COOKIE,
  };
5d062de7f   Eric Dumazet   tcp: constify tcp...
433
  struct sk_buff *tcp_make_synack(const struct sock *sk, struct dst_entry *dst,
5c9f30236   Joe Perches   tcp.h: Remove ext...
434
  				struct request_sock *req,
ca6fb0651   Eric Dumazet   tcp: attach SYNAC...
435
  				struct tcp_fastopen_cookie *foc,
331fca431   Martin KaFai Lau   bpf: tcp: Add bpf...
436
437
  				enum tcp_synack_type synack_type,
  				struct sk_buff *syn_skb);
5c9f30236   Joe Perches   tcp.h: Remove ext...
438
  int tcp_disconnect(struct sock *sk, int flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
439

370816aef   Pavel Emelyanov   tcp: Move code ar...
440
  void tcp_finish_connect(struct sock *sk, struct sk_buff *skb);
292e8d8c8   Pavel Emelyanov   tcp: Move rcvq se...
441
  int tcp_send_rcvq(struct sock *sk, struct msghdr *msg, size_t size);
63d02d157   Eric Dumazet   net: tcp: ipv6_ma...
442
  void inet_sk_rx_dst_set(struct sock *sk, const struct sk_buff *skb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
443

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
444
  /* From syncookies.c */
b80c0e785   Eric Dumazet   tcp: get_cookie_s...
445
446
  struct sock *tcp_get_cookie_sock(struct sock *sk, struct sk_buff *skb,
  				 struct request_sock *req,
84b114b98   Eric Dumazet   tcp: randomize ti...
447
  				 struct dst_entry *dst, u32 tsoff);
5c9f30236   Joe Perches   tcp.h: Remove ext...
448
449
  int __cookie_v4_check(const struct iphdr *iph, const struct tcphdr *th,
  		      u32 cookie);
461b74c39   Cong Wang   ipv4: clean up co...
450
  struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb);
6fc8c827d   Florian Westphal   tcp: syncookies: ...
451
452
  struct request_sock *cookie_tcp_reqsk_alloc(const struct request_sock_ops *ops,
  					    struct sock *sk, struct sk_buff *skb);
e05c82d36   Eric Dumazet   tcp: fix build er...
453
  #ifdef CONFIG_SYN_COOKIES
8c27bd75f   Florian Westphal   tcp: syncookies: ...
454

632623153   Eric Dumazet   tcp: syncookies: ...
455
  /* Syncookies use a monotonic timer which increments every 60 seconds.
8c27bd75f   Florian Westphal   tcp: syncookies: ...
456
457
458
   * This counter is used both as a hash input and partially encoded into
   * the cookie value.  A cookie is only validated further if the delta
   * between the current counter value and the encoded one is less than this,
632623153   Eric Dumazet   tcp: syncookies: ...
459
   * i.e. a sent cookie is valid only at most for 2*60 seconds (or less if
8c27bd75f   Florian Westphal   tcp: syncookies: ...
460
461
   * the counter advances immediately after a cookie is generated).
   */
264ea103a   Eric Dumazet   tcp: syncookies: ...
462
463
464
465
466
467
  #define MAX_SYNCOOKIE_AGE	2
  #define TCP_SYNCOOKIE_PERIOD	(60 * HZ)
  #define TCP_SYNCOOKIE_VALID	(MAX_SYNCOOKIE_AGE * TCP_SYNCOOKIE_PERIOD)
  
  /* syncookies: remember time of last synqueue overflow
   * But do not dirty this field too often (once per second is enough)
3f684b4b1   Eric Dumazet   tcp: cookie_init_...
468
   * It is racy as we do not hold a lock, but race is very minor.
264ea103a   Eric Dumazet   tcp: syncookies: ...
469
   */
3f684b4b1   Eric Dumazet   tcp: cookie_init_...
470
  static inline void tcp_synq_overflow(const struct sock *sk)
264ea103a   Eric Dumazet   tcp: syncookies: ...
471
  {
40a1227ea   Martin KaFai Lau   tcp: Avoid TCP sy...
472
  	unsigned int last_overflow;
cca9bab1b   Arnd Bergmann   tcp: use monotoni...
473
  	unsigned int now = jiffies;
264ea103a   Eric Dumazet   tcp: syncookies: ...
474

40a1227ea   Martin KaFai Lau   tcp: Avoid TCP sy...
475
476
477
478
479
480
  	if (sk->sk_reuseport) {
  		struct sock_reuseport *reuse;
  
  		reuse = rcu_dereference(sk->sk_reuseport_cb);
  		if (likely(reuse)) {
  			last_overflow = READ_ONCE(reuse->synq_overflow_ts);
04d26e7b1   Guillaume Nault   tcp: fix rejected...
481
482
  			if (!time_between32(now, last_overflow,
  					    last_overflow + HZ))
40a1227ea   Martin KaFai Lau   tcp: Avoid TCP sy...
483
484
485
486
  				WRITE_ONCE(reuse->synq_overflow_ts, now);
  			return;
  		}
  	}
721c8dafa   Guillaume Nault   tcp: Protect acce...
487
  	last_overflow = READ_ONCE(tcp_sk(sk)->rx_opt.ts_recent_stamp);
04d26e7b1   Guillaume Nault   tcp: fix rejected...
488
  	if (!time_between32(now, last_overflow, last_overflow + HZ))
721c8dafa   Guillaume Nault   tcp: Protect acce...
489
  		WRITE_ONCE(tcp_sk(sk)->rx_opt.ts_recent_stamp, now);
264ea103a   Eric Dumazet   tcp: syncookies: ...
490
491
492
493
494
  }
  
  /* syncookies: no recent synqueue overflow on this listening socket? */
  static inline bool tcp_synq_no_recent_overflow(const struct sock *sk)
  {
40a1227ea   Martin KaFai Lau   tcp: Avoid TCP sy...
495
  	unsigned int last_overflow;
cca9bab1b   Arnd Bergmann   tcp: use monotoni...
496
  	unsigned int now = jiffies;
264ea103a   Eric Dumazet   tcp: syncookies: ...
497

40a1227ea   Martin KaFai Lau   tcp: Avoid TCP sy...
498
499
500
501
502
503
  	if (sk->sk_reuseport) {
  		struct sock_reuseport *reuse;
  
  		reuse = rcu_dereference(sk->sk_reuseport_cb);
  		if (likely(reuse)) {
  			last_overflow = READ_ONCE(reuse->synq_overflow_ts);
cb44a08f8   Guillaume Nault   tcp: tighten acce...
504
505
506
  			return !time_between32(now, last_overflow - HZ,
  					       last_overflow +
  					       TCP_SYNCOOKIE_VALID);
40a1227ea   Martin KaFai Lau   tcp: Avoid TCP sy...
507
508
  		}
  	}
721c8dafa   Guillaume Nault   tcp: Protect acce...
509
  	last_overflow = READ_ONCE(tcp_sk(sk)->rx_opt.ts_recent_stamp);
cb44a08f8   Guillaume Nault   tcp: tighten acce...
510
511
512
513
514
515
516
517
518
519
  
  	/* If last_overflow <= jiffies <= last_overflow + TCP_SYNCOOKIE_VALID,
  	 * then we're under synflood. However, we have to use
  	 * 'last_overflow - HZ' as lower bound. That's because a concurrent
  	 * tcp_synq_overflow() could update .ts_recent_stamp after we read
  	 * jiffies but before we store .ts_recent_stamp into last_overflow,
  	 * which could lead to rejecting a valid syncookie.
  	 */
  	return !time_between32(now, last_overflow - HZ,
  			       last_overflow + TCP_SYNCOOKIE_VALID);
264ea103a   Eric Dumazet   tcp: syncookies: ...
520
  }
8c27bd75f   Florian Westphal   tcp: syncookies: ...
521
522
523
  
  static inline u32 tcp_cookie_time(void)
  {
632623153   Eric Dumazet   tcp: syncookies: ...
524
  	u64 val = get_jiffies_64();
264ea103a   Eric Dumazet   tcp: syncookies: ...
525
  	do_div(val, TCP_SYNCOOKIE_PERIOD);
632623153   Eric Dumazet   tcp: syncookies: ...
526
  	return val;
8c27bd75f   Florian Westphal   tcp: syncookies: ...
527
  }
5c9f30236   Joe Perches   tcp.h: Remove ext...
528
529
  u32 __cookie_v4_init_sequence(const struct iphdr *iph, const struct tcphdr *th,
  			      u16 *mssp);
3f684b4b1   Eric Dumazet   tcp: cookie_init_...
530
  __u32 cookie_v4_init_sequence(const struct sk_buff *skb, __u16 *mss);
200ecef67   Eric Dumazet   tcp: Remove one e...
531
  u64 cookie_init_timestamp(struct request_sock *req, u64 now);
f93010342   Eric Dumazet   tcp: Namespaceify...
532
533
  bool cookie_timestamp_decode(const struct net *net,
  			     struct tcp_options_received *opt);
f1673381b   Florian Westphal   syncookies: split...
534
  bool cookie_ecn_ok(const struct tcp_options_received *opt,
f7b3bec6f   Florian Westphal   net: allow settin...
535
  		   const struct net *net, const struct dst_entry *dst);
4dfc28170   Florian Westphal   [Syncookies]: Add...
536

c6aefafb7   Glenn Griffin   [TCP]: Add IPv6 s...
537
  /* From net/ipv6/syncookies.c */
5c9f30236   Joe Perches   tcp.h: Remove ext...
538
539
540
  int __cookie_v6_check(const struct ipv6hdr *iph, const struct tcphdr *th,
  		      u32 cookie);
  struct sock *cookie_v6_check(struct sock *sk, struct sk_buff *skb);
f1673381b   Florian Westphal   syncookies: split...
541

5c9f30236   Joe Perches   tcp.h: Remove ext...
542
543
  u32 __cookie_v6_init_sequence(const struct ipv6hdr *iph,
  			      const struct tcphdr *th, u16 *mssp);
3f684b4b1   Eric Dumazet   tcp: cookie_init_...
544
  __u32 cookie_v6_init_sequence(const struct sk_buff *skb, __u16 *mss);
e05c82d36   Eric Dumazet   tcp: fix build er...
545
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
546
  /* tcp_output.c */
5c9f30236   Joe Perches   tcp.h: Remove ext...
547
548
  void __tcp_push_pending_frames(struct sock *sk, unsigned int cur_mss,
  			       int nonagle);
10d3be569   Eric Dumazet   tcp-tso: do not s...
549
550
  int __tcp_retransmit_skb(struct sock *sk, struct sk_buff *skb, int segs);
  int tcp_retransmit_skb(struct sock *sk, struct sk_buff *skb, int segs);
5c9f30236   Joe Perches   tcp.h: Remove ext...
551
552
553
  void tcp_retransmit_timer(struct sock *sk);
  void tcp_xmit_retransmit_queue(struct sock *);
  void tcp_simple_retransmit(struct sock *);
57dde7f70   Yuchung Cheng   tcp: add reorderi...
554
  void tcp_enter_recovery(struct sock *sk, bool ece_ack);
5c9f30236   Joe Perches   tcp.h: Remove ext...
555
  int tcp_trim_head(struct sock *, struct sk_buff *, u32);
75c119afe   Eric Dumazet   tcp: implement rb...
556
557
558
559
560
561
562
  enum tcp_queue {
  	TCP_FRAG_IN_WRITE_QUEUE,
  	TCP_FRAG_IN_RTX_QUEUE,
  };
  int tcp_fragment(struct sock *sk, enum tcp_queue tcp_queue,
  		 struct sk_buff *skb, u32 len,
  		 unsigned int mss_now, gfp_t gfp);
5c9f30236   Joe Perches   tcp.h: Remove ext...
563
564
565
  
  void tcp_send_probe0(struct sock *);
  void tcp_send_partial(struct sock *);
e520af48c   Eric Dumazet   tcp: add TCPWinPr...
566
  int tcp_write_wakeup(struct sock *, int mib);
5c9f30236   Joe Perches   tcp.h: Remove ext...
567
568
569
  void tcp_send_fin(struct sock *sk);
  void tcp_send_active_reset(struct sock *sk, gfp_t priority);
  int tcp_send_synack(struct sock *);
5c9f30236   Joe Perches   tcp.h: Remove ext...
570
  void tcp_push_one(struct sock *, unsigned int mss_now);
27cde44a2   Yuchung Cheng   tcp: do not cance...
571
  void __tcp_send_ack(struct sock *sk, u32 rcv_nxt);
5c9f30236   Joe Perches   tcp.h: Remove ext...
572
573
574
  void tcp_send_ack(struct sock *sk);
  void tcp_send_delayed_ack(struct sock *sk);
  void tcp_send_loss_probe(struct sock *sk);
ed66dfaf2   Neal Cardwell   tcp: when schedul...
575
  bool tcp_schedule_loss_probe(struct sock *sk, bool advancing_rto);
cfea5a688   Martin KaFai Lau   tcp: Merge tx_fla...
576
577
  void tcp_skb_collapse_tstamp(struct sk_buff *skb,
  			     const struct sk_buff *next_skb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
578

a762a9800   David S. Miller   [TCP]: Kill extra...
579
  /* tcp_input.c */
5c9f30236   Joe Perches   tcp.h: Remove ext...
580
  void tcp_rearm_rto(struct sock *sk);
0f1c28ae7   Yuchung Cheng   tcp: usec resolut...
581
  void tcp_synack_rtt_meas(struct sock *sk, struct request_sock *req);
5c9f30236   Joe Perches   tcp.h: Remove ext...
582
  void tcp_reset(struct sock *sk);
4f41b1c58   Yuchung Cheng   tcp: use RACK to ...
583
  void tcp_skb_mark_lost_uncond_verify(struct tcp_sock *tp, struct sk_buff *skb);
e3e17b773   Eric Dumazet   tcp: fastopen: ca...
584
  void tcp_fin(struct sock *sk);
a762a9800   David S. Miller   [TCP]: Kill extra...
585

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
586
  /* tcp_timer.c */
5c9f30236   Joe Perches   tcp.h: Remove ext...
587
  void tcp_init_xmit_timers(struct sock *);
463c84b97   Arnaldo Carvalho de Melo   [NET]: Introduce ...
588
589
  static inline void tcp_clear_xmit_timers(struct sock *sk)
  {
73a6bab5a   Eric Dumazet   tcp: switch pacin...
590
  	if (hrtimer_try_to_cancel(&tcp_sk(sk)->pacing_timer) == 1)
cf0dd2037   Eric Dumazet   tcp: use __sock_p...
591
  		__sock_put(sk);
73a6bab5a   Eric Dumazet   tcp: switch pacin...
592

5d9f4262b   Eric Dumazet   tcp: add SACK com...
593
594
  	if (hrtimer_try_to_cancel(&tcp_sk(sk)->compressed_ack_timer) == 1)
  		__sock_put(sk);
463c84b97   Arnaldo Carvalho de Melo   [NET]: Introduce ...
595
596
  	inet_csk_clear_xmit_timers(sk);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
597

5c9f30236   Joe Perches   tcp.h: Remove ext...
598
599
  unsigned int tcp_sync_mss(struct sock *sk, u32 pmtu);
  unsigned int tcp_current_mss(struct sock *sk);
0c54b85f2   Ilpo Järvinen   tcp: simplify tcp...
600
601
602
603
  
  /* Bound MSS / TSO packet size with the half of the window */
  static inline int tcp_bound_to_half_wnd(struct tcp_sock *tp, int pktsize)
  {
01f83d698   Alexey Kuznetsov   tcp: Prevent over...
604
605
606
607
608
609
610
611
612
  	int cutoff;
  
  	/* When peer uses tiny windows, there is no use in packetizing
  	 * to sub-MSS pieces for the sake of SWS or making sure there
  	 * are enough packets in the pipe for fast recovery.
  	 *
  	 * On the other hand, for extremely large MSS devices, handling
  	 * smaller than MSS windows in this way does make sense.
  	 */
2631b79f6   Seymour, Shane M   tcp: increase siz...
613
  	if (tp->max_window > TCP_MSS_DEFAULT)
01f83d698   Alexey Kuznetsov   tcp: Prevent over...
614
615
616
617
618
619
  		cutoff = (tp->max_window >> 1);
  	else
  		cutoff = tp->max_window;
  
  	if (cutoff && pktsize > cutoff)
  		return max_t(int, cutoff, 68U - tp->tcp_header_len);
0c54b85f2   Ilpo Järvinen   tcp: simplify tcp...
620
621
622
  	else
  		return pktsize;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
623

17b085eac   Arnaldo Carvalho de Melo   [INET_DIAG]: Move...
624
  /* tcp.c */
0df48c26d   Eric Dumazet   tcp: add tcpi_byt...
625
  void tcp_get_info(struct sock *, struct tcp_info *);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
626
627
  
  /* Read 'sendfile()'-style from a TCP socket */
5c9f30236   Joe Perches   tcp.h: Remove ext...
628
629
  int tcp_read_sock(struct sock *sk, read_descriptor_t *desc,
  		  sk_read_actor_t recv_actor);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
630

5c9f30236   Joe Perches   tcp.h: Remove ext...
631
  void tcp_initialize_rcv_mss(struct sock *sk);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
632

5c9f30236   Joe Perches   tcp.h: Remove ext...
633
634
635
  int tcp_mtu_to_mss(struct sock *sk, int pmtu);
  int tcp_mss_to_mtu(struct sock *sk, int mss);
  void tcp_mtup_init(struct sock *sk);
5d424d5a6   John Heffner   [TCP]: MTU probing
636

f1ecd5d9e   Damian Lukowski   Revert Backoff [v...
637
638
639
640
641
642
643
644
  static inline void tcp_bound_rto(const struct sock *sk)
  {
  	if (inet_csk(sk)->icsk_rto > TCP_RTO_MAX)
  		inet_csk(sk)->icsk_rto = TCP_RTO_MAX;
  }
  
  static inline u32 __tcp_set_rto(const struct tcp_sock *tp)
  {
740b0f184   Eric Dumazet   tcp: switch rtt e...
645
  	return usecs_to_jiffies((tp->srtt_us >> 3) + tp->rttvar_us);
f1ecd5d9e   Damian Lukowski   Revert Backoff [v...
646
  }
31770e34e   Florian Westphal   tcp: Revert "tcp:...
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
  static inline void __tcp_fast_path_on(struct tcp_sock *tp, u32 snd_wnd)
  {
  	tp->pred_flags = htonl((tp->tcp_header_len << 26) |
  			       ntohl(TCP_FLAG_ACK) |
  			       snd_wnd);
  }
  
  static inline void tcp_fast_path_on(struct tcp_sock *tp)
  {
  	__tcp_fast_path_on(tp, tp->snd_wnd >> tp->rx_opt.snd_wscale);
  }
  
  static inline void tcp_fast_path_check(struct sock *sk)
  {
  	struct tcp_sock *tp = tcp_sk(sk);
  
  	if (RB_EMPTY_ROOT(&tp->out_of_order_queue) &&
  	    tp->rcv_wnd &&
  	    atomic_read(&sk->sk_rmem_alloc) < sk->sk_rcvbuf &&
  	    !tp->urg_data)
  		tcp_fast_path_on(tp);
  }
0c266898b   Satoru SATOH   tcp: Fix tcp_preq...
669
670
671
  /* Compute the actual rto_min value */
  static inline u32 tcp_rto_min(struct sock *sk)
  {
cf533ea53   Eric Dumazet   tcp: add const qu...
672
  	const struct dst_entry *dst = __sk_dst_get(sk);
ca584ba07   Martin KaFai Lau   tcp: bpf: Add TCP...
673
  	u32 rto_min = inet_csk(sk)->icsk_rto_min;
0c266898b   Satoru SATOH   tcp: Fix tcp_preq...
674
675
676
677
678
  
  	if (dst && dst_metric_locked(dst, RTAX_RTO_MIN))
  		rto_min = dst_metric_rtt(dst, RTAX_RTO_MIN);
  	return rto_min;
  }
740b0f184   Eric Dumazet   tcp: switch rtt e...
679
680
681
682
  static inline u32 tcp_rto_min_us(struct sock *sk)
  {
  	return jiffies_to_usecs(tcp_rto_min(sk));
  }
81164413a   Daniel Borkmann   net: tcp: add per...
683
684
685
686
  static inline bool tcp_ca_dst_locked(const struct dst_entry *dst)
  {
  	return dst_metric_locked(dst, RTAX_CC_ALGO);
  }
f67225839   Yuchung Cheng   tcp: track min RT...
687
688
689
  /* Minimum RTT in usec. ~0 means not available. */
  static inline u32 tcp_min_rtt(const struct tcp_sock *tp)
  {
640338921   Neal Cardwell   tcp: use windowed...
690
  	return minmax_get(&tp->rtt_min);
f67225839   Yuchung Cheng   tcp: track min RT...
691
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
692
693
694
695
  /* Compute the actual receive window we are currently advertising.
   * Rcv_nxt can be after the window if our peer push more data
   * than the offered window.
   */
40efc6fa1   Stephen Hemminger   [TCP]: less inline's
696
  static inline u32 tcp_receive_window(const struct tcp_sock *tp)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
697
698
699
700
701
702
703
704
705
706
707
708
  {
  	s32 win = tp->rcv_wup + tp->rcv_wnd - tp->rcv_nxt;
  
  	if (win < 0)
  		win = 0;
  	return (u32) win;
  }
  
  /* Choose a new window, without checks for shrinking, and without
   * scaling applied to the result.  The caller does these things
   * if necessary.  This is a "raw" window selection.
   */
5c9f30236   Joe Perches   tcp.h: Remove ext...
709
  u32 __tcp_select_window(struct sock *sk);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
710

ee9952831   Pavel Emelyanov   tcp: Initial repa...
711
  void tcp_send_window_probe(struct sock *sk);
ec66eda82   Eric Dumazet   tcp: introduce tc...
712
713
714
715
716
  /* TCP uses 32bit jiffies to save some space.
   * Note that this is different from tcp_time_stamp, which
   * historically has been the same until linux-4.13.
   */
  #define tcp_jiffies32 ((u32)jiffies)
9a568de48   Eric Dumazet   tcp: switch TCP T...
717
718
719
720
721
722
723
724
725
  /*
   * Deliver a 32bit value for TCP timestamp option (RFC 7323)
   * It is no longer tied to jiffies, but to 1 ms clock.
   * Note: double check if you want to use tcp_jiffies32 instead of this.
   */
  #define TCP_TS_HZ	1000
  
  static inline u64 tcp_clock_ns(void)
  {
fb420d5d9   Eric Dumazet   tcp/fq: move back...
726
  	return ktime_get_ns();
9a568de48   Eric Dumazet   tcp: switch TCP T...
727
728
729
730
731
732
733
734
735
736
737
738
  }
  
  static inline u64 tcp_clock_us(void)
  {
  	return div_u64(tcp_clock_ns(), NSEC_PER_USEC);
  }
  
  /* This should only be used in contexts where tp->tcp_mstamp is up to date */
  static inline u32 tcp_time_stamp(const struct tcp_sock *tp)
  {
  	return div_u64(tp->tcp_mstamp, USEC_PER_SEC / TCP_TS_HZ);
  }
200ecef67   Eric Dumazet   tcp: Remove one e...
739
740
741
742
743
  /* Convert a nsec timestamp into TCP TSval timestamp (ms based currently) */
  static inline u32 tcp_ns_to_ts(u64 ns)
  {
  	return div_u64(ns, NSEC_PER_SEC / TCP_TS_HZ);
  }
9a568de48   Eric Dumazet   tcp: switch TCP T...
744
745
746
  /* Could use tcp_clock_us() / 1000, but this version uses a single divide */
  static inline u32 tcp_time_stamp_raw(void)
  {
200ecef67   Eric Dumazet   tcp: Remove one e...
747
  	return tcp_ns_to_ts(tcp_clock_ns());
9a568de48   Eric Dumazet   tcp: switch TCP T...
748
  }
9799ccb0e   Eric Dumazet   tcp: add tcp_wsta...
749
  void tcp_mstamp_refresh(struct tcp_sock *tp);
9a568de48   Eric Dumazet   tcp: switch TCP T...
750
751
752
753
754
  
  static inline u32 tcp_stamp_us_delta(u64 t1, u64 t0)
  {
  	return max_t(s64, t1 - t0, 0);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
755

7faee5c0d   Eric Dumazet   tcp: remove TCP_S...
756
757
  static inline u32 tcp_skb_timestamp(const struct sk_buff *skb)
  {
200ecef67   Eric Dumazet   tcp: Remove one e...
758
  	return tcp_ns_to_ts(skb->skb_mstamp_ns);
7faee5c0d   Eric Dumazet   tcp: remove TCP_S...
759
  }
2fd66ffba   Eric Dumazet   tcp: introduce tc...
760
761
762
  /* provide the departure time in us unit */
  static inline u64 tcp_skb_timestamp_us(const struct sk_buff *skb)
  {
d3edd06ea   Eric Dumazet   tcp: provide earl...
763
  	return div_u64(skb->skb_mstamp_ns, NSEC_PER_USEC);
2fd66ffba   Eric Dumazet   tcp: introduce tc...
764
  }
7faee5c0d   Eric Dumazet   tcp: remove TCP_S...
765

a3433f35a   Changli Gao   tcp: unify tcp fl...
766
767
768
769
770
771
772
773
774
775
  #define tcp_flag_byte(th) (((u_int8_t *)th)[13])
  
  #define TCPHDR_FIN 0x01
  #define TCPHDR_SYN 0x02
  #define TCPHDR_RST 0x04
  #define TCPHDR_PSH 0x08
  #define TCPHDR_ACK 0x10
  #define TCPHDR_URG 0x20
  #define TCPHDR_ECE 0x40
  #define TCPHDR_CWR 0x80
492135557   Daniel Borkmann   tcp: add rfc3168,...
776
  #define TCPHDR_SYN_ECN	(TCPHDR_SYN | TCPHDR_ECE | TCPHDR_CWR)
caa20d9ab   Stephen Hemminger   [TCP]: spelling f...
777
  /* This is what the send packet queuing engine uses to pass
f86586fa4   Eric Dumazet   tcp: sizeof struc...
778
779
780
781
   * TCP per-packet control information to the transmission code.
   * We also store the host-order sequence numbers in here too.
   * This is 44 bytes if IPV6 is enabled.
   * If this grows please adjust skbuff.h:skbuff->cb[xxx] size appropriately.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
782
783
   */
  struct tcp_skb_cb {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
784
785
  	__u32		seq;		/* Starting sequence number	*/
  	__u32		end_seq;	/* SEQ + FIN + SYN + datalen	*/
cd7d8498c   Eric Dumazet   tcp: change tcp_s...
786
787
788
789
  	union {
  		/* Note : tcp_tw_isn is used in input path only
  		 *	  (isn chosen by tcp_timewait_state_process())
  		 *
f69ad292c   Eric Dumazet   tcp: fill shinfo-...
790
791
  		 * 	  tcp_gso_segs/size are used in write queue only,
  		 *	  cf tcp_skb_pcount()/tcp_skb_mss()
cd7d8498c   Eric Dumazet   tcp: change tcp_s...
792
793
  		 */
  		__u32		tcp_tw_isn;
f69ad292c   Eric Dumazet   tcp: fill shinfo-...
794
795
796
797
  		struct {
  			u16	tcp_gso_segs;
  			u16	tcp_gso_size;
  		};
cd7d8498c   Eric Dumazet   tcp: change tcp_s...
798
  	};
4de075e04   Eric Dumazet   tcp: rename tcp_s...
799
  	__u8		tcp_flags;	/* TCP header flags. (tcp[13])	*/
f4f9f6e75   Neal Cardwell   tcp: restore form...
800

713bafea9   Yuchung Cheng   tcp: retire FACK ...
801
  	__u8		sacked;		/* State flags for SACK.	*/
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
802
803
804
805
  #define TCPCB_SACKED_ACKED	0x01	/* SKB ACK'd by a SACK block	*/
  #define TCPCB_SACKED_RETRANS	0x02	/* SKB retransmitted		*/
  #define TCPCB_LOST		0x04	/* SKB is lost			*/
  #define TCPCB_TAGBITS		0x07	/* All tag bits			*/
d3edd06ea   Eric Dumazet   tcp: provide earl...
806
  #define TCPCB_REPAIRED		0x10	/* SKB repaired (no skb_mstamp_ns)	*/
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
807
  #define TCPCB_EVER_RETRANS	0x80	/* Ever retransmitted frame	*/
9d186cac7   Andrey Vagin   tcp: don't use ti...
808
809
  #define TCPCB_RETRANS		(TCPCB_SACKED_RETRANS|TCPCB_EVER_RETRANS| \
  				TCPCB_REPAIRED)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
810

f4f9f6e75   Neal Cardwell   tcp: restore form...
811
  	__u8		ip_dsfield;	/* IPv4 tos or IPv6 dsfield	*/
6b084928b   Soheil Hassas Yeganeh   tcp: use one bit ...
812
  	__u8		txstamp_ack:1,	/* Record TX timestamp for ack? */
c134ecb87   Martin KaFai Lau   tcp: Make use of ...
813
  			eor:1,		/* Is skb MSG_EOR marked? */
98aaa913b   Mike Maloney   tcp: Extend SOF_T...
814
815
  			has_rxtstamp:1,	/* SKB has a RX timestamp	*/
  			unused:5;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
816
  	__u32		ack_seq;	/* Sequence number ACK'd	*/
971f10eca   Eric Dumazet   tcp: better TCP_S...
817
  	union {
b75803d52   Lawrence Brakmo   tcp: refactor str...
818
  		struct {
b9f64820f   Yuchung Cheng   tcp: track data d...
819
  			/* There is space for up to 24 bytes */
d7722e857   Soheil Hassas Yeganeh   tcp: track applic...
820
821
822
  			__u32 in_flight:30,/* Bytes in flight at transmit */
  			      is_app_limited:1, /* cwnd not fully used? */
  			      unused:1;
b9f64820f   Yuchung Cheng   tcp: track data d...
823
824
825
  			/* pkts S/ACKed so far upon tx of skb, incl retrans: */
  			__u32 delivered;
  			/* start of send pipeline phase */
9a568de48   Eric Dumazet   tcp: switch TCP T...
826
  			u64 first_tx_mstamp;
b9f64820f   Yuchung Cheng   tcp: track data d...
827
  			/* when we reached the "delivered" count */
9a568de48   Eric Dumazet   tcp: switch TCP T...
828
  			u64 delivered_mstamp;
b75803d52   Lawrence Brakmo   tcp: refactor str...
829
830
831
  		} tx;   /* only used for outgoing skbs */
  		union {
  			struct inet_skb_parm	h4;
971f10eca   Eric Dumazet   tcp: better TCP_S...
832
  #if IS_ENABLED(CONFIG_IPV6)
b75803d52   Lawrence Brakmo   tcp: refactor str...
833
  			struct inet6_skb_parm	h6;
971f10eca   Eric Dumazet   tcp: better TCP_S...
834
  #endif
b75803d52   Lawrence Brakmo   tcp: refactor str...
835
  		} header;	/* For incoming skbs */
34f79502b   John Fastabend   bpf: avoid preemp...
836
  		struct {
34f79502b   John Fastabend   bpf: avoid preemp...
837
  			__u32 flags;
e5cd3abcb   John Fastabend   bpf: sockmap, ref...
838
  			struct sock *sk_redir;
8108a7751   John Fastabend   bpf: bpf_compute_...
839
  			void *data_end;
34f79502b   John Fastabend   bpf: avoid preemp...
840
  		} bpf;
b75803d52   Lawrence Brakmo   tcp: refactor str...
841
  	};
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
842
843
844
  };
  
  #define TCP_SKB_CB(__skb)	((struct tcp_skb_cb *)&((__skb)->cb[0]))
0ea488ff8   John Fastabend   bpf: sockmap, con...
845
846
847
848
  static inline void bpf_compute_data_end_sk_skb(struct sk_buff *skb)
  {
  	TCP_SKB_CB(skb)->bpf.data_end = skb->data + skb_headlen(skb);
  }
870c31513   Eric Dumazet   ipv6: introduce t...
849

604326b41   Daniel Borkmann   bpf, sockmap: con...
850
851
852
853
854
855
856
857
858
859
860
861
862
863
  static inline bool tcp_skb_bpf_ingress(const struct sk_buff *skb)
  {
  	return TCP_SKB_CB(skb)->bpf.flags & BPF_F_INGRESS;
  }
  
  static inline struct sock *tcp_skb_bpf_redirect_fetch(struct sk_buff *skb)
  {
  	return TCP_SKB_CB(skb)->bpf.sk_redir;
  }
  
  static inline void tcp_skb_bpf_redirect_clear(struct sk_buff *skb)
  {
  	TCP_SKB_CB(skb)->bpf.sk_redir = NULL;
  }
9b9e2f250   Eric Dumazet   tcp: move ipv4_sp...
864
  extern const struct inet_connection_sock_af_ops ipv4_specific;
815afe178   Eric Dumazet   tcp: fix build er...
865
  #if IS_ENABLED(CONFIG_IPV6)
870c31513   Eric Dumazet   ipv6: introduce t...
866
867
868
869
870
  /* This is the variant of inet6_iif() that must be used by TCP,
   * as TCP moves IP6CB into a different location in skb->cb[]
   */
  static inline int tcp_v6_iif(const struct sk_buff *skb)
  {
24b711edf   David Ahern   net/ipv6: Fix lin...
871
872
873
874
875
  	return TCP_SKB_CB(skb)->header.h6.iif;
  }
  
  static inline int tcp_v6_iif_l3_slave(const struct sk_buff *skb)
  {
a04a480d4   David Ahern   net: Require exac...
876
  	bool l3_slave = ipv6_l3mdev_skb(TCP_SKB_CB(skb)->header.h6.flags);
74b20582a   David Ahern   net: l3mdev: Add ...
877
878
  
  	return l3_slave ? skb->skb_iif : TCP_SKB_CB(skb)->header.h6.iif;
870c31513   Eric Dumazet   ipv6: introduce t...
879
  }
4297a0ef0   David Ahern   net: ipv6: add se...
880
881
882
883
884
885
886
887
888
889
  
  /* TCP_SKB_CB reference means this can not be used from early demux */
  static inline int tcp_v6_sdif(const struct sk_buff *skb)
  {
  #if IS_ENABLED(CONFIG_NET_L3_MASTER_DEV)
  	if (skb && ipv6_l3mdev_skb(TCP_SKB_CB(skb)->header.h6.flags))
  		return TCP_SKB_CB(skb)->header.h6.iif;
  #endif
  	return 0;
  }
dd2e0b86f   Eric Dumazet   tcp: remove indir...
890

b03d2142b   Eric Dumazet   tcp: move ipv6_sp...
891
  extern const struct inet_connection_sock_af_ops ipv6_specific;
dd2e0b86f   Eric Dumazet   tcp: remove indir...
892
  INDIRECT_CALLABLE_DECLARE(void tcp_v6_send_check(struct sock *sk, struct sk_buff *skb));
243600ee6   Eric Dumazet   tcp: add declarat...
893
894
  INDIRECT_CALLABLE_DECLARE(int tcp_v6_rcv(struct sk_buff *skb));
  INDIRECT_CALLABLE_DECLARE(void tcp_v6_early_demux(struct sk_buff *skb));
dd2e0b86f   Eric Dumazet   tcp: remove indir...
895

815afe178   Eric Dumazet   tcp: fix build er...
896
  #endif
870c31513   Eric Dumazet   ipv6: introduce t...
897

3fa6f616a   David Ahern   net: ipv4: add se...
898
899
900
901
902
903
904
905
906
  /* TCP_SKB_CB reference means this can not be used from early demux */
  static inline int tcp_v4_sdif(struct sk_buff *skb)
  {
  #if IS_ENABLED(CONFIG_NET_L3_MASTER_DEV)
  	if (skb && ipv4_l3mdev_skb(TCP_SKB_CB(skb)->header.h4.flags))
  		return TCP_SKB_CB(skb)->header.h4.iif;
  #endif
  	return 0;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
907
908
  /* Due to TSO, an SKB can be composed of multiple actual
   * packets.  To keep these tracked properly, we use this.
bd14b1b2e   Eric Dumazet   tcp: be more stri...
909
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
910
  static inline int tcp_skb_pcount(const struct sk_buff *skb)
bd14b1b2e   Eric Dumazet   tcp: be more stri...
911
  {
cd7d8498c   Eric Dumazet   tcp: change tcp_s...
912
913
  	return TCP_SKB_CB(skb)->tcp_gso_segs;
  }
bd14b1b2e   Eric Dumazet   tcp: be more stri...
914

cd7d8498c   Eric Dumazet   tcp: change tcp_s...
915
916
917
  static inline void tcp_skb_pcount_set(struct sk_buff *skb, int segs)
  {
  	TCP_SKB_CB(skb)->tcp_gso_segs = segs;
bd14b1b2e   Eric Dumazet   tcp: be more stri...
918
  }
cd7d8498c   Eric Dumazet   tcp: change tcp_s...
919
  static inline void tcp_skb_pcount_add(struct sk_buff *skb, int segs)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
920
  {
cd7d8498c   Eric Dumazet   tcp: change tcp_s...
921
  	TCP_SKB_CB(skb)->tcp_gso_segs += segs;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
922
  }
f69ad292c   Eric Dumazet   tcp: fill shinfo-...
923
  /* This is valid iff skb is in write queue and tcp_skb_pcount() > 1. */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
924
925
  static inline int tcp_skb_mss(const struct sk_buff *skb)
  {
f69ad292c   Eric Dumazet   tcp: fill shinfo-...
926
  	return TCP_SKB_CB(skb)->tcp_gso_size;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
927
  }
c134ecb87   Martin KaFai Lau   tcp: Make use of ...
928
929
930
931
  static inline bool tcp_skb_can_collapse_to(const struct sk_buff *skb)
  {
  	return likely(!TCP_SKB_CB(skb)->eor);
  }
857124841   Mat Martineau   tcp: coalesce/col...
932
933
934
935
936
937
  static inline bool tcp_skb_can_collapse(const struct sk_buff *to,
  					const struct sk_buff *from)
  {
  	return likely(tcp_skb_can_collapse_to(to) &&
  		      mptcp_skb_can_collapse(to, from));
  }
317a76f9a   Stephen Hemminger   [TCP]: Add plugga...
938
939
940
941
942
  /* Events passed to congestion control interface */
  enum tcp_ca_event {
  	CA_EVENT_TX_START,	/* first transmit when no packets in flight */
  	CA_EVENT_CWND_RESTART,	/* congestion window restart */
  	CA_EVENT_COMPLETE_CWR,	/* end of congestion recovery */
317a76f9a   Stephen Hemminger   [TCP]: Add plugga...
943
  	CA_EVENT_LOSS,		/* loss timeout */
9890092e4   Florian Westphal   net: tcp: more de...
944
945
  	CA_EVENT_ECN_NO_CE,	/* ECT set, but not CE marked */
  	CA_EVENT_ECN_IS_CE,	/* received CE marked IP packet */
7354c8c38   Florian Westphal   net: tcp: split a...
946
  };
9890092e4   Florian Westphal   net: tcp: more de...
947
  /* Information about inbound ACK, passed to cong_ops->in_ack_event() */
7354c8c38   Florian Westphal   net: tcp: split a...
948
  enum tcp_ca_ack_event_flags {
c1d2b4c3e   Florian Westphal   tcp: Revert "tcp:...
949
950
951
  	CA_ACK_SLOWPATH		= (1 << 0),	/* In slow path processing */
  	CA_ACK_WIN_UPDATE	= (1 << 1),	/* ACK updated window */
  	CA_ACK_ECE		= (1 << 2),	/* ECE bit is set on ack */
317a76f9a   Stephen Hemminger   [TCP]: Add plugga...
952
953
954
955
956
957
  };
  
  /*
   * Interface for adding new TCP congestion control handlers
   */
  #define TCP_CA_NAME_MAX	16
3ff825b28   Stephen Hemminger   [TCP]: Add tcp_av...
958
959
  #define TCP_CA_MAX	128
  #define TCP_CA_BUF_MAX	(TCP_CA_NAME_MAX*TCP_CA_MAX)
c5c6a8ab4   Daniel Borkmann   net: tcp: add key...
960
  #define TCP_CA_UNSPEC	0
30e502a34   Daniel Borkmann   net: tcp: add fla...
961
  /* Algorithm can be set on socket without CAP_NET_ADMIN privileges */
164891aad   Stephen Hemminger   [TCP]: Congestion...
962
  #define TCP_CONG_NON_RESTRICTED 0x1
30e502a34   Daniel Borkmann   net: tcp: add fla...
963
964
  /* Requires ECN/ECT set on all packets */
  #define TCP_CONG_NEEDS_ECN	0x2
0baf26b0f   Martin KaFai Lau   bpf: tcp: Support...
965
  #define TCP_CONG_MASK	(TCP_CONG_NON_RESTRICTED | TCP_CONG_NEEDS_ECN)
164891aad   Stephen Hemminger   [TCP]: Congestion...
966

64f40ff5b   Eric Dumazet   tcp: prepare CC g...
967
  union tcp_cc_info;
756ee1729   Lawrence Brakmo   tcp: replace cnt ...
968
969
970
  struct ack_sample {
  	u32 pkts_acked;
  	s32 rtt_us;
6f094b9ec   Lawrence Brakmo   tcp: add in_fligh...
971
  	u32 in_flight;
756ee1729   Lawrence Brakmo   tcp: replace cnt ...
972
  };
b9f64820f   Yuchung Cheng   tcp: track data d...
973
974
975
976
977
978
979
980
981
  /* A rate sample measures the number of (original/retransmitted) data
   * packets delivered "delivered" over an interval of time "interval_us".
   * The tcp_rate.c code fills in the rate sample, and congestion
   * control modules that define a cong_control function to run at the end
   * of ACK processing can optionally chose to consult this sample when
   * setting cwnd and pacing rate.
   * A sample is invalid if "delivered" or "interval_us" is negative.
   */
  struct rate_sample {
9a568de48   Eric Dumazet   tcp: switch TCP T...
982
  	u64  prior_mstamp; /* starting timestamp for interval */
b9f64820f   Yuchung Cheng   tcp: track data d...
983
984
985
  	u32  prior_delivered;	/* tp->delivered at "prior_mstamp" */
  	s32  delivered;		/* number of packets delivered over interval */
  	long interval_us;	/* time for tp->delivered to incr "delivered" */
4929c9428   Deepti Raghavan   tcp: expose both ...
986
987
  	u32 snd_interval_us;	/* snd interval for delivered packets */
  	u32 rcv_interval_us;	/* rcv interval for delivered packets */
b9f64820f   Yuchung Cheng   tcp: track data d...
988
989
990
991
  	long rtt_us;		/* RTT of last (S)ACKed packet (or -1) */
  	int  losses;		/* number of packets marked lost upon ACK */
  	u32  acked_sacked;	/* number of packets newly (S)ACKed upon ACK */
  	u32  prior_in_flight;	/* in flight before this ACK */
d7722e857   Soheil Hassas Yeganeh   tcp: track applic...
992
  	bool is_app_limited;	/* is sample from packet with bubble in pipe? */
b9f64820f   Yuchung Cheng   tcp: track data d...
993
  	bool is_retrans;	/* is sample from retransmission? */
e42866031   Yuchung Cheng   tcp: avoid min RT...
994
  	bool is_ack_delayed;	/* is this (likely) a delayed ACK? */
b9f64820f   Yuchung Cheng   tcp: track data d...
995
  };
317a76f9a   Stephen Hemminger   [TCP]: Add plugga...
996
997
  struct tcp_congestion_ops {
  	struct list_head	list;
c5c6a8ab4   Daniel Borkmann   net: tcp: add key...
998
999
  	u32 key;
  	u32 flags;
317a76f9a   Stephen Hemminger   [TCP]: Add plugga...
1000
1001
  
  	/* initialize private data (optional) */
6687e988d   Arnaldo Carvalho de Melo   [ICSK]: Move TCP ...
1002
  	void (*init)(struct sock *sk);
317a76f9a   Stephen Hemminger   [TCP]: Add plugga...
1003
  	/* cleanup private data  (optional) */
6687e988d   Arnaldo Carvalho de Melo   [ICSK]: Move TCP ...
1004
  	void (*release)(struct sock *sk);
317a76f9a   Stephen Hemminger   [TCP]: Add plugga...
1005
1006
  
  	/* return slow start threshold (required) */
6687e988d   Arnaldo Carvalho de Melo   [ICSK]: Move TCP ...
1007
  	u32 (*ssthresh)(struct sock *sk);
317a76f9a   Stephen Hemminger   [TCP]: Add plugga...
1008
  	/* do new cwnd calculation (required) */
249015515   Eric Dumazet   tcp: remove in_fl...
1009
  	void (*cong_avoid)(struct sock *sk, u32 ack, u32 acked);
317a76f9a   Stephen Hemminger   [TCP]: Add plugga...
1010
  	/* call before changing ca_state (optional) */
6687e988d   Arnaldo Carvalho de Melo   [ICSK]: Move TCP ...
1011
  	void (*set_state)(struct sock *sk, u8 new_state);
317a76f9a   Stephen Hemminger   [TCP]: Add plugga...
1012
  	/* call when cwnd event occurs (optional) */
6687e988d   Arnaldo Carvalho de Melo   [ICSK]: Move TCP ...
1013
  	void (*cwnd_event)(struct sock *sk, enum tcp_ca_event ev);
7354c8c38   Florian Westphal   net: tcp: split a...
1014
1015
  	/* call when ack arrives (optional) */
  	void (*in_ack_event)(struct sock *sk, u32 flags);
1e0ce2a1e   Anmol Sarma   net: Update TCP c...
1016
  	/* new value of cwnd after loss (required) */
6687e988d   Arnaldo Carvalho de Melo   [ICSK]: Move TCP ...
1017
  	u32  (*undo_cwnd)(struct sock *sk);
317a76f9a   Stephen Hemminger   [TCP]: Add plugga...
1018
  	/* hook for packet ack accounting (optional) */
756ee1729   Lawrence Brakmo   tcp: replace cnt ...
1019
  	void (*pkts_acked)(struct sock *sk, const struct ack_sample *sample);
dcb8c9b43   Eric Dumazet   tcp_bbr: better d...
1020
1021
  	/* override sysctl_tcp_min_tso_segs */
  	u32 (*min_tso_segs)(struct sock *sk);
77bfc174c   Yuchung Cheng   tcp: allow conges...
1022
1023
  	/* returns the multiplier used in tcp_sndbuf_expand (optional) */
  	u32 (*sndbuf_expand)(struct sock *sk);
c0402760f   Yuchung Cheng   tcp: new CC hook ...
1024
1025
1026
1027
  	/* call when packets are delivered to update cwnd and pacing rate,
  	 * after all the ca_state processing. (optional)
  	 */
  	void (*cong_control)(struct sock *sk, const struct rate_sample *rs);
73c1f4a03   Arnaldo Carvalho de Melo   [TCPDIAG]: Just r...
1028
  	/* get info for inet_diag (optional) */
64f40ff5b   Eric Dumazet   tcp: prepare CC g...
1029
1030
  	size_t (*get_info)(struct sock *sk, u32 ext, int *attr,
  			   union tcp_cc_info *info);
317a76f9a   Stephen Hemminger   [TCP]: Add plugga...
1031
1032
1033
1034
  
  	char 		name[TCP_CA_NAME_MAX];
  	struct module 	*owner;
  };
5c9f30236   Joe Perches   tcp.h: Remove ext...
1035
1036
  int tcp_register_congestion_control(struct tcp_congestion_ops *type);
  void tcp_unregister_congestion_control(struct tcp_congestion_ops *type);
317a76f9a   Stephen Hemminger   [TCP]: Add plugga...
1037

55d8694fa   Florian Westphal   net: tcp: assign ...
1038
  void tcp_assign_congestion_control(struct sock *sk);
5c9f30236   Joe Perches   tcp.h: Remove ext...
1039
1040
  void tcp_init_congestion_control(struct sock *sk);
  void tcp_cleanup_congestion_control(struct sock *sk);
6670e1524   Stephen Hemminger   tcp: Namespace-if...
1041
1042
  int tcp_set_default_congestion_control(struct net *net, const char *name);
  void tcp_get_default_congestion_control(struct net *net, char *name);
5c9f30236   Joe Perches   tcp.h: Remove ext...
1043
1044
1045
  void tcp_get_available_congestion_control(char *buf, size_t len);
  void tcp_get_allowed_congestion_control(char *buf, size_t len);
  int tcp_set_allowed_congestion_control(char *allowed);
8d650cded   Eric Dumazet   tcp: fix tcp_set_...
1046
  int tcp_set_congestion_control(struct sock *sk, const char *name, bool load,
29a949325   Neal Cardwell   tcp: simplify tcp...
1047
  			       bool cap_net_admin);
e73ebb088   Neal Cardwell   tcp: stretch ACK ...
1048
1049
  u32 tcp_slow_start(struct tcp_sock *tp, u32 acked);
  void tcp_cong_avoid_ai(struct tcp_sock *tp, u32 w, u32 acked);
317a76f9a   Stephen Hemminger   [TCP]: Add plugga...
1050

5c9f30236   Joe Perches   tcp.h: Remove ext...
1051
  u32 tcp_reno_ssthresh(struct sock *sk);
e97991832   Florian Westphal   tcp: make undo_cw...
1052
  u32 tcp_reno_undo_cwnd(struct sock *sk);
249015515   Eric Dumazet   tcp: remove in_fl...
1053
  void tcp_reno_cong_avoid(struct sock *sk, u32 ack, u32 acked);
a8acfbac7   David S. Miller   [TCP]: Need to de...
1054
  extern struct tcp_congestion_ops tcp_reno;
317a76f9a   Stephen Hemminger   [TCP]: Add plugga...
1055

0baf26b0f   Martin KaFai Lau   bpf: tcp: Support...
1056
  struct tcp_congestion_ops *tcp_ca_find(const char *name);
c5c6a8ab4   Daniel Borkmann   net: tcp: add key...
1057
  struct tcp_congestion_ops *tcp_ca_find_key(u32 key);
6670e1524   Stephen Hemminger   tcp: Namespace-if...
1058
  u32 tcp_ca_get_key_by_name(struct net *net, const char *name, bool *ecn_ca);
ea6976399   Daniel Borkmann   net: tcp: add RTA...
1059
  #ifdef CONFIG_INET
c5c6a8ab4   Daniel Borkmann   net: tcp: add key...
1060
  char *tcp_ca_get_name_by_key(u32 key, char *buffer);
ea6976399   Daniel Borkmann   net: tcp: add RTA...
1061
1062
1063
1064
1065
1066
  #else
  static inline char *tcp_ca_get_name_by_key(u32 key, char *buffer)
  {
  	return NULL;
  }
  #endif
c5c6a8ab4   Daniel Borkmann   net: tcp: add key...
1067

30e502a34   Daniel Borkmann   net: tcp: add fla...
1068
1069
1070
1071
1072
1073
  static inline bool tcp_ca_needs_ecn(const struct sock *sk)
  {
  	const struct inet_connection_sock *icsk = inet_csk(sk);
  
  	return icsk->icsk_ca_ops->flags & TCP_CONG_NEEDS_ECN;
  }
6687e988d   Arnaldo Carvalho de Melo   [ICSK]: Move TCP ...
1074
  static inline void tcp_set_ca_state(struct sock *sk, const u8 ca_state)
317a76f9a   Stephen Hemminger   [TCP]: Add plugga...
1075
  {
6687e988d   Arnaldo Carvalho de Melo   [ICSK]: Move TCP ...
1076
1077
1078
1079
1080
  	struct inet_connection_sock *icsk = inet_csk(sk);
  
  	if (icsk->icsk_ca_ops->set_state)
  		icsk->icsk_ca_ops->set_state(sk, ca_state);
  	icsk->icsk_ca_state = ca_state;
317a76f9a   Stephen Hemminger   [TCP]: Add plugga...
1081
  }
6687e988d   Arnaldo Carvalho de Melo   [ICSK]: Move TCP ...
1082
  static inline void tcp_ca_event(struct sock *sk, const enum tcp_ca_event event)
317a76f9a   Stephen Hemminger   [TCP]: Add plugga...
1083
  {
6687e988d   Arnaldo Carvalho de Melo   [ICSK]: Move TCP ...
1084
1085
1086
1087
  	const struct inet_connection_sock *icsk = inet_csk(sk);
  
  	if (icsk->icsk_ca_ops->cwnd_event)
  		icsk->icsk_ca_ops->cwnd_event(sk, event);
317a76f9a   Stephen Hemminger   [TCP]: Add plugga...
1088
  }
b9f64820f   Yuchung Cheng   tcp: track data d...
1089
1090
1091
1092
1093
  /* From tcp_rate.c */
  void tcp_rate_skb_sent(struct sock *sk, struct sk_buff *skb);
  void tcp_rate_skb_delivered(struct sock *sk, struct sk_buff *skb,
  			    struct rate_sample *rs);
  void tcp_rate_gen(struct sock *sk, u32 delivered, u32 lost,
d4761754b   Yousuk Seung   tcp: invalidate r...
1094
  		  bool is_sack_reneg, struct rate_sample *rs);
d7722e857   Soheil Hassas Yeganeh   tcp: track applic...
1095
  void tcp_rate_check_app_limited(struct sock *sk);
b9f64820f   Yuchung Cheng   tcp: track data d...
1096

e60402d0a   Ilpo Järvinen   [TCP]: Move sack_...
1097
1098
1099
1100
1101
1102
  /* These functions determine how the current flow behaves in respect of SACK
   * handling. SACK is negotiated with the peer, and therefore it can vary
   * between different flows.
   *
   * tcp_is_sack - SACK enabled
   * tcp_is_reno - No SACK
e60402d0a   Ilpo Järvinen   [TCP]: Move sack_...
1103
1104
1105
   */
  static inline int tcp_is_sack(const struct tcp_sock *tp)
  {
ebeef4bcc   Eric Dumazet   tcp: hint compile...
1106
  	return likely(tp->rx_opt.sack_ok);
e60402d0a   Ilpo Järvinen   [TCP]: Move sack_...
1107
  }
a2a385d62   Eric Dumazet   tcp: bool convers...
1108
  static inline bool tcp_is_reno(const struct tcp_sock *tp)
e60402d0a   Ilpo Järvinen   [TCP]: Move sack_...
1109
1110
1111
  {
  	return !tcp_is_sack(tp);
  }
83ae40885   Ilpo Järvinen   [TCP]: Add tcp_le...
1112
1113
1114
1115
  static inline unsigned int tcp_left_out(const struct tcp_sock *tp)
  {
  	return tp->sacked_out + tp->lost_out;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
  /* This determines how many packets are "in the network" to the best
   * of our knowledge.  In many cases it is conservative, but where
   * detailed information is available from the receiver (via SACK
   * blocks etc.) we can make more aggressive calculations.
   *
   * Use this for decisions involving congestion control, use just
   * tp->packets_out to determine if the send queue is empty or not.
   *
   * Read this equation as:
   *
   *	"Packets sent once on transmission queue" MINUS
   *	"Packets left network, but not honestly ACKed yet" PLUS
   *	"Packets fast retransmitted"
   */
40efc6fa1   Stephen Hemminger   [TCP]: less inline's
1130
  static inline unsigned int tcp_packets_in_flight(const struct tcp_sock *tp)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1131
  {
83ae40885   Ilpo Järvinen   [TCP]: Add tcp_le...
1132
  	return tp->packets_out - tcp_left_out(tp) + tp->retrans_out;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1133
  }
0b6a05c1d   Ilpo Järvinen   tcp: fix ssthresh...
1134
  #define TCP_INFINITE_SSTHRESH	0x7fffffff
071d5080e   Yuchung Cheng   tcp: add tcp_in_s...
1135
1136
  static inline bool tcp_in_slow_start(const struct tcp_sock *tp)
  {
76174004a   Yuchung Cheng   tcp: do not slow ...
1137
  	return tp->snd_cwnd < tp->snd_ssthresh;
071d5080e   Yuchung Cheng   tcp: add tcp_in_s...
1138
  }
0b6a05c1d   Ilpo Järvinen   tcp: fix ssthresh...
1139
1140
1141
1142
  static inline bool tcp_in_initial_slowstart(const struct tcp_sock *tp)
  {
  	return tp->snd_ssthresh >= TCP_INFINITE_SSTHRESH;
  }
684bad110   Yuchung Cheng   tcp: use PRR to r...
1143
1144
1145
1146
1147
  static inline bool tcp_in_cwnd_reduction(const struct sock *sk)
  {
  	return (TCPF_CA_CWR | TCPF_CA_Recovery) &
  	       (1 << inet_csk(sk)->icsk_ca_state);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1148
  /* If cwnd > ssthresh, we may raise ssthresh to be half-way to cwnd.
684bad110   Yuchung Cheng   tcp: use PRR to r...
1149
   * The exception is cwnd reduction phase, when cwnd is decreasing towards
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1150
1151
   * ssthresh.
   */
6687e988d   Arnaldo Carvalho de Melo   [ICSK]: Move TCP ...
1152
  static inline __u32 tcp_current_ssthresh(const struct sock *sk)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1153
  {
6687e988d   Arnaldo Carvalho de Melo   [ICSK]: Move TCP ...
1154
  	const struct tcp_sock *tp = tcp_sk(sk);
cf533ea53   Eric Dumazet   tcp: add const qu...
1155

684bad110   Yuchung Cheng   tcp: use PRR to r...
1156
  	if (tcp_in_cwnd_reduction(sk))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1157
1158
1159
1160
1161
1162
  		return tp->snd_ssthresh;
  	else
  		return max(tp->snd_ssthresh,
  			   ((tp->snd_cwnd >> 1) +
  			    (tp->snd_cwnd >> 2)));
  }
b9c4595bc   Ilpo Järvinen   [TCP]: Don't pani...
1163
1164
  /* Use define here intentionally to get WARN_ON location shown at the caller */
  #define tcp_verify_left_out(tp)	WARN_ON(tcp_left_out(tp) > tp->packets_out)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1165

5ee2c941b   Christoph Paasch   tcp: Remove unnec...
1166
  void tcp_enter_cwr(struct sock *sk);
5c9f30236   Joe Perches   tcp.h: Remove ext...
1167
  __u32 tcp_init_cwnd(const struct tcp_sock *tp, const struct dst_entry *dst);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1168

6b5a5c0db   Neal Cardwell   tcp: do not scale...
1169
1170
1171
1172
1173
1174
1175
  /* The maximum number of MSS of available cwnd for which TSO defers
   * sending if not using sysctl_tcp_tso_win_divisor.
   */
  static inline __u32 tcp_max_tso_deferred_mss(const struct tcp_sock *tp)
  {
  	return 3;
  }
90840defa   Ilpo Järvinen   [TCP]: Introduce ...
1176
1177
1178
1179
1180
  /* Returns end sequence number of the receiver's advertised window */
  static inline u32 tcp_wnd_end(const struct tcp_sock *tp)
  {
  	return tp->snd_una + tp->snd_wnd;
  }
e114a710a   Eric Dumazet   tcp: fix cwnd lim...
1181
1182
1183
  
  /* We follow the spirit of RFC2861 to validate cwnd but implement a more
   * flexible approach. The RFC suggests cwnd should not be raised unless
ca8a22634   Neal Cardwell   tcp: make cwnd-li...
1184
1185
1186
   * it was fully used previously. And that's exactly what we do in
   * congestion avoidance mode. But in slow start we allow cwnd to grow
   * as long as the application has used half the cwnd.
e114a710a   Eric Dumazet   tcp: fix cwnd lim...
1187
1188
1189
1190
1191
1192
1193
   * Example :
   *    cwnd is 10 (IW10), but application sends 9 frames.
   *    We allow cwnd to reach 18 when all frames are ACKed.
   * This check is safe because it's as aggressive as slow start which already
   * risks 100% overshoot. The advantage is that we discourage application to
   * either send more filler packets or data to artificially blow up the cwnd
   * usage, and allow application-limited process to probe bw more aggressively.
e114a710a   Eric Dumazet   tcp: fix cwnd lim...
1194
   */
249015515   Eric Dumazet   tcp: remove in_fl...
1195
  static inline bool tcp_is_cwnd_limited(const struct sock *sk)
e114a710a   Eric Dumazet   tcp: fix cwnd lim...
1196
1197
  {
  	const struct tcp_sock *tp = tcp_sk(sk);
ca8a22634   Neal Cardwell   tcp: make cwnd-li...
1198
  	/* If in slow start, ensure cwnd grows to twice what was ACKed. */
071d5080e   Yuchung Cheng   tcp: add tcp_in_s...
1199
  	if (tcp_in_slow_start(tp))
ca8a22634   Neal Cardwell   tcp: make cwnd-li...
1200
1201
1202
  		return tp->snd_cwnd < 2 * tp->max_packets_out;
  
  	return tp->is_cwnd_limited;
e114a710a   Eric Dumazet   tcp: fix cwnd lim...
1203
  }
f4805eded   Stephen Hemminger   [TCP]: fix conges...
1204

cadefe5f5   Eric Dumazet   tcp_bbr: fix bbr ...
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
  /* BBR congestion control needs pacing.
   * Same remark for SO_MAX_PACING_RATE.
   * sch_fq packet scheduler is efficiently handling pacing,
   * but is not always installed/used.
   * Return true if TCP stack should pace packets itself.
   */
  static inline bool tcp_needs_internal_pacing(const struct sock *sk)
  {
  	return smp_load_acquire(&sk->sk_pacing_status) == SK_PACING_NEEDED;
  }
8dc242ad6   Eric Dumazet   tcp: refine tcp_p...
1215
1216
  /* Estimates in how many jiffies next packet for this flow can be sent.
   * Scheduling a retransmit timer too early would be silly.
3f80e08f4   Eric Dumazet   tcp: add tcp_rese...
1217
   */
8dc242ad6   Eric Dumazet   tcp: refine tcp_p...
1218
  static inline unsigned long tcp_pacing_delay(const struct sock *sk)
3f80e08f4   Eric Dumazet   tcp: add tcp_rese...
1219
  {
8dc242ad6   Eric Dumazet   tcp: refine tcp_p...
1220
  	s64 delay = tcp_sk(sk)->tcp_wstamp_ns - tcp_sk(sk)->tcp_clock_cache;
3f80e08f4   Eric Dumazet   tcp: add tcp_rese...
1221

8dc242ad6   Eric Dumazet   tcp: refine tcp_p...
1222
  	return delay > 0 ? nsecs_to_jiffies(delay) : 0;
3f80e08f4   Eric Dumazet   tcp: add tcp_rese...
1223
1224
1225
1226
1227
  }
  
  static inline void tcp_reset_xmit_timer(struct sock *sk,
  					const int what,
  					unsigned long when,
8dc242ad6   Eric Dumazet   tcp: refine tcp_p...
1228
  					const unsigned long max_when)
3f80e08f4   Eric Dumazet   tcp: add tcp_rese...
1229
  {
8dc242ad6   Eric Dumazet   tcp: refine tcp_p...
1230
  	inet_csk_reset_xmit_timer(sk, what, when + tcp_pacing_delay(sk),
3f80e08f4   Eric Dumazet   tcp: add tcp_rese...
1231
1232
  				  max_when);
  }
21c8fe991   Eric Dumazet   tcp: adjust windo...
1233
  /* Something is really bad, we could not queue an additional packet,
3f80e08f4   Eric Dumazet   tcp: add tcp_rese...
1234
   * because qdisc is full or receiver sent a 0 window, or we are paced.
21c8fe991   Eric Dumazet   tcp: adjust windo...
1235
1236
1237
1238
1239
   * We do not want to add fuel to the fire, or abort too early,
   * so make sure the timer we arm now is at least 200ms in the future,
   * regardless of current icsk_rto value (as it could be ~2ms)
   */
  static inline unsigned long tcp_probe0_base(const struct sock *sk)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1240
  {
21c8fe991   Eric Dumazet   tcp: adjust windo...
1241
1242
  	return max_t(unsigned long, inet_csk(sk)->icsk_rto, TCP_RTO_MIN);
  }
9e412ba76   Ilpo Järvinen   [TCP]: Sed magic ...
1243

21c8fe991   Eric Dumazet   tcp: adjust windo...
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
  /* Variant of inet_csk_rto_backoff() used for zero window probes */
  static inline unsigned long tcp_probe0_when(const struct sock *sk,
  					    unsigned long max_when)
  {
  	u64 when = (u64)tcp_probe0_base(sk) << inet_csk(sk)->icsk_backoff;
  
  	return (unsigned long)min_t(u64, when, max_when);
  }
  
  static inline void tcp_check_probe_timer(struct sock *sk)
  {
  	if (!tcp_sk(sk)->packets_out && !inet_csk(sk)->icsk_pending)
3f80e08f4   Eric Dumazet   tcp: add tcp_rese...
1256
  		tcp_reset_xmit_timer(sk, ICSK_TIME_PROBE0,
8dc242ad6   Eric Dumazet   tcp: refine tcp_p...
1257
  				     tcp_probe0_base(sk), TCP_RTO_MAX);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1258
  }
ee7537b63   Hantzis Fotis   tcp: tcp_init_wl ...
1259
  static inline void tcp_init_wl(struct tcp_sock *tp, u32 seq)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1260
1261
1262
  {
  	tp->snd_wl1 = seq;
  }
ee7537b63   Hantzis Fotis   tcp: tcp_init_wl ...
1263
  static inline void tcp_update_wl(struct tcp_sock *tp, u32 seq)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1264
1265
1266
  {
  	tp->snd_wl1 = seq;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1267
1268
1269
  /*
   * Calculate(/check) TCP checksum
   */
ba7808eac   Frederik Deweerdt   [TCP]: remove tcp...
1270
1271
  static inline __sum16 tcp_v4_check(int len, __be32 saddr,
  				   __be32 daddr, __wsum base)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1272
  {
0b13c9bb9   Daniel T. Lee   include/net/tcp.h...
1273
  	return csum_tcpudp_magic(saddr, daddr, len, IPPROTO_TCP, base);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1274
  }
a2a385d62   Eric Dumazet   tcp: bool convers...
1275
  static inline bool tcp_checksum_complete(struct sk_buff *skb)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1276
  {
604763722   Herbert Xu   [NET]: Treat CHEC...
1277
  	return !skb_csum_unnecessary(skb) &&
6ab6dfa6b   Cong Wang   net: get rid of _...
1278
  		__skb_checksum_complete(skb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1279
  }
c9c332125   Eric Dumazet   tcp: add tcp_add_...
1280
  bool tcp_add_backlog(struct sock *sk, struct sk_buff *skb);
ac6e78007   Eric Dumazet   tcp: take care of...
1281
  int tcp_filter(struct sock *sk, struct sk_buff *skb);
5c9f30236   Joe Perches   tcp.h: Remove ext...
1282
  void tcp_set_state(struct sock *sk, int state);
5c9f30236   Joe Perches   tcp.h: Remove ext...
1283
  void tcp_done(struct sock *sk);
c1e64e298   Lorenzo Colitti   net: diag: Suppor...
1284
  int tcp_abort(struct sock *sk, int err);
40efc6fa1   Stephen Hemminger   [TCP]: less inline's
1285
  static inline void tcp_sack_reset(struct tcp_options_received *rx_opt)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1286
1287
  {
  	rx_opt->dsack = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1288
1289
  	rx_opt->num_sacks = 0;
  }
6f021c62d   Eric Dumazet   tcp: fix slow sta...
1290
1291
1292
1293
  void tcp_cwnd_restart(struct sock *sk, s32 delta);
  
  static inline void tcp_slow_start_after_idle_check(struct sock *sk)
  {
1b1fc3fdd   Wei Wang   tcp: make congest...
1294
  	const struct tcp_congestion_ops *ca_ops = inet_csk(sk)->icsk_ca_ops;
6f021c62d   Eric Dumazet   tcp: fix slow sta...
1295
1296
  	struct tcp_sock *tp = tcp_sk(sk);
  	s32 delta;
b510f0d23   Eric Dumazet   tcp: Namespace-if...
1297
  	if (!sock_net(sk)->ipv4.sysctl_tcp_slow_start_after_idle || tp->packets_out ||
1b1fc3fdd   Wei Wang   tcp: make congest...
1298
  	    ca_ops->cong_control)
6f021c62d   Eric Dumazet   tcp: fix slow sta...
1299
  		return;
d635fbe27   Eric Dumazet   tcp: use tcp_jiff...
1300
  	delta = tcp_jiffies32 - tp->lsndtime;
6f021c62d   Eric Dumazet   tcp: fix slow sta...
1301
1302
1303
  	if (delta > inet_csk(sk)->icsk_rto)
  		tcp_cwnd_restart(sk, delta);
  }
85f16525a   Yuchung Cheng   tcp: properly sen...
1304

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1305
  /* Determine a window scaling and initial window to offer. */
ceef9ab6b   Eric Dumazet   tcp: Namespace-if...
1306
1307
  void tcp_select_initial_window(const struct sock *sk, int __space,
  			       __u32 mss, __u32 *rcv_wnd,
5c9f30236   Joe Perches   tcp.h: Remove ext...
1308
1309
  			       __u32 *window_clamp, int wscale_ok,
  			       __u8 *rcv_wscale, __u32 init_rcv_wnd);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1310

94f0893e0   Eric Dumazet   tcp: Namespace-if...
1311
  static inline int tcp_win_from_space(const struct sock *sk, int space)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1312
  {
94f0893e0   Eric Dumazet   tcp: Namespace-if...
1313
  	int tcp_adv_win_scale = sock_net(sk)->ipv4.sysctl_tcp_adv_win_scale;
c48367427   Gao Feng   tcp: sysctl: Fix ...
1314
1315
1316
1317
  
  	return tcp_adv_win_scale <= 0 ?
  		(space>>(-tcp_adv_win_scale)) :
  		space - (space>>tcp_adv_win_scale);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1318
  }
105970f60   Kenjiro Nakayama   net: Remove trail...
1319
  /* Note: caller must be prepared to deal with negative returns */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1320
1321
  static inline int tcp_space(const struct sock *sk)
  {
ebb3b78db   Eric Dumazet   tcp: annotate sk-...
1322
  	return tcp_win_from_space(sk, READ_ONCE(sk->sk_rcvbuf) -
70c265584   Eric Dumazet   net: silence KCSA...
1323
  				  READ_ONCE(sk->sk_backlog.len) -
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1324
  				  atomic_read(&sk->sk_rmem_alloc));
105970f60   Kenjiro Nakayama   net: Remove trail...
1325
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1326
1327
1328
  
  static inline int tcp_full_space(const struct sock *sk)
  {
ebb3b78db   Eric Dumazet   tcp: annotate sk-...
1329
  	return tcp_win_from_space(sk, READ_ONCE(sk->sk_rcvbuf));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1330
  }
c76c69565   Paolo Abeni   mptcp: call tcp_c...
1331
  void tcp_cleanup_rbuf(struct sock *sk, int copied);
24adbc167   Eric Dumazet   tcp: fix SO_RCVLO...
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
  /* We provision sk_rcvbuf around 200% of sk_rcvlowat.
   * If 87.5 % (7/8) of the space has been consumed, we want to override
   * SO_RCVLOWAT constraint, since we are receiving skbs with too small
   * len/truesize ratio.
   */
  static inline bool tcp_rmem_pressure(const struct sock *sk)
  {
  	int rcvbuf = READ_ONCE(sk->sk_rcvbuf);
  	int threshold = rcvbuf - (rcvbuf >> 3);
  
  	return atomic_read(&sk->sk_rmem_alloc) > threshold;
  }
843f4a55e   Yuchung Cheng   tcp: use tcp_v4_s...
1344
  extern void tcp_openreq_init_rwin(struct request_sock *req,
b1964b5fc   Eric Dumazet   tcp: constify tcp...
1345
1346
  				  const struct sock *sk_listener,
  				  const struct dst_entry *dst);
843f4a55e   Yuchung Cheng   tcp: use tcp_v4_s...
1347

5c9f30236   Joe Perches   tcp.h: Remove ext...
1348
  void tcp_enter_memory_pressure(struct sock *sk);
060447511   Eric Dumazet   tcp: add TCPMemor...
1349
  void tcp_leave_memory_pressure(struct sock *sk);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1350

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1351
1352
  static inline int keepalive_intvl_when(const struct tcp_sock *tp)
  {
b840d15d3   Nikolay Borisov   ipv4: Namespecify...
1353
1354
1355
  	struct net *net = sock_net((struct sock *)tp);
  
  	return tp->keepalive_intvl ? : net->ipv4.sysctl_tcp_keepalive_intvl;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1356
1357
1358
1359
  }
  
  static inline int keepalive_time_when(const struct tcp_sock *tp)
  {
13b287e8d   Nikolay Borisov   ipv4: Namespaceif...
1360
1361
1362
  	struct net *net = sock_net((struct sock *)tp);
  
  	return tp->keepalive_time ? : net->ipv4.sysctl_tcp_keepalive_time;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1363
  }
df19a6267   Eric Dumazet   tcp: keepalive cl...
1364
1365
  static inline int keepalive_probes(const struct tcp_sock *tp)
  {
9bd6861bd   Nikolay Borisov   ipv4: Namespecify...
1366
1367
1368
  	struct net *net = sock_net((struct sock *)tp);
  
  	return tp->keepalive_probes ? : net->ipv4.sysctl_tcp_keepalive_probes;
df19a6267   Eric Dumazet   tcp: keepalive cl...
1369
  }
6c37e5de4   Flavio Leitner   TCP: avoid to sen...
1370
1371
1372
  static inline u32 keepalive_time_elapsed(const struct tcp_sock *tp)
  {
  	const struct inet_connection_sock *icsk = &tp->inet_conn;
70eabf0e1   Eric Dumazet   tcp: use tcp_jiff...
1373
1374
  	return min_t(u32, tcp_jiffies32 - icsk->icsk_ack.lrcvtime,
  			  tcp_jiffies32 - tp->rcv_tstamp);
6c37e5de4   Flavio Leitner   TCP: avoid to sen...
1375
  }
463c84b97   Arnaldo Carvalho de Melo   [NET]: Introduce ...
1376
  static inline int tcp_fin_time(const struct sock *sk)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1377
  {
1e579caa1   Nikolay Borisov   ipv4: Namespaceif...
1378
  	int fin_timeout = tcp_sk(sk)->linger2 ? : sock_net(sk)->ipv4.sysctl_tcp_fin_timeout;
463c84b97   Arnaldo Carvalho de Melo   [NET]: Introduce ...
1379
  	const int rto = inet_csk(sk)->icsk_rto;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1380

463c84b97   Arnaldo Carvalho de Melo   [NET]: Introduce ...
1381
1382
  	if (fin_timeout < (rto << 2) - (rto >> 1))
  		fin_timeout = (rto << 2) - (rto >> 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1383
1384
1385
  
  	return fin_timeout;
  }
a2a385d62   Eric Dumazet   tcp: bool convers...
1386
1387
  static inline bool tcp_paws_check(const struct tcp_options_received *rx_opt,
  				  int paws_win)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1388
  {
c887e6d2d   Ilpo Järvinen   tcp: consolidate ...
1389
  	if ((s32)(rx_opt->ts_recent - rx_opt->rcv_tsval) <= paws_win)
a2a385d62   Eric Dumazet   tcp: bool convers...
1390
  		return true;
cca9bab1b   Arnd Bergmann   tcp: use monotoni...
1391
1392
  	if (unlikely(!time_before32(ktime_get_seconds(),
  				    rx_opt->ts_recent_stamp + TCP_PAWS_24DAYS)))
a2a385d62   Eric Dumazet   tcp: bool convers...
1393
  		return true;
bc2ce894e   Eric Dumazet   tcp: relax tcp_pa...
1394
1395
1396
1397
1398
1399
  	/*
  	 * Some OSes send SYN and SYNACK messages with tsval=0 tsecr=0,
  	 * then following tcp messages have valid values. Ignore 0 value,
  	 * or else 'negative' tsval might forbid us to accept their packets.
  	 */
  	if (!rx_opt->ts_recent)
a2a385d62   Eric Dumazet   tcp: bool convers...
1400
1401
  		return true;
  	return false;
c887e6d2d   Ilpo Järvinen   tcp: consolidate ...
1402
  }
a2a385d62   Eric Dumazet   tcp: bool convers...
1403
1404
  static inline bool tcp_paws_reject(const struct tcp_options_received *rx_opt,
  				   int rst)
c887e6d2d   Ilpo Järvinen   tcp: consolidate ...
1405
1406
  {
  	if (tcp_paws_check(rx_opt, 0))
a2a385d62   Eric Dumazet   tcp: bool convers...
1407
  		return false;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
  
  	/* RST segments are not recommended to carry timestamp,
  	   and, if they do, it is recommended to ignore PAWS because
  	   "their cleanup function should take precedence over timestamps."
  	   Certainly, it is mistake. It is necessary to understand the reasons
  	   of this constraint to relax it: if peer reboots, clock may go
  	   out-of-sync and half-open connections will not be reset.
  	   Actually, the problem would be not existing if all
  	   the implementations followed draft about maintaining clock
  	   via reboots. Linux-2.2 DOES NOT!
  
  	   However, we can relax time bounds for RST segments to MSL.
  	 */
cca9bab1b   Arnd Bergmann   tcp: use monotoni...
1421
1422
  	if (rst && !time_before32(ktime_get_seconds(),
  				  rx_opt->ts_recent_stamp + TCP_PAWS_MSL))
a2a385d62   Eric Dumazet   tcp: bool convers...
1423
1424
  		return false;
  	return true;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1425
  }
7970ddc8f   Eric Dumazet   tcp: uninline tcp...
1426
1427
  bool tcp_oow_rate_limited(struct net *net, const struct sk_buff *skb,
  			  int mib_idx, u32 *last_oow_ack_time);
032ee4236   Neal Cardwell   tcp: helpers to m...
1428

a9c19329e   Pavel Emelyanov   tcp: add net to t...
1429
  static inline void tcp_mib_init(struct net *net)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1430
1431
  {
  	/* See RFC 2012 */
6aef70a85   Eric Dumazet   net: snmp: kill v...
1432
1433
1434
1435
  	TCP_ADD_STATS(net, TCP_MIB_RTOALGORITHM, 1);
  	TCP_ADD_STATS(net, TCP_MIB_RTOMIN, TCP_RTO_MIN*1000/HZ);
  	TCP_ADD_STATS(net, TCP_MIB_RTOMAX, TCP_RTO_MAX*1000/HZ);
  	TCP_ADD_STATS(net, TCP_MIB_MAXCONN, -1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1436
  }
5af4ec236   Ilpo Järvinen   [TCP]: clear_all_...
1437
  /* from STCP */
ef9da47c7   Ilpo Järvinen   tcp: don't clear ...
1438
  static inline void tcp_clear_retrans_hints_partial(struct tcp_sock *tp)
0800f1702   David S. Miller   [TCP]: Minor codi...
1439
  {
6a438bbe6   Stephen Hemminger   [TCP]: speed up S...
1440
  	tp->lost_skb_hint = NULL;
ef9da47c7   Ilpo Järvinen   tcp: don't clear ...
1441
1442
1443
1444
1445
  }
  
  static inline void tcp_clear_all_retrans_hints(struct tcp_sock *tp)
  {
  	tcp_clear_retrans_hints_partial(tp);
6a438bbe6   Stephen Hemminger   [TCP]: speed up S...
1446
  	tp->retransmit_skb_hint = NULL;
b76892051   Ilpo Järvinen   [TCP]: Avoid clea...
1447
  }
a915da9b6   Eric Dumazet   tcp: md5: rcu con...
1448
1449
1450
1451
1452
1453
  union tcp_md5_addr {
  	struct in_addr  a4;
  #if IS_ENABLED(CONFIG_IPV6)
  	struct in6_addr	a6;
  #endif
  };
cfb6eeb4c   YOSHIFUJI Hideaki   [TCP]: MD5 Signat...
1454
1455
  /* - key database */
  struct tcp_md5sig_key {
a915da9b6   Eric Dumazet   tcp: md5: rcu con...
1456
  	struct hlist_node	node;
cfb6eeb4c   YOSHIFUJI Hideaki   [TCP]: MD5 Signat...
1457
  	u8			keylen;
a915da9b6   Eric Dumazet   tcp: md5: rcu con...
1458
  	u8			family; /* AF_INET or AF_INET6 */
6797318e6   Ivan Delalande   tcp: md5: add an ...
1459
  	u8			prefixlen;
dea53bb80   David Ahern   tcp: Add l3index ...
1460
1461
  	union tcp_md5_addr	addr;
  	int			l3index; /* set if key added with L3 scope */
a915da9b6   Eric Dumazet   tcp: md5: rcu con...
1462
1463
  	u8			key[TCP_MD5SIG_MAXKEYLEN];
  	struct rcu_head		rcu;
cfb6eeb4c   YOSHIFUJI Hideaki   [TCP]: MD5 Signat...
1464
1465
1466
1467
  };
  
  /* - sock block */
  struct tcp_md5sig_info {
a915da9b6   Eric Dumazet   tcp: md5: rcu con...
1468
  	struct hlist_head	head;
a8afca032   Eric Dumazet   tcp: md5: protect...
1469
  	struct rcu_head		rcu;
cfb6eeb4c   YOSHIFUJI Hideaki   [TCP]: MD5 Signat...
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
  };
  
  /* - pseudo header */
  struct tcp4_pseudohdr {
  	__be32		saddr;
  	__be32		daddr;
  	__u8		pad;
  	__u8		protocol;
  	__be16		len;
  };
  
  struct tcp6_pseudohdr {
  	struct in6_addr	saddr;
  	struct in6_addr daddr;
  	__be32		len;
  	__be32		protocol;	/* including padding */
  };
  
  union tcp_md5sum_block {
  	struct tcp4_pseudohdr ip4;
dfd56b8b3   Eric Dumazet   net: use IS_ENABL...
1490
  #if IS_ENABLED(CONFIG_IPV6)
cfb6eeb4c   YOSHIFUJI Hideaki   [TCP]: MD5 Signat...
1491
1492
1493
1494
1495
1496
  	struct tcp6_pseudohdr ip6;
  #endif
  };
  
  /* - pool: digest algorithm, hash description and scratch buffer */
  struct tcp_md5sig_pool {
cf80e0e47   Herbert Xu   tcp: Use ahash
1497
  	struct ahash_request	*md5_req;
19689e38e   Eric Dumazet   tcp: md5: use kma...
1498
  	void			*scratch;
cfb6eeb4c   YOSHIFUJI Hideaki   [TCP]: MD5 Signat...
1499
  };
cfb6eeb4c   YOSHIFUJI Hideaki   [TCP]: MD5 Signat...
1500
  /* - functions */
39f8e58e5   Eric Dumazet   tcp: md5: remove ...
1501
1502
  int tcp_v4_md5_hash_skb(char *md5_hash, const struct tcp_md5sig_key *key,
  			const struct sock *sk, const struct sk_buff *skb);
5c9f30236   Joe Perches   tcp.h: Remove ext...
1503
  int tcp_md5_do_add(struct sock *sk, const union tcp_md5_addr *addr,
dea53bb80   David Ahern   tcp: Add l3index ...
1504
1505
  		   int family, u8 prefixlen, int l3index,
  		   const u8 *newkey, u8 newkeylen, gfp_t gfp);
5c9f30236   Joe Perches   tcp.h: Remove ext...
1506
  int tcp_md5_do_del(struct sock *sk, const union tcp_md5_addr *addr,
dea53bb80   David Ahern   tcp: Add l3index ...
1507
  		   int family, u8 prefixlen, int l3index);
b83e3deb9   Eric Dumazet   tcp: md5: constif...
1508
  struct tcp_md5sig_key *tcp_v4_md5_lookup(const struct sock *sk,
fd3a154a0   Eric Dumazet   tcp: md5: get rid...
1509
  					 const struct sock *addr_sk);
cfb6eeb4c   YOSHIFUJI Hideaki   [TCP]: MD5 Signat...
1510

9501f9722   YOSHIFUJI Hideaki   tcp md5sig: Let t...
1511
  #ifdef CONFIG_TCP_MD5SIG
6015c71e6   Eric Dumazet   tcp: md5: add tcp...
1512
  #include <linux/jump_label.h>
921f9a0f2   Eric Dumazet   tcp: convert tcp_...
1513
  extern struct static_key_false tcp_md5_needed;
dea53bb80   David Ahern   tcp: Add l3index ...
1514
  struct tcp_md5sig_key *__tcp_md5_do_lookup(const struct sock *sk, int l3index,
6015c71e6   Eric Dumazet   tcp: md5: add tcp...
1515
1516
1517
  					   const union tcp_md5_addr *addr,
  					   int family);
  static inline struct tcp_md5sig_key *
dea53bb80   David Ahern   tcp: Add l3index ...
1518
1519
  tcp_md5_do_lookup(const struct sock *sk, int l3index,
  		  const union tcp_md5_addr *addr, int family)
6015c71e6   Eric Dumazet   tcp: md5: add tcp...
1520
  {
921f9a0f2   Eric Dumazet   tcp: convert tcp_...
1521
  	if (!static_branch_unlikely(&tcp_md5_needed))
6015c71e6   Eric Dumazet   tcp: md5: add tcp...
1522
  		return NULL;
dea53bb80   David Ahern   tcp: Add l3index ...
1523
  	return __tcp_md5_do_lookup(sk, l3index, addr, family);
6015c71e6   Eric Dumazet   tcp: md5: add tcp...
1524
  }
a915da9b6   Eric Dumazet   tcp: md5: rcu con...
1525
  #define tcp_twsk_md5_key(twsk)	((twsk)->tw_md5_key)
9501f9722   YOSHIFUJI Hideaki   tcp md5sig: Let t...
1526
  #else
dea53bb80   David Ahern   tcp: Add l3index ...
1527
1528
1529
  static inline struct tcp_md5sig_key *
  tcp_md5_do_lookup(const struct sock *sk, int l3index,
  		  const union tcp_md5_addr *addr, int family)
a915da9b6   Eric Dumazet   tcp: md5: rcu con...
1530
1531
1532
  {
  	return NULL;
  }
9501f9722   YOSHIFUJI Hideaki   tcp md5sig: Let t...
1533
1534
  #define tcp_twsk_md5_key(twsk)	NULL
  #endif
5c9f30236   Joe Perches   tcp.h: Remove ext...
1535
  bool tcp_alloc_md5sig_pool(void);
cfb6eeb4c   YOSHIFUJI Hideaki   [TCP]: MD5 Signat...
1536

5c9f30236   Joe Perches   tcp.h: Remove ext...
1537
  struct tcp_md5sig_pool *tcp_get_md5sig_pool(void);
71cea17ed   Eric Dumazet   tcp: md5: remove ...
1538
1539
1540
1541
  static inline void tcp_put_md5sig_pool(void)
  {
  	local_bh_enable();
  }
35790c042   Eric Dumazet   tcp: fix MD5 (RFC...
1542

5c9f30236   Joe Perches   tcp.h: Remove ext...
1543
1544
1545
1546
  int tcp_md5_hash_skb_data(struct tcp_md5sig_pool *, const struct sk_buff *,
  			  unsigned int header_len);
  int tcp_md5_hash_key(struct tcp_md5sig_pool *hp,
  		     const struct tcp_md5sig_key *key);
cfb6eeb4c   YOSHIFUJI Hideaki   [TCP]: MD5 Signat...
1547

104671636   Jerry Chu   tcp: TCP Fast Ope...
1548
  /* From tcp_fastopen.c */
5c9f30236   Joe Perches   tcp.h: Remove ext...
1549
  void tcp_fastopen_cache_get(struct sock *sk, u16 *mss,
7268586ba   Yuchung Cheng   tcp: pause Fast O...
1550
  			    struct tcp_fastopen_cookie *cookie);
5c9f30236   Joe Perches   tcp.h: Remove ext...
1551
  void tcp_fastopen_cache_set(struct sock *sk, u16 mss,
2646c831c   Daniel Lee   tcp: RFC7413 opti...
1552
1553
  			    struct tcp_fastopen_cookie *cookie, bool syn_lost,
  			    u16 try_exp);
783237e8d   Yuchung Cheng   net-tcp: Fast Ope...
1554
1555
1556
1557
  struct tcp_fastopen_request {
  	/* Fast Open cookie. Size 0 means a cookie request */
  	struct tcp_fastopen_cookie	cookie;
  	struct msghdr			*data;  /* data in MSG_FASTOPEN */
f5ddcbbb4   Eric Dumazet   net-tcp: fastopen...
1558
1559
  	size_t				size;
  	int				copied;	/* queued in tcp_connect() */
f859a4484   Willem de Bruijn   tcp: allow zeroco...
1560
  	struct ubuf_info		*uarg;
783237e8d   Yuchung Cheng   net-tcp: Fast Ope...
1561
  };
783237e8d   Yuchung Cheng   net-tcp: Fast Ope...
1562
  void tcp_free_fastopen_req(struct tcp_sock *tp);
1fba70e5b   Yuchung Cheng   tcp: socket optio...
1563
  void tcp_fastopen_destroy_cipher(struct sock *sk);
437138485   Haishuang Yan   ipv4: Namespaceif...
1564
  void tcp_fastopen_ctx_destroy(struct net *net);
1fba70e5b   Yuchung Cheng   tcp: socket optio...
1565
  int tcp_fastopen_reset_cipher(struct net *net, struct sock *sk,
438ac8800   Ard Biesheuvel   net: fastopen: ro...
1566
  			      void *primary_key, void *backup_key);
f19008e67   Jason Baron   tcp: correct read...
1567
1568
  int tcp_fastopen_get_cipher(struct net *net, struct inet_connection_sock *icsk,
  			    u64 *key);
61d2bcae9   Eric Dumazet   tcp: fastopen: ac...
1569
  void tcp_fastopen_add_skb(struct sock *sk, struct sk_buff *skb);
7c85af881   Eric Dumazet   tcp: avoid reorde...
1570
1571
  struct sock *tcp_try_fastopen(struct sock *sk, struct sk_buff *skb,
  			      struct request_sock *req,
71c02379c   Christoph Paasch   tcp: Configure TF...
1572
1573
  			      struct tcp_fastopen_cookie *foc,
  			      const struct dst_entry *dst);
437138485   Haishuang Yan   ipv4: Namespaceif...
1574
  void tcp_fastopen_init_key_once(struct net *net);
065263f40   Wei Wang   net/tcp-fastopen:...
1575
1576
  bool tcp_fastopen_cookie_check(struct sock *sk, u16 *mss,
  			     struct tcp_fastopen_cookie *cookie);
19f6d3f3c   Wei Wang   net/tcp-fastopen:...
1577
  bool tcp_fastopen_defer_connect(struct sock *sk, int *err);
438ac8800   Ard Biesheuvel   net: fastopen: ro...
1578
  #define TCP_FASTOPEN_KEY_LENGTH sizeof(siphash_key_t)
9092a76d3   Jason Baron   tcp: add backup T...
1579
1580
1581
  #define TCP_FASTOPEN_KEY_MAX 2
  #define TCP_FASTOPEN_KEY_BUF_LENGTH \
  	(TCP_FASTOPEN_KEY_LENGTH * TCP_FASTOPEN_KEY_MAX)
104671636   Jerry Chu   tcp: TCP Fast Ope...
1582
1583
1584
  
  /* Fastopen key context */
  struct tcp_fastopen_context {
438ac8800   Ard Biesheuvel   net: fastopen: ro...
1585
  	siphash_key_t	key[TCP_FASTOPEN_KEY_MAX];
c681edae3   Ard Biesheuvel   net: ipv4: move t...
1586
1587
  	int		num;
  	struct rcu_head	rcu;
104671636   Jerry Chu   tcp: TCP Fast Ope...
1588
  };
cf1ef3f07   Wei Wang   net/tcp_fastopen:...
1589
  extern unsigned int sysctl_tcp_fastopen_blackhole_timeout;
46c2fa398   Wei Wang   net/tcp_fastopen:...
1590
  void tcp_fastopen_active_disable(struct sock *sk);
cf1ef3f07   Wei Wang   net/tcp_fastopen:...
1591
1592
  bool tcp_fastopen_active_should_disable(struct sock *sk);
  void tcp_fastopen_active_disable_ofo_check(struct sock *sk);
7268586ba   Yuchung Cheng   tcp: pause Fast O...
1593
  void tcp_fastopen_active_detect_blackhole(struct sock *sk, bool expired);
cf1ef3f07   Wei Wang   net/tcp_fastopen:...
1594

9092a76d3   Jason Baron   tcp: add backup T...
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
  /* Caller needs to wrap with rcu_read_(un)lock() */
  static inline
  struct tcp_fastopen_context *tcp_fastopen_get_ctx(const struct sock *sk)
  {
  	struct tcp_fastopen_context *ctx;
  
  	ctx = rcu_dereference(inet_csk(sk)->icsk_accept_queue.fastopenq.ctx);
  	if (!ctx)
  		ctx = rcu_dereference(sock_net(sk)->ipv4.tcp_fastopen_ctx);
  	return ctx;
  }
  
  static inline
  bool tcp_fastopen_cookie_match(const struct tcp_fastopen_cookie *foc,
  			       const struct tcp_fastopen_cookie *orig)
  {
  	if (orig->len == TCP_FASTOPEN_COOKIE_SIZE &&
  	    orig->len == foc->len &&
  	    !memcmp(orig->val, foc->val, foc->len))
  		return true;
  	return false;
  }
  
  static inline
  int tcp_fastopen_context_len(const struct tcp_fastopen_context *ctx)
  {
c681edae3   Ard Biesheuvel   net: ipv4: move t...
1621
  	return ctx->num;
9092a76d3   Jason Baron   tcp: add backup T...
1622
  }
05b055e89   Francis Yan   tcp: instrument t...
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
  /* Latencies incurred by various limits for a sender. They are
   * chronograph-like stats that are mutually exclusive.
   */
  enum tcp_chrono {
  	TCP_CHRONO_UNSPEC,
  	TCP_CHRONO_BUSY, /* Actively sending data (non-empty write queue) */
  	TCP_CHRONO_RWND_LIMITED, /* Stalled by insufficient receive window */
  	TCP_CHRONO_SNDBUF_LIMITED, /* Stalled by insufficient send buffer */
  	__TCP_CHRONO_MAX,
  };
  
  void tcp_chrono_start(struct sock *sk, const enum tcp_chrono type);
  void tcp_chrono_stop(struct sock *sk, const enum tcp_chrono type);
e2080072e   Eric Dumazet   tcp: new list for...
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
  /* This helper is needed, because skb->tcp_tsorted_anchor uses
   * the same memory storage than skb->destructor/_skb_refdst
   */
  static inline void tcp_skb_tsorted_anchor_cleanup(struct sk_buff *skb)
  {
  	skb->destructor = NULL;
  	skb->_skb_refdst = 0UL;
  }
  
  #define tcp_skb_tsorted_save(skb) {		\
  	unsigned long _save = skb->_skb_refdst;	\
  	skb->_skb_refdst = 0UL;
  
  #define tcp_skb_tsorted_restore(skb)		\
  	skb->_skb_refdst = _save;		\
  }
ac3f09ba3   Eric Dumazet   tcp: uninline tcp...
1652
  void tcp_write_queue_purge(struct sock *sk);
fe067e8ab   David S. Miller   [TCP]: Abstract o...
1653

75c119afe   Eric Dumazet   tcp: implement rb...
1654
1655
1656
1657
  static inline struct sk_buff *tcp_rtx_queue_head(const struct sock *sk)
  {
  	return skb_rb_first(&sk->tcp_rtx_queue);
  }
b617158dc   Eric Dumazet   tcp: be more care...
1658
1659
1660
1661
  static inline struct sk_buff *tcp_rtx_queue_tail(const struct sock *sk)
  {
  	return skb_rb_last(&sk->tcp_rtx_queue);
  }
cf533ea53   Eric Dumazet   tcp: add const qu...
1662
  static inline struct sk_buff *tcp_write_queue_head(const struct sock *sk)
fe067e8ab   David S. Miller   [TCP]: Abstract o...
1663
  {
cd07a8ea0   David S. Miller   tcp: Use SKB queu...
1664
  	return skb_peek(&sk->sk_write_queue);
fe067e8ab   David S. Miller   [TCP]: Abstract o...
1665
  }
cf533ea53   Eric Dumazet   tcp: add const qu...
1666
  static inline struct sk_buff *tcp_write_queue_tail(const struct sock *sk)
fe067e8ab   David S. Miller   [TCP]: Abstract o...
1667
  {
cd07a8ea0   David S. Miller   tcp: Use SKB queu...
1668
  	return skb_peek_tail(&sk->sk_write_queue);
fe067e8ab   David S. Miller   [TCP]: Abstract o...
1669
  }
234b68607   Ilpo Järvinen   [TCP]: Add tcp_fo...
1670
  #define tcp_for_write_queue_from_safe(skb, tmp, sk)			\
cd07a8ea0   David S. Miller   tcp: Use SKB queu...
1671
  	skb_queue_walk_from_safe(&(sk)->sk_write_queue, skb, tmp)
234b68607   Ilpo Järvinen   [TCP]: Add tcp_fo...
1672

cf533ea53   Eric Dumazet   tcp: add const qu...
1673
  static inline struct sk_buff *tcp_send_head(const struct sock *sk)
fe067e8ab   David S. Miller   [TCP]: Abstract o...
1674
  {
75c119afe   Eric Dumazet   tcp: implement rb...
1675
  	return skb_peek(&sk->sk_write_queue);
fe067e8ab   David S. Miller   [TCP]: Abstract o...
1676
  }
cd07a8ea0   David S. Miller   tcp: Use SKB queu...
1677
1678
1679
1680
1681
  static inline bool tcp_skb_is_last(const struct sock *sk,
  				   const struct sk_buff *skb)
  {
  	return skb_queue_is_last(&sk->sk_write_queue, skb);
  }
ee2aabd3f   Eric Dumazet   tcp: refine tcp_w...
1682
1683
1684
1685
1686
1687
1688
  /**
   * tcp_write_queue_empty - test if any payload (or FIN) is available in write queue
   * @sk: socket
   *
   * Since the write queue can have a temporary empty skb in it,
   * we must not use "return skb_queue_empty(&sk->sk_write_queue)"
   */
75c119afe   Eric Dumazet   tcp: implement rb...
1689
  static inline bool tcp_write_queue_empty(const struct sock *sk)
fe067e8ab   David S. Miller   [TCP]: Abstract o...
1690
  {
ee2aabd3f   Eric Dumazet   tcp: refine tcp_w...
1691
1692
1693
  	const struct tcp_sock *tp = tcp_sk(sk);
  
  	return tp->write_seq == tp->snd_nxt;
75c119afe   Eric Dumazet   tcp: implement rb...
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
  }
  
  static inline bool tcp_rtx_queue_empty(const struct sock *sk)
  {
  	return RB_EMPTY_ROOT(&sk->tcp_rtx_queue);
  }
  
  static inline bool tcp_rtx_and_write_queues_empty(const struct sock *sk)
  {
  	return tcp_rtx_queue_empty(sk) && tcp_write_queue_empty(sk);
fe067e8ab   David S. Miller   [TCP]: Abstract o...
1704
  }
fe067e8ab   David S. Miller   [TCP]: Abstract o...
1705
1706
  static inline void tcp_add_write_queue_tail(struct sock *sk, struct sk_buff *skb)
  {
a43e052be   Eric Dumazet   tcp: get rid of _...
1707
  	__skb_queue_tail(&sk->sk_write_queue, skb);
fe067e8ab   David S. Miller   [TCP]: Abstract o...
1708
1709
  
  	/* Queue it, remembering where we must start sending. */
50895b9de   Eric Dumazet   tcp: highest_sack...
1710
  	if (sk->sk_write_queue.next == skb)
0f87230d1   Francis Yan   tcp: instrument h...
1711
  		tcp_chrono_start(sk, TCP_CHRONO_BUSY);
fe067e8ab   David S. Miller   [TCP]: Abstract o...
1712
  }
43f59c893   David S. Miller   net: Remove __skb...
1713
  /* Insert new before skb on the write queue of sk.  */
fe067e8ab   David S. Miller   [TCP]: Abstract o...
1714
1715
1716
1717
  static inline void tcp_insert_write_queue_before(struct sk_buff *new,
  						  struct sk_buff *skb,
  						  struct sock *sk)
  {
43f59c893   David S. Miller   net: Remove __skb...
1718
  	__skb_queue_before(&sk->sk_write_queue, skb, new);
fe067e8ab   David S. Miller   [TCP]: Abstract o...
1719
1720
1721
1722
  }
  
  static inline void tcp_unlink_write_queue(struct sk_buff *skb, struct sock *sk)
  {
4a269818a   Eric Dumazet   tcp: fix tcp_unli...
1723
  	tcp_skb_tsorted_anchor_cleanup(skb);
fe067e8ab   David S. Miller   [TCP]: Abstract o...
1724
1725
  	__skb_unlink(skb, &sk->sk_write_queue);
  }
75c119afe   Eric Dumazet   tcp: implement rb...
1726
1727
1728
  void tcp_rbtree_insert(struct rb_root *root, struct sk_buff *skb);
  
  static inline void tcp_rtx_queue_unlink(struct sk_buff *skb, struct sock *sk)
fe067e8ab   David S. Miller   [TCP]: Abstract o...
1729
  {
75c119afe   Eric Dumazet   tcp: implement rb...
1730
1731
1732
1733
1734
1735
1736
1737
1738
  	tcp_skb_tsorted_anchor_cleanup(skb);
  	rb_erase(&skb->rbnode, &sk->tcp_rtx_queue);
  }
  
  static inline void tcp_rtx_queue_unlink_and_free(struct sk_buff *skb, struct sock *sk)
  {
  	list_del(&skb->tcp_tsorted_anchor);
  	tcp_rtx_queue_unlink(skb, sk);
  	sk_wmem_free_skb(sk, skb);
fe067e8ab   David S. Miller   [TCP]: Abstract o...
1739
  }
12d50c46d   Krishna Kumar   tcp: Remove check...
1740
1741
1742
1743
1744
1745
1746
1747
  static inline void tcp_push_pending_frames(struct sock *sk)
  {
  	if (tcp_send_head(sk)) {
  		struct tcp_sock *tp = tcp_sk(sk);
  
  		__tcp_push_pending_frames(sk, tcp_current_mss(sk), tp->nonagle);
  	}
  }
ecb971923   Neal Cardwell   tcp: fix comment ...
1748
1749
1750
  /* Start sequence of the skb just after the highest skb with SACKed
   * bit, valid only if sacked_out > 0 or when the caller has ensured
   * validity by itself.
a47e5a988   Ilpo Järvinen   [TCP]: Convert hi...
1751
1752
1753
1754
1755
   */
  static inline u32 tcp_highest_sack_seq(struct tcp_sock *tp)
  {
  	if (!tp->sacked_out)
  		return tp->snd_una;
6859d4947   Ilpo Järvinen   [TCP]: Abstract t...
1756
1757
1758
  
  	if (tp->highest_sack == NULL)
  		return tp->snd_nxt;
a47e5a988   Ilpo Järvinen   [TCP]: Convert hi...
1759
1760
  	return TCP_SKB_CB(tp->highest_sack)->seq;
  }
6859d4947   Ilpo Järvinen   [TCP]: Abstract t...
1761
1762
  static inline void tcp_advance_highest_sack(struct sock *sk, struct sk_buff *skb)
  {
50895b9de   Eric Dumazet   tcp: highest_sack...
1763
  	tcp_sk(sk)->highest_sack = skb_rb_next(skb);
6859d4947   Ilpo Järvinen   [TCP]: Abstract t...
1764
1765
1766
1767
1768
1769
1770
1771
1772
  }
  
  static inline struct sk_buff *tcp_highest_sack(struct sock *sk)
  {
  	return tcp_sk(sk)->highest_sack;
  }
  
  static inline void tcp_highest_sack_reset(struct sock *sk)
  {
50895b9de   Eric Dumazet   tcp: highest_sack...
1773
  	tcp_sk(sk)->highest_sack = tcp_rtx_queue_head(sk);
6859d4947   Ilpo Järvinen   [TCP]: Abstract t...
1774
  }
2b7cda9c3   Eric Dumazet   tcp: fix tcp_mtu_...
1775
1776
  /* Called when old skb is about to be deleted and replaced by new skb */
  static inline void tcp_highest_sack_replace(struct sock *sk,
6859d4947   Ilpo Järvinen   [TCP]: Abstract t...
1777
1778
1779
  					    struct sk_buff *old,
  					    struct sk_buff *new)
  {
2b7cda9c3   Eric Dumazet   tcp: fix tcp_mtu_...
1780
  	if (old == tcp_highest_sack(sk))
6859d4947   Ilpo Järvinen   [TCP]: Abstract t...
1781
1782
  		tcp_sk(sk)->highest_sack = new;
  }
b1f0a0e99   Florian Westphal   net: add inet_sk_...
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
  /* This helper checks if socket has IP_TRANSPARENT set */
  static inline bool inet_sk_transparent(const struct sock *sk)
  {
  	switch (sk->sk_state) {
  	case TCP_TIME_WAIT:
  		return inet_twsk(sk)->tw_transparent;
  	case TCP_NEW_SYN_RECV:
  		return inet_rsk(inet_reqsk(sk))->no_srccheck;
  	}
  	return inet_sk(sk)->transparent;
  }
5aa4b32fc   Andreas Petlund   net: TCP thin-str...
1794
1795
1796
  /* Determines whether this is a thin stream (which may suffer from
   * increased latency). Used to trigger latency-reducing mechanisms.
   */
a2a385d62   Eric Dumazet   tcp: bool convers...
1797
  static inline bool tcp_stream_is_thin(struct tcp_sock *tp)
5aa4b32fc   Andreas Petlund   net: TCP thin-str...
1798
1799
1800
  {
  	return tp->packets_out < 4 && !tcp_in_initial_slowstart(tp);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1801
1802
1803
  /* /proc */
  enum tcp_seq_states {
  	TCP_SEQ_STATE_LISTENING,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1804
  	TCP_SEQ_STATE_ESTABLISHED,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1805
  };
37d849bb4   Christoph Hellwig   ipv{4,6}/tcp: sim...
1806
1807
1808
  void *tcp_seq_start(struct seq_file *seq, loff_t *pos);
  void *tcp_seq_next(struct seq_file *seq, void *v, loff_t *pos);
  void tcp_seq_stop(struct seq_file *seq, void *v);
73cb88ecb   Arjan van de Ven   net: make the tcp...
1809

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1810
  struct tcp_seq_afinfo {
73cb88ecb   Arjan van de Ven   net: make the tcp...
1811
  	sa_family_t			family;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1812
1813
1814
  };
  
  struct tcp_iter_state {
a4146b1b2   Denis V. Lunev   [TCP]: Replace st...
1815
  	struct seq_net_private	p;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1816
1817
  	enum tcp_seq_states	state;
  	struct sock		*syn_wait_sk;
b08d4d3b6   Yonghong Song   net: bpf: Add bpf...
1818
  	struct tcp_seq_afinfo	*bpf_seq_afinfo;
a7cb5a49b   Eric W. Biederman   userns: Print out...
1819
  	int			bucket, offset, sbucket, num;
a8b690f98   Tom Herbert   tcp: Fix slowness...
1820
  	loff_t			last_pos;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1821
  };
20380731b   Arnaldo Carvalho de Melo   [NET]: Fix sparse...
1822
  extern struct request_sock_ops tcp_request_sock_ops;
c6aefafb7   Glenn Griffin   [TCP]: Add IPv6 s...
1823
  extern struct request_sock_ops tcp6_request_sock_ops;
20380731b   Arnaldo Carvalho de Melo   [NET]: Fix sparse...
1824

5c9f30236   Joe Perches   tcp.h: Remove ext...
1825
  void tcp_v4_destroy_sock(struct sock *sk);
20380731b   Arnaldo Carvalho de Melo   [NET]: Fix sparse...
1826

28be6e07e   Eric Dumazet   tcp: rename tcp_t...
1827
  struct sk_buff *tcp_gso_segment(struct sk_buff *skb,
5c9f30236   Joe Perches   tcp.h: Remove ext...
1828
  				netdev_features_t features);
d4546c250   David Miller   net: Convert GRO ...
1829
  struct sk_buff *tcp_gro_receive(struct list_head *head, struct sk_buff *skb);
5521d95e0   Eric Dumazet   net: move tcp gro...
1830
1831
1832
1833
  INDIRECT_CALLABLE_DECLARE(int tcp4_gro_complete(struct sk_buff *skb, int thoff));
  INDIRECT_CALLABLE_DECLARE(struct sk_buff *tcp4_gro_receive(struct list_head *head, struct sk_buff *skb));
  INDIRECT_CALLABLE_DECLARE(int tcp6_gro_complete(struct sk_buff *skb, int thoff));
  INDIRECT_CALLABLE_DECLARE(struct sk_buff *tcp6_gro_receive(struct list_head *head, struct sk_buff *skb));
5c9f30236   Joe Perches   tcp.h: Remove ext...
1834
  int tcp_gro_complete(struct sk_buff *skb);
28850dc7c   Daniel Borkmann   net: tcp: move GR...
1835

5c9f30236   Joe Perches   tcp.h: Remove ext...
1836
  void __tcp_v4_send_check(struct sk_buff *skb, __be32 saddr, __be32 daddr);
f4c50d990   Herbert Xu   [NET]: Add softwa...
1837

c9bee3b7f   Eric Dumazet   tcp: TCP_NOTSENT_...
1838
1839
  static inline u32 tcp_notsent_lowat(const struct tcp_sock *tp)
  {
4979f2d9f   Nikolay Borisov   ipv4: Namespaceif...
1840
1841
  	struct net *net = sock_net((struct sock *)tp);
  	return tp->notsent_lowat ?: net->ipv4.sysctl_tcp_notsent_lowat;
c9bee3b7f   Eric Dumazet   tcp: TCP_NOTSENT_...
1842
  }
a74f0fa08   Eric Dumazet   tcp: reduce POLLO...
1843
1844
1845
1846
1847
  /* @wake is one when sk_stream_write_space() calls us.
   * This sends EPOLLOUT only if notsent_bytes is half the limit.
   * This mimics the strategy used in sock_def_write_space().
   */
  static inline bool tcp_stream_memory_free(const struct sock *sk, int wake)
c9bee3b7f   Eric Dumazet   tcp: TCP_NOTSENT_...
1848
1849
  {
  	const struct tcp_sock *tp = tcp_sk(sk);
e0d694d63   Eric Dumazet   tcp: annotate tp-...
1850
1851
  	u32 notsent_bytes = READ_ONCE(tp->write_seq) -
  			    READ_ONCE(tp->snd_nxt);
c9bee3b7f   Eric Dumazet   tcp: TCP_NOTSENT_...
1852

a74f0fa08   Eric Dumazet   tcp: reduce POLLO...
1853
  	return (notsent_bytes << wake) < tcp_notsent_lowat(tp);
c9bee3b7f   Eric Dumazet   tcp: TCP_NOTSENT_...
1854
  }
20380731b   Arnaldo Carvalho de Melo   [NET]: Fix sparse...
1855
  #ifdef CONFIG_PROC_FS
5c9f30236   Joe Perches   tcp.h: Remove ext...
1856
1857
  int tcp4_proc_init(void);
  void tcp4_proc_exit(void);
20380731b   Arnaldo Carvalho de Melo   [NET]: Fix sparse...
1858
  #endif
ea3bea3a1   Eric Dumazet   tcp/dccp: constif...
1859
  int tcp_rtx_synack(const struct sock *sk, struct request_sock *req);
1fb6f159f   Octavian Purdila   tcp: add tcp_conn...
1860
1861
1862
  int tcp_conn_request(struct request_sock_ops *rsk_ops,
  		     const struct tcp_request_sock_ops *af_ops,
  		     struct sock *sk, struct sk_buff *skb);
5db92c994   Octavian Purdila   tcp: unify tcp_v4...
1863

cfb6eeb4c   YOSHIFUJI Hideaki   [TCP]: MD5 Signat...
1864
1865
1866
  /* TCP af-specific functions */
  struct tcp_sock_af_ops {
  #ifdef CONFIG_TCP_MD5SIG
b83e3deb9   Eric Dumazet   tcp: md5: constif...
1867
  	struct tcp_md5sig_key	*(*md5_lookup) (const struct sock *sk,
fd3a154a0   Eric Dumazet   tcp: md5: get rid...
1868
  						const struct sock *addr_sk);
39f8e58e5   Eric Dumazet   tcp: md5: remove ...
1869
1870
1871
1872
1873
  	int		(*calc_md5_hash)(char *location,
  					 const struct tcp_md5sig_key *md5,
  					 const struct sock *sk,
  					 const struct sk_buff *skb);
  	int		(*md5_parse)(struct sock *sk,
8917a777b   Ivan Delalande   tcp: md5: add TCP...
1874
  				     int optname,
d4c19c491   Christoph Hellwig   net/tcp: switch -...
1875
  				     sockptr_t optval,
39f8e58e5   Eric Dumazet   tcp: md5: remove ...
1876
  				     int optlen);
cfb6eeb4c   YOSHIFUJI Hideaki   [TCP]: MD5 Signat...
1877
1878
1879
1880
  #endif
  };
  
  struct tcp_request_sock_ops {
2aec4a297   Octavian Purdila   tcp: add mss_clam...
1881
  	u16 mss_clamp;
cfb6eeb4c   YOSHIFUJI Hideaki   [TCP]: MD5 Signat...
1882
  #ifdef CONFIG_TCP_MD5SIG
b83e3deb9   Eric Dumazet   tcp: md5: constif...
1883
  	struct tcp_md5sig_key *(*req_md5_lookup)(const struct sock *sk,
fd3a154a0   Eric Dumazet   tcp: md5: get rid...
1884
  						 const struct sock *addr_sk);
39f8e58e5   Eric Dumazet   tcp: md5: remove ...
1885
1886
1887
1888
  	int		(*calc_md5_hash) (char *location,
  					  const struct tcp_md5sig_key *md5,
  					  const struct sock *sk,
  					  const struct sk_buff *skb);
cfb6eeb4c   YOSHIFUJI Hideaki   [TCP]: MD5 Signat...
1889
  #endif
b40cf18ef   Eric Dumazet   tcp: constify lis...
1890
1891
  	void (*init_req)(struct request_sock *req,
  			 const struct sock *sk_listener,
16bea70aa   Octavian Purdila   tcp: add init_req...
1892
  			 struct sk_buff *skb);
fb7b37a7f   Octavian Purdila   tcp: add init_coo...
1893
  #ifdef CONFIG_SYN_COOKIES
3f684b4b1   Eric Dumazet   tcp: cookie_init_...
1894
  	__u32 (*cookie_init_seq)(const struct sk_buff *skb,
fb7b37a7f   Octavian Purdila   tcp: add init_coo...
1895
1896
  				 __u16 *mss);
  #endif
f964629e3   Eric Dumazet   tcp: constify tcp...
1897
  	struct dst_entry *(*route_req)(const struct sock *sk, struct flowi *fl,
4396e4618   Soheil Hassas Yeganeh   tcp: remove tcp_t...
1898
  				       const struct request_sock *req);
84b114b98   Eric Dumazet   tcp: randomize ti...
1899
  	u32 (*init_seq)(const struct sk_buff *skb);
5d2ed0521   Eric Dumazet   tcp: Namespaceify...
1900
  	u32 (*init_ts_off)(const struct net *net, const struct sk_buff *skb);
0f935dbed   Eric Dumazet   tcp: constify tcp...
1901
  	int (*send_synack)(const struct sock *sk, struct dst_entry *dst,
d6274bd8d   Octavian Purdila   tcp: add send_syn...
1902
  			   struct flowi *fl, struct request_sock *req,
dc6ef6be5   Eric Dumazet   tcp: do not set q...
1903
  			   struct tcp_fastopen_cookie *foc,
331fca431   Martin KaFai Lau   bpf: tcp: Add bpf...
1904
1905
  			   enum tcp_synack_type synack_type,
  			   struct sk_buff *syn_skb);
cfb6eeb4c   YOSHIFUJI Hideaki   [TCP]: MD5 Signat...
1906
  };
35b2c3211   Mat Martineau   tcp: Export TCP f...
1907
1908
1909
1910
  extern const struct tcp_request_sock_ops tcp_request_sock_ipv4_ops;
  #if IS_ENABLED(CONFIG_IPV6)
  extern const struct tcp_request_sock_ops tcp_request_sock_ipv6_ops;
  #endif
fb7b37a7f   Octavian Purdila   tcp: add init_coo...
1911
1912
  #ifdef CONFIG_SYN_COOKIES
  static inline __u32 cookie_init_sequence(const struct tcp_request_sock_ops *ops,
3f684b4b1   Eric Dumazet   tcp: cookie_init_...
1913
  					 const struct sock *sk, struct sk_buff *skb,
fb7b37a7f   Octavian Purdila   tcp: add init_coo...
1914
1915
  					 __u16 *mss)
  {
3f684b4b1   Eric Dumazet   tcp: cookie_init_...
1916
  	tcp_synq_overflow(sk);
02a1d6e7a   Eric Dumazet   net: rename NET_{...
1917
  	__NET_INC_STATS(sock_net(sk), LINUX_MIB_SYNCOOKIESSENT);
3f684b4b1   Eric Dumazet   tcp: cookie_init_...
1918
  	return ops->cookie_init_seq(skb, mss);
fb7b37a7f   Octavian Purdila   tcp: add init_coo...
1919
1920
1921
  }
  #else
  static inline __u32 cookie_init_sequence(const struct tcp_request_sock_ops *ops,
3f684b4b1   Eric Dumazet   tcp: cookie_init_...
1922
  					 const struct sock *sk, struct sk_buff *skb,
fb7b37a7f   Octavian Purdila   tcp: add init_coo...
1923
1924
1925
1926
1927
  					 __u16 *mss)
  {
  	return 0;
  }
  #endif
5c9f30236   Joe Perches   tcp.h: Remove ext...
1928
  int tcpv4_offload_init(void);
28850dc7c   Daniel Borkmann   net: tcp: move GR...
1929

5c9f30236   Joe Perches   tcp.h: Remove ext...
1930
1931
  void tcp_v4_init(void);
  void tcp_init(void);
20380731b   Arnaldo Carvalho de Melo   [NET]: Fix sparse...
1932

659a8ad56   Yuchung Cheng   tcp: track the pa...
1933
  /* tcp_recovery.c */
d716bfdb1   Yuchung Cheng   tcp: account lost...
1934
  void tcp_mark_skb_lost(struct sock *sk, struct sk_buff *skb);
6ac06ecd3   Yuchung Cheng   tcp: simpler NewR...
1935
  void tcp_newreno_mark_lost(struct sock *sk, bool snd_una_advanced);
b8fef65a8   Yuchung Cheng   tcp: new helper t...
1936
1937
  extern s32 tcp_rack_skb_timeout(struct tcp_sock *tp, struct sk_buff *skb,
  				u32 reo_wnd);
128eda86b   Eric Dumazet   tcp: do not pass ...
1938
  extern void tcp_rack_mark_lost(struct sock *sk);
1d0833df5   Yuchung Cheng   tcp: use sequence...
1939
  extern void tcp_rack_advance(struct tcp_sock *tp, u8 sacked, u32 end_seq,
9a568de48   Eric Dumazet   tcp: switch TCP T...
1940
  			     u64 xmit_time);
57dde7f70   Yuchung Cheng   tcp: add reorderi...
1941
  extern void tcp_rack_reo_timeout(struct sock *sk);
1f2556916   Priyaranjan Jha   tcp: higher throu...
1942
  extern void tcp_rack_update_reo_wnd(struct sock *sk, struct rate_sample *rs);
659a8ad56   Yuchung Cheng   tcp: track the pa...
1943

e1a10ef7f   Neal Cardwell   tcp: introduce tc...
1944
1945
1946
  /* At how many usecs into the future should the RTO fire? */
  static inline s64 tcp_rto_delta_us(const struct sock *sk)
  {
75c119afe   Eric Dumazet   tcp: implement rb...
1947
  	const struct sk_buff *skb = tcp_rtx_queue_head(sk);
e1a10ef7f   Neal Cardwell   tcp: introduce tc...
1948
  	u32 rto = inet_csk(sk)->icsk_rto;
2fd66ffba   Eric Dumazet   tcp: introduce tc...
1949
  	u64 rto_time_stamp_us = tcp_skb_timestamp_us(skb) + jiffies_to_usecs(rto);
e1a10ef7f   Neal Cardwell   tcp: introduce tc...
1950
1951
1952
  
  	return rto_time_stamp_us - tcp_sk(sk)->tcp_mstamp;
  }
e25f866fb   Cong Wang   ipv4: share tcp_v...
1953
1954
1955
  /*
   * Save and compile IPv4 options, return a pointer to it
   */
91ed1e666   Paolo Abeni   ip/options: expli...
1956
1957
  static inline struct ip_options_rcu *tcp_v4_save_options(struct net *net,
  							 struct sk_buff *skb)
e25f866fb   Cong Wang   ipv4: share tcp_v...
1958
1959
1960
  {
  	const struct ip_options *opt = &TCP_SKB_CB(skb)->header.h4.opt;
  	struct ip_options_rcu *dopt = NULL;
461b74c39   Cong Wang   ipv4: clean up co...
1961
  	if (opt->optlen) {
e25f866fb   Cong Wang   ipv4: share tcp_v...
1962
1963
1964
  		int opt_size = sizeof(*dopt) + opt->optlen;
  
  		dopt = kmalloc(opt_size, GFP_ATOMIC);
91ed1e666   Paolo Abeni   ip/options: expli...
1965
  		if (dopt && __ip_options_echo(net, &dopt->opt, skb, opt)) {
e25f866fb   Cong Wang   ipv4: share tcp_v...
1966
1967
1968
1969
1970
1971
  			kfree(dopt);
  			dopt = NULL;
  		}
  	}
  	return dopt;
  }
987819657   Eric Dumazet   tcp: do not pace ...
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
  /* locally generated TCP pure ACKs have skb->truesize == 2
   * (check tcp_send_ack() in net/ipv4/tcp_output.c )
   * This is much faster than dissecting the packet to find out.
   * (Think of GRE encapsulations, IPv4, IPv6, ...)
   */
  static inline bool skb_is_tcp_pure_ack(const struct sk_buff *skb)
  {
  	return skb->truesize == 2;
  }
  
  static inline void skb_set_tcp_pure_ack(struct sk_buff *skb)
  {
  	skb->truesize = 2;
  }
473bd239b   Tom Herbert   tcp: Add tcp_inq ...
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
  static inline int tcp_inq(struct sock *sk)
  {
  	struct tcp_sock *tp = tcp_sk(sk);
  	int answ;
  
  	if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) {
  		answ = 0;
  	} else if (sock_flag(sk, SOCK_URGINLINE) ||
  		   !tp->urg_data ||
  		   before(tp->urg_seq, tp->copied_seq) ||
  		   !before(tp->urg_seq, tp->rcv_nxt)) {
  
  		answ = tp->rcv_nxt - tp->copied_seq;
  
  		/* Subtract 1, if FIN was received */
  		if (answ && sock_flag(sk, SOCK_DONE))
  			answ--;
  	} else {
  		answ = tp->urg_seq - tp->copied_seq;
  	}
  
  	return answ;
  }
320355858   Tom Herbert   tcp: Set read_soc...
2009
  int tcp_peek_len(struct socket *sock);
a44d6eacd   Martin KaFai Lau   tcp: Add RFC4898 ...
2010
2011
2012
2013
2014
2015
2016
2017
2018
  static inline void tcp_segs_in(struct tcp_sock *tp, const struct sk_buff *skb)
  {
  	u16 segs_in;
  
  	segs_in = max_t(u16, 1, skb_shinfo(skb)->gso_segs);
  	tp->segs_in += segs_in;
  	if (skb->len > tcp_hdrlen(skb))
  		tp->data_segs_in += segs_in;
  }
9caad8641   Eric Dumazet   tcp: increment sk...
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
  /*
   * TCP listen path runs lockless.
   * We forced "struct sock" to be const qualified to make sure
   * we don't modify one of its field by mistake.
   * Here, we increment sk_drops which is an atomic_t, so we can safely
   * make sock writable again.
   */
  static inline void tcp_listendrop(const struct sock *sk)
  {
  	atomic_inc(&((struct sock *)sk)->sk_drops);
02a1d6e7a   Eric Dumazet   net: rename NET_{...
2029
  	__NET_INC_STATS(sock_net(sk), LINUX_MIB_LISTENDROPS);
9caad8641   Eric Dumazet   tcp: increment sk...
2030
  }
218af599f   Eric Dumazet   tcp: internal imp...
2031
  enum hrtimer_restart tcp_pace_kick(struct hrtimer *timer);
734942cc4   Dave Watson   tcp: ULP infrastr...
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
  /*
   * Interface for adding Upper Level Protocols over TCP
   */
  
  #define TCP_ULP_NAME_MAX	16
  #define TCP_ULP_MAX		128
  #define TCP_ULP_BUF_MAX		(TCP_ULP_NAME_MAX*TCP_ULP_MAX)
  
  struct tcp_ulp_ops {
  	struct list_head	list;
  
  	/* initialize ulp */
  	int (*init)(struct sock *sk);
95fa14547   John Fastabend   bpf: sockmap/tls,...
2045
  	/* update ulp */
33bfe20dd   John Fastabend   bpf: Sockmap/tls,...
2046
2047
  	void (*update)(struct sock *sk, struct proto *p,
  		       void (*write_space)(struct sock *sk));
734942cc4   Dave Watson   tcp: ULP infrastr...
2048
2049
  	/* cleanup ulp */
  	void (*release)(struct sock *sk);
61723b393   Davide Caratti   tcp: ulp: add fun...
2050
2051
2052
  	/* diagnostic */
  	int (*get_info)(const struct sock *sk, struct sk_buff *skb);
  	size_t (*get_info_size)(const struct sock *sk);
132305930   Mat Martineau   tcp, ulp: Add clo...
2053
2054
2055
  	/* clone ulp */
  	void (*clone)(const struct request_sock *req, struct sock *newsk,
  		      const gfp_t priority);
734942cc4   Dave Watson   tcp: ULP infrastr...
2056
2057
2058
2059
2060
2061
2062
2063
2064
  
  	char		name[TCP_ULP_NAME_MAX];
  	struct module	*owner;
  };
  int tcp_register_ulp(struct tcp_ulp_ops *type);
  void tcp_unregister_ulp(struct tcp_ulp_ops *type);
  int tcp_set_ulp(struct sock *sk, const char *name);
  void tcp_get_available_ulp(char *buf, size_t len);
  void tcp_cleanup_ulp(struct sock *sk);
33bfe20dd   John Fastabend   bpf: Sockmap/tls,...
2065
2066
  void tcp_update_ulp(struct sock *sk, struct proto *p,
  		    void (*write_space)(struct sock *sk));
734942cc4   Dave Watson   tcp: ULP infrastr...
2067

037b0b86e   Daniel Borkmann   tcp, ulp: add ali...
2068
2069
2070
  #define MODULE_ALIAS_TCP_ULP(name)				\
  	__MODULE_INFO(alias, alias_userspace, name);		\
  	__MODULE_INFO(alias, alias_tcp_ulp, "tcp-ulp-" name)
604326b41   Daniel Borkmann   bpf, sockmap: con...
2071
2072
  struct sk_msg;
  struct sk_psock;
f747632b6   Lorenz Bauer   bpf: sockmap: Mov...
2073
2074
2075
2076
2077
2078
2079
2080
  #ifdef CONFIG_BPF_STREAM_PARSER
  struct proto *tcp_bpf_get_proto(struct sock *sk, struct sk_psock *psock);
  void tcp_bpf_clone(const struct sock *sk, struct sock *newsk);
  #else
  static inline void tcp_bpf_clone(const struct sock *sk, struct sock *newsk)
  {
  }
  #endif /* CONFIG_BPF_STREAM_PARSER */
5da004044   Lorenz Bauer   bpf: tcp: Guard d...
2081
  #ifdef CONFIG_NET_SOCK_MSG
604326b41   Daniel Borkmann   bpf, sockmap: con...
2082
2083
  int tcp_bpf_sendmsg_redir(struct sock *sk, struct sk_msg *msg, u32 bytes,
  			  int flags);
604326b41   Daniel Borkmann   bpf, sockmap: con...
2084
  int __tcp_bpf_recvmsg(struct sock *sk, struct sk_psock *psock,
02c558b2d   John Fastabend   bpf: sockmap, sup...
2085
  		      struct msghdr *msg, int len, int flags);
5da004044   Lorenz Bauer   bpf: tcp: Guard d...
2086
  #endif /* CONFIG_NET_SOCK_MSG */
604326b41   Daniel Borkmann   bpf, sockmap: con...
2087

0813a8415   Martin KaFai Lau   bpf: tcp: Allow b...
2088
  #ifdef CONFIG_CGROUP_BPF
0813a8415   Martin KaFai Lau   bpf: tcp: Allow b...
2089
2090
2091
2092
2093
2094
2095
2096
  static inline void bpf_skops_init_skb(struct bpf_sock_ops_kern *skops,
  				      struct sk_buff *skb,
  				      unsigned int end_offset)
  {
  	skops->skb = skb;
  	skops->skb_data_end = skb->data + end_offset;
  }
  #else
0813a8415   Martin KaFai Lau   bpf: tcp: Allow b...
2097
2098
2099
2100
2101
2102
  static inline void bpf_skops_init_skb(struct bpf_sock_ops_kern *skops,
  				      struct sk_buff *skb,
  				      unsigned int end_offset)
  {
  }
  #endif
40304b2a1   Lawrence Brakmo   bpf: BPF support ...
2103
2104
2105
2106
2107
2108
  /* Call BPF_SOCK_OPS program that returns an int. If the return value
   * is < 0, then the BPF op failed (for example if the loaded BPF
   * program does not support the chosen operation or there is no BPF
   * program loaded).
   */
  #ifdef CONFIG_BPF
de525be2c   Lawrence Brakmo   bpf: Support pass...
2109
  static inline int tcp_call_bpf(struct sock *sk, int op, u32 nargs, u32 *args)
40304b2a1   Lawrence Brakmo   bpf: BPF support ...
2110
2111
2112
  {
  	struct bpf_sock_ops_kern sock_ops;
  	int ret;
b73042b8a   Lawrence Brakmo   bpf: Add write ac...
2113
  	memset(&sock_ops, 0, offsetof(struct bpf_sock_ops_kern, temp));
f19397a5c   Lawrence Brakmo   bpf: Add access t...
2114
2115
  	if (sk_fullsock(sk)) {
  		sock_ops.is_fullsock = 1;
40304b2a1   Lawrence Brakmo   bpf: BPF support ...
2116
  		sock_owned_by_me(sk);
f19397a5c   Lawrence Brakmo   bpf: Add access t...
2117
  	}
40304b2a1   Lawrence Brakmo   bpf: BPF support ...
2118

40304b2a1   Lawrence Brakmo   bpf: BPF support ...
2119
2120
  	sock_ops.sk = sk;
  	sock_ops.op = op;
de525be2c   Lawrence Brakmo   bpf: Support pass...
2121
2122
  	if (nargs > 0)
  		memcpy(sock_ops.args, args, nargs * sizeof(*args));
40304b2a1   Lawrence Brakmo   bpf: BPF support ...
2123
2124
2125
2126
2127
2128
2129
2130
  
  	ret = BPF_CGROUP_RUN_PROG_SOCK_OPS(&sock_ops);
  	if (ret == 0)
  		ret = sock_ops.reply;
  	else
  		ret = -1;
  	return ret;
  }
de525be2c   Lawrence Brakmo   bpf: Support pass...
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
  
  static inline int tcp_call_bpf_2arg(struct sock *sk, int op, u32 arg1, u32 arg2)
  {
  	u32 args[2] = {arg1, arg2};
  
  	return tcp_call_bpf(sk, op, 2, args);
  }
  
  static inline int tcp_call_bpf_3arg(struct sock *sk, int op, u32 arg1, u32 arg2,
  				    u32 arg3)
  {
  	u32 args[3] = {arg1, arg2, arg3};
  
  	return tcp_call_bpf(sk, op, 3, args);
  }
40304b2a1   Lawrence Brakmo   bpf: BPF support ...
2146
  #else
de525be2c   Lawrence Brakmo   bpf: Support pass...
2147
  static inline int tcp_call_bpf(struct sock *sk, int op, u32 nargs, u32 *args)
40304b2a1   Lawrence Brakmo   bpf: BPF support ...
2148
2149
2150
  {
  	return -EPERM;
  }
de525be2c   Lawrence Brakmo   bpf: Support pass...
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
  
  static inline int tcp_call_bpf_2arg(struct sock *sk, int op, u32 arg1, u32 arg2)
  {
  	return -EPERM;
  }
  
  static inline int tcp_call_bpf_3arg(struct sock *sk, int op, u32 arg1, u32 arg2,
  				    u32 arg3)
  {
  	return -EPERM;
  }
40304b2a1   Lawrence Brakmo   bpf: BPF support ...
2162
  #endif
8550f328f   Lawrence Brakmo   bpf: Support for ...
2163
2164
2165
  static inline u32 tcp_timeout_init(struct sock *sk)
  {
  	int timeout;
de525be2c   Lawrence Brakmo   bpf: Support pass...
2166
  	timeout = tcp_call_bpf(sk, BPF_SOCK_OPS_TIMEOUT_INIT, 0, NULL);
8550f328f   Lawrence Brakmo   bpf: Support for ...
2167
2168
2169
2170
2171
  
  	if (timeout <= 0)
  		timeout = TCP_TIMEOUT_INIT;
  	return timeout;
  }
13d3b1ebe   Lawrence Brakmo   bpf: Support for ...
2172
2173
2174
  static inline u32 tcp_rwnd_init_bpf(struct sock *sk)
  {
  	int rwnd;
de525be2c   Lawrence Brakmo   bpf: Support pass...
2175
  	rwnd = tcp_call_bpf(sk, BPF_SOCK_OPS_RWND_INIT, 0, NULL);
13d3b1ebe   Lawrence Brakmo   bpf: Support for ...
2176
2177
2178
2179
2180
  
  	if (rwnd < 0)
  		rwnd = 0;
  	return rwnd;
  }
91b5b21c7   Lawrence Brakmo   bpf: Add support ...
2181
2182
2183
  
  static inline bool tcp_bpf_ca_needs_ecn(struct sock *sk)
  {
de525be2c   Lawrence Brakmo   bpf: Support pass...
2184
  	return (tcp_call_bpf(sk, BPF_SOCK_OPS_NEEDS_ECN, 0, NULL) == 1);
91b5b21c7   Lawrence Brakmo   bpf: Add support ...
2185
  }
60e2a7780   Ursula Braun   tcp: TCP experime...
2186

23729ff23   Stanislav Fomichev   bpf: add BPF_CGRO...
2187
2188
  static inline void tcp_bpf_rtt(struct sock *sk)
  {
bef8e2639   Arnd Bergmann   bpf: avoid unused...
2189
  	if (BPF_SOCK_OPS_TEST_FLAG(tcp_sk(sk), BPF_SOCK_OPS_RTT_CB_FLAG))
23729ff23   Stanislav Fomichev   bpf: add BPF_CGRO...
2190
2191
  		tcp_call_bpf(sk, BPF_SOCK_OPS_RTT_CB, 0, NULL);
  }
60e2a7780   Ursula Braun   tcp: TCP experime...
2192
2193
2194
  #if IS_ENABLED(CONFIG_SMC)
  extern struct static_key_false tcp_have_smc;
  #endif
6dac15235   Ilya Lesokhin   tcp: Add clean ac...
2195
2196
2197
2198
2199
  
  #if IS_ENABLED(CONFIG_TLS_DEVICE)
  void clean_acked_data_enable(struct inet_connection_sock *icsk,
  			     void (*cad)(struct sock *sk, u32 ack_seq));
  void clean_acked_data_disable(struct inet_connection_sock *icsk);
494bc1d28   Jakub Kicinski   net/tcp: use defe...
2200
  void clean_acked_data_flush(void);
6dac15235   Ilya Lesokhin   tcp: Add clean ac...
2201
  #endif
a842fe142   Eric Dumazet   tcp: add optional...
2202
2203
2204
2205
2206
2207
2208
  DECLARE_STATIC_KEY_FALSE(tcp_tx_delay_enabled);
  static inline void tcp_add_tx_delay(struct sk_buff *skb,
  				    const struct tcp_sock *tp)
  {
  	if (static_branch_unlikely(&tcp_tx_delay_enabled))
  		skb->skb_mstamp_ns += (u64)tp->tcp_tx_delay * NSEC_PER_USEC;
  }
d6fb396cf   Eric Dumazet   ipv4: tcp: fix AC...
2209
2210
2211
2212
  /* Compute Earliest Departure Time for some control packets
   * like ACK or RST for TIME_WAIT or non ESTABLISHED sockets.
   */
  static inline u64 tcp_transmit_time(const struct sock *sk)
a842fe142   Eric Dumazet   tcp: add optional...
2213
2214
2215
2216
  {
  	if (static_branch_unlikely(&tcp_tx_delay_enabled)) {
  		u32 delay = (sk->sk_state == TCP_TIME_WAIT) ?
  			tcp_twsk(sk)->tw_tx_delay : tcp_sk(sk)->tcp_tx_delay;
d6fb396cf   Eric Dumazet   ipv4: tcp: fix AC...
2217
  		return tcp_clock_ns() + (u64)delay * NSEC_PER_USEC;
a842fe142   Eric Dumazet   tcp: add optional...
2218
  	}
d6fb396cf   Eric Dumazet   ipv4: tcp: fix AC...
2219
  	return 0;
a842fe142   Eric Dumazet   tcp: add optional...
2220
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2221
  #endif	/* _TCP_H */