STM32F769IDiscovery  1.00
uDANTE Audio Networking with STM32F7 DISCO board
tcp.c
Go to the documentation of this file.
1 
11 /*
12  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
13  * All rights reserved.
14  *
15  * Redistribution and use in source and binary forms, with or without modification,
16  * are permitted provided that the following conditions are met:
17  *
18  * 1. Redistributions of source code must retain the above copyright notice,
19  * this list of conditions and the following disclaimer.
20  * 2. Redistributions in binary form must reproduce the above copyright notice,
21  * this list of conditions and the following disclaimer in the documentation
22  * and/or other materials provided with the distribution.
23  * 3. The name of the author may not be used to endorse or promote products
24  * derived from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
27  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
28  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
29  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
31  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
34  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
35  * OF SUCH DAMAGE.
36  *
37  * This file is part of the lwIP TCP/IP stack.
38  *
39  * Author: Adam Dunkels <adam@sics.se>
40  *
41  */
42 
43 #include "lwip/opt.h"
44 
45 #if LWIP_TCP /* don't build if not configured for use in lwipopts.h */
46 
47 #include "lwip/def.h"
48 #include "lwip/mem.h"
49 #include "lwip/memp.h"
50 #include "lwip/tcp.h"
51 #include "lwip/priv/tcp_priv.h"
52 #include "lwip/debug.h"
53 #include "lwip/stats.h"
54 #include "lwip/ip6.h"
55 #include "lwip/ip6_addr.h"
56 #include "lwip/nd6.h"
57 
58 #include <string.h>
59 
60 #ifndef TCP_LOCAL_PORT_RANGE_START
61 /* From http://www.iana.org/assignments/port-numbers:
62  "The Dynamic and/or Private Ports are those from 49152 through 65535" */
63 #define TCP_LOCAL_PORT_RANGE_START 0xc000
64 #define TCP_LOCAL_PORT_RANGE_END 0xffff
65 #define TCP_ENSURE_LOCAL_PORT_RANGE(port) ((u16_t)(((port) & ~TCP_LOCAL_PORT_RANGE_START) + TCP_LOCAL_PORT_RANGE_START))
66 #endif
67 
68 #if LWIP_TCP_KEEPALIVE
69 #define TCP_KEEP_DUR(pcb) ((pcb)->keep_cnt * (pcb)->keep_intvl)
70 #define TCP_KEEP_INTVL(pcb) ((pcb)->keep_intvl)
71 #else /* LWIP_TCP_KEEPALIVE */
72 #define TCP_KEEP_DUR(pcb) TCP_MAXIDLE
73 #define TCP_KEEP_INTVL(pcb) TCP_KEEPINTVL_DEFAULT
74 #endif /* LWIP_TCP_KEEPALIVE */
75 
76 const char * const tcp_state_str[] = {
77  "CLOSED",
78  "LISTEN",
79  "SYN_SENT",
80  "SYN_RCVD",
81  "ESTABLISHED",
82  "FIN_WAIT_1",
83  "FIN_WAIT_2",
84  "CLOSE_WAIT",
85  "CLOSING",
86  "LAST_ACK",
87  "TIME_WAIT"
88 };
89 
90 /* last local TCP port */
91 static u16_t tcp_port = TCP_LOCAL_PORT_RANGE_START;
92 
93 /* Incremented every coarse grained timer shot (typically every 500 ms). */
94 u32_t tcp_ticks;
95 const u8_t tcp_backoff[13] =
96  { 1, 2, 3, 4, 5, 6, 7, 7, 7, 7, 7, 7, 7};
97  /* Times per slowtmr hits */
98 const u8_t tcp_persist_backoff[7] = { 3, 6, 12, 24, 48, 96, 120 };
99 
100 /* The TCP PCB lists. */
101 
103 struct tcp_pcb *tcp_bound_pcbs;
105 union tcp_listen_pcbs_t tcp_listen_pcbs;
108 struct tcp_pcb *tcp_active_pcbs;
110 struct tcp_pcb *tcp_tw_pcbs;
111 
113 struct tcp_pcb ** const tcp_pcb_lists[] = {&tcp_listen_pcbs.pcbs, &tcp_bound_pcbs,
114  &tcp_active_pcbs, &tcp_tw_pcbs};
115 
116 u8_t tcp_active_pcbs_changed;
117 
119 static u8_t tcp_timer;
120 static u8_t tcp_timer_ctr;
121 static u16_t tcp_new_port(void);
122 
126 void
127 tcp_init(void)
128 {
129 #if LWIP_RANDOMIZE_INITIAL_LOCAL_PORTS && defined(LWIP_RAND)
130  tcp_port = TCP_ENSURE_LOCAL_PORT_RANGE(LWIP_RAND());
131 #endif /* LWIP_RANDOMIZE_INITIAL_LOCAL_PORTS && defined(LWIP_RAND) */
132 }
133 
137 void
138 tcp_tmr(void)
139 {
140  /* Call tcp_fasttmr() every 250 ms */
141  tcp_fasttmr();
142 
143  if (++tcp_timer & 1) {
144  /* Call tcp_slowtmr() every 500 ms, i.e., every other timer
145  tcp_tmr() is called. */
146  tcp_slowtmr();
147  }
148 }
149 
166 static err_t
167 tcp_close_shutdown(struct tcp_pcb *pcb, u8_t rst_on_unacked_data)
168 {
169  err_t err;
170 
171  if (rst_on_unacked_data && ((pcb->state == ESTABLISHED) || (pcb->state == CLOSE_WAIT))) {
172  if ((pcb->refused_data != NULL) || (pcb->rcv_wnd != TCP_WND_MAX(pcb))) {
173  /* Not all data received by application, send RST to tell the remote
174  side about this. */
175  LWIP_ASSERT("pcb->flags & TF_RXCLOSED", pcb->flags & TF_RXCLOSED);
176 
177  /* don't call tcp_abort here: we must not deallocate the pcb since
178  that might not be expected when calling tcp_close */
179  tcp_rst(pcb->snd_nxt, pcb->rcv_nxt, &pcb->local_ip, &pcb->remote_ip,
180  pcb->local_port, pcb->remote_port);
181 
182  tcp_pcb_purge(pcb);
183  TCP_RMV_ACTIVE(pcb);
184  if (pcb->state == ESTABLISHED) {
185  /* move to TIME_WAIT since we close actively */
186  pcb->state = TIME_WAIT;
187  TCP_REG(&tcp_tw_pcbs, pcb);
188  } else {
189  /* CLOSE_WAIT: deallocate the pcb since we already sent a RST for it */
190  if (tcp_input_pcb == pcb) {
191  /* prevent using a deallocated pcb: free it from tcp_input later */
192  tcp_trigger_input_pcb_close();
193  } else {
194  memp_free(MEMP_TCP_PCB, pcb);
195  }
196  }
197  return ERR_OK;
198  }
199  }
200 
201  switch (pcb->state) {
202  case CLOSED:
203  /* Closing a pcb in the CLOSED state might seem erroneous,
204  * however, it is in this state once allocated and as yet unused
205  * and the user needs some way to free it should the need arise.
206  * Calling tcp_close() with a pcb that has already been closed, (i.e. twice)
207  * or for a pcb that has been used and then entered the CLOSED state
208  * is erroneous, but this should never happen as the pcb has in those cases
209  * been freed, and so any remaining handles are bogus. */
210  err = ERR_OK;
211  if (pcb->local_port != 0) {
212  TCP_RMV(&tcp_bound_pcbs, pcb);
213  }
214  memp_free(MEMP_TCP_PCB, pcb);
215  pcb = NULL;
216  break;
217  case LISTEN:
218  err = ERR_OK;
219  tcp_pcb_remove(&tcp_listen_pcbs.pcbs, pcb);
220  memp_free(MEMP_TCP_PCB_LISTEN, pcb);
221  pcb = NULL;
222  break;
223  case SYN_SENT:
224  err = ERR_OK;
225  TCP_PCB_REMOVE_ACTIVE(pcb);
226  memp_free(MEMP_TCP_PCB, pcb);
227  pcb = NULL;
228  MIB2_STATS_INC(mib2.tcpattemptfails);
229  break;
230  case SYN_RCVD:
231  err = tcp_send_fin(pcb);
232  if (err == ERR_OK) {
233  MIB2_STATS_INC(mib2.tcpattemptfails);
234  pcb->state = FIN_WAIT_1;
235  }
236  break;
237  case ESTABLISHED:
238  err = tcp_send_fin(pcb);
239  if (err == ERR_OK) {
240  MIB2_STATS_INC(mib2.tcpestabresets);
241  pcb->state = FIN_WAIT_1;
242  }
243  break;
244  case CLOSE_WAIT:
245  err = tcp_send_fin(pcb);
246  if (err == ERR_OK) {
247  MIB2_STATS_INC(mib2.tcpestabresets);
248  pcb->state = LAST_ACK;
249  }
250  break;
251  default:
252  /* Has already been closed, do nothing. */
253  err = ERR_OK;
254  pcb = NULL;
255  break;
256  }
257 
258  if (pcb != NULL && err == ERR_OK) {
259  /* To ensure all data has been sent when tcp_close returns, we have
260  to make sure tcp_output doesn't fail.
261  Since we don't really have to ensure all data has been sent when tcp_close
262  returns (unsent data is sent from tcp timer functions, also), we don't care
263  for the return value of tcp_output for now. */
264  tcp_output(pcb);
265  }
266  return err;
267 }
268 
283 err_t
284 tcp_close(struct tcp_pcb *pcb)
285 {
286  LWIP_DEBUGF(TCP_DEBUG, ("tcp_close: closing in "));
287  tcp_debug_print_state(pcb->state);
288 
289  if (pcb->state != LISTEN) {
290  /* Set a flag not to receive any more data... */
291  pcb->flags |= TF_RXCLOSED;
292  }
293  /* ... and close */
294  return tcp_close_shutdown(pcb, 1);
295 }
296 
309 err_t
310 tcp_shutdown(struct tcp_pcb *pcb, int shut_rx, int shut_tx)
311 {
312  if (pcb->state == LISTEN) {
313  return ERR_CONN;
314  }
315  if (shut_rx) {
316  /* shut down the receive side: set a flag not to receive any more data... */
317  pcb->flags |= TF_RXCLOSED;
318  if (shut_tx) {
319  /* shutting down the tx AND rx side is the same as closing for the raw API */
320  return tcp_close_shutdown(pcb, 1);
321  }
322  /* ... and free buffered data */
323  if (pcb->refused_data != NULL) {
324  pbuf_free(pcb->refused_data);
325  pcb->refused_data = NULL;
326  }
327  }
328  if (shut_tx) {
329  /* This can't happen twice since if it succeeds, the pcb's state is changed.
330  Only close in these states as the others directly deallocate the PCB */
331  switch (pcb->state) {
332  case SYN_RCVD:
333  case ESTABLISHED:
334  case CLOSE_WAIT:
335  return tcp_close_shutdown(pcb, (u8_t)shut_rx);
336  default:
337  /* Not (yet?) connected, cannot shutdown the TX side as that would bring us
338  into CLOSED state, where the PCB is deallocated. */
339  return ERR_CONN;
340  }
341  }
342  return ERR_OK;
343 }
344 
353 void
354 tcp_abandon(struct tcp_pcb *pcb, int reset)
355 {
356  u32_t seqno, ackno;
357 #if LWIP_CALLBACK_API
358  tcp_err_fn errf;
359 #endif /* LWIP_CALLBACK_API */
360  void *errf_arg;
361 
362  /* pcb->state LISTEN not allowed here */
363  LWIP_ASSERT("don't call tcp_abort/tcp_abandon for listen-pcbs",
364  pcb->state != LISTEN);
365  /* Figure out on which TCP PCB list we are, and remove us. If we
366  are in an active state, call the receive function associated with
367  the PCB with a NULL argument, and send an RST to the remote end. */
368  if (pcb->state == TIME_WAIT) {
369  tcp_pcb_remove(&tcp_tw_pcbs, pcb);
370  memp_free(MEMP_TCP_PCB, pcb);
371  } else {
372  int send_rst = 0;
373  u16_t local_port = 0;
374  seqno = pcb->snd_nxt;
375  ackno = pcb->rcv_nxt;
376 #if LWIP_CALLBACK_API
377  errf = pcb->errf;
378 #endif /* LWIP_CALLBACK_API */
379  errf_arg = pcb->callback_arg;
380  if ((pcb->state == CLOSED) && (pcb->local_port != 0)) {
381  /* bound, not yet opened */
382  TCP_RMV(&tcp_bound_pcbs, pcb);
383  } else {
384  send_rst = reset;
385  local_port = pcb->local_port;
386  TCP_PCB_REMOVE_ACTIVE(pcb);
387  }
388  if (pcb->unacked != NULL) {
389  tcp_segs_free(pcb->unacked);
390  }
391  if (pcb->unsent != NULL) {
392  tcp_segs_free(pcb->unsent);
393  }
394 #if TCP_QUEUE_OOSEQ
395  if (pcb->ooseq != NULL) {
396  tcp_segs_free(pcb->ooseq);
397  }
398 #endif /* TCP_QUEUE_OOSEQ */
399  if (send_rst) {
400  LWIP_DEBUGF(TCP_RST_DEBUG, ("tcp_abandon: sending RST\n"));
401  tcp_rst(seqno, ackno, &pcb->local_ip, &pcb->remote_ip, local_port, pcb->remote_port);
402  }
403  memp_free(MEMP_TCP_PCB, pcb);
404  TCP_EVENT_ERR(errf, errf_arg, ERR_ABRT);
405  }
406 }
407 
418 void
419 tcp_abort(struct tcp_pcb *pcb)
420 {
421  tcp_abandon(pcb, 1);
422 }
423 
438 err_t
439 tcp_bind(struct tcp_pcb *pcb, const ip_addr_t *ipaddr, u16_t port)
440 {
441  int i;
442  int max_pcb_list = NUM_TCP_PCB_LISTS;
443  struct tcp_pcb *cpcb;
444 
445  if ((pcb == NULL) || !IP_ADDR_PCB_VERSION_MATCH(pcb, ipaddr)) {
446  return ERR_VAL;
447  }
448 
449  LWIP_ERROR("tcp_bind: can only bind in state CLOSED", pcb->state == CLOSED, return ERR_VAL);
450 
451 #if SO_REUSE
452  /* Unless the REUSEADDR flag is set,
453  we have to check the pcbs in TIME-WAIT state, also.
454  We do not dump TIME_WAIT pcb's; they can still be matched by incoming
455  packets using both local and remote IP addresses and ports to distinguish.
456  */
457  if (ip_get_option(pcb, SOF_REUSEADDR)) {
458  max_pcb_list = NUM_TCP_PCB_LISTS_NO_TIME_WAIT;
459  }
460 #endif /* SO_REUSE */
461 
462  if (port == 0) {
463  port = tcp_new_port();
464  if (port == 0) {
465  return ERR_BUF;
466  }
467  } else {
468  /* Check if the address already is in use (on all lists) */
469  for (i = 0; i < max_pcb_list; i++) {
470  for (cpcb = *tcp_pcb_lists[i]; cpcb != NULL; cpcb = cpcb->next) {
471  if (cpcb->local_port == port) {
472 #if SO_REUSE
473  /* Omit checking for the same port if both pcbs have REUSEADDR set.
474  For SO_REUSEADDR, the duplicate-check for a 5-tuple is done in
475  tcp_connect. */
476  if (!ip_get_option(pcb, SOF_REUSEADDR) ||
478 #endif /* SO_REUSE */
479  {
480  /* @todo: check accept_any_ip_version */
481  if (IP_PCB_IPVER_EQ(pcb, cpcb) &&
482  (ip_addr_isany(&cpcb->local_ip) ||
483  ip_addr_isany(ipaddr) ||
484  ip_addr_cmp(&cpcb->local_ip, ipaddr))) {
485  return ERR_USE;
486  }
487  }
488  }
489  }
490  }
491  }
492 
493  if (!ip_addr_isany(ipaddr)) {
494  ip_addr_set(&pcb->local_ip, ipaddr);
495  }
496  pcb->local_port = port;
497  TCP_REG(&tcp_bound_pcbs, pcb);
498  LWIP_DEBUGF(TCP_DEBUG, ("tcp_bind: bind to port %"U16_F"\n", port));
499  return ERR_OK;
500 }
501 #if LWIP_CALLBACK_API
502 
505 static err_t
506 tcp_accept_null(void *arg, struct tcp_pcb *pcb, err_t err)
507 {
508  LWIP_UNUSED_ARG(arg);
509  LWIP_UNUSED_ARG(err);
510 
511  tcp_abort(pcb);
512 
513  return ERR_ABRT;
514 }
515 #endif /* LWIP_CALLBACK_API */
516 
531 struct tcp_pcb *
532 tcp_listen_with_backlog(struct tcp_pcb *pcb, u8_t backlog)
533 {
534  struct tcp_pcb_listen *lpcb;
535 
536  LWIP_UNUSED_ARG(backlog);
537  LWIP_ERROR("tcp_listen: pcb already connected", pcb->state == CLOSED, return NULL);
538 
539  /* already listening? */
540  if (pcb->state == LISTEN) {
541  return pcb;
542  }
543 #if SO_REUSE
544  if (ip_get_option(pcb, SOF_REUSEADDR)) {
545  /* Since SOF_REUSEADDR allows reusing a local address before the pcb's usage
546  is declared (listen-/connection-pcb), we have to make sure now that
547  this port is only used once for every local IP. */
548  for (lpcb = tcp_listen_pcbs.listen_pcbs; lpcb != NULL; lpcb = lpcb->next) {
549  if ((lpcb->local_port == pcb->local_port) &&
550  IP_PCB_IPVER_EQ(pcb, lpcb)) {
551  if (ip_addr_cmp(&lpcb->local_ip, &pcb->local_ip)) {
552  /* this address/port is already used */
553  return NULL;
554  }
555  }
556  }
557  }
558 #endif /* SO_REUSE */
559  lpcb = (struct tcp_pcb_listen *)memp_malloc(MEMP_TCP_PCB_LISTEN);
560  if (lpcb == NULL) {
561  return NULL;
562  }
563  lpcb->callback_arg = pcb->callback_arg;
564  lpcb->local_port = pcb->local_port;
565  lpcb->state = LISTEN;
566  lpcb->prio = pcb->prio;
567  lpcb->so_options = pcb->so_options;
568  lpcb->ttl = pcb->ttl;
569  lpcb->tos = pcb->tos;
570 #if LWIP_IPV4 && LWIP_IPV6
571  PCB_ISIPV6(lpcb) = PCB_ISIPV6(pcb);
572  lpcb->accept_any_ip_version = 0;
573 #endif /* LWIP_IPV4 && LWIP_IPV6 */
574  ip_addr_copy(lpcb->local_ip, pcb->local_ip);
575  if (pcb->local_port != 0) {
576  TCP_RMV(&tcp_bound_pcbs, pcb);
577  }
578  memp_free(MEMP_TCP_PCB, pcb);
579 #if LWIP_CALLBACK_API
580  lpcb->accept = tcp_accept_null;
581 #endif /* LWIP_CALLBACK_API */
582 #if TCP_LISTEN_BACKLOG
583  lpcb->accepts_pending = 0;
584  lpcb->backlog = (backlog ? backlog : 1);
585 #endif /* TCP_LISTEN_BACKLOG */
586  TCP_REG(&tcp_listen_pcbs.pcbs, (struct tcp_pcb *)lpcb);
587  return (struct tcp_pcb *)lpcb;
588 }
589 
590 #if LWIP_IPV4 && LWIP_IPV6
591 
595 struct tcp_pcb *
596 tcp_listen_dual_with_backlog(struct tcp_pcb *pcb, u8_t backlog)
597 {
598  struct tcp_pcb *lpcb;
599  struct tcp_pcb_listen *l;
600 
601  if (pcb->local_port != 0) {
602  /* Check that there's noone listening on this port already
603  (don't check the IP address since we'll set it to ANY */
604  for (l = tcp_listen_pcbs.listen_pcbs; l != NULL; l = l->next) {
605  if (l->local_port == pcb->local_port) {
606  /* this port is already used */
607  return NULL;
608  }
609  }
610  }
611 
612  lpcb = tcp_listen_with_backlog(pcb, backlog);
613  if ((lpcb != NULL) &&
614  ip_addr_isany(&pcb->local_ip)) {
615  /* The default behavior is to accept connections on either
616  * IPv4 or IPv6, if not bound. */
617  /* @see NETCONN_FLAG_IPV6_V6ONLY for changing this behavior */
618  ((struct tcp_pcb_listen*)lpcb)->accept_any_ip_version = 1;
619  }
620  return lpcb;
621 }
622 #endif /* LWIP_IPV4 && LWIP_IPV6 */
623 
630 u32_t tcp_update_rcv_ann_wnd(struct tcp_pcb *pcb)
631 {
632  u32_t new_right_edge = pcb->rcv_nxt + pcb->rcv_wnd;
633 
634  if (TCP_SEQ_GEQ(new_right_edge, pcb->rcv_ann_right_edge + LWIP_MIN((TCP_WND / 2), pcb->mss))) {
635  /* we can advertise more window */
636  pcb->rcv_ann_wnd = pcb->rcv_wnd;
637  return new_right_edge - pcb->rcv_ann_right_edge;
638  } else {
639  if (TCP_SEQ_GT(pcb->rcv_nxt, pcb->rcv_ann_right_edge)) {
640  /* Can happen due to other end sending out of advertised window,
641  * but within actual available (but not yet advertised) window */
642  pcb->rcv_ann_wnd = 0;
643  } else {
644  /* keep the right edge of window constant */
645  u32_t new_rcv_ann_wnd = pcb->rcv_ann_right_edge - pcb->rcv_nxt;
646 #if !LWIP_WND_SCALE
647  LWIP_ASSERT("new_rcv_ann_wnd <= 0xffff", new_rcv_ann_wnd <= 0xffff);
648 #endif
649  pcb->rcv_ann_wnd = (tcpwnd_size_t)new_rcv_ann_wnd;
650  }
651  return 0;
652  }
653 }
654 
663 void
664 tcp_recved(struct tcp_pcb *pcb, u16_t len)
665 {
666  int wnd_inflation;
667 
668  /* pcb->state LISTEN not allowed here */
669  LWIP_ASSERT("don't call tcp_recved for listen-pcbs",
670  pcb->state != LISTEN);
671 
672  pcb->rcv_wnd += len;
673  if (pcb->rcv_wnd > TCP_WND_MAX(pcb)) {
674  pcb->rcv_wnd = TCP_WND_MAX(pcb);
675  } else if (pcb->rcv_wnd == 0) {
676  /* rcv_wnd overflowed */
677  if ((pcb->state == CLOSE_WAIT) || (pcb->state == LAST_ACK)) {
678  /* In passive close, we allow this, since the FIN bit is added to rcv_wnd
679  by the stack itself, since it is not mandatory for an application
680  to call tcp_recved() for the FIN bit, but e.g. the netconn API does so. */
681  pcb->rcv_wnd = TCP_WND_MAX(pcb);
682  } else {
683  LWIP_ASSERT("tcp_recved: len wrapped rcv_wnd\n", 0);
684  }
685  }
686 
687  wnd_inflation = tcp_update_rcv_ann_wnd(pcb);
688 
689  /* If the change in the right edge of window is significant (default
690  * watermark is TCP_WND/4), then send an explicit update now.
691  * Otherwise wait for a packet to be sent in the normal course of
692  * events (or more window to be available later) */
693  if (wnd_inflation >= TCP_WND_UPDATE_THRESHOLD) {
694  tcp_ack_now(pcb);
695  tcp_output(pcb);
696  }
697 
698  LWIP_DEBUGF(TCP_DEBUG, ("tcp_recved: received %"U16_F" bytes, wnd %"TCPWNDSIZE_F" (%"TCPWNDSIZE_F").\n",
699  len, pcb->rcv_wnd, TCP_WND_MAX(pcb) - pcb->rcv_wnd));
700 }
701 
707 static u16_t
708 tcp_new_port(void)
709 {
710  u8_t i;
711  u16_t n = 0;
712  struct tcp_pcb *pcb;
713 
714 again:
715  if (tcp_port++ == TCP_LOCAL_PORT_RANGE_END) {
716  tcp_port = TCP_LOCAL_PORT_RANGE_START;
717  }
718  /* Check all PCB lists. */
719  for (i = 0; i < NUM_TCP_PCB_LISTS; i++) {
720  for (pcb = *tcp_pcb_lists[i]; pcb != NULL; pcb = pcb->next) {
721  if (pcb->local_port == tcp_port) {
722  if (++n > (TCP_LOCAL_PORT_RANGE_END - TCP_LOCAL_PORT_RANGE_START)) {
723  return 0;
724  }
725  goto again;
726  }
727  }
728  }
729  return tcp_port;
730 }
731 
745 err_t
746 tcp_connect(struct tcp_pcb *pcb, const ip_addr_t *ipaddr, u16_t port,
747  tcp_connected_fn connected)
748 {
749  err_t ret;
750  u32_t iss;
751  u16_t old_local_port;
752 
753  if ((pcb == NULL) || !IP_ADDR_PCB_VERSION_MATCH(pcb, ipaddr)) {
754  return ERR_VAL;
755  }
756 
757  LWIP_ERROR("tcp_connect: can only connect from state CLOSED", pcb->state == CLOSED, return ERR_ISCONN);
758 
759  LWIP_DEBUGF(TCP_DEBUG, ("tcp_connect to port %"U16_F"\n", port));
760  if (ipaddr != NULL) {
761  ip_addr_set(&pcb->remote_ip, ipaddr);
762  } else {
763  return ERR_VAL;
764  }
765  pcb->remote_port = port;
766 
767  /* check if we have a route to the remote host */
768  if (ip_addr_isany(&pcb->local_ip)) {
769  /* no local IP address set, yet. */
770  struct netif *netif;
771  const ip_addr_t *local_ip;
772  ip_route_get_local_ip(PCB_ISIPV6(pcb), &pcb->local_ip, &pcb->remote_ip, netif, local_ip);
773  if ((netif == NULL) || (local_ip == NULL)) {
774  /* Don't even try to send a SYN packet if we have no route
775  since that will fail. */
776  return ERR_RTE;
777  }
778  /* Use the address as local address of the pcb. */
779  ip_addr_copy(pcb->local_ip, *local_ip);
780  }
781 
782  old_local_port = pcb->local_port;
783  if (pcb->local_port == 0) {
784  pcb->local_port = tcp_new_port();
785  if (pcb->local_port == 0) {
786  return ERR_BUF;
787  }
788  } else {
789 #if SO_REUSE
790  if (ip_get_option(pcb, SOF_REUSEADDR)) {
791  /* Since SOF_REUSEADDR allows reusing a local address, we have to make sure
792  now that the 5-tuple is unique. */
793  struct tcp_pcb *cpcb;
794  int i;
795  /* Don't check listen- and bound-PCBs, check active- and TIME-WAIT PCBs. */
796  for (i = 2; i < NUM_TCP_PCB_LISTS; i++) {
797  for (cpcb = *tcp_pcb_lists[i]; cpcb != NULL; cpcb = cpcb->next) {
798  if ((cpcb->local_port == pcb->local_port) &&
799  (cpcb->remote_port == port) &&
800  IP_PCB_IPVER_EQ(cpcb, pcb) &&
801  ip_addr_cmp(&cpcb->local_ip, &pcb->local_ip) &&
802  ip_addr_cmp(&cpcb->remote_ip, ipaddr)) {
803  /* linux returns EISCONN here, but ERR_USE should be OK for us */
804  return ERR_USE;
805  }
806  }
807  }
808  }
809 #endif /* SO_REUSE */
810  }
811 
812  iss = tcp_next_iss();
813  pcb->rcv_nxt = 0;
814  pcb->snd_nxt = iss;
815  pcb->lastack = iss - 1;
816  pcb->snd_lbb = iss - 1;
817  /* Start with a window that does not need scaling. When window scaling is
818  enabled and used, the window is enlarged when both sides agree on scaling. */
819  pcb->rcv_wnd = pcb->rcv_ann_wnd = TCPWND_MIN16(TCP_WND);
820  pcb->rcv_ann_right_edge = pcb->rcv_nxt;
821  pcb->snd_wnd = TCP_WND;
822  /* As initial send MSS, we use TCP_MSS but limit it to 536.
823  The send MSS is updated when an MSS option is received. */
824  pcb->mss = (TCP_MSS > 536) ? 536 : TCP_MSS;
825 #if TCP_CALCULATE_EFF_SEND_MSS
826  pcb->mss = tcp_eff_send_mss(pcb->mss, &pcb->local_ip, &pcb->remote_ip, PCB_ISIPV6(pcb));
827 #endif /* TCP_CALCULATE_EFF_SEND_MSS */
828  pcb->cwnd = 1;
829  pcb->ssthresh = TCP_WND;
830 #if LWIP_CALLBACK_API
831  pcb->connected = connected;
832 #else /* LWIP_CALLBACK_API */
833  LWIP_UNUSED_ARG(connected);
834 #endif /* LWIP_CALLBACK_API */
835 
836  /* Send a SYN together with the MSS option. */
837  ret = tcp_enqueue_flags(pcb, TCP_SYN);
838  if (ret == ERR_OK) {
839  /* SYN segment was enqueued, changed the pcbs state now */
840  pcb->state = SYN_SENT;
841  if (old_local_port != 0) {
842  TCP_RMV(&tcp_bound_pcbs, pcb);
843  }
844  TCP_REG_ACTIVE(pcb);
845  MIB2_STATS_INC(mib2.tcpactiveopens);
846 
847  tcp_output(pcb);
848  }
849  return ret;
850 }
851 
859 void
860 tcp_slowtmr(void)
861 {
862  struct tcp_pcb *pcb, *prev;
863  tcpwnd_size_t eff_wnd;
864  u8_t pcb_remove; /* flag if a PCB should be removed */
865  u8_t pcb_reset; /* flag if a RST should be sent when removing */
866  err_t err;
867 
868  err = ERR_OK;
869 
870  ++tcp_ticks;
871  ++tcp_timer_ctr;
872 
873 tcp_slowtmr_start:
874  /* Steps through all of the active PCBs. */
875  prev = NULL;
876  pcb = tcp_active_pcbs;
877  if (pcb == NULL) {
878  LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: no active pcbs\n"));
879  }
880  while (pcb != NULL) {
881  LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: processing active pcb\n"));
882  LWIP_ASSERT("tcp_slowtmr: active pcb->state != CLOSED\n", pcb->state != CLOSED);
883  LWIP_ASSERT("tcp_slowtmr: active pcb->state != LISTEN\n", pcb->state != LISTEN);
884  LWIP_ASSERT("tcp_slowtmr: active pcb->state != TIME-WAIT\n", pcb->state != TIME_WAIT);
885  if (pcb->last_timer == tcp_timer_ctr) {
886  /* skip this pcb, we have already processed it */
887  pcb = pcb->next;
888  continue;
889  }
890  pcb->last_timer = tcp_timer_ctr;
891 
892  pcb_remove = 0;
893  pcb_reset = 0;
894 
895  if (pcb->state == SYN_SENT && pcb->nrtx == TCP_SYNMAXRTX) {
896  ++pcb_remove;
897  LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: max SYN retries reached\n"));
898  }
899  else if (pcb->nrtx == TCP_MAXRTX) {
900  ++pcb_remove;
901  LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: max DATA retries reached\n"));
902  } else {
903  if (pcb->persist_backoff > 0) {
904  /* If snd_wnd is zero, use persist timer to send 1 byte probes
905  * instead of using the standard retransmission mechanism. */
906  u8_t backoff_cnt = tcp_persist_backoff[pcb->persist_backoff-1];
907  if (pcb->persist_cnt < backoff_cnt) {
908  pcb->persist_cnt++;
909  }
910  if (pcb->persist_cnt >= backoff_cnt) {
911  if (tcp_zero_window_probe(pcb) == ERR_OK) {
912  pcb->persist_cnt = 0;
913  if (pcb->persist_backoff < sizeof(tcp_persist_backoff)) {
914  pcb->persist_backoff++;
915  }
916  }
917  }
918  } else {
919  /* Increase the retransmission timer if it is running */
920  if (pcb->rtime >= 0) {
921  ++pcb->rtime;
922  }
923 
924  if (pcb->unacked != NULL && pcb->rtime >= pcb->rto) {
925  /* Time for a retransmission. */
926  LWIP_DEBUGF(TCP_RTO_DEBUG, ("tcp_slowtmr: rtime %"S16_F
927  " pcb->rto %"S16_F"\n",
928  pcb->rtime, pcb->rto));
929 
930  /* Double retransmission time-out unless we are trying to
931  * connect to somebody (i.e., we are in SYN_SENT). */
932  if (pcb->state != SYN_SENT) {
933  pcb->rto = ((pcb->sa >> 3) + pcb->sv) << tcp_backoff[pcb->nrtx];
934  }
935 
936  /* Reset the retransmission timer. */
937  pcb->rtime = 0;
938 
939  /* Reduce congestion window and ssthresh. */
940  eff_wnd = LWIP_MIN(pcb->cwnd, pcb->snd_wnd);
941  pcb->ssthresh = eff_wnd >> 1;
942  if (pcb->ssthresh < (tcpwnd_size_t)(pcb->mss << 1)) {
943  pcb->ssthresh = (pcb->mss << 1);
944  }
945  pcb->cwnd = pcb->mss;
946  LWIP_DEBUGF(TCP_CWND_DEBUG, ("tcp_slowtmr: cwnd %"TCPWNDSIZE_F
947  " ssthresh %"TCPWNDSIZE_F"\n",
948  pcb->cwnd, pcb->ssthresh));
949 
950  /* The following needs to be called AFTER cwnd is set to one
951  mss - STJ */
952  tcp_rexmit_rto(pcb);
953  }
954  }
955  }
956  /* Check if this PCB has stayed too long in FIN-WAIT-2 */
957  if (pcb->state == FIN_WAIT_2) {
958  /* If this PCB is in FIN_WAIT_2 because of SHUT_WR don't let it time out. */
959  if (pcb->flags & TF_RXCLOSED) {
960  /* PCB was fully closed (either through close() or SHUT_RDWR):
961  normal FIN-WAIT timeout handling. */
962  if ((u32_t)(tcp_ticks - pcb->tmr) >
963  TCP_FIN_WAIT_TIMEOUT / TCP_SLOW_INTERVAL) {
964  ++pcb_remove;
965  LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: removing pcb stuck in FIN-WAIT-2\n"));
966  }
967  }
968  }
969 
970  /* Check if KEEPALIVE should be sent */
971  if (ip_get_option(pcb, SOF_KEEPALIVE) &&
972  ((pcb->state == ESTABLISHED) ||
973  (pcb->state == CLOSE_WAIT))) {
974  if ((u32_t)(tcp_ticks - pcb->tmr) >
975  (pcb->keep_idle + TCP_KEEP_DUR(pcb)) / TCP_SLOW_INTERVAL)
976  {
977  LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: KEEPALIVE timeout. Aborting connection to "));
978  ip_addr_debug_print(TCP_DEBUG, &pcb->remote_ip);
979  LWIP_DEBUGF(TCP_DEBUG, ("\n"));
980 
981  ++pcb_remove;
982  ++pcb_reset;
983  } else if ((u32_t)(tcp_ticks - pcb->tmr) >
984  (pcb->keep_idle + pcb->keep_cnt_sent * TCP_KEEP_INTVL(pcb))
985  / TCP_SLOW_INTERVAL)
986  {
987  err = tcp_keepalive(pcb);
988  if (err == ERR_OK) {
989  pcb->keep_cnt_sent++;
990  }
991  }
992  }
993 
994  /* If this PCB has queued out of sequence data, but has been
995  inactive for too long, will drop the data (it will eventually
996  be retransmitted). */
997 #if TCP_QUEUE_OOSEQ
998  if (pcb->ooseq != NULL &&
999  (u32_t)tcp_ticks - pcb->tmr >= pcb->rto * TCP_OOSEQ_TIMEOUT) {
1000  tcp_segs_free(pcb->ooseq);
1001  pcb->ooseq = NULL;
1002  LWIP_DEBUGF(TCP_CWND_DEBUG, ("tcp_slowtmr: dropping OOSEQ queued data\n"));
1003  }
1004 #endif /* TCP_QUEUE_OOSEQ */
1005 
1006  /* Check if this PCB has stayed too long in SYN-RCVD */
1007  if (pcb->state == SYN_RCVD) {
1008  if ((u32_t)(tcp_ticks - pcb->tmr) >
1009  TCP_SYN_RCVD_TIMEOUT / TCP_SLOW_INTERVAL) {
1010  ++pcb_remove;
1011  LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: removing pcb stuck in SYN-RCVD\n"));
1012  }
1013  }
1014 
1015  /* Check if this PCB has stayed too long in LAST-ACK */
1016  if (pcb->state == LAST_ACK) {
1017  if ((u32_t)(tcp_ticks - pcb->tmr) > 2 * TCP_MSL / TCP_SLOW_INTERVAL) {
1018  ++pcb_remove;
1019  LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: removing pcb stuck in LAST-ACK\n"));
1020  }
1021  }
1022 
1023  /* If the PCB should be removed, do it. */
1024  if (pcb_remove) {
1025  struct tcp_pcb *pcb2;
1026  tcp_err_fn err_fn;
1027  void *err_arg;
1028  tcp_pcb_purge(pcb);
1029  /* Remove PCB from tcp_active_pcbs list. */
1030  if (prev != NULL) {
1031  LWIP_ASSERT("tcp_slowtmr: middle tcp != tcp_active_pcbs", pcb != tcp_active_pcbs);
1032  prev->next = pcb->next;
1033  } else {
1034  /* This PCB was the first. */
1035  LWIP_ASSERT("tcp_slowtmr: first pcb == tcp_active_pcbs", tcp_active_pcbs == pcb);
1036  tcp_active_pcbs = pcb->next;
1037  }
1038 
1039  if (pcb_reset) {
1040  tcp_rst(pcb->snd_nxt, pcb->rcv_nxt, &pcb->local_ip, &pcb->remote_ip,
1041  pcb->local_port, pcb->remote_port);
1042  }
1043 
1044  err_fn = pcb->errf;
1045  err_arg = pcb->callback_arg;
1046  pcb2 = pcb;
1047  pcb = pcb->next;
1048  memp_free(MEMP_TCP_PCB, pcb2);
1049 
1050  tcp_active_pcbs_changed = 0;
1051  TCP_EVENT_ERR(err_fn, err_arg, ERR_ABRT);
1052  if (tcp_active_pcbs_changed) {
1053  goto tcp_slowtmr_start;
1054  }
1055  } else {
1056  /* get the 'next' element now and work with 'prev' below (in case of abort) */
1057  prev = pcb;
1058  pcb = pcb->next;
1059 
1060  /* We check if we should poll the connection. */
1061  ++prev->polltmr;
1062  if (prev->polltmr >= prev->pollinterval) {
1063  prev->polltmr = 0;
1064  LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: polling application\n"));
1065  tcp_active_pcbs_changed = 0;
1066  TCP_EVENT_POLL(prev, err);
1067  if (tcp_active_pcbs_changed) {
1068  goto tcp_slowtmr_start;
1069  }
1070  /* if err == ERR_ABRT, 'prev' is already deallocated */
1071  if (err == ERR_OK) {
1072  tcp_output(prev);
1073  }
1074  }
1075  }
1076  }
1077 
1078 
1079  /* Steps through all of the TIME-WAIT PCBs. */
1080  prev = NULL;
1081  pcb = tcp_tw_pcbs;
1082  while (pcb != NULL) {
1083  LWIP_ASSERT("tcp_slowtmr: TIME-WAIT pcb->state == TIME-WAIT", pcb->state == TIME_WAIT);
1084  pcb_remove = 0;
1085 
1086  /* Check if this PCB has stayed long enough in TIME-WAIT */
1087  if ((u32_t)(tcp_ticks - pcb->tmr) > 2 * TCP_MSL / TCP_SLOW_INTERVAL) {
1088  ++pcb_remove;
1089  }
1090 
1091  /* If the PCB should be removed, do it. */
1092  if (pcb_remove) {
1093  struct tcp_pcb *pcb2;
1094  tcp_pcb_purge(pcb);
1095  /* Remove PCB from tcp_tw_pcbs list. */
1096  if (prev != NULL) {
1097  LWIP_ASSERT("tcp_slowtmr: middle tcp != tcp_tw_pcbs", pcb != tcp_tw_pcbs);
1098  prev->next = pcb->next;
1099  } else {
1100  /* This PCB was the first. */
1101  LWIP_ASSERT("tcp_slowtmr: first pcb == tcp_tw_pcbs", tcp_tw_pcbs == pcb);
1102  tcp_tw_pcbs = pcb->next;
1103  }
1104  pcb2 = pcb;
1105  pcb = pcb->next;
1106  memp_free(MEMP_TCP_PCB, pcb2);
1107  } else {
1108  prev = pcb;
1109  pcb = pcb->next;
1110  }
1111  }
1112 }
1113 
1120 void
1121 tcp_fasttmr(void)
1122 {
1123  struct tcp_pcb *pcb;
1124 
1125  ++tcp_timer_ctr;
1126 
1127 tcp_fasttmr_start:
1128  pcb = tcp_active_pcbs;
1129 
1130  while (pcb != NULL) {
1131  if (pcb->last_timer != tcp_timer_ctr) {
1132  struct tcp_pcb *next;
1133  pcb->last_timer = tcp_timer_ctr;
1134  /* send delayed ACKs */
1135  if (pcb->flags & TF_ACK_DELAY) {
1136  LWIP_DEBUGF(TCP_DEBUG, ("tcp_fasttmr: delayed ACK\n"));
1137  tcp_ack_now(pcb);
1138  tcp_output(pcb);
1139  pcb->flags &= ~(TF_ACK_DELAY | TF_ACK_NOW);
1140  }
1141 
1142  next = pcb->next;
1143 
1144  /* If there is data which was previously "refused" by upper layer */
1145  if (pcb->refused_data != NULL) {
1146  tcp_active_pcbs_changed = 0;
1147  tcp_process_refused_data(pcb);
1148  if (tcp_active_pcbs_changed) {
1149  /* application callback has changed the pcb list: restart the loop */
1150  goto tcp_fasttmr_start;
1151  }
1152  }
1153  pcb = next;
1154  } else {
1155  pcb = pcb->next;
1156  }
1157  }
1158 }
1159 
1161 void
1162 tcp_txnow(void)
1163 {
1164  struct tcp_pcb *pcb;
1165 
1166  for (pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) {
1167  if (pcb->flags & TF_NAGLEMEMERR) {
1168  tcp_output(pcb);
1169  }
1170  }
1171 }
1172 
1174 err_t
1175 tcp_process_refused_data(struct tcp_pcb *pcb)
1176 {
1177 #if TCP_QUEUE_OOSEQ && LWIP_WND_SCALE
1178  struct pbuf *rest;
1179  while (pcb->refused_data != NULL)
1180 #endif /* TCP_QUEUE_OOSEQ && LWIP_WND_SCALE */
1181  {
1182  err_t err;
1183  u8_t refused_flags = pcb->refused_data->flags;
1184  /* set pcb->refused_data to NULL in case the callback frees it and then
1185  closes the pcb */
1186  struct pbuf *refused_data = pcb->refused_data;
1187 #if TCP_QUEUE_OOSEQ && LWIP_WND_SCALE
1188  pbuf_split_64k(refused_data, &rest);
1189  pcb->refused_data = rest;
1190 #else /* TCP_QUEUE_OOSEQ && LWIP_WND_SCALE */
1191  pcb->refused_data = NULL;
1192 #endif /* TCP_QUEUE_OOSEQ && LWIP_WND_SCALE */
1193  /* Notify again application with data previously received. */
1194  LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_input: notify kept packet\n"));
1195  TCP_EVENT_RECV(pcb, refused_data, ERR_OK, err);
1196  if (err == ERR_OK) {
1197  /* did refused_data include a FIN? */
1198  if (refused_flags & PBUF_FLAG_TCP_FIN
1200  && (rest == NULL)
1201 #endif /* TCP_QUEUE_OOSEQ && LWIP_WND_SCALE */
1202  ) {
1203  /* correct rcv_wnd as the application won't call tcp_recved()
1204  for the FIN's seqno */
1205  if (pcb->rcv_wnd != TCP_WND_MAX(pcb)) {
1206  pcb->rcv_wnd++;
1207  }
1208  TCP_EVENT_CLOSED(pcb, err);
1209  if (err == ERR_ABRT) {
1210  return ERR_ABRT;
1211  }
1212  }
1213  } else if (err == ERR_ABRT) {
1214  /* if err == ERR_ABRT, 'pcb' is already deallocated */
1215  /* Drop incoming packets because pcb is "full" (only if the incoming
1216  segment contains data). */
1217  LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_input: drop incoming packets, because pcb is \"full\"\n"));
1218  return ERR_ABRT;
1219  } else {
1220  /* data is still refused, pbuf is still valid (go on for ACK-only packets) */
1221 #if TCP_QUEUE_OOSEQ && LWIP_WND_SCALE
1222  if (rest != NULL) {
1223  pbuf_cat(refused_data, rest);
1224  }
1225 #endif /* TCP_QUEUE_OOSEQ && LWIP_WND_SCALE */
1226  pcb->refused_data = refused_data;
1227  return ERR_INPROGRESS;
1228  }
1229  }
1230  return ERR_OK;
1231 }
1232 
1238 void
1239 tcp_segs_free(struct tcp_seg *seg)
1240 {
1241  while (seg != NULL) {
1242  struct tcp_seg *next = seg->next;
1243  tcp_seg_free(seg);
1244  seg = next;
1245  }
1246 }
1247 
1253 void
1254 tcp_seg_free(struct tcp_seg *seg)
1255 {
1256  if (seg != NULL) {
1257  if (seg->p != NULL) {
1258  pbuf_free(seg->p);
1259 #if TCP_DEBUG
1260  seg->p = NULL;
1261 #endif /* TCP_DEBUG */
1262  }
1263  memp_free(MEMP_TCP_SEG, seg);
1264  }
1265 }
1266 
1273 void
1274 tcp_setprio(struct tcp_pcb *pcb, u8_t prio)
1275 {
1276  pcb->prio = prio;
1277 }
1278 
1279 #if TCP_QUEUE_OOSEQ
1280 
1287 struct tcp_seg *
1288 tcp_seg_copy(struct tcp_seg *seg)
1289 {
1290  struct tcp_seg *cseg;
1291 
1292  cseg = (struct tcp_seg *)memp_malloc(MEMP_TCP_SEG);
1293  if (cseg == NULL) {
1294  return NULL;
1295  }
1296  SMEMCPY((u8_t *)cseg, (const u8_t *)seg, sizeof(struct tcp_seg));
1297  pbuf_ref(cseg->p);
1298  return cseg;
1299 }
1300 #endif /* TCP_QUEUE_OOSEQ */
1301 
1302 #if LWIP_CALLBACK_API
1303 
1307 err_t
1308 tcp_recv_null(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
1309 {
1310  LWIP_UNUSED_ARG(arg);
1311  if (p != NULL) {
1312  tcp_recved(pcb, p->tot_len);
1313  pbuf_free(p);
1314  } else if (err == ERR_OK) {
1315  return tcp_close(pcb);
1316  }
1317  return ERR_OK;
1318 }
1319 #endif /* LWIP_CALLBACK_API */
1320 
1327 static void
1328 tcp_kill_prio(u8_t prio)
1329 {
1330  struct tcp_pcb *pcb, *inactive;
1331  u32_t inactivity;
1332  u8_t mprio;
1333 
1334  mprio = LWIP_MIN(TCP_PRIO_MAX, prio);
1335 
1336  /* We kill the oldest active connection that has lower priority than prio. */
1337  inactivity = 0;
1338  inactive = NULL;
1339  for (pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) {
1340  if (pcb->prio <= mprio &&
1341  (u32_t)(tcp_ticks - pcb->tmr) >= inactivity) {
1342  inactivity = tcp_ticks - pcb->tmr;
1343  inactive = pcb;
1344  mprio = pcb->prio;
1345  }
1346  }
1347  if (inactive != NULL) {
1348  LWIP_DEBUGF(TCP_DEBUG, ("tcp_kill_prio: killing oldest PCB %p (%"S32_F")\n",
1349  (void *)inactive, inactivity));
1350  tcp_abort(inactive);
1351  }
1352 }
1353 
1358 static void
1359 tcp_kill_state(enum tcp_state state)
1360 {
1361  struct tcp_pcb *pcb, *inactive;
1362  u32_t inactivity;
1363 
1364  LWIP_ASSERT("invalid state", (state == CLOSING) || (state == LAST_ACK));
1365 
1366  inactivity = 0;
1367  inactive = NULL;
1368  /* Go through the list of active pcbs and get the oldest pcb that is in state
1369  CLOSING/LAST_ACK. */
1370  for (pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) {
1371  if (pcb->state == state) {
1372  if ((u32_t)(tcp_ticks - pcb->tmr) >= inactivity) {
1373  inactivity = tcp_ticks - pcb->tmr;
1374  inactive = pcb;
1375  }
1376  }
1377  }
1378  if (inactive != NULL) {
1379  LWIP_DEBUGF(TCP_DEBUG, ("tcp_kill_closing: killing oldest %s PCB %p (%"S32_F")\n",
1380  tcp_state_str[state], (void *)inactive, inactivity));
1381  /* Don't send a RST, since no data is lost. */
1382  tcp_abandon(inactive, 0);
1383  }
1384 }
1385 
1390 static void
1391 tcp_kill_timewait(void)
1392 {
1393  struct tcp_pcb *pcb, *inactive;
1394  u32_t inactivity;
1395 
1396  inactivity = 0;
1397  inactive = NULL;
1398  /* Go through the list of TIME_WAIT pcbs and get the oldest pcb. */
1399  for (pcb = tcp_tw_pcbs; pcb != NULL; pcb = pcb->next) {
1400  if ((u32_t)(tcp_ticks - pcb->tmr) >= inactivity) {
1401  inactivity = tcp_ticks - pcb->tmr;
1402  inactive = pcb;
1403  }
1404  }
1405  if (inactive != NULL) {
1406  LWIP_DEBUGF(TCP_DEBUG, ("tcp_kill_timewait: killing oldest TIME-WAIT PCB %p (%"S32_F")\n",
1407  (void *)inactive, inactivity));
1408  tcp_abort(inactive);
1409  }
1410 }
1411 
1418 struct tcp_pcb *
1419 tcp_alloc(u8_t prio)
1420 {
1421  struct tcp_pcb *pcb;
1422  u32_t iss;
1423 
1424  pcb = (struct tcp_pcb *)memp_malloc(MEMP_TCP_PCB);
1425  if (pcb == NULL) {
1426  /* Try killing oldest connection in TIME-WAIT. */
1427  LWIP_DEBUGF(TCP_DEBUG, ("tcp_alloc: killing off oldest TIME-WAIT connection\n"));
1428  tcp_kill_timewait();
1429  /* Try to allocate a tcp_pcb again. */
1430  pcb = (struct tcp_pcb *)memp_malloc(MEMP_TCP_PCB);
1431  if (pcb == NULL) {
1432  /* Try killing oldest connection in LAST-ACK (these wouldn't go to TIME-WAIT). */
1433  LWIP_DEBUGF(TCP_DEBUG, ("tcp_alloc: killing off oldest LAST-ACK connection\n"));
1434  tcp_kill_state(LAST_ACK);
1435  /* Try to allocate a tcp_pcb again. */
1436  pcb = (struct tcp_pcb *)memp_malloc(MEMP_TCP_PCB);
1437  if (pcb == NULL) {
1438  /* Try killing oldest connection in CLOSING. */
1439  LWIP_DEBUGF(TCP_DEBUG, ("tcp_alloc: killing off oldest CLOSING connection\n"));
1440  tcp_kill_state(CLOSING);
1441  /* Try to allocate a tcp_pcb again. */
1442  pcb = (struct tcp_pcb *)memp_malloc(MEMP_TCP_PCB);
1443  if (pcb == NULL) {
1444  /* Try killing active connections with lower priority than the new one. */
1445  LWIP_DEBUGF(TCP_DEBUG, ("tcp_alloc: killing connection with prio lower than %d\n", prio));
1446  tcp_kill_prio(prio);
1447  /* Try to allocate a tcp_pcb again. */
1448  pcb = (struct tcp_pcb *)memp_malloc(MEMP_TCP_PCB);
1449  if (pcb != NULL) {
1450  /* adjust err stats: memp_malloc failed multiple times before */
1451  MEMP_STATS_DEC(err, MEMP_TCP_PCB);
1452  }
1453  }
1454  if (pcb != NULL) {
1455  /* adjust err stats: memp_malloc failed multiple times before */
1456  MEMP_STATS_DEC(err, MEMP_TCP_PCB);
1457  }
1458  }
1459  if (pcb != NULL) {
1460  /* adjust err stats: memp_malloc failed multiple times before */
1461  MEMP_STATS_DEC(err, MEMP_TCP_PCB);
1462  }
1463  }
1464  if (pcb != NULL) {
1465  /* adjust err stats: memp_malloc failed above */
1466  MEMP_STATS_DEC(err, MEMP_TCP_PCB);
1467  }
1468  }
1469  if (pcb != NULL) {
1470  memset(pcb, 0, sizeof(struct tcp_pcb));
1471  pcb->prio = prio;
1472  pcb->snd_buf = TCP_SND_BUF;
1473  pcb->snd_queuelen = 0;
1474  /* Start with a window that does not need scaling. When window scaling is
1475  enabled and used, the window is enlarged when both sides agree on scaling. */
1476  pcb->rcv_wnd = pcb->rcv_ann_wnd = TCPWND_MIN16(TCP_WND);
1477 #if LWIP_WND_SCALE
1478  /* snd_scale and rcv_scale are zero unless both sides agree to use scaling */
1479  pcb->snd_scale = 0;
1480  pcb->rcv_scale = 0;
1481 #endif
1482  pcb->tos = 0;
1483  pcb->ttl = TCP_TTL;
1484  /* As initial send MSS, we use TCP_MSS but limit it to 536.
1485  The send MSS is updated when an MSS option is received. */
1486  pcb->mss = (TCP_MSS > 536) ? 536 : TCP_MSS;
1487  pcb->rto = 3000 / TCP_SLOW_INTERVAL;
1488  pcb->sa = 0;
1489  pcb->sv = 3000 / TCP_SLOW_INTERVAL;
1490  pcb->rtime = -1;
1491  pcb->cwnd = 1;
1492  iss = tcp_next_iss();
1493  pcb->snd_wl2 = iss;
1494  pcb->snd_nxt = iss;
1495  pcb->lastack = iss;
1496  pcb->snd_lbb = iss;
1497  pcb->tmr = tcp_ticks;
1498  pcb->last_timer = tcp_timer_ctr;
1499 
1500  pcb->polltmr = 0;
1501 
1502 #if LWIP_CALLBACK_API
1503  pcb->recv = tcp_recv_null;
1504 #endif /* LWIP_CALLBACK_API */
1505 
1506  /* Init KEEPALIVE timer */
1507  pcb->keep_idle = TCP_KEEPIDLE_DEFAULT;
1508 
1509 #if LWIP_TCP_KEEPALIVE
1510  pcb->keep_intvl = TCP_KEEPINTVL_DEFAULT;
1511  pcb->keep_cnt = TCP_KEEPCNT_DEFAULT;
1512 #endif /* LWIP_TCP_KEEPALIVE */
1513 
1514  pcb->keep_cnt_sent = 0;
1515  }
1516  return pcb;
1517 }
1518 
1531 struct tcp_pcb *
1532 tcp_new(void)
1533 {
1534  return tcp_alloc(TCP_PRIO_NORMAL);
1535 }
1536 
1537 #if LWIP_IPV6
1538 
1545 struct tcp_pcb *
1546 tcp_new_ip6(void)
1547 {
1548  struct tcp_pcb * pcb;
1549  pcb = tcp_alloc(TCP_PRIO_NORMAL);
1550 #if LWIP_IPV4
1551  ip_set_v6(pcb, 1);
1552 #endif /* LWIP_IPV4 */
1553  return pcb;
1554 }
1555 #endif /* LWIP_IPV6 */
1556 
1564 void
1565 tcp_arg(struct tcp_pcb *pcb, void *arg)
1566 {
1567  /* This function is allowed to be called for both listen pcbs and
1568  connection pcbs. */
1569  pcb->callback_arg = arg;
1570 }
1571 #if LWIP_CALLBACK_API
1572 
1580 void
1581 tcp_recv(struct tcp_pcb *pcb, tcp_recv_fn recv)
1582 {
1583  LWIP_ASSERT("invalid socket state for recv callback", pcb->state != LISTEN);
1584  pcb->recv = recv;
1585 }
1586 
1594 void
1595 tcp_sent(struct tcp_pcb *pcb, tcp_sent_fn sent)
1596 {
1597  LWIP_ASSERT("invalid socket state for sent callback", pcb->state != LISTEN);
1598  pcb->sent = sent;
1599 }
1600 
1609 void
1610 tcp_err(struct tcp_pcb *pcb, tcp_err_fn err)
1611 {
1612  LWIP_ASSERT("invalid socket state for err callback", pcb->state != LISTEN);
1613  pcb->errf = err;
1614 }
1615 
1624 void
1625 tcp_accept(struct tcp_pcb *pcb, tcp_accept_fn accept)
1626 {
1627  /* This function is allowed to be called for both listen pcbs and
1628  connection pcbs. */
1629  pcb->accept = accept;
1630 }
1631 #endif /* LWIP_CALLBACK_API */
1632 
1633 
1640 void
1641 tcp_poll(struct tcp_pcb *pcb, tcp_poll_fn poll, u8_t interval)
1642 {
1643  LWIP_ASSERT("invalid socket state for poll", pcb->state != LISTEN);
1644 #if LWIP_CALLBACK_API
1645  pcb->poll = poll;
1646 #else /* LWIP_CALLBACK_API */
1647  LWIP_UNUSED_ARG(poll);
1648 #endif /* LWIP_CALLBACK_API */
1649  pcb->pollinterval = interval;
1650 }
1651 
1658 void
1659 tcp_pcb_purge(struct tcp_pcb *pcb)
1660 {
1661  if (pcb->state != CLOSED &&
1662  pcb->state != TIME_WAIT &&
1663  pcb->state != LISTEN) {
1664 
1665  LWIP_DEBUGF(TCP_DEBUG, ("tcp_pcb_purge\n"));
1666 
1667 #if TCP_LISTEN_BACKLOG
1668  if (pcb->state == SYN_RCVD) {
1669  /* Need to find the corresponding listen_pcb and decrease its accepts_pending */
1670  struct tcp_pcb_listen *lpcb;
1671  LWIP_ASSERT("tcp_pcb_purge: pcb->state == SYN_RCVD but tcp_listen_pcbs is NULL",
1672  tcp_listen_pcbs.listen_pcbs != NULL);
1673  for (lpcb = tcp_listen_pcbs.listen_pcbs; lpcb != NULL; lpcb = lpcb->next) {
1674  if ((lpcb->local_port == pcb->local_port) &&
1675  IP_PCB_IPVER_EQ(pcb, lpcb) &&
1676  (ip_addr_isany(&lpcb->local_ip) ||
1677  ip_addr_cmp(&pcb->local_ip, &lpcb->local_ip))) {
1678  /* port and address of the listen pcb match the timed-out pcb */
1679  LWIP_ASSERT("tcp_pcb_purge: listen pcb does not have accepts pending",
1680  lpcb->accepts_pending > 0);
1681  lpcb->accepts_pending--;
1682  break;
1683  }
1684  }
1685  }
1686 #endif /* TCP_LISTEN_BACKLOG */
1687 
1688 
1689  if (pcb->refused_data != NULL) {
1690  LWIP_DEBUGF(TCP_DEBUG, ("tcp_pcb_purge: data left on ->refused_data\n"));
1691  pbuf_free(pcb->refused_data);
1692  pcb->refused_data = NULL;
1693  }
1694  if (pcb->unsent != NULL) {
1695  LWIP_DEBUGF(TCP_DEBUG, ("tcp_pcb_purge: not all data sent\n"));
1696  }
1697  if (pcb->unacked != NULL) {
1698  LWIP_DEBUGF(TCP_DEBUG, ("tcp_pcb_purge: data left on ->unacked\n"));
1699  }
1700 #if TCP_QUEUE_OOSEQ
1701  if (pcb->ooseq != NULL) {
1702  LWIP_DEBUGF(TCP_DEBUG, ("tcp_pcb_purge: data left on ->ooseq\n"));
1703  }
1704  tcp_segs_free(pcb->ooseq);
1705  pcb->ooseq = NULL;
1706 #endif /* TCP_QUEUE_OOSEQ */
1707 
1708  /* Stop the retransmission timer as it will expect data on unacked
1709  queue if it fires */
1710  pcb->rtime = -1;
1711 
1712  tcp_segs_free(pcb->unsent);
1713  tcp_segs_free(pcb->unacked);
1714  pcb->unacked = pcb->unsent = NULL;
1715 #if TCP_OVERSIZE
1716  pcb->unsent_oversize = 0;
1717 #endif /* TCP_OVERSIZE */
1718  }
1719 }
1720 
1727 void
1728 tcp_pcb_remove(struct tcp_pcb **pcblist, struct tcp_pcb *pcb)
1729 {
1730  TCP_RMV(pcblist, pcb);
1731 
1732  tcp_pcb_purge(pcb);
1733 
1734  /* if there is an outstanding delayed ACKs, send it */
1735  if (pcb->state != TIME_WAIT &&
1736  pcb->state != LISTEN &&
1737  pcb->flags & TF_ACK_DELAY) {
1738  pcb->flags |= TF_ACK_NOW;
1739  tcp_output(pcb);
1740  }
1741 
1742  if (pcb->state != LISTEN) {
1743  LWIP_ASSERT("unsent segments leaking", pcb->unsent == NULL);
1744  LWIP_ASSERT("unacked segments leaking", pcb->unacked == NULL);
1745 #if TCP_QUEUE_OOSEQ
1746  LWIP_ASSERT("ooseq segments leaking", pcb->ooseq == NULL);
1747 #endif /* TCP_QUEUE_OOSEQ */
1748  }
1749 
1750  pcb->state = CLOSED;
1751  /* reset the local port to prevent the pcb from being 'bound' */
1752  pcb->local_port = 0;
1753 
1754  LWIP_ASSERT("tcp_pcb_remove: tcp_pcbs_sane()", tcp_pcbs_sane());
1755 }
1756 
1762 u32_t
1763 tcp_next_iss(void)
1764 {
1765  static u32_t iss = 6510;
1766 
1767  iss += tcp_ticks; /* XXX */
1768  return iss;
1769 }
1770 
1771 #if TCP_CALCULATE_EFF_SEND_MSS
1772 
1777 u16_t
1778 tcp_eff_send_mss_impl(u16_t sendmss, const ip_addr_t *dest
1779 #if LWIP_IPV6 || LWIP_IPV4_SRC_ROUTING
1780  , const ip_addr_t *src
1781 #endif /* LWIP_IPV6 || LWIP_IPV4_SRC_ROUTING */
1782 #if LWIP_IPV6 && LWIP_IPV4
1783  , u8_t isipv6
1784 #endif /* LWIP_IPV6 && LWIP_IPV4 */
1785  )
1786 {
1787  u16_t mss_s;
1788  struct netif *outif;
1789  s16_t mtu;
1790 
1791  outif = ip_route(isipv6, src, dest);
1792 #if LWIP_IPV6
1793 #if LWIP_IPV4
1794  if (isipv6)
1795 #endif /* LWIP_IPV4 */
1796  {
1797  /* First look in destination cache, to see if there is a Path MTU. */
1798  mtu = nd6_get_destination_mtu(ip_2_ip6(dest), outif);
1799  }
1800 #if LWIP_IPV4
1801  else
1802 #endif /* LWIP_IPV4 */
1803 #endif /* LWIP_IPV6 */
1804 #if LWIP_IPV4
1805  {
1806  if (outif == NULL) {
1807  return sendmss;
1808  }
1809  mtu = outif->mtu;
1810  }
1811 #endif /* LWIP_IPV4 */
1812 
1813  if (mtu != 0) {
1814 #if LWIP_IPV6
1815 #if LWIP_IPV4
1816  if (isipv6)
1817 #endif /* LWIP_IPV4 */
1818  {
1819  mss_s = mtu - IP6_HLEN - TCP_HLEN;
1820  }
1821 #if LWIP_IPV4
1822  else
1823 #endif /* LWIP_IPV4 */
1824 #endif /* LWIP_IPV6 */
1825 #if LWIP_IPV4
1826  {
1827  mss_s = mtu - IP_HLEN - TCP_HLEN;
1828  }
1829 #endif /* LWIP_IPV4 */
1830  /* RFC 1122, chap 4.2.2.6:
1831  * Eff.snd.MSS = min(SendMSS+20, MMS_S) - TCPhdrsize - IPoptionsize
1832  * We correct for TCP options in tcp_write(), and don't support IP options.
1833  */
1834  sendmss = LWIP_MIN(sendmss, mss_s);
1835  }
1836  return sendmss;
1837 }
1838 #endif /* TCP_CALCULATE_EFF_SEND_MSS */
1839 
1840 #if LWIP_IPV4
1841 
1842 static void
1843 tcp_netif_ipv4_addr_changed_pcblist(const ip4_addr_t* old_addr, struct tcp_pcb* pcb_list)
1844 {
1845  struct tcp_pcb *pcb;
1846  pcb = pcb_list;
1847  while (pcb != NULL) {
1848  /* PCB bound to current local interface address? */
1849  if (!IP_IS_V6_VAL(pcb->local_ip) && ip4_addr_cmp(ip_2_ip4(&pcb->local_ip), old_addr)
1850 #if LWIP_AUTOIP
1851  /* connections to link-local addresses must persist (RFC3927 ch. 1.9) */
1852  && !ip4_addr_islinklocal(ip_2_ip4(&pcb->local_ip))
1853 #endif /* LWIP_AUTOIP */
1854  ) {
1855  /* this connection must be aborted */
1856  struct tcp_pcb *next = pcb->next;
1857  LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_STATE, ("netif_set_ipaddr: aborting TCP pcb %p\n", (void *)pcb));
1858  tcp_abort(pcb);
1859  pcb = next;
1860  } else {
1861  pcb = pcb->next;
1862  }
1863  }
1864 }
1865 
1871 void tcp_netif_ipv4_addr_changed(const ip4_addr_t* old_addr, const ip4_addr_t* new_addr)
1872 {
1873  struct tcp_pcb_listen *lpcb, *next;
1874 
1875  tcp_netif_ipv4_addr_changed_pcblist(old_addr, tcp_active_pcbs);
1876  tcp_netif_ipv4_addr_changed_pcblist(old_addr, tcp_bound_pcbs);
1877 
1878  if (!ip4_addr_isany(new_addr)) {
1879  /* PCB bound to current local interface address? */
1880  for (lpcb = tcp_listen_pcbs.listen_pcbs; lpcb != NULL; lpcb = next) {
1881  next = lpcb->next;
1882  /* Is this an IPv4 pcb? */
1883  if (!IP_IS_V6_VAL(lpcb->local_ip)) {
1884  /* PCB bound to current local interface address? */
1885  if ((!(ip4_addr_isany(ip_2_ip4(&lpcb->local_ip)))) &&
1886  (ip4_addr_cmp(ip_2_ip4(&lpcb->local_ip), old_addr))) {
1887  /* The PCB is listening to the old ipaddr and
1888  * is set to listen to the new one instead */
1889  ip_addr_copy_from_ip4(lpcb->local_ip, *new_addr);
1890  }
1891  }
1892  }
1893  }
1894 }
1895 #endif /* LWIP_IPV4 */
1896 
1897 const char*
1898 tcp_debug_state_str(enum tcp_state s)
1899 {
1900  return tcp_state_str[s];
1901 }
1902 
1903 #if TCP_DEBUG || TCP_INPUT_DEBUG || TCP_OUTPUT_DEBUG
1904 
1909 void
1910 tcp_debug_print(struct tcp_hdr *tcphdr)
1911 {
1912  LWIP_DEBUGF(TCP_DEBUG, ("TCP header:\n"));
1913  LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n"));
1914  LWIP_DEBUGF(TCP_DEBUG, ("| %5"U16_F" | %5"U16_F" | (src port, dest port)\n",
1915  ntohs(tcphdr->src), ntohs(tcphdr->dest)));
1916  LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n"));
1917  LWIP_DEBUGF(TCP_DEBUG, ("| %010"U32_F" | (seq no)\n",
1918  ntohl(tcphdr->seqno)));
1919  LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n"));
1920  LWIP_DEBUGF(TCP_DEBUG, ("| %010"U32_F" | (ack no)\n",
1921  ntohl(tcphdr->ackno)));
1922  LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n"));
1923  LWIP_DEBUGF(TCP_DEBUG, ("| %2"U16_F" | |%"U16_F"%"U16_F"%"U16_F"%"U16_F"%"U16_F"%"U16_F"| %5"U16_F" | (hdrlen, flags (",
1924  TCPH_HDRLEN(tcphdr),
1925  TCPH_FLAGS(tcphdr) >> 5 & 1,
1926  TCPH_FLAGS(tcphdr) >> 4 & 1,
1927  TCPH_FLAGS(tcphdr) >> 3 & 1,
1928  TCPH_FLAGS(tcphdr) >> 2 & 1,
1929  TCPH_FLAGS(tcphdr) >> 1 & 1,
1930  TCPH_FLAGS(tcphdr) & 1,
1931  ntohs(tcphdr->wnd)));
1932  tcp_debug_print_flags(TCPH_FLAGS(tcphdr));
1933  LWIP_DEBUGF(TCP_DEBUG, ("), win)\n"));
1934  LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n"));
1935  LWIP_DEBUGF(TCP_DEBUG, ("| 0x%04"X16_F" | %5"U16_F" | (chksum, urgp)\n",
1936  ntohs(tcphdr->chksum), ntohs(tcphdr->urgp)));
1937  LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n"));
1938 }
1939 
1945 void
1946 tcp_debug_print_state(enum tcp_state s)
1947 {
1948  LWIP_DEBUGF(TCP_DEBUG, ("State: %s\n", tcp_state_str[s]));
1949 }
1950 
1956 void
1957 tcp_debug_print_flags(u8_t flags)
1958 {
1959  if (flags & TCP_FIN) {
1960  LWIP_DEBUGF(TCP_DEBUG, ("FIN "));
1961  }
1962  if (flags & TCP_SYN) {
1963  LWIP_DEBUGF(TCP_DEBUG, ("SYN "));
1964  }
1965  if (flags & TCP_RST) {
1966  LWIP_DEBUGF(TCP_DEBUG, ("RST "));
1967  }
1968  if (flags & TCP_PSH) {
1969  LWIP_DEBUGF(TCP_DEBUG, ("PSH "));
1970  }
1971  if (flags & TCP_ACK) {
1972  LWIP_DEBUGF(TCP_DEBUG, ("ACK "));
1973  }
1974  if (flags & TCP_URG) {
1975  LWIP_DEBUGF(TCP_DEBUG, ("URG "));
1976  }
1977  if (flags & TCP_ECE) {
1978  LWIP_DEBUGF(TCP_DEBUG, ("ECE "));
1979  }
1980  if (flags & TCP_CWR) {
1981  LWIP_DEBUGF(TCP_DEBUG, ("CWR "));
1982  }
1983  LWIP_DEBUGF(TCP_DEBUG, ("\n"));
1984 }
1985 
1989 void
1990 tcp_debug_print_pcbs(void)
1991 {
1992  struct tcp_pcb *pcb;
1993  LWIP_DEBUGF(TCP_DEBUG, ("Active PCB states:\n"));
1994  for (pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) {
1995  LWIP_DEBUGF(TCP_DEBUG, ("Local port %"U16_F", foreign port %"U16_F" snd_nxt %"U32_F" rcv_nxt %"U32_F" ",
1996  pcb->local_port, pcb->remote_port,
1997  pcb->snd_nxt, pcb->rcv_nxt));
1998  tcp_debug_print_state(pcb->state);
1999  }
2000  LWIP_DEBUGF(TCP_DEBUG, ("Listen PCB states:\n"));
2001  for (pcb = (struct tcp_pcb *)tcp_listen_pcbs.pcbs; pcb != NULL; pcb = pcb->next) {
2002  LWIP_DEBUGF(TCP_DEBUG, ("Local port %"U16_F", foreign port %"U16_F" snd_nxt %"U32_F" rcv_nxt %"U32_F" ",
2003  pcb->local_port, pcb->remote_port,
2004  pcb->snd_nxt, pcb->rcv_nxt));
2005  tcp_debug_print_state(pcb->state);
2006  }
2007  LWIP_DEBUGF(TCP_DEBUG, ("TIME-WAIT PCB states:\n"));
2008  for (pcb = tcp_tw_pcbs; pcb != NULL; pcb = pcb->next) {
2009  LWIP_DEBUGF(TCP_DEBUG, ("Local port %"U16_F", foreign port %"U16_F" snd_nxt %"U32_F" rcv_nxt %"U32_F" ",
2010  pcb->local_port, pcb->remote_port,
2011  pcb->snd_nxt, pcb->rcv_nxt));
2012  tcp_debug_print_state(pcb->state);
2013  }
2014 }
2015 
2019 s16_t
2020 tcp_pcbs_sane(void)
2021 {
2022  struct tcp_pcb *pcb;
2023  for (pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) {
2024  LWIP_ASSERT("tcp_pcbs_sane: active pcb->state != CLOSED", pcb->state != CLOSED);
2025  LWIP_ASSERT("tcp_pcbs_sane: active pcb->state != LISTEN", pcb->state != LISTEN);
2026  LWIP_ASSERT("tcp_pcbs_sane: active pcb->state != TIME-WAIT", pcb->state != TIME_WAIT);
2027  }
2028  for (pcb = tcp_tw_pcbs; pcb != NULL; pcb = pcb->next) {
2029  LWIP_ASSERT("tcp_pcbs_sane: tw pcb->state == TIME-WAIT", pcb->state == TIME_WAIT);
2030  }
2031  return 1;
2032 }
2033 #endif /* TCP_DEBUG */
2034 
2035 #endif /* LWIP_TCP */
2036 
#define ERR_ISCONN
Definition: err.h:62
#define TCP_MAXRTX
Definition: opt.h:1000
#define TCP_INPUT_DEBUG
Definition: opt.h:2893
#define MIB2_STATS_INC(x)
Definition: stats.h:407
#define ERR_CONN
Definition: err.h:64
#define ip_route_get_local_ip(isipv6, src, dest, netif, ipaddr)
Definition: ip.h:302
#define ERR_USE
Definition: err.h:60
#define SOF_REUSEADDR
Definition: ip.h:118
#define ip_addr_isany(ipaddr)
Definition: ip_addr.h:215
#define IP_PCB_IPVER_EQ(pcb1, pcb2)
Definition: ip.h:87
#define ip_2_ip6(ipaddr)
Definition: ip_addr.h:200
#define U16_F
Definition: cc.h:48
signed short s16_t
Definition: cc.h:41
#define ERR_VAL
Definition: err.h:58
#define TCP_CWND_DEBUG
Definition: opt.h:2915
#define LWIP_IPV4
Definition: opt.h:549
u16_t mtu
Definition: netif.h:263
#define NETIF_DEBUG
Definition: opt.h:2781
#define TCP_RTO_DEBUG
Definition: opt.h:2908
#define SMEMCPY(dst, src, len)
Definition: opt.h:92
void memp_free(memp_t type, void *mem)
Definition: memp.c:399
#define LWIP_RAND()
Definition: cc.h:95
#define TCP_SYNMAXRTX
Definition: opt.h:1007
#define SOF_KEEPALIVE
Definition: ip.h:119
#define ERR_ABRT
Definition: err.h:67
#define S32_F
Definition: cc.h:52
#define IP_ADDR_PCB_VERSION_MATCH(addr, pcb)
Definition: ip_addr.h:153
#define LWIP_DBG_STATE
Definition: debug.h:59
u8_t flags
Definition: pbuf.h:131
#define LWIP_MIN(x, y)
Definition: def.h:50
#define NULL
Definition: usbd_def.h:53
u8_t pbuf_free(struct pbuf *p)
Definition: pbuf.c:652
#define U32_F
Definition: cc.h:51
#define TCP_TTL
Definition: lwipopts.h:110
#define ntohl(x)
Definition: def.h:89
#define PCB_ISIPV6(pcb)
Definition: ip.h:89
#define ip_addr_copy(dest, src)
Definition: ip_addr.h:203
unsigned long u32_t
Definition: cc.h:42
#define ERR_RTE
Definition: err.h:56
#define ERR_OK
Definition: err.h:52
u16_t tot_len
Definition: pbuf.h:122
#define IP_IS_V6_VAL(ipaddr)
Definition: ip_addr.h:196
Definition: pbuf.h:108
s8_t err_t
Definition: err.h:47
#define TCP_WND
Definition: lwipopts.h:128
#define ip_addr_cmp(addr1, addr2)
Definition: ip_addr.h:214
#define LWIP_ASSERT(message, assertion)
Definition: debug.h:70
#define ip_addr_set(dest, src)
Definition: ip_addr.h:205
Definition: netif.h:182
#define ip_addr_debug_print(debug, ipaddr)
Definition: ip_addr.h:221
void pbuf_ref(struct pbuf *p)
Definition: pbuf.c:757
#define TCP_DEBUG
Definition: opt.h:2886
#define LWIP_WND_SCALE
Definition: opt.h:1165
#define LWIP_AUTOIP
Definition: opt.h:802
#define S16_F
Definition: cc.h:49
#define PBUF_FLAG_TCP_FIN
Definition: pbuf.h:106
#define TCP_SND_BUF
Definition: lwipopts.h:120
void pbuf_cat(struct pbuf *h, struct pbuf *t)
Definition: pbuf.c:779
unsigned char u8_t
Definition: cc.h:38
#define TCP_MSS
Definition: lwipopts.h:117
ip6_addr_t ip_addr_t
Definition: ip_addr.h:194
#define TCP_WND_UPDATE_THRESHOLD
Definition: opt.h:1141
#define LWIP_ERROR(message, expression, handler)
Definition: debug.h:89
#define MEMP_STATS_DEC(x, i)
Definition: stats.h:346
#define TCP_QUEUE_OOSEQ
Definition: lwipopts.h:114
#define TCP_RST_DEBUG
Definition: opt.h:2936
#define ERR_INPROGRESS
Definition: err.h:57
void * memp_malloc(memp_t type)
Definition: memp.c:303
#define LWIP_DEBUGF(debug, message)
Definition: debug.h:113
#define LWIP_UNUSED_ARG(x)
Definition: arch.h:89
#define LWIP_IPV6
Definition: opt.h:2444
#define ntohs(x)
Definition: def.h:87
unsigned short u16_t
Definition: cc.h:40
#define ip_get_option(pcb, opt)
Definition: ip.h:241
#define X16_F
Definition: cc.h:50
#define ERR_BUF
Definition: err.h:54