Bonjour/Zerconf library

Dependencies:   mbed

Committer:
dirkx
Date:
Sat Aug 14 15:54:31 2010 +0000
Revision:
5:8e53abda9900
Parent:
0:355018f44c9f

        

Who changed what in which revision?

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