A version of LWIP, provided for backwards compatibility.

Dependents:   AA_DemoBoard DemoBoard HelloServerDemo DemoBoard_RangeIndicator ... more

Committer:
root@mbed.org
Date:
Tue May 08 15:32:10 2012 +0100
Revision:
0:5e1631496985
initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
root@mbed.org 0:5e1631496985 1 /**
root@mbed.org 0:5e1631496985 2 * @file
root@mbed.org 0:5e1631496985 3 * Transmission Control Protocol, outgoing traffic
root@mbed.org 0:5e1631496985 4 *
root@mbed.org 0:5e1631496985 5 * The output functions of TCP.
root@mbed.org 0:5e1631496985 6 *
root@mbed.org 0:5e1631496985 7 */
root@mbed.org 0:5e1631496985 8
root@mbed.org 0:5e1631496985 9 /*
root@mbed.org 0:5e1631496985 10 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
root@mbed.org 0:5e1631496985 11 * All rights reserved.
root@mbed.org 0:5e1631496985 12 *
root@mbed.org 0:5e1631496985 13 * Redistribution and use in source and binary forms, with or without modification,
root@mbed.org 0:5e1631496985 14 * are permitted provided that the following conditions are met:
root@mbed.org 0:5e1631496985 15 *
root@mbed.org 0:5e1631496985 16 * 1. Redistributions of source code must retain the above copyright notice,
root@mbed.org 0:5e1631496985 17 * this list of conditions and the following disclaimer.
root@mbed.org 0:5e1631496985 18 * 2. Redistributions in binary form must reproduce the above copyright notice,
root@mbed.org 0:5e1631496985 19 * this list of conditions and the following disclaimer in the documentation
root@mbed.org 0:5e1631496985 20 * and/or other materials provided with the distribution.
root@mbed.org 0:5e1631496985 21 * 3. The name of the author may not be used to endorse or promote products
root@mbed.org 0:5e1631496985 22 * derived from this software without specific prior written permission.
root@mbed.org 0:5e1631496985 23 *
root@mbed.org 0:5e1631496985 24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
root@mbed.org 0:5e1631496985 25 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
root@mbed.org 0:5e1631496985 26 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
root@mbed.org 0:5e1631496985 27 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
root@mbed.org 0:5e1631496985 28 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
root@mbed.org 0:5e1631496985 29 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
root@mbed.org 0:5e1631496985 30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
root@mbed.org 0:5e1631496985 31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
root@mbed.org 0:5e1631496985 32 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
root@mbed.org 0:5e1631496985 33 * OF SUCH DAMAGE.
root@mbed.org 0:5e1631496985 34 *
root@mbed.org 0:5e1631496985 35 * This file is part of the lwIP TCP/IP stack.
root@mbed.org 0:5e1631496985 36 *
root@mbed.org 0:5e1631496985 37 * Author: Adam Dunkels <adam@sics.se>
root@mbed.org 0:5e1631496985 38 *
root@mbed.org 0:5e1631496985 39 */
root@mbed.org 0:5e1631496985 40
root@mbed.org 0:5e1631496985 41 #include "lwip/opt.h"
root@mbed.org 0:5e1631496985 42
root@mbed.org 0:5e1631496985 43 #if LWIP_TCP /* don't build if not configured for use in lwipopts.h */
root@mbed.org 0:5e1631496985 44
root@mbed.org 0:5e1631496985 45 #include "lwip/tcp.h"
root@mbed.org 0:5e1631496985 46 #include "lwip/def.h"
root@mbed.org 0:5e1631496985 47 #include "lwip/mem.h"
root@mbed.org 0:5e1631496985 48 #include "lwip/memp.h"
root@mbed.org 0:5e1631496985 49 #include "lwip/sys.h"
root@mbed.org 0:5e1631496985 50 #include "lwip/ip_addr.h"
root@mbed.org 0:5e1631496985 51 #include "lwip/netif.h"
root@mbed.org 0:5e1631496985 52 #include "lwip/inet.h"
root@mbed.org 0:5e1631496985 53 #include "lwip/inet_chksum.h"
root@mbed.org 0:5e1631496985 54 #include "lwip/stats.h"
root@mbed.org 0:5e1631496985 55 #include "lwip/snmp.h"
root@mbed.org 0:5e1631496985 56
root@mbed.org 0:5e1631496985 57 #include <string.h>
root@mbed.org 0:5e1631496985 58
root@mbed.org 0:5e1631496985 59 /* Forward declarations.*/
root@mbed.org 0:5e1631496985 60 static void tcp_output_segment(struct tcp_seg *seg, struct tcp_pcb *pcb);
root@mbed.org 0:5e1631496985 61
root@mbed.org 0:5e1631496985 62 static struct tcp_hdr *
root@mbed.org 0:5e1631496985 63 tcp_output_set_header(struct tcp_pcb *pcb, struct pbuf *p, int optlen,
root@mbed.org 0:5e1631496985 64 u32_t seqno_be /* already in network byte order */)
root@mbed.org 0:5e1631496985 65 {
root@mbed.org 0:5e1631496985 66 struct tcp_hdr *tcphdr = (struct tcp_hdr *)p->payload;
root@mbed.org 0:5e1631496985 67 tcphdr->src = htons(pcb->local_port);
root@mbed.org 0:5e1631496985 68 tcphdr->dest = htons(pcb->remote_port);
root@mbed.org 0:5e1631496985 69 tcphdr->seqno = seqno_be;
root@mbed.org 0:5e1631496985 70 tcphdr->ackno = htonl(pcb->rcv_nxt);
root@mbed.org 0:5e1631496985 71 TCPH_FLAGS_SET(tcphdr, TCP_ACK);
root@mbed.org 0:5e1631496985 72 tcphdr->wnd = htons(pcb->rcv_ann_wnd);
root@mbed.org 0:5e1631496985 73 tcphdr->urgp = 0;
root@mbed.org 0:5e1631496985 74 TCPH_HDRLEN_SET(tcphdr, (5 + optlen / 4));
root@mbed.org 0:5e1631496985 75 tcphdr->chksum = 0;
root@mbed.org 0:5e1631496985 76
root@mbed.org 0:5e1631496985 77 /* If we're sending a packet, update the announced right window edge */
root@mbed.org 0:5e1631496985 78 pcb->rcv_ann_right_edge = pcb->rcv_nxt + pcb->rcv_ann_wnd;
root@mbed.org 0:5e1631496985 79
root@mbed.org 0:5e1631496985 80 return tcphdr;
root@mbed.org 0:5e1631496985 81 }
root@mbed.org 0:5e1631496985 82
root@mbed.org 0:5e1631496985 83 /**
root@mbed.org 0:5e1631496985 84 * Called by tcp_close() to send a segment including flags but not data.
root@mbed.org 0:5e1631496985 85 *
root@mbed.org 0:5e1631496985 86 * @param pcb the tcp_pcb over which to send a segment
root@mbed.org 0:5e1631496985 87 * @param flags the flags to set in the segment header
root@mbed.org 0:5e1631496985 88 * @return ERR_OK if sent, another err_t otherwise
root@mbed.org 0:5e1631496985 89 */
root@mbed.org 0:5e1631496985 90 err_t
root@mbed.org 0:5e1631496985 91 tcp_send_ctrl(struct tcp_pcb *pcb, u8_t flags)
root@mbed.org 0:5e1631496985 92 {
root@mbed.org 0:5e1631496985 93 /* no data, no length, flags, copy=1, no optdata */
root@mbed.org 0:5e1631496985 94 return tcp_enqueue(pcb, NULL, 0, flags, TCP_WRITE_FLAG_COPY, 0);
root@mbed.org 0:5e1631496985 95 }
root@mbed.org 0:5e1631496985 96
root@mbed.org 0:5e1631496985 97 /**
root@mbed.org 0:5e1631496985 98 * Write data for sending (but does not send it immediately).
root@mbed.org 0:5e1631496985 99 *
root@mbed.org 0:5e1631496985 100 * It waits in the expectation of more data being sent soon (as
root@mbed.org 0:5e1631496985 101 * it can send them more efficiently by combining them together).
root@mbed.org 0:5e1631496985 102 * To prompt the system to send data now, call tcp_output() after
root@mbed.org 0:5e1631496985 103 * calling tcp_write().
root@mbed.org 0:5e1631496985 104 *
root@mbed.org 0:5e1631496985 105 * @param pcb Protocol control block of the TCP connection to enqueue data for.
root@mbed.org 0:5e1631496985 106 * @param data pointer to the data to send
root@mbed.org 0:5e1631496985 107 * @param len length (in bytes) of the data to send
root@mbed.org 0:5e1631496985 108 * @param apiflags combination of following flags :
root@mbed.org 0:5e1631496985 109 * - TCP_WRITE_FLAG_COPY (0x01) data will be copied into memory belonging to the stack
root@mbed.org 0:5e1631496985 110 * - TCP_WRITE_FLAG_MORE (0x02) for TCP connection, PSH flag will be set on last segment sent,
root@mbed.org 0:5e1631496985 111 * @return ERR_OK if enqueued, another err_t on error
root@mbed.org 0:5e1631496985 112 *
root@mbed.org 0:5e1631496985 113 * @see tcp_write()
root@mbed.org 0:5e1631496985 114 */
root@mbed.org 0:5e1631496985 115 err_t
root@mbed.org 0:5e1631496985 116 tcp_write(struct tcp_pcb *pcb, const void *data, u16_t len, u8_t apiflags)
root@mbed.org 0:5e1631496985 117 {
root@mbed.org 0:5e1631496985 118 LWIP_DEBUGF(TCP_OUTPUT_DEBUG, ("tcp_write(pcb=%p, data=%p, len=%"U16_F", apiflags=%"U16_F")\n", (void *)pcb,
root@mbed.org 0:5e1631496985 119 data, len, (u16_t)apiflags));
root@mbed.org 0:5e1631496985 120 /* connection is in valid state for data transmission? */
root@mbed.org 0:5e1631496985 121 if (pcb->state == ESTABLISHED ||
root@mbed.org 0:5e1631496985 122 pcb->state == CLOSE_WAIT ||
root@mbed.org 0:5e1631496985 123 pcb->state == SYN_SENT ||
root@mbed.org 0:5e1631496985 124 pcb->state == SYN_RCVD) {
root@mbed.org 0:5e1631496985 125 if (len > 0) {
root@mbed.org 0:5e1631496985 126 #if LWIP_TCP_TIMESTAMPS
root@mbed.org 0:5e1631496985 127 return tcp_enqueue(pcb, (void *)data, len, 0, apiflags,
root@mbed.org 0:5e1631496985 128 pcb->flags & TF_TIMESTAMP ? TF_SEG_OPTS_TS : 0);
root@mbed.org 0:5e1631496985 129 #else
root@mbed.org 0:5e1631496985 130 return tcp_enqueue(pcb, (void *)data, len, 0, apiflags, 0);
root@mbed.org 0:5e1631496985 131 #endif
root@mbed.org 0:5e1631496985 132 }
root@mbed.org 0:5e1631496985 133 return ERR_OK;
root@mbed.org 0:5e1631496985 134 } else {
root@mbed.org 0:5e1631496985 135 LWIP_DEBUGF(TCP_OUTPUT_DEBUG | LWIP_DBG_STATE | 3, ("tcp_write() called in invalid state\n"));
root@mbed.org 0:5e1631496985 136 return ERR_CONN;
root@mbed.org 0:5e1631496985 137 }
root@mbed.org 0:5e1631496985 138 }
root@mbed.org 0:5e1631496985 139
root@mbed.org 0:5e1631496985 140 /**
root@mbed.org 0:5e1631496985 141 * Enqueue data and/or TCP options for transmission
root@mbed.org 0:5e1631496985 142 *
root@mbed.org 0:5e1631496985 143 * Called by tcp_connect(), tcp_listen_input(), tcp_send_ctrl() and tcp_write().
root@mbed.org 0:5e1631496985 144 *
root@mbed.org 0:5e1631496985 145 * @param pcb Protocol control block for the TCP connection to enqueue data for.
root@mbed.org 0:5e1631496985 146 * @param arg Pointer to the data to be enqueued for sending.
root@mbed.org 0:5e1631496985 147 * @param len Data length in bytes
root@mbed.org 0:5e1631496985 148 * @param flags tcp header flags to set in the outgoing segment
root@mbed.org 0:5e1631496985 149 * @param apiflags combination of following flags :
root@mbed.org 0:5e1631496985 150 * - TCP_WRITE_FLAG_COPY (0x01) data will be copied into memory belonging to the stack
root@mbed.org 0:5e1631496985 151 * - TCP_WRITE_FLAG_MORE (0x02) for TCP connection, PSH flag will be set on last segment sent,
root@mbed.org 0:5e1631496985 152 * @param optflags options to include in segment later on (see definition of struct tcp_seg)
root@mbed.org 0:5e1631496985 153 */
root@mbed.org 0:5e1631496985 154 err_t
root@mbed.org 0:5e1631496985 155 tcp_enqueue(struct tcp_pcb *pcb, void *arg, u16_t len,
root@mbed.org 0:5e1631496985 156 u8_t flags, u8_t apiflags, u8_t optflags)
root@mbed.org 0:5e1631496985 157 {
root@mbed.org 0:5e1631496985 158 struct pbuf *p;
root@mbed.org 0:5e1631496985 159 struct tcp_seg *seg, *useg, *queue;
root@mbed.org 0:5e1631496985 160 u32_t seqno;
root@mbed.org 0:5e1631496985 161 u16_t left, seglen;
root@mbed.org 0:5e1631496985 162 void *ptr;
root@mbed.org 0:5e1631496985 163 u16_t queuelen;
root@mbed.org 0:5e1631496985 164 u8_t optlen;
root@mbed.org 0:5e1631496985 165
root@mbed.org 0:5e1631496985 166 LWIP_DEBUGF(TCP_OUTPUT_DEBUG,
root@mbed.org 0:5e1631496985 167 ("tcp_enqueue(pcb=%p, arg=%p, len=%"U16_F", flags=%"X16_F", apiflags=%"U16_F")\n",
root@mbed.org 0:5e1631496985 168 (void *)pcb, arg, len, (u16_t)flags, (u16_t)apiflags));
root@mbed.org 0:5e1631496985 169 LWIP_ERROR("tcp_enqueue: packet needs payload, options, or SYN/FIN (programmer violates API)",
root@mbed.org 0:5e1631496985 170 ((len != 0) || (optflags != 0) || ((flags & (TCP_SYN | TCP_FIN)) != 0)),
root@mbed.org 0:5e1631496985 171 return ERR_ARG;);
root@mbed.org 0:5e1631496985 172 LWIP_ERROR("tcp_enqueue: len != 0 || arg == NULL (programmer violates API)",
root@mbed.org 0:5e1631496985 173 ((len != 0) || (arg == NULL)), return ERR_ARG;);
root@mbed.org 0:5e1631496985 174
root@mbed.org 0:5e1631496985 175 /* fail on too much data */
root@mbed.org 0:5e1631496985 176 if (len > pcb->snd_buf) {
root@mbed.org 0:5e1631496985 177 LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 3, ("tcp_enqueue: too much data (len=%"U16_F" > snd_buf=%"U16_F")\n", len, pcb->snd_buf));
root@mbed.org 0:5e1631496985 178 pcb->flags |= TF_NAGLEMEMERR;
root@mbed.org 0:5e1631496985 179 return ERR_MEM;
root@mbed.org 0:5e1631496985 180 }
root@mbed.org 0:5e1631496985 181 left = len;
root@mbed.org 0:5e1631496985 182 ptr = arg;
root@mbed.org 0:5e1631496985 183
root@mbed.org 0:5e1631496985 184 optlen = LWIP_TCP_OPT_LENGTH(optflags);
root@mbed.org 0:5e1631496985 185
root@mbed.org 0:5e1631496985 186 /* seqno will be the sequence number of the first segment enqueued
root@mbed.org 0:5e1631496985 187 * by the call to this function. */
root@mbed.org 0:5e1631496985 188 seqno = pcb->snd_lbb;
root@mbed.org 0:5e1631496985 189
root@mbed.org 0:5e1631496985 190 LWIP_DEBUGF(TCP_QLEN_DEBUG, ("tcp_enqueue: queuelen: %"U16_F"\n", (u16_t)pcb->snd_queuelen));
root@mbed.org 0:5e1631496985 191
root@mbed.org 0:5e1631496985 192 /* If total number of pbufs on the unsent/unacked queues exceeds the
root@mbed.org 0:5e1631496985 193 * configured maximum, return an error */
root@mbed.org 0:5e1631496985 194 queuelen = pcb->snd_queuelen;
root@mbed.org 0:5e1631496985 195 /* check for configured max queuelen and possible overflow */
root@mbed.org 0:5e1631496985 196 if ((queuelen >= TCP_SND_QUEUELEN) || (queuelen > TCP_SNDQUEUELEN_OVERFLOW)) {
root@mbed.org 0:5e1631496985 197 LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 3, ("tcp_enqueue: too long queue %"U16_F" (max %"U16_F")\n", queuelen, TCP_SND_QUEUELEN));
root@mbed.org 0:5e1631496985 198 TCP_STATS_INC(tcp.memerr);
root@mbed.org 0:5e1631496985 199 pcb->flags |= TF_NAGLEMEMERR;
root@mbed.org 0:5e1631496985 200 return ERR_MEM;
root@mbed.org 0:5e1631496985 201 }
root@mbed.org 0:5e1631496985 202 if (queuelen != 0) {
root@mbed.org 0:5e1631496985 203 LWIP_ASSERT("tcp_enqueue: pbufs on queue => at least one queue non-empty",
root@mbed.org 0:5e1631496985 204 pcb->unacked != NULL || pcb->unsent != NULL);
root@mbed.org 0:5e1631496985 205 } else {
root@mbed.org 0:5e1631496985 206 LWIP_ASSERT("tcp_enqueue: no pbufs on queue => both queues empty",
root@mbed.org 0:5e1631496985 207 pcb->unacked == NULL && pcb->unsent == NULL);
root@mbed.org 0:5e1631496985 208 }
root@mbed.org 0:5e1631496985 209
root@mbed.org 0:5e1631496985 210 /* First, break up the data into segments and tuck them together in
root@mbed.org 0:5e1631496985 211 * the local "queue" variable. */
root@mbed.org 0:5e1631496985 212 useg = queue = seg = NULL;
root@mbed.org 0:5e1631496985 213 seglen = 0;
root@mbed.org 0:5e1631496985 214 while (queue == NULL || left > 0) {
root@mbed.org 0:5e1631496985 215 /* The segment length (including options) should be at most the MSS */
root@mbed.org 0:5e1631496985 216 seglen = left > (pcb->mss - optlen) ? (pcb->mss - optlen) : left;
root@mbed.org 0:5e1631496985 217
root@mbed.org 0:5e1631496985 218 /* Allocate memory for tcp_seg, and fill in fields. */
root@mbed.org 0:5e1631496985 219 seg = (struct tcp_seg *)memp_malloc(MEMP_TCP_SEG);
root@mbed.org 0:5e1631496985 220 if (seg == NULL) {
root@mbed.org 0:5e1631496985 221 LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 2,
root@mbed.org 0:5e1631496985 222 ("tcp_enqueue: could not allocate memory for tcp_seg\n"));
root@mbed.org 0:5e1631496985 223 goto memerr;
root@mbed.org 0:5e1631496985 224 }
root@mbed.org 0:5e1631496985 225 seg->next = NULL;
root@mbed.org 0:5e1631496985 226 seg->p = NULL;
root@mbed.org 0:5e1631496985 227
root@mbed.org 0:5e1631496985 228 /* first segment of to-be-queued data? */
root@mbed.org 0:5e1631496985 229 if (queue == NULL) {
root@mbed.org 0:5e1631496985 230 queue = seg;
root@mbed.org 0:5e1631496985 231 }
root@mbed.org 0:5e1631496985 232 /* subsequent segments of to-be-queued data */
root@mbed.org 0:5e1631496985 233 else {
root@mbed.org 0:5e1631496985 234 /* Attach the segment to the end of the queued segments */
root@mbed.org 0:5e1631496985 235 LWIP_ASSERT("useg != NULL", useg != NULL);
root@mbed.org 0:5e1631496985 236 useg->next = seg;
root@mbed.org 0:5e1631496985 237 }
root@mbed.org 0:5e1631496985 238 /* remember last segment of to-be-queued data for next iteration */
root@mbed.org 0:5e1631496985 239 useg = seg;
root@mbed.org 0:5e1631496985 240
root@mbed.org 0:5e1631496985 241 /* If copy is set, memory should be allocated
root@mbed.org 0:5e1631496985 242 * and data copied into pbuf, otherwise data comes from
root@mbed.org 0:5e1631496985 243 * ROM or other static memory, and need not be copied. */
root@mbed.org 0:5e1631496985 244 if (apiflags & TCP_WRITE_FLAG_COPY) {
root@mbed.org 0:5e1631496985 245 if ((seg->p = pbuf_alloc(PBUF_TRANSPORT, seglen + optlen, PBUF_RAM)) == NULL) {
root@mbed.org 0:5e1631496985 246 LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 2,
root@mbed.org 0:5e1631496985 247 ("tcp_enqueue : could not allocate memory for pbuf copy size %"U16_F"\n", seglen));
root@mbed.org 0:5e1631496985 248 goto memerr;
root@mbed.org 0:5e1631496985 249 }
root@mbed.org 0:5e1631496985 250 LWIP_ASSERT("check that first pbuf can hold the complete seglen",
root@mbed.org 0:5e1631496985 251 (seg->p->len >= seglen + optlen));
root@mbed.org 0:5e1631496985 252 queuelen += pbuf_clen(seg->p);
root@mbed.org 0:5e1631496985 253 if (arg != NULL) {
root@mbed.org 0:5e1631496985 254 MEMCPY((char *)seg->p->payload + optlen, ptr, seglen);
root@mbed.org 0:5e1631496985 255 }
root@mbed.org 0:5e1631496985 256 seg->dataptr = seg->p->payload;
root@mbed.org 0:5e1631496985 257 }
root@mbed.org 0:5e1631496985 258 /* do not copy data */
root@mbed.org 0:5e1631496985 259 else {
root@mbed.org 0:5e1631496985 260 /* First, allocate a pbuf for the headers. */
root@mbed.org 0:5e1631496985 261 if ((seg->p = pbuf_alloc(PBUF_TRANSPORT, optlen, PBUF_RAM)) == NULL) {
root@mbed.org 0:5e1631496985 262 LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 2,
root@mbed.org 0:5e1631496985 263 ("tcp_enqueue: could not allocate memory for header pbuf\n"));
root@mbed.org 0:5e1631496985 264 goto memerr;
root@mbed.org 0:5e1631496985 265 }
root@mbed.org 0:5e1631496985 266 queuelen += pbuf_clen(seg->p);
root@mbed.org 0:5e1631496985 267
root@mbed.org 0:5e1631496985 268 /* Second, allocate a pbuf for holding the data.
root@mbed.org 0:5e1631496985 269 * since the referenced data is available at least until it is sent out on the
root@mbed.org 0:5e1631496985 270 * link (as it has to be ACKed by the remote party) we can safely use PBUF_ROM
root@mbed.org 0:5e1631496985 271 * instead of PBUF_REF here.
root@mbed.org 0:5e1631496985 272 */
root@mbed.org 0:5e1631496985 273 if (left > 0) {
root@mbed.org 0:5e1631496985 274 if ((p = pbuf_alloc(PBUF_RAW, seglen, PBUF_ROM)) == NULL) {
root@mbed.org 0:5e1631496985 275 /* If allocation fails, we have to deallocate the header pbuf as well. */
root@mbed.org 0:5e1631496985 276 pbuf_free(seg->p);
root@mbed.org 0:5e1631496985 277 seg->p = NULL;
root@mbed.org 0:5e1631496985 278 LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 2,
root@mbed.org 0:5e1631496985 279 ("tcp_enqueue: could not allocate memory for zero-copy pbuf\n"));
root@mbed.org 0:5e1631496985 280 goto memerr;
root@mbed.org 0:5e1631496985 281 }
root@mbed.org 0:5e1631496985 282 ++queuelen;
root@mbed.org 0:5e1631496985 283 /* reference the non-volatile payload data */
root@mbed.org 0:5e1631496985 284 p->payload = ptr;
root@mbed.org 0:5e1631496985 285 seg->dataptr = ptr;
root@mbed.org 0:5e1631496985 286
root@mbed.org 0:5e1631496985 287 /* Concatenate the headers and data pbufs together. */
root@mbed.org 0:5e1631496985 288 pbuf_cat(seg->p/*header*/, p/*data*/);
root@mbed.org 0:5e1631496985 289 p = NULL;
root@mbed.org 0:5e1631496985 290 }
root@mbed.org 0:5e1631496985 291 }
root@mbed.org 0:5e1631496985 292
root@mbed.org 0:5e1631496985 293 /* Now that there are more segments queued, we check again if the
root@mbed.org 0:5e1631496985 294 length of the queue exceeds the configured maximum or overflows. */
root@mbed.org 0:5e1631496985 295 if ((queuelen > TCP_SND_QUEUELEN) || (queuelen > TCP_SNDQUEUELEN_OVERFLOW)) {
root@mbed.org 0:5e1631496985 296 LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 2, ("tcp_enqueue: queue too long %"U16_F" (%"U16_F")\n", queuelen, TCP_SND_QUEUELEN));
root@mbed.org 0:5e1631496985 297 goto memerr;
root@mbed.org 0:5e1631496985 298 }
root@mbed.org 0:5e1631496985 299
root@mbed.org 0:5e1631496985 300 seg->len = seglen;
root@mbed.org 0:5e1631496985 301
root@mbed.org 0:5e1631496985 302 /* build TCP header */
root@mbed.org 0:5e1631496985 303 if (pbuf_header(seg->p, TCP_HLEN)) {
root@mbed.org 0:5e1631496985 304 LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 2, ("tcp_enqueue: no room for TCP header in pbuf.\n"));
root@mbed.org 0:5e1631496985 305 TCP_STATS_INC(tcp.err);
root@mbed.org 0:5e1631496985 306 goto memerr;
root@mbed.org 0:5e1631496985 307 }
root@mbed.org 0:5e1631496985 308 seg->tcphdr = (struct tcp_hdr *)seg->p->payload;
root@mbed.org 0:5e1631496985 309 seg->tcphdr->src = htons(pcb->local_port);
root@mbed.org 0:5e1631496985 310 seg->tcphdr->dest = htons(pcb->remote_port);
root@mbed.org 0:5e1631496985 311 seg->tcphdr->seqno = htonl(seqno);
root@mbed.org 0:5e1631496985 312 seg->tcphdr->urgp = 0;
root@mbed.org 0:5e1631496985 313 TCPH_FLAGS_SET(seg->tcphdr, flags);
root@mbed.org 0:5e1631496985 314 /* don't fill in tcphdr->ackno and tcphdr->wnd until later */
root@mbed.org 0:5e1631496985 315
root@mbed.org 0:5e1631496985 316 seg->flags = optflags;
root@mbed.org 0:5e1631496985 317
root@mbed.org 0:5e1631496985 318 /* Set the length of the header */
root@mbed.org 0:5e1631496985 319 TCPH_HDRLEN_SET(seg->tcphdr, (5 + optlen / 4));
root@mbed.org 0:5e1631496985 320 LWIP_DEBUGF(TCP_OUTPUT_DEBUG | LWIP_DBG_TRACE, ("tcp_enqueue: queueing %"U32_F":%"U32_F" (0x%"X16_F")\n",
root@mbed.org 0:5e1631496985 321 ntohl(seg->tcphdr->seqno),
root@mbed.org 0:5e1631496985 322 ntohl(seg->tcphdr->seqno) + TCP_TCPLEN(seg),
root@mbed.org 0:5e1631496985 323 (u16_t)flags));
root@mbed.org 0:5e1631496985 324
root@mbed.org 0:5e1631496985 325 left -= seglen;
root@mbed.org 0:5e1631496985 326 seqno += seglen;
root@mbed.org 0:5e1631496985 327 ptr = (void *)((u8_t *)ptr + seglen);
root@mbed.org 0:5e1631496985 328 }
root@mbed.org 0:5e1631496985 329
root@mbed.org 0:5e1631496985 330 /* Now that the data to be enqueued has been broken up into TCP
root@mbed.org 0:5e1631496985 331 segments in the queue variable, we add them to the end of the
root@mbed.org 0:5e1631496985 332 pcb->unsent queue. */
root@mbed.org 0:5e1631496985 333 if (pcb->unsent == NULL) {
root@mbed.org 0:5e1631496985 334 useg = NULL;
root@mbed.org 0:5e1631496985 335 }
root@mbed.org 0:5e1631496985 336 else {
root@mbed.org 0:5e1631496985 337 for (useg = pcb->unsent; useg->next != NULL; useg = useg->next);
root@mbed.org 0:5e1631496985 338 }
root@mbed.org 0:5e1631496985 339 /* { useg is last segment on the unsent queue, NULL if list is empty } */
root@mbed.org 0:5e1631496985 340
root@mbed.org 0:5e1631496985 341 /* If there is room in the last pbuf on the unsent queue,
root@mbed.org 0:5e1631496985 342 chain the first pbuf on the queue together with that. */
root@mbed.org 0:5e1631496985 343 if (useg != NULL &&
root@mbed.org 0:5e1631496985 344 TCP_TCPLEN(useg) != 0 &&
root@mbed.org 0:5e1631496985 345 !(TCPH_FLAGS(useg->tcphdr) & (TCP_SYN | TCP_FIN)) &&
root@mbed.org 0:5e1631496985 346 !(flags & (TCP_SYN | TCP_FIN)) &&
root@mbed.org 0:5e1631496985 347 /* fit within max seg size */
root@mbed.org 0:5e1631496985 348 (useg->len + queue->len <= pcb->mss) &&
root@mbed.org 0:5e1631496985 349 /* only concatenate segments with the same options */
root@mbed.org 0:5e1631496985 350 (useg->flags == queue->flags) &&
root@mbed.org 0:5e1631496985 351 /* segments are consecutive */
root@mbed.org 0:5e1631496985 352 (ntohl(useg->tcphdr->seqno) + useg->len == ntohl(queue->tcphdr->seqno)) ) {
root@mbed.org 0:5e1631496985 353 /* Remove TCP header from first segment of our to-be-queued list */
root@mbed.org 0:5e1631496985 354 if(pbuf_header(queue->p, -(TCP_HLEN + optlen))) {
root@mbed.org 0:5e1631496985 355 /* Can we cope with this failing? Just assert for now */
root@mbed.org 0:5e1631496985 356 LWIP_ASSERT("pbuf_header failed\n", 0);
root@mbed.org 0:5e1631496985 357 TCP_STATS_INC(tcp.err);
root@mbed.org 0:5e1631496985 358 goto memerr;
root@mbed.org 0:5e1631496985 359 }
root@mbed.org 0:5e1631496985 360 if (queue->p->len == 0) {
root@mbed.org 0:5e1631496985 361 /* free the first (header-only) pbuf if it is now empty (contained only headers) */
root@mbed.org 0:5e1631496985 362 struct pbuf *old_q = queue->p;
root@mbed.org 0:5e1631496985 363 queue->p = queue->p->next;
root@mbed.org 0:5e1631496985 364 old_q->next = NULL;
root@mbed.org 0:5e1631496985 365 queuelen--;
root@mbed.org 0:5e1631496985 366 pbuf_free(old_q);
root@mbed.org 0:5e1631496985 367 }
root@mbed.org 0:5e1631496985 368 LWIP_ASSERT("zero-length pbuf", (queue->p != NULL) && (queue->p->len > 0));
root@mbed.org 0:5e1631496985 369 pbuf_cat(useg->p, queue->p);
root@mbed.org 0:5e1631496985 370 useg->len += queue->len;
root@mbed.org 0:5e1631496985 371 useg->next = queue->next;
root@mbed.org 0:5e1631496985 372
root@mbed.org 0:5e1631496985 373 LWIP_DEBUGF(TCP_OUTPUT_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("tcp_enqueue: chaining segments, new len %"U16_F"\n", useg->len));
root@mbed.org 0:5e1631496985 374 if (seg == queue) {
root@mbed.org 0:5e1631496985 375 seg = useg;
root@mbed.org 0:5e1631496985 376 seglen = useg->len;
root@mbed.org 0:5e1631496985 377 }
root@mbed.org 0:5e1631496985 378 memp_free(MEMP_TCP_SEG, queue);
root@mbed.org 0:5e1631496985 379 }
root@mbed.org 0:5e1631496985 380 else {
root@mbed.org 0:5e1631496985 381 /* empty list */
root@mbed.org 0:5e1631496985 382 if (useg == NULL) {
root@mbed.org 0:5e1631496985 383 /* initialize list with this segment */
root@mbed.org 0:5e1631496985 384 pcb->unsent = queue;
root@mbed.org 0:5e1631496985 385 }
root@mbed.org 0:5e1631496985 386 /* enqueue segment */
root@mbed.org 0:5e1631496985 387 else {
root@mbed.org 0:5e1631496985 388 useg->next = queue;
root@mbed.org 0:5e1631496985 389 }
root@mbed.org 0:5e1631496985 390 }
root@mbed.org 0:5e1631496985 391 if ((flags & TCP_SYN) || (flags & TCP_FIN)) {
root@mbed.org 0:5e1631496985 392 ++len;
root@mbed.org 0:5e1631496985 393 }
root@mbed.org 0:5e1631496985 394 if (flags & TCP_FIN) {
root@mbed.org 0:5e1631496985 395 pcb->flags |= TF_FIN;
root@mbed.org 0:5e1631496985 396 }
root@mbed.org 0:5e1631496985 397 pcb->snd_lbb += len;
root@mbed.org 0:5e1631496985 398
root@mbed.org 0:5e1631496985 399 pcb->snd_buf -= len;
root@mbed.org 0:5e1631496985 400
root@mbed.org 0:5e1631496985 401 /* update number of segments on the queues */
root@mbed.org 0:5e1631496985 402 pcb->snd_queuelen = queuelen;
root@mbed.org 0:5e1631496985 403 LWIP_DEBUGF(TCP_QLEN_DEBUG, ("tcp_enqueue: %"S16_F" (after enqueued)\n", pcb->snd_queuelen));
root@mbed.org 0:5e1631496985 404 if (pcb->snd_queuelen != 0) {
root@mbed.org 0:5e1631496985 405 LWIP_ASSERT("tcp_enqueue: valid queue length",
root@mbed.org 0:5e1631496985 406 pcb->unacked != NULL || pcb->unsent != NULL);
root@mbed.org 0:5e1631496985 407 }
root@mbed.org 0:5e1631496985 408
root@mbed.org 0:5e1631496985 409 /* Set the PSH flag in the last segment that we enqueued, but only
root@mbed.org 0:5e1631496985 410 if the segment has data (indicated by seglen > 0). */
root@mbed.org 0:5e1631496985 411 if (seg != NULL && seglen > 0 && seg->tcphdr != NULL && ((apiflags & TCP_WRITE_FLAG_MORE)==0)) {
root@mbed.org 0:5e1631496985 412 TCPH_SET_FLAG(seg->tcphdr, TCP_PSH);
root@mbed.org 0:5e1631496985 413 }
root@mbed.org 0:5e1631496985 414
root@mbed.org 0:5e1631496985 415 return ERR_OK;
root@mbed.org 0:5e1631496985 416 memerr:
root@mbed.org 0:5e1631496985 417 pcb->flags |= TF_NAGLEMEMERR;
root@mbed.org 0:5e1631496985 418 TCP_STATS_INC(tcp.memerr);
root@mbed.org 0:5e1631496985 419
root@mbed.org 0:5e1631496985 420 if (queue != NULL) {
root@mbed.org 0:5e1631496985 421 tcp_segs_free(queue);
root@mbed.org 0:5e1631496985 422 }
root@mbed.org 0:5e1631496985 423 if (pcb->snd_queuelen != 0) {
root@mbed.org 0:5e1631496985 424 LWIP_ASSERT("tcp_enqueue: valid queue length", pcb->unacked != NULL ||
root@mbed.org 0:5e1631496985 425 pcb->unsent != NULL);
root@mbed.org 0:5e1631496985 426 }
root@mbed.org 0:5e1631496985 427 LWIP_DEBUGF(TCP_QLEN_DEBUG | LWIP_DBG_STATE, ("tcp_enqueue: %"S16_F" (with mem err)\n", pcb->snd_queuelen));
root@mbed.org 0:5e1631496985 428 return ERR_MEM;
root@mbed.org 0:5e1631496985 429 }
root@mbed.org 0:5e1631496985 430
root@mbed.org 0:5e1631496985 431
root@mbed.org 0:5e1631496985 432 #if LWIP_TCP_TIMESTAMPS
root@mbed.org 0:5e1631496985 433 /* Build a timestamp option (12 bytes long) at the specified options pointer)
root@mbed.org 0:5e1631496985 434 *
root@mbed.org 0:5e1631496985 435 * @param pcb tcp_pcb
root@mbed.org 0:5e1631496985 436 * @param opts option pointer where to store the timestamp option
root@mbed.org 0:5e1631496985 437 */
root@mbed.org 0:5e1631496985 438 static void
root@mbed.org 0:5e1631496985 439 tcp_build_timestamp_option(struct tcp_pcb *pcb, u32_t *opts)
root@mbed.org 0:5e1631496985 440 {
root@mbed.org 0:5e1631496985 441 /* Pad with two NOP options to make everything nicely aligned */
root@mbed.org 0:5e1631496985 442 opts[0] = htonl(0x0101080A);
root@mbed.org 0:5e1631496985 443 opts[1] = htonl(sys_now());
root@mbed.org 0:5e1631496985 444 opts[2] = htonl(pcb->ts_recent);
root@mbed.org 0:5e1631496985 445 }
root@mbed.org 0:5e1631496985 446 #endif
root@mbed.org 0:5e1631496985 447
root@mbed.org 0:5e1631496985 448
root@mbed.org 0:5e1631496985 449 /**
root@mbed.org 0:5e1631496985 450 * Find out what we can send and send it
root@mbed.org 0:5e1631496985 451 *
root@mbed.org 0:5e1631496985 452 * @param pcb Protocol control block for the TCP connection to send data
root@mbed.org 0:5e1631496985 453 * @return ERR_OK if data has been sent or nothing to send
root@mbed.org 0:5e1631496985 454 * another err_t on error
root@mbed.org 0:5e1631496985 455 */
root@mbed.org 0:5e1631496985 456 err_t
root@mbed.org 0:5e1631496985 457 tcp_output(struct tcp_pcb *pcb)
root@mbed.org 0:5e1631496985 458 {
root@mbed.org 0:5e1631496985 459 struct pbuf *p;
root@mbed.org 0:5e1631496985 460 struct tcp_hdr *tcphdr;
root@mbed.org 0:5e1631496985 461 struct tcp_seg *seg, *useg;
root@mbed.org 0:5e1631496985 462 u32_t wnd, snd_nxt;
root@mbed.org 0:5e1631496985 463 #if TCP_CWND_DEBUG
root@mbed.org 0:5e1631496985 464 s16_t i = 0;
root@mbed.org 0:5e1631496985 465 #endif /* TCP_CWND_DEBUG */
root@mbed.org 0:5e1631496985 466 u8_t optlen = 0;
root@mbed.org 0:5e1631496985 467
root@mbed.org 0:5e1631496985 468 /* First, check if we are invoked by the TCP input processing
root@mbed.org 0:5e1631496985 469 code. If so, we do not output anything. Instead, we rely on the
root@mbed.org 0:5e1631496985 470 input processing code to call us when input processing is done
root@mbed.org 0:5e1631496985 471 with. */
root@mbed.org 0:5e1631496985 472 if (tcp_input_pcb == pcb) {
root@mbed.org 0:5e1631496985 473 return ERR_OK;
root@mbed.org 0:5e1631496985 474 }
root@mbed.org 0:5e1631496985 475
root@mbed.org 0:5e1631496985 476 wnd = LWIP_MIN(pcb->snd_wnd, pcb->cwnd);
root@mbed.org 0:5e1631496985 477
root@mbed.org 0:5e1631496985 478 seg = pcb->unsent;
root@mbed.org 0:5e1631496985 479
root@mbed.org 0:5e1631496985 480 /* useg should point to last segment on unacked queue */
root@mbed.org 0:5e1631496985 481 useg = pcb->unacked;
root@mbed.org 0:5e1631496985 482 if (useg != NULL) {
root@mbed.org 0:5e1631496985 483 for (; useg->next != NULL; useg = useg->next);
root@mbed.org 0:5e1631496985 484 }
root@mbed.org 0:5e1631496985 485
root@mbed.org 0:5e1631496985 486 /* If the TF_ACK_NOW flag is set and no data will be sent (either
root@mbed.org 0:5e1631496985 487 * because the ->unsent queue is empty or because the window does
root@mbed.org 0:5e1631496985 488 * not allow it), construct an empty ACK segment and send it.
root@mbed.org 0:5e1631496985 489 *
root@mbed.org 0:5e1631496985 490 * If data is to be sent, we will just piggyback the ACK (see below).
root@mbed.org 0:5e1631496985 491 */
root@mbed.org 0:5e1631496985 492 if (pcb->flags & TF_ACK_NOW &&
root@mbed.org 0:5e1631496985 493 (seg == NULL ||
root@mbed.org 0:5e1631496985 494 ntohl(seg->tcphdr->seqno) - pcb->lastack + seg->len > wnd)) {
root@mbed.org 0:5e1631496985 495 #if LWIP_TCP_TIMESTAMPS
root@mbed.org 0:5e1631496985 496 if (pcb->flags & TF_TIMESTAMP)
root@mbed.org 0:5e1631496985 497 optlen = LWIP_TCP_OPT_LENGTH(TF_SEG_OPTS_TS);
root@mbed.org 0:5e1631496985 498 #endif
root@mbed.org 0:5e1631496985 499 p = pbuf_alloc(PBUF_IP, TCP_HLEN + optlen, PBUF_RAM);
root@mbed.org 0:5e1631496985 500 if (p == NULL) {
root@mbed.org 0:5e1631496985 501 LWIP_DEBUGF(TCP_OUTPUT_DEBUG, ("tcp_output: (ACK) could not allocate pbuf\n"));
root@mbed.org 0:5e1631496985 502 return ERR_BUF;
root@mbed.org 0:5e1631496985 503 }
root@mbed.org 0:5e1631496985 504 LWIP_DEBUGF(TCP_OUTPUT_DEBUG,
root@mbed.org 0:5e1631496985 505 ("tcp_output: sending ACK for %"U32_F"\n", pcb->rcv_nxt));
root@mbed.org 0:5e1631496985 506 /* remove ACK flags from the PCB, as we send an empty ACK now */
root@mbed.org 0:5e1631496985 507 pcb->flags &= ~(TF_ACK_DELAY | TF_ACK_NOW);
root@mbed.org 0:5e1631496985 508
root@mbed.org 0:5e1631496985 509 tcphdr = tcp_output_set_header(pcb, p, optlen, htonl(pcb->snd_nxt));
root@mbed.org 0:5e1631496985 510
root@mbed.org 0:5e1631496985 511 /* NB. MSS option is only sent on SYNs, so ignore it here */
root@mbed.org 0:5e1631496985 512 #if LWIP_TCP_TIMESTAMPS
root@mbed.org 0:5e1631496985 513 pcb->ts_lastacksent = pcb->rcv_nxt;
root@mbed.org 0:5e1631496985 514
root@mbed.org 0:5e1631496985 515 if (pcb->flags & TF_TIMESTAMP)
root@mbed.org 0:5e1631496985 516 tcp_build_timestamp_option(pcb, (u32_t *)(tcphdr + 1));
root@mbed.org 0:5e1631496985 517 #endif
root@mbed.org 0:5e1631496985 518
root@mbed.org 0:5e1631496985 519 #if CHECKSUM_GEN_TCP
root@mbed.org 0:5e1631496985 520 tcphdr->chksum = inet_chksum_pseudo(p, &(pcb->local_ip), &(pcb->remote_ip),
root@mbed.org 0:5e1631496985 521 IP_PROTO_TCP, p->tot_len);
root@mbed.org 0:5e1631496985 522 #endif
root@mbed.org 0:5e1631496985 523 #if LWIP_NETIF_HWADDRHINT
root@mbed.org 0:5e1631496985 524 ip_output_hinted(p, &(pcb->local_ip), &(pcb->remote_ip), pcb->ttl, pcb->tos,
root@mbed.org 0:5e1631496985 525 IP_PROTO_TCP, &(pcb->addr_hint));
root@mbed.org 0:5e1631496985 526 #else /* LWIP_NETIF_HWADDRHINT*/
root@mbed.org 0:5e1631496985 527 ip_output(p, &(pcb->local_ip), &(pcb->remote_ip), pcb->ttl, pcb->tos,
root@mbed.org 0:5e1631496985 528 IP_PROTO_TCP);
root@mbed.org 0:5e1631496985 529 #endif /* LWIP_NETIF_HWADDRHINT*/
root@mbed.org 0:5e1631496985 530 pbuf_free(p);
root@mbed.org 0:5e1631496985 531
root@mbed.org 0:5e1631496985 532 return ERR_OK;
root@mbed.org 0:5e1631496985 533 }
root@mbed.org 0:5e1631496985 534
root@mbed.org 0:5e1631496985 535 #if TCP_OUTPUT_DEBUG
root@mbed.org 0:5e1631496985 536 if (seg == NULL) {
root@mbed.org 0:5e1631496985 537 LWIP_DEBUGF(TCP_OUTPUT_DEBUG, ("tcp_output: nothing to send (%p)\n",
root@mbed.org 0:5e1631496985 538 (void*)pcb->unsent));
root@mbed.org 0:5e1631496985 539 }
root@mbed.org 0:5e1631496985 540 #endif /* TCP_OUTPUT_DEBUG */
root@mbed.org 0:5e1631496985 541 #if TCP_CWND_DEBUG
root@mbed.org 0:5e1631496985 542 if (seg == NULL) {
root@mbed.org 0:5e1631496985 543 LWIP_DEBUGF(TCP_CWND_DEBUG, ("tcp_output: snd_wnd %"U16_F
root@mbed.org 0:5e1631496985 544 ", cwnd %"U16_F", wnd %"U32_F
root@mbed.org 0:5e1631496985 545 ", seg == NULL, ack %"U32_F"\n",
root@mbed.org 0:5e1631496985 546 pcb->snd_wnd, pcb->cwnd, wnd, pcb->lastack));
root@mbed.org 0:5e1631496985 547 } else {
root@mbed.org 0:5e1631496985 548 LWIP_DEBUGF(TCP_CWND_DEBUG,
root@mbed.org 0:5e1631496985 549 ("tcp_output: snd_wnd %"U16_F", cwnd %"U16_F", wnd %"U32_F
root@mbed.org 0:5e1631496985 550 ", effwnd %"U32_F", seq %"U32_F", ack %"U32_F"\n",
root@mbed.org 0:5e1631496985 551 pcb->snd_wnd, pcb->cwnd, wnd,
root@mbed.org 0:5e1631496985 552 ntohl(seg->tcphdr->seqno) - pcb->lastack + seg->len,
root@mbed.org 0:5e1631496985 553 ntohl(seg->tcphdr->seqno), pcb->lastack));
root@mbed.org 0:5e1631496985 554 }
root@mbed.org 0:5e1631496985 555 #endif /* TCP_CWND_DEBUG */
root@mbed.org 0:5e1631496985 556 /* data available and window allows it to be sent? */
root@mbed.org 0:5e1631496985 557 while (seg != NULL &&
root@mbed.org 0:5e1631496985 558 ntohl(seg->tcphdr->seqno) - pcb->lastack + seg->len <= wnd) {
root@mbed.org 0:5e1631496985 559 LWIP_ASSERT("RST not expected here!",
root@mbed.org 0:5e1631496985 560 (TCPH_FLAGS(seg->tcphdr) & TCP_RST) == 0);
root@mbed.org 0:5e1631496985 561 /* Stop sending if the nagle algorithm would prevent it
root@mbed.org 0:5e1631496985 562 * Don't stop:
root@mbed.org 0:5e1631496985 563 * - if tcp_enqueue had a memory error before (prevent delayed ACK timeout) or
root@mbed.org 0:5e1631496985 564 * - if FIN was already enqueued for this PCB (SYN is always alone in a segment -
root@mbed.org 0:5e1631496985 565 * either seg->next != NULL or pcb->unacked == NULL;
root@mbed.org 0:5e1631496985 566 * RST is no sent using tcp_enqueue/tcp_output.
root@mbed.org 0:5e1631496985 567 */
root@mbed.org 0:5e1631496985 568 if((tcp_do_output_nagle(pcb) == 0) &&
root@mbed.org 0:5e1631496985 569 ((pcb->flags & (TF_NAGLEMEMERR | TF_FIN)) == 0)){
root@mbed.org 0:5e1631496985 570 break;
root@mbed.org 0:5e1631496985 571 }
root@mbed.org 0:5e1631496985 572 #if TCP_CWND_DEBUG
root@mbed.org 0:5e1631496985 573 LWIP_DEBUGF(TCP_CWND_DEBUG, ("tcp_output: snd_wnd %"U16_F", cwnd %"U16_F", wnd %"U32_F", effwnd %"U32_F", seq %"U32_F", ack %"U32_F", i %"S16_F"\n",
root@mbed.org 0:5e1631496985 574 pcb->snd_wnd, pcb->cwnd, wnd,
root@mbed.org 0:5e1631496985 575 ntohl(seg->tcphdr->seqno) + seg->len -
root@mbed.org 0:5e1631496985 576 pcb->lastack,
root@mbed.org 0:5e1631496985 577 ntohl(seg->tcphdr->seqno), pcb->lastack, i));
root@mbed.org 0:5e1631496985 578 ++i;
root@mbed.org 0:5e1631496985 579 #endif /* TCP_CWND_DEBUG */
root@mbed.org 0:5e1631496985 580
root@mbed.org 0:5e1631496985 581 pcb->unsent = seg->next;
root@mbed.org 0:5e1631496985 582
root@mbed.org 0:5e1631496985 583 if (pcb->state != SYN_SENT) {
root@mbed.org 0:5e1631496985 584 TCPH_SET_FLAG(seg->tcphdr, TCP_ACK);
root@mbed.org 0:5e1631496985 585 pcb->flags &= ~(TF_ACK_DELAY | TF_ACK_NOW);
root@mbed.org 0:5e1631496985 586 }
root@mbed.org 0:5e1631496985 587
root@mbed.org 0:5e1631496985 588 tcp_output_segment(seg, pcb);
root@mbed.org 0:5e1631496985 589 snd_nxt = ntohl(seg->tcphdr->seqno) + TCP_TCPLEN(seg);
root@mbed.org 0:5e1631496985 590 if (TCP_SEQ_LT(pcb->snd_nxt, snd_nxt)) {
root@mbed.org 0:5e1631496985 591 pcb->snd_nxt = snd_nxt;
root@mbed.org 0:5e1631496985 592 }
root@mbed.org 0:5e1631496985 593 /* put segment on unacknowledged list if length > 0 */
root@mbed.org 0:5e1631496985 594 if (TCP_TCPLEN(seg) > 0) {
root@mbed.org 0:5e1631496985 595 seg->next = NULL;
root@mbed.org 0:5e1631496985 596 /* unacked list is empty? */
root@mbed.org 0:5e1631496985 597 if (pcb->unacked == NULL) {
root@mbed.org 0:5e1631496985 598 pcb->unacked = seg;
root@mbed.org 0:5e1631496985 599 useg = seg;
root@mbed.org 0:5e1631496985 600 /* unacked list is not empty? */
root@mbed.org 0:5e1631496985 601 } else {
root@mbed.org 0:5e1631496985 602 /* In the case of fast retransmit, the packet should not go to the tail
root@mbed.org 0:5e1631496985 603 * of the unacked queue, but rather somewhere before it. We need to check for
root@mbed.org 0:5e1631496985 604 * this case. -STJ Jul 27, 2004 */
root@mbed.org 0:5e1631496985 605 if (TCP_SEQ_LT(ntohl(seg->tcphdr->seqno), ntohl(useg->tcphdr->seqno))){
root@mbed.org 0:5e1631496985 606 /* add segment to before tail of unacked list, keeping the list sorted */
root@mbed.org 0:5e1631496985 607 struct tcp_seg **cur_seg = &(pcb->unacked);
root@mbed.org 0:5e1631496985 608 while (*cur_seg &&
root@mbed.org 0:5e1631496985 609 TCP_SEQ_LT(ntohl((*cur_seg)->tcphdr->seqno), ntohl(seg->tcphdr->seqno))) {
root@mbed.org 0:5e1631496985 610 cur_seg = &((*cur_seg)->next );
root@mbed.org 0:5e1631496985 611 }
root@mbed.org 0:5e1631496985 612 seg->next = (*cur_seg);
root@mbed.org 0:5e1631496985 613 (*cur_seg) = seg;
root@mbed.org 0:5e1631496985 614 } else {
root@mbed.org 0:5e1631496985 615 /* add segment to tail of unacked list */
root@mbed.org 0:5e1631496985 616 useg->next = seg;
root@mbed.org 0:5e1631496985 617 useg = useg->next;
root@mbed.org 0:5e1631496985 618 }
root@mbed.org 0:5e1631496985 619 }
root@mbed.org 0:5e1631496985 620 /* do not queue empty segments on the unacked list */
root@mbed.org 0:5e1631496985 621 } else {
root@mbed.org 0:5e1631496985 622 tcp_seg_free(seg);
root@mbed.org 0:5e1631496985 623 }
root@mbed.org 0:5e1631496985 624 seg = pcb->unsent;
root@mbed.org 0:5e1631496985 625 }
root@mbed.org 0:5e1631496985 626
root@mbed.org 0:5e1631496985 627 if (seg != NULL && pcb->persist_backoff == 0 &&
root@mbed.org 0:5e1631496985 628 ntohl(seg->tcphdr->seqno) - pcb->lastack + seg->len > pcb->snd_wnd) {
root@mbed.org 0:5e1631496985 629 /* prepare for persist timer */
root@mbed.org 0:5e1631496985 630 pcb->persist_cnt = 0;
root@mbed.org 0:5e1631496985 631 pcb->persist_backoff = 1;
root@mbed.org 0:5e1631496985 632 }
root@mbed.org 0:5e1631496985 633
root@mbed.org 0:5e1631496985 634 pcb->flags &= ~TF_NAGLEMEMERR;
root@mbed.org 0:5e1631496985 635 return ERR_OK;
root@mbed.org 0:5e1631496985 636 }
root@mbed.org 0:5e1631496985 637
root@mbed.org 0:5e1631496985 638 /**
root@mbed.org 0:5e1631496985 639 * Called by tcp_output() to actually send a TCP segment over IP.
root@mbed.org 0:5e1631496985 640 *
root@mbed.org 0:5e1631496985 641 * @param seg the tcp_seg to send
root@mbed.org 0:5e1631496985 642 * @param pcb the tcp_pcb for the TCP connection used to send the segment
root@mbed.org 0:5e1631496985 643 */
root@mbed.org 0:5e1631496985 644 static void
root@mbed.org 0:5e1631496985 645 tcp_output_segment(struct tcp_seg *seg, struct tcp_pcb *pcb)
root@mbed.org 0:5e1631496985 646 {
root@mbed.org 0:5e1631496985 647 u16_t len;
root@mbed.org 0:5e1631496985 648 struct netif *netif;
root@mbed.org 0:5e1631496985 649 u32_t *opts;
root@mbed.org 0:5e1631496985 650
root@mbed.org 0:5e1631496985 651 /** @bug Exclude retransmitted segments from this count. */
root@mbed.org 0:5e1631496985 652 snmp_inc_tcpoutsegs();
root@mbed.org 0:5e1631496985 653
root@mbed.org 0:5e1631496985 654 /* The TCP header has already been constructed, but the ackno and
root@mbed.org 0:5e1631496985 655 wnd fields remain. */
root@mbed.org 0:5e1631496985 656 seg->tcphdr->ackno = htonl(pcb->rcv_nxt);
root@mbed.org 0:5e1631496985 657
root@mbed.org 0:5e1631496985 658 /* advertise our receive window size in this TCP segment */
root@mbed.org 0:5e1631496985 659 seg->tcphdr->wnd = htons(pcb->rcv_ann_wnd);
root@mbed.org 0:5e1631496985 660
root@mbed.org 0:5e1631496985 661 pcb->rcv_ann_right_edge = pcb->rcv_nxt + pcb->rcv_ann_wnd;
root@mbed.org 0:5e1631496985 662
root@mbed.org 0:5e1631496985 663 /* Add any requested options. NB MSS option is only set on SYN
root@mbed.org 0:5e1631496985 664 packets, so ignore it here */
root@mbed.org 0:5e1631496985 665 opts = (u32_t *)(seg->tcphdr + 1);
root@mbed.org 0:5e1631496985 666 if (seg->flags & TF_SEG_OPTS_MSS) {
root@mbed.org 0:5e1631496985 667 TCP_BUILD_MSS_OPTION(*opts);
root@mbed.org 0:5e1631496985 668 opts += 1;
root@mbed.org 0:5e1631496985 669 }
root@mbed.org 0:5e1631496985 670 #if LWIP_TCP_TIMESTAMPS
root@mbed.org 0:5e1631496985 671 pcb->ts_lastacksent = pcb->rcv_nxt;
root@mbed.org 0:5e1631496985 672
root@mbed.org 0:5e1631496985 673 if (seg->flags & TF_SEG_OPTS_TS) {
root@mbed.org 0:5e1631496985 674 tcp_build_timestamp_option(pcb, opts);
root@mbed.org 0:5e1631496985 675 opts += 3;
root@mbed.org 0:5e1631496985 676 }
root@mbed.org 0:5e1631496985 677 #endif
root@mbed.org 0:5e1631496985 678
root@mbed.org 0:5e1631496985 679 /* If we don't have a local IP address, we get one by
root@mbed.org 0:5e1631496985 680 calling ip_route(). */
root@mbed.org 0:5e1631496985 681 if (ip_addr_isany(&(pcb->local_ip))) {
root@mbed.org 0:5e1631496985 682 netif = ip_route(&(pcb->remote_ip));
root@mbed.org 0:5e1631496985 683 if (netif == NULL) {
root@mbed.org 0:5e1631496985 684 return;
root@mbed.org 0:5e1631496985 685 }
root@mbed.org 0:5e1631496985 686 ip_addr_set(&(pcb->local_ip), &(netif->ip_addr));
root@mbed.org 0:5e1631496985 687 }
root@mbed.org 0:5e1631496985 688
root@mbed.org 0:5e1631496985 689 /* Set retransmission timer running if it is not currently enabled */
root@mbed.org 0:5e1631496985 690 if(pcb->rtime == -1)
root@mbed.org 0:5e1631496985 691 pcb->rtime = 0;
root@mbed.org 0:5e1631496985 692
root@mbed.org 0:5e1631496985 693 if (pcb->rttest == 0) {
root@mbed.org 0:5e1631496985 694 pcb->rttest = tcp_ticks;
root@mbed.org 0:5e1631496985 695 pcb->rtseq = ntohl(seg->tcphdr->seqno);
root@mbed.org 0:5e1631496985 696
root@mbed.org 0:5e1631496985 697 LWIP_DEBUGF(TCP_RTO_DEBUG, ("tcp_output_segment: rtseq %"U32_F"\n", pcb->rtseq));
root@mbed.org 0:5e1631496985 698 }
root@mbed.org 0:5e1631496985 699 LWIP_DEBUGF(TCP_OUTPUT_DEBUG, ("tcp_output_segment: %"U32_F":%"U32_F"\n",
root@mbed.org 0:5e1631496985 700 htonl(seg->tcphdr->seqno), htonl(seg->tcphdr->seqno) +
root@mbed.org 0:5e1631496985 701 seg->len));
root@mbed.org 0:5e1631496985 702
root@mbed.org 0:5e1631496985 703 len = (u16_t)((u8_t *)seg->tcphdr - (u8_t *)seg->p->payload);
root@mbed.org 0:5e1631496985 704
root@mbed.org 0:5e1631496985 705 seg->p->len -= len;
root@mbed.org 0:5e1631496985 706 seg->p->tot_len -= len;
root@mbed.org 0:5e1631496985 707
root@mbed.org 0:5e1631496985 708 seg->p->payload = seg->tcphdr;
root@mbed.org 0:5e1631496985 709
root@mbed.org 0:5e1631496985 710 seg->tcphdr->chksum = 0;
root@mbed.org 0:5e1631496985 711 #if CHECKSUM_GEN_TCP
root@mbed.org 0:5e1631496985 712 seg->tcphdr->chksum = inet_chksum_pseudo(seg->p,
root@mbed.org 0:5e1631496985 713 &(pcb->local_ip),
root@mbed.org 0:5e1631496985 714 &(pcb->remote_ip),
root@mbed.org 0:5e1631496985 715 IP_PROTO_TCP, seg->p->tot_len);
root@mbed.org 0:5e1631496985 716 #endif
root@mbed.org 0:5e1631496985 717 TCP_STATS_INC(tcp.xmit);
root@mbed.org 0:5e1631496985 718
root@mbed.org 0:5e1631496985 719 #if LWIP_NETIF_HWADDRHINT
root@mbed.org 0:5e1631496985 720 ip_output_hinted(seg->p, &(pcb->local_ip), &(pcb->remote_ip), pcb->ttl, pcb->tos,
root@mbed.org 0:5e1631496985 721 IP_PROTO_TCP, &(pcb->addr_hint));
root@mbed.org 0:5e1631496985 722 #else /* LWIP_NETIF_HWADDRHINT*/
root@mbed.org 0:5e1631496985 723 ip_output(seg->p, &(pcb->local_ip), &(pcb->remote_ip), pcb->ttl, pcb->tos,
root@mbed.org 0:5e1631496985 724 IP_PROTO_TCP);
root@mbed.org 0:5e1631496985 725 #endif /* LWIP_NETIF_HWADDRHINT*/
root@mbed.org 0:5e1631496985 726 }
root@mbed.org 0:5e1631496985 727
root@mbed.org 0:5e1631496985 728 /**
root@mbed.org 0:5e1631496985 729 * Send a TCP RESET packet (empty segment with RST flag set) either to
root@mbed.org 0:5e1631496985 730 * abort a connection or to show that there is no matching local connection
root@mbed.org 0:5e1631496985 731 * for a received segment.
root@mbed.org 0:5e1631496985 732 *
root@mbed.org 0:5e1631496985 733 * Called by tcp_abort() (to abort a local connection), tcp_input() (if no
root@mbed.org 0:5e1631496985 734 * matching local pcb was found), tcp_listen_input() (if incoming segment
root@mbed.org 0:5e1631496985 735 * has ACK flag set) and tcp_process() (received segment in the wrong state)
root@mbed.org 0:5e1631496985 736 *
root@mbed.org 0:5e1631496985 737 * Since a RST segment is in most cases not sent for an active connection,
root@mbed.org 0:5e1631496985 738 * tcp_rst() has a number of arguments that are taken from a tcp_pcb for
root@mbed.org 0:5e1631496985 739 * most other segment output functions.
root@mbed.org 0:5e1631496985 740 *
root@mbed.org 0:5e1631496985 741 * @param seqno the sequence number to use for the outgoing segment
root@mbed.org 0:5e1631496985 742 * @param ackno the acknowledge number to use for the outgoing segment
root@mbed.org 0:5e1631496985 743 * @param local_ip the local IP address to send the segment from
root@mbed.org 0:5e1631496985 744 * @param remote_ip the remote IP address to send the segment to
root@mbed.org 0:5e1631496985 745 * @param local_port the local TCP port to send the segment from
root@mbed.org 0:5e1631496985 746 * @param remote_port the remote TCP port to send the segment to
root@mbed.org 0:5e1631496985 747 */
root@mbed.org 0:5e1631496985 748 void
root@mbed.org 0:5e1631496985 749 tcp_rst(u32_t seqno, u32_t ackno,
root@mbed.org 0:5e1631496985 750 struct ip_addr *local_ip, struct ip_addr *remote_ip,
root@mbed.org 0:5e1631496985 751 u16_t local_port, u16_t remote_port)
root@mbed.org 0:5e1631496985 752 {
root@mbed.org 0:5e1631496985 753 struct pbuf *p;
root@mbed.org 0:5e1631496985 754 struct tcp_hdr *tcphdr;
root@mbed.org 0:5e1631496985 755 p = pbuf_alloc(PBUF_IP, TCP_HLEN, PBUF_RAM);
root@mbed.org 0:5e1631496985 756 if (p == NULL) {
root@mbed.org 0:5e1631496985 757 LWIP_DEBUGF(TCP_DEBUG, ("tcp_rst: could not allocate memory for pbuf\n"));
root@mbed.org 0:5e1631496985 758 return;
root@mbed.org 0:5e1631496985 759 }
root@mbed.org 0:5e1631496985 760 LWIP_ASSERT("check that first pbuf can hold struct tcp_hdr",
root@mbed.org 0:5e1631496985 761 (p->len >= sizeof(struct tcp_hdr)));
root@mbed.org 0:5e1631496985 762
root@mbed.org 0:5e1631496985 763 tcphdr = (struct tcp_hdr *)p->payload;
root@mbed.org 0:5e1631496985 764 tcphdr->src = htons(local_port);
root@mbed.org 0:5e1631496985 765 tcphdr->dest = htons(remote_port);
root@mbed.org 0:5e1631496985 766 tcphdr->seqno = htonl(seqno);
root@mbed.org 0:5e1631496985 767 tcphdr->ackno = htonl(ackno);
root@mbed.org 0:5e1631496985 768 TCPH_FLAGS_SET(tcphdr, TCP_RST | TCP_ACK);
root@mbed.org 0:5e1631496985 769 tcphdr->wnd = htons(TCP_WND);
root@mbed.org 0:5e1631496985 770 tcphdr->urgp = 0;
root@mbed.org 0:5e1631496985 771 TCPH_HDRLEN_SET(tcphdr, 5);
root@mbed.org 0:5e1631496985 772
root@mbed.org 0:5e1631496985 773 tcphdr->chksum = 0;
root@mbed.org 0:5e1631496985 774 #if CHECKSUM_GEN_TCP
root@mbed.org 0:5e1631496985 775 tcphdr->chksum = inet_chksum_pseudo(p, local_ip, remote_ip,
root@mbed.org 0:5e1631496985 776 IP_PROTO_TCP, p->tot_len);
root@mbed.org 0:5e1631496985 777 #endif
root@mbed.org 0:5e1631496985 778 TCP_STATS_INC(tcp.xmit);
root@mbed.org 0:5e1631496985 779 snmp_inc_tcpoutrsts();
root@mbed.org 0:5e1631496985 780 /* Send output with hardcoded TTL since we have no access to the pcb */
root@mbed.org 0:5e1631496985 781 ip_output(p, local_ip, remote_ip, TCP_TTL, 0, IP_PROTO_TCP);
root@mbed.org 0:5e1631496985 782 pbuf_free(p);
root@mbed.org 0:5e1631496985 783 LWIP_DEBUGF(TCP_RST_DEBUG, ("tcp_rst: seqno %"U32_F" ackno %"U32_F".\n", seqno, ackno));
root@mbed.org 0:5e1631496985 784 }
root@mbed.org 0:5e1631496985 785
root@mbed.org 0:5e1631496985 786 /**
root@mbed.org 0:5e1631496985 787 * Requeue all unacked segments for retransmission
root@mbed.org 0:5e1631496985 788 *
root@mbed.org 0:5e1631496985 789 * Called by tcp_slowtmr() for slow retransmission.
root@mbed.org 0:5e1631496985 790 *
root@mbed.org 0:5e1631496985 791 * @param pcb the tcp_pcb for which to re-enqueue all unacked segments
root@mbed.org 0:5e1631496985 792 */
root@mbed.org 0:5e1631496985 793 void
root@mbed.org 0:5e1631496985 794 tcp_rexmit_rto(struct tcp_pcb *pcb)
root@mbed.org 0:5e1631496985 795 {
root@mbed.org 0:5e1631496985 796 struct tcp_seg *seg;
root@mbed.org 0:5e1631496985 797
root@mbed.org 0:5e1631496985 798 if (pcb->unacked == NULL) {
root@mbed.org 0:5e1631496985 799 return;
root@mbed.org 0:5e1631496985 800 }
root@mbed.org 0:5e1631496985 801
root@mbed.org 0:5e1631496985 802 /* Move all unacked segments to the head of the unsent queue */
root@mbed.org 0:5e1631496985 803 for (seg = pcb->unacked; seg->next != NULL; seg = seg->next);
root@mbed.org 0:5e1631496985 804 /* concatenate unsent queue after unacked queue */
root@mbed.org 0:5e1631496985 805 seg->next = pcb->unsent;
root@mbed.org 0:5e1631496985 806 /* unsent queue is the concatenated queue (of unacked, unsent) */
root@mbed.org 0:5e1631496985 807 pcb->unsent = pcb->unacked;
root@mbed.org 0:5e1631496985 808 /* unacked queue is now empty */
root@mbed.org 0:5e1631496985 809 pcb->unacked = NULL;
root@mbed.org 0:5e1631496985 810
root@mbed.org 0:5e1631496985 811 /* increment number of retransmissions */
root@mbed.org 0:5e1631496985 812 ++pcb->nrtx;
root@mbed.org 0:5e1631496985 813
root@mbed.org 0:5e1631496985 814 /* Don't take any RTT measurements after retransmitting. */
root@mbed.org 0:5e1631496985 815 pcb->rttest = 0;
root@mbed.org 0:5e1631496985 816
root@mbed.org 0:5e1631496985 817 /* Do the actual retransmission */
root@mbed.org 0:5e1631496985 818 tcp_output(pcb);
root@mbed.org 0:5e1631496985 819 }
root@mbed.org 0:5e1631496985 820
root@mbed.org 0:5e1631496985 821 /**
root@mbed.org 0:5e1631496985 822 * Requeue the first unacked segment for retransmission
root@mbed.org 0:5e1631496985 823 *
root@mbed.org 0:5e1631496985 824 * Called by tcp_receive() for fast retramsmit.
root@mbed.org 0:5e1631496985 825 *
root@mbed.org 0:5e1631496985 826 * @param pcb the tcp_pcb for which to retransmit the first unacked segment
root@mbed.org 0:5e1631496985 827 */
root@mbed.org 0:5e1631496985 828 void
root@mbed.org 0:5e1631496985 829 tcp_rexmit(struct tcp_pcb *pcb)
root@mbed.org 0:5e1631496985 830 {
root@mbed.org 0:5e1631496985 831 struct tcp_seg *seg;
root@mbed.org 0:5e1631496985 832 struct tcp_seg **cur_seg;
root@mbed.org 0:5e1631496985 833
root@mbed.org 0:5e1631496985 834 if (pcb->unacked == NULL) {
root@mbed.org 0:5e1631496985 835 return;
root@mbed.org 0:5e1631496985 836 }
root@mbed.org 0:5e1631496985 837
root@mbed.org 0:5e1631496985 838 /* Move the first unacked segment to the unsent queue */
root@mbed.org 0:5e1631496985 839 /* Keep the unsent queue sorted. */
root@mbed.org 0:5e1631496985 840 seg = pcb->unacked;
root@mbed.org 0:5e1631496985 841 pcb->unacked = seg->next;
root@mbed.org 0:5e1631496985 842
root@mbed.org 0:5e1631496985 843 cur_seg = &(pcb->unsent);
root@mbed.org 0:5e1631496985 844 while (*cur_seg &&
root@mbed.org 0:5e1631496985 845 TCP_SEQ_LT(ntohl((*cur_seg)->tcphdr->seqno), ntohl(seg->tcphdr->seqno))) {
root@mbed.org 0:5e1631496985 846 cur_seg = &((*cur_seg)->next );
root@mbed.org 0:5e1631496985 847 }
root@mbed.org 0:5e1631496985 848 seg->next = *cur_seg;
root@mbed.org 0:5e1631496985 849 *cur_seg = seg;
root@mbed.org 0:5e1631496985 850
root@mbed.org 0:5e1631496985 851 ++pcb->nrtx;
root@mbed.org 0:5e1631496985 852
root@mbed.org 0:5e1631496985 853 /* Don't take any rtt measurements after retransmitting. */
root@mbed.org 0:5e1631496985 854 pcb->rttest = 0;
root@mbed.org 0:5e1631496985 855
root@mbed.org 0:5e1631496985 856 /* Do the actual retransmission. */
root@mbed.org 0:5e1631496985 857 snmp_inc_tcpretranssegs();
root@mbed.org 0:5e1631496985 858 tcp_output(pcb);
root@mbed.org 0:5e1631496985 859 }
root@mbed.org 0:5e1631496985 860
root@mbed.org 0:5e1631496985 861
root@mbed.org 0:5e1631496985 862 /**
root@mbed.org 0:5e1631496985 863 * Handle retransmission after three dupacks received
root@mbed.org 0:5e1631496985 864 *
root@mbed.org 0:5e1631496985 865 * @param pcb the tcp_pcb for which to retransmit the first unacked segment
root@mbed.org 0:5e1631496985 866 */
root@mbed.org 0:5e1631496985 867 void
root@mbed.org 0:5e1631496985 868 tcp_rexmit_fast(struct tcp_pcb *pcb)
root@mbed.org 0:5e1631496985 869 {
root@mbed.org 0:5e1631496985 870 if (pcb->unacked != NULL && !(pcb->flags & TF_INFR)) {
root@mbed.org 0:5e1631496985 871 /* This is fast retransmit. Retransmit the first unacked segment. */
root@mbed.org 0:5e1631496985 872 LWIP_DEBUGF(TCP_FR_DEBUG,
root@mbed.org 0:5e1631496985 873 ("tcp_receive: dupacks %"U16_F" (%"U32_F
root@mbed.org 0:5e1631496985 874 "), fast retransmit %"U32_F"\n",
root@mbed.org 0:5e1631496985 875 (u16_t)pcb->dupacks, pcb->lastack,
root@mbed.org 0:5e1631496985 876 ntohl(pcb->unacked->tcphdr->seqno)));
root@mbed.org 0:5e1631496985 877 tcp_rexmit(pcb);
root@mbed.org 0:5e1631496985 878
root@mbed.org 0:5e1631496985 879 /* Set ssthresh to half of the minimum of the current
root@mbed.org 0:5e1631496985 880 * cwnd and the advertised window */
root@mbed.org 0:5e1631496985 881 if (pcb->cwnd > pcb->snd_wnd)
root@mbed.org 0:5e1631496985 882 pcb->ssthresh = pcb->snd_wnd / 2;
root@mbed.org 0:5e1631496985 883 else
root@mbed.org 0:5e1631496985 884 pcb->ssthresh = pcb->cwnd / 2;
root@mbed.org 0:5e1631496985 885
root@mbed.org 0:5e1631496985 886 /* The minimum value for ssthresh should be 2 MSS */
root@mbed.org 0:5e1631496985 887 if (pcb->ssthresh < 2*pcb->mss) {
root@mbed.org 0:5e1631496985 888 LWIP_DEBUGF(TCP_FR_DEBUG,
root@mbed.org 0:5e1631496985 889 ("tcp_receive: The minimum value for ssthresh %"U16_F
root@mbed.org 0:5e1631496985 890 " should be min 2 mss %"U16_F"...\n",
root@mbed.org 0:5e1631496985 891 pcb->ssthresh, 2*pcb->mss));
root@mbed.org 0:5e1631496985 892 pcb->ssthresh = 2*pcb->mss;
root@mbed.org 0:5e1631496985 893 }
root@mbed.org 0:5e1631496985 894
root@mbed.org 0:5e1631496985 895 pcb->cwnd = pcb->ssthresh + 3 * pcb->mss;
root@mbed.org 0:5e1631496985 896 pcb->flags |= TF_INFR;
root@mbed.org 0:5e1631496985 897 }
root@mbed.org 0:5e1631496985 898 }
root@mbed.org 0:5e1631496985 899
root@mbed.org 0:5e1631496985 900
root@mbed.org 0:5e1631496985 901 /**
root@mbed.org 0:5e1631496985 902 * Send keepalive packets to keep a connection active although
root@mbed.org 0:5e1631496985 903 * no data is sent over it.
root@mbed.org 0:5e1631496985 904 *
root@mbed.org 0:5e1631496985 905 * Called by tcp_slowtmr()
root@mbed.org 0:5e1631496985 906 *
root@mbed.org 0:5e1631496985 907 * @param pcb the tcp_pcb for which to send a keepalive packet
root@mbed.org 0:5e1631496985 908 */
root@mbed.org 0:5e1631496985 909 void
root@mbed.org 0:5e1631496985 910 tcp_keepalive(struct tcp_pcb *pcb)
root@mbed.org 0:5e1631496985 911 {
root@mbed.org 0:5e1631496985 912 struct pbuf *p;
root@mbed.org 0:5e1631496985 913 struct tcp_hdr *tcphdr;
root@mbed.org 0:5e1631496985 914
root@mbed.org 0:5e1631496985 915 LWIP_DEBUGF(TCP_DEBUG, ("tcp_keepalive: sending KEEPALIVE probe to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
root@mbed.org 0:5e1631496985 916 ip4_addr1(&pcb->remote_ip), ip4_addr2(&pcb->remote_ip),
root@mbed.org 0:5e1631496985 917 ip4_addr3(&pcb->remote_ip), ip4_addr4(&pcb->remote_ip)));
root@mbed.org 0:5e1631496985 918
root@mbed.org 0:5e1631496985 919 LWIP_DEBUGF(TCP_DEBUG, ("tcp_keepalive: tcp_ticks %"U32_F" pcb->tmr %"U32_F" pcb->keep_cnt_sent %"U16_F"\n",
root@mbed.org 0:5e1631496985 920 tcp_ticks, pcb->tmr, pcb->keep_cnt_sent));
root@mbed.org 0:5e1631496985 921
root@mbed.org 0:5e1631496985 922 p = pbuf_alloc(PBUF_IP, TCP_HLEN, PBUF_RAM);
root@mbed.org 0:5e1631496985 923
root@mbed.org 0:5e1631496985 924 if(p == NULL) {
root@mbed.org 0:5e1631496985 925 LWIP_DEBUGF(TCP_DEBUG,
root@mbed.org 0:5e1631496985 926 ("tcp_keepalive: could not allocate memory for pbuf\n"));
root@mbed.org 0:5e1631496985 927 return;
root@mbed.org 0:5e1631496985 928 }
root@mbed.org 0:5e1631496985 929 LWIP_ASSERT("check that first pbuf can hold struct tcp_hdr",
root@mbed.org 0:5e1631496985 930 (p->len >= sizeof(struct tcp_hdr)));
root@mbed.org 0:5e1631496985 931
root@mbed.org 0:5e1631496985 932 tcphdr = tcp_output_set_header(pcb, p, 0, htonl(pcb->snd_nxt - 1));
root@mbed.org 0:5e1631496985 933
root@mbed.org 0:5e1631496985 934 #if CHECKSUM_GEN_TCP
root@mbed.org 0:5e1631496985 935 tcphdr->chksum = inet_chksum_pseudo(p, &pcb->local_ip, &pcb->remote_ip,
root@mbed.org 0:5e1631496985 936 IP_PROTO_TCP, p->tot_len);
root@mbed.org 0:5e1631496985 937 #endif
root@mbed.org 0:5e1631496985 938 TCP_STATS_INC(tcp.xmit);
root@mbed.org 0:5e1631496985 939
root@mbed.org 0:5e1631496985 940 /* Send output to IP */
root@mbed.org 0:5e1631496985 941 #if LWIP_NETIF_HWADDRHINT
root@mbed.org 0:5e1631496985 942 ip_output_hinted(p, &pcb->local_ip, &pcb->remote_ip, pcb->ttl, 0, IP_PROTO_TCP,
root@mbed.org 0:5e1631496985 943 &(pcb->addr_hint));
root@mbed.org 0:5e1631496985 944 #else /* LWIP_NETIF_HWADDRHINT*/
root@mbed.org 0:5e1631496985 945 ip_output(p, &pcb->local_ip, &pcb->remote_ip, pcb->ttl, 0, IP_PROTO_TCP);
root@mbed.org 0:5e1631496985 946 #endif /* LWIP_NETIF_HWADDRHINT*/
root@mbed.org 0:5e1631496985 947
root@mbed.org 0:5e1631496985 948 pbuf_free(p);
root@mbed.org 0:5e1631496985 949
root@mbed.org 0:5e1631496985 950 LWIP_DEBUGF(TCP_DEBUG, ("tcp_keepalive: seqno %"U32_F" ackno %"U32_F".\n",
root@mbed.org 0:5e1631496985 951 pcb->snd_nxt - 1, pcb->rcv_nxt));
root@mbed.org 0:5e1631496985 952 }
root@mbed.org 0:5e1631496985 953
root@mbed.org 0:5e1631496985 954
root@mbed.org 0:5e1631496985 955 /**
root@mbed.org 0:5e1631496985 956 * Send persist timer zero-window probes to keep a connection active
root@mbed.org 0:5e1631496985 957 * when a window update is lost.
root@mbed.org 0:5e1631496985 958 *
root@mbed.org 0:5e1631496985 959 * Called by tcp_slowtmr()
root@mbed.org 0:5e1631496985 960 *
root@mbed.org 0:5e1631496985 961 * @param pcb the tcp_pcb for which to send a zero-window probe packet
root@mbed.org 0:5e1631496985 962 */
root@mbed.org 0:5e1631496985 963 void
root@mbed.org 0:5e1631496985 964 tcp_zero_window_probe(struct tcp_pcb *pcb)
root@mbed.org 0:5e1631496985 965 {
root@mbed.org 0:5e1631496985 966 struct pbuf *p;
root@mbed.org 0:5e1631496985 967 struct tcp_hdr *tcphdr;
root@mbed.org 0:5e1631496985 968 struct tcp_seg *seg;
root@mbed.org 0:5e1631496985 969 u16_t len;
root@mbed.org 0:5e1631496985 970 u8_t is_fin;
root@mbed.org 0:5e1631496985 971
root@mbed.org 0:5e1631496985 972 LWIP_DEBUGF(TCP_DEBUG,
root@mbed.org 0:5e1631496985 973 ("tcp_zero_window_probe: sending ZERO WINDOW probe to %"
root@mbed.org 0:5e1631496985 974 U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
root@mbed.org 0:5e1631496985 975 ip4_addr1(&pcb->remote_ip), ip4_addr2(&pcb->remote_ip),
root@mbed.org 0:5e1631496985 976 ip4_addr3(&pcb->remote_ip), ip4_addr4(&pcb->remote_ip)));
root@mbed.org 0:5e1631496985 977
root@mbed.org 0:5e1631496985 978 LWIP_DEBUGF(TCP_DEBUG,
root@mbed.org 0:5e1631496985 979 ("tcp_zero_window_probe: tcp_ticks %"U32_F
root@mbed.org 0:5e1631496985 980 " pcb->tmr %"U32_F" pcb->keep_cnt_sent %"U16_F"\n",
root@mbed.org 0:5e1631496985 981 tcp_ticks, pcb->tmr, pcb->keep_cnt_sent));
root@mbed.org 0:5e1631496985 982
root@mbed.org 0:5e1631496985 983 seg = pcb->unacked;
root@mbed.org 0:5e1631496985 984
root@mbed.org 0:5e1631496985 985 if(seg == NULL)
root@mbed.org 0:5e1631496985 986 seg = pcb->unsent;
root@mbed.org 0:5e1631496985 987
root@mbed.org 0:5e1631496985 988 if(seg == NULL)
root@mbed.org 0:5e1631496985 989 return;
root@mbed.org 0:5e1631496985 990
root@mbed.org 0:5e1631496985 991 is_fin = ((TCPH_FLAGS(seg->tcphdr) & TCP_FIN) != 0) && (seg->len == 0);
root@mbed.org 0:5e1631496985 992 len = is_fin ? TCP_HLEN : TCP_HLEN + 1;
root@mbed.org 0:5e1631496985 993
root@mbed.org 0:5e1631496985 994 p = pbuf_alloc(PBUF_IP, len, PBUF_RAM);
root@mbed.org 0:5e1631496985 995 if(p == NULL) {
root@mbed.org 0:5e1631496985 996 LWIP_DEBUGF(TCP_DEBUG, ("tcp_zero_window_probe: no memory for pbuf\n"));
root@mbed.org 0:5e1631496985 997 return;
root@mbed.org 0:5e1631496985 998 }
root@mbed.org 0:5e1631496985 999 LWIP_ASSERT("check that first pbuf can hold struct tcp_hdr",
root@mbed.org 0:5e1631496985 1000 (p->len >= sizeof(struct tcp_hdr)));
root@mbed.org 0:5e1631496985 1001
root@mbed.org 0:5e1631496985 1002 tcphdr = tcp_output_set_header(pcb, p, 0, seg->tcphdr->seqno);
root@mbed.org 0:5e1631496985 1003
root@mbed.org 0:5e1631496985 1004 if (is_fin) {
root@mbed.org 0:5e1631496985 1005 /* FIN segment, no data */
root@mbed.org 0:5e1631496985 1006 TCPH_FLAGS_SET(tcphdr, TCP_ACK | TCP_FIN);
root@mbed.org 0:5e1631496985 1007 } else {
root@mbed.org 0:5e1631496985 1008 /* Data segment, copy in one byte from the head of the unacked queue */
root@mbed.org 0:5e1631496985 1009 *((char *)p->payload + sizeof(struct tcp_hdr)) = *(char *)seg->dataptr;
root@mbed.org 0:5e1631496985 1010 }
root@mbed.org 0:5e1631496985 1011
root@mbed.org 0:5e1631496985 1012 #if CHECKSUM_GEN_TCP
root@mbed.org 0:5e1631496985 1013 tcphdr->chksum = inet_chksum_pseudo(p, &pcb->local_ip, &pcb->remote_ip,
root@mbed.org 0:5e1631496985 1014 IP_PROTO_TCP, p->tot_len);
root@mbed.org 0:5e1631496985 1015 #endif
root@mbed.org 0:5e1631496985 1016 TCP_STATS_INC(tcp.xmit);
root@mbed.org 0:5e1631496985 1017
root@mbed.org 0:5e1631496985 1018 /* Send output to IP */
root@mbed.org 0:5e1631496985 1019 #if LWIP_NETIF_HWADDRHINT
root@mbed.org 0:5e1631496985 1020 ip_output_hinted(p, &pcb->local_ip, &pcb->remote_ip, pcb->ttl, 0, IP_PROTO_TCP,
root@mbed.org 0:5e1631496985 1021 &(pcb->addr_hint));
root@mbed.org 0:5e1631496985 1022 #else /* LWIP_NETIF_HWADDRHINT*/
root@mbed.org 0:5e1631496985 1023 ip_output(p, &pcb->local_ip, &pcb->remote_ip, pcb->ttl, 0, IP_PROTO_TCP);
root@mbed.org 0:5e1631496985 1024 #endif /* LWIP_NETIF_HWADDRHINT*/
root@mbed.org 0:5e1631496985 1025
root@mbed.org 0:5e1631496985 1026 pbuf_free(p);
root@mbed.org 0:5e1631496985 1027
root@mbed.org 0:5e1631496985 1028 LWIP_DEBUGF(TCP_DEBUG, ("tcp_zero_window_probe: seqno %"U32_F
root@mbed.org 0:5e1631496985 1029 " ackno %"U32_F".\n",
root@mbed.org 0:5e1631496985 1030 pcb->snd_nxt - 1, pcb->rcv_nxt));
root@mbed.org 0:5e1631496985 1031 }
root@mbed.org 0:5e1631496985 1032 #endif /* LWIP_TCP */