This library is stripped down version of NetServices library. HTTP server and client function is NOT supported.

Dependents:   imu-daq-eth

Committer:
idinor
Date:
Wed Jul 20 11:45:39 2011 +0000
Revision:
0:dcf3c92487ca

        

Who changed what in which revision?

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