NetTribute library with debug turned on in FShandler Donatien Garner -> Segundo Equipo -> this version

Committer:
hexley
Date:
Fri Nov 19 01:54:45 2010 +0000
Revision:
0:281d6ff68967

        

Who changed what in which revision?

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