This short program illustrates how to use the DS130x_I2C library. My objective is to share the same RTC with Microchip 18F MCU.

Dependencies:   mbed DebugLibrary

Committer:
Yann
Date:
Fri Feb 11 10:17:20 2011 +0000
Revision:
1:995212d326ca
Parent:
0:f30e2135b0db
V0.0.0.2

Who changed what in which revision?

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