21 #define FASTRETRANS_DEBUG 1
23 #include <linux/list.h>
24 #include <linux/tcp.h>
26 #include <linux/slab.h>
55 #define MAX_TCP_HEADER (128 + MAX_HEADER)
56 #define MAX_TCP_OPTION_SPACE 40
62 #define MAX_TCP_WINDOW 32767U
65 #define TCP_DEFAULT_INIT_RCVWND 10
68 #define TCP_MIN_MSS 88U
71 #define TCP_BASE_MSS 512
74 #define TCP_FASTRETRANS_THRESH 3
77 #define TCP_MAX_REORDERING 127
80 #define TCP_MAX_QUICKACKS 16U
83 #define TCP_URG_VALID 0x0100
84 #define TCP_URG_NOTYET 0x0200
85 #define TCP_URG_READ 0x0400
101 #define TCP_SYN_RETRIES 6
110 #define TCP_SYNACK_RETRIES 5
117 #define TCP_TIMEWAIT_LEN (60*HZ)
119 #define TCP_FIN_TIMEOUT TCP_TIMEWAIT_LEN
126 #define TCP_DELACK_MAX ((unsigned)(HZ/5))
128 #define TCP_DELACK_MIN ((unsigned)(HZ/25))
129 #define TCP_ATO_MIN ((unsigned)(HZ/25))
131 #define TCP_DELACK_MIN 4U
132 #define TCP_ATO_MIN 4U
134 #define TCP_RTO_MAX ((unsigned)(120*HZ))
135 #define TCP_RTO_MIN ((unsigned)(HZ/5))
136 #define TCP_TIMEOUT_INIT ((unsigned)(1*HZ))
137 #define TCP_TIMEOUT_FALLBACK ((unsigned)(3*HZ))
144 #define TCP_RESOURCE_PROBE_INTERVAL ((unsigned)(HZ/2U))
148 #define TCP_KEEPALIVE_TIME (120*60*HZ)
149 #define TCP_KEEPALIVE_PROBES 9
150 #define TCP_KEEPALIVE_INTVL (75*HZ)
152 #define MAX_TCP_KEEPIDLE 32767
153 #define MAX_TCP_KEEPINTVL 32767
154 #define MAX_TCP_KEEPCNT 127
155 #define MAX_TCP_SYNCNT 127
157 #define TCP_SYNQ_INTERVAL (HZ/5)
159 #define TCP_PAWS_24DAYS (60 * 60 * 24 * 24)
160 #define TCP_PAWS_MSL 60
166 #define TCP_PAWS_WINDOW 1
177 #define TCPOPT_WINDOW 3
178 #define TCPOPT_SACK_PERM 4
179 #define TCPOPT_SACK 5
180 #define TCPOPT_TIMESTAMP 8
181 #define TCPOPT_MD5SIG 19
182 #define TCPOPT_COOKIE 253
183 #define TCPOPT_EXP 254
187 #define TCPOPT_FASTOPEN_MAGIC 0xF989
193 #define TCPOLEN_MSS 4
194 #define TCPOLEN_WINDOW 3
195 #define TCPOLEN_SACK_PERM 2
196 #define TCPOLEN_TIMESTAMP 10
197 #define TCPOLEN_MD5SIG 18
198 #define TCPOLEN_EXP_FASTOPEN_BASE 4
199 #define TCPOLEN_COOKIE_BASE 2
200 #define TCPOLEN_COOKIE_PAIR 3
201 #define TCPOLEN_COOKIE_MIN (TCPOLEN_COOKIE_BASE+TCP_COOKIE_MIN)
202 #define TCPOLEN_COOKIE_MAX (TCPOLEN_COOKIE_BASE+TCP_COOKIE_MAX)
205 #define TCPOLEN_TSTAMP_ALIGNED 12
206 #define TCPOLEN_WSCALE_ALIGNED 4
207 #define TCPOLEN_SACKPERM_ALIGNED 4
208 #define TCPOLEN_SACK_BASE 2
209 #define TCPOLEN_SACK_BASE_ALIGNED 4
210 #define TCPOLEN_SACK_PERBLOCK 8
211 #define TCPOLEN_MD5SIG_ALIGNED 20
212 #define TCPOLEN_MSS_ALIGNED 4
215 #define TCP_NAGLE_OFF 1
216 #define TCP_NAGLE_CORK 2
217 #define TCP_NAGLE_PUSH 4
220 #define TCP_THIN_LINEAR_RETRIES 6
223 #define TCP_INIT_CWND 10
226 #define TFO_CLIENT_ENABLE 1
227 #define TFO_SERVER_ENABLE 2
228 #define TFO_CLIENT_NO_COOKIE 4
231 #define TFO_SERVER_COOKIE_NOT_CHKED 0x100
233 #define TFO_SERVER_COOKIE_NOT_REQD 0x200
238 #define TFO_SERVER_WO_SOCKOPT1 0x400
239 #define TFO_SERVER_WO_SOCKOPT2 0x800
243 #define TFO_SERVER_ALWAYS 0x1000
305 static inline bool before(
__u32 seq1,
__u32 seq2)
307 return (
__s32)(seq1-seq2) < 0;
309 #define after(seq2, seq1) before(seq1, seq2)
314 return seq3 - seq2 >= seq1 - seq2;
317 static inline bool tcp_out_of_memory(
struct sock *
sk)
320 sk_memory_allocated(sk) > sk_prot_mem_limits(sk, 2))
325 static inline bool tcp_too_many_orphans(
struct sock *sk,
int shift)
328 int orphans = percpu_counter_read_positive(ocp);
331 orphans = percpu_counter_sum_positive(ocp);
341 static inline void tcp_synq_overflow(
struct sock *sk)
343 tcp_sk(sk)->rx_opt.ts_recent_stamp =
jiffies;
347 static inline bool tcp_synq_no_recent_overflow(
const struct sock *sk)
349 unsigned long last_overflow = tcp_sk(sk)->rx_opt.ts_recent_stamp;
355 #define TCP_INC_STATS(net, field) SNMP_INC_STATS((net)->mib.tcp_statistics, field)
356 #define TCP_INC_STATS_BH(net, field) SNMP_INC_STATS_BH((net)->mib.tcp_statistics, field)
357 #define TCP_DEC_STATS(net, field) SNMP_DEC_STATS((net)->mib.tcp_statistics, field)
358 #define TCP_ADD_STATS_USER(net, field, val) SNMP_ADD_STATS_USER((net)->mib.tcp_statistics, field, val)
359 #define TCP_ADD_STATS(net, field, val) SNMP_ADD_STATS((net)->mib.tcp_statistics, field, val)
383 const struct tcphdr *
th,
unsigned int len);
385 const struct tcphdr *
th,
unsigned int len);
394 static inline void tcp_dec_quickack_mode(
struct sock *sk,
395 const unsigned int pkts)
410 #define TCP_ECN_QUEUE_CWR 2
411 #define TCP_ECN_DEMAND_CWR 4
412 #define TCP_ECN_SEEN 8
448 char __user *optval,
int __user *optlen);
450 char __user *optval,
unsigned int optlen);
452 char __user *optval,
int __user *optlen);
454 char __user *optval,
unsigned int optlen);
458 size_t len,
int nonblock,
int flags,
int *addr_len);
495 #ifdef CONFIG_SYN_COOKIES
512 #ifdef CONFIG_SYN_COOKIES
557 static inline void tcp_clear_xmit_timers(
struct sock *sk)
566 static inline int tcp_bound_to_half_wnd(
struct tcp_sock *tp,
int pktsize)
582 if (cutoff && pktsize > cutoff)
605 static inline void tcp_bound_rto(
const struct sock *sk)
611 static inline u32 __tcp_set_rto(
const struct tcp_sock *tp)
618 static inline void __tcp_fast_path_on(
struct tcp_sock *tp,
u32 snd_wnd)
625 static inline void tcp_fast_path_on(
struct tcp_sock *tp)
627 __tcp_fast_path_on(tp, tp->
snd_wnd >> tp->
rx_opt.snd_wscale);
630 static inline void tcp_fast_path_check(
struct sock *sk)
638 tcp_fast_path_on(tp);
642 static inline u32 tcp_rto_min(
struct sock *sk)
656 static inline u32 tcp_receive_window(
const struct tcp_sock *tp)
679 #define tcp_time_stamp ((__u32)(jiffies))
681 #define tcp_flag_byte(th) (((u_int8_t *)th)[13])
683 #define TCPHDR_FIN 0x01
684 #define TCPHDR_SYN 0x02
685 #define TCPHDR_RST 0x04
686 #define TCPHDR_PSH 0x08
687 #define TCPHDR_ACK 0x10
688 #define TCPHDR_URG 0x20
689 #define TCPHDR_ECE 0x40
690 #define TCPHDR_CWR 0x80
701 #if IS_ENABLED(CONFIG_IPV6)
711 #define TCPCB_SACKED_ACKED 0x01
712 #define TCPCB_SACKED_RETRANS 0x02
713 #define TCPCB_LOST 0x04
714 #define TCPCB_TAGBITS 0x07
715 #define TCPCB_EVER_RETRANS 0x80
716 #define TCPCB_RETRANS (TCPCB_SACKED_RETRANS|TCPCB_EVER_RETRANS)
723 #define TCP_SKB_CB(__skb) ((struct tcp_skb_cb *)&((__skb)->cb[0]))
734 const struct tcphdr *
th = tcp_hdr(skb);
736 if (sysctl_tcp_ecn && th->ece && th->cwr &&
737 INET_ECN_is_not_ect(
TCP_SKB_CB(skb)->ip_dsfield))
738 inet_rsk(req)->ecn_ok = 1;
744 static inline int tcp_skb_pcount(
const struct sk_buff *skb)
746 return skb_shinfo(skb)->gso_segs;
750 static inline int tcp_skb_mss(
const struct sk_buff *skb)
752 return skb_shinfo(skb)->gso_size;
769 #define TCP_CA_NAME_MAX 16
770 #define TCP_CA_MAX 128
771 #define TCP_CA_BUF_MAX (TCP_CA_NAME_MAX*TCP_CA_MAX)
773 #define TCP_CONG_NON_RESTRICTED 0x1
774 #define TCP_CONG_RTT_STAMP 0x2
826 static inline void tcp_set_ca_state(
struct sock *sk,
const u8 ca_state)
851 static inline int tcp_is_sack(
const struct tcp_sock *tp)
853 return tp->
rx_opt.sack_ok;
856 static inline bool tcp_is_reno(
const struct tcp_sock *tp)
858 return !tcp_is_sack(tp);
861 static inline bool tcp_is_fack(
const struct tcp_sock *tp)
866 static inline void tcp_enable_fack(
struct tcp_sock *tp)
874 static inline void tcp_enable_early_retrans(
struct tcp_sock *tp)
881 static inline void tcp_disable_early_retrans(
struct tcp_sock *tp)
886 static inline unsigned int tcp_left_out(
const struct tcp_sock *tp)
905 static inline unsigned int tcp_packets_in_flight(
const struct tcp_sock *tp)
910 #define TCP_INFINITE_SSTHRESH 0x7fffffff
912 static inline bool tcp_in_initial_slowstart(
const struct tcp_sock *tp)
917 static inline bool tcp_in_cwnd_reduction(
const struct sock *sk)
920 (1 << inet_csk(sk)->icsk_ca_state);
927 static inline __u32 tcp_current_ssthresh(
const struct sock *sk)
929 const struct tcp_sock *tp = tcp_sk(sk);
931 if (tcp_in_cwnd_reduction(sk))
940 #define tcp_verify_left_out(tp) WARN_ON(tcp_left_out(tp) > tp->packets_out)
948 static inline __u32 tcp_max_tso_deferred_mss(
const struct tcp_sock *tp)
965 static inline u32 tcp_wnd_end(
const struct tcp_sock *tp)
971 static inline void tcp_minshall_update(
struct tcp_sock *tp,
unsigned int mss,
978 static inline void tcp_check_probe_timer(
struct sock *sk)
980 const struct tcp_sock *tp = tcp_sk(sk);
988 static inline void tcp_init_wl(
struct tcp_sock *tp,
u32 seq)
993 static inline void tcp_update_wl(
struct tcp_sock *tp,
u32 seq)
1007 static inline __sum16 __tcp_checksum_complete(
struct sk_buff *skb)
1012 static inline bool tcp_checksum_complete(
struct sk_buff *skb)
1014 return !skb_csum_unnecessary(skb) &&
1015 __tcp_checksum_complete(skb);
1020 static inline void tcp_prequeue_init(
struct tcp_sock *tp)
1024 tp->
ucopy.memory = 0;
1025 skb_queue_head_init(&tp->
ucopy.prequeue);
1026 #ifdef CONFIG_NET_DMA
1028 tp->
ucopy.wakeup = 0;
1030 tp->
ucopy.dma_cookie = 0;
1042 static inline bool tcp_prequeue(
struct sock *sk,
struct sk_buff *skb)
1046 if (sysctl_tcp_low_latency || !tp->
ucopy.task)
1049 __skb_queue_tail(&tp->
ucopy.prequeue, skb);
1056 while ((skb1 = __skb_dequeue(&tp->
ucopy.prequeue)) !=
NULL) {
1057 sk_backlog_rcv(sk, skb1);
1062 tp->
ucopy.memory = 0;
1063 }
else if (skb_queue_len(&tp->
ucopy.prequeue) == 1) {
1066 if (!inet_csk_ack_scheduled(sk))
1068 (3 * tcp_rto_min(sk)) / 4,
1078 static const char *statename[]={
1079 "Unused",
"Established",
"Syn Sent",
"Syn Recv",
1080 "Fin Wait 1",
"Fin Wait 2",
"Time Wait",
"Close",
1081 "Close Wait",
"Last ACK",
"Listen",
"Closing"
1097 int wscale_ok,
__u8 *rcv_wscale,
1098 __u32 init_rcv_wnd);
1100 static inline int tcp_win_from_space(
int space)
1108 static inline int tcp_space(
const struct sock *sk)
1110 return tcp_win_from_space(sk->
sk_rcvbuf -
1114 static inline int tcp_full_space(
const struct sock *sk)
1116 return tcp_win_from_space(sk->
sk_rcvbuf);
1119 static inline void tcp_openreq_init(
struct request_sock *req,
1127 tcp_rsk(req)->rcv_isn =
TCP_SKB_CB(skb)->seq;
1128 tcp_rsk(req)->rcv_nxt =
TCP_SKB_CB(skb)->seq + 1;
1129 tcp_rsk(req)->snt_synack = 0;
1138 ireq->
rmt_port = tcp_hdr(skb)->source;
1139 ireq->
loc_port = tcp_hdr(skb)->dest;
1143 static inline void tcp_synack_rtt_meas(
struct sock *sk,
1146 if (tcp_rsk(req)->snt_synack)
1153 static inline int keepalive_intvl_when(
const struct tcp_sock *tp)
1158 static inline int keepalive_time_when(
const struct tcp_sock *tp)
1163 static inline int keepalive_probes(
const struct tcp_sock *tp)
1168 static inline u32 keepalive_time_elapsed(
const struct tcp_sock *tp)
1176 static inline int tcp_fin_time(
const struct sock *sk)
1179 const int rto = inet_csk(sk)->icsk_rto;
1181 if (fin_timeout < (rto << 2) - (rto >> 1))
1182 fin_timeout = (rto << 2) - (rto >> 1);
1207 if (tcp_paws_check(rx_opt, 0))
1227 static inline void tcp_mib_init(
struct net *
net)
1237 static inline void tcp_clear_retrans_hints_partial(
struct tcp_sock *tp)
1243 static inline void tcp_clear_all_retrans_hints(
struct tcp_sock *tp)
1245 tcp_clear_retrans_hints_partial(tp);
1254 #if IS_ENABLED(CONFIG_IPV6)
1293 #if IS_ENABLED(CONFIG_IPV6)
1306 const struct sock *sk,
1317 #ifdef CONFIG_TCP_MD5SIG
1320 #define tcp_twsk_md5_key(twsk) ((twsk)->tw_md5_key)
1328 #define tcp_twsk_md5_key(twsk) NULL
1346 int *syn_loss,
unsigned long *last_syn_loss);
1362 #define TCP_FASTOPEN_KEY_LENGTH 16
1372 static inline void tcp_write_queue_purge(
struct sock *sk)
1377 sk_wmem_free_skb(sk, skb);
1379 tcp_clear_all_retrans_hints(tcp_sk(sk));
1382 static inline struct sk_buff *tcp_write_queue_head(
const struct sock *sk)
1387 static inline struct sk_buff *tcp_write_queue_tail(
const struct sock *sk)
1392 static inline struct sk_buff *tcp_write_queue_next(
const struct sock *sk,
1398 static inline struct sk_buff *tcp_write_queue_prev(
const struct sock *sk,
1404 #define tcp_for_write_queue(skb, sk) \
1405 skb_queue_walk(&(sk)->sk_write_queue, skb)
1407 #define tcp_for_write_queue_from(skb, sk) \
1408 skb_queue_walk_from(&(sk)->sk_write_queue, skb)
1410 #define tcp_for_write_queue_from_safe(skb, tmp, sk) \
1411 skb_queue_walk_from_safe(&(sk)->sk_write_queue, skb, tmp)
1413 static inline struct sk_buff *tcp_send_head(
const struct sock *sk)
1418 static inline bool tcp_skb_is_last(
const struct sock *sk,
1424 static inline void tcp_advance_send_head(
struct sock *sk,
const struct sk_buff *skb)
1426 if (tcp_skb_is_last(sk, skb))
1432 static inline void tcp_check_send_head(
struct sock *sk,
struct sk_buff *skb_unlinked)
1438 static inline void tcp_init_send_head(
struct sock *sk)
1443 static inline void __tcp_add_write_queue_tail(
struct sock *sk,
struct sk_buff *skb)
1448 static inline void tcp_add_write_queue_tail(
struct sock *sk,
struct sk_buff *skb)
1450 __tcp_add_write_queue_tail(sk, skb);
1456 if (tcp_sk(sk)->highest_sack ==
NULL)
1457 tcp_sk(sk)->highest_sack =
skb;
1461 static inline void __tcp_add_write_queue_head(
struct sock *sk,
struct sk_buff *skb)
1467 static inline void tcp_insert_write_queue_after(
struct sk_buff *skb,
1475 static inline void tcp_insert_write_queue_before(
struct sk_buff *
new,
1485 static inline void tcp_unlink_write_queue(
struct sk_buff *skb,
struct sock *sk)
1490 static inline bool tcp_write_queue_empty(
struct sock *sk)
1495 static inline void tcp_push_pending_frames(
struct sock *sk)
1497 if (tcp_send_head(sk)) {
1508 static inline u32 tcp_highest_sack_seq(
struct tcp_sock *tp)
1519 static inline void tcp_advance_highest_sack(
struct sock *sk,
struct sk_buff *skb)
1521 tcp_sk(sk)->highest_sack = tcp_skb_is_last(sk, skb) ?
NULL :
1522 tcp_write_queue_next(sk, skb);
1527 return tcp_sk(sk)->highest_sack;
1530 static inline void tcp_highest_sack_reset(
struct sock *sk)
1532 tcp_sk(sk)->highest_sack = tcp_write_queue_head(sk);
1536 static inline void tcp_highest_sack_combine(
struct sock *sk,
1540 if (tcp_sk(sk)->sacked_out && (old == tcp_sk(sk)->highest_sack))
1541 tcp_sk(sk)->highest_sack =
new;
1547 static inline bool tcp_stream_is_thin(
struct tcp_sock *tp)
1549 return tp->
packets_out < 4 && !tcp_in_initial_slowstart(tp);
1597 #ifdef CONFIG_PROC_FS
1598 extern int tcp4_proc_init(
void);
1599 extern void tcp4_proc_exit(
void);
1604 #ifdef CONFIG_TCP_MD5SIG
1606 struct sock *addr_sk);
1619 #ifdef CONFIG_TCP_MD5SIG
1632 #define COOKIE_DIGEST_WORDS (SHA_DIGEST_WORDS)
1633 #define COOKIE_MESSAGE_WORDS (SHA_MESSAGE_BYTES / 4)
1634 #define COOKIE_WORKSPACE_WORDS (COOKIE_DIGEST_WORDS + COOKIE_MESSAGE_WORDS)
1674 static inline void tcp_cookie_values_release(
struct kref *
kref)
1683 static inline int tcp_s_data_size(
const struct tcp_sock *tp)