UbixOS V2  2.0
tcp.c
Go to the documentation of this file.
1 
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 "net/opt.h"
49 
50 #if LWIP_TCP /* don't build if not configured for use in lwipopts.h */
51 
52 #include "net/def.h"
53 #include "net/mem.h"
54 #include "net/memp.h"
55 #include "net/tcp.h"
56 #include "net/priv/tcp_priv.h"
57 #include "net/debug.h"
58 #include "net/stats.h"
59 #include "net/ip6.h"
60 #include "net/ip6_addr.h"
61 #include "net/nd6.h"
62 
63 #include <string.h>
64 
65 #ifdef LWIP_HOOK_FILENAME
66 #include LWIP_HOOK_FILENAME
67 #endif
68 
69 #ifndef TCP_LOCAL_PORT_RANGE_START
70 /* From http://www.iana.org/assignments/port-numbers:
71  "The Dynamic and/or Private Ports are those from 49152 through 65535" */
72 #define TCP_LOCAL_PORT_RANGE_START 0xc000
73 #define TCP_LOCAL_PORT_RANGE_END 0xffff
74 #define TCP_ENSURE_LOCAL_PORT_RANGE(port) ((u16_t)(((port) & ~TCP_LOCAL_PORT_RANGE_START) + TCP_LOCAL_PORT_RANGE_START))
75 #endif
76 
77 #if LWIP_TCP_KEEPALIVE
78 #define TCP_KEEP_DUR(pcb) ((pcb)->keep_cnt * (pcb)->keep_intvl)
79 #define TCP_KEEP_INTVL(pcb) ((pcb)->keep_intvl)
80 #else /* LWIP_TCP_KEEPALIVE */
81 #define TCP_KEEP_DUR(pcb) TCP_MAXIDLE
82 #define TCP_KEEP_INTVL(pcb) TCP_KEEPINTVL_DEFAULT
83 #endif /* LWIP_TCP_KEEPALIVE */
84 
85 /* As initial send MSS, we use TCP_MSS but limit it to 536. */
86 #if TCP_MSS > 536
87 #define INITIAL_MSS 536
88 #else
89 #define INITIAL_MSS TCP_MSS
90 #endif
91 
92 static const char * const tcp_state_str[] = {
93  "CLOSED",
94  "LISTEN",
95  "SYN_SENT",
96  "SYN_RCVD",
97  "ESTABLISHED",
98  "FIN_WAIT_1",
99  "FIN_WAIT_2",
100  "CLOSE_WAIT",
101  "CLOSING",
102  "LAST_ACK",
103  "TIME_WAIT"
104 };
105 
106 /* last local TCP port */
107 static u16_t tcp_port = TCP_LOCAL_PORT_RANGE_START;
108 
109 /* Incremented every coarse grained timer shot (typically every 500 ms). */
110 u32_t tcp_ticks;
111 static const u8_t tcp_backoff[13] =
112  { 1, 2, 3, 4, 5, 6, 7, 7, 7, 7, 7, 7, 7};
113  /* Times per slowtmr hits */
114 static const u8_t tcp_persist_backoff[7] = { 3, 6, 12, 24, 48, 96, 120 };
115 
116 /* The TCP PCB lists. */
117 
119 struct tcp_pcb *tcp_bound_pcbs;
121 union tcp_listen_pcbs_t tcp_listen_pcbs;
124 struct tcp_pcb *tcp_active_pcbs;
126 struct tcp_pcb *tcp_tw_pcbs;
127 
129 struct tcp_pcb ** const tcp_pcb_lists[] = {&tcp_listen_pcbs.pcbs, &tcp_bound_pcbs,
130  &tcp_active_pcbs, &tcp_tw_pcbs};
131 
132 u8_t tcp_active_pcbs_changed;
133 
135 static u8_t tcp_timer;
136 static u8_t tcp_timer_ctr;
137 static u16_t tcp_new_port(void);
138 
139 static err_t tcp_close_shutdown_fin(struct tcp_pcb *pcb);
140 
144 void
145 tcp_init(void)
146 {
147 #if LWIP_RANDOMIZE_INITIAL_LOCAL_PORTS && defined(LWIP_RAND)
148  tcp_port = TCP_ENSURE_LOCAL_PORT_RANGE(LWIP_RAND());
149 #endif /* LWIP_RANDOMIZE_INITIAL_LOCAL_PORTS && defined(LWIP_RAND) */
150 }
151 
155 void
156 tcp_tmr(void)
157 {
158  /* Call tcp_fasttmr() every 250 ms */
159  tcp_fasttmr();
160 
161  if (++tcp_timer & 1) {
162  /* Call tcp_slowtmr() every 500 ms, i.e., every other timer
163  tcp_tmr() is called. */
164  tcp_slowtmr();
165  }
166 }
167 
168 #if LWIP_CALLBACK_API || TCP_LISTEN_BACKLOG
169 
172 static void
173 tcp_remove_listener(struct tcp_pcb *list, struct tcp_pcb_listen *lpcb)
174 {
175  struct tcp_pcb *pcb;
176  for (pcb = list; pcb != NULL; pcb = pcb->next) {
177  if (pcb->listener == lpcb) {
178  pcb->listener = NULL;
179  }
180  }
181 }
182 #endif
183 
187 static void
188 tcp_listen_closed(struct tcp_pcb *pcb)
189 {
190 #if LWIP_CALLBACK_API || TCP_LISTEN_BACKLOG
191  size_t i;
192  LWIP_ASSERT("pcb != NULL", pcb != NULL);
193  LWIP_ASSERT("pcb->state == LISTEN", pcb->state == LISTEN);
194  for (i = 1; i < LWIP_ARRAYSIZE(tcp_pcb_lists); i++) {
195  tcp_remove_listener(*tcp_pcb_lists[i], (struct tcp_pcb_listen*)pcb);
196  }
197 #endif
198  LWIP_UNUSED_ARG(pcb);
199 }
200 
201 #if TCP_LISTEN_BACKLOG
202 
212 void
213 tcp_backlog_delayed(struct tcp_pcb* pcb)
214 {
215  LWIP_ASSERT("pcb != NULL", pcb != NULL);
216  if ((pcb->flags & TF_BACKLOGPEND) == 0) {
217  if (pcb->listener != NULL) {
218  pcb->listener->accepts_pending++;
219  LWIP_ASSERT("accepts_pending != 0", pcb->listener->accepts_pending != 0);
220  pcb->flags |= TF_BACKLOGPEND;
221  }
222  }
223 }
224 
234 void
235 tcp_backlog_accepted(struct tcp_pcb* pcb)
236 {
237  LWIP_ASSERT("pcb != NULL", pcb != NULL);
238  if ((pcb->flags & TF_BACKLOGPEND) != 0) {
239  if (pcb->listener != NULL) {
240  LWIP_ASSERT("accepts_pending != 0", pcb->listener->accepts_pending != 0);
241  pcb->listener->accepts_pending--;
242  pcb->flags &= ~TF_BACKLOGPEND;
243  }
244  }
245 }
246 #endif /* TCP_LISTEN_BACKLOG */
247 
264 static err_t
265 tcp_close_shutdown(struct tcp_pcb *pcb, u8_t rst_on_unacked_data)
266 {
267  if (rst_on_unacked_data && ((pcb->state == ESTABLISHED) || (pcb->state == CLOSE_WAIT))) {
268  if ((pcb->refused_data != NULL) || (pcb->rcv_wnd != TCP_WND_MAX(pcb))) {
269  /* Not all data received by application, send RST to tell the remote
270  side about this. */
271  LWIP_ASSERT("pcb->flags & TF_RXCLOSED", pcb->flags & TF_RXCLOSED);
272 
273  /* don't call tcp_abort here: we must not deallocate the pcb since
274  that might not be expected when calling tcp_close */
275  tcp_rst(pcb->snd_nxt, pcb->rcv_nxt, &pcb->local_ip, &pcb->remote_ip,
276  pcb->local_port, pcb->remote_port);
277 
278  tcp_pcb_purge(pcb);
279  TCP_RMV_ACTIVE(pcb);
280  if (pcb->state == ESTABLISHED) {
281  /* move to TIME_WAIT since we close actively */
282  pcb->state = TIME_WAIT;
283  TCP_REG(&tcp_tw_pcbs, pcb);
284  } else {
285  /* CLOSE_WAIT: deallocate the pcb since we already sent a RST for it */
286  if (tcp_input_pcb == pcb) {
287  /* prevent using a deallocated pcb: free it from tcp_input later */
288  tcp_trigger_input_pcb_close();
289  } else {
290  memp_free(MEMP_TCP_PCB, pcb);
291  }
292  }
293  return ERR_OK;
294  }
295  }
296 
297  /* - states which free the pcb are handled here,
298  - states which send FIN and change state are handled in tcp_close_shutdown_fin() */
299  switch (pcb->state) {
300  case CLOSED:
301  /* Closing a pcb in the CLOSED state might seem erroneous,
302  * however, it is in this state once allocated and as yet unused
303  * and the user needs some way to free it should the need arise.
304  * Calling tcp_close() with a pcb that has already been closed, (i.e. twice)
305  * or for a pcb that has been used and then entered the CLOSED state
306  * is erroneous, but this should never happen as the pcb has in those cases
307  * been freed, and so any remaining handles are bogus. */
308  if (pcb->local_port != 0) {
309  TCP_RMV(&tcp_bound_pcbs, pcb);
310  }
311  memp_free(MEMP_TCP_PCB, pcb);
312  break;
313  case LISTEN:
314  tcp_listen_closed(pcb);
315  tcp_pcb_remove(&tcp_listen_pcbs.pcbs, pcb);
316  memp_free(MEMP_TCP_PCB_LISTEN, pcb);
317  break;
318  case SYN_SENT:
319  TCP_PCB_REMOVE_ACTIVE(pcb);
320  memp_free(MEMP_TCP_PCB, pcb);
321  MIB2_STATS_INC(mib2.tcpattemptfails);
322  break;
323  default:
324  return tcp_close_shutdown_fin(pcb);
325  }
326  return ERR_OK;
327 }
328 
329 static err_t
330 tcp_close_shutdown_fin(struct tcp_pcb *pcb)
331 {
332  err_t err;
333  LWIP_ASSERT("pcb != NULL", pcb != NULL);
334 
335  switch (pcb->state) {
336  case SYN_RCVD:
337  err = tcp_send_fin(pcb);
338  if (err == ERR_OK) {
339  tcp_backlog_accepted(pcb);
340  MIB2_STATS_INC(mib2.tcpattemptfails);
341  pcb->state = FIN_WAIT_1;
342  }
343  break;
344  case ESTABLISHED:
345  err = tcp_send_fin(pcb);
346  if (err == ERR_OK) {
347  MIB2_STATS_INC(mib2.tcpestabresets);
348  pcb->state = FIN_WAIT_1;
349  }
350  break;
351  case CLOSE_WAIT:
352  err = tcp_send_fin(pcb);
353  if (err == ERR_OK) {
354  MIB2_STATS_INC(mib2.tcpestabresets);
355  pcb->state = LAST_ACK;
356  }
357  break;
358  default:
359  /* Has already been closed, do nothing. */
360  return ERR_OK;
361  }
362 
363  if (err == ERR_OK) {
364  /* To ensure all data has been sent when tcp_close returns, we have
365  to make sure tcp_output doesn't fail.
366  Since we don't really have to ensure all data has been sent when tcp_close
367  returns (unsent data is sent from tcp timer functions, also), we don't care
368  for the return value of tcp_output for now. */
369  tcp_output(pcb);
370  } else if (err == ERR_MEM) {
371  /* Mark this pcb for closing. Closing is retried from tcp_tmr. */
372  pcb->flags |= TF_CLOSEPEND;
373  /* We have to return ERR_OK from here to indicate to the callers that this
374  pcb should not be used any more as it will be freed soon via tcp_tmr.
375  This is OK here since sending FIN does not guarantee a time frime for
376  actually freeing the pcb, either (it is left in closure states for
377  remote ACK or timeout) */
378  return ERR_OK;
379  }
380  return err;
381 }
382 
398 err_t
399 tcp_close(struct tcp_pcb *pcb)
400 {
401  LWIP_DEBUGF(TCP_DEBUG, ("tcp_close: closing in "));
402  tcp_debug_print_state(pcb->state);
403 
404  if (pcb->state != LISTEN) {
405  /* Set a flag not to receive any more data... */
406  pcb->flags |= TF_RXCLOSED;
407  }
408  /* ... and close */
409  return tcp_close_shutdown(pcb, 1);
410 }
411 
425 err_t
426 tcp_shutdown(struct tcp_pcb *pcb, int shut_rx, int shut_tx)
427 {
428  if (pcb->state == LISTEN) {
429  return ERR_CONN;
430  }
431  if (shut_rx) {
432  /* shut down the receive side: set a flag not to receive any more data... */
433  pcb->flags |= TF_RXCLOSED;
434  if (shut_tx) {
435  /* shutting down the tx AND rx side is the same as closing for the raw API */
436  return tcp_close_shutdown(pcb, 1);
437  }
438  /* ... and free buffered data */
439  if (pcb->refused_data != NULL) {
440  pbuf_free(pcb->refused_data);
441  pcb->refused_data = NULL;
442  }
443  }
444  if (shut_tx) {
445  /* This can't happen twice since if it succeeds, the pcb's state is changed.
446  Only close in these states as the others directly deallocate the PCB */
447  switch (pcb->state) {
448  case SYN_RCVD:
449  case ESTABLISHED:
450  case CLOSE_WAIT:
451  return tcp_close_shutdown(pcb, (u8_t)shut_rx);
452  default:
453  /* Not (yet?) connected, cannot shutdown the TX side as that would bring us
454  into CLOSED state, where the PCB is deallocated. */
455  return ERR_CONN;
456  }
457  }
458  return ERR_OK;
459 }
460 
469 void
470 tcp_abandon(struct tcp_pcb *pcb, int reset)
471 {
472  u32_t seqno, ackno;
473 #if LWIP_CALLBACK_API
474  tcp_err_fn errf;
475 #endif /* LWIP_CALLBACK_API */
476  void *errf_arg;
477 
478  /* pcb->state LISTEN not allowed here */
479  LWIP_ASSERT("don't call tcp_abort/tcp_abandon for listen-pcbs",
480  pcb->state != LISTEN);
481  /* Figure out on which TCP PCB list we are, and remove us. If we
482  are in an active state, call the receive function associated with
483  the PCB with a NULL argument, and send an RST to the remote end. */
484  if (pcb->state == TIME_WAIT) {
485  tcp_pcb_remove(&tcp_tw_pcbs, pcb);
486  memp_free(MEMP_TCP_PCB, pcb);
487  } else {
488  int send_rst = 0;
489  u16_t local_port = 0;
490  enum tcp_state last_state;
491  seqno = pcb->snd_nxt;
492  ackno = pcb->rcv_nxt;
493 #if LWIP_CALLBACK_API
494  errf = pcb->errf;
495 #endif /* LWIP_CALLBACK_API */
496  errf_arg = pcb->callback_arg;
497  if (pcb->state == CLOSED) {
498  if (pcb->local_port != 0) {
499  /* bound, not yet opened */
500  TCP_RMV(&tcp_bound_pcbs, pcb);
501  }
502  } else {
503  send_rst = reset;
504  local_port = pcb->local_port;
505  TCP_PCB_REMOVE_ACTIVE(pcb);
506  }
507  if (pcb->unacked != NULL) {
508  tcp_segs_free(pcb->unacked);
509  }
510  if (pcb->unsent != NULL) {
511  tcp_segs_free(pcb->unsent);
512  }
513 #if TCP_QUEUE_OOSEQ
514  if (pcb->ooseq != NULL) {
515  tcp_segs_free(pcb->ooseq);
516  }
517 #endif /* TCP_QUEUE_OOSEQ */
518  tcp_backlog_accepted(pcb);
519  if (send_rst) {
520  LWIP_DEBUGF(TCP_RST_DEBUG, ("tcp_abandon: sending RST\n"));
521  tcp_rst(seqno, ackno, &pcb->local_ip, &pcb->remote_ip, local_port, pcb->remote_port);
522  }
523  last_state = pcb->state;
524  memp_free(MEMP_TCP_PCB, pcb);
525  TCP_EVENT_ERR(last_state, errf, errf_arg, ERR_ABRT);
526  }
527 }
528 
540 void
541 tcp_abort(struct tcp_pcb *pcb)
542 {
543  tcp_abandon(pcb, 1);
544 }
545 
561 err_t
562 tcp_bind(struct tcp_pcb *pcb, const ip_addr_t *ipaddr, u16_t port)
563 {
564  int i;
565  int max_pcb_list = NUM_TCP_PCB_LISTS;
566  struct tcp_pcb *cpcb;
567 
568 #if LWIP_IPV4
569  /* Don't propagate NULL pointer (IPv4 ANY) to subsequent functions */
570  if (ipaddr == NULL) {
571  ipaddr = IP4_ADDR_ANY;
572  }
573 #endif /* LWIP_IPV4 */
574 
575  /* still need to check for ipaddr == NULL in IPv6 only case */
576  if ((pcb == NULL) || (ipaddr == NULL)) {
577  return ERR_VAL;
578  }
579 
580  LWIP_ERROR("tcp_bind: can only bind in state CLOSED", pcb->state == CLOSED, return ERR_VAL);
581 
582 #if SO_REUSE
583  /* Unless the REUSEADDR flag is set,
584  we have to check the pcbs in TIME-WAIT state, also.
585  We do not dump TIME_WAIT pcb's; they can still be matched by incoming
586  packets using both local and remote IP addresses and ports to distinguish.
587  */
588  if (ip_get_option(pcb, SOF_REUSEADDR)) {
589  max_pcb_list = NUM_TCP_PCB_LISTS_NO_TIME_WAIT;
590  }
591 #endif /* SO_REUSE */
592 
593  if (port == 0) {
594  port = tcp_new_port();
595  if (port == 0) {
596  return ERR_BUF;
597  }
598  } else {
599  /* Check if the address already is in use (on all lists) */
600  for (i = 0; i < max_pcb_list; i++) {
601  for (cpcb = *tcp_pcb_lists[i]; cpcb != NULL; cpcb = cpcb->next) {
602  if (cpcb->local_port == port) {
603 #if SO_REUSE
604  /* Omit checking for the same port if both pcbs have REUSEADDR set.
605  For SO_REUSEADDR, the duplicate-check for a 5-tuple is done in
606  tcp_connect. */
607  if (!ip_get_option(pcb, SOF_REUSEADDR) ||
609 #endif /* SO_REUSE */
610  {
611  /* @todo: check accept_any_ip_version */
612  if ((IP_IS_V6(ipaddr) == IP_IS_V6_VAL(cpcb->local_ip)) &&
613  (ip_addr_isany(&cpcb->local_ip) ||
614  ip_addr_isany(ipaddr) ||
615  ip_addr_cmp(&cpcb->local_ip, ipaddr))) {
616  return ERR_USE;
617  }
618  }
619  }
620  }
621  }
622  }
623 
624  if (!ip_addr_isany(ipaddr)) {
625  ip_addr_set(&pcb->local_ip, ipaddr);
626  }
627  pcb->local_port = port;
628  TCP_REG(&tcp_bound_pcbs, pcb);
629  LWIP_DEBUGF(TCP_DEBUG, ("tcp_bind: bind to port %"U16_F"\n", port));
630  return ERR_OK;
631 }
632 #if LWIP_CALLBACK_API
633 
636 static err_t
637 tcp_accept_null(void *arg, struct tcp_pcb *pcb, err_t err)
638 {
639  LWIP_UNUSED_ARG(arg);
640  LWIP_UNUSED_ARG(err);
641 
642  tcp_abort(pcb);
643 
644  return ERR_ABRT;
645 }
646 #endif /* LWIP_CALLBACK_API */
647 
663 struct tcp_pcb *
664 tcp_listen_with_backlog(struct tcp_pcb *pcb, u8_t backlog)
665 {
666  return tcp_listen_with_backlog_and_err(pcb, backlog, NULL);
667 }
668 
685 struct tcp_pcb *
686 tcp_listen_with_backlog_and_err(struct tcp_pcb *pcb, u8_t backlog, err_t *err)
687 {
688  struct tcp_pcb_listen *lpcb = NULL;
689  err_t res;
690 
691  LWIP_UNUSED_ARG(backlog);
692  LWIP_ERROR("tcp_listen: pcb already connected", pcb->state == CLOSED, res = ERR_CLSD; goto done);
693 
694  /* already listening? */
695  if (pcb->state == LISTEN) {
696  lpcb = (struct tcp_pcb_listen*)pcb;
697  res = ERR_ALREADY;
698  goto done;
699  }
700 #if SO_REUSE
701  if (ip_get_option(pcb, SOF_REUSEADDR)) {
702  /* Since SOF_REUSEADDR allows reusing a local address before the pcb's usage
703  is declared (listen-/connection-pcb), we have to make sure now that
704  this port is only used once for every local IP. */
705  for (lpcb = tcp_listen_pcbs.listen_pcbs; lpcb != NULL; lpcb = lpcb->next) {
706  if ((lpcb->local_port == pcb->local_port) &&
707  ip_addr_cmp(&lpcb->local_ip, &pcb->local_ip)) {
708  /* this address/port is already used */
709  lpcb = NULL;
710  res = ERR_USE;
711  goto done;
712  }
713  }
714  }
715 #endif /* SO_REUSE */
716  lpcb = (struct tcp_pcb_listen *)memp_malloc(MEMP_TCP_PCB_LISTEN);
717  if (lpcb == NULL) {
718  res = ERR_MEM;
719  goto done;
720  }
721  lpcb->callback_arg = pcb->callback_arg;
722  lpcb->local_port = pcb->local_port;
723  lpcb->state = LISTEN;
724  lpcb->prio = pcb->prio;
725  lpcb->so_options = pcb->so_options;
726  lpcb->ttl = pcb->ttl;
727  lpcb->tos = pcb->tos;
728 #if LWIP_IPV4 && LWIP_IPV6
729  IP_SET_TYPE_VAL(lpcb->remote_ip, pcb->local_ip.type);
730 #endif /* LWIP_IPV4 && LWIP_IPV6 */
731  ip_addr_copy(lpcb->local_ip, pcb->local_ip);
732  if (pcb->local_port != 0) {
733  TCP_RMV(&tcp_bound_pcbs, pcb);
734  }
735  memp_free(MEMP_TCP_PCB, pcb);
736 #if LWIP_CALLBACK_API
737  lpcb->accept = tcp_accept_null;
738 #endif /* LWIP_CALLBACK_API */
739 #if TCP_LISTEN_BACKLOG
740  lpcb->accepts_pending = 0;
741  tcp_backlog_set(lpcb, backlog);
742 #endif /* TCP_LISTEN_BACKLOG */
743  TCP_REG(&tcp_listen_pcbs.pcbs, (struct tcp_pcb *)lpcb);
744  res = ERR_OK;
745 done:
746  if (err != NULL) {
747  *err = res;
748  }
749  return (struct tcp_pcb *)lpcb;
750 }
751 
758 u32_t
759 tcp_update_rcv_ann_wnd(struct tcp_pcb *pcb)
760 {
761  u32_t new_right_edge = pcb->rcv_nxt + pcb->rcv_wnd;
762 
763  if (TCP_SEQ_GEQ(new_right_edge, pcb->rcv_ann_right_edge + LWIP_MIN((TCP_WND / 2), pcb->mss))) {
764  /* we can advertise more window */
765  pcb->rcv_ann_wnd = pcb->rcv_wnd;
766  return new_right_edge - pcb->rcv_ann_right_edge;
767  } else {
768  if (TCP_SEQ_GT(pcb->rcv_nxt, pcb->rcv_ann_right_edge)) {
769  /* Can happen due to other end sending out of advertised window,
770  * but within actual available (but not yet advertised) window */
771  pcb->rcv_ann_wnd = 0;
772  } else {
773  /* keep the right edge of window constant */
774  u32_t new_rcv_ann_wnd = pcb->rcv_ann_right_edge - pcb->rcv_nxt;
775 #if !LWIP_WND_SCALE
776  LWIP_ASSERT("new_rcv_ann_wnd <= 0xffff", new_rcv_ann_wnd <= 0xffff);
777 #endif
778  pcb->rcv_ann_wnd = (tcpwnd_size_t)new_rcv_ann_wnd;
779  }
780  return 0;
781  }
782 }
783 
793 void
794 tcp_recved(struct tcp_pcb *pcb, u16_t len)
795 {
796  int wnd_inflation;
797 
798  /* pcb->state LISTEN not allowed here */
799  LWIP_ASSERT("don't call tcp_recved for listen-pcbs",
800  pcb->state != LISTEN);
801 
802  pcb->rcv_wnd += len;
803  if (pcb->rcv_wnd > TCP_WND_MAX(pcb)) {
804  pcb->rcv_wnd = TCP_WND_MAX(pcb);
805  } else if (pcb->rcv_wnd == 0) {
806  /* rcv_wnd overflowed */
807  if ((pcb->state == CLOSE_WAIT) || (pcb->state == LAST_ACK)) {
808  /* In passive close, we allow this, since the FIN bit is added to rcv_wnd
809  by the stack itself, since it is not mandatory for an application
810  to call tcp_recved() for the FIN bit, but e.g. the netconn API does so. */
811  pcb->rcv_wnd = TCP_WND_MAX(pcb);
812  } else {
813  LWIP_ASSERT("tcp_recved: len wrapped rcv_wnd\n", 0);
814  }
815  }
816 
817  wnd_inflation = tcp_update_rcv_ann_wnd(pcb);
818 
819  /* If the change in the right edge of window is significant (default
820  * watermark is TCP_WND/4), then send an explicit update now.
821  * Otherwise wait for a packet to be sent in the normal course of
822  * events (or more window to be available later) */
823  if (wnd_inflation >= TCP_WND_UPDATE_THRESHOLD) {
824  tcp_ack_now(pcb);
825  tcp_output(pcb);
826  }
827 
828  LWIP_DEBUGF(TCP_DEBUG, ("tcp_recved: received %"U16_F" bytes, wnd %"TCPWNDSIZE_F" (%"TCPWNDSIZE_F").\n",
829  len, pcb->rcv_wnd, (u16_t)(TCP_WND_MAX(pcb) - pcb->rcv_wnd)));
830 }
831 
837 static u16_t
838 tcp_new_port(void)
839 {
840  u8_t i;
841  u16_t n = 0;
842  struct tcp_pcb *pcb;
843 
844 again:
845  if (tcp_port++ == TCP_LOCAL_PORT_RANGE_END) {
846  tcp_port = TCP_LOCAL_PORT_RANGE_START;
847  }
848  /* Check all PCB lists. */
849  for (i = 0; i < NUM_TCP_PCB_LISTS; i++) {
850  for (pcb = *tcp_pcb_lists[i]; pcb != NULL; pcb = pcb->next) {
851  if (pcb->local_port == tcp_port) {
852  if (++n > (TCP_LOCAL_PORT_RANGE_END - TCP_LOCAL_PORT_RANGE_START)) {
853  return 0;
854  }
855  goto again;
856  }
857  }
858  }
859  return tcp_port;
860 }
861 
876 err_t
877 tcp_connect(struct tcp_pcb *pcb, const ip_addr_t *ipaddr, u16_t port,
878  tcp_connected_fn connected)
879 {
880  err_t ret;
881  u32_t iss;
882  u16_t old_local_port;
883 
884  if ((pcb == NULL) || (ipaddr == NULL)) {
885  return ERR_VAL;
886  }
887 
888  LWIP_ERROR("tcp_connect: can only connect from state CLOSED", pcb->state == CLOSED, return ERR_ISCONN);
889 
890  LWIP_DEBUGF(TCP_DEBUG, ("tcp_connect to port %"U16_F"\n", port));
891  ip_addr_set(&pcb->remote_ip, ipaddr);
892  pcb->remote_port = port;
893 
894  /* check if we have a route to the remote host */
895  if (ip_addr_isany(&pcb->local_ip)) {
896  /* no local IP address set, yet. */
897  struct netif *netif;
898  const ip_addr_t *local_ip;
899  ip_route_get_local_ip(&pcb->local_ip, &pcb->remote_ip, netif, local_ip);
900  if ((netif == NULL) || (local_ip == NULL)) {
901  /* Don't even try to send a SYN packet if we have no route
902  since that will fail. */
903  return ERR_RTE;
904  }
905  /* Use the address as local address of the pcb. */
906  ip_addr_copy(pcb->local_ip, *local_ip);
907  }
908 
909  old_local_port = pcb->local_port;
910  if (pcb->local_port == 0) {
911  pcb->local_port = tcp_new_port();
912  if (pcb->local_port == 0) {
913  return ERR_BUF;
914  }
915  } else {
916 #if SO_REUSE
917  if (ip_get_option(pcb, SOF_REUSEADDR)) {
918  /* Since SOF_REUSEADDR allows reusing a local address, we have to make sure
919  now that the 5-tuple is unique. */
920  struct tcp_pcb *cpcb;
921  int i;
922  /* Don't check listen- and bound-PCBs, check active- and TIME-WAIT PCBs. */
923  for (i = 2; i < NUM_TCP_PCB_LISTS; i++) {
924  for (cpcb = *tcp_pcb_lists[i]; cpcb != NULL; cpcb = cpcb->next) {
925  if ((cpcb->local_port == pcb->local_port) &&
926  (cpcb->remote_port == port) &&
927  ip_addr_cmp(&cpcb->local_ip, &pcb->local_ip) &&
928  ip_addr_cmp(&cpcb->remote_ip, ipaddr)) {
929  /* linux returns EISCONN here, but ERR_USE should be OK for us */
930  return ERR_USE;
931  }
932  }
933  }
934  }
935 #endif /* SO_REUSE */
936  }
937 
938  iss = tcp_next_iss(pcb);
939  pcb->rcv_nxt = 0;
940  pcb->snd_nxt = iss;
941  pcb->lastack = iss - 1;
942  pcb->snd_wl2 = iss - 1;
943  pcb->snd_lbb = iss - 1;
944  /* Start with a window that does not need scaling. When window scaling is
945  enabled and used, the window is enlarged when both sides agree on scaling. */
946  pcb->rcv_wnd = pcb->rcv_ann_wnd = TCPWND_MIN16(TCP_WND);
947  pcb->rcv_ann_right_edge = pcb->rcv_nxt;
948  pcb->snd_wnd = TCP_WND;
949  /* As initial send MSS, we use TCP_MSS but limit it to 536.
950  The send MSS is updated when an MSS option is received. */
951  pcb->mss = INITIAL_MSS;
952 #if TCP_CALCULATE_EFF_SEND_MSS
953  pcb->mss = tcp_eff_send_mss(pcb->mss, &pcb->local_ip, &pcb->remote_ip);
954 #endif /* TCP_CALCULATE_EFF_SEND_MSS */
955  pcb->cwnd = 1;
956 #if LWIP_CALLBACK_API
957  pcb->connected = connected;
958 #else /* LWIP_CALLBACK_API */
959  LWIP_UNUSED_ARG(connected);
960 #endif /* LWIP_CALLBACK_API */
961 
962  /* Send a SYN together with the MSS option. */
963  ret = tcp_enqueue_flags(pcb, TCP_SYN);
964  if (ret == ERR_OK) {
965  /* SYN segment was enqueued, changed the pcbs state now */
966  pcb->state = SYN_SENT;
967  if (old_local_port != 0) {
968  TCP_RMV(&tcp_bound_pcbs, pcb);
969  }
970  TCP_REG_ACTIVE(pcb);
971  MIB2_STATS_INC(mib2.tcpactiveopens);
972 
973  tcp_output(pcb);
974  }
975  return ret;
976 }
977 
985 void
986 tcp_slowtmr(void)
987 {
988  struct tcp_pcb *pcb, *prev;
989  tcpwnd_size_t eff_wnd;
990  u8_t pcb_remove; /* flag if a PCB should be removed */
991  u8_t pcb_reset; /* flag if a RST should be sent when removing */
992  err_t err;
993 
994  err = ERR_OK;
995 
996  ++tcp_ticks;
997  ++tcp_timer_ctr;
998 
999 tcp_slowtmr_start:
1000  /* Steps through all of the active PCBs. */
1001  prev = NULL;
1002  pcb = tcp_active_pcbs;
1003  if (pcb == NULL) {
1004  LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: no active pcbs\n"));
1005  }
1006  while (pcb != NULL) {
1007  LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: processing active pcb\n"));
1008  LWIP_ASSERT("tcp_slowtmr: active pcb->state != CLOSED\n", pcb->state != CLOSED);
1009  LWIP_ASSERT("tcp_slowtmr: active pcb->state != LISTEN\n", pcb->state != LISTEN);
1010  LWIP_ASSERT("tcp_slowtmr: active pcb->state != TIME-WAIT\n", pcb->state != TIME_WAIT);
1011  if (pcb->last_timer == tcp_timer_ctr) {
1012  /* skip this pcb, we have already processed it */
1013  pcb = pcb->next;
1014  continue;
1015  }
1016  pcb->last_timer = tcp_timer_ctr;
1017 
1018  pcb_remove = 0;
1019  pcb_reset = 0;
1020 
1021  if (pcb->state == SYN_SENT && pcb->nrtx >= TCP_SYNMAXRTX) {
1022  ++pcb_remove;
1023  LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: max SYN retries reached\n"));
1024  }
1025  else if (pcb->nrtx >= TCP_MAXRTX) {
1026  ++pcb_remove;
1027  LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: max DATA retries reached\n"));
1028  } else {
1029  if (pcb->persist_backoff > 0) {
1030  /* If snd_wnd is zero, use persist timer to send 1 byte probes
1031  * instead of using the standard retransmission mechanism. */
1032  u8_t backoff_cnt = tcp_persist_backoff[pcb->persist_backoff-1];
1033  if (pcb->persist_cnt < backoff_cnt) {
1034  pcb->persist_cnt++;
1035  }
1036  if (pcb->persist_cnt >= backoff_cnt) {
1037  if (tcp_zero_window_probe(pcb) == ERR_OK) {
1038  pcb->persist_cnt = 0;
1039  if (pcb->persist_backoff < sizeof(tcp_persist_backoff)) {
1040  pcb->persist_backoff++;
1041  }
1042  }
1043  }
1044  } else {
1045  /* Increase the retransmission timer if it is running */
1046  if (pcb->rtime >= 0) {
1047  ++pcb->rtime;
1048  }
1049 
1050  if (pcb->unacked != NULL && pcb->rtime >= pcb->rto) {
1051  /* Time for a retransmission. */
1052  LWIP_DEBUGF(TCP_RTO_DEBUG, ("tcp_slowtmr: rtime %"S16_F
1053  " pcb->rto %"S16_F"\n",
1054  pcb->rtime, pcb->rto));
1055 
1056  /* Double retransmission time-out unless we are trying to
1057  * connect to somebody (i.e., we are in SYN_SENT). */
1058  if (pcb->state != SYN_SENT) {
1059  u8_t backoff_idx = LWIP_MIN(pcb->nrtx, sizeof(tcp_backoff)-1);
1060  pcb->rto = ((pcb->sa >> 3) + pcb->sv) << tcp_backoff[backoff_idx];
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  enum tcp_state last_state;
1158  tcp_pcb_purge(pcb);
1159  /* Remove PCB from tcp_active_pcbs list. */
1160  if (prev != NULL) {
1161  LWIP_ASSERT("tcp_slowtmr: middle tcp != tcp_active_pcbs", pcb != tcp_active_pcbs);
1162  prev->next = pcb->next;
1163  } else {
1164  /* This PCB was the first. */
1165  LWIP_ASSERT("tcp_slowtmr: first pcb == tcp_active_pcbs", tcp_active_pcbs == pcb);
1166  tcp_active_pcbs = pcb->next;
1167  }
1168 
1169  if (pcb_reset) {
1170  tcp_rst(pcb->snd_nxt, pcb->rcv_nxt, &pcb->local_ip, &pcb->remote_ip,
1171  pcb->local_port, pcb->remote_port);
1172  }
1173 
1174  err_arg = pcb->callback_arg;
1175  last_state = pcb->state;
1176  pcb2 = pcb;
1177  pcb = pcb->next;
1178  memp_free(MEMP_TCP_PCB, pcb2);
1179 
1180  tcp_active_pcbs_changed = 0;
1181  TCP_EVENT_ERR(last_state, err_fn, err_arg, ERR_ABRT);
1182  if (tcp_active_pcbs_changed) {
1183  goto tcp_slowtmr_start;
1184  }
1185  } else {
1186  /* get the 'next' element now and work with 'prev' below (in case of abort) */
1187  prev = pcb;
1188  pcb = pcb->next;
1189 
1190  /* We check if we should poll the connection. */
1191  ++prev->polltmr;
1192  if (prev->polltmr >= prev->pollinterval) {
1193  prev->polltmr = 0;
1194  LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: polling application\n"));
1195  tcp_active_pcbs_changed = 0;
1196  TCP_EVENT_POLL(prev, err);
1197  if (tcp_active_pcbs_changed) {
1198  goto tcp_slowtmr_start;
1199  }
1200  /* if err == ERR_ABRT, 'prev' is already deallocated */
1201  if (err == ERR_OK) {
1202  tcp_output(prev);
1203  }
1204  }
1205  }
1206  }
1207 
1208 
1209  /* Steps through all of the TIME-WAIT PCBs. */
1210  prev = NULL;
1211  pcb = tcp_tw_pcbs;
1212  while (pcb != NULL) {
1213  LWIP_ASSERT("tcp_slowtmr: TIME-WAIT pcb->state == TIME-WAIT", pcb->state == TIME_WAIT);
1214  pcb_remove = 0;
1215 
1216  /* Check if this PCB has stayed long enough in TIME-WAIT */
1217  if ((u32_t)(tcp_ticks - pcb->tmr) > 2 * TCP_MSL / TCP_SLOW_INTERVAL) {
1218  ++pcb_remove;
1219  }
1220 
1221  /* If the PCB should be removed, do it. */
1222  if (pcb_remove) {
1223  struct tcp_pcb *pcb2;
1224  tcp_pcb_purge(pcb);
1225  /* Remove PCB from tcp_tw_pcbs list. */
1226  if (prev != NULL) {
1227  LWIP_ASSERT("tcp_slowtmr: middle tcp != tcp_tw_pcbs", pcb != tcp_tw_pcbs);
1228  prev->next = pcb->next;
1229  } else {
1230  /* This PCB was the first. */
1231  LWIP_ASSERT("tcp_slowtmr: first pcb == tcp_tw_pcbs", tcp_tw_pcbs == pcb);
1232  tcp_tw_pcbs = pcb->next;
1233  }
1234  pcb2 = pcb;
1235  pcb = pcb->next;
1236  memp_free(MEMP_TCP_PCB, pcb2);
1237  } else {
1238  prev = pcb;
1239  pcb = pcb->next;
1240  }
1241  }
1242 }
1243 
1250 void
1251 tcp_fasttmr(void)
1252 {
1253  struct tcp_pcb *pcb;
1254 
1255  ++tcp_timer_ctr;
1256 
1257 tcp_fasttmr_start:
1258  pcb = tcp_active_pcbs;
1259 
1260  while (pcb != NULL) {
1261  if (pcb->last_timer != tcp_timer_ctr) {
1262  struct tcp_pcb *next;
1263  pcb->last_timer = tcp_timer_ctr;
1264  /* send delayed ACKs */
1265  if (pcb->flags & TF_ACK_DELAY) {
1266  LWIP_DEBUGF(TCP_DEBUG, ("tcp_fasttmr: delayed ACK\n"));
1267  tcp_ack_now(pcb);
1268  tcp_output(pcb);
1269  pcb->flags &= ~(TF_ACK_DELAY | TF_ACK_NOW);
1270  }
1271  /* send pending FIN */
1272  if (pcb->flags & TF_CLOSEPEND) {
1273  LWIP_DEBUGF(TCP_DEBUG, ("tcp_fasttmr: pending FIN\n"));
1274  pcb->flags &= ~(TF_CLOSEPEND);
1275  tcp_close_shutdown_fin(pcb);
1276  }
1277 
1278  next = pcb->next;
1279 
1280  /* If there is data which was previously "refused" by upper layer */
1281  if (pcb->refused_data != NULL) {
1282  tcp_active_pcbs_changed = 0;
1283  tcp_process_refused_data(pcb);
1284  if (tcp_active_pcbs_changed) {
1285  /* application callback has changed the pcb list: restart the loop */
1286  goto tcp_fasttmr_start;
1287  }
1288  }
1289  pcb = next;
1290  } else {
1291  pcb = pcb->next;
1292  }
1293  }
1294 }
1295 
1297 void
1298 tcp_txnow(void)
1299 {
1300  struct tcp_pcb *pcb;
1301 
1302  for (pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) {
1303  if (pcb->flags & TF_NAGLEMEMERR) {
1304  tcp_output(pcb);
1305  }
1306  }
1307 }
1308 
1310 err_t
1311 tcp_process_refused_data(struct tcp_pcb *pcb)
1312 {
1313 #if TCP_QUEUE_OOSEQ && LWIP_WND_SCALE
1314  struct pbuf *rest;
1315  while (pcb->refused_data != NULL)
1316 #endif /* TCP_QUEUE_OOSEQ && LWIP_WND_SCALE */
1317  {
1318  err_t err;
1319  u8_t refused_flags = pcb->refused_data->flags;
1320  /* set pcb->refused_data to NULL in case the callback frees it and then
1321  closes the pcb */
1322  struct pbuf *refused_data = pcb->refused_data;
1323 #if TCP_QUEUE_OOSEQ && LWIP_WND_SCALE
1324  pbuf_split_64k(refused_data, &rest);
1325  pcb->refused_data = rest;
1326 #else /* TCP_QUEUE_OOSEQ && LWIP_WND_SCALE */
1327  pcb->refused_data = NULL;
1328 #endif /* TCP_QUEUE_OOSEQ && LWIP_WND_SCALE */
1329  /* Notify again application with data previously received. */
1330  LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_input: notify kept packet\n"));
1331  TCP_EVENT_RECV(pcb, refused_data, ERR_OK, err);
1332  if (err == ERR_OK) {
1333  /* did refused_data include a FIN? */
1334  if (refused_flags & PBUF_FLAG_TCP_FIN
1336  && (rest == NULL)
1337 #endif /* TCP_QUEUE_OOSEQ && LWIP_WND_SCALE */
1338  ) {
1339  /* correct rcv_wnd as the application won't call tcp_recved()
1340  for the FIN's seqno */
1341  if (pcb->rcv_wnd != TCP_WND_MAX(pcb)) {
1342  pcb->rcv_wnd++;
1343  }
1344  TCP_EVENT_CLOSED(pcb, err);
1345  if (err == ERR_ABRT) {
1346  return ERR_ABRT;
1347  }
1348  }
1349  } else if (err == ERR_ABRT) {
1350  /* if err == ERR_ABRT, 'pcb' is already deallocated */
1351  /* Drop incoming packets because pcb is "full" (only if the incoming
1352  segment contains data). */
1353  LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_input: drop incoming packets, because pcb is \"full\"\n"));
1354  return ERR_ABRT;
1355  } else {
1356  /* data is still refused, pbuf is still valid (go on for ACK-only packets) */
1357 #if TCP_QUEUE_OOSEQ && LWIP_WND_SCALE
1358  if (rest != NULL) {
1359  pbuf_cat(refused_data, rest);
1360  }
1361 #endif /* TCP_QUEUE_OOSEQ && LWIP_WND_SCALE */
1362  pcb->refused_data = refused_data;
1363  return ERR_INPROGRESS;
1364  }
1365  }
1366  return ERR_OK;
1367 }
1368 
1374 void
1375 tcp_segs_free(struct tcp_seg *seg)
1376 {
1377  while (seg != NULL) {
1378  struct tcp_seg *next = seg->next;
1379  tcp_seg_free(seg);
1380  seg = next;
1381  }
1382 }
1383 
1389 void
1390 tcp_seg_free(struct tcp_seg *seg)
1391 {
1392  if (seg != NULL) {
1393  if (seg->p != NULL) {
1394  pbuf_free(seg->p);
1395 #if TCP_DEBUG
1396  seg->p = NULL;
1397 #endif /* TCP_DEBUG */
1398  }
1399  memp_free(MEMP_TCP_SEG, seg);
1400  }
1401 }
1402 
1409 void
1410 tcp_setprio(struct tcp_pcb *pcb, u8_t prio)
1411 {
1412  pcb->prio = prio;
1413 }
1414 
1415 #if TCP_QUEUE_OOSEQ
1416 
1423 struct tcp_seg *
1424 tcp_seg_copy(struct tcp_seg *seg)
1425 {
1426  struct tcp_seg *cseg;
1427 
1428  cseg = (struct tcp_seg *)memp_malloc(MEMP_TCP_SEG);
1429  if (cseg == NULL) {
1430  return NULL;
1431  }
1432  SMEMCPY((u8_t *)cseg, (const u8_t *)seg, sizeof(struct tcp_seg));
1433  pbuf_ref(cseg->p);
1434  return cseg;
1435 }
1436 #endif /* TCP_QUEUE_OOSEQ */
1437 
1438 #if LWIP_CALLBACK_API
1439 
1443 err_t
1444 tcp_recv_null(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
1445 {
1446  LWIP_UNUSED_ARG(arg);
1447  if (p != NULL) {
1448  tcp_recved(pcb, p->tot_len);
1449  pbuf_free(p);
1450  } else if (err == ERR_OK) {
1451  return tcp_close(pcb);
1452  }
1453  return ERR_OK;
1454 }
1455 #endif /* LWIP_CALLBACK_API */
1456 
1463 static void
1464 tcp_kill_prio(u8_t prio)
1465 {
1466  struct tcp_pcb *pcb, *inactive;
1467  u32_t inactivity;
1468  u8_t mprio;
1469 
1470  mprio = LWIP_MIN(TCP_PRIO_MAX, prio);
1471 
1472  /* We kill the oldest active connection that has lower priority than prio. */
1473  inactivity = 0;
1474  inactive = NULL;
1475  for (pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) {
1476  if (pcb->prio <= mprio &&
1477  (u32_t)(tcp_ticks - pcb->tmr) >= inactivity) {
1478  inactivity = tcp_ticks - pcb->tmr;
1479  inactive = pcb;
1480  mprio = pcb->prio;
1481  }
1482  }
1483  if (inactive != NULL) {
1484  LWIP_DEBUGF(TCP_DEBUG, ("tcp_kill_prio: killing oldest PCB %p (%"S32_F")\n",
1485  (void *)inactive, inactivity));
1486  tcp_abort(inactive);
1487  }
1488 }
1489 
1494 static void
1495 tcp_kill_state(enum tcp_state state)
1496 {
1497  struct tcp_pcb *pcb, *inactive;
1498  u32_t inactivity;
1499 
1500  LWIP_ASSERT("invalid state", (state == CLOSING) || (state == LAST_ACK));
1501 
1502  inactivity = 0;
1503  inactive = NULL;
1504  /* Go through the list of active pcbs and get the oldest pcb that is in state
1505  CLOSING/LAST_ACK. */
1506  for (pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) {
1507  if (pcb->state == state) {
1508  if ((u32_t)(tcp_ticks - pcb->tmr) >= inactivity) {
1509  inactivity = tcp_ticks - pcb->tmr;
1510  inactive = pcb;
1511  }
1512  }
1513  }
1514  if (inactive != NULL) {
1515  LWIP_DEBUGF(TCP_DEBUG, ("tcp_kill_closing: killing oldest %s PCB %p (%"S32_F")\n",
1516  tcp_state_str[state], (void *)inactive, inactivity));
1517  /* Don't send a RST, since no data is lost. */
1518  tcp_abandon(inactive, 0);
1519  }
1520 }
1521 
1526 static void
1527 tcp_kill_timewait(void)
1528 {
1529  struct tcp_pcb *pcb, *inactive;
1530  u32_t inactivity;
1531 
1532  inactivity = 0;
1533  inactive = NULL;
1534  /* Go through the list of TIME_WAIT pcbs and get the oldest pcb. */
1535  for (pcb = tcp_tw_pcbs; pcb != NULL; pcb = pcb->next) {
1536  if ((u32_t)(tcp_ticks - pcb->tmr) >= inactivity) {
1537  inactivity = tcp_ticks - pcb->tmr;
1538  inactive = pcb;
1539  }
1540  }
1541  if (inactive != NULL) {
1542  LWIP_DEBUGF(TCP_DEBUG, ("tcp_kill_timewait: killing oldest TIME-WAIT PCB %p (%"S32_F")\n",
1543  (void *)inactive, inactivity));
1544  tcp_abort(inactive);
1545  }
1546 }
1547 
1554 struct tcp_pcb *
1555 tcp_alloc(u8_t prio)
1556 {
1557  struct tcp_pcb *pcb;
1558 
1559  pcb = (struct tcp_pcb *)memp_malloc(MEMP_TCP_PCB);
1560  if (pcb == NULL) {
1561  /* Try killing oldest connection in TIME-WAIT. */
1562  LWIP_DEBUGF(TCP_DEBUG, ("tcp_alloc: killing off oldest TIME-WAIT connection\n"));
1563  tcp_kill_timewait();
1564  /* Try to allocate a tcp_pcb again. */
1565  pcb = (struct tcp_pcb *)memp_malloc(MEMP_TCP_PCB);
1566  if (pcb == NULL) {
1567  /* Try killing oldest connection in LAST-ACK (these wouldn't go to TIME-WAIT). */
1568  LWIP_DEBUGF(TCP_DEBUG, ("tcp_alloc: killing off oldest LAST-ACK connection\n"));
1569  tcp_kill_state(LAST_ACK);
1570  /* Try to allocate a tcp_pcb again. */
1571  pcb = (struct tcp_pcb *)memp_malloc(MEMP_TCP_PCB);
1572  if (pcb == NULL) {
1573  /* Try killing oldest connection in CLOSING. */
1574  LWIP_DEBUGF(TCP_DEBUG, ("tcp_alloc: killing off oldest CLOSING connection\n"));
1575  tcp_kill_state(CLOSING);
1576  /* Try to allocate a tcp_pcb again. */
1577  pcb = (struct tcp_pcb *)memp_malloc(MEMP_TCP_PCB);
1578  if (pcb == NULL) {
1579  /* Try killing active connections with lower priority than the new one. */
1580  LWIP_DEBUGF(TCP_DEBUG, ("tcp_alloc: killing connection with prio lower than %d\n", prio));
1581  tcp_kill_prio(prio);
1582  /* Try to allocate a tcp_pcb again. */
1583  pcb = (struct tcp_pcb *)memp_malloc(MEMP_TCP_PCB);
1584  if (pcb != NULL) {
1585  /* adjust err stats: memp_malloc failed multiple times before */
1586  MEMP_STATS_DEC(err, MEMP_TCP_PCB);
1587  }
1588  }
1589  if (pcb != NULL) {
1590  /* adjust err stats: memp_malloc failed multiple times before */
1591  MEMP_STATS_DEC(err, MEMP_TCP_PCB);
1592  }
1593  }
1594  if (pcb != NULL) {
1595  /* adjust err stats: memp_malloc failed multiple times before */
1596  MEMP_STATS_DEC(err, MEMP_TCP_PCB);
1597  }
1598  }
1599  if (pcb != NULL) {
1600  /* adjust err stats: memp_malloc failed above */
1601  MEMP_STATS_DEC(err, MEMP_TCP_PCB);
1602  }
1603  }
1604  if (pcb != NULL) {
1605  /* zero out the whole pcb, so there is no need to initialize members to zero */
1606  memset(pcb, 0, sizeof(struct tcp_pcb));
1607  pcb->prio = prio;
1608  pcb->snd_buf = TCP_SND_BUF;
1609  /* Start with a window that does not need scaling. When window scaling is
1610  enabled and used, the window is enlarged when both sides agree on scaling. */
1611  pcb->rcv_wnd = pcb->rcv_ann_wnd = TCPWND_MIN16(TCP_WND);
1612  pcb->ttl = TCP_TTL;
1613  /* As initial send MSS, we use TCP_MSS but limit it to 536.
1614  The send MSS is updated when an MSS option is received. */
1615  pcb->mss = INITIAL_MSS;
1616  pcb->rto = 3000 / TCP_SLOW_INTERVAL;
1617  pcb->sv = 3000 / TCP_SLOW_INTERVAL;
1618  pcb->rtime = -1;
1619  pcb->cwnd = 1;
1620  pcb->tmr = tcp_ticks;
1621  pcb->last_timer = tcp_timer_ctr;
1622 
1623  /* RFC 5681 recommends setting ssthresh abritrarily high and gives an example
1624  of using the largest advertised receive window. We've seen complications with
1625  receiving TCPs that use window scaling and/or window auto-tuning where the
1626  initial advertised window is very small and then grows rapidly once the
1627  connection is established. To avoid these complications, we set ssthresh to the
1628  largest effective cwnd (amount of in-flight data) that the sender can have. */
1629  pcb->ssthresh = TCP_SND_BUF;
1630 
1631 #if LWIP_CALLBACK_API
1632  pcb->recv = tcp_recv_null;
1633 #endif /* LWIP_CALLBACK_API */
1634 
1635  /* Init KEEPALIVE timer */
1636  pcb->keep_idle = TCP_KEEPIDLE_DEFAULT;
1637 
1638 #if LWIP_TCP_KEEPALIVE
1639  pcb->keep_intvl = TCP_KEEPINTVL_DEFAULT;
1640  pcb->keep_cnt = TCP_KEEPCNT_DEFAULT;
1641 #endif /* LWIP_TCP_KEEPALIVE */
1642  }
1643  return pcb;
1644 }
1645 
1659 struct tcp_pcb *
1660 tcp_new(void)
1661 {
1662  return tcp_alloc(TCP_PRIO_NORMAL);
1663 }
1664 
1676 struct tcp_pcb *
1677 tcp_new_ip_type(u8_t type)
1678 {
1679  struct tcp_pcb * pcb;
1680  pcb = tcp_alloc(TCP_PRIO_NORMAL);
1681 #if LWIP_IPV4 && LWIP_IPV6
1682  if (pcb != NULL) {
1683  IP_SET_TYPE_VAL(pcb->local_ip, type);
1684  IP_SET_TYPE_VAL(pcb->remote_ip, type);
1685  }
1686 #else
1687  LWIP_UNUSED_ARG(type);
1688 #endif /* LWIP_IPV4 && LWIP_IPV6 */
1689  return pcb;
1690 }
1691 
1700 void
1701 tcp_arg(struct tcp_pcb *pcb, void *arg)
1702 {
1703  /* This function is allowed to be called for both listen pcbs and
1704  connection pcbs. */
1705  if (pcb != NULL) {
1706  pcb->callback_arg = arg;
1707  }
1708 }
1709 #if LWIP_CALLBACK_API
1710 
1719 void
1720 tcp_recv(struct tcp_pcb *pcb, tcp_recv_fn recv)
1721 {
1722  if (pcb != NULL) {
1723  LWIP_ASSERT("invalid socket state for recv callback", pcb->state != LISTEN);
1724  pcb->recv = recv;
1725  }
1726 }
1727 
1736 void
1737 tcp_sent(struct tcp_pcb *pcb, tcp_sent_fn sent)
1738 {
1739  if (pcb != NULL) {
1740  LWIP_ASSERT("invalid socket state for sent callback", pcb->state != LISTEN);
1741  pcb->sent = sent;
1742  }
1743 }
1744 
1756 void
1757 tcp_err(struct tcp_pcb *pcb, tcp_err_fn err)
1758 {
1759  if (pcb != NULL) {
1760  LWIP_ASSERT("invalid socket state for err callback", pcb->state != LISTEN);
1761  pcb->errf = err;
1762  }
1763 }
1764 
1774 void
1775 tcp_accept(struct tcp_pcb *pcb, tcp_accept_fn accept)
1776 {
1777  if ((pcb != NULL) && (pcb->state == LISTEN)) {
1778  struct tcp_pcb_listen *lpcb = (struct tcp_pcb_listen*)pcb;
1779  lpcb->accept = accept;
1780  }
1781 }
1782 #endif /* LWIP_CALLBACK_API */
1783 
1784 
1792 void
1793 tcp_poll(struct tcp_pcb *pcb, tcp_poll_fn poll, u8_t interval)
1794 {
1795  LWIP_ASSERT("invalid socket state for poll", pcb->state != LISTEN);
1796 #if LWIP_CALLBACK_API
1797  pcb->poll = poll;
1798 #else /* LWIP_CALLBACK_API */
1799  LWIP_UNUSED_ARG(poll);
1800 #endif /* LWIP_CALLBACK_API */
1801  pcb->pollinterval = interval;
1802 }
1803 
1810 void
1811 tcp_pcb_purge(struct tcp_pcb *pcb)
1812 {
1813  if (pcb->state != CLOSED &&
1814  pcb->state != TIME_WAIT &&
1815  pcb->state != LISTEN) {
1816 
1817  LWIP_DEBUGF(TCP_DEBUG, ("tcp_pcb_purge\n"));
1818 
1819  tcp_backlog_accepted(pcb);
1820 
1821  if (pcb->refused_data != NULL) {
1822  LWIP_DEBUGF(TCP_DEBUG, ("tcp_pcb_purge: data left on ->refused_data\n"));
1823  pbuf_free(pcb->refused_data);
1824  pcb->refused_data = NULL;
1825  }
1826  if (pcb->unsent != NULL) {
1827  LWIP_DEBUGF(TCP_DEBUG, ("tcp_pcb_purge: not all data sent\n"));
1828  }
1829  if (pcb->unacked != NULL) {
1830  LWIP_DEBUGF(TCP_DEBUG, ("tcp_pcb_purge: data left on ->unacked\n"));
1831  }
1832 #if TCP_QUEUE_OOSEQ
1833  if (pcb->ooseq != NULL) {
1834  LWIP_DEBUGF(TCP_DEBUG, ("tcp_pcb_purge: data left on ->ooseq\n"));
1835  }
1836  tcp_segs_free(pcb->ooseq);
1837  pcb->ooseq = NULL;
1838 #endif /* TCP_QUEUE_OOSEQ */
1839 
1840  /* Stop the retransmission timer as it will expect data on unacked
1841  queue if it fires */
1842  pcb->rtime = -1;
1843 
1844  tcp_segs_free(pcb->unsent);
1845  tcp_segs_free(pcb->unacked);
1846  pcb->unacked = pcb->unsent = NULL;
1847 #if TCP_OVERSIZE
1848  pcb->unsent_oversize = 0;
1849 #endif /* TCP_OVERSIZE */
1850  }
1851 }
1852 
1859 void
1860 tcp_pcb_remove(struct tcp_pcb **pcblist, struct tcp_pcb *pcb)
1861 {
1862  TCP_RMV(pcblist, pcb);
1863 
1864  tcp_pcb_purge(pcb);
1865 
1866  /* if there is an outstanding delayed ACKs, send it */
1867  if (pcb->state != TIME_WAIT &&
1868  pcb->state != LISTEN &&
1869  pcb->flags & TF_ACK_DELAY) {
1870  pcb->flags |= TF_ACK_NOW;
1871  tcp_output(pcb);
1872  }
1873 
1874  if (pcb->state != LISTEN) {
1875  LWIP_ASSERT("unsent segments leaking", pcb->unsent == NULL);
1876  LWIP_ASSERT("unacked segments leaking", pcb->unacked == NULL);
1877 #if TCP_QUEUE_OOSEQ
1878  LWIP_ASSERT("ooseq segments leaking", pcb->ooseq == NULL);
1879 #endif /* TCP_QUEUE_OOSEQ */
1880  }
1881 
1882  pcb->state = CLOSED;
1883  /* reset the local port to prevent the pcb from being 'bound' */
1884  pcb->local_port = 0;
1885 
1886  LWIP_ASSERT("tcp_pcb_remove: tcp_pcbs_sane()", tcp_pcbs_sane());
1887 }
1888 
1894 u32_t
1895 tcp_next_iss(struct tcp_pcb *pcb)
1896 {
1897 #ifdef LWIP_HOOK_TCP_ISN
1898  return LWIP_HOOK_TCP_ISN(&pcb->local_ip, pcb->local_port, &pcb->remote_ip, pcb->remote_port);
1899 #else /* LWIP_HOOK_TCP_ISN */
1900  static u32_t iss = 6510;
1901 
1902  LWIP_UNUSED_ARG(pcb);
1903 
1904  iss += tcp_ticks; /* XXX */
1905  return iss;
1906 #endif /* LWIP_HOOK_TCP_ISN */
1907 }
1908 
1909 #if TCP_CALCULATE_EFF_SEND_MSS
1910 
1915 u16_t
1916 tcp_eff_send_mss_impl(u16_t sendmss, const ip_addr_t *dest
1917 #if LWIP_IPV6 || LWIP_IPV4_SRC_ROUTING
1918  , const ip_addr_t *src
1919 #endif /* LWIP_IPV6 || LWIP_IPV4_SRC_ROUTING */
1920  )
1921 {
1922  u16_t mss_s;
1923  struct netif *outif;
1924  s16_t mtu;
1925 
1926  outif = ip_route(src, dest);
1927 #if LWIP_IPV6
1928 #if LWIP_IPV4
1929  if (IP_IS_V6(dest))
1930 #endif /* LWIP_IPV4 */
1931  {
1932  /* First look in destination cache, to see if there is a Path MTU. */
1933  mtu = nd6_get_destination_mtu(ip_2_ip6(dest), outif);
1934  }
1935 #if LWIP_IPV4
1936  else
1937 #endif /* LWIP_IPV4 */
1938 #endif /* LWIP_IPV6 */
1939 #if LWIP_IPV4
1940  {
1941  if (outif == NULL) {
1942  return sendmss;
1943  }
1944  mtu = outif->mtu;
1945  }
1946 #endif /* LWIP_IPV4 */
1947 
1948  if (mtu != 0) {
1949 #if LWIP_IPV6
1950 #if LWIP_IPV4
1951  if (IP_IS_V6(dest))
1952 #endif /* LWIP_IPV4 */
1953  {
1954  mss_s = mtu - IP6_HLEN - TCP_HLEN;
1955  }
1956 #if LWIP_IPV4
1957  else
1958 #endif /* LWIP_IPV4 */
1959 #endif /* LWIP_IPV6 */
1960 #if LWIP_IPV4
1961  {
1962  mss_s = mtu - IP_HLEN - TCP_HLEN;
1963  }
1964 #endif /* LWIP_IPV4 */
1965  /* RFC 1122, chap 4.2.2.6:
1966  * Eff.snd.MSS = min(SendMSS+20, MMS_S) - TCPhdrsize - IPoptionsize
1967  * We correct for TCP options in tcp_write(), and don't support IP options.
1968  */
1969  sendmss = LWIP_MIN(sendmss, mss_s);
1970  }
1971  return sendmss;
1972 }
1973 #endif /* TCP_CALCULATE_EFF_SEND_MSS */
1974 
1976 static void
1977 tcp_netif_ip_addr_changed_pcblist(const ip_addr_t* old_addr, struct tcp_pcb* pcb_list)
1978 {
1979  struct tcp_pcb *pcb;
1980  pcb = pcb_list;
1981  while (pcb != NULL) {
1982  /* PCB bound to current local interface address? */
1983  if (ip_addr_cmp(&pcb->local_ip, old_addr)
1984 #if LWIP_AUTOIP
1985  /* connections to link-local addresses must persist (RFC3927 ch. 1.9) */
1986  && (!IP_IS_V4_VAL(pcb->local_ip) || !ip4_addr_islinklocal(ip_2_ip4(&pcb->local_ip)))
1987 #endif /* LWIP_AUTOIP */
1988  ) {
1989  /* this connection must be aborted */
1990  struct tcp_pcb *next = pcb->next;
1991  LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_STATE, ("netif_set_ipaddr: aborting TCP pcb %p\n", (void *)pcb));
1992  tcp_abort(pcb);
1993  pcb = next;
1994  } else {
1995  pcb = pcb->next;
1996  }
1997  }
1998 }
1999 
2005 void
2006 tcp_netif_ip_addr_changed(const ip_addr_t* old_addr, const ip_addr_t* new_addr)
2007 {
2008  struct tcp_pcb_listen *lpcb, *next;
2009 
2010  if (!ip_addr_isany(old_addr)) {
2011  tcp_netif_ip_addr_changed_pcblist(old_addr, tcp_active_pcbs);
2012  tcp_netif_ip_addr_changed_pcblist(old_addr, tcp_bound_pcbs);
2013 
2014  if (!ip_addr_isany(new_addr)) {
2015  /* PCB bound to current local interface address? */
2016  for (lpcb = tcp_listen_pcbs.listen_pcbs; lpcb != NULL; lpcb = next) {
2017  next = lpcb->next;
2018  /* PCB bound to current local interface address? */
2019  if (ip_addr_cmp(&lpcb->local_ip, old_addr)) {
2020  /* The PCB is listening to the old ipaddr and
2021  * is set to listen to the new one instead */
2022  ip_addr_copy(lpcb->local_ip, *new_addr);
2023  }
2024  }
2025  }
2026  }
2027 }
2028 
2029 const char*
2030 tcp_debug_state_str(enum tcp_state s)
2031 {
2032  return tcp_state_str[s];
2033 }
2034 
2035 #if TCP_DEBUG || TCP_INPUT_DEBUG || TCP_OUTPUT_DEBUG
2036 
2041 void
2042 tcp_debug_print(struct tcp_hdr *tcphdr)
2043 {
2044  LWIP_DEBUGF(TCP_DEBUG, ("TCP header:\n"));
2045  LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n"));
2046  LWIP_DEBUGF(TCP_DEBUG, ("| %5"U16_F" | %5"U16_F" | (src port, dest port)\n",
2047  lwip_ntohs(tcphdr->src), lwip_ntohs(tcphdr->dest)));
2048  LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n"));
2049  LWIP_DEBUGF(TCP_DEBUG, ("| %010"U32_F" | (seq no)\n",
2050  lwip_ntohl(tcphdr->seqno)));
2051  LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n"));
2052  LWIP_DEBUGF(TCP_DEBUG, ("| %010"U32_F" | (ack no)\n",
2053  lwip_ntohl(tcphdr->ackno)));
2054  LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n"));
2055  LWIP_DEBUGF(TCP_DEBUG, ("| %2"U16_F" | |%"U16_F"%"U16_F"%"U16_F"%"U16_F"%"U16_F"%"U16_F"| %5"U16_F" | (hdrlen, flags (",
2056  TCPH_HDRLEN(tcphdr),
2057  (u16_t)(TCPH_FLAGS(tcphdr) >> 5 & 1),
2058  (u16_t)(TCPH_FLAGS(tcphdr) >> 4 & 1),
2059  (u16_t)(TCPH_FLAGS(tcphdr) >> 3 & 1),
2060  (u16_t)(TCPH_FLAGS(tcphdr) >> 2 & 1),
2061  (u16_t)(TCPH_FLAGS(tcphdr) >> 1 & 1),
2062  (u16_t)(TCPH_FLAGS(tcphdr) & 1),
2063  lwip_ntohs(tcphdr->wnd)));
2064  tcp_debug_print_flags(TCPH_FLAGS(tcphdr));
2065  LWIP_DEBUGF(TCP_DEBUG, ("), win)\n"));
2066  LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n"));
2067  LWIP_DEBUGF(TCP_DEBUG, ("| 0x%04"X16_F" | %5"U16_F" | (chksum, urgp)\n",
2068  lwip_ntohs(tcphdr->chksum), lwip_ntohs(tcphdr->urgp)));
2069  LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n"));
2070 }
2071 
2077 void
2078 tcp_debug_print_state(enum tcp_state s)
2079 {
2080  LWIP_DEBUGF(TCP_DEBUG, ("State: %s\n", tcp_state_str[s]));
2081 }
2082 
2088 void
2089 tcp_debug_print_flags(u8_t flags)
2090 {
2091  if (flags & TCP_FIN) {
2092  LWIP_DEBUGF(TCP_DEBUG, ("FIN "));
2093  }
2094  if (flags & TCP_SYN) {
2095  LWIP_DEBUGF(TCP_DEBUG, ("SYN "));
2096  }
2097  if (flags & TCP_RST) {
2098  LWIP_DEBUGF(TCP_DEBUG, ("RST "));
2099  }
2100  if (flags & TCP_PSH) {
2101  LWIP_DEBUGF(TCP_DEBUG, ("PSH "));
2102  }
2103  if (flags & TCP_ACK) {
2104  LWIP_DEBUGF(TCP_DEBUG, ("ACK "));
2105  }
2106  if (flags & TCP_URG) {
2107  LWIP_DEBUGF(TCP_DEBUG, ("URG "));
2108  }
2109  if (flags & TCP_ECE) {
2110  LWIP_DEBUGF(TCP_DEBUG, ("ECE "));
2111  }
2112  if (flags & TCP_CWR) {
2113  LWIP_DEBUGF(TCP_DEBUG, ("CWR "));
2114  }
2115  LWIP_DEBUGF(TCP_DEBUG, ("\n"));
2116 }
2117 
2121 void
2122 tcp_debug_print_pcbs(void)
2123 {
2124  struct tcp_pcb *pcb;
2125  struct tcp_pcb_listen *pcbl;
2126 
2127  LWIP_DEBUGF(TCP_DEBUG, ("Active PCB states:\n"));
2128  for (pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) {
2129  LWIP_DEBUGF(TCP_DEBUG, ("Local port %"U16_F", foreign port %"U16_F" snd_nxt %"U32_F" rcv_nxt %"U32_F" ",
2130  pcb->local_port, pcb->remote_port,
2131  pcb->snd_nxt, pcb->rcv_nxt));
2132  tcp_debug_print_state(pcb->state);
2133  }
2134 
2135  LWIP_DEBUGF(TCP_DEBUG, ("Listen PCB states:\n"));
2136  for (pcbl = tcp_listen_pcbs.listen_pcbs; pcbl != NULL; pcbl = pcbl->next) {
2137  LWIP_DEBUGF(TCP_DEBUG, ("Local port %"U16_F" ", pcbl->local_port));
2138  tcp_debug_print_state(pcbl->state);
2139  }
2140 
2141  LWIP_DEBUGF(TCP_DEBUG, ("TIME-WAIT PCB states:\n"));
2142  for (pcb = tcp_tw_pcbs; pcb != NULL; pcb = pcb->next) {
2143  LWIP_DEBUGF(TCP_DEBUG, ("Local port %"U16_F", foreign port %"U16_F" snd_nxt %"U32_F" rcv_nxt %"U32_F" ",
2144  pcb->local_port, pcb->remote_port,
2145  pcb->snd_nxt, pcb->rcv_nxt));
2146  tcp_debug_print_state(pcb->state);
2147  }
2148 }
2149 
2153 s16_t
2154 tcp_pcbs_sane(void)
2155 {
2156  struct tcp_pcb *pcb;
2157  for (pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) {
2158  LWIP_ASSERT("tcp_pcbs_sane: active pcb->state != CLOSED", pcb->state != CLOSED);
2159  LWIP_ASSERT("tcp_pcbs_sane: active pcb->state != LISTEN", pcb->state != LISTEN);
2160  LWIP_ASSERT("tcp_pcbs_sane: active pcb->state != TIME-WAIT", pcb->state != TIME_WAIT);
2161  }
2162  for (pcb = tcp_tw_pcbs; pcb != NULL; pcb = pcb->next) {
2163  LWIP_ASSERT("tcp_pcbs_sane: tw pcb->state == TIME-WAIT", pcb->state == TIME_WAIT);
2164  }
2165  return 1;
2166 }
2167 #endif /* TCP_DEBUG */
2168 
2169 #endif /* LWIP_TCP */
TCP_SYN
#define TCP_SYN
Definition: tcp.h:73
TCP_SND_BUF
#define TCP_SND_BUF
Definition: lwipopts.h:184
S16_F
#define S16_F
Definition: arch.h:151
TCP_ACK
#define TCP_ACK
Definition: tcp.h:76
lwip_ntohs
#define lwip_ntohs(x)
Definition: def.h:76
ip_route_get_local_ip
#define ip_route_get_local_ip(src, dest, netif, ipaddr)
Definition: ip.h:308
opt.h
ip_addr_cmp
#define ip_addr_cmp(addr1, addr2)
Definition: ip_addr.h:316
IP_IS_V6
#define IP_IS_V6(ipaddr)
Definition: ip_addr.h:296
s16_t
int16_t s16_t
Definition: arch.h:125
def.h
IP_IS_V6_VAL
#define IP_IS_V6_VAL(ipaddr)
Definition: ip_addr.h:294
nd6.h
LWIP_ASSERT
#define LWIP_ASSERT(message, assertion)
Definition: debug.h:116
ERR_ABRT
Definition: err.h:90
LWIP_IPV6
#define LWIP_IPV6
Definition: lwipopts.h:366
ERR_BUF
Definition: err.h:67
u16_t
uint16_t u16_t
Definition: arch.h:124
string.h
LWIP_ARRAYSIZE
#define LWIP_ARRAYSIZE(x)
Definition: def.h:58
TCP_CWND_DEBUG
#define TCP_CWND_DEBUG
Definition: lwipopts.h:471
ip_addr_set
#define ip_addr_set(dest, src)
Definition: ip_addr.h:307
ip_addr_isany
#define ip_addr_isany(ipaddr)
Definition: ip_addr.h:317
pbuf::tot_len
u16_t tot_len
Definition: pbuf.h:156
u32_t
uint32_t u32_t
Definition: arch.h:126
NETIF_DEBUG
#define NETIF_DEBUG
Definition: lwipopts.h:433
LWIP_AUTOIP
#define LWIP_AUTOIP
Definition: lwipopts.h:151
pbuf_free
u8_t pbuf_free(struct pbuf *p)
Definition: pbuf.c:715
netif::mtu
u16_t mtu
Definition: netif.h:307
memp_malloc
void * memp_malloc(memp_t type)
Definition: memp.c:385
S32_F
#define S32_F
Definition: arch.h:160
PBUF_FLAG_TCP_FIN
#define PBUF_FLAG_TCP_FIN
Definition: pbuf.h:139
X16_F
#define X16_F
Definition: arch.h:154
LWIP_MIN
#define LWIP_MIN(x, y)
Definition: def.h:55
memp_free
void memp_free(memp_t type, void *mem)
Definition: memp.c:469
IP_HLEN
#define IP_HLEN
Definition: ip4.h:64
TCP_ECE
#define TCP_ECE
Definition: tcp.h:78
pbuf::flags
u8_t flags
Definition: pbuf.h:165
ip_addr_debug_print
#define ip_addr_debug_print(debug, ipaddr)
Definition: ip_addr.h:323
ERR_INPROGRESS
Definition: err.h:73
TCP_RST_DEBUG
#define TCP_RST_DEBUG
Definition: lwipopts.h:477
TCP_QUEUE_OOSEQ
#define TCP_QUEUE_OOSEQ
Definition: lwipopts.h:178
stats.h
ERR_MEM
Definition: err.h:65
ERR_ISCONN
Definition: err.h:83
LWIP_ERROR
#define LWIP_ERROR(message, expression, handler)
Definition: debug.h:135
ERR_CLSD
Definition: err.h:94
lwip_ntohl
#define lwip_ntohl(x)
Definition: def.h:78
u8_t
uint8_t u8_t
Definition: arch.h:122
ip_addr_t
ip6_addr_t ip_addr_t
Definition: ip_addr.h:290
ERR_CONN
Definition: err.h:85
TCP_RST
#define TCP_RST
Definition: tcp.h:74
TCP_DEBUG
#define TCP_DEBUG
Definition: lwipopts.h:463
tcp_priv.h
LWIP_WND_SCALE
#define LWIP_WND_SCALE
Definition: lwipopts.h:207
netif
Definition: netif.h:233
ip_2_ip6
#define ip_2_ip6(ipaddr)
Definition: ip_addr.h:301
MIB2_STATS_INC
#define MIB2_STATS_INC(x)
Definition: stats.h:467
LWIP_UNUSED_ARG
#define LWIP_UNUSED_ARG(x)
Definition: arch.h:315
TCP_FIN
#define TCP_FIN
Definition: tcp.h:72
reset
void reset(void)
Definition: fdc.c:288
IP_IS_V4_VAL
#define IP_IS_V4_VAL(ipaddr)
Definition: ip_addr.h:293
pbuf_ref
void pbuf_ref(struct pbuf *p)
Definition: pbuf.c:821
pbuf_cat
void pbuf_cat(struct pbuf *head, struct pbuf *tail)
Definition: pbuf.c:841
SOF_KEEPALIVE
#define SOF_KEEPALIVE
Definition: ip.h:98
U16_F
#define U16_F
Definition: arch.h:148
TCP_WND_UPDATE_THRESHOLD
#define TCP_WND_UPDATE_THRESHOLD
Definition: lwipopts.h:204
TCP_WND
#define TCP_WND
Definition: lwipopts.h:172
ERR_OK
Definition: err.h:63
SOF_REUSEADDR
#define SOF_REUSEADDR
Definition: ip.h:97
err_t
s8_t err_t
Definition: err.h:57
ip_addr_copy
#define ip_addr_copy(dest, src)
Definition: ip_addr.h:305
tcp.h
TCP_TTL
#define TCP_TTL
Definition: lwipopts.h:171
MEMP_STATS_DEC
#define MEMP_STATS_DEC(x, i)
Definition: stats.h:407
IP6_HLEN
#define IP6_HLEN
Definition: ip6.h:62
ip6_addr.h
TCP_INPUT_DEBUG
#define TCP_INPUT_DEBUG
Definition: lwipopts.h:465
memset
void * memset(void *dst, int c, size_t length)
IP_SET_TYPE_VAL
#define IP_SET_TYPE_VAL(ipaddr, iptype)
Definition: ip_addr.h:298
TCP_PSH
#define TCP_PSH
Definition: tcp.h:75
TCP_HLEN
#define TCP_HLEN
Definition: tcp.h:47
TCP_URG
#define TCP_URG
Definition: tcp.h:77
TCPH_HDRLEN
#define TCPH_HDRLEN(phdr)
Definition: tcp.h:83
ip_get_option
#define ip_get_option(pcb, opt)
Definition: ip.h:215
tcp_hdr
Definition: tcp.h:56
ERR_ALREADY
Definition: err.h:81
TCP_CWR
#define TCP_CWR
Definition: tcp.h:79
U32_F
#define U32_F
Definition: arch.h:157
memp.h
TCP_RTO_DEBUG
#define TCP_RTO_DEBUG
Definition: lwipopts.h:469
ERR_USE
Definition: err.h:79
mem.h
ERR_RTE
Definition: err.h:71
SMEMCPY
#define SMEMCPY(dst, src, len)
Definition: lwipopts.h:44
ERR_VAL
Definition: err.h:75
pbuf
Definition: pbuf.h:142
LWIP_DEBUGF
#define LWIP_DEBUGF(debug, message)
Definition: debug.h:164
TCP_MAXRTX
#define TCP_MAXRTX
Definition: lwipopts.h:174
ip6.h
LWIP_DBG_STATE
#define LWIP_DBG_STATE
Definition: debug.h:85
TCPH_FLAGS
#define TCPH_FLAGS(phdr)
Definition: tcp.h:84
TCP_SYNMAXRTX
#define TCP_SYNMAXRTX
Definition: lwipopts.h:176
debug.h
NULL
#define NULL
Definition: fat_string.h:17