My fork of the HTTPServer (working)

Dependents:   DGWWebServer LAN2

Committer:
screamer
Date:
Mon Aug 06 09:23:14 2012 +0000
Revision:
0:7a64fbb4069d
[mbed] converted /DGWWebServer/HTTPServer

Who changed what in which revision?

UserRevisionLine numberNew contents of line
screamer 0:7a64fbb4069d 1 /**
screamer 0:7a64fbb4069d 2 * @file
screamer 0:7a64fbb4069d 3 * Transmission Control Protocol, incoming traffic
screamer 0:7a64fbb4069d 4 *
screamer 0:7a64fbb4069d 5 * The input processing functions of the TCP layer.
screamer 0:7a64fbb4069d 6 *
screamer 0:7a64fbb4069d 7 * These functions are generally called in the order (ip_input() ->)
screamer 0:7a64fbb4069d 8 * tcp_input() -> * tcp_process() -> tcp_receive() (-> application).
screamer 0:7a64fbb4069d 9 *
screamer 0:7a64fbb4069d 10 */
screamer 0:7a64fbb4069d 11
screamer 0:7a64fbb4069d 12 /*
screamer 0:7a64fbb4069d 13 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
screamer 0:7a64fbb4069d 14 * All rights reserved.
screamer 0:7a64fbb4069d 15 *
screamer 0:7a64fbb4069d 16 * Redistribution and use in source and binary forms, with or without modification,
screamer 0:7a64fbb4069d 17 * are permitted provided that the following conditions are met:
screamer 0:7a64fbb4069d 18 *
screamer 0:7a64fbb4069d 19 * 1. Redistributions of source code must retain the above copyright notice,
screamer 0:7a64fbb4069d 20 * this list of conditions and the following disclaimer.
screamer 0:7a64fbb4069d 21 * 2. Redistributions in binary form must reproduce the above copyright notice,
screamer 0:7a64fbb4069d 22 * this list of conditions and the following disclaimer in the documentation
screamer 0:7a64fbb4069d 23 * and/or other materials provided with the distribution.
screamer 0:7a64fbb4069d 24 * 3. The name of the author may not be used to endorse or promote products
screamer 0:7a64fbb4069d 25 * derived from this software without specific prior written permission.
screamer 0:7a64fbb4069d 26 *
screamer 0:7a64fbb4069d 27 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
screamer 0:7a64fbb4069d 28 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
screamer 0:7a64fbb4069d 29 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
screamer 0:7a64fbb4069d 30 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
screamer 0:7a64fbb4069d 31 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
screamer 0:7a64fbb4069d 32 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
screamer 0:7a64fbb4069d 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
screamer 0:7a64fbb4069d 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
screamer 0:7a64fbb4069d 35 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
screamer 0:7a64fbb4069d 36 * OF SUCH DAMAGE.
screamer 0:7a64fbb4069d 37 *
screamer 0:7a64fbb4069d 38 * This file is part of the lwIP TCP/IP stack.
screamer 0:7a64fbb4069d 39 *
screamer 0:7a64fbb4069d 40 * Author: Adam Dunkels <adam@sics.se>
screamer 0:7a64fbb4069d 41 *
screamer 0:7a64fbb4069d 42 */
screamer 0:7a64fbb4069d 43
screamer 0:7a64fbb4069d 44 #include "lwip/opt.h"
screamer 0:7a64fbb4069d 45
screamer 0:7a64fbb4069d 46 #if LWIP_TCP /* don't build if not configured for use in lwipopts.h */
screamer 0:7a64fbb4069d 47
screamer 0:7a64fbb4069d 48 #include "lwip/tcp.h"
screamer 0:7a64fbb4069d 49 #include "lwip/def.h"
screamer 0:7a64fbb4069d 50 #include "lwip/ip_addr.h"
screamer 0:7a64fbb4069d 51 #include "lwip/netif.h"
screamer 0:7a64fbb4069d 52 #include "lwip/mem.h"
screamer 0:7a64fbb4069d 53 #include "lwip/memp.h"
screamer 0:7a64fbb4069d 54 #include "lwip/inet.h"
screamer 0:7a64fbb4069d 55 #include "lwip/inet_chksum.h"
screamer 0:7a64fbb4069d 56 #include "lwip/stats.h"
screamer 0:7a64fbb4069d 57 #include "lwip/snmp.h"
screamer 0:7a64fbb4069d 58 #include "arch/perf.h"
screamer 0:7a64fbb4069d 59
screamer 0:7a64fbb4069d 60 /* These variables are global to all functions involved in the input
screamer 0:7a64fbb4069d 61 processing of TCP segments. They are set by the tcp_input()
screamer 0:7a64fbb4069d 62 function. */
screamer 0:7a64fbb4069d 63 static struct tcp_seg inseg;
screamer 0:7a64fbb4069d 64 static struct tcp_hdr *tcphdr;
screamer 0:7a64fbb4069d 65 static struct ip_hdr *iphdr;
screamer 0:7a64fbb4069d 66 static u32_t seqno, ackno;
screamer 0:7a64fbb4069d 67 static u8_t flags;
screamer 0:7a64fbb4069d 68 static u16_t tcplen;
screamer 0:7a64fbb4069d 69
screamer 0:7a64fbb4069d 70 static u8_t recv_flags;
screamer 0:7a64fbb4069d 71 static struct pbuf *recv_data;
screamer 0:7a64fbb4069d 72
screamer 0:7a64fbb4069d 73 struct tcp_pcb *tcp_input_pcb;
screamer 0:7a64fbb4069d 74
screamer 0:7a64fbb4069d 75 /* Forward declarations. */
screamer 0:7a64fbb4069d 76 static err_t tcp_process(struct tcp_pcb *pcb);
screamer 0:7a64fbb4069d 77 static u8_t tcp_receive(struct tcp_pcb *pcb);
screamer 0:7a64fbb4069d 78 static void tcp_parseopt(struct tcp_pcb *pcb);
screamer 0:7a64fbb4069d 79
screamer 0:7a64fbb4069d 80 static err_t tcp_listen_input(struct tcp_pcb_listen *pcb);
screamer 0:7a64fbb4069d 81 static err_t tcp_timewait_input(struct tcp_pcb *pcb);
screamer 0:7a64fbb4069d 82
screamer 0:7a64fbb4069d 83 /**
screamer 0:7a64fbb4069d 84 * The initial input processing of TCP. It verifies the TCP header, demultiplexes
screamer 0:7a64fbb4069d 85 * the segment between the PCBs and passes it on to tcp_process(), which implements
screamer 0:7a64fbb4069d 86 * the TCP finite state machine. This function is called by the IP layer (in
screamer 0:7a64fbb4069d 87 * ip_input()).
screamer 0:7a64fbb4069d 88 *
screamer 0:7a64fbb4069d 89 * @param p received TCP segment to process (p->payload pointing to the IP header)
screamer 0:7a64fbb4069d 90 * @param inp network interface on which this segment was received
screamer 0:7a64fbb4069d 91 */
screamer 0:7a64fbb4069d 92 void
screamer 0:7a64fbb4069d 93 tcp_input(struct pbuf *p, struct netif *inp)
screamer 0:7a64fbb4069d 94 {
screamer 0:7a64fbb4069d 95 struct tcp_pcb *pcb, *prev;
screamer 0:7a64fbb4069d 96 struct tcp_pcb_listen *lpcb;
screamer 0:7a64fbb4069d 97 u8_t hdrlen;
screamer 0:7a64fbb4069d 98 err_t err;
screamer 0:7a64fbb4069d 99
screamer 0:7a64fbb4069d 100 PERF_START;
screamer 0:7a64fbb4069d 101
screamer 0:7a64fbb4069d 102 TCP_STATS_INC(tcp.recv);
screamer 0:7a64fbb4069d 103 snmp_inc_tcpinsegs();
screamer 0:7a64fbb4069d 104
screamer 0:7a64fbb4069d 105 iphdr = (struct ip_hdr *)(p->payload);
screamer 0:7a64fbb4069d 106 tcphdr = (struct tcp_hdr *)((u8_t *)p->payload + IPH_HL(iphdr) * 4);
screamer 0:7a64fbb4069d 107
screamer 0:7a64fbb4069d 108 #if TCP_INPUT_DEBUG
screamer 0:7a64fbb4069d 109 tcp_debug_print(tcphdr);
screamer 0:7a64fbb4069d 110 #endif
screamer 0:7a64fbb4069d 111
screamer 0:7a64fbb4069d 112 /* remove header from payload */
screamer 0:7a64fbb4069d 113 if (pbuf_header(p, -((s16_t)(IPH_HL(iphdr) * 4))) || (p->tot_len < sizeof(struct tcp_hdr))) {
screamer 0:7a64fbb4069d 114 /* drop short packets */
screamer 0:7a64fbb4069d 115 LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_input: short packet (%"U16_F" bytes) discarded\n", p->tot_len));
screamer 0:7a64fbb4069d 116 TCP_STATS_INC(tcp.lenerr);
screamer 0:7a64fbb4069d 117 TCP_STATS_INC(tcp.drop);
screamer 0:7a64fbb4069d 118 snmp_inc_tcpinerrs();
screamer 0:7a64fbb4069d 119 pbuf_free(p);
screamer 0:7a64fbb4069d 120 return;
screamer 0:7a64fbb4069d 121 }
screamer 0:7a64fbb4069d 122
screamer 0:7a64fbb4069d 123 /* Don't even process incoming broadcasts/multicasts. */
screamer 0:7a64fbb4069d 124 if (ip_addr_isbroadcast(&(iphdr->dest), inp) ||
screamer 0:7a64fbb4069d 125 ip_addr_ismulticast(&(iphdr->dest))) {
screamer 0:7a64fbb4069d 126 TCP_STATS_INC(tcp.proterr);
screamer 0:7a64fbb4069d 127 TCP_STATS_INC(tcp.drop);
screamer 0:7a64fbb4069d 128 snmp_inc_tcpinerrs();
screamer 0:7a64fbb4069d 129 pbuf_free(p);
screamer 0:7a64fbb4069d 130 return;
screamer 0:7a64fbb4069d 131 }
screamer 0:7a64fbb4069d 132
screamer 0:7a64fbb4069d 133 #if CHECKSUM_CHECK_TCP
screamer 0:7a64fbb4069d 134 /* Verify TCP checksum. */
screamer 0:7a64fbb4069d 135 if (inet_chksum_pseudo(p, (struct ip_addr *)&(iphdr->src),
screamer 0:7a64fbb4069d 136 (struct ip_addr *)&(iphdr->dest),
screamer 0:7a64fbb4069d 137 IP_PROTO_TCP, p->tot_len) != 0) {
screamer 0:7a64fbb4069d 138 LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_input: packet discarded due to failing checksum 0x%04"X16_F"\n",
screamer 0:7a64fbb4069d 139 inet_chksum_pseudo(p, (struct ip_addr *)&(iphdr->src), (struct ip_addr *)&(iphdr->dest),
screamer 0:7a64fbb4069d 140 IP_PROTO_TCP, p->tot_len)));
screamer 0:7a64fbb4069d 141 #if TCP_DEBUG
screamer 0:7a64fbb4069d 142 tcp_debug_print(tcphdr);
screamer 0:7a64fbb4069d 143 #endif /* TCP_DEBUG */
screamer 0:7a64fbb4069d 144 TCP_STATS_INC(tcp.chkerr);
screamer 0:7a64fbb4069d 145 TCP_STATS_INC(tcp.drop);
screamer 0:7a64fbb4069d 146 snmp_inc_tcpinerrs();
screamer 0:7a64fbb4069d 147 pbuf_free(p);
screamer 0:7a64fbb4069d 148 return;
screamer 0:7a64fbb4069d 149 }
screamer 0:7a64fbb4069d 150 #endif
screamer 0:7a64fbb4069d 151
screamer 0:7a64fbb4069d 152 /* Move the payload pointer in the pbuf so that it points to the
screamer 0:7a64fbb4069d 153 TCP data instead of the TCP header. */
screamer 0:7a64fbb4069d 154 hdrlen = TCPH_HDRLEN(tcphdr);
screamer 0:7a64fbb4069d 155 if(pbuf_header(p, -(hdrlen * 4))){
screamer 0:7a64fbb4069d 156 /* drop short packets */
screamer 0:7a64fbb4069d 157 LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_input: short packet\n"));
screamer 0:7a64fbb4069d 158 TCP_STATS_INC(tcp.lenerr);
screamer 0:7a64fbb4069d 159 TCP_STATS_INC(tcp.drop);
screamer 0:7a64fbb4069d 160 snmp_inc_tcpinerrs();
screamer 0:7a64fbb4069d 161 pbuf_free(p);
screamer 0:7a64fbb4069d 162 return;
screamer 0:7a64fbb4069d 163 }
screamer 0:7a64fbb4069d 164
screamer 0:7a64fbb4069d 165 /* Convert fields in TCP header to host byte order. */
screamer 0:7a64fbb4069d 166 tcphdr->src = ntohs(tcphdr->src);
screamer 0:7a64fbb4069d 167 tcphdr->dest = ntohs(tcphdr->dest);
screamer 0:7a64fbb4069d 168 seqno = tcphdr->seqno = ntohl(tcphdr->seqno);
screamer 0:7a64fbb4069d 169 ackno = tcphdr->ackno = ntohl(tcphdr->ackno);
screamer 0:7a64fbb4069d 170 tcphdr->wnd = ntohs(tcphdr->wnd);
screamer 0:7a64fbb4069d 171
screamer 0:7a64fbb4069d 172 flags = TCPH_FLAGS(tcphdr) & TCP_FLAGS;
screamer 0:7a64fbb4069d 173 tcplen = p->tot_len + ((flags & TCP_FIN || flags & TCP_SYN)? 1: 0);
screamer 0:7a64fbb4069d 174
screamer 0:7a64fbb4069d 175 /* Demultiplex an incoming segment. First, we check if it is destined
screamer 0:7a64fbb4069d 176 for an active connection. */
screamer 0:7a64fbb4069d 177 prev = (struct tcp_pcb *)(NULL);
screamer 0:7a64fbb4069d 178
screamer 0:7a64fbb4069d 179
screamer 0:7a64fbb4069d 180 for(pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) {
screamer 0:7a64fbb4069d 181 LWIP_ASSERT("tcp_input: active pcb->state != CLOSED", pcb->state != CLOSED);
screamer 0:7a64fbb4069d 182 LWIP_ASSERT("tcp_input: active pcb->state != TIME-WAIT", pcb->state != TIME_WAIT);
screamer 0:7a64fbb4069d 183 LWIP_ASSERT("tcp_input: active pcb->state != LISTEN", pcb->state != LISTEN);
screamer 0:7a64fbb4069d 184 if (pcb->remote_port == tcphdr->src &&
screamer 0:7a64fbb4069d 185 pcb->local_port == tcphdr->dest &&
screamer 0:7a64fbb4069d 186 ip_addr_cmp(&(pcb->remote_ip), &(iphdr->src)) &&
screamer 0:7a64fbb4069d 187 ip_addr_cmp(&(pcb->local_ip), &(iphdr->dest))) {
screamer 0:7a64fbb4069d 188
screamer 0:7a64fbb4069d 189 /* Move this PCB to the front of the list so that subsequent
screamer 0:7a64fbb4069d 190 lookups will be faster (we exploit locality in TCP segment
screamer 0:7a64fbb4069d 191 arrivals). */
screamer 0:7a64fbb4069d 192 LWIP_ASSERT("tcp_input: pcb->next != pcb (before cache)", pcb->next != pcb);
screamer 0:7a64fbb4069d 193 if (prev != NULL) {
screamer 0:7a64fbb4069d 194 prev->next = pcb->next;
screamer 0:7a64fbb4069d 195 pcb->next = tcp_active_pcbs;
screamer 0:7a64fbb4069d 196 tcp_active_pcbs = pcb;
screamer 0:7a64fbb4069d 197 }
screamer 0:7a64fbb4069d 198 LWIP_ASSERT("tcp_input: pcb->next != pcb (after cache)", pcb->next != pcb);
screamer 0:7a64fbb4069d 199 break;
screamer 0:7a64fbb4069d 200 }
screamer 0:7a64fbb4069d 201 prev = pcb;
screamer 0:7a64fbb4069d 202 }
screamer 0:7a64fbb4069d 203
screamer 0:7a64fbb4069d 204 if (pcb == NULL) {
screamer 0:7a64fbb4069d 205 /* If it did not go to an active connection, we check the connections
screamer 0:7a64fbb4069d 206 in the TIME-WAIT state. */
screamer 0:7a64fbb4069d 207 for(pcb = tcp_tw_pcbs; pcb != NULL; pcb = pcb->next) {
screamer 0:7a64fbb4069d 208 LWIP_ASSERT("tcp_input: TIME-WAIT pcb->state == TIME-WAIT", pcb->state == TIME_WAIT);
screamer 0:7a64fbb4069d 209 if (pcb->remote_port == tcphdr->src &&
screamer 0:7a64fbb4069d 210 pcb->local_port == tcphdr->dest &&
screamer 0:7a64fbb4069d 211 ip_addr_cmp(&(pcb->remote_ip), &(iphdr->src)) &&
screamer 0:7a64fbb4069d 212 ip_addr_cmp(&(pcb->local_ip), &(iphdr->dest))) {
screamer 0:7a64fbb4069d 213 /* We don't really care enough to move this PCB to the front
screamer 0:7a64fbb4069d 214 of the list since we are not very likely to receive that
screamer 0:7a64fbb4069d 215 many segments for connections in TIME-WAIT. */
screamer 0:7a64fbb4069d 216 LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_input: packed for TIME_WAITing connection.\n"));
screamer 0:7a64fbb4069d 217 tcp_timewait_input(pcb);
screamer 0:7a64fbb4069d 218 pbuf_free(p);
screamer 0:7a64fbb4069d 219 return;
screamer 0:7a64fbb4069d 220 }
screamer 0:7a64fbb4069d 221 }
screamer 0:7a64fbb4069d 222
screamer 0:7a64fbb4069d 223 /* Finally, if we still did not get a match, we check all PCBs that
screamer 0:7a64fbb4069d 224 are LISTENing for incoming connections. */
screamer 0:7a64fbb4069d 225 prev = (struct tcp_pcb *)(NULL);
screamer 0:7a64fbb4069d 226 for(lpcb = tcp_listen_pcbs.listen_pcbs; lpcb != NULL; lpcb = lpcb->next) {
screamer 0:7a64fbb4069d 227 if ((ip_addr_isany(&(lpcb->local_ip)) ||
screamer 0:7a64fbb4069d 228 ip_addr_cmp(&(lpcb->local_ip), &(iphdr->dest))) &&
screamer 0:7a64fbb4069d 229 lpcb->local_port == tcphdr->dest) {
screamer 0:7a64fbb4069d 230 /* Move this PCB to the front of the list so that subsequent
screamer 0:7a64fbb4069d 231 lookups will be faster (we exploit locality in TCP segment
screamer 0:7a64fbb4069d 232 arrivals). */
screamer 0:7a64fbb4069d 233 if (prev != NULL) {
screamer 0:7a64fbb4069d 234 ((struct tcp_pcb_listen *)prev)->next = lpcb->next;
screamer 0:7a64fbb4069d 235 /* our successor is the remainder of the listening list */
screamer 0:7a64fbb4069d 236 lpcb->next = tcp_listen_pcbs.listen_pcbs;
screamer 0:7a64fbb4069d 237 /* put this listening pcb at the head of the listening list */
screamer 0:7a64fbb4069d 238 tcp_listen_pcbs.listen_pcbs = lpcb;
screamer 0:7a64fbb4069d 239 }
screamer 0:7a64fbb4069d 240
screamer 0:7a64fbb4069d 241 LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_input: packed for LISTENing connection.\n"));
screamer 0:7a64fbb4069d 242 tcp_listen_input(lpcb);
screamer 0:7a64fbb4069d 243 pbuf_free(p);
screamer 0:7a64fbb4069d 244 return;
screamer 0:7a64fbb4069d 245 }
screamer 0:7a64fbb4069d 246 prev = (struct tcp_pcb *)lpcb;
screamer 0:7a64fbb4069d 247 }
screamer 0:7a64fbb4069d 248 }
screamer 0:7a64fbb4069d 249
screamer 0:7a64fbb4069d 250 #if TCP_INPUT_DEBUG
screamer 0:7a64fbb4069d 251 LWIP_DEBUGF(TCP_INPUT_DEBUG, ("+-+-+-+-+-+-+-+-+-+-+-+-+-+- tcp_input: flags "));
screamer 0:7a64fbb4069d 252 tcp_debug_print_flags(TCPH_FLAGS(tcphdr));
screamer 0:7a64fbb4069d 253 LWIP_DEBUGF(TCP_INPUT_DEBUG, ("-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n"));
screamer 0:7a64fbb4069d 254 #endif /* TCP_INPUT_DEBUG */
screamer 0:7a64fbb4069d 255
screamer 0:7a64fbb4069d 256
screamer 0:7a64fbb4069d 257 if (pcb != NULL) {
screamer 0:7a64fbb4069d 258 /* The incoming segment belongs to a connection. */
screamer 0:7a64fbb4069d 259 #if TCP_INPUT_DEBUG
screamer 0:7a64fbb4069d 260 #if TCP_DEBUG
screamer 0:7a64fbb4069d 261 tcp_debug_print_state(pcb->state);
screamer 0:7a64fbb4069d 262 #endif /* TCP_DEBUG */
screamer 0:7a64fbb4069d 263 #endif /* TCP_INPUT_DEBUG */
screamer 0:7a64fbb4069d 264
screamer 0:7a64fbb4069d 265 /* Set up a tcp_seg structure. */
screamer 0:7a64fbb4069d 266 inseg.next = (struct tcp_seg *)(NULL);
screamer 0:7a64fbb4069d 267 inseg.len = p->tot_len;
screamer 0:7a64fbb4069d 268 inseg.dataptr = p->payload;
screamer 0:7a64fbb4069d 269 inseg.p = p;
screamer 0:7a64fbb4069d 270 inseg.tcphdr = tcphdr;
screamer 0:7a64fbb4069d 271
screamer 0:7a64fbb4069d 272 recv_data = (struct pbuf *)(NULL);
screamer 0:7a64fbb4069d 273 recv_flags = 0;
screamer 0:7a64fbb4069d 274
screamer 0:7a64fbb4069d 275 /* If there is data which was previously "refused" by upper layer */
screamer 0:7a64fbb4069d 276 if (pcb->refused_data != NULL) {
screamer 0:7a64fbb4069d 277 /* Notify again application with data previously received. */
screamer 0:7a64fbb4069d 278 LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_input: notify kept packet\n"));
screamer 0:7a64fbb4069d 279 TCP_EVENT_RECV(pcb, pcb->refused_data, ERR_OK, err);
screamer 0:7a64fbb4069d 280 if (err == ERR_OK) {
screamer 0:7a64fbb4069d 281 pcb->refused_data = (struct pbuf *)(NULL);
screamer 0:7a64fbb4069d 282 } else {
screamer 0:7a64fbb4069d 283 /* drop incoming packets, because pcb is "full" */
screamer 0:7a64fbb4069d 284 LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_input: drop incoming packets, because pcb is \"full\"\n"));
screamer 0:7a64fbb4069d 285 TCP_STATS_INC(tcp.drop);
screamer 0:7a64fbb4069d 286 snmp_inc_tcpinerrs();
screamer 0:7a64fbb4069d 287 pbuf_free(p);
screamer 0:7a64fbb4069d 288 return;
screamer 0:7a64fbb4069d 289 }
screamer 0:7a64fbb4069d 290 }
screamer 0:7a64fbb4069d 291
screamer 0:7a64fbb4069d 292 tcp_input_pcb = pcb;
screamer 0:7a64fbb4069d 293 err = tcp_process(pcb);
screamer 0:7a64fbb4069d 294 tcp_input_pcb = (struct tcp_pcb *)(NULL);
screamer 0:7a64fbb4069d 295 /* A return value of ERR_ABRT means that tcp_abort() was called
screamer 0:7a64fbb4069d 296 and that the pcb has been freed. If so, we don't do anything. */
screamer 0:7a64fbb4069d 297 if (err != ERR_ABRT) {
screamer 0:7a64fbb4069d 298 if (recv_flags & TF_RESET) {
screamer 0:7a64fbb4069d 299 /* TF_RESET means that the connection was reset by the other
screamer 0:7a64fbb4069d 300 end. We then call the error callback to inform the
screamer 0:7a64fbb4069d 301 application that the connection is dead before we
screamer 0:7a64fbb4069d 302 deallocate the PCB. */
screamer 0:7a64fbb4069d 303 TCP_EVENT_ERR(pcb->errf, pcb->callback_arg, ERR_RST);
screamer 0:7a64fbb4069d 304 tcp_pcb_remove(&tcp_active_pcbs, pcb);
screamer 0:7a64fbb4069d 305 memp_free(MEMP_TCP_PCB, pcb);
screamer 0:7a64fbb4069d 306 } else if (recv_flags & TF_CLOSED) {
screamer 0:7a64fbb4069d 307 /* The connection has been closed and we will deallocate the
screamer 0:7a64fbb4069d 308 PCB. */
screamer 0:7a64fbb4069d 309 tcp_pcb_remove(&tcp_active_pcbs, pcb);
screamer 0:7a64fbb4069d 310 memp_free(MEMP_TCP_PCB, pcb);
screamer 0:7a64fbb4069d 311 } else {
screamer 0:7a64fbb4069d 312 err = ERR_OK;
screamer 0:7a64fbb4069d 313 /* If the application has registered a "sent" function to be
screamer 0:7a64fbb4069d 314 called when new send buffer space is available, we call it
screamer 0:7a64fbb4069d 315 now. */
screamer 0:7a64fbb4069d 316 if (pcb->acked > 0) {
screamer 0:7a64fbb4069d 317 TCP_EVENT_SENT(pcb, pcb->acked, err);
screamer 0:7a64fbb4069d 318 }
screamer 0:7a64fbb4069d 319
screamer 0:7a64fbb4069d 320 if (recv_data != NULL) {
screamer 0:7a64fbb4069d 321 if(flags & TCP_PSH) {
screamer 0:7a64fbb4069d 322 recv_data->flags |= PBUF_FLAG_PUSH;
screamer 0:7a64fbb4069d 323 }
screamer 0:7a64fbb4069d 324
screamer 0:7a64fbb4069d 325 /* Notify application that data has been received. */
screamer 0:7a64fbb4069d 326 TCP_EVENT_RECV(pcb, recv_data, ERR_OK, err);
screamer 0:7a64fbb4069d 327
screamer 0:7a64fbb4069d 328 /* If the upper layer can't receive this data, store it */
screamer 0:7a64fbb4069d 329 if (err != ERR_OK) {
screamer 0:7a64fbb4069d 330 pcb->refused_data = recv_data;
screamer 0:7a64fbb4069d 331 LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_input: keep incoming packet, because pcb is \"full\"\n"));
screamer 0:7a64fbb4069d 332 }
screamer 0:7a64fbb4069d 333 }
screamer 0:7a64fbb4069d 334
screamer 0:7a64fbb4069d 335 /* If a FIN segment was received, we call the callback
screamer 0:7a64fbb4069d 336 function with a NULL buffer to indicate EOF. */
screamer 0:7a64fbb4069d 337 if (recv_flags & TF_GOT_FIN) {
screamer 0:7a64fbb4069d 338 TCP_EVENT_RECV(pcb, (NULL), ERR_OK, err);
screamer 0:7a64fbb4069d 339 }
screamer 0:7a64fbb4069d 340
screamer 0:7a64fbb4069d 341 /* If there were no errors, we try to send something out. */
screamer 0:7a64fbb4069d 342 if (err == ERR_OK) {
screamer 0:7a64fbb4069d 343 tcp_output(pcb);
screamer 0:7a64fbb4069d 344 }
screamer 0:7a64fbb4069d 345 }
screamer 0:7a64fbb4069d 346 }
screamer 0:7a64fbb4069d 347
screamer 0:7a64fbb4069d 348
screamer 0:7a64fbb4069d 349 /* give up our reference to inseg.p */
screamer 0:7a64fbb4069d 350 if (inseg.p != NULL)
screamer 0:7a64fbb4069d 351 {
screamer 0:7a64fbb4069d 352 pbuf_free(inseg.p);
screamer 0:7a64fbb4069d 353 inseg.p = (struct pbuf *)(NULL);
screamer 0:7a64fbb4069d 354 }
screamer 0:7a64fbb4069d 355 #if TCP_INPUT_DEBUG
screamer 0:7a64fbb4069d 356 #if TCP_DEBUG
screamer 0:7a64fbb4069d 357 tcp_debug_print_state(pcb->state);
screamer 0:7a64fbb4069d 358 #endif /* TCP_DEBUG */
screamer 0:7a64fbb4069d 359 #endif /* TCP_INPUT_DEBUG */
screamer 0:7a64fbb4069d 360
screamer 0:7a64fbb4069d 361 } else {
screamer 0:7a64fbb4069d 362
screamer 0:7a64fbb4069d 363 /* If no matching PCB was found, send a TCP RST (reset) to the
screamer 0:7a64fbb4069d 364 sender. */
screamer 0:7a64fbb4069d 365 LWIP_DEBUGF(TCP_RST_DEBUG, ("tcp_input: no PCB match found, resetting.\n"));
screamer 0:7a64fbb4069d 366 if (!(TCPH_FLAGS(tcphdr) & TCP_RST)) {
screamer 0:7a64fbb4069d 367 TCP_STATS_INC(tcp.proterr);
screamer 0:7a64fbb4069d 368 TCP_STATS_INC(tcp.drop);
screamer 0:7a64fbb4069d 369 struct ip_addr iphdrdest;
screamer 0:7a64fbb4069d 370 iphdrdest.addr = iphdr->dest.addr;
screamer 0:7a64fbb4069d 371 struct ip_addr iphdrsrc;
screamer 0:7a64fbb4069d 372 iphdrsrc.addr = iphdr->src.addr;
screamer 0:7a64fbb4069d 373 tcp_rst(ackno, seqno + tcplen,
screamer 0:7a64fbb4069d 374 &(iphdrdest), &(iphdrsrc),
screamer 0:7a64fbb4069d 375 tcphdr->dest, tcphdr->src);
screamer 0:7a64fbb4069d 376 }
screamer 0:7a64fbb4069d 377 pbuf_free(p);
screamer 0:7a64fbb4069d 378 }
screamer 0:7a64fbb4069d 379
screamer 0:7a64fbb4069d 380 LWIP_ASSERT("tcp_input: tcp_pcbs_sane()", tcp_pcbs_sane());
screamer 0:7a64fbb4069d 381 PERF_STOP("tcp_input");
screamer 0:7a64fbb4069d 382 }
screamer 0:7a64fbb4069d 383
screamer 0:7a64fbb4069d 384 /**
screamer 0:7a64fbb4069d 385 * Called by tcp_input() when a segment arrives for a listening
screamer 0:7a64fbb4069d 386 * connection (from tcp_input()).
screamer 0:7a64fbb4069d 387 *
screamer 0:7a64fbb4069d 388 * @param pcb the tcp_pcb_listen for which a segment arrived
screamer 0:7a64fbb4069d 389 * @return ERR_OK if the segment was processed
screamer 0:7a64fbb4069d 390 * another err_t on error
screamer 0:7a64fbb4069d 391 *
screamer 0:7a64fbb4069d 392 * @note the return value is not (yet?) used in tcp_input()
screamer 0:7a64fbb4069d 393 * @note the segment which arrived is saved in global variables, therefore only the pcb
screamer 0:7a64fbb4069d 394 * involved is passed as a parameter to this function
screamer 0:7a64fbb4069d 395 */
screamer 0:7a64fbb4069d 396 static err_t
screamer 0:7a64fbb4069d 397 tcp_listen_input(struct tcp_pcb_listen *pcb)
screamer 0:7a64fbb4069d 398 {
screamer 0:7a64fbb4069d 399 struct tcp_pcb *npcb;
screamer 0:7a64fbb4069d 400 err_t rc;
screamer 0:7a64fbb4069d 401
screamer 0:7a64fbb4069d 402 /* In the LISTEN state, we check for incoming SYN segments,
screamer 0:7a64fbb4069d 403 creates a new PCB, and responds with a SYN|ACK. */
screamer 0:7a64fbb4069d 404 if (flags & TCP_ACK) {
screamer 0:7a64fbb4069d 405 /* For incoming segments with the ACK flag set, respond with a
screamer 0:7a64fbb4069d 406 RST. */
screamer 0:7a64fbb4069d 407 LWIP_DEBUGF(TCP_RST_DEBUG, ("tcp_listen_input: ACK in LISTEN, sending reset\n"));
screamer 0:7a64fbb4069d 408 struct ip_addr iphdrdest;
screamer 0:7a64fbb4069d 409 iphdrdest.addr = iphdr->dest.addr;
screamer 0:7a64fbb4069d 410 struct ip_addr iphdrsrc;
screamer 0:7a64fbb4069d 411 iphdrsrc.addr = iphdr->src.addr;
screamer 0:7a64fbb4069d 412 tcp_rst(ackno + 1, seqno + tcplen,
screamer 0:7a64fbb4069d 413 &(iphdrdest), &(iphdrsrc),
screamer 0:7a64fbb4069d 414 tcphdr->dest, tcphdr->src);
screamer 0:7a64fbb4069d 415 } else if (flags & TCP_SYN) {
screamer 0:7a64fbb4069d 416 LWIP_DEBUGF(TCP_DEBUG, ("TCP connection request %"U16_F" -> %"U16_F".\n", tcphdr->src, tcphdr->dest));
screamer 0:7a64fbb4069d 417 #if TCP_LISTEN_BACKLOG
screamer 0:7a64fbb4069d 418 if (pcb->accepts_pending >= pcb->backlog) {
screamer 0:7a64fbb4069d 419 return ERR_ABRT;
screamer 0:7a64fbb4069d 420 }
screamer 0:7a64fbb4069d 421 #endif /* TCP_LISTEN_BACKLOG */
screamer 0:7a64fbb4069d 422 npcb = tcp_alloc(pcb->prio);
screamer 0:7a64fbb4069d 423 /* If a new PCB could not be created (probably due to lack of memory),
screamer 0:7a64fbb4069d 424 we don't do anything, but rely on the sender will retransmit the
screamer 0:7a64fbb4069d 425 SYN at a time when we have more memory available. */
screamer 0:7a64fbb4069d 426 if (npcb == NULL) {
screamer 0:7a64fbb4069d 427 LWIP_DEBUGF(TCP_DEBUG, ("tcp_listen_input: could not allocate PCB\n"));
screamer 0:7a64fbb4069d 428 TCP_STATS_INC(tcp.memerr);
screamer 0:7a64fbb4069d 429 return ERR_MEM;
screamer 0:7a64fbb4069d 430 }
screamer 0:7a64fbb4069d 431 #if TCP_LISTEN_BACKLOG
screamer 0:7a64fbb4069d 432 pcb->accepts_pending++;
screamer 0:7a64fbb4069d 433 #endif /* TCP_LISTEN_BACKLOG */
screamer 0:7a64fbb4069d 434 /* Set up the new PCB. */
screamer 0:7a64fbb4069d 435 ip_addr_set(&(npcb->local_ip), &(iphdr->dest));
screamer 0:7a64fbb4069d 436 npcb->local_port = pcb->local_port;
screamer 0:7a64fbb4069d 437 ip_addr_set(&(npcb->remote_ip), &(iphdr->src));
screamer 0:7a64fbb4069d 438 npcb->remote_port = tcphdr->src;
screamer 0:7a64fbb4069d 439 npcb->state = SYN_RCVD;
screamer 0:7a64fbb4069d 440 npcb->rcv_nxt = seqno + 1;
screamer 0:7a64fbb4069d 441 npcb->rcv_ann_right_edge = npcb->rcv_nxt;
screamer 0:7a64fbb4069d 442 npcb->snd_wnd = tcphdr->wnd;
screamer 0:7a64fbb4069d 443 npcb->ssthresh = npcb->snd_wnd;
screamer 0:7a64fbb4069d 444 npcb->snd_wl1 = seqno - 1;/* initialise to seqno-1 to force window update */
screamer 0:7a64fbb4069d 445 npcb->callback_arg = pcb->callback_arg;
screamer 0:7a64fbb4069d 446 #if LWIP_CALLBACK_API
screamer 0:7a64fbb4069d 447 npcb->accept = pcb->accept;
screamer 0:7a64fbb4069d 448 #endif /* LWIP_CALLBACK_API */
screamer 0:7a64fbb4069d 449 /* inherit socket options */
screamer 0:7a64fbb4069d 450 npcb->so_options = pcb->so_options & (SOF_DEBUG|SOF_DONTROUTE|SOF_KEEPALIVE|SOF_OOBINLINE|SOF_LINGER);
screamer 0:7a64fbb4069d 451 /* Register the new PCB so that we can begin receiving segments
screamer 0:7a64fbb4069d 452 for it. */
screamer 0:7a64fbb4069d 453 TCP_REG(&tcp_active_pcbs, npcb);
screamer 0:7a64fbb4069d 454
screamer 0:7a64fbb4069d 455 /* Parse any options in the SYN. */
screamer 0:7a64fbb4069d 456 tcp_parseopt(npcb);
screamer 0:7a64fbb4069d 457 #if TCP_CALCULATE_EFF_SEND_MSS
screamer 0:7a64fbb4069d 458 npcb->mss = tcp_eff_send_mss(npcb->mss, &(npcb->remote_ip));
screamer 0:7a64fbb4069d 459 #endif /* TCP_CALCULATE_EFF_SEND_MSS */
screamer 0:7a64fbb4069d 460
screamer 0:7a64fbb4069d 461 snmp_inc_tcppassiveopens();
screamer 0:7a64fbb4069d 462
screamer 0:7a64fbb4069d 463 /* Send a SYN|ACK together with the MSS option. */
screamer 0:7a64fbb4069d 464 rc = tcp_enqueue(npcb, NULL, 0, TCP_SYN | TCP_ACK, 0, TF_SEG_OPTS_MSS
screamer 0:7a64fbb4069d 465 #if LWIP_TCP_TIMESTAMPS
screamer 0:7a64fbb4069d 466 /* and maybe include the TIMESTAMP option */
screamer 0:7a64fbb4069d 467 | (npcb->flags & TF_TIMESTAMP ? TF_SEG_OPTS_TS : 0)
screamer 0:7a64fbb4069d 468 #endif
screamer 0:7a64fbb4069d 469 );
screamer 0:7a64fbb4069d 470 if (rc != ERR_OK) {
screamer 0:7a64fbb4069d 471 tcp_abandon(npcb, 0);
screamer 0:7a64fbb4069d 472 return rc;
screamer 0:7a64fbb4069d 473 }
screamer 0:7a64fbb4069d 474 return tcp_output(npcb);
screamer 0:7a64fbb4069d 475 }
screamer 0:7a64fbb4069d 476 return ERR_OK;
screamer 0:7a64fbb4069d 477 }
screamer 0:7a64fbb4069d 478
screamer 0:7a64fbb4069d 479 /**
screamer 0:7a64fbb4069d 480 * Called by tcp_input() when a segment arrives for a connection in
screamer 0:7a64fbb4069d 481 * TIME_WAIT.
screamer 0:7a64fbb4069d 482 *
screamer 0:7a64fbb4069d 483 * @param pcb the tcp_pcb for which a segment arrived
screamer 0:7a64fbb4069d 484 *
screamer 0:7a64fbb4069d 485 * @note the segment which arrived is saved in global variables, therefore only the pcb
screamer 0:7a64fbb4069d 486 * involved is passed as a parameter to this function
screamer 0:7a64fbb4069d 487 */
screamer 0:7a64fbb4069d 488 static err_t
screamer 0:7a64fbb4069d 489 tcp_timewait_input(struct tcp_pcb *pcb)
screamer 0:7a64fbb4069d 490 {
screamer 0:7a64fbb4069d 491 if (TCP_SEQ_GT(seqno + tcplen, pcb->rcv_nxt)) {
screamer 0:7a64fbb4069d 492 pcb->rcv_nxt = seqno + tcplen;
screamer 0:7a64fbb4069d 493 }
screamer 0:7a64fbb4069d 494 if (tcplen > 0) {
screamer 0:7a64fbb4069d 495 tcp_ack_now(pcb);
screamer 0:7a64fbb4069d 496 }
screamer 0:7a64fbb4069d 497 return tcp_output(pcb);
screamer 0:7a64fbb4069d 498 }
screamer 0:7a64fbb4069d 499
screamer 0:7a64fbb4069d 500 /**
screamer 0:7a64fbb4069d 501 * Implements the TCP state machine. Called by tcp_input. In some
screamer 0:7a64fbb4069d 502 * states tcp_receive() is called to receive data. The tcp_seg
screamer 0:7a64fbb4069d 503 * argument will be freed by the caller (tcp_input()) unless the
screamer 0:7a64fbb4069d 504 * recv_data pointer in the pcb is set.
screamer 0:7a64fbb4069d 505 *
screamer 0:7a64fbb4069d 506 * @param pcb the tcp_pcb for which a segment arrived
screamer 0:7a64fbb4069d 507 *
screamer 0:7a64fbb4069d 508 * @note the segment which arrived is saved in global variables, therefore only the pcb
screamer 0:7a64fbb4069d 509 * involved is passed as a parameter to this function
screamer 0:7a64fbb4069d 510 */
screamer 0:7a64fbb4069d 511 static err_t
screamer 0:7a64fbb4069d 512 tcp_process(struct tcp_pcb *pcb)
screamer 0:7a64fbb4069d 513 {
screamer 0:7a64fbb4069d 514 struct tcp_seg *rseg;
screamer 0:7a64fbb4069d 515 u8_t acceptable = 0;
screamer 0:7a64fbb4069d 516 err_t err;
screamer 0:7a64fbb4069d 517 u8_t accepted_inseq;
screamer 0:7a64fbb4069d 518
screamer 0:7a64fbb4069d 519 err = ERR_OK;
screamer 0:7a64fbb4069d 520
screamer 0:7a64fbb4069d 521 /* Process incoming RST segments. */
screamer 0:7a64fbb4069d 522 if (flags & TCP_RST) {
screamer 0:7a64fbb4069d 523 /* First, determine if the reset is acceptable. */
screamer 0:7a64fbb4069d 524 if (pcb->state == SYN_SENT) {
screamer 0:7a64fbb4069d 525 if (ackno == pcb->snd_nxt) {
screamer 0:7a64fbb4069d 526 acceptable = 1;
screamer 0:7a64fbb4069d 527 }
screamer 0:7a64fbb4069d 528 } else {
screamer 0:7a64fbb4069d 529 if (TCP_SEQ_BETWEEN(seqno, pcb->rcv_nxt,
screamer 0:7a64fbb4069d 530 pcb->rcv_nxt+pcb->rcv_wnd)) {
screamer 0:7a64fbb4069d 531 acceptable = 1;
screamer 0:7a64fbb4069d 532 }
screamer 0:7a64fbb4069d 533 }
screamer 0:7a64fbb4069d 534
screamer 0:7a64fbb4069d 535 if (acceptable) {
screamer 0:7a64fbb4069d 536 LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_process: Connection RESET\n"));
screamer 0:7a64fbb4069d 537 LWIP_ASSERT("tcp_input: pcb->state != CLOSED", pcb->state != CLOSED);
screamer 0:7a64fbb4069d 538 recv_flags = TF_RESET;
screamer 0:7a64fbb4069d 539 pcb->flags &= ~TF_ACK_DELAY;
screamer 0:7a64fbb4069d 540 return ERR_RST;
screamer 0:7a64fbb4069d 541 } else {
screamer 0:7a64fbb4069d 542 LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_process: unacceptable reset seqno %"U32_F" rcv_nxt %"U32_F"\n",
screamer 0:7a64fbb4069d 543 seqno, pcb->rcv_nxt));
screamer 0:7a64fbb4069d 544 LWIP_DEBUGF(TCP_DEBUG, ("tcp_process: unacceptable reset seqno %"U32_F" rcv_nxt %"U32_F"\n",
screamer 0:7a64fbb4069d 545 seqno, pcb->rcv_nxt));
screamer 0:7a64fbb4069d 546 return ERR_OK;
screamer 0:7a64fbb4069d 547 }
screamer 0:7a64fbb4069d 548 }
screamer 0:7a64fbb4069d 549
screamer 0:7a64fbb4069d 550 if ((flags & TCP_SYN) && (pcb->state != SYN_SENT && pcb->state != SYN_RCVD)) {
screamer 0:7a64fbb4069d 551 /* Cope with new connection attempt after remote end crashed */
screamer 0:7a64fbb4069d 552 tcp_ack_now(pcb);
screamer 0:7a64fbb4069d 553 return ERR_OK;
screamer 0:7a64fbb4069d 554 }
screamer 0:7a64fbb4069d 555
screamer 0:7a64fbb4069d 556 /* Update the PCB (in)activity timer. */
screamer 0:7a64fbb4069d 557 pcb->tmr = tcp_ticks;
screamer 0:7a64fbb4069d 558 pcb->keep_cnt_sent = 0;
screamer 0:7a64fbb4069d 559
screamer 0:7a64fbb4069d 560 tcp_parseopt(pcb);
screamer 0:7a64fbb4069d 561
screamer 0:7a64fbb4069d 562 /* Do different things depending on the TCP state. */
screamer 0:7a64fbb4069d 563 switch (pcb->state) {
screamer 0:7a64fbb4069d 564 case SYN_SENT:
screamer 0:7a64fbb4069d 565 LWIP_DEBUGF(TCP_INPUT_DEBUG, ("SYN-SENT: ackno %"U32_F" pcb->snd_nxt %"U32_F" unacked %"U32_F"\n", ackno,
screamer 0:7a64fbb4069d 566 pcb->snd_nxt, ntohl(pcb->unacked->tcphdr->seqno)));
screamer 0:7a64fbb4069d 567 /* received SYN ACK with expected sequence number? */
screamer 0:7a64fbb4069d 568 if ((flags & TCP_ACK) && (flags & TCP_SYN)
screamer 0:7a64fbb4069d 569 && ackno == ntohl(pcb->unacked->tcphdr->seqno) + 1) {
screamer 0:7a64fbb4069d 570 pcb->snd_buf++;
screamer 0:7a64fbb4069d 571 pcb->rcv_nxt = seqno + 1;
screamer 0:7a64fbb4069d 572 pcb->rcv_ann_right_edge = pcb->rcv_nxt;
screamer 0:7a64fbb4069d 573 pcb->lastack = ackno;
screamer 0:7a64fbb4069d 574 pcb->snd_wnd = tcphdr->wnd;
screamer 0:7a64fbb4069d 575 pcb->snd_wl1 = seqno - 1; /* initialise to seqno - 1 to force window update */
screamer 0:7a64fbb4069d 576 pcb->state = ESTABLISHED;
screamer 0:7a64fbb4069d 577
screamer 0:7a64fbb4069d 578 #if TCP_CALCULATE_EFF_SEND_MSS
screamer 0:7a64fbb4069d 579 pcb->mss = tcp_eff_send_mss(pcb->mss, &(pcb->remote_ip));
screamer 0:7a64fbb4069d 580 #endif /* TCP_CALCULATE_EFF_SEND_MSS */
screamer 0:7a64fbb4069d 581
screamer 0:7a64fbb4069d 582 /* Set ssthresh again after changing pcb->mss (already set in tcp_connect
screamer 0:7a64fbb4069d 583 * but for the default value of pcb->mss) */
screamer 0:7a64fbb4069d 584 pcb->ssthresh = pcb->mss * 10;
screamer 0:7a64fbb4069d 585
screamer 0:7a64fbb4069d 586 pcb->cwnd = ((pcb->cwnd == 1) ? (pcb->mss * 2) : pcb->mss);
screamer 0:7a64fbb4069d 587 LWIP_ASSERT("pcb->snd_queuelen > 0", (pcb->snd_queuelen > 0));
screamer 0:7a64fbb4069d 588 --pcb->snd_queuelen;
screamer 0:7a64fbb4069d 589 LWIP_DEBUGF(TCP_QLEN_DEBUG, ("tcp_process: SYN-SENT --queuelen %"U16_F"\n", (u16_t)pcb->snd_queuelen));
screamer 0:7a64fbb4069d 590 rseg = pcb->unacked;
screamer 0:7a64fbb4069d 591 pcb->unacked = rseg->next;
screamer 0:7a64fbb4069d 592
screamer 0:7a64fbb4069d 593 /* If there's nothing left to acknowledge, stop the retransmit
screamer 0:7a64fbb4069d 594 timer, otherwise reset it to start again */
screamer 0:7a64fbb4069d 595 if(pcb->unacked == NULL)
screamer 0:7a64fbb4069d 596 pcb->rtime = -1;
screamer 0:7a64fbb4069d 597 else {
screamer 0:7a64fbb4069d 598 pcb->rtime = 0;
screamer 0:7a64fbb4069d 599 pcb->nrtx = 0;
screamer 0:7a64fbb4069d 600 }
screamer 0:7a64fbb4069d 601
screamer 0:7a64fbb4069d 602 tcp_seg_free(rseg);
screamer 0:7a64fbb4069d 603
screamer 0:7a64fbb4069d 604 /* Call the user specified function to call when sucessfully
screamer 0:7a64fbb4069d 605 * connected. */
screamer 0:7a64fbb4069d 606 TCP_EVENT_CONNECTED(pcb, ERR_OK, err);
screamer 0:7a64fbb4069d 607 tcp_ack_now(pcb);
screamer 0:7a64fbb4069d 608 }
screamer 0:7a64fbb4069d 609 /* received ACK? possibly a half-open connection */
screamer 0:7a64fbb4069d 610 else if (flags & TCP_ACK) {
screamer 0:7a64fbb4069d 611 /* send a RST to bring the other side in a non-synchronized state. */
screamer 0:7a64fbb4069d 612 struct ip_addr iphdrdest;
screamer 0:7a64fbb4069d 613 iphdrdest.addr = iphdr->dest.addr;
screamer 0:7a64fbb4069d 614 struct ip_addr iphdrsrc;
screamer 0:7a64fbb4069d 615 iphdrsrc.addr = iphdr->src.addr;
screamer 0:7a64fbb4069d 616 tcp_rst(ackno, seqno + tcplen, &(iphdrdest), &(iphdrsrc),
screamer 0:7a64fbb4069d 617 tcphdr->dest, tcphdr->src);
screamer 0:7a64fbb4069d 618 }
screamer 0:7a64fbb4069d 619 break;
screamer 0:7a64fbb4069d 620 case SYN_RCVD:
screamer 0:7a64fbb4069d 621 if (flags & TCP_ACK) {
screamer 0:7a64fbb4069d 622 /* expected ACK number? */
screamer 0:7a64fbb4069d 623 if (TCP_SEQ_BETWEEN(ackno, pcb->lastack+1, pcb->snd_nxt)) {
screamer 0:7a64fbb4069d 624 u16_t old_cwnd;
screamer 0:7a64fbb4069d 625 pcb->state = ESTABLISHED;
screamer 0:7a64fbb4069d 626 LWIP_DEBUGF(TCP_DEBUG, ("TCP connection established %"U16_F" -> %"U16_F".\n", inseg.tcphdr->src, inseg.tcphdr->dest));
screamer 0:7a64fbb4069d 627 #if LWIP_CALLBACK_API
screamer 0:7a64fbb4069d 628 LWIP_ASSERT("pcb->accept != NULL", pcb->accept != NULL);
screamer 0:7a64fbb4069d 629 #endif
screamer 0:7a64fbb4069d 630 /* Call the accept function. */
screamer 0:7a64fbb4069d 631 TCP_EVENT_ACCEPT(pcb, ERR_OK, err);
screamer 0:7a64fbb4069d 632 if (err != ERR_OK) {
screamer 0:7a64fbb4069d 633 /* If the accept function returns with an error, we abort
screamer 0:7a64fbb4069d 634 * the connection. */
screamer 0:7a64fbb4069d 635 tcp_abort(pcb);
screamer 0:7a64fbb4069d 636 return ERR_ABRT;
screamer 0:7a64fbb4069d 637 }
screamer 0:7a64fbb4069d 638 old_cwnd = pcb->cwnd;
screamer 0:7a64fbb4069d 639 /* If there was any data contained within this ACK,
screamer 0:7a64fbb4069d 640 * we'd better pass it on to the application as well. */
screamer 0:7a64fbb4069d 641 accepted_inseq = tcp_receive(pcb);
screamer 0:7a64fbb4069d 642
screamer 0:7a64fbb4069d 643 pcb->cwnd = ((old_cwnd == 1) ? (pcb->mss * 2) : pcb->mss);
screamer 0:7a64fbb4069d 644
screamer 0:7a64fbb4069d 645 if ((flags & TCP_FIN) && accepted_inseq) {
screamer 0:7a64fbb4069d 646 tcp_ack_now(pcb);
screamer 0:7a64fbb4069d 647 pcb->state = CLOSE_WAIT;
screamer 0:7a64fbb4069d 648 }
screamer 0:7a64fbb4069d 649 }
screamer 0:7a64fbb4069d 650 /* incorrect ACK number */
screamer 0:7a64fbb4069d 651 else {
screamer 0:7a64fbb4069d 652 /* send RST */
screamer 0:7a64fbb4069d 653 struct ip_addr iphdrdest;
screamer 0:7a64fbb4069d 654 iphdrdest.addr = iphdr->dest.addr;
screamer 0:7a64fbb4069d 655 struct ip_addr iphdrsrc;
screamer 0:7a64fbb4069d 656 iphdrsrc.addr = iphdr->src.addr;
screamer 0:7a64fbb4069d 657 tcp_rst(ackno, seqno + tcplen, &(iphdrdest), &(iphdrsrc),
screamer 0:7a64fbb4069d 658 tcphdr->dest, tcphdr->src);
screamer 0:7a64fbb4069d 659 }
screamer 0:7a64fbb4069d 660 } else if ((flags & TCP_SYN) && (seqno == pcb->rcv_nxt - 1)) {
screamer 0:7a64fbb4069d 661 /* Looks like another copy of the SYN - retransmit our SYN-ACK */
screamer 0:7a64fbb4069d 662 tcp_rexmit(pcb);
screamer 0:7a64fbb4069d 663 }
screamer 0:7a64fbb4069d 664 break;
screamer 0:7a64fbb4069d 665 case CLOSE_WAIT:
screamer 0:7a64fbb4069d 666 /* FALLTHROUGH */
screamer 0:7a64fbb4069d 667 case ESTABLISHED:
screamer 0:7a64fbb4069d 668 accepted_inseq = tcp_receive(pcb);
screamer 0:7a64fbb4069d 669 if ((flags & TCP_FIN) && accepted_inseq) { /* passive close */
screamer 0:7a64fbb4069d 670 tcp_ack_now(pcb);
screamer 0:7a64fbb4069d 671 pcb->state = CLOSE_WAIT;
screamer 0:7a64fbb4069d 672 }
screamer 0:7a64fbb4069d 673 break;
screamer 0:7a64fbb4069d 674 case FIN_WAIT_1:
screamer 0:7a64fbb4069d 675 tcp_receive(pcb);
screamer 0:7a64fbb4069d 676 if (flags & TCP_FIN) {
screamer 0:7a64fbb4069d 677 if (flags & TCP_ACK && ackno == pcb->snd_nxt) {
screamer 0:7a64fbb4069d 678 LWIP_DEBUGF(TCP_DEBUG,
screamer 0:7a64fbb4069d 679 ("TCP connection closed %"U16_F" -> %"U16_F".\n", inseg.tcphdr->src, inseg.tcphdr->dest));
screamer 0:7a64fbb4069d 680 tcp_ack_now(pcb);
screamer 0:7a64fbb4069d 681 tcp_pcb_purge(pcb);
screamer 0:7a64fbb4069d 682 TCP_RMV(&tcp_active_pcbs, pcb);
screamer 0:7a64fbb4069d 683 pcb->state = TIME_WAIT;
screamer 0:7a64fbb4069d 684 TCP_REG(&tcp_tw_pcbs, pcb);
screamer 0:7a64fbb4069d 685 } else {
screamer 0:7a64fbb4069d 686 tcp_ack_now(pcb);
screamer 0:7a64fbb4069d 687 pcb->state = CLOSING;
screamer 0:7a64fbb4069d 688 }
screamer 0:7a64fbb4069d 689 } else if (flags & TCP_ACK && ackno == pcb->snd_nxt) {
screamer 0:7a64fbb4069d 690 pcb->state = FIN_WAIT_2;
screamer 0:7a64fbb4069d 691 }
screamer 0:7a64fbb4069d 692 break;
screamer 0:7a64fbb4069d 693 case FIN_WAIT_2:
screamer 0:7a64fbb4069d 694 tcp_receive(pcb);
screamer 0:7a64fbb4069d 695 if (flags & TCP_FIN) {
screamer 0:7a64fbb4069d 696 LWIP_DEBUGF(TCP_DEBUG, ("TCP connection closed %"U16_F" -> %"U16_F".\n", inseg.tcphdr->src, inseg.tcphdr->dest));
screamer 0:7a64fbb4069d 697 tcp_ack_now(pcb);
screamer 0:7a64fbb4069d 698 tcp_pcb_purge(pcb);
screamer 0:7a64fbb4069d 699 TCP_RMV(&tcp_active_pcbs, pcb);
screamer 0:7a64fbb4069d 700 pcb->state = TIME_WAIT;
screamer 0:7a64fbb4069d 701 TCP_REG(&tcp_tw_pcbs, pcb);
screamer 0:7a64fbb4069d 702 }
screamer 0:7a64fbb4069d 703 break;
screamer 0:7a64fbb4069d 704 case CLOSING:
screamer 0:7a64fbb4069d 705 tcp_receive(pcb);
screamer 0:7a64fbb4069d 706 if (flags & TCP_ACK && ackno == pcb->snd_nxt) {
screamer 0:7a64fbb4069d 707 LWIP_DEBUGF(TCP_DEBUG, ("TCP connection closed %"U16_F" -> %"U16_F".\n", inseg.tcphdr->src, inseg.tcphdr->dest));
screamer 0:7a64fbb4069d 708 tcp_ack_now(pcb);
screamer 0:7a64fbb4069d 709 tcp_pcb_purge(pcb);
screamer 0:7a64fbb4069d 710 TCP_RMV(&tcp_active_pcbs, pcb);
screamer 0:7a64fbb4069d 711 pcb->state = TIME_WAIT;
screamer 0:7a64fbb4069d 712 TCP_REG(&tcp_tw_pcbs, pcb);
screamer 0:7a64fbb4069d 713 }
screamer 0:7a64fbb4069d 714 break;
screamer 0:7a64fbb4069d 715 case LAST_ACK:
screamer 0:7a64fbb4069d 716 tcp_receive(pcb);
screamer 0:7a64fbb4069d 717 if (flags & TCP_ACK && ackno == pcb->snd_nxt) {
screamer 0:7a64fbb4069d 718 LWIP_DEBUGF(TCP_DEBUG, ("TCP connection closed %"U16_F" -> %"U16_F".\n", inseg.tcphdr->src, inseg.tcphdr->dest));
screamer 0:7a64fbb4069d 719 /* bugfix #21699: don't set pcb->state to CLOSED here or we risk leaking segments */
screamer 0:7a64fbb4069d 720 recv_flags = TF_CLOSED;
screamer 0:7a64fbb4069d 721 }
screamer 0:7a64fbb4069d 722 break;
screamer 0:7a64fbb4069d 723 default:
screamer 0:7a64fbb4069d 724 break;
screamer 0:7a64fbb4069d 725 }
screamer 0:7a64fbb4069d 726 return ERR_OK;
screamer 0:7a64fbb4069d 727 }
screamer 0:7a64fbb4069d 728
screamer 0:7a64fbb4069d 729 /**
screamer 0:7a64fbb4069d 730 * Called by tcp_process. Checks if the given segment is an ACK for outstanding
screamer 0:7a64fbb4069d 731 * data, and if so frees the memory of the buffered data. Next, is places the
screamer 0:7a64fbb4069d 732 * segment on any of the receive queues (pcb->recved or pcb->ooseq). If the segment
screamer 0:7a64fbb4069d 733 * is buffered, the pbuf is referenced by pbuf_ref so that it will not be freed until
screamer 0:7a64fbb4069d 734 * i it has been removed from the buffer.
screamer 0:7a64fbb4069d 735 *
screamer 0:7a64fbb4069d 736 * If the incoming segment constitutes an ACK for a segment that was used for RTT
screamer 0:7a64fbb4069d 737 * estimation, the RTT is estimated here as well.
screamer 0:7a64fbb4069d 738 *
screamer 0:7a64fbb4069d 739 * Called from tcp_process().
screamer 0:7a64fbb4069d 740 *
screamer 0:7a64fbb4069d 741 * @return 1 if the incoming segment is the next in sequence, 0 if not
screamer 0:7a64fbb4069d 742 */
screamer 0:7a64fbb4069d 743 static u8_t
screamer 0:7a64fbb4069d 744 tcp_receive(struct tcp_pcb *pcb)
screamer 0:7a64fbb4069d 745 {
screamer 0:7a64fbb4069d 746 struct tcp_seg *next;
screamer 0:7a64fbb4069d 747 #if TCP_QUEUE_OOSEQ
screamer 0:7a64fbb4069d 748 struct tcp_seg *prev, *cseg;
screamer 0:7a64fbb4069d 749 #endif
screamer 0:7a64fbb4069d 750 struct pbuf *p;
screamer 0:7a64fbb4069d 751 s32_t off;
screamer 0:7a64fbb4069d 752 s16_t m;
screamer 0:7a64fbb4069d 753 u32_t right_wnd_edge;
screamer 0:7a64fbb4069d 754 u16_t new_tot_len;
screamer 0:7a64fbb4069d 755 u8_t accepted_inseq = 0;
screamer 0:7a64fbb4069d 756
screamer 0:7a64fbb4069d 757 if (flags & TCP_ACK) {
screamer 0:7a64fbb4069d 758 right_wnd_edge = pcb->snd_wnd + pcb->snd_wl1;
screamer 0:7a64fbb4069d 759
screamer 0:7a64fbb4069d 760 /* Update window. */
screamer 0:7a64fbb4069d 761 if (TCP_SEQ_LT(pcb->snd_wl1, seqno) ||
screamer 0:7a64fbb4069d 762 (pcb->snd_wl1 == seqno && TCP_SEQ_LT(pcb->snd_wl2, ackno)) ||
screamer 0:7a64fbb4069d 763 (pcb->snd_wl2 == ackno && tcphdr->wnd > pcb->snd_wnd)) {
screamer 0:7a64fbb4069d 764 pcb->snd_wnd = tcphdr->wnd;
screamer 0:7a64fbb4069d 765 pcb->snd_wl1 = seqno;
screamer 0:7a64fbb4069d 766 pcb->snd_wl2 = ackno;
screamer 0:7a64fbb4069d 767 if (pcb->snd_wnd > 0 && pcb->persist_backoff > 0) {
screamer 0:7a64fbb4069d 768 pcb->persist_backoff = 0;
screamer 0:7a64fbb4069d 769 }
screamer 0:7a64fbb4069d 770 LWIP_DEBUGF(TCP_WND_DEBUG, ("tcp_receive: window update %"U16_F"\n", pcb->snd_wnd));
screamer 0:7a64fbb4069d 771 #if TCP_WND_DEBUG
screamer 0:7a64fbb4069d 772 } else {
screamer 0:7a64fbb4069d 773 if (pcb->snd_wnd != tcphdr->wnd) {
screamer 0:7a64fbb4069d 774 LWIP_DEBUGF(TCP_WND_DEBUG, ("tcp_receive: no window update lastack %"U32_F" snd_max %"U32_F" ackno %"U32_F" wl1 %"U32_F" seqno %"U32_F" wl2 %"U32_F"\n",
screamer 0:7a64fbb4069d 775 pcb->lastack, pcb->snd_max, ackno, pcb->snd_wl1, seqno, pcb->snd_wl2));
screamer 0:7a64fbb4069d 776 }
screamer 0:7a64fbb4069d 777 #endif /* TCP_WND_DEBUG */
screamer 0:7a64fbb4069d 778 }
screamer 0:7a64fbb4069d 779
screamer 0:7a64fbb4069d 780 if (pcb->lastack == ackno) {
screamer 0:7a64fbb4069d 781 pcb->acked = 0;
screamer 0:7a64fbb4069d 782
screamer 0:7a64fbb4069d 783 if (pcb->snd_wl1 + pcb->snd_wnd == right_wnd_edge){
screamer 0:7a64fbb4069d 784 ++pcb->dupacks;
screamer 0:7a64fbb4069d 785 if (pcb->dupacks >= 3 && pcb->unacked != NULL) {
screamer 0:7a64fbb4069d 786 if (!(pcb->flags & TF_INFR)) {
screamer 0:7a64fbb4069d 787 /* This is fast retransmit. Retransmit the first unacked segment. */
screamer 0:7a64fbb4069d 788 LWIP_DEBUGF(TCP_FR_DEBUG, ("tcp_receive: dupacks %"U16_F" (%"U32_F"), fast retransmit %"U32_F"\n",
screamer 0:7a64fbb4069d 789 (u16_t)pcb->dupacks, pcb->lastack,
screamer 0:7a64fbb4069d 790 ntohl(pcb->unacked->tcphdr->seqno)));
screamer 0:7a64fbb4069d 791 tcp_rexmit(pcb);
screamer 0:7a64fbb4069d 792 /* Set ssthresh to max (FlightSize / 2, 2*SMSS) */
screamer 0:7a64fbb4069d 793 /*pcb->ssthresh = LWIP_MAX((pcb->snd_max -
screamer 0:7a64fbb4069d 794 pcb->lastack) / 2,
screamer 0:7a64fbb4069d 795 2 * pcb->mss);*/
screamer 0:7a64fbb4069d 796 /* Set ssthresh to half of the minimum of the current cwnd and the advertised window */
screamer 0:7a64fbb4069d 797 if (pcb->cwnd > pcb->snd_wnd)
screamer 0:7a64fbb4069d 798 pcb->ssthresh = pcb->snd_wnd / 2;
screamer 0:7a64fbb4069d 799 else
screamer 0:7a64fbb4069d 800 pcb->ssthresh = pcb->cwnd / 2;
screamer 0:7a64fbb4069d 801
screamer 0:7a64fbb4069d 802 /* The minimum value for ssthresh should be 2 MSS */
screamer 0:7a64fbb4069d 803 if (pcb->ssthresh < 2*pcb->mss) {
screamer 0:7a64fbb4069d 804 LWIP_DEBUGF(TCP_FR_DEBUG, ("tcp_receive: The minimum value for ssthresh %"U16_F" should be min 2 mss %"U16_F"...\n", pcb->ssthresh, 2*pcb->mss));
screamer 0:7a64fbb4069d 805 pcb->ssthresh = 2*pcb->mss;
screamer 0:7a64fbb4069d 806 }
screamer 0:7a64fbb4069d 807
screamer 0:7a64fbb4069d 808 pcb->cwnd = pcb->ssthresh + 3 * pcb->mss;
screamer 0:7a64fbb4069d 809 pcb->flags |= TF_INFR;
screamer 0:7a64fbb4069d 810 } else {
screamer 0:7a64fbb4069d 811 /* Inflate the congestion window, but not if it means that
screamer 0:7a64fbb4069d 812 the value overflows. */
screamer 0:7a64fbb4069d 813 if ((u16_t)(pcb->cwnd + pcb->mss) > pcb->cwnd) {
screamer 0:7a64fbb4069d 814 pcb->cwnd += pcb->mss;
screamer 0:7a64fbb4069d 815 }
screamer 0:7a64fbb4069d 816 }
screamer 0:7a64fbb4069d 817 }
screamer 0:7a64fbb4069d 818 } else {
screamer 0:7a64fbb4069d 819 LWIP_DEBUGF(TCP_FR_DEBUG, ("tcp_receive: dupack averted %"U32_F" %"U32_F"\n",
screamer 0:7a64fbb4069d 820 pcb->snd_wl1 + pcb->snd_wnd, right_wnd_edge));
screamer 0:7a64fbb4069d 821 }
screamer 0:7a64fbb4069d 822 } else if (TCP_SEQ_BETWEEN(ackno, pcb->lastack+1, pcb->snd_max)){
screamer 0:7a64fbb4069d 823 /* We come here when the ACK acknowledges new data. */
screamer 0:7a64fbb4069d 824
screamer 0:7a64fbb4069d 825 /* Reset the "IN Fast Retransmit" flag, since we are no longer
screamer 0:7a64fbb4069d 826 in fast retransmit. Also reset the congestion window to the
screamer 0:7a64fbb4069d 827 slow start threshold. */
screamer 0:7a64fbb4069d 828 if (pcb->flags & TF_INFR) {
screamer 0:7a64fbb4069d 829 pcb->flags &= ~TF_INFR;
screamer 0:7a64fbb4069d 830 pcb->cwnd = pcb->ssthresh;
screamer 0:7a64fbb4069d 831 }
screamer 0:7a64fbb4069d 832
screamer 0:7a64fbb4069d 833 /* Reset the number of retransmissions. */
screamer 0:7a64fbb4069d 834 pcb->nrtx = 0;
screamer 0:7a64fbb4069d 835
screamer 0:7a64fbb4069d 836 /* Reset the retransmission time-out. */
screamer 0:7a64fbb4069d 837 pcb->rto = (pcb->sa >> 3) + pcb->sv;
screamer 0:7a64fbb4069d 838
screamer 0:7a64fbb4069d 839 /* Update the send buffer space. Diff between the two can never exceed 64K? */
screamer 0:7a64fbb4069d 840 pcb->acked = (u16_t)(ackno - pcb->lastack);
screamer 0:7a64fbb4069d 841
screamer 0:7a64fbb4069d 842 pcb->snd_buf += pcb->acked;
screamer 0:7a64fbb4069d 843
screamer 0:7a64fbb4069d 844 /* Reset the fast retransmit variables. */
screamer 0:7a64fbb4069d 845 pcb->dupacks = 0;
screamer 0:7a64fbb4069d 846 pcb->lastack = ackno;
screamer 0:7a64fbb4069d 847
screamer 0:7a64fbb4069d 848 /* Update the congestion control variables (cwnd and
screamer 0:7a64fbb4069d 849 ssthresh). */
screamer 0:7a64fbb4069d 850 if (pcb->state >= ESTABLISHED) {
screamer 0:7a64fbb4069d 851 if (pcb->cwnd < pcb->ssthresh) {
screamer 0:7a64fbb4069d 852 if ((u16_t)(pcb->cwnd + pcb->mss) > pcb->cwnd) {
screamer 0:7a64fbb4069d 853 pcb->cwnd += pcb->mss;
screamer 0:7a64fbb4069d 854 }
screamer 0:7a64fbb4069d 855 LWIP_DEBUGF(TCP_CWND_DEBUG, ("tcp_receive: slow start cwnd %"U16_F"\n", pcb->cwnd));
screamer 0:7a64fbb4069d 856 } else {
screamer 0:7a64fbb4069d 857 u16_t new_cwnd = (pcb->cwnd + pcb->mss * pcb->mss / pcb->cwnd);
screamer 0:7a64fbb4069d 858 if (new_cwnd > pcb->cwnd) {
screamer 0:7a64fbb4069d 859 pcb->cwnd = new_cwnd;
screamer 0:7a64fbb4069d 860 }
screamer 0:7a64fbb4069d 861 LWIP_DEBUGF(TCP_CWND_DEBUG, ("tcp_receive: congestion avoidance cwnd %"U16_F"\n", pcb->cwnd));
screamer 0:7a64fbb4069d 862 }
screamer 0:7a64fbb4069d 863 }
screamer 0:7a64fbb4069d 864 LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_receive: ACK for %"U32_F", unacked->seqno %"U32_F":%"U32_F"\n",
screamer 0:7a64fbb4069d 865 ackno,
screamer 0:7a64fbb4069d 866 pcb->unacked != NULL?
screamer 0:7a64fbb4069d 867 ntohl(pcb->unacked->tcphdr->seqno): 0,
screamer 0:7a64fbb4069d 868 pcb->unacked != NULL?
screamer 0:7a64fbb4069d 869 ntohl(pcb->unacked->tcphdr->seqno) + TCP_TCPLEN(pcb->unacked): 0));
screamer 0:7a64fbb4069d 870
screamer 0:7a64fbb4069d 871 /* Remove segment from the unacknowledged list if the incoming
screamer 0:7a64fbb4069d 872 ACK acknowlegdes them. */
screamer 0:7a64fbb4069d 873 while (pcb->unacked != NULL &&
screamer 0:7a64fbb4069d 874 TCP_SEQ_LEQ(ntohl(pcb->unacked->tcphdr->seqno) +
screamer 0:7a64fbb4069d 875 TCP_TCPLEN(pcb->unacked), ackno)) {
screamer 0:7a64fbb4069d 876 LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_receive: removing %"U32_F":%"U32_F" from pcb->unacked\n",
screamer 0:7a64fbb4069d 877 ntohl(pcb->unacked->tcphdr->seqno),
screamer 0:7a64fbb4069d 878 ntohl(pcb->unacked->tcphdr->seqno) +
screamer 0:7a64fbb4069d 879 TCP_TCPLEN(pcb->unacked)));
screamer 0:7a64fbb4069d 880
screamer 0:7a64fbb4069d 881 next = pcb->unacked;
screamer 0:7a64fbb4069d 882 pcb->unacked = pcb->unacked->next;
screamer 0:7a64fbb4069d 883
screamer 0:7a64fbb4069d 884 LWIP_DEBUGF(TCP_QLEN_DEBUG, ("tcp_receive: queuelen %"U16_F" ... ", (u16_t)pcb->snd_queuelen));
screamer 0:7a64fbb4069d 885 LWIP_ASSERT("pcb->snd_queuelen >= pbuf_clen(next->p)", (pcb->snd_queuelen >= pbuf_clen(next->p)));
screamer 0:7a64fbb4069d 886 pcb->snd_queuelen -= pbuf_clen(next->p);
screamer 0:7a64fbb4069d 887 tcp_seg_free(next);
screamer 0:7a64fbb4069d 888
screamer 0:7a64fbb4069d 889 LWIP_DEBUGF(TCP_QLEN_DEBUG, ("%"U16_F" (after freeing unacked)\n", (u16_t)pcb->snd_queuelen));
screamer 0:7a64fbb4069d 890 if (pcb->snd_queuelen != 0) {
screamer 0:7a64fbb4069d 891 LWIP_ASSERT("tcp_receive: valid queue length", pcb->unacked != NULL ||
screamer 0:7a64fbb4069d 892 pcb->unsent != NULL);
screamer 0:7a64fbb4069d 893 }
screamer 0:7a64fbb4069d 894 }
screamer 0:7a64fbb4069d 895
screamer 0:7a64fbb4069d 896 /* If there's nothing left to acknowledge, stop the retransmit
screamer 0:7a64fbb4069d 897 timer, otherwise reset it to start again */
screamer 0:7a64fbb4069d 898 if(pcb->unacked == NULL)
screamer 0:7a64fbb4069d 899 pcb->rtime = -1;
screamer 0:7a64fbb4069d 900 else
screamer 0:7a64fbb4069d 901 pcb->rtime = 0;
screamer 0:7a64fbb4069d 902
screamer 0:7a64fbb4069d 903 pcb->polltmr = 0;
screamer 0:7a64fbb4069d 904 } else {
screamer 0:7a64fbb4069d 905 /* Fix bug bug #21582: out of sequence ACK, didn't really ack anything */
screamer 0:7a64fbb4069d 906 pcb->acked = 0;
screamer 0:7a64fbb4069d 907 }
screamer 0:7a64fbb4069d 908
screamer 0:7a64fbb4069d 909 /* We go through the ->unsent list to see if any of the segments
screamer 0:7a64fbb4069d 910 on the list are acknowledged by the ACK. This may seem
screamer 0:7a64fbb4069d 911 strange since an "unsent" segment shouldn't be acked. The
screamer 0:7a64fbb4069d 912 rationale is that lwIP puts all outstanding segments on the
screamer 0:7a64fbb4069d 913 ->unsent list after a retransmission, so these segments may
screamer 0:7a64fbb4069d 914 in fact have been sent once. */
screamer 0:7a64fbb4069d 915 while (pcb->unsent != NULL &&
screamer 0:7a64fbb4069d 916 /*TCP_SEQ_LEQ(ntohl(pcb->unsent->tcphdr->seqno) + TCP_TCPLEN(pcb->unsent), ackno) &&
screamer 0:7a64fbb4069d 917 TCP_SEQ_LEQ(ackno, pcb->snd_max)*/
screamer 0:7a64fbb4069d 918 TCP_SEQ_BETWEEN(ackno, ntohl(pcb->unsent->tcphdr->seqno) + TCP_TCPLEN(pcb->unsent), pcb->snd_max)
screamer 0:7a64fbb4069d 919 ) {
screamer 0:7a64fbb4069d 920 LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_receive: removing %"U32_F":%"U32_F" from pcb->unsent\n",
screamer 0:7a64fbb4069d 921 ntohl(pcb->unsent->tcphdr->seqno), ntohl(pcb->unsent->tcphdr->seqno) +
screamer 0:7a64fbb4069d 922 TCP_TCPLEN(pcb->unsent)));
screamer 0:7a64fbb4069d 923
screamer 0:7a64fbb4069d 924 next = pcb->unsent;
screamer 0:7a64fbb4069d 925 pcb->unsent = pcb->unsent->next;
screamer 0:7a64fbb4069d 926 LWIP_DEBUGF(TCP_QLEN_DEBUG, ("tcp_receive: queuelen %"U16_F" ... ", (u16_t)pcb->snd_queuelen));
screamer 0:7a64fbb4069d 927 LWIP_ASSERT("pcb->snd_queuelen >= pbuf_clen(next->p)", (pcb->snd_queuelen >= pbuf_clen(next->p)));
screamer 0:7a64fbb4069d 928 pcb->snd_queuelen -= pbuf_clen(next->p);
screamer 0:7a64fbb4069d 929 tcp_seg_free(next);
screamer 0:7a64fbb4069d 930 LWIP_DEBUGF(TCP_QLEN_DEBUG, ("%"U16_F" (after freeing unsent)\n", (u16_t)pcb->snd_queuelen));
screamer 0:7a64fbb4069d 931 if (pcb->snd_queuelen != 0) {
screamer 0:7a64fbb4069d 932 LWIP_ASSERT("tcp_receive: valid queue length",
screamer 0:7a64fbb4069d 933 pcb->unacked != NULL || pcb->unsent != NULL);
screamer 0:7a64fbb4069d 934 }
screamer 0:7a64fbb4069d 935
screamer 0:7a64fbb4069d 936 if (pcb->unsent != NULL) {
screamer 0:7a64fbb4069d 937 pcb->snd_nxt = htonl(pcb->unsent->tcphdr->seqno);
screamer 0:7a64fbb4069d 938 }
screamer 0:7a64fbb4069d 939 }
screamer 0:7a64fbb4069d 940 /* End of ACK for new data processing. */
screamer 0:7a64fbb4069d 941
screamer 0:7a64fbb4069d 942 LWIP_DEBUGF(TCP_RTO_DEBUG, ("tcp_receive: pcb->rttest %"U32_F" rtseq %"U32_F" ackno %"U32_F"\n",
screamer 0:7a64fbb4069d 943 pcb->rttest, pcb->rtseq, ackno));
screamer 0:7a64fbb4069d 944
screamer 0:7a64fbb4069d 945 /* RTT estimation calculations. This is done by checking if the
screamer 0:7a64fbb4069d 946 incoming segment acknowledges the segment we use to take a
screamer 0:7a64fbb4069d 947 round-trip time measurement. */
screamer 0:7a64fbb4069d 948 if (pcb->rttest && TCP_SEQ_LT(pcb->rtseq, ackno)) {
screamer 0:7a64fbb4069d 949 /* diff between this shouldn't exceed 32K since this are tcp timer ticks
screamer 0:7a64fbb4069d 950 and a round-trip shouldn't be that long... */
screamer 0:7a64fbb4069d 951 m = (s16_t)(tcp_ticks - pcb->rttest);
screamer 0:7a64fbb4069d 952
screamer 0:7a64fbb4069d 953 LWIP_DEBUGF(TCP_RTO_DEBUG, ("tcp_receive: experienced rtt %"U16_F" ticks (%"U16_F" msec).\n",
screamer 0:7a64fbb4069d 954 m, m * TCP_SLOW_INTERVAL));
screamer 0:7a64fbb4069d 955
screamer 0:7a64fbb4069d 956 /* This is taken directly from VJs original code in his paper */
screamer 0:7a64fbb4069d 957 m = m - (pcb->sa >> 3);
screamer 0:7a64fbb4069d 958 pcb->sa += m;
screamer 0:7a64fbb4069d 959 if (m < 0) {
screamer 0:7a64fbb4069d 960 m = -m;
screamer 0:7a64fbb4069d 961 }
screamer 0:7a64fbb4069d 962 m = m - (pcb->sv >> 2);
screamer 0:7a64fbb4069d 963 pcb->sv += m;
screamer 0:7a64fbb4069d 964 pcb->rto = (pcb->sa >> 3) + pcb->sv;
screamer 0:7a64fbb4069d 965
screamer 0:7a64fbb4069d 966 LWIP_DEBUGF(TCP_RTO_DEBUG, ("tcp_receive: RTO %"U16_F" (%"U16_F" milliseconds)\n",
screamer 0:7a64fbb4069d 967 pcb->rto, pcb->rto * TCP_SLOW_INTERVAL));
screamer 0:7a64fbb4069d 968
screamer 0:7a64fbb4069d 969 pcb->rttest = 0;
screamer 0:7a64fbb4069d 970 }
screamer 0:7a64fbb4069d 971 }
screamer 0:7a64fbb4069d 972
screamer 0:7a64fbb4069d 973 /* If the incoming segment contains data, we must process it
screamer 0:7a64fbb4069d 974 further. */
screamer 0:7a64fbb4069d 975 if (tcplen > 0) {
screamer 0:7a64fbb4069d 976 /* This code basically does three things:
screamer 0:7a64fbb4069d 977
screamer 0:7a64fbb4069d 978 +) If the incoming segment contains data that is the next
screamer 0:7a64fbb4069d 979 in-sequence data, this data is passed to the application. This
screamer 0:7a64fbb4069d 980 might involve trimming the first edge of the data. The rcv_nxt
screamer 0:7a64fbb4069d 981 variable and the advertised window are adjusted.
screamer 0:7a64fbb4069d 982
screamer 0:7a64fbb4069d 983 +) If the incoming segment has data that is above the next
screamer 0:7a64fbb4069d 984 sequence number expected (->rcv_nxt), the segment is placed on
screamer 0:7a64fbb4069d 985 the ->ooseq queue. This is done by finding the appropriate
screamer 0:7a64fbb4069d 986 place in the ->ooseq queue (which is ordered by sequence
screamer 0:7a64fbb4069d 987 number) and trim the segment in both ends if needed. An
screamer 0:7a64fbb4069d 988 immediate ACK is sent to indicate that we received an
screamer 0:7a64fbb4069d 989 out-of-sequence segment.
screamer 0:7a64fbb4069d 990
screamer 0:7a64fbb4069d 991 +) Finally, we check if the first segment on the ->ooseq queue
screamer 0:7a64fbb4069d 992 now is in sequence (i.e., if rcv_nxt >= ooseq->seqno). If
screamer 0:7a64fbb4069d 993 rcv_nxt > ooseq->seqno, we must trim the first edge of the
screamer 0:7a64fbb4069d 994 segment on ->ooseq before we adjust rcv_nxt. The data in the
screamer 0:7a64fbb4069d 995 segments that are now on sequence are chained onto the
screamer 0:7a64fbb4069d 996 incoming segment so that we only need to call the application
screamer 0:7a64fbb4069d 997 once.
screamer 0:7a64fbb4069d 998 */
screamer 0:7a64fbb4069d 999
screamer 0:7a64fbb4069d 1000 /* First, we check if we must trim the first edge. We have to do
screamer 0:7a64fbb4069d 1001 this if the sequence number of the incoming segment is less
screamer 0:7a64fbb4069d 1002 than rcv_nxt, and the sequence number plus the length of the
screamer 0:7a64fbb4069d 1003 segment is larger than rcv_nxt. */
screamer 0:7a64fbb4069d 1004 /* if (TCP_SEQ_LT(seqno, pcb->rcv_nxt)){
screamer 0:7a64fbb4069d 1005 if (TCP_SEQ_LT(pcb->rcv_nxt, seqno + tcplen)) {*/
screamer 0:7a64fbb4069d 1006 if (TCP_SEQ_BETWEEN(pcb->rcv_nxt, seqno + 1, seqno + tcplen - 1)){
screamer 0:7a64fbb4069d 1007 /* Trimming the first edge is done by pushing the payload
screamer 0:7a64fbb4069d 1008 pointer in the pbuf downwards. This is somewhat tricky since
screamer 0:7a64fbb4069d 1009 we do not want to discard the full contents of the pbuf up to
screamer 0:7a64fbb4069d 1010 the new starting point of the data since we have to keep the
screamer 0:7a64fbb4069d 1011 TCP header which is present in the first pbuf in the chain.
screamer 0:7a64fbb4069d 1012
screamer 0:7a64fbb4069d 1013 What is done is really quite a nasty hack: the first pbuf in
screamer 0:7a64fbb4069d 1014 the pbuf chain is pointed to by inseg.p. Since we need to be
screamer 0:7a64fbb4069d 1015 able to deallocate the whole pbuf, we cannot change this
screamer 0:7a64fbb4069d 1016 inseg.p pointer to point to any of the later pbufs in the
screamer 0:7a64fbb4069d 1017 chain. Instead, we point the ->payload pointer in the first
screamer 0:7a64fbb4069d 1018 pbuf to data in one of the later pbufs. We also set the
screamer 0:7a64fbb4069d 1019 inseg.data pointer to point to the right place. This way, the
screamer 0:7a64fbb4069d 1020 ->p pointer will still point to the first pbuf, but the
screamer 0:7a64fbb4069d 1021 ->p->payload pointer will point to data in another pbuf.
screamer 0:7a64fbb4069d 1022
screamer 0:7a64fbb4069d 1023 After we are done with adjusting the pbuf pointers we must
screamer 0:7a64fbb4069d 1024 adjust the ->data pointer in the seg and the segment
screamer 0:7a64fbb4069d 1025 length.*/
screamer 0:7a64fbb4069d 1026
screamer 0:7a64fbb4069d 1027 off = pcb->rcv_nxt - seqno;
screamer 0:7a64fbb4069d 1028 p = inseg.p;
screamer 0:7a64fbb4069d 1029 LWIP_ASSERT("inseg.p != NULL", inseg.p);
screamer 0:7a64fbb4069d 1030 LWIP_ASSERT("insane offset!", (off < 0x7fff));
screamer 0:7a64fbb4069d 1031 if (inseg.p->len < off) {
screamer 0:7a64fbb4069d 1032 LWIP_ASSERT("pbuf too short!", (((s32_t)inseg.p->tot_len) >= off));
screamer 0:7a64fbb4069d 1033 new_tot_len = (u16_t)(inseg.p->tot_len - off);
screamer 0:7a64fbb4069d 1034 while (p->len < off) {
screamer 0:7a64fbb4069d 1035 off -= p->len;
screamer 0:7a64fbb4069d 1036 /* KJM following line changed (with addition of new_tot_len var)
screamer 0:7a64fbb4069d 1037 to fix bug #9076
screamer 0:7a64fbb4069d 1038 inseg.p->tot_len -= p->len; */
screamer 0:7a64fbb4069d 1039 p->tot_len = new_tot_len;
screamer 0:7a64fbb4069d 1040 p->len = 0;
screamer 0:7a64fbb4069d 1041 p = p->next;
screamer 0:7a64fbb4069d 1042 }
screamer 0:7a64fbb4069d 1043 if(pbuf_header(p, (s16_t)-off)) {
screamer 0:7a64fbb4069d 1044 /* Do we need to cope with this failing? Assert for now */
screamer 0:7a64fbb4069d 1045 LWIP_ASSERT("pbuf_header failed", 0);
screamer 0:7a64fbb4069d 1046 }
screamer 0:7a64fbb4069d 1047 } else {
screamer 0:7a64fbb4069d 1048 if(pbuf_header(inseg.p, (s16_t)-off)) {
screamer 0:7a64fbb4069d 1049 /* Do we need to cope with this failing? Assert for now */
screamer 0:7a64fbb4069d 1050 LWIP_ASSERT("pbuf_header failed", 0);
screamer 0:7a64fbb4069d 1051 }
screamer 0:7a64fbb4069d 1052 }
screamer 0:7a64fbb4069d 1053 /* KJM following line changed to use p->payload rather than inseg->p->payload
screamer 0:7a64fbb4069d 1054 to fix bug #9076 */
screamer 0:7a64fbb4069d 1055 inseg.dataptr = p->payload;
screamer 0:7a64fbb4069d 1056 inseg.len -= (u16_t)(pcb->rcv_nxt - seqno);
screamer 0:7a64fbb4069d 1057 inseg.tcphdr->seqno = seqno = pcb->rcv_nxt;
screamer 0:7a64fbb4069d 1058 }
screamer 0:7a64fbb4069d 1059 else {
screamer 0:7a64fbb4069d 1060 if (TCP_SEQ_LT(seqno, pcb->rcv_nxt)){
screamer 0:7a64fbb4069d 1061 /* the whole segment is < rcv_nxt */
screamer 0:7a64fbb4069d 1062 /* must be a duplicate of a packet that has already been correctly handled */
screamer 0:7a64fbb4069d 1063
screamer 0:7a64fbb4069d 1064 LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_receive: duplicate seqno %"U32_F"\n", seqno));
screamer 0:7a64fbb4069d 1065 tcp_ack_now(pcb);
screamer 0:7a64fbb4069d 1066 }
screamer 0:7a64fbb4069d 1067 }
screamer 0:7a64fbb4069d 1068
screamer 0:7a64fbb4069d 1069 /* The sequence number must be within the window (above rcv_nxt
screamer 0:7a64fbb4069d 1070 and below rcv_nxt + rcv_wnd) in order to be further
screamer 0:7a64fbb4069d 1071 processed. */
screamer 0:7a64fbb4069d 1072 if (TCP_SEQ_BETWEEN(seqno, pcb->rcv_nxt,
screamer 0:7a64fbb4069d 1073 pcb->rcv_nxt + pcb->rcv_wnd - 1)){
screamer 0:7a64fbb4069d 1074 if (pcb->rcv_nxt == seqno) {
screamer 0:7a64fbb4069d 1075 accepted_inseq = 1;
screamer 0:7a64fbb4069d 1076 /* The incoming segment is the next in sequence. We check if
screamer 0:7a64fbb4069d 1077 we have to trim the end of the segment and update rcv_nxt
screamer 0:7a64fbb4069d 1078 and pass the data to the application. */
screamer 0:7a64fbb4069d 1079 #if TCP_QUEUE_OOSEQ
screamer 0:7a64fbb4069d 1080 if (pcb->ooseq != NULL &&
screamer 0:7a64fbb4069d 1081 TCP_SEQ_LEQ(pcb->ooseq->tcphdr->seqno, seqno + inseg.len)) {
screamer 0:7a64fbb4069d 1082 if (pcb->ooseq->len > 0) {
screamer 0:7a64fbb4069d 1083 /* We have to trim the second edge of the incoming
screamer 0:7a64fbb4069d 1084 segment. */
screamer 0:7a64fbb4069d 1085 inseg.len = (u16_t)(pcb->ooseq->tcphdr->seqno - seqno);
screamer 0:7a64fbb4069d 1086 pbuf_realloc(inseg.p, inseg.len);
screamer 0:7a64fbb4069d 1087 } else {
screamer 0:7a64fbb4069d 1088 /* does the ooseq segment contain only flags that are in inseg also? */
screamer 0:7a64fbb4069d 1089 if ((TCPH_FLAGS(inseg.tcphdr) & (TCP_FIN|TCP_SYN)) ==
screamer 0:7a64fbb4069d 1090 (TCPH_FLAGS(pcb->ooseq->tcphdr) & (TCP_FIN|TCP_SYN))) {
screamer 0:7a64fbb4069d 1091 struct tcp_seg *old_ooseq = pcb->ooseq;
screamer 0:7a64fbb4069d 1092 pcb->ooseq = pcb->ooseq->next;
screamer 0:7a64fbb4069d 1093 memp_free(MEMP_TCP_SEG, old_ooseq);
screamer 0:7a64fbb4069d 1094 }
screamer 0:7a64fbb4069d 1095 }
screamer 0:7a64fbb4069d 1096 }
screamer 0:7a64fbb4069d 1097 #endif /* TCP_QUEUE_OOSEQ */
screamer 0:7a64fbb4069d 1098
screamer 0:7a64fbb4069d 1099 tcplen = TCP_TCPLEN(&inseg);
screamer 0:7a64fbb4069d 1100
screamer 0:7a64fbb4069d 1101 /* First received FIN will be ACKed +1, on any successive (duplicate)
screamer 0:7a64fbb4069d 1102 * FINs we are already in CLOSE_WAIT and have already done +1.
screamer 0:7a64fbb4069d 1103 */
screamer 0:7a64fbb4069d 1104 if (pcb->state != CLOSE_WAIT) {
screamer 0:7a64fbb4069d 1105 pcb->rcv_nxt += tcplen;
screamer 0:7a64fbb4069d 1106 }
screamer 0:7a64fbb4069d 1107
screamer 0:7a64fbb4069d 1108 /* Update the receiver's (our) window. */
screamer 0:7a64fbb4069d 1109 if (pcb->rcv_wnd < tcplen) {
screamer 0:7a64fbb4069d 1110 pcb->rcv_wnd = 0;
screamer 0:7a64fbb4069d 1111 } else {
screamer 0:7a64fbb4069d 1112 pcb->rcv_wnd -= tcplen;
screamer 0:7a64fbb4069d 1113 }
screamer 0:7a64fbb4069d 1114
screamer 0:7a64fbb4069d 1115 tcp_update_rcv_ann_wnd(pcb);
screamer 0:7a64fbb4069d 1116
screamer 0:7a64fbb4069d 1117 /* If there is data in the segment, we make preparations to
screamer 0:7a64fbb4069d 1118 pass this up to the application. The ->recv_data variable
screamer 0:7a64fbb4069d 1119 is used for holding the pbuf that goes to the
screamer 0:7a64fbb4069d 1120 application. The code for reassembling out-of-sequence data
screamer 0:7a64fbb4069d 1121 chains its data on this pbuf as well.
screamer 0:7a64fbb4069d 1122
screamer 0:7a64fbb4069d 1123 If the segment was a FIN, we set the TF_GOT_FIN flag that will
screamer 0:7a64fbb4069d 1124 be used to indicate to the application that the remote side has
screamer 0:7a64fbb4069d 1125 closed its end of the connection. */
screamer 0:7a64fbb4069d 1126 if (inseg.p->tot_len > 0) {
screamer 0:7a64fbb4069d 1127 recv_data = inseg.p;
screamer 0:7a64fbb4069d 1128 /* Since this pbuf now is the responsibility of the
screamer 0:7a64fbb4069d 1129 application, we delete our reference to it so that we won't
screamer 0:7a64fbb4069d 1130 (mistakingly) deallocate it. */
screamer 0:7a64fbb4069d 1131 inseg.p = NULL;
screamer 0:7a64fbb4069d 1132 }
screamer 0:7a64fbb4069d 1133 if (TCPH_FLAGS(inseg.tcphdr) & TCP_FIN) {
screamer 0:7a64fbb4069d 1134 LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_receive: received FIN.\n"));
screamer 0:7a64fbb4069d 1135 recv_flags = TF_GOT_FIN;
screamer 0:7a64fbb4069d 1136 }
screamer 0:7a64fbb4069d 1137
screamer 0:7a64fbb4069d 1138 #if TCP_QUEUE_OOSEQ
screamer 0:7a64fbb4069d 1139 /* We now check if we have segments on the ->ooseq queue that
screamer 0:7a64fbb4069d 1140 is now in sequence. */
screamer 0:7a64fbb4069d 1141 while (pcb->ooseq != NULL &&
screamer 0:7a64fbb4069d 1142 pcb->ooseq->tcphdr->seqno == pcb->rcv_nxt) {
screamer 0:7a64fbb4069d 1143
screamer 0:7a64fbb4069d 1144 cseg = pcb->ooseq;
screamer 0:7a64fbb4069d 1145 seqno = pcb->ooseq->tcphdr->seqno;
screamer 0:7a64fbb4069d 1146
screamer 0:7a64fbb4069d 1147 pcb->rcv_nxt += TCP_TCPLEN(cseg);
screamer 0:7a64fbb4069d 1148 if (pcb->rcv_wnd < TCP_TCPLEN(cseg)) {
screamer 0:7a64fbb4069d 1149 pcb->rcv_wnd = 0;
screamer 0:7a64fbb4069d 1150 } else {
screamer 0:7a64fbb4069d 1151 pcb->rcv_wnd -= TCP_TCPLEN(cseg);
screamer 0:7a64fbb4069d 1152 }
screamer 0:7a64fbb4069d 1153
screamer 0:7a64fbb4069d 1154 tcp_update_rcv_ann_wnd(pcb);
screamer 0:7a64fbb4069d 1155
screamer 0:7a64fbb4069d 1156 if (cseg->p->tot_len > 0) {
screamer 0:7a64fbb4069d 1157 /* Chain this pbuf onto the pbuf that we will pass to
screamer 0:7a64fbb4069d 1158 the application. */
screamer 0:7a64fbb4069d 1159 if (recv_data) {
screamer 0:7a64fbb4069d 1160 pbuf_cat(recv_data, cseg->p);
screamer 0:7a64fbb4069d 1161 } else {
screamer 0:7a64fbb4069d 1162 recv_data = cseg->p;
screamer 0:7a64fbb4069d 1163 }
screamer 0:7a64fbb4069d 1164 cseg->p = NULL;
screamer 0:7a64fbb4069d 1165 }
screamer 0:7a64fbb4069d 1166 if (TCPH_FLAGS(cseg->tcphdr) & TCP_FIN) {
screamer 0:7a64fbb4069d 1167 LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_receive: dequeued FIN.\n"));
screamer 0:7a64fbb4069d 1168 recv_flags = TF_GOT_FIN;
screamer 0:7a64fbb4069d 1169 if (pcb->state == ESTABLISHED) { /* force passive close or we can move to active close */
screamer 0:7a64fbb4069d 1170 pcb->state = CLOSE_WAIT;
screamer 0:7a64fbb4069d 1171 }
screamer 0:7a64fbb4069d 1172 }
screamer 0:7a64fbb4069d 1173
screamer 0:7a64fbb4069d 1174
screamer 0:7a64fbb4069d 1175 pcb->ooseq = cseg->next;
screamer 0:7a64fbb4069d 1176 tcp_seg_free(cseg);
screamer 0:7a64fbb4069d 1177 }
screamer 0:7a64fbb4069d 1178 #endif /* TCP_QUEUE_OOSEQ */
screamer 0:7a64fbb4069d 1179
screamer 0:7a64fbb4069d 1180
screamer 0:7a64fbb4069d 1181 /* Acknowledge the segment(s). */
screamer 0:7a64fbb4069d 1182 tcp_ack(pcb);
screamer 0:7a64fbb4069d 1183
screamer 0:7a64fbb4069d 1184 } else {
screamer 0:7a64fbb4069d 1185 /* We get here if the incoming segment is out-of-sequence. */
screamer 0:7a64fbb4069d 1186 tcp_ack_now(pcb);
screamer 0:7a64fbb4069d 1187 #if TCP_QUEUE_OOSEQ
screamer 0:7a64fbb4069d 1188 /* We queue the segment on the ->ooseq queue. */
screamer 0:7a64fbb4069d 1189 if (pcb->ooseq == NULL) {
screamer 0:7a64fbb4069d 1190 pcb->ooseq = tcp_seg_copy(&inseg);
screamer 0:7a64fbb4069d 1191 } else {
screamer 0:7a64fbb4069d 1192 /* If the queue is not empty, we walk through the queue and
screamer 0:7a64fbb4069d 1193 try to find a place where the sequence number of the
screamer 0:7a64fbb4069d 1194 incoming segment is between the sequence numbers of the
screamer 0:7a64fbb4069d 1195 previous and the next segment on the ->ooseq queue. That is
screamer 0:7a64fbb4069d 1196 the place where we put the incoming segment. If needed, we
screamer 0:7a64fbb4069d 1197 trim the second edges of the previous and the incoming
screamer 0:7a64fbb4069d 1198 segment so that it will fit into the sequence.
screamer 0:7a64fbb4069d 1199
screamer 0:7a64fbb4069d 1200 If the incoming segment has the same sequence number as a
screamer 0:7a64fbb4069d 1201 segment on the ->ooseq queue, we discard the segment that
screamer 0:7a64fbb4069d 1202 contains less data. */
screamer 0:7a64fbb4069d 1203
screamer 0:7a64fbb4069d 1204 prev = NULL;
screamer 0:7a64fbb4069d 1205 for(next = pcb->ooseq; next != NULL; next = next->next) {
screamer 0:7a64fbb4069d 1206 if (seqno == next->tcphdr->seqno) {
screamer 0:7a64fbb4069d 1207 /* The sequence number of the incoming segment is the
screamer 0:7a64fbb4069d 1208 same as the sequence number of the segment on
screamer 0:7a64fbb4069d 1209 ->ooseq. We check the lengths to see which one to
screamer 0:7a64fbb4069d 1210 discard. */
screamer 0:7a64fbb4069d 1211 if (inseg.len > next->len) {
screamer 0:7a64fbb4069d 1212 /* The incoming segment is larger than the old
screamer 0:7a64fbb4069d 1213 segment. We replace the old segment with the new
screamer 0:7a64fbb4069d 1214 one. */
screamer 0:7a64fbb4069d 1215 cseg = tcp_seg_copy(&inseg);
screamer 0:7a64fbb4069d 1216 if (cseg != NULL) {
screamer 0:7a64fbb4069d 1217 cseg->next = next->next;
screamer 0:7a64fbb4069d 1218 if (prev != NULL) {
screamer 0:7a64fbb4069d 1219 prev->next = cseg;
screamer 0:7a64fbb4069d 1220 } else {
screamer 0:7a64fbb4069d 1221 pcb->ooseq = cseg;
screamer 0:7a64fbb4069d 1222 }
screamer 0:7a64fbb4069d 1223 tcp_seg_free(next);
screamer 0:7a64fbb4069d 1224 if (cseg->next != NULL) {
screamer 0:7a64fbb4069d 1225 next = cseg->next;
screamer 0:7a64fbb4069d 1226 if (TCP_SEQ_GT(seqno + cseg->len, next->tcphdr->seqno)) {
screamer 0:7a64fbb4069d 1227 /* We need to trim the incoming segment. */
screamer 0:7a64fbb4069d 1228 cseg->len = (u16_t)(next->tcphdr->seqno - seqno);
screamer 0:7a64fbb4069d 1229 pbuf_realloc(cseg->p, cseg->len);
screamer 0:7a64fbb4069d 1230 }
screamer 0:7a64fbb4069d 1231 }
screamer 0:7a64fbb4069d 1232 }
screamer 0:7a64fbb4069d 1233 break;
screamer 0:7a64fbb4069d 1234 } else {
screamer 0:7a64fbb4069d 1235 /* Either the lenghts are the same or the incoming
screamer 0:7a64fbb4069d 1236 segment was smaller than the old one; in either
screamer 0:7a64fbb4069d 1237 case, we ditch the incoming segment. */
screamer 0:7a64fbb4069d 1238 break;
screamer 0:7a64fbb4069d 1239 }
screamer 0:7a64fbb4069d 1240 } else {
screamer 0:7a64fbb4069d 1241 if (prev == NULL) {
screamer 0:7a64fbb4069d 1242 if (TCP_SEQ_LT(seqno, next->tcphdr->seqno)) {
screamer 0:7a64fbb4069d 1243 /* The sequence number of the incoming segment is lower
screamer 0:7a64fbb4069d 1244 than the sequence number of the first segment on the
screamer 0:7a64fbb4069d 1245 queue. We put the incoming segment first on the
screamer 0:7a64fbb4069d 1246 queue. */
screamer 0:7a64fbb4069d 1247
screamer 0:7a64fbb4069d 1248 if (TCP_SEQ_GT(seqno + inseg.len, next->tcphdr->seqno)) {
screamer 0:7a64fbb4069d 1249 /* We need to trim the incoming segment. */
screamer 0:7a64fbb4069d 1250 inseg.len = (u16_t)(next->tcphdr->seqno - seqno);
screamer 0:7a64fbb4069d 1251 pbuf_realloc(inseg.p, inseg.len);
screamer 0:7a64fbb4069d 1252 }
screamer 0:7a64fbb4069d 1253 cseg = tcp_seg_copy(&inseg);
screamer 0:7a64fbb4069d 1254 if (cseg != NULL) {
screamer 0:7a64fbb4069d 1255 cseg->next = next;
screamer 0:7a64fbb4069d 1256 pcb->ooseq = cseg;
screamer 0:7a64fbb4069d 1257 }
screamer 0:7a64fbb4069d 1258 break;
screamer 0:7a64fbb4069d 1259 }
screamer 0:7a64fbb4069d 1260 } else
screamer 0:7a64fbb4069d 1261 /*if (TCP_SEQ_LT(prev->tcphdr->seqno, seqno) &&
screamer 0:7a64fbb4069d 1262 TCP_SEQ_LT(seqno, next->tcphdr->seqno)) {*/
screamer 0:7a64fbb4069d 1263 if(TCP_SEQ_BETWEEN(seqno, prev->tcphdr->seqno+1, next->tcphdr->seqno-1)){
screamer 0:7a64fbb4069d 1264 /* The sequence number of the incoming segment is in
screamer 0:7a64fbb4069d 1265 between the sequence numbers of the previous and
screamer 0:7a64fbb4069d 1266 the next segment on ->ooseq. We trim and insert the
screamer 0:7a64fbb4069d 1267 incoming segment and trim the previous segment, if
screamer 0:7a64fbb4069d 1268 needed. */
screamer 0:7a64fbb4069d 1269 if (TCP_SEQ_GT(seqno + inseg.len, next->tcphdr->seqno)) {
screamer 0:7a64fbb4069d 1270 /* We need to trim the incoming segment. */
screamer 0:7a64fbb4069d 1271 inseg.len = (u16_t)(next->tcphdr->seqno - seqno);
screamer 0:7a64fbb4069d 1272 pbuf_realloc(inseg.p, inseg.len);
screamer 0:7a64fbb4069d 1273 }
screamer 0:7a64fbb4069d 1274
screamer 0:7a64fbb4069d 1275 cseg = tcp_seg_copy(&inseg);
screamer 0:7a64fbb4069d 1276 if (cseg != NULL) {
screamer 0:7a64fbb4069d 1277 cseg->next = next;
screamer 0:7a64fbb4069d 1278 prev->next = cseg;
screamer 0:7a64fbb4069d 1279 if (TCP_SEQ_GT(prev->tcphdr->seqno + prev->len, seqno)) {
screamer 0:7a64fbb4069d 1280 /* We need to trim the prev segment. */
screamer 0:7a64fbb4069d 1281 prev->len = (u16_t)(seqno - prev->tcphdr->seqno);
screamer 0:7a64fbb4069d 1282 pbuf_realloc(prev->p, prev->len);
screamer 0:7a64fbb4069d 1283 }
screamer 0:7a64fbb4069d 1284 }
screamer 0:7a64fbb4069d 1285 break;
screamer 0:7a64fbb4069d 1286 }
screamer 0:7a64fbb4069d 1287 /* If the "next" segment is the last segment on the
screamer 0:7a64fbb4069d 1288 ooseq queue, we add the incoming segment to the end
screamer 0:7a64fbb4069d 1289 of the list. */
screamer 0:7a64fbb4069d 1290 if (next->next == NULL &&
screamer 0:7a64fbb4069d 1291 TCP_SEQ_GT(seqno, next->tcphdr->seqno)) {
screamer 0:7a64fbb4069d 1292 next->next = tcp_seg_copy(&inseg);
screamer 0:7a64fbb4069d 1293 if (next->next != NULL) {
screamer 0:7a64fbb4069d 1294 if (TCP_SEQ_GT(next->tcphdr->seqno + next->len, seqno)) {
screamer 0:7a64fbb4069d 1295 /* We need to trim the last segment. */
screamer 0:7a64fbb4069d 1296 next->len = (u16_t)(seqno - next->tcphdr->seqno);
screamer 0:7a64fbb4069d 1297 pbuf_realloc(next->p, next->len);
screamer 0:7a64fbb4069d 1298 }
screamer 0:7a64fbb4069d 1299 }
screamer 0:7a64fbb4069d 1300 break;
screamer 0:7a64fbb4069d 1301 }
screamer 0:7a64fbb4069d 1302 }
screamer 0:7a64fbb4069d 1303 prev = next;
screamer 0:7a64fbb4069d 1304 }
screamer 0:7a64fbb4069d 1305 }
screamer 0:7a64fbb4069d 1306 #endif /* TCP_QUEUE_OOSEQ */
screamer 0:7a64fbb4069d 1307
screamer 0:7a64fbb4069d 1308 }
screamer 0:7a64fbb4069d 1309 } else {
screamer 0:7a64fbb4069d 1310 tcp_ack_now(pcb);
screamer 0:7a64fbb4069d 1311 }
screamer 0:7a64fbb4069d 1312 } else {
screamer 0:7a64fbb4069d 1313 /* Segments with length 0 is taken care of here. Segments that
screamer 0:7a64fbb4069d 1314 fall out of the window are ACKed. */
screamer 0:7a64fbb4069d 1315 /*if (TCP_SEQ_GT(pcb->rcv_nxt, seqno) ||
screamer 0:7a64fbb4069d 1316 TCP_SEQ_GEQ(seqno, pcb->rcv_nxt + pcb->rcv_wnd)) {*/
screamer 0:7a64fbb4069d 1317 if(!TCP_SEQ_BETWEEN(seqno, pcb->rcv_nxt, pcb->rcv_nxt + pcb->rcv_wnd-1)){
screamer 0:7a64fbb4069d 1318 tcp_ack_now(pcb);
screamer 0:7a64fbb4069d 1319 }
screamer 0:7a64fbb4069d 1320 }
screamer 0:7a64fbb4069d 1321 return accepted_inseq;
screamer 0:7a64fbb4069d 1322 }
screamer 0:7a64fbb4069d 1323
screamer 0:7a64fbb4069d 1324 /**
screamer 0:7a64fbb4069d 1325 * Parses the options contained in the incoming segment.
screamer 0:7a64fbb4069d 1326 *
screamer 0:7a64fbb4069d 1327 * Called from tcp_listen_input() and tcp_process().
screamer 0:7a64fbb4069d 1328 * Currently, only the MSS option is supported!
screamer 0:7a64fbb4069d 1329 *
screamer 0:7a64fbb4069d 1330 * @param pcb the tcp_pcb for which a segment arrived
screamer 0:7a64fbb4069d 1331 */
screamer 0:7a64fbb4069d 1332 static void
screamer 0:7a64fbb4069d 1333 tcp_parseopt(struct tcp_pcb *pcb)
screamer 0:7a64fbb4069d 1334 {
screamer 0:7a64fbb4069d 1335 u16_t c, max_c;
screamer 0:7a64fbb4069d 1336 u16_t mss;
screamer 0:7a64fbb4069d 1337 u8_t *opts, opt;
screamer 0:7a64fbb4069d 1338 #if LWIP_TCP_TIMESTAMPS
screamer 0:7a64fbb4069d 1339 u32_t tsval;
screamer 0:7a64fbb4069d 1340 #endif
screamer 0:7a64fbb4069d 1341
screamer 0:7a64fbb4069d 1342 opts = (u8_t *)tcphdr + TCP_HLEN;
screamer 0:7a64fbb4069d 1343
screamer 0:7a64fbb4069d 1344 /* Parse the TCP MSS option, if present. */
screamer 0:7a64fbb4069d 1345 if(TCPH_HDRLEN(tcphdr) > 0x5) {
screamer 0:7a64fbb4069d 1346 max_c = (TCPH_HDRLEN(tcphdr) - 5) << 2;
screamer 0:7a64fbb4069d 1347 for (c = 0; c < max_c; ) {
screamer 0:7a64fbb4069d 1348 opt = opts[c];
screamer 0:7a64fbb4069d 1349 switch (opt) {
screamer 0:7a64fbb4069d 1350 case 0x00:
screamer 0:7a64fbb4069d 1351 /* End of options. */
screamer 0:7a64fbb4069d 1352 LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_parseopt: EOL\n"));
screamer 0:7a64fbb4069d 1353 return;
screamer 0:7a64fbb4069d 1354 case 0x01:
screamer 0:7a64fbb4069d 1355 /* NOP option. */
screamer 0:7a64fbb4069d 1356 ++c;
screamer 0:7a64fbb4069d 1357 LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_parseopt: NOP\n"));
screamer 0:7a64fbb4069d 1358 break;
screamer 0:7a64fbb4069d 1359 case 0x02:
screamer 0:7a64fbb4069d 1360 LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_parseopt: MSS\n"));
screamer 0:7a64fbb4069d 1361 if (opts[c + 1] != 0x04 || c + 0x04 > max_c) {
screamer 0:7a64fbb4069d 1362 /* Bad length */
screamer 0:7a64fbb4069d 1363 LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_parseopt: bad length\n"));
screamer 0:7a64fbb4069d 1364 return;
screamer 0:7a64fbb4069d 1365 }
screamer 0:7a64fbb4069d 1366 /* An MSS option with the right option length. */
screamer 0:7a64fbb4069d 1367 mss = (opts[c + 2] << 8) | opts[c + 3];
screamer 0:7a64fbb4069d 1368 /* Limit the mss to the configured TCP_MSS and prevent division by zero */
screamer 0:7a64fbb4069d 1369 pcb->mss = ((mss > TCP_MSS) || (mss == 0)) ? TCP_MSS : mss;
screamer 0:7a64fbb4069d 1370 /* Advance to next option */
screamer 0:7a64fbb4069d 1371 c += 0x04;
screamer 0:7a64fbb4069d 1372 break;
screamer 0:7a64fbb4069d 1373 #if LWIP_TCP_TIMESTAMPS
screamer 0:7a64fbb4069d 1374 case 0x08:
screamer 0:7a64fbb4069d 1375 LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_parseopt: TS\n"));
screamer 0:7a64fbb4069d 1376 if (opts[c + 1] != 0x0A || c + 0x0A > max_c) {
screamer 0:7a64fbb4069d 1377 /* Bad length */
screamer 0:7a64fbb4069d 1378 LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_parseopt: bad length\n"));
screamer 0:7a64fbb4069d 1379 return;
screamer 0:7a64fbb4069d 1380 }
screamer 0:7a64fbb4069d 1381 /* TCP timestamp option with valid length */
screamer 0:7a64fbb4069d 1382 tsval = (opts[c+2]) | (opts[c+3] << 8) |
screamer 0:7a64fbb4069d 1383 (opts[c+4] << 16) | (opts[c+5] << 24);
screamer 0:7a64fbb4069d 1384 if (flags & TCP_SYN) {
screamer 0:7a64fbb4069d 1385 pcb->ts_recent = ntohl(tsval);
screamer 0:7a64fbb4069d 1386 pcb->flags |= TF_TIMESTAMP;
screamer 0:7a64fbb4069d 1387 } else if (TCP_SEQ_BETWEEN(pcb->ts_lastacksent, seqno, seqno+tcplen)) {
screamer 0:7a64fbb4069d 1388 pcb->ts_recent = ntohl(tsval);
screamer 0:7a64fbb4069d 1389 }
screamer 0:7a64fbb4069d 1390 /* Advance to next option */
screamer 0:7a64fbb4069d 1391 c += 0x0A;
screamer 0:7a64fbb4069d 1392 break;
screamer 0:7a64fbb4069d 1393 #endif
screamer 0:7a64fbb4069d 1394 default:
screamer 0:7a64fbb4069d 1395 LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_parseopt: other\n"));
screamer 0:7a64fbb4069d 1396 if (opts[c + 1] == 0) {
screamer 0:7a64fbb4069d 1397 LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_parseopt: bad length\n"));
screamer 0:7a64fbb4069d 1398 /* If the length field is zero, the options are malformed
screamer 0:7a64fbb4069d 1399 and we don't process them further. */
screamer 0:7a64fbb4069d 1400 return;
screamer 0:7a64fbb4069d 1401 }
screamer 0:7a64fbb4069d 1402 /* All other options have a length field, so that we easily
screamer 0:7a64fbb4069d 1403 can skip past them. */
screamer 0:7a64fbb4069d 1404 c += opts[c + 1];
screamer 0:7a64fbb4069d 1405 }
screamer 0:7a64fbb4069d 1406 }
screamer 0:7a64fbb4069d 1407 }
screamer 0:7a64fbb4069d 1408 }
screamer 0:7a64fbb4069d 1409
screamer 0:7a64fbb4069d 1410 #endif /* LWIP_TCP */