1 /**
2 * @file
3 * Transmission Control Protocol for IP
4 * See also @ref tcp_raw
5 *
6 * @defgroup tcp_raw TCP
7 * @ingroup callbackstyle_api
8 * Transmission Control Protocol for IP\n
9 * @see @ref raw_api and @ref netconn
10 *
11 * Common functions for the TCP implementation, such as functinos
12 * for manipulating the data structures and the TCP timer functions. TCP functions
13 * related to input and output is found in tcp_in.c and tcp_out.c respectively.\n
14 */
15
16 /*
17 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
18 * All rights reserved.
19 *
20 * Redistribution and use in source and binary forms, with or without modification,
21 * are permitted provided that the following conditions are met:
22 *
23 * 1. Redistributions of source code must retain the above copyright notice,
24 * this list of conditions and the following disclaimer.
25 * 2. Redistributions in binary form must reproduce the above copyright notice,
26 * this list of conditions and the following disclaimer in the documentation
27 * and/or other materials provided with the distribution.
28 * 3. The name of the author may not be used to endorse or promote products
29 * derived from this software without specific prior written permission.
30 *
31 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
32 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
33 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
34 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
35 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
36 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
37 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
38 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
39 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
40 * OF SUCH DAMAGE.
41 *
42 * This file is part of the lwIP TCP/IP stack.
43 *
44 * Author: Adam Dunkels <adam@sics.se>
45 *
46 */
47
48 #include "lwip/opt.h"
49
50 #if LWIP_TCP /* don't build if not configured for use in lwipopts.h */
51
52 #include "lwip/def.h"
53 #include "lwip/mem.h"
54 #include "lwip/memp.h"
55 #include "lwip/tcp.h"
56 #include "lwip/priv/tcp_priv.h"
57 #include "lwip/debug.h"
58 #include "lwip/stats.h"
59 #include "lwip/ip6.h"
60 #include "lwip/ip6_addr.h"
61 #include "lwip/nd6.h"
62
63 #include <string.h>
64
65 #ifndef TCP_LOCAL_PORT_RANGE_START
66 /* From http://www.iana.org/assignments/port-numbers:
67 "The Dynamic and/or Private Ports are those from 49152 through 65535" */
68 #define TCP_LOCAL_PORT_RANGE_START 0xc000
69 #define TCP_LOCAL_PORT_RANGE_END 0xffff
70 #define TCP_ENSURE_LOCAL_PORT_RANGE(port) ((u16_t)(((port) & ~TCP_LOCAL_PORT_RANGE_START) + TCP_LOCAL_PORT_RANGE_START))
71 #endif
72
73 #if LWIP_TCP_KEEPALIVE
74 #define TCP_KEEP_DUR(pcb) ((pcb)->keep_cnt * (pcb)->keep_intvl)
75 #define TCP_KEEP_INTVL(pcb) ((pcb)->keep_intvl)
76 #else /* LWIP_TCP_KEEPALIVE */
77 #define TCP_KEEP_DUR(pcb) TCP_MAXIDLE
78 #define TCP_KEEP_INTVL(pcb) TCP_KEEPINTVL_DEFAULT
79 #endif /* LWIP_TCP_KEEPALIVE */
80
81 /* As initial send MSS, we use TCP_MSS but limit it to 536. */
82 #if TCP_MSS > 536
83 #define INITIAL_MSS 536
84 #else
85 #define INITIAL_MSS TCP_MSS
86 #endif
87
88 static const char * const tcp_state_str[] = {
89 "CLOSED",
90 "LISTEN",
91 "SYN_SENT",
92 "SYN_RCVD",
93 "ESTABLISHED",
94 "FIN_WAIT_1",
95 "FIN_WAIT_2",
96 "CLOSE_WAIT",
97 "CLOSING",
98 "LAST_ACK",
99 "TIME_WAIT"
100 };
101
102 /* last local TCP port */
103 static u16_t tcp_port = TCP_LOCAL_PORT_RANGE_START;
104
105 /* Incremented every coarse grained timer shot (typically every 500 ms). */
106 u32_t tcp_ticks;
107 static const u8_t tcp_backoff[13] =
108 { 1, 2, 3, 4, 5, 6, 7, 7, 7, 7, 7, 7, 7};
109 /* Times per slowtmr hits */
110 static const u8_t tcp_persist_backoff[7] = { 3, 6, 12, 24, 48, 96, 120 };
111
112 /* lwip rto/wnd flags */
113 int lwip_rto_flags = RTO_FLAGS_DEFAULT;
114 int lwip_rcv_wnd_flags = WND_FLAGS_DEFAULT;
115 /* The TCP PCB lists. */
116
117 /** List of all TCP PCBs bound but not yet (connected || listening) */
118 struct tcp_pcb *tcp_bound_pcbs;
119 /** List of all TCP PCBs in LISTEN state */
120 union tcp_listen_pcbs_t tcp_listen_pcbs;
121 /** List of all TCP PCBs that are in a state in which
122 * they accept or send data. */
123 struct tcp_pcb *tcp_active_pcbs;
124 /** List of all TCP PCBs in TIME-WAIT state */
125 struct tcp_pcb *tcp_tw_pcbs;
126
127 /** An array with all (non-temporary) PCB lists, mainly used for smaller code size */
128 struct tcp_pcb ** const tcp_pcb_lists[] = {&tcp_listen_pcbs.pcbs, &tcp_bound_pcbs,
129 &tcp_active_pcbs, &tcp_tw_pcbs};
130
131 u8_t tcp_active_pcbs_changed;
132
133 /** Timer counter to handle calling slow-timer from tcp_tmr() */
134 static u8_t tcp_timer;
135 static u8_t tcp_timer_ctr;
136 static u16_t tcp_new_port(void);
137
138 /**
139 * Initialize this module.
140 */
141 void
tcp_init(void)142 tcp_init(void)
143 {
144 #if LWIP_RANDOMIZE_INITIAL_LOCAL_PORTS && defined(LWIP_RAND)
145 tcp_port = TCP_ENSURE_LOCAL_PORT_RANGE(LWIP_RAND());
146 #endif /* LWIP_RANDOMIZE_INITIAL_LOCAL_PORTS && defined(LWIP_RAND) */
147 }
148
149 /**
150 * Called periodically to dispatch TCP timers.
151 */
152 void
tcp_tmr(void)153 tcp_tmr(void)
154 {
155 /* Call tcp_fasttmr() every 250 ms */
156 tcp_fasttmr();
157
158 if (++tcp_timer & 1) {
159 /* Call tcp_slowtmr() every 500 ms, i.e., every other timer
160 tcp_tmr() is called. */
161 tcp_slowtmr();
162 }
163 }
164
165 #if LWIP_CALLBACK_API || TCP_LISTEN_BACKLOG
166 /** Called when a listen pcb is closed. Iterates one pcb list and removes the
167 * closed listener pcb from pcb->listener if matching.
168 */
169 static void
tcp_remove_listener(struct tcp_pcb * list,struct tcp_pcb_listen * lpcb)170 tcp_remove_listener(struct tcp_pcb *list, struct tcp_pcb_listen *lpcb)
171 {
172 struct tcp_pcb *pcb;
173 for (pcb = list; pcb != NULL; pcb = pcb->next) {
174 if (pcb->listener == lpcb) {
175 pcb->listener = NULL;
176 }
177 }
178 }
179 #endif
180
181 /** Called when a listen pcb is closed. Iterates all pcb lists and removes the
182 * closed listener pcb from pcb->listener if matching.
183 */
184 static void
tcp_listen_closed(struct tcp_pcb * pcb)185 tcp_listen_closed(struct tcp_pcb *pcb)
186 {
187 #if LWIP_CALLBACK_API || TCP_LISTEN_BACKLOG
188 size_t i;
189 LWIP_ASSERT("pcb != NULL", pcb != NULL);
190 LWIP_ASSERT("pcb->state == LISTEN", pcb->state == LISTEN);
191 for (i = 1; i < LWIP_ARRAYSIZE(tcp_pcb_lists); i++) {
192 tcp_remove_listener(*tcp_pcb_lists[i], (struct tcp_pcb_listen*)pcb);
193 }
194 #endif
195 LWIP_UNUSED_ARG(pcb);
196 }
197
198 #if TCP_LISTEN_BACKLOG
199 /** @ingroup tcp_raw
200 * Delay accepting a connection in respect to the listen backlog:
201 * the number of outstanding connections is increased until
202 * tcp_backlog_accepted() is called.
203 *
204 * ATTENTION: the caller is responsible for calling tcp_backlog_accepted()
205 * or else the backlog feature will get out of sync!
206 *
207 * @param pcb the connection pcb which is not fully accepted yet
208 */
209 void
tcp_backlog_delayed(struct tcp_pcb * pcb)210 tcp_backlog_delayed(struct tcp_pcb* pcb)
211 {
212 LWIP_ASSERT("pcb != NULL", pcb != NULL);
213 if ((pcb->flags & TF_BACKLOGPEND) == 0) {
214 if (pcb->listener != NULL) {
215 pcb->listener->accepts_pending++;
216 LWIP_ASSERT("accepts_pending != 0", pcb->listener->accepts_pending != 0);
217 pcb->flags |= TF_BACKLOGPEND;
218 }
219 }
220 }
221
222 /** @ingroup tcp_raw
223 * A delayed-accept a connection is accepted (or closed/aborted): decreases
224 * the number of outstanding connections after calling tcp_backlog_delayed().
225 *
226 * ATTENTION: the caller is responsible for calling tcp_backlog_accepted()
227 * or else the backlog feature will get out of sync!
228 *
229 * @param pcb the connection pcb which is now fully accepted (or closed/aborted)
230 */
231 void
tcp_backlog_accepted(struct tcp_pcb * pcb)232 tcp_backlog_accepted(struct tcp_pcb* pcb)
233 {
234 LWIP_ASSERT("pcb != NULL", pcb != NULL);
235 if ((pcb->flags & TF_BACKLOGPEND) != 0) {
236 if (pcb->listener != NULL) {
237 LWIP_ASSERT("accepts_pending != 0", pcb->listener->accepts_pending != 0);
238 pcb->listener->accepts_pending--;
239 pcb->flags &= ~TF_BACKLOGPEND;
240 }
241 }
242 }
243 #endif /* TCP_LISTEN_BACKLOG */
244
245 /**
246 * Closes the TX side of a connection held by the PCB.
247 * For tcp_close(), a RST is sent if the application didn't receive all data
248 * (tcp_recved() not called for all data passed to recv callback).
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.
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 static err_t
tcp_close_shutdown(struct tcp_pcb * pcb,u8_t rst_on_unacked_data)262 tcp_close_shutdown(struct tcp_pcb *pcb, u8_t rst_on_unacked_data)
263 {
264 err_t err;
265
266 if (rst_on_unacked_data && ((pcb->state == ESTABLISHED) || (pcb->state == CLOSE_WAIT))) {
267 if ((pcb->refused_data != NULL) || (pcb->rcv_wnd != TCP_WND_MAX(pcb))) {
268 /* Not all data received by application, send RST to tell the remote
269 side about this. */
270 LWIP_ASSERT("pcb->flags & TF_RXCLOSED", pcb->flags & TF_RXCLOSED);
271
272 /* don't call tcp_abort here: we must not deallocate the pcb since
273 that might not be expected when calling tcp_close */
274 tcp_rst(pcb->snd_nxt, pcb->rcv_nxt, &pcb->local_ip, &pcb->remote_ip,
275 pcb->local_port, pcb->remote_port);
276
277 tcp_pcb_purge(pcb);
278 TCP_RMV_ACTIVE(pcb);
279 if (pcb->state == ESTABLISHED) {
280 /* move to TIME_WAIT since we close actively */
281 pcb->state = TIME_WAIT;
282 TCP_REG(&tcp_tw_pcbs, pcb);
283 } else {
284 /* CLOSE_WAIT: deallocate the pcb since we already sent a RST for it */
285 if (tcp_input_pcb == pcb) {
286 /* prevent using a deallocated pcb: free it from tcp_input later */
287 tcp_trigger_input_pcb_close();
288 } else {
289 memp_free(MEMP_TCP_PCB, pcb);
290 }
291 }
292 return ERR_OK;
293 }
294 }
295
296 switch (pcb->state) {
297 case CLOSED:
298 /* Closing a pcb in the CLOSED state might seem erroneous,
299 * however, it is in this state once allocated and as yet unused
300 * and the user needs some way to free it should the need arise.
301 * Calling tcp_close() with a pcb that has already been closed, (i.e. twice)
302 * or for a pcb that has been used and then entered the CLOSED state
303 * is erroneous, but this should never happen as the pcb has in those cases
304 * been freed, and so any remaining handles are bogus. */
305 err = ERR_OK;
306 if (pcb->local_port != 0) {
307 TCP_RMV(&tcp_bound_pcbs, pcb);
308 }
309 memp_free(MEMP_TCP_PCB, pcb);
310 pcb = NULL;
311 break;
312 case LISTEN:
313 err = ERR_OK;
314 tcp_listen_closed(pcb);
315 tcp_pcb_remove(&tcp_listen_pcbs.pcbs, pcb);
316 memp_free(MEMP_TCP_PCB_LISTEN, pcb);
317 pcb = NULL;
318 break;
319 case SYN_SENT:
320 err = ERR_OK;
321 TCP_PCB_REMOVE_ACTIVE(pcb);
322 memp_free(MEMP_TCP_PCB, pcb);
323 pcb = NULL;
324 MIB2_STATS_INC(mib2.tcpattemptfails);
325 break;
326 case SYN_RCVD:
327 err = tcp_send_fin(pcb);
328 if (err == ERR_OK) {
329 tcp_backlog_accepted(pcb);
330 MIB2_STATS_INC(mib2.tcpattemptfails);
331 pcb->state = FIN_WAIT_1;
332 }
333 break;
334 case ESTABLISHED:
335 err = tcp_send_fin(pcb);
336 if (err == ERR_OK) {
337 MIB2_STATS_INC(mib2.tcpestabresets);
338 pcb->state = FIN_WAIT_1;
339 }
340 break;
341 case CLOSE_WAIT:
342 err = tcp_send_fin(pcb);
343 if (err == ERR_OK) {
344 MIB2_STATS_INC(mib2.tcpestabresets);
345 pcb->state = LAST_ACK;
346 }
347 break;
348 default:
349 /* Has already been closed, do nothing. */
350 err = ERR_OK;
351 pcb = NULL;
352 break;
353 }
354
355 if (pcb != NULL && err == ERR_OK) {
356 /* To ensure all data has been sent when tcp_close returns, we have
357 to make sure tcp_output doesn't fail.
358 Since we don't really have to ensure all data has been sent when tcp_close
359 returns (unsent data is sent from tcp timer functions, also), we don't care
360 for the return value of tcp_output for now. */
361 tcp_output(pcb);
362 }
363 return err;
364 }
365
366 /**
367 * @ingroup tcp_raw
368 * Closes the connection held by the PCB.
369 *
370 * Listening pcbs are freed and may not be referenced any more.
371 * Connection pcbs are freed if not yet connected and may not be referenced
372 * any more. If a connection is established (at least SYN received or in
373 * a closing state), the connection is closed, and put in a closing state.
374 * The pcb is then automatically freed in tcp_slowtmr(). It is therefore
375 * unsafe to reference it (unless an error is returned).
376 *
377 * @param pcb the tcp_pcb to close
378 * @return ERR_OK if connection has been closed
379 * another err_t if closing failed and pcb is not freed
380 */
381 err_t
tcp_close(struct tcp_pcb * pcb)382 tcp_close(struct tcp_pcb *pcb)
383 {
384 LWIP_DEBUGF(TCP_DEBUG, ("tcp_close: closing in "));
385 tcp_debug_print_state(pcb->state);
386
387 if (pcb->state != LISTEN) {
388 /* Set a flag not to receive any more data... */
389 pcb->flags |= TF_RXCLOSED;
390 }
391 /* ... and close */
392 return tcp_close_shutdown(pcb, 1);
393 }
394
395 /**
396 * @ingroup tcp_raw
397 * Causes all or part of a full-duplex connection of this PCB to be shut down.
398 * This doesn't deallocate the PCB unless shutting down both sides!
399 * Shutting down both sides is the same as calling tcp_close, so if it succeds,
400 * the PCB should not be referenced any more.
401 *
402 * @param pcb PCB to shutdown
403 * @param shut_rx shut down receive side if this is != 0
404 * @param shut_tx shut down send side if this is != 0
405 * @return ERR_OK if shutdown succeeded (or the PCB has already been shut down)
406 * another err_t on error.
407 */
408 err_t
tcp_shutdown(struct tcp_pcb * pcb,int shut_rx,int shut_tx)409 tcp_shutdown(struct tcp_pcb *pcb, int shut_rx, int shut_tx)
410 {
411 if (pcb->state == LISTEN) {
412 return ERR_CONN;
413 }
414 if (shut_rx) {
415 /* shut down the receive side: set a flag not to receive any more data... */
416 pcb->flags |= TF_RXCLOSED;
417 if (shut_tx) {
418 /* shutting down the tx AND rx side is the same as closing for the raw API */
419 return tcp_close_shutdown(pcb, 1);
420 }
421 /* ... and free buffered data */
422 if (pcb->refused_data != NULL) {
423 pbuf_free(pcb->refused_data);
424 pcb->refused_data = NULL;
425 }
426 }
427 if (shut_tx) {
428 /* This can't happen twice since if it succeeds, the pcb's state is changed.
429 Only close in these states as the others directly deallocate the PCB */
430 switch (pcb->state) {
431 case SYN_RCVD:
432 case ESTABLISHED:
433 case CLOSE_WAIT:
434 return tcp_close_shutdown(pcb, (u8_t)shut_rx);
435 default:
436 /* Not (yet?) connected, cannot shutdown the TX side as that would bring us
437 into CLOSED state, where the PCB is deallocated. */
438 return ERR_CONN;
439 }
440 }
441 return ERR_OK;
442 }
443
444 /**
445 * Abandons a connection and optionally sends a RST to the remote
446 * host. Deletes the local protocol control block. This is done when
447 * a connection is killed because of shortage of memory.
448 *
449 * @param pcb the tcp_pcb to abort
450 * @param reset boolean to indicate whether a reset should be sent
451 */
452 void
tcp_abandon(struct tcp_pcb * pcb,int reset)453 tcp_abandon(struct tcp_pcb *pcb, int reset)
454 {
455 u32_t seqno, ackno;
456 #if LWIP_CALLBACK_API
457 tcp_err_fn errf;
458 #endif /* LWIP_CALLBACK_API */
459 void *errf_arg;
460
461 /* pcb->state LISTEN not allowed here */
462 LWIP_ASSERT("don't call tcp_abort/tcp_abandon for listen-pcbs",
463 pcb->state != LISTEN);
464 /* Figure out on which TCP PCB list we are, and remove us. If we
465 are in an active state, call the receive function associated with
466 the PCB with a NULL argument, and send an RST to the remote end. */
467 if (pcb->state == TIME_WAIT) {
468 tcp_pcb_remove(&tcp_tw_pcbs, pcb);
469 memp_free(MEMP_TCP_PCB, pcb);
470 } else {
471 int send_rst = 0;
472 u16_t local_port = 0;
473 seqno = pcb->snd_nxt;
474 ackno = pcb->rcv_nxt;
475 #if LWIP_CALLBACK_API
476 errf = pcb->errf;
477 #endif /* LWIP_CALLBACK_API */
478 errf_arg = pcb->callback_arg;
479 if (pcb->state == CLOSED) {
480 if (pcb->local_port != 0) {
481 /* bound, not yet opened */
482 TCP_RMV(&tcp_bound_pcbs, pcb);
483 }
484 } else {
485 send_rst = reset;
486 local_port = pcb->local_port;
487 TCP_PCB_REMOVE_ACTIVE(pcb);
488 }
489 if (pcb->unacked != NULL) {
490 tcp_segs_free(pcb->unacked);
491 }
492 if (pcb->unsent != NULL) {
493 tcp_segs_free(pcb->unsent);
494 }
495 #if TCP_QUEUE_OOSEQ
496 if (pcb->ooseq != NULL) {
497 tcp_segs_free(pcb->ooseq);
498 }
499 #endif /* TCP_QUEUE_OOSEQ */
500 tcp_backlog_accepted(pcb);
501 if (send_rst) {
502 LWIP_DEBUGF(TCP_RST_DEBUG, ("tcp_abandon: sending RST\n"));
503 tcp_rst(seqno, ackno, &pcb->local_ip, &pcb->remote_ip, local_port, pcb->remote_port);
504 }
505 memp_free(MEMP_TCP_PCB, pcb);
506 TCP_EVENT_ERR(errf, errf_arg, ERR_ABRT);
507 }
508 }
509
510 /**
511 * @ingroup tcp_raw
512 * Aborts the connection by sending a RST (reset) segment to the remote
513 * host. The pcb is deallocated. This function never fails.
514 *
515 * ATTENTION: When calling this from one of the TCP callbacks, make
516 * sure you always return ERR_ABRT (and never return ERR_ABRT otherwise
517 * or you will risk accessing deallocated memory or memory leaks!
518 *
519 * @param pcb the tcp pcb to abort
520 */
521 void
tcp_abort(struct tcp_pcb * pcb)522 tcp_abort(struct tcp_pcb *pcb)
523 {
524 tcp_abandon(pcb, 1);
525 }
526
527 /**
528 * @ingroup tcp_raw
529 * Binds the connection to a local port number and IP address. If the
530 * IP address is not given (i.e., ipaddr == NULL), the IP address of
531 * the outgoing network interface is used instead.
532 *
533 * @param pcb the tcp_pcb to bind (no check is done whether this pcb is
534 * already bound!)
535 * @param ipaddr the local ip address to bind to (use IP4_ADDR_ANY to bind
536 * to any local address
537 * @param port the local port to bind to
538 * @return ERR_USE if the port is already in use
539 * ERR_VAL if bind failed because the PCB is not in a valid state
540 * ERR_OK if bound
541 */
542 err_t
tcp_bind(struct tcp_pcb * pcb,const ip_addr_t * ipaddr,u16_t port)543 tcp_bind(struct tcp_pcb *pcb, const ip_addr_t *ipaddr, u16_t port)
544 {
545 int i;
546 int max_pcb_list = NUM_TCP_PCB_LISTS;
547 struct tcp_pcb *cpcb;
548
549 #if LWIP_IPV4
550 /* Don't propagate NULL pointer (IPv4 ANY) to subsequent functions */
551 if (ipaddr == NULL) {
552 ipaddr = IP4_ADDR_ANY;
553 }
554 #endif /* LWIP_IPV4 */
555
556 /* still need to check for ipaddr == NULL in IPv6 only case */
557 if ((pcb == NULL) || (ipaddr == NULL) || !IP_ADDR_PCB_VERSION_MATCH_EXACT(pcb, ipaddr)) {
558 return ERR_VAL;
559 }
560
561 LWIP_ERROR("tcp_bind: can only bind in state CLOSED", pcb->state == CLOSED, return ERR_VAL);
562
563 #if SO_REUSE
564 /* Unless the REUSEADDR flag is set,
565 we have to check the pcbs in TIME-WAIT state, also.
566 We do not dump TIME_WAIT pcb's; they can still be matched by incoming
567 packets using both local and remote IP addresses and ports to distinguish.
568 */
569 if (ip_get_option(pcb, SOF_REUSEADDR)) {
570 max_pcb_list = NUM_TCP_PCB_LISTS_NO_TIME_WAIT;
571 }
572 #endif /* SO_REUSE */
573
574 if (port == 0) {
575 port = tcp_new_port();
576 if (port == 0) {
577 return ERR_BUF;
578 }
579 } else {
580 /* Check if the address already is in use (on all lists) */
581 for (i = 0; i < max_pcb_list; i++) {
582 for (cpcb = *tcp_pcb_lists[i]; cpcb != NULL; cpcb = cpcb->next) {
583 if (cpcb->local_port == port) {
584 #if SO_REUSE
585 /* Omit checking for the same port if both pcbs have REUSEADDR set.
586 For SO_REUSEADDR, the duplicate-check for a 5-tuple is done in
587 tcp_connect. */
588 if (!ip_get_option(pcb, SOF_REUSEADDR) ||
589 !ip_get_option(cpcb, SOF_REUSEADDR))
590 #endif /* SO_REUSE */
591 {
592 /* @todo: check accept_any_ip_version */
593 if ((IP_IS_V6(ipaddr) == IP_IS_V6_VAL(cpcb->local_ip)) &&
594 (ip_addr_isany(&cpcb->local_ip) ||
595 ip_addr_isany(ipaddr) ||
596 ip_addr_cmp(&cpcb->local_ip, ipaddr))) {
597 return ERR_USE;
598 }
599 }
600 }
601 }
602 }
603 }
604
605 if (!ip_addr_isany(ipaddr)) {
606 ip_addr_set(&pcb->local_ip, ipaddr);
607 }
608 pcb->local_port = port;
609 TCP_REG(&tcp_bound_pcbs, pcb);
610 LWIP_DEBUGF(TCP_DEBUG, ("tcp_bind: bind to port %"U16_F"\n", port));
611 return ERR_OK;
612 }
613 #if LWIP_CALLBACK_API
614 /**
615 * Default accept callback if no accept callback is specified by the user.
616 */
617 static err_t
tcp_accept_null(void * arg,struct tcp_pcb * pcb,err_t err)618 tcp_accept_null(void *arg, struct tcp_pcb *pcb, err_t err)
619 {
620 LWIP_UNUSED_ARG(arg);
621 LWIP_UNUSED_ARG(err);
622
623 tcp_abort(pcb);
624
625 return ERR_ABRT;
626 }
627 #endif /* LWIP_CALLBACK_API */
628
629 /**
630 * @ingroup tcp_raw
631 * Set the state of the connection to be LISTEN, which means that it
632 * is able to accept incoming connections. The protocol control block
633 * is reallocated in order to consume less memory. Setting the
634 * connection to LISTEN is an irreversible process.
635 *
636 * @param pcb the original tcp_pcb
637 * @param backlog the incoming connections queue limit
638 * @return tcp_pcb used for listening, consumes less memory.
639 *
640 * @note The original tcp_pcb is freed. This function therefore has to be
641 * called like this:
642 * tpcb = tcp_listen(tpcb);
643 */
644 struct tcp_pcb *
tcp_listen_with_backlog(struct tcp_pcb * pcb,u8_t backlog)645 tcp_listen_with_backlog(struct tcp_pcb *pcb, u8_t backlog)
646 {
647 struct tcp_pcb_listen *lpcb;
648
649 LWIP_UNUSED_ARG(backlog);
650 LWIP_ERROR("tcp_listen: pcb already connected", pcb->state == CLOSED, return NULL);
651
652 /* already listening? */
653 if (pcb->state == LISTEN) {
654 return pcb;
655 }
656 #if SO_REUSE
657 if (ip_get_option(pcb, SOF_REUSEADDR)) {
658 /* Since SOF_REUSEADDR allows reusing a local address before the pcb's usage
659 is declared (listen-/connection-pcb), we have to make sure now that
660 this port is only used once for every local IP. */
661 for (lpcb = tcp_listen_pcbs.listen_pcbs; lpcb != NULL; lpcb = lpcb->next) {
662 if ((lpcb->local_port == pcb->local_port) &&
663 ip_addr_cmp(&lpcb->local_ip, &pcb->local_ip)) {
664 /* this address/port is already used */
665 return NULL;
666 }
667 }
668 }
669 #endif /* SO_REUSE */
670 lpcb = (struct tcp_pcb_listen *)memp_malloc(MEMP_TCP_PCB_LISTEN);
671 if (lpcb == NULL) {
672 return NULL;
673 }
674 lpcb->callback_arg = pcb->callback_arg;
675 lpcb->local_port = pcb->local_port;
676 lpcb->state = LISTEN;
677 lpcb->prio = pcb->prio;
678 lpcb->so_options = pcb->so_options;
679 lpcb->ttl = pcb->ttl;
680 lpcb->tos = pcb->tos;
681 #if LWIP_IPV4 && LWIP_IPV6
682 IP_SET_TYPE_VAL(lpcb->remote_ip, pcb->local_ip.type);
683 #endif /* LWIP_IPV4 && LWIP_IPV6 */
684 ip_addr_copy(lpcb->local_ip, pcb->local_ip);
685 if (pcb->local_port != 0) {
686 TCP_RMV(&tcp_bound_pcbs, pcb);
687 }
688 memp_free(MEMP_TCP_PCB, pcb);
689 #if LWIP_CALLBACK_API
690 lpcb->accept = tcp_accept_null;
691 #endif /* LWIP_CALLBACK_API */
692 #if TCP_LISTEN_BACKLOG
693 lpcb->accepts_pending = 0;
694 tcp_backlog_set(lpcb, backlog);
695 #endif /* TCP_LISTEN_BACKLOG */
696 TCP_REG(&tcp_listen_pcbs.pcbs, (struct tcp_pcb *)lpcb);
697 return (struct tcp_pcb *)lpcb;
698 }
699
700 /**
701 * Update the state that tracks the available window space to advertise.
702 *
703 * Returns how much extra window would be advertised if we sent an
704 * update now.
705 */
706 u32_t
tcp_update_rcv_ann_wnd(struct tcp_pcb * pcb)707 tcp_update_rcv_ann_wnd(struct tcp_pcb *pcb)
708 {
709 u32_t new_right_edge = pcb->rcv_nxt + pcb->rcv_wnd;
710 tcpwnd_size_t tcp_wnd = TCP_WND;
711
712 if(pcb->usr_rcv_wnd != 0) {
713 tcp_wnd = pcb->usr_rcv_wnd;
714 }
715
716 if(lwip_rcv_wnd_flags == WND_FLAGS_SMALL) {
717 tcp_wnd = (tcp_wnd < TCP_SMALL_WND ? tcp_wnd : TCP_SMALL_WND);
718 } else if(lwip_rcv_wnd_flags == WND_FLAGS_LARGE) {
719 tcp_wnd = (tcp_wnd > TCP_LARGE_WND ? tcp_wnd : TCP_LARGE_WND);
720 }
721
722 if (TCP_SEQ_GEQ(new_right_edge, pcb->rcv_ann_right_edge + LWIP_MIN((tcp_wnd / 2), pcb->mss))) {
723 /* we can advertise more window */
724 pcb->rcv_ann_wnd = pcb->rcv_wnd;
725 return new_right_edge - pcb->rcv_ann_right_edge;
726 } else {
727 if (TCP_SEQ_GT(pcb->rcv_nxt, pcb->rcv_ann_right_edge)) {
728 /* Can happen due to other end sending out of advertised window,
729 * but within actual available (but not yet advertised) window */
730 pcb->rcv_ann_wnd = 0;
731 } else {
732 /* keep the right edge of window constant */
733 u32_t new_rcv_ann_wnd = pcb->rcv_ann_right_edge - pcb->rcv_nxt;
734 #if !LWIP_WND_SCALE
735 LWIP_ASSERT("new_rcv_ann_wnd <= 0xffff", new_rcv_ann_wnd <= 0xffff);
736 #endif
737 pcb->rcv_ann_wnd = (tcpwnd_size_t)new_rcv_ann_wnd;
738 }
739 return 0;
740 }
741 }
742
743 /**
744 * @ingroup tcp_raw
745 * This function should be called by the application when it has
746 * processed the data. The purpose is to advertise a larger window
747 * when the data has been processed.
748 *
749 * @param pcb the tcp_pcb for which data is read
750 * @param len the amount of bytes that have been read by the application
751 */
752 void
tcp_recved(struct tcp_pcb * pcb,u16_t len)753 tcp_recved(struct tcp_pcb *pcb, u16_t len)
754 {
755 int wnd_inflation;
756
757 /* pcb->state LISTEN not allowed here */
758 LWIP_ASSERT("don't call tcp_recved for listen-pcbs",
759 pcb->state != LISTEN);
760
761 pcb->rcv_wnd += len;
762
763 if (pcb->rcv_wnd > TCP_WND_MAX(pcb)) {
764 pcb->rcv_wnd = TCP_WND_MAX(pcb);
765 } else if (pcb->rcv_wnd == 0) {
766 /* rcv_wnd overflowed */
767 if ((pcb->state == CLOSE_WAIT) || (pcb->state == LAST_ACK)) {
768 /* In passive close, we allow this, since the FIN bit is added to rcv_wnd
769 by the stack itself, since it is not mandatory for an application
770 to call tcp_recved() for the FIN bit, but e.g. the netconn API does so. */
771 pcb->rcv_wnd = TCP_WND_MAX(pcb);
772 } else {
773 LWIP_ASSERT("tcp_recved: len wrapped rcv_wnd\n", 0);
774 }
775 }
776 wnd_inflation = tcp_update_rcv_ann_wnd(pcb);
777
778 /* If the change in the right edge of window is significant (default
779 * watermark is TCP_WND/4), then send an explicit update now.
780 * Otherwise wait for a packet to be sent in the normal course of
781 * events (or more window to be available later) */
782 int tcp_wnd_update_threshold = TCP_WND_UPDATE_THRESHOLD;
783 tcpwnd_size_t tcp_wnd = TCP_WND;
784
785 if(pcb->usr_rcv_wnd != 0) {
786 tcp_wnd = pcb->usr_rcv_wnd;
787 }
788
789 if(lwip_rcv_wnd_flags == WND_FLAGS_SMALL) {
790 tcp_wnd = (tcp_wnd < TCP_SMALL_WND ? tcp_wnd : TCP_SMALL_WND);
791 } else if(lwip_rcv_wnd_flags == WND_FLAGS_LARGE) {
792 tcp_wnd = (tcp_wnd > TCP_LARGE_WND ? tcp_wnd : TCP_LARGE_WND);
793 }
794
795 tcp_wnd_update_threshold = LWIP_MIN((tcp_wnd / 4), (TCP_MSS * 4));
796
797 if (wnd_inflation >= tcp_wnd_update_threshold) {
798 tcp_ack_now(pcb);
799 tcp_output(pcb);
800 }
801
802 LWIP_DEBUGF(TCP_DEBUG, ("tcp_recved: received %"U16_F" bytes, wnd %"TCPWNDSIZE_F" (%"TCPWNDSIZE_F").\n",
803 len, pcb->rcv_wnd, (u16_t)(TCP_WND_MAX(pcb) - pcb->rcv_wnd)));
804 }
805
806 /**
807 * Allocate a new local TCP port.
808 *
809 * @return a new (free) local TCP port number
810 */
811 static u16_t
tcp_new_port(void)812 tcp_new_port(void)
813 {
814 u8_t i;
815 u16_t n = 0;
816 struct tcp_pcb *pcb;
817
818 again:
819 if (tcp_port++ == TCP_LOCAL_PORT_RANGE_END) {
820 tcp_port = TCP_LOCAL_PORT_RANGE_START;
821 }
822 /* Check all PCB lists. */
823 for (i = 0; i < NUM_TCP_PCB_LISTS; i++) {
824 for (pcb = *tcp_pcb_lists[i]; pcb != NULL; pcb = pcb->next) {
825 if (pcb->local_port == tcp_port) {
826 if (++n > (TCP_LOCAL_PORT_RANGE_END - TCP_LOCAL_PORT_RANGE_START)) {
827 return 0;
828 }
829 goto again;
830 }
831 }
832 }
833 return tcp_port;
834 }
835
836 /**
837 * @ingroup tcp_raw
838 * Connects to another host. The function given as the "connected"
839 * argument will be called when the connection has been established.
840 *
841 * @param pcb the tcp_pcb used to establish the connection
842 * @param ipaddr the remote ip address to connect to
843 * @param port the remote tcp port to connect to
844 * @param connected callback function to call when connected (on error,
845 the err calback will be called)
846 * @return ERR_VAL if invalid arguments are given
847 * ERR_OK if connect request has been sent
848 * other err_t values if connect request couldn't be sent
849 */
850 err_t
tcp_connect(struct tcp_pcb * pcb,const ip_addr_t * ipaddr,u16_t port,tcp_connected_fn connected)851 tcp_connect(struct tcp_pcb *pcb, const ip_addr_t *ipaddr, u16_t port,
852 tcp_connected_fn connected)
853 {
854 err_t ret;
855 u32_t iss;
856 u16_t old_local_port;
857 tcpwnd_size_t tcp_wnd = TCP_WND;
858
859 if ((pcb == NULL) || (ipaddr == NULL) || !IP_ADDR_PCB_VERSION_MATCH_EXACT(pcb, ipaddr)) {
860 return ERR_VAL;
861 }
862
863 LWIP_ERROR("tcp_connect: can only connect from state CLOSED", pcb->state == CLOSED, return ERR_ISCONN);
864
865 LWIP_DEBUGF(TCP_DEBUG, ("tcp_connect to port %"U16_F"\n", port));
866 ip_addr_set(&pcb->remote_ip, ipaddr);
867 pcb->remote_port = port;
868
869 /* check if we have a route to the remote host */
870 if (ip_addr_isany(&pcb->local_ip)) {
871 /* no local IP address set, yet. */
872 struct netif *netif;
873 const ip_addr_t *local_ip;
874 ip_route_get_local_ip(&pcb->local_ip, &pcb->remote_ip, netif, local_ip);
875 if ((netif == NULL) || (local_ip == NULL)) {
876 /* Don't even try to send a SYN packet if we have no route
877 since that will fail. */
878 return ERR_RTE;
879 }
880 /* Use the address as local address of the pcb. */
881 ip_addr_copy(pcb->local_ip, *local_ip);
882 }
883
884 old_local_port = pcb->local_port;
885 if (pcb->local_port == 0) {
886 pcb->local_port = tcp_new_port();
887 if (pcb->local_port == 0) {
888 return ERR_BUF;
889 }
890 } else {
891 #if SO_REUSE
892 if (ip_get_option(pcb, SOF_REUSEADDR)) {
893 /* Since SOF_REUSEADDR allows reusing a local address, we have to make sure
894 now that the 5-tuple is unique. */
895 struct tcp_pcb *cpcb;
896 int i;
897 /* Don't check listen- and bound-PCBs, check active- and TIME-WAIT PCBs. */
898 for (i = 2; i < NUM_TCP_PCB_LISTS; i++) {
899 for (cpcb = *tcp_pcb_lists[i]; cpcb != NULL; cpcb = cpcb->next) {
900 if ((cpcb->local_port == pcb->local_port) &&
901 (cpcb->remote_port == port) &&
902 ip_addr_cmp(&cpcb->local_ip, &pcb->local_ip) &&
903 ip_addr_cmp(&cpcb->remote_ip, ipaddr)) {
904 /* linux returns EISCONN here, but ERR_USE should be OK for us */
905 return ERR_USE;
906 }
907 }
908 }
909 }
910 #endif /* SO_REUSE */
911 }
912
913 iss = tcp_next_iss();
914 pcb->rcv_nxt = 0;
915 pcb->snd_nxt = iss;
916 pcb->lastack = iss - 1;
917 pcb->snd_lbb = iss - 1;
918 /* Start with a window that does not need scaling. When window scaling is
919 enabled and used, the window is enlarged when both sides agree on scaling. */
920 if(pcb->usr_rcv_wnd != 0) {
921 tcp_wnd = pcb->usr_rcv_wnd;
922 }
923
924 if(lwip_rcv_wnd_flags == WND_FLAGS_SMALL) {
925 tcp_wnd = (tcp_wnd < TCP_SMALL_WND ? tcp_wnd : TCP_SMALL_WND);
926 } else if(lwip_rcv_wnd_flags == WND_FLAGS_LARGE) {
927 tcp_wnd = (tcp_wnd > TCP_LARGE_WND ? tcp_wnd : TCP_LARGE_WND);
928 }
929
930 pcb->rcv_wnd = pcb->rcv_ann_wnd = TCPWND_MIN16(tcp_wnd);
931 pcb->snd_wnd = tcp_wnd;
932 pcb->ssthresh = tcp_wnd;
933
934 pcb->rcv_ann_right_edge = pcb->rcv_nxt;
935 /* As initial send MSS, we use TCP_MSS but limit it to 536.
936 The send MSS is updated when an MSS option is received. */
937 pcb->mss = INITIAL_MSS;
938 #if TCP_CALCULATE_EFF_SEND_MSS
939 pcb->mss = tcp_eff_send_mss(pcb->mss, &pcb->local_ip, &pcb->remote_ip);
940 #endif /* TCP_CALCULATE_EFF_SEND_MSS */
941 pcb->cwnd = 1;
942 #if LWIP_CALLBACK_API
943 pcb->connected = connected;
944 #else /* LWIP_CALLBACK_API */
945 LWIP_UNUSED_ARG(connected);
946 #endif /* LWIP_CALLBACK_API */
947
948 /* Send a SYN together with the MSS option. */
949 ret = tcp_enqueue_flags(pcb, TCP_SYN);
950 if (ret == ERR_OK) {
951 /* SYN segment was enqueued, changed the pcbs state now */
952 pcb->state = SYN_SENT;
953 if (old_local_port != 0) {
954 TCP_RMV(&tcp_bound_pcbs, pcb);
955 }
956 TCP_REG_ACTIVE(pcb);
957 MIB2_STATS_INC(mib2.tcpactiveopens);
958
959 /* need to know low layer situation */
960 ret = tcp_output(pcb);
961 }
962 return ret;
963 }
964
965 /**
966 * Called every 500 ms and implements the retransmission timer and the timer that
967 * removes PCBs that have been in TIME-WAIT for enough time. It also increments
968 * various timers such as the inactivity timer in each PCB.
969 *
970 * Automatically called from tcp_tmr().
971 */
972 void
tcp_slowtmr(void)973 tcp_slowtmr(void)
974 {
975 struct tcp_pcb *pcb, *prev;
976 tcpwnd_size_t eff_wnd;
977 u8_t pcb_remove; /* flag if a PCB should be removed */
978 u8_t pcb_reset; /* flag if a RST should be sent when removing */
979 err_t err;
980
981 err = ERR_OK;
982
983 ++tcp_ticks;
984 ++tcp_timer_ctr;
985
986 tcp_slowtmr_start:
987 /* Steps through all of the active PCBs. */
988 prev = NULL;
989 pcb = tcp_active_pcbs;
990 if (pcb == NULL) {
991 LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: no active pcbs\n"));
992 }
993 while (pcb != NULL) {
994 LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: processing active pcb\n"));
995 LWIP_ASSERT("tcp_slowtmr: active pcb->state != CLOSED\n", pcb->state != CLOSED);
996 LWIP_ASSERT("tcp_slowtmr: active pcb->state != LISTEN\n", pcb->state != LISTEN);
997 LWIP_ASSERT("tcp_slowtmr: active pcb->state != TIME-WAIT\n", pcb->state != TIME_WAIT);
998 if (pcb->last_timer == tcp_timer_ctr) {
999 /* skip this pcb, we have already processed it */
1000 prev = pcb;
1001 pcb = pcb->next;
1002 continue;
1003 }
1004 pcb->last_timer = tcp_timer_ctr;
1005
1006 pcb_remove = 0;
1007 pcb_reset = 0;
1008
1009 if (pcb->state == SYN_SENT && pcb->nrtx == TCP_SYNMAXRTX) {
1010 ++pcb_remove;
1011 LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: max SYN retries reached\n"));
1012 }
1013 else if (pcb->nrtx == TCP_MAXRTX) {
1014 ++pcb_remove;
1015 LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: max DATA retries reached\n"));
1016 } else {
1017 if (pcb->persist_backoff > 0) {
1018 /* If snd_wnd is zero, use persist timer to send 1 byte probes
1019 * instead of using the standard retransmission mechanism. */
1020 u8_t backoff_cnt = tcp_persist_backoff[pcb->persist_backoff-1];
1021 if (pcb->persist_cnt < backoff_cnt) {
1022 pcb->persist_cnt++;
1023 }
1024 if (pcb->persist_cnt >= backoff_cnt) {
1025 if (tcp_zero_window_probe(pcb) == ERR_OK) {
1026 pcb->persist_cnt = 0;
1027 if (pcb->persist_backoff < sizeof(tcp_persist_backoff)) {
1028 pcb->persist_backoff++;
1029 }
1030 }
1031 }
1032 } else {
1033 /* Increase the retransmission timer if it is running */
1034 if (pcb->rtime >= 0) {
1035 ++pcb->rtime;
1036 }
1037
1038 if (pcb->unacked != NULL && pcb->rtime >= pcb->rto) {
1039 /* Time for a retransmission. */
1040 LWIP_DEBUGF(TCP_RTO_DEBUG, ("tcp_slowtmr: rtime %"S16_F
1041 " pcb->rto %"S16_F"\n",
1042 pcb->rtime, pcb->rto));
1043
1044 /* Double retransmission time-out unless we are trying to
1045 * connect to somebody (i.e., we are in SYN_SENT). */
1046 if (pcb->state != SYN_SENT) {
1047 if(lwip_rto_flags != pcb->adjrto) {
1048 pcb->adjrto = lwip_rto_flags;
1049 if(lwip_rto_flags == RTO_FLAGS_LARGE) {
1050 LWIP_DEBUGF(TCP_RTO_DEBUG, ("%s RTO:2s\n", __func__));
1051 pcb->rto = TCP_LARGE_RTO / TCP_SLOW_INTERVAL; /* lwip's default rto is 3000. */
1052 pcb->sv = TCP_LARGE_RTO / TCP_SLOW_INTERVAL;
1053 }
1054 else {
1055 LWIP_DEBUGF(TCP_RTO_DEBUG, ("%s RTO:1s\n", __func__));
1056 pcb->rto = TCP_SMALL_RTO / TCP_SLOW_INTERVAL; /* lwip's default is 3000. */
1057 pcb->sv = TCP_SMALL_RTO / TCP_SLOW_INTERVAL;
1058 }
1059 }
1060 pcb->rto = ((pcb->sa >> 3) + pcb->sv) << tcp_backoff[pcb->nrtx];
1061 }
1062
1063 /* Reset the retransmission timer. */
1064 pcb->rtime = 0;
1065
1066 /* Reduce congestion window and ssthresh. */
1067 eff_wnd = LWIP_MIN(pcb->cwnd, pcb->snd_wnd);
1068 pcb->ssthresh = eff_wnd >> 1;
1069 if (pcb->ssthresh < (tcpwnd_size_t)(pcb->mss << 1)) {
1070 pcb->ssthresh = (pcb->mss << 1);
1071 }
1072 pcb->cwnd = pcb->mss;
1073 LWIP_DEBUGF(TCP_CWND_DEBUG, ("tcp_slowtmr: cwnd %"TCPWNDSIZE_F
1074 " ssthresh %"TCPWNDSIZE_F"\n",
1075 pcb->cwnd, pcb->ssthresh));
1076
1077 /* The following needs to be called AFTER cwnd is set to one
1078 mss - STJ */
1079 tcp_rexmit_rto(pcb);
1080 }
1081 }
1082 }
1083 /* Check if this PCB has stayed too long in FIN-WAIT-2 */
1084 if (pcb->state == FIN_WAIT_2) {
1085 /* If this PCB is in FIN_WAIT_2 because of SHUT_WR don't let it time out. */
1086 if (pcb->flags & TF_RXCLOSED) {
1087 /* PCB was fully closed (either through close() or SHUT_RDWR):
1088 normal FIN-WAIT timeout handling. */
1089 if ((u32_t)(tcp_ticks - pcb->tmr) >
1090 TCP_FIN_WAIT_TIMEOUT / TCP_SLOW_INTERVAL) {
1091 ++pcb_remove;
1092 LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: removing pcb stuck in FIN-WAIT-2\n"));
1093 }
1094 }
1095 }
1096
1097 /* Check if KEEPALIVE should be sent */
1098 if (ip_get_option(pcb, SOF_KEEPALIVE) &&
1099 ((pcb->state == ESTABLISHED) ||
1100 (pcb->state == CLOSE_WAIT))) {
1101 if ((u32_t)(tcp_ticks - pcb->tmr) >
1102 (pcb->keep_idle + TCP_KEEP_DUR(pcb)) / TCP_SLOW_INTERVAL)
1103 {
1104 LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: KEEPALIVE timeout. Aborting connection to "));
1105 ip_addr_debug_print(TCP_DEBUG, &pcb->remote_ip);
1106 LWIP_DEBUGF(TCP_DEBUG, ("\n"));
1107
1108 ++pcb_remove;
1109 ++pcb_reset;
1110 } else if ((u32_t)(tcp_ticks - pcb->tmr) >
1111 (pcb->keep_idle + pcb->keep_cnt_sent * TCP_KEEP_INTVL(pcb))
1112 / TCP_SLOW_INTERVAL)
1113 {
1114 err = tcp_keepalive(pcb);
1115 if (err == ERR_OK) {
1116 pcb->keep_cnt_sent++;
1117 }
1118 }
1119 }
1120
1121 /* If this PCB has queued out of sequence data, but has been
1122 inactive for too long, will drop the data (it will eventually
1123 be retransmitted). */
1124 #if TCP_QUEUE_OOSEQ
1125 if (pcb->ooseq != NULL &&
1126 (u32_t)tcp_ticks - pcb->tmr >= pcb->rto * TCP_OOSEQ_TIMEOUT) {
1127 tcp_segs_free(pcb->ooseq);
1128 pcb->ooseq = NULL;
1129 LWIP_DEBUGF(TCP_CWND_DEBUG, ("tcp_slowtmr: dropping OOSEQ queued data\n"));
1130 }
1131 #endif /* TCP_QUEUE_OOSEQ */
1132
1133 /* Check if this PCB has stayed too long in SYN-RCVD */
1134 if (pcb->state == SYN_RCVD) {
1135 if ((u32_t)(tcp_ticks - pcb->tmr) >
1136 TCP_SYN_RCVD_TIMEOUT / TCP_SLOW_INTERVAL) {
1137 ++pcb_remove;
1138 LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: removing pcb stuck in SYN-RCVD\n"));
1139 }
1140 }
1141
1142 /* Check if this PCB has stayed too long in LAST-ACK */
1143 if (pcb->state == LAST_ACK) {
1144 if ((u32_t)(tcp_ticks - pcb->tmr) > 2 * TCP_MSL / TCP_SLOW_INTERVAL) {
1145 ++pcb_remove;
1146 LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: removing pcb stuck in LAST-ACK\n"));
1147 }
1148 }
1149
1150 /* If the PCB should be removed, do it. */
1151 if (pcb_remove) {
1152 struct tcp_pcb *pcb2;
1153 #if LWIP_CALLBACK_API
1154 tcp_err_fn err_fn = pcb->errf;
1155 #endif /* LWIP_CALLBACK_API */
1156 void *err_arg;
1157 tcp_pcb_purge(pcb);
1158 /* Remove PCB from tcp_active_pcbs list. */
1159 if (prev != NULL) {
1160 LWIP_ASSERT("tcp_slowtmr: middle tcp != tcp_active_pcbs", pcb != tcp_active_pcbs);
1161 prev->next = pcb->next;
1162 } else {
1163 /* This PCB was the first. */
1164 LWIP_ASSERT("tcp_slowtmr: first pcb == tcp_active_pcbs", tcp_active_pcbs == pcb);
1165 tcp_active_pcbs = pcb->next;
1166 }
1167
1168 if (pcb_reset) {
1169 tcp_rst(pcb->snd_nxt, pcb->rcv_nxt, &pcb->local_ip, &pcb->remote_ip,
1170 pcb->local_port, pcb->remote_port);
1171 }
1172
1173 err_arg = pcb->callback_arg;
1174 pcb2 = pcb;
1175 pcb = pcb->next;
1176 memp_free(MEMP_TCP_PCB, pcb2);
1177
1178 tcp_active_pcbs_changed = 0;
1179 TCP_EVENT_ERR(err_fn, err_arg, ERR_ABRT);
1180 if (tcp_active_pcbs_changed) {
1181 goto tcp_slowtmr_start;
1182 }
1183 } else {
1184 /* get the 'next' element now and work with 'prev' below (in case of abort) */
1185 prev = pcb;
1186 pcb = pcb->next;
1187
1188 if (prev->callback_arg == NULL) {
1189 LWIP_DEBUGF(TCP_DEBUG, ("skip this pcb %p, this pcb will be removed by app\n", prev));
1190 continue;
1191 }
1192
1193 /* We check if we should poll the connection. */
1194 ++prev->polltmr;
1195 if (prev->polltmr >= prev->pollinterval) {
1196 prev->polltmr = 0;
1197 LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: polling application\n"));
1198 tcp_active_pcbs_changed = 0;
1199 TCP_EVENT_POLL(prev, err);
1200 if (tcp_active_pcbs_changed) {
1201 goto tcp_slowtmr_start;
1202 }
1203 /* if err == ERR_ABRT, 'prev' is already deallocated */
1204 if (err == ERR_OK) {
1205 tcp_output(prev);
1206 }
1207 }
1208 }
1209 }
1210
1211
1212 /* Steps through all of the TIME-WAIT PCBs. */
1213 prev = NULL;
1214 pcb = tcp_tw_pcbs;
1215 while (pcb != NULL) {
1216 LWIP_ASSERT("tcp_slowtmr: TIME-WAIT pcb->state == TIME-WAIT", pcb->state == TIME_WAIT);
1217 pcb_remove = 0;
1218
1219 /* Check if this PCB has stayed long enough in TIME-WAIT */
1220 if ((u32_t)(tcp_ticks - pcb->tmr) > 2 * TCP_MSL / TCP_SLOW_INTERVAL) {
1221 ++pcb_remove;
1222 }
1223
1224 /* If the PCB should be removed, do it. */
1225 if (pcb_remove) {
1226 struct tcp_pcb *pcb2;
1227 tcp_pcb_purge(pcb);
1228 /* Remove PCB from tcp_tw_pcbs list. */
1229 if (prev != NULL) {
1230 LWIP_ASSERT("tcp_slowtmr: middle tcp != tcp_tw_pcbs", pcb != tcp_tw_pcbs);
1231 prev->next = pcb->next;
1232 } else {
1233 /* This PCB was the first. */
1234 LWIP_ASSERT("tcp_slowtmr: first pcb == tcp_tw_pcbs", tcp_tw_pcbs == pcb);
1235 tcp_tw_pcbs = pcb->next;
1236 }
1237 pcb2 = pcb;
1238 pcb = pcb->next;
1239 memp_free(MEMP_TCP_PCB, pcb2);
1240 } else {
1241 prev = pcb;
1242 pcb = pcb->next;
1243 }
1244 }
1245 }
1246
1247 /**
1248 * Is called every TCP_FAST_INTERVAL (250 ms) and process data previously
1249 * "refused" by upper layer (application) and sends delayed ACKs.
1250 *
1251 * Automatically called from tcp_tmr().
1252 */
1253 void
tcp_fasttmr(void)1254 tcp_fasttmr(void)
1255 {
1256 struct tcp_pcb *pcb;
1257
1258 ++tcp_timer_ctr;
1259
1260 tcp_fasttmr_start:
1261 pcb = tcp_active_pcbs;
1262
1263 while (pcb != NULL) {
1264 if (pcb->last_timer != tcp_timer_ctr) {
1265 struct tcp_pcb *next;
1266 pcb->last_timer = tcp_timer_ctr;
1267 /* send delayed ACKs */
1268 if (pcb->flags & TF_ACK_DELAY) {
1269 LWIP_DEBUGF(TCP_DEBUG, ("tcp_fasttmr: delayed ACK\n"));
1270 tcp_ack_now(pcb);
1271 tcp_output(pcb);
1272 pcb->flags &= ~(TF_ACK_DELAY | TF_ACK_NOW);
1273 }
1274
1275 next = pcb->next;
1276
1277 /* If there is data which was previously "refused" by upper layer */
1278 if (pcb->refused_data != NULL) {
1279 tcp_active_pcbs_changed = 0;
1280 tcp_process_refused_data(pcb);
1281 if (tcp_active_pcbs_changed) {
1282 /* application callback has changed the pcb list: restart the loop */
1283 goto tcp_fasttmr_start;
1284 }
1285 }
1286 pcb = next;
1287 } else {
1288 pcb = pcb->next;
1289 }
1290 }
1291 }
1292
1293 /** Call tcp_output for all active pcbs that have TF_NAGLEMEMERR set */
1294 void
tcp_txnow(void)1295 tcp_txnow(void)
1296 {
1297 struct tcp_pcb *pcb;
1298
1299 for (pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) {
1300 if (pcb->flags & TF_NAGLEMEMERR) {
1301 tcp_output(pcb);
1302 }
1303 }
1304 }
1305
1306 /** Pass pcb->refused_data to the recv callback */
1307 err_t
tcp_process_refused_data(struct tcp_pcb * pcb)1308 tcp_process_refused_data(struct tcp_pcb *pcb)
1309 {
1310 #if TCP_QUEUE_OOSEQ && LWIP_WND_SCALE
1311 struct pbuf *rest;
1312 while (pcb->refused_data != NULL)
1313 #endif /* TCP_QUEUE_OOSEQ && LWIP_WND_SCALE */
1314 {
1315 err_t err;
1316 u8_t refused_flags = pcb->refused_data->flags;
1317 /* set pcb->refused_data to NULL in case the callback frees it and then
1318 closes the pcb */
1319 struct pbuf *refused_data = pcb->refused_data;
1320 #if TCP_QUEUE_OOSEQ && LWIP_WND_SCALE
1321 pbuf_split_64k(refused_data, &rest);
1322 pcb->refused_data = rest;
1323 #else /* TCP_QUEUE_OOSEQ && LWIP_WND_SCALE */
1324 pcb->refused_data = NULL;
1325 #endif /* TCP_QUEUE_OOSEQ && LWIP_WND_SCALE */
1326 /* Notify again application with data previously received. */
1327 LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_input: notify kept packet\n"));
1328 TCP_EVENT_RECV(pcb, refused_data, ERR_OK, err);
1329 if (err == ERR_OK) {
1330 /* did refused_data include a FIN? */
1331 if (refused_flags & PBUF_FLAG_TCP_FIN
1332 #if TCP_QUEUE_OOSEQ && LWIP_WND_SCALE
1333 && (rest == NULL)
1334 #endif /* TCP_QUEUE_OOSEQ && LWIP_WND_SCALE */
1335 ) {
1336 /* correct rcv_wnd as the application won't call tcp_recved()
1337 for the FIN's seqno */
1338 if (pcb->rcv_wnd != TCP_WND_MAX(pcb)) {
1339 pcb->rcv_wnd++;
1340 }
1341 TCP_EVENT_CLOSED(pcb, err);
1342 if (err == ERR_ABRT) {
1343 return ERR_ABRT;
1344 }
1345 }
1346 } else if (err == ERR_ABRT) {
1347 /* if err == ERR_ABRT, 'pcb' is already deallocated */
1348 /* Drop incoming packets because pcb is "full" (only if the incoming
1349 segment contains data). */
1350 LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_input: drop incoming packets, because pcb is \"full\"\n"));
1351 return ERR_ABRT;
1352 } else {
1353 /* data is still refused, pbuf is still valid (go on for ACK-only packets) */
1354 #if TCP_QUEUE_OOSEQ && LWIP_WND_SCALE
1355 if (rest != NULL) {
1356 pbuf_cat(refused_data, rest);
1357 }
1358 #endif /* TCP_QUEUE_OOSEQ && LWIP_WND_SCALE */
1359 pcb->refused_data = refused_data;
1360 return ERR_INPROGRESS;
1361 }
1362 }
1363 return ERR_OK;
1364 }
1365
1366 /**
1367 * Deallocates a list of TCP segments (tcp_seg structures).
1368 *
1369 * @param seg tcp_seg list of TCP segments to free
1370 */
1371 void
tcp_segs_free(struct tcp_seg * seg)1372 tcp_segs_free(struct tcp_seg *seg)
1373 {
1374 while (seg != NULL) {
1375 struct tcp_seg *next = seg->next;
1376 tcp_seg_free(seg);
1377 seg = next;
1378 }
1379 }
1380
1381 /**
1382 * Frees a TCP segment (tcp_seg structure).
1383 *
1384 * @param seg single tcp_seg to free
1385 */
1386 void
tcp_seg_free(struct tcp_seg * seg)1387 tcp_seg_free(struct tcp_seg *seg)
1388 {
1389 if (seg != NULL) {
1390 if (seg->p != NULL) {
1391 pbuf_free(seg->p);
1392 #if TCP_DEBUG
1393 seg->p = NULL;
1394 #endif /* TCP_DEBUG */
1395 }
1396 memp_free(MEMP_TCP_SEG, seg);
1397 }
1398 }
1399
1400 /**
1401 * Sets the priority of a connection.
1402 *
1403 * @param pcb the tcp_pcb to manipulate
1404 * @param prio new priority
1405 */
1406 void
tcp_setprio(struct tcp_pcb * pcb,u8_t prio)1407 tcp_setprio(struct tcp_pcb *pcb, u8_t prio)
1408 {
1409 pcb->prio = prio;
1410 }
1411
1412 #if TCP_QUEUE_OOSEQ
1413 /**
1414 * Returns a copy of the given TCP segment.
1415 * The pbuf and data are not copied, only the pointers
1416 *
1417 * @param seg the old tcp_seg
1418 * @return a copy of seg
1419 */
1420 struct tcp_seg *
tcp_seg_copy(struct tcp_seg * seg)1421 tcp_seg_copy(struct tcp_seg *seg)
1422 {
1423 struct tcp_seg *cseg;
1424
1425 cseg = (struct tcp_seg *)memp_malloc(MEMP_TCP_SEG);
1426 if (cseg == NULL) {
1427 return NULL;
1428 }
1429 SMEMCPY((u8_t *)cseg, (const u8_t *)seg, sizeof(struct tcp_seg));
1430 pbuf_ref(cseg->p);
1431 return cseg;
1432 }
1433 #endif /* TCP_QUEUE_OOSEQ */
1434
1435 #if LWIP_CALLBACK_API
1436 /**
1437 * Default receive callback that is called if the user didn't register
1438 * a recv callback for the pcb.
1439 */
1440 err_t
tcp_recv_null(void * arg,struct tcp_pcb * pcb,struct pbuf * p,err_t err)1441 tcp_recv_null(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
1442 {
1443 LWIP_UNUSED_ARG(arg);
1444 if (p != NULL) {
1445 tcp_recved(pcb, p->tot_len);
1446 pbuf_free(p);
1447 } else if (err == ERR_OK) {
1448 return tcp_close(pcb);
1449 }
1450 return ERR_OK;
1451 }
1452 #endif /* LWIP_CALLBACK_API */
1453
1454 /**
1455 * Kills the oldest active connection that has the same or lower priority than
1456 * 'prio'.
1457 *
1458 * @param prio minimum priority
1459 */
1460 static void
tcp_kill_prio(u8_t prio)1461 tcp_kill_prio(u8_t prio)
1462 {
1463 struct tcp_pcb *pcb, *inactive;
1464 u32_t inactivity;
1465 u8_t mprio;
1466
1467 mprio = LWIP_MIN(TCP_PRIO_MAX, prio);
1468
1469 /* We kill the oldest active connection that has lower priority than prio. */
1470 inactivity = 0;
1471 inactive = NULL;
1472 for (pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) {
1473 if (pcb->prio <= mprio &&
1474 (u32_t)(tcp_ticks - pcb->tmr) >= inactivity) {
1475 inactivity = tcp_ticks - pcb->tmr;
1476 inactive = pcb;
1477 mprio = pcb->prio;
1478 }
1479 }
1480 if (inactive != NULL) {
1481 LWIP_DEBUGF(TCP_DEBUG, ("tcp_kill_prio: killing oldest PCB %p (%"S32_F")\n",
1482 (void *)inactive, inactivity));
1483 tcp_abort(inactive);
1484 }
1485 }
1486
1487 /**
1488 * Kills the oldest connection that is in specific state.
1489 * Called from tcp_alloc() for LAST_ACK and CLOSING if no more connections are available.
1490 */
1491 static void
tcp_kill_state(enum tcp_state state)1492 tcp_kill_state(enum tcp_state state)
1493 {
1494 struct tcp_pcb *pcb, *inactive;
1495 u32_t inactivity;
1496
1497 LWIP_ASSERT("invalid state", (state == CLOSING) || (state == LAST_ACK));
1498
1499 inactivity = 0;
1500 inactive = NULL;
1501 /* Go through the list of active pcbs and get the oldest pcb that is in state
1502 CLOSING/LAST_ACK. */
1503 for (pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) {
1504 if (pcb->state == state) {
1505 if ((u32_t)(tcp_ticks - pcb->tmr) >= inactivity) {
1506 inactivity = tcp_ticks - pcb->tmr;
1507 inactive = pcb;
1508 }
1509 }
1510 }
1511 if (inactive != NULL) {
1512 LWIP_DEBUGF(TCP_DEBUG, ("tcp_kill_closing: killing oldest %s PCB %p (%"S32_F")\n",
1513 tcp_state_str[state], (void *)inactive, inactivity));
1514 /* Don't send a RST, since no data is lost. */
1515 tcp_abandon(inactive, 0);
1516 }
1517 }
1518
1519 /**
1520 * Kills the oldest connection that is in TIME_WAIT state.
1521 * Called from tcp_alloc() if no more connections are available.
1522 */
1523 static void
tcp_kill_timewait(void)1524 tcp_kill_timewait(void)
1525 {
1526 struct tcp_pcb *pcb, *inactive;
1527 u32_t inactivity;
1528
1529 inactivity = 0;
1530 inactive = NULL;
1531 /* Go through the list of TIME_WAIT pcbs and get the oldest pcb. */
1532 for (pcb = tcp_tw_pcbs; pcb != NULL; pcb = pcb->next) {
1533 if ((u32_t)(tcp_ticks - pcb->tmr) >= inactivity) {
1534 inactivity = tcp_ticks - pcb->tmr;
1535 inactive = pcb;
1536 }
1537 }
1538 if (inactive != NULL) {
1539 LWIP_DEBUGF(TCP_DEBUG, ("tcp_kill_timewait: killing oldest TIME-WAIT PCB %p (%"S32_F")\n",
1540 (void *)inactive, inactivity));
1541 tcp_abort(inactive);
1542 }
1543 }
1544
1545 /**
1546 * Allocate a new tcp_pcb structure.
1547 *
1548 * @param prio priority for the new pcb
1549 * @return a new tcp_pcb that initially is in state CLOSED
1550 */
1551 struct tcp_pcb *
tcp_alloc(u8_t prio)1552 tcp_alloc(u8_t prio)
1553 {
1554 struct tcp_pcb *pcb;
1555 u32_t iss;
1556
1557 pcb = (struct tcp_pcb *)memp_malloc(MEMP_TCP_PCB);
1558 if (pcb == NULL) {
1559 /* Try killing oldest connection in TIME-WAIT. */
1560 LWIP_DEBUGF(TCP_DEBUG, ("tcp_alloc: killing off oldest TIME-WAIT connection\n"));
1561 tcp_kill_timewait();
1562 /* Try to allocate a tcp_pcb again. */
1563 pcb = (struct tcp_pcb *)memp_malloc(MEMP_TCP_PCB);
1564 if (pcb == NULL) {
1565 /* Try killing oldest connection in LAST-ACK (these wouldn't go to TIME-WAIT). */
1566 LWIP_DEBUGF(TCP_DEBUG, ("tcp_alloc: killing off oldest LAST-ACK connection\n"));
1567 tcp_kill_state(LAST_ACK);
1568 /* Try to allocate a tcp_pcb again. */
1569 pcb = (struct tcp_pcb *)memp_malloc(MEMP_TCP_PCB);
1570 if (pcb == NULL) {
1571 /* Try killing oldest connection in CLOSING. */
1572 LWIP_DEBUGF(TCP_DEBUG, ("tcp_alloc: killing off oldest CLOSING connection\n"));
1573 tcp_kill_state(CLOSING);
1574 /* Try to allocate a tcp_pcb again. */
1575 pcb = (struct tcp_pcb *)memp_malloc(MEMP_TCP_PCB);
1576 if (pcb == NULL) {
1577 /* Try killing active connections with lower priority than the new one. */
1578 LWIP_DEBUGF(TCP_DEBUG, ("tcp_alloc: killing connection with prio lower than %d\n", prio));
1579 tcp_kill_prio(prio);
1580 /* Try to allocate a tcp_pcb again. */
1581 pcb = (struct tcp_pcb *)memp_malloc(MEMP_TCP_PCB);
1582 if (pcb != NULL) {
1583 /* adjust err stats: memp_malloc failed multiple times before */
1584 MEMP_STATS_DEC(err, MEMP_TCP_PCB);
1585 }
1586 }
1587 if (pcb != NULL) {
1588 /* adjust err stats: memp_malloc failed multiple times before */
1589 MEMP_STATS_DEC(err, MEMP_TCP_PCB);
1590 }
1591 }
1592 if (pcb != NULL) {
1593 /* adjust err stats: memp_malloc failed multiple times before */
1594 MEMP_STATS_DEC(err, MEMP_TCP_PCB);
1595 }
1596 }
1597 if (pcb != NULL) {
1598 /* adjust err stats: memp_malloc failed above */
1599 MEMP_STATS_DEC(err, MEMP_TCP_PCB);
1600 }
1601 }
1602 if (pcb != NULL) {
1603 /* zero out the whole pcb, so there is no need to initialize members to zero */
1604 memset(pcb, 0, sizeof(struct tcp_pcb));
1605 pcb->prio = prio;
1606 pcb->snd_buf = TCP_SND_BUF;
1607 /* Start with a window that does not need scaling. When window scaling is
1608 enabled and used, the window is enlarged when both sides agree on scaling. */
1609 pcb->usr_rcv_wnd = 0;
1610 if(lwip_rcv_wnd_flags == WND_FLAGS_SMALL) {
1611 pcb->rcv_wnd = pcb->rcv_ann_wnd = TCPWND_MIN16(TCP_SMALL_WND);
1612 } else if(lwip_rcv_wnd_flags == WND_FLAGS_LARGE) {
1613 pcb->rcv_wnd = pcb->rcv_ann_wnd = TCPWND_MIN16(TCP_LARGE_WND);
1614 } else {
1615 pcb->rcv_wnd = pcb->rcv_ann_wnd = TCPWND_MIN16(TCP_WND);
1616 }
1617 pcb->ttl = TCP_TTL;
1618 /* As initial send MSS, we use TCP_MSS but limit it to 536.
1619 The send MSS is updated when an MSS option is received. */
1620 pcb->mss = INITIAL_MSS;
1621 if(lwip_rto_flags == RTO_FLAGS_LARGE) {
1622 LWIP_DEBUGF(TCP_RTO_DEBUG, ("%s RTO:4s\n", __func__));
1623 pcb->rto = TCP_LARGE_RTO / TCP_SLOW_INTERVAL; /* lwip's default is 3000. */
1624 pcb->sv = TCP_LARGE_RTO / TCP_SLOW_INTERVAL;
1625 }
1626 else {
1627 LWIP_DEBUGF(TCP_RTO_DEBUG, ("%s RTO:2s\n", __func__));
1628 pcb->rto = TCP_SMALL_RTO / TCP_SLOW_INTERVAL; /* lwip's default is 3000. */
1629 pcb->sv = TCP_SMALL_RTO / TCP_SLOW_INTERVAL;
1630 }
1631 pcb->adjrto = lwip_rto_flags;
1632 pcb->rtime = -1;
1633 pcb->cwnd = 1;
1634 iss = tcp_next_iss();
1635 pcb->snd_wl2 = iss;
1636 pcb->snd_nxt = iss;
1637 pcb->lastack = iss;
1638 pcb->snd_lbb = iss;
1639 pcb->tmr = tcp_ticks;
1640 pcb->last_timer = tcp_timer_ctr;
1641
1642 #if LWIP_CALLBACK_API
1643 pcb->recv = tcp_recv_null;
1644 #endif /* LWIP_CALLBACK_API */
1645
1646 /* Init KEEPALIVE timer */
1647 pcb->keep_idle = TCP_KEEPIDLE_DEFAULT;
1648
1649 #if LWIP_TCP_KEEPALIVE
1650 pcb->keep_intvl = TCP_KEEPINTVL_DEFAULT;
1651 pcb->keep_cnt = TCP_KEEPCNT_DEFAULT;
1652 #endif /* LWIP_TCP_KEEPALIVE */
1653 }
1654 return pcb;
1655 }
1656
1657 /**
1658 * @ingroup tcp_raw
1659 * Creates a new TCP protocol control block but doesn't place it on
1660 * any of the TCP PCB lists.
1661 * The pcb is not put on any list until binding using tcp_bind().
1662 *
1663 * @internal: Maybe there should be a idle TCP PCB list where these
1664 * PCBs are put on. Port reservation using tcp_bind() is implemented but
1665 * allocated pcbs that are not bound can't be killed automatically if wanting
1666 * to allocate a pcb with higher prio (@see tcp_kill_prio())
1667 *
1668 * @return a new tcp_pcb that initially is in state CLOSED
1669 */
1670 struct tcp_pcb *
tcp_new(void)1671 tcp_new(void)
1672 {
1673 return tcp_alloc(TCP_PRIO_NORMAL);
1674 }
1675
1676 /**
1677 * @ingroup tcp_raw
1678 * Creates a new TCP protocol control block but doesn't
1679 * place it on any of the TCP PCB lists.
1680 * The pcb is not put on any list until binding using tcp_bind().
1681 *
1682 * @param type IP address type, see @ref lwip_ip_addr_type definitions.
1683 * If you want to listen to IPv4 and IPv6 (dual-stack) connections,
1684 * supply @ref IPADDR_TYPE_ANY as argument and bind to @ref IP_ANY_TYPE.
1685 * @return a new tcp_pcb that initially is in state CLOSED
1686 */
1687 struct tcp_pcb *
tcp_new_ip_type(u8_t type)1688 tcp_new_ip_type(u8_t type)
1689 {
1690 struct tcp_pcb * pcb;
1691 pcb = tcp_alloc(TCP_PRIO_NORMAL);
1692 #if LWIP_IPV4 && LWIP_IPV6
1693 if (pcb != NULL) {
1694 IP_SET_TYPE_VAL(pcb->local_ip, type);
1695 IP_SET_TYPE_VAL(pcb->remote_ip, type);
1696 }
1697 #else
1698 LWIP_UNUSED_ARG(type);
1699 #endif /* LWIP_IPV4 && LWIP_IPV6 */
1700 return pcb;
1701 }
1702
1703 /**
1704 * @ingroup tcp_raw
1705 * Used to specify the argument that should be passed callback
1706 * functions.
1707 *
1708 * @param pcb tcp_pcb to set the callback argument
1709 * @param arg void pointer argument to pass to callback functions
1710 */
1711 void
tcp_arg(struct tcp_pcb * pcb,void * arg)1712 tcp_arg(struct tcp_pcb *pcb, void *arg)
1713 {
1714 /* This function is allowed to be called for both listen pcbs and
1715 connection pcbs. */
1716 if (pcb != NULL) {
1717 pcb->callback_arg = arg;
1718 }
1719 }
1720
1721 void
tcp_setrcvwnd(struct tcp_pcb * pcb,u32_t rcvwnd)1722 tcp_setrcvwnd(struct tcp_pcb *pcb, u32_t rcvwnd)
1723 {
1724 /* This function is allowed to set rcv wnd */
1725 if ((pcb != NULL) && (rcvwnd != 0)) {
1726 pcb->usr_rcv_wnd = (tcpwnd_size_t)TCPWND_MIN16(rcvwnd);
1727 }
1728 }
1729
1730 #if LWIP_CALLBACK_API
1731
1732 /**
1733 * @ingroup tcp_raw
1734 * Used to specify the function that should be called when a TCP
1735 * connection receives data.
1736 *
1737 * @param pcb tcp_pcb to set the recv callback
1738 * @param recv callback function to call for this pcb when data is received
1739 */
1740 void
tcp_recv(struct tcp_pcb * pcb,tcp_recv_fn recv)1741 tcp_recv(struct tcp_pcb *pcb, tcp_recv_fn recv)
1742 {
1743 if (pcb != NULL) {
1744 LWIP_ASSERT("invalid socket state for recv callback", pcb->state != LISTEN);
1745 pcb->recv = recv;
1746 }
1747 }
1748
1749 /**
1750 * @ingroup tcp_raw
1751 * Used to specify the function that should be called when TCP data
1752 * has been successfully delivered to the remote host.
1753 *
1754 * @param pcb tcp_pcb to set the sent callback
1755 * @param sent callback function to call for this pcb when data is successfully sent
1756 */
1757 void
tcp_sent(struct tcp_pcb * pcb,tcp_sent_fn sent)1758 tcp_sent(struct tcp_pcb *pcb, tcp_sent_fn sent)
1759 {
1760 if (pcb != NULL) {
1761 LWIP_ASSERT("invalid socket state for sent callback", pcb->state != LISTEN);
1762 pcb->sent = sent;
1763 }
1764 }
1765
1766 /**
1767 * @ingroup tcp_raw
1768 * Used to specify the function that should be called when a fatal error
1769 * has occurred on the connection.
1770 *
1771 * @note The corresponding pcb is already freed when this callback is called!
1772 *
1773 * @param pcb tcp_pcb to set the err callback
1774 * @param err callback function to call for this pcb when a fatal error
1775 * has occurred on the connection
1776 */
1777 void
tcp_err(struct tcp_pcb * pcb,tcp_err_fn err)1778 tcp_err(struct tcp_pcb *pcb, tcp_err_fn err)
1779 {
1780 if (pcb != NULL) {
1781 LWIP_ASSERT("invalid socket state for err callback", pcb->state != LISTEN);
1782 pcb->errf = err;
1783 }
1784 }
1785
1786 /**
1787 * @ingroup tcp_raw
1788 * Used for specifying the function that should be called when a
1789 * LISTENing connection has been connected to another host.
1790 *
1791 * @param pcb tcp_pcb to set the accept callback
1792 * @param accept callback function to call for this pcb when LISTENing
1793 * connection has been connected to another host
1794 */
1795 void
tcp_accept(struct tcp_pcb * pcb,tcp_accept_fn accept)1796 tcp_accept(struct tcp_pcb *pcb, tcp_accept_fn accept)
1797 {
1798 if ((pcb != NULL) && (pcb->state == LISTEN)) {
1799 struct tcp_pcb_listen *lpcb = (struct tcp_pcb_listen*)pcb;
1800 lpcb->accept = accept;
1801 }
1802 }
1803 #endif /* LWIP_CALLBACK_API */
1804
1805
1806 /**
1807 * @ingroup tcp_raw
1808 * Used to specify the function that should be called periodically
1809 * from TCP. The interval is specified in terms of the TCP coarse
1810 * timer interval, which is called twice a second.
1811 *
1812 */
1813 void
tcp_poll(struct tcp_pcb * pcb,tcp_poll_fn poll,u8_t interval)1814 tcp_poll(struct tcp_pcb *pcb, tcp_poll_fn poll, u8_t interval)
1815 {
1816 LWIP_ASSERT("invalid socket state for poll", pcb->state != LISTEN);
1817 #if LWIP_CALLBACK_API
1818 pcb->poll = poll;
1819 #else /* LWIP_CALLBACK_API */
1820 LWIP_UNUSED_ARG(poll);
1821 #endif /* LWIP_CALLBACK_API */
1822 pcb->pollinterval = interval;
1823 }
1824
1825 /**
1826 * Purges a TCP PCB. Removes any buffered data and frees the buffer memory
1827 * (pcb->ooseq, pcb->unsent and pcb->unacked are freed).
1828 *
1829 * @param pcb tcp_pcb to purge. The pcb itself is not deallocated!
1830 */
1831 void
tcp_pcb_purge(struct tcp_pcb * pcb)1832 tcp_pcb_purge(struct tcp_pcb *pcb)
1833 {
1834 if (pcb->state != CLOSED &&
1835 pcb->state != TIME_WAIT &&
1836 pcb->state != LISTEN) {
1837
1838 LWIP_DEBUGF(TCP_DEBUG, ("tcp_pcb_purge\n"));
1839
1840 tcp_backlog_accepted(pcb);
1841
1842 if (pcb->refused_data != NULL) {
1843 LWIP_DEBUGF(TCP_DEBUG, ("tcp_pcb_purge: data left on ->refused_data\n"));
1844 pbuf_free(pcb->refused_data);
1845 pcb->refused_data = NULL;
1846 }
1847 if (pcb->unsent != NULL) {
1848 LWIP_DEBUGF(TCP_DEBUG, ("tcp_pcb_purge: not all data sent\n"));
1849 }
1850 if (pcb->unacked != NULL) {
1851 LWIP_DEBUGF(TCP_DEBUG, ("tcp_pcb_purge: data left on ->unacked\n"));
1852 }
1853 #if TCP_QUEUE_OOSEQ
1854 if (pcb->ooseq != NULL) {
1855 LWIP_DEBUGF(TCP_DEBUG, ("tcp_pcb_purge: data left on ->ooseq\n"));
1856 }
1857 tcp_segs_free(pcb->ooseq);
1858 pcb->ooseq = NULL;
1859 #endif /* TCP_QUEUE_OOSEQ */
1860
1861 /* Stop the retransmission timer as it will expect data on unacked
1862 queue if it fires */
1863 pcb->rtime = -1;
1864
1865 tcp_segs_free(pcb->unsent);
1866 tcp_segs_free(pcb->unacked);
1867 pcb->unacked = pcb->unsent = NULL;
1868 #if TCP_OVERSIZE
1869 pcb->unsent_oversize = 0;
1870 #endif /* TCP_OVERSIZE */
1871 }
1872 }
1873
1874 /**
1875 * Purges the PCB and removes it from a PCB list. Any delayed ACKs are sent first.
1876 *
1877 * @param pcblist PCB list to purge.
1878 * @param pcb tcp_pcb to purge. The pcb itself is NOT deallocated!
1879 */
1880 void
tcp_pcb_remove(struct tcp_pcb ** pcblist,struct tcp_pcb * pcb)1881 tcp_pcb_remove(struct tcp_pcb **pcblist, struct tcp_pcb *pcb)
1882 {
1883 TCP_RMV(pcblist, pcb);
1884
1885 tcp_pcb_purge(pcb);
1886
1887 /* if there is an outstanding delayed ACKs, send it */
1888 if (pcb->state != TIME_WAIT &&
1889 pcb->state != LISTEN &&
1890 pcb->flags & TF_ACK_DELAY) {
1891 pcb->flags |= TF_ACK_NOW;
1892 tcp_output(pcb);
1893 }
1894
1895 if (pcb->state != LISTEN) {
1896 LWIP_ASSERT("unsent segments leaking", pcb->unsent == NULL);
1897 LWIP_ASSERT("unacked segments leaking", pcb->unacked == NULL);
1898 #if TCP_QUEUE_OOSEQ
1899 LWIP_ASSERT("ooseq segments leaking", pcb->ooseq == NULL);
1900 #endif /* TCP_QUEUE_OOSEQ */
1901 }
1902
1903 pcb->state = CLOSED;
1904 /* reset the local port to prevent the pcb from being 'bound' */
1905 pcb->local_port = 0;
1906
1907 LWIP_ASSERT("tcp_pcb_remove: tcp_pcbs_sane()", tcp_pcbs_sane());
1908 }
1909
1910 /**
1911 * Calculates a new initial sequence number for new connections.
1912 *
1913 * @return u32_t pseudo random sequence number
1914 */
1915 u32_t
tcp_next_iss(void)1916 tcp_next_iss(void)
1917 {
1918 static u32_t iss = 6510;
1919
1920 iss += tcp_ticks; /* XXX */
1921 return iss;
1922 }
1923
1924 #if TCP_CALCULATE_EFF_SEND_MSS
1925 /**
1926 * Calculates the effective send mss that can be used for a specific IP address
1927 * by using ip_route to determine the netif used to send to the address and
1928 * calculating the minimum of TCP_MSS and that netif's mtu (if set).
1929 */
1930 u16_t
tcp_eff_send_mss_impl(u16_t sendmss,const ip_addr_t * dest,const ip_addr_t * src)1931 tcp_eff_send_mss_impl(u16_t sendmss, const ip_addr_t *dest
1932 #if LWIP_IPV6 || LWIP_IPV4_SRC_ROUTING
1933 , const ip_addr_t *src
1934 #endif /* LWIP_IPV6 || LWIP_IPV4_SRC_ROUTING */
1935 )
1936 {
1937 u16_t mss_s;
1938 struct netif *outif;
1939 s16_t mtu;
1940
1941 outif = ip_route(src, dest);
1942 #if LWIP_IPV6
1943 #if LWIP_IPV4
1944 if (IP_IS_V6(dest))
1945 #endif /* LWIP_IPV4 */
1946 {
1947 /* First look in destination cache, to see if there is a Path MTU. */
1948 mtu = nd6_get_destination_mtu(ip_2_ip6(dest), outif);
1949 }
1950 #if LWIP_IPV4
1951 else
1952 #endif /* LWIP_IPV4 */
1953 #endif /* LWIP_IPV6 */
1954 #if LWIP_IPV4
1955 {
1956 if (outif == NULL) {
1957 return sendmss;
1958 }
1959 mtu = outif->mtu;
1960 }
1961 #endif /* LWIP_IPV4 */
1962
1963 if (mtu != 0) {
1964 #if LWIP_IPV6
1965 #if LWIP_IPV4
1966 if (IP_IS_V6(dest))
1967 #endif /* LWIP_IPV4 */
1968 {
1969 mss_s = mtu - IP6_HLEN - TCP_HLEN;
1970 }
1971 #if LWIP_IPV4
1972 else
1973 #endif /* LWIP_IPV4 */
1974 #endif /* LWIP_IPV6 */
1975 #if LWIP_IPV4
1976 {
1977 mss_s = mtu - IP_HLEN - TCP_HLEN;
1978 }
1979 #endif /* LWIP_IPV4 */
1980 /* RFC 1122, chap 4.2.2.6:
1981 * Eff.snd.MSS = min(SendMSS+20, MMS_S) - TCPhdrsize - IPoptionsize
1982 * We correct for TCP options in tcp_write(), and don't support IP options.
1983 */
1984 sendmss = LWIP_MIN(sendmss, mss_s);
1985 }
1986 return sendmss;
1987 }
1988 #endif /* TCP_CALCULATE_EFF_SEND_MSS */
1989
1990 /** Helper function for tcp_netif_ip_addr_changed() that iterates a pcb list */
1991 static void
tcp_netif_ip_addr_changed_pcblist(const ip_addr_t * old_addr,struct tcp_pcb * pcb_list)1992 tcp_netif_ip_addr_changed_pcblist(const ip_addr_t* old_addr, struct tcp_pcb* pcb_list)
1993 {
1994 struct tcp_pcb *pcb;
1995 pcb = pcb_list;
1996 while (pcb != NULL) {
1997 /* PCB bound to current local interface address? */
1998 if (ip_addr_cmp(&pcb->local_ip, old_addr)
1999 #if LWIP_AUTOIP
2000 /* connections to link-local addresses must persist (RFC3927 ch. 1.9) */
2001 && (!IP_IS_V4_VAL(pcb->local_ip) || !ip4_addr_islinklocal(ip_2_ip4(&pcb->local_ip)))
2002 #endif /* LWIP_AUTOIP */
2003 ) {
2004 /* this connection must be aborted */
2005 struct tcp_pcb *next = pcb->next;
2006 LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_STATE, ("netif_set_ipaddr: aborting TCP pcb %p\n", (void *)pcb));
2007 tcp_abort(pcb);
2008 pcb = next;
2009 } else {
2010 pcb = pcb->next;
2011 }
2012 }
2013 }
2014
2015 /** This function is called from netif.c when address is changed or netif is removed
2016 *
2017 * @param old_addr IP address of the netif before change
2018 * @param new_addr IP address of the netif after change or NULL if netif has been removed
2019 */
2020 void
tcp_netif_ip_addr_changed(const ip_addr_t * old_addr,const ip_addr_t * new_addr)2021 tcp_netif_ip_addr_changed(const ip_addr_t* old_addr, const ip_addr_t* new_addr)
2022 {
2023 struct tcp_pcb_listen *lpcb, *next;
2024
2025 if (!ip_addr_isany(old_addr)) {
2026 tcp_netif_ip_addr_changed_pcblist(old_addr, tcp_active_pcbs);
2027 tcp_netif_ip_addr_changed_pcblist(old_addr, tcp_bound_pcbs);
2028
2029 if (!ip_addr_isany(new_addr)) {
2030 /* PCB bound to current local interface address? */
2031 for (lpcb = tcp_listen_pcbs.listen_pcbs; lpcb != NULL; lpcb = next) {
2032 next = lpcb->next;
2033 /* PCB bound to current local interface address? */
2034 if (ip_addr_cmp(&lpcb->local_ip, old_addr)) {
2035 /* The PCB is listening to the old ipaddr and
2036 * is set to listen to the new one instead */
2037 ip_addr_copy(lpcb->local_ip, *new_addr);
2038 }
2039 }
2040 }
2041 }
2042 }
2043
2044 const char*
tcp_debug_state_str(enum tcp_state s)2045 tcp_debug_state_str(enum tcp_state s)
2046 {
2047 return tcp_state_str[s];
2048 }
2049
2050 #if TCP_DEBUG || TCP_INPUT_DEBUG || TCP_OUTPUT_DEBUG
2051 /**
2052 * Print a tcp header for debugging purposes.
2053 *
2054 * @param tcphdr pointer to a struct tcp_hdr
2055 */
2056 void
tcp_debug_print(struct tcp_hdr * tcphdr)2057 tcp_debug_print(struct tcp_hdr *tcphdr)
2058 {
2059 LWIP_DEBUGF(TCP_DEBUG, ("TCP header:\n"));
2060 LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n"));
2061 LWIP_DEBUGF(TCP_DEBUG, ("| %5"U16_F" | %5"U16_F" | (src port, dest port)\n",
2062 lwip_ntohs(tcphdr->src), lwip_ntohs(tcphdr->dest)));
2063 LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n"));
2064 LWIP_DEBUGF(TCP_DEBUG, ("| %010"U32_F" | (seq no)\n",
2065 lwip_ntohl(tcphdr->seqno)));
2066 LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n"));
2067 LWIP_DEBUGF(TCP_DEBUG, ("| %010"U32_F" | (ack no)\n",
2068 lwip_ntohl(tcphdr->ackno)));
2069 LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n"));
2070 LWIP_DEBUGF(TCP_DEBUG, ("| %2"U16_F" | |%"U16_F"%"U16_F"%"U16_F"%"U16_F"%"U16_F"%"U16_F"| %5"U16_F" | (hdrlen, flags (",
2071 TCPH_HDRLEN(tcphdr),
2072 (u16_t)(TCPH_FLAGS(tcphdr) >> 5 & 1),
2073 (u16_t)(TCPH_FLAGS(tcphdr) >> 4 & 1),
2074 (u16_t)(TCPH_FLAGS(tcphdr) >> 3 & 1),
2075 (u16_t)(TCPH_FLAGS(tcphdr) >> 2 & 1),
2076 (u16_t)(TCPH_FLAGS(tcphdr) >> 1 & 1),
2077 (u16_t)(TCPH_FLAGS(tcphdr) & 1),
2078 lwip_ntohs(tcphdr->wnd)));
2079 tcp_debug_print_flags(TCPH_FLAGS(tcphdr));
2080 LWIP_DEBUGF(TCP_DEBUG, ("), win)\n"));
2081 LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n"));
2082 LWIP_DEBUGF(TCP_DEBUG, ("| 0x%04"X16_F" | %5"U16_F" | (chksum, urgp)\n",
2083 lwip_ntohs(tcphdr->chksum), lwip_ntohs(tcphdr->urgp)));
2084 LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n"));
2085 }
2086
2087 /**
2088 * Print a tcp state for debugging purposes.
2089 *
2090 * @param s enum tcp_state to print
2091 */
2092 void
tcp_debug_print_state(enum tcp_state s)2093 tcp_debug_print_state(enum tcp_state s)
2094 {
2095 LWIP_DEBUGF(TCP_DEBUG, ("State: %s\n", tcp_state_str[s]));
2096 }
2097
2098 /**
2099 * Print tcp flags for debugging purposes.
2100 *
2101 * @param flags tcp flags, all active flags are printed
2102 */
2103 void
tcp_debug_print_flags(u8_t flags)2104 tcp_debug_print_flags(u8_t flags)
2105 {
2106 if (flags & TCP_FIN) {
2107 LWIP_DEBUGF(TCP_DEBUG, ("FIN "));
2108 }
2109 if (flags & TCP_SYN) {
2110 LWIP_DEBUGF(TCP_DEBUG, ("SYN "));
2111 }
2112 if (flags & TCP_RST) {
2113 LWIP_DEBUGF(TCP_DEBUG, ("RST "));
2114 }
2115 if (flags & TCP_PSH) {
2116 LWIP_DEBUGF(TCP_DEBUG, ("PSH "));
2117 }
2118 if (flags & TCP_ACK) {
2119 LWIP_DEBUGF(TCP_DEBUG, ("ACK "));
2120 }
2121 if (flags & TCP_URG) {
2122 LWIP_DEBUGF(TCP_DEBUG, ("URG "));
2123 }
2124 if (flags & TCP_ECE) {
2125 LWIP_DEBUGF(TCP_DEBUG, ("ECE "));
2126 }
2127 if (flags & TCP_CWR) {
2128 LWIP_DEBUGF(TCP_DEBUG, ("CWR "));
2129 }
2130 LWIP_DEBUGF(TCP_DEBUG, ("\n"));
2131 }
2132
2133 /**
2134 * Print all tcp_pcbs in every list for debugging purposes.
2135 */
2136 void
tcp_debug_print_pcbs(void)2137 tcp_debug_print_pcbs(void)
2138 {
2139 struct tcp_pcb *pcb;
2140 struct tcp_pcb_listen *pcbl;
2141
2142 LWIP_DEBUGF(TCP_DEBUG, ("Active PCB states:\n"));
2143 for (pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) {
2144 LWIP_DEBUGF(TCP_DEBUG, ("Local port %"U16_F", foreign port %"U16_F" snd_nxt %"U32_F" rcv_nxt %"U32_F" ",
2145 pcb->local_port, pcb->remote_port,
2146 pcb->snd_nxt, pcb->rcv_nxt));
2147 tcp_debug_print_state(pcb->state);
2148 }
2149
2150 LWIP_DEBUGF(TCP_DEBUG, ("Listen PCB states:\n"));
2151 for (pcbl = tcp_listen_pcbs.listen_pcbs; pcbl != NULL; pcbl = pcbl->next) {
2152 LWIP_DEBUGF(TCP_DEBUG, ("Local port %"U16_F" ", pcbl->local_port));
2153 tcp_debug_print_state(pcbl->state);
2154 }
2155
2156 LWIP_DEBUGF(TCP_DEBUG, ("TIME-WAIT PCB states:\n"));
2157 for (pcb = tcp_tw_pcbs; pcb != NULL; pcb = pcb->next) {
2158 LWIP_DEBUGF(TCP_DEBUG, ("Local port %"U16_F", foreign port %"U16_F" snd_nxt %"U32_F" rcv_nxt %"U32_F" ",
2159 pcb->local_port, pcb->remote_port,
2160 pcb->snd_nxt, pcb->rcv_nxt));
2161 tcp_debug_print_state(pcb->state);
2162 }
2163 }
2164
2165 /**
2166 * Check state consistency of the tcp_pcb lists.
2167 */
2168 s16_t
tcp_pcbs_sane(void)2169 tcp_pcbs_sane(void)
2170 {
2171 struct tcp_pcb *pcb;
2172 for (pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) {
2173 LWIP_ASSERT("tcp_pcbs_sane: active pcb->state != CLOSED", pcb->state != CLOSED);
2174 LWIP_ASSERT("tcp_pcbs_sane: active pcb->state != LISTEN", pcb->state != LISTEN);
2175 LWIP_ASSERT("tcp_pcbs_sane: active pcb->state != TIME-WAIT", pcb->state != TIME_WAIT);
2176 }
2177 for (pcb = tcp_tw_pcbs; pcb != NULL; pcb = pcb->next) {
2178 LWIP_ASSERT("tcp_pcbs_sane: tw pcb->state == TIME-WAIT", pcb->state == TIME_WAIT);
2179 }
2180 return 1;
2181 }
2182 #endif /* TCP_DEBUG */
2183
2184 #endif /* LWIP_TCP */
2185