Leest de waarde van een sensor binnen een maakt deze beschikbaar via internet

Dependencies:   NTPClient_NetServices mbed

Committer:
hendrikvincent
Date:
Mon Dec 02 09:01:23 2013 +0000
Revision:
0:05ccbd4f84f1
eerste programma;

Who changed what in which revision?

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