Deprecated fork of old network stack source from github. Please use official library instead: https://mbed.org/users/mbed_official/code/EthernetInterface/

Committer:
AdamGreen
Date:
Sat Oct 26 08:51:36 2013 +0000
Revision:
1:eadc868c2acf
Parent:
0:3b00827bb0b7
Fix TCP checksum bug and stranded large TCP segments.

Who changed what in which revision?

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