Integrating the ublox LISA C200 modem

Fork of SprintUSBModemHTTPClientTest by Donatien Garnier

Committer:
sam_grove
Date:
Thu Sep 26 00:44:20 2013 -0500
Revision:
5:3f93dd1d4cb3
Exported program and replaced contents of the repo with the source
to build and debug using keil mdk. Libs NOT upto date are lwip, lwip-sys
and socket. these have newer versions under mbed_official but were starting
from a know working point

Who changed what in which revision?

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