tcp-fastopen.patch 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082
  1. diff -Nur linux-3.15-rc7.orig/include/linux/tcp.h linux-3.15-rc7/include/linux/tcp.h
  2. --- linux-3.15-rc7.orig/include/linux/tcp.h 2014-05-26 01:06:00.000000000 +0200
  3. +++ linux-3.15-rc7/include/linux/tcp.h 2014-05-31 22:42:49.024605293 +0200
  4. @@ -359,6 +359,9 @@
  5. return (struct tcp_timewait_sock *)sk;
  6. }
  7. +extern void tcp_sock_destruct(struct sock *sk);
  8. +
  9. +#ifdef CONFIG_TCP_FASTOPEN
  10. static inline bool tcp_passive_fastopen(const struct sock *sk)
  11. {
  12. return (sk->sk_state == TCP_SYN_RECV &&
  13. @@ -370,8 +373,6 @@
  14. return foc->len != -1;
  15. }
  16. -extern void tcp_sock_destruct(struct sock *sk);
  17. -
  18. static inline int fastopen_init_queue(struct sock *sk, int backlog)
  19. {
  20. struct request_sock_queue *queue =
  21. @@ -391,4 +392,13 @@
  22. return 0;
  23. }
  24. +#else
  25. +static inline bool tcp_passive_fastopen(const struct sock *sk)
  26. +{ return false; }
  27. +static inline bool fastopen_cookie_present(struct tcp_fastopen_cookie *foc)
  28. +{ return false; }
  29. +static inline int fastopen_init_queue(struct sock *sk, int backlog)
  30. +{ return 0; }
  31. +#endif
  32. +
  33. #endif /* _LINUX_TCP_H */
  34. diff -Nur linux-3.15-rc7.orig/include/net/request_sock.h linux-3.15-rc7/include/net/request_sock.h
  35. --- linux-3.15-rc7.orig/include/net/request_sock.h 2014-05-26 01:06:00.000000000 +0200
  36. +++ linux-3.15-rc7/include/net/request_sock.h 2014-05-31 22:42:49.024605293 +0200
  37. @@ -168,8 +168,13 @@
  38. void __reqsk_queue_destroy(struct request_sock_queue *queue);
  39. void reqsk_queue_destroy(struct request_sock_queue *queue);
  40. +#ifdef CONFIG_TCP_FASTOPEN
  41. void reqsk_fastopen_remove(struct sock *sk, struct request_sock *req,
  42. bool reset);
  43. +#else
  44. +static inline void reqsk_fastopen_remove(struct sock *sk, struct request_sock *req,
  45. + bool reset) {}
  46. +#endif
  47. static inline struct request_sock *
  48. reqsk_queue_yank_acceptq(struct request_sock_queue *queue)
  49. diff -Nur linux-3.15-rc7.orig/include/net/tcp.h linux-3.15-rc7/include/net/tcp.h
  50. --- linux-3.15-rc7.orig/include/net/tcp.h 2014-05-26 01:06:00.000000000 +0200
  51. +++ linux-3.15-rc7/include/net/tcp.h 2014-05-31 22:42:49.028605326 +0200
  52. @@ -251,7 +251,11 @@
  53. extern int sysctl_tcp_retries2;
  54. extern int sysctl_tcp_orphan_retries;
  55. extern int sysctl_tcp_syncookies;
  56. +#ifdef CONFIG_TCP_FASTOPEN
  57. extern int sysctl_tcp_fastopen;
  58. +#else
  59. +#define sysctl_tcp_fastopen 0
  60. +#endif
  61. extern int sysctl_tcp_retrans_collapse;
  62. extern int sysctl_tcp_stdurg;
  63. extern int sysctl_tcp_rfc1337;
  64. @@ -1308,7 +1312,12 @@
  65. size_t size;
  66. int copied; /* queued in tcp_connect() */
  67. };
  68. +
  69. +#ifdef CONFIG_TCP_FASTOPEN
  70. void tcp_free_fastopen_req(struct tcp_sock *tp);
  71. +#else
  72. +static inline void tcp_free_fastopen_req(struct tcp_sock *tp) {}
  73. +#endif
  74. extern struct tcp_fastopen_context __rcu *tcp_fastopen_ctx;
  75. int tcp_fastopen_reset_cipher(void *key, unsigned int len);
  76. diff -Nur linux-3.15-rc7.orig/include/net/tcp.h.orig linux-3.15-rc7/include/net/tcp.h.orig
  77. --- linux-3.15-rc7.orig/include/net/tcp.h.orig 1970-01-01 01:00:00.000000000 +0100
  78. +++ linux-3.15-rc7/include/net/tcp.h.orig 2014-05-26 01:06:00.000000000 +0200
  79. @@ -0,0 +1,1600 @@
  80. +/*
  81. + * INET An implementation of the TCP/IP protocol suite for the LINUX
  82. + * operating system. INET is implemented using the BSD Socket
  83. + * interface as the means of communication with the user level.
  84. + *
  85. + * Definitions for the TCP module.
  86. + *
  87. + * Version: @(#)tcp.h 1.0.5 05/23/93
  88. + *
  89. + * Authors: Ross Biro
  90. + * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
  91. + *
  92. + * This program is free software; you can redistribute it and/or
  93. + * modify it under the terms of the GNU General Public License
  94. + * as published by the Free Software Foundation; either version
  95. + * 2 of the License, or (at your option) any later version.
  96. + */
  97. +#ifndef _TCP_H
  98. +#define _TCP_H
  99. +
  100. +#define FASTRETRANS_DEBUG 1
  101. +
  102. +#include <linux/list.h>
  103. +#include <linux/tcp.h>
  104. +#include <linux/bug.h>
  105. +#include <linux/slab.h>
  106. +#include <linux/cache.h>
  107. +#include <linux/percpu.h>
  108. +#include <linux/skbuff.h>
  109. +#include <linux/dmaengine.h>
  110. +#include <linux/crypto.h>
  111. +#include <linux/cryptohash.h>
  112. +#include <linux/kref.h>
  113. +#include <linux/ktime.h>
  114. +
  115. +#include <net/inet_connection_sock.h>
  116. +#include <net/inet_timewait_sock.h>
  117. +#include <net/inet_hashtables.h>
  118. +#include <net/checksum.h>
  119. +#include <net/request_sock.h>
  120. +#include <net/sock.h>
  121. +#include <net/snmp.h>
  122. +#include <net/ip.h>
  123. +#include <net/tcp_states.h>
  124. +#include <net/inet_ecn.h>
  125. +#include <net/dst.h>
  126. +
  127. +#include <linux/seq_file.h>
  128. +#include <linux/memcontrol.h>
  129. +
  130. +extern struct inet_hashinfo tcp_hashinfo;
  131. +
  132. +extern struct percpu_counter tcp_orphan_count;
  133. +void tcp_time_wait(struct sock *sk, int state, int timeo);
  134. +
  135. +#define MAX_TCP_HEADER (128 + MAX_HEADER)
  136. +#define MAX_TCP_OPTION_SPACE 40
  137. +
  138. +/*
  139. + * Never offer a window over 32767 without using window scaling. Some
  140. + * poor stacks do signed 16bit maths!
  141. + */
  142. +#define MAX_TCP_WINDOW 32767U
  143. +
  144. +/* Minimal accepted MSS. It is (60+60+8) - (20+20). */
  145. +#define TCP_MIN_MSS 88U
  146. +
  147. +/* The least MTU to use for probing */
  148. +#define TCP_BASE_MSS 512
  149. +
  150. +/* After receiving this amount of duplicate ACKs fast retransmit starts. */
  151. +#define TCP_FASTRETRANS_THRESH 3
  152. +
  153. +/* Maximal reordering. */
  154. +#define TCP_MAX_REORDERING 127
  155. +
  156. +/* Maximal number of ACKs sent quickly to accelerate slow-start. */
  157. +#define TCP_MAX_QUICKACKS 16U
  158. +
  159. +/* urg_data states */
  160. +#define TCP_URG_VALID 0x0100
  161. +#define TCP_URG_NOTYET 0x0200
  162. +#define TCP_URG_READ 0x0400
  163. +
  164. +#define TCP_RETR1 3 /*
  165. + * This is how many retries it does before it
  166. + * tries to figure out if the gateway is
  167. + * down. Minimal RFC value is 3; it corresponds
  168. + * to ~3sec-8min depending on RTO.
  169. + */
  170. +
  171. +#define TCP_RETR2 15 /*
  172. + * This should take at least
  173. + * 90 minutes to time out.
  174. + * RFC1122 says that the limit is 100 sec.
  175. + * 15 is ~13-30min depending on RTO.
  176. + */
  177. +
  178. +#define TCP_SYN_RETRIES 6 /* This is how many retries are done
  179. + * when active opening a connection.
  180. + * RFC1122 says the minimum retry MUST
  181. + * be at least 180secs. Nevertheless
  182. + * this value is corresponding to
  183. + * 63secs of retransmission with the
  184. + * current initial RTO.
  185. + */
  186. +
  187. +#define TCP_SYNACK_RETRIES 5 /* This is how may retries are done
  188. + * when passive opening a connection.
  189. + * This is corresponding to 31secs of
  190. + * retransmission with the current
  191. + * initial RTO.
  192. + */
  193. +
  194. +#define TCP_TIMEWAIT_LEN (60*HZ) /* how long to wait to destroy TIME-WAIT
  195. + * state, about 60 seconds */
  196. +#define TCP_FIN_TIMEOUT TCP_TIMEWAIT_LEN
  197. + /* BSD style FIN_WAIT2 deadlock breaker.
  198. + * It used to be 3min, new value is 60sec,
  199. + * to combine FIN-WAIT-2 timeout with
  200. + * TIME-WAIT timer.
  201. + */
  202. +
  203. +#define TCP_DELACK_MAX ((unsigned)(HZ/5)) /* maximal time to delay before sending an ACK */
  204. +#if HZ >= 100
  205. +#define TCP_DELACK_MIN ((unsigned)(HZ/25)) /* minimal time to delay before sending an ACK */
  206. +#define TCP_ATO_MIN ((unsigned)(HZ/25))
  207. +#else
  208. +#define TCP_DELACK_MIN 4U
  209. +#define TCP_ATO_MIN 4U
  210. +#endif
  211. +#define TCP_RTO_MAX ((unsigned)(120*HZ))
  212. +#define TCP_RTO_MIN ((unsigned)(HZ/5))
  213. +#define TCP_TIMEOUT_INIT ((unsigned)(1*HZ)) /* RFC6298 2.1 initial RTO value */
  214. +#define TCP_TIMEOUT_FALLBACK ((unsigned)(3*HZ)) /* RFC 1122 initial RTO value, now
  215. + * used as a fallback RTO for the
  216. + * initial data transmission if no
  217. + * valid RTT sample has been acquired,
  218. + * most likely due to retrans in 3WHS.
  219. + */
  220. +
  221. +#define TCP_RESOURCE_PROBE_INTERVAL ((unsigned)(HZ/2U)) /* Maximal interval between probes
  222. + * for local resources.
  223. + */
  224. +
  225. +#define TCP_KEEPALIVE_TIME (120*60*HZ) /* two hours */
  226. +#define TCP_KEEPALIVE_PROBES 9 /* Max of 9 keepalive probes */
  227. +#define TCP_KEEPALIVE_INTVL (75*HZ)
  228. +
  229. +#define MAX_TCP_KEEPIDLE 32767
  230. +#define MAX_TCP_KEEPINTVL 32767
  231. +#define MAX_TCP_KEEPCNT 127
  232. +#define MAX_TCP_SYNCNT 127
  233. +
  234. +#define TCP_SYNQ_INTERVAL (HZ/5) /* Period of SYNACK timer */
  235. +
  236. +#define TCP_PAWS_24DAYS (60 * 60 * 24 * 24)
  237. +#define TCP_PAWS_MSL 60 /* Per-host timestamps are invalidated
  238. + * after this time. It should be equal
  239. + * (or greater than) TCP_TIMEWAIT_LEN
  240. + * to provide reliability equal to one
  241. + * provided by timewait state.
  242. + */
  243. +#define TCP_PAWS_WINDOW 1 /* Replay window for per-host
  244. + * timestamps. It must be less than
  245. + * minimal timewait lifetime.
  246. + */
  247. +/*
  248. + * TCP option
  249. + */
  250. +
  251. +#define TCPOPT_NOP 1 /* Padding */
  252. +#define TCPOPT_EOL 0 /* End of options */
  253. +#define TCPOPT_MSS 2 /* Segment size negotiating */
  254. +#define TCPOPT_WINDOW 3 /* Window scaling */
  255. +#define TCPOPT_SACK_PERM 4 /* SACK Permitted */
  256. +#define TCPOPT_SACK 5 /* SACK Block */
  257. +#define TCPOPT_TIMESTAMP 8 /* Better RTT estimations/PAWS */
  258. +#define TCPOPT_MD5SIG 19 /* MD5 Signature (RFC2385) */
  259. +#define TCPOPT_EXP 254 /* Experimental */
  260. +/* Magic number to be after the option value for sharing TCP
  261. + * experimental options. See draft-ietf-tcpm-experimental-options-00.txt
  262. + */
  263. +#define TCPOPT_FASTOPEN_MAGIC 0xF989
  264. +
  265. +/*
  266. + * TCP option lengths
  267. + */
  268. +
  269. +#define TCPOLEN_MSS 4
  270. +#define TCPOLEN_WINDOW 3
  271. +#define TCPOLEN_SACK_PERM 2
  272. +#define TCPOLEN_TIMESTAMP 10
  273. +#define TCPOLEN_MD5SIG 18
  274. +#define TCPOLEN_EXP_FASTOPEN_BASE 4
  275. +
  276. +/* But this is what stacks really send out. */
  277. +#define TCPOLEN_TSTAMP_ALIGNED 12
  278. +#define TCPOLEN_WSCALE_ALIGNED 4
  279. +#define TCPOLEN_SACKPERM_ALIGNED 4
  280. +#define TCPOLEN_SACK_BASE 2
  281. +#define TCPOLEN_SACK_BASE_ALIGNED 4
  282. +#define TCPOLEN_SACK_PERBLOCK 8
  283. +#define TCPOLEN_MD5SIG_ALIGNED 20
  284. +#define TCPOLEN_MSS_ALIGNED 4
  285. +
  286. +/* Flags in tp->nonagle */
  287. +#define TCP_NAGLE_OFF 1 /* Nagle's algo is disabled */
  288. +#define TCP_NAGLE_CORK 2 /* Socket is corked */
  289. +#define TCP_NAGLE_PUSH 4 /* Cork is overridden for already queued data */
  290. +
  291. +/* TCP thin-stream limits */
  292. +#define TCP_THIN_LINEAR_RETRIES 6 /* After 6 linear retries, do exp. backoff */
  293. +
  294. +/* TCP initial congestion window as per draft-hkchu-tcpm-initcwnd-01 */
  295. +#define TCP_INIT_CWND 10
  296. +
  297. +/* Bit Flags for sysctl_tcp_fastopen */
  298. +#define TFO_CLIENT_ENABLE 1
  299. +#define TFO_SERVER_ENABLE 2
  300. +#define TFO_CLIENT_NO_COOKIE 4 /* Data in SYN w/o cookie option */
  301. +
  302. +/* Process SYN data but skip cookie validation */
  303. +#define TFO_SERVER_COOKIE_NOT_CHKED 0x100
  304. +/* Accept SYN data w/o any cookie option */
  305. +#define TFO_SERVER_COOKIE_NOT_REQD 0x200
  306. +
  307. +/* Force enable TFO on all listeners, i.e., not requiring the
  308. + * TCP_FASTOPEN socket option. SOCKOPT1/2 determine how to set max_qlen.
  309. + */
  310. +#define TFO_SERVER_WO_SOCKOPT1 0x400
  311. +#define TFO_SERVER_WO_SOCKOPT2 0x800
  312. +/* Always create TFO child sockets on a TFO listener even when
  313. + * cookie/data not present. (For testing purpose!)
  314. + */
  315. +#define TFO_SERVER_ALWAYS 0x1000
  316. +
  317. +extern struct inet_timewait_death_row tcp_death_row;
  318. +
  319. +/* sysctl variables for tcp */
  320. +extern int sysctl_tcp_timestamps;
  321. +extern int sysctl_tcp_window_scaling;
  322. +extern int sysctl_tcp_sack;
  323. +extern int sysctl_tcp_fin_timeout;
  324. +extern int sysctl_tcp_keepalive_time;
  325. +extern int sysctl_tcp_keepalive_probes;
  326. +extern int sysctl_tcp_keepalive_intvl;
  327. +extern int sysctl_tcp_syn_retries;
  328. +extern int sysctl_tcp_synack_retries;
  329. +extern int sysctl_tcp_retries1;
  330. +extern int sysctl_tcp_retries2;
  331. +extern int sysctl_tcp_orphan_retries;
  332. +extern int sysctl_tcp_syncookies;
  333. +extern int sysctl_tcp_fastopen;
  334. +extern int sysctl_tcp_retrans_collapse;
  335. +extern int sysctl_tcp_stdurg;
  336. +extern int sysctl_tcp_rfc1337;
  337. +extern int sysctl_tcp_abort_on_overflow;
  338. +extern int sysctl_tcp_max_orphans;
  339. +extern int sysctl_tcp_fack;
  340. +extern int sysctl_tcp_reordering;
  341. +extern int sysctl_tcp_dsack;
  342. +extern long sysctl_tcp_mem[3];
  343. +extern int sysctl_tcp_wmem[3];
  344. +extern int sysctl_tcp_rmem[3];
  345. +extern int sysctl_tcp_app_win;
  346. +extern int sysctl_tcp_adv_win_scale;
  347. +extern int sysctl_tcp_tw_reuse;
  348. +extern int sysctl_tcp_frto;
  349. +extern int sysctl_tcp_low_latency;
  350. +extern int sysctl_tcp_dma_copybreak;
  351. +extern int sysctl_tcp_nometrics_save;
  352. +extern int sysctl_tcp_moderate_rcvbuf;
  353. +extern int sysctl_tcp_tso_win_divisor;
  354. +extern int sysctl_tcp_mtu_probing;
  355. +extern int sysctl_tcp_base_mss;
  356. +extern int sysctl_tcp_workaround_signed_windows;
  357. +extern int sysctl_tcp_slow_start_after_idle;
  358. +extern int sysctl_tcp_thin_linear_timeouts;
  359. +extern int sysctl_tcp_thin_dupack;
  360. +extern int sysctl_tcp_early_retrans;
  361. +extern int sysctl_tcp_limit_output_bytes;
  362. +extern int sysctl_tcp_challenge_ack_limit;
  363. +extern unsigned int sysctl_tcp_notsent_lowat;
  364. +extern int sysctl_tcp_min_tso_segs;
  365. +extern int sysctl_tcp_autocorking;
  366. +
  367. +extern atomic_long_t tcp_memory_allocated;
  368. +extern struct percpu_counter tcp_sockets_allocated;
  369. +extern int tcp_memory_pressure;
  370. +
  371. +/*
  372. + * The next routines deal with comparing 32 bit unsigned ints
  373. + * and worry about wraparound (automatic with unsigned arithmetic).
  374. + */
  375. +
  376. +static inline bool before(__u32 seq1, __u32 seq2)
  377. +{
  378. + return (__s32)(seq1-seq2) < 0;
  379. +}
  380. +#define after(seq2, seq1) before(seq1, seq2)
  381. +
  382. +/* is s2<=s1<=s3 ? */
  383. +static inline bool between(__u32 seq1, __u32 seq2, __u32 seq3)
  384. +{
  385. + return seq3 - seq2 >= seq1 - seq2;
  386. +}
  387. +
  388. +static inline bool tcp_out_of_memory(struct sock *sk)
  389. +{
  390. + if (sk->sk_wmem_queued > SOCK_MIN_SNDBUF &&
  391. + sk_memory_allocated(sk) > sk_prot_mem_limits(sk, 2))
  392. + return true;
  393. + return false;
  394. +}
  395. +
  396. +static inline bool tcp_too_many_orphans(struct sock *sk, int shift)
  397. +{
  398. + struct percpu_counter *ocp = sk->sk_prot->orphan_count;
  399. + int orphans = percpu_counter_read_positive(ocp);
  400. +
  401. + if (orphans << shift > sysctl_tcp_max_orphans) {
  402. + orphans = percpu_counter_sum_positive(ocp);
  403. + if (orphans << shift > sysctl_tcp_max_orphans)
  404. + return true;
  405. + }
  406. + return false;
  407. +}
  408. +
  409. +bool tcp_check_oom(struct sock *sk, int shift);
  410. +
  411. +/* syncookies: remember time of last synqueue overflow */
  412. +static inline void tcp_synq_overflow(struct sock *sk)
  413. +{
  414. + tcp_sk(sk)->rx_opt.ts_recent_stamp = jiffies;
  415. +}
  416. +
  417. +/* syncookies: no recent synqueue overflow on this listening socket? */
  418. +static inline bool tcp_synq_no_recent_overflow(const struct sock *sk)
  419. +{
  420. + unsigned long last_overflow = tcp_sk(sk)->rx_opt.ts_recent_stamp;
  421. + return time_after(jiffies, last_overflow + TCP_TIMEOUT_FALLBACK);
  422. +}
  423. +
  424. +extern struct proto tcp_prot;
  425. +
  426. +#define TCP_INC_STATS(net, field) SNMP_INC_STATS((net)->mib.tcp_statistics, field)
  427. +#define TCP_INC_STATS_BH(net, field) SNMP_INC_STATS_BH((net)->mib.tcp_statistics, field)
  428. +#define TCP_DEC_STATS(net, field) SNMP_DEC_STATS((net)->mib.tcp_statistics, field)
  429. +#define TCP_ADD_STATS_USER(net, field, val) SNMP_ADD_STATS_USER((net)->mib.tcp_statistics, field, val)
  430. +#define TCP_ADD_STATS(net, field, val) SNMP_ADD_STATS((net)->mib.tcp_statistics, field, val)
  431. +
  432. +void tcp_tasklet_init(void);
  433. +
  434. +void tcp_v4_err(struct sk_buff *skb, u32);
  435. +
  436. +void tcp_shutdown(struct sock *sk, int how);
  437. +
  438. +void tcp_v4_early_demux(struct sk_buff *skb);
  439. +int tcp_v4_rcv(struct sk_buff *skb);
  440. +
  441. +int tcp_v4_tw_remember_stamp(struct inet_timewait_sock *tw);
  442. +int tcp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
  443. + size_t size);
  444. +int tcp_sendpage(struct sock *sk, struct page *page, int offset, size_t size,
  445. + int flags);
  446. +void tcp_release_cb(struct sock *sk);
  447. +void tcp_wfree(struct sk_buff *skb);
  448. +void tcp_write_timer_handler(struct sock *sk);
  449. +void tcp_delack_timer_handler(struct sock *sk);
  450. +int tcp_ioctl(struct sock *sk, int cmd, unsigned long arg);
  451. +int tcp_rcv_state_process(struct sock *sk, struct sk_buff *skb,
  452. + const struct tcphdr *th, unsigned int len);
  453. +void tcp_rcv_established(struct sock *sk, struct sk_buff *skb,
  454. + const struct tcphdr *th, unsigned int len);
  455. +void tcp_rcv_space_adjust(struct sock *sk);
  456. +void tcp_cleanup_rbuf(struct sock *sk, int copied);
  457. +int tcp_twsk_unique(struct sock *sk, struct sock *sktw, void *twp);
  458. +void tcp_twsk_destructor(struct sock *sk);
  459. +ssize_t tcp_splice_read(struct socket *sk, loff_t *ppos,
  460. + struct pipe_inode_info *pipe, size_t len,
  461. + unsigned int flags);
  462. +
  463. +static inline void tcp_dec_quickack_mode(struct sock *sk,
  464. + const unsigned int pkts)
  465. +{
  466. + struct inet_connection_sock *icsk = inet_csk(sk);
  467. +
  468. + if (icsk->icsk_ack.quick) {
  469. + if (pkts >= icsk->icsk_ack.quick) {
  470. + icsk->icsk_ack.quick = 0;
  471. + /* Leaving quickack mode we deflate ATO. */
  472. + icsk->icsk_ack.ato = TCP_ATO_MIN;
  473. + } else
  474. + icsk->icsk_ack.quick -= pkts;
  475. + }
  476. +}
  477. +
  478. +#define TCP_ECN_OK 1
  479. +#define TCP_ECN_QUEUE_CWR 2
  480. +#define TCP_ECN_DEMAND_CWR 4
  481. +#define TCP_ECN_SEEN 8
  482. +
  483. +enum tcp_tw_status {
  484. + TCP_TW_SUCCESS = 0,
  485. + TCP_TW_RST = 1,
  486. + TCP_TW_ACK = 2,
  487. + TCP_TW_SYN = 3
  488. +};
  489. +
  490. +
  491. +enum tcp_tw_status tcp_timewait_state_process(struct inet_timewait_sock *tw,
  492. + struct sk_buff *skb,
  493. + const struct tcphdr *th);
  494. +struct sock *tcp_check_req(struct sock *sk, struct sk_buff *skb,
  495. + struct request_sock *req, struct request_sock **prev,
  496. + bool fastopen);
  497. +int tcp_child_process(struct sock *parent, struct sock *child,
  498. + struct sk_buff *skb);
  499. +void tcp_enter_loss(struct sock *sk, int how);
  500. +void tcp_clear_retrans(struct tcp_sock *tp);
  501. +void tcp_update_metrics(struct sock *sk);
  502. +void tcp_init_metrics(struct sock *sk);
  503. +void tcp_metrics_init(void);
  504. +bool tcp_peer_is_proven(struct request_sock *req, struct dst_entry *dst,
  505. + bool paws_check);
  506. +bool tcp_remember_stamp(struct sock *sk);
  507. +bool tcp_tw_remember_stamp(struct inet_timewait_sock *tw);
  508. +void tcp_fetch_timewait_stamp(struct sock *sk, struct dst_entry *dst);
  509. +void tcp_disable_fack(struct tcp_sock *tp);
  510. +void tcp_close(struct sock *sk, long timeout);
  511. +void tcp_init_sock(struct sock *sk);
  512. +unsigned int tcp_poll(struct file *file, struct socket *sock,
  513. + struct poll_table_struct *wait);
  514. +int tcp_getsockopt(struct sock *sk, int level, int optname,
  515. + char __user *optval, int __user *optlen);
  516. +int tcp_setsockopt(struct sock *sk, int level, int optname,
  517. + char __user *optval, unsigned int optlen);
  518. +int compat_tcp_getsockopt(struct sock *sk, int level, int optname,
  519. + char __user *optval, int __user *optlen);
  520. +int compat_tcp_setsockopt(struct sock *sk, int level, int optname,
  521. + char __user *optval, unsigned int optlen);
  522. +void tcp_set_keepalive(struct sock *sk, int val);
  523. +void tcp_syn_ack_timeout(struct sock *sk, struct request_sock *req);
  524. +int tcp_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
  525. + size_t len, int nonblock, int flags, int *addr_len);
  526. +void tcp_parse_options(const struct sk_buff *skb,
  527. + struct tcp_options_received *opt_rx,
  528. + int estab, struct tcp_fastopen_cookie *foc);
  529. +const u8 *tcp_parse_md5sig_option(const struct tcphdr *th);
  530. +
  531. +/*
  532. + * TCP v4 functions exported for the inet6 API
  533. + */
  534. +
  535. +void tcp_v4_send_check(struct sock *sk, struct sk_buff *skb);
  536. +int tcp_v4_conn_request(struct sock *sk, struct sk_buff *skb);
  537. +struct sock *tcp_create_openreq_child(struct sock *sk,
  538. + struct request_sock *req,
  539. + struct sk_buff *skb);
  540. +struct sock *tcp_v4_syn_recv_sock(struct sock *sk, struct sk_buff *skb,
  541. + struct request_sock *req,
  542. + struct dst_entry *dst);
  543. +int tcp_v4_do_rcv(struct sock *sk, struct sk_buff *skb);
  544. +int tcp_v4_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len);
  545. +int tcp_connect(struct sock *sk);
  546. +struct sk_buff *tcp_make_synack(struct sock *sk, struct dst_entry *dst,
  547. + struct request_sock *req,
  548. + struct tcp_fastopen_cookie *foc);
  549. +int tcp_disconnect(struct sock *sk, int flags);
  550. +
  551. +void tcp_finish_connect(struct sock *sk, struct sk_buff *skb);
  552. +int tcp_send_rcvq(struct sock *sk, struct msghdr *msg, size_t size);
  553. +void inet_sk_rx_dst_set(struct sock *sk, const struct sk_buff *skb);
  554. +
  555. +/* From syncookies.c */
  556. +int __cookie_v4_check(const struct iphdr *iph, const struct tcphdr *th,
  557. + u32 cookie);
  558. +struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb,
  559. + struct ip_options *opt);
  560. +#ifdef CONFIG_SYN_COOKIES
  561. +
  562. +/* Syncookies use a monotonic timer which increments every 60 seconds.
  563. + * This counter is used both as a hash input and partially encoded into
  564. + * the cookie value. A cookie is only validated further if the delta
  565. + * between the current counter value and the encoded one is less than this,
  566. + * i.e. a sent cookie is valid only at most for 2*60 seconds (or less if
  567. + * the counter advances immediately after a cookie is generated).
  568. + */
  569. +#define MAX_SYNCOOKIE_AGE 2
  570. +
  571. +static inline u32 tcp_cookie_time(void)
  572. +{
  573. + u64 val = get_jiffies_64();
  574. +
  575. + do_div(val, 60 * HZ);
  576. + return val;
  577. +}
  578. +
  579. +u32 __cookie_v4_init_sequence(const struct iphdr *iph, const struct tcphdr *th,
  580. + u16 *mssp);
  581. +__u32 cookie_v4_init_sequence(struct sock *sk, struct sk_buff *skb, __u16 *mss);
  582. +#else
  583. +static inline __u32 cookie_v4_init_sequence(struct sock *sk,
  584. + struct sk_buff *skb,
  585. + __u16 *mss)
  586. +{
  587. + return 0;
  588. +}
  589. +#endif
  590. +
  591. +__u32 cookie_init_timestamp(struct request_sock *req);
  592. +bool cookie_check_timestamp(struct tcp_options_received *opt, struct net *net,
  593. + bool *ecn_ok);
  594. +
  595. +/* From net/ipv6/syncookies.c */
  596. +int __cookie_v6_check(const struct ipv6hdr *iph, const struct tcphdr *th,
  597. + u32 cookie);
  598. +struct sock *cookie_v6_check(struct sock *sk, struct sk_buff *skb);
  599. +#ifdef CONFIG_SYN_COOKIES
  600. +u32 __cookie_v6_init_sequence(const struct ipv6hdr *iph,
  601. + const struct tcphdr *th, u16 *mssp);
  602. +__u32 cookie_v6_init_sequence(struct sock *sk, const struct sk_buff *skb,
  603. + __u16 *mss);
  604. +#else
  605. +static inline __u32 cookie_v6_init_sequence(struct sock *sk,
  606. + struct sk_buff *skb,
  607. + __u16 *mss)
  608. +{
  609. + return 0;
  610. +}
  611. +#endif
  612. +/* tcp_output.c */
  613. +
  614. +void __tcp_push_pending_frames(struct sock *sk, unsigned int cur_mss,
  615. + int nonagle);
  616. +bool tcp_may_send_now(struct sock *sk);
  617. +int __tcp_retransmit_skb(struct sock *, struct sk_buff *);
  618. +int tcp_retransmit_skb(struct sock *, struct sk_buff *);
  619. +void tcp_retransmit_timer(struct sock *sk);
  620. +void tcp_xmit_retransmit_queue(struct sock *);
  621. +void tcp_simple_retransmit(struct sock *);
  622. +int tcp_trim_head(struct sock *, struct sk_buff *, u32);
  623. +int tcp_fragment(struct sock *, struct sk_buff *, u32, unsigned int);
  624. +
  625. +void tcp_send_probe0(struct sock *);
  626. +void tcp_send_partial(struct sock *);
  627. +int tcp_write_wakeup(struct sock *);
  628. +void tcp_send_fin(struct sock *sk);
  629. +void tcp_send_active_reset(struct sock *sk, gfp_t priority);
  630. +int tcp_send_synack(struct sock *);
  631. +bool tcp_syn_flood_action(struct sock *sk, const struct sk_buff *skb,
  632. + const char *proto);
  633. +void tcp_push_one(struct sock *, unsigned int mss_now);
  634. +void tcp_send_ack(struct sock *sk);
  635. +void tcp_send_delayed_ack(struct sock *sk);
  636. +void tcp_send_loss_probe(struct sock *sk);
  637. +bool tcp_schedule_loss_probe(struct sock *sk);
  638. +
  639. +/* tcp_input.c */
  640. +void tcp_cwnd_application_limited(struct sock *sk);
  641. +void tcp_resume_early_retransmit(struct sock *sk);
  642. +void tcp_rearm_rto(struct sock *sk);
  643. +void tcp_reset(struct sock *sk);
  644. +
  645. +/* tcp_timer.c */
  646. +void tcp_init_xmit_timers(struct sock *);
  647. +static inline void tcp_clear_xmit_timers(struct sock *sk)
  648. +{
  649. + inet_csk_clear_xmit_timers(sk);
  650. +}
  651. +
  652. +unsigned int tcp_sync_mss(struct sock *sk, u32 pmtu);
  653. +unsigned int tcp_current_mss(struct sock *sk);
  654. +
  655. +/* Bound MSS / TSO packet size with the half of the window */
  656. +static inline int tcp_bound_to_half_wnd(struct tcp_sock *tp, int pktsize)
  657. +{
  658. + int cutoff;
  659. +
  660. + /* When peer uses tiny windows, there is no use in packetizing
  661. + * to sub-MSS pieces for the sake of SWS or making sure there
  662. + * are enough packets in the pipe for fast recovery.
  663. + *
  664. + * On the other hand, for extremely large MSS devices, handling
  665. + * smaller than MSS windows in this way does make sense.
  666. + */
  667. + if (tp->max_window >= 512)
  668. + cutoff = (tp->max_window >> 1);
  669. + else
  670. + cutoff = tp->max_window;
  671. +
  672. + if (cutoff && pktsize > cutoff)
  673. + return max_t(int, cutoff, 68U - tp->tcp_header_len);
  674. + else
  675. + return pktsize;
  676. +}
  677. +
  678. +/* tcp.c */
  679. +void tcp_get_info(const struct sock *, struct tcp_info *);
  680. +
  681. +/* Read 'sendfile()'-style from a TCP socket */
  682. +typedef int (*sk_read_actor_t)(read_descriptor_t *, struct sk_buff *,
  683. + unsigned int, size_t);
  684. +int tcp_read_sock(struct sock *sk, read_descriptor_t *desc,
  685. + sk_read_actor_t recv_actor);
  686. +
  687. +void tcp_initialize_rcv_mss(struct sock *sk);
  688. +
  689. +int tcp_mtu_to_mss(struct sock *sk, int pmtu);
  690. +int tcp_mss_to_mtu(struct sock *sk, int mss);
  691. +void tcp_mtup_init(struct sock *sk);
  692. +void tcp_init_buffer_space(struct sock *sk);
  693. +
  694. +static inline void tcp_bound_rto(const struct sock *sk)
  695. +{
  696. + if (inet_csk(sk)->icsk_rto > TCP_RTO_MAX)
  697. + inet_csk(sk)->icsk_rto = TCP_RTO_MAX;
  698. +}
  699. +
  700. +static inline u32 __tcp_set_rto(const struct tcp_sock *tp)
  701. +{
  702. + return usecs_to_jiffies((tp->srtt_us >> 3) + tp->rttvar_us);
  703. +}
  704. +
  705. +static inline void __tcp_fast_path_on(struct tcp_sock *tp, u32 snd_wnd)
  706. +{
  707. + tp->pred_flags = htonl((tp->tcp_header_len << 26) |
  708. + ntohl(TCP_FLAG_ACK) |
  709. + snd_wnd);
  710. +}
  711. +
  712. +static inline void tcp_fast_path_on(struct tcp_sock *tp)
  713. +{
  714. + __tcp_fast_path_on(tp, tp->snd_wnd >> tp->rx_opt.snd_wscale);
  715. +}
  716. +
  717. +static inline void tcp_fast_path_check(struct sock *sk)
  718. +{
  719. + struct tcp_sock *tp = tcp_sk(sk);
  720. +
  721. + if (skb_queue_empty(&tp->out_of_order_queue) &&
  722. + tp->rcv_wnd &&
  723. + atomic_read(&sk->sk_rmem_alloc) < sk->sk_rcvbuf &&
  724. + !tp->urg_data)
  725. + tcp_fast_path_on(tp);
  726. +}
  727. +
  728. +/* Compute the actual rto_min value */
  729. +static inline u32 tcp_rto_min(struct sock *sk)
  730. +{
  731. + const struct dst_entry *dst = __sk_dst_get(sk);
  732. + u32 rto_min = TCP_RTO_MIN;
  733. +
  734. + if (dst && dst_metric_locked(dst, RTAX_RTO_MIN))
  735. + rto_min = dst_metric_rtt(dst, RTAX_RTO_MIN);
  736. + return rto_min;
  737. +}
  738. +
  739. +static inline u32 tcp_rto_min_us(struct sock *sk)
  740. +{
  741. + return jiffies_to_usecs(tcp_rto_min(sk));
  742. +}
  743. +
  744. +/* Compute the actual receive window we are currently advertising.
  745. + * Rcv_nxt can be after the window if our peer push more data
  746. + * than the offered window.
  747. + */
  748. +static inline u32 tcp_receive_window(const struct tcp_sock *tp)
  749. +{
  750. + s32 win = tp->rcv_wup + tp->rcv_wnd - tp->rcv_nxt;
  751. +
  752. + if (win < 0)
  753. + win = 0;
  754. + return (u32) win;
  755. +}
  756. +
  757. +/* Choose a new window, without checks for shrinking, and without
  758. + * scaling applied to the result. The caller does these things
  759. + * if necessary. This is a "raw" window selection.
  760. + */
  761. +u32 __tcp_select_window(struct sock *sk);
  762. +
  763. +void tcp_send_window_probe(struct sock *sk);
  764. +
  765. +/* TCP timestamps are only 32-bits, this causes a slight
  766. + * complication on 64-bit systems since we store a snapshot
  767. + * of jiffies in the buffer control blocks below. We decided
  768. + * to use only the low 32-bits of jiffies and hide the ugly
  769. + * casts with the following macro.
  770. + */
  771. +#define tcp_time_stamp ((__u32)(jiffies))
  772. +
  773. +#define tcp_flag_byte(th) (((u_int8_t *)th)[13])
  774. +
  775. +#define TCPHDR_FIN 0x01
  776. +#define TCPHDR_SYN 0x02
  777. +#define TCPHDR_RST 0x04
  778. +#define TCPHDR_PSH 0x08
  779. +#define TCPHDR_ACK 0x10
  780. +#define TCPHDR_URG 0x20
  781. +#define TCPHDR_ECE 0x40
  782. +#define TCPHDR_CWR 0x80
  783. +
  784. +/* This is what the send packet queuing engine uses to pass
  785. + * TCP per-packet control information to the transmission code.
  786. + * We also store the host-order sequence numbers in here too.
  787. + * This is 44 bytes if IPV6 is enabled.
  788. + * If this grows please adjust skbuff.h:skbuff->cb[xxx] size appropriately.
  789. + */
  790. +struct tcp_skb_cb {
  791. + union {
  792. + struct inet_skb_parm h4;
  793. +#if IS_ENABLED(CONFIG_IPV6)
  794. + struct inet6_skb_parm h6;
  795. +#endif
  796. + } header; /* For incoming frames */
  797. + __u32 seq; /* Starting sequence number */
  798. + __u32 end_seq; /* SEQ + FIN + SYN + datalen */
  799. + __u32 when; /* used to compute rtt's */
  800. + __u8 tcp_flags; /* TCP header flags. (tcp[13]) */
  801. +
  802. + __u8 sacked; /* State flags for SACK/FACK. */
  803. +#define TCPCB_SACKED_ACKED 0x01 /* SKB ACK'd by a SACK block */
  804. +#define TCPCB_SACKED_RETRANS 0x02 /* SKB retransmitted */
  805. +#define TCPCB_LOST 0x04 /* SKB is lost */
  806. +#define TCPCB_TAGBITS 0x07 /* All tag bits */
  807. +#define TCPCB_EVER_RETRANS 0x80 /* Ever retransmitted frame */
  808. +#define TCPCB_RETRANS (TCPCB_SACKED_RETRANS|TCPCB_EVER_RETRANS)
  809. +
  810. + __u8 ip_dsfield; /* IPv4 tos or IPv6 dsfield */
  811. + /* 1 byte hole */
  812. + __u32 ack_seq; /* Sequence number ACK'd */
  813. +};
  814. +
  815. +#define TCP_SKB_CB(__skb) ((struct tcp_skb_cb *)&((__skb)->cb[0]))
  816. +
  817. +/* RFC3168 : 6.1.1 SYN packets must not have ECT/ECN bits set
  818. + *
  819. + * If we receive a SYN packet with these bits set, it means a network is
  820. + * playing bad games with TOS bits. In order to avoid possible false congestion
  821. + * notifications, we disable TCP ECN negociation.
  822. + */
  823. +static inline void
  824. +TCP_ECN_create_request(struct request_sock *req, const struct sk_buff *skb,
  825. + struct net *net)
  826. +{
  827. + const struct tcphdr *th = tcp_hdr(skb);
  828. +
  829. + if (net->ipv4.sysctl_tcp_ecn && th->ece && th->cwr &&
  830. + INET_ECN_is_not_ect(TCP_SKB_CB(skb)->ip_dsfield))
  831. + inet_rsk(req)->ecn_ok = 1;
  832. +}
  833. +
  834. +/* Due to TSO, an SKB can be composed of multiple actual
  835. + * packets. To keep these tracked properly, we use this.
  836. + */
  837. +static inline int tcp_skb_pcount(const struct sk_buff *skb)
  838. +{
  839. + return skb_shinfo(skb)->gso_segs;
  840. +}
  841. +
  842. +/* This is valid iff tcp_skb_pcount() > 1. */
  843. +static inline int tcp_skb_mss(const struct sk_buff *skb)
  844. +{
  845. + return skb_shinfo(skb)->gso_size;
  846. +}
  847. +
  848. +/* Events passed to congestion control interface */
  849. +enum tcp_ca_event {
  850. + CA_EVENT_TX_START, /* first transmit when no packets in flight */
  851. + CA_EVENT_CWND_RESTART, /* congestion window restart */
  852. + CA_EVENT_COMPLETE_CWR, /* end of congestion recovery */
  853. + CA_EVENT_LOSS, /* loss timeout */
  854. + CA_EVENT_FAST_ACK, /* in sequence ack */
  855. + CA_EVENT_SLOW_ACK, /* other ack */
  856. +};
  857. +
  858. +/*
  859. + * Interface for adding new TCP congestion control handlers
  860. + */
  861. +#define TCP_CA_NAME_MAX 16
  862. +#define TCP_CA_MAX 128
  863. +#define TCP_CA_BUF_MAX (TCP_CA_NAME_MAX*TCP_CA_MAX)
  864. +
  865. +#define TCP_CONG_NON_RESTRICTED 0x1
  866. +
  867. +struct tcp_congestion_ops {
  868. + struct list_head list;
  869. + unsigned long flags;
  870. +
  871. + /* initialize private data (optional) */
  872. + void (*init)(struct sock *sk);
  873. + /* cleanup private data (optional) */
  874. + void (*release)(struct sock *sk);
  875. +
  876. + /* return slow start threshold (required) */
  877. + u32 (*ssthresh)(struct sock *sk);
  878. + /* do new cwnd calculation (required) */
  879. + void (*cong_avoid)(struct sock *sk, u32 ack, u32 acked, u32 in_flight);
  880. + /* call before changing ca_state (optional) */
  881. + void (*set_state)(struct sock *sk, u8 new_state);
  882. + /* call when cwnd event occurs (optional) */
  883. + void (*cwnd_event)(struct sock *sk, enum tcp_ca_event ev);
  884. + /* new value of cwnd after loss (optional) */
  885. + u32 (*undo_cwnd)(struct sock *sk);
  886. + /* hook for packet ack accounting (optional) */
  887. + void (*pkts_acked)(struct sock *sk, u32 num_acked, s32 rtt_us);
  888. + /* get info for inet_diag (optional) */
  889. + void (*get_info)(struct sock *sk, u32 ext, struct sk_buff *skb);
  890. +
  891. + char name[TCP_CA_NAME_MAX];
  892. + struct module *owner;
  893. +};
  894. +
  895. +int tcp_register_congestion_control(struct tcp_congestion_ops *type);
  896. +void tcp_unregister_congestion_control(struct tcp_congestion_ops *type);
  897. +
  898. +void tcp_init_congestion_control(struct sock *sk);
  899. +void tcp_cleanup_congestion_control(struct sock *sk);
  900. +int tcp_set_default_congestion_control(const char *name);
  901. +void tcp_get_default_congestion_control(char *name);
  902. +void tcp_get_available_congestion_control(char *buf, size_t len);
  903. +void tcp_get_allowed_congestion_control(char *buf, size_t len);
  904. +int tcp_set_allowed_congestion_control(char *allowed);
  905. +int tcp_set_congestion_control(struct sock *sk, const char *name);
  906. +int tcp_slow_start(struct tcp_sock *tp, u32 acked);
  907. +void tcp_cong_avoid_ai(struct tcp_sock *tp, u32 w);
  908. +
  909. +extern struct tcp_congestion_ops tcp_init_congestion_ops;
  910. +u32 tcp_reno_ssthresh(struct sock *sk);
  911. +void tcp_reno_cong_avoid(struct sock *sk, u32 ack, u32 acked, u32 in_flight);
  912. +extern struct tcp_congestion_ops tcp_reno;
  913. +
  914. +static inline void tcp_set_ca_state(struct sock *sk, const u8 ca_state)
  915. +{
  916. + struct inet_connection_sock *icsk = inet_csk(sk);
  917. +
  918. + if (icsk->icsk_ca_ops->set_state)
  919. + icsk->icsk_ca_ops->set_state(sk, ca_state);
  920. + icsk->icsk_ca_state = ca_state;
  921. +}
  922. +
  923. +static inline void tcp_ca_event(struct sock *sk, const enum tcp_ca_event event)
  924. +{
  925. + const struct inet_connection_sock *icsk = inet_csk(sk);
  926. +
  927. + if (icsk->icsk_ca_ops->cwnd_event)
  928. + icsk->icsk_ca_ops->cwnd_event(sk, event);
  929. +}
  930. +
  931. +/* These functions determine how the current flow behaves in respect of SACK
  932. + * handling. SACK is negotiated with the peer, and therefore it can vary
  933. + * between different flows.
  934. + *
  935. + * tcp_is_sack - SACK enabled
  936. + * tcp_is_reno - No SACK
  937. + * tcp_is_fack - FACK enabled, implies SACK enabled
  938. + */
  939. +static inline int tcp_is_sack(const struct tcp_sock *tp)
  940. +{
  941. + return tp->rx_opt.sack_ok;
  942. +}
  943. +
  944. +static inline bool tcp_is_reno(const struct tcp_sock *tp)
  945. +{
  946. + return !tcp_is_sack(tp);
  947. +}
  948. +
  949. +static inline bool tcp_is_fack(const struct tcp_sock *tp)
  950. +{
  951. + return tp->rx_opt.sack_ok & TCP_FACK_ENABLED;
  952. +}
  953. +
  954. +static inline void tcp_enable_fack(struct tcp_sock *tp)
  955. +{
  956. + tp->rx_opt.sack_ok |= TCP_FACK_ENABLED;
  957. +}
  958. +
  959. +/* TCP early-retransmit (ER) is similar to but more conservative than
  960. + * the thin-dupack feature. Enable ER only if thin-dupack is disabled.
  961. + */
  962. +static inline void tcp_enable_early_retrans(struct tcp_sock *tp)
  963. +{
  964. + tp->do_early_retrans = sysctl_tcp_early_retrans &&
  965. + sysctl_tcp_early_retrans < 4 && !sysctl_tcp_thin_dupack &&
  966. + sysctl_tcp_reordering == 3;
  967. +}
  968. +
  969. +static inline void tcp_disable_early_retrans(struct tcp_sock *tp)
  970. +{
  971. + tp->do_early_retrans = 0;
  972. +}
  973. +
  974. +static inline unsigned int tcp_left_out(const struct tcp_sock *tp)
  975. +{
  976. + return tp->sacked_out + tp->lost_out;
  977. +}
  978. +
  979. +/* This determines how many packets are "in the network" to the best
  980. + * of our knowledge. In many cases it is conservative, but where
  981. + * detailed information is available from the receiver (via SACK
  982. + * blocks etc.) we can make more aggressive calculations.
  983. + *
  984. + * Use this for decisions involving congestion control, use just
  985. + * tp->packets_out to determine if the send queue is empty or not.
  986. + *
  987. + * Read this equation as:
  988. + *
  989. + * "Packets sent once on transmission queue" MINUS
  990. + * "Packets left network, but not honestly ACKed yet" PLUS
  991. + * "Packets fast retransmitted"
  992. + */
  993. +static inline unsigned int tcp_packets_in_flight(const struct tcp_sock *tp)
  994. +{
  995. + return tp->packets_out - tcp_left_out(tp) + tp->retrans_out;
  996. +}
  997. +
  998. +#define TCP_INFINITE_SSTHRESH 0x7fffffff
  999. +
  1000. +static inline bool tcp_in_initial_slowstart(const struct tcp_sock *tp)
  1001. +{
  1002. + return tp->snd_ssthresh >= TCP_INFINITE_SSTHRESH;
  1003. +}
  1004. +
  1005. +static inline bool tcp_in_cwnd_reduction(const struct sock *sk)
  1006. +{
  1007. + return (TCPF_CA_CWR | TCPF_CA_Recovery) &
  1008. + (1 << inet_csk(sk)->icsk_ca_state);
  1009. +}
  1010. +
  1011. +/* If cwnd > ssthresh, we may raise ssthresh to be half-way to cwnd.
  1012. + * The exception is cwnd reduction phase, when cwnd is decreasing towards
  1013. + * ssthresh.
  1014. + */
  1015. +static inline __u32 tcp_current_ssthresh(const struct sock *sk)
  1016. +{
  1017. + const struct tcp_sock *tp = tcp_sk(sk);
  1018. +
  1019. + if (tcp_in_cwnd_reduction(sk))
  1020. + return tp->snd_ssthresh;
  1021. + else
  1022. + return max(tp->snd_ssthresh,
  1023. + ((tp->snd_cwnd >> 1) +
  1024. + (tp->snd_cwnd >> 2)));
  1025. +}
  1026. +
  1027. +/* Use define here intentionally to get WARN_ON location shown at the caller */
  1028. +#define tcp_verify_left_out(tp) WARN_ON(tcp_left_out(tp) > tp->packets_out)
  1029. +
  1030. +void tcp_enter_cwr(struct sock *sk, const int set_ssthresh);
  1031. +__u32 tcp_init_cwnd(const struct tcp_sock *tp, const struct dst_entry *dst);
  1032. +
  1033. +/* The maximum number of MSS of available cwnd for which TSO defers
  1034. + * sending if not using sysctl_tcp_tso_win_divisor.
  1035. + */
  1036. +static inline __u32 tcp_max_tso_deferred_mss(const struct tcp_sock *tp)
  1037. +{
  1038. + return 3;
  1039. +}
  1040. +
  1041. +/* Slow start with delack produces 3 packets of burst, so that
  1042. + * it is safe "de facto". This will be the default - same as
  1043. + * the default reordering threshold - but if reordering increases,
  1044. + * we must be able to allow cwnd to burst at least this much in order
  1045. + * to not pull it back when holes are filled.
  1046. + */
  1047. +static __inline__ __u32 tcp_max_burst(const struct tcp_sock *tp)
  1048. +{
  1049. + return tp->reordering;
  1050. +}
  1051. +
  1052. +/* Returns end sequence number of the receiver's advertised window */
  1053. +static inline u32 tcp_wnd_end(const struct tcp_sock *tp)
  1054. +{
  1055. + return tp->snd_una + tp->snd_wnd;
  1056. +}
  1057. +bool tcp_is_cwnd_limited(const struct sock *sk, u32 in_flight);
  1058. +
  1059. +static inline void tcp_check_probe_timer(struct sock *sk)
  1060. +{
  1061. + const struct tcp_sock *tp = tcp_sk(sk);
  1062. + const struct inet_connection_sock *icsk = inet_csk(sk);
  1063. +
  1064. + if (!tp->packets_out && !icsk->icsk_pending)
  1065. + inet_csk_reset_xmit_timer(sk, ICSK_TIME_PROBE0,
  1066. + icsk->icsk_rto, TCP_RTO_MAX);
  1067. +}
  1068. +
  1069. +static inline void tcp_init_wl(struct tcp_sock *tp, u32 seq)
  1070. +{
  1071. + tp->snd_wl1 = seq;
  1072. +}
  1073. +
  1074. +static inline void tcp_update_wl(struct tcp_sock *tp, u32 seq)
  1075. +{
  1076. + tp->snd_wl1 = seq;
  1077. +}
  1078. +
  1079. +/*
  1080. + * Calculate(/check) TCP checksum
  1081. + */
  1082. +static inline __sum16 tcp_v4_check(int len, __be32 saddr,
  1083. + __be32 daddr, __wsum base)
  1084. +{
  1085. + return csum_tcpudp_magic(saddr,daddr,len,IPPROTO_TCP,base);
  1086. +}
  1087. +
  1088. +static inline __sum16 __tcp_checksum_complete(struct sk_buff *skb)
  1089. +{
  1090. + return __skb_checksum_complete(skb);
  1091. +}
  1092. +
  1093. +static inline bool tcp_checksum_complete(struct sk_buff *skb)
  1094. +{
  1095. + return !skb_csum_unnecessary(skb) &&
  1096. + __tcp_checksum_complete(skb);
  1097. +}
  1098. +
  1099. +/* Prequeue for VJ style copy to user, combined with checksumming. */
  1100. +
  1101. +static inline void tcp_prequeue_init(struct tcp_sock *tp)
  1102. +{
  1103. + tp->ucopy.task = NULL;
  1104. + tp->ucopy.len = 0;
  1105. + tp->ucopy.memory = 0;
  1106. + skb_queue_head_init(&tp->ucopy.prequeue);
  1107. +#ifdef CONFIG_NET_DMA
  1108. + tp->ucopy.dma_chan = NULL;
  1109. + tp->ucopy.wakeup = 0;
  1110. + tp->ucopy.pinned_list = NULL;
  1111. + tp->ucopy.dma_cookie = 0;
  1112. +#endif
  1113. +}
  1114. +
  1115. +bool tcp_prequeue(struct sock *sk, struct sk_buff *skb);
  1116. +
  1117. +#undef STATE_TRACE
  1118. +
  1119. +#ifdef STATE_TRACE
  1120. +static const char *statename[]={
  1121. + "Unused","Established","Syn Sent","Syn Recv",
  1122. + "Fin Wait 1","Fin Wait 2","Time Wait", "Close",
  1123. + "Close Wait","Last ACK","Listen","Closing"
  1124. +};
  1125. +#endif
  1126. +void tcp_set_state(struct sock *sk, int state);
  1127. +
  1128. +void tcp_done(struct sock *sk);
  1129. +
  1130. +static inline void tcp_sack_reset(struct tcp_options_received *rx_opt)
  1131. +{
  1132. + rx_opt->dsack = 0;
  1133. + rx_opt->num_sacks = 0;
  1134. +}
  1135. +
  1136. +u32 tcp_default_init_rwnd(u32 mss);
  1137. +
  1138. +/* Determine a window scaling and initial window to offer. */
  1139. +void tcp_select_initial_window(int __space, __u32 mss, __u32 *rcv_wnd,
  1140. + __u32 *window_clamp, int wscale_ok,
  1141. + __u8 *rcv_wscale, __u32 init_rcv_wnd);
  1142. +
  1143. +static inline int tcp_win_from_space(int space)
  1144. +{
  1145. + return sysctl_tcp_adv_win_scale<=0 ?
  1146. + (space>>(-sysctl_tcp_adv_win_scale)) :
  1147. + space - (space>>sysctl_tcp_adv_win_scale);
  1148. +}
  1149. +
  1150. +/* Note: caller must be prepared to deal with negative returns */
  1151. +static inline int tcp_space(const struct sock *sk)
  1152. +{
  1153. + return tcp_win_from_space(sk->sk_rcvbuf -
  1154. + atomic_read(&sk->sk_rmem_alloc));
  1155. +}
  1156. +
  1157. +static inline int tcp_full_space(const struct sock *sk)
  1158. +{
  1159. + return tcp_win_from_space(sk->sk_rcvbuf);
  1160. +}
  1161. +
  1162. +static inline void tcp_openreq_init(struct request_sock *req,
  1163. + struct tcp_options_received *rx_opt,
  1164. + struct sk_buff *skb)
  1165. +{
  1166. + struct inet_request_sock *ireq = inet_rsk(req);
  1167. +
  1168. + req->rcv_wnd = 0; /* So that tcp_send_synack() knows! */
  1169. + req->cookie_ts = 0;
  1170. + tcp_rsk(req)->rcv_isn = TCP_SKB_CB(skb)->seq;
  1171. + tcp_rsk(req)->rcv_nxt = TCP_SKB_CB(skb)->seq + 1;
  1172. + tcp_rsk(req)->snt_synack = 0;
  1173. + req->mss = rx_opt->mss_clamp;
  1174. + req->ts_recent = rx_opt->saw_tstamp ? rx_opt->rcv_tsval : 0;
  1175. + ireq->tstamp_ok = rx_opt->tstamp_ok;
  1176. + ireq->sack_ok = rx_opt->sack_ok;
  1177. + ireq->snd_wscale = rx_opt->snd_wscale;
  1178. + ireq->wscale_ok = rx_opt->wscale_ok;
  1179. + ireq->acked = 0;
  1180. + ireq->ecn_ok = 0;
  1181. + ireq->ir_rmt_port = tcp_hdr(skb)->source;
  1182. + ireq->ir_num = ntohs(tcp_hdr(skb)->dest);
  1183. +}
  1184. +
  1185. +void tcp_enter_memory_pressure(struct sock *sk);
  1186. +
  1187. +static inline int keepalive_intvl_when(const struct tcp_sock *tp)
  1188. +{
  1189. + return tp->keepalive_intvl ? : sysctl_tcp_keepalive_intvl;
  1190. +}
  1191. +
  1192. +static inline int keepalive_time_when(const struct tcp_sock *tp)
  1193. +{
  1194. + return tp->keepalive_time ? : sysctl_tcp_keepalive_time;
  1195. +}
  1196. +
  1197. +static inline int keepalive_probes(const struct tcp_sock *tp)
  1198. +{
  1199. + return tp->keepalive_probes ? : sysctl_tcp_keepalive_probes;
  1200. +}
  1201. +
  1202. +static inline u32 keepalive_time_elapsed(const struct tcp_sock *tp)
  1203. +{
  1204. + const struct inet_connection_sock *icsk = &tp->inet_conn;
  1205. +
  1206. + return min_t(u32, tcp_time_stamp - icsk->icsk_ack.lrcvtime,
  1207. + tcp_time_stamp - tp->rcv_tstamp);
  1208. +}
  1209. +
  1210. +static inline int tcp_fin_time(const struct sock *sk)
  1211. +{
  1212. + int fin_timeout = tcp_sk(sk)->linger2 ? : sysctl_tcp_fin_timeout;
  1213. + const int rto = inet_csk(sk)->icsk_rto;
  1214. +
  1215. + if (fin_timeout < (rto << 2) - (rto >> 1))
  1216. + fin_timeout = (rto << 2) - (rto >> 1);
  1217. +
  1218. + return fin_timeout;
  1219. +}
  1220. +
  1221. +static inline bool tcp_paws_check(const struct tcp_options_received *rx_opt,
  1222. + int paws_win)
  1223. +{
  1224. + if ((s32)(rx_opt->ts_recent - rx_opt->rcv_tsval) <= paws_win)
  1225. + return true;
  1226. + if (unlikely(get_seconds() >= rx_opt->ts_recent_stamp + TCP_PAWS_24DAYS))
  1227. + return true;
  1228. + /*
  1229. + * Some OSes send SYN and SYNACK messages with tsval=0 tsecr=0,
  1230. + * then following tcp messages have valid values. Ignore 0 value,
  1231. + * or else 'negative' tsval might forbid us to accept their packets.
  1232. + */
  1233. + if (!rx_opt->ts_recent)
  1234. + return true;
  1235. + return false;
  1236. +}
  1237. +
  1238. +static inline bool tcp_paws_reject(const struct tcp_options_received *rx_opt,
  1239. + int rst)
  1240. +{
  1241. + if (tcp_paws_check(rx_opt, 0))
  1242. + return false;
  1243. +
  1244. + /* RST segments are not recommended to carry timestamp,
  1245. + and, if they do, it is recommended to ignore PAWS because
  1246. + "their cleanup function should take precedence over timestamps."
  1247. + Certainly, it is mistake. It is necessary to understand the reasons
  1248. + of this constraint to relax it: if peer reboots, clock may go
  1249. + out-of-sync and half-open connections will not be reset.
  1250. + Actually, the problem would be not existing if all
  1251. + the implementations followed draft about maintaining clock
  1252. + via reboots. Linux-2.2 DOES NOT!
  1253. +
  1254. + However, we can relax time bounds for RST segments to MSL.
  1255. + */
  1256. + if (rst && get_seconds() >= rx_opt->ts_recent_stamp + TCP_PAWS_MSL)
  1257. + return false;
  1258. + return true;
  1259. +}
  1260. +
  1261. +static inline void tcp_mib_init(struct net *net)
  1262. +{
  1263. + /* See RFC 2012 */
  1264. + TCP_ADD_STATS_USER(net, TCP_MIB_RTOALGORITHM, 1);
  1265. + TCP_ADD_STATS_USER(net, TCP_MIB_RTOMIN, TCP_RTO_MIN*1000/HZ);
  1266. + TCP_ADD_STATS_USER(net, TCP_MIB_RTOMAX, TCP_RTO_MAX*1000/HZ);
  1267. + TCP_ADD_STATS_USER(net, TCP_MIB_MAXCONN, -1);
  1268. +}
  1269. +
  1270. +/* from STCP */
  1271. +static inline void tcp_clear_retrans_hints_partial(struct tcp_sock *tp)
  1272. +{
  1273. + tp->lost_skb_hint = NULL;
  1274. +}
  1275. +
  1276. +static inline void tcp_clear_all_retrans_hints(struct tcp_sock *tp)
  1277. +{
  1278. + tcp_clear_retrans_hints_partial(tp);
  1279. + tp->retransmit_skb_hint = NULL;
  1280. +}
  1281. +
  1282. +/* MD5 Signature */
  1283. +struct crypto_hash;
  1284. +
  1285. +union tcp_md5_addr {
  1286. + struct in_addr a4;
  1287. +#if IS_ENABLED(CONFIG_IPV6)
  1288. + struct in6_addr a6;
  1289. +#endif
  1290. +};
  1291. +
  1292. +/* - key database */
  1293. +struct tcp_md5sig_key {
  1294. + struct hlist_node node;
  1295. + u8 keylen;
  1296. + u8 family; /* AF_INET or AF_INET6 */
  1297. + union tcp_md5_addr addr;
  1298. + u8 key[TCP_MD5SIG_MAXKEYLEN];
  1299. + struct rcu_head rcu;
  1300. +};
  1301. +
  1302. +/* - sock block */
  1303. +struct tcp_md5sig_info {
  1304. + struct hlist_head head;
  1305. + struct rcu_head rcu;
  1306. +};
  1307. +
  1308. +/* - pseudo header */
  1309. +struct tcp4_pseudohdr {
  1310. + __be32 saddr;
  1311. + __be32 daddr;
  1312. + __u8 pad;
  1313. + __u8 protocol;
  1314. + __be16 len;
  1315. +};
  1316. +
  1317. +struct tcp6_pseudohdr {
  1318. + struct in6_addr saddr;
  1319. + struct in6_addr daddr;
  1320. + __be32 len;
  1321. + __be32 protocol; /* including padding */
  1322. +};
  1323. +
  1324. +union tcp_md5sum_block {
  1325. + struct tcp4_pseudohdr ip4;
  1326. +#if IS_ENABLED(CONFIG_IPV6)
  1327. + struct tcp6_pseudohdr ip6;
  1328. +#endif
  1329. +};
  1330. +
  1331. +/* - pool: digest algorithm, hash description and scratch buffer */
  1332. +struct tcp_md5sig_pool {
  1333. + struct hash_desc md5_desc;
  1334. + union tcp_md5sum_block md5_blk;
  1335. +};
  1336. +
  1337. +/* - functions */
  1338. +int tcp_v4_md5_hash_skb(char *md5_hash, struct tcp_md5sig_key *key,
  1339. + const struct sock *sk, const struct request_sock *req,
  1340. + const struct sk_buff *skb);
  1341. +int tcp_md5_do_add(struct sock *sk, const union tcp_md5_addr *addr,
  1342. + int family, const u8 *newkey, u8 newkeylen, gfp_t gfp);
  1343. +int tcp_md5_do_del(struct sock *sk, const union tcp_md5_addr *addr,
  1344. + int family);
  1345. +struct tcp_md5sig_key *tcp_v4_md5_lookup(struct sock *sk,
  1346. + struct sock *addr_sk);
  1347. +
  1348. +#ifdef CONFIG_TCP_MD5SIG
  1349. +struct tcp_md5sig_key *tcp_md5_do_lookup(struct sock *sk,
  1350. + const union tcp_md5_addr *addr,
  1351. + int family);
  1352. +#define tcp_twsk_md5_key(twsk) ((twsk)->tw_md5_key)
  1353. +#else
  1354. +static inline struct tcp_md5sig_key *tcp_md5_do_lookup(struct sock *sk,
  1355. + const union tcp_md5_addr *addr,
  1356. + int family)
  1357. +{
  1358. + return NULL;
  1359. +}
  1360. +#define tcp_twsk_md5_key(twsk) NULL
  1361. +#endif
  1362. +
  1363. +bool tcp_alloc_md5sig_pool(void);
  1364. +
  1365. +struct tcp_md5sig_pool *tcp_get_md5sig_pool(void);
  1366. +static inline void tcp_put_md5sig_pool(void)
  1367. +{
  1368. + local_bh_enable();
  1369. +}
  1370. +
  1371. +int tcp_md5_hash_header(struct tcp_md5sig_pool *, const struct tcphdr *);
  1372. +int tcp_md5_hash_skb_data(struct tcp_md5sig_pool *, const struct sk_buff *,
  1373. + unsigned int header_len);
  1374. +int tcp_md5_hash_key(struct tcp_md5sig_pool *hp,
  1375. + const struct tcp_md5sig_key *key);
  1376. +
  1377. +/* From tcp_fastopen.c */
  1378. +void tcp_fastopen_cache_get(struct sock *sk, u16 *mss,
  1379. + struct tcp_fastopen_cookie *cookie, int *syn_loss,
  1380. + unsigned long *last_syn_loss);
  1381. +void tcp_fastopen_cache_set(struct sock *sk, u16 mss,
  1382. + struct tcp_fastopen_cookie *cookie, bool syn_lost);
  1383. +struct tcp_fastopen_request {
  1384. + /* Fast Open cookie. Size 0 means a cookie request */
  1385. + struct tcp_fastopen_cookie cookie;
  1386. + struct msghdr *data; /* data in MSG_FASTOPEN */
  1387. + size_t size;
  1388. + int copied; /* queued in tcp_connect() */
  1389. +};
  1390. +void tcp_free_fastopen_req(struct tcp_sock *tp);
  1391. +
  1392. +extern struct tcp_fastopen_context __rcu *tcp_fastopen_ctx;
  1393. +int tcp_fastopen_reset_cipher(void *key, unsigned int len);
  1394. +void tcp_fastopen_cookie_gen(__be32 src, __be32 dst,
  1395. + struct tcp_fastopen_cookie *foc);
  1396. +void tcp_fastopen_init_key_once(bool publish);
  1397. +#define TCP_FASTOPEN_KEY_LENGTH 16
  1398. +
  1399. +/* Fastopen key context */
  1400. +struct tcp_fastopen_context {
  1401. + struct crypto_cipher *tfm;
  1402. + __u8 key[TCP_FASTOPEN_KEY_LENGTH];
  1403. + struct rcu_head rcu;
  1404. +};
  1405. +
  1406. +/* write queue abstraction */
  1407. +static inline void tcp_write_queue_purge(struct sock *sk)
  1408. +{
  1409. + struct sk_buff *skb;
  1410. +
  1411. + while ((skb = __skb_dequeue(&sk->sk_write_queue)) != NULL)
  1412. + sk_wmem_free_skb(sk, skb);
  1413. + sk_mem_reclaim(sk);
  1414. + tcp_clear_all_retrans_hints(tcp_sk(sk));
  1415. +}
  1416. +
  1417. +static inline struct sk_buff *tcp_write_queue_head(const struct sock *sk)
  1418. +{
  1419. + return skb_peek(&sk->sk_write_queue);
  1420. +}
  1421. +
  1422. +static inline struct sk_buff *tcp_write_queue_tail(const struct sock *sk)
  1423. +{
  1424. + return skb_peek_tail(&sk->sk_write_queue);
  1425. +}
  1426. +
  1427. +static inline struct sk_buff *tcp_write_queue_next(const struct sock *sk,
  1428. + const struct sk_buff *skb)
  1429. +{
  1430. + return skb_queue_next(&sk->sk_write_queue, skb);
  1431. +}
  1432. +
  1433. +static inline struct sk_buff *tcp_write_queue_prev(const struct sock *sk,
  1434. + const struct sk_buff *skb)
  1435. +{
  1436. + return skb_queue_prev(&sk->sk_write_queue, skb);
  1437. +}
  1438. +
  1439. +#define tcp_for_write_queue(skb, sk) \
  1440. + skb_queue_walk(&(sk)->sk_write_queue, skb)
  1441. +
  1442. +#define tcp_for_write_queue_from(skb, sk) \
  1443. + skb_queue_walk_from(&(sk)->sk_write_queue, skb)
  1444. +
  1445. +#define tcp_for_write_queue_from_safe(skb, tmp, sk) \
  1446. + skb_queue_walk_from_safe(&(sk)->sk_write_queue, skb, tmp)
  1447. +
  1448. +static inline struct sk_buff *tcp_send_head(const struct sock *sk)
  1449. +{
  1450. + return sk->sk_send_head;
  1451. +}
  1452. +
  1453. +static inline bool tcp_skb_is_last(const struct sock *sk,
  1454. + const struct sk_buff *skb)
  1455. +{
  1456. + return skb_queue_is_last(&sk->sk_write_queue, skb);
  1457. +}
  1458. +
  1459. +static inline void tcp_advance_send_head(struct sock *sk, const struct sk_buff *skb)
  1460. +{
  1461. + if (tcp_skb_is_last(sk, skb))
  1462. + sk->sk_send_head = NULL;
  1463. + else
  1464. + sk->sk_send_head = tcp_write_queue_next(sk, skb);
  1465. +}
  1466. +
  1467. +static inline void tcp_check_send_head(struct sock *sk, struct sk_buff *skb_unlinked)
  1468. +{
  1469. + if (sk->sk_send_head == skb_unlinked)
  1470. + sk->sk_send_head = NULL;
  1471. +}
  1472. +
  1473. +static inline void tcp_init_send_head(struct sock *sk)
  1474. +{
  1475. + sk->sk_send_head = NULL;
  1476. +}
  1477. +
  1478. +static inline void __tcp_add_write_queue_tail(struct sock *sk, struct sk_buff *skb)
  1479. +{
  1480. + __skb_queue_tail(&sk->sk_write_queue, skb);
  1481. +}
  1482. +
  1483. +static inline void tcp_add_write_queue_tail(struct sock *sk, struct sk_buff *skb)
  1484. +{
  1485. + __tcp_add_write_queue_tail(sk, skb);
  1486. +
  1487. + /* Queue it, remembering where we must start sending. */
  1488. + if (sk->sk_send_head == NULL) {
  1489. + sk->sk_send_head = skb;
  1490. +
  1491. + if (tcp_sk(sk)->highest_sack == NULL)
  1492. + tcp_sk(sk)->highest_sack = skb;
  1493. + }
  1494. +}
  1495. +
  1496. +static inline void __tcp_add_write_queue_head(struct sock *sk, struct sk_buff *skb)
  1497. +{
  1498. + __skb_queue_head(&sk->sk_write_queue, skb);
  1499. +}
  1500. +
  1501. +/* Insert buff after skb on the write queue of sk. */
  1502. +static inline void tcp_insert_write_queue_after(struct sk_buff *skb,
  1503. + struct sk_buff *buff,
  1504. + struct sock *sk)
  1505. +{
  1506. + __skb_queue_after(&sk->sk_write_queue, skb, buff);
  1507. +}
  1508. +
  1509. +/* Insert new before skb on the write queue of sk. */
  1510. +static inline void tcp_insert_write_queue_before(struct sk_buff *new,
  1511. + struct sk_buff *skb,
  1512. + struct sock *sk)
  1513. +{
  1514. + __skb_queue_before(&sk->sk_write_queue, skb, new);
  1515. +
  1516. + if (sk->sk_send_head == skb)
  1517. + sk->sk_send_head = new;
  1518. +}
  1519. +
  1520. +static inline void tcp_unlink_write_queue(struct sk_buff *skb, struct sock *sk)
  1521. +{
  1522. + __skb_unlink(skb, &sk->sk_write_queue);
  1523. +}
  1524. +
  1525. +static inline bool tcp_write_queue_empty(struct sock *sk)
  1526. +{
  1527. + return skb_queue_empty(&sk->sk_write_queue);
  1528. +}
  1529. +
  1530. +static inline void tcp_push_pending_frames(struct sock *sk)
  1531. +{
  1532. + if (tcp_send_head(sk)) {
  1533. + struct tcp_sock *tp = tcp_sk(sk);
  1534. +
  1535. + __tcp_push_pending_frames(sk, tcp_current_mss(sk), tp->nonagle);
  1536. + }
  1537. +}
  1538. +
  1539. +/* Start sequence of the skb just after the highest skb with SACKed
  1540. + * bit, valid only if sacked_out > 0 or when the caller has ensured
  1541. + * validity by itself.
  1542. + */
  1543. +static inline u32 tcp_highest_sack_seq(struct tcp_sock *tp)
  1544. +{
  1545. + if (!tp->sacked_out)
  1546. + return tp->snd_una;
  1547. +
  1548. + if (tp->highest_sack == NULL)
  1549. + return tp->snd_nxt;
  1550. +
  1551. + return TCP_SKB_CB(tp->highest_sack)->seq;
  1552. +}
  1553. +
  1554. +static inline void tcp_advance_highest_sack(struct sock *sk, struct sk_buff *skb)
  1555. +{
  1556. + tcp_sk(sk)->highest_sack = tcp_skb_is_last(sk, skb) ? NULL :
  1557. + tcp_write_queue_next(sk, skb);
  1558. +}
  1559. +
  1560. +static inline struct sk_buff *tcp_highest_sack(struct sock *sk)
  1561. +{
  1562. + return tcp_sk(sk)->highest_sack;
  1563. +}
  1564. +
  1565. +static inline void tcp_highest_sack_reset(struct sock *sk)
  1566. +{
  1567. + tcp_sk(sk)->highest_sack = tcp_write_queue_head(sk);
  1568. +}
  1569. +
  1570. +/* Called when old skb is about to be deleted (to be combined with new skb) */
  1571. +static inline void tcp_highest_sack_combine(struct sock *sk,
  1572. + struct sk_buff *old,
  1573. + struct sk_buff *new)
  1574. +{
  1575. + if (tcp_sk(sk)->sacked_out && (old == tcp_sk(sk)->highest_sack))
  1576. + tcp_sk(sk)->highest_sack = new;
  1577. +}
  1578. +
  1579. +/* Determines whether this is a thin stream (which may suffer from
  1580. + * increased latency). Used to trigger latency-reducing mechanisms.
  1581. + */
  1582. +static inline bool tcp_stream_is_thin(struct tcp_sock *tp)
  1583. +{
  1584. + return tp->packets_out < 4 && !tcp_in_initial_slowstart(tp);
  1585. +}
  1586. +
  1587. +/* /proc */
  1588. +enum tcp_seq_states {
  1589. + TCP_SEQ_STATE_LISTENING,
  1590. + TCP_SEQ_STATE_OPENREQ,
  1591. + TCP_SEQ_STATE_ESTABLISHED,
  1592. +};
  1593. +
  1594. +int tcp_seq_open(struct inode *inode, struct file *file);
  1595. +
  1596. +struct tcp_seq_afinfo {
  1597. + char *name;
  1598. + sa_family_t family;
  1599. + const struct file_operations *seq_fops;
  1600. + struct seq_operations seq_ops;
  1601. +};
  1602. +
  1603. +struct tcp_iter_state {
  1604. + struct seq_net_private p;
  1605. + sa_family_t family;
  1606. + enum tcp_seq_states state;
  1607. + struct sock *syn_wait_sk;
  1608. + int bucket, offset, sbucket, num;
  1609. + kuid_t uid;
  1610. + loff_t last_pos;
  1611. +};
  1612. +
  1613. +int tcp_proc_register(struct net *net, struct tcp_seq_afinfo *afinfo);
  1614. +void tcp_proc_unregister(struct net *net, struct tcp_seq_afinfo *afinfo);
  1615. +
  1616. +extern struct request_sock_ops tcp_request_sock_ops;
  1617. +extern struct request_sock_ops tcp6_request_sock_ops;
  1618. +
  1619. +void tcp_v4_destroy_sock(struct sock *sk);
  1620. +
  1621. +struct sk_buff *tcp_gso_segment(struct sk_buff *skb,
  1622. + netdev_features_t features);
  1623. +struct sk_buff **tcp_gro_receive(struct sk_buff **head, struct sk_buff *skb);
  1624. +int tcp_gro_complete(struct sk_buff *skb);
  1625. +
  1626. +void __tcp_v4_send_check(struct sk_buff *skb, __be32 saddr, __be32 daddr);
  1627. +
  1628. +static inline u32 tcp_notsent_lowat(const struct tcp_sock *tp)
  1629. +{
  1630. + return tp->notsent_lowat ?: sysctl_tcp_notsent_lowat;
  1631. +}
  1632. +
  1633. +static inline bool tcp_stream_memory_free(const struct sock *sk)
  1634. +{
  1635. + const struct tcp_sock *tp = tcp_sk(sk);
  1636. + u32 notsent_bytes = tp->write_seq - tp->snd_nxt;
  1637. +
  1638. + return notsent_bytes < tcp_notsent_lowat(tp);
  1639. +}
  1640. +
  1641. +#ifdef CONFIG_PROC_FS
  1642. +int tcp4_proc_init(void);
  1643. +void tcp4_proc_exit(void);
  1644. +#endif
  1645. +
  1646. +/* TCP af-specific functions */
  1647. +struct tcp_sock_af_ops {
  1648. +#ifdef CONFIG_TCP_MD5SIG
  1649. + struct tcp_md5sig_key *(*md5_lookup) (struct sock *sk,
  1650. + struct sock *addr_sk);
  1651. + int (*calc_md5_hash) (char *location,
  1652. + struct tcp_md5sig_key *md5,
  1653. + const struct sock *sk,
  1654. + const struct request_sock *req,
  1655. + const struct sk_buff *skb);
  1656. + int (*md5_parse) (struct sock *sk,
  1657. + char __user *optval,
  1658. + int optlen);
  1659. +#endif
  1660. +};
  1661. +
  1662. +struct tcp_request_sock_ops {
  1663. +#ifdef CONFIG_TCP_MD5SIG
  1664. + struct tcp_md5sig_key *(*md5_lookup) (struct sock *sk,
  1665. + struct request_sock *req);
  1666. + int (*calc_md5_hash) (char *location,
  1667. + struct tcp_md5sig_key *md5,
  1668. + const struct sock *sk,
  1669. + const struct request_sock *req,
  1670. + const struct sk_buff *skb);
  1671. +#endif
  1672. +};
  1673. +
  1674. +int tcpv4_offload_init(void);
  1675. +
  1676. +void tcp_v4_init(void);
  1677. +void tcp_init(void);
  1678. +
  1679. +#endif /* _TCP_H */
  1680. diff -Nur linux-3.15-rc7.orig/net/core/request_sock.c linux-3.15-rc7/net/core/request_sock.c
  1681. --- linux-3.15-rc7.orig/net/core/request_sock.c 2014-05-26 01:06:00.000000000 +0200
  1682. +++ linux-3.15-rc7/net/core/request_sock.c 2014-05-31 22:42:49.028605326 +0200
  1683. @@ -131,6 +131,7 @@
  1684. kfree(lopt);
  1685. }
  1686. +#ifdef CONFIG_TCP_FASTOPEN
  1687. /*
  1688. * This function is called to set a Fast Open socket's "fastopen_rsk" field
  1689. * to NULL when a TFO socket no longer needs to access the request_sock.
  1690. @@ -222,3 +223,4 @@
  1691. spin_unlock_bh(&fastopenq->lock);
  1692. sock_put(lsk);
  1693. }
  1694. +#endif
  1695. diff -Nur linux-3.15-rc7.orig/net/core/request_sock.c.orig linux-3.15-rc7/net/core/request_sock.c.orig
  1696. --- linux-3.15-rc7.orig/net/core/request_sock.c.orig 1970-01-01 01:00:00.000000000 +0100
  1697. +++ linux-3.15-rc7/net/core/request_sock.c.orig 2014-05-26 01:06:00.000000000 +0200
  1698. @@ -0,0 +1,224 @@
  1699. +/*
  1700. + * NET Generic infrastructure for Network protocols.
  1701. + *
  1702. + * Authors: Arnaldo Carvalho de Melo <acme@conectiva.com.br>
  1703. + *
  1704. + * From code originally in include/net/tcp.h
  1705. + *
  1706. + * This program is free software; you can redistribute it and/or
  1707. + * modify it under the terms of the GNU General Public License
  1708. + * as published by the Free Software Foundation; either version
  1709. + * 2 of the License, or (at your option) any later version.
  1710. + */
  1711. +
  1712. +#include <linux/module.h>
  1713. +#include <linux/random.h>
  1714. +#include <linux/slab.h>
  1715. +#include <linux/string.h>
  1716. +#include <linux/tcp.h>
  1717. +#include <linux/vmalloc.h>
  1718. +
  1719. +#include <net/request_sock.h>
  1720. +
  1721. +/*
  1722. + * Maximum number of SYN_RECV sockets in queue per LISTEN socket.
  1723. + * One SYN_RECV socket costs about 80bytes on a 32bit machine.
  1724. + * It would be better to replace it with a global counter for all sockets
  1725. + * but then some measure against one socket starving all other sockets
  1726. + * would be needed.
  1727. + *
  1728. + * The minimum value of it is 128. Experiments with real servers show that
  1729. + * it is absolutely not enough even at 100conn/sec. 256 cures most
  1730. + * of problems.
  1731. + * This value is adjusted to 128 for low memory machines,
  1732. + * and it will increase in proportion to the memory of machine.
  1733. + * Note : Dont forget somaxconn that may limit backlog too.
  1734. + */
  1735. +int sysctl_max_syn_backlog = 256;
  1736. +EXPORT_SYMBOL(sysctl_max_syn_backlog);
  1737. +
  1738. +int reqsk_queue_alloc(struct request_sock_queue *queue,
  1739. + unsigned int nr_table_entries)
  1740. +{
  1741. + size_t lopt_size = sizeof(struct listen_sock);
  1742. + struct listen_sock *lopt;
  1743. +
  1744. + nr_table_entries = min_t(u32, nr_table_entries, sysctl_max_syn_backlog);
  1745. + nr_table_entries = max_t(u32, nr_table_entries, 8);
  1746. + nr_table_entries = roundup_pow_of_two(nr_table_entries + 1);
  1747. + lopt_size += nr_table_entries * sizeof(struct request_sock *);
  1748. + if (lopt_size > PAGE_SIZE)
  1749. + lopt = vzalloc(lopt_size);
  1750. + else
  1751. + lopt = kzalloc(lopt_size, GFP_KERNEL);
  1752. + if (lopt == NULL)
  1753. + return -ENOMEM;
  1754. +
  1755. + for (lopt->max_qlen_log = 3;
  1756. + (1 << lopt->max_qlen_log) < nr_table_entries;
  1757. + lopt->max_qlen_log++);
  1758. +
  1759. + get_random_bytes(&lopt->hash_rnd, sizeof(lopt->hash_rnd));
  1760. + rwlock_init(&queue->syn_wait_lock);
  1761. + queue->rskq_accept_head = NULL;
  1762. + lopt->nr_table_entries = nr_table_entries;
  1763. +
  1764. + write_lock_bh(&queue->syn_wait_lock);
  1765. + queue->listen_opt = lopt;
  1766. + write_unlock_bh(&queue->syn_wait_lock);
  1767. +
  1768. + return 0;
  1769. +}
  1770. +
  1771. +void __reqsk_queue_destroy(struct request_sock_queue *queue)
  1772. +{
  1773. + struct listen_sock *lopt;
  1774. + size_t lopt_size;
  1775. +
  1776. + /*
  1777. + * this is an error recovery path only
  1778. + * no locking needed and the lopt is not NULL
  1779. + */
  1780. +
  1781. + lopt = queue->listen_opt;
  1782. + lopt_size = sizeof(struct listen_sock) +
  1783. + lopt->nr_table_entries * sizeof(struct request_sock *);
  1784. +
  1785. + if (lopt_size > PAGE_SIZE)
  1786. + vfree(lopt);
  1787. + else
  1788. + kfree(lopt);
  1789. +}
  1790. +
  1791. +static inline struct listen_sock *reqsk_queue_yank_listen_sk(
  1792. + struct request_sock_queue *queue)
  1793. +{
  1794. + struct listen_sock *lopt;
  1795. +
  1796. + write_lock_bh(&queue->syn_wait_lock);
  1797. + lopt = queue->listen_opt;
  1798. + queue->listen_opt = NULL;
  1799. + write_unlock_bh(&queue->syn_wait_lock);
  1800. +
  1801. + return lopt;
  1802. +}
  1803. +
  1804. +void reqsk_queue_destroy(struct request_sock_queue *queue)
  1805. +{
  1806. + /* make all the listen_opt local to us */
  1807. + struct listen_sock *lopt = reqsk_queue_yank_listen_sk(queue);
  1808. + size_t lopt_size = sizeof(struct listen_sock) +
  1809. + lopt->nr_table_entries * sizeof(struct request_sock *);
  1810. +
  1811. + if (lopt->qlen != 0) {
  1812. + unsigned int i;
  1813. +
  1814. + for (i = 0; i < lopt->nr_table_entries; i++) {
  1815. + struct request_sock *req;
  1816. +
  1817. + while ((req = lopt->syn_table[i]) != NULL) {
  1818. + lopt->syn_table[i] = req->dl_next;
  1819. + lopt->qlen--;
  1820. + reqsk_free(req);
  1821. + }
  1822. + }
  1823. + }
  1824. +
  1825. + WARN_ON(lopt->qlen != 0);
  1826. + if (lopt_size > PAGE_SIZE)
  1827. + vfree(lopt);
  1828. + else
  1829. + kfree(lopt);
  1830. +}
  1831. +
  1832. +/*
  1833. + * This function is called to set a Fast Open socket's "fastopen_rsk" field
  1834. + * to NULL when a TFO socket no longer needs to access the request_sock.
  1835. + * This happens only after 3WHS has been either completed or aborted (e.g.,
  1836. + * RST is received).
  1837. + *
  1838. + * Before TFO, a child socket is created only after 3WHS is completed,
  1839. + * hence it never needs to access the request_sock. things get a lot more
  1840. + * complex with TFO. A child socket, accepted or not, has to access its
  1841. + * request_sock for 3WHS processing, e.g., to retransmit SYN-ACK pkts,
  1842. + * until 3WHS is either completed or aborted. Afterwards the req will stay
  1843. + * until either the child socket is accepted, or in the rare case when the
  1844. + * listener is closed before the child is accepted.
  1845. + *
  1846. + * In short, a request socket is only freed after BOTH 3WHS has completed
  1847. + * (or aborted) and the child socket has been accepted (or listener closed).
  1848. + * When a child socket is accepted, its corresponding req->sk is set to
  1849. + * NULL since it's no longer needed. More importantly, "req->sk == NULL"
  1850. + * will be used by the code below to determine if a child socket has been
  1851. + * accepted or not, and the check is protected by the fastopenq->lock
  1852. + * described below.
  1853. + *
  1854. + * Note that fastopen_rsk is only accessed from the child socket's context
  1855. + * with its socket lock held. But a request_sock (req) can be accessed by
  1856. + * both its child socket through fastopen_rsk, and a listener socket through
  1857. + * icsk_accept_queue.rskq_accept_head. To protect the access a simple spin
  1858. + * lock per listener "icsk->icsk_accept_queue.fastopenq->lock" is created.
  1859. + * only in the rare case when both the listener and the child locks are held,
  1860. + * e.g., in inet_csk_listen_stop() do we not need to acquire the lock.
  1861. + * The lock also protects other fields such as fastopenq->qlen, which is
  1862. + * decremented by this function when fastopen_rsk is no longer needed.
  1863. + *
  1864. + * Note that another solution was to simply use the existing socket lock
  1865. + * from the listener. But first socket lock is difficult to use. It is not
  1866. + * a simple spin lock - one must consider sock_owned_by_user() and arrange
  1867. + * to use sk_add_backlog() stuff. But what really makes it infeasible is the
  1868. + * locking hierarchy violation. E.g., inet_csk_listen_stop() may try to
  1869. + * acquire a child's lock while holding listener's socket lock. A corner
  1870. + * case might also exist in tcp_v4_hnd_req() that will trigger this locking
  1871. + * order.
  1872. + *
  1873. + * When a TFO req is created, it needs to sock_hold its listener to prevent
  1874. + * the latter data structure from going away.
  1875. + *
  1876. + * This function also sets "treq->listener" to NULL and unreference listener
  1877. + * socket. treq->listener is used by the listener so it is protected by the
  1878. + * fastopenq->lock in this function.
  1879. + */
  1880. +void reqsk_fastopen_remove(struct sock *sk, struct request_sock *req,
  1881. + bool reset)
  1882. +{
  1883. + struct sock *lsk = tcp_rsk(req)->listener;
  1884. + struct fastopen_queue *fastopenq =
  1885. + inet_csk(lsk)->icsk_accept_queue.fastopenq;
  1886. +
  1887. + tcp_sk(sk)->fastopen_rsk = NULL;
  1888. + spin_lock_bh(&fastopenq->lock);
  1889. + fastopenq->qlen--;
  1890. + tcp_rsk(req)->listener = NULL;
  1891. + if (req->sk) /* the child socket hasn't been accepted yet */
  1892. + goto out;
  1893. +
  1894. + if (!reset || lsk->sk_state != TCP_LISTEN) {
  1895. + /* If the listener has been closed don't bother with the
  1896. + * special RST handling below.
  1897. + */
  1898. + spin_unlock_bh(&fastopenq->lock);
  1899. + sock_put(lsk);
  1900. + reqsk_free(req);
  1901. + return;
  1902. + }
  1903. + /* Wait for 60secs before removing a req that has triggered RST.
  1904. + * This is a simple defense against TFO spoofing attack - by
  1905. + * counting the req against fastopen.max_qlen, and disabling
  1906. + * TFO when the qlen exceeds max_qlen.
  1907. + *
  1908. + * For more details see CoNext'11 "TCP Fast Open" paper.
  1909. + */
  1910. + req->expires = jiffies + 60*HZ;
  1911. + if (fastopenq->rskq_rst_head == NULL)
  1912. + fastopenq->rskq_rst_head = req;
  1913. + else
  1914. + fastopenq->rskq_rst_tail->dl_next = req;
  1915. +
  1916. + req->dl_next = NULL;
  1917. + fastopenq->rskq_rst_tail = req;
  1918. + fastopenq->qlen++;
  1919. +out:
  1920. + spin_unlock_bh(&fastopenq->lock);
  1921. + sock_put(lsk);
  1922. +}
  1923. diff -Nur linux-3.15-rc7.orig/net/ipv4/Kconfig linux-3.15-rc7/net/ipv4/Kconfig
  1924. --- linux-3.15-rc7.orig/net/ipv4/Kconfig 2014-05-26 01:06:00.000000000 +0200
  1925. +++ linux-3.15-rc7/net/ipv4/Kconfig 2014-05-31 22:42:49.028605326 +0200
  1926. @@ -307,6 +307,10 @@
  1927. the notion of a secure tunnel for IPSEC and then use routing protocol
  1928. on top.
  1929. +config TCP_FASTOPEN
  1930. + bool "Enable TCP fastopen"
  1931. + default n
  1932. +
  1933. config INET_AH
  1934. tristate "IP: AH transformation"
  1935. select XFRM_ALGO
  1936. diff -Nur linux-3.15-rc7.orig/net/ipv4/Makefile linux-3.15-rc7/net/ipv4/Makefile
  1937. --- linux-3.15-rc7.orig/net/ipv4/Makefile 2014-05-26 01:06:00.000000000 +0200
  1938. +++ linux-3.15-rc7/net/ipv4/Makefile 2014-05-31 22:42:49.028605326 +0200
  1939. @@ -7,7 +7,7 @@
  1940. ip_output.o ip_sockglue.o inet_hashtables.o \
  1941. inet_timewait_sock.o inet_connection_sock.o \
  1942. tcp.o tcp_input.o tcp_output.o tcp_timer.o tcp_ipv4.o \
  1943. - tcp_minisocks.o tcp_cong.o tcp_metrics.o tcp_fastopen.o \
  1944. + tcp_minisocks.o tcp_cong.o tcp_metrics.o \
  1945. tcp_offload.o datagram.o raw.o udp.o udplite.o \
  1946. udp_offload.o arp.o icmp.o devinet.o af_inet.o igmp.o \
  1947. fib_frontend.o fib_semantics.o fib_trie.o \
  1948. @@ -51,6 +51,7 @@
  1949. obj-$(CONFIG_TCP_CONG_LP) += tcp_lp.o
  1950. obj-$(CONFIG_TCP_CONG_YEAH) += tcp_yeah.o
  1951. obj-$(CONFIG_TCP_CONG_ILLINOIS) += tcp_illinois.o
  1952. +obj-$(CONFIG_TCP_FASTOPEN) += tcp_fastopen.o
  1953. obj-$(CONFIG_MEMCG_KMEM) += tcp_memcontrol.o
  1954. obj-$(CONFIG_NETLABEL) += cipso_ipv4.o
  1955. diff -Nur linux-3.15-rc7.orig/net/ipv4/sysctl_net_ipv4.c linux-3.15-rc7/net/ipv4/sysctl_net_ipv4.c
  1956. --- linux-3.15-rc7.orig/net/ipv4/sysctl_net_ipv4.c 2014-05-26 01:06:00.000000000 +0200
  1957. +++ linux-3.15-rc7/net/ipv4/sysctl_net_ipv4.c 2014-05-31 22:42:49.028605326 +0200
  1958. @@ -200,6 +200,7 @@
  1959. return ret;
  1960. }
  1961. +#ifdef CONFIG_TCP_FASTOPEN
  1962. static int proc_tcp_fastopen_key(struct ctl_table *ctl, int write,
  1963. void __user *buffer, size_t *lenp,
  1964. loff_t *ppos)
  1965. @@ -246,6 +247,7 @@
  1966. kfree(tbl.data);
  1967. return ret;
  1968. }
  1969. +#endif
  1970. static struct ctl_table ipv4_table[] = {
  1971. {
  1972. @@ -388,6 +390,7 @@
  1973. .proc_handler = proc_dointvec
  1974. },
  1975. #endif
  1976. +#ifdef CONFIG_TCP_FASTOPEN
  1977. {
  1978. .procname = "tcp_fastopen",
  1979. .data = &sysctl_tcp_fastopen,
  1980. @@ -401,6 +404,7 @@
  1981. .maxlen = ((TCP_FASTOPEN_KEY_LENGTH * 2) + 10),
  1982. .proc_handler = proc_tcp_fastopen_key,
  1983. },
  1984. +#endif
  1985. {
  1986. .procname = "tcp_tw_recycle",
  1987. .data = &tcp_death_row.sysctl_tw_recycle,
  1988. diff -Nur linux-3.15-rc7.orig/net/ipv4/tcp.c linux-3.15-rc7/net/ipv4/tcp.c
  1989. --- linux-3.15-rc7.orig/net/ipv4/tcp.c 2014-05-26 01:06:00.000000000 +0200
  1990. +++ linux-3.15-rc7/net/ipv4/tcp.c 2014-05-31 22:42:49.028605326 +0200
  1991. @@ -1036,6 +1036,7 @@
  1992. return tmp;
  1993. }
  1994. +#ifdef CONFIG_TCP_FASTOPEN
  1995. void tcp_free_fastopen_req(struct tcp_sock *tp)
  1996. {
  1997. if (tp->fastopen_req != NULL) {
  1998. @@ -1069,6 +1070,7 @@
  1999. tcp_free_fastopen_req(tp);
  2000. return err;
  2001. }
  2002. +#endif
  2003. int tcp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
  2004. size_t size)
  2005. @@ -1084,6 +1086,7 @@
  2006. lock_sock(sk);
  2007. flags = msg->msg_flags;
  2008. +#ifdef CONFIG_TCP_FASTOPEN
  2009. if (flags & MSG_FASTOPEN) {
  2010. err = tcp_sendmsg_fastopen(sk, msg, &copied_syn, size);
  2011. if (err == -EINPROGRESS && copied_syn > 0)
  2012. @@ -1092,6 +1095,7 @@
  2013. goto out_err;
  2014. offset = copied_syn;
  2015. }
  2016. +#endif
  2017. timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT);
  2018. diff -Nur linux-3.15-rc7.orig/net/ipv4/tcp_ipv4.c linux-3.15-rc7/net/ipv4/tcp_ipv4.c
  2019. --- linux-3.15-rc7.orig/net/ipv4/tcp_ipv4.c 2014-05-26 01:06:00.000000000 +0200
  2020. +++ linux-3.15-rc7/net/ipv4/tcp_ipv4.c 2014-05-31 22:42:49.032605359 +0200
  2021. @@ -1260,6 +1260,7 @@
  2022. };
  2023. #endif
  2024. +#ifdef CONFIG_TCP_FASTOPEN
  2025. static bool tcp_fastopen_check(struct sock *sk, struct sk_buff *skb,
  2026. struct request_sock *req,
  2027. struct tcp_fastopen_cookie *foc,
  2028. @@ -1440,6 +1441,23 @@
  2029. WARN_ON(req->sk == NULL);
  2030. return 0;
  2031. }
  2032. +#else
  2033. +static bool tcp_fastopen_check(struct sock *sk, struct sk_buff *skb,
  2034. + struct request_sock *req,
  2035. + struct tcp_fastopen_cookie *foc,
  2036. + struct tcp_fastopen_cookie *valid_foc)
  2037. +{
  2038. + return false;
  2039. +}
  2040. +
  2041. +static int tcp_v4_conn_req_fastopen(struct sock *sk,
  2042. + struct sk_buff *skb,
  2043. + struct sk_buff *skb_synack,
  2044. + struct request_sock *req)
  2045. +{
  2046. + return 0;
  2047. +}
  2048. +#endif
  2049. int tcp_v4_conn_request(struct sock *sk, struct sk_buff *skb)
  2050. {
  2051. diff -Nur linux-3.15-rc7.orig/net/Kconfig linux-3.15-rc7/net/Kconfig
  2052. --- linux-3.15-rc7.orig/net/Kconfig 2014-05-26 01:06:00.000000000 +0200
  2053. +++ linux-3.15-rc7/net/Kconfig 2014-05-31 22:42:49.028605326 +0200
  2054. @@ -53,8 +53,8 @@
  2055. config INET
  2056. bool "TCP/IP networking"
  2057. - select CRYPTO
  2058. - select CRYPTO_AES
  2059. + select CRYPTO if TCP_FASTOPEN
  2060. + select CRYPTO_AES if TCP_FASTOPEN
  2061. ---help---
  2062. These are the protocols used on the Internet and on most local
  2063. Ethernets. It is highly recommended to say Y here (this will enlarge