Ethernet for Nucleo and Disco board STM32F746 works with gcc and arm. IAC is untested

Dependents:   STM32F746_iothub_client_sample_mqtt DISCO-F746NG_Ethernet Nucleo_F746ZG_Ethernet thethingsiO-DISCO_F746NG-mqtt ... more

Committer:
DieterGraef
Date:
Thu Jun 23 09:04:23 2016 +0000
Revision:
1:28ba13dd96f7
Parent:
0:d26c1b55cfca
corrected MAC issue. The MAC is now 02:00:00:xx:xx:xx where xx is the sum over the unique device register

Who changed what in which revision?

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