uc-sdk
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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/snmp.h"
51 #include "lwip/tcp.h"
52 #include "lwip/tcp_impl.h"
53 #include "lwip/debug.h"
54 #include "lwip/stats.h"
55 
56 #include <string.h>
57 
58 const char * const tcp_state_str[] = {
59  "CLOSED",
60  "LISTEN",
61  "SYN_SENT",
62  "SYN_RCVD",
63  "ESTABLISHED",
64  "FIN_WAIT_1",
65  "FIN_WAIT_2",
66  "CLOSE_WAIT",
67  "CLOSING",
68  "LAST_ACK",
69  "TIME_WAIT"
70 };
71 
72 /* Incremented every coarse grained timer shot (typically every 500 ms). */
73 u32_t tcp_ticks;
74 const u8_t tcp_backoff[13] =
75  { 1, 2, 3, 4, 5, 6, 7, 7, 7, 7, 7, 7, 7};
76  /* Times per slowtmr hits */
77 const u8_t tcp_persist_backoff[7] = { 3, 6, 12, 24, 48, 96, 120 };
78 
79 /* The TCP PCB lists. */
80 
82 struct tcp_pcb *tcp_bound_pcbs;
84 union tcp_listen_pcbs_t tcp_listen_pcbs;
87 struct tcp_pcb *tcp_active_pcbs;
89 struct tcp_pcb *tcp_tw_pcbs;
90 
91 #define NUM_TCP_PCB_LISTS 4
92 #define NUM_TCP_PCB_LISTS_NO_TIME_WAIT 3
93 
94 struct tcp_pcb **tcp_pcb_lists[] = {&tcp_listen_pcbs.pcbs, &tcp_bound_pcbs,
95  &tcp_active_pcbs, &tcp_tw_pcbs};
96 
98 struct tcp_pcb *tcp_tmp_pcb;
99 
101 static u8_t tcp_timer;
102 static u16_t tcp_new_port(void);
103 
108 void
109 tcp_tmr(void)
110 {
111  /* Call tcp_fasttmr() every 250 ms */
112  tcp_fasttmr();
113 
114  if (++tcp_timer & 1) {
115  /* Call tcp_tmr() every 500 ms, i.e., every other timer
116  tcp_tmr() is called. */
117  tcp_slowtmr();
118  }
119 }
120 
137 static err_t
138 tcp_close_shutdown(struct tcp_pcb *pcb, u8_t rst_on_unacked_data)
139 {
140  err_t err;
141 
142  if (rst_on_unacked_data && (pcb->state != LISTEN)) {
143  if ((pcb->refused_data != NULL) || (pcb->rcv_wnd != TCP_WND)) {
144  /* Not all data received by application, send RST to tell the remote
145  side about this. */
146  LWIP_ASSERT("pcb->flags & TF_RXCLOSED", pcb->flags & TF_RXCLOSED);
147 
148  /* don't call tcp_abort here: we must not deallocate the pcb since
149  that might not be expected when calling tcp_close */
150  tcp_rst(pcb->snd_nxt, pcb->rcv_nxt, &pcb->local_ip, &pcb->remote_ip,
151  pcb->local_port, pcb->remote_port);
152 
153  tcp_pcb_purge(pcb);
154 
155  /* TODO: to which state do we move now? */
156 
157  /* move to TIME_WAIT since we close actively */
158  TCP_RMV(&tcp_active_pcbs, pcb);
159  pcb->state = TIME_WAIT;
160  TCP_REG(&tcp_tw_pcbs, pcb);
161 
162  return ERR_OK;
163  }
164  }
165 
166  switch (pcb->state) {
167  case CLOSED:
168  /* Closing a pcb in the CLOSED state might seem erroneous,
169  * however, it is in this state once allocated and as yet unused
170  * and the user needs some way to free it should the need arise.
171  * Calling tcp_close() with a pcb that has already been closed, (i.e. twice)
172  * or for a pcb that has been used and then entered the CLOSED state
173  * is erroneous, but this should never happen as the pcb has in those cases
174  * been freed, and so any remaining handles are bogus. */
175  err = ERR_OK;
176  TCP_RMV(&tcp_bound_pcbs, pcb);
177  memp_free(MEMP_TCP_PCB, pcb);
178  pcb = NULL;
179  break;
180  case LISTEN:
181  err = ERR_OK;
182  tcp_pcb_remove(&tcp_listen_pcbs.pcbs, pcb);
183  memp_free(MEMP_TCP_PCB_LISTEN, pcb);
184  pcb = NULL;
185  break;
186  case SYN_SENT:
187  err = ERR_OK;
188  tcp_pcb_remove(&tcp_active_pcbs, pcb);
189  memp_free(MEMP_TCP_PCB, pcb);
190  pcb = NULL;
192  break;
193  case SYN_RCVD:
194  err = tcp_send_fin(pcb);
195  if (err == ERR_OK) {
197  pcb->state = FIN_WAIT_1;
198  }
199  break;
200  case ESTABLISHED:
201  err = tcp_send_fin(pcb);
202  if (err == ERR_OK) {
204  pcb->state = FIN_WAIT_1;
205  }
206  break;
207  case CLOSE_WAIT:
208  err = tcp_send_fin(pcb);
209  if (err == ERR_OK) {
211  pcb->state = LAST_ACK;
212  }
213  break;
214  default:
215  /* Has already been closed, do nothing. */
216  err = ERR_OK;
217  pcb = NULL;
218  break;
219  }
220 
221  if (pcb != NULL && err == ERR_OK) {
222  /* To ensure all data has been sent when tcp_close returns, we have
223  to make sure tcp_output doesn't fail.
224  Since we don't really have to ensure all data has been sent when tcp_close
225  returns (unsent data is sent from tcp timer functions, also), we don't care
226  for the return value of tcp_output for now. */
227  /* @todo: When implementing SO_LINGER, this must be changed somehow:
228  If SOF_LINGER is set, the data should be sent and acked before close returns.
229  This can only be valid for sequential APIs, not for the raw API. */
230  tcp_output(pcb);
231  }
232  return err;
233 }
234 
249 err_t
250 tcp_close(struct tcp_pcb *pcb)
251 {
252 #if TCP_DEBUG
253  LWIP_DEBUGF(TCP_DEBUG, ("tcp_close: closing in "));
254  tcp_debug_print_state(pcb->state);
255 #endif /* TCP_DEBUG */
256 
257  if (pcb->state != LISTEN) {
258  /* Set a flag not to receive any more data... */
259  pcb->flags |= TF_RXCLOSED;
260  }
261  /* ... and close */
262  return tcp_close_shutdown(pcb, 1);
263 }
264 
275 err_t
276 tcp_shutdown(struct tcp_pcb *pcb, int shut_rx, int shut_tx)
277 {
278  if (pcb->state == LISTEN) {
279  return ERR_CONN;
280  }
281  if (shut_rx) {
282  /* shut down the receive side: free buffered data... */
283  if (pcb->refused_data != NULL) {
284  pbuf_free(pcb->refused_data);
285  pcb->refused_data = NULL;
286  }
287  /* ... and set a flag not to receive any more data */
288  pcb->flags |= TF_RXCLOSED;
289  }
290  if (shut_tx) {
291  /* This can't happen twice since if it succeeds, the pcb's state is changed.
292  Only close in these states as the others directly deallocate the PCB */
293  switch (pcb->state) {
294  case SYN_RCVD:
295  case ESTABLISHED:
296  case CLOSE_WAIT:
297  return tcp_close_shutdown(pcb, 0);
298  default:
299  /* don't shut down other states */
300  break;
301  }
302  }
303  /* @todo: return another err_t if not in correct state or already shut? */
304  return ERR_OK;
305 }
306 
315 void
316 tcp_abandon(struct tcp_pcb *pcb, int reset)
317 {
318  u32_t seqno, ackno;
319  u16_t remote_port, local_port;
320  ip_addr_t remote_ip, local_ip;
321 #if LWIP_CALLBACK_API
322  tcp_err_fn errf;
323 #endif /* LWIP_CALLBACK_API */
324  void *errf_arg;
325 
326  /* pcb->state LISTEN not allowed here */
327  LWIP_ASSERT("don't call tcp_abort/tcp_abandon for listen-pcbs",
328  pcb->state != LISTEN);
329  /* Figure out on which TCP PCB list we are, and remove us. If we
330  are in an active state, call the receive function associated with
331  the PCB with a NULL argument, and send an RST to the remote end. */
332  if (pcb->state == TIME_WAIT) {
333  tcp_pcb_remove(&tcp_tw_pcbs, pcb);
334  memp_free(MEMP_TCP_PCB, pcb);
335  } else {
336  seqno = pcb->snd_nxt;
337  ackno = pcb->rcv_nxt;
338  ip_addr_copy(local_ip, pcb->local_ip);
339  ip_addr_copy(remote_ip, pcb->remote_ip);
340  local_port = pcb->local_port;
341  remote_port = pcb->remote_port;
342 #if LWIP_CALLBACK_API
343  errf = pcb->errf;
344 #endif /* LWIP_CALLBACK_API */
345  errf_arg = pcb->callback_arg;
346  tcp_pcb_remove(&tcp_active_pcbs, pcb);
347  if (pcb->unacked != NULL) {
348  tcp_segs_free(pcb->unacked);
349  }
350  if (pcb->unsent != NULL) {
351  tcp_segs_free(pcb->unsent);
352  }
353 #if TCP_QUEUE_OOSEQ
354  if (pcb->ooseq != NULL) {
355  tcp_segs_free(pcb->ooseq);
356  }
357 #endif /* TCP_QUEUE_OOSEQ */
358  memp_free(MEMP_TCP_PCB, pcb);
359  TCP_EVENT_ERR(errf, errf_arg, ERR_ABRT);
360  if (reset) {
361  LWIP_DEBUGF(TCP_RST_DEBUG, ("tcp_abandon: sending RST\n"));
362  tcp_rst(seqno, ackno, &local_ip, &remote_ip, local_port, remote_port);
363  }
364  }
365 }
366 
377 void
378 tcp_abort(struct tcp_pcb *pcb)
379 {
380  tcp_abandon(pcb, 1);
381 }
382 
396 err_t
397 tcp_bind(struct tcp_pcb *pcb, ip_addr_t *ipaddr, u16_t port)
398 {
399  int i;
400  int max_pcb_list = NUM_TCP_PCB_LISTS;
401  struct tcp_pcb *cpcb;
402 
403  LWIP_ERROR("tcp_bind: can only bind in state CLOSED", pcb->state == CLOSED, return ERR_ISCONN);
404 
405 #if SO_REUSE
406  /* Unless the REUSEADDR flag is set,
407  we have to check the pcbs in TIME-WAIT state, also.
408  We do not dump TIME_WAIT pcb's; they can still be matched by incoming
409  packets using both local and remote IP addresses and ports to distinguish.
410  */
411  if ((pcb->so_options & SOF_REUSEADDR) != 0) {
412  max_pcb_list = NUM_TCP_PCB_LISTS_NO_TIME_WAIT;
413  }
414 #endif /* SO_REUSE */
415 
416  if (port == 0) {
417  port = tcp_new_port();
418  }
419 
420  /* Check if the address already is in use (on all lists) */
421  for (i = 0; i < max_pcb_list; i++) {
422  for(cpcb = *tcp_pcb_lists[i]; cpcb != NULL; cpcb = cpcb->next) {
423  if (cpcb->local_port == port) {
424 #if SO_REUSE
425  /* Omit checking for the same port if both pcbs have REUSEADDR set.
426  For SO_REUSEADDR, the duplicate-check for a 5-tuple is done in
427  tcp_connect. */
428  if (((pcb->so_options & SOF_REUSEADDR) == 0) ||
429  ((cpcb->so_options & SOF_REUSEADDR) == 0))
430 #endif /* SO_REUSE */
431  {
432  if (ip_addr_isany(&(cpcb->local_ip)) ||
433  ip_addr_isany(ipaddr) ||
434  ip_addr_cmp(&(cpcb->local_ip), ipaddr)) {
435  return ERR_USE;
436  }
437  }
438  }
439  }
440  }
441 
442  if (!ip_addr_isany(ipaddr)) {
443  pcb->local_ip = *ipaddr;
444  }
445  pcb->local_port = port;
446  TCP_REG(&tcp_bound_pcbs, pcb);
447  LWIP_DEBUGF(TCP_DEBUG, ("tcp_bind: bind to port %"U16_F"\n", port));
448  return ERR_OK;
449 }
450 #if LWIP_CALLBACK_API
451 
454 static err_t
455 tcp_accept_null(void *arg, struct tcp_pcb *pcb, err_t err)
456 {
457  LWIP_UNUSED_ARG(arg);
458  LWIP_UNUSED_ARG(pcb);
459  LWIP_UNUSED_ARG(err);
460 
461  return ERR_ABRT;
462 }
463 #endif /* LWIP_CALLBACK_API */
464 
479 struct tcp_pcb *
480 tcp_listen_with_backlog(struct tcp_pcb *pcb, u8_t backlog)
481 {
482  struct tcp_pcb_listen *lpcb;
483 
484  LWIP_UNUSED_ARG(backlog);
485  LWIP_ERROR("tcp_listen: pcb already connected", pcb->state == CLOSED, return NULL);
486 
487  /* already listening? */
488  if (pcb->state == LISTEN) {
489  return pcb;
490  }
491 #if SO_REUSE
492  if ((pcb->so_options & SOF_REUSEADDR) != 0) {
493  /* Since SOF_REUSEADDR allows reusing a local address before the pcb's usage
494  is declared (listen-/connection-pcb), we have to make sure now that
495  this port is only used once for every local IP. */
496  for(lpcb = tcp_listen_pcbs.listen_pcbs; lpcb != NULL; lpcb = lpcb->next) {
497  if (lpcb->local_port == pcb->local_port) {
498  if (ip_addr_cmp(&lpcb->local_ip, &pcb->local_ip)) {
499  /* this address/port is already used */
500  return NULL;
501  }
502  }
503  }
504  }
505 #endif /* SO_REUSE */
506  lpcb = (struct tcp_pcb_listen *)memp_malloc(MEMP_TCP_PCB_LISTEN);
507  if (lpcb == NULL) {
508  return NULL;
509  }
510  lpcb->callback_arg = pcb->callback_arg;
511  lpcb->local_port = pcb->local_port;
512  lpcb->state = LISTEN;
513  lpcb->prio = pcb->prio;
514  lpcb->so_options = pcb->so_options;
515  lpcb->so_options |= SOF_ACCEPTCONN;
516  lpcb->ttl = pcb->ttl;
517  lpcb->tos = pcb->tos;
518  ip_addr_copy(lpcb->local_ip, pcb->local_ip);
519  TCP_RMV(&tcp_bound_pcbs, pcb);
520  memp_free(MEMP_TCP_PCB, pcb);
521 #if LWIP_CALLBACK_API
522  lpcb->accept = tcp_accept_null;
523 #endif /* LWIP_CALLBACK_API */
524 #if TCP_LISTEN_BACKLOG
525  lpcb->accepts_pending = 0;
526  lpcb->backlog = (backlog ? backlog : 1);
527 #endif /* TCP_LISTEN_BACKLOG */
528  TCP_REG(&tcp_listen_pcbs.pcbs, (struct tcp_pcb *)lpcb);
529  return (struct tcp_pcb *)lpcb;
530 }
531 
538 u32_t tcp_update_rcv_ann_wnd(struct tcp_pcb *pcb)
539 {
540  u32_t new_right_edge = pcb->rcv_nxt + pcb->rcv_wnd;
541 
542  if (TCP_SEQ_GEQ(new_right_edge, pcb->rcv_ann_right_edge + LWIP_MIN((TCP_WND / 2), pcb->mss))) {
543  /* we can advertise more window */
544  pcb->rcv_ann_wnd = pcb->rcv_wnd;
545  return new_right_edge - pcb->rcv_ann_right_edge;
546  } else {
547  if (TCP_SEQ_GT(pcb->rcv_nxt, pcb->rcv_ann_right_edge)) {
548  /* Can happen due to other end sending out of advertised window,
549  * but within actual available (but not yet advertised) window */
550  pcb->rcv_ann_wnd = 0;
551  } else {
552  /* keep the right edge of window constant */
553  u32_t new_rcv_ann_wnd = pcb->rcv_ann_right_edge - pcb->rcv_nxt;
554  LWIP_ASSERT("new_rcv_ann_wnd <= 0xffff", new_rcv_ann_wnd <= 0xffff);
555  pcb->rcv_ann_wnd = (u16_t)new_rcv_ann_wnd;
556  }
557  return 0;
558  }
559 }
560 
569 void
570 tcp_recved(struct tcp_pcb *pcb, u16_t len)
571 {
572  int wnd_inflation;
573 
574  LWIP_ASSERT("tcp_recved: len would wrap rcv_wnd\n",
575  len <= 0xffff - pcb->rcv_wnd );
576 
577  pcb->rcv_wnd += len;
578  if (pcb->rcv_wnd > TCP_WND) {
579  pcb->rcv_wnd = TCP_WND;
580  }
581 
582  wnd_inflation = tcp_update_rcv_ann_wnd(pcb);
583 
584  /* If the change in the right edge of window is significant (default
585  * watermark is TCP_WND/4), then send an explicit update now.
586  * Otherwise wait for a packet to be sent in the normal course of
587  * events (or more window to be available later) */
588  if (wnd_inflation >= TCP_WND_UPDATE_THRESHOLD) {
589  tcp_ack_now(pcb);
590  tcp_output(pcb);
591  }
592 
593  LWIP_DEBUGF(TCP_DEBUG, ("tcp_recved: recveived %"U16_F" bytes, wnd %"U16_F" (%"U16_F").\n",
594  len, pcb->rcv_wnd, TCP_WND - pcb->rcv_wnd));
595 }
596 
603 static u16_t
604 tcp_new_port(void)
605 {
606  int i;
607  struct tcp_pcb *pcb;
608 #ifndef TCP_LOCAL_PORT_RANGE_START
609 #define TCP_LOCAL_PORT_RANGE_START 4096
610 #define TCP_LOCAL_PORT_RANGE_END 0x7fff
611 #endif
612  static u16_t port = TCP_LOCAL_PORT_RANGE_START;
613 
614  again:
615  if (++port > TCP_LOCAL_PORT_RANGE_END) {
616  port = TCP_LOCAL_PORT_RANGE_START;
617  }
618  /* Check all PCB lists. */
619  for (i = 0; i < NUM_TCP_PCB_LISTS; i++) {
620  for(pcb = *tcp_pcb_lists[i]; pcb != NULL; pcb = pcb->next) {
621  if (pcb->local_port == port) {
622  goto again;
623  }
624  }
625  }
626  return port;
627 }
628 
641 err_t
642 tcp_connect(struct tcp_pcb *pcb, ip_addr_t *ipaddr, u16_t port,
643  tcp_connected_fn connected)
644 {
645  err_t ret;
646  u32_t iss;
647 
648  LWIP_ERROR("tcp_connect: can only connected from state CLOSED", pcb->state == CLOSED, return ERR_ISCONN);
649 
650  LWIP_DEBUGF(TCP_DEBUG, ("tcp_connect to port %"U16_F"\n", port));
651  if (ipaddr != NULL) {
652  pcb->remote_ip = *ipaddr;
653  } else {
654  return ERR_VAL;
655  }
656  pcb->remote_port = port;
657 
658  /* check if we have a route to the remote host */
659  if (ip_addr_isany(&(pcb->local_ip))) {
660  /* no local IP address set, yet. */
661  struct netif *netif = ip_route(&(pcb->remote_ip));
662  if (netif == NULL) {
663  /* Don't even try to send a SYN packet if we have no route
664  since that will fail. */
665  return ERR_RTE;
666  }
667  /* Use the netif's IP address as local address. */
668  ip_addr_copy(pcb->local_ip, netif->ip_addr);
669  }
670 
671  if (pcb->local_port == 0) {
672  pcb->local_port = tcp_new_port();
673  }
674 #if SO_REUSE
675  if ((pcb->so_options & SOF_REUSEADDR) != 0) {
676  /* Since SOF_REUSEADDR allows reusing a local address, we have to make sure
677  now that the 5-tuple is unique. */
678  struct tcp_pcb *cpcb;
679  int i;
680  /* Don't check listen- and bound-PCBs, check active- and TIME-WAIT PCBs. */
681  for (i = 2; i < NUM_TCP_PCB_LISTS; i++) {
682  for(cpcb = *tcp_pcb_lists[i]; cpcb != NULL; cpcb = cpcb->next) {
683  if ((cpcb->local_port == pcb->local_port) &&
684  (cpcb->remote_port == port) &&
685  ip_addr_cmp(&cpcb->local_ip, &pcb->local_ip) &&
686  ip_addr_cmp(&cpcb->remote_ip, ipaddr)) {
687  /* linux returns EISCONN here, but ERR_USE should be OK for us */
688  return ERR_USE;
689  }
690  }
691  }
692  }
693 #endif /* SO_REUSE */
694  iss = tcp_next_iss();
695  pcb->rcv_nxt = 0;
696  pcb->snd_nxt = iss;
697  pcb->lastack = iss - 1;
698  pcb->snd_lbb = iss - 1;
699  pcb->rcv_wnd = TCP_WND;
700  pcb->rcv_ann_wnd = TCP_WND;
701  pcb->rcv_ann_right_edge = pcb->rcv_nxt;
702  pcb->snd_wnd = TCP_WND;
703  /* As initial send MSS, we use TCP_MSS but limit it to 536.
704  The send MSS is updated when an MSS option is received. */
705  pcb->mss = (TCP_MSS > 536) ? 536 : TCP_MSS;
706 #if TCP_CALCULATE_EFF_SEND_MSS
707  pcb->mss = tcp_eff_send_mss(pcb->mss, ipaddr);
708 #endif /* TCP_CALCULATE_EFF_SEND_MSS */
709  pcb->cwnd = 1;
710  pcb->ssthresh = pcb->mss * 10;
711 #if LWIP_CALLBACK_API
712  pcb->connected = connected;
713 #else /* LWIP_CALLBACK_API */
714  LWIP_UNUSED_ARG(connected);
715 #endif /* LWIP_CALLBACK_API */
716 
717  /* Send a SYN together with the MSS option. */
718  ret = tcp_enqueue_flags(pcb, TCP_SYN);
719  if (ret == ERR_OK) {
720  /* SYN segment was enqueued, changed the pcbs state now */
721  pcb->state = SYN_SENT;
722  TCP_RMV(&tcp_bound_pcbs, pcb);
723  TCP_REG(&tcp_active_pcbs, pcb);
725 
726  tcp_output(pcb);
727  }
728  return ret;
729 }
730 
738 void
739 tcp_slowtmr(void)
740 {
741  struct tcp_pcb *pcb, *prev;
742  u16_t eff_wnd;
743  u8_t pcb_remove; /* flag if a PCB should be removed */
744  u8_t pcb_reset; /* flag if a RST should be sent when removing */
745  err_t err;
746 
747  err = ERR_OK;
748 
749  ++tcp_ticks;
750 
751  /* Steps through all of the active PCBs. */
752  prev = NULL;
753  pcb = tcp_active_pcbs;
754  if (pcb == NULL) {
755  LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: no active pcbs\n"));
756  }
757  while (pcb != NULL) {
758  LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: processing active pcb\n"));
759  LWIP_ASSERT("tcp_slowtmr: active pcb->state != CLOSED\n", pcb->state != CLOSED);
760  LWIP_ASSERT("tcp_slowtmr: active pcb->state != LISTEN\n", pcb->state != LISTEN);
761  LWIP_ASSERT("tcp_slowtmr: active pcb->state != TIME-WAIT\n", pcb->state != TIME_WAIT);
762 
763  pcb_remove = 0;
764  pcb_reset = 0;
765 
766  if (pcb->state == SYN_SENT && pcb->nrtx == TCP_SYNMAXRTX) {
767  ++pcb_remove;
768  LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: max SYN retries reached\n"));
769  }
770  else if (pcb->nrtx == TCP_MAXRTX) {
771  ++pcb_remove;
772  LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: max DATA retries reached\n"));
773  } else {
774  if (pcb->persist_backoff > 0) {
775  /* If snd_wnd is zero, use persist timer to send 1 byte probes
776  * instead of using the standard retransmission mechanism. */
777  pcb->persist_cnt++;
778  if (pcb->persist_cnt >= tcp_persist_backoff[pcb->persist_backoff-1]) {
779  pcb->persist_cnt = 0;
780  if (pcb->persist_backoff < sizeof(tcp_persist_backoff)) {
781  pcb->persist_backoff++;
782  }
783  tcp_zero_window_probe(pcb);
784  }
785  } else {
786  /* Increase the retransmission timer if it is running */
787  if(pcb->rtime >= 0)
788  ++pcb->rtime;
789 
790  if (pcb->unacked != NULL && pcb->rtime >= pcb->rto) {
791  /* Time for a retransmission. */
792  LWIP_DEBUGF(TCP_RTO_DEBUG, ("tcp_slowtmr: rtime %"S16_F
793  " pcb->rto %"S16_F"\n",
794  pcb->rtime, pcb->rto));
795 
796  /* Double retransmission time-out unless we are trying to
797  * connect to somebody (i.e., we are in SYN_SENT). */
798  if (pcb->state != SYN_SENT) {
799  pcb->rto = ((pcb->sa >> 3) + pcb->sv) << tcp_backoff[pcb->nrtx];
800  }
801 
802  /* Reset the retransmission timer. */
803  pcb->rtime = 0;
804 
805  /* Reduce congestion window and ssthresh. */
806  eff_wnd = LWIP_MIN(pcb->cwnd, pcb->snd_wnd);
807  pcb->ssthresh = eff_wnd >> 1;
808  if (pcb->ssthresh < (pcb->mss << 1)) {
809  pcb->ssthresh = (pcb->mss << 1);
810  }
811  pcb->cwnd = pcb->mss;
812  LWIP_DEBUGF(TCP_CWND_DEBUG, ("tcp_slowtmr: cwnd %"U16_F
813  " ssthresh %"U16_F"\n",
814  pcb->cwnd, pcb->ssthresh));
815 
816  /* The following needs to be called AFTER cwnd is set to one
817  mss - STJ */
818  tcp_rexmit_rto(pcb);
819  }
820  }
821  }
822  /* Check if this PCB has stayed too long in FIN-WAIT-2 */
823  if (pcb->state == FIN_WAIT_2) {
824  if ((u32_t)(tcp_ticks - pcb->tmr) >
825  TCP_FIN_WAIT_TIMEOUT / TCP_SLOW_INTERVAL) {
826  ++pcb_remove;
827  LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: removing pcb stuck in FIN-WAIT-2\n"));
828  }
829  }
830 
831  /* Check if KEEPALIVE should be sent */
832  if((pcb->so_options & SOF_KEEPALIVE) &&
833  ((pcb->state == ESTABLISHED) ||
834  (pcb->state == CLOSE_WAIT))) {
835 #if LWIP_TCP_KEEPALIVE
836  if((u32_t)(tcp_ticks - pcb->tmr) >
837  (pcb->keep_idle + (pcb->keep_cnt*pcb->keep_intvl))
838  / TCP_SLOW_INTERVAL)
839 #else
840  if((u32_t)(tcp_ticks - pcb->tmr) >
841  (pcb->keep_idle + TCP_MAXIDLE) / TCP_SLOW_INTERVAL)
842 #endif /* LWIP_TCP_KEEPALIVE */
843  {
844  LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: KEEPALIVE timeout. Aborting connection to %"U16_F".%"U16_F".%"U16_F".%"U16_F".\n",
845  ip4_addr1_16(&pcb->remote_ip), ip4_addr2_16(&pcb->remote_ip),
846  ip4_addr3_16(&pcb->remote_ip), ip4_addr4_16(&pcb->remote_ip)));
847 
848  ++pcb_remove;
849  ++pcb_reset;
850  }
851 #if LWIP_TCP_KEEPALIVE
852  else if((u32_t)(tcp_ticks - pcb->tmr) >
853  (pcb->keep_idle + pcb->keep_cnt_sent * pcb->keep_intvl)
854  / TCP_SLOW_INTERVAL)
855 #else
856  else if((u32_t)(tcp_ticks - pcb->tmr) >
857  (pcb->keep_idle + pcb->keep_cnt_sent * TCP_KEEPINTVL_DEFAULT)
858  / TCP_SLOW_INTERVAL)
859 #endif /* LWIP_TCP_KEEPALIVE */
860  {
861  tcp_keepalive(pcb);
862  pcb->keep_cnt_sent++;
863  }
864  }
865 
866  /* If this PCB has queued out of sequence data, but has been
867  inactive for too long, will drop the data (it will eventually
868  be retransmitted). */
869 #if TCP_QUEUE_OOSEQ
870  if (pcb->ooseq != NULL &&
871  (u32_t)tcp_ticks - pcb->tmr >= pcb->rto * TCP_OOSEQ_TIMEOUT) {
872  tcp_segs_free(pcb->ooseq);
873  pcb->ooseq = NULL;
874  LWIP_DEBUGF(TCP_CWND_DEBUG, ("tcp_slowtmr: dropping OOSEQ queued data\n"));
875  }
876 #endif /* TCP_QUEUE_OOSEQ */
877 
878  /* Check if this PCB has stayed too long in SYN-RCVD */
879  if (pcb->state == SYN_RCVD) {
880  if ((u32_t)(tcp_ticks - pcb->tmr) >
881  TCP_SYN_RCVD_TIMEOUT / TCP_SLOW_INTERVAL) {
882  ++pcb_remove;
883  LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: removing pcb stuck in SYN-RCVD\n"));
884  }
885  }
886 
887  /* Check if this PCB has stayed too long in LAST-ACK */
888  if (pcb->state == LAST_ACK) {
889  if ((u32_t)(tcp_ticks - pcb->tmr) > 2 * TCP_MSL / TCP_SLOW_INTERVAL) {
890  ++pcb_remove;
891  LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: removing pcb stuck in LAST-ACK\n"));
892  }
893  }
894 
895  /* If the PCB should be removed, do it. */
896  if (pcb_remove) {
897  struct tcp_pcb *pcb2;
898  tcp_pcb_purge(pcb);
899  /* Remove PCB from tcp_active_pcbs list. */
900  if (prev != NULL) {
901  LWIP_ASSERT("tcp_slowtmr: middle tcp != tcp_active_pcbs", pcb != tcp_active_pcbs);
902  prev->next = pcb->next;
903  } else {
904  /* This PCB was the first. */
905  LWIP_ASSERT("tcp_slowtmr: first pcb == tcp_active_pcbs", tcp_active_pcbs == pcb);
906  tcp_active_pcbs = pcb->next;
907  }
908 
909  TCP_EVENT_ERR(pcb->errf, pcb->callback_arg, ERR_ABRT);
910  if (pcb_reset) {
911  tcp_rst(pcb->snd_nxt, pcb->rcv_nxt, &pcb->local_ip, &pcb->remote_ip,
912  pcb->local_port, pcb->remote_port);
913  }
914 
915  pcb2 = pcb;
916  pcb = pcb->next;
917  memp_free(MEMP_TCP_PCB, pcb2);
918  } else {
919  /* get the 'next' element now and work with 'prev' below (in case of abort) */
920  prev = pcb;
921  pcb = pcb->next;
922 
923  /* We check if we should poll the connection. */
924  ++prev->polltmr;
925  if (prev->polltmr >= prev->pollinterval) {
926  prev->polltmr = 0;
927  LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: polling application\n"));
928  TCP_EVENT_POLL(prev, err);
929  /* if err == ERR_ABRT, 'prev' is already deallocated */
930  if (err == ERR_OK) {
931  tcp_output(prev);
932  }
933  }
934  }
935  }
936 
937 
938  /* Steps through all of the TIME-WAIT PCBs. */
939  prev = NULL;
940  pcb = tcp_tw_pcbs;
941  while (pcb != NULL) {
942  LWIP_ASSERT("tcp_slowtmr: TIME-WAIT pcb->state == TIME-WAIT", pcb->state == TIME_WAIT);
943  pcb_remove = 0;
944 
945  /* Check if this PCB has stayed long enough in TIME-WAIT */
946  if ((u32_t)(tcp_ticks - pcb->tmr) > 2 * TCP_MSL / TCP_SLOW_INTERVAL) {
947  ++pcb_remove;
948  }
949 
950 
951 
952  /* If the PCB should be removed, do it. */
953  if (pcb_remove) {
954  struct tcp_pcb *pcb2;
955  tcp_pcb_purge(pcb);
956  /* Remove PCB from tcp_tw_pcbs list. */
957  if (prev != NULL) {
958  LWIP_ASSERT("tcp_slowtmr: middle tcp != tcp_tw_pcbs", pcb != tcp_tw_pcbs);
959  prev->next = pcb->next;
960  } else {
961  /* This PCB was the first. */
962  LWIP_ASSERT("tcp_slowtmr: first pcb == tcp_tw_pcbs", tcp_tw_pcbs == pcb);
963  tcp_tw_pcbs = pcb->next;
964  }
965  pcb2 = pcb;
966  pcb = pcb->next;
967  memp_free(MEMP_TCP_PCB, pcb2);
968  } else {
969  prev = pcb;
970  pcb = pcb->next;
971  }
972  }
973 }
974 
981 void
982 tcp_fasttmr(void)
983 {
984  struct tcp_pcb *pcb = tcp_active_pcbs;
985 
986  while(pcb != NULL) {
987  struct tcp_pcb *next = pcb->next;
988  /* If there is data which was previously "refused" by upper layer */
989  if (pcb->refused_data != NULL) {
990  /* Notify again application with data previously received. */
991  err_t err;
992  LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_fasttmr: notify kept packet\n"));
993  TCP_EVENT_RECV(pcb, pcb->refused_data, ERR_OK, err);
994  if (err == ERR_OK) {
995  pcb->refused_data = NULL;
996  } else if (err == ERR_ABRT) {
997  /* if err == ERR_ABRT, 'pcb' is already deallocated */
998  pcb = NULL;
999  }
1000  }
1001 
1002  /* send delayed ACKs */
1003  if (pcb && (pcb->flags & TF_ACK_DELAY)) {
1004  LWIP_DEBUGF(TCP_DEBUG, ("tcp_fasttmr: delayed ACK\n"));
1005  tcp_ack_now(pcb);
1006  tcp_output(pcb);
1007  pcb->flags &= ~(TF_ACK_DELAY | TF_ACK_NOW);
1008  }
1009 
1010  pcb = next;
1011  }
1012 }
1013 
1019 void
1020 tcp_segs_free(struct tcp_seg *seg)
1021 {
1022  while (seg != NULL) {
1023  struct tcp_seg *next = seg->next;
1024  tcp_seg_free(seg);
1025  seg = next;
1026  }
1027 }
1028 
1034 void
1035 tcp_seg_free(struct tcp_seg *seg)
1036 {
1037  if (seg != NULL) {
1038  if (seg->p != NULL) {
1039  pbuf_free(seg->p);
1040 #if TCP_DEBUG
1041  seg->p = NULL;
1042 #endif /* TCP_DEBUG */
1043  }
1044  memp_free(MEMP_TCP_SEG, seg);
1045  }
1046 }
1047 
1054 void
1055 tcp_setprio(struct tcp_pcb *pcb, u8_t prio)
1056 {
1057  pcb->prio = prio;
1058 }
1059 
1060 #if TCP_QUEUE_OOSEQ
1061 
1068 struct tcp_seg *
1069 tcp_seg_copy(struct tcp_seg *seg)
1070 {
1071  struct tcp_seg *cseg;
1072 
1073  cseg = (struct tcp_seg *)memp_malloc(MEMP_TCP_SEG);
1074  if (cseg == NULL) {
1075  return NULL;
1076  }
1077  SMEMCPY((u8_t *)cseg, (const u8_t *)seg, sizeof(struct tcp_seg));
1078  pbuf_ref(cseg->p);
1079  return cseg;
1080 }
1081 #endif /* TCP_QUEUE_OOSEQ */
1082 
1083 #if LWIP_CALLBACK_API
1084 
1088 err_t
1089 tcp_recv_null(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
1090 {
1091  LWIP_UNUSED_ARG(arg);
1092  if (p != NULL) {
1093  tcp_recved(pcb, p->tot_len);
1094  pbuf_free(p);
1095  } else if (err == ERR_OK) {
1096  return tcp_close(pcb);
1097  }
1098  return ERR_OK;
1099 }
1100 #endif /* LWIP_CALLBACK_API */
1101 
1107 static void
1108 tcp_kill_prio(u8_t prio)
1109 {
1110  struct tcp_pcb *pcb, *inactive;
1111  u32_t inactivity;
1112  u8_t mprio;
1113 
1114 
1115  mprio = TCP_PRIO_MAX;
1116 
1117  /* We kill the oldest active connection that has lower priority than prio. */
1118  inactivity = 0;
1119  inactive = NULL;
1120  for(pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) {
1121  if (pcb->prio <= prio &&
1122  pcb->prio <= mprio &&
1123  (u32_t)(tcp_ticks - pcb->tmr) >= inactivity) {
1124  inactivity = tcp_ticks - pcb->tmr;
1125  inactive = pcb;
1126  mprio = pcb->prio;
1127  }
1128  }
1129  if (inactive != NULL) {
1130  LWIP_DEBUGF(TCP_DEBUG, ("tcp_kill_prio: killing oldest PCB %p (%"S32_F")\n",
1131  (void *)inactive, inactivity));
1132  tcp_abort(inactive);
1133  }
1134 }
1135 
1140 static void
1141 tcp_kill_timewait(void)
1142 {
1143  struct tcp_pcb *pcb, *inactive;
1144  u32_t inactivity;
1145 
1146  inactivity = 0;
1147  inactive = NULL;
1148  /* Go through the list of TIME_WAIT pcbs and get the oldest pcb. */
1149  for(pcb = tcp_tw_pcbs; pcb != NULL; pcb = pcb->next) {
1150  if ((u32_t)(tcp_ticks - pcb->tmr) >= inactivity) {
1151  inactivity = tcp_ticks - pcb->tmr;
1152  inactive = pcb;
1153  }
1154  }
1155  if (inactive != NULL) {
1156  LWIP_DEBUGF(TCP_DEBUG, ("tcp_kill_timewait: killing oldest TIME-WAIT PCB %p (%"S32_F")\n",
1157  (void *)inactive, inactivity));
1158  tcp_abort(inactive);
1159  }
1160 }
1161 
1168 struct tcp_pcb *
1169 tcp_alloc(u8_t prio)
1170 {
1171  struct tcp_pcb *pcb;
1172  u32_t iss;
1173 
1174  pcb = (struct tcp_pcb *)memp_malloc(MEMP_TCP_PCB);
1175  if (pcb == NULL) {
1176  /* Try killing oldest connection in TIME-WAIT. */
1177  LWIP_DEBUGF(TCP_DEBUG, ("tcp_alloc: killing off oldest TIME-WAIT connection\n"));
1178  tcp_kill_timewait();
1179  /* Try to allocate a tcp_pcb again. */
1180  pcb = (struct tcp_pcb *)memp_malloc(MEMP_TCP_PCB);
1181  if (pcb == NULL) {
1182  /* Try killing active connections with lower priority than the new one. */
1183  LWIP_DEBUGF(TCP_DEBUG, ("tcp_alloc: killing connection with prio lower than %d\n", prio));
1184  tcp_kill_prio(prio);
1185  /* Try to allocate a tcp_pcb again. */
1186  pcb = (struct tcp_pcb *)memp_malloc(MEMP_TCP_PCB);
1187  if (pcb != NULL) {
1188  /* adjust err stats: memp_malloc failed twice before */
1189  MEMP_STATS_DEC(err, MEMP_TCP_PCB);
1190  }
1191  }
1192  if (pcb != NULL) {
1193  /* adjust err stats: timewait PCB was freed above */
1194  MEMP_STATS_DEC(err, MEMP_TCP_PCB);
1195  }
1196  }
1197  if (pcb != NULL) {
1198  memset(pcb, 0, sizeof(struct tcp_pcb));
1199  pcb->prio = prio;
1200  pcb->snd_buf = TCP_SND_BUF;
1201  pcb->snd_queuelen = 0;
1202  pcb->rcv_wnd = TCP_WND;
1203  pcb->rcv_ann_wnd = TCP_WND;
1204  pcb->tos = 0;
1205  pcb->ttl = TCP_TTL;
1206  /* As initial send MSS, we use TCP_MSS but limit it to 536.
1207  The send MSS is updated when an MSS option is received. */
1208  pcb->mss = (TCP_MSS > 536) ? 536 : TCP_MSS;
1209  pcb->rto = 3000 / TCP_SLOW_INTERVAL;
1210  pcb->sa = 0;
1211  pcb->sv = 3000 / TCP_SLOW_INTERVAL;
1212  pcb->rtime = -1;
1213  pcb->cwnd = 1;
1214  iss = tcp_next_iss();
1215  pcb->snd_wl2 = iss;
1216  pcb->snd_nxt = iss;
1217  pcb->lastack = iss;
1218  pcb->snd_lbb = iss;
1219  pcb->tmr = tcp_ticks;
1220 
1221  pcb->polltmr = 0;
1222 
1223 #if LWIP_CALLBACK_API
1224  pcb->recv = tcp_recv_null;
1225 #endif /* LWIP_CALLBACK_API */
1226 
1227  /* Init KEEPALIVE timer */
1228  pcb->keep_idle = TCP_KEEPIDLE_DEFAULT;
1229 
1230 #if LWIP_TCP_KEEPALIVE
1231  pcb->keep_intvl = TCP_KEEPINTVL_DEFAULT;
1232  pcb->keep_cnt = TCP_KEEPCNT_DEFAULT;
1233 #endif /* LWIP_TCP_KEEPALIVE */
1234 
1235  pcb->keep_cnt_sent = 0;
1236  }
1237  return pcb;
1238 }
1239 
1252 struct tcp_pcb *
1253 tcp_new(void)
1254 {
1255  return tcp_alloc(TCP_PRIO_NORMAL);
1256 }
1257 
1265 void
1266 tcp_arg(struct tcp_pcb *pcb, void *arg)
1267 {
1268  pcb->callback_arg = arg;
1269 }
1270 #if LWIP_CALLBACK_API
1271 
1279 void
1280 tcp_recv(struct tcp_pcb *pcb, tcp_recv_fn recv)
1281 {
1282  pcb->recv = recv;
1283 }
1284 
1292 void
1293 tcp_sent(struct tcp_pcb *pcb, tcp_sent_fn sent)
1294 {
1295  pcb->sent = sent;
1296 }
1297 
1306 void
1307 tcp_err(struct tcp_pcb *pcb, tcp_err_fn err)
1308 {
1309  pcb->errf = err;
1310 }
1311 
1320 void
1321 tcp_accept(struct tcp_pcb *pcb, tcp_accept_fn accept)
1322 {
1323  pcb->accept = accept;
1324 }
1325 #endif /* LWIP_CALLBACK_API */
1326 
1327 
1334 void
1335 tcp_poll(struct tcp_pcb *pcb, tcp_poll_fn poll, u8_t interval)
1336 {
1337 #if LWIP_CALLBACK_API
1338  pcb->poll = poll;
1339 #else /* LWIP_CALLBACK_API */
1340  LWIP_UNUSED_ARG(poll);
1341 #endif /* LWIP_CALLBACK_API */
1342  pcb->pollinterval = interval;
1343 }
1344 
1351 void
1352 tcp_pcb_purge(struct tcp_pcb *pcb)
1353 {
1354  if (pcb->state != CLOSED &&
1355  pcb->state != TIME_WAIT &&
1356  pcb->state != LISTEN) {
1357 
1358  LWIP_DEBUGF(TCP_DEBUG, ("tcp_pcb_purge\n"));
1359 
1360 #if TCP_LISTEN_BACKLOG
1361  if (pcb->state == SYN_RCVD) {
1362  /* Need to find the corresponding listen_pcb and decrease its accepts_pending */
1363  struct tcp_pcb_listen *lpcb;
1364  LWIP_ASSERT("tcp_pcb_purge: pcb->state == SYN_RCVD but tcp_listen_pcbs is NULL",
1365  tcp_listen_pcbs.listen_pcbs != NULL);
1366  for (lpcb = tcp_listen_pcbs.listen_pcbs; lpcb != NULL; lpcb = lpcb->next) {
1367  if ((lpcb->local_port == pcb->local_port) &&
1368  (ip_addr_isany(&lpcb->local_ip) ||
1369  ip_addr_cmp(&pcb->local_ip, &lpcb->local_ip))) {
1370  /* port and address of the listen pcb match the timed-out pcb */
1371  LWIP_ASSERT("tcp_pcb_purge: listen pcb does not have accepts pending",
1372  lpcb->accepts_pending > 0);
1373  lpcb->accepts_pending--;
1374  break;
1375  }
1376  }
1377  }
1378 #endif /* TCP_LISTEN_BACKLOG */
1379 
1380 
1381  if (pcb->refused_data != NULL) {
1382  LWIP_DEBUGF(TCP_DEBUG, ("tcp_pcb_purge: data left on ->refused_data\n"));
1383  pbuf_free(pcb->refused_data);
1384  pcb->refused_data = NULL;
1385  }
1386  if (pcb->unsent != NULL) {
1387  LWIP_DEBUGF(TCP_DEBUG, ("tcp_pcb_purge: not all data sent\n"));
1388  }
1389  if (pcb->unacked != NULL) {
1390  LWIP_DEBUGF(TCP_DEBUG, ("tcp_pcb_purge: data left on ->unacked\n"));
1391  }
1392 #if TCP_QUEUE_OOSEQ
1393  if (pcb->ooseq != NULL) {
1394  LWIP_DEBUGF(TCP_DEBUG, ("tcp_pcb_purge: data left on ->ooseq\n"));
1395  }
1396  tcp_segs_free(pcb->ooseq);
1397  pcb->ooseq = NULL;
1398 #endif /* TCP_QUEUE_OOSEQ */
1399 
1400  /* Stop the retransmission timer as it will expect data on unacked
1401  queue if it fires */
1402  pcb->rtime = -1;
1403 
1404  tcp_segs_free(pcb->unsent);
1405  tcp_segs_free(pcb->unacked);
1406  pcb->unacked = pcb->unsent = NULL;
1407 #if TCP_OVERSIZE
1408  pcb->unsent_oversize = 0;
1409 #endif /* TCP_OVERSIZE */
1410  }
1411 }
1412 
1419 void
1420 tcp_pcb_remove(struct tcp_pcb **pcblist, struct tcp_pcb *pcb)
1421 {
1422  TCP_RMV(pcblist, pcb);
1423 
1424  tcp_pcb_purge(pcb);
1425 
1426  /* if there is an outstanding delayed ACKs, send it */
1427  if (pcb->state != TIME_WAIT &&
1428  pcb->state != LISTEN &&
1429  pcb->flags & TF_ACK_DELAY) {
1430  pcb->flags |= TF_ACK_NOW;
1431  tcp_output(pcb);
1432  }
1433 
1434  if (pcb->state != LISTEN) {
1435  LWIP_ASSERT("unsent segments leaking", pcb->unsent == NULL);
1436  LWIP_ASSERT("unacked segments leaking", pcb->unacked == NULL);
1437 #if TCP_QUEUE_OOSEQ
1438  LWIP_ASSERT("ooseq segments leaking", pcb->ooseq == NULL);
1439 #endif /* TCP_QUEUE_OOSEQ */
1440  }
1441 
1442  pcb->state = CLOSED;
1443 
1444  LWIP_ASSERT("tcp_pcb_remove: tcp_pcbs_sane()", tcp_pcbs_sane());
1445 }
1446 
1452 u32_t
1453 tcp_next_iss(void)
1454 {
1455  static u32_t iss = 6510;
1456 
1457  iss += tcp_ticks; /* XXX */
1458  return iss;
1459 }
1460 
1461 #if TCP_CALCULATE_EFF_SEND_MSS
1462 
1467 u16_t
1468 tcp_eff_send_mss(u16_t sendmss, ip_addr_t *addr)
1469 {
1470  u16_t mss_s;
1471  struct netif *outif;
1472 
1473  outif = ip_route(addr);
1474  if ((outif != NULL) && (outif->mtu != 0)) {
1475  mss_s = outif->mtu - IP_HLEN - TCP_HLEN;
1476  /* RFC 1122, chap 4.2.2.6:
1477  * Eff.snd.MSS = min(SendMSS+20, MMS_S) - TCPhdrsize - IPoptionsize
1478  * We correct for TCP options in tcp_write(), and don't support IP options.
1479  */
1480  sendmss = LWIP_MIN(sendmss, mss_s);
1481  }
1482  return sendmss;
1483 }
1484 #endif /* TCP_CALCULATE_EFF_SEND_MSS */
1485 
1486 const char*
1487 tcp_debug_state_str(enum tcp_state s)
1488 {
1489  return tcp_state_str[s];
1490 }
1491 
1492 #if TCP_DEBUG || TCP_INPUT_DEBUG || TCP_OUTPUT_DEBUG
1493 
1498 void
1499 tcp_debug_print(struct tcp_hdr *tcphdr)
1500 {
1501  LWIP_DEBUGF(TCP_DEBUG, ("TCP header:\n"));
1502  LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n"));
1503  LWIP_DEBUGF(TCP_DEBUG, ("| %5"U16_F" | %5"U16_F" | (src port, dest port)\n",
1504  ntohs(tcphdr->src), ntohs(tcphdr->dest)));
1505  LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n"));
1506  LWIP_DEBUGF(TCP_DEBUG, ("| %010"U32_F" | (seq no)\n",
1507  ntohl(tcphdr->seqno)));
1508  LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n"));
1509  LWIP_DEBUGF(TCP_DEBUG, ("| %010"U32_F" | (ack no)\n",
1510  ntohl(tcphdr->ackno)));
1511  LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n"));
1512  LWIP_DEBUGF(TCP_DEBUG, ("| %2"U16_F" | |%"U16_F"%"U16_F"%"U16_F"%"U16_F"%"U16_F"%"U16_F"| %5"U16_F" | (hdrlen, flags (",
1513  TCPH_HDRLEN(tcphdr),
1514  TCPH_FLAGS(tcphdr) >> 5 & 1,
1515  TCPH_FLAGS(tcphdr) >> 4 & 1,
1516  TCPH_FLAGS(tcphdr) >> 3 & 1,
1517  TCPH_FLAGS(tcphdr) >> 2 & 1,
1518  TCPH_FLAGS(tcphdr) >> 1 & 1,
1519  TCPH_FLAGS(tcphdr) & 1,
1520  ntohs(tcphdr->wnd)));
1521  tcp_debug_print_flags(TCPH_FLAGS(tcphdr));
1522  LWIP_DEBUGF(TCP_DEBUG, ("), win)\n"));
1523  LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n"));
1524  LWIP_DEBUGF(TCP_DEBUG, ("| 0x%04"X16_F" | %5"U16_F" | (chksum, urgp)\n",
1525  ntohs(tcphdr->chksum), ntohs(tcphdr->urgp)));
1526  LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n"));
1527 }
1528 
1534 void
1535 tcp_debug_print_state(enum tcp_state s)
1536 {
1537  LWIP_DEBUGF(TCP_DEBUG, ("State: %s\n", tcp_state_str[s]));
1538 }
1539 
1545 void
1546 tcp_debug_print_flags(u8_t flags)
1547 {
1548  if (flags & TCP_FIN) {
1549  LWIP_DEBUGF(TCP_DEBUG, ("FIN "));
1550  }
1551  if (flags & TCP_SYN) {
1552  LWIP_DEBUGF(TCP_DEBUG, ("SYN "));
1553  }
1554  if (flags & TCP_RST) {
1555  LWIP_DEBUGF(TCP_DEBUG, ("RST "));
1556  }
1557  if (flags & TCP_PSH) {
1558  LWIP_DEBUGF(TCP_DEBUG, ("PSH "));
1559  }
1560  if (flags & TCP_ACK) {
1561  LWIP_DEBUGF(TCP_DEBUG, ("ACK "));
1562  }
1563  if (flags & TCP_URG) {
1564  LWIP_DEBUGF(TCP_DEBUG, ("URG "));
1565  }
1566  if (flags & TCP_ECE) {
1567  LWIP_DEBUGF(TCP_DEBUG, ("ECE "));
1568  }
1569  if (flags & TCP_CWR) {
1570  LWIP_DEBUGF(TCP_DEBUG, ("CWR "));
1571  }
1572  LWIP_DEBUGF(TCP_DEBUG, ("\n"));
1573 }
1574 
1578 void
1579 tcp_debug_print_pcbs(void)
1580 {
1581  struct tcp_pcb *pcb;
1582  LWIP_DEBUGF(TCP_DEBUG, ("Active PCB states:\n"));
1583  for(pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) {
1584  LWIP_DEBUGF(TCP_DEBUG, ("Local port %"U16_F", foreign port %"U16_F" snd_nxt %"U32_F" rcv_nxt %"U32_F" ",
1585  pcb->local_port, pcb->remote_port,
1586  pcb->snd_nxt, pcb->rcv_nxt));
1587  tcp_debug_print_state(pcb->state);
1588  }
1589  LWIP_DEBUGF(TCP_DEBUG, ("Listen PCB states:\n"));
1590  for(pcb = (struct tcp_pcb *)tcp_listen_pcbs.pcbs; pcb != NULL; pcb = pcb->next) {
1591  LWIP_DEBUGF(TCP_DEBUG, ("Local port %"U16_F", foreign port %"U16_F" snd_nxt %"U32_F" rcv_nxt %"U32_F" ",
1592  pcb->local_port, pcb->remote_port,
1593  pcb->snd_nxt, pcb->rcv_nxt));
1594  tcp_debug_print_state(pcb->state);
1595  }
1596  LWIP_DEBUGF(TCP_DEBUG, ("TIME-WAIT PCB states:\n"));
1597  for(pcb = tcp_tw_pcbs; pcb != NULL; pcb = pcb->next) {
1598  LWIP_DEBUGF(TCP_DEBUG, ("Local port %"U16_F", foreign port %"U16_F" snd_nxt %"U32_F" rcv_nxt %"U32_F" ",
1599  pcb->local_port, pcb->remote_port,
1600  pcb->snd_nxt, pcb->rcv_nxt));
1601  tcp_debug_print_state(pcb->state);
1602  }
1603 }
1604 
1608 s16_t
1609 tcp_pcbs_sane(void)
1610 {
1611  struct tcp_pcb *pcb;
1612  for(pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) {
1613  LWIP_ASSERT("tcp_pcbs_sane: active pcb->state != CLOSED", pcb->state != CLOSED);
1614  LWIP_ASSERT("tcp_pcbs_sane: active pcb->state != LISTEN", pcb->state != LISTEN);
1615  LWIP_ASSERT("tcp_pcbs_sane: active pcb->state != TIME-WAIT", pcb->state != TIME_WAIT);
1616  }
1617  for(pcb = tcp_tw_pcbs; pcb != NULL; pcb = pcb->next) {
1618  LWIP_ASSERT("tcp_pcbs_sane: tw pcb->state == TIME-WAIT", pcb->state == TIME_WAIT);
1619  }
1620  return 1;
1621 }
1622 #endif /* TCP_DEBUG */
1623 
1624 #endif /* LWIP_TCP */