tcp.c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852
  1. /**
  2. * @file
  3. * Transmission Control Protocol for IP
  4. *
  5. * This file contains common functions for the TCP implementation, such as functinos
  6. * for manipulating the data structures and the TCP timer functions. TCP functions
  7. * related to input and output is found in tcp_in.c and tcp_out.c respectively.
  8. *
  9. */
  10. /*
  11. * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or without modification,
  15. * are permitted provided that the following conditions are met:
  16. *
  17. * 1. Redistributions of source code must retain the above copyright notice,
  18. * this list of conditions and the following disclaimer.
  19. * 2. Redistributions in binary form must reproduce the above copyright notice,
  20. * this list of conditions and the following disclaimer in the documentation
  21. * and/or other materials provided with the distribution.
  22. * 3. The name of the author may not be used to endorse or promote products
  23. * derived from this software without specific prior written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  26. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  27. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  28. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  29. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  30. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  31. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  32. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  33. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  34. * OF SUCH DAMAGE.
  35. *
  36. * This file is part of the lwIP TCP/IP stack.
  37. *
  38. * Author: Adam Dunkels <adam@sics.se>
  39. *
  40. */
  41. #include "lwip/opt.h"
  42. #if LWIP_TCP /* don't build if not configured for use in lwipopts.h */
  43. #include "lwip/def.h"
  44. #include "lwip/mem.h"
  45. #include "lwip/memp.h"
  46. #include "lwip/snmp.h"
  47. #include "lwip/tcp.h"
  48. #include "lwip/tcp_impl.h"
  49. #include "lwip/debug.h"
  50. #include "lwip/stats.h"
  51. #include "lwip/ip6.h"
  52. #include "lwip/ip6_addr.h"
  53. #include "lwip/nd6.h"
  54. #include <string.h>
  55. #ifndef TCP_LOCAL_PORT_RANGE_START
  56. /* From http://www.iana.org/assignments/port-numbers:
  57. "The Dynamic and/or Private Ports are those from 49152 through 65535" */
  58. #define TCP_LOCAL_PORT_RANGE_START 0xc000
  59. #define TCP_LOCAL_PORT_RANGE_END 0xffff
  60. #define TCP_ENSURE_LOCAL_PORT_RANGE(port) (((port) & ~TCP_LOCAL_PORT_RANGE_START) + TCP_LOCAL_PORT_RANGE_START)
  61. #endif
  62. #if LWIP_TCP_KEEPALIVE
  63. #define TCP_KEEP_DUR(pcb) ((pcb)->keep_cnt * (pcb)->keep_intvl)
  64. #define TCP_KEEP_INTVL(pcb) ((pcb)->keep_intvl)
  65. #else /* LWIP_TCP_KEEPALIVE */
  66. #define TCP_KEEP_DUR(pcb) TCP_MAXIDLE
  67. #define TCP_KEEP_INTVL(pcb) TCP_KEEPINTVL_DEFAULT
  68. #endif /* LWIP_TCP_KEEPALIVE */
  69. const char * const tcp_state_str[] = {
  70. "CLOSED",
  71. "LISTEN",
  72. "SYN_SENT",
  73. "SYN_RCVD",
  74. "ESTABLISHED",
  75. "FIN_WAIT_1",
  76. "FIN_WAIT_2",
  77. "CLOSE_WAIT",
  78. "CLOSING",
  79. "LAST_ACK",
  80. "TIME_WAIT"
  81. };
  82. /* last local TCP port */
  83. static u16_t tcp_port = TCP_LOCAL_PORT_RANGE_START;
  84. /* Incremented every coarse grained timer shot (typically every 500 ms). */
  85. u32_t tcp_ticks;
  86. const u8_t tcp_backoff[13] =
  87. { 1, 2, 3, 4, 5, 6, 7, 7, 7, 7, 7, 7, 7};
  88. /* Times per slowtmr hits */
  89. const u8_t tcp_persist_backoff[7] = { 3, 6, 12, 24, 48, 96, 120 };
  90. /* The TCP PCB lists. */
  91. /** List of all TCP PCBs bound but not yet (connected || listening) */
  92. struct tcp_pcb *tcp_bound_pcbs;
  93. /** List of all TCP PCBs in LISTEN state */
  94. union tcp_listen_pcbs_t tcp_listen_pcbs;
  95. /** List of all TCP PCBs that are in a state in which
  96. * they accept or send data. */
  97. struct tcp_pcb *tcp_active_pcbs;
  98. /** List of all TCP PCBs in TIME-WAIT state */
  99. struct tcp_pcb *tcp_tw_pcbs;
  100. #define NUM_TCP_PCB_LISTS 4
  101. #define NUM_TCP_PCB_LISTS_NO_TIME_WAIT 3
  102. /** An array with all (non-temporary) PCB lists, mainly used for smaller code size */
  103. struct tcp_pcb ** const tcp_pcb_lists[] = {&tcp_listen_pcbs.pcbs, &tcp_bound_pcbs,
  104. &tcp_active_pcbs, &tcp_tw_pcbs};
  105. /** Only used for temporary storage. */
  106. struct tcp_pcb *tcp_tmp_pcb;
  107. u8_t tcp_active_pcbs_changed;
  108. /** Timer counter to handle calling slow-timer from tcp_tmr() */
  109. static u8_t tcp_timer;
  110. static u8_t tcp_timer_ctr;
  111. static u16_t tcp_new_port(void);
  112. /**
  113. * Initialize this module.
  114. */
  115. void
  116. tcp_init(void)
  117. {
  118. #if LWIP_RANDOMIZE_INITIAL_LOCAL_PORTS && defined(LWIP_RAND)
  119. tcp_port = TCP_ENSURE_LOCAL_PORT_RANGE(LWIP_RAND());
  120. #endif /* LWIP_RANDOMIZE_INITIAL_LOCAL_PORTS && defined(LWIP_RAND) */
  121. }
  122. /**
  123. * Called periodically to dispatch TCP timers.
  124. */
  125. void
  126. tcp_tmr(void)
  127. {
  128. /* Call tcp_fasttmr() every 250 ms */
  129. tcp_fasttmr();
  130. if (++tcp_timer & 1) {
  131. /* Call tcp_tmr() every 500 ms, i.e., every other timer
  132. tcp_tmr() is called. */
  133. tcp_slowtmr();
  134. }
  135. }
  136. /**
  137. * Closes the TX side of a connection held by the PCB.
  138. * For tcp_close(), a RST is sent if the application didn't receive all data
  139. * (tcp_recved() not called for all data passed to recv callback).
  140. *
  141. * Listening pcbs are freed and may not be referenced any more.
  142. * Connection pcbs are freed if not yet connected and may not be referenced
  143. * any more. If a connection is established (at least SYN received or in
  144. * a closing state), the connection is closed, and put in a closing state.
  145. * The pcb is then automatically freed in tcp_slowtmr(). It is therefore
  146. * unsafe to reference it.
  147. *
  148. * @param pcb the tcp_pcb to close
  149. * @return ERR_OK if connection has been closed
  150. * another err_t if closing failed and pcb is not freed
  151. */
  152. static err_t
  153. tcp_close_shutdown(struct tcp_pcb *pcb, u8_t rst_on_unacked_data)
  154. {
  155. err_t err;
  156. if (rst_on_unacked_data && ((pcb->state == ESTABLISHED) || (pcb->state == CLOSE_WAIT))) {
  157. if ((pcb->refused_data != NULL) || (pcb->rcv_wnd != TCP_WND)) {
  158. /* Not all data received by application, send RST to tell the remote
  159. side about this. */
  160. LWIP_ASSERT("pcb->flags & TF_RXCLOSED", pcb->flags & TF_RXCLOSED);
  161. /* don't call tcp_abort here: we must not deallocate the pcb since
  162. that might not be expected when calling tcp_close */
  163. tcp_rst(pcb->snd_nxt, pcb->rcv_nxt, &pcb->local_ip, &pcb->remote_ip,
  164. pcb->local_port, pcb->remote_port, PCB_ISIPV6(pcb));
  165. tcp_pcb_purge(pcb);
  166. TCP_RMV_ACTIVE(pcb);
  167. if (pcb->state == ESTABLISHED) {
  168. /* move to TIME_WAIT since we close actively */
  169. pcb->state = TIME_WAIT;
  170. TCP_REG(&tcp_tw_pcbs, pcb);
  171. } else {
  172. /* CLOSE_WAIT: deallocate the pcb since we already sent a RST for it */
  173. memp_free(MEMP_TCP_PCB, pcb);
  174. }
  175. return ERR_OK;
  176. }
  177. }
  178. switch (pcb->state) {
  179. case CLOSED:
  180. /* Closing a pcb in the CLOSED state might seem erroneous,
  181. * however, it is in this state once allocated and as yet unused
  182. * and the user needs some way to free it should the need arise.
  183. * Calling tcp_close() with a pcb that has already been closed, (i.e. twice)
  184. * or for a pcb that has been used and then entered the CLOSED state
  185. * is erroneous, but this should never happen as the pcb has in those cases
  186. * been freed, and so any remaining handles are bogus. */
  187. err = ERR_OK;
  188. if (pcb->local_port != 0 || pcb->bound_to_netif) {
  189. TCP_RMV(&tcp_bound_pcbs, pcb);
  190. }
  191. memp_free(MEMP_TCP_PCB, pcb);
  192. pcb = NULL;
  193. break;
  194. case LISTEN:
  195. err = ERR_OK;
  196. tcp_pcb_remove(&tcp_listen_pcbs.pcbs, pcb);
  197. memp_free(MEMP_TCP_PCB_LISTEN, pcb);
  198. pcb = NULL;
  199. break;
  200. case SYN_SENT:
  201. err = ERR_OK;
  202. TCP_PCB_REMOVE_ACTIVE(pcb);
  203. memp_free(MEMP_TCP_PCB, pcb);
  204. pcb = NULL;
  205. snmp_inc_tcpattemptfails();
  206. break;
  207. case SYN_RCVD:
  208. err = tcp_send_fin(pcb);
  209. if (err == ERR_OK) {
  210. snmp_inc_tcpattemptfails();
  211. pcb->state = FIN_WAIT_1;
  212. }
  213. break;
  214. case ESTABLISHED:
  215. err = tcp_send_fin(pcb);
  216. if (err == ERR_OK) {
  217. snmp_inc_tcpestabresets();
  218. pcb->state = FIN_WAIT_1;
  219. }
  220. break;
  221. case CLOSE_WAIT:
  222. err = tcp_send_fin(pcb);
  223. if (err == ERR_OK) {
  224. snmp_inc_tcpestabresets();
  225. pcb->state = LAST_ACK;
  226. }
  227. break;
  228. default:
  229. /* Has already been closed, do nothing. */
  230. err = ERR_OK;
  231. pcb = NULL;
  232. break;
  233. }
  234. if (pcb != NULL && err == ERR_OK) {
  235. /* To ensure all data has been sent when tcp_close returns, we have
  236. to make sure tcp_output doesn't fail.
  237. Since we don't really have to ensure all data has been sent when tcp_close
  238. returns (unsent data is sent from tcp timer functions, also), we don't care
  239. for the return value of tcp_output for now. */
  240. /* @todo: When implementing SO_LINGER, this must be changed somehow:
  241. If SOF_LINGER is set, the data should be sent and acked before close returns.
  242. This can only be valid for sequential APIs, not for the raw API. */
  243. tcp_output(pcb);
  244. }
  245. return err;
  246. }
  247. /**
  248. * Closes the connection held by the PCB.
  249. *
  250. * Listening pcbs are freed and may not be referenced any more.
  251. * Connection pcbs are freed if not yet connected and may not be referenced
  252. * any more. If a connection is established (at least SYN received or in
  253. * a closing state), the connection is closed, and put in a closing state.
  254. * The pcb is then automatically freed in tcp_slowtmr(). It is therefore
  255. * unsafe to reference it (unless an error is returned).
  256. *
  257. * @param pcb the tcp_pcb to close
  258. * @return ERR_OK if connection has been closed
  259. * another err_t if closing failed and pcb is not freed
  260. */
  261. err_t
  262. tcp_close(struct tcp_pcb *pcb)
  263. {
  264. #if TCP_DEBUG
  265. LWIP_DEBUGF(TCP_DEBUG, ("tcp_close: closing in "));
  266. tcp_debug_print_state(pcb->state);
  267. #endif /* TCP_DEBUG */
  268. if (pcb->state != LISTEN) {
  269. /* Set a flag not to receive any more data... */
  270. pcb->flags |= TF_RXCLOSED;
  271. }
  272. /* ... and close */
  273. return tcp_close_shutdown(pcb, 1);
  274. }
  275. /**
  276. * Causes all or part of a full-duplex connection of this PCB to be shut down.
  277. * This doesn't deallocate the PCB unless shutting down both sides!
  278. * Shutting down both sides is the same as calling tcp_close, so if it succeds,
  279. * the PCB should not be referenced any more.
  280. *
  281. * @param pcb PCB to shutdown
  282. * @param shut_rx shut down receive side if this is != 0
  283. * @param shut_tx shut down send side if this is != 0
  284. * @return ERR_OK if shutdown succeeded (or the PCB has already been shut down)
  285. * another err_t on error.
  286. */
  287. err_t
  288. tcp_shutdown(struct tcp_pcb *pcb, int shut_rx, int shut_tx)
  289. {
  290. if (pcb->state == LISTEN) {
  291. return ERR_CONN;
  292. }
  293. if (shut_rx) {
  294. /* shut down the receive side: set a flag not to receive any more data... */
  295. pcb->flags |= TF_RXCLOSED;
  296. if (shut_tx) {
  297. /* shutting down the tx AND rx side is the same as closing for the raw API */
  298. return tcp_close_shutdown(pcb, 1);
  299. }
  300. /* ... and free buffered data */
  301. if (pcb->refused_data != NULL) {
  302. pbuf_free(pcb->refused_data);
  303. pcb->refused_data = NULL;
  304. }
  305. }
  306. if (shut_tx) {
  307. /* This can't happen twice since if it succeeds, the pcb's state is changed.
  308. Only close in these states as the others directly deallocate the PCB */
  309. switch (pcb->state) {
  310. case SYN_RCVD:
  311. case ESTABLISHED:
  312. case CLOSE_WAIT:
  313. return tcp_close_shutdown(pcb, shut_rx);
  314. default:
  315. /* Not (yet?) connected, cannot shutdown the TX side as that would bring us
  316. into CLOSED state, where the PCB is deallocated. */
  317. return ERR_CONN;
  318. }
  319. }
  320. return ERR_OK;
  321. }
  322. /**
  323. * Abandons a connection and optionally sends a RST to the remote
  324. * host. Deletes the local protocol control block. This is done when
  325. * a connection is killed because of shortage of memory.
  326. *
  327. * @param pcb the tcp_pcb to abort
  328. * @param reset boolean to indicate whether a reset should be sent
  329. */
  330. void
  331. tcp_abandon(struct tcp_pcb *pcb, int reset)
  332. {
  333. u32_t seqno, ackno;
  334. #if LWIP_CALLBACK_API
  335. tcp_err_fn errf;
  336. #endif /* LWIP_CALLBACK_API */
  337. void *errf_arg;
  338. /* pcb->state LISTEN not allowed here */
  339. LWIP_ASSERT("don't call tcp_abort/tcp_abandon for listen-pcbs",
  340. pcb->state != LISTEN);
  341. /* Figure out on which TCP PCB list we are, and remove us. If we
  342. are in an active state, call the receive function associated with
  343. the PCB with a NULL argument, and send an RST to the remote end. */
  344. if (pcb->state == TIME_WAIT) {
  345. tcp_pcb_remove(&tcp_tw_pcbs, pcb);
  346. memp_free(MEMP_TCP_PCB, pcb);
  347. } else {
  348. int send_rst = reset && (pcb->state != CLOSED);
  349. seqno = pcb->snd_nxt;
  350. ackno = pcb->rcv_nxt;
  351. #if LWIP_CALLBACK_API
  352. errf = pcb->errf;
  353. #endif /* LWIP_CALLBACK_API */
  354. errf_arg = pcb->callback_arg;
  355. TCP_PCB_REMOVE_ACTIVE(pcb);
  356. if (pcb->unacked != NULL) {
  357. tcp_segs_free(pcb->unacked);
  358. }
  359. if (pcb->unsent != NULL) {
  360. tcp_segs_free(pcb->unsent);
  361. }
  362. #if TCP_QUEUE_OOSEQ
  363. if (pcb->ooseq != NULL) {
  364. tcp_segs_free(pcb->ooseq);
  365. }
  366. #endif /* TCP_QUEUE_OOSEQ */
  367. if (send_rst) {
  368. LWIP_DEBUGF(TCP_RST_DEBUG, ("tcp_abandon: sending RST\n"));
  369. tcp_rst(seqno, ackno, &pcb->local_ip, &pcb->remote_ip, pcb->local_port, pcb->remote_port, PCB_ISIPV6(pcb));
  370. }
  371. memp_free(MEMP_TCP_PCB, pcb);
  372. TCP_EVENT_ERR(errf, errf_arg, ERR_ABRT);
  373. }
  374. }
  375. /**
  376. * Aborts the connection by sending a RST (reset) segment to the remote
  377. * host. The pcb is deallocated. This function never fails.
  378. *
  379. * ATTENTION: When calling this from one of the TCP callbacks, make
  380. * sure you always return ERR_ABRT (and never return ERR_ABRT otherwise
  381. * or you will risk accessing deallocated memory or memory leaks!
  382. *
  383. * @param pcb the tcp pcb to abort
  384. */
  385. void
  386. tcp_abort(struct tcp_pcb *pcb)
  387. {
  388. tcp_abandon(pcb, 1);
  389. }
  390. /**
  391. * Binds the connection to a local portnumber and IP address. If the
  392. * IP address is not given (i.e., ipaddr == NULL), the IP address of
  393. * the outgoing network interface is used instead.
  394. *
  395. * @param pcb the tcp_pcb to bind (no check is done whether this pcb is
  396. * already bound!)
  397. * @param ipaddr the local ip address to bind to (use IP_ADDR_ANY to bind
  398. * to any local address
  399. * @param port the local port to bind to
  400. * @return ERR_USE if the port is already in use
  401. * ERR_VAL if bind failed because the PCB is not in a valid state
  402. * ERR_OK if bound
  403. */
  404. err_t
  405. tcp_bind(struct tcp_pcb *pcb, ip_addr_t *ipaddr, u16_t port)
  406. {
  407. int i;
  408. int max_pcb_list = NUM_TCP_PCB_LISTS;
  409. struct tcp_pcb *cpcb;
  410. LWIP_ERROR("tcp_bind: can only bind in state CLOSED", pcb->state == CLOSED, return ERR_VAL);
  411. #if SO_REUSE
  412. /* Unless the REUSEADDR flag is set,
  413. we have to check the pcbs in TIME-WAIT state, also.
  414. We do not dump TIME_WAIT pcb's; they can still be matched by incoming
  415. packets using both local and remote IP addresses and ports to distinguish.
  416. */
  417. if (ip_get_option(pcb, SOF_REUSEADDR)) {
  418. max_pcb_list = NUM_TCP_PCB_LISTS_NO_TIME_WAIT;
  419. }
  420. #endif /* SO_REUSE */
  421. if (port == 0) {
  422. port = tcp_new_port();
  423. if (port == 0) {
  424. return ERR_BUF;
  425. }
  426. }
  427. /* Check if the address already is in use (on all lists) */
  428. for (i = 0; i < max_pcb_list; i++) {
  429. for(cpcb = *tcp_pcb_lists[i]; cpcb != NULL; cpcb = cpcb->next) {
  430. if (cpcb->local_port == port) {
  431. #if SO_REUSE
  432. /* Omit checking for the same port if both pcbs have REUSEADDR set.
  433. For SO_REUSEADDR, the duplicate-check for a 5-tuple is done in
  434. tcp_connect. */
  435. if (!ip_get_option(pcb, SOF_REUSEADDR) ||
  436. !ip_get_option(cpcb, SOF_REUSEADDR))
  437. #endif /* SO_REUSE */
  438. {
  439. /* @todo: check accept_any_ip_version */
  440. if (IP_PCB_IPVER_EQ(pcb, cpcb) &&
  441. (ipX_addr_isany(PCB_ISIPV6(pcb), &cpcb->local_ip) ||
  442. ipX_addr_isany(PCB_ISIPV6(pcb), ip_2_ipX(ipaddr)) ||
  443. ipX_addr_cmp(PCB_ISIPV6(pcb), &cpcb->local_ip, ip_2_ipX(ipaddr)))) {
  444. return ERR_USE;
  445. }
  446. }
  447. }
  448. }
  449. }
  450. pcb->bound_to_netif = 0;
  451. if (!ipX_addr_isany(PCB_ISIPV6(pcb), ip_2_ipX(ipaddr))) {
  452. ipX_addr_set(PCB_ISIPV6(pcb), &pcb->local_ip, ip_2_ipX(ipaddr));
  453. }
  454. pcb->local_port = port;
  455. TCP_REG(&tcp_bound_pcbs, pcb);
  456. LWIP_DEBUGF(TCP_DEBUG, ("tcp_bind: bind to port %"U16_F"\n", port));
  457. return ERR_OK;
  458. }
  459. err_t
  460. tcp_bind_to_netif(struct tcp_pcb *pcb, const char ifname[3])
  461. {
  462. LWIP_ERROR("tcp_bind_if: can only bind in state CLOSED", pcb->state == CLOSED, return ERR_ISCONN);
  463. /* Check if the interface is already in use */
  464. for (int i = 0; i < NUM_TCP_PCB_LISTS; i++) {
  465. for(struct tcp_pcb *cpcb = *tcp_pcb_lists[i]; cpcb != NULL; cpcb = cpcb->next) {
  466. if (IP_PCB_IPVER_EQ(pcb, cpcb) &&
  467. cpcb->bound_to_netif &&
  468. !memcmp(cpcb->local_netif, ifname, sizeof(cpcb->local_netif))) {
  469. return ERR_USE;
  470. }
  471. }
  472. }
  473. pcb->bound_to_netif = 1;
  474. ipX_addr_set_any(PCB_ISIPV6(pcb), &pcb->local_ip);
  475. pcb->local_port = 0;
  476. memcpy(pcb->local_netif, ifname, sizeof(pcb->local_netif));
  477. TCP_REG(&tcp_bound_pcbs, pcb);
  478. LWIP_DEBUGF(TCP_DEBUG, ("tcp_bind_to_netif: bind to interface %c%c%c\n", ifname[0], ifname[1], ifname[2]));
  479. return ERR_OK;
  480. }
  481. #if LWIP_CALLBACK_API
  482. /**
  483. * Default accept callback if no accept callback is specified by the user.
  484. */
  485. static err_t
  486. tcp_accept_null(void *arg, struct tcp_pcb *pcb, err_t err)
  487. {
  488. LWIP_UNUSED_ARG(arg);
  489. LWIP_UNUSED_ARG(pcb);
  490. LWIP_UNUSED_ARG(err);
  491. return ERR_ABRT;
  492. }
  493. #endif /* LWIP_CALLBACK_API */
  494. /**
  495. * Set the state of the connection to be LISTEN, which means that it
  496. * is able to accept incoming connections. The protocol control block
  497. * is reallocated in order to consume less memory. Setting the
  498. * connection to LISTEN is an irreversible process.
  499. *
  500. * @param pcb the original tcp_pcb
  501. * @param backlog the incoming connections queue limit
  502. * @return tcp_pcb used for listening, consumes less memory.
  503. *
  504. * @note The original tcp_pcb is freed. This function therefore has to be
  505. * called like this:
  506. * tpcb = tcp_listen(tpcb);
  507. */
  508. struct tcp_pcb *
  509. tcp_listen_with_backlog(struct tcp_pcb *pcb, u8_t backlog)
  510. {
  511. struct tcp_pcb_listen *lpcb;
  512. LWIP_UNUSED_ARG(backlog);
  513. LWIP_ERROR("tcp_listen: pcb already connected", pcb->state == CLOSED, return NULL);
  514. /* already listening? */
  515. if (pcb->state == LISTEN) {
  516. return pcb;
  517. }
  518. #if SO_REUSE
  519. if (ip_get_option(pcb, SOF_REUSEADDR) && !pcb->have_local_netif) {
  520. /* Since SOF_REUSEADDR allows reusing a local address before the pcb's usage
  521. is declared (listen-/connection-pcb), we have to make sure now that
  522. this port is only used once for every local IP. */
  523. for(lpcb = tcp_listen_pcbs.listen_pcbs; lpcb != NULL; lpcb = lpcb->next) {
  524. if ((lpcb->local_port == pcb->local_port) &&
  525. IP_PCB_IPVER_EQ(pcb, lpcb)) {
  526. if (ipX_addr_cmp(PCB_ISIPV6(pcb), &lpcb->local_ip, &pcb->local_ip)) {
  527. /* this address/port is already used */
  528. return NULL;
  529. }
  530. }
  531. }
  532. }
  533. #endif /* SO_REUSE */
  534. lpcb = (struct tcp_pcb_listen *)memp_malloc(MEMP_TCP_PCB_LISTEN);
  535. if (lpcb == NULL) {
  536. return NULL;
  537. }
  538. lpcb->callback_arg = pcb->callback_arg;
  539. lpcb->bound_to_netif = pcb->bound_to_netif;
  540. lpcb->local_port = pcb->local_port;
  541. memcpy(lpcb->local_netif, pcb->local_netif, sizeof(pcb->local_netif));
  542. lpcb->state = LISTEN;
  543. lpcb->prio = pcb->prio;
  544. lpcb->so_options = pcb->so_options;
  545. ip_set_option(lpcb, SOF_ACCEPTCONN);
  546. lpcb->ttl = pcb->ttl;
  547. lpcb->tos = pcb->tos;
  548. #if LWIP_IPV6
  549. PCB_ISIPV6(lpcb) = PCB_ISIPV6(pcb);
  550. lpcb->accept_any_ip_version = 0;
  551. #endif /* LWIP_IPV6 */
  552. ipX_addr_copy(PCB_ISIPV6(pcb), lpcb->local_ip, pcb->local_ip);
  553. if (pcb->local_port != 0 || pcb->bound_to_netif) {
  554. TCP_RMV(&tcp_bound_pcbs, pcb);
  555. }
  556. memp_free(MEMP_TCP_PCB, pcb);
  557. #if LWIP_CALLBACK_API
  558. lpcb->accept = tcp_accept_null;
  559. #endif /* LWIP_CALLBACK_API */
  560. #if TCP_LISTEN_BACKLOG
  561. lpcb->accepts_pending = 0;
  562. lpcb->backlog = (backlog ? backlog : 1);
  563. #endif /* TCP_LISTEN_BACKLOG */
  564. TCP_REG(&tcp_listen_pcbs.pcbs, (struct tcp_pcb *)lpcb);
  565. return (struct tcp_pcb *)lpcb;
  566. }
  567. #if LWIP_IPV6
  568. /**
  569. * Same as tcp_listen_with_backlog, but allows to accept IPv4 and IPv6
  570. * connections, if the pcb's local address is set to ANY.
  571. */
  572. struct tcp_pcb *
  573. tcp_listen_dual_with_backlog(struct tcp_pcb *pcb, u8_t backlog)
  574. {
  575. struct tcp_pcb *lpcb;
  576. lpcb = tcp_listen_with_backlog(pcb, backlog);
  577. if ((lpcb != NULL) &&
  578. ipX_addr_isany(PCB_ISIPV6(pcb), &pcb->local_ip)) {
  579. /* The default behavior is to accept connections on either
  580. * IPv4 or IPv6, if not bound. */
  581. /* @see NETCONN_FLAG_IPV6_V6ONLY for changing this behavior */
  582. ((struct tcp_pcb_listen*)lpcb)->accept_any_ip_version = 1;
  583. }
  584. return lpcb;
  585. }
  586. #endif /* LWIP_IPV6 */
  587. /**
  588. * Update the state that tracks the available window space to advertise.
  589. *
  590. * Returns how much extra window would be advertised if we sent an
  591. * update now.
  592. */
  593. u32_t tcp_update_rcv_ann_wnd(struct tcp_pcb *pcb)
  594. {
  595. u32_t new_right_edge = pcb->rcv_nxt + pcb->rcv_wnd;
  596. if (TCP_SEQ_GEQ(new_right_edge, pcb->rcv_ann_right_edge + LWIP_MIN((TCP_WND / 2), pcb->mss))) {
  597. /* we can advertise more window */
  598. pcb->rcv_ann_wnd = pcb->rcv_wnd;
  599. return new_right_edge - pcb->rcv_ann_right_edge;
  600. } else {
  601. if (TCP_SEQ_GT(pcb->rcv_nxt, pcb->rcv_ann_right_edge)) {
  602. /* Can happen due to other end sending out of advertised window,
  603. * but within actual available (but not yet advertised) window */
  604. pcb->rcv_ann_wnd = 0;
  605. } else {
  606. /* keep the right edge of window constant */
  607. u32_t new_rcv_ann_wnd = pcb->rcv_ann_right_edge - pcb->rcv_nxt;
  608. LWIP_ASSERT("new_rcv_ann_wnd <= 0xffff", new_rcv_ann_wnd <= 0xffff);
  609. pcb->rcv_ann_wnd = (u16_t)new_rcv_ann_wnd;
  610. }
  611. return 0;
  612. }
  613. }
  614. /**
  615. * This function should be called by the application when it has
  616. * processed the data. The purpose is to advertise a larger window
  617. * when the data has been processed.
  618. *
  619. * @param pcb the tcp_pcb for which data is read
  620. * @param len the amount of bytes that have been read by the application
  621. */
  622. void
  623. tcp_recved(struct tcp_pcb *pcb, u16_t len)
  624. {
  625. int wnd_inflation;
  626. /* pcb->state LISTEN not allowed here */
  627. LWIP_ASSERT("don't call tcp_recved for listen-pcbs",
  628. pcb->state != LISTEN);
  629. LWIP_ASSERT("tcp_recved: len would wrap rcv_wnd\n",
  630. len <= 0xffff - pcb->rcv_wnd );
  631. pcb->rcv_wnd += len;
  632. if (pcb->rcv_wnd > TCP_WND) {
  633. pcb->rcv_wnd = TCP_WND;
  634. }
  635. wnd_inflation = tcp_update_rcv_ann_wnd(pcb);
  636. /* If the change in the right edge of window is significant (default
  637. * watermark is TCP_WND/4), then send an explicit update now.
  638. * Otherwise wait for a packet to be sent in the normal course of
  639. * events (or more window to be available later) */
  640. if (wnd_inflation >= TCP_WND_UPDATE_THRESHOLD) {
  641. tcp_ack_now(pcb);
  642. tcp_output(pcb);
  643. }
  644. LWIP_DEBUGF(TCP_DEBUG, ("tcp_recved: recveived %"U16_F" bytes, wnd %"U16_F" (%"U16_F").\n",
  645. len, pcb->rcv_wnd, TCP_WND - pcb->rcv_wnd));
  646. }
  647. /**
  648. * Allocate a new local TCP port.
  649. *
  650. * @return a new (free) local TCP port number
  651. */
  652. static u16_t
  653. tcp_new_port(void)
  654. {
  655. u8_t i;
  656. u16_t n = 0;
  657. struct tcp_pcb *pcb;
  658. again:
  659. if (tcp_port++ == TCP_LOCAL_PORT_RANGE_END) {
  660. tcp_port = TCP_LOCAL_PORT_RANGE_START;
  661. }
  662. /* Check all PCB lists. */
  663. for (i = 0; i < NUM_TCP_PCB_LISTS; i++) {
  664. for(pcb = *tcp_pcb_lists[i]; pcb != NULL; pcb = pcb->next) {
  665. if (pcb->local_port == tcp_port) {
  666. if (++n > (TCP_LOCAL_PORT_RANGE_END - TCP_LOCAL_PORT_RANGE_START)) {
  667. return 0;
  668. }
  669. goto again;
  670. }
  671. }
  672. }
  673. return tcp_port;
  674. }
  675. /**
  676. * Connects to another host. The function given as the "connected"
  677. * argument will be called when the connection has been established.
  678. *
  679. * @param pcb the tcp_pcb used to establish the connection
  680. * @param ipaddr the remote ip address to connect to
  681. * @param port the remote tcp port to connect to
  682. * @param connected callback function to call when connected (or on error)
  683. * @return ERR_VAL if invalid arguments are given
  684. * ERR_OK if connect request has been sent
  685. * other err_t values if connect request couldn't be sent
  686. */
  687. err_t
  688. tcp_connect(struct tcp_pcb *pcb, ip_addr_t *ipaddr, u16_t port,
  689. tcp_connected_fn connected)
  690. {
  691. err_t ret;
  692. u32_t iss;
  693. u16_t old_local_port;
  694. LWIP_ERROR("tcp_connect: can only connect from state CLOSED", pcb->state == CLOSED, return ERR_ISCONN);
  695. LWIP_ERROR("tcp_connect: cannot connect pcb bound to netif", !pcb->bound_to_netif, return ERR_VAL);
  696. LWIP_DEBUGF(TCP_DEBUG, ("tcp_connect to port %"U16_F"\n", port));
  697. if (ipaddr != NULL) {
  698. ipX_addr_set(PCB_ISIPV6(pcb), &pcb->remote_ip, ip_2_ipX(ipaddr));
  699. } else {
  700. return ERR_VAL;
  701. }
  702. pcb->remote_port = port;
  703. /* check if we have a route to the remote host */
  704. if (ipX_addr_isany(PCB_ISIPV6(pcb), &pcb->local_ip)) {
  705. /* no local IP address set, yet. */
  706. struct netif *netif;
  707. ipX_addr_t *local_ip;
  708. ipX_route_get_local_ipX(PCB_ISIPV6(pcb), &pcb->local_ip, &pcb->remote_ip, netif, local_ip);
  709. if ((netif == NULL) || (local_ip == NULL)) {
  710. /* Don't even try to send a SYN packet if we have no route
  711. since that will fail. */
  712. return ERR_RTE;
  713. }
  714. /* Use the address as local address of the pcb. */
  715. ipX_addr_copy(PCB_ISIPV6(pcb), pcb->local_ip, *local_ip);
  716. }
  717. old_local_port = pcb->local_port;
  718. if (pcb->local_port == 0) {
  719. pcb->local_port = tcp_new_port();
  720. if (pcb->local_port == 0) {
  721. return ERR_BUF;
  722. }
  723. }
  724. #if SO_REUSE
  725. if (ip_get_option(pcb, SOF_REUSEADDR)) {
  726. /* Since SOF_REUSEADDR allows reusing a local address, we have to make sure
  727. now that the 5-tuple is unique. */
  728. struct tcp_pcb *cpcb;
  729. int i;
  730. /* Don't check listen- and bound-PCBs, check active- and TIME-WAIT PCBs. */
  731. for (i = 2; i < NUM_TCP_PCB_LISTS; i++) {
  732. for(cpcb = *tcp_pcb_lists[i]; cpcb != NULL; cpcb = cpcb->next) {
  733. if ((cpcb->local_port == pcb->local_port) &&
  734. (cpcb->remote_port == port) &&
  735. IP_PCB_IPVER_EQ(cpcb, pcb) &&
  736. ipX_addr_cmp(PCB_ISIPV6(pcb), &cpcb->local_ip, &pcb->local_ip) &&
  737. ipX_addr_cmp(PCB_ISIPV6(pcb), &cpcb->remote_ip, ip_2_ipX(ipaddr))) {
  738. /* linux returns EISCONN here, but ERR_USE should be OK for us */
  739. return ERR_USE;
  740. }
  741. }
  742. }
  743. }
  744. #endif /* SO_REUSE */
  745. iss = tcp_next_iss();
  746. pcb->rcv_nxt = 0;
  747. pcb->snd_nxt = iss;
  748. pcb->lastack = iss - 1;
  749. pcb->snd_lbb = iss - 1;
  750. pcb->rcv_wnd = TCP_WND;
  751. pcb->rcv_ann_wnd = TCP_WND;
  752. pcb->rcv_ann_right_edge = pcb->rcv_nxt;
  753. pcb->snd_wnd = TCP_WND;
  754. /* As initial send MSS, we use TCP_MSS but limit it to 536.
  755. The send MSS is updated when an MSS option is received. */
  756. pcb->mss = (TCP_MSS > 536) ? 536 : TCP_MSS;
  757. #if TCP_CALCULATE_EFF_SEND_MSS
  758. pcb->mss = tcp_eff_send_mss(pcb->mss, &pcb->local_ip, &pcb->remote_ip, PCB_ISIPV6(pcb));
  759. #endif /* TCP_CALCULATE_EFF_SEND_MSS */
  760. pcb->cwnd = 1;
  761. pcb->ssthresh = pcb->mss * 10;
  762. #if LWIP_CALLBACK_API
  763. pcb->connected = connected;
  764. #else /* LWIP_CALLBACK_API */
  765. LWIP_UNUSED_ARG(connected);
  766. #endif /* LWIP_CALLBACK_API */
  767. /* Send a SYN together with the MSS option. */
  768. ret = tcp_enqueue_flags(pcb, TCP_SYN);
  769. if (ret == ERR_OK) {
  770. /* SYN segment was enqueued, changed the pcbs state now */
  771. pcb->state = SYN_SENT;
  772. if (old_local_port != 0) {
  773. TCP_RMV(&tcp_bound_pcbs, pcb);
  774. }
  775. TCP_REG_ACTIVE(pcb);
  776. snmp_inc_tcpactiveopens();
  777. tcp_output(pcb);
  778. }
  779. return ret;
  780. }
  781. /**
  782. * Called every 500 ms and implements the retransmission timer and the timer that
  783. * removes PCBs that have been in TIME-WAIT for enough time. It also increments
  784. * various timers such as the inactivity timer in each PCB.
  785. *
  786. * Automatically called from tcp_tmr().
  787. */
  788. void
  789. tcp_slowtmr(void)
  790. {
  791. struct tcp_pcb *pcb, *prev;
  792. u16_t eff_wnd;
  793. u8_t pcb_remove; /* flag if a PCB should be removed */
  794. u8_t pcb_reset; /* flag if a RST should be sent when removing */
  795. err_t err;
  796. err = ERR_OK;
  797. ++tcp_ticks;
  798. ++tcp_timer_ctr;
  799. tcp_slowtmr_start:
  800. /* Steps through all of the active PCBs. */
  801. prev = NULL;
  802. pcb = tcp_active_pcbs;
  803. if (pcb == NULL) {
  804. LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: no active pcbs\n"));
  805. }
  806. while (pcb != NULL) {
  807. LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: processing active pcb\n"));
  808. LWIP_ASSERT("tcp_slowtmr: active pcb->state != CLOSED\n", pcb->state != CLOSED);
  809. LWIP_ASSERT("tcp_slowtmr: active pcb->state != LISTEN\n", pcb->state != LISTEN);
  810. LWIP_ASSERT("tcp_slowtmr: active pcb->state != TIME-WAIT\n", pcb->state != TIME_WAIT);
  811. if (pcb->last_timer == tcp_timer_ctr) {
  812. /* skip this pcb, we have already processed it */
  813. pcb = pcb->next;
  814. continue;
  815. }
  816. pcb->last_timer = tcp_timer_ctr;
  817. pcb_remove = 0;
  818. pcb_reset = 0;
  819. if (pcb->state == SYN_SENT && pcb->nrtx == TCP_SYNMAXRTX) {
  820. ++pcb_remove;
  821. LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: max SYN retries reached\n"));
  822. }
  823. else if (pcb->nrtx == TCP_MAXRTX) {
  824. ++pcb_remove;
  825. LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: max DATA retries reached\n"));
  826. } else {
  827. if (pcb->persist_backoff > 0) {
  828. /* If snd_wnd is zero, use persist timer to send 1 byte probes
  829. * instead of using the standard retransmission mechanism. */
  830. pcb->persist_cnt++;
  831. if (pcb->persist_cnt >= tcp_persist_backoff[pcb->persist_backoff-1]) {
  832. pcb->persist_cnt = 0;
  833. if (pcb->persist_backoff < sizeof(tcp_persist_backoff)) {
  834. pcb->persist_backoff++;
  835. }
  836. tcp_zero_window_probe(pcb);
  837. }
  838. } else {
  839. /* Increase the retransmission timer if it is running */
  840. if(pcb->rtime >= 0) {
  841. ++pcb->rtime;
  842. }
  843. if (pcb->unacked != NULL && pcb->rtime >= pcb->rto) {
  844. /* Time for a retransmission. */
  845. LWIP_DEBUGF(TCP_RTO_DEBUG, ("tcp_slowtmr: rtime %"S16_F
  846. " pcb->rto %"S16_F"\n",
  847. pcb->rtime, pcb->rto));
  848. /* Double retransmission time-out unless we are trying to
  849. * connect to somebody (i.e., we are in SYN_SENT). */
  850. if (pcb->state != SYN_SENT) {
  851. pcb->rto = ((pcb->sa >> 3) + pcb->sv) << tcp_backoff[pcb->nrtx];
  852. }
  853. /* Reset the retransmission timer. */
  854. pcb->rtime = 0;
  855. /* Reduce congestion window and ssthresh. */
  856. eff_wnd = LWIP_MIN(pcb->cwnd, pcb->snd_wnd);
  857. pcb->ssthresh = eff_wnd >> 1;
  858. if (pcb->ssthresh < (pcb->mss << 1)) {
  859. pcb->ssthresh = (pcb->mss << 1);
  860. }
  861. pcb->cwnd = pcb->mss;
  862. LWIP_DEBUGF(TCP_CWND_DEBUG, ("tcp_slowtmr: cwnd %"U16_F
  863. " ssthresh %"U16_F"\n",
  864. pcb->cwnd, pcb->ssthresh));
  865. /* The following needs to be called AFTER cwnd is set to one
  866. mss - STJ */
  867. tcp_rexmit_rto(pcb);
  868. }
  869. }
  870. }
  871. /* Check if this PCB has stayed too long in FIN-WAIT-2 */
  872. if (pcb->state == FIN_WAIT_2) {
  873. /* If this PCB is in FIN_WAIT_2 because of SHUT_WR don't let it time out. */
  874. if (pcb->flags & TF_RXCLOSED) {
  875. /* PCB was fully closed (either through close() or SHUT_RDWR):
  876. normal FIN-WAIT timeout handling. */
  877. if ((u32_t)(tcp_ticks - pcb->tmr) >
  878. TCP_FIN_WAIT_TIMEOUT / TCP_SLOW_INTERVAL) {
  879. ++pcb_remove;
  880. LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: removing pcb stuck in FIN-WAIT-2\n"));
  881. }
  882. }
  883. }
  884. /* Check if KEEPALIVE should be sent */
  885. if(ip_get_option(pcb, SOF_KEEPALIVE) &&
  886. ((pcb->state == ESTABLISHED) ||
  887. (pcb->state == CLOSE_WAIT))) {
  888. if((u32_t)(tcp_ticks - pcb->tmr) >
  889. (pcb->keep_idle + TCP_KEEP_DUR(pcb)) / TCP_SLOW_INTERVAL)
  890. {
  891. LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: KEEPALIVE timeout. Aborting connection to "));
  892. ipX_addr_debug_print(PCB_ISIPV6(pcb), TCP_DEBUG, &pcb->remote_ip);
  893. LWIP_DEBUGF(TCP_DEBUG, ("\n"));
  894. ++pcb_remove;
  895. ++pcb_reset;
  896. }
  897. else if((u32_t)(tcp_ticks - pcb->tmr) >
  898. (pcb->keep_idle + pcb->keep_cnt_sent * TCP_KEEP_INTVL(pcb))
  899. / TCP_SLOW_INTERVAL)
  900. {
  901. tcp_keepalive(pcb);
  902. pcb->keep_cnt_sent++;
  903. }
  904. }
  905. /* If this PCB has queued out of sequence data, but has been
  906. inactive for too long, will drop the data (it will eventually
  907. be retransmitted). */
  908. #if TCP_QUEUE_OOSEQ
  909. if (pcb->ooseq != NULL &&
  910. (u32_t)tcp_ticks - pcb->tmr >= pcb->rto * TCP_OOSEQ_TIMEOUT) {
  911. tcp_segs_free(pcb->ooseq);
  912. pcb->ooseq = NULL;
  913. LWIP_DEBUGF(TCP_CWND_DEBUG, ("tcp_slowtmr: dropping OOSEQ queued data\n"));
  914. }
  915. #endif /* TCP_QUEUE_OOSEQ */
  916. /* Check if this PCB has stayed too long in SYN-RCVD */
  917. if (pcb->state == SYN_RCVD) {
  918. if ((u32_t)(tcp_ticks - pcb->tmr) >
  919. TCP_SYN_RCVD_TIMEOUT / TCP_SLOW_INTERVAL) {
  920. ++pcb_remove;
  921. LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: removing pcb stuck in SYN-RCVD\n"));
  922. }
  923. }
  924. /* Check if this PCB has stayed too long in LAST-ACK */
  925. if (pcb->state == LAST_ACK) {
  926. if ((u32_t)(tcp_ticks - pcb->tmr) > 2 * TCP_MSL / TCP_SLOW_INTERVAL) {
  927. ++pcb_remove;
  928. LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: removing pcb stuck in LAST-ACK\n"));
  929. }
  930. }
  931. /* If the PCB should be removed, do it. */
  932. if (pcb_remove) {
  933. struct tcp_pcb *pcb2;
  934. tcp_err_fn err_fn;
  935. void *err_arg;
  936. tcp_pcb_purge(pcb);
  937. /* Remove PCB from tcp_active_pcbs list. */
  938. if (prev != NULL) {
  939. LWIP_ASSERT("tcp_slowtmr: middle tcp != tcp_active_pcbs", pcb != tcp_active_pcbs);
  940. prev->next = pcb->next;
  941. } else {
  942. /* This PCB was the first. */
  943. LWIP_ASSERT("tcp_slowtmr: first pcb == tcp_active_pcbs", tcp_active_pcbs == pcb);
  944. tcp_active_pcbs = pcb->next;
  945. }
  946. if (pcb_reset) {
  947. tcp_rst(pcb->snd_nxt, pcb->rcv_nxt, &pcb->local_ip, &pcb->remote_ip,
  948. pcb->local_port, pcb->remote_port, PCB_ISIPV6(pcb));
  949. }
  950. err_fn = pcb->errf;
  951. err_arg = pcb->callback_arg;
  952. pcb2 = pcb;
  953. pcb = pcb->next;
  954. memp_free(MEMP_TCP_PCB, pcb2);
  955. tcp_active_pcbs_changed = 0;
  956. TCP_EVENT_ERR(err_fn, err_arg, ERR_ABRT);
  957. if (tcp_active_pcbs_changed) {
  958. goto tcp_slowtmr_start;
  959. }
  960. } else {
  961. /* get the 'next' element now and work with 'prev' below (in case of abort) */
  962. prev = pcb;
  963. pcb = pcb->next;
  964. /* We check if we should poll the connection. */
  965. ++prev->polltmr;
  966. if (prev->polltmr >= prev->pollinterval) {
  967. prev->polltmr = 0;
  968. LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: polling application\n"));
  969. tcp_active_pcbs_changed = 0;
  970. TCP_EVENT_POLL(prev, err);
  971. if (tcp_active_pcbs_changed) {
  972. goto tcp_slowtmr_start;
  973. }
  974. /* if err == ERR_ABRT, 'prev' is already deallocated */
  975. if (err == ERR_OK) {
  976. tcp_output(prev);
  977. }
  978. }
  979. }
  980. }
  981. /* Steps through all of the TIME-WAIT PCBs. */
  982. prev = NULL;
  983. pcb = tcp_tw_pcbs;
  984. while (pcb != NULL) {
  985. LWIP_ASSERT("tcp_slowtmr: TIME-WAIT pcb->state == TIME-WAIT", pcb->state == TIME_WAIT);
  986. pcb_remove = 0;
  987. /* Check if this PCB has stayed long enough in TIME-WAIT */
  988. if ((u32_t)(tcp_ticks - pcb->tmr) > 2 * TCP_MSL / TCP_SLOW_INTERVAL) {
  989. ++pcb_remove;
  990. }
  991. /* If the PCB should be removed, do it. */
  992. if (pcb_remove) {
  993. struct tcp_pcb *pcb2;
  994. tcp_pcb_purge(pcb);
  995. /* Remove PCB from tcp_tw_pcbs list. */
  996. if (prev != NULL) {
  997. LWIP_ASSERT("tcp_slowtmr: middle tcp != tcp_tw_pcbs", pcb != tcp_tw_pcbs);
  998. prev->next = pcb->next;
  999. } else {
  1000. /* This PCB was the first. */
  1001. LWIP_ASSERT("tcp_slowtmr: first pcb == tcp_tw_pcbs", tcp_tw_pcbs == pcb);
  1002. tcp_tw_pcbs = pcb->next;
  1003. }
  1004. pcb2 = pcb;
  1005. pcb = pcb->next;
  1006. memp_free(MEMP_TCP_PCB, pcb2);
  1007. } else {
  1008. prev = pcb;
  1009. pcb = pcb->next;
  1010. }
  1011. }
  1012. }
  1013. /**
  1014. * Is called every TCP_FAST_INTERVAL (250 ms) and process data previously
  1015. * "refused" by upper layer (application) and sends delayed ACKs.
  1016. *
  1017. * Automatically called from tcp_tmr().
  1018. */
  1019. void
  1020. tcp_fasttmr(void)
  1021. {
  1022. struct tcp_pcb *pcb;
  1023. ++tcp_timer_ctr;
  1024. tcp_fasttmr_start:
  1025. pcb = tcp_active_pcbs;
  1026. while(pcb != NULL) {
  1027. if (pcb->last_timer != tcp_timer_ctr) {
  1028. struct tcp_pcb *next;
  1029. pcb->last_timer = tcp_timer_ctr;
  1030. /* send delayed ACKs */
  1031. if (pcb->flags & TF_ACK_DELAY) {
  1032. LWIP_DEBUGF(TCP_DEBUG, ("tcp_fasttmr: delayed ACK\n"));
  1033. tcp_ack_now(pcb);
  1034. tcp_output(pcb);
  1035. pcb->flags &= ~(TF_ACK_DELAY | TF_ACK_NOW);
  1036. }
  1037. next = pcb->next;
  1038. /* If there is data which was previously "refused" by upper layer */
  1039. if (pcb->refused_data != NULL) {
  1040. tcp_active_pcbs_changed = 0;
  1041. tcp_process_refused_data(pcb);
  1042. if (tcp_active_pcbs_changed) {
  1043. /* application callback has changed the pcb list: restart the loop */
  1044. goto tcp_fasttmr_start;
  1045. }
  1046. }
  1047. pcb = next;
  1048. }
  1049. }
  1050. }
  1051. /** Pass pcb->refused_data to the recv callback */
  1052. err_t
  1053. tcp_process_refused_data(struct tcp_pcb *pcb)
  1054. {
  1055. err_t err;
  1056. u8_t refused_flags = pcb->refused_data->flags;
  1057. /* set pcb->refused_data to NULL in case the callback frees it and then
  1058. closes the pcb */
  1059. struct pbuf *refused_data = pcb->refused_data;
  1060. pcb->refused_data = NULL;
  1061. /* Notify again application with data previously received. */
  1062. LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_input: notify kept packet\n"));
  1063. TCP_EVENT_RECV(pcb, refused_data, ERR_OK, err);
  1064. if (err == ERR_OK) {
  1065. /* did refused_data include a FIN? */
  1066. if (refused_flags & PBUF_FLAG_TCP_FIN) {
  1067. /* correct rcv_wnd as the application won't call tcp_recved()
  1068. for the FIN's seqno */
  1069. if (pcb->rcv_wnd != TCP_WND) {
  1070. pcb->rcv_wnd++;
  1071. }
  1072. TCP_EVENT_CLOSED(pcb, err);
  1073. if (err == ERR_ABRT) {
  1074. return ERR_ABRT;
  1075. }
  1076. }
  1077. } else if (err == ERR_ABRT) {
  1078. /* if err == ERR_ABRT, 'pcb' is already deallocated */
  1079. /* Drop incoming packets because pcb is "full" (only if the incoming
  1080. segment contains data). */
  1081. LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_input: drop incoming packets, because pcb is \"full\"\n"));
  1082. return ERR_ABRT;
  1083. } else {
  1084. /* data is still refused, pbuf is still valid (go on for ACK-only packets) */
  1085. pcb->refused_data = refused_data;
  1086. }
  1087. return ERR_OK;
  1088. }
  1089. /**
  1090. * Deallocates a list of TCP segments (tcp_seg structures).
  1091. *
  1092. * @param seg tcp_seg list of TCP segments to free
  1093. */
  1094. void
  1095. tcp_segs_free(struct tcp_seg *seg)
  1096. {
  1097. while (seg != NULL) {
  1098. struct tcp_seg *next = seg->next;
  1099. tcp_seg_free(seg);
  1100. seg = next;
  1101. }
  1102. }
  1103. /**
  1104. * Frees a TCP segment (tcp_seg structure).
  1105. *
  1106. * @param seg single tcp_seg to free
  1107. */
  1108. void
  1109. tcp_seg_free(struct tcp_seg *seg)
  1110. {
  1111. if (seg != NULL) {
  1112. if (seg->p != NULL) {
  1113. pbuf_free(seg->p);
  1114. #if TCP_DEBUG
  1115. seg->p = NULL;
  1116. #endif /* TCP_DEBUG */
  1117. }
  1118. memp_free(MEMP_TCP_SEG, seg);
  1119. }
  1120. }
  1121. /**
  1122. * Sets the priority of a connection.
  1123. *
  1124. * @param pcb the tcp_pcb to manipulate
  1125. * @param prio new priority
  1126. */
  1127. void
  1128. tcp_setprio(struct tcp_pcb *pcb, u8_t prio)
  1129. {
  1130. pcb->prio = prio;
  1131. }
  1132. #if TCP_QUEUE_OOSEQ
  1133. /**
  1134. * Returns a copy of the given TCP segment.
  1135. * The pbuf and data are not copied, only the pointers
  1136. *
  1137. * @param seg the old tcp_seg
  1138. * @return a copy of seg
  1139. */
  1140. struct tcp_seg *
  1141. tcp_seg_copy(struct tcp_seg *seg)
  1142. {
  1143. struct tcp_seg *cseg;
  1144. cseg = (struct tcp_seg *)memp_malloc(MEMP_TCP_SEG);
  1145. if (cseg == NULL) {
  1146. return NULL;
  1147. }
  1148. SMEMCPY((u8_t *)cseg, (const u8_t *)seg, sizeof(struct tcp_seg));
  1149. pbuf_ref(cseg->p);
  1150. return cseg;
  1151. }
  1152. #endif /* TCP_QUEUE_OOSEQ */
  1153. #if LWIP_CALLBACK_API
  1154. /**
  1155. * Default receive callback that is called if the user didn't register
  1156. * a recv callback for the pcb.
  1157. */
  1158. err_t
  1159. tcp_recv_null(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
  1160. {
  1161. LWIP_UNUSED_ARG(arg);
  1162. if (p != NULL) {
  1163. tcp_recved(pcb, p->tot_len);
  1164. pbuf_free(p);
  1165. } else if (err == ERR_OK) {
  1166. return tcp_close(pcb);
  1167. }
  1168. return ERR_OK;
  1169. }
  1170. #endif /* LWIP_CALLBACK_API */
  1171. /**
  1172. * Kills the oldest active connection that has the same or lower priority than
  1173. * 'prio'.
  1174. *
  1175. * @param prio minimum priority
  1176. */
  1177. static void
  1178. tcp_kill_prio(u8_t prio)
  1179. {
  1180. struct tcp_pcb *pcb, *inactive;
  1181. u32_t inactivity;
  1182. u8_t mprio;
  1183. mprio = TCP_PRIO_MAX;
  1184. /* We kill the oldest active connection that has lower priority than prio. */
  1185. inactivity = 0;
  1186. inactive = NULL;
  1187. for(pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) {
  1188. if (pcb->prio <= prio &&
  1189. pcb->prio <= mprio &&
  1190. (u32_t)(tcp_ticks - pcb->tmr) >= inactivity) {
  1191. inactivity = tcp_ticks - pcb->tmr;
  1192. inactive = pcb;
  1193. mprio = pcb->prio;
  1194. }
  1195. }
  1196. if (inactive != NULL) {
  1197. LWIP_DEBUGF(TCP_DEBUG, ("tcp_kill_prio: killing oldest PCB %p (%"S32_F")\n",
  1198. (void *)inactive, inactivity));
  1199. tcp_abort(inactive);
  1200. }
  1201. }
  1202. /**
  1203. * Kills the oldest connection that is in TIME_WAIT state.
  1204. * Called from tcp_alloc() if no more connections are available.
  1205. */
  1206. static void
  1207. tcp_kill_timewait(void)
  1208. {
  1209. struct tcp_pcb *pcb, *inactive;
  1210. u32_t inactivity;
  1211. inactivity = 0;
  1212. inactive = NULL;
  1213. /* Go through the list of TIME_WAIT pcbs and get the oldest pcb. */
  1214. for(pcb = tcp_tw_pcbs; pcb != NULL; pcb = pcb->next) {
  1215. if ((u32_t)(tcp_ticks - pcb->tmr) >= inactivity) {
  1216. inactivity = tcp_ticks - pcb->tmr;
  1217. inactive = pcb;
  1218. }
  1219. }
  1220. if (inactive != NULL) {
  1221. LWIP_DEBUGF(TCP_DEBUG, ("tcp_kill_timewait: killing oldest TIME-WAIT PCB %p (%"S32_F")\n",
  1222. (void *)inactive, inactivity));
  1223. tcp_abort(inactive);
  1224. }
  1225. }
  1226. /**
  1227. * Allocate a new tcp_pcb structure.
  1228. *
  1229. * @param prio priority for the new pcb
  1230. * @return a new tcp_pcb that initially is in state CLOSED
  1231. */
  1232. struct tcp_pcb *
  1233. tcp_alloc(u8_t prio)
  1234. {
  1235. struct tcp_pcb *pcb;
  1236. u32_t iss;
  1237. pcb = (struct tcp_pcb *)memp_malloc(MEMP_TCP_PCB);
  1238. if (pcb == NULL) {
  1239. /* Try killing oldest connection in TIME-WAIT. */
  1240. LWIP_DEBUGF(TCP_DEBUG, ("tcp_alloc: killing off oldest TIME-WAIT connection\n"));
  1241. tcp_kill_timewait();
  1242. /* Try to allocate a tcp_pcb again. */
  1243. pcb = (struct tcp_pcb *)memp_malloc(MEMP_TCP_PCB);
  1244. if (pcb == NULL) {
  1245. /* Try killing active connections with lower priority than the new one. */
  1246. LWIP_DEBUGF(TCP_DEBUG, ("tcp_alloc: killing connection with prio lower than %d\n", prio));
  1247. tcp_kill_prio(prio);
  1248. /* Try to allocate a tcp_pcb again. */
  1249. pcb = (struct tcp_pcb *)memp_malloc(MEMP_TCP_PCB);
  1250. if (pcb != NULL) {
  1251. /* adjust err stats: memp_malloc failed twice before */
  1252. MEMP_STATS_DEC(err, MEMP_TCP_PCB);
  1253. }
  1254. }
  1255. if (pcb != NULL) {
  1256. /* adjust err stats: timewait PCB was freed above */
  1257. MEMP_STATS_DEC(err, MEMP_TCP_PCB);
  1258. }
  1259. }
  1260. if (pcb != NULL) {
  1261. memset(pcb, 0, sizeof(struct tcp_pcb));
  1262. pcb->prio = prio;
  1263. pcb->snd_buf = TCP_SND_BUF;
  1264. pcb->snd_queuelen = 0;
  1265. pcb->rcv_wnd = TCP_WND;
  1266. pcb->rcv_ann_wnd = TCP_WND;
  1267. pcb->tos = 0;
  1268. pcb->ttl = TCP_TTL;
  1269. /* As initial send MSS, we use TCP_MSS but limit it to 536.
  1270. The send MSS is updated when an MSS option is received. */
  1271. pcb->mss = (TCP_MSS > 536) ? 536 : TCP_MSS;
  1272. pcb->rto = 3000 / TCP_SLOW_INTERVAL;
  1273. pcb->sa = 0;
  1274. pcb->sv = 3000 / TCP_SLOW_INTERVAL;
  1275. pcb->rtime = -1;
  1276. pcb->cwnd = 1;
  1277. iss = tcp_next_iss();
  1278. pcb->snd_wl2 = iss;
  1279. pcb->snd_nxt = iss;
  1280. pcb->lastack = iss;
  1281. pcb->snd_lbb = iss;
  1282. pcb->tmr = tcp_ticks;
  1283. pcb->last_timer = tcp_timer_ctr;
  1284. pcb->polltmr = 0;
  1285. #if LWIP_CALLBACK_API
  1286. pcb->recv = tcp_recv_null;
  1287. #endif /* LWIP_CALLBACK_API */
  1288. /* Init KEEPALIVE timer */
  1289. pcb->keep_idle = TCP_KEEPIDLE_DEFAULT;
  1290. #if LWIP_TCP_KEEPALIVE
  1291. pcb->keep_intvl = TCP_KEEPINTVL_DEFAULT;
  1292. pcb->keep_cnt = TCP_KEEPCNT_DEFAULT;
  1293. #endif /* LWIP_TCP_KEEPALIVE */
  1294. pcb->keep_cnt_sent = 0;
  1295. }
  1296. return pcb;
  1297. }
  1298. /**
  1299. * Creates a new TCP protocol control block but doesn't place it on
  1300. * any of the TCP PCB lists.
  1301. * The pcb is not put on any list until binding using tcp_bind().
  1302. *
  1303. * @internal: Maybe there should be a idle TCP PCB list where these
  1304. * PCBs are put on. Port reservation using tcp_bind() is implemented but
  1305. * allocated pcbs that are not bound can't be killed automatically if wanting
  1306. * to allocate a pcb with higher prio (@see tcp_kill_prio())
  1307. *
  1308. * @return a new tcp_pcb that initially is in state CLOSED
  1309. */
  1310. struct tcp_pcb *
  1311. tcp_new(void)
  1312. {
  1313. return tcp_alloc(TCP_PRIO_NORMAL);
  1314. }
  1315. #if LWIP_IPV6
  1316. /**
  1317. * Creates a new TCP-over-IPv6 protocol control block but doesn't
  1318. * place it on any of the TCP PCB lists.
  1319. * The pcb is not put on any list until binding using tcp_bind().
  1320. *
  1321. * @return a new tcp_pcb that initially is in state CLOSED
  1322. */
  1323. struct tcp_pcb *
  1324. tcp_new_ip6(void)
  1325. {
  1326. struct tcp_pcb * pcb;
  1327. pcb = tcp_alloc(TCP_PRIO_NORMAL);
  1328. ip_set_v6(pcb, 1);
  1329. return pcb;
  1330. }
  1331. #endif /* LWIP_IPV6 */
  1332. /**
  1333. * Used to specify the argument that should be passed callback
  1334. * functions.
  1335. *
  1336. * @param pcb tcp_pcb to set the callback argument
  1337. * @param arg void pointer argument to pass to callback functions
  1338. */
  1339. void
  1340. tcp_arg(struct tcp_pcb *pcb, void *arg)
  1341. {
  1342. /* This function is allowed to be called for both listen pcbs and
  1343. connection pcbs. */
  1344. pcb->callback_arg = arg;
  1345. }
  1346. #if LWIP_CALLBACK_API
  1347. /**
  1348. * Used to specify the function that should be called when a TCP
  1349. * connection receives data.
  1350. *
  1351. * @param pcb tcp_pcb to set the recv callback
  1352. * @param recv callback function to call for this pcb when data is received
  1353. */
  1354. void
  1355. tcp_recv(struct tcp_pcb *pcb, tcp_recv_fn recv)
  1356. {
  1357. LWIP_ASSERT("invalid socket state for recv callback", pcb->state != LISTEN);
  1358. pcb->recv = recv;
  1359. }
  1360. /**
  1361. * Used to specify the function that should be called when TCP data
  1362. * has been successfully delivered to the remote host.
  1363. *
  1364. * @param pcb tcp_pcb to set the sent callback
  1365. * @param sent callback function to call for this pcb when data is successfully sent
  1366. */
  1367. void
  1368. tcp_sent(struct tcp_pcb *pcb, tcp_sent_fn sent)
  1369. {
  1370. LWIP_ASSERT("invalid socket state for sent callback", pcb->state != LISTEN);
  1371. pcb->sent = sent;
  1372. }
  1373. /**
  1374. * Used to specify the function that should be called when a fatal error
  1375. * has occured on the connection.
  1376. *
  1377. * @param pcb tcp_pcb to set the err callback
  1378. * @param err callback function to call for this pcb when a fatal error
  1379. * has occured on the connection
  1380. */
  1381. void
  1382. tcp_err(struct tcp_pcb *pcb, tcp_err_fn err)
  1383. {
  1384. LWIP_ASSERT("invalid socket state for err callback", pcb->state != LISTEN);
  1385. pcb->errf = err;
  1386. }
  1387. /**
  1388. * Used for specifying the function that should be called when a
  1389. * LISTENing connection has been connected to another host.
  1390. *
  1391. * @param pcb tcp_pcb to set the accept callback
  1392. * @param accept callback function to call for this pcb when LISTENing
  1393. * connection has been connected to another host
  1394. */
  1395. void
  1396. tcp_accept(struct tcp_pcb *pcb, tcp_accept_fn accept)
  1397. {
  1398. /* This function is allowed to be called for both listen pcbs and
  1399. connection pcbs. */
  1400. pcb->accept = accept;
  1401. }
  1402. #endif /* LWIP_CALLBACK_API */
  1403. /**
  1404. * Used to specify the function that should be called periodically
  1405. * from TCP. The interval is specified in terms of the TCP coarse
  1406. * timer interval, which is called twice a second.
  1407. *
  1408. */
  1409. void
  1410. tcp_poll(struct tcp_pcb *pcb, tcp_poll_fn poll, u8_t interval)
  1411. {
  1412. LWIP_ASSERT("invalid socket state for poll", pcb->state != LISTEN);
  1413. #if LWIP_CALLBACK_API
  1414. pcb->poll = poll;
  1415. #else /* LWIP_CALLBACK_API */
  1416. LWIP_UNUSED_ARG(poll);
  1417. #endif /* LWIP_CALLBACK_API */
  1418. pcb->pollinterval = interval;
  1419. }
  1420. /**
  1421. * Purges a TCP PCB. Removes any buffered data and frees the buffer memory
  1422. * (pcb->ooseq, pcb->unsent and pcb->unacked are freed).
  1423. *
  1424. * @param pcb tcp_pcb to purge. The pcb itself is not deallocated!
  1425. */
  1426. void
  1427. tcp_pcb_purge(struct tcp_pcb *pcb)
  1428. {
  1429. if (pcb->state != CLOSED &&
  1430. pcb->state != TIME_WAIT &&
  1431. pcb->state != LISTEN) {
  1432. LWIP_DEBUGF(TCP_DEBUG, ("tcp_pcb_purge\n"));
  1433. #if TCP_LISTEN_BACKLOG
  1434. if (pcb->state == SYN_RCVD) {
  1435. /* Need to find the corresponding listen_pcb and decrease its accepts_pending */
  1436. struct tcp_pcb_listen *lpcb;
  1437. LWIP_ASSERT("tcp_pcb_purge: pcb->state == SYN_RCVD but tcp_listen_pcbs is NULL",
  1438. tcp_listen_pcbs.listen_pcbs != NULL);
  1439. for (lpcb = tcp_listen_pcbs.listen_pcbs; lpcb != NULL; lpcb = lpcb->next) {
  1440. if ((!lpcb->bound_to_netif && !pcb->bound_to_netif &&
  1441. (lpcb->local_port == pcb->local_port) &&
  1442. IP_PCB_IPVER_EQ(pcb, lpcb) &&
  1443. (ipX_addr_isany(PCB_ISIPV6(lpcb), &lpcb->local_ip) ||
  1444. ipX_addr_cmp(PCB_ISIPV6(lpcb), &pcb->local_ip, &lpcb->local_ip))) ||
  1445. (lpcb->bound_to_netif && pcb->bound_to_netif &&
  1446. !memcmp(lpcb->local_netif, pcb->local_netif, sizeof(pcb->local_netif)))) {
  1447. /* port and address of the listen pcb match the timed-out pcb */
  1448. LWIP_ASSERT("tcp_pcb_purge: listen pcb does not have accepts pending",
  1449. lpcb->accepts_pending > 0);
  1450. lpcb->accepts_pending--;
  1451. break;
  1452. }
  1453. }
  1454. }
  1455. #endif /* TCP_LISTEN_BACKLOG */
  1456. if (pcb->refused_data != NULL) {
  1457. LWIP_DEBUGF(TCP_DEBUG, ("tcp_pcb_purge: data left on ->refused_data\n"));
  1458. pbuf_free(pcb->refused_data);
  1459. pcb->refused_data = NULL;
  1460. }
  1461. if (pcb->unsent != NULL) {
  1462. LWIP_DEBUGF(TCP_DEBUG, ("tcp_pcb_purge: not all data sent\n"));
  1463. }
  1464. if (pcb->unacked != NULL) {
  1465. LWIP_DEBUGF(TCP_DEBUG, ("tcp_pcb_purge: data left on ->unacked\n"));
  1466. }
  1467. #if TCP_QUEUE_OOSEQ
  1468. if (pcb->ooseq != NULL) {
  1469. LWIP_DEBUGF(TCP_DEBUG, ("tcp_pcb_purge: data left on ->ooseq\n"));
  1470. }
  1471. tcp_segs_free(pcb->ooseq);
  1472. pcb->ooseq = NULL;
  1473. #endif /* TCP_QUEUE_OOSEQ */
  1474. /* Stop the retransmission timer as it will expect data on unacked
  1475. queue if it fires */
  1476. pcb->rtime = -1;
  1477. tcp_segs_free(pcb->unsent);
  1478. tcp_segs_free(pcb->unacked);
  1479. pcb->unacked = pcb->unsent = NULL;
  1480. #if TCP_OVERSIZE
  1481. pcb->unsent_oversize = 0;
  1482. #endif /* TCP_OVERSIZE */
  1483. }
  1484. }
  1485. /**
  1486. * Purges the PCB and removes it from a PCB list. Any delayed ACKs are sent first.
  1487. *
  1488. * @param pcblist PCB list to purge.
  1489. * @param pcb tcp_pcb to purge. The pcb itself is NOT deallocated!
  1490. */
  1491. void
  1492. tcp_pcb_remove(struct tcp_pcb **pcblist, struct tcp_pcb *pcb)
  1493. {
  1494. TCP_RMV(pcblist, pcb);
  1495. tcp_pcb_purge(pcb);
  1496. /* if there is an outstanding delayed ACKs, send it */
  1497. if (pcb->state != TIME_WAIT &&
  1498. pcb->state != LISTEN &&
  1499. pcb->flags & TF_ACK_DELAY) {
  1500. pcb->flags |= TF_ACK_NOW;
  1501. tcp_output(pcb);
  1502. }
  1503. if (pcb->state != LISTEN) {
  1504. LWIP_ASSERT("unsent segments leaking", pcb->unsent == NULL);
  1505. LWIP_ASSERT("unacked segments leaking", pcb->unacked == NULL);
  1506. #if TCP_QUEUE_OOSEQ
  1507. LWIP_ASSERT("ooseq segments leaking", pcb->ooseq == NULL);
  1508. #endif /* TCP_QUEUE_OOSEQ */
  1509. }
  1510. pcb->state = CLOSED;
  1511. LWIP_ASSERT("tcp_pcb_remove: tcp_pcbs_sane()", tcp_pcbs_sane());
  1512. }
  1513. /**
  1514. * Calculates a new initial sequence number for new connections.
  1515. *
  1516. * @return u32_t pseudo random sequence number
  1517. */
  1518. u32_t
  1519. tcp_next_iss(void)
  1520. {
  1521. static u32_t iss = 6510;
  1522. iss += tcp_ticks; /* XXX */
  1523. return iss;
  1524. }
  1525. #if TCP_CALCULATE_EFF_SEND_MSS
  1526. /**
  1527. * Calcluates the effective send mss that can be used for a specific IP address
  1528. * by using ip_route to determin the netif used to send to the address and
  1529. * calculating the minimum of TCP_MSS and that netif's mtu (if set).
  1530. */
  1531. u16_t
  1532. tcp_eff_send_mss_impl(u16_t sendmss, ipX_addr_t *dest
  1533. #if LWIP_IPV6
  1534. , ipX_addr_t *src, u8_t isipv6
  1535. #endif /* LWIP_IPV6 */
  1536. )
  1537. {
  1538. u16_t mss_s;
  1539. struct netif *outif;
  1540. s16_t mtu;
  1541. outif = ipX_route(isipv6, src, dest);
  1542. #if LWIP_IPV6
  1543. if (isipv6) {
  1544. /* First look in destination cache, to see if there is a Path MTU. */
  1545. mtu = nd6_get_destination_mtu(ipX_2_ip6(dest), outif);
  1546. } else
  1547. #endif /* LWIP_IPV6 */
  1548. {
  1549. if (outif == NULL) {
  1550. return sendmss;
  1551. }
  1552. mtu = outif->mtu;
  1553. }
  1554. if (mtu != 0) {
  1555. mss_s = mtu - IP_HLEN - TCP_HLEN;
  1556. #if LWIP_IPV6
  1557. /* for IPv6, substract the difference in header size */
  1558. mss_s -= (IP6_HLEN - IP_HLEN);
  1559. #endif /* LWIP_IPV6 */
  1560. /* RFC 1122, chap 4.2.2.6:
  1561. * Eff.snd.MSS = min(SendMSS+20, MMS_S) - TCPhdrsize - IPoptionsize
  1562. * We correct for TCP options in tcp_write(), and don't support IP options.
  1563. */
  1564. sendmss = LWIP_MIN(sendmss, mss_s);
  1565. }
  1566. return sendmss;
  1567. }
  1568. #endif /* TCP_CALCULATE_EFF_SEND_MSS */
  1569. const char*
  1570. tcp_debug_state_str(enum tcp_state s)
  1571. {
  1572. return tcp_state_str[s];
  1573. }
  1574. #if TCP_DEBUG || TCP_INPUT_DEBUG || TCP_OUTPUT_DEBUG
  1575. /**
  1576. * Print a tcp header for debugging purposes.
  1577. *
  1578. * @param tcphdr pointer to a struct tcp_hdr
  1579. */
  1580. void
  1581. tcp_debug_print(struct tcp_hdr *tcphdr)
  1582. {
  1583. LWIP_DEBUGF(TCP_DEBUG, ("TCP header:\n"));
  1584. LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n"));
  1585. LWIP_DEBUGF(TCP_DEBUG, ("| %5"U16_F" | %5"U16_F" | (src port, dest port)\n",
  1586. ntohs(tcphdr->src), ntohs(tcphdr->dest)));
  1587. LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n"));
  1588. LWIP_DEBUGF(TCP_DEBUG, ("| %010"U32_F" | (seq no)\n",
  1589. ntohl(tcphdr->seqno)));
  1590. LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n"));
  1591. LWIP_DEBUGF(TCP_DEBUG, ("| %010"U32_F" | (ack no)\n",
  1592. ntohl(tcphdr->ackno)));
  1593. LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n"));
  1594. LWIP_DEBUGF(TCP_DEBUG, ("| %2"U16_F" | |%"U16_F"%"U16_F"%"U16_F"%"U16_F"%"U16_F"%"U16_F"| %5"U16_F" | (hdrlen, flags (",
  1595. TCPH_HDRLEN(tcphdr),
  1596. TCPH_FLAGS(tcphdr) >> 5 & 1,
  1597. TCPH_FLAGS(tcphdr) >> 4 & 1,
  1598. TCPH_FLAGS(tcphdr) >> 3 & 1,
  1599. TCPH_FLAGS(tcphdr) >> 2 & 1,
  1600. TCPH_FLAGS(tcphdr) >> 1 & 1,
  1601. TCPH_FLAGS(tcphdr) & 1,
  1602. ntohs(tcphdr->wnd)));
  1603. tcp_debug_print_flags(TCPH_FLAGS(tcphdr));
  1604. LWIP_DEBUGF(TCP_DEBUG, ("), win)\n"));
  1605. LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n"));
  1606. LWIP_DEBUGF(TCP_DEBUG, ("| 0x%04"X16_F" | %5"U16_F" | (chksum, urgp)\n",
  1607. ntohs(tcphdr->chksum), ntohs(tcphdr->urgp)));
  1608. LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n"));
  1609. }
  1610. /**
  1611. * Print a tcp state for debugging purposes.
  1612. *
  1613. * @param s enum tcp_state to print
  1614. */
  1615. void
  1616. tcp_debug_print_state(enum tcp_state s)
  1617. {
  1618. LWIP_DEBUGF(TCP_DEBUG, ("State: %s\n", tcp_state_str[s]));
  1619. }
  1620. /**
  1621. * Print tcp flags for debugging purposes.
  1622. *
  1623. * @param flags tcp flags, all active flags are printed
  1624. */
  1625. void
  1626. tcp_debug_print_flags(u8_t flags)
  1627. {
  1628. if (flags & TCP_FIN) {
  1629. LWIP_DEBUGF(TCP_DEBUG, ("FIN "));
  1630. }
  1631. if (flags & TCP_SYN) {
  1632. LWIP_DEBUGF(TCP_DEBUG, ("SYN "));
  1633. }
  1634. if (flags & TCP_RST) {
  1635. LWIP_DEBUGF(TCP_DEBUG, ("RST "));
  1636. }
  1637. if (flags & TCP_PSH) {
  1638. LWIP_DEBUGF(TCP_DEBUG, ("PSH "));
  1639. }
  1640. if (flags & TCP_ACK) {
  1641. LWIP_DEBUGF(TCP_DEBUG, ("ACK "));
  1642. }
  1643. if (flags & TCP_URG) {
  1644. LWIP_DEBUGF(TCP_DEBUG, ("URG "));
  1645. }
  1646. if (flags & TCP_ECE) {
  1647. LWIP_DEBUGF(TCP_DEBUG, ("ECE "));
  1648. }
  1649. if (flags & TCP_CWR) {
  1650. LWIP_DEBUGF(TCP_DEBUG, ("CWR "));
  1651. }
  1652. LWIP_DEBUGF(TCP_DEBUG, ("\n"));
  1653. }
  1654. /**
  1655. * Print all tcp_pcbs in every list for debugging purposes.
  1656. */
  1657. void
  1658. tcp_debug_print_pcbs(void)
  1659. {
  1660. struct tcp_pcb *pcb;
  1661. LWIP_DEBUGF(TCP_DEBUG, ("Active PCB states:\n"));
  1662. for(pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) {
  1663. LWIP_DEBUGF(TCP_DEBUG, ("Local port %"U16_F", foreign port %"U16_F" snd_nxt %"U32_F" rcv_nxt %"U32_F" ",
  1664. pcb->local_port, pcb->remote_port,
  1665. pcb->snd_nxt, pcb->rcv_nxt));
  1666. tcp_debug_print_state(pcb->state);
  1667. }
  1668. LWIP_DEBUGF(TCP_DEBUG, ("Listen PCB states:\n"));
  1669. for(pcb = (struct tcp_pcb *)tcp_listen_pcbs.pcbs; pcb != NULL; pcb = pcb->next) {
  1670. LWIP_DEBUGF(TCP_DEBUG, ("Local port %"U16_F", foreign port %"U16_F" snd_nxt %"U32_F" rcv_nxt %"U32_F" ",
  1671. pcb->local_port, pcb->remote_port,
  1672. pcb->snd_nxt, pcb->rcv_nxt));
  1673. tcp_debug_print_state(pcb->state);
  1674. }
  1675. LWIP_DEBUGF(TCP_DEBUG, ("TIME-WAIT PCB states:\n"));
  1676. for(pcb = tcp_tw_pcbs; pcb != NULL; pcb = pcb->next) {
  1677. LWIP_DEBUGF(TCP_DEBUG, ("Local port %"U16_F", foreign port %"U16_F" snd_nxt %"U32_F" rcv_nxt %"U32_F" ",
  1678. pcb->local_port, pcb->remote_port,
  1679. pcb->snd_nxt, pcb->rcv_nxt));
  1680. tcp_debug_print_state(pcb->state);
  1681. }
  1682. }
  1683. /**
  1684. * Check state consistency of the tcp_pcb lists.
  1685. */
  1686. s16_t
  1687. tcp_pcbs_sane(void)
  1688. {
  1689. struct tcp_pcb *pcb;
  1690. for(pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) {
  1691. LWIP_ASSERT("tcp_pcbs_sane: active pcb->state != CLOSED", pcb->state != CLOSED);
  1692. LWIP_ASSERT("tcp_pcbs_sane: active pcb->state != LISTEN", pcb->state != LISTEN);
  1693. LWIP_ASSERT("tcp_pcbs_sane: active pcb->state != TIME-WAIT", pcb->state != TIME_WAIT);
  1694. }
  1695. for(pcb = tcp_tw_pcbs; pcb != NULL; pcb = pcb->next) {
  1696. LWIP_ASSERT("tcp_pcbs_sane: tw pcb->state == TIME-WAIT", pcb->state == TIME_WAIT);
  1697. }
  1698. return 1;
  1699. }
  1700. #endif /* TCP_DEBUG */
  1701. #endif /* LWIP_TCP */