HTTPClient using static IP

Dependencies:   mbed

Committer:
mr_q
Date:
Mon May 30 11:53:37 2011 +0000
Revision:
0:d8f2f7d5f31b
v0.01 Draft

Who changed what in which revision?

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