Modified version of NetServices. Fixes an issue where connections failed should the HTTP response status line be received in a packet on its own prior to any further headers. Changes are made to the HTTPClient.cpp file's readHeaders method.

Committer:
andrewbonney
Date:
Fri Apr 08 14:39:41 2011 +0000
Revision:
0:ec559500a63f

        

Who changed what in which revision?

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