UbixOS  2.0
tcp_in.c
Go to the documentation of this file.
1 
12 /*
13  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
14  * All rights reserved.
15  *
16  * Redistribution and use in source and binary forms, with or without modification,
17  * are permitted provided that the following conditions are met:
18  *
19  * 1. Redistributions of source code must retain the above copyright notice,
20  * this list of conditions and the following disclaimer.
21  * 2. Redistributions in binary form must reproduce the above copyright notice,
22  * this list of conditions and the following disclaimer in the documentation
23  * and/or other materials provided with the distribution.
24  * 3. The name of the author may not be used to endorse or promote products
25  * derived from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
28  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
29  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
30  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
32  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
35  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
36  * OF SUCH DAMAGE.
37  *
38  * This file is part of the lwIP TCP/IP stack.
39  *
40  * Author: Adam Dunkels <adam@sics.se>
41  *
42  */
43 
44 #include "net/opt.h"
45 
46 #if LWIP_TCP /* don't build if not configured for use in lwipopts.h */
47 
48 #include "net/priv/tcp_priv.h"
49 #include "net/def.h"
50 #include "net/ip_addr.h"
51 #include "net/netif.h"
52 #include "net/mem.h"
53 #include "net/memp.h"
54 #include "net/inet_chksum.h"
55 #include "net/stats.h"
56 #include "net/ip6.h"
57 #include "net/ip6_addr.h"
58 #if LWIP_ND6_TCP_REACHABILITY_HINTS
59 #include "net/nd6.h"
60 #endif /* LWIP_ND6_TCP_REACHABILITY_HINTS */
61 
63 #define LWIP_TCP_CALC_INITIAL_CWND(mss) LWIP_MIN((4U * (mss)), LWIP_MAX((2U * (mss)), 4380U));
64 
65 /* These variables are global to all functions involved in the input
66  processing of TCP segments. They are set by the tcp_input()
67  function. */
68 static struct tcp_seg inseg;
69 static struct tcp_hdr *tcphdr;
70 static u16_t tcphdr_optlen;
71 static u16_t tcphdr_opt1len;
72 static u8_t* tcphdr_opt2;
73 static u16_t tcp_optidx;
74 static u32_t seqno, ackno;
75 static tcpwnd_size_t recv_acked;
76 static u16_t tcplen;
77 static u8_t flags;
78 
79 static u8_t recv_flags;
80 static struct pbuf *recv_data;
81 
82 struct tcp_pcb *tcp_input_pcb;
83 
84 /* Forward declarations. */
85 static err_t tcp_process(struct tcp_pcb *pcb);
86 static void tcp_receive(struct tcp_pcb *pcb);
87 static void tcp_parseopt(struct tcp_pcb *pcb);
88 
89 static void tcp_listen_input(struct tcp_pcb_listen *pcb);
90 static void tcp_timewait_input(struct tcp_pcb *pcb);
91 
92 static int tcp_input_delayed_close(struct tcp_pcb *pcb);
93 
103 void
104 tcp_input(struct pbuf *p, struct netif *inp)
105 {
106  struct tcp_pcb *pcb, *prev;
107  struct tcp_pcb_listen *lpcb;
108 #if SO_REUSE
109  struct tcp_pcb *lpcb_prev = NULL;
110  struct tcp_pcb_listen *lpcb_any = NULL;
111 #endif /* SO_REUSE */
112  u8_t hdrlen_bytes;
113  err_t err;
114 
115  LWIP_UNUSED_ARG(inp);
116 
117  PERF_START;
118 
119  TCP_STATS_INC(tcp.recv);
120  MIB2_STATS_INC(mib2.tcpinsegs);
121 
122  tcphdr = (struct tcp_hdr *)p->payload;
123 
124 #if TCP_INPUT_DEBUG
125  tcp_debug_print(tcphdr);
126 #endif
127 
128  /* Check that TCP header fits in payload */
129  if (p->len < TCP_HLEN) {
130  /* drop short packets */
131  LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_input: short packet (%"U16_F" bytes) discarded\n", p->tot_len));
132  TCP_STATS_INC(tcp.lenerr);
133  goto dropped;
134  }
135 
136  /* Don't even process incoming broadcasts/multicasts. */
139  TCP_STATS_INC(tcp.proterr);
140  goto dropped;
141  }
142 
143 #if CHECKSUM_CHECK_TCP
144  IF__NETIF_CHECKSUM_ENABLED(inp, NETIF_CHECKSUM_CHECK_TCP) {
145  /* Verify TCP checksum. */
146  u16_t chksum = ip_chksum_pseudo(p, IP_PROTO_TCP, p->tot_len,
148  if (chksum != 0) {
149  LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_input: packet discarded due to failing checksum 0x%04"X16_F"\n",
150  chksum));
151  tcp_debug_print(tcphdr);
152  TCP_STATS_INC(tcp.chkerr);
153  goto dropped;
154  }
155  }
156 #endif /* CHECKSUM_CHECK_TCP */
157 
158  /* sanity-check header length */
159  hdrlen_bytes = TCPH_HDRLEN(tcphdr) * 4;
160  if ((hdrlen_bytes < TCP_HLEN) || (hdrlen_bytes > p->tot_len)) {
161  LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_input: invalid header length (%"U16_F")\n", (u16_t)hdrlen_bytes));
162  TCP_STATS_INC(tcp.lenerr);
163  goto dropped;
164  }
165 
166  /* Move the payload pointer in the pbuf so that it points to the
167  TCP data instead of the TCP header. */
168  tcphdr_optlen = hdrlen_bytes - TCP_HLEN;
169  tcphdr_opt2 = NULL;
170  if (p->len >= hdrlen_bytes) {
171  /* all options are in the first pbuf */
172  tcphdr_opt1len = tcphdr_optlen;
173  pbuf_header(p, -(s16_t)hdrlen_bytes); /* cannot fail */
174  } else {
175  u16_t opt2len;
176  /* TCP header fits into first pbuf, options don't - data is in the next pbuf */
177  /* there must be a next pbuf, due to hdrlen_bytes sanity check above */
178  LWIP_ASSERT("p->next != NULL", p->next != NULL);
179 
180  /* advance over the TCP header (cannot fail) */
181  pbuf_header(p, -TCP_HLEN);
182 
183  /* determine how long the first and second parts of the options are */
184  tcphdr_opt1len = p->len;
185  opt2len = tcphdr_optlen - tcphdr_opt1len;
186 
187  /* options continue in the next pbuf: set p to zero length and hide the
188  options in the next pbuf (adjusting p->tot_len) */
189  pbuf_header(p, -(s16_t)tcphdr_opt1len);
190 
191  /* check that the options fit in the second pbuf */
192  if (opt2len > p->next->len) {
193  /* drop short packets */
194  LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_input: options overflow second pbuf (%"U16_F" bytes)\n", p->next->len));
195  TCP_STATS_INC(tcp.lenerr);
196  goto dropped;
197  }
198 
199  /* remember the pointer to the second part of the options */
200  tcphdr_opt2 = (u8_t*)p->next->payload;
201 
202  /* advance p->next to point after the options, and manually
203  adjust p->tot_len to keep it consistent with the changed p->next */
204  pbuf_header(p->next, -(s16_t)opt2len);
205  p->tot_len -= opt2len;
206 
207  LWIP_ASSERT("p->len == 0", p->len == 0);
208  LWIP_ASSERT("p->tot_len == p->next->tot_len", p->tot_len == p->next->tot_len);
209  }
210 
211  /* Convert fields in TCP header to host byte order. */
212  tcphdr->src = lwip_ntohs(tcphdr->src);
213  tcphdr->dest = lwip_ntohs(tcphdr->dest);
214  seqno = tcphdr->seqno = lwip_ntohl(tcphdr->seqno);
215  ackno = tcphdr->ackno = lwip_ntohl(tcphdr->ackno);
216  tcphdr->wnd = lwip_ntohs(tcphdr->wnd);
217 
218  flags = TCPH_FLAGS(tcphdr);
219  tcplen = p->tot_len + ((flags & (TCP_FIN | TCP_SYN)) ? 1 : 0);
220 
221  /* Demultiplex an incoming segment. First, we check if it is destined
222  for an active connection. */
223  prev = NULL;
224 
225  for (pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) {
226  LWIP_ASSERT("tcp_input: active pcb->state != CLOSED", pcb->state != CLOSED);
227  LWIP_ASSERT("tcp_input: active pcb->state != TIME-WAIT", pcb->state != TIME_WAIT);
228  LWIP_ASSERT("tcp_input: active pcb->state != LISTEN", pcb->state != LISTEN);
229  if (pcb->remote_port == tcphdr->src &&
230  pcb->local_port == tcphdr->dest &&
231  ip_addr_cmp(&pcb->remote_ip, ip_current_src_addr()) &&
232  ip_addr_cmp(&pcb->local_ip, ip_current_dest_addr())) {
233  /* Move this PCB to the front of the list so that subsequent
234  lookups will be faster (we exploit locality in TCP segment
235  arrivals). */
236  LWIP_ASSERT("tcp_input: pcb->next != pcb (before cache)", pcb->next != pcb);
237  if (prev != NULL) {
238  prev->next = pcb->next;
239  pcb->next = tcp_active_pcbs;
240  tcp_active_pcbs = pcb;
241  } else {
242  TCP_STATS_INC(tcp.cachehit);
243  }
244  LWIP_ASSERT("tcp_input: pcb->next != pcb (after cache)", pcb->next != pcb);
245  break;
246  }
247  prev = pcb;
248  }
249 
250  if (pcb == NULL) {
251  /* If it did not go to an active connection, we check the connections
252  in the TIME-WAIT state. */
253  for (pcb = tcp_tw_pcbs; pcb != NULL; pcb = pcb->next) {
254  LWIP_ASSERT("tcp_input: TIME-WAIT pcb->state == TIME-WAIT", pcb->state == TIME_WAIT);
255  if (pcb->remote_port == tcphdr->src &&
256  pcb->local_port == tcphdr->dest &&
257  ip_addr_cmp(&pcb->remote_ip, ip_current_src_addr()) &&
258  ip_addr_cmp(&pcb->local_ip, ip_current_dest_addr())) {
259  /* We don't really care enough to move this PCB to the front
260  of the list since we are not very likely to receive that
261  many segments for connections in TIME-WAIT. */
262  LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_input: packed for TIME_WAITing connection.\n"));
263  tcp_timewait_input(pcb);
264  pbuf_free(p);
265  return;
266  }
267  }
268 
269  /* Finally, if we still did not get a match, we check all PCBs that
270  are LISTENing for incoming connections. */
271  prev = NULL;
272  for (lpcb = tcp_listen_pcbs.listen_pcbs; lpcb != NULL; lpcb = lpcb->next) {
273  if (lpcb->local_port == tcphdr->dest) {
274  if (IP_IS_ANY_TYPE_VAL(lpcb->local_ip)) {
275  /* found an ANY TYPE (IPv4/IPv6) match */
276 #if SO_REUSE
277  lpcb_any = lpcb;
278  lpcb_prev = prev;
279 #else /* SO_REUSE */
280  break;
281 #endif /* SO_REUSE */
283  if (ip_addr_cmp(&lpcb->local_ip, ip_current_dest_addr())) {
284  /* found an exact match */
285  break;
286  } else if (ip_addr_isany(&lpcb->local_ip)) {
287  /* found an ANY-match */
288 #if SO_REUSE
289  lpcb_any = lpcb;
290  lpcb_prev = prev;
291 #else /* SO_REUSE */
292  break;
293  #endif /* SO_REUSE */
294  }
295  }
296  }
297  prev = (struct tcp_pcb *)lpcb;
298  }
299 #if SO_REUSE
300  /* first try specific local IP */
301  if (lpcb == NULL) {
302  /* only pass to ANY if no specific local IP has been found */
303  lpcb = lpcb_any;
304  prev = lpcb_prev;
305  }
306 #endif /* SO_REUSE */
307  if (lpcb != NULL) {
308  /* Move this PCB to the front of the list so that subsequent
309  lookups will be faster (we exploit locality in TCP segment
310  arrivals). */
311  if (prev != NULL) {
312  ((struct tcp_pcb_listen *)prev)->next = lpcb->next;
313  /* our successor is the remainder of the listening list */
314  lpcb->next = tcp_listen_pcbs.listen_pcbs;
315  /* put this listening pcb at the head of the listening list */
316  tcp_listen_pcbs.listen_pcbs = lpcb;
317  } else {
318  TCP_STATS_INC(tcp.cachehit);
319  }
320 
321  LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_input: packed for LISTENing connection.\n"));
322  tcp_listen_input(lpcb);
323  pbuf_free(p);
324  return;
325  }
326  }
327 
328 #if TCP_INPUT_DEBUG
329  LWIP_DEBUGF(TCP_INPUT_DEBUG, ("+-+-+-+-+-+-+-+-+-+-+-+-+-+- tcp_input: flags "));
330  tcp_debug_print_flags(TCPH_FLAGS(tcphdr));
331  LWIP_DEBUGF(TCP_INPUT_DEBUG, ("-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n"));
332 #endif /* TCP_INPUT_DEBUG */
333 
334 
335  if (pcb != NULL) {
336  /* The incoming segment belongs to a connection. */
337 #if TCP_INPUT_DEBUG
338  tcp_debug_print_state(pcb->state);
339 #endif /* TCP_INPUT_DEBUG */
340 
341  /* Set up a tcp_seg structure. */
342  inseg.next = NULL;
343  inseg.len = p->tot_len;
344  inseg.p = p;
345  inseg.tcphdr = tcphdr;
346 
347  recv_data = NULL;
348  recv_flags = 0;
349  recv_acked = 0;
350 
351  if (flags & TCP_PSH) {
352  p->flags |= PBUF_FLAG_PUSH;
353  }
354 
355  /* If there is data which was previously "refused" by upper layer */
356  if (pcb->refused_data != NULL) {
357  if ((tcp_process_refused_data(pcb) == ERR_ABRT) ||
358  ((pcb->refused_data != NULL) && (tcplen > 0))) {
359  /* pcb has been aborted or refused data is still refused and the new
360  segment contains data */
361  if (pcb->rcv_ann_wnd == 0) {
362  /* this is a zero-window probe, we respond to it with current RCV.NXT
363  and drop the data segment */
364  tcp_send_empty_ack(pcb);
365  }
366  TCP_STATS_INC(tcp.drop);
367  MIB2_STATS_INC(mib2.tcpinerrs);
368  goto aborted;
369  }
370  }
371  tcp_input_pcb = pcb;
372  err = tcp_process(pcb);
373  /* A return value of ERR_ABRT means that tcp_abort() was called
374  and that the pcb has been freed. If so, we don't do anything. */
375  if (err != ERR_ABRT) {
376  if (recv_flags & TF_RESET) {
377  /* TF_RESET means that the connection was reset by the other
378  end. We then call the error callback to inform the
379  application that the connection is dead before we
380  deallocate the PCB. */
381  TCP_EVENT_ERR(pcb->state, pcb->errf, pcb->callback_arg, ERR_RST);
382  tcp_pcb_remove(&tcp_active_pcbs, pcb);
383  memp_free(MEMP_TCP_PCB, pcb);
384  } else {
385  err = ERR_OK;
386  /* If the application has registered a "sent" function to be
387  called when new send buffer space is available, we call it
388  now. */
389  if (recv_acked > 0) {
390  u16_t acked16;
391 #if LWIP_WND_SCALE
392  /* recv_acked is u32_t but the sent callback only takes a u16_t,
393  so we might have to call it multiple times. */
394  u32_t acked = recv_acked;
395  while (acked > 0) {
396  acked16 = (u16_t)LWIP_MIN(acked, 0xffffu);
397  acked -= acked16;
398 #else
399  {
400  acked16 = recv_acked;
401 #endif
402  TCP_EVENT_SENT(pcb, (u16_t)acked16, err);
403  if (err == ERR_ABRT) {
404  goto aborted;
405  }
406  }
407  recv_acked = 0;
408  }
409  if (tcp_input_delayed_close(pcb)) {
410  goto aborted;
411  }
412 #if TCP_QUEUE_OOSEQ && LWIP_WND_SCALE
413  while (recv_data != NULL) {
414  struct pbuf *rest = NULL;
415  pbuf_split_64k(recv_data, &rest);
416 #else /* TCP_QUEUE_OOSEQ && LWIP_WND_SCALE */
417  if (recv_data != NULL) {
418 #endif /* TCP_QUEUE_OOSEQ && LWIP_WND_SCALE */
419 
420  LWIP_ASSERT("pcb->refused_data == NULL", pcb->refused_data == NULL);
421  if (pcb->flags & TF_RXCLOSED) {
422  /* received data although already closed -> abort (send RST) to
423  notify the remote host that not all data has been processed */
424  pbuf_free(recv_data);
425 #if TCP_QUEUE_OOSEQ && LWIP_WND_SCALE
426  if (rest != NULL) {
427  pbuf_free(rest);
428  }
429 #endif /* TCP_QUEUE_OOSEQ && LWIP_WND_SCALE */
430  tcp_abort(pcb);
431  goto aborted;
432  }
433 
434  /* Notify application that data has been received. */
435  TCP_EVENT_RECV(pcb, recv_data, ERR_OK, err);
436  if (err == ERR_ABRT) {
437 #if TCP_QUEUE_OOSEQ && LWIP_WND_SCALE
438  if (rest != NULL) {
439  pbuf_free(rest);
440  }
441 #endif /* TCP_QUEUE_OOSEQ && LWIP_WND_SCALE */
442  goto aborted;
443  }
444 
445  /* If the upper layer can't receive this data, store it */
446  if (err != ERR_OK) {
447 #if TCP_QUEUE_OOSEQ && LWIP_WND_SCALE
448  if (rest != NULL) {
449  pbuf_cat(recv_data, rest);
450  }
451 #endif /* TCP_QUEUE_OOSEQ && LWIP_WND_SCALE */
452  pcb->refused_data = recv_data;
453  LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_input: keep incoming packet, because pcb is \"full\"\n"));
454 #if TCP_QUEUE_OOSEQ && LWIP_WND_SCALE
455  break;
456  } else {
457  /* Upper layer received the data, go on with the rest if > 64K */
458  recv_data = rest;
459 #endif /* TCP_QUEUE_OOSEQ && LWIP_WND_SCALE */
460  }
461  }
462 
463  /* If a FIN segment was received, we call the callback
464  function with a NULL buffer to indicate EOF. */
465  if (recv_flags & TF_GOT_FIN) {
466  if (pcb->refused_data != NULL) {
467  /* Delay this if we have refused data. */
468  pcb->refused_data->flags |= PBUF_FLAG_TCP_FIN;
469  } else {
470  /* correct rcv_wnd as the application won't call tcp_recved()
471  for the FIN's seqno */
472  if (pcb->rcv_wnd != TCP_WND_MAX(pcb)) {
473  pcb->rcv_wnd++;
474  }
475  TCP_EVENT_CLOSED(pcb, err);
476  if (err == ERR_ABRT) {
477  goto aborted;
478  }
479  }
480  }
481 
482  tcp_input_pcb = NULL;
483  if (tcp_input_delayed_close(pcb)) {
484  goto aborted;
485  }
486  /* Try to send something out. */
487  tcp_output(pcb);
488 #if TCP_INPUT_DEBUG
489 #if TCP_DEBUG
490  tcp_debug_print_state(pcb->state);
491 #endif /* TCP_DEBUG */
492 #endif /* TCP_INPUT_DEBUG */
493  }
494  }
495  /* Jump target if pcb has been aborted in a callback (by calling tcp_abort()).
496  Below this line, 'pcb' may not be dereferenced! */
497 aborted:
498  tcp_input_pcb = NULL;
499  recv_data = NULL;
500 
501  /* give up our reference to inseg.p */
502  if (inseg.p != NULL)
503  {
504  pbuf_free(inseg.p);
505  inseg.p = NULL;
506  }
507  } else {
508 
509  /* If no matching PCB was found, send a TCP RST (reset) to the
510  sender. */
511  LWIP_DEBUGF(TCP_RST_DEBUG, ("tcp_input: no PCB match found, resetting.\n"));
512  if (!(TCPH_FLAGS(tcphdr) & TCP_RST)) {
513  TCP_STATS_INC(tcp.proterr);
514  TCP_STATS_INC(tcp.drop);
515  tcp_rst(ackno, seqno + tcplen, ip_current_dest_addr(),
516  ip_current_src_addr(), tcphdr->dest, tcphdr->src);
517  }
518  pbuf_free(p);
519  }
520 
521  LWIP_ASSERT("tcp_input: tcp_pcbs_sane()", tcp_pcbs_sane());
522  PERF_STOP("tcp_input");
523  return;
524 dropped:
525  TCP_STATS_INC(tcp.drop);
526  MIB2_STATS_INC(mib2.tcpinerrs);
527  pbuf_free(p);
528 }
529 
535 static int
536 tcp_input_delayed_close(struct tcp_pcb *pcb)
537 {
538  if (recv_flags & TF_CLOSED) {
539  /* The connection has been closed and we will deallocate the
540  PCB. */
541  if (!(pcb->flags & TF_RXCLOSED)) {
542  /* Connection closed although the application has only shut down the
543  tx side: call the PCB's err callback and indicate the closure to
544  ensure the application doesn't continue using the PCB. */
545  TCP_EVENT_ERR(pcb->state, pcb->errf, pcb->callback_arg, ERR_CLSD);
546  }
547  tcp_pcb_remove(&tcp_active_pcbs, pcb);
548  memp_free(MEMP_TCP_PCB, pcb);
549  return 1;
550  }
551  return 0;
552 }
553 
563 static void
564 tcp_listen_input(struct tcp_pcb_listen *pcb)
565 {
566  struct tcp_pcb *npcb;
567  u32_t iss;
568  err_t rc;
569 
570  if (flags & TCP_RST) {
571  /* An incoming RST should be ignored. Return. */
572  return;
573  }
574 
575  /* In the LISTEN state, we check for incoming SYN segments,
576  creates a new PCB, and responds with a SYN|ACK. */
577  if (flags & TCP_ACK) {
578  /* For incoming segments with the ACK flag set, respond with a
579  RST. */
580  LWIP_DEBUGF(TCP_RST_DEBUG, ("tcp_listen_input: ACK in LISTEN, sending reset\n"));
581  tcp_rst(ackno, seqno + tcplen, ip_current_dest_addr(),
582  ip_current_src_addr(), tcphdr->dest, tcphdr->src);
583  } else if (flags & TCP_SYN) {
584  LWIP_DEBUGF(TCP_DEBUG, ("TCP connection request %"U16_F" -> %"U16_F".\n", tcphdr->src, tcphdr->dest));
585 #if TCP_LISTEN_BACKLOG
586  if (pcb->accepts_pending >= pcb->backlog) {
587  LWIP_DEBUGF(TCP_DEBUG, ("tcp_listen_input: listen backlog exceeded for port %"U16_F"\n", tcphdr->dest));
588  return;
589  }
590 #endif /* TCP_LISTEN_BACKLOG */
591  npcb = tcp_alloc(pcb->prio);
592  /* If a new PCB could not be created (probably due to lack of memory),
593  we don't do anything, but rely on the sender will retransmit the
594  SYN at a time when we have more memory available. */
595  if (npcb == NULL) {
596  err_t err;
597  LWIP_DEBUGF(TCP_DEBUG, ("tcp_listen_input: could not allocate PCB\n"));
598  TCP_STATS_INC(tcp.memerr);
599  TCP_EVENT_ACCEPT(pcb, NULL, pcb->callback_arg, ERR_MEM, err);
600  LWIP_UNUSED_ARG(err); /* err not useful here */
601  return;
602  }
603 #if TCP_LISTEN_BACKLOG
604  pcb->accepts_pending++;
605  npcb->flags |= TF_BACKLOGPEND;
606 #endif /* TCP_LISTEN_BACKLOG */
607  /* Set up the new PCB. */
608  ip_addr_copy(npcb->local_ip, *ip_current_dest_addr());
609  ip_addr_copy(npcb->remote_ip, *ip_current_src_addr());
610  npcb->local_port = pcb->local_port;
611  npcb->remote_port = tcphdr->src;
612  npcb->state = SYN_RCVD;
613  npcb->rcv_nxt = seqno + 1;
614  npcb->rcv_ann_right_edge = npcb->rcv_nxt;
615  iss = tcp_next_iss(npcb);
616  npcb->snd_wl2 = iss;
617  npcb->snd_nxt = iss;
618  npcb->lastack = iss;
619  npcb->snd_lbb = iss;
620  npcb->snd_wl1 = seqno - 1;/* initialise to seqno-1 to force window update */
621  npcb->callback_arg = pcb->callback_arg;
622 #if LWIP_CALLBACK_API || TCP_LISTEN_BACKLOG
623  npcb->listener = pcb;
624 #endif /* LWIP_CALLBACK_API || TCP_LISTEN_BACKLOG */
625  /* inherit socket options */
626  npcb->so_options = pcb->so_options & SOF_INHERITED;
627  /* Register the new PCB so that we can begin receiving segments
628  for it. */
629  TCP_REG_ACTIVE(npcb);
630 
631  /* Parse any options in the SYN. */
632  tcp_parseopt(npcb);
633  npcb->snd_wnd = tcphdr->wnd;
634  npcb->snd_wnd_max = npcb->snd_wnd;
635 
636 #if TCP_CALCULATE_EFF_SEND_MSS
637  npcb->mss = tcp_eff_send_mss(npcb->mss, &npcb->local_ip, &npcb->remote_ip);
638 #endif /* TCP_CALCULATE_EFF_SEND_MSS */
639 
640  MIB2_STATS_INC(mib2.tcppassiveopens);
641 
642  /* Send a SYN|ACK together with the MSS option. */
643  rc = tcp_enqueue_flags(npcb, TCP_SYN | TCP_ACK);
644  if (rc != ERR_OK) {
645  tcp_abandon(npcb, 0);
646  return;
647  }
648  tcp_output(npcb);
649  }
650  return;
651 }
652 
662 static void
663 tcp_timewait_input(struct tcp_pcb *pcb)
664 {
665  /* RFC 1337: in TIME_WAIT, ignore RST and ACK FINs + any 'acceptable' segments */
666  /* RFC 793 3.9 Event Processing - Segment Arrives:
667  * - first check sequence number - we skip that one in TIME_WAIT (always
668  * acceptable since we only send ACKs)
669  * - second check the RST bit (... return) */
670  if (flags & TCP_RST) {
671  return;
672  }
673  /* - fourth, check the SYN bit, */
674  if (flags & TCP_SYN) {
675  /* If an incoming segment is not acceptable, an acknowledgment
676  should be sent in reply */
677  if (TCP_SEQ_BETWEEN(seqno, pcb->rcv_nxt, pcb->rcv_nxt + pcb->rcv_wnd)) {
678  /* If the SYN is in the window it is an error, send a reset */
679  tcp_rst(ackno, seqno + tcplen, ip_current_dest_addr(),
680  ip_current_src_addr(), tcphdr->dest, tcphdr->src);
681  return;
682  }
683  } else if (flags & TCP_FIN) {
684  /* - eighth, check the FIN bit: Remain in the TIME-WAIT state.
685  Restart the 2 MSL time-wait timeout.*/
686  pcb->tmr = tcp_ticks;
687  }
688 
689  if ((tcplen > 0)) {
690  /* Acknowledge data, FIN or out-of-window SYN */
691  pcb->flags |= TF_ACK_NOW;
692  tcp_output(pcb);
693  }
694  return;
695 }
696 
708 static err_t
709 tcp_process(struct tcp_pcb *pcb)
710 {
711  struct tcp_seg *rseg;
712  u8_t acceptable = 0;
713  err_t err;
714 
715  err = ERR_OK;
716 
717  /* Process incoming RST segments. */
718  if (flags & TCP_RST) {
719  /* First, determine if the reset is acceptable. */
720  if (pcb->state == SYN_SENT) {
721  /* "In the SYN-SENT state (a RST received in response to an initial SYN),
722  the RST is acceptable if the ACK field acknowledges the SYN." */
723  if (ackno == pcb->snd_nxt) {
724  acceptable = 1;
725  }
726  } else {
727  /* "In all states except SYN-SENT, all reset (RST) segments are validated
728  by checking their SEQ-fields." */
729  if (seqno == pcb->rcv_nxt) {
730  acceptable = 1;
731  } else if (TCP_SEQ_BETWEEN(seqno, pcb->rcv_nxt,
732  pcb->rcv_nxt + pcb->rcv_wnd)) {
733  /* If the sequence number is inside the window, we only send an ACK
734  and wait for a re-send with matching sequence number.
735  This violates RFC 793, but is required to protection against
736  CVE-2004-0230 (RST spoofing attack). */
737  tcp_ack_now(pcb);
738  }
739  }
740 
741  if (acceptable) {
742  LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_process: Connection RESET\n"));
743  LWIP_ASSERT("tcp_input: pcb->state != CLOSED", pcb->state != CLOSED);
744  recv_flags |= TF_RESET;
745  pcb->flags &= ~TF_ACK_DELAY;
746  return ERR_RST;
747  } else {
748  LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_process: unacceptable reset seqno %"U32_F" rcv_nxt %"U32_F"\n",
749  seqno, pcb->rcv_nxt));
750  LWIP_DEBUGF(TCP_DEBUG, ("tcp_process: unacceptable reset seqno %"U32_F" rcv_nxt %"U32_F"\n",
751  seqno, pcb->rcv_nxt));
752  return ERR_OK;
753  }
754  }
755 
756  if ((flags & TCP_SYN) && (pcb->state != SYN_SENT && pcb->state != SYN_RCVD)) {
757  /* Cope with new connection attempt after remote end crashed */
758  tcp_ack_now(pcb);
759  return ERR_OK;
760  }
761 
762  if ((pcb->flags & TF_RXCLOSED) == 0) {
763  /* Update the PCB (in)activity timer unless rx is closed (see tcp_shutdown) */
764  pcb->tmr = tcp_ticks;
765  }
766  pcb->keep_cnt_sent = 0;
767 
768  tcp_parseopt(pcb);
769 
770  /* Do different things depending on the TCP state. */
771  switch (pcb->state) {
772  case SYN_SENT:
773  LWIP_DEBUGF(TCP_INPUT_DEBUG, ("SYN-SENT: ackno %"U32_F" pcb->snd_nxt %"U32_F" unacked %"U32_F"\n", ackno,
774  pcb->snd_nxt, lwip_ntohl(pcb->unacked->tcphdr->seqno)));
775  /* received SYN ACK with expected sequence number? */
776  if ((flags & TCP_ACK) && (flags & TCP_SYN)
777  && (ackno == pcb->lastack + 1)) {
778  pcb->rcv_nxt = seqno + 1;
779  pcb->rcv_ann_right_edge = pcb->rcv_nxt;
780  pcb->lastack = ackno;
781  pcb->snd_wnd = tcphdr->wnd;
782  pcb->snd_wnd_max = pcb->snd_wnd;
783  pcb->snd_wl1 = seqno - 1; /* initialise to seqno - 1 to force window update */
784  pcb->state = ESTABLISHED;
785 
786 #if TCP_CALCULATE_EFF_SEND_MSS
787  pcb->mss = tcp_eff_send_mss(pcb->mss, &pcb->local_ip, &pcb->remote_ip);
788 #endif /* TCP_CALCULATE_EFF_SEND_MSS */
789 
790  pcb->cwnd = LWIP_TCP_CALC_INITIAL_CWND(pcb->mss);
791  LWIP_DEBUGF(TCP_CWND_DEBUG, ("tcp_process (SENT): cwnd %"TCPWNDSIZE_F
792  " ssthresh %"TCPWNDSIZE_F"\n",
793  pcb->cwnd, pcb->ssthresh));
794  LWIP_ASSERT("pcb->snd_queuelen > 0", (pcb->snd_queuelen > 0));
795  --pcb->snd_queuelen;
796  LWIP_DEBUGF(TCP_QLEN_DEBUG, ("tcp_process: SYN-SENT --queuelen %"TCPWNDSIZE_F"\n", (tcpwnd_size_t)pcb->snd_queuelen));
797  rseg = pcb->unacked;
798  if (rseg == NULL) {
799  /* might happen if tcp_output fails in tcp_rexmit_rto()
800  in which case the segment is on the unsent list */
801  rseg = pcb->unsent;
802  LWIP_ASSERT("no segment to free", rseg != NULL);
803  pcb->unsent = rseg->next;
804  } else {
805  pcb->unacked = rseg->next;
806  }
807  tcp_seg_free(rseg);
808 
809  /* If there's nothing left to acknowledge, stop the retransmit
810  timer, otherwise reset it to start again */
811  if (pcb->unacked == NULL) {
812  pcb->rtime = -1;
813  } else {
814  pcb->rtime = 0;
815  pcb->nrtx = 0;
816  }
817 
818  /* Call the user specified function to call when successfully
819  * connected. */
820  TCP_EVENT_CONNECTED(pcb, ERR_OK, err);
821  if (err == ERR_ABRT) {
822  return ERR_ABRT;
823  }
824  tcp_ack_now(pcb);
825  }
826  /* received ACK? possibly a half-open connection */
827  else if (flags & TCP_ACK) {
828  /* send a RST to bring the other side in a non-synchronized state. */
829  tcp_rst(ackno, seqno + tcplen, ip_current_dest_addr(),
830  ip_current_src_addr(), tcphdr->dest, tcphdr->src);
831  /* Resend SYN immediately (don't wait for rto timeout) to establish
832  connection faster, but do not send more SYNs than we otherwise would
833  have, or we might get caught in a loop on loopback interfaces. */
834  if (pcb->nrtx < TCP_SYNMAXRTX) {
835  pcb->rtime = 0;
836  tcp_rexmit_rto(pcb);
837  }
838  }
839  break;
840  case SYN_RCVD:
841  if (flags & TCP_ACK) {
842  /* expected ACK number? */
843  if (TCP_SEQ_BETWEEN(ackno, pcb->lastack+1, pcb->snd_nxt)) {
844  pcb->state = ESTABLISHED;
845  LWIP_DEBUGF(TCP_DEBUG, ("TCP connection established %"U16_F" -> %"U16_F".\n", inseg.tcphdr->src, inseg.tcphdr->dest));
846 #if LWIP_CALLBACK_API || TCP_LISTEN_BACKLOG
847 #if LWIP_CALLBACK_API
848  LWIP_ASSERT("pcb->listener->accept != NULL",
849  (pcb->listener == NULL) || (pcb->listener->accept != NULL));
850 #endif
851  if (pcb->listener == NULL) {
852  /* listen pcb might be closed by now */
853  err = ERR_VAL;
854  } else
855 #endif /* LWIP_CALLBACK_API || TCP_LISTEN_BACKLOG */
856  {
857  tcp_backlog_accepted(pcb);
858  /* Call the accept function. */
859  TCP_EVENT_ACCEPT(pcb->listener, pcb, pcb->callback_arg, ERR_OK, err);
860  }
861  if (err != ERR_OK) {
862  /* If the accept function returns with an error, we abort
863  * the connection. */
864  /* Already aborted? */
865  if (err != ERR_ABRT) {
866  tcp_abort(pcb);
867  }
868  return ERR_ABRT;
869  }
870  /* If there was any data contained within this ACK,
871  * we'd better pass it on to the application as well. */
872  tcp_receive(pcb);
873 
874  /* Prevent ACK for SYN to generate a sent event */
875  if (recv_acked != 0) {
876  recv_acked--;
877  }
878 
879  pcb->cwnd = LWIP_TCP_CALC_INITIAL_CWND(pcb->mss);
880  LWIP_DEBUGF(TCP_CWND_DEBUG, ("tcp_process (SYN_RCVD): cwnd %"TCPWNDSIZE_F
881  " ssthresh %"TCPWNDSIZE_F"\n",
882  pcb->cwnd, pcb->ssthresh));
883 
884  if (recv_flags & TF_GOT_FIN) {
885  tcp_ack_now(pcb);
886  pcb->state = CLOSE_WAIT;
887  }
888  } else {
889  /* incorrect ACK number, send RST */
890  tcp_rst(ackno, seqno + tcplen, ip_current_dest_addr(),
891  ip_current_src_addr(), tcphdr->dest, tcphdr->src);
892  }
893  } else if ((flags & TCP_SYN) && (seqno == pcb->rcv_nxt - 1)) {
894  /* Looks like another copy of the SYN - retransmit our SYN-ACK */
895  tcp_rexmit(pcb);
896  }
897  break;
898  case CLOSE_WAIT:
899  /* FALLTHROUGH */
900  case ESTABLISHED:
901  tcp_receive(pcb);
902  if (recv_flags & TF_GOT_FIN) { /* passive close */
903  tcp_ack_now(pcb);
904  pcb->state = CLOSE_WAIT;
905  }
906  break;
907  case FIN_WAIT_1:
908  tcp_receive(pcb);
909  if (recv_flags & TF_GOT_FIN) {
910  if ((flags & TCP_ACK) && (ackno == pcb->snd_nxt) &&
911  pcb->unsent == NULL) {
913  ("TCP connection closed: FIN_WAIT_1 %"U16_F" -> %"U16_F".\n", inseg.tcphdr->src, inseg.tcphdr->dest));
914  tcp_ack_now(pcb);
915  tcp_pcb_purge(pcb);
916  TCP_RMV_ACTIVE(pcb);
917  pcb->state = TIME_WAIT;
918  TCP_REG(&tcp_tw_pcbs, pcb);
919  } else {
920  tcp_ack_now(pcb);
921  pcb->state = CLOSING;
922  }
923  } else if ((flags & TCP_ACK) && (ackno == pcb->snd_nxt) &&
924  pcb->unsent == NULL) {
925  pcb->state = FIN_WAIT_2;
926  }
927  break;
928  case FIN_WAIT_2:
929  tcp_receive(pcb);
930  if (recv_flags & TF_GOT_FIN) {
931  LWIP_DEBUGF(TCP_DEBUG, ("TCP connection closed: FIN_WAIT_2 %"U16_F" -> %"U16_F".\n", inseg.tcphdr->src, inseg.tcphdr->dest));
932  tcp_ack_now(pcb);
933  tcp_pcb_purge(pcb);
934  TCP_RMV_ACTIVE(pcb);
935  pcb->state = TIME_WAIT;
936  TCP_REG(&tcp_tw_pcbs, pcb);
937  }
938  break;
939  case CLOSING:
940  tcp_receive(pcb);
941  if ((flags & TCP_ACK) && ackno == pcb->snd_nxt && pcb->unsent == NULL) {
942  LWIP_DEBUGF(TCP_DEBUG, ("TCP connection closed: CLOSING %"U16_F" -> %"U16_F".\n", inseg.tcphdr->src, inseg.tcphdr->dest));
943  tcp_pcb_purge(pcb);
944  TCP_RMV_ACTIVE(pcb);
945  pcb->state = TIME_WAIT;
946  TCP_REG(&tcp_tw_pcbs, pcb);
947  }
948  break;
949  case LAST_ACK:
950  tcp_receive(pcb);
951  if ((flags & TCP_ACK) && ackno == pcb->snd_nxt && pcb->unsent == NULL) {
952  LWIP_DEBUGF(TCP_DEBUG, ("TCP connection closed: LAST_ACK %"U16_F" -> %"U16_F".\n", inseg.tcphdr->src, inseg.tcphdr->dest));
953  /* bugfix #21699: don't set pcb->state to CLOSED here or we risk leaking segments */
954  recv_flags |= TF_CLOSED;
955  }
956  break;
957  default:
958  break;
959  }
960  return ERR_OK;
961 }
962 
963 #if TCP_QUEUE_OOSEQ
964 
969 static void
970 tcp_oos_insert_segment(struct tcp_seg *cseg, struct tcp_seg *next)
971 {
972  struct tcp_seg *old_seg;
973 
974  if (TCPH_FLAGS(cseg->tcphdr) & TCP_FIN) {
975  /* received segment overlaps all following segments */
976  tcp_segs_free(next);
977  next = NULL;
978  } else {
979  /* delete some following segments
980  oos queue may have segments with FIN flag */
981  while (next &&
982  TCP_SEQ_GEQ((seqno + cseg->len),
983  (next->tcphdr->seqno + next->len))) {
984  /* cseg with FIN already processed */
985  if (TCPH_FLAGS(next->tcphdr) & TCP_FIN) {
986  TCPH_SET_FLAG(cseg->tcphdr, TCP_FIN);
987  }
988  old_seg = next;
989  next = next->next;
990  tcp_seg_free(old_seg);
991  }
992  if (next &&
993  TCP_SEQ_GT(seqno + cseg->len, next->tcphdr->seqno)) {
994  /* We need to trim the incoming segment. */
995  cseg->len = (u16_t)(next->tcphdr->seqno - seqno);
996  pbuf_realloc(cseg->p, cseg->len);
997  }
998  }
999  cseg->next = next;
1000 }
1001 #endif /* TCP_QUEUE_OOSEQ */
1002 
1015 static void
1016 tcp_receive(struct tcp_pcb *pcb)
1017 {
1018  struct tcp_seg *next;
1019 #if TCP_QUEUE_OOSEQ
1020  struct tcp_seg *prev, *cseg;
1021 #endif /* TCP_QUEUE_OOSEQ */
1022  s32_t off;
1023  s16_t m;
1024  u32_t right_wnd_edge;
1025  u16_t new_tot_len;
1026  int found_dupack = 0;
1027 #if TCP_OOSEQ_MAX_BYTES || TCP_OOSEQ_MAX_PBUFS
1028  u32_t ooseq_blen;
1029  u16_t ooseq_qlen;
1030 #endif /* TCP_OOSEQ_MAX_BYTES || TCP_OOSEQ_MAX_PBUFS */
1031 
1032  LWIP_ASSERT("tcp_receive: wrong state", pcb->state >= ESTABLISHED);
1033 
1034  if (flags & TCP_ACK) {
1035  right_wnd_edge = pcb->snd_wnd + pcb->snd_wl2;
1036 
1037  /* Update window. */
1038  if (TCP_SEQ_LT(pcb->snd_wl1, seqno) ||
1039  (pcb->snd_wl1 == seqno && TCP_SEQ_LT(pcb->snd_wl2, ackno)) ||
1040  (pcb->snd_wl2 == ackno && (u32_t)SND_WND_SCALE(pcb, tcphdr->wnd) > pcb->snd_wnd)) {
1041  pcb->snd_wnd = SND_WND_SCALE(pcb, tcphdr->wnd);
1042  /* keep track of the biggest window announced by the remote host to calculate
1043  the maximum segment size */
1044  if (pcb->snd_wnd_max < pcb->snd_wnd) {
1045  pcb->snd_wnd_max = pcb->snd_wnd;
1046  }
1047  pcb->snd_wl1 = seqno;
1048  pcb->snd_wl2 = ackno;
1049  if (pcb->snd_wnd == 0) {
1050  if (pcb->persist_backoff == 0) {
1051  /* start persist timer */
1052  pcb->persist_cnt = 0;
1053  pcb->persist_backoff = 1;
1054  }
1055  } else if (pcb->persist_backoff > 0) {
1056  /* stop persist timer */
1057  pcb->persist_backoff = 0;
1058  }
1059  LWIP_DEBUGF(TCP_WND_DEBUG, ("tcp_receive: window update %"TCPWNDSIZE_F"\n", pcb->snd_wnd));
1060 #if TCP_WND_DEBUG
1061  } else {
1062  if (pcb->snd_wnd != (tcpwnd_size_t)SND_WND_SCALE(pcb, tcphdr->wnd)) {
1064  ("tcp_receive: no window update lastack %"U32_F" ackno %"
1065  U32_F" wl1 %"U32_F" seqno %"U32_F" wl2 %"U32_F"\n",
1066  pcb->lastack, ackno, pcb->snd_wl1, seqno, pcb->snd_wl2));
1067  }
1068 #endif /* TCP_WND_DEBUG */
1069  }
1070 
1071  /* (From Stevens TCP/IP Illustrated Vol II, p970.) Its only a
1072  * duplicate ack if:
1073  * 1) It doesn't ACK new data
1074  * 2) length of received packet is zero (i.e. no payload)
1075  * 3) the advertised window hasn't changed
1076  * 4) There is outstanding unacknowledged data (retransmission timer running)
1077  * 5) The ACK is == biggest ACK sequence number so far seen (snd_una)
1078  *
1079  * If it passes all five, should process as a dupack:
1080  * a) dupacks < 3: do nothing
1081  * b) dupacks == 3: fast retransmit
1082  * c) dupacks > 3: increase cwnd
1083  *
1084  * If it only passes 1-3, should reset dupack counter (and add to
1085  * stats, which we don't do in lwIP)
1086  *
1087  * If it only passes 1, should reset dupack counter
1088  *
1089  */
1090 
1091  /* Clause 1 */
1092  if (TCP_SEQ_LEQ(ackno, pcb->lastack)) {
1093  /* Clause 2 */
1094  if (tcplen == 0) {
1095  /* Clause 3 */
1096  if (pcb->snd_wl2 + pcb->snd_wnd == right_wnd_edge) {
1097  /* Clause 4 */
1098  if (pcb->rtime >= 0) {
1099  /* Clause 5 */
1100  if (pcb->lastack == ackno) {
1101  found_dupack = 1;
1102  if ((u8_t)(pcb->dupacks + 1) > pcb->dupacks) {
1103  ++pcb->dupacks;
1104  }
1105  if (pcb->dupacks > 3) {
1106  /* Inflate the congestion window, but not if it means that
1107  the value overflows. */
1108  if ((tcpwnd_size_t)(pcb->cwnd + pcb->mss) > pcb->cwnd) {
1109  pcb->cwnd += pcb->mss;
1110  }
1111  } else if (pcb->dupacks == 3) {
1112  /* Do fast retransmit */
1113  tcp_rexmit_fast(pcb);
1114  }
1115  }
1116  }
1117  }
1118  }
1119  /* If Clause (1) or more is true, but not a duplicate ack, reset
1120  * count of consecutive duplicate acks */
1121  if (!found_dupack) {
1122  pcb->dupacks = 0;
1123  }
1124  } else if (TCP_SEQ_BETWEEN(ackno, pcb->lastack+1, pcb->snd_nxt)) {
1125  /* We come here when the ACK acknowledges new data. */
1126 
1127  /* Reset the "IN Fast Retransmit" flag, since we are no longer
1128  in fast retransmit. Also reset the congestion window to the
1129  slow start threshold. */
1130  if (pcb->flags & TF_INFR) {
1131  pcb->flags &= ~TF_INFR;
1132  pcb->cwnd = pcb->ssthresh;
1133  }
1134 
1135  /* Reset the number of retransmissions. */
1136  pcb->nrtx = 0;
1137 
1138  /* Reset the retransmission time-out. */
1139  pcb->rto = (pcb->sa >> 3) + pcb->sv;
1140 
1141  /* Reset the fast retransmit variables. */
1142  pcb->dupacks = 0;
1143  pcb->lastack = ackno;
1144 
1145  /* Update the congestion control variables (cwnd and
1146  ssthresh). */
1147  if (pcb->state >= ESTABLISHED) {
1148  if (pcb->cwnd < pcb->ssthresh) {
1149  if ((tcpwnd_size_t)(pcb->cwnd + pcb->mss) > pcb->cwnd) {
1150  pcb->cwnd += pcb->mss;
1151  }
1152  LWIP_DEBUGF(TCP_CWND_DEBUG, ("tcp_receive: slow start cwnd %"TCPWNDSIZE_F"\n", pcb->cwnd));
1153  } else {
1154  tcpwnd_size_t new_cwnd = (pcb->cwnd + pcb->mss * pcb->mss / pcb->cwnd);
1155  if (new_cwnd > pcb->cwnd) {
1156  pcb->cwnd = new_cwnd;
1157  }
1158  LWIP_DEBUGF(TCP_CWND_DEBUG, ("tcp_receive: congestion avoidance cwnd %"TCPWNDSIZE_F"\n", pcb->cwnd));
1159  }
1160  }
1161  LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_receive: ACK for %"U32_F", unacked->seqno %"U32_F":%"U32_F"\n",
1162  ackno,
1163  pcb->unacked != NULL?
1164  lwip_ntohl(pcb->unacked->tcphdr->seqno): 0,
1165  pcb->unacked != NULL?
1166  lwip_ntohl(pcb->unacked->tcphdr->seqno) + TCP_TCPLEN(pcb->unacked): 0));
1167 
1168  /* Remove segment from the unacknowledged list if the incoming
1169  ACK acknowledges them. */
1170  while (pcb->unacked != NULL &&
1171  TCP_SEQ_LEQ(lwip_ntohl(pcb->unacked->tcphdr->seqno) +
1172  TCP_TCPLEN(pcb->unacked), ackno)) {
1173  LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_receive: removing %"U32_F":%"U32_F" from pcb->unacked\n",
1174  lwip_ntohl(pcb->unacked->tcphdr->seqno),
1175  lwip_ntohl(pcb->unacked->tcphdr->seqno) +
1176  TCP_TCPLEN(pcb->unacked)));
1177 
1178  next = pcb->unacked;
1179  pcb->unacked = pcb->unacked->next;
1180 
1181  LWIP_DEBUGF(TCP_QLEN_DEBUG, ("tcp_receive: queuelen %"TCPWNDSIZE_F" ... ", (tcpwnd_size_t)pcb->snd_queuelen));
1182  LWIP_ASSERT("pcb->snd_queuelen >= pbuf_clen(next->p)", (pcb->snd_queuelen >= pbuf_clen(next->p)));
1183 
1184  pcb->snd_queuelen -= pbuf_clen(next->p);
1185  recv_acked += next->len;
1186  tcp_seg_free(next);
1187 
1188  LWIP_DEBUGF(TCP_QLEN_DEBUG, ("%"TCPWNDSIZE_F" (after freeing unacked)\n", (tcpwnd_size_t)pcb->snd_queuelen));
1189  if (pcb->snd_queuelen != 0) {
1190  LWIP_ASSERT("tcp_receive: valid queue length", pcb->unacked != NULL ||
1191  pcb->unsent != NULL);
1192  }
1193  }
1194 
1195  /* If there's nothing left to acknowledge, stop the retransmit
1196  timer, otherwise reset it to start again */
1197  if (pcb->unacked == NULL) {
1198  pcb->rtime = -1;
1199  } else {
1200  pcb->rtime = 0;
1201  }
1202 
1203  pcb->polltmr = 0;
1204 
1205 #if LWIP_IPV6 && LWIP_ND6_TCP_REACHABILITY_HINTS
1206  if (ip_current_is_v6()) {
1207  /* Inform neighbor reachability of forward progress. */
1208  nd6_reachability_hint(ip6_current_src_addr());
1209  }
1210 #endif /* LWIP_IPV6 && LWIP_ND6_TCP_REACHABILITY_HINTS*/
1211  } else {
1212  /* Out of sequence ACK, didn't really ack anything */
1213  tcp_send_empty_ack(pcb);
1214  }
1215 
1216  /* We go through the ->unsent list to see if any of the segments
1217  on the list are acknowledged by the ACK. This may seem
1218  strange since an "unsent" segment shouldn't be acked. The
1219  rationale is that lwIP puts all outstanding segments on the
1220  ->unsent list after a retransmission, so these segments may
1221  in fact have been sent once. */
1222  while (pcb->unsent != NULL &&
1223  TCP_SEQ_BETWEEN(ackno, lwip_ntohl(pcb->unsent->tcphdr->seqno) +
1224  TCP_TCPLEN(pcb->unsent), pcb->snd_nxt)) {
1225  LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_receive: removing %"U32_F":%"U32_F" from pcb->unsent\n",
1226  lwip_ntohl(pcb->unsent->tcphdr->seqno), lwip_ntohl(pcb->unsent->tcphdr->seqno) +
1227  TCP_TCPLEN(pcb->unsent)));
1228 
1229  next = pcb->unsent;
1230  pcb->unsent = pcb->unsent->next;
1231 #if TCP_OVERSIZE
1232  if (pcb->unsent == NULL) {
1233  pcb->unsent_oversize = 0;
1234  }
1235 #endif /* TCP_OVERSIZE */
1236  LWIP_DEBUGF(TCP_QLEN_DEBUG, ("tcp_receive: queuelen %"TCPWNDSIZE_F" ... ", (tcpwnd_size_t)pcb->snd_queuelen));
1237  LWIP_ASSERT("pcb->snd_queuelen >= pbuf_clen(next->p)", (pcb->snd_queuelen >= pbuf_clen(next->p)));
1238  /* Prevent ACK for FIN to generate a sent event */
1239  pcb->snd_queuelen -= pbuf_clen(next->p);
1240  recv_acked += next->len;
1241  tcp_seg_free(next);
1242  LWIP_DEBUGF(TCP_QLEN_DEBUG, ("%"TCPWNDSIZE_F" (after freeing unsent)\n", (tcpwnd_size_t)pcb->snd_queuelen));
1243  if (pcb->snd_queuelen != 0) {
1244  LWIP_ASSERT("tcp_receive: valid queue length",
1245  pcb->unacked != NULL || pcb->unsent != NULL);
1246  }
1247  }
1248  pcb->snd_buf += recv_acked;
1249  /* End of ACK for new data processing. */
1250 
1251  LWIP_DEBUGF(TCP_RTO_DEBUG, ("tcp_receive: pcb->rttest %"U32_F" rtseq %"U32_F" ackno %"U32_F"\n",
1252  pcb->rttest, pcb->rtseq, ackno));
1253 
1254  /* RTT estimation calculations. This is done by checking if the
1255  incoming segment acknowledges the segment we use to take a
1256  round-trip time measurement. */
1257  if (pcb->rttest && TCP_SEQ_LT(pcb->rtseq, ackno)) {
1258  /* diff between this shouldn't exceed 32K since this are tcp timer ticks
1259  and a round-trip shouldn't be that long... */
1260  m = (s16_t)(tcp_ticks - pcb->rttest);
1261 
1262  LWIP_DEBUGF(TCP_RTO_DEBUG, ("tcp_receive: experienced rtt %"U16_F" ticks (%"U16_F" msec).\n",
1263  m, (u16_t)(m * TCP_SLOW_INTERVAL)));
1264 
1265  /* This is taken directly from VJs original code in his paper */
1266  m = m - (pcb->sa >> 3);
1267  pcb->sa += m;
1268  if (m < 0) {
1269  m = -m;
1270  }
1271  m = m - (pcb->sv >> 2);
1272  pcb->sv += m;
1273  pcb->rto = (pcb->sa >> 3) + pcb->sv;
1274 
1275  LWIP_DEBUGF(TCP_RTO_DEBUG, ("tcp_receive: RTO %"U16_F" (%"U16_F" milliseconds)\n",
1276  pcb->rto, (u16_t)(pcb->rto * TCP_SLOW_INTERVAL)));
1277 
1278  pcb->rttest = 0;
1279  }
1280  }
1281 
1282  /* If the incoming segment contains data, we must process it
1283  further unless the pcb already received a FIN.
1284  (RFC 793, chapter 3.9, "SEGMENT ARRIVES" in states CLOSE-WAIT, CLOSING,
1285  LAST-ACK and TIME-WAIT: "Ignore the segment text.") */
1286  if ((tcplen > 0) && (pcb->state < CLOSE_WAIT)) {
1287  /* This code basically does three things:
1288 
1289  +) If the incoming segment contains data that is the next
1290  in-sequence data, this data is passed to the application. This
1291  might involve trimming the first edge of the data. The rcv_nxt
1292  variable and the advertised window are adjusted.
1293 
1294  +) If the incoming segment has data that is above the next
1295  sequence number expected (->rcv_nxt), the segment is placed on
1296  the ->ooseq queue. This is done by finding the appropriate
1297  place in the ->ooseq queue (which is ordered by sequence
1298  number) and trim the segment in both ends if needed. An
1299  immediate ACK is sent to indicate that we received an
1300  out-of-sequence segment.
1301 
1302  +) Finally, we check if the first segment on the ->ooseq queue
1303  now is in sequence (i.e., if rcv_nxt >= ooseq->seqno). If
1304  rcv_nxt > ooseq->seqno, we must trim the first edge of the
1305  segment on ->ooseq before we adjust rcv_nxt. The data in the
1306  segments that are now on sequence are chained onto the
1307  incoming segment so that we only need to call the application
1308  once.
1309  */
1310 
1311  /* First, we check if we must trim the first edge. We have to do
1312  this if the sequence number of the incoming segment is less
1313  than rcv_nxt, and the sequence number plus the length of the
1314  segment is larger than rcv_nxt. */
1315  /* if (TCP_SEQ_LT(seqno, pcb->rcv_nxt)) {
1316  if (TCP_SEQ_LT(pcb->rcv_nxt, seqno + tcplen)) {*/
1317  if (TCP_SEQ_BETWEEN(pcb->rcv_nxt, seqno + 1, seqno + tcplen - 1)) {
1318  /* Trimming the first edge is done by pushing the payload
1319  pointer in the pbuf downwards. This is somewhat tricky since
1320  we do not want to discard the full contents of the pbuf up to
1321  the new starting point of the data since we have to keep the
1322  TCP header which is present in the first pbuf in the chain.
1323 
1324  What is done is really quite a nasty hack: the first pbuf in
1325  the pbuf chain is pointed to by inseg.p. Since we need to be
1326  able to deallocate the whole pbuf, we cannot change this
1327  inseg.p pointer to point to any of the later pbufs in the
1328  chain. Instead, we point the ->payload pointer in the first
1329  pbuf to data in one of the later pbufs. We also set the
1330  inseg.data pointer to point to the right place. This way, the
1331  ->p pointer will still point to the first pbuf, but the
1332  ->p->payload pointer will point to data in another pbuf.
1333 
1334  After we are done with adjusting the pbuf pointers we must
1335  adjust the ->data pointer in the seg and the segment
1336  length.*/
1337 
1338  struct pbuf *p = inseg.p;
1339  off = pcb->rcv_nxt - seqno;
1340  LWIP_ASSERT("inseg.p != NULL", inseg.p);
1341  LWIP_ASSERT("insane offset!", (off < 0x7fff));
1342  if (inseg.p->len < off) {
1343  LWIP_ASSERT("pbuf too short!", (((s32_t)inseg.p->tot_len) >= off));
1344  new_tot_len = (u16_t)(inseg.p->tot_len - off);
1345  while (p->len < off) {
1346  off -= p->len;
1347  /* KJM following line changed (with addition of new_tot_len var)
1348  to fix bug #9076
1349  inseg.p->tot_len -= p->len; */
1350  p->tot_len = new_tot_len;
1351  p->len = 0;
1352  p = p->next;
1353  }
1354  if (pbuf_header(p, (s16_t)-off)) {
1355  /* Do we need to cope with this failing? Assert for now */
1356  LWIP_ASSERT("pbuf_header failed", 0);
1357  }
1358  } else {
1359  if (pbuf_header(inseg.p, (s16_t)-off)) {
1360  /* Do we need to cope with this failing? Assert for now */
1361  LWIP_ASSERT("pbuf_header failed", 0);
1362  }
1363  }
1364  inseg.len -= (u16_t)(pcb->rcv_nxt - seqno);
1365  inseg.tcphdr->seqno = seqno = pcb->rcv_nxt;
1366  }
1367  else {
1368  if (TCP_SEQ_LT(seqno, pcb->rcv_nxt)) {
1369  /* the whole segment is < rcv_nxt */
1370  /* must be a duplicate of a packet that has already been correctly handled */
1371 
1372  LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_receive: duplicate seqno %"U32_F"\n", seqno));
1373  tcp_ack_now(pcb);
1374  }
1375  }
1376 
1377  /* The sequence number must be within the window (above rcv_nxt
1378  and below rcv_nxt + rcv_wnd) in order to be further
1379  processed. */
1380  if (TCP_SEQ_BETWEEN(seqno, pcb->rcv_nxt,
1381  pcb->rcv_nxt + pcb->rcv_wnd - 1)) {
1382  if (pcb->rcv_nxt == seqno) {
1383  /* The incoming segment is the next in sequence. We check if
1384  we have to trim the end of the segment and update rcv_nxt
1385  and pass the data to the application. */
1386  tcplen = TCP_TCPLEN(&inseg);
1387 
1388  if (tcplen > pcb->rcv_wnd) {
1390  ("tcp_receive: other end overran receive window"
1391  "seqno %"U32_F" len %"U16_F" right edge %"U32_F"\n",
1392  seqno, tcplen, pcb->rcv_nxt + pcb->rcv_wnd));
1393  if (TCPH_FLAGS(inseg.tcphdr) & TCP_FIN) {
1394  /* Must remove the FIN from the header as we're trimming
1395  * that byte of sequence-space from the packet */
1396  TCPH_FLAGS_SET(inseg.tcphdr, TCPH_FLAGS(inseg.tcphdr) & ~(unsigned int)TCP_FIN);
1397  }
1398  /* Adjust length of segment to fit in the window. */
1399  TCPWND_CHECK16(pcb->rcv_wnd);
1400  inseg.len = (u16_t)pcb->rcv_wnd;
1401  if (TCPH_FLAGS(inseg.tcphdr) & TCP_SYN) {
1402  inseg.len -= 1;
1403  }
1404  pbuf_realloc(inseg.p, inseg.len);
1405  tcplen = TCP_TCPLEN(&inseg);
1406  LWIP_ASSERT("tcp_receive: segment not trimmed correctly to rcv_wnd\n",
1407  (seqno + tcplen) == (pcb->rcv_nxt + pcb->rcv_wnd));
1408  }
1409 #if TCP_QUEUE_OOSEQ
1410  /* Received in-sequence data, adjust ooseq data if:
1411  - FIN has been received or
1412  - inseq overlaps with ooseq */
1413  if (pcb->ooseq != NULL) {
1414  if (TCPH_FLAGS(inseg.tcphdr) & TCP_FIN) {
1416  ("tcp_receive: received in-order FIN, binning ooseq queue\n"));
1417  /* Received in-order FIN means anything that was received
1418  * out of order must now have been received in-order, so
1419  * bin the ooseq queue */
1420  while (pcb->ooseq != NULL) {
1421  struct tcp_seg *old_ooseq = pcb->ooseq;
1422  pcb->ooseq = pcb->ooseq->next;
1423  tcp_seg_free(old_ooseq);
1424  }
1425  } else {
1426  next = pcb->ooseq;
1427  /* Remove all segments on ooseq that are covered by inseg already.
1428  * FIN is copied from ooseq to inseg if present. */
1429  while (next &&
1430  TCP_SEQ_GEQ(seqno + tcplen,
1431  next->tcphdr->seqno + next->len)) {
1432  /* inseg cannot have FIN here (already processed above) */
1433  if ((TCPH_FLAGS(next->tcphdr) & TCP_FIN) != 0 &&
1434  (TCPH_FLAGS(inseg.tcphdr) & TCP_SYN) == 0) {
1435  TCPH_SET_FLAG(inseg.tcphdr, TCP_FIN);
1436  tcplen = TCP_TCPLEN(&inseg);
1437  }
1438  prev = next;
1439  next = next->next;
1440  tcp_seg_free(prev);
1441  }
1442  /* Now trim right side of inseg if it overlaps with the first
1443  * segment on ooseq */
1444  if (next &&
1445  TCP_SEQ_GT(seqno + tcplen,
1446  next->tcphdr->seqno)) {
1447  /* inseg cannot have FIN here (already processed above) */
1448  inseg.len = (u16_t)(next->tcphdr->seqno - seqno);
1449  if (TCPH_FLAGS(inseg.tcphdr) & TCP_SYN) {
1450  inseg.len -= 1;
1451  }
1452  pbuf_realloc(inseg.p, inseg.len);
1453  tcplen = TCP_TCPLEN(&inseg);
1454  LWIP_ASSERT("tcp_receive: segment not trimmed correctly to ooseq queue\n",
1455  (seqno + tcplen) == next->tcphdr->seqno);
1456  }
1457  pcb->ooseq = next;
1458  }
1459  }
1460 #endif /* TCP_QUEUE_OOSEQ */
1461 
1462  pcb->rcv_nxt = seqno + tcplen;
1463 
1464  /* Update the receiver's (our) window. */
1465  LWIP_ASSERT("tcp_receive: tcplen > rcv_wnd\n", pcb->rcv_wnd >= tcplen);
1466  pcb->rcv_wnd -= tcplen;
1467 
1468  tcp_update_rcv_ann_wnd(pcb);
1469 
1470  /* If there is data in the segment, we make preparations to
1471  pass this up to the application. The ->recv_data variable
1472  is used for holding the pbuf that goes to the
1473  application. The code for reassembling out-of-sequence data
1474  chains its data on this pbuf as well.
1475 
1476  If the segment was a FIN, we set the TF_GOT_FIN flag that will
1477  be used to indicate to the application that the remote side has
1478  closed its end of the connection. */
1479  if (inseg.p->tot_len > 0) {
1480  recv_data = inseg.p;
1481  /* Since this pbuf now is the responsibility of the
1482  application, we delete our reference to it so that we won't
1483  (mistakingly) deallocate it. */
1484  inseg.p = NULL;
1485  }
1486  if (TCPH_FLAGS(inseg.tcphdr) & TCP_FIN) {
1487  LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_receive: received FIN.\n"));
1488  recv_flags |= TF_GOT_FIN;
1489  }
1490 
1491 #if TCP_QUEUE_OOSEQ
1492  /* We now check if we have segments on the ->ooseq queue that
1493  are now in sequence. */
1494  while (pcb->ooseq != NULL &&
1495  pcb->ooseq->tcphdr->seqno == pcb->rcv_nxt) {
1496 
1497  cseg = pcb->ooseq;
1498  seqno = pcb->ooseq->tcphdr->seqno;
1499 
1500  pcb->rcv_nxt += TCP_TCPLEN(cseg);
1501  LWIP_ASSERT("tcp_receive: ooseq tcplen > rcv_wnd\n",
1502  pcb->rcv_wnd >= TCP_TCPLEN(cseg));
1503  pcb->rcv_wnd -= TCP_TCPLEN(cseg);
1504 
1505  tcp_update_rcv_ann_wnd(pcb);
1506 
1507  if (cseg->p->tot_len > 0) {
1508  /* Chain this pbuf onto the pbuf that we will pass to
1509  the application. */
1510  /* With window scaling, this can overflow recv_data->tot_len, but
1511  that's not a problem since we explicitly fix that before passing
1512  recv_data to the application. */
1513  if (recv_data) {
1514  pbuf_cat(recv_data, cseg->p);
1515  } else {
1516  recv_data = cseg->p;
1517  }
1518  cseg->p = NULL;
1519  }
1520  if (TCPH_FLAGS(cseg->tcphdr) & TCP_FIN) {
1521  LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_receive: dequeued FIN.\n"));
1522  recv_flags |= TF_GOT_FIN;
1523  if (pcb->state == ESTABLISHED) { /* force passive close or we can move to active close */
1524  pcb->state = CLOSE_WAIT;
1525  }
1526  }
1527 
1528  pcb->ooseq = cseg->next;
1529  tcp_seg_free(cseg);
1530  }
1531 #endif /* TCP_QUEUE_OOSEQ */
1532 
1533 
1534  /* Acknowledge the segment(s). */
1535  tcp_ack(pcb);
1536 
1537 #if LWIP_IPV6 && LWIP_ND6_TCP_REACHABILITY_HINTS
1538  if (ip_current_is_v6()) {
1539  /* Inform neighbor reachability of forward progress. */
1540  nd6_reachability_hint(ip6_current_src_addr());
1541  }
1542 #endif /* LWIP_IPV6 && LWIP_ND6_TCP_REACHABILITY_HINTS*/
1543 
1544  } else {
1545  /* We get here if the incoming segment is out-of-sequence. */
1546  tcp_send_empty_ack(pcb);
1547 #if TCP_QUEUE_OOSEQ
1548  /* We queue the segment on the ->ooseq queue. */
1549  if (pcb->ooseq == NULL) {
1550  pcb->ooseq = tcp_seg_copy(&inseg);
1551  } else {
1552  /* If the queue is not empty, we walk through the queue and
1553  try to find a place where the sequence number of the
1554  incoming segment is between the sequence numbers of the
1555  previous and the next segment on the ->ooseq queue. That is
1556  the place where we put the incoming segment. If needed, we
1557  trim the second edges of the previous and the incoming
1558  segment so that it will fit into the sequence.
1559 
1560  If the incoming segment has the same sequence number as a
1561  segment on the ->ooseq queue, we discard the segment that
1562  contains less data. */
1563 
1564  prev = NULL;
1565  for (next = pcb->ooseq; next != NULL; next = next->next) {
1566  if (seqno == next->tcphdr->seqno) {
1567  /* The sequence number of the incoming segment is the
1568  same as the sequence number of the segment on
1569  ->ooseq. We check the lengths to see which one to
1570  discard. */
1571  if (inseg.len > next->len) {
1572  /* The incoming segment is larger than the old
1573  segment. We replace some segments with the new
1574  one. */
1575  cseg = tcp_seg_copy(&inseg);
1576  if (cseg != NULL) {
1577  if (prev != NULL) {
1578  prev->next = cseg;
1579  } else {
1580  pcb->ooseq = cseg;
1581  }
1582  tcp_oos_insert_segment(cseg, next);
1583  }
1584  break;
1585  } else {
1586  /* Either the lengths are the same or the incoming
1587  segment was smaller than the old one; in either
1588  case, we ditch the incoming segment. */
1589  break;
1590  }
1591  } else {
1592  if (prev == NULL) {
1593  if (TCP_SEQ_LT(seqno, next->tcphdr->seqno)) {
1594  /* The sequence number of the incoming segment is lower
1595  than the sequence number of the first segment on the
1596  queue. We put the incoming segment first on the
1597  queue. */
1598  cseg = tcp_seg_copy(&inseg);
1599  if (cseg != NULL) {
1600  pcb->ooseq = cseg;
1601  tcp_oos_insert_segment(cseg, next);
1602  }
1603  break;
1604  }
1605  } else {
1606  /*if (TCP_SEQ_LT(prev->tcphdr->seqno, seqno) &&
1607  TCP_SEQ_LT(seqno, next->tcphdr->seqno)) {*/
1608  if (TCP_SEQ_BETWEEN(seqno, prev->tcphdr->seqno+1, next->tcphdr->seqno-1)) {
1609  /* The sequence number of the incoming segment is in
1610  between the sequence numbers of the previous and
1611  the next segment on ->ooseq. We trim trim the previous
1612  segment, delete next segments that included in received segment
1613  and trim received, if needed. */
1614  cseg = tcp_seg_copy(&inseg);
1615  if (cseg != NULL) {
1616  if (TCP_SEQ_GT(prev->tcphdr->seqno + prev->len, seqno)) {
1617  /* We need to trim the prev segment. */
1618  prev->len = (u16_t)(seqno - prev->tcphdr->seqno);
1619  pbuf_realloc(prev->p, prev->len);
1620  }
1621  prev->next = cseg;
1622  tcp_oos_insert_segment(cseg, next);
1623  }
1624  break;
1625  }
1626  }
1627  /* If the "next" segment is the last segment on the
1628  ooseq queue, we add the incoming segment to the end
1629  of the list. */
1630  if (next->next == NULL &&
1631  TCP_SEQ_GT(seqno, next->tcphdr->seqno)) {
1632  if (TCPH_FLAGS(next->tcphdr) & TCP_FIN) {
1633  /* segment "next" already contains all data */
1634  break;
1635  }
1636  next->next = tcp_seg_copy(&inseg);
1637  if (next->next != NULL) {
1638  if (TCP_SEQ_GT(next->tcphdr->seqno + next->len, seqno)) {
1639  /* We need to trim the last segment. */
1640  next->len = (u16_t)(seqno - next->tcphdr->seqno);
1641  pbuf_realloc(next->p, next->len);
1642  }
1643  /* check if the remote side overruns our receive window */
1644  if (TCP_SEQ_GT((u32_t)tcplen + seqno, pcb->rcv_nxt + (u32_t)pcb->rcv_wnd)) {
1646  ("tcp_receive: other end overran receive window"
1647  "seqno %"U32_F" len %"U16_F" right edge %"U32_F"\n",
1648  seqno, tcplen, pcb->rcv_nxt + pcb->rcv_wnd));
1649  if (TCPH_FLAGS(next->next->tcphdr) & TCP_FIN) {
1650  /* Must remove the FIN from the header as we're trimming
1651  * that byte of sequence-space from the packet */
1652  TCPH_FLAGS_SET(next->next->tcphdr, TCPH_FLAGS(next->next->tcphdr) & ~TCP_FIN);
1653  }
1654  /* Adjust length of segment to fit in the window. */
1655  next->next->len = (u16_t)(pcb->rcv_nxt + pcb->rcv_wnd - seqno);
1656  pbuf_realloc(next->next->p, next->next->len);
1657  tcplen = TCP_TCPLEN(next->next);
1658  LWIP_ASSERT("tcp_receive: segment not trimmed correctly to rcv_wnd\n",
1659  (seqno + tcplen) == (pcb->rcv_nxt + pcb->rcv_wnd));
1660  }
1661  }
1662  break;
1663  }
1664  }
1665  prev = next;
1666  }
1667  }
1668 #if TCP_OOSEQ_MAX_BYTES || TCP_OOSEQ_MAX_PBUFS
1669  /* Check that the data on ooseq doesn't exceed one of the limits
1670  and throw away everything above that limit. */
1671  ooseq_blen = 0;
1672  ooseq_qlen = 0;
1673  prev = NULL;
1674  for (next = pcb->ooseq; next != NULL; prev = next, next = next->next) {
1675  struct pbuf *p = next->p;
1676  ooseq_blen += p->tot_len;
1677  ooseq_qlen += pbuf_clen(p);
1678  if ((ooseq_blen > TCP_OOSEQ_MAX_BYTES) ||
1679  (ooseq_qlen > TCP_OOSEQ_MAX_PBUFS)) {
1680  /* too much ooseq data, dump this and everything after it */
1681  tcp_segs_free(next);
1682  if (prev == NULL) {
1683  /* first ooseq segment is too much, dump the whole queue */
1684  pcb->ooseq = NULL;
1685  } else {
1686  /* just dump 'next' and everything after it */
1687  prev->next = NULL;
1688  }
1689  break;
1690  }
1691  }
1692 #endif /* TCP_OOSEQ_MAX_BYTES || TCP_OOSEQ_MAX_PBUFS */
1693 #endif /* TCP_QUEUE_OOSEQ */
1694  }
1695  } else {
1696  /* The incoming segment is not within the window. */
1697  tcp_send_empty_ack(pcb);
1698  }
1699  } else {
1700  /* Segments with length 0 is taken care of here. Segments that
1701  fall out of the window are ACKed. */
1702  if (!TCP_SEQ_BETWEEN(seqno, pcb->rcv_nxt, pcb->rcv_nxt + pcb->rcv_wnd - 1)) {
1703  tcp_ack_now(pcb);
1704  }
1705  }
1706 }
1707 
1708 static u8_t
1709 tcp_getoptbyte(void)
1710 {
1711  if ((tcphdr_opt2 == NULL) || (tcp_optidx < tcphdr_opt1len)) {
1712  u8_t* opts = (u8_t *)tcphdr + TCP_HLEN;
1713  return opts[tcp_optidx++];
1714  } else {
1715  u8_t idx = (u8_t)(tcp_optidx++ - tcphdr_opt1len);
1716  return tcphdr_opt2[idx];
1717  }
1718 }
1719 
1728 static void
1729 tcp_parseopt(struct tcp_pcb *pcb)
1730 {
1731  u8_t data;
1732  u16_t mss;
1733 #if LWIP_TCP_TIMESTAMPS
1734  u32_t tsval;
1735 #endif
1736 
1737  /* Parse the TCP MSS option, if present. */
1738  if (tcphdr_optlen != 0) {
1739  for (tcp_optidx = 0; tcp_optidx < tcphdr_optlen; ) {
1740  u8_t opt = tcp_getoptbyte();
1741  switch (opt) {
1742  case LWIP_TCP_OPT_EOL:
1743  /* End of options. */
1744  LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_parseopt: EOL\n"));
1745  return;
1746  case LWIP_TCP_OPT_NOP:
1747  /* NOP option. */
1748  LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_parseopt: NOP\n"));
1749  break;
1750  case LWIP_TCP_OPT_MSS:
1751  LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_parseopt: MSS\n"));
1752  if (tcp_getoptbyte() != LWIP_TCP_OPT_LEN_MSS || (tcp_optidx - 2 + LWIP_TCP_OPT_LEN_MSS) > tcphdr_optlen) {
1753  /* Bad length */
1754  LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_parseopt: bad length\n"));
1755  return;
1756  }
1757  /* An MSS option with the right option length. */
1758  mss = (tcp_getoptbyte() << 8);
1759  mss |= tcp_getoptbyte();
1760  /* Limit the mss to the configured TCP_MSS and prevent division by zero */
1761  pcb->mss = ((mss > TCP_MSS) || (mss == 0)) ? TCP_MSS : mss;
1762  break;
1763 #if LWIP_WND_SCALE
1764  case LWIP_TCP_OPT_WS:
1765  LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_parseopt: WND_SCALE\n"));
1766  if (tcp_getoptbyte() != LWIP_TCP_OPT_LEN_WS || (tcp_optidx - 2 + LWIP_TCP_OPT_LEN_WS) > tcphdr_optlen) {
1767  /* Bad length */
1768  LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_parseopt: bad length\n"));
1769  return;
1770  }
1771  /* An WND_SCALE option with the right option length. */
1772  data = tcp_getoptbyte();
1773  /* If syn was received with wnd scale option,
1774  activate wnd scale opt, but only if this is not a retransmission */
1775  if ((flags & TCP_SYN) && !(pcb->flags & TF_WND_SCALE)) {
1776  pcb->snd_scale = data;
1777  if (pcb->snd_scale > 14U) {
1778  pcb->snd_scale = 14U;
1779  }
1780  pcb->rcv_scale = TCP_RCV_SCALE;
1781  pcb->flags |= TF_WND_SCALE;
1782  /* window scaling is enabled, we can use the full receive window */
1783  LWIP_ASSERT("window not at default value", pcb->rcv_wnd == TCPWND_MIN16(TCP_WND));
1784  LWIP_ASSERT("window not at default value", pcb->rcv_ann_wnd == TCPWND_MIN16(TCP_WND));
1785  pcb->rcv_wnd = pcb->rcv_ann_wnd = TCP_WND;
1786  }
1787  break;
1788 #endif
1789 #if LWIP_TCP_TIMESTAMPS
1790  case LWIP_TCP_OPT_TS:
1791  LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_parseopt: TS\n"));
1792  if (tcp_getoptbyte() != LWIP_TCP_OPT_LEN_TS || (tcp_optidx - 2 + LWIP_TCP_OPT_LEN_TS) > tcphdr_optlen) {
1793  /* Bad length */
1794  LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_parseopt: bad length\n"));
1795  return;
1796  }
1797  /* TCP timestamp option with valid length */
1798  tsval = tcp_getoptbyte();
1799  tsval |= (tcp_getoptbyte() << 8);
1800  tsval |= (tcp_getoptbyte() << 16);
1801  tsval |= (tcp_getoptbyte() << 24);
1802  if (flags & TCP_SYN) {
1803  pcb->ts_recent = lwip_ntohl(tsval);
1804  /* Enable sending timestamps in every segment now that we know
1805  the remote host supports it. */
1806  pcb->flags |= TF_TIMESTAMP;
1807  } else if (TCP_SEQ_BETWEEN(pcb->ts_lastacksent, seqno, seqno+tcplen)) {
1808  pcb->ts_recent = lwip_ntohl(tsval);
1809  }
1810  /* Advance to next option (6 bytes already read) */
1811  tcp_optidx += LWIP_TCP_OPT_LEN_TS - 6;
1812  break;
1813 #endif
1814  default:
1815  LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_parseopt: other\n"));
1816  data = tcp_getoptbyte();
1817  if (data < 2) {
1818  LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_parseopt: bad length\n"));
1819  /* If the length field is zero, the options are malformed
1820  and we don't process them further. */
1821  return;
1822  }
1823  /* All other options have a length field, so that we easily
1824  can skip past them. */
1825  tcp_optidx += data - 2;
1826  }
1827  }
1828  }
1829 }
1830 
1831 void
1832 tcp_trigger_input_pcb_close(void)
1833 {
1834  recv_flags |= TF_CLOSED;
1835 }
1836 
1837 #endif /* LWIP_TCP */
TCP_SYN
#define TCP_SYN
Definition: tcp.h:73
TCP_ACK
#define TCP_ACK
Definition: tcp.h:76
lwip_ntohs
#define lwip_ntohs(x)
Definition: def.h:76
opt.h
ip_addr_cmp
#define ip_addr_cmp(addr1, addr2)
Definition: ip_addr.h:316
pbuf::len
u16_t len
Definition: pbuf.h:159
s16_t
int16_t s16_t
Definition: arch.h:125
def.h
PERF_STOP
#define PERF_STOP(x)
Definition: perf.h:21
nd6.h
LWIP_ASSERT
#define LWIP_ASSERT(message, assertion)
Definition: debug.h:116
TCPH_FLAGS_SET
#define TCPH_FLAGS_SET(phdr, flags)
Definition: tcp.h:87
ERR_ABRT
Definition: err.h:90
ip_current_dest_addr
#define ip_current_dest_addr()
Definition: ip.h:212
u16_t
uint16_t u16_t
Definition: arch.h:124
PERF_START
#define PERF_START
Definition: perf.h:20
TCP_CWND_DEBUG
#define TCP_CWND_DEBUG
Definition: lwipopts.h:471
ip_addr_ismulticast
#define ip_addr_ismulticast(ipaddr)
Definition: ip_addr.h:322
IP_IS_ANY_TYPE_VAL
#define IP_IS_ANY_TYPE_VAL(ipaddr)
Definition: ip_addr.h:297
ip_addr_isany
#define ip_addr_isany(ipaddr)
Definition: ip_addr.h:317
pbuf::tot_len
u16_t tot_len
Definition: pbuf.h:156
SOF_INHERITED
#define SOF_INHERITED
Definition: ip.h:102
u32_t
uint32_t u32_t
Definition: arch.h:126
pbuf_clen
u16_t pbuf_clen(const struct pbuf *p)
Definition: pbuf.c:801
pbuf::next
struct pbuf * next
Definition: pbuf.h:144
pbuf_free
u8_t pbuf_free(struct pbuf *p)
Definition: pbuf.c:715
ip_addr_isbroadcast
#define ip_addr_isbroadcast(addr, netif)
Definition: ip_addr.h:321
TCP_QLEN_DEBUG
#define TCP_QLEN_DEBUG
Definition: lwipopts.h:479
IP_ADDR_PCB_VERSION_MATCH_EXACT
#define IP_ADDR_PCB_VERSION_MATCH_EXACT(pcb, ipaddr)
Definition: ip_addr.h:240
ip_chksum_pseudo
u16_t ip_chksum_pseudo(struct pbuf *p, u8_t proto, u16_t proto_len, const ip_addr_t *src, const ip_addr_t *dest)
Definition: inet_chksum.c:379
PBUF_FLAG_TCP_FIN
#define PBUF_FLAG_TCP_FIN
Definition: pbuf.h:139
LWIP_MIN
#define LWIP_MIN(x, y)
Definition: def.h:55
X16_F
#define X16_F
Definition: arch.h:154
memp_free
void memp_free(memp_t type, void *mem)
Definition: memp.c:469
IF__NETIF_CHECKSUM_ENABLED
#define IF__NETIF_CHECKSUM_ENABLED(netif, chksumflag)
Definition: netif.h:357
TCP_OOSEQ_MAX_PBUFS
#define TCP_OOSEQ_MAX_PBUFS
Definition: lwipopts.h:194
pbuf::flags
u8_t flags
Definition: pbuf.h:165
TCP_RST_DEBUG
#define TCP_RST_DEBUG
Definition: lwipopts.h:477
stats.h
ERR_MEM
Definition: err.h:65
TCP_WND_DEBUG
#define TCP_WND_DEBUG
Definition: lwipopts.h:473
netif.h
ERR_CLSD
Definition: err.h:94
IP_PROTO_TCP
#define IP_PROTO_TCP
Definition: ip.h:46
ip_current_netif
#define ip_current_netif()
Definition: ip.h:133
lwip_ntohl
#define lwip_ntohl(x)
Definition: def.h:78
TCP_MSS
#define TCP_MSS
Definition: lwipopts.h:180
s32_t
int32_t s32_t
Definition: arch.h:127
TCPH_SET_FLAG
#define TCPH_SET_FLAG(phdr, flags)
Definition: tcp.h:90
u8_t
uint8_t u8_t
Definition: arch.h:122
TCP_RST
#define TCP_RST
Definition: tcp.h:74
TCP_DEBUG
#define TCP_DEBUG
Definition: lwipopts.h:463
tcp_priv.h
PBUF_FLAG_PUSH
#define PBUF_FLAG_PUSH
Definition: pbuf.h:128
ip_current_src_addr
#define ip_current_src_addr()
Definition: ip.h:210
netif
Definition: netif.h:233
pbuf_realloc
void pbuf_realloc(struct pbuf *p, u16_t size)
Definition: pbuf.c:493
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
pbuf_cat
void pbuf_cat(struct pbuf *head, struct pbuf *tail)
Definition: pbuf.c:841
TCP_STATS_INC
#define TCP_STATS_INC(x)
Definition: stats.h:328
U16_F
#define U16_F
Definition: arch.h:148
TCP_WND
#define TCP_WND
Definition: lwipopts.h:172
ERR_OK
Definition: err.h:63
err_t
s8_t err_t
Definition: err.h:57
ip_addr_copy
#define ip_addr_copy(dest, src)
Definition: ip_addr.h:305
ERR_RST
Definition: err.h:92
ip6_addr.h
TCP_INPUT_DEBUG
#define TCP_INPUT_DEBUG
Definition: lwipopts.h:465
TCP_RCV_SCALE
#define TCP_RCV_SCALE
Definition: opt.h:1289
TCP_PSH
#define TCP_PSH
Definition: tcp.h:75
TCP_HLEN
#define TCP_HLEN
Definition: tcp.h:47
TCPH_HDRLEN
#define TCPH_HDRLEN(phdr)
Definition: tcp.h:83
tcp_hdr
Definition: tcp.h:56
U32_F
#define U32_F
Definition: arch.h:157
memp.h
TCP_OOSEQ_MAX_BYTES
#define TCP_OOSEQ_MAX_BYTES
Definition: lwipopts.h:192
TCP_RTO_DEBUG
#define TCP_RTO_DEBUG
Definition: lwipopts.h:469
mem.h
ERR_VAL
Definition: err.h:75
pbuf
Definition: pbuf.h:142
LWIP_DEBUGF
#define LWIP_DEBUGF(debug, message)
Definition: debug.h:164
ip6.h
TCPH_FLAGS
#define TCPH_FLAGS(phdr)
Definition: tcp.h:84
ip_addr.h
TCP_SYNMAXRTX
#define TCP_SYNMAXRTX
Definition: lwipopts.h:176
inet_chksum.h
pbuf_header
u8_t pbuf_header(struct pbuf *p, s16_t header_size)
Definition: pbuf.c:665
pbuf::payload
void * payload
Definition: pbuf.h:147
NULL
#define NULL
Definition: fat_string.h:17