tcp.h 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194
  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/cryptohash.h>
  29. #include <linux/kref.h>
  30. #include <linux/ktime.h>
  31. #include <net/inet_connection_sock.h>
  32. #include <net/inet_timewait_sock.h>
  33. #include <net/inet_hashtables.h>
  34. #include <net/checksum.h>
  35. #include <net/request_sock.h>
  36. #include <net/sock_reuseport.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. #include <linux/bpf-cgroup.h>
  46. extern struct inet_hashinfo tcp_hashinfo;
  47. extern struct percpu_counter tcp_orphan_count;
  48. void tcp_time_wait(struct sock *sk, int state, int timeo);
  49. #define MAX_TCP_HEADER (128 + MAX_HEADER)
  50. #define MAX_TCP_OPTION_SPACE 40
  51. #define TCP_MIN_SND_MSS 48
  52. #define TCP_MIN_GSO_SIZE (TCP_MIN_SND_MSS - MAX_TCP_OPTION_SPACE)
  53. /*
  54. * Never offer a window over 32767 without using window scaling. Some
  55. * poor stacks do signed 16bit maths!
  56. */
  57. #define MAX_TCP_WINDOW 32767U
  58. /* Minimal accepted MSS. It is (60+60+8) - (20+20). */
  59. #define TCP_MIN_MSS 88U
  60. /* The least MTU to use for probing */
  61. #define TCP_BASE_MSS 1024
  62. /* probing interval, default to 10 minutes as per RFC4821 */
  63. #define TCP_PROBE_INTERVAL 600
  64. /* Specify interval when tcp mtu probing will stop */
  65. #define TCP_PROBE_THRESHOLD 8
  66. /* After receiving this amount of duplicate ACKs fast retransmit starts. */
  67. #define TCP_FASTRETRANS_THRESH 3
  68. /* Maximal number of ACKs sent quickly to accelerate slow-start. */
  69. #define TCP_MAX_QUICKACKS 16U
  70. /* Maximal number of window scale according to RFC1323 */
  71. #define TCP_MAX_WSCALE 14U
  72. /* urg_data states */
  73. #define TCP_URG_VALID 0x0100
  74. #define TCP_URG_NOTYET 0x0200
  75. #define TCP_URG_READ 0x0400
  76. #define TCP_RETR1 3 /*
  77. * This is how many retries it does before it
  78. * tries to figure out if the gateway is
  79. * down. Minimal RFC value is 3; it corresponds
  80. * to ~3sec-8min depending on RTO.
  81. */
  82. #define TCP_RETR2 15 /*
  83. * This should take at least
  84. * 90 minutes to time out.
  85. * RFC1122 says that the limit is 100 sec.
  86. * 15 is ~13-30min depending on RTO.
  87. */
  88. #define TCP_SYN_RETRIES 6 /* This is how many retries are done
  89. * when active opening a connection.
  90. * RFC1122 says the minimum retry MUST
  91. * be at least 180secs. Nevertheless
  92. * this value is corresponding to
  93. * 63secs of retransmission with the
  94. * current initial RTO.
  95. */
  96. #define TCP_SYNACK_RETRIES 5 /* This is how may retries are done
  97. * when passive opening a connection.
  98. * This is corresponding to 31secs of
  99. * retransmission with the current
  100. * initial RTO.
  101. */
  102. #define TCP_TIMEWAIT_LEN (60*HZ) /* how long to wait to destroy TIME-WAIT
  103. * state, about 60 seconds */
  104. #define TCP_FIN_TIMEOUT TCP_TIMEWAIT_LEN
  105. /* BSD style FIN_WAIT2 deadlock breaker.
  106. * It used to be 3min, new value is 60sec,
  107. * to combine FIN-WAIT-2 timeout with
  108. * TIME-WAIT timer.
  109. */
  110. #define TCP_DELACK_MAX ((unsigned)(HZ/5)) /* maximal time to delay before sending an ACK */
  111. #if HZ >= 100
  112. #define TCP_DELACK_MIN ((unsigned)(HZ/25)) /* minimal time to delay before sending an ACK */
  113. #define TCP_ATO_MIN ((unsigned)(HZ/25))
  114. #else
  115. #define TCP_DELACK_MIN 4U
  116. #define TCP_ATO_MIN 4U
  117. #endif
  118. #define TCP_RTO_MAX ((unsigned)(120*HZ))
  119. #define TCP_RTO_MIN ((unsigned)(HZ/5))
  120. #define TCP_TIMEOUT_MIN (2U) /* Min timeout for TCP timers in jiffies */
  121. #define TCP_TIMEOUT_INIT ((unsigned)(1*HZ)) /* RFC6298 2.1 initial RTO value */
  122. #define TCP_TIMEOUT_FALLBACK ((unsigned)(3*HZ)) /* RFC 1122 initial RTO value, now
  123. * used as a fallback RTO for the
  124. * initial data transmission if no
  125. * valid RTT sample has been acquired,
  126. * most likely due to retrans in 3WHS.
  127. */
  128. #define TCP_RESOURCE_PROBE_INTERVAL ((unsigned)(HZ/2U)) /* Maximal interval between probes
  129. * for local resources.
  130. */
  131. #define TCP_KEEPALIVE_TIME (120*60*HZ) /* two hours */
  132. #define TCP_KEEPALIVE_PROBES 9 /* Max of 9 keepalive probes */
  133. #define TCP_KEEPALIVE_INTVL (75*HZ)
  134. #define MAX_TCP_KEEPIDLE 32767
  135. #define MAX_TCP_KEEPINTVL 32767
  136. #define MAX_TCP_KEEPCNT 127
  137. #define MAX_TCP_SYNCNT 127
  138. #define TCP_SYNQ_INTERVAL (HZ/5) /* Period of SYNACK timer */
  139. #define TCP_PAWS_24DAYS (60 * 60 * 24 * 24)
  140. #define TCP_PAWS_MSL 60 /* Per-host timestamps are invalidated
  141. * after this time. It should be equal
  142. * (or greater than) TCP_TIMEWAIT_LEN
  143. * to provide reliability equal to one
  144. * provided by timewait state.
  145. */
  146. #define TCP_PAWS_WINDOW 1 /* Replay window for per-host
  147. * timestamps. It must be less than
  148. * minimal timewait lifetime.
  149. */
  150. /*
  151. * TCP option
  152. */
  153. #define TCPOPT_NOP 1 /* Padding */
  154. #define TCPOPT_EOL 0 /* End of options */
  155. #define TCPOPT_MSS 2 /* Segment size negotiating */
  156. #define TCPOPT_WINDOW 3 /* Window scaling */
  157. #define TCPOPT_SACK_PERM 4 /* SACK Permitted */
  158. #define TCPOPT_SACK 5 /* SACK Block */
  159. #define TCPOPT_TIMESTAMP 8 /* Better RTT estimations/PAWS */
  160. #define TCPOPT_MD5SIG 19 /* MD5 Signature (RFC2385) */
  161. #define TCPOPT_FASTOPEN 34 /* Fast open (RFC7413) */
  162. #define TCPOPT_EXP 254 /* Experimental */
  163. /* Magic number to be after the option value for sharing TCP
  164. * experimental options. See draft-ietf-tcpm-experimental-options-00.txt
  165. */
  166. #define TCPOPT_FASTOPEN_MAGIC 0xF989
  167. #define TCPOPT_SMC_MAGIC 0xE2D4C3D9
  168. /*
  169. * TCP option lengths
  170. */
  171. #define TCPOLEN_MSS 4
  172. #define TCPOLEN_WINDOW 3
  173. #define TCPOLEN_SACK_PERM 2
  174. #define TCPOLEN_TIMESTAMP 10
  175. #define TCPOLEN_MD5SIG 18
  176. #define TCPOLEN_FASTOPEN_BASE 2
  177. #define TCPOLEN_EXP_FASTOPEN_BASE 4
  178. #define TCPOLEN_EXP_SMC_BASE 6
  179. /* But this is what stacks really send out. */
  180. #define TCPOLEN_TSTAMP_ALIGNED 12
  181. #define TCPOLEN_WSCALE_ALIGNED 4
  182. #define TCPOLEN_SACKPERM_ALIGNED 4
  183. #define TCPOLEN_SACK_BASE 2
  184. #define TCPOLEN_SACK_BASE_ALIGNED 4
  185. #define TCPOLEN_SACK_PERBLOCK 8
  186. #define TCPOLEN_MD5SIG_ALIGNED 20
  187. #define TCPOLEN_MSS_ALIGNED 4
  188. #define TCPOLEN_EXP_SMC_BASE_ALIGNED 8
  189. /* Flags in tp->nonagle */
  190. #define TCP_NAGLE_OFF 1 /* Nagle's algo is disabled */
  191. #define TCP_NAGLE_CORK 2 /* Socket is corked */
  192. #define TCP_NAGLE_PUSH 4 /* Cork is overridden for already queued data */
  193. /* TCP thin-stream limits */
  194. #define TCP_THIN_LINEAR_RETRIES 6 /* After 6 linear retries, do exp. backoff */
  195. /* TCP initial congestion window as per rfc6928 */
  196. #define TCP_INIT_CWND 10
  197. /* Bit Flags for sysctl_tcp_fastopen */
  198. #define TFO_CLIENT_ENABLE 1
  199. #define TFO_SERVER_ENABLE 2
  200. #define TFO_CLIENT_NO_COOKIE 4 /* Data in SYN w/o cookie option */
  201. /* Accept SYN data w/o any cookie option */
  202. #define TFO_SERVER_COOKIE_NOT_REQD 0x200
  203. /* Force enable TFO on all listeners, i.e., not requiring the
  204. * TCP_FASTOPEN socket option.
  205. */
  206. #define TFO_SERVER_WO_SOCKOPT1 0x400
  207. /* sysctl variables for tcp */
  208. extern int sysctl_tcp_max_orphans;
  209. extern long sysctl_tcp_mem[3];
  210. #define TCP_RACK_LOSS_DETECTION 0x1 /* Use RACK to detect losses */
  211. #define TCP_RACK_STATIC_REO_WND 0x2 /* Use static RACK reo wnd */
  212. #define TCP_RACK_NO_DUPTHRESH 0x4 /* Do not use DUPACK threshold in RACK */
  213. extern atomic_long_t tcp_memory_allocated;
  214. extern struct percpu_counter tcp_sockets_allocated;
  215. extern unsigned long tcp_memory_pressure;
  216. /* optimized version of sk_under_memory_pressure() for TCP sockets */
  217. static inline bool tcp_under_memory_pressure(const struct sock *sk)
  218. {
  219. if (mem_cgroup_sockets_enabled && sk->sk_memcg &&
  220. mem_cgroup_under_socket_pressure(sk->sk_memcg))
  221. return true;
  222. return READ_ONCE(tcp_memory_pressure);
  223. }
  224. /*
  225. * The next routines deal with comparing 32 bit unsigned ints
  226. * and worry about wraparound (automatic with unsigned arithmetic).
  227. */
  228. static inline bool before(__u32 seq1, __u32 seq2)
  229. {
  230. return (__s32)(seq1-seq2) < 0;
  231. }
  232. #define after(seq2, seq1) before(seq1, seq2)
  233. /* is s2<=s1<=s3 ? */
  234. static inline bool between(__u32 seq1, __u32 seq2, __u32 seq3)
  235. {
  236. return seq3 - seq2 >= seq1 - seq2;
  237. }
  238. static inline bool tcp_out_of_memory(struct sock *sk)
  239. {
  240. if (sk->sk_wmem_queued > SOCK_MIN_SNDBUF &&
  241. sk_memory_allocated(sk) > sk_prot_mem_limits(sk, 2))
  242. return true;
  243. return false;
  244. }
  245. void sk_forced_mem_schedule(struct sock *sk, int size);
  246. static inline bool tcp_too_many_orphans(struct sock *sk, int shift)
  247. {
  248. struct percpu_counter *ocp = sk->sk_prot->orphan_count;
  249. int orphans = percpu_counter_read_positive(ocp);
  250. if (orphans << shift > sysctl_tcp_max_orphans) {
  251. orphans = percpu_counter_sum_positive(ocp);
  252. if (orphans << shift > sysctl_tcp_max_orphans)
  253. return true;
  254. }
  255. return false;
  256. }
  257. bool tcp_check_oom(struct sock *sk, int shift);
  258. extern struct proto tcp_prot;
  259. #define TCP_INC_STATS(net, field) SNMP_INC_STATS((net)->mib.tcp_statistics, field)
  260. #define __TCP_INC_STATS(net, field) __SNMP_INC_STATS((net)->mib.tcp_statistics, field)
  261. #define TCP_DEC_STATS(net, field) SNMP_DEC_STATS((net)->mib.tcp_statistics, field)
  262. #define TCP_ADD_STATS(net, field, val) SNMP_ADD_STATS((net)->mib.tcp_statistics, field, val)
  263. void tcp_tasklet_init(void);
  264. void tcp_v4_err(struct sk_buff *skb, u32);
  265. void tcp_shutdown(struct sock *sk, int how);
  266. int tcp_v4_early_demux(struct sk_buff *skb);
  267. int tcp_v4_rcv(struct sk_buff *skb);
  268. int tcp_v4_tw_remember_stamp(struct inet_timewait_sock *tw);
  269. int tcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t size);
  270. int tcp_sendmsg_locked(struct sock *sk, struct msghdr *msg, size_t size);
  271. int tcp_sendpage(struct sock *sk, struct page *page, int offset, size_t size,
  272. int flags);
  273. int tcp_sendpage_locked(struct sock *sk, struct page *page, int offset,
  274. size_t size, int flags);
  275. ssize_t do_tcp_sendpages(struct sock *sk, struct page *page, int offset,
  276. size_t size, int flags);
  277. void tcp_release_cb(struct sock *sk);
  278. void tcp_wfree(struct sk_buff *skb);
  279. void tcp_write_timer_handler(struct sock *sk);
  280. void tcp_delack_timer_handler(struct sock *sk);
  281. int tcp_ioctl(struct sock *sk, int cmd, unsigned long arg);
  282. int tcp_rcv_state_process(struct sock *sk, struct sk_buff *skb);
  283. void tcp_rcv_established(struct sock *sk, struct sk_buff *skb);
  284. void tcp_rcv_space_adjust(struct sock *sk);
  285. int tcp_twsk_unique(struct sock *sk, struct sock *sktw, void *twp);
  286. void tcp_twsk_destructor(struct sock *sk);
  287. ssize_t tcp_splice_read(struct socket *sk, loff_t *ppos,
  288. struct pipe_inode_info *pipe, size_t len,
  289. unsigned int flags);
  290. void tcp_enter_quickack_mode(struct sock *sk, unsigned int max_quickacks);
  291. static inline void tcp_dec_quickack_mode(struct sock *sk,
  292. const unsigned int pkts)
  293. {
  294. struct inet_connection_sock *icsk = inet_csk(sk);
  295. if (icsk->icsk_ack.quick) {
  296. if (pkts >= icsk->icsk_ack.quick) {
  297. icsk->icsk_ack.quick = 0;
  298. /* Leaving quickack mode we deflate ATO. */
  299. icsk->icsk_ack.ato = TCP_ATO_MIN;
  300. } else
  301. icsk->icsk_ack.quick -= pkts;
  302. }
  303. }
  304. #define TCP_ECN_OK 1
  305. #define TCP_ECN_QUEUE_CWR 2
  306. #define TCP_ECN_DEMAND_CWR 4
  307. #define TCP_ECN_SEEN 8
  308. enum tcp_tw_status {
  309. TCP_TW_SUCCESS = 0,
  310. TCP_TW_RST = 1,
  311. TCP_TW_ACK = 2,
  312. TCP_TW_SYN = 3
  313. };
  314. enum tcp_tw_status tcp_timewait_state_process(struct inet_timewait_sock *tw,
  315. struct sk_buff *skb,
  316. const struct tcphdr *th);
  317. struct sock *tcp_check_req(struct sock *sk, struct sk_buff *skb,
  318. struct request_sock *req, bool fastopen,
  319. bool *lost_race);
  320. int tcp_child_process(struct sock *parent, struct sock *child,
  321. struct sk_buff *skb);
  322. void tcp_enter_loss(struct sock *sk);
  323. void tcp_cwnd_reduction(struct sock *sk, int newly_acked_sacked, int flag);
  324. void tcp_clear_retrans(struct tcp_sock *tp);
  325. void tcp_update_metrics(struct sock *sk);
  326. void tcp_init_metrics(struct sock *sk);
  327. void tcp_metrics_init(void);
  328. bool tcp_peer_is_proven(struct request_sock *req, struct dst_entry *dst);
  329. void tcp_close(struct sock *sk, long timeout);
  330. void tcp_init_sock(struct sock *sk);
  331. void tcp_init_transfer(struct sock *sk, int bpf_op);
  332. __poll_t tcp_poll(struct file *file, struct socket *sock,
  333. struct poll_table_struct *wait);
  334. int tcp_getsockopt(struct sock *sk, int level, int optname,
  335. char __user *optval, int __user *optlen);
  336. int tcp_setsockopt(struct sock *sk, int level, int optname,
  337. char __user *optval, unsigned int optlen);
  338. int compat_tcp_getsockopt(struct sock *sk, int level, int optname,
  339. char __user *optval, int __user *optlen);
  340. int compat_tcp_setsockopt(struct sock *sk, int level, int optname,
  341. char __user *optval, unsigned int optlen);
  342. void tcp_set_keepalive(struct sock *sk, int val);
  343. void tcp_syn_ack_timeout(const struct request_sock *req);
  344. int tcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int nonblock,
  345. int flags, int *addr_len);
  346. int tcp_set_rcvlowat(struct sock *sk, int val);
  347. void tcp_data_ready(struct sock *sk);
  348. int tcp_mmap(struct file *file, struct socket *sock,
  349. struct vm_area_struct *vma);
  350. void tcp_parse_options(const struct net *net, const struct sk_buff *skb,
  351. struct tcp_options_received *opt_rx,
  352. int estab, struct tcp_fastopen_cookie *foc);
  353. const u8 *tcp_parse_md5sig_option(const struct tcphdr *th);
  354. /*
  355. * TCP v4 functions exported for the inet6 API
  356. */
  357. void tcp_v4_send_check(struct sock *sk, struct sk_buff *skb);
  358. void tcp_v4_mtu_reduced(struct sock *sk);
  359. void tcp_req_err(struct sock *sk, u32 seq, bool abort);
  360. int tcp_v4_conn_request(struct sock *sk, struct sk_buff *skb);
  361. struct sock *tcp_create_openreq_child(const struct sock *sk,
  362. struct request_sock *req,
  363. struct sk_buff *skb);
  364. void tcp_ca_openreq_child(struct sock *sk, const struct dst_entry *dst);
  365. struct sock *tcp_v4_syn_recv_sock(const struct sock *sk, struct sk_buff *skb,
  366. struct request_sock *req,
  367. struct dst_entry *dst,
  368. struct request_sock *req_unhash,
  369. bool *own_req);
  370. int tcp_v4_do_rcv(struct sock *sk, struct sk_buff *skb);
  371. int tcp_v4_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len);
  372. int tcp_connect(struct sock *sk);
  373. enum tcp_synack_type {
  374. TCP_SYNACK_NORMAL,
  375. TCP_SYNACK_FASTOPEN,
  376. TCP_SYNACK_COOKIE,
  377. };
  378. struct sk_buff *tcp_make_synack(const struct sock *sk, struct dst_entry *dst,
  379. struct request_sock *req,
  380. struct tcp_fastopen_cookie *foc,
  381. enum tcp_synack_type synack_type);
  382. int tcp_disconnect(struct sock *sk, int flags);
  383. void tcp_finish_connect(struct sock *sk, struct sk_buff *skb);
  384. int tcp_send_rcvq(struct sock *sk, struct msghdr *msg, size_t size);
  385. void inet_sk_rx_dst_set(struct sock *sk, const struct sk_buff *skb);
  386. /* From syncookies.c */
  387. struct sock *tcp_get_cookie_sock(struct sock *sk, struct sk_buff *skb,
  388. struct request_sock *req,
  389. struct dst_entry *dst, u32 tsoff);
  390. int __cookie_v4_check(const struct iphdr *iph, const struct tcphdr *th,
  391. u32 cookie);
  392. struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb);
  393. #ifdef CONFIG_SYN_COOKIES
  394. /* Syncookies use a monotonic timer which increments every 60 seconds.
  395. * This counter is used both as a hash input and partially encoded into
  396. * the cookie value. A cookie is only validated further if the delta
  397. * between the current counter value and the encoded one is less than this,
  398. * i.e. a sent cookie is valid only at most for 2*60 seconds (or less if
  399. * the counter advances immediately after a cookie is generated).
  400. */
  401. #define MAX_SYNCOOKIE_AGE 2
  402. #define TCP_SYNCOOKIE_PERIOD (60 * HZ)
  403. #define TCP_SYNCOOKIE_VALID (MAX_SYNCOOKIE_AGE * TCP_SYNCOOKIE_PERIOD)
  404. /* syncookies: remember time of last synqueue overflow
  405. * But do not dirty this field too often (once per second is enough)
  406. * It is racy as we do not hold a lock, but race is very minor.
  407. */
  408. static inline void tcp_synq_overflow(const struct sock *sk)
  409. {
  410. unsigned int last_overflow;
  411. unsigned int now = jiffies;
  412. if (sk->sk_reuseport) {
  413. struct sock_reuseport *reuse;
  414. reuse = rcu_dereference(sk->sk_reuseport_cb);
  415. if (likely(reuse)) {
  416. last_overflow = READ_ONCE(reuse->synq_overflow_ts);
  417. if (!time_between32(now, last_overflow,
  418. last_overflow + HZ))
  419. WRITE_ONCE(reuse->synq_overflow_ts, now);
  420. return;
  421. }
  422. }
  423. last_overflow = READ_ONCE(tcp_sk(sk)->rx_opt.ts_recent_stamp);
  424. if (!time_between32(now, last_overflow, last_overflow + HZ))
  425. WRITE_ONCE(tcp_sk(sk)->rx_opt.ts_recent_stamp, now);
  426. }
  427. /* syncookies: no recent synqueue overflow on this listening socket? */
  428. static inline bool tcp_synq_no_recent_overflow(const struct sock *sk)
  429. {
  430. unsigned int last_overflow;
  431. unsigned int now = jiffies;
  432. if (sk->sk_reuseport) {
  433. struct sock_reuseport *reuse;
  434. reuse = rcu_dereference(sk->sk_reuseport_cb);
  435. if (likely(reuse)) {
  436. last_overflow = READ_ONCE(reuse->synq_overflow_ts);
  437. return !time_between32(now, last_overflow - HZ,
  438. last_overflow +
  439. TCP_SYNCOOKIE_VALID);
  440. }
  441. }
  442. last_overflow = READ_ONCE(tcp_sk(sk)->rx_opt.ts_recent_stamp);
  443. /* If last_overflow <= jiffies <= last_overflow + TCP_SYNCOOKIE_VALID,
  444. * then we're under synflood. However, we have to use
  445. * 'last_overflow - HZ' as lower bound. That's because a concurrent
  446. * tcp_synq_overflow() could update .ts_recent_stamp after we read
  447. * jiffies but before we store .ts_recent_stamp into last_overflow,
  448. * which could lead to rejecting a valid syncookie.
  449. */
  450. return !time_between32(now, last_overflow - HZ,
  451. last_overflow + TCP_SYNCOOKIE_VALID);
  452. }
  453. static inline u32 tcp_cookie_time(void)
  454. {
  455. u64 val = get_jiffies_64();
  456. do_div(val, TCP_SYNCOOKIE_PERIOD);
  457. return val;
  458. }
  459. u32 __cookie_v4_init_sequence(const struct iphdr *iph, const struct tcphdr *th,
  460. u16 *mssp);
  461. __u32 cookie_v4_init_sequence(const struct sk_buff *skb, __u16 *mss);
  462. u64 cookie_init_timestamp(struct request_sock *req);
  463. bool cookie_timestamp_decode(const struct net *net,
  464. struct tcp_options_received *opt);
  465. bool cookie_ecn_ok(const struct tcp_options_received *opt,
  466. const struct net *net, const struct dst_entry *dst);
  467. /* From net/ipv6/syncookies.c */
  468. int __cookie_v6_check(const struct ipv6hdr *iph, const struct tcphdr *th,
  469. u32 cookie);
  470. struct sock *cookie_v6_check(struct sock *sk, struct sk_buff *skb);
  471. u32 __cookie_v6_init_sequence(const struct ipv6hdr *iph,
  472. const struct tcphdr *th, u16 *mssp);
  473. __u32 cookie_v6_init_sequence(const struct sk_buff *skb, __u16 *mss);
  474. #endif
  475. /* tcp_output.c */
  476. void __tcp_push_pending_frames(struct sock *sk, unsigned int cur_mss,
  477. int nonagle);
  478. int __tcp_retransmit_skb(struct sock *sk, struct sk_buff *skb, int segs);
  479. int tcp_retransmit_skb(struct sock *sk, struct sk_buff *skb, int segs);
  480. void tcp_retransmit_timer(struct sock *sk);
  481. void tcp_xmit_retransmit_queue(struct sock *);
  482. void tcp_simple_retransmit(struct sock *);
  483. void tcp_enter_recovery(struct sock *sk, bool ece_ack);
  484. int tcp_trim_head(struct sock *, struct sk_buff *, u32);
  485. enum tcp_queue {
  486. TCP_FRAG_IN_WRITE_QUEUE,
  487. TCP_FRAG_IN_RTX_QUEUE,
  488. };
  489. int tcp_fragment(struct sock *sk, enum tcp_queue tcp_queue,
  490. struct sk_buff *skb, u32 len,
  491. unsigned int mss_now, gfp_t gfp);
  492. void tcp_send_probe0(struct sock *);
  493. void tcp_send_partial(struct sock *);
  494. int tcp_write_wakeup(struct sock *, int mib);
  495. void tcp_send_fin(struct sock *sk);
  496. void tcp_send_active_reset(struct sock *sk, gfp_t priority);
  497. int tcp_send_synack(struct sock *);
  498. void tcp_push_one(struct sock *, unsigned int mss_now);
  499. void __tcp_send_ack(struct sock *sk, u32 rcv_nxt);
  500. void tcp_send_ack(struct sock *sk);
  501. void tcp_send_delayed_ack(struct sock *sk);
  502. void tcp_send_loss_probe(struct sock *sk);
  503. bool tcp_schedule_loss_probe(struct sock *sk, bool advancing_rto);
  504. void tcp_skb_collapse_tstamp(struct sk_buff *skb,
  505. const struct sk_buff *next_skb);
  506. /* tcp_input.c */
  507. void tcp_rearm_rto(struct sock *sk);
  508. void tcp_synack_rtt_meas(struct sock *sk, struct request_sock *req);
  509. void tcp_reset(struct sock *sk);
  510. void tcp_skb_mark_lost_uncond_verify(struct tcp_sock *tp, struct sk_buff *skb);
  511. void tcp_fin(struct sock *sk);
  512. /* tcp_timer.c */
  513. void tcp_init_xmit_timers(struct sock *);
  514. static inline void tcp_clear_xmit_timers(struct sock *sk)
  515. {
  516. if (hrtimer_try_to_cancel(&tcp_sk(sk)->pacing_timer) == 1)
  517. __sock_put(sk);
  518. if (hrtimer_try_to_cancel(&tcp_sk(sk)->compressed_ack_timer) == 1)
  519. __sock_put(sk);
  520. inet_csk_clear_xmit_timers(sk);
  521. }
  522. unsigned int tcp_sync_mss(struct sock *sk, u32 pmtu);
  523. unsigned int tcp_current_mss(struct sock *sk);
  524. /* Bound MSS / TSO packet size with the half of the window */
  525. static inline int tcp_bound_to_half_wnd(struct tcp_sock *tp, int pktsize)
  526. {
  527. int cutoff;
  528. /* When peer uses tiny windows, there is no use in packetizing
  529. * to sub-MSS pieces for the sake of SWS or making sure there
  530. * are enough packets in the pipe for fast recovery.
  531. *
  532. * On the other hand, for extremely large MSS devices, handling
  533. * smaller than MSS windows in this way does make sense.
  534. */
  535. if (tp->max_window > TCP_MSS_DEFAULT)
  536. cutoff = (tp->max_window >> 1);
  537. else
  538. cutoff = tp->max_window;
  539. if (cutoff && pktsize > cutoff)
  540. return max_t(int, cutoff, 68U - tp->tcp_header_len);
  541. else
  542. return pktsize;
  543. }
  544. /* tcp.c */
  545. void tcp_get_info(struct sock *, struct tcp_info *);
  546. /* Read 'sendfile()'-style from a TCP socket */
  547. int tcp_read_sock(struct sock *sk, read_descriptor_t *desc,
  548. sk_read_actor_t recv_actor);
  549. void tcp_initialize_rcv_mss(struct sock *sk);
  550. int tcp_mtu_to_mss(struct sock *sk, int pmtu);
  551. int tcp_mss_to_mtu(struct sock *sk, int mss);
  552. void tcp_mtup_init(struct sock *sk);
  553. void tcp_init_buffer_space(struct sock *sk);
  554. static inline void tcp_bound_rto(const struct sock *sk)
  555. {
  556. if (inet_csk(sk)->icsk_rto > TCP_RTO_MAX)
  557. inet_csk(sk)->icsk_rto = TCP_RTO_MAX;
  558. }
  559. static inline u32 __tcp_set_rto(const struct tcp_sock *tp)
  560. {
  561. return usecs_to_jiffies((tp->srtt_us >> 3) + tp->rttvar_us);
  562. }
  563. static inline void __tcp_fast_path_on(struct tcp_sock *tp, u32 snd_wnd)
  564. {
  565. tp->pred_flags = htonl((tp->tcp_header_len << 26) |
  566. ntohl(TCP_FLAG_ACK) |
  567. snd_wnd);
  568. }
  569. static inline void tcp_fast_path_on(struct tcp_sock *tp)
  570. {
  571. __tcp_fast_path_on(tp, tp->snd_wnd >> tp->rx_opt.snd_wscale);
  572. }
  573. static inline void tcp_fast_path_check(struct sock *sk)
  574. {
  575. struct tcp_sock *tp = tcp_sk(sk);
  576. if (RB_EMPTY_ROOT(&tp->out_of_order_queue) &&
  577. tp->rcv_wnd &&
  578. atomic_read(&sk->sk_rmem_alloc) < sk->sk_rcvbuf &&
  579. !tp->urg_data)
  580. tcp_fast_path_on(tp);
  581. }
  582. /* Compute the actual rto_min value */
  583. static inline u32 tcp_rto_min(struct sock *sk)
  584. {
  585. const struct dst_entry *dst = __sk_dst_get(sk);
  586. u32 rto_min = TCP_RTO_MIN;
  587. if (dst && dst_metric_locked(dst, RTAX_RTO_MIN))
  588. rto_min = dst_metric_rtt(dst, RTAX_RTO_MIN);
  589. return rto_min;
  590. }
  591. static inline u32 tcp_rto_min_us(struct sock *sk)
  592. {
  593. return jiffies_to_usecs(tcp_rto_min(sk));
  594. }
  595. static inline bool tcp_ca_dst_locked(const struct dst_entry *dst)
  596. {
  597. return dst_metric_locked(dst, RTAX_CC_ALGO);
  598. }
  599. /* Minimum RTT in usec. ~0 means not available. */
  600. static inline u32 tcp_min_rtt(const struct tcp_sock *tp)
  601. {
  602. return minmax_get(&tp->rtt_min);
  603. }
  604. /* Compute the actual receive window we are currently advertising.
  605. * Rcv_nxt can be after the window if our peer push more data
  606. * than the offered window.
  607. */
  608. static inline u32 tcp_receive_window(const struct tcp_sock *tp)
  609. {
  610. s32 win = tp->rcv_wup + tp->rcv_wnd - tp->rcv_nxt;
  611. if (win < 0)
  612. win = 0;
  613. return (u32) win;
  614. }
  615. /* Choose a new window, without checks for shrinking, and without
  616. * scaling applied to the result. The caller does these things
  617. * if necessary. This is a "raw" window selection.
  618. */
  619. u32 __tcp_select_window(struct sock *sk);
  620. void tcp_send_window_probe(struct sock *sk);
  621. /* TCP uses 32bit jiffies to save some space.
  622. * Note that this is different from tcp_time_stamp, which
  623. * historically has been the same until linux-4.13.
  624. */
  625. #define tcp_jiffies32 ((u32)jiffies)
  626. /*
  627. * Deliver a 32bit value for TCP timestamp option (RFC 7323)
  628. * It is no longer tied to jiffies, but to 1 ms clock.
  629. * Note: double check if you want to use tcp_jiffies32 instead of this.
  630. */
  631. #define TCP_TS_HZ 1000
  632. static inline u64 tcp_clock_ns(void)
  633. {
  634. return local_clock();
  635. }
  636. static inline u64 tcp_clock_us(void)
  637. {
  638. return div_u64(tcp_clock_ns(), NSEC_PER_USEC);
  639. }
  640. /* This should only be used in contexts where tp->tcp_mstamp is up to date */
  641. static inline u32 tcp_time_stamp(const struct tcp_sock *tp)
  642. {
  643. return div_u64(tp->tcp_mstamp, USEC_PER_SEC / TCP_TS_HZ);
  644. }
  645. /* Could use tcp_clock_us() / 1000, but this version uses a single divide */
  646. static inline u32 tcp_time_stamp_raw(void)
  647. {
  648. return div_u64(tcp_clock_ns(), NSEC_PER_SEC / TCP_TS_HZ);
  649. }
  650. /* Refresh 1us clock of a TCP socket,
  651. * ensuring monotically increasing values.
  652. */
  653. static inline void tcp_mstamp_refresh(struct tcp_sock *tp)
  654. {
  655. u64 val = tcp_clock_us();
  656. if (val > tp->tcp_mstamp)
  657. tp->tcp_mstamp = val;
  658. }
  659. static inline u32 tcp_stamp_us_delta(u64 t1, u64 t0)
  660. {
  661. return max_t(s64, t1 - t0, 0);
  662. }
  663. static inline u32 tcp_skb_timestamp(const struct sk_buff *skb)
  664. {
  665. return div_u64(skb->skb_mstamp, USEC_PER_SEC / TCP_TS_HZ);
  666. }
  667. #define tcp_flag_byte(th) (((u_int8_t *)th)[13])
  668. #define TCPHDR_FIN 0x01
  669. #define TCPHDR_SYN 0x02
  670. #define TCPHDR_RST 0x04
  671. #define TCPHDR_PSH 0x08
  672. #define TCPHDR_ACK 0x10
  673. #define TCPHDR_URG 0x20
  674. #define TCPHDR_ECE 0x40
  675. #define TCPHDR_CWR 0x80
  676. #define TCPHDR_SYN_ECN (TCPHDR_SYN | TCPHDR_ECE | TCPHDR_CWR)
  677. /* This is what the send packet queuing engine uses to pass
  678. * TCP per-packet control information to the transmission code.
  679. * We also store the host-order sequence numbers in here too.
  680. * This is 44 bytes if IPV6 is enabled.
  681. * If this grows please adjust skbuff.h:skbuff->cb[xxx] size appropriately.
  682. */
  683. struct tcp_skb_cb {
  684. __u32 seq; /* Starting sequence number */
  685. __u32 end_seq; /* SEQ + FIN + SYN + datalen */
  686. union {
  687. /* Note : tcp_tw_isn is used in input path only
  688. * (isn chosen by tcp_timewait_state_process())
  689. *
  690. * tcp_gso_segs/size are used in write queue only,
  691. * cf tcp_skb_pcount()/tcp_skb_mss()
  692. */
  693. __u32 tcp_tw_isn;
  694. struct {
  695. u16 tcp_gso_segs;
  696. u16 tcp_gso_size;
  697. };
  698. };
  699. __u8 tcp_flags; /* TCP header flags. (tcp[13]) */
  700. __u8 sacked; /* State flags for SACK. */
  701. #define TCPCB_SACKED_ACKED 0x01 /* SKB ACK'd by a SACK block */
  702. #define TCPCB_SACKED_RETRANS 0x02 /* SKB retransmitted */
  703. #define TCPCB_LOST 0x04 /* SKB is lost */
  704. #define TCPCB_TAGBITS 0x07 /* All tag bits */
  705. #define TCPCB_REPAIRED 0x10 /* SKB repaired (no skb_mstamp) */
  706. #define TCPCB_EVER_RETRANS 0x80 /* Ever retransmitted frame */
  707. #define TCPCB_RETRANS (TCPCB_SACKED_RETRANS|TCPCB_EVER_RETRANS| \
  708. TCPCB_REPAIRED)
  709. __u8 ip_dsfield; /* IPv4 tos or IPv6 dsfield */
  710. __u8 txstamp_ack:1, /* Record TX timestamp for ack? */
  711. eor:1, /* Is skb MSG_EOR marked? */
  712. has_rxtstamp:1, /* SKB has a RX timestamp */
  713. unused:5;
  714. __u32 ack_seq; /* Sequence number ACK'd */
  715. union {
  716. struct {
  717. /* There is space for up to 24 bytes */
  718. __u32 in_flight:30,/* Bytes in flight at transmit */
  719. is_app_limited:1, /* cwnd not fully used? */
  720. unused:1;
  721. /* pkts S/ACKed so far upon tx of skb, incl retrans: */
  722. __u32 delivered;
  723. /* start of send pipeline phase */
  724. u64 first_tx_mstamp;
  725. /* when we reached the "delivered" count */
  726. u64 delivered_mstamp;
  727. } tx; /* only used for outgoing skbs */
  728. union {
  729. struct inet_skb_parm h4;
  730. #if IS_ENABLED(CONFIG_IPV6)
  731. struct inet6_skb_parm h6;
  732. #endif
  733. } header; /* For incoming skbs */
  734. struct {
  735. __u32 flags;
  736. struct sock *sk_redir;
  737. void *data_end;
  738. } bpf;
  739. };
  740. };
  741. #define TCP_SKB_CB(__skb) ((struct tcp_skb_cb *)&((__skb)->cb[0]))
  742. static inline void bpf_compute_data_end_sk_skb(struct sk_buff *skb)
  743. {
  744. TCP_SKB_CB(skb)->bpf.data_end = skb->data + skb_headlen(skb);
  745. }
  746. #if IS_ENABLED(CONFIG_IPV6)
  747. /* This is the variant of inet6_iif() that must be used by TCP,
  748. * as TCP moves IP6CB into a different location in skb->cb[]
  749. */
  750. static inline int tcp_v6_iif(const struct sk_buff *skb)
  751. {
  752. return TCP_SKB_CB(skb)->header.h6.iif;
  753. }
  754. static inline int tcp_v6_iif_l3_slave(const struct sk_buff *skb)
  755. {
  756. bool l3_slave = ipv6_l3mdev_skb(TCP_SKB_CB(skb)->header.h6.flags);
  757. return l3_slave ? skb->skb_iif : TCP_SKB_CB(skb)->header.h6.iif;
  758. }
  759. /* TCP_SKB_CB reference means this can not be used from early demux */
  760. static inline int tcp_v6_sdif(const struct sk_buff *skb)
  761. {
  762. #if IS_ENABLED(CONFIG_NET_L3_MASTER_DEV)
  763. if (skb && ipv6_l3mdev_skb(TCP_SKB_CB(skb)->header.h6.flags))
  764. return TCP_SKB_CB(skb)->header.h6.iif;
  765. #endif
  766. return 0;
  767. }
  768. #endif
  769. static inline bool inet_exact_dif_match(struct net *net, struct sk_buff *skb)
  770. {
  771. #if IS_ENABLED(CONFIG_NET_L3_MASTER_DEV)
  772. if (!net->ipv4.sysctl_tcp_l3mdev_accept &&
  773. skb && ipv4_l3mdev_skb(IPCB(skb)->flags))
  774. return true;
  775. #endif
  776. return false;
  777. }
  778. /* TCP_SKB_CB reference means this can not be used from early demux */
  779. static inline int tcp_v4_sdif(struct sk_buff *skb)
  780. {
  781. #if IS_ENABLED(CONFIG_NET_L3_MASTER_DEV)
  782. if (skb && ipv4_l3mdev_skb(TCP_SKB_CB(skb)->header.h4.flags))
  783. return TCP_SKB_CB(skb)->header.h4.iif;
  784. #endif
  785. return 0;
  786. }
  787. /* Due to TSO, an SKB can be composed of multiple actual
  788. * packets. To keep these tracked properly, we use this.
  789. */
  790. static inline int tcp_skb_pcount(const struct sk_buff *skb)
  791. {
  792. return TCP_SKB_CB(skb)->tcp_gso_segs;
  793. }
  794. static inline void tcp_skb_pcount_set(struct sk_buff *skb, int segs)
  795. {
  796. TCP_SKB_CB(skb)->tcp_gso_segs = segs;
  797. }
  798. static inline void tcp_skb_pcount_add(struct sk_buff *skb, int segs)
  799. {
  800. TCP_SKB_CB(skb)->tcp_gso_segs += segs;
  801. }
  802. /* This is valid iff skb is in write queue and tcp_skb_pcount() > 1. */
  803. static inline int tcp_skb_mss(const struct sk_buff *skb)
  804. {
  805. return TCP_SKB_CB(skb)->tcp_gso_size;
  806. }
  807. static inline bool tcp_skb_can_collapse_to(const struct sk_buff *skb)
  808. {
  809. return likely(!TCP_SKB_CB(skb)->eor);
  810. }
  811. /* Events passed to congestion control interface */
  812. enum tcp_ca_event {
  813. CA_EVENT_TX_START, /* first transmit when no packets in flight */
  814. CA_EVENT_CWND_RESTART, /* congestion window restart */
  815. CA_EVENT_COMPLETE_CWR, /* end of congestion recovery */
  816. CA_EVENT_LOSS, /* loss timeout */
  817. CA_EVENT_ECN_NO_CE, /* ECT set, but not CE marked */
  818. CA_EVENT_ECN_IS_CE, /* received CE marked IP packet */
  819. };
  820. /* Information about inbound ACK, passed to cong_ops->in_ack_event() */
  821. enum tcp_ca_ack_event_flags {
  822. CA_ACK_SLOWPATH = (1 << 0), /* In slow path processing */
  823. CA_ACK_WIN_UPDATE = (1 << 1), /* ACK updated window */
  824. CA_ACK_ECE = (1 << 2), /* ECE bit is set on ack */
  825. };
  826. /*
  827. * Interface for adding new TCP congestion control handlers
  828. */
  829. #define TCP_CA_NAME_MAX 16
  830. #define TCP_CA_MAX 128
  831. #define TCP_CA_BUF_MAX (TCP_CA_NAME_MAX*TCP_CA_MAX)
  832. #define TCP_CA_UNSPEC 0
  833. /* Algorithm can be set on socket without CAP_NET_ADMIN privileges */
  834. #define TCP_CONG_NON_RESTRICTED 0x1
  835. /* Requires ECN/ECT set on all packets */
  836. #define TCP_CONG_NEEDS_ECN 0x2
  837. union tcp_cc_info;
  838. struct ack_sample {
  839. u32 pkts_acked;
  840. s32 rtt_us;
  841. u32 in_flight;
  842. };
  843. /* A rate sample measures the number of (original/retransmitted) data
  844. * packets delivered "delivered" over an interval of time "interval_us".
  845. * The tcp_rate.c code fills in the rate sample, and congestion
  846. * control modules that define a cong_control function to run at the end
  847. * of ACK processing can optionally chose to consult this sample when
  848. * setting cwnd and pacing rate.
  849. * A sample is invalid if "delivered" or "interval_us" is negative.
  850. */
  851. struct rate_sample {
  852. u64 prior_mstamp; /* starting timestamp for interval */
  853. u32 prior_delivered; /* tp->delivered at "prior_mstamp" */
  854. s32 delivered; /* number of packets delivered over interval */
  855. long interval_us; /* time for tp->delivered to incr "delivered" */
  856. u32 snd_interval_us; /* snd interval for delivered packets */
  857. u32 rcv_interval_us; /* rcv interval for delivered packets */
  858. long rtt_us; /* RTT of last (S)ACKed packet (or -1) */
  859. int losses; /* number of packets marked lost upon ACK */
  860. u32 acked_sacked; /* number of packets newly (S)ACKed upon ACK */
  861. u32 prior_in_flight; /* in flight before this ACK */
  862. bool is_app_limited; /* is sample from packet with bubble in pipe? */
  863. bool is_retrans; /* is sample from retransmission? */
  864. bool is_ack_delayed; /* is this (likely) a delayed ACK? */
  865. };
  866. struct tcp_congestion_ops {
  867. struct list_head list;
  868. u32 key;
  869. u32 flags;
  870. /* initialize private data (optional) */
  871. void (*init)(struct sock *sk);
  872. /* cleanup private data (optional) */
  873. void (*release)(struct sock *sk);
  874. /* return slow start threshold (required) */
  875. u32 (*ssthresh)(struct sock *sk);
  876. /* do new cwnd calculation (required) */
  877. void (*cong_avoid)(struct sock *sk, u32 ack, u32 acked);
  878. /* call before changing ca_state (optional) */
  879. void (*set_state)(struct sock *sk, u8 new_state);
  880. /* call when cwnd event occurs (optional) */
  881. void (*cwnd_event)(struct sock *sk, enum tcp_ca_event ev);
  882. /* call when ack arrives (optional) */
  883. void (*in_ack_event)(struct sock *sk, u32 flags);
  884. /* new value of cwnd after loss (required) */
  885. u32 (*undo_cwnd)(struct sock *sk);
  886. /* hook for packet ack accounting (optional) */
  887. void (*pkts_acked)(struct sock *sk, const struct ack_sample *sample);
  888. /* override sysctl_tcp_min_tso_segs */
  889. u32 (*min_tso_segs)(struct sock *sk);
  890. /* returns the multiplier used in tcp_sndbuf_expand (optional) */
  891. u32 (*sndbuf_expand)(struct sock *sk);
  892. /* call when packets are delivered to update cwnd and pacing rate,
  893. * after all the ca_state processing. (optional)
  894. */
  895. void (*cong_control)(struct sock *sk, const struct rate_sample *rs);
  896. /* get info for inet_diag (optional) */
  897. size_t (*get_info)(struct sock *sk, u32 ext, int *attr,
  898. union tcp_cc_info *info);
  899. char name[TCP_CA_NAME_MAX];
  900. struct module *owner;
  901. };
  902. int tcp_register_congestion_control(struct tcp_congestion_ops *type);
  903. void tcp_unregister_congestion_control(struct tcp_congestion_ops *type);
  904. void tcp_assign_congestion_control(struct sock *sk);
  905. void tcp_init_congestion_control(struct sock *sk);
  906. void tcp_cleanup_congestion_control(struct sock *sk);
  907. int tcp_set_default_congestion_control(struct net *net, const char *name);
  908. void tcp_get_default_congestion_control(struct net *net, char *name);
  909. void tcp_get_available_congestion_control(char *buf, size_t len);
  910. void tcp_get_allowed_congestion_control(char *buf, size_t len);
  911. int tcp_set_allowed_congestion_control(char *allowed);
  912. int tcp_set_congestion_control(struct sock *sk, const char *name, bool load,
  913. bool reinit, bool cap_net_admin);
  914. u32 tcp_slow_start(struct tcp_sock *tp, u32 acked);
  915. void tcp_cong_avoid_ai(struct tcp_sock *tp, u32 w, u32 acked);
  916. u32 tcp_reno_ssthresh(struct sock *sk);
  917. u32 tcp_reno_undo_cwnd(struct sock *sk);
  918. void tcp_reno_cong_avoid(struct sock *sk, u32 ack, u32 acked);
  919. extern struct tcp_congestion_ops tcp_reno;
  920. struct tcp_congestion_ops *tcp_ca_find_key(u32 key);
  921. u32 tcp_ca_get_key_by_name(struct net *net, const char *name, bool *ecn_ca);
  922. #ifdef CONFIG_INET
  923. char *tcp_ca_get_name_by_key(u32 key, char *buffer);
  924. #else
  925. static inline char *tcp_ca_get_name_by_key(u32 key, char *buffer)
  926. {
  927. return NULL;
  928. }
  929. #endif
  930. static inline bool tcp_ca_needs_ecn(const struct sock *sk)
  931. {
  932. const struct inet_connection_sock *icsk = inet_csk(sk);
  933. return icsk->icsk_ca_ops->flags & TCP_CONG_NEEDS_ECN;
  934. }
  935. static inline void tcp_set_ca_state(struct sock *sk, const u8 ca_state)
  936. {
  937. struct inet_connection_sock *icsk = inet_csk(sk);
  938. if (icsk->icsk_ca_ops->set_state)
  939. icsk->icsk_ca_ops->set_state(sk, ca_state);
  940. icsk->icsk_ca_state = ca_state;
  941. }
  942. static inline void tcp_ca_event(struct sock *sk, const enum tcp_ca_event event)
  943. {
  944. const struct inet_connection_sock *icsk = inet_csk(sk);
  945. if (icsk->icsk_ca_ops->cwnd_event)
  946. icsk->icsk_ca_ops->cwnd_event(sk, event);
  947. }
  948. /* From tcp_rate.c */
  949. void tcp_rate_skb_sent(struct sock *sk, struct sk_buff *skb);
  950. void tcp_rate_skb_delivered(struct sock *sk, struct sk_buff *skb,
  951. struct rate_sample *rs);
  952. void tcp_rate_gen(struct sock *sk, u32 delivered, u32 lost,
  953. bool is_sack_reneg, struct rate_sample *rs);
  954. void tcp_rate_check_app_limited(struct sock *sk);
  955. /* These functions determine how the current flow behaves in respect of SACK
  956. * handling. SACK is negotiated with the peer, and therefore it can vary
  957. * between different flows.
  958. *
  959. * tcp_is_sack - SACK enabled
  960. * tcp_is_reno - No SACK
  961. */
  962. static inline int tcp_is_sack(const struct tcp_sock *tp)
  963. {
  964. return tp->rx_opt.sack_ok;
  965. }
  966. static inline bool tcp_is_reno(const struct tcp_sock *tp)
  967. {
  968. return !tcp_is_sack(tp);
  969. }
  970. static inline unsigned int tcp_left_out(const struct tcp_sock *tp)
  971. {
  972. return tp->sacked_out + tp->lost_out;
  973. }
  974. /* This determines how many packets are "in the network" to the best
  975. * of our knowledge. In many cases it is conservative, but where
  976. * detailed information is available from the receiver (via SACK
  977. * blocks etc.) we can make more aggressive calculations.
  978. *
  979. * Use this for decisions involving congestion control, use just
  980. * tp->packets_out to determine if the send queue is empty or not.
  981. *
  982. * Read this equation as:
  983. *
  984. * "Packets sent once on transmission queue" MINUS
  985. * "Packets left network, but not honestly ACKed yet" PLUS
  986. * "Packets fast retransmitted"
  987. */
  988. static inline unsigned int tcp_packets_in_flight(const struct tcp_sock *tp)
  989. {
  990. return tp->packets_out - tcp_left_out(tp) + tp->retrans_out;
  991. }
  992. #define TCP_INFINITE_SSTHRESH 0x7fffffff
  993. static inline bool tcp_in_slow_start(const struct tcp_sock *tp)
  994. {
  995. return tp->snd_cwnd < tp->snd_ssthresh;
  996. }
  997. static inline bool tcp_in_initial_slowstart(const struct tcp_sock *tp)
  998. {
  999. return tp->snd_ssthresh >= TCP_INFINITE_SSTHRESH;
  1000. }
  1001. static inline bool tcp_in_cwnd_reduction(const struct sock *sk)
  1002. {
  1003. return (TCPF_CA_CWR | TCPF_CA_Recovery) &
  1004. (1 << inet_csk(sk)->icsk_ca_state);
  1005. }
  1006. /* If cwnd > ssthresh, we may raise ssthresh to be half-way to cwnd.
  1007. * The exception is cwnd reduction phase, when cwnd is decreasing towards
  1008. * ssthresh.
  1009. */
  1010. static inline __u32 tcp_current_ssthresh(const struct sock *sk)
  1011. {
  1012. const struct tcp_sock *tp = tcp_sk(sk);
  1013. if (tcp_in_cwnd_reduction(sk))
  1014. return tp->snd_ssthresh;
  1015. else
  1016. return max(tp->snd_ssthresh,
  1017. ((tp->snd_cwnd >> 1) +
  1018. (tp->snd_cwnd >> 2)));
  1019. }
  1020. /* Use define here intentionally to get WARN_ON location shown at the caller */
  1021. #define tcp_verify_left_out(tp) WARN_ON(tcp_left_out(tp) > tp->packets_out)
  1022. void tcp_enter_cwr(struct sock *sk);
  1023. __u32 tcp_init_cwnd(const struct tcp_sock *tp, const struct dst_entry *dst);
  1024. /* The maximum number of MSS of available cwnd for which TSO defers
  1025. * sending if not using sysctl_tcp_tso_win_divisor.
  1026. */
  1027. static inline __u32 tcp_max_tso_deferred_mss(const struct tcp_sock *tp)
  1028. {
  1029. return 3;
  1030. }
  1031. /* Returns end sequence number of the receiver's advertised window */
  1032. static inline u32 tcp_wnd_end(const struct tcp_sock *tp)
  1033. {
  1034. return tp->snd_una + tp->snd_wnd;
  1035. }
  1036. /* We follow the spirit of RFC2861 to validate cwnd but implement a more
  1037. * flexible approach. The RFC suggests cwnd should not be raised unless
  1038. * it was fully used previously. And that's exactly what we do in
  1039. * congestion avoidance mode. But in slow start we allow cwnd to grow
  1040. * as long as the application has used half the cwnd.
  1041. * Example :
  1042. * cwnd is 10 (IW10), but application sends 9 frames.
  1043. * We allow cwnd to reach 18 when all frames are ACKed.
  1044. * This check is safe because it's as aggressive as slow start which already
  1045. * risks 100% overshoot. The advantage is that we discourage application to
  1046. * either send more filler packets or data to artificially blow up the cwnd
  1047. * usage, and allow application-limited process to probe bw more aggressively.
  1048. */
  1049. static inline bool tcp_is_cwnd_limited(const struct sock *sk)
  1050. {
  1051. const struct tcp_sock *tp = tcp_sk(sk);
  1052. /* If in slow start, ensure cwnd grows to twice what was ACKed. */
  1053. if (tcp_in_slow_start(tp))
  1054. return tp->snd_cwnd < 2 * tp->max_packets_out;
  1055. return tp->is_cwnd_limited;
  1056. }
  1057. /* BBR congestion control needs pacing.
  1058. * Same remark for SO_MAX_PACING_RATE.
  1059. * sch_fq packet scheduler is efficiently handling pacing,
  1060. * but is not always installed/used.
  1061. * Return true if TCP stack should pace packets itself.
  1062. */
  1063. static inline bool tcp_needs_internal_pacing(const struct sock *sk)
  1064. {
  1065. return smp_load_acquire(&sk->sk_pacing_status) == SK_PACING_NEEDED;
  1066. }
  1067. /* Something is really bad, we could not queue an additional packet,
  1068. * because qdisc is full or receiver sent a 0 window.
  1069. * We do not want to add fuel to the fire, or abort too early,
  1070. * so make sure the timer we arm now is at least 200ms in the future,
  1071. * regardless of current icsk_rto value (as it could be ~2ms)
  1072. */
  1073. static inline unsigned long tcp_probe0_base(const struct sock *sk)
  1074. {
  1075. return max_t(unsigned long, inet_csk(sk)->icsk_rto, TCP_RTO_MIN);
  1076. }
  1077. /* Variant of inet_csk_rto_backoff() used for zero window probes */
  1078. static inline unsigned long tcp_probe0_when(const struct sock *sk,
  1079. unsigned long max_when)
  1080. {
  1081. u64 when = (u64)tcp_probe0_base(sk) << inet_csk(sk)->icsk_backoff;
  1082. return (unsigned long)min_t(u64, when, max_when);
  1083. }
  1084. static inline void tcp_check_probe_timer(struct sock *sk)
  1085. {
  1086. if (!tcp_sk(sk)->packets_out && !inet_csk(sk)->icsk_pending)
  1087. inet_csk_reset_xmit_timer(sk, ICSK_TIME_PROBE0,
  1088. tcp_probe0_base(sk), TCP_RTO_MAX);
  1089. }
  1090. static inline void tcp_init_wl(struct tcp_sock *tp, u32 seq)
  1091. {
  1092. tp->snd_wl1 = seq;
  1093. }
  1094. static inline void tcp_update_wl(struct tcp_sock *tp, u32 seq)
  1095. {
  1096. tp->snd_wl1 = seq;
  1097. }
  1098. /*
  1099. * Calculate(/check) TCP checksum
  1100. */
  1101. static inline __sum16 tcp_v4_check(int len, __be32 saddr,
  1102. __be32 daddr, __wsum base)
  1103. {
  1104. return csum_tcpudp_magic(saddr,daddr,len,IPPROTO_TCP,base);
  1105. }
  1106. static inline __sum16 __tcp_checksum_complete(struct sk_buff *skb)
  1107. {
  1108. return __skb_checksum_complete(skb);
  1109. }
  1110. static inline bool tcp_checksum_complete(struct sk_buff *skb)
  1111. {
  1112. return !skb_csum_unnecessary(skb) &&
  1113. __tcp_checksum_complete(skb);
  1114. }
  1115. bool tcp_add_backlog(struct sock *sk, struct sk_buff *skb);
  1116. int tcp_filter(struct sock *sk, struct sk_buff *skb);
  1117. #undef STATE_TRACE
  1118. #ifdef STATE_TRACE
  1119. static const char *statename[]={
  1120. "Unused","Established","Syn Sent","Syn Recv",
  1121. "Fin Wait 1","Fin Wait 2","Time Wait", "Close",
  1122. "Close Wait","Last ACK","Listen","Closing"
  1123. };
  1124. #endif
  1125. void tcp_set_state(struct sock *sk, int state);
  1126. void tcp_done(struct sock *sk);
  1127. int tcp_abort(struct sock *sk, int err);
  1128. static inline void tcp_sack_reset(struct tcp_options_received *rx_opt)
  1129. {
  1130. rx_opt->dsack = 0;
  1131. rx_opt->num_sacks = 0;
  1132. }
  1133. u32 tcp_default_init_rwnd(u32 mss);
  1134. void tcp_cwnd_restart(struct sock *sk, s32 delta);
  1135. static inline void tcp_slow_start_after_idle_check(struct sock *sk)
  1136. {
  1137. const struct tcp_congestion_ops *ca_ops = inet_csk(sk)->icsk_ca_ops;
  1138. struct tcp_sock *tp = tcp_sk(sk);
  1139. s32 delta;
  1140. if (!sock_net(sk)->ipv4.sysctl_tcp_slow_start_after_idle || tp->packets_out ||
  1141. ca_ops->cong_control)
  1142. return;
  1143. delta = tcp_jiffies32 - tp->lsndtime;
  1144. if (delta > inet_csk(sk)->icsk_rto)
  1145. tcp_cwnd_restart(sk, delta);
  1146. }
  1147. /* Determine a window scaling and initial window to offer. */
  1148. void tcp_select_initial_window(const struct sock *sk, int __space,
  1149. __u32 mss, __u32 *rcv_wnd,
  1150. __u32 *window_clamp, int wscale_ok,
  1151. __u8 *rcv_wscale, __u32 init_rcv_wnd);
  1152. static inline int tcp_win_from_space(const struct sock *sk, int space)
  1153. {
  1154. int tcp_adv_win_scale = sock_net(sk)->ipv4.sysctl_tcp_adv_win_scale;
  1155. return tcp_adv_win_scale <= 0 ?
  1156. (space>>(-tcp_adv_win_scale)) :
  1157. space - (space>>tcp_adv_win_scale);
  1158. }
  1159. /* Note: caller must be prepared to deal with negative returns */
  1160. static inline int tcp_space(const struct sock *sk)
  1161. {
  1162. return tcp_win_from_space(sk, sk->sk_rcvbuf - sk->sk_backlog.len -
  1163. atomic_read(&sk->sk_rmem_alloc));
  1164. }
  1165. static inline int tcp_full_space(const struct sock *sk)
  1166. {
  1167. return tcp_win_from_space(sk, sk->sk_rcvbuf);
  1168. }
  1169. extern void tcp_openreq_init_rwin(struct request_sock *req,
  1170. const struct sock *sk_listener,
  1171. const struct dst_entry *dst);
  1172. void tcp_enter_memory_pressure(struct sock *sk);
  1173. void tcp_leave_memory_pressure(struct sock *sk);
  1174. static inline int keepalive_intvl_when(const struct tcp_sock *tp)
  1175. {
  1176. struct net *net = sock_net((struct sock *)tp);
  1177. return tp->keepalive_intvl ? : net->ipv4.sysctl_tcp_keepalive_intvl;
  1178. }
  1179. static inline int keepalive_time_when(const struct tcp_sock *tp)
  1180. {
  1181. struct net *net = sock_net((struct sock *)tp);
  1182. return tp->keepalive_time ? : net->ipv4.sysctl_tcp_keepalive_time;
  1183. }
  1184. static inline int keepalive_probes(const struct tcp_sock *tp)
  1185. {
  1186. struct net *net = sock_net((struct sock *)tp);
  1187. return tp->keepalive_probes ? : net->ipv4.sysctl_tcp_keepalive_probes;
  1188. }
  1189. static inline u32 keepalive_time_elapsed(const struct tcp_sock *tp)
  1190. {
  1191. const struct inet_connection_sock *icsk = &tp->inet_conn;
  1192. return min_t(u32, tcp_jiffies32 - icsk->icsk_ack.lrcvtime,
  1193. tcp_jiffies32 - tp->rcv_tstamp);
  1194. }
  1195. static inline int tcp_fin_time(const struct sock *sk)
  1196. {
  1197. int fin_timeout = tcp_sk(sk)->linger2 ? : sock_net(sk)->ipv4.sysctl_tcp_fin_timeout;
  1198. const int rto = inet_csk(sk)->icsk_rto;
  1199. if (fin_timeout < (rto << 2) - (rto >> 1))
  1200. fin_timeout = (rto << 2) - (rto >> 1);
  1201. return fin_timeout;
  1202. }
  1203. static inline bool tcp_paws_check(const struct tcp_options_received *rx_opt,
  1204. int paws_win)
  1205. {
  1206. if ((s32)(rx_opt->ts_recent - rx_opt->rcv_tsval) <= paws_win)
  1207. return true;
  1208. if (unlikely(!time_before32(ktime_get_seconds(),
  1209. rx_opt->ts_recent_stamp + TCP_PAWS_24DAYS)))
  1210. return true;
  1211. /*
  1212. * Some OSes send SYN and SYNACK messages with tsval=0 tsecr=0,
  1213. * then following tcp messages have valid values. Ignore 0 value,
  1214. * or else 'negative' tsval might forbid us to accept their packets.
  1215. */
  1216. if (!rx_opt->ts_recent)
  1217. return true;
  1218. return false;
  1219. }
  1220. static inline bool tcp_paws_reject(const struct tcp_options_received *rx_opt,
  1221. int rst)
  1222. {
  1223. if (tcp_paws_check(rx_opt, 0))
  1224. return false;
  1225. /* RST segments are not recommended to carry timestamp,
  1226. and, if they do, it is recommended to ignore PAWS because
  1227. "their cleanup function should take precedence over timestamps."
  1228. Certainly, it is mistake. It is necessary to understand the reasons
  1229. of this constraint to relax it: if peer reboots, clock may go
  1230. out-of-sync and half-open connections will not be reset.
  1231. Actually, the problem would be not existing if all
  1232. the implementations followed draft about maintaining clock
  1233. via reboots. Linux-2.2 DOES NOT!
  1234. However, we can relax time bounds for RST segments to MSL.
  1235. */
  1236. if (rst && !time_before32(ktime_get_seconds(),
  1237. rx_opt->ts_recent_stamp + TCP_PAWS_MSL))
  1238. return false;
  1239. return true;
  1240. }
  1241. bool tcp_oow_rate_limited(struct net *net, const struct sk_buff *skb,
  1242. int mib_idx, u32 *last_oow_ack_time);
  1243. static inline void tcp_mib_init(struct net *net)
  1244. {
  1245. /* See RFC 2012 */
  1246. TCP_ADD_STATS(net, TCP_MIB_RTOALGORITHM, 1);
  1247. TCP_ADD_STATS(net, TCP_MIB_RTOMIN, TCP_RTO_MIN*1000/HZ);
  1248. TCP_ADD_STATS(net, TCP_MIB_RTOMAX, TCP_RTO_MAX*1000/HZ);
  1249. TCP_ADD_STATS(net, TCP_MIB_MAXCONN, -1);
  1250. }
  1251. /* from STCP */
  1252. static inline void tcp_clear_retrans_hints_partial(struct tcp_sock *tp)
  1253. {
  1254. tp->lost_skb_hint = NULL;
  1255. }
  1256. static inline void tcp_clear_all_retrans_hints(struct tcp_sock *tp)
  1257. {
  1258. tcp_clear_retrans_hints_partial(tp);
  1259. tp->retransmit_skb_hint = NULL;
  1260. }
  1261. union tcp_md5_addr {
  1262. struct in_addr a4;
  1263. #if IS_ENABLED(CONFIG_IPV6)
  1264. struct in6_addr a6;
  1265. #endif
  1266. };
  1267. /* - key database */
  1268. struct tcp_md5sig_key {
  1269. struct hlist_node node;
  1270. u8 keylen;
  1271. u8 family; /* AF_INET or AF_INET6 */
  1272. union tcp_md5_addr addr;
  1273. u8 prefixlen;
  1274. u8 key[TCP_MD5SIG_MAXKEYLEN];
  1275. struct rcu_head rcu;
  1276. };
  1277. /* - sock block */
  1278. struct tcp_md5sig_info {
  1279. struct hlist_head head;
  1280. struct rcu_head rcu;
  1281. };
  1282. /* - pseudo header */
  1283. struct tcp4_pseudohdr {
  1284. __be32 saddr;
  1285. __be32 daddr;
  1286. __u8 pad;
  1287. __u8 protocol;
  1288. __be16 len;
  1289. };
  1290. struct tcp6_pseudohdr {
  1291. struct in6_addr saddr;
  1292. struct in6_addr daddr;
  1293. __be32 len;
  1294. __be32 protocol; /* including padding */
  1295. };
  1296. union tcp_md5sum_block {
  1297. struct tcp4_pseudohdr ip4;
  1298. #if IS_ENABLED(CONFIG_IPV6)
  1299. struct tcp6_pseudohdr ip6;
  1300. #endif
  1301. };
  1302. /* - pool: digest algorithm, hash description and scratch buffer */
  1303. struct tcp_md5sig_pool {
  1304. struct ahash_request *md5_req;
  1305. void *scratch;
  1306. };
  1307. /* - functions */
  1308. int tcp_v4_md5_hash_skb(char *md5_hash, const struct tcp_md5sig_key *key,
  1309. const struct sock *sk, const struct sk_buff *skb);
  1310. int tcp_md5_do_add(struct sock *sk, const union tcp_md5_addr *addr,
  1311. int family, u8 prefixlen, const u8 *newkey, u8 newkeylen,
  1312. gfp_t gfp);
  1313. int tcp_md5_do_del(struct sock *sk, const union tcp_md5_addr *addr,
  1314. int family, u8 prefixlen);
  1315. struct tcp_md5sig_key *tcp_v4_md5_lookup(const struct sock *sk,
  1316. const struct sock *addr_sk);
  1317. #ifdef CONFIG_TCP_MD5SIG
  1318. struct tcp_md5sig_key *tcp_md5_do_lookup(const struct sock *sk,
  1319. const union tcp_md5_addr *addr,
  1320. int family);
  1321. #define tcp_twsk_md5_key(twsk) ((twsk)->tw_md5_key)
  1322. #else
  1323. static inline struct tcp_md5sig_key *tcp_md5_do_lookup(const struct sock *sk,
  1324. const union tcp_md5_addr *addr,
  1325. int family)
  1326. {
  1327. return NULL;
  1328. }
  1329. #define tcp_twsk_md5_key(twsk) NULL
  1330. #endif
  1331. bool tcp_alloc_md5sig_pool(void);
  1332. struct tcp_md5sig_pool *tcp_get_md5sig_pool(void);
  1333. static inline void tcp_put_md5sig_pool(void)
  1334. {
  1335. local_bh_enable();
  1336. }
  1337. int tcp_md5_hash_skb_data(struct tcp_md5sig_pool *, const struct sk_buff *,
  1338. unsigned int header_len);
  1339. int tcp_md5_hash_key(struct tcp_md5sig_pool *hp,
  1340. const struct tcp_md5sig_key *key);
  1341. /* From tcp_fastopen.c */
  1342. void tcp_fastopen_cache_get(struct sock *sk, u16 *mss,
  1343. struct tcp_fastopen_cookie *cookie);
  1344. void tcp_fastopen_cache_set(struct sock *sk, u16 mss,
  1345. struct tcp_fastopen_cookie *cookie, bool syn_lost,
  1346. u16 try_exp);
  1347. struct tcp_fastopen_request {
  1348. /* Fast Open cookie. Size 0 means a cookie request */
  1349. struct tcp_fastopen_cookie cookie;
  1350. struct msghdr *data; /* data in MSG_FASTOPEN */
  1351. size_t size;
  1352. int copied; /* queued in tcp_connect() */
  1353. };
  1354. void tcp_free_fastopen_req(struct tcp_sock *tp);
  1355. void tcp_fastopen_destroy_cipher(struct sock *sk);
  1356. void tcp_fastopen_ctx_destroy(struct net *net);
  1357. int tcp_fastopen_reset_cipher(struct net *net, struct sock *sk,
  1358. void *key, unsigned int len);
  1359. void tcp_fastopen_add_skb(struct sock *sk, struct sk_buff *skb);
  1360. struct sock *tcp_try_fastopen(struct sock *sk, struct sk_buff *skb,
  1361. struct request_sock *req,
  1362. struct tcp_fastopen_cookie *foc,
  1363. const struct dst_entry *dst);
  1364. void tcp_fastopen_init_key_once(struct net *net);
  1365. bool tcp_fastopen_cookie_check(struct sock *sk, u16 *mss,
  1366. struct tcp_fastopen_cookie *cookie);
  1367. bool tcp_fastopen_defer_connect(struct sock *sk, int *err);
  1368. #define TCP_FASTOPEN_KEY_LENGTH 16
  1369. /* Fastopen key context */
  1370. struct tcp_fastopen_context {
  1371. struct crypto_cipher *tfm;
  1372. __u8 key[TCP_FASTOPEN_KEY_LENGTH];
  1373. struct rcu_head rcu;
  1374. };
  1375. extern unsigned int sysctl_tcp_fastopen_blackhole_timeout;
  1376. void tcp_fastopen_active_disable(struct sock *sk);
  1377. bool tcp_fastopen_active_should_disable(struct sock *sk);
  1378. void tcp_fastopen_active_disable_ofo_check(struct sock *sk);
  1379. void tcp_fastopen_active_detect_blackhole(struct sock *sk, bool expired);
  1380. /* Latencies incurred by various limits for a sender. They are
  1381. * chronograph-like stats that are mutually exclusive.
  1382. */
  1383. enum tcp_chrono {
  1384. TCP_CHRONO_UNSPEC,
  1385. TCP_CHRONO_BUSY, /* Actively sending data (non-empty write queue) */
  1386. TCP_CHRONO_RWND_LIMITED, /* Stalled by insufficient receive window */
  1387. TCP_CHRONO_SNDBUF_LIMITED, /* Stalled by insufficient send buffer */
  1388. __TCP_CHRONO_MAX,
  1389. };
  1390. void tcp_chrono_start(struct sock *sk, const enum tcp_chrono type);
  1391. void tcp_chrono_stop(struct sock *sk, const enum tcp_chrono type);
  1392. /* This helper is needed, because skb->tcp_tsorted_anchor uses
  1393. * the same memory storage than skb->destructor/_skb_refdst
  1394. */
  1395. static inline void tcp_skb_tsorted_anchor_cleanup(struct sk_buff *skb)
  1396. {
  1397. skb->destructor = NULL;
  1398. skb->_skb_refdst = 0UL;
  1399. }
  1400. #define tcp_skb_tsorted_save(skb) { \
  1401. unsigned long _save = skb->_skb_refdst; \
  1402. skb->_skb_refdst = 0UL;
  1403. #define tcp_skb_tsorted_restore(skb) \
  1404. skb->_skb_refdst = _save; \
  1405. }
  1406. void tcp_write_queue_purge(struct sock *sk);
  1407. static inline struct sk_buff *tcp_rtx_queue_head(const struct sock *sk)
  1408. {
  1409. return skb_rb_first(&sk->tcp_rtx_queue);
  1410. }
  1411. static inline struct sk_buff *tcp_rtx_queue_tail(const struct sock *sk)
  1412. {
  1413. return skb_rb_last(&sk->tcp_rtx_queue);
  1414. }
  1415. static inline struct sk_buff *tcp_write_queue_head(const struct sock *sk)
  1416. {
  1417. return skb_peek(&sk->sk_write_queue);
  1418. }
  1419. static inline struct sk_buff *tcp_write_queue_tail(const struct sock *sk)
  1420. {
  1421. return skb_peek_tail(&sk->sk_write_queue);
  1422. }
  1423. #define tcp_for_write_queue_from_safe(skb, tmp, sk) \
  1424. skb_queue_walk_from_safe(&(sk)->sk_write_queue, skb, tmp)
  1425. static inline struct sk_buff *tcp_send_head(const struct sock *sk)
  1426. {
  1427. return skb_peek(&sk->sk_write_queue);
  1428. }
  1429. static inline bool tcp_skb_is_last(const struct sock *sk,
  1430. const struct sk_buff *skb)
  1431. {
  1432. return skb_queue_is_last(&sk->sk_write_queue, skb);
  1433. }
  1434. static inline bool tcp_write_queue_empty(const struct sock *sk)
  1435. {
  1436. return skb_queue_empty(&sk->sk_write_queue);
  1437. }
  1438. static inline bool tcp_rtx_queue_empty(const struct sock *sk)
  1439. {
  1440. return RB_EMPTY_ROOT(&sk->tcp_rtx_queue);
  1441. }
  1442. static inline bool tcp_rtx_and_write_queues_empty(const struct sock *sk)
  1443. {
  1444. return tcp_rtx_queue_empty(sk) && tcp_write_queue_empty(sk);
  1445. }
  1446. static inline void tcp_check_send_head(struct sock *sk, struct sk_buff *skb_unlinked)
  1447. {
  1448. if (tcp_write_queue_empty(sk))
  1449. tcp_chrono_stop(sk, TCP_CHRONO_BUSY);
  1450. }
  1451. static inline void __tcp_add_write_queue_tail(struct sock *sk, struct sk_buff *skb)
  1452. {
  1453. __skb_queue_tail(&sk->sk_write_queue, skb);
  1454. }
  1455. static inline void tcp_add_write_queue_tail(struct sock *sk, struct sk_buff *skb)
  1456. {
  1457. __tcp_add_write_queue_tail(sk, skb);
  1458. /* Queue it, remembering where we must start sending. */
  1459. if (sk->sk_write_queue.next == skb)
  1460. tcp_chrono_start(sk, TCP_CHRONO_BUSY);
  1461. }
  1462. /* Insert new before skb on the write queue of sk. */
  1463. static inline void tcp_insert_write_queue_before(struct sk_buff *new,
  1464. struct sk_buff *skb,
  1465. struct sock *sk)
  1466. {
  1467. __skb_queue_before(&sk->sk_write_queue, skb, new);
  1468. }
  1469. static inline void tcp_unlink_write_queue(struct sk_buff *skb, struct sock *sk)
  1470. {
  1471. tcp_skb_tsorted_anchor_cleanup(skb);
  1472. __skb_unlink(skb, &sk->sk_write_queue);
  1473. }
  1474. void tcp_rbtree_insert(struct rb_root *root, struct sk_buff *skb);
  1475. static inline void tcp_rtx_queue_unlink(struct sk_buff *skb, struct sock *sk)
  1476. {
  1477. tcp_skb_tsorted_anchor_cleanup(skb);
  1478. rb_erase(&skb->rbnode, &sk->tcp_rtx_queue);
  1479. }
  1480. static inline void tcp_rtx_queue_unlink_and_free(struct sk_buff *skb, struct sock *sk)
  1481. {
  1482. list_del(&skb->tcp_tsorted_anchor);
  1483. tcp_rtx_queue_unlink(skb, sk);
  1484. sk_wmem_free_skb(sk, skb);
  1485. }
  1486. static inline void tcp_push_pending_frames(struct sock *sk)
  1487. {
  1488. if (tcp_send_head(sk)) {
  1489. struct tcp_sock *tp = tcp_sk(sk);
  1490. __tcp_push_pending_frames(sk, tcp_current_mss(sk), tp->nonagle);
  1491. }
  1492. }
  1493. /* Start sequence of the skb just after the highest skb with SACKed
  1494. * bit, valid only if sacked_out > 0 or when the caller has ensured
  1495. * validity by itself.
  1496. */
  1497. static inline u32 tcp_highest_sack_seq(struct tcp_sock *tp)
  1498. {
  1499. if (!tp->sacked_out)
  1500. return tp->snd_una;
  1501. if (tp->highest_sack == NULL)
  1502. return tp->snd_nxt;
  1503. return TCP_SKB_CB(tp->highest_sack)->seq;
  1504. }
  1505. static inline void tcp_advance_highest_sack(struct sock *sk, struct sk_buff *skb)
  1506. {
  1507. tcp_sk(sk)->highest_sack = skb_rb_next(skb);
  1508. }
  1509. static inline struct sk_buff *tcp_highest_sack(struct sock *sk)
  1510. {
  1511. return tcp_sk(sk)->highest_sack;
  1512. }
  1513. static inline void tcp_highest_sack_reset(struct sock *sk)
  1514. {
  1515. tcp_sk(sk)->highest_sack = tcp_rtx_queue_head(sk);
  1516. }
  1517. /* Called when old skb is about to be deleted and replaced by new skb */
  1518. static inline void tcp_highest_sack_replace(struct sock *sk,
  1519. struct sk_buff *old,
  1520. struct sk_buff *new)
  1521. {
  1522. if (old == tcp_highest_sack(sk))
  1523. tcp_sk(sk)->highest_sack = new;
  1524. }
  1525. /* This helper checks if socket has IP_TRANSPARENT set */
  1526. static inline bool inet_sk_transparent(const struct sock *sk)
  1527. {
  1528. switch (sk->sk_state) {
  1529. case TCP_TIME_WAIT:
  1530. return inet_twsk(sk)->tw_transparent;
  1531. case TCP_NEW_SYN_RECV:
  1532. return inet_rsk(inet_reqsk(sk))->no_srccheck;
  1533. }
  1534. return inet_sk(sk)->transparent;
  1535. }
  1536. /* Determines whether this is a thin stream (which may suffer from
  1537. * increased latency). Used to trigger latency-reducing mechanisms.
  1538. */
  1539. static inline bool tcp_stream_is_thin(struct tcp_sock *tp)
  1540. {
  1541. return tp->packets_out < 4 && !tcp_in_initial_slowstart(tp);
  1542. }
  1543. /* /proc */
  1544. enum tcp_seq_states {
  1545. TCP_SEQ_STATE_LISTENING,
  1546. TCP_SEQ_STATE_ESTABLISHED,
  1547. };
  1548. void *tcp_seq_start(struct seq_file *seq, loff_t *pos);
  1549. void *tcp_seq_next(struct seq_file *seq, void *v, loff_t *pos);
  1550. void tcp_seq_stop(struct seq_file *seq, void *v);
  1551. struct tcp_seq_afinfo {
  1552. sa_family_t family;
  1553. };
  1554. struct tcp_iter_state {
  1555. struct seq_net_private p;
  1556. enum tcp_seq_states state;
  1557. struct sock *syn_wait_sk;
  1558. int bucket, offset, sbucket, num;
  1559. loff_t last_pos;
  1560. };
  1561. extern struct request_sock_ops tcp_request_sock_ops;
  1562. extern struct request_sock_ops tcp6_request_sock_ops;
  1563. void tcp_v4_destroy_sock(struct sock *sk);
  1564. struct sk_buff *tcp_gso_segment(struct sk_buff *skb,
  1565. netdev_features_t features);
  1566. struct sk_buff *tcp_gro_receive(struct list_head *head, struct sk_buff *skb);
  1567. int tcp_gro_complete(struct sk_buff *skb);
  1568. void __tcp_v4_send_check(struct sk_buff *skb, __be32 saddr, __be32 daddr);
  1569. static inline u32 tcp_notsent_lowat(const struct tcp_sock *tp)
  1570. {
  1571. struct net *net = sock_net((struct sock *)tp);
  1572. return tp->notsent_lowat ?: net->ipv4.sysctl_tcp_notsent_lowat;
  1573. }
  1574. static inline bool tcp_stream_memory_free(const struct sock *sk)
  1575. {
  1576. const struct tcp_sock *tp = tcp_sk(sk);
  1577. u32 notsent_bytes = tp->write_seq - tp->snd_nxt;
  1578. return notsent_bytes < tcp_notsent_lowat(tp);
  1579. }
  1580. #ifdef CONFIG_PROC_FS
  1581. int tcp4_proc_init(void);
  1582. void tcp4_proc_exit(void);
  1583. #endif
  1584. int tcp_rtx_synack(const struct sock *sk, struct request_sock *req);
  1585. int tcp_conn_request(struct request_sock_ops *rsk_ops,
  1586. const struct tcp_request_sock_ops *af_ops,
  1587. struct sock *sk, struct sk_buff *skb);
  1588. /* TCP af-specific functions */
  1589. struct tcp_sock_af_ops {
  1590. #ifdef CONFIG_TCP_MD5SIG
  1591. struct tcp_md5sig_key *(*md5_lookup) (const struct sock *sk,
  1592. const struct sock *addr_sk);
  1593. int (*calc_md5_hash)(char *location,
  1594. const struct tcp_md5sig_key *md5,
  1595. const struct sock *sk,
  1596. const struct sk_buff *skb);
  1597. int (*md5_parse)(struct sock *sk,
  1598. int optname,
  1599. char __user *optval,
  1600. int optlen);
  1601. #endif
  1602. };
  1603. struct tcp_request_sock_ops {
  1604. u16 mss_clamp;
  1605. #ifdef CONFIG_TCP_MD5SIG
  1606. struct tcp_md5sig_key *(*req_md5_lookup)(const struct sock *sk,
  1607. const struct sock *addr_sk);
  1608. int (*calc_md5_hash) (char *location,
  1609. const struct tcp_md5sig_key *md5,
  1610. const struct sock *sk,
  1611. const struct sk_buff *skb);
  1612. #endif
  1613. void (*init_req)(struct request_sock *req,
  1614. const struct sock *sk_listener,
  1615. struct sk_buff *skb);
  1616. #ifdef CONFIG_SYN_COOKIES
  1617. __u32 (*cookie_init_seq)(const struct sk_buff *skb,
  1618. __u16 *mss);
  1619. #endif
  1620. struct dst_entry *(*route_req)(const struct sock *sk, struct flowi *fl,
  1621. const struct request_sock *req);
  1622. u32 (*init_seq)(const struct sk_buff *skb);
  1623. u32 (*init_ts_off)(const struct net *net, const struct sk_buff *skb);
  1624. int (*send_synack)(const struct sock *sk, struct dst_entry *dst,
  1625. struct flowi *fl, struct request_sock *req,
  1626. struct tcp_fastopen_cookie *foc,
  1627. enum tcp_synack_type synack_type);
  1628. };
  1629. #ifdef CONFIG_SYN_COOKIES
  1630. static inline __u32 cookie_init_sequence(const struct tcp_request_sock_ops *ops,
  1631. const struct sock *sk, struct sk_buff *skb,
  1632. __u16 *mss)
  1633. {
  1634. tcp_synq_overflow(sk);
  1635. __NET_INC_STATS(sock_net(sk), LINUX_MIB_SYNCOOKIESSENT);
  1636. return ops->cookie_init_seq(skb, mss);
  1637. }
  1638. #else
  1639. static inline __u32 cookie_init_sequence(const struct tcp_request_sock_ops *ops,
  1640. const struct sock *sk, struct sk_buff *skb,
  1641. __u16 *mss)
  1642. {
  1643. return 0;
  1644. }
  1645. #endif
  1646. int tcpv4_offload_init(void);
  1647. void tcp_v4_init(void);
  1648. void tcp_init(void);
  1649. /* tcp_recovery.c */
  1650. void tcp_mark_skb_lost(struct sock *sk, struct sk_buff *skb);
  1651. void tcp_newreno_mark_lost(struct sock *sk, bool snd_una_advanced);
  1652. extern s32 tcp_rack_skb_timeout(struct tcp_sock *tp, struct sk_buff *skb,
  1653. u32 reo_wnd);
  1654. extern void tcp_rack_mark_lost(struct sock *sk);
  1655. extern void tcp_rack_advance(struct tcp_sock *tp, u8 sacked, u32 end_seq,
  1656. u64 xmit_time);
  1657. extern void tcp_rack_reo_timeout(struct sock *sk);
  1658. extern void tcp_rack_update_reo_wnd(struct sock *sk, struct rate_sample *rs);
  1659. /* At how many usecs into the future should the RTO fire? */
  1660. static inline s64 tcp_rto_delta_us(const struct sock *sk)
  1661. {
  1662. const struct sk_buff *skb = tcp_rtx_queue_head(sk);
  1663. u32 rto = inet_csk(sk)->icsk_rto;
  1664. u64 rto_time_stamp_us = skb->skb_mstamp + jiffies_to_usecs(rto);
  1665. return rto_time_stamp_us - tcp_sk(sk)->tcp_mstamp;
  1666. }
  1667. /*
  1668. * Save and compile IPv4 options, return a pointer to it
  1669. */
  1670. static inline struct ip_options_rcu *tcp_v4_save_options(struct net *net,
  1671. struct sk_buff *skb)
  1672. {
  1673. const struct ip_options *opt = &TCP_SKB_CB(skb)->header.h4.opt;
  1674. struct ip_options_rcu *dopt = NULL;
  1675. if (opt->optlen) {
  1676. int opt_size = sizeof(*dopt) + opt->optlen;
  1677. dopt = kmalloc(opt_size, GFP_ATOMIC);
  1678. if (dopt && __ip_options_echo(net, &dopt->opt, skb, opt)) {
  1679. kfree(dopt);
  1680. dopt = NULL;
  1681. }
  1682. }
  1683. return dopt;
  1684. }
  1685. /* locally generated TCP pure ACKs have skb->truesize == 2
  1686. * (check tcp_send_ack() in net/ipv4/tcp_output.c )
  1687. * This is much faster than dissecting the packet to find out.
  1688. * (Think of GRE encapsulations, IPv4, IPv6, ...)
  1689. */
  1690. static inline bool skb_is_tcp_pure_ack(const struct sk_buff *skb)
  1691. {
  1692. return skb->truesize == 2;
  1693. }
  1694. static inline void skb_set_tcp_pure_ack(struct sk_buff *skb)
  1695. {
  1696. skb->truesize = 2;
  1697. }
  1698. static inline int tcp_inq(struct sock *sk)
  1699. {
  1700. struct tcp_sock *tp = tcp_sk(sk);
  1701. int answ;
  1702. if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) {
  1703. answ = 0;
  1704. } else if (sock_flag(sk, SOCK_URGINLINE) ||
  1705. !tp->urg_data ||
  1706. before(tp->urg_seq, tp->copied_seq) ||
  1707. !before(tp->urg_seq, tp->rcv_nxt)) {
  1708. answ = tp->rcv_nxt - tp->copied_seq;
  1709. /* Subtract 1, if FIN was received */
  1710. if (answ && sock_flag(sk, SOCK_DONE))
  1711. answ--;
  1712. } else {
  1713. answ = tp->urg_seq - tp->copied_seq;
  1714. }
  1715. return answ;
  1716. }
  1717. int tcp_peek_len(struct socket *sock);
  1718. static inline void tcp_segs_in(struct tcp_sock *tp, const struct sk_buff *skb)
  1719. {
  1720. u16 segs_in;
  1721. segs_in = max_t(u16, 1, skb_shinfo(skb)->gso_segs);
  1722. tp->segs_in += segs_in;
  1723. if (skb->len > tcp_hdrlen(skb))
  1724. tp->data_segs_in += segs_in;
  1725. }
  1726. /*
  1727. * TCP listen path runs lockless.
  1728. * We forced "struct sock" to be const qualified to make sure
  1729. * we don't modify one of its field by mistake.
  1730. * Here, we increment sk_drops which is an atomic_t, so we can safely
  1731. * make sock writable again.
  1732. */
  1733. static inline void tcp_listendrop(const struct sock *sk)
  1734. {
  1735. atomic_inc(&((struct sock *)sk)->sk_drops);
  1736. __NET_INC_STATS(sock_net(sk), LINUX_MIB_LISTENDROPS);
  1737. }
  1738. enum hrtimer_restart tcp_pace_kick(struct hrtimer *timer);
  1739. /*
  1740. * Interface for adding Upper Level Protocols over TCP
  1741. */
  1742. #define TCP_ULP_NAME_MAX 16
  1743. #define TCP_ULP_MAX 128
  1744. #define TCP_ULP_BUF_MAX (TCP_ULP_NAME_MAX*TCP_ULP_MAX)
  1745. enum {
  1746. TCP_ULP_TLS,
  1747. TCP_ULP_BPF,
  1748. };
  1749. struct tcp_ulp_ops {
  1750. struct list_head list;
  1751. /* initialize ulp */
  1752. int (*init)(struct sock *sk);
  1753. /* cleanup ulp */
  1754. void (*release)(struct sock *sk);
  1755. int uid;
  1756. char name[TCP_ULP_NAME_MAX];
  1757. bool user_visible;
  1758. struct module *owner;
  1759. };
  1760. int tcp_register_ulp(struct tcp_ulp_ops *type);
  1761. void tcp_unregister_ulp(struct tcp_ulp_ops *type);
  1762. int tcp_set_ulp(struct sock *sk, const char *name);
  1763. int tcp_set_ulp_id(struct sock *sk, const int ulp);
  1764. void tcp_get_available_ulp(char *buf, size_t len);
  1765. void tcp_cleanup_ulp(struct sock *sk);
  1766. #define MODULE_ALIAS_TCP_ULP(name) \
  1767. __MODULE_INFO(alias, alias_userspace, name); \
  1768. __MODULE_INFO(alias, alias_tcp_ulp, "tcp-ulp-" name)
  1769. /* Call BPF_SOCK_OPS program that returns an int. If the return value
  1770. * is < 0, then the BPF op failed (for example if the loaded BPF
  1771. * program does not support the chosen operation or there is no BPF
  1772. * program loaded).
  1773. */
  1774. #ifdef CONFIG_BPF
  1775. static inline int tcp_call_bpf(struct sock *sk, int op, u32 nargs, u32 *args)
  1776. {
  1777. struct bpf_sock_ops_kern sock_ops;
  1778. int ret;
  1779. memset(&sock_ops, 0, offsetof(struct bpf_sock_ops_kern, temp));
  1780. if (sk_fullsock(sk)) {
  1781. sock_ops.is_fullsock = 1;
  1782. sock_owned_by_me(sk);
  1783. }
  1784. sock_ops.sk = sk;
  1785. sock_ops.op = op;
  1786. if (nargs > 0)
  1787. memcpy(sock_ops.args, args, nargs * sizeof(*args));
  1788. ret = BPF_CGROUP_RUN_PROG_SOCK_OPS(&sock_ops);
  1789. if (ret == 0)
  1790. ret = sock_ops.reply;
  1791. else
  1792. ret = -1;
  1793. return ret;
  1794. }
  1795. static inline int tcp_call_bpf_2arg(struct sock *sk, int op, u32 arg1, u32 arg2)
  1796. {
  1797. u32 args[2] = {arg1, arg2};
  1798. return tcp_call_bpf(sk, op, 2, args);
  1799. }
  1800. static inline int tcp_call_bpf_3arg(struct sock *sk, int op, u32 arg1, u32 arg2,
  1801. u32 arg3)
  1802. {
  1803. u32 args[3] = {arg1, arg2, arg3};
  1804. return tcp_call_bpf(sk, op, 3, args);
  1805. }
  1806. #else
  1807. static inline int tcp_call_bpf(struct sock *sk, int op, u32 nargs, u32 *args)
  1808. {
  1809. return -EPERM;
  1810. }
  1811. static inline int tcp_call_bpf_2arg(struct sock *sk, int op, u32 arg1, u32 arg2)
  1812. {
  1813. return -EPERM;
  1814. }
  1815. static inline int tcp_call_bpf_3arg(struct sock *sk, int op, u32 arg1, u32 arg2,
  1816. u32 arg3)
  1817. {
  1818. return -EPERM;
  1819. }
  1820. #endif
  1821. static inline u32 tcp_timeout_init(struct sock *sk)
  1822. {
  1823. int timeout;
  1824. timeout = tcp_call_bpf(sk, BPF_SOCK_OPS_TIMEOUT_INIT, 0, NULL);
  1825. if (timeout <= 0)
  1826. timeout = TCP_TIMEOUT_INIT;
  1827. return timeout;
  1828. }
  1829. static inline u32 tcp_rwnd_init_bpf(struct sock *sk)
  1830. {
  1831. int rwnd;
  1832. rwnd = tcp_call_bpf(sk, BPF_SOCK_OPS_RWND_INIT, 0, NULL);
  1833. if (rwnd < 0)
  1834. rwnd = 0;
  1835. return rwnd;
  1836. }
  1837. static inline bool tcp_bpf_ca_needs_ecn(struct sock *sk)
  1838. {
  1839. return (tcp_call_bpf(sk, BPF_SOCK_OPS_NEEDS_ECN, 0, NULL) == 1);
  1840. }
  1841. #if IS_ENABLED(CONFIG_SMC)
  1842. extern struct static_key_false tcp_have_smc;
  1843. #endif
  1844. #if IS_ENABLED(CONFIG_TLS_DEVICE)
  1845. void clean_acked_data_enable(struct inet_connection_sock *icsk,
  1846. void (*cad)(struct sock *sk, u32 ack_seq));
  1847. void clean_acked_data_disable(struct inet_connection_sock *icsk);
  1848. #endif
  1849. #endif /* _TCP_H */