Netservices modded to read fragmented HTTP respsonse/payload from special purpose server - 180 bytes only

Committer:
RodColeman
Date:
Thu Sep 08 10:41:36 2011 +0000
Revision:
0:8f5825f330b0
setDataLen hacked to 180bytes

Who changed what in which revision?

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