Example self-announcing webserver which controls a servo through a smallHTML userinterface.

Dependencies:   mbed

Committer:
dirkx
Date:
Sat Aug 14 15:56:01 2010 +0000
Revision:
0:a259777c45a3

        

Who changed what in which revision?

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