1
2
3
4
5
6
7
8
9
10
11
12
13#ifndef _LINUX_TCP_H
14#define _LINUX_TCP_H
15
16
17#include <linux/skbuff.h>
18#include <linux/win_minmax.h>
19#include <net/sock.h>
20#include <net/inet_connection_sock.h>
21#include <net/inet_timewait_sock.h>
22#include <uapi/linux/tcp.h>
23
24static inline struct tcphdr *tcp_hdr(const struct sk_buff *skb)
25{
26 return (struct tcphdr *)skb_transport_header(skb);
27}
28
29static inline unsigned int __tcp_hdrlen(const struct tcphdr *th)
30{
31 return th->doff * 4;
32}
33
34static inline unsigned int tcp_hdrlen(const struct sk_buff *skb)
35{
36 return __tcp_hdrlen(tcp_hdr(skb));
37}
38
39static inline struct tcphdr *inner_tcp_hdr(const struct sk_buff *skb)
40{
41 return (struct tcphdr *)skb_inner_transport_header(skb);
42}
43
44static inline unsigned int inner_tcp_hdrlen(const struct sk_buff *skb)
45{
46 return inner_tcp_hdr(skb)->doff * 4;
47}
48
49static inline unsigned int tcp_optlen(const struct sk_buff *skb)
50{
51 return (tcp_hdr(skb)->doff - 5) * 4;
52}
53
54
55#define TCP_FASTOPEN_COOKIE_MIN 4
56#define TCP_FASTOPEN_COOKIE_MAX 16
57#define TCP_FASTOPEN_COOKIE_SIZE 8
58
59
60struct tcp_fastopen_cookie {
61 __le64 val[DIV_ROUND_UP(TCP_FASTOPEN_COOKIE_MAX, sizeof(u64))];
62 s8 len;
63 bool exp;
64};
65
66
67struct tcp_sack_block_wire {
68 __be32 start_seq;
69 __be32 end_seq;
70};
71
72struct tcp_sack_block {
73 u32 start_seq;
74 u32 end_seq;
75};
76
77
78#define TCP_SACK_SEEN (1 << 0)
79#define TCP_DSACK_SEEN (1 << 2)
80
81struct tcp_options_received {
82
83 int ts_recent_stamp;
84 u32 ts_recent;
85 u32 rcv_tsval;
86 u32 rcv_tsecr;
87 u16 saw_tstamp : 1,
88 tstamp_ok : 1,
89 dsack : 1,
90 wscale_ok : 1,
91 sack_ok : 3,
92 smc_ok : 1,
93 snd_wscale : 4,
94 rcv_wscale : 4;
95 u8 saw_unknown:1,
96 unused:7;
97 u8 num_sacks;
98 u16 user_mss;
99 u16 mss_clamp;
100};
101
102static inline void tcp_clear_options(struct tcp_options_received *rx_opt)
103{
104 rx_opt->tstamp_ok = rx_opt->sack_ok = 0;
105 rx_opt->wscale_ok = rx_opt->snd_wscale = 0;
106#if IS_ENABLED(CONFIG_SMC)
107 rx_opt->smc_ok = 0;
108#endif
109}
110
111
112
113
114
115#define TCP_NUM_SACKS 4
116
117struct tcp_request_sock_ops;
118
119struct tcp_request_sock {
120 struct inet_request_sock req;
121 const struct tcp_request_sock_ops *af_specific;
122 u64 snt_synack;
123 bool tfo_listener;
124 bool is_mptcp;
125#if IS_ENABLED(CONFIG_MPTCP)
126 bool drop_req;
127#endif
128 u32 txhash;
129 u32 rcv_isn;
130 u32 snt_isn;
131 u32 ts_off;
132 u32 last_oow_ack_time;
133 u32 rcv_nxt;
134
135
136
137 u8 syn_tos;
138};
139
140static inline struct tcp_request_sock *tcp_rsk(const struct request_sock *req)
141{
142 return (struct tcp_request_sock *)req;
143}
144
145struct tcp_sock {
146
147 struct inet_connection_sock inet_conn;
148 u16 tcp_header_len;
149 u16 gso_segs;
150
151
152
153
154
155 __be32 pred_flags;
156
157
158
159
160
161
162 u64 bytes_received;
163
164
165
166 u32 segs_in;
167
168
169 u32 data_segs_in;
170
171
172 u32 rcv_nxt;
173 u32 copied_seq;
174 u32 rcv_wup;
175 u32 snd_nxt;
176 u32 segs_out;
177
178
179 u32 data_segs_out;
180
181
182 u64 bytes_sent;
183
184
185 u64 bytes_acked;
186
187
188
189 u32 dsack_dups;
190
191
192 u32 snd_una;
193 u32 snd_sml;
194 u32 rcv_tstamp;
195 u32 lsndtime;
196 u32 last_oow_ack_time;
197 u32 compressed_ack_rcv_nxt;
198
199 u32 tsoffset;
200
201 struct list_head tsq_node;
202 struct list_head tsorted_sent_queue;
203
204 u32 snd_wl1;
205 u32 snd_wnd;
206 u32 max_window;
207 u32 mss_cache;
208
209 u32 window_clamp;
210 u32 rcv_ssthresh;
211
212
213 struct tcp_rack {
214 u64 mstamp;
215 u32 rtt_us;
216 u32 end_seq;
217 u32 last_delivered;
218 u8 reo_wnd_steps;
219#define TCP_RACK_RECOVERY_THRESH 16
220 u8 reo_wnd_persist:5,
221 dsack_seen:1,
222 advanced:1;
223 } rack;
224 u16 advmss;
225 u8 compressed_ack;
226 u8 dup_ack_counter:2,
227 tlp_retrans:1,
228 unused:5;
229 u32 chrono_start;
230 u32 chrono_stat[3];
231 u8 chrono_type:2,
232 rate_app_limited:1,
233 fastopen_connect:1,
234 fastopen_no_cookie:1,
235 is_sack_reneg:1,
236 fastopen_client_fail:2;
237 u8 nonagle : 4,
238 thin_lto : 1,
239 recvmsg_inq : 1,
240 repair : 1,
241 frto : 1;
242 u8 repair_queue;
243 u8 save_syn:2,
244 syn_data:1,
245 syn_fastopen:1,
246 syn_fastopen_exp:1,
247 syn_fastopen_ch:1,
248 syn_data_acked:1,
249 is_cwnd_limited:1;
250 u32 tlp_high_seq;
251
252 u32 tcp_tx_delay;
253 u64 tcp_wstamp_ns;
254 u64 tcp_clock_cache;
255
256
257 u64 tcp_mstamp;
258 u32 srtt_us;
259 u32 mdev_us;
260 u32 mdev_max_us;
261 u32 rttvar_us;
262 u32 rtt_seq;
263 struct minmax rtt_min;
264
265 u32 packets_out;
266 u32 retrans_out;
267 u32 max_packets_out;
268 u32 max_packets_seq;
269
270 u16 urg_data;
271 u8 ecn_flags;
272 u8 keepalive_probes;
273 u32 reordering;
274 u32 reord_seen;
275 u32 snd_up;
276
277
278
279
280 struct tcp_options_received rx_opt;
281
282
283
284
285 u32 snd_ssthresh;
286 u32 snd_cwnd;
287 u32 snd_cwnd_cnt;
288 u32 snd_cwnd_clamp;
289 u32 snd_cwnd_used;
290 u32 snd_cwnd_stamp;
291 u32 prior_cwnd;
292 u32 prr_delivered;
293
294 u32 prr_out;
295 u32 delivered;
296 u32 delivered_ce;
297 u32 lost;
298 u32 app_limited;
299 u64 first_tx_mstamp;
300 u64 delivered_mstamp;
301 u32 rate_delivered;
302 u32 rate_interval_us;
303
304 u32 rcv_wnd;
305 u32 write_seq;
306 u32 notsent_lowat;
307 u32 pushed_seq;
308 u32 lost_out;
309 u32 sacked_out;
310
311 struct hrtimer pacing_timer;
312 struct hrtimer compressed_ack_timer;
313
314
315 struct sk_buff* lost_skb_hint;
316 struct sk_buff *retransmit_skb_hint;
317
318
319 struct rb_root out_of_order_queue;
320 struct sk_buff *ooo_last_skb;
321
322
323 struct tcp_sack_block duplicate_sack[1];
324 struct tcp_sack_block selective_acks[4];
325
326 struct tcp_sack_block recv_sack_cache[4];
327
328 struct sk_buff *highest_sack;
329
330
331
332
333
334 int lost_cnt_hint;
335
336 u32 prior_ssthresh;
337 u32 high_seq;
338
339 u32 retrans_stamp;
340
341
342 u32 undo_marker;
343 int undo_retrans;
344 u64 bytes_retrans;
345
346
347 u32 total_retrans;
348
349 u32 urg_seq;
350 unsigned int keepalive_time;
351 unsigned int keepalive_intvl;
352
353 int linger2;
354
355
356
357#ifdef CONFIG_BPF
358 u8 bpf_sock_ops_cb_flags;
359
360
361#define BPF_SOCK_OPS_TEST_FLAG(TP, ARG) (TP->bpf_sock_ops_cb_flags & ARG)
362#else
363#define BPF_SOCK_OPS_TEST_FLAG(TP, ARG) 0
364#endif
365
366 u16 timeout_rehash;
367
368 u32 rcv_ooopack;
369
370
371 u32 rcv_rtt_last_tsecr;
372 struct {
373 u32 rtt_us;
374 u32 seq;
375 u64 time;
376 } rcv_rtt_est;
377
378
379 struct {
380 u32 space;
381 u32 seq;
382 u64 time;
383 } rcvq_space;
384
385
386 struct {
387 u32 probe_seq_start;
388 u32 probe_seq_end;
389 } mtu_probe;
390 u32 mtu_info;
391
392
393#if IS_ENABLED(CONFIG_MPTCP)
394 bool is_mptcp;
395#endif
396#if IS_ENABLED(CONFIG_SMC)
397 bool (*smc_hs_congested)(const struct sock *sk);
398 bool syn_smc;
399#endif
400
401#ifdef CONFIG_TCP_MD5SIG
402
403 const struct tcp_sock_af_ops *af_specific;
404
405
406 struct tcp_md5sig_info __rcu *md5sig_info;
407#endif
408
409
410 struct tcp_fastopen_request *fastopen_req;
411
412
413
414 struct request_sock __rcu *fastopen_rsk;
415 struct saved_syn *saved_syn;
416};
417
418enum tsq_enum {
419 TSQ_THROTTLED,
420 TSQ_QUEUED,
421 TCP_TSQ_DEFERRED,
422 TCP_WRITE_TIMER_DEFERRED,
423 TCP_DELACK_TIMER_DEFERRED,
424 TCP_MTU_REDUCED_DEFERRED,
425
426
427};
428
429enum tsq_flags {
430 TSQF_THROTTLED = (1UL << TSQ_THROTTLED),
431 TSQF_QUEUED = (1UL << TSQ_QUEUED),
432 TCPF_TSQ_DEFERRED = (1UL << TCP_TSQ_DEFERRED),
433 TCPF_WRITE_TIMER_DEFERRED = (1UL << TCP_WRITE_TIMER_DEFERRED),
434 TCPF_DELACK_TIMER_DEFERRED = (1UL << TCP_DELACK_TIMER_DEFERRED),
435 TCPF_MTU_REDUCED_DEFERRED = (1UL << TCP_MTU_REDUCED_DEFERRED),
436};
437
438static inline struct tcp_sock *tcp_sk(const struct sock *sk)
439{
440 return (struct tcp_sock *)sk;
441}
442
443struct tcp_timewait_sock {
444 struct inet_timewait_sock tw_sk;
445#define tw_rcv_nxt tw_sk.__tw_common.skc_tw_rcv_nxt
446#define tw_snd_nxt tw_sk.__tw_common.skc_tw_snd_nxt
447 u32 tw_rcv_wnd;
448 u32 tw_ts_offset;
449 u32 tw_ts_recent;
450
451
452 u32 tw_last_oow_ack_time;
453
454 int tw_ts_recent_stamp;
455 u32 tw_tx_delay;
456#ifdef CONFIG_TCP_MD5SIG
457 struct tcp_md5sig_key *tw_md5_key;
458#endif
459};
460
461static inline struct tcp_timewait_sock *tcp_twsk(const struct sock *sk)
462{
463 return (struct tcp_timewait_sock *)sk;
464}
465
466static inline bool tcp_passive_fastopen(const struct sock *sk)
467{
468 return sk->sk_state == TCP_SYN_RECV &&
469 rcu_access_pointer(tcp_sk(sk)->fastopen_rsk) != NULL;
470}
471
472static inline void fastopen_queue_tune(struct sock *sk, int backlog)
473{
474 struct request_sock_queue *queue = &inet_csk(sk)->icsk_accept_queue;
475 int somaxconn = READ_ONCE(sock_net(sk)->core.sysctl_somaxconn);
476
477 queue->fastopenq.max_qlen = min_t(unsigned int, backlog, somaxconn);
478}
479
480static inline void tcp_move_syn(struct tcp_sock *tp,
481 struct request_sock *req)
482{
483 tp->saved_syn = req->saved_syn;
484 req->saved_syn = NULL;
485}
486
487static inline void tcp_saved_syn_free(struct tcp_sock *tp)
488{
489 kfree(tp->saved_syn);
490 tp->saved_syn = NULL;
491}
492
493static inline u32 tcp_saved_syn_len(const struct saved_syn *saved_syn)
494{
495 return saved_syn->mac_hdrlen + saved_syn->network_hdrlen +
496 saved_syn->tcp_hdrlen;
497}
498
499struct sk_buff *tcp_get_timestamping_opt_stats(const struct sock *sk,
500 const struct sk_buff *orig_skb,
501 const struct sk_buff *ack_skb);
502
503static inline u16 tcp_mss_clamp(const struct tcp_sock *tp, u16 mss)
504{
505
506
507
508 u16 user_mss = READ_ONCE(tp->rx_opt.user_mss);
509
510 return (user_mss && user_mss < mss) ? user_mss : mss;
511}
512
513int tcp_skb_shift(struct sk_buff *to, struct sk_buff *from, int pcount,
514 int shiftlen);
515
516void __tcp_sock_set_cork(struct sock *sk, bool on);
517void tcp_sock_set_cork(struct sock *sk, bool on);
518int tcp_sock_set_keepcnt(struct sock *sk, int val);
519int tcp_sock_set_keepidle_locked(struct sock *sk, int val);
520int tcp_sock_set_keepidle(struct sock *sk, int val);
521int tcp_sock_set_keepintvl(struct sock *sk, int val);
522void __tcp_sock_set_nodelay(struct sock *sk, bool on);
523void tcp_sock_set_nodelay(struct sock *sk);
524void tcp_sock_set_quickack(struct sock *sk, int val);
525int tcp_sock_set_syncnt(struct sock *sk, int val);
526void tcp_sock_set_user_timeout(struct sock *sk, u32 val);
527
528#endif
529