tcp.h 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810
  1. /*
  2. * INET An implementation of the TCP/IP protocol suite for the LINUX
  3. * operating system. INET is implemented using the BSD Socket
  4. * interface as the means of communication with the user level.
  5. *
  6. * Definitions for the TCP module.
  7. *
  8. * Version: @(#)tcp.h 1.0.5 05/23/93
  9. *
  10. * Authors: Ross Biro
  11. * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
  12. *
  13. * This program is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU General Public License
  15. * as published by the Free Software Foundation; either version
  16. * 2 of the License, or (at your option) any later version.
  17. */
  18. #ifndef _TCP_H
  19. #define _TCP_H
  20. #define FASTRETRANS_DEBUG 1
  21. #include <linux/list.h>
  22. #include <linux/tcp.h>
  23. #include <linux/bug.h>
  24. #include <linux/slab.h>
  25. #include <linux/cache.h>
  26. #include <linux/percpu.h>
  27. #include <linux/skbuff.h>
  28. #include <linux/crypto.h>
  29. #include <linux/cryptohash.h>
  30. #include <linux/kref.h>
  31. #include <linux/ktime.h>
  32. #include <net/inet_connection_sock.h>
  33. #include <net/inet_timewait_sock.h>
  34. #include <net/inet_hashtables.h>
  35. #include <net/checksum.h>
  36. #include <net/request_sock.h>
  37. #include <net/sock.h>
  38. #include <net/snmp.h>
  39. #include <net/ip.h>
  40. #include <net/tcp_states.h>
  41. #include <net/inet_ecn.h>
  42. #include <net/dst.h>
  43. #include <linux/seq_file.h>
  44. #include <linux/memcontrol.h>
  45. extern struct inet_hashinfo tcp_hashinfo;
  46. extern struct percpu_counter tcp_orphan_count;
  47. void tcp_time_wait(struct sock *sk, int state, int timeo);
  48. #define MAX_TCP_HEADER (128 + MAX_HEADER)
  49. #define MAX_TCP_OPTION_SPACE 40
  50. /*
  51. * Never offer a window over 32767 without using window scaling. Some
  52. * poor stacks do signed 16bit maths!
  53. */
  54. #define MAX_TCP_WINDOW 32767U
  55. /* Minimal accepted MSS. It is (60+60+8) - (20+20). */
  56. #define TCP_MIN_MSS 88U
  57. /* The least MTU to use for probing */
  58. #define TCP_BASE_MSS 1024
  59. /* probing interval, default to 10 minutes as per RFC4821 */
  60. #define TCP_PROBE_INTERVAL 600
  61. /* Specify interval when tcp mtu probing will stop */
  62. #define TCP_PROBE_THRESHOLD 8
  63. /* After receiving this amount of duplicate ACKs fast retransmit starts. */
  64. #define TCP_FASTRETRANS_THRESH 3
  65. /* Maximal number of ACKs sent quickly to accelerate slow-start. */
  66. #define TCP_MAX_QUICKACKS 16U
  67. /* urg_data states */
  68. #define TCP_URG_VALID 0x0100
  69. #define TCP_URG_NOTYET 0x0200
  70. #define TCP_URG_READ 0x0400
  71. #define TCP_RETR1 3 /*
  72. * This is how many retries it does before it
  73. * tries to figure out if the gateway is
  74. * down. Minimal RFC value is 3; it corresponds
  75. * to ~3sec-8min depending on RTO.
  76. */
  77. #define TCP_RETR2 15 /*
  78. * This should take at least
  79. * 90 minutes to time out.
  80. * RFC1122 says that the limit is 100 sec.
  81. * 15 is ~13-30min depending on RTO.
  82. */
  83. #define TCP_SYN_RETRIES 6 /* This is how many retries are done
  84. * when active opening a connection.
  85. * RFC1122 says the minimum retry MUST
  86. * be at least 180secs. Nevertheless
  87. * this value is corresponding to
  88. * 63secs of retransmission with the
  89. * current initial RTO.
  90. */
  91. #define TCP_SYNACK_RETRIES 5 /* This is how may retries are done
  92. * when passive opening a connection.
  93. * This is corresponding to 31secs of
  94. * retransmission with the current
  95. * initial RTO.
  96. */
  97. #define TCP_TIMEWAIT_LEN (60*HZ) /* how long to wait to destroy TIME-WAIT
  98. * state, about 60 seconds */
  99. #define TCP_FIN_TIMEOUT TCP_TIMEWAIT_LEN
  100. /* BSD style FIN_WAIT2 deadlock breaker.
  101. * It used to be 3min, new value is 60sec,
  102. * to combine FIN-WAIT-2 timeout with
  103. * TIME-WAIT timer.
  104. */
  105. #define TCP_DELACK_MAX ((unsigned)(HZ/5)) /* maximal time to delay before sending an ACK */
  106. #if HZ >= 100
  107. #define TCP_DELACK_MIN ((unsigned)(HZ/25)) /* minimal time to delay before sending an ACK */
  108. #define TCP_ATO_MIN ((unsigned)(HZ/25))
  109. #else
  110. #define TCP_DELACK_MIN 4U
  111. #define TCP_ATO_MIN 4U
  112. #endif
  113. #define TCP_RTO_MAX ((unsigned)(120*HZ))
  114. #define TCP_RTO_MIN ((unsigned)(HZ/5))
  115. #define TCP_TIMEOUT_INIT ((unsigned)(1*HZ)) /* RFC6298 2.1 initial RTO value */
  116. #define TCP_TIMEOUT_FALLBACK ((unsigned)(3*HZ)) /* RFC 1122 initial RTO value, now
  117. * used as a fallback RTO for the
  118. * initial data transmission if no
  119. * valid RTT sample has been acquired,
  120. * most likely due to retrans in 3WHS.
  121. */
  122. #define TCP_RESOURCE_PROBE_INTERVAL ((unsigned)(HZ/2U)) /* Maximal interval between probes
  123. * for local resources.
  124. */
  125. #define TCP_KEEPALIVE_TIME (120*60*HZ) /* two hours */
  126. #define TCP_KEEPALIVE_PROBES 9 /* Max of 9 keepalive probes */
  127. #define TCP_KEEPALIVE_INTVL (75*HZ)
  128. #define MAX_TCP_KEEPIDLE 32767
  129. #define MAX_TCP_KEEPINTVL 32767
  130. #define MAX_TCP_KEEPCNT 127
  131. #define MAX_TCP_SYNCNT 127
  132. #define TCP_SYNQ_INTERVAL (HZ/5) /* Period of SYNACK timer */
  133. #define TCP_PAWS_24DAYS (60 * 60 * 24 * 24)
  134. #define TCP_PAWS_MSL 60 /* Per-host timestamps are invalidated
  135. * after this time. It should be equal
  136. * (or greater than) TCP_TIMEWAIT_LEN
  137. * to provide reliability equal to one
  138. * provided by timewait state.
  139. */
  140. #define TCP_PAWS_WINDOW 1 /* Replay window for per-host
  141. * timestamps. It must be less than
  142. * minimal timewait lifetime.
  143. */
  144. /*
  145. * TCP option
  146. */
  147. #define TCPOPT_NOP 1 /* Padding */
  148. #define TCPOPT_EOL 0 /* End of options */
  149. #define TCPOPT_MSS 2 /* Segment size negotiating */
  150. #define TCPOPT_WINDOW 3 /* Window scaling */
  151. #define TCPOPT_SACK_PERM 4 /* SACK Permitted */
  152. #define TCPOPT_SACK 5 /* SACK Block */
  153. #define TCPOPT_TIMESTAMP 8 /* Better RTT estimations/PAWS */
  154. #define TCPOPT_MD5SIG 19 /* MD5 Signature (RFC2385) */
  155. #define TCPOPT_FASTOPEN 34 /* Fast open (RFC7413) */
  156. #define TCPOPT_EXP 254 /* Experimental */
  157. /* Magic number to be after the option value for sharing TCP
  158. * experimental options. See draft-ietf-tcpm-experimental-options-00.txt
  159. */
  160. #define TCPOPT_FASTOPEN_MAGIC 0xF989
  161. /*
  162. * TCP option lengths
  163. */
  164. #define TCPOLEN_MSS 4
  165. #define TCPOLEN_WINDOW 3
  166. #define TCPOLEN_SACK_PERM 2
  167. #define TCPOLEN_TIMESTAMP 10
  168. #define TCPOLEN_MD5SIG 18
  169. #define TCPOLEN_FASTOPEN_BASE 2
  170. #define TCPOLEN_EXP_FASTOPEN_BASE 4
  171. /* But this is what stacks really send out. */
  172. #define TCPOLEN_TSTAMP_ALIGNED 12
  173. #define TCPOLEN_WSCALE_ALIGNED 4
  174. #define TCPOLEN_SACKPERM_ALIGNED 4
  175. #define TCPOLEN_SACK_BASE 2
  176. #define TCPOLEN_SACK_BASE_ALIGNED 4
  177. #define TCPOLEN_SACK_PERBLOCK 8
  178. #define TCPOLEN_MD5SIG_ALIGNED 20
  179. #define TCPOLEN_MSS_ALIGNED 4
  180. /* Flags in tp->nonagle */
  181. #define TCP_NAGLE_OFF 1 /* Nagle's algo is disabled */
  182. #define TCP_NAGLE_CORK 2 /* Socket is corked */
  183. #define TCP_NAGLE_PUSH 4 /* Cork is overridden for already queued data */
  184. /* TCP thin-stream limits */
  185. #define TCP_THIN_LINEAR_RETRIES 6 /* After 6 linear retries, do exp. backoff */
  186. /* TCP initial congestion window as per draft-hkchu-tcpm-initcwnd-01 */
  187. #define TCP_INIT_CWND 10
  188. /* Bit Flags for sysctl_tcp_fastopen */
  189. #define TFO_CLIENT_ENABLE 1
  190. #define TFO_SERVER_ENABLE 2
  191. #define TFO_CLIENT_NO_COOKIE 4 /* Data in SYN w/o cookie option */
  192. /* Accept SYN data w/o any cookie option */
  193. #define TFO_SERVER_COOKIE_NOT_REQD 0x200
  194. /* Force enable TFO on all listeners, i.e., not requiring the
  195. * TCP_FASTOPEN socket option. SOCKOPT1/2 determine how to set max_qlen.
  196. */
  197. #define TFO_SERVER_WO_SOCKOPT1 0x400
  198. #define TFO_SERVER_WO_SOCKOPT2 0x800
  199. extern struct inet_timewait_death_row tcp_death_row;
  200. /* sysctl variables for tcp */
  201. extern int sysctl_tcp_timestamps;
  202. extern int sysctl_tcp_window_scaling;
  203. extern int sysctl_tcp_sack;
  204. extern int sysctl_tcp_fin_timeout;
  205. extern int sysctl_tcp_keepalive_time;
  206. extern int sysctl_tcp_keepalive_probes;
  207. extern int sysctl_tcp_keepalive_intvl;
  208. extern int sysctl_tcp_syn_retries;
  209. extern int sysctl_tcp_synack_retries;
  210. extern int sysctl_tcp_retries1;
  211. extern int sysctl_tcp_retries2;
  212. extern int sysctl_tcp_orphan_retries;
  213. extern int sysctl_tcp_syncookies;
  214. extern int sysctl_tcp_fastopen;
  215. extern int sysctl_tcp_retrans_collapse;
  216. extern int sysctl_tcp_stdurg;
  217. extern int sysctl_tcp_rfc1337;
  218. extern int sysctl_tcp_abort_on_overflow;
  219. extern int sysctl_tcp_max_orphans;
  220. extern int sysctl_tcp_fack;
  221. extern int sysctl_tcp_reordering;
  222. extern int sysctl_tcp_max_reordering;
  223. extern int sysctl_tcp_dsack;
  224. extern long sysctl_tcp_mem[3];
  225. extern int sysctl_tcp_wmem[3];
  226. extern int sysctl_tcp_rmem[3];
  227. extern int sysctl_tcp_app_win;
  228. extern int sysctl_tcp_adv_win_scale;
  229. extern int sysctl_tcp_tw_reuse;
  230. extern int sysctl_tcp_frto;
  231. extern int sysctl_tcp_low_latency;
  232. extern int sysctl_tcp_nometrics_save;
  233. extern int sysctl_tcp_moderate_rcvbuf;
  234. extern int sysctl_tcp_tso_win_divisor;
  235. extern int sysctl_tcp_workaround_signed_windows;
  236. extern int sysctl_tcp_slow_start_after_idle;
  237. extern int sysctl_tcp_thin_linear_timeouts;
  238. extern int sysctl_tcp_thin_dupack;
  239. extern int sysctl_tcp_early_retrans;
  240. extern int sysctl_tcp_limit_output_bytes;
  241. extern int sysctl_tcp_challenge_ack_limit;
  242. extern unsigned int sysctl_tcp_notsent_lowat;
  243. extern int sysctl_tcp_min_tso_segs;
  244. extern int sysctl_tcp_min_rtt_wlen;
  245. extern int sysctl_tcp_autocorking;
  246. extern int sysctl_tcp_invalid_ratelimit;
  247. extern int sysctl_tcp_pacing_ss_ratio;
  248. extern int sysctl_tcp_pacing_ca_ratio;
  249. extern atomic_long_t tcp_memory_allocated;
  250. extern struct percpu_counter tcp_sockets_allocated;
  251. extern int tcp_memory_pressure;
  252. /* optimized version of sk_under_memory_pressure() for TCP sockets */
  253. static inline bool tcp_under_memory_pressure(const struct sock *sk)
  254. {
  255. if (mem_cgroup_sockets_enabled && sk->sk_cgrp)
  256. return !!sk->sk_cgrp->memory_pressure;
  257. return tcp_memory_pressure;
  258. }
  259. /*
  260. * The next routines deal with comparing 32 bit unsigned ints
  261. * and worry about wraparound (automatic with unsigned arithmetic).
  262. */
  263. static inline bool before(__u32 seq1, __u32 seq2)
  264. {
  265. return (__s32)(seq1-seq2) < 0;
  266. }
  267. #define after(seq2, seq1) before(seq1, seq2)
  268. /* is s2<=s1<=s3 ? */
  269. static inline bool between(__u32 seq1, __u32 seq2, __u32 seq3)
  270. {
  271. return seq3 - seq2 >= seq1 - seq2;
  272. }
  273. static inline bool tcp_out_of_memory(struct sock *sk)
  274. {
  275. if (sk->sk_wmem_queued > SOCK_MIN_SNDBUF &&
  276. sk_memory_allocated(sk) > sk_prot_mem_limits(sk, 2))
  277. return true;
  278. return false;
  279. }
  280. void sk_forced_mem_schedule(struct sock *sk, int size);
  281. static inline bool tcp_too_many_orphans(struct sock *sk, int shift)
  282. {
  283. struct percpu_counter *ocp = sk->sk_prot->orphan_count;
  284. int orphans = percpu_counter_read_positive(ocp);
  285. if (orphans << shift > sysctl_tcp_max_orphans) {
  286. orphans = percpu_counter_sum_positive(ocp);
  287. if (orphans << shift > sysctl_tcp_max_orphans)
  288. return true;
  289. }
  290. return false;
  291. }
  292. bool tcp_check_oom(struct sock *sk, int shift);
  293. extern struct proto tcp_prot;
  294. #define TCP_INC_STATS(net, field) SNMP_INC_STATS((net)->mib.tcp_statistics, field)
  295. #define TCP_INC_STATS_BH(net, field) SNMP_INC_STATS_BH((net)->mib.tcp_statistics, field)
  296. #define TCP_DEC_STATS(net, field) SNMP_DEC_STATS((net)->mib.tcp_statistics, field)
  297. #define TCP_ADD_STATS_USER(net, field, val) SNMP_ADD_STATS_USER((net)->mib.tcp_statistics, field, val)
  298. #define TCP_ADD_STATS(net, field, val) SNMP_ADD_STATS((net)->mib.tcp_statistics, field, val)
  299. void tcp_tasklet_init(void);
  300. void tcp_v4_err(struct sk_buff *skb, u32);
  301. void tcp_shutdown(struct sock *sk, int how);
  302. void tcp_v4_early_demux(struct sk_buff *skb);
  303. int tcp_v4_rcv(struct sk_buff *skb);
  304. int tcp_v4_tw_remember_stamp(struct inet_timewait_sock *tw);
  305. int tcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t size);
  306. int tcp_sendpage(struct sock *sk, struct page *page, int offset, size_t size,
  307. int flags);
  308. void tcp_release_cb(struct sock *sk);
  309. void tcp_wfree(struct sk_buff *skb);
  310. void tcp_write_timer_handler(struct sock *sk);
  311. void tcp_delack_timer_handler(struct sock *sk);
  312. int tcp_ioctl(struct sock *sk, int cmd, unsigned long arg);
  313. int tcp_rcv_state_process(struct sock *sk, struct sk_buff *skb);
  314. void tcp_rcv_established(struct sock *sk, struct sk_buff *skb,
  315. const struct tcphdr *th, unsigned int len);
  316. void tcp_rcv_space_adjust(struct sock *sk);
  317. int tcp_twsk_unique(struct sock *sk, struct sock *sktw, void *twp);
  318. void tcp_twsk_destructor(struct sock *sk);
  319. ssize_t tcp_splice_read(struct socket *sk, loff_t *ppos,
  320. struct pipe_inode_info *pipe, size_t len,
  321. unsigned int flags);
  322. void tcp_enter_quickack_mode(struct sock *sk, unsigned int max_quickacks);
  323. static inline void tcp_dec_quickack_mode(struct sock *sk,
  324. const unsigned int pkts)
  325. {
  326. struct inet_connection_sock *icsk = inet_csk(sk);
  327. if (icsk->icsk_ack.quick) {
  328. if (pkts >= icsk->icsk_ack.quick) {
  329. icsk->icsk_ack.quick = 0;
  330. /* Leaving quickack mode we deflate ATO. */
  331. icsk->icsk_ack.ato = TCP_ATO_MIN;
  332. } else
  333. icsk->icsk_ack.quick -= pkts;
  334. }
  335. }
  336. #define TCP_ECN_OK 1
  337. #define TCP_ECN_QUEUE_CWR 2
  338. #define TCP_ECN_DEMAND_CWR 4
  339. #define TCP_ECN_SEEN 8
  340. enum tcp_tw_status {
  341. TCP_TW_SUCCESS = 0,
  342. TCP_TW_RST = 1,
  343. TCP_TW_ACK = 2,
  344. TCP_TW_SYN = 3
  345. };
  346. enum tcp_tw_status tcp_timewait_state_process(struct inet_timewait_sock *tw,
  347. struct sk_buff *skb,
  348. const struct tcphdr *th);
  349. struct sock *tcp_check_req(struct sock *sk, struct sk_buff *skb,
  350. struct request_sock *req, bool fastopen);
  351. int tcp_child_process(struct sock *parent, struct sock *child,
  352. struct sk_buff *skb);
  353. void tcp_enter_loss(struct sock *sk);
  354. void tcp_clear_retrans(struct tcp_sock *tp);
  355. void tcp_update_metrics(struct sock *sk);
  356. void tcp_init_metrics(struct sock *sk);
  357. void tcp_metrics_init(void);
  358. bool tcp_peer_is_proven(struct request_sock *req, struct dst_entry *dst,
  359. bool paws_check, bool timestamps);
  360. bool tcp_remember_stamp(struct sock *sk);
  361. bool tcp_tw_remember_stamp(struct inet_timewait_sock *tw);
  362. void tcp_fetch_timewait_stamp(struct sock *sk, struct dst_entry *dst);
  363. void tcp_disable_fack(struct tcp_sock *tp);
  364. void tcp_close(struct sock *sk, long timeout);
  365. void tcp_init_sock(struct sock *sk);
  366. unsigned int tcp_poll(struct file *file, struct socket *sock,
  367. struct poll_table_struct *wait);
  368. int tcp_getsockopt(struct sock *sk, int level, int optname,
  369. char __user *optval, int __user *optlen);
  370. int tcp_setsockopt(struct sock *sk, int level, int optname,
  371. char __user *optval, unsigned int optlen);
  372. int compat_tcp_getsockopt(struct sock *sk, int level, int optname,
  373. char __user *optval, int __user *optlen);
  374. int compat_tcp_setsockopt(struct sock *sk, int level, int optname,
  375. char __user *optval, unsigned int optlen);
  376. void tcp_set_keepalive(struct sock *sk, int val);
  377. void tcp_syn_ack_timeout(const struct request_sock *req);
  378. int tcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int nonblock,
  379. int flags, int *addr_len);
  380. void tcp_parse_options(const struct sk_buff *skb,
  381. struct tcp_options_received *opt_rx,
  382. int estab, struct tcp_fastopen_cookie *foc);
  383. const u8 *tcp_parse_md5sig_option(const struct tcphdr *th);
  384. /*
  385. * TCP v4 functions exported for the inet6 API
  386. */
  387. void tcp_v4_send_check(struct sock *sk, struct sk_buff *skb);
  388. void tcp_v4_mtu_reduced(struct sock *sk);
  389. void tcp_req_err(struct sock *sk, u32 seq, bool abort);
  390. int tcp_v4_conn_request(struct sock *sk, struct sk_buff *skb);
  391. struct sock *tcp_create_openreq_child(const struct sock *sk,
  392. struct request_sock *req,
  393. struct sk_buff *skb);
  394. void tcp_ca_openreq_child(struct sock *sk, const struct dst_entry *dst);
  395. struct sock *tcp_v4_syn_recv_sock(const struct sock *sk, struct sk_buff *skb,
  396. struct request_sock *req,
  397. struct dst_entry *dst,
  398. struct request_sock *req_unhash,
  399. bool *own_req);
  400. int tcp_v4_do_rcv(struct sock *sk, struct sk_buff *skb);
  401. int tcp_v4_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len);
  402. int tcp_connect(struct sock *sk);
  403. struct sk_buff *tcp_make_synack(const struct sock *sk, struct dst_entry *dst,
  404. struct request_sock *req,
  405. struct tcp_fastopen_cookie *foc,
  406. bool attach_req);
  407. int tcp_disconnect(struct sock *sk, int flags);
  408. void tcp_finish_connect(struct sock *sk, struct sk_buff *skb);
  409. int tcp_send_rcvq(struct sock *sk, struct msghdr *msg, size_t size);
  410. void inet_sk_rx_dst_set(struct sock *sk, const struct sk_buff *skb);
  411. /* From syncookies.c */
  412. struct sock *tcp_get_cookie_sock(struct sock *sk, struct sk_buff *skb,
  413. struct request_sock *req,
  414. struct dst_entry *dst);
  415. int __cookie_v4_check(const struct iphdr *iph, const struct tcphdr *th,
  416. u32 cookie);
  417. struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb);
  418. #ifdef CONFIG_SYN_COOKIES
  419. /* Syncookies use a monotonic timer which increments every 60 seconds.
  420. * This counter is used both as a hash input and partially encoded into
  421. * the cookie value. A cookie is only validated further if the delta
  422. * between the current counter value and the encoded one is less than this,
  423. * i.e. a sent cookie is valid only at most for 2*60 seconds (or less if
  424. * the counter advances immediately after a cookie is generated).
  425. */
  426. #define MAX_SYNCOOKIE_AGE 2
  427. #define TCP_SYNCOOKIE_PERIOD (60 * HZ)
  428. #define TCP_SYNCOOKIE_VALID (MAX_SYNCOOKIE_AGE * TCP_SYNCOOKIE_PERIOD)
  429. /* syncookies: remember time of last synqueue overflow
  430. * But do not dirty this field too often (once per second is enough)
  431. * It is racy as we do not hold a lock, but race is very minor.
  432. */
  433. static inline void tcp_synq_overflow(const struct sock *sk)
  434. {
  435. unsigned long last_overflow = tcp_sk(sk)->rx_opt.ts_recent_stamp;
  436. unsigned long now = jiffies;
  437. if (time_after(now, last_overflow + HZ))
  438. tcp_sk(sk)->rx_opt.ts_recent_stamp = now;
  439. }
  440. /* syncookies: no recent synqueue overflow on this listening socket? */
  441. static inline bool tcp_synq_no_recent_overflow(const struct sock *sk)
  442. {
  443. unsigned long last_overflow = tcp_sk(sk)->rx_opt.ts_recent_stamp;
  444. return time_after(jiffies, last_overflow + TCP_SYNCOOKIE_VALID);
  445. }
  446. static inline u32 tcp_cookie_time(void)
  447. {
  448. u64 val = get_jiffies_64();
  449. do_div(val, TCP_SYNCOOKIE_PERIOD);
  450. return val;
  451. }
  452. u32 __cookie_v4_init_sequence(const struct iphdr *iph, const struct tcphdr *th,
  453. u16 *mssp);
  454. __u32 cookie_v4_init_sequence(const struct sk_buff *skb, __u16 *mss);
  455. __u32 cookie_init_timestamp(struct request_sock *req);
  456. bool cookie_timestamp_decode(struct tcp_options_received *opt);
  457. bool cookie_ecn_ok(const struct tcp_options_received *opt,
  458. const struct net *net, const struct dst_entry *dst);
  459. /* From net/ipv6/syncookies.c */
  460. int __cookie_v6_check(const struct ipv6hdr *iph, const struct tcphdr *th,
  461. u32 cookie);
  462. struct sock *cookie_v6_check(struct sock *sk, struct sk_buff *skb);
  463. u32 __cookie_v6_init_sequence(const struct ipv6hdr *iph,
  464. const struct tcphdr *th, u16 *mssp);
  465. __u32 cookie_v6_init_sequence(const struct sk_buff *skb, __u16 *mss);
  466. #endif
  467. /* tcp_output.c */
  468. void __tcp_push_pending_frames(struct sock *sk, unsigned int cur_mss,
  469. int nonagle);
  470. bool tcp_may_send_now(struct sock *sk);
  471. int __tcp_retransmit_skb(struct sock *, struct sk_buff *);
  472. int tcp_retransmit_skb(struct sock *, struct sk_buff *);
  473. void tcp_retransmit_timer(struct sock *sk);
  474. void tcp_xmit_retransmit_queue(struct sock *);
  475. void tcp_simple_retransmit(struct sock *);
  476. int tcp_trim_head(struct sock *, struct sk_buff *, u32);
  477. int tcp_fragment(struct sock *, struct sk_buff *, u32, unsigned int, gfp_t);
  478. void tcp_send_probe0(struct sock *);
  479. void tcp_send_partial(struct sock *);
  480. int tcp_write_wakeup(struct sock *, int mib);
  481. void tcp_send_fin(struct sock *sk);
  482. void tcp_send_active_reset(struct sock *sk, gfp_t priority);
  483. int tcp_send_synack(struct sock *);
  484. void tcp_push_one(struct sock *, unsigned int mss_now);
  485. void __tcp_send_ack(struct sock *sk, u32 rcv_nxt);
  486. void tcp_send_ack(struct sock *sk);
  487. void tcp_send_delayed_ack(struct sock *sk);
  488. void tcp_send_loss_probe(struct sock *sk);
  489. bool tcp_schedule_loss_probe(struct sock *sk);
  490. /* tcp_input.c */
  491. void tcp_resume_early_retransmit(struct sock *sk);
  492. void tcp_rearm_rto(struct sock *sk);
  493. void tcp_synack_rtt_meas(struct sock *sk, struct request_sock *req);
  494. void tcp_reset(struct sock *sk);
  495. void tcp_skb_mark_lost_uncond_verify(struct tcp_sock *tp, struct sk_buff *skb);
  496. /* tcp_timer.c */
  497. void tcp_init_xmit_timers(struct sock *);
  498. static inline void tcp_clear_xmit_timers(struct sock *sk)
  499. {
  500. inet_csk_clear_xmit_timers(sk);
  501. }
  502. unsigned int tcp_sync_mss(struct sock *sk, u32 pmtu);
  503. unsigned int tcp_current_mss(struct sock *sk);
  504. /* Bound MSS / TSO packet size with the half of the window */
  505. static inline int tcp_bound_to_half_wnd(struct tcp_sock *tp, int pktsize)
  506. {
  507. int cutoff;
  508. /* When peer uses tiny windows, there is no use in packetizing
  509. * to sub-MSS pieces for the sake of SWS or making sure there
  510. * are enough packets in the pipe for fast recovery.
  511. *
  512. * On the other hand, for extremely large MSS devices, handling
  513. * smaller than MSS windows in this way does make sense.
  514. */
  515. if (tp->max_window >= 512)
  516. cutoff = (tp->max_window >> 1);
  517. else
  518. cutoff = tp->max_window;
  519. if (cutoff && pktsize > cutoff)
  520. return max_t(int, cutoff, 68U - tp->tcp_header_len);
  521. else
  522. return pktsize;
  523. }
  524. /* tcp.c */
  525. void tcp_get_info(struct sock *, struct tcp_info *);
  526. /* Read 'sendfile()'-style from a TCP socket */
  527. typedef int (*sk_read_actor_t)(read_descriptor_t *, struct sk_buff *,
  528. unsigned int, size_t);
  529. int tcp_read_sock(struct sock *sk, read_descriptor_t *desc,
  530. sk_read_actor_t recv_actor);
  531. void tcp_initialize_rcv_mss(struct sock *sk);
  532. int tcp_mtu_to_mss(struct sock *sk, int pmtu);
  533. int tcp_mss_to_mtu(struct sock *sk, int mss);
  534. void tcp_mtup_init(struct sock *sk);
  535. void tcp_init_buffer_space(struct sock *sk);
  536. static inline void tcp_bound_rto(const struct sock *sk)
  537. {
  538. if (inet_csk(sk)->icsk_rto > TCP_RTO_MAX)
  539. inet_csk(sk)->icsk_rto = TCP_RTO_MAX;
  540. }
  541. static inline u32 __tcp_set_rto(const struct tcp_sock *tp)
  542. {
  543. return usecs_to_jiffies((tp->srtt_us >> 3) + tp->rttvar_us);
  544. }
  545. static inline void __tcp_fast_path_on(struct tcp_sock *tp, u32 snd_wnd)
  546. {
  547. tp->pred_flags = htonl((tp->tcp_header_len << 26) |
  548. ntohl(TCP_FLAG_ACK) |
  549. snd_wnd);
  550. }
  551. static inline void tcp_fast_path_on(struct tcp_sock *tp)
  552. {
  553. __tcp_fast_path_on(tp, tp->snd_wnd >> tp->rx_opt.snd_wscale);
  554. }
  555. static inline void tcp_fast_path_check(struct sock *sk)
  556. {
  557. struct tcp_sock *tp = tcp_sk(sk);
  558. if (RB_EMPTY_ROOT(&tp->out_of_order_queue) &&
  559. tp->rcv_wnd &&
  560. atomic_read(&sk->sk_rmem_alloc) < sk->sk_rcvbuf &&
  561. !tp->urg_data)
  562. tcp_fast_path_on(tp);
  563. }
  564. /* Compute the actual rto_min value */
  565. static inline u32 tcp_rto_min(struct sock *sk)
  566. {
  567. const struct dst_entry *dst = __sk_dst_get(sk);
  568. u32 rto_min = TCP_RTO_MIN;
  569. if (dst && dst_metric_locked(dst, RTAX_RTO_MIN))
  570. rto_min = dst_metric_rtt(dst, RTAX_RTO_MIN);
  571. return rto_min;
  572. }
  573. static inline u32 tcp_rto_min_us(struct sock *sk)
  574. {
  575. return jiffies_to_usecs(tcp_rto_min(sk));
  576. }
  577. static inline bool tcp_ca_dst_locked(const struct dst_entry *dst)
  578. {
  579. return dst_metric_locked(dst, RTAX_CC_ALGO);
  580. }
  581. /* Minimum RTT in usec. ~0 means not available. */
  582. static inline u32 tcp_min_rtt(const struct tcp_sock *tp)
  583. {
  584. return tp->rtt_min[0].rtt;
  585. }
  586. /* Compute the actual receive window we are currently advertising.
  587. * Rcv_nxt can be after the window if our peer push more data
  588. * than the offered window.
  589. */
  590. static inline u32 tcp_receive_window(const struct tcp_sock *tp)
  591. {
  592. s32 win = tp->rcv_wup + tp->rcv_wnd - tp->rcv_nxt;
  593. if (win < 0)
  594. win = 0;
  595. return (u32) win;
  596. }
  597. /* Choose a new window, without checks for shrinking, and without
  598. * scaling applied to the result. The caller does these things
  599. * if necessary. This is a "raw" window selection.
  600. */
  601. u32 __tcp_select_window(struct sock *sk);
  602. void tcp_send_window_probe(struct sock *sk);
  603. /* TCP timestamps are only 32-bits, this causes a slight
  604. * complication on 64-bit systems since we store a snapshot
  605. * of jiffies in the buffer control blocks below. We decided
  606. * to use only the low 32-bits of jiffies and hide the ugly
  607. * casts with the following macro.
  608. */
  609. #define tcp_time_stamp ((__u32)(jiffies))
  610. static inline u32 tcp_skb_timestamp(const struct sk_buff *skb)
  611. {
  612. return skb->skb_mstamp.stamp_jiffies;
  613. }
  614. #define tcp_flag_byte(th) (((u_int8_t *)th)[13])
  615. #define TCPHDR_FIN 0x01
  616. #define TCPHDR_SYN 0x02
  617. #define TCPHDR_RST 0x04
  618. #define TCPHDR_PSH 0x08
  619. #define TCPHDR_ACK 0x10
  620. #define TCPHDR_URG 0x20
  621. #define TCPHDR_ECE 0x40
  622. #define TCPHDR_CWR 0x80
  623. #define TCPHDR_SYN_ECN (TCPHDR_SYN | TCPHDR_ECE | TCPHDR_CWR)
  624. /* This is what the send packet queuing engine uses to pass
  625. * TCP per-packet control information to the transmission code.
  626. * We also store the host-order sequence numbers in here too.
  627. * This is 44 bytes if IPV6 is enabled.
  628. * If this grows please adjust skbuff.h:skbuff->cb[xxx] size appropriately.
  629. */
  630. struct tcp_skb_cb {
  631. __u32 seq; /* Starting sequence number */
  632. __u32 end_seq; /* SEQ + FIN + SYN + datalen */
  633. union {
  634. /* Note : tcp_tw_isn is used in input path only
  635. * (isn chosen by tcp_timewait_state_process())
  636. *
  637. * tcp_gso_segs/size are used in write queue only,
  638. * cf tcp_skb_pcount()/tcp_skb_mss()
  639. */
  640. __u32 tcp_tw_isn;
  641. struct {
  642. u16 tcp_gso_segs;
  643. u16 tcp_gso_size;
  644. };
  645. };
  646. __u8 tcp_flags; /* TCP header flags. (tcp[13]) */
  647. __u8 sacked; /* State flags for SACK/FACK. */
  648. #define TCPCB_SACKED_ACKED 0x01 /* SKB ACK'd by a SACK block */
  649. #define TCPCB_SACKED_RETRANS 0x02 /* SKB retransmitted */
  650. #define TCPCB_LOST 0x04 /* SKB is lost */
  651. #define TCPCB_TAGBITS 0x07 /* All tag bits */
  652. #define TCPCB_REPAIRED 0x10 /* SKB repaired (no skb_mstamp) */
  653. #define TCPCB_EVER_RETRANS 0x80 /* Ever retransmitted frame */
  654. #define TCPCB_RETRANS (TCPCB_SACKED_RETRANS|TCPCB_EVER_RETRANS| \
  655. TCPCB_REPAIRED)
  656. __u8 ip_dsfield; /* IPv4 tos or IPv6 dsfield */
  657. /* 1 byte hole */
  658. __u32 ack_seq; /* Sequence number ACK'd */
  659. union {
  660. struct inet_skb_parm h4;
  661. #if IS_ENABLED(CONFIG_IPV6)
  662. struct inet6_skb_parm h6;
  663. #endif
  664. } header; /* For incoming frames */
  665. };
  666. #define TCP_SKB_CB(__skb) ((struct tcp_skb_cb *)&((__skb)->cb[0]))
  667. #if IS_ENABLED(CONFIG_IPV6)
  668. /* This is the variant of inet6_iif() that must be used by TCP,
  669. * as TCP moves IP6CB into a different location in skb->cb[]
  670. */
  671. static inline int tcp_v6_iif(const struct sk_buff *skb)
  672. {
  673. return TCP_SKB_CB(skb)->header.h6.iif;
  674. }
  675. #endif
  676. /* Due to TSO, an SKB can be composed of multiple actual
  677. * packets. To keep these tracked properly, we use this.
  678. */
  679. static inline int tcp_skb_pcount(const struct sk_buff *skb)
  680. {
  681. return TCP_SKB_CB(skb)->tcp_gso_segs;
  682. }
  683. static inline void tcp_skb_pcount_set(struct sk_buff *skb, int segs)
  684. {
  685. TCP_SKB_CB(skb)->tcp_gso_segs = segs;
  686. }
  687. static inline void tcp_skb_pcount_add(struct sk_buff *skb, int segs)
  688. {
  689. TCP_SKB_CB(skb)->tcp_gso_segs += segs;
  690. }
  691. /* This is valid iff skb is in write queue and tcp_skb_pcount() > 1. */
  692. static inline int tcp_skb_mss(const struct sk_buff *skb)
  693. {
  694. return TCP_SKB_CB(skb)->tcp_gso_size;
  695. }
  696. /* Events passed to congestion control interface */
  697. enum tcp_ca_event {
  698. CA_EVENT_TX_START, /* first transmit when no packets in flight */
  699. CA_EVENT_CWND_RESTART, /* congestion window restart */
  700. CA_EVENT_COMPLETE_CWR, /* end of congestion recovery */
  701. CA_EVENT_LOSS, /* loss timeout */
  702. CA_EVENT_ECN_NO_CE, /* ECT set, but not CE marked */
  703. CA_EVENT_ECN_IS_CE, /* received CE marked IP packet */
  704. };
  705. /* Information about inbound ACK, passed to cong_ops->in_ack_event() */
  706. enum tcp_ca_ack_event_flags {
  707. CA_ACK_SLOWPATH = (1 << 0), /* In slow path processing */
  708. CA_ACK_WIN_UPDATE = (1 << 1), /* ACK updated window */
  709. CA_ACK_ECE = (1 << 2), /* ECE bit is set on ack */
  710. };
  711. /*
  712. * Interface for adding new TCP congestion control handlers
  713. */
  714. #define TCP_CA_NAME_MAX 16
  715. #define TCP_CA_MAX 128
  716. #define TCP_CA_BUF_MAX (TCP_CA_NAME_MAX*TCP_CA_MAX)
  717. #define TCP_CA_UNSPEC 0
  718. /* Algorithm can be set on socket without CAP_NET_ADMIN privileges */
  719. #define TCP_CONG_NON_RESTRICTED 0x1
  720. /* Requires ECN/ECT set on all packets */
  721. #define TCP_CONG_NEEDS_ECN 0x2
  722. union tcp_cc_info;
  723. struct tcp_congestion_ops {
  724. struct list_head list;
  725. u32 key;
  726. u32 flags;
  727. /* initialize private data (optional) */
  728. void (*init)(struct sock *sk);
  729. /* cleanup private data (optional) */
  730. void (*release)(struct sock *sk);
  731. /* return slow start threshold (required) */
  732. u32 (*ssthresh)(struct sock *sk);
  733. /* do new cwnd calculation (required) */
  734. void (*cong_avoid)(struct sock *sk, u32 ack, u32 acked);
  735. /* call before changing ca_state (optional) */
  736. void (*set_state)(struct sock *sk, u8 new_state);
  737. /* call when cwnd event occurs (optional) */
  738. void (*cwnd_event)(struct sock *sk, enum tcp_ca_event ev);
  739. /* call when ack arrives (optional) */
  740. void (*in_ack_event)(struct sock *sk, u32 flags);
  741. /* new value of cwnd after loss (optional) */
  742. u32 (*undo_cwnd)(struct sock *sk);
  743. /* hook for packet ack accounting (optional) */
  744. void (*pkts_acked)(struct sock *sk, u32 num_acked, s32 rtt_us);
  745. /* get info for inet_diag (optional) */
  746. size_t (*get_info)(struct sock *sk, u32 ext, int *attr,
  747. union tcp_cc_info *info);
  748. char name[TCP_CA_NAME_MAX];
  749. struct module *owner;
  750. };
  751. int tcp_register_congestion_control(struct tcp_congestion_ops *type);
  752. void tcp_unregister_congestion_control(struct tcp_congestion_ops *type);
  753. void tcp_assign_congestion_control(struct sock *sk);
  754. void tcp_init_congestion_control(struct sock *sk);
  755. void tcp_cleanup_congestion_control(struct sock *sk);
  756. int tcp_set_default_congestion_control(const char *name);
  757. void tcp_get_default_congestion_control(char *name);
  758. void tcp_get_available_congestion_control(char *buf, size_t len);
  759. void tcp_get_allowed_congestion_control(char *buf, size_t len);
  760. int tcp_set_allowed_congestion_control(char *allowed);
  761. int tcp_set_congestion_control(struct sock *sk, const char *name);
  762. u32 tcp_slow_start(struct tcp_sock *tp, u32 acked);
  763. void tcp_cong_avoid_ai(struct tcp_sock *tp, u32 w, u32 acked);
  764. u32 tcp_reno_ssthresh(struct sock *sk);
  765. void tcp_reno_cong_avoid(struct sock *sk, u32 ack, u32 acked);
  766. extern struct tcp_congestion_ops tcp_reno;
  767. struct tcp_congestion_ops *tcp_ca_find_key(u32 key);
  768. u32 tcp_ca_get_key_by_name(const char *name, bool *ecn_ca);
  769. #ifdef CONFIG_INET
  770. char *tcp_ca_get_name_by_key(u32 key, char *buffer);
  771. #else
  772. static inline char *tcp_ca_get_name_by_key(u32 key, char *buffer)
  773. {
  774. return NULL;
  775. }
  776. #endif
  777. static inline bool tcp_ca_needs_ecn(const struct sock *sk)
  778. {
  779. const struct inet_connection_sock *icsk = inet_csk(sk);
  780. return icsk->icsk_ca_ops->flags & TCP_CONG_NEEDS_ECN;
  781. }
  782. static inline void tcp_set_ca_state(struct sock *sk, const u8 ca_state)
  783. {
  784. struct inet_connection_sock *icsk = inet_csk(sk);
  785. if (icsk->icsk_ca_ops->set_state)
  786. icsk->icsk_ca_ops->set_state(sk, ca_state);
  787. icsk->icsk_ca_state = ca_state;
  788. }
  789. static inline void tcp_ca_event(struct sock *sk, const enum tcp_ca_event event)
  790. {
  791. const struct inet_connection_sock *icsk = inet_csk(sk);
  792. if (icsk->icsk_ca_ops->cwnd_event)
  793. icsk->icsk_ca_ops->cwnd_event(sk, event);
  794. }
  795. /* These functions determine how the current flow behaves in respect of SACK
  796. * handling. SACK is negotiated with the peer, and therefore it can vary
  797. * between different flows.
  798. *
  799. * tcp_is_sack - SACK enabled
  800. * tcp_is_reno - No SACK
  801. * tcp_is_fack - FACK enabled, implies SACK enabled
  802. */
  803. static inline int tcp_is_sack(const struct tcp_sock *tp)
  804. {
  805. return tp->rx_opt.sack_ok;
  806. }
  807. static inline bool tcp_is_reno(const struct tcp_sock *tp)
  808. {
  809. return !tcp_is_sack(tp);
  810. }
  811. static inline bool tcp_is_fack(const struct tcp_sock *tp)
  812. {
  813. return tp->rx_opt.sack_ok & TCP_FACK_ENABLED;
  814. }
  815. static inline void tcp_enable_fack(struct tcp_sock *tp)
  816. {
  817. tp->rx_opt.sack_ok |= TCP_FACK_ENABLED;
  818. }
  819. /* TCP early-retransmit (ER) is similar to but more conservative than
  820. * the thin-dupack feature. Enable ER only if thin-dupack is disabled.
  821. */
  822. static inline void tcp_enable_early_retrans(struct tcp_sock *tp)
  823. {
  824. tp->do_early_retrans = sysctl_tcp_early_retrans &&
  825. sysctl_tcp_early_retrans < 4 && !sysctl_tcp_thin_dupack &&
  826. sysctl_tcp_reordering == 3;
  827. }
  828. static inline void tcp_disable_early_retrans(struct tcp_sock *tp)
  829. {
  830. tp->do_early_retrans = 0;
  831. }
  832. static inline unsigned int tcp_left_out(const struct tcp_sock *tp)
  833. {
  834. return tp->sacked_out + tp->lost_out;
  835. }
  836. /* This determines how many packets are "in the network" to the best
  837. * of our knowledge. In many cases it is conservative, but where
  838. * detailed information is available from the receiver (via SACK
  839. * blocks etc.) we can make more aggressive calculations.
  840. *
  841. * Use this for decisions involving congestion control, use just
  842. * tp->packets_out to determine if the send queue is empty or not.
  843. *
  844. * Read this equation as:
  845. *
  846. * "Packets sent once on transmission queue" MINUS
  847. * "Packets left network, but not honestly ACKed yet" PLUS
  848. * "Packets fast retransmitted"
  849. */
  850. static inline unsigned int tcp_packets_in_flight(const struct tcp_sock *tp)
  851. {
  852. return tp->packets_out - tcp_left_out(tp) + tp->retrans_out;
  853. }
  854. #define TCP_INFINITE_SSTHRESH 0x7fffffff
  855. static inline bool tcp_in_slow_start(const struct tcp_sock *tp)
  856. {
  857. return tp->snd_cwnd < tp->snd_ssthresh;
  858. }
  859. static inline bool tcp_in_initial_slowstart(const struct tcp_sock *tp)
  860. {
  861. return tp->snd_ssthresh >= TCP_INFINITE_SSTHRESH;
  862. }
  863. static inline bool tcp_in_cwnd_reduction(const struct sock *sk)
  864. {
  865. return (TCPF_CA_CWR | TCPF_CA_Recovery) &
  866. (1 << inet_csk(sk)->icsk_ca_state);
  867. }
  868. /* If cwnd > ssthresh, we may raise ssthresh to be half-way to cwnd.
  869. * The exception is cwnd reduction phase, when cwnd is decreasing towards
  870. * ssthresh.
  871. */
  872. static inline __u32 tcp_current_ssthresh(const struct sock *sk)
  873. {
  874. const struct tcp_sock *tp = tcp_sk(sk);
  875. if (tcp_in_cwnd_reduction(sk))
  876. return tp->snd_ssthresh;
  877. else
  878. return max(tp->snd_ssthresh,
  879. ((tp->snd_cwnd >> 1) +
  880. (tp->snd_cwnd >> 2)));
  881. }
  882. /* Use define here intentionally to get WARN_ON location shown at the caller */
  883. #define tcp_verify_left_out(tp) WARN_ON(tcp_left_out(tp) > tp->packets_out)
  884. void tcp_enter_cwr(struct sock *sk);
  885. __u32 tcp_init_cwnd(const struct tcp_sock *tp, const struct dst_entry *dst);
  886. /* The maximum number of MSS of available cwnd for which TSO defers
  887. * sending if not using sysctl_tcp_tso_win_divisor.
  888. */
  889. static inline __u32 tcp_max_tso_deferred_mss(const struct tcp_sock *tp)
  890. {
  891. return 3;
  892. }
  893. /* Slow start with delack produces 3 packets of burst, so that
  894. * it is safe "de facto". This will be the default - same as
  895. * the default reordering threshold - but if reordering increases,
  896. * we must be able to allow cwnd to burst at least this much in order
  897. * to not pull it back when holes are filled.
  898. */
  899. static __inline__ __u32 tcp_max_burst(const struct tcp_sock *tp)
  900. {
  901. return tp->reordering;
  902. }
  903. /* Returns end sequence number of the receiver's advertised window */
  904. static inline u32 tcp_wnd_end(const struct tcp_sock *tp)
  905. {
  906. return tp->snd_una + tp->snd_wnd;
  907. }
  908. /* We follow the spirit of RFC2861 to validate cwnd but implement a more
  909. * flexible approach. The RFC suggests cwnd should not be raised unless
  910. * it was fully used previously. And that's exactly what we do in
  911. * congestion avoidance mode. But in slow start we allow cwnd to grow
  912. * as long as the application has used half the cwnd.
  913. * Example :
  914. * cwnd is 10 (IW10), but application sends 9 frames.
  915. * We allow cwnd to reach 18 when all frames are ACKed.
  916. * This check is safe because it's as aggressive as slow start which already
  917. * risks 100% overshoot. The advantage is that we discourage application to
  918. * either send more filler packets or data to artificially blow up the cwnd
  919. * usage, and allow application-limited process to probe bw more aggressively.
  920. */
  921. static inline bool tcp_is_cwnd_limited(const struct sock *sk)
  922. {
  923. const struct tcp_sock *tp = tcp_sk(sk);
  924. /* If in slow start, ensure cwnd grows to twice what was ACKed. */
  925. if (tcp_in_slow_start(tp))
  926. return tp->snd_cwnd < 2 * tp->max_packets_out;
  927. return tp->is_cwnd_limited;
  928. }
  929. /* Something is really bad, we could not queue an additional packet,
  930. * because qdisc is full or receiver sent a 0 window.
  931. * We do not want to add fuel to the fire, or abort too early,
  932. * so make sure the timer we arm now is at least 200ms in the future,
  933. * regardless of current icsk_rto value (as it could be ~2ms)
  934. */
  935. static inline unsigned long tcp_probe0_base(const struct sock *sk)
  936. {
  937. return max_t(unsigned long, inet_csk(sk)->icsk_rto, TCP_RTO_MIN);
  938. }
  939. /* Variant of inet_csk_rto_backoff() used for zero window probes */
  940. static inline unsigned long tcp_probe0_when(const struct sock *sk,
  941. unsigned long max_when)
  942. {
  943. u64 when = (u64)tcp_probe0_base(sk) << inet_csk(sk)->icsk_backoff;
  944. return (unsigned long)min_t(u64, when, max_when);
  945. }
  946. static inline void tcp_check_probe_timer(struct sock *sk)
  947. {
  948. if (!tcp_sk(sk)->packets_out && !inet_csk(sk)->icsk_pending)
  949. inet_csk_reset_xmit_timer(sk, ICSK_TIME_PROBE0,
  950. tcp_probe0_base(sk), TCP_RTO_MAX);
  951. }
  952. static inline void tcp_init_wl(struct tcp_sock *tp, u32 seq)
  953. {
  954. tp->snd_wl1 = seq;
  955. }
  956. static inline void tcp_update_wl(struct tcp_sock *tp, u32 seq)
  957. {
  958. tp->snd_wl1 = seq;
  959. }
  960. /*
  961. * Calculate(/check) TCP checksum
  962. */
  963. static inline __sum16 tcp_v4_check(int len, __be32 saddr,
  964. __be32 daddr, __wsum base)
  965. {
  966. return csum_tcpudp_magic(saddr,daddr,len,IPPROTO_TCP,base);
  967. }
  968. static inline __sum16 __tcp_checksum_complete(struct sk_buff *skb)
  969. {
  970. return __skb_checksum_complete(skb);
  971. }
  972. static inline bool tcp_checksum_complete(struct sk_buff *skb)
  973. {
  974. return !skb_csum_unnecessary(skb) &&
  975. __tcp_checksum_complete(skb);
  976. }
  977. /* Prequeue for VJ style copy to user, combined with checksumming. */
  978. static inline void tcp_prequeue_init(struct tcp_sock *tp)
  979. {
  980. tp->ucopy.task = NULL;
  981. tp->ucopy.len = 0;
  982. tp->ucopy.memory = 0;
  983. skb_queue_head_init(&tp->ucopy.prequeue);
  984. }
  985. bool tcp_prequeue(struct sock *sk, struct sk_buff *skb);
  986. int tcp_filter(struct sock *sk, struct sk_buff *skb);
  987. #undef STATE_TRACE
  988. #ifdef STATE_TRACE
  989. static const char *statename[]={
  990. "Unused","Established","Syn Sent","Syn Recv",
  991. "Fin Wait 1","Fin Wait 2","Time Wait", "Close",
  992. "Close Wait","Last ACK","Listen","Closing"
  993. };
  994. #endif
  995. void tcp_set_state(struct sock *sk, int state);
  996. void tcp_done(struct sock *sk);
  997. static inline void tcp_sack_reset(struct tcp_options_received *rx_opt)
  998. {
  999. rx_opt->dsack = 0;
  1000. rx_opt->num_sacks = 0;
  1001. }
  1002. u32 tcp_default_init_rwnd(u32 mss);
  1003. void tcp_cwnd_restart(struct sock *sk, s32 delta);
  1004. static inline void tcp_slow_start_after_idle_check(struct sock *sk)
  1005. {
  1006. struct tcp_sock *tp = tcp_sk(sk);
  1007. s32 delta;
  1008. if (!sysctl_tcp_slow_start_after_idle || tp->packets_out)
  1009. return;
  1010. delta = tcp_time_stamp - tp->lsndtime;
  1011. if (delta > inet_csk(sk)->icsk_rto)
  1012. tcp_cwnd_restart(sk, delta);
  1013. }
  1014. /* Determine a window scaling and initial window to offer. */
  1015. void tcp_select_initial_window(int __space, __u32 mss, __u32 *rcv_wnd,
  1016. __u32 *window_clamp, int wscale_ok,
  1017. __u8 *rcv_wscale, __u32 init_rcv_wnd);
  1018. static inline int tcp_win_from_space(int space)
  1019. {
  1020. int tcp_adv_win_scale = sysctl_tcp_adv_win_scale;
  1021. return tcp_adv_win_scale <= 0 ?
  1022. (space>>(-tcp_adv_win_scale)) :
  1023. space - (space>>tcp_adv_win_scale);
  1024. }
  1025. /* Note: caller must be prepared to deal with negative returns */
  1026. static inline int tcp_space(const struct sock *sk)
  1027. {
  1028. return tcp_win_from_space(sk->sk_rcvbuf -
  1029. atomic_read(&sk->sk_rmem_alloc));
  1030. }
  1031. static inline int tcp_full_space(const struct sock *sk)
  1032. {
  1033. return tcp_win_from_space(sk->sk_rcvbuf);
  1034. }
  1035. extern void tcp_openreq_init_rwin(struct request_sock *req,
  1036. const struct sock *sk_listener,
  1037. const struct dst_entry *dst);
  1038. void tcp_enter_memory_pressure(struct sock *sk);
  1039. static inline int keepalive_intvl_when(const struct tcp_sock *tp)
  1040. {
  1041. return tp->keepalive_intvl ? : sysctl_tcp_keepalive_intvl;
  1042. }
  1043. static inline int keepalive_time_when(const struct tcp_sock *tp)
  1044. {
  1045. return tp->keepalive_time ? : sysctl_tcp_keepalive_time;
  1046. }
  1047. static inline int keepalive_probes(const struct tcp_sock *tp)
  1048. {
  1049. return tp->keepalive_probes ? : sysctl_tcp_keepalive_probes;
  1050. }
  1051. static inline u32 keepalive_time_elapsed(const struct tcp_sock *tp)
  1052. {
  1053. const struct inet_connection_sock *icsk = &tp->inet_conn;
  1054. return min_t(u32, tcp_time_stamp - icsk->icsk_ack.lrcvtime,
  1055. tcp_time_stamp - tp->rcv_tstamp);
  1056. }
  1057. static inline int tcp_fin_time(const struct sock *sk)
  1058. {
  1059. int fin_timeout = tcp_sk(sk)->linger2 ? : sysctl_tcp_fin_timeout;
  1060. const int rto = inet_csk(sk)->icsk_rto;
  1061. if (fin_timeout < (rto << 2) - (rto >> 1))
  1062. fin_timeout = (rto << 2) - (rto >> 1);
  1063. return fin_timeout;
  1064. }
  1065. static inline bool tcp_paws_check(const struct tcp_options_received *rx_opt,
  1066. int paws_win)
  1067. {
  1068. if ((s32)(rx_opt->ts_recent - rx_opt->rcv_tsval) <= paws_win)
  1069. return true;
  1070. if (unlikely(get_seconds() >= rx_opt->ts_recent_stamp + TCP_PAWS_24DAYS))
  1071. return true;
  1072. /*
  1073. * Some OSes send SYN and SYNACK messages with tsval=0 tsecr=0,
  1074. * then following tcp messages have valid values. Ignore 0 value,
  1075. * or else 'negative' tsval might forbid us to accept their packets.
  1076. */
  1077. if (!rx_opt->ts_recent)
  1078. return true;
  1079. return false;
  1080. }
  1081. static inline bool tcp_paws_reject(const struct tcp_options_received *rx_opt,
  1082. int rst)
  1083. {
  1084. if (tcp_paws_check(rx_opt, 0))
  1085. return false;
  1086. /* RST segments are not recommended to carry timestamp,
  1087. and, if they do, it is recommended to ignore PAWS because
  1088. "their cleanup function should take precedence over timestamps."
  1089. Certainly, it is mistake. It is necessary to understand the reasons
  1090. of this constraint to relax it: if peer reboots, clock may go
  1091. out-of-sync and half-open connections will not be reset.
  1092. Actually, the problem would be not existing if all
  1093. the implementations followed draft about maintaining clock
  1094. via reboots. Linux-2.2 DOES NOT!
  1095. However, we can relax time bounds for RST segments to MSL.
  1096. */
  1097. if (rst && get_seconds() >= rx_opt->ts_recent_stamp + TCP_PAWS_MSL)
  1098. return false;
  1099. return true;
  1100. }
  1101. bool tcp_oow_rate_limited(struct net *net, const struct sk_buff *skb,
  1102. int mib_idx, u32 *last_oow_ack_time);
  1103. static inline void tcp_mib_init(struct net *net)
  1104. {
  1105. /* See RFC 2012 */
  1106. TCP_ADD_STATS_USER(net, TCP_MIB_RTOALGORITHM, 1);
  1107. TCP_ADD_STATS_USER(net, TCP_MIB_RTOMIN, TCP_RTO_MIN*1000/HZ);
  1108. TCP_ADD_STATS_USER(net, TCP_MIB_RTOMAX, TCP_RTO_MAX*1000/HZ);
  1109. TCP_ADD_STATS_USER(net, TCP_MIB_MAXCONN, -1);
  1110. }
  1111. /* from STCP */
  1112. static inline void tcp_clear_retrans_hints_partial(struct tcp_sock *tp)
  1113. {
  1114. tp->lost_skb_hint = NULL;
  1115. }
  1116. static inline void tcp_clear_all_retrans_hints(struct tcp_sock *tp)
  1117. {
  1118. tcp_clear_retrans_hints_partial(tp);
  1119. tp->retransmit_skb_hint = NULL;
  1120. }
  1121. /* MD5 Signature */
  1122. struct crypto_hash;
  1123. union tcp_md5_addr {
  1124. struct in_addr a4;
  1125. #if IS_ENABLED(CONFIG_IPV6)
  1126. struct in6_addr a6;
  1127. #endif
  1128. };
  1129. /* - key database */
  1130. struct tcp_md5sig_key {
  1131. struct hlist_node node;
  1132. u8 keylen;
  1133. u8 family; /* AF_INET or AF_INET6 */
  1134. union tcp_md5_addr addr;
  1135. u8 key[TCP_MD5SIG_MAXKEYLEN];
  1136. struct rcu_head rcu;
  1137. };
  1138. /* - sock block */
  1139. struct tcp_md5sig_info {
  1140. struct hlist_head head;
  1141. struct rcu_head rcu;
  1142. };
  1143. /* - pseudo header */
  1144. struct tcp4_pseudohdr {
  1145. __be32 saddr;
  1146. __be32 daddr;
  1147. __u8 pad;
  1148. __u8 protocol;
  1149. __be16 len;
  1150. };
  1151. struct tcp6_pseudohdr {
  1152. struct in6_addr saddr;
  1153. struct in6_addr daddr;
  1154. __be32 len;
  1155. __be32 protocol; /* including padding */
  1156. };
  1157. union tcp_md5sum_block {
  1158. struct tcp4_pseudohdr ip4;
  1159. #if IS_ENABLED(CONFIG_IPV6)
  1160. struct tcp6_pseudohdr ip6;
  1161. #endif
  1162. };
  1163. /* - pool: digest algorithm, hash description and scratch buffer */
  1164. struct tcp_md5sig_pool {
  1165. struct hash_desc md5_desc;
  1166. union tcp_md5sum_block md5_blk;
  1167. };
  1168. /* - functions */
  1169. int tcp_v4_md5_hash_skb(char *md5_hash, const struct tcp_md5sig_key *key,
  1170. const struct sock *sk, const struct sk_buff *skb);
  1171. int tcp_md5_do_add(struct sock *sk, const union tcp_md5_addr *addr,
  1172. int family, const u8 *newkey, u8 newkeylen, gfp_t gfp);
  1173. int tcp_md5_do_del(struct sock *sk, const union tcp_md5_addr *addr,
  1174. int family);
  1175. struct tcp_md5sig_key *tcp_v4_md5_lookup(const struct sock *sk,
  1176. const struct sock *addr_sk);
  1177. #ifdef CONFIG_TCP_MD5SIG
  1178. struct tcp_md5sig_key *tcp_md5_do_lookup(const struct sock *sk,
  1179. const union tcp_md5_addr *addr,
  1180. int family);
  1181. #define tcp_twsk_md5_key(twsk) ((twsk)->tw_md5_key)
  1182. #else
  1183. static inline struct tcp_md5sig_key *tcp_md5_do_lookup(const struct sock *sk,
  1184. const union tcp_md5_addr *addr,
  1185. int family)
  1186. {
  1187. return NULL;
  1188. }
  1189. #define tcp_twsk_md5_key(twsk) NULL
  1190. #endif
  1191. bool tcp_alloc_md5sig_pool(void);
  1192. struct tcp_md5sig_pool *tcp_get_md5sig_pool(void);
  1193. static inline void tcp_put_md5sig_pool(void)
  1194. {
  1195. local_bh_enable();
  1196. }
  1197. int tcp_md5_hash_header(struct tcp_md5sig_pool *, const struct tcphdr *);
  1198. int tcp_md5_hash_skb_data(struct tcp_md5sig_pool *, const struct sk_buff *,
  1199. unsigned int header_len);
  1200. int tcp_md5_hash_key(struct tcp_md5sig_pool *hp,
  1201. const struct tcp_md5sig_key *key);
  1202. /* From tcp_fastopen.c */
  1203. void tcp_fastopen_cache_get(struct sock *sk, u16 *mss,
  1204. struct tcp_fastopen_cookie *cookie, int *syn_loss,
  1205. unsigned long *last_syn_loss);
  1206. void tcp_fastopen_cache_set(struct sock *sk, u16 mss,
  1207. struct tcp_fastopen_cookie *cookie, bool syn_lost,
  1208. u16 try_exp);
  1209. struct tcp_fastopen_request {
  1210. /* Fast Open cookie. Size 0 means a cookie request */
  1211. struct tcp_fastopen_cookie cookie;
  1212. struct msghdr *data; /* data in MSG_FASTOPEN */
  1213. size_t size;
  1214. int copied; /* queued in tcp_connect() */
  1215. };
  1216. void tcp_free_fastopen_req(struct tcp_sock *tp);
  1217. extern struct tcp_fastopen_context __rcu *tcp_fastopen_ctx;
  1218. int tcp_fastopen_reset_cipher(void *key, unsigned int len);
  1219. struct sock *tcp_try_fastopen(struct sock *sk, struct sk_buff *skb,
  1220. struct request_sock *req,
  1221. struct tcp_fastopen_cookie *foc,
  1222. struct dst_entry *dst);
  1223. void tcp_fastopen_init_key_once(bool publish);
  1224. #define TCP_FASTOPEN_KEY_LENGTH 16
  1225. /* Fastopen key context */
  1226. struct tcp_fastopen_context {
  1227. struct crypto_cipher *tfm;
  1228. __u8 key[TCP_FASTOPEN_KEY_LENGTH];
  1229. struct rcu_head rcu;
  1230. };
  1231. /* write queue abstraction */
  1232. static inline void tcp_write_queue_purge(struct sock *sk)
  1233. {
  1234. struct sk_buff *skb;
  1235. while ((skb = __skb_dequeue(&sk->sk_write_queue)) != NULL)
  1236. sk_wmem_free_skb(sk, skb);
  1237. sk_mem_reclaim(sk);
  1238. tcp_clear_all_retrans_hints(tcp_sk(sk));
  1239. inet_csk(sk)->icsk_backoff = 0;
  1240. }
  1241. static inline struct sk_buff *tcp_write_queue_head(const struct sock *sk)
  1242. {
  1243. return skb_peek(&sk->sk_write_queue);
  1244. }
  1245. static inline struct sk_buff *tcp_write_queue_tail(const struct sock *sk)
  1246. {
  1247. return skb_peek_tail(&sk->sk_write_queue);
  1248. }
  1249. static inline struct sk_buff *tcp_write_queue_next(const struct sock *sk,
  1250. const struct sk_buff *skb)
  1251. {
  1252. return skb_queue_next(&sk->sk_write_queue, skb);
  1253. }
  1254. static inline struct sk_buff *tcp_write_queue_prev(const struct sock *sk,
  1255. const struct sk_buff *skb)
  1256. {
  1257. return skb_queue_prev(&sk->sk_write_queue, skb);
  1258. }
  1259. #define tcp_for_write_queue(skb, sk) \
  1260. skb_queue_walk(&(sk)->sk_write_queue, skb)
  1261. #define tcp_for_write_queue_from(skb, sk) \
  1262. skb_queue_walk_from(&(sk)->sk_write_queue, skb)
  1263. #define tcp_for_write_queue_from_safe(skb, tmp, sk) \
  1264. skb_queue_walk_from_safe(&(sk)->sk_write_queue, skb, tmp)
  1265. static inline struct sk_buff *tcp_send_head(const struct sock *sk)
  1266. {
  1267. return sk->sk_send_head;
  1268. }
  1269. static inline bool tcp_skb_is_last(const struct sock *sk,
  1270. const struct sk_buff *skb)
  1271. {
  1272. return skb_queue_is_last(&sk->sk_write_queue, skb);
  1273. }
  1274. static inline void tcp_advance_send_head(struct sock *sk, const struct sk_buff *skb)
  1275. {
  1276. if (tcp_skb_is_last(sk, skb))
  1277. sk->sk_send_head = NULL;
  1278. else
  1279. sk->sk_send_head = tcp_write_queue_next(sk, skb);
  1280. }
  1281. static inline void tcp_check_send_head(struct sock *sk, struct sk_buff *skb_unlinked)
  1282. {
  1283. if (sk->sk_send_head == skb_unlinked)
  1284. sk->sk_send_head = NULL;
  1285. if (tcp_sk(sk)->highest_sack == skb_unlinked)
  1286. tcp_sk(sk)->highest_sack = NULL;
  1287. }
  1288. static inline void tcp_init_send_head(struct sock *sk)
  1289. {
  1290. sk->sk_send_head = NULL;
  1291. }
  1292. static inline void __tcp_add_write_queue_tail(struct sock *sk, struct sk_buff *skb)
  1293. {
  1294. __skb_queue_tail(&sk->sk_write_queue, skb);
  1295. }
  1296. static inline void tcp_add_write_queue_tail(struct sock *sk, struct sk_buff *skb)
  1297. {
  1298. __tcp_add_write_queue_tail(sk, skb);
  1299. /* Queue it, remembering where we must start sending. */
  1300. if (sk->sk_send_head == NULL) {
  1301. sk->sk_send_head = skb;
  1302. if (tcp_sk(sk)->highest_sack == NULL)
  1303. tcp_sk(sk)->highest_sack = skb;
  1304. }
  1305. }
  1306. static inline void __tcp_add_write_queue_head(struct sock *sk, struct sk_buff *skb)
  1307. {
  1308. __skb_queue_head(&sk->sk_write_queue, skb);
  1309. }
  1310. /* Insert buff after skb on the write queue of sk. */
  1311. static inline void tcp_insert_write_queue_after(struct sk_buff *skb,
  1312. struct sk_buff *buff,
  1313. struct sock *sk)
  1314. {
  1315. __skb_queue_after(&sk->sk_write_queue, skb, buff);
  1316. }
  1317. /* Insert new before skb on the write queue of sk. */
  1318. static inline void tcp_insert_write_queue_before(struct sk_buff *new,
  1319. struct sk_buff *skb,
  1320. struct sock *sk)
  1321. {
  1322. __skb_queue_before(&sk->sk_write_queue, skb, new);
  1323. if (sk->sk_send_head == skb)
  1324. sk->sk_send_head = new;
  1325. }
  1326. static inline void tcp_unlink_write_queue(struct sk_buff *skb, struct sock *sk)
  1327. {
  1328. __skb_unlink(skb, &sk->sk_write_queue);
  1329. }
  1330. static inline bool tcp_write_queue_empty(struct sock *sk)
  1331. {
  1332. return skb_queue_empty(&sk->sk_write_queue);
  1333. }
  1334. static inline void tcp_push_pending_frames(struct sock *sk)
  1335. {
  1336. if (tcp_send_head(sk)) {
  1337. struct tcp_sock *tp = tcp_sk(sk);
  1338. __tcp_push_pending_frames(sk, tcp_current_mss(sk), tp->nonagle);
  1339. }
  1340. }
  1341. /* Start sequence of the skb just after the highest skb with SACKed
  1342. * bit, valid only if sacked_out > 0 or when the caller has ensured
  1343. * validity by itself.
  1344. */
  1345. static inline u32 tcp_highest_sack_seq(struct tcp_sock *tp)
  1346. {
  1347. if (!tp->sacked_out)
  1348. return tp->snd_una;
  1349. if (tp->highest_sack == NULL)
  1350. return tp->snd_nxt;
  1351. return TCP_SKB_CB(tp->highest_sack)->seq;
  1352. }
  1353. static inline void tcp_advance_highest_sack(struct sock *sk, struct sk_buff *skb)
  1354. {
  1355. tcp_sk(sk)->highest_sack = tcp_skb_is_last(sk, skb) ? NULL :
  1356. tcp_write_queue_next(sk, skb);
  1357. }
  1358. static inline struct sk_buff *tcp_highest_sack(struct sock *sk)
  1359. {
  1360. return tcp_sk(sk)->highest_sack;
  1361. }
  1362. static inline void tcp_highest_sack_reset(struct sock *sk)
  1363. {
  1364. tcp_sk(sk)->highest_sack = tcp_write_queue_head(sk);
  1365. }
  1366. /* Called when old skb is about to be deleted and replaced by new skb */
  1367. static inline void tcp_highest_sack_replace(struct sock *sk,
  1368. struct sk_buff *old,
  1369. struct sk_buff *new)
  1370. {
  1371. if (old == tcp_highest_sack(sk))
  1372. tcp_sk(sk)->highest_sack = new;
  1373. }
  1374. /* Determines whether this is a thin stream (which may suffer from
  1375. * increased latency). Used to trigger latency-reducing mechanisms.
  1376. */
  1377. static inline bool tcp_stream_is_thin(struct tcp_sock *tp)
  1378. {
  1379. return tp->packets_out < 4 && !tcp_in_initial_slowstart(tp);
  1380. }
  1381. /* /proc */
  1382. enum tcp_seq_states {
  1383. TCP_SEQ_STATE_LISTENING,
  1384. TCP_SEQ_STATE_ESTABLISHED,
  1385. };
  1386. int tcp_seq_open(struct inode *inode, struct file *file);
  1387. struct tcp_seq_afinfo {
  1388. char *name;
  1389. sa_family_t family;
  1390. const struct file_operations *seq_fops;
  1391. struct seq_operations seq_ops;
  1392. };
  1393. struct tcp_iter_state {
  1394. struct seq_net_private p;
  1395. sa_family_t family;
  1396. enum tcp_seq_states state;
  1397. struct sock *syn_wait_sk;
  1398. int bucket, offset, sbucket, num;
  1399. loff_t last_pos;
  1400. };
  1401. int tcp_proc_register(struct net *net, struct tcp_seq_afinfo *afinfo);
  1402. void tcp_proc_unregister(struct net *net, struct tcp_seq_afinfo *afinfo);
  1403. extern struct request_sock_ops tcp_request_sock_ops;
  1404. extern struct request_sock_ops tcp6_request_sock_ops;
  1405. void tcp_v4_destroy_sock(struct sock *sk);
  1406. struct sk_buff *tcp_gso_segment(struct sk_buff *skb,
  1407. netdev_features_t features);
  1408. struct sk_buff **tcp_gro_receive(struct sk_buff **head, struct sk_buff *skb);
  1409. int tcp_gro_complete(struct sk_buff *skb);
  1410. void __tcp_v4_send_check(struct sk_buff *skb, __be32 saddr, __be32 daddr);
  1411. static inline u32 tcp_notsent_lowat(const struct tcp_sock *tp)
  1412. {
  1413. return tp->notsent_lowat ?: sysctl_tcp_notsent_lowat;
  1414. }
  1415. static inline bool tcp_stream_memory_free(const struct sock *sk)
  1416. {
  1417. const struct tcp_sock *tp = tcp_sk(sk);
  1418. u32 notsent_bytes = tp->write_seq - tp->snd_nxt;
  1419. return notsent_bytes < tcp_notsent_lowat(tp);
  1420. }
  1421. #ifdef CONFIG_PROC_FS
  1422. int tcp4_proc_init(void);
  1423. void tcp4_proc_exit(void);
  1424. #endif
  1425. int tcp_rtx_synack(const struct sock *sk, struct request_sock *req);
  1426. int tcp_conn_request(struct request_sock_ops *rsk_ops,
  1427. const struct tcp_request_sock_ops *af_ops,
  1428. struct sock *sk, struct sk_buff *skb);
  1429. /* TCP af-specific functions */
  1430. struct tcp_sock_af_ops {
  1431. #ifdef CONFIG_TCP_MD5SIG
  1432. struct tcp_md5sig_key *(*md5_lookup) (const struct sock *sk,
  1433. const struct sock *addr_sk);
  1434. int (*calc_md5_hash)(char *location,
  1435. const struct tcp_md5sig_key *md5,
  1436. const struct sock *sk,
  1437. const struct sk_buff *skb);
  1438. int (*md5_parse)(struct sock *sk,
  1439. char __user *optval,
  1440. int optlen);
  1441. #endif
  1442. };
  1443. struct tcp_request_sock_ops {
  1444. u16 mss_clamp;
  1445. #ifdef CONFIG_TCP_MD5SIG
  1446. struct tcp_md5sig_key *(*req_md5_lookup)(const struct sock *sk,
  1447. const struct sock *addr_sk);
  1448. int (*calc_md5_hash) (char *location,
  1449. const struct tcp_md5sig_key *md5,
  1450. const struct sock *sk,
  1451. const struct sk_buff *skb);
  1452. #endif
  1453. void (*init_req)(struct request_sock *req,
  1454. const struct sock *sk_listener,
  1455. struct sk_buff *skb);
  1456. #ifdef CONFIG_SYN_COOKIES
  1457. __u32 (*cookie_init_seq)(const struct sk_buff *skb,
  1458. __u16 *mss);
  1459. #endif
  1460. struct dst_entry *(*route_req)(const struct sock *sk, struct flowi *fl,
  1461. const struct request_sock *req,
  1462. bool *strict);
  1463. __u32 (*init_seq)(const struct sk_buff *skb);
  1464. int (*send_synack)(const struct sock *sk, struct dst_entry *dst,
  1465. struct flowi *fl, struct request_sock *req,
  1466. struct tcp_fastopen_cookie *foc,
  1467. bool attach_req);
  1468. };
  1469. #ifdef CONFIG_SYN_COOKIES
  1470. static inline __u32 cookie_init_sequence(const struct tcp_request_sock_ops *ops,
  1471. const struct sock *sk, struct sk_buff *skb,
  1472. __u16 *mss)
  1473. {
  1474. tcp_synq_overflow(sk);
  1475. NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_SYNCOOKIESSENT);
  1476. return ops->cookie_init_seq(skb, mss);
  1477. }
  1478. #else
  1479. static inline __u32 cookie_init_sequence(const struct tcp_request_sock_ops *ops,
  1480. const struct sock *sk, struct sk_buff *skb,
  1481. __u16 *mss)
  1482. {
  1483. return 0;
  1484. }
  1485. #endif
  1486. int tcpv4_offload_init(void);
  1487. void tcp_v4_init(void);
  1488. void tcp_init(void);
  1489. /* tcp_recovery.c */
  1490. /* Flags to enable various loss recovery features. See below */
  1491. extern int sysctl_tcp_recovery;
  1492. /* Use TCP RACK to detect (some) tail and retransmit losses */
  1493. #define TCP_RACK_LOST_RETRANS 0x1
  1494. extern int tcp_rack_mark_lost(struct sock *sk);
  1495. extern void tcp_rack_advance(struct tcp_sock *tp,
  1496. const struct skb_mstamp *xmit_time, u8 sacked);
  1497. /*
  1498. * Save and compile IPv4 options, return a pointer to it
  1499. */
  1500. static inline struct ip_options_rcu *tcp_v4_save_options(struct sk_buff *skb)
  1501. {
  1502. const struct ip_options *opt = &TCP_SKB_CB(skb)->header.h4.opt;
  1503. struct ip_options_rcu *dopt = NULL;
  1504. if (opt->optlen) {
  1505. int opt_size = sizeof(*dopt) + opt->optlen;
  1506. dopt = kmalloc(opt_size, GFP_ATOMIC);
  1507. if (dopt && __ip_options_echo(&dopt->opt, skb, opt)) {
  1508. kfree(dopt);
  1509. dopt = NULL;
  1510. }
  1511. }
  1512. return dopt;
  1513. }
  1514. /* locally generated TCP pure ACKs have skb->truesize == 2
  1515. * (check tcp_send_ack() in net/ipv4/tcp_output.c )
  1516. * This is much faster than dissecting the packet to find out.
  1517. * (Think of GRE encapsulations, IPv4, IPv6, ...)
  1518. */
  1519. static inline bool skb_is_tcp_pure_ack(const struct sk_buff *skb)
  1520. {
  1521. return skb->truesize == 2;
  1522. }
  1523. static inline void skb_set_tcp_pure_ack(struct sk_buff *skb)
  1524. {
  1525. skb->truesize = 2;
  1526. }
  1527. #endif /* _TCP_H */