Port of LwIP performed by Ralf in 2010. Not recommended for use with recent mbed libraries, but good demos of raw LwIP possible

Dependents:   LwIP_raw_API_serverExample tiny-dtls

Committer:
RodColeman
Date:
Tue Sep 18 14:41:24 2012 +0000
Revision:
0:0791c1fece8e
[mbed] converted /Eth_TCP_Wei_Server/lwip

Who changed what in which revision?

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