Version of http://mbed.org/cookbook/NetServicesTribute with setting set the same for LPC2368

Dependents:   UDPSocketExample 24LCxx_I2CApp WeatherPlatform_pachube HvZServerLib ... more

Committer:
simon
Date:
Tue Nov 23 14:15:36 2010 +0000
Revision:
0:350011bf8be7
Experimental version for testing UDP

Who changed what in which revision?

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