Modify changes to test TCP socket.

Dependents:   EthernetInterface

Fork of lwip by mbed official

Committer:
rebonatto
Date:
Fri Dec 13 11:39:39 2013 +0000
Revision:
12:963887732b7c
Parent:
0:51ac1d130fd4
modify;

Who changed what in which revision?

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