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