The Pedigree Project  0.1
tcp_in.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008-2014, Pedigree Developers
3  *
4  * Please see the CONTRIB file in the root of the source tree for a full
5  * list of contributors.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
31 /*
32  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
33  * All rights reserved.
34  *
35  * Redistribution and use in source and binary forms, with or without modification,
36  * are permitted provided that the following conditions are met:
37  *
38  * 1. Redistributions of source code must retain the above copyright notice,
39  * this list of conditions and the following disclaimer.
40  * 2. Redistributions in binary form must reproduce the above copyright notice,
41  * this list of conditions and the following disclaimer in the documentation
42  * and/or other materials provided with the distribution.
43  * 3. The name of the author may not be used to endorse or promote products
44  * derived from this software without specific prior written permission.
45  *
46  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
47  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
48  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
49  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
50  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
51  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
52  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
53  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
54  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
55  * OF SUCH DAMAGE.
56  *
57  * This file is part of the lwIP TCP/IP stack.
58  *
59  * Author: Adam Dunkels <adam@sics.se>
60  *
61  */
62 
63 #include "lwip/opt.h"
64 
65 #if LWIP_TCP /* don't build if not configured for use in lwipopts.h */
66 
67 #include "lwip/priv/tcp_priv.h"
68 #include "lwip/def.h"
69 #include "lwip/ip_addr.h"
70 #include "lwip/netif.h"
71 #include "lwip/mem.h"
72 #include "lwip/memp.h"
73 #include "lwip/inet_chksum.h"
74 #include "lwip/stats.h"
75 #include "lwip/ip6.h"
76 #include "lwip/ip6_addr.h"
77 #if LWIP_ND6_TCP_REACHABILITY_HINTS
78 #include "lwip/nd6.h"
79 #endif /* LWIP_ND6_TCP_REACHABILITY_HINTS */
80 
82 #define LWIP_TCP_CALC_INITIAL_CWND(mss) LWIP_MIN((4U * (mss)), LWIP_MAX((2U * (mss)), 4380U));
83 
84 /* These variables are global to all functions involved in the input
85  processing of TCP segments. They are set by the tcp_input()
86  function. */
87 static struct tcp_seg inseg;
88 static struct tcp_hdr *tcphdr;
89 static u16_t tcphdr_optlen;
90 static u16_t tcphdr_opt1len;
91 static u8_t* tcphdr_opt2;
92 static u16_t tcp_optidx;
93 static u32_t seqno, ackno;
94 static tcpwnd_size_t recv_acked;
95 static u16_t tcplen;
96 static u8_t flags;
97 
98 static u8_t recv_flags;
99 static struct pbuf *recv_data;
100 
101 struct tcp_pcb *tcp_input_pcb;
102 
103 /* Forward declarations. */
104 static err_t tcp_process(struct tcp_pcb *pcb);
105 static void tcp_receive(struct tcp_pcb *pcb);
106 static void tcp_parseopt(struct tcp_pcb *pcb);
107 
108 static void tcp_listen_input(struct tcp_pcb_listen *pcb);
109 static void tcp_timewait_input(struct tcp_pcb *pcb);
110 
120 void
121 tcp_input(struct pbuf *p, struct netif *inp)
122 {
123  struct tcp_pcb *pcb, *prev;
124  struct tcp_pcb_listen *lpcb;
125 #if SO_REUSE
126  struct tcp_pcb *lpcb_prev = NULL;
127  struct tcp_pcb_listen *lpcb_any = NULL;
128 #endif /* SO_REUSE */
129  u8_t hdrlen_bytes;
130  err_t err;
131 
132  LWIP_UNUSED_ARG(inp);
133 
134  PERF_START;
135 
136  TCP_STATS_INC(tcp.recv);
137  MIB2_STATS_INC(mib2.tcpinsegs);
138 
139  tcphdr = (struct tcp_hdr *)p->payload;
140 
141 #if TCP_INPUT_DEBUG
142  tcp_debug_print(tcphdr);
143 #endif
144 
145  /* Check that TCP header fits in payload */
146  if (p->len < TCP_HLEN) {
147  /* drop short packets */
148  LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_input: short packet (%"U16_F" bytes) discarded\n", p->tot_len));
149  TCP_STATS_INC(tcp.lenerr);
150  goto dropped;
151  }
152 
153  /* Don't even process incoming broadcasts/multicasts. */
154  if (ip_addr_isbroadcast(ip_current_dest_addr(), ip_current_netif()) ||
155  ip_addr_ismulticast(ip_current_dest_addr())) {
156  TCP_STATS_INC(tcp.proterr);
157  goto dropped;
158  }
159 
160 #if CHECKSUM_CHECK_TCP
161  IF__NETIF_CHECKSUM_ENABLED(inp, NETIF_CHECKSUM_CHECK_TCP) {
162  /* Verify TCP checksum. */
163  u16_t chksum = ip_chksum_pseudo(p, IP_PROTO_TCP, p->tot_len,
165  if (chksum != 0) {
166  LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_input: packet discarded due to failing checksum 0x%04"X16_F"\n",
167  chksum));
168  tcp_debug_print(tcphdr);
169  TCP_STATS_INC(tcp.chkerr);
170  goto dropped;
171  }
172  }
173 #endif /* CHECKSUM_CHECK_TCP */
174 
175  /* sanity-check header length */
176  hdrlen_bytes = TCPH_HDRLEN(tcphdr) * 4;
177  if ((hdrlen_bytes < TCP_HLEN) || (hdrlen_bytes > p->tot_len)) {
178  LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_input: invalid header length (%"U16_F")\n", (u16_t)hdrlen_bytes));
179  TCP_STATS_INC(tcp.lenerr);
180  goto dropped;
181  }
182 
183  /* Move the payload pointer in the pbuf so that it points to the
184  TCP data instead of the TCP header. */
185  tcphdr_optlen = hdrlen_bytes - TCP_HLEN;
186  tcphdr_opt2 = NULL;
187  if (p->len >= hdrlen_bytes) {
188  /* all options are in the first pbuf */
189  tcphdr_opt1len = tcphdr_optlen;
190  pbuf_header(p, -(s16_t)hdrlen_bytes); /* cannot fail */
191  } else {
192  u16_t opt2len;
193  /* TCP header fits into first pbuf, options don't - data is in the next pbuf */
194  /* there must be a next pbuf, due to hdrlen_bytes sanity check above */
195  LWIP_ASSERT("p->next != NULL", p->next != NULL);
196 
197  /* advance over the TCP header (cannot fail) */
198  pbuf_header(p, -TCP_HLEN);
199 
200  /* determine how long the first and second parts of the options are */
201  tcphdr_opt1len = p->len;
202  opt2len = tcphdr_optlen - tcphdr_opt1len;
203 
204  /* options continue in the next pbuf: set p to zero length and hide the
205  options in the next pbuf (adjusting p->tot_len) */
206  pbuf_header(p, -(s16_t)tcphdr_opt1len);
207 
208  /* check that the options fit in the second pbuf */
209  if (opt2len > p->next->len) {
210  /* drop short packets */
211  LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_input: options overflow second pbuf (%"U16_F" bytes)\n", p->next->len));
212  TCP_STATS_INC(tcp.lenerr);
213  goto dropped;
214  }
215 
216  /* remember the pointer to the second part of the options */
217  tcphdr_opt2 = (u8_t*)p->next->payload;
218 
219  /* advance p->next to point after the options, and manually
220  adjust p->tot_len to keep it consistent with the changed p->next */
221  pbuf_header(p->next, -(s16_t)opt2len);
222  p->tot_len -= opt2len;
223 
224  LWIP_ASSERT("p->len == 0", p->len == 0);
225  LWIP_ASSERT("p->tot_len == p->next->tot_len", p->tot_len == p->next->tot_len);
226  }
227 
228  /* Convert fields in TCP header to host byte order. */
229  tcphdr->src = lwip_ntohs(tcphdr->src);
230  tcphdr->dest = lwip_ntohs(tcphdr->dest);
231  seqno = tcphdr->seqno = lwip_ntohl(tcphdr->seqno);
232  ackno = tcphdr->ackno = lwip_ntohl(tcphdr->ackno);
233  tcphdr->wnd = lwip_ntohs(tcphdr->wnd);
234 
235  flags = TCPH_FLAGS(tcphdr);
236  tcplen = p->tot_len + ((flags & (TCP_FIN | TCP_SYN)) ? 1 : 0);
237 
238  /* Demultiplex an incoming segment. First, we check if it is destined
239  for an active connection. */
240  prev = NULL;
241 
242  for (pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) {
243  LWIP_ASSERT("tcp_input: active pcb->state != CLOSED", pcb->state != CLOSED);
244  LWIP_ASSERT("tcp_input: active pcb->state != TIME-WAIT", pcb->state != TIME_WAIT);
245  LWIP_ASSERT("tcp_input: active pcb->state != LISTEN", pcb->state != LISTEN);
246  if (pcb->remote_port == tcphdr->src &&
247  pcb->local_port == tcphdr->dest &&
248  ip_addr_cmp(&pcb->remote_ip, ip_current_src_addr()) &&
249  ip_addr_cmp(&pcb->local_ip, ip_current_dest_addr())) {
250  /* Move this PCB to the front of the list so that subsequent
251  lookups will be faster (we exploit locality in TCP segment
252  arrivals). */
253  LWIP_ASSERT("tcp_input: pcb->next != pcb (before cache)", pcb->next != pcb);
254  if (prev != NULL) {
255  prev->next = pcb->next;
256  pcb->next = tcp_active_pcbs;
257  tcp_active_pcbs = pcb;
258  } else {
259  TCP_STATS_INC(tcp.cachehit);
260  }
261  LWIP_ASSERT("tcp_input: pcb->next != pcb (after cache)", pcb->next != pcb);
262  break;
263  }
264  prev = pcb;
265  }
266 
267  if (pcb == NULL) {
268  /* If it did not go to an active connection, we check the connections
269  in the TIME-WAIT state. */
270  for (pcb = tcp_tw_pcbs; pcb != NULL; pcb = pcb->next) {
271  LWIP_ASSERT("tcp_input: TIME-WAIT pcb->state == TIME-WAIT", pcb->state == TIME_WAIT);
272  if (pcb->remote_port == tcphdr->src &&
273  pcb->local_port == tcphdr->dest &&
274  ip_addr_cmp(&pcb->remote_ip, ip_current_src_addr()) &&
275  ip_addr_cmp(&pcb->local_ip, ip_current_dest_addr())) {
276  /* We don't really care enough to move this PCB to the front
277  of the list since we are not very likely to receive that
278  many segments for connections in TIME-WAIT. */
279  LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_input: packed for TIME_WAITing connection.\n"));
280  tcp_timewait_input(pcb);
281  pbuf_free(p);
282  return;
283  }
284  }
285 
286  /* Finally, if we still did not get a match, we check all PCBs that
287  are LISTENing for incoming connections. */
288  prev = NULL;
289  for (lpcb = tcp_listen_pcbs.listen_pcbs; lpcb != NULL; lpcb = lpcb->next) {
290  if (lpcb->local_port == tcphdr->dest) {
291  if (IP_IS_ANY_TYPE_VAL(lpcb->local_ip)) {
292  /* found an ANY TYPE (IPv4/IPv6) match */
293 #if SO_REUSE
294  lpcb_any = lpcb;
295  lpcb_prev = prev;
296 #else /* SO_REUSE */
297  break;
298 #endif /* SO_REUSE */
299  } else if (IP_ADDR_PCB_VERSION_MATCH_EXACT(lpcb, ip_current_dest_addr())) {
300  if (ip_addr_cmp(&lpcb->local_ip, ip_current_dest_addr())) {
301  /* found an exact match */
302  break;
303  } else if (ip_addr_isany(&lpcb->local_ip)) {
304  /* found an ANY-match */
305 #if SO_REUSE
306  lpcb_any = lpcb;
307  lpcb_prev = prev;
308 #else /* SO_REUSE */
309  break;
310  #endif /* SO_REUSE */
311  }
312  }
313  }
314  prev = (struct tcp_pcb *)lpcb;
315  }
316 #if SO_REUSE
317  /* first try specific local IP */
318  if (lpcb == NULL) {
319  /* only pass to ANY if no specific local IP has been found */
320  lpcb = lpcb_any;
321  prev = lpcb_prev;
322  }
323 #endif /* SO_REUSE */
324  if (lpcb != NULL) {
325  /* Move this PCB to the front of the list so that subsequent
326  lookups will be faster (we exploit locality in TCP segment
327  arrivals). */
328  if (prev != NULL) {
329  ((struct tcp_pcb_listen *)prev)->next = lpcb->next;
330  /* our successor is the remainder of the listening list */
331  lpcb->next = tcp_listen_pcbs.listen_pcbs;
332  /* put this listening pcb at the head of the listening list */
333  tcp_listen_pcbs.listen_pcbs = lpcb;
334  } else {
335  TCP_STATS_INC(tcp.cachehit);
336  }
337 
338  LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_input: packed for LISTENing connection.\n"));
339  tcp_listen_input(lpcb);
340  pbuf_free(p);
341  return;
342  }
343  }
344 
345 #if TCP_INPUT_DEBUG
346  LWIP_DEBUGF(TCP_INPUT_DEBUG, ("+-+-+-+-+-+-+-+-+-+-+-+-+-+- tcp_input: flags "));
347  tcp_debug_print_flags(TCPH_FLAGS(tcphdr));
348  LWIP_DEBUGF(TCP_INPUT_DEBUG, ("-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n"));
349 #endif /* TCP_INPUT_DEBUG */
350 
351 
352  if (pcb != NULL) {
353  /* The incoming segment belongs to a connection. */
354 #if TCP_INPUT_DEBUG
355  tcp_debug_print_state(pcb->state);
356 #endif /* TCP_INPUT_DEBUG */
357 
358  /* Set up a tcp_seg structure. */
359  inseg.next = NULL;
360  inseg.len = p->tot_len;
361  inseg.p = p;
362  inseg.tcphdr = tcphdr;
363 
364  recv_data = NULL;
365  recv_flags = 0;
366  recv_acked = 0;
367 
368  if (flags & TCP_PSH) {
369  p->flags |= PBUF_FLAG_PUSH;
370  }
371 
372  /* If there is data which was previously "refused" by upper layer */
373  if (pcb->refused_data != NULL) {
374  if ((tcp_process_refused_data(pcb) == ERR_ABRT) ||
375  ((pcb->refused_data != NULL) && (tcplen > 0))) {
376  /* pcb has been aborted or refused data is still refused and the new
377  segment contains data */
378  if (pcb->rcv_ann_wnd == 0) {
379  /* this is a zero-window probe, we respond to it with current RCV.NXT
380  and drop the data segment */
381  tcp_send_empty_ack(pcb);
382  }
383  TCP_STATS_INC(tcp.drop);
384  MIB2_STATS_INC(mib2.tcpinerrs);
385  goto aborted;
386  }
387  }
388  tcp_input_pcb = pcb;
389  err = tcp_process(pcb);
390  /* A return value of ERR_ABRT means that tcp_abort() was called
391  and that the pcb has been freed. If so, we don't do anything. */
392  if (err != ERR_ABRT) {
393  if (recv_flags & TF_RESET) {
394  /* TF_RESET means that the connection was reset by the other
395  end. We then call the error callback to inform the
396  application that the connection is dead before we
397  deallocate the PCB. */
398  TCP_EVENT_ERR(pcb->state, pcb->errf, pcb->callback_arg, ERR_RST);
399  tcp_pcb_remove(&tcp_active_pcbs, pcb);
400  memp_free(MEMP_TCP_PCB, pcb);
401  } else {
402  err = ERR_OK;
403  /* If the application has registered a "sent" function to be
404  called when new send buffer space is available, we call it
405  now. */
406  if (recv_acked > 0) {
407  u16_t acked16;
408 #if LWIP_WND_SCALE
409  /* recv_acked is u32_t but the sent callback only takes a u16_t,
410  so we might have to call it multiple times. */
411  u32_t acked = recv_acked;
412  while (acked > 0) {
413  acked16 = (u16_t)LWIP_MIN(acked, 0xffffu);
414  acked -= acked16;
415 #else
416  {
417  acked16 = recv_acked;
418 #endif
419  TCP_EVENT_SENT(pcb, (u16_t)acked16, err);
420  if (err == ERR_ABRT) {
421  goto aborted;
422  }
423  }
424  recv_acked = 0;
425  }
426  if (recv_flags & TF_CLOSED) {
427  /* The connection has been closed and we will deallocate the
428  PCB. */
429  if (!(pcb->flags & TF_RXCLOSED)) {
430  /* Connection closed although the application has only shut down the
431  tx side: call the PCB's err callback and indicate the closure to
432  ensure the application doesn't continue using the PCB. */
433  TCP_EVENT_ERR(pcb->state, pcb->errf, pcb->callback_arg, ERR_CLSD);
434  }
435  tcp_pcb_remove(&tcp_active_pcbs, pcb);
436  memp_free(MEMP_TCP_PCB, pcb);
437  goto aborted;
438  }
439 #if TCP_QUEUE_OOSEQ && LWIP_WND_SCALE
440  while (recv_data != NULL) {
441  struct pbuf *rest = NULL;
442  pbuf_split_64k(recv_data, &rest);
443 #else /* TCP_QUEUE_OOSEQ && LWIP_WND_SCALE */
444  if (recv_data != NULL) {
445 #endif /* TCP_QUEUE_OOSEQ && LWIP_WND_SCALE */
446 
447  LWIP_ASSERT("pcb->refused_data == NULL", pcb->refused_data == NULL);
448  if (pcb->flags & TF_RXCLOSED) {
449  /* received data although already closed -> abort (send RST) to
450  notify the remote host that not all data has been processed */
451  pbuf_free(recv_data);
452 #if TCP_QUEUE_OOSEQ && LWIP_WND_SCALE
453  if (rest != NULL) {
454  pbuf_free(rest);
455  }
456 #endif /* TCP_QUEUE_OOSEQ && LWIP_WND_SCALE */
457  tcp_abort(pcb);
458  goto aborted;
459  }
460 
461  /* Notify application that data has been received. */
462  TCP_EVENT_RECV(pcb, recv_data, ERR_OK, err);
463  if (err == ERR_ABRT) {
464 #if TCP_QUEUE_OOSEQ && LWIP_WND_SCALE
465  if (rest != NULL) {
466  pbuf_free(rest);
467  }
468 #endif /* TCP_QUEUE_OOSEQ && LWIP_WND_SCALE */
469  goto aborted;
470  }
471 
472  /* If the upper layer can't receive this data, store it */
473  if (err != ERR_OK) {
474 #if TCP_QUEUE_OOSEQ && LWIP_WND_SCALE
475  if (rest != NULL) {
476  pbuf_cat(recv_data, rest);
477  }
478 #endif /* TCP_QUEUE_OOSEQ && LWIP_WND_SCALE */
479  pcb->refused_data = recv_data;
480  LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_input: keep incoming packet, because pcb is \"full\"\n"));
481 #if TCP_QUEUE_OOSEQ && LWIP_WND_SCALE
482  break;
483  } else {
484  /* Upper layer received the data, go on with the rest if > 64K */
485  recv_data = rest;
486 #endif /* TCP_QUEUE_OOSEQ && LWIP_WND_SCALE */
487  }
488  }
489 
490  /* If a FIN segment was received, we call the callback
491  function with a NULL buffer to indicate EOF. */
492  if (recv_flags & TF_GOT_FIN) {
493  if (pcb->refused_data != NULL) {
494  /* Delay this if we have refused data. */
495  pcb->refused_data->flags |= PBUF_FLAG_TCP_FIN;
496  } else {
497  /* correct rcv_wnd as the application won't call tcp_recved()
498  for the FIN's seqno */
499  if (pcb->rcv_wnd != TCP_WND_MAX(pcb)) {
500  pcb->rcv_wnd++;
501  }
502  TCP_EVENT_CLOSED(pcb, err);
503  if (err == ERR_ABRT) {
504  goto aborted;
505  }
506  }
507  }
508 
509  tcp_input_pcb = NULL;
510  /* Try to send something out. */
511  tcp_output(pcb);
512 #if TCP_INPUT_DEBUG
513 #if TCP_DEBUG
514  tcp_debug_print_state(pcb->state);
515 #endif /* TCP_DEBUG */
516 #endif /* TCP_INPUT_DEBUG */
517  }
518  }
519  /* Jump target if pcb has been aborted in a callback (by calling tcp_abort()).
520  Below this line, 'pcb' may not be dereferenced! */
521 aborted:
522  tcp_input_pcb = NULL;
523  recv_data = NULL;
524 
525  /* give up our reference to inseg.p */
526  if (inseg.p != NULL)
527  {
528  pbuf_free(inseg.p);
529  inseg.p = NULL;
530  }
531  } else {
532 
533  /* If no matching PCB was found, send a TCP RST (reset) to the
534  sender. */
535  LWIP_DEBUGF(TCP_RST_DEBUG, ("tcp_input: no PCB match found, resetting.\n"));
536  if (!(TCPH_FLAGS(tcphdr) & TCP_RST)) {
537  TCP_STATS_INC(tcp.proterr);
538  TCP_STATS_INC(tcp.drop);
539  tcp_rst(ackno, seqno + tcplen, ip_current_dest_addr(),
540  ip_current_src_addr(), tcphdr->dest, tcphdr->src);
541  }
542  pbuf_free(p);
543  }
544 
545  LWIP_ASSERT("tcp_input: tcp_pcbs_sane()", tcp_pcbs_sane());
546  PERF_STOP("tcp_input");
547  return;
548 dropped:
549  TCP_STATS_INC(tcp.drop);
550  MIB2_STATS_INC(mib2.tcpinerrs);
551  pbuf_free(p);
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  /* If syn was received with wnd scale option,
1772  activate wnd scale opt, but only if this is not a retransmission */
1773  if ((flags & TCP_SYN) && !(pcb->flags & TF_WND_SCALE)) {
1774  /* An WND_SCALE option with the right option length. */
1775  data = tcp_getoptbyte();
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 */
u16_t tot_len
Definition: pbuf.h:175
void pbuf_realloc(struct pbuf *p, u16_t new_len)
Definition: pbuf.c:512
Definition: err.h:109
struct pbuf * next
Definition: pbuf.h:163
u16_t pbuf_clen(const struct pbuf *p)
Definition: pbuf.c:819
u16_t len
Definition: pbuf.h:178
#define ip_current_src_addr()
Definition: ip.h:229
u8_t pbuf_header(struct pbuf *p, s16_t header_size_increment)
Definition: pbuf.c:684
#define PBUF_FLAG_PUSH
Definition: pbuf.h:147
#define ip_current_netif()
Definition: ip.h:152
void memp_free(memp_t type, void *mem)
Definition: memp.c:488
#define ip_current_dest_addr()
Definition: ip.h:231
#define TCP_QLEN_DEBUG
Definition: opt.h:2819
Definition: err.h:84
#define TCP_RST_DEBUG
Definition: opt.h:2812
#define TCP_CWND_DEBUG
Definition: opt.h:2791
Definition: err.h:113
#define TCP_DEBUG
Definition: opt.h:2762
u8_t flags
Definition: pbuf.h:184
#define TCP_OOSEQ_MAX_BYTES
Definition: opt.h:1257
#define TCP_SYNMAXRTX
Definition: opt.h:1183
#define TCP_INPUT_DEBUG
Definition: opt.h:2769
Definition: pbuf.h:161
Definition: netif.h:244
void pbuf_cat(struct pbuf *h, struct pbuf *t)
Definition: pbuf.c:859
#define TCP_RTO_DEBUG
Definition: opt.h:2784
s8_t err_t
Definition: err.h:76
#define LWIP_DEBUGF(debug, message)
#define PBUF_FLAG_TCP_FIN
Definition: pbuf.h:158
Definition: err.h:111
Definition: err.h:94
#define LWIP_UNUSED_ARG(x)
Definition: arch.h:327
#define TCP_OOSEQ_MAX_PBUFS
Definition: opt.h:1265
u8_t pbuf_free(struct pbuf *p)
Definition: pbuf.c:734
Definition: err.h:82
void * payload
Definition: pbuf.h:166
#define TCP_WND_DEBUG
Definition: opt.h:2798