Port of LwIP performed by Ralf in 2010. Not recommended for use with recent mbed libraries, but good demos of raw LwIP possible

Dependents:   LwIP_raw_API_serverExample tiny-dtls

Committer:
RodColeman
Date:
Tue Sep 18 14:41:24 2012 +0000
Revision:
0:0791c1fece8e
[mbed] converted /Eth_TCP_Wei_Server/lwip

Who changed what in which revision?

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