NetServices Stack source

Dependents:   HelloWorld ServoInterfaceBoardExample1 4180_Lab4

Committer:
donatien
Date:
Thu Aug 05 15:01:33 2010 +0000
Revision:
11:da4498f591ee
Parent:
5:dd63a1e02b1b

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
donatien 0:632c9925f013 1 /**
donatien 0:632c9925f013 2 * @file
donatien 0:632c9925f013 3 * Transmission Control Protocol, outgoing traffic
donatien 0:632c9925f013 4 *
donatien 0:632c9925f013 5 * The output functions of TCP.
donatien 0:632c9925f013 6 *
donatien 0:632c9925f013 7 */
donatien 0:632c9925f013 8
donatien 0:632c9925f013 9 /*
donatien 0:632c9925f013 10 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
donatien 0:632c9925f013 11 * All rights reserved.
donatien 0:632c9925f013 12 *
donatien 0:632c9925f013 13 * Redistribution and use in source and binary forms, with or without modification,
donatien 0:632c9925f013 14 * are permitted provided that the following conditions are met:
donatien 0:632c9925f013 15 *
donatien 0:632c9925f013 16 * 1. Redistributions of source code must retain the above copyright notice,
donatien 0:632c9925f013 17 * this list of conditions and the following disclaimer.
donatien 0:632c9925f013 18 * 2. Redistributions in binary form must reproduce the above copyright notice,
donatien 0:632c9925f013 19 * this list of conditions and the following disclaimer in the documentation
donatien 0:632c9925f013 20 * and/or other materials provided with the distribution.
donatien 0:632c9925f013 21 * 3. The name of the author may not be used to endorse or promote products
donatien 0:632c9925f013 22 * derived from this software without specific prior written permission.
donatien 0:632c9925f013 23 *
donatien 0:632c9925f013 24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
donatien 0:632c9925f013 25 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
donatien 0:632c9925f013 26 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
donatien 0:632c9925f013 27 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
donatien 0:632c9925f013 28 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
donatien 0:632c9925f013 29 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
donatien 0:632c9925f013 30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
donatien 0:632c9925f013 31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
donatien 0:632c9925f013 32 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
donatien 0:632c9925f013 33 * OF SUCH DAMAGE.
donatien 0:632c9925f013 34 *
donatien 0:632c9925f013 35 * This file is part of the lwIP TCP/IP stack.
donatien 0:632c9925f013 36 *
donatien 0:632c9925f013 37 * Author: Adam Dunkels <adam@sics.se>
donatien 0:632c9925f013 38 *
donatien 0:632c9925f013 39 */
donatien 0:632c9925f013 40
donatien 0:632c9925f013 41 #include "lwip/opt.h"
donatien 0:632c9925f013 42
donatien 0:632c9925f013 43 #if LWIP_TCP /* don't build if not configured for use in lwipopts.h */
donatien 0:632c9925f013 44
donatien 0:632c9925f013 45 #include "lwip/tcp_impl.h"
donatien 0:632c9925f013 46 #include "lwip/def.h"
donatien 0:632c9925f013 47 #include "lwip/mem.h"
donatien 0:632c9925f013 48 #include "lwip/memp.h"
donatien 0:632c9925f013 49 #include "lwip/sys.h"
donatien 0:632c9925f013 50 #include "lwip/ip_addr.h"
donatien 0:632c9925f013 51 #include "lwip/netif.h"
donatien 0:632c9925f013 52 #include "lwip/inet_chksum.h"
donatien 0:632c9925f013 53 #include "lwip/stats.h"
donatien 0:632c9925f013 54 #include "lwip/snmp.h"
donatien 0:632c9925f013 55
donatien 0:632c9925f013 56 #include <string.h>
donatien 0:632c9925f013 57
donatien 0:632c9925f013 58 /* Define some copy-macros for checksum-on-copy so that the code looks
donatien 0:632c9925f013 59 nicer by preventing too many ifdef's. */
donatien 0:632c9925f013 60 #if TCP_CHECKSUM_ON_COPY
donatien 0:632c9925f013 61 #define TCP_DATA_COPY(dst, src, len, seg) do { \
donatien 0:632c9925f013 62 tcp_seg_add_chksum(LWIP_CHKSUM_COPY(dst, src, len), \
donatien 0:632c9925f013 63 len, &seg->chksum, &seg->chksum_swapped); \
donatien 0:632c9925f013 64 seg->flags |= TF_SEG_DATA_CHECKSUMMED; } while(0)
donatien 0:632c9925f013 65 #define TCP_DATA_COPY2(dst, src, len, chksum, chksum_swapped) \
donatien 0:632c9925f013 66 tcp_seg_add_chksum(LWIP_CHKSUM_COPY(dst, src, len), len, chksum, chksum_swapped);
donatien 0:632c9925f013 67 #else /* TCP_CHECKSUM_ON_COPY*/
donatien 0:632c9925f013 68 #define TCP_DATA_COPY(dst, src, len, seg) MEMCPY(dst, src, len)
donatien 0:632c9925f013 69 #define TCP_DATA_COPY2(dst, src, len, chksum, chksum_swapped) MEMCPY(dst, src, len)
donatien 0:632c9925f013 70 #endif /* TCP_CHECKSUM_ON_COPY*/
donatien 0:632c9925f013 71
donatien 0:632c9925f013 72 /** Define this to 1 for an extra check that the output checksum is valid
donatien 0:632c9925f013 73 * (usefule when the checksum is generated by the application, not the stack) */
donatien 0:632c9925f013 74 #ifndef TCP_CHECKSUM_ON_COPY_SANITY_CHECK
donatien 0:632c9925f013 75 #define TCP_CHECKSUM_ON_COPY_SANITY_CHECK 0
donatien 0:632c9925f013 76 #endif
donatien 0:632c9925f013 77
donatien 0:632c9925f013 78 /* Forward declarations.*/
donatien 0:632c9925f013 79 static void tcp_output_segment(struct tcp_seg *seg, struct tcp_pcb *pcb);
donatien 0:632c9925f013 80
donatien 0:632c9925f013 81 /** Allocate a pbuf and create a tcphdr at p->payload, used for output
donatien 0:632c9925f013 82 * functions other than the default tcp_output -> tcp_output_segment
donatien 0:632c9925f013 83 * (e.g. tcp_send_empty_ack, etc.)
donatien 0:632c9925f013 84 *
donatien 0:632c9925f013 85 * @param pcb tcp pcb for which to send a packet (used to initialize tcp_hdr)
donatien 0:632c9925f013 86 * @param optlen length of header-options
donatien 0:632c9925f013 87 * @param datalen length of tcp data to reserve in pbuf
donatien 0:632c9925f013 88 * @param seqno_be seqno in network byte order (big-endian)
donatien 0:632c9925f013 89 * @return pbuf with p->payload being the tcp_hdr
donatien 0:632c9925f013 90 */
donatien 0:632c9925f013 91 static struct pbuf *
donatien 0:632c9925f013 92 tcp_output_alloc_header(struct tcp_pcb *pcb, u16_t optlen, u16_t datalen,
donatien 0:632c9925f013 93 u32_t seqno_be /* already in network byte order */)
donatien 0:632c9925f013 94 {
donatien 0:632c9925f013 95 struct tcp_hdr *tcphdr;
donatien 0:632c9925f013 96 struct pbuf *p = pbuf_alloc(PBUF_IP, TCP_HLEN + optlen + datalen, PBUF_RAM);
donatien 0:632c9925f013 97 if (p != NULL) {
donatien 0:632c9925f013 98 LWIP_ASSERT("check that first pbuf can hold struct tcp_hdr",
donatien 0:632c9925f013 99 (p->len >= TCP_HLEN + optlen));
donatien 0:632c9925f013 100 tcphdr = (struct tcp_hdr *)p->payload;
donatien 0:632c9925f013 101 tcphdr->src = htons(pcb->local_port);
donatien 0:632c9925f013 102 tcphdr->dest = htons(pcb->remote_port);
donatien 0:632c9925f013 103 tcphdr->seqno = seqno_be;
donatien 0:632c9925f013 104 tcphdr->ackno = htonl(pcb->rcv_nxt);
donatien 0:632c9925f013 105 TCPH_HDRLEN_FLAGS_SET(tcphdr, (5 + optlen / 4), TCP_ACK);
donatien 0:632c9925f013 106 tcphdr->wnd = htons(pcb->rcv_ann_wnd);
donatien 0:632c9925f013 107 tcphdr->chksum = 0;
donatien 0:632c9925f013 108 tcphdr->urgp = 0;
donatien 0:632c9925f013 109
donatien 0:632c9925f013 110 /* If we're sending a packet, update the announced right window edge */
donatien 0:632c9925f013 111 pcb->rcv_ann_right_edge = pcb->rcv_nxt + pcb->rcv_ann_wnd;
donatien 0:632c9925f013 112 }
donatien 0:632c9925f013 113 return p;
donatien 0:632c9925f013 114 }
donatien 0:632c9925f013 115
donatien 0:632c9925f013 116 /**
donatien 0:632c9925f013 117 * Called by tcp_close() to send a segment including FIN flag but not data.
donatien 0:632c9925f013 118 *
donatien 0:632c9925f013 119 * @param pcb the tcp_pcb over which to send a segment
donatien 0:632c9925f013 120 * @return ERR_OK if sent, another err_t otherwise
donatien 0:632c9925f013 121 */
donatien 0:632c9925f013 122 err_t
donatien 0:632c9925f013 123 tcp_send_fin(struct tcp_pcb *pcb)
donatien 0:632c9925f013 124 {
donatien 0:632c9925f013 125 /* first, try to add the fin to the last unsent segment */
donatien 0:632c9925f013 126 if (pcb->unsent != NULL) {
donatien 0:632c9925f013 127 struct tcp_seg *last_unsent;
donatien 0:632c9925f013 128 for (last_unsent = pcb->unsent; last_unsent->next != NULL;
donatien 0:632c9925f013 129 last_unsent = last_unsent->next);
donatien 0:632c9925f013 130
donatien 0:632c9925f013 131 if ((TCPH_FLAGS(last_unsent->tcphdr) & (TCP_SYN | TCP_FIN | TCP_RST)) == 0) {
donatien 0:632c9925f013 132 /* no SYN/FIN/RST flag in the header, we can add the FIN flag */
donatien 0:632c9925f013 133 TCPH_SET_FLAG(last_unsent->tcphdr, TCP_FIN);
donatien 0:632c9925f013 134 return ERR_OK;
donatien 0:632c9925f013 135 }
donatien 0:632c9925f013 136 }
donatien 0:632c9925f013 137 /* no data, no length, flags, copy=1, no optdata */
donatien 0:632c9925f013 138 return tcp_enqueue_flags(pcb, TCP_FIN);
donatien 0:632c9925f013 139 }
donatien 0:632c9925f013 140
donatien 0:632c9925f013 141 /**
donatien 0:632c9925f013 142 * Create a TCP segment with prefilled header.
donatien 0:632c9925f013 143 *
donatien 0:632c9925f013 144 * Called by tcp_write and tcp_enqueue_flags.
donatien 0:632c9925f013 145 *
donatien 0:632c9925f013 146 * @param pcb Protocol control block for the TCP connection.
donatien 0:632c9925f013 147 * @param p pbuf that is used to hold the TCP header.
donatien 0:632c9925f013 148 * @param flags TCP flags for header.
donatien 0:632c9925f013 149 * @param seqno TCP sequence number of this packet
donatien 0:632c9925f013 150 * @param optflags options to include in TCP header
donatien 0:632c9925f013 151 * @return a new tcp_seg pointing to p, or NULL.
donatien 0:632c9925f013 152 * The TCP header is filled in except ackno and wnd.
donatien 0:632c9925f013 153 * p is freed on failure.
donatien 0:632c9925f013 154 */
donatien 0:632c9925f013 155 static struct tcp_seg *
donatien 0:632c9925f013 156 tcp_create_segment(struct tcp_pcb *pcb, struct pbuf *p, u8_t flags, u32_t seqno, u8_t optflags)
donatien 0:632c9925f013 157 {
donatien 0:632c9925f013 158 struct tcp_seg *seg;
donatien 0:632c9925f013 159 u8_t optlen = LWIP_TCP_OPT_LENGTH(optflags);
donatien 0:632c9925f013 160
donatien 0:632c9925f013 161 if ((seg = (struct tcp_seg *)memp_malloc(MEMP_TCP_SEG)) == NULL) {
donatien 0:632c9925f013 162 LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 2, ("tcp_create_segment: no memory.\n"));
donatien 0:632c9925f013 163 pbuf_free(p);
donatien 0:632c9925f013 164 return NULL;
donatien 0:632c9925f013 165 }
donatien 0:632c9925f013 166 seg->flags = optflags;
donatien 0:632c9925f013 167 seg->next = NULL;
donatien 0:632c9925f013 168 seg->p = p;
donatien 0:632c9925f013 169 seg->dataptr = p->payload;
donatien 0:632c9925f013 170 seg->len = p->tot_len - optlen;
donatien 0:632c9925f013 171 #if TCP_OVERSIZE_DBGCHECK
donatien 0:632c9925f013 172 seg->oversize_left = 0;
donatien 0:632c9925f013 173 #endif /* TCP_OVERSIZE_DBGCHECK */
donatien 0:632c9925f013 174 #if TCP_CHECKSUM_ON_COPY
donatien 0:632c9925f013 175 seg->chksum = 0;
donatien 0:632c9925f013 176 seg->chksum_swapped = 0;
donatien 0:632c9925f013 177 /* check optflags */
donatien 0:632c9925f013 178 LWIP_ASSERT("invalid optflags passed: TF_SEG_DATA_CHECKSUMMED",
donatien 0:632c9925f013 179 (optflags & TF_SEG_DATA_CHECKSUMMED) == 0);
donatien 0:632c9925f013 180 #endif /* TCP_CHECKSUM_ON_COPY */
donatien 0:632c9925f013 181
donatien 0:632c9925f013 182 /* build TCP header */
donatien 0:632c9925f013 183 if (pbuf_header(p, TCP_HLEN)) {
donatien 0:632c9925f013 184 LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 2, ("tcp_create_segment: no room for TCP header in pbuf.\n"));
donatien 0:632c9925f013 185 TCP_STATS_INC(tcp.err);
donatien 0:632c9925f013 186 tcp_seg_free(seg);
donatien 0:632c9925f013 187 return NULL;
donatien 0:632c9925f013 188 }
donatien 0:632c9925f013 189 seg->tcphdr = (struct tcp_hdr *)seg->p->payload;
donatien 0:632c9925f013 190 seg->tcphdr->src = htons(pcb->local_port);
donatien 0:632c9925f013 191 seg->tcphdr->dest = htons(pcb->remote_port);
donatien 0:632c9925f013 192 seg->tcphdr->seqno = htonl(seqno);
donatien 0:632c9925f013 193 /* ackno is set in tcp_output */
donatien 0:632c9925f013 194 TCPH_HDRLEN_FLAGS_SET(seg->tcphdr, (5 + optlen / 4), flags);
donatien 0:632c9925f013 195 /* wnd and chksum are set in tcp_output */
donatien 0:632c9925f013 196 seg->tcphdr->urgp = 0;
donatien 0:632c9925f013 197 return seg;
donatien 0:632c9925f013 198 }
donatien 0:632c9925f013 199
donatien 0:632c9925f013 200 /**
donatien 0:632c9925f013 201 * Allocate a PBUF_RAM pbuf, perhaps with extra space at the end.
donatien 0:632c9925f013 202 *
donatien 0:632c9925f013 203 * This function is like pbuf_alloc(layer, length, PBUF_RAM) except
donatien 0:632c9925f013 204 * there may be extra bytes available at the end.
donatien 0:632c9925f013 205 *
donatien 0:632c9925f013 206 * @param layer flag to define header size.
donatien 0:632c9925f013 207 * @param length size of the pbuf's payload.
donatien 0:632c9925f013 208 * @param max_length maximum usable size of payload+oversize.
donatien 0:632c9925f013 209 * @param oversize pointer to a u16_t that will receive the number of usable tail bytes.
donatien 0:632c9925f013 210 * @param pcb The TCP connection that willo enqueue the pbuf.
donatien 0:632c9925f013 211 * @param apiflags API flags given to tcp_write.
donatien 0:632c9925f013 212 * @param first_seg true when this pbuf will be used in the first enqueued segment.
donatien 0:632c9925f013 213 * @param
donatien 0:632c9925f013 214 */
donatien 0:632c9925f013 215 #if TCP_OVERSIZE
donatien 0:632c9925f013 216 static struct pbuf *
donatien 0:632c9925f013 217 tcp_pbuf_prealloc(pbuf_layer layer, u16_t length, u16_t max_length,
donatien 0:632c9925f013 218 u16_t *oversize, struct tcp_pcb *pcb, u8_t apiflags,
donatien 0:632c9925f013 219 u8_t first_seg)
donatien 0:632c9925f013 220 {
donatien 0:632c9925f013 221 struct pbuf *p;
donatien 0:632c9925f013 222 u16_t alloc = length;
donatien 0:632c9925f013 223
donatien 5:dd63a1e02b1b 224 #if LWIP_NETIF_TX_SINGLE_PBUF
donatien 5:dd63a1e02b1b 225 LWIP_UNUSED_ARG(max_length);
donatien 5:dd63a1e02b1b 226 LWIP_UNUSED_ARG(pcb);
donatien 5:dd63a1e02b1b 227 LWIP_UNUSED_ARG(apiflags);
donatien 5:dd63a1e02b1b 228 LWIP_UNUSED_ARG(first_seg);
donatien 5:dd63a1e02b1b 229 /* always create MSS-sized pbufs */
donatien 5:dd63a1e02b1b 230 alloc = TCP_MSS;
donatien 5:dd63a1e02b1b 231 #else /* LWIP_NETIF_TX_SINGLE_PBUF */
donatien 0:632c9925f013 232 if (length < max_length) {
donatien 0:632c9925f013 233 /* Should we allocate an oversized pbuf, or just the minimum
donatien 0:632c9925f013 234 * length required? If tcp_write is going to be called again
donatien 0:632c9925f013 235 * before this segment is transmitted, we want the oversized
donatien 0:632c9925f013 236 * buffer. If the segment will be transmitted immediately, we can
donatien 0:632c9925f013 237 * save memory by allocating only length. We use a simple
donatien 0:632c9925f013 238 * heuristic based on the following information:
donatien 0:632c9925f013 239 *
donatien 0:632c9925f013 240 * Did the user set TCP_WRITE_FLAG_MORE?
donatien 0:632c9925f013 241 *
donatien 0:632c9925f013 242 * Will the Nagle algorithm defer transmission of this segment?
donatien 0:632c9925f013 243 */
donatien 0:632c9925f013 244 if ((apiflags & TCP_WRITE_FLAG_MORE) ||
donatien 0:632c9925f013 245 (!(pcb->flags & TF_NODELAY) &&
donatien 0:632c9925f013 246 (!first_seg ||
donatien 0:632c9925f013 247 pcb->unsent != NULL ||
donatien 0:632c9925f013 248 pcb->unacked != NULL))) {
donatien 0:632c9925f013 249 alloc = LWIP_MIN(max_length, LWIP_MEM_ALIGN_SIZE(length + TCP_OVERSIZE));
donatien 0:632c9925f013 250 }
donatien 0:632c9925f013 251 }
donatien 5:dd63a1e02b1b 252 #endif /* LWIP_NETIF_TX_SINGLE_PBUF */
donatien 0:632c9925f013 253 p = pbuf_alloc(layer, alloc, PBUF_RAM);
donatien 0:632c9925f013 254 if (p == NULL) {
donatien 0:632c9925f013 255 return NULL;
donatien 0:632c9925f013 256 }
donatien 0:632c9925f013 257 LWIP_ASSERT("need unchained pbuf", p->next == NULL);
donatien 0:632c9925f013 258 *oversize = p->len - length;
donatien 0:632c9925f013 259 /* trim p->len to the currently used size */
donatien 0:632c9925f013 260 p->len = p->tot_len = length;
donatien 0:632c9925f013 261 return p;
donatien 0:632c9925f013 262 }
donatien 0:632c9925f013 263 #else /* TCP_OVERSIZE */
donatien 0:632c9925f013 264 #define tcp_pbuf_prealloc(layer, length, mx, os, pcb, api, fst) pbuf_alloc((layer), (length), PBUF_RAM)
donatien 0:632c9925f013 265 #endif /* TCP_OVERSIZE */
donatien 0:632c9925f013 266
donatien 0:632c9925f013 267 #if TCP_CHECKSUM_ON_COPY
donatien 0:632c9925f013 268 /** Add a checksum of newly added data to the segment */
donatien 0:632c9925f013 269 static void
donatien 0:632c9925f013 270 tcp_seg_add_chksum(u16_t chksum, u16_t len, u16_t *seg_chksum,
donatien 0:632c9925f013 271 u8_t *seg_chksum_swapped)
donatien 0:632c9925f013 272 {
donatien 0:632c9925f013 273 u32_t helper;
donatien 0:632c9925f013 274 /* add chksum to old chksum and fold to u16_t */
donatien 0:632c9925f013 275 helper = chksum + *seg_chksum;
donatien 0:632c9925f013 276 chksum = FOLD_U32T(helper);
donatien 0:632c9925f013 277 if ((len & 1) != 0) {
donatien 0:632c9925f013 278 *seg_chksum_swapped = 1 - *seg_chksum_swapped;
donatien 0:632c9925f013 279 chksum = SWAP_BYTES_IN_WORD(chksum);
donatien 0:632c9925f013 280 }
donatien 0:632c9925f013 281 *seg_chksum = chksum;
donatien 0:632c9925f013 282 }
donatien 0:632c9925f013 283 #endif /* TCP_CHECKSUM_ON_COPY */
donatien 0:632c9925f013 284
donatien 5:dd63a1e02b1b 285 /** Checks if tcp_write is allowed or not (checks state, snd_buf and snd_queuelen).
donatien 5:dd63a1e02b1b 286 *
donatien 5:dd63a1e02b1b 287 * @param pcb the tcp pcb to check for
donatien 5:dd63a1e02b1b 288 * @param len length of data to send (checked agains snd_buf)
donatien 5:dd63a1e02b1b 289 * @return ERR_OK if tcp_write is allowed to proceed, another err_t otherwise
donatien 5:dd63a1e02b1b 290 */
donatien 5:dd63a1e02b1b 291 static err_t
donatien 5:dd63a1e02b1b 292 tcp_write_checks(struct tcp_pcb *pcb, u16_t len)
donatien 5:dd63a1e02b1b 293 {
donatien 5:dd63a1e02b1b 294 /* connection is in invalid state for data transmission? */
donatien 5:dd63a1e02b1b 295 if ((pcb->state != ESTABLISHED) &&
donatien 5:dd63a1e02b1b 296 (pcb->state != CLOSE_WAIT) &&
donatien 5:dd63a1e02b1b 297 (pcb->state != SYN_SENT) &&
donatien 5:dd63a1e02b1b 298 (pcb->state != SYN_RCVD)) {
donatien 5:dd63a1e02b1b 299 LWIP_DEBUGF(TCP_OUTPUT_DEBUG | LWIP_DBG_STATE | LWIP_DBG_LEVEL_SEVERE, ("tcp_write() called in invalid state\n"));
donatien 5:dd63a1e02b1b 300 return ERR_CONN;
donatien 5:dd63a1e02b1b 301 } else if (len == 0) {
donatien 5:dd63a1e02b1b 302 return ERR_OK;
donatien 5:dd63a1e02b1b 303 }
donatien 5:dd63a1e02b1b 304
donatien 5:dd63a1e02b1b 305 /* fail on too much data */
donatien 5:dd63a1e02b1b 306 if (len > pcb->snd_buf) {
donatien 5:dd63a1e02b1b 307 LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 3, ("tcp_write: too much data (len=%"U16_F" > snd_buf=%"U16_F")\n",
donatien 5:dd63a1e02b1b 308 len, pcb->snd_buf));
donatien 5:dd63a1e02b1b 309 pcb->flags |= TF_NAGLEMEMERR;
donatien 5:dd63a1e02b1b 310 return ERR_MEM;
donatien 5:dd63a1e02b1b 311 }
donatien 5:dd63a1e02b1b 312
donatien 5:dd63a1e02b1b 313 LWIP_DEBUGF(TCP_QLEN_DEBUG, ("tcp_write: queuelen: %"U16_F"\n", (u16_t)pcb->snd_queuelen));
donatien 5:dd63a1e02b1b 314
donatien 5:dd63a1e02b1b 315 /* If total number of pbufs on the unsent/unacked queues exceeds the
donatien 5:dd63a1e02b1b 316 * configured maximum, return an error */
donatien 5:dd63a1e02b1b 317 /* check for configured max queuelen and possible overflow */
donatien 5:dd63a1e02b1b 318 if ((pcb->snd_queuelen >= TCP_SND_QUEUELEN) || (pcb->snd_queuelen > TCP_SNDQUEUELEN_OVERFLOW)) {
donatien 5:dd63a1e02b1b 319 LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 3, ("tcp_write: too long queue %"U16_F" (max %"U16_F")\n",
donatien 5:dd63a1e02b1b 320 pcb->snd_queuelen, TCP_SND_QUEUELEN));
donatien 5:dd63a1e02b1b 321 TCP_STATS_INC(tcp.memerr);
donatien 5:dd63a1e02b1b 322 pcb->flags |= TF_NAGLEMEMERR;
donatien 5:dd63a1e02b1b 323 return ERR_MEM;
donatien 5:dd63a1e02b1b 324 }
donatien 5:dd63a1e02b1b 325 if (pcb->snd_queuelen != 0) {
donatien 5:dd63a1e02b1b 326 LWIP_ASSERT("tcp_write: pbufs on queue => at least one queue non-empty",
donatien 5:dd63a1e02b1b 327 pcb->unacked != NULL || pcb->unsent != NULL);
donatien 5:dd63a1e02b1b 328 } else {
donatien 5:dd63a1e02b1b 329 LWIP_ASSERT("tcp_write: no pbufs on queue => both queues empty",
donatien 5:dd63a1e02b1b 330 pcb->unacked == NULL && pcb->unsent == NULL);
donatien 5:dd63a1e02b1b 331 }
donatien 5:dd63a1e02b1b 332 return ERR_OK;
donatien 5:dd63a1e02b1b 333 }
donatien 5:dd63a1e02b1b 334
donatien 0:632c9925f013 335 /**
donatien 0:632c9925f013 336 * Write data for sending (but does not send it immediately).
donatien 0:632c9925f013 337 *
donatien 0:632c9925f013 338 * It waits in the expectation of more data being sent soon (as
donatien 0:632c9925f013 339 * it can send them more efficiently by combining them together).
donatien 0:632c9925f013 340 * To prompt the system to send data now, call tcp_output() after
donatien 0:632c9925f013 341 * calling tcp_write().
donatien 0:632c9925f013 342 *
donatien 0:632c9925f013 343 * @param pcb Protocol control block for the TCP connection to enqueue data for.
donatien 0:632c9925f013 344 * @param arg Pointer to the data to be enqueued for sending.
donatien 0:632c9925f013 345 * @param len Data length in bytes
donatien 0:632c9925f013 346 * @param apiflags combination of following flags :
donatien 0:632c9925f013 347 * - TCP_WRITE_FLAG_COPY (0x01) data will be copied into memory belonging to the stack
donatien 0:632c9925f013 348 * - TCP_WRITE_FLAG_MORE (0x02) for TCP connection, PSH flag will be set on last segment sent,
donatien 0:632c9925f013 349 * @return ERR_OK if enqueued, another err_t on error
donatien 0:632c9925f013 350 */
donatien 0:632c9925f013 351 err_t
donatien 0:632c9925f013 352 tcp_write(struct tcp_pcb *pcb, const void *arg, u16_t len, u8_t apiflags)
donatien 0:632c9925f013 353 {
donatien 0:632c9925f013 354 struct pbuf *concat_p = NULL;
donatien 0:632c9925f013 355 struct tcp_seg *last_unsent = NULL, *seg = NULL, *prev_seg = NULL, *queue = NULL;
donatien 0:632c9925f013 356 u16_t pos = 0; /* position in 'arg' data */
donatien 0:632c9925f013 357 u16_t queuelen;
donatien 0:632c9925f013 358 u8_t optlen = 0;
donatien 0:632c9925f013 359 u8_t optflags = 0;
donatien 0:632c9925f013 360 #if TCP_OVERSIZE
donatien 0:632c9925f013 361 u16_t oversize = 0;
donatien 0:632c9925f013 362 u16_t oversize_used = 0;
donatien 0:632c9925f013 363 #endif /* TCP_OVERSIZE */
donatien 0:632c9925f013 364 #if TCP_CHECKSUM_ON_COPY
donatien 0:632c9925f013 365 u16_t concat_chksum = 0;
donatien 0:632c9925f013 366 u8_t concat_chksum_swapped = 0;
donatien 0:632c9925f013 367 u16_t concat_chksummed = 0;
donatien 0:632c9925f013 368 #endif /* TCP_CHECKSUM_ON_COPY */
donatien 5:dd63a1e02b1b 369 err_t err;
donatien 0:632c9925f013 370
donatien 0:632c9925f013 371 #if LWIP_NETIF_TX_SINGLE_PBUF
donatien 0:632c9925f013 372 /* Always copy to try to create single pbufs for TX */
donatien 0:632c9925f013 373 apiflags |= TCP_WRITE_FLAG_COPY;
donatien 0:632c9925f013 374 #endif /* LWIP_NETIF_TX_SINGLE_PBUF */
donatien 0:632c9925f013 375
donatien 0:632c9925f013 376 LWIP_DEBUGF(TCP_OUTPUT_DEBUG, ("tcp_write(pcb=%p, data=%p, len=%"U16_F", apiflags=%"U16_F")\n",
donatien 0:632c9925f013 377 (void *)pcb, arg, len, (u16_t)apiflags));
donatien 0:632c9925f013 378 LWIP_ERROR("tcp_write: arg == NULL (programmer violates API)",
donatien 0:632c9925f013 379 arg != NULL, return ERR_ARG;);
donatien 0:632c9925f013 380
donatien 5:dd63a1e02b1b 381 err = tcp_write_checks(pcb, len);
donatien 5:dd63a1e02b1b 382 if (err != ERR_OK) {
donatien 5:dd63a1e02b1b 383 return err;
donatien 0:632c9925f013 384 }
donatien 0:632c9925f013 385 queuelen = pcb->snd_queuelen;
donatien 0:632c9925f013 386
donatien 0:632c9925f013 387 #if LWIP_TCP_TIMESTAMPS
donatien 0:632c9925f013 388 if ((pcb->flags & TF_TIMESTAMP)) {
donatien 0:632c9925f013 389 optflags = TF_SEG_OPTS_TS;
donatien 0:632c9925f013 390 optlen = LWIP_TCP_OPT_LENGTH(TF_SEG_OPTS_TS);
donatien 0:632c9925f013 391 }
donatien 0:632c9925f013 392 #endif /* LWIP_TCP_TIMESTAMPS */
donatien 0:632c9925f013 393
donatien 0:632c9925f013 394
donatien 0:632c9925f013 395 /*
donatien 0:632c9925f013 396 * TCP segmentation is done in three phases with increasing complexity:
donatien 0:632c9925f013 397 *
donatien 0:632c9925f013 398 * 1. Copy data directly into an oversized pbuf.
donatien 0:632c9925f013 399 * 2. Chain a new pbuf to the end of pcb->unsent.
donatien 0:632c9925f013 400 * 3. Create new segments.
donatien 0:632c9925f013 401 *
donatien 0:632c9925f013 402 * We may run out of memory at any point. In that case we must
donatien 0:632c9925f013 403 * return ERR_MEM and not change anything in pcb. Therefore, all
donatien 0:632c9925f013 404 * changes are recorded in local variables and committed at the end
donatien 0:632c9925f013 405 * of the function. Some pcb fields are maintained in local copies:
donatien 0:632c9925f013 406 *
donatien 0:632c9925f013 407 * queuelen = pcb->snd_queuelen
donatien 0:632c9925f013 408 * oversize = pcb->unsent_oversize
donatien 0:632c9925f013 409 *
donatien 0:632c9925f013 410 * These variables are set consistently by the phases:
donatien 0:632c9925f013 411 *
donatien 0:632c9925f013 412 * seg points to the last segment tampered with.
donatien 0:632c9925f013 413 *
donatien 0:632c9925f013 414 * pos records progress as data is segmented.
donatien 0:632c9925f013 415 */
donatien 0:632c9925f013 416
donatien 0:632c9925f013 417 /* Find the tail of the unsent queue. */
donatien 0:632c9925f013 418 if (pcb->unsent != NULL) {
donatien 0:632c9925f013 419 u16_t space;
donatien 0:632c9925f013 420 u16_t unsent_optlen;
donatien 0:632c9925f013 421
donatien 0:632c9925f013 422 /* @todo: this could be sped up by keeping last_unsent in the pcb */
donatien 0:632c9925f013 423 for (last_unsent = pcb->unsent; last_unsent->next != NULL;
donatien 0:632c9925f013 424 last_unsent = last_unsent->next);
donatien 0:632c9925f013 425
donatien 0:632c9925f013 426 /* Usable space at the end of the last unsent segment */
donatien 0:632c9925f013 427 unsent_optlen = LWIP_TCP_OPT_LENGTH(last_unsent->flags);
donatien 0:632c9925f013 428 space = pcb->mss - (last_unsent->len + unsent_optlen);
donatien 0:632c9925f013 429
donatien 0:632c9925f013 430 /*
donatien 0:632c9925f013 431 * Phase 1: Copy data directly into an oversized pbuf.
donatien 0:632c9925f013 432 *
donatien 0:632c9925f013 433 * The number of bytes copied is recorded in the oversize_used
donatien 0:632c9925f013 434 * variable. The actual copying is done at the bottom of the
donatien 0:632c9925f013 435 * function.
donatien 0:632c9925f013 436 */
donatien 0:632c9925f013 437 #if TCP_OVERSIZE
donatien 0:632c9925f013 438 #if TCP_OVERSIZE_DBGCHECK
donatien 0:632c9925f013 439 /* check that pcb->unsent_oversize matches last_unsent->unsent_oversize */
donatien 0:632c9925f013 440 LWIP_ASSERT("unsent_oversize mismatch (pcb vs. last_unsent)",
donatien 0:632c9925f013 441 pcb->unsent_oversize == last_unsent->oversize_left);
donatien 0:632c9925f013 442 #endif /* TCP_OVERSIZE_DBGCHECK */
donatien 0:632c9925f013 443 oversize = pcb->unsent_oversize;
donatien 0:632c9925f013 444 if (oversize > 0) {
donatien 0:632c9925f013 445 LWIP_ASSERT("inconsistent oversize vs. space", oversize_used <= space);
donatien 0:632c9925f013 446 seg = last_unsent;
donatien 0:632c9925f013 447 oversize_used = oversize < len ? oversize : len;
donatien 0:632c9925f013 448 pos += oversize_used;
donatien 0:632c9925f013 449 oversize -= oversize_used;
donatien 0:632c9925f013 450 space -= oversize_used;
donatien 0:632c9925f013 451 }
donatien 0:632c9925f013 452 /* now we are either finished or oversize is zero */
donatien 0:632c9925f013 453 LWIP_ASSERT("inconsistend oversize vs. len", (oversize == 0) || (pos == len));
donatien 0:632c9925f013 454 #endif /* TCP_OVERSIZE */
donatien 0:632c9925f013 455
donatien 0:632c9925f013 456 /*
donatien 0:632c9925f013 457 * Phase 2: Chain a new pbuf to the end of pcb->unsent.
donatien 0:632c9925f013 458 *
donatien 0:632c9925f013 459 * We don't extend segments containing SYN/FIN flags or options
donatien 0:632c9925f013 460 * (len==0). The new pbuf is kept in concat_p and pbuf_cat'ed at
donatien 0:632c9925f013 461 * the end.
donatien 0:632c9925f013 462 */
donatien 0:632c9925f013 463 if ((pos < len) && (space > 0) && (last_unsent->len > 0)) {
donatien 0:632c9925f013 464 u16_t seglen = space < len - pos ? space : len - pos;
donatien 0:632c9925f013 465 seg = last_unsent;
donatien 0:632c9925f013 466
donatien 0:632c9925f013 467 /* Create a pbuf with a copy or reference to seglen bytes. We
donatien 0:632c9925f013 468 * can use PBUF_RAW here since the data appears in the middle of
donatien 0:632c9925f013 469 * a segment. A header will never be prepended. */
donatien 0:632c9925f013 470 if (apiflags & TCP_WRITE_FLAG_COPY) {
donatien 0:632c9925f013 471 /* Data is copied */
donatien 0:632c9925f013 472 if ((concat_p = tcp_pbuf_prealloc(PBUF_RAW, seglen, space, &oversize, pcb, apiflags, 1)) == NULL) {
donatien 0:632c9925f013 473 LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 2,
donatien 0:632c9925f013 474 ("tcp_write : could not allocate memory for pbuf copy size %"U16_F"\n",
donatien 0:632c9925f013 475 seglen));
donatien 0:632c9925f013 476 goto memerr;
donatien 0:632c9925f013 477 }
donatien 0:632c9925f013 478 #if TCP_OVERSIZE_DBGCHECK
donatien 0:632c9925f013 479 last_unsent->oversize_left = oversize;
donatien 0:632c9925f013 480 #endif /* TCP_OVERSIZE_DBGCHECK */
donatien 0:632c9925f013 481 TCP_DATA_COPY2(concat_p->payload, (u8_t*)arg + pos, seglen, &concat_chksum, &concat_chksum_swapped);
donatien 0:632c9925f013 482 #if TCP_CHECKSUM_ON_COPY
donatien 0:632c9925f013 483 concat_chksummed += seglen;
donatien 0:632c9925f013 484 #endif /* TCP_CHECKSUM_ON_COPY */
donatien 0:632c9925f013 485 } else {
donatien 0:632c9925f013 486 /* Data is not copied */
donatien 0:632c9925f013 487 if ((concat_p = pbuf_alloc(PBUF_RAW, seglen, PBUF_ROM)) == NULL) {
donatien 0:632c9925f013 488 LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 2,
donatien 0:632c9925f013 489 ("tcp_write: could not allocate memory for zero-copy pbuf\n"));
donatien 0:632c9925f013 490 goto memerr;
donatien 0:632c9925f013 491 }
donatien 0:632c9925f013 492 #if TCP_CHECKSUM_ON_COPY
donatien 0:632c9925f013 493 /* calculate the checksum of nocopy-data */
donatien 0:632c9925f013 494 tcp_seg_add_chksum(~inet_chksum((u8_t*)arg + pos, seglen), seglen,
donatien 0:632c9925f013 495 &concat_chksum, &concat_chksum_swapped);
donatien 0:632c9925f013 496 concat_chksummed += seglen;
donatien 0:632c9925f013 497 #endif /* TCP_CHECKSUM_ON_COPY */
donatien 0:632c9925f013 498 /* reference the non-volatile payload data */
donatien 0:632c9925f013 499 concat_p->payload = (u8_t*)arg + pos;
donatien 0:632c9925f013 500 }
donatien 0:632c9925f013 501
donatien 0:632c9925f013 502 pos += seglen;
donatien 0:632c9925f013 503 queuelen += pbuf_clen(concat_p);
donatien 0:632c9925f013 504 }
donatien 0:632c9925f013 505 } else {
donatien 0:632c9925f013 506 #if TCP_OVERSIZE
donatien 0:632c9925f013 507 LWIP_ASSERT("unsent_oversize mismatch (pcb->unsent is NULL)",
donatien 0:632c9925f013 508 pcb->unsent_oversize == 0);
donatien 0:632c9925f013 509 #endif /* TCP_OVERSIZE */
donatien 0:632c9925f013 510 }
donatien 0:632c9925f013 511
donatien 0:632c9925f013 512 /*
donatien 0:632c9925f013 513 * Phase 3: Create new segments.
donatien 0:632c9925f013 514 *
donatien 0:632c9925f013 515 * The new segments are chained together in the local 'queue'
donatien 0:632c9925f013 516 * variable, ready to be appended to pcb->unsent.
donatien 0:632c9925f013 517 */
donatien 0:632c9925f013 518 while (pos < len) {
donatien 0:632c9925f013 519 struct pbuf *p;
donatien 0:632c9925f013 520 u16_t left = len - pos;
donatien 0:632c9925f013 521 u16_t max_len = pcb->mss - optlen;
donatien 0:632c9925f013 522 u16_t seglen = left > max_len ? max_len : left;
donatien 0:632c9925f013 523 #if TCP_CHECKSUM_ON_COPY
donatien 0:632c9925f013 524 u16_t chksum = 0;
donatien 0:632c9925f013 525 u8_t chksum_swapped = 0;
donatien 0:632c9925f013 526 #endif /* TCP_CHECKSUM_ON_COPY */
donatien 0:632c9925f013 527
donatien 0:632c9925f013 528 if (apiflags & TCP_WRITE_FLAG_COPY) {
donatien 0:632c9925f013 529 /* If copy is set, memory should be allocated and data copied
donatien 0:632c9925f013 530 * into pbuf */
donatien 0:632c9925f013 531 if ((p = tcp_pbuf_prealloc(PBUF_TRANSPORT, seglen + optlen, pcb->mss, &oversize, pcb, apiflags, queue == NULL)) == NULL) {
donatien 0:632c9925f013 532 LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 2, ("tcp_write : could not allocate memory for pbuf copy size %"U16_F"\n", seglen));
donatien 0:632c9925f013 533 goto memerr;
donatien 0:632c9925f013 534 }
donatien 0:632c9925f013 535 LWIP_ASSERT("tcp_write: check that first pbuf can hold the complete seglen",
donatien 0:632c9925f013 536 (p->len >= seglen));
donatien 0:632c9925f013 537 TCP_DATA_COPY2((char *)p->payload + optlen, (u8_t*)arg + pos, seglen, &chksum, &chksum_swapped);
donatien 0:632c9925f013 538 } else {
donatien 0:632c9925f013 539 /* Copy is not set: First allocate a pbuf for holding the data.
donatien 0:632c9925f013 540 * Since the referenced data is available at least until it is
donatien 0:632c9925f013 541 * sent out on the link (as it has to be ACKed by the remote
donatien 0:632c9925f013 542 * party) we can safely use PBUF_ROM instead of PBUF_REF here.
donatien 0:632c9925f013 543 */
donatien 0:632c9925f013 544 struct pbuf *p2;
donatien 0:632c9925f013 545 #if TCP_OVERSIZE
donatien 0:632c9925f013 546 LWIP_ASSERT("oversize == 0", oversize == 0);
donatien 0:632c9925f013 547 #endif /* TCP_OVERSIZE */
donatien 0:632c9925f013 548 if ((p2 = pbuf_alloc(PBUF_TRANSPORT, seglen, PBUF_ROM)) == NULL) {
donatien 0:632c9925f013 549 LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 2, ("tcp_write: could not allocate memory for zero-copy pbuf\n"));
donatien 0:632c9925f013 550 goto memerr;
donatien 0:632c9925f013 551 }
donatien 0:632c9925f013 552 #if TCP_CHECKSUM_ON_COPY
donatien 0:632c9925f013 553 /* calculate the checksum of nocopy-data */
donatien 0:632c9925f013 554 chksum = ~inet_chksum((u8_t*)arg + pos, seglen);
donatien 0:632c9925f013 555 #endif /* TCP_CHECKSUM_ON_COPY */
donatien 0:632c9925f013 556 /* reference the non-volatile payload data */
donatien 0:632c9925f013 557 p2->payload = (u8_t*)arg + pos;
donatien 0:632c9925f013 558
donatien 0:632c9925f013 559 /* Second, allocate a pbuf for the headers. */
donatien 0:632c9925f013 560 if ((p = pbuf_alloc(PBUF_TRANSPORT, optlen, PBUF_RAM)) == NULL) {
donatien 0:632c9925f013 561 /* If allocation fails, we have to deallocate the data pbuf as
donatien 0:632c9925f013 562 * well. */
donatien 0:632c9925f013 563 pbuf_free(p2);
donatien 0:632c9925f013 564 LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 2, ("tcp_write: could not allocate memory for header pbuf\n"));
donatien 0:632c9925f013 565 goto memerr;
donatien 0:632c9925f013 566 }
donatien 0:632c9925f013 567 /* Concatenate the headers and data pbufs together. */
donatien 0:632c9925f013 568 pbuf_cat(p/*header*/, p2/*data*/);
donatien 0:632c9925f013 569 }
donatien 0:632c9925f013 570
donatien 0:632c9925f013 571 queuelen += pbuf_clen(p);
donatien 0:632c9925f013 572
donatien 0:632c9925f013 573 /* Now that there are more segments queued, we check again if the
donatien 0:632c9925f013 574 * length of the queue exceeds the configured maximum or
donatien 0:632c9925f013 575 * overflows. */
donatien 0:632c9925f013 576 if ((queuelen > TCP_SND_QUEUELEN) || (queuelen > TCP_SNDQUEUELEN_OVERFLOW)) {
donatien 0:632c9925f013 577 LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 2, ("tcp_write: queue too long %"U16_F" (%"U16_F")\n", queuelen, TCP_SND_QUEUELEN));
donatien 0:632c9925f013 578 pbuf_free(p);
donatien 0:632c9925f013 579 goto memerr;
donatien 0:632c9925f013 580 }
donatien 0:632c9925f013 581
donatien 0:632c9925f013 582 if ((seg = tcp_create_segment(pcb, p, 0, pcb->snd_lbb + pos, optflags)) == NULL) {
donatien 0:632c9925f013 583 goto memerr;
donatien 0:632c9925f013 584 }
donatien 0:632c9925f013 585 #if TCP_OVERSIZE_DBGCHECK
donatien 0:632c9925f013 586 seg->oversize_left = oversize;
donatien 0:632c9925f013 587 #endif /* TCP_OVERSIZE_DBGCHECK */
donatien 0:632c9925f013 588 #if TCP_CHECKSUM_ON_COPY
donatien 0:632c9925f013 589 seg->chksum = chksum;
donatien 0:632c9925f013 590 seg->chksum_swapped = chksum_swapped;
donatien 0:632c9925f013 591 seg->flags |= TF_SEG_DATA_CHECKSUMMED;
donatien 0:632c9925f013 592 #endif /* TCP_CHECKSUM_ON_COPY */
donatien 0:632c9925f013 593 /* Fix dataptr for the nocopy case */
donatien 0:632c9925f013 594 if ((apiflags & TCP_WRITE_FLAG_COPY) == 0) {
donatien 0:632c9925f013 595 seg->dataptr = (u8_t*)arg + pos;
donatien 0:632c9925f013 596 }
donatien 0:632c9925f013 597
donatien 0:632c9925f013 598 /* first segment of to-be-queued data? */
donatien 0:632c9925f013 599 if (queue == NULL) {
donatien 0:632c9925f013 600 queue = seg;
donatien 0:632c9925f013 601 } else {
donatien 0:632c9925f013 602 /* Attach the segment to the end of the queued segments */
donatien 0:632c9925f013 603 LWIP_ASSERT("prev_seg != NULL", prev_seg != NULL);
donatien 0:632c9925f013 604 prev_seg->next = seg;
donatien 0:632c9925f013 605 }
donatien 0:632c9925f013 606 /* remember last segment of to-be-queued data for next iteration */
donatien 0:632c9925f013 607 prev_seg = seg;
donatien 0:632c9925f013 608
donatien 0:632c9925f013 609 LWIP_DEBUGF(TCP_OUTPUT_DEBUG | LWIP_DBG_TRACE, ("tcp_write: queueing %"U32_F":%"U32_F"\n",
donatien 0:632c9925f013 610 ntohl(seg->tcphdr->seqno),
donatien 0:632c9925f013 611 ntohl(seg->tcphdr->seqno) + TCP_TCPLEN(seg)));
donatien 0:632c9925f013 612
donatien 0:632c9925f013 613 pos += seglen;
donatien 0:632c9925f013 614 }
donatien 0:632c9925f013 615
donatien 0:632c9925f013 616 /*
donatien 0:632c9925f013 617 * All three segmentation phases were successful. We can commit the
donatien 0:632c9925f013 618 * transaction.
donatien 0:632c9925f013 619 */
donatien 0:632c9925f013 620
donatien 0:632c9925f013 621 /*
donatien 0:632c9925f013 622 * Phase 1: If data has been added to the preallocated tail of
donatien 0:632c9925f013 623 * last_unsent, we update the length fields of the pbuf chain.
donatien 0:632c9925f013 624 */
donatien 0:632c9925f013 625 #if TCP_OVERSIZE
donatien 0:632c9925f013 626 if (oversize_used > 0) {
donatien 0:632c9925f013 627 struct pbuf *p;
donatien 0:632c9925f013 628 /* Bump tot_len of whole chain, len of tail */
donatien 0:632c9925f013 629 for (p = last_unsent->p; p; p = p->next) {
donatien 0:632c9925f013 630 p->tot_len += oversize_used;
donatien 0:632c9925f013 631 if (p->next == NULL) {
donatien 0:632c9925f013 632 TCP_DATA_COPY((char *)p->payload + p->len, arg, oversize_used, last_unsent);
donatien 0:632c9925f013 633 p->len += oversize_used;
donatien 0:632c9925f013 634 }
donatien 0:632c9925f013 635 }
donatien 0:632c9925f013 636 last_unsent->len += oversize_used;
donatien 0:632c9925f013 637 #if TCP_OVERSIZE_DBGCHECK
donatien 0:632c9925f013 638 last_unsent->oversize_left -= oversize_used;
donatien 0:632c9925f013 639 #endif /* TCP_OVERSIZE_DBGCHECK */
donatien 0:632c9925f013 640 }
donatien 0:632c9925f013 641 pcb->unsent_oversize = oversize;
donatien 0:632c9925f013 642 #endif /* TCP_OVERSIZE */
donatien 0:632c9925f013 643
donatien 0:632c9925f013 644 /*
donatien 0:632c9925f013 645 * Phase 2: concat_p can be concatenated onto last_unsent->p
donatien 0:632c9925f013 646 */
donatien 0:632c9925f013 647 if (concat_p != NULL) {
donatien 0:632c9925f013 648 LWIP_ASSERT("tcp_write: cannot concatenate when pcb->unsent is empty",
donatien 0:632c9925f013 649 (last_unsent != NULL));
donatien 0:632c9925f013 650 pbuf_cat(last_unsent->p, concat_p);
donatien 0:632c9925f013 651 last_unsent->len += concat_p->tot_len;
donatien 0:632c9925f013 652 #if TCP_CHECKSUM_ON_COPY
donatien 0:632c9925f013 653 if (concat_chksummed) {
donatien 0:632c9925f013 654 tcp_seg_add_chksum(concat_chksum, concat_chksummed, &last_unsent->chksum,
donatien 0:632c9925f013 655 &last_unsent->chksum_swapped);
donatien 0:632c9925f013 656 last_unsent->flags |= TF_SEG_DATA_CHECKSUMMED;
donatien 0:632c9925f013 657 }
donatien 0:632c9925f013 658 #endif /* TCP_CHECKSUM_ON_COPY */
donatien 0:632c9925f013 659 }
donatien 0:632c9925f013 660
donatien 0:632c9925f013 661 /*
donatien 0:632c9925f013 662 * Phase 3: Append queue to pcb->unsent. Queue may be NULL, but that
donatien 0:632c9925f013 663 * is harmless
donatien 0:632c9925f013 664 */
donatien 0:632c9925f013 665 if (last_unsent == NULL) {
donatien 0:632c9925f013 666 pcb->unsent = queue;
donatien 0:632c9925f013 667 } else {
donatien 0:632c9925f013 668 last_unsent->next = queue;
donatien 0:632c9925f013 669 }
donatien 0:632c9925f013 670
donatien 0:632c9925f013 671 /*
donatien 0:632c9925f013 672 * Finally update the pcb state.
donatien 0:632c9925f013 673 */
donatien 0:632c9925f013 674 pcb->snd_lbb += len;
donatien 0:632c9925f013 675 pcb->snd_buf -= len;
donatien 0:632c9925f013 676 pcb->snd_queuelen = queuelen;
donatien 0:632c9925f013 677
donatien 0:632c9925f013 678 LWIP_DEBUGF(TCP_QLEN_DEBUG, ("tcp_write: %"S16_F" (after enqueued)\n",
donatien 0:632c9925f013 679 pcb->snd_queuelen));
donatien 0:632c9925f013 680 if (pcb->snd_queuelen != 0) {
donatien 0:632c9925f013 681 LWIP_ASSERT("tcp_write: valid queue length",
donatien 0:632c9925f013 682 pcb->unacked != NULL || pcb->unsent != NULL);
donatien 0:632c9925f013 683 }
donatien 0:632c9925f013 684
donatien 0:632c9925f013 685 /* Set the PSH flag in the last segment that we enqueued. */
donatien 0:632c9925f013 686 if (seg != NULL && seg->tcphdr != NULL && ((apiflags & TCP_WRITE_FLAG_MORE)==0)) {
donatien 0:632c9925f013 687 TCPH_SET_FLAG(seg->tcphdr, TCP_PSH);
donatien 0:632c9925f013 688 }
donatien 0:632c9925f013 689
donatien 0:632c9925f013 690 return ERR_OK;
donatien 0:632c9925f013 691 memerr:
donatien 0:632c9925f013 692 pcb->flags |= TF_NAGLEMEMERR;
donatien 0:632c9925f013 693 TCP_STATS_INC(tcp.memerr);
donatien 0:632c9925f013 694
donatien 0:632c9925f013 695 if (concat_p != NULL) {
donatien 0:632c9925f013 696 pbuf_free(concat_p);
donatien 0:632c9925f013 697 }
donatien 0:632c9925f013 698 if (queue != NULL) {
donatien 0:632c9925f013 699 tcp_segs_free(queue);
donatien 0:632c9925f013 700 }
donatien 0:632c9925f013 701 if (pcb->snd_queuelen != 0) {
donatien 0:632c9925f013 702 LWIP_ASSERT("tcp_write: valid queue length", pcb->unacked != NULL ||
donatien 0:632c9925f013 703 pcb->unsent != NULL);
donatien 0:632c9925f013 704 }
donatien 0:632c9925f013 705 LWIP_DEBUGF(TCP_QLEN_DEBUG | LWIP_DBG_STATE, ("tcp_write: %"S16_F" (with mem err)\n", pcb->snd_queuelen));
donatien 0:632c9925f013 706 return ERR_MEM;
donatien 0:632c9925f013 707 }
donatien 0:632c9925f013 708
donatien 0:632c9925f013 709 /**
donatien 0:632c9925f013 710 * Enqueue TCP options for transmission.
donatien 0:632c9925f013 711 *
donatien 0:632c9925f013 712 * Called by tcp_connect(), tcp_listen_input(), and tcp_send_ctrl().
donatien 0:632c9925f013 713 *
donatien 0:632c9925f013 714 * @param pcb Protocol control block for the TCP connection.
donatien 0:632c9925f013 715 * @param flags TCP header flags to set in the outgoing segment.
donatien 0:632c9925f013 716 * @param optdata pointer to TCP options, or NULL.
donatien 0:632c9925f013 717 * @param optlen length of TCP options in bytes.
donatien 0:632c9925f013 718 */
donatien 0:632c9925f013 719 err_t
donatien 0:632c9925f013 720 tcp_enqueue_flags(struct tcp_pcb *pcb, u8_t flags)
donatien 0:632c9925f013 721 {
donatien 0:632c9925f013 722 struct pbuf *p;
donatien 0:632c9925f013 723 struct tcp_seg *seg;
donatien 0:632c9925f013 724 u8_t optflags = 0;
donatien 0:632c9925f013 725 u8_t optlen = 0;
donatien 0:632c9925f013 726
donatien 0:632c9925f013 727 LWIP_DEBUGF(TCP_QLEN_DEBUG, ("tcp_enqueue_flags: queuelen: %"U16_F"\n", (u16_t)pcb->snd_queuelen));
donatien 0:632c9925f013 728
donatien 0:632c9925f013 729 LWIP_ASSERT("tcp_enqueue_flags: need either TCP_SYN or TCP_FIN in flags (programmer violates API)",
donatien 0:632c9925f013 730 (flags & (TCP_SYN | TCP_FIN)) != 0);
donatien 0:632c9925f013 731
donatien 0:632c9925f013 732 /* check for configured max queuelen and possible overflow */
donatien 0:632c9925f013 733 if ((pcb->snd_queuelen >= TCP_SND_QUEUELEN) || (pcb->snd_queuelen > TCP_SNDQUEUELEN_OVERFLOW)) {
donatien 0:632c9925f013 734 LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 3, ("tcp_enqueue_flags: too long queue %"U16_F" (max %"U16_F")\n",
donatien 0:632c9925f013 735 pcb->snd_queuelen, TCP_SND_QUEUELEN));
donatien 0:632c9925f013 736 TCP_STATS_INC(tcp.memerr);
donatien 0:632c9925f013 737 pcb->flags |= TF_NAGLEMEMERR;
donatien 0:632c9925f013 738 return ERR_MEM;
donatien 0:632c9925f013 739 }
donatien 0:632c9925f013 740
donatien 0:632c9925f013 741 if (flags & TCP_SYN) {
donatien 0:632c9925f013 742 optflags = TF_SEG_OPTS_MSS;
donatien 0:632c9925f013 743 }
donatien 0:632c9925f013 744 #if LWIP_TCP_TIMESTAMPS
donatien 0:632c9925f013 745 if ((pcb->flags & TF_TIMESTAMP)) {
donatien 0:632c9925f013 746 optflags |= TF_SEG_OPTS_TS;
donatien 0:632c9925f013 747 }
donatien 0:632c9925f013 748 #endif /* LWIP_TCP_TIMESTAMPS */
donatien 0:632c9925f013 749 optlen = LWIP_TCP_OPT_LENGTH(optflags);
donatien 0:632c9925f013 750
donatien 0:632c9925f013 751 /* tcp_enqueue_flags is always called with either SYN or FIN in flags.
donatien 0:632c9925f013 752 * We need one available snd_buf byte to do that.
donatien 0:632c9925f013 753 * This means we can't send FIN while snd_buf==0. A better fix would be to
donatien 0:632c9925f013 754 * not include SYN and FIN sequence numbers in the snd_buf count. */
donatien 0:632c9925f013 755 if (pcb->snd_buf == 0) {
donatien 0:632c9925f013 756 LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 3, ("tcp_enqueue_flags: no send buffer available\n"));
donatien 0:632c9925f013 757 TCP_STATS_INC(tcp.memerr);
donatien 0:632c9925f013 758 return ERR_MEM;
donatien 0:632c9925f013 759 }
donatien 0:632c9925f013 760
donatien 0:632c9925f013 761 /* Allocate pbuf with room for TCP header + options */
donatien 0:632c9925f013 762 if ((p = pbuf_alloc(PBUF_TRANSPORT, optlen, PBUF_RAM)) == NULL) {
donatien 0:632c9925f013 763 pcb->flags |= TF_NAGLEMEMERR;
donatien 0:632c9925f013 764 TCP_STATS_INC(tcp.memerr);
donatien 0:632c9925f013 765 return ERR_MEM;
donatien 0:632c9925f013 766 }
donatien 0:632c9925f013 767 LWIP_ASSERT("tcp_enqueue_flags: check that first pbuf can hold optlen",
donatien 0:632c9925f013 768 (p->len >= optlen));
donatien 0:632c9925f013 769
donatien 0:632c9925f013 770 /* Allocate memory for tcp_seg, and fill in fields. */
donatien 0:632c9925f013 771 if ((seg = tcp_create_segment(pcb, p, flags, pcb->snd_lbb, optflags)) == NULL) {
donatien 0:632c9925f013 772 pcb->flags |= TF_NAGLEMEMERR;
donatien 0:632c9925f013 773 TCP_STATS_INC(tcp.memerr);
donatien 0:632c9925f013 774 return ERR_MEM;
donatien 0:632c9925f013 775 }
donatien 0:632c9925f013 776 LWIP_ASSERT("tcp_enqueue_flags: invalid segment length", seg->len == 0);
donatien 0:632c9925f013 777
donatien 0:632c9925f013 778 LWIP_DEBUGF(TCP_OUTPUT_DEBUG | LWIP_DBG_TRACE,
donatien 0:632c9925f013 779 ("tcp_enqueue_flags: queueing %"U32_F":%"U32_F" (0x%"X16_F")\n",
donatien 0:632c9925f013 780 ntohl(seg->tcphdr->seqno),
donatien 0:632c9925f013 781 ntohl(seg->tcphdr->seqno) + TCP_TCPLEN(seg),
donatien 0:632c9925f013 782 (u16_t)flags));
donatien 0:632c9925f013 783
donatien 0:632c9925f013 784 /* Now append seg to pcb->unsent queue */
donatien 0:632c9925f013 785 if (pcb->unsent == NULL) {
donatien 0:632c9925f013 786 pcb->unsent = seg;
donatien 0:632c9925f013 787 } else {
donatien 0:632c9925f013 788 struct tcp_seg *useg;
donatien 0:632c9925f013 789 for (useg = pcb->unsent; useg->next != NULL; useg = useg->next);
donatien 0:632c9925f013 790 useg->next = seg;
donatien 0:632c9925f013 791 }
donatien 0:632c9925f013 792 #if TCP_OVERSIZE
donatien 0:632c9925f013 793 /* The new unsent tail has no space */
donatien 0:632c9925f013 794 pcb->unsent_oversize = 0;
donatien 0:632c9925f013 795 #endif /* TCP_OVERSIZE */
donatien 0:632c9925f013 796
donatien 0:632c9925f013 797 /* SYN and FIN bump the sequence number */
donatien 0:632c9925f013 798 if ((flags & TCP_SYN) || (flags & TCP_FIN)) {
donatien 0:632c9925f013 799 pcb->snd_lbb++;
donatien 0:632c9925f013 800 /* optlen does not influence snd_buf */
donatien 0:632c9925f013 801 pcb->snd_buf--;
donatien 0:632c9925f013 802 }
donatien 0:632c9925f013 803 if (flags & TCP_FIN) {
donatien 0:632c9925f013 804 pcb->flags |= TF_FIN;
donatien 0:632c9925f013 805 }
donatien 0:632c9925f013 806
donatien 0:632c9925f013 807 /* update number of segments on the queues */
donatien 0:632c9925f013 808 pcb->snd_queuelen += pbuf_clen(seg->p);
donatien 0:632c9925f013 809 LWIP_DEBUGF(TCP_QLEN_DEBUG, ("tcp_enqueue_flags: %"S16_F" (after enqueued)\n", pcb->snd_queuelen));
donatien 0:632c9925f013 810 if (pcb->snd_queuelen != 0) {
donatien 0:632c9925f013 811 LWIP_ASSERT("tcp_enqueue_flags: invalid queue length",
donatien 0:632c9925f013 812 pcb->unacked != NULL || pcb->unsent != NULL);
donatien 0:632c9925f013 813 }
donatien 0:632c9925f013 814
donatien 0:632c9925f013 815 return ERR_OK;
donatien 0:632c9925f013 816 }
donatien 0:632c9925f013 817
donatien 0:632c9925f013 818
donatien 0:632c9925f013 819 #if LWIP_TCP_TIMESTAMPS
donatien 0:632c9925f013 820 /* Build a timestamp option (12 bytes long) at the specified options pointer)
donatien 0:632c9925f013 821 *
donatien 0:632c9925f013 822 * @param pcb tcp_pcb
donatien 0:632c9925f013 823 * @param opts option pointer where to store the timestamp option
donatien 0:632c9925f013 824 */
donatien 0:632c9925f013 825 static void
donatien 0:632c9925f013 826 tcp_build_timestamp_option(struct tcp_pcb *pcb, u32_t *opts)
donatien 0:632c9925f013 827 {
donatien 0:632c9925f013 828 /* Pad with two NOP options to make everything nicely aligned */
donatien 5:dd63a1e02b1b 829 opts[0] = PP_HTONL(0x0101080A);
donatien 0:632c9925f013 830 opts[1] = htonl(sys_now());
donatien 0:632c9925f013 831 opts[2] = htonl(pcb->ts_recent);
donatien 0:632c9925f013 832 }
donatien 0:632c9925f013 833 #endif
donatien 0:632c9925f013 834
donatien 0:632c9925f013 835 /** Send an ACK without data.
donatien 0:632c9925f013 836 *
donatien 0:632c9925f013 837 * @param pcb Protocol control block for the TCP connection to send the ACK
donatien 0:632c9925f013 838 */
donatien 0:632c9925f013 839 err_t
donatien 0:632c9925f013 840 tcp_send_empty_ack(struct tcp_pcb *pcb)
donatien 0:632c9925f013 841 {
donatien 0:632c9925f013 842 struct pbuf *p;
donatien 0:632c9925f013 843 struct tcp_hdr *tcphdr;
donatien 0:632c9925f013 844 u8_t optlen = 0;
donatien 0:632c9925f013 845
donatien 0:632c9925f013 846 #if LWIP_TCP_TIMESTAMPS
donatien 0:632c9925f013 847 if (pcb->flags & TF_TIMESTAMP) {
donatien 0:632c9925f013 848 optlen = LWIP_TCP_OPT_LENGTH(TF_SEG_OPTS_TS);
donatien 0:632c9925f013 849 }
donatien 0:632c9925f013 850 #endif
donatien 0:632c9925f013 851
donatien 0:632c9925f013 852 p = tcp_output_alloc_header(pcb, optlen, 0, htonl(pcb->snd_nxt));
donatien 0:632c9925f013 853 if (p == NULL) {
donatien 0:632c9925f013 854 LWIP_DEBUGF(TCP_OUTPUT_DEBUG, ("tcp_output: (ACK) could not allocate pbuf\n"));
donatien 0:632c9925f013 855 return ERR_BUF;
donatien 0:632c9925f013 856 }
donatien 0:632c9925f013 857 tcphdr = (struct tcp_hdr *)p->payload;
donatien 0:632c9925f013 858 LWIP_DEBUGF(TCP_OUTPUT_DEBUG,
donatien 0:632c9925f013 859 ("tcp_output: sending ACK for %"U32_F"\n", pcb->rcv_nxt));
donatien 0:632c9925f013 860 /* remove ACK flags from the PCB, as we send an empty ACK now */
donatien 0:632c9925f013 861 pcb->flags &= ~(TF_ACK_DELAY | TF_ACK_NOW);
donatien 0:632c9925f013 862
donatien 0:632c9925f013 863 /* NB. MSS option is only sent on SYNs, so ignore it here */
donatien 0:632c9925f013 864 #if LWIP_TCP_TIMESTAMPS
donatien 0:632c9925f013 865 pcb->ts_lastacksent = pcb->rcv_nxt;
donatien 0:632c9925f013 866
donatien 0:632c9925f013 867 if (pcb->flags & TF_TIMESTAMP) {
donatien 0:632c9925f013 868 tcp_build_timestamp_option(pcb, (u32_t *)(tcphdr + 1));
donatien 0:632c9925f013 869 }
donatien 0:632c9925f013 870 #endif
donatien 0:632c9925f013 871
donatien 0:632c9925f013 872 #if CHECKSUM_GEN_TCP
donatien 0:632c9925f013 873 tcphdr->chksum = inet_chksum_pseudo(p, &(pcb->local_ip), &(pcb->remote_ip),
donatien 0:632c9925f013 874 IP_PROTO_TCP, p->tot_len);
donatien 0:632c9925f013 875 #endif
donatien 0:632c9925f013 876 #if LWIP_NETIF_HWADDRHINT
donatien 0:632c9925f013 877 ip_output_hinted(p, &(pcb->local_ip), &(pcb->remote_ip), pcb->ttl, pcb->tos,
donatien 0:632c9925f013 878 IP_PROTO_TCP, &(pcb->addr_hint));
donatien 0:632c9925f013 879 #else /* LWIP_NETIF_HWADDRHINT*/
donatien 0:632c9925f013 880 ip_output(p, &(pcb->local_ip), &(pcb->remote_ip), pcb->ttl, pcb->tos,
donatien 0:632c9925f013 881 IP_PROTO_TCP);
donatien 0:632c9925f013 882 #endif /* LWIP_NETIF_HWADDRHINT*/
donatien 0:632c9925f013 883 pbuf_free(p);
donatien 0:632c9925f013 884
donatien 0:632c9925f013 885 return ERR_OK;
donatien 0:632c9925f013 886 }
donatien 0:632c9925f013 887
donatien 0:632c9925f013 888 /**
donatien 0:632c9925f013 889 * Find out what we can send and send it
donatien 0:632c9925f013 890 *
donatien 0:632c9925f013 891 * @param pcb Protocol control block for the TCP connection to send data
donatien 0:632c9925f013 892 * @return ERR_OK if data has been sent or nothing to send
donatien 0:632c9925f013 893 * another err_t on error
donatien 0:632c9925f013 894 */
donatien 0:632c9925f013 895 err_t
donatien 0:632c9925f013 896 tcp_output(struct tcp_pcb *pcb)
donatien 0:632c9925f013 897 {
donatien 0:632c9925f013 898 struct tcp_seg *seg, *useg;
donatien 0:632c9925f013 899 u32_t wnd, snd_nxt;
donatien 0:632c9925f013 900 #if TCP_CWND_DEBUG
donatien 0:632c9925f013 901 s16_t i = 0;
donatien 0:632c9925f013 902 #endif /* TCP_CWND_DEBUG */
donatien 0:632c9925f013 903
donatien 0:632c9925f013 904 /* First, check if we are invoked by the TCP input processing
donatien 0:632c9925f013 905 code. If so, we do not output anything. Instead, we rely on the
donatien 0:632c9925f013 906 input processing code to call us when input processing is done
donatien 0:632c9925f013 907 with. */
donatien 0:632c9925f013 908 if (tcp_input_pcb == pcb) {
donatien 0:632c9925f013 909 return ERR_OK;
donatien 0:632c9925f013 910 }
donatien 0:632c9925f013 911
donatien 0:632c9925f013 912 wnd = LWIP_MIN(pcb->snd_wnd, pcb->cwnd);
donatien 0:632c9925f013 913
donatien 0:632c9925f013 914 seg = pcb->unsent;
donatien 0:632c9925f013 915
donatien 0:632c9925f013 916 /* If the TF_ACK_NOW flag is set and no data will be sent (either
donatien 0:632c9925f013 917 * because the ->unsent queue is empty or because the window does
donatien 0:632c9925f013 918 * not allow it), construct an empty ACK segment and send it.
donatien 0:632c9925f013 919 *
donatien 0:632c9925f013 920 * If data is to be sent, we will just piggyback the ACK (see below).
donatien 0:632c9925f013 921 */
donatien 0:632c9925f013 922 if (pcb->flags & TF_ACK_NOW &&
donatien 0:632c9925f013 923 (seg == NULL ||
donatien 0:632c9925f013 924 ntohl(seg->tcphdr->seqno) - pcb->lastack + seg->len > wnd)) {
donatien 0:632c9925f013 925 return tcp_send_empty_ack(pcb);
donatien 0:632c9925f013 926 }
donatien 0:632c9925f013 927
donatien 0:632c9925f013 928 /* useg should point to last segment on unacked queue */
donatien 0:632c9925f013 929 useg = pcb->unacked;
donatien 0:632c9925f013 930 if (useg != NULL) {
donatien 0:632c9925f013 931 for (; useg->next != NULL; useg = useg->next);
donatien 0:632c9925f013 932 }
donatien 0:632c9925f013 933
donatien 0:632c9925f013 934 #if TCP_OUTPUT_DEBUG
donatien 0:632c9925f013 935 if (seg == NULL) {
donatien 0:632c9925f013 936 LWIP_DEBUGF(TCP_OUTPUT_DEBUG, ("tcp_output: nothing to send (%p)\n",
donatien 0:632c9925f013 937 (void*)pcb->unsent));
donatien 0:632c9925f013 938 }
donatien 0:632c9925f013 939 #endif /* TCP_OUTPUT_DEBUG */
donatien 0:632c9925f013 940 #if TCP_CWND_DEBUG
donatien 0:632c9925f013 941 if (seg == NULL) {
donatien 0:632c9925f013 942 LWIP_DEBUGF(TCP_CWND_DEBUG, ("tcp_output: snd_wnd %"U16_F
donatien 0:632c9925f013 943 ", cwnd %"U16_F", wnd %"U32_F
donatien 0:632c9925f013 944 ", seg == NULL, ack %"U32_F"\n",
donatien 0:632c9925f013 945 pcb->snd_wnd, pcb->cwnd, wnd, pcb->lastack));
donatien 0:632c9925f013 946 } else {
donatien 0:632c9925f013 947 LWIP_DEBUGF(TCP_CWND_DEBUG,
donatien 0:632c9925f013 948 ("tcp_output: snd_wnd %"U16_F", cwnd %"U16_F", wnd %"U32_F
donatien 0:632c9925f013 949 ", effwnd %"U32_F", seq %"U32_F", ack %"U32_F"\n",
donatien 0:632c9925f013 950 pcb->snd_wnd, pcb->cwnd, wnd,
donatien 0:632c9925f013 951 ntohl(seg->tcphdr->seqno) - pcb->lastack + seg->len,
donatien 0:632c9925f013 952 ntohl(seg->tcphdr->seqno), pcb->lastack));
donatien 0:632c9925f013 953 }
donatien 0:632c9925f013 954 #endif /* TCP_CWND_DEBUG */
donatien 0:632c9925f013 955 /* data available and window allows it to be sent? */
donatien 0:632c9925f013 956 while (seg != NULL &&
donatien 0:632c9925f013 957 ntohl(seg->tcphdr->seqno) - pcb->lastack + seg->len <= wnd) {
donatien 0:632c9925f013 958 LWIP_ASSERT("RST not expected here!",
donatien 0:632c9925f013 959 (TCPH_FLAGS(seg->tcphdr) & TCP_RST) == 0);
donatien 0:632c9925f013 960 /* Stop sending if the nagle algorithm would prevent it
donatien 0:632c9925f013 961 * Don't stop:
donatien 0:632c9925f013 962 * - if tcp_write had a memory error before (prevent delayed ACK timeout) or
donatien 0:632c9925f013 963 * - if FIN was already enqueued for this PCB (SYN is always alone in a segment -
donatien 0:632c9925f013 964 * either seg->next != NULL or pcb->unacked == NULL;
donatien 0:632c9925f013 965 * RST is no sent using tcp_write/tcp_output.
donatien 0:632c9925f013 966 */
donatien 0:632c9925f013 967 if((tcp_do_output_nagle(pcb) == 0) &&
donatien 0:632c9925f013 968 ((pcb->flags & (TF_NAGLEMEMERR | TF_FIN)) == 0)){
donatien 0:632c9925f013 969 break;
donatien 0:632c9925f013 970 }
donatien 0:632c9925f013 971 #if TCP_CWND_DEBUG
donatien 0:632c9925f013 972 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",
donatien 0:632c9925f013 973 pcb->snd_wnd, pcb->cwnd, wnd,
donatien 0:632c9925f013 974 ntohl(seg->tcphdr->seqno) + seg->len -
donatien 0:632c9925f013 975 pcb->lastack,
donatien 0:632c9925f013 976 ntohl(seg->tcphdr->seqno), pcb->lastack, i));
donatien 0:632c9925f013 977 ++i;
donatien 0:632c9925f013 978 #endif /* TCP_CWND_DEBUG */
donatien 0:632c9925f013 979
donatien 0:632c9925f013 980 pcb->unsent = seg->next;
donatien 0:632c9925f013 981
donatien 0:632c9925f013 982 if (pcb->state != SYN_SENT) {
donatien 0:632c9925f013 983 TCPH_SET_FLAG(seg->tcphdr, TCP_ACK);
donatien 0:632c9925f013 984 pcb->flags &= ~(TF_ACK_DELAY | TF_ACK_NOW);
donatien 0:632c9925f013 985 }
donatien 0:632c9925f013 986
donatien 0:632c9925f013 987 tcp_output_segment(seg, pcb);
donatien 5:dd63a1e02b1b 988
donatien 0:632c9925f013 989 snd_nxt = ntohl(seg->tcphdr->seqno) + TCP_TCPLEN(seg);
donatien 5:dd63a1e02b1b 990
donatien 5:dd63a1e02b1b 991 LWIP_DEBUGF(TCP_OUTPUT_DEBUG, ("tcp_output: pcb->snd_nxt was %"U32_F" seqno=%"U32_F" len=%"U32_F" new snd_nxt=%"U32_F"\n", //DG
donatien 5:dd63a1e02b1b 992 pcb->snd_nxt, ntohl(seg->tcphdr->seqno), TCP_TCPLEN(seg), snd_nxt));
donatien 5:dd63a1e02b1b 993
donatien 0:632c9925f013 994 if (TCP_SEQ_LT(pcb->snd_nxt, snd_nxt)) {
donatien 0:632c9925f013 995 pcb->snd_nxt = snd_nxt;
donatien 0:632c9925f013 996 }
donatien 0:632c9925f013 997 /* put segment on unacknowledged list if length > 0 */
donatien 0:632c9925f013 998 if (TCP_TCPLEN(seg) > 0) {
donatien 0:632c9925f013 999 seg->next = NULL;
donatien 0:632c9925f013 1000 /* unacked list is empty? */
donatien 0:632c9925f013 1001 if (pcb->unacked == NULL) {
donatien 0:632c9925f013 1002 pcb->unacked = seg;
donatien 0:632c9925f013 1003 useg = seg;
donatien 0:632c9925f013 1004 /* unacked list is not empty? */
donatien 0:632c9925f013 1005 } else {
donatien 0:632c9925f013 1006 /* In the case of fast retransmit, the packet should not go to the tail
donatien 0:632c9925f013 1007 * of the unacked queue, but rather somewhere before it. We need to check for
donatien 0:632c9925f013 1008 * this case. -STJ Jul 27, 2004 */
donatien 0:632c9925f013 1009 if (TCP_SEQ_LT(ntohl(seg->tcphdr->seqno), ntohl(useg->tcphdr->seqno))) {
donatien 0:632c9925f013 1010 /* add segment to before tail of unacked list, keeping the list sorted */
donatien 0:632c9925f013 1011 struct tcp_seg **cur_seg = &(pcb->unacked);
donatien 0:632c9925f013 1012 while (*cur_seg &&
donatien 0:632c9925f013 1013 TCP_SEQ_LT(ntohl((*cur_seg)->tcphdr->seqno), ntohl(seg->tcphdr->seqno))) {
donatien 0:632c9925f013 1014 cur_seg = &((*cur_seg)->next );
donatien 0:632c9925f013 1015 }
donatien 0:632c9925f013 1016 seg->next = (*cur_seg);
donatien 0:632c9925f013 1017 (*cur_seg) = seg;
donatien 0:632c9925f013 1018 } else {
donatien 0:632c9925f013 1019 /* add segment to tail of unacked list */
donatien 0:632c9925f013 1020 useg->next = seg;
donatien 0:632c9925f013 1021 useg = useg->next;
donatien 0:632c9925f013 1022 }
donatien 0:632c9925f013 1023 }
donatien 0:632c9925f013 1024 /* do not queue empty segments on the unacked list */
donatien 0:632c9925f013 1025 } else {
donatien 0:632c9925f013 1026 tcp_seg_free(seg);
donatien 0:632c9925f013 1027 }
donatien 0:632c9925f013 1028 seg = pcb->unsent;
donatien 0:632c9925f013 1029 }
donatien 0:632c9925f013 1030 #if TCP_OVERSIZE
donatien 0:632c9925f013 1031 if (pcb->unsent == NULL) {
donatien 0:632c9925f013 1032 /* last unsent has been removed, reset unsent_oversize */
donatien 0:632c9925f013 1033 pcb->unsent_oversize = 0;
donatien 0:632c9925f013 1034 }
donatien 0:632c9925f013 1035 #endif /* TCP_OVERSIZE */
donatien 0:632c9925f013 1036
donatien 0:632c9925f013 1037 if (seg != NULL && pcb->persist_backoff == 0 &&
donatien 0:632c9925f013 1038 ntohl(seg->tcphdr->seqno) - pcb->lastack + seg->len > pcb->snd_wnd) {
donatien 0:632c9925f013 1039 /* prepare for persist timer */
donatien 0:632c9925f013 1040 pcb->persist_cnt = 0;
donatien 0:632c9925f013 1041 pcb->persist_backoff = 1;
donatien 0:632c9925f013 1042 }
donatien 0:632c9925f013 1043
donatien 0:632c9925f013 1044 pcb->flags &= ~TF_NAGLEMEMERR;
donatien 0:632c9925f013 1045 return ERR_OK;
donatien 0:632c9925f013 1046 }
donatien 0:632c9925f013 1047
donatien 0:632c9925f013 1048 /**
donatien 0:632c9925f013 1049 * Called by tcp_output() to actually send a TCP segment over IP.
donatien 0:632c9925f013 1050 *
donatien 0:632c9925f013 1051 * @param seg the tcp_seg to send
donatien 0:632c9925f013 1052 * @param pcb the tcp_pcb for the TCP connection used to send the segment
donatien 0:632c9925f013 1053 */
donatien 0:632c9925f013 1054 static void
donatien 0:632c9925f013 1055 tcp_output_segment(struct tcp_seg *seg, struct tcp_pcb *pcb)
donatien 0:632c9925f013 1056 {
donatien 0:632c9925f013 1057 u16_t len;
donatien 0:632c9925f013 1058 struct netif *netif;
donatien 0:632c9925f013 1059 u32_t *opts;
donatien 0:632c9925f013 1060
donatien 0:632c9925f013 1061 /** @bug Exclude retransmitted segments from this count. */
donatien 0:632c9925f013 1062 snmp_inc_tcpoutsegs();
donatien 0:632c9925f013 1063
donatien 0:632c9925f013 1064 /* The TCP header has already been constructed, but the ackno and
donatien 0:632c9925f013 1065 wnd fields remain. */
donatien 0:632c9925f013 1066 seg->tcphdr->ackno = htonl(pcb->rcv_nxt);
donatien 0:632c9925f013 1067
donatien 0:632c9925f013 1068 /* advertise our receive window size in this TCP segment */
donatien 0:632c9925f013 1069 seg->tcphdr->wnd = htons(pcb->rcv_ann_wnd);
donatien 0:632c9925f013 1070
donatien 0:632c9925f013 1071 pcb->rcv_ann_right_edge = pcb->rcv_nxt + pcb->rcv_ann_wnd;
donatien 0:632c9925f013 1072
donatien 0:632c9925f013 1073 /* Add any requested options. NB MSS option is only set on SYN
donatien 0:632c9925f013 1074 packets, so ignore it here */
donatien 0:632c9925f013 1075 LWIP_ASSERT("seg->tcphdr not aligned", ((mem_ptr_t)(seg->tcphdr + 1) % 4) == 0);
donatien 0:632c9925f013 1076 opts = (u32_t *)(void *)(seg->tcphdr + 1);
donatien 0:632c9925f013 1077 if (seg->flags & TF_SEG_OPTS_MSS) {
donatien 0:632c9925f013 1078 TCP_BUILD_MSS_OPTION(*opts);
donatien 0:632c9925f013 1079 opts += 1;
donatien 0:632c9925f013 1080 }
donatien 0:632c9925f013 1081 #if LWIP_TCP_TIMESTAMPS
donatien 0:632c9925f013 1082 pcb->ts_lastacksent = pcb->rcv_nxt;
donatien 0:632c9925f013 1083
donatien 0:632c9925f013 1084 if (seg->flags & TF_SEG_OPTS_TS) {
donatien 0:632c9925f013 1085 tcp_build_timestamp_option(pcb, opts);
donatien 0:632c9925f013 1086 opts += 3;
donatien 0:632c9925f013 1087 }
donatien 0:632c9925f013 1088 #endif
donatien 0:632c9925f013 1089
donatien 0:632c9925f013 1090 /* If we don't have a local IP address, we get one by
donatien 0:632c9925f013 1091 calling ip_route(). */
donatien 0:632c9925f013 1092 if (ip_addr_isany(&(pcb->local_ip))) {
donatien 0:632c9925f013 1093 netif = ip_route(&(pcb->remote_ip));
donatien 0:632c9925f013 1094 if (netif == NULL) {
donatien 0:632c9925f013 1095 return;
donatien 0:632c9925f013 1096 }
donatien 0:632c9925f013 1097 ip_addr_copy(pcb->local_ip, netif->ip_addr);
donatien 0:632c9925f013 1098 }
donatien 0:632c9925f013 1099
donatien 0:632c9925f013 1100 /* Set retransmission timer running if it is not currently enabled */
donatien 0:632c9925f013 1101 if(pcb->rtime == -1) {
donatien 0:632c9925f013 1102 pcb->rtime = 0;
donatien 0:632c9925f013 1103 }
donatien 0:632c9925f013 1104
donatien 0:632c9925f013 1105 if (pcb->rttest == 0) {
donatien 0:632c9925f013 1106 pcb->rttest = tcp_ticks;
donatien 0:632c9925f013 1107 pcb->rtseq = ntohl(seg->tcphdr->seqno);
donatien 0:632c9925f013 1108
donatien 0:632c9925f013 1109 LWIP_DEBUGF(TCP_RTO_DEBUG, ("tcp_output_segment: rtseq %"U32_F"\n", pcb->rtseq));
donatien 0:632c9925f013 1110 }
donatien 0:632c9925f013 1111 LWIP_DEBUGF(TCP_OUTPUT_DEBUG, ("tcp_output_segment: %"U32_F":%"U32_F"\n",
donatien 0:632c9925f013 1112 htonl(seg->tcphdr->seqno), htonl(seg->tcphdr->seqno) +
donatien 0:632c9925f013 1113 seg->len));
donatien 0:632c9925f013 1114
donatien 0:632c9925f013 1115 len = (u16_t)((u8_t *)seg->tcphdr - (u8_t *)seg->p->payload);
donatien 0:632c9925f013 1116
donatien 0:632c9925f013 1117 seg->p->len -= len;
donatien 0:632c9925f013 1118 seg->p->tot_len -= len;
donatien 0:632c9925f013 1119
donatien 0:632c9925f013 1120 seg->p->payload = seg->tcphdr;
donatien 0:632c9925f013 1121
donatien 0:632c9925f013 1122 seg->tcphdr->chksum = 0;
donatien 0:632c9925f013 1123 #if CHECKSUM_GEN_TCP
donatien 0:632c9925f013 1124 #if TCP_CHECKSUM_ON_COPY
donatien 0:632c9925f013 1125 {
donatien 0:632c9925f013 1126 u32_t acc;
donatien 0:632c9925f013 1127 #if TCP_CHECKSUM_ON_COPY_SANITY_CHECK
donatien 0:632c9925f013 1128 u16_t chksum_slow = inet_chksum_pseudo(seg->p, &(pcb->local_ip),
donatien 0:632c9925f013 1129 &(pcb->remote_ip),
donatien 0:632c9925f013 1130 IP_PROTO_TCP, seg->p->tot_len);
donatien 0:632c9925f013 1131 #endif /* TCP_CHECKSUM_ON_COPY_SANITY_CHECK */
donatien 0:632c9925f013 1132 if ((seg->flags & TF_SEG_DATA_CHECKSUMMED) == 0) {
donatien 0:632c9925f013 1133 LWIP_ASSERT("data included but not checksummed",
donatien 0:632c9925f013 1134 seg->p->tot_len == (TCPH_HDRLEN(seg->tcphdr) * 4));
donatien 0:632c9925f013 1135 }
donatien 0:632c9925f013 1136
donatien 0:632c9925f013 1137 /* rebuild TCP header checksum (TCP header changes for retransmissions!) */
donatien 0:632c9925f013 1138 acc = inet_chksum_pseudo_partial(seg->p, &(pcb->local_ip),
donatien 0:632c9925f013 1139 &(pcb->remote_ip),
donatien 0:632c9925f013 1140 IP_PROTO_TCP, seg->p->tot_len, TCPH_HDRLEN(seg->tcphdr) * 4);
donatien 0:632c9925f013 1141 /* add payload checksum */
donatien 0:632c9925f013 1142 if (seg->chksum_swapped) {
donatien 0:632c9925f013 1143 seg->chksum = SWAP_BYTES_IN_WORD(seg->chksum);
donatien 0:632c9925f013 1144 seg->chksum_swapped = 0;
donatien 0:632c9925f013 1145 }
donatien 0:632c9925f013 1146 acc += (u16_t)~(seg->chksum);
donatien 0:632c9925f013 1147 seg->tcphdr->chksum = FOLD_U32T(acc);
donatien 0:632c9925f013 1148 #if TCP_CHECKSUM_ON_COPY_SANITY_CHECK
donatien 0:632c9925f013 1149 if (chksum_slow != seg->tcphdr->chksum) {
donatien 0:632c9925f013 1150 LWIP_DEBUGF(TCP_DEBUG | LWIP_DBG_LEVEL_WARNING,
donatien 0:632c9925f013 1151 ("tcp_output_segment: calculated checksum is %"X16_F" instead of %"X16_F"\n",
donatien 0:632c9925f013 1152 seg->tcphdr->chksum, chksum_slow));
donatien 0:632c9925f013 1153 seg->tcphdr->chksum = chksum_slow;
donatien 0:632c9925f013 1154 }
donatien 0:632c9925f013 1155 #endif /* TCP_CHECKSUM_ON_COPY_SANITY_CHECK */
donatien 0:632c9925f013 1156 }
donatien 0:632c9925f013 1157 #else /* TCP_CHECKSUM_ON_COPY */
donatien 0:632c9925f013 1158 seg->tcphdr->chksum = inet_chksum_pseudo(seg->p, &(pcb->local_ip),
donatien 0:632c9925f013 1159 &(pcb->remote_ip),
donatien 0:632c9925f013 1160 IP_PROTO_TCP, seg->p->tot_len);
donatien 0:632c9925f013 1161 #endif /* TCP_CHECKSUM_ON_COPY */
donatien 0:632c9925f013 1162 #endif /* CHECKSUM_GEN_TCP */
donatien 0:632c9925f013 1163 TCP_STATS_INC(tcp.xmit);
donatien 5:dd63a1e02b1b 1164
donatien 5:dd63a1e02b1b 1165 LWIP_DEBUGF(TCP_OUTPUT_DEBUG, ("tcp_output_segment: Before ip_out seqno = %"U32_F"\n",
donatien 5:dd63a1e02b1b 1166 htonl(seg->tcphdr->seqno))); //DG
donatien 0:632c9925f013 1167
donatien 0:632c9925f013 1168 #if LWIP_NETIF_HWADDRHINT
donatien 0:632c9925f013 1169 ip_output_hinted(seg->p, &(pcb->local_ip), &(pcb->remote_ip), pcb->ttl, pcb->tos,
donatien 0:632c9925f013 1170 IP_PROTO_TCP, &(pcb->addr_hint));
donatien 0:632c9925f013 1171 #else /* LWIP_NETIF_HWADDRHINT*/
donatien 0:632c9925f013 1172 ip_output(seg->p, &(pcb->local_ip), &(pcb->remote_ip), pcb->ttl, pcb->tos,
donatien 0:632c9925f013 1173 IP_PROTO_TCP);
donatien 0:632c9925f013 1174 #endif /* LWIP_NETIF_HWADDRHINT*/
donatien 5:dd63a1e02b1b 1175
donatien 5:dd63a1e02b1b 1176 LWIP_DEBUGF(TCP_OUTPUT_DEBUG, ("tcp_output_segment: After ip_out seqno = %"U32_F"\n",
donatien 5:dd63a1e02b1b 1177 htonl(seg->tcphdr->seqno))); //DG
donatien 5:dd63a1e02b1b 1178
donatien 5:dd63a1e02b1b 1179
donatien 0:632c9925f013 1180 }
donatien 0:632c9925f013 1181
donatien 0:632c9925f013 1182 /**
donatien 0:632c9925f013 1183 * Send a TCP RESET packet (empty segment with RST flag set) either to
donatien 0:632c9925f013 1184 * abort a connection or to show that there is no matching local connection
donatien 0:632c9925f013 1185 * for a received segment.
donatien 0:632c9925f013 1186 *
donatien 0:632c9925f013 1187 * Called by tcp_abort() (to abort a local connection), tcp_input() (if no
donatien 0:632c9925f013 1188 * matching local pcb was found), tcp_listen_input() (if incoming segment
donatien 0:632c9925f013 1189 * has ACK flag set) and tcp_process() (received segment in the wrong state)
donatien 0:632c9925f013 1190 *
donatien 0:632c9925f013 1191 * Since a RST segment is in most cases not sent for an active connection,
donatien 0:632c9925f013 1192 * tcp_rst() has a number of arguments that are taken from a tcp_pcb for
donatien 0:632c9925f013 1193 * most other segment output functions.
donatien 0:632c9925f013 1194 *
donatien 0:632c9925f013 1195 * @param seqno the sequence number to use for the outgoing segment
donatien 0:632c9925f013 1196 * @param ackno the acknowledge number to use for the outgoing segment
donatien 0:632c9925f013 1197 * @param local_ip the local IP address to send the segment from
donatien 0:632c9925f013 1198 * @param remote_ip the remote IP address to send the segment to
donatien 0:632c9925f013 1199 * @param local_port the local TCP port to send the segment from
donatien 0:632c9925f013 1200 * @param remote_port the remote TCP port to send the segment to
donatien 0:632c9925f013 1201 */
donatien 0:632c9925f013 1202 void
donatien 0:632c9925f013 1203 tcp_rst(u32_t seqno, u32_t ackno,
donatien 0:632c9925f013 1204 ip_addr_t *local_ip, ip_addr_t *remote_ip,
donatien 0:632c9925f013 1205 u16_t local_port, u16_t remote_port)
donatien 0:632c9925f013 1206 {
donatien 0:632c9925f013 1207 struct pbuf *p;
donatien 0:632c9925f013 1208 struct tcp_hdr *tcphdr;
donatien 0:632c9925f013 1209 p = pbuf_alloc(PBUF_IP, TCP_HLEN, PBUF_RAM);
donatien 0:632c9925f013 1210 if (p == NULL) {
donatien 0:632c9925f013 1211 LWIP_DEBUGF(TCP_DEBUG, ("tcp_rst: could not allocate memory for pbuf\n"));
donatien 0:632c9925f013 1212 return;
donatien 0:632c9925f013 1213 }
donatien 0:632c9925f013 1214 LWIP_ASSERT("check that first pbuf can hold struct tcp_hdr",
donatien 0:632c9925f013 1215 (p->len >= sizeof(struct tcp_hdr)));
donatien 0:632c9925f013 1216
donatien 0:632c9925f013 1217 tcphdr = (struct tcp_hdr *)p->payload;
donatien 0:632c9925f013 1218 tcphdr->src = htons(local_port);
donatien 0:632c9925f013 1219 tcphdr->dest = htons(remote_port);
donatien 0:632c9925f013 1220 tcphdr->seqno = htonl(seqno);
donatien 0:632c9925f013 1221 tcphdr->ackno = htonl(ackno);
donatien 0:632c9925f013 1222 TCPH_HDRLEN_FLAGS_SET(tcphdr, TCP_HLEN/4, TCP_RST | TCP_ACK);
donatien 5:dd63a1e02b1b 1223 tcphdr->wnd = PP_HTONS(TCP_WND);
donatien 0:632c9925f013 1224 tcphdr->chksum = 0;
donatien 0:632c9925f013 1225 tcphdr->urgp = 0;
donatien 0:632c9925f013 1226
donatien 0:632c9925f013 1227 #if CHECKSUM_GEN_TCP
donatien 0:632c9925f013 1228 tcphdr->chksum = inet_chksum_pseudo(p, local_ip, remote_ip,
donatien 0:632c9925f013 1229 IP_PROTO_TCP, p->tot_len);
donatien 0:632c9925f013 1230 #endif
donatien 0:632c9925f013 1231 TCP_STATS_INC(tcp.xmit);
donatien 0:632c9925f013 1232 snmp_inc_tcpoutrsts();
donatien 0:632c9925f013 1233 /* Send output with hardcoded TTL since we have no access to the pcb */
donatien 0:632c9925f013 1234 ip_output(p, local_ip, remote_ip, TCP_TTL, 0, IP_PROTO_TCP);
donatien 0:632c9925f013 1235 pbuf_free(p);
donatien 0:632c9925f013 1236 LWIP_DEBUGF(TCP_RST_DEBUG, ("tcp_rst: seqno %"U32_F" ackno %"U32_F".\n", seqno, ackno));
donatien 0:632c9925f013 1237 }
donatien 0:632c9925f013 1238
donatien 0:632c9925f013 1239 /**
donatien 0:632c9925f013 1240 * Requeue all unacked segments for retransmission
donatien 0:632c9925f013 1241 *
donatien 0:632c9925f013 1242 * Called by tcp_slowtmr() for slow retransmission.
donatien 0:632c9925f013 1243 *
donatien 0:632c9925f013 1244 * @param pcb the tcp_pcb for which to re-enqueue all unacked segments
donatien 0:632c9925f013 1245 */
donatien 0:632c9925f013 1246 void
donatien 0:632c9925f013 1247 tcp_rexmit_rto(struct tcp_pcb *pcb)
donatien 0:632c9925f013 1248 {
donatien 0:632c9925f013 1249 struct tcp_seg *seg;
donatien 0:632c9925f013 1250
donatien 0:632c9925f013 1251 if (pcb->unacked == NULL) {
donatien 0:632c9925f013 1252 return;
donatien 0:632c9925f013 1253 }
donatien 0:632c9925f013 1254
donatien 0:632c9925f013 1255 /* Move all unacked segments to the head of the unsent queue */
donatien 0:632c9925f013 1256 for (seg = pcb->unacked; seg->next != NULL; seg = seg->next);
donatien 0:632c9925f013 1257 /* concatenate unsent queue after unacked queue */
donatien 0:632c9925f013 1258 seg->next = pcb->unsent;
donatien 0:632c9925f013 1259 /* unsent queue is the concatenated queue (of unacked, unsent) */
donatien 0:632c9925f013 1260 pcb->unsent = pcb->unacked;
donatien 0:632c9925f013 1261 /* unacked queue is now empty */
donatien 0:632c9925f013 1262 pcb->unacked = NULL;
donatien 0:632c9925f013 1263
donatien 0:632c9925f013 1264 /* increment number of retransmissions */
donatien 0:632c9925f013 1265 ++pcb->nrtx;
donatien 0:632c9925f013 1266
donatien 0:632c9925f013 1267 /* Don't take any RTT measurements after retransmitting. */
donatien 0:632c9925f013 1268 pcb->rttest = 0;
donatien 0:632c9925f013 1269
donatien 0:632c9925f013 1270 /* Do the actual retransmission */
donatien 0:632c9925f013 1271 tcp_output(pcb);
donatien 0:632c9925f013 1272 }
donatien 0:632c9925f013 1273
donatien 0:632c9925f013 1274 /**
donatien 0:632c9925f013 1275 * Requeue the first unacked segment for retransmission
donatien 0:632c9925f013 1276 *
donatien 0:632c9925f013 1277 * Called by tcp_receive() for fast retramsmit.
donatien 0:632c9925f013 1278 *
donatien 0:632c9925f013 1279 * @param pcb the tcp_pcb for which to retransmit the first unacked segment
donatien 0:632c9925f013 1280 */
donatien 0:632c9925f013 1281 void
donatien 0:632c9925f013 1282 tcp_rexmit(struct tcp_pcb *pcb)
donatien 0:632c9925f013 1283 {
donatien 0:632c9925f013 1284 struct tcp_seg *seg;
donatien 0:632c9925f013 1285 struct tcp_seg **cur_seg;
donatien 0:632c9925f013 1286
donatien 0:632c9925f013 1287 if (pcb->unacked == NULL) {
donatien 0:632c9925f013 1288 return;
donatien 0:632c9925f013 1289 }
donatien 0:632c9925f013 1290
donatien 0:632c9925f013 1291 /* Move the first unacked segment to the unsent queue */
donatien 0:632c9925f013 1292 /* Keep the unsent queue sorted. */
donatien 0:632c9925f013 1293 seg = pcb->unacked;
donatien 0:632c9925f013 1294 pcb->unacked = seg->next;
donatien 0:632c9925f013 1295
donatien 0:632c9925f013 1296 cur_seg = &(pcb->unsent);
donatien 0:632c9925f013 1297 while (*cur_seg &&
donatien 0:632c9925f013 1298 TCP_SEQ_LT(ntohl((*cur_seg)->tcphdr->seqno), ntohl(seg->tcphdr->seqno))) {
donatien 0:632c9925f013 1299 cur_seg = &((*cur_seg)->next );
donatien 0:632c9925f013 1300 }
donatien 0:632c9925f013 1301 seg->next = *cur_seg;
donatien 0:632c9925f013 1302 *cur_seg = seg;
donatien 0:632c9925f013 1303
donatien 0:632c9925f013 1304 ++pcb->nrtx;
donatien 0:632c9925f013 1305
donatien 0:632c9925f013 1306 /* Don't take any rtt measurements after retransmitting. */
donatien 0:632c9925f013 1307 pcb->rttest = 0;
donatien 0:632c9925f013 1308
donatien 0:632c9925f013 1309 /* Do the actual retransmission. */
donatien 0:632c9925f013 1310 snmp_inc_tcpretranssegs();
donatien 0:632c9925f013 1311 /* No need to call tcp_output: we are always called from tcp_input()
donatien 0:632c9925f013 1312 and thus tcp_output directly returns. */
donatien 0:632c9925f013 1313 }
donatien 0:632c9925f013 1314
donatien 0:632c9925f013 1315
donatien 0:632c9925f013 1316 /**
donatien 0:632c9925f013 1317 * Handle retransmission after three dupacks received
donatien 0:632c9925f013 1318 *
donatien 0:632c9925f013 1319 * @param pcb the tcp_pcb for which to retransmit the first unacked segment
donatien 0:632c9925f013 1320 */
donatien 0:632c9925f013 1321 void
donatien 0:632c9925f013 1322 tcp_rexmit_fast(struct tcp_pcb *pcb)
donatien 0:632c9925f013 1323 {
donatien 0:632c9925f013 1324 if (pcb->unacked != NULL && !(pcb->flags & TF_INFR)) {
donatien 0:632c9925f013 1325 /* This is fast retransmit. Retransmit the first unacked segment. */
donatien 0:632c9925f013 1326 LWIP_DEBUGF(TCP_FR_DEBUG,
donatien 0:632c9925f013 1327 ("tcp_receive: dupacks %"U16_F" (%"U32_F
donatien 0:632c9925f013 1328 "), fast retransmit %"U32_F"\n",
donatien 0:632c9925f013 1329 (u16_t)pcb->dupacks, pcb->lastack,
donatien 0:632c9925f013 1330 ntohl(pcb->unacked->tcphdr->seqno)));
donatien 0:632c9925f013 1331 tcp_rexmit(pcb);
donatien 0:632c9925f013 1332
donatien 0:632c9925f013 1333 /* Set ssthresh to half of the minimum of the current
donatien 0:632c9925f013 1334 * cwnd and the advertised window */
donatien 0:632c9925f013 1335 if (pcb->cwnd > pcb->snd_wnd) {
donatien 0:632c9925f013 1336 pcb->ssthresh = pcb->snd_wnd / 2;
donatien 0:632c9925f013 1337 } else {
donatien 0:632c9925f013 1338 pcb->ssthresh = pcb->cwnd / 2;
donatien 0:632c9925f013 1339 }
donatien 0:632c9925f013 1340
donatien 0:632c9925f013 1341 /* The minimum value for ssthresh should be 2 MSS */
donatien 0:632c9925f013 1342 if (pcb->ssthresh < 2*pcb->mss) {
donatien 0:632c9925f013 1343 LWIP_DEBUGF(TCP_FR_DEBUG,
donatien 0:632c9925f013 1344 ("tcp_receive: The minimum value for ssthresh %"U16_F
donatien 0:632c9925f013 1345 " should be min 2 mss %"U16_F"...\n",
donatien 0:632c9925f013 1346 pcb->ssthresh, 2*pcb->mss));
donatien 0:632c9925f013 1347 pcb->ssthresh = 2*pcb->mss;
donatien 0:632c9925f013 1348 }
donatien 0:632c9925f013 1349
donatien 0:632c9925f013 1350 pcb->cwnd = pcb->ssthresh + 3 * pcb->mss;
donatien 0:632c9925f013 1351 pcb->flags |= TF_INFR;
donatien 0:632c9925f013 1352 }
donatien 0:632c9925f013 1353 }
donatien 0:632c9925f013 1354
donatien 0:632c9925f013 1355
donatien 0:632c9925f013 1356 /**
donatien 0:632c9925f013 1357 * Send keepalive packets to keep a connection active although
donatien 0:632c9925f013 1358 * no data is sent over it.
donatien 0:632c9925f013 1359 *
donatien 0:632c9925f013 1360 * Called by tcp_slowtmr()
donatien 0:632c9925f013 1361 *
donatien 0:632c9925f013 1362 * @param pcb the tcp_pcb for which to send a keepalive packet
donatien 0:632c9925f013 1363 */
donatien 0:632c9925f013 1364 void
donatien 0:632c9925f013 1365 tcp_keepalive(struct tcp_pcb *pcb)
donatien 0:632c9925f013 1366 {
donatien 0:632c9925f013 1367 struct pbuf *p;
donatien 0:632c9925f013 1368 struct tcp_hdr *tcphdr;
donatien 0:632c9925f013 1369
donatien 0:632c9925f013 1370 LWIP_DEBUGF(TCP_DEBUG, ("tcp_keepalive: sending KEEPALIVE probe to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
donatien 0:632c9925f013 1371 ip4_addr1_16(&pcb->remote_ip), ip4_addr2_16(&pcb->remote_ip),
donatien 0:632c9925f013 1372 ip4_addr3_16(&pcb->remote_ip), ip4_addr4_16(&pcb->remote_ip)));
donatien 0:632c9925f013 1373
donatien 0:632c9925f013 1374 LWIP_DEBUGF(TCP_DEBUG, ("tcp_keepalive: tcp_ticks %"U32_F" pcb->tmr %"U32_F" pcb->keep_cnt_sent %"U16_F"\n",
donatien 0:632c9925f013 1375 tcp_ticks, pcb->tmr, pcb->keep_cnt_sent));
donatien 0:632c9925f013 1376
donatien 0:632c9925f013 1377 p = tcp_output_alloc_header(pcb, 0, 0, htonl(pcb->snd_nxt - 1));
donatien 0:632c9925f013 1378 if(p == NULL) {
donatien 0:632c9925f013 1379 LWIP_DEBUGF(TCP_DEBUG,
donatien 0:632c9925f013 1380 ("tcp_keepalive: could not allocate memory for pbuf\n"));
donatien 0:632c9925f013 1381 return;
donatien 0:632c9925f013 1382 }
donatien 0:632c9925f013 1383 tcphdr = (struct tcp_hdr *)p->payload;
donatien 0:632c9925f013 1384
donatien 0:632c9925f013 1385 #if CHECKSUM_GEN_TCP
donatien 0:632c9925f013 1386 tcphdr->chksum = inet_chksum_pseudo(p, &pcb->local_ip, &pcb->remote_ip,
donatien 0:632c9925f013 1387 IP_PROTO_TCP, p->tot_len);
donatien 0:632c9925f013 1388 #endif
donatien 0:632c9925f013 1389 TCP_STATS_INC(tcp.xmit);
donatien 0:632c9925f013 1390
donatien 0:632c9925f013 1391 /* Send output to IP */
donatien 0:632c9925f013 1392 #if LWIP_NETIF_HWADDRHINT
donatien 0:632c9925f013 1393 ip_output_hinted(p, &pcb->local_ip, &pcb->remote_ip, pcb->ttl, 0, IP_PROTO_TCP,
donatien 0:632c9925f013 1394 &(pcb->addr_hint));
donatien 0:632c9925f013 1395 #else /* LWIP_NETIF_HWADDRHINT*/
donatien 0:632c9925f013 1396 ip_output(p, &pcb->local_ip, &pcb->remote_ip, pcb->ttl, 0, IP_PROTO_TCP);
donatien 0:632c9925f013 1397 #endif /* LWIP_NETIF_HWADDRHINT*/
donatien 0:632c9925f013 1398
donatien 0:632c9925f013 1399 pbuf_free(p);
donatien 0:632c9925f013 1400
donatien 0:632c9925f013 1401 LWIP_DEBUGF(TCP_DEBUG, ("tcp_keepalive: seqno %"U32_F" ackno %"U32_F".\n",
donatien 0:632c9925f013 1402 pcb->snd_nxt - 1, pcb->rcv_nxt));
donatien 0:632c9925f013 1403 }
donatien 0:632c9925f013 1404
donatien 0:632c9925f013 1405
donatien 0:632c9925f013 1406 /**
donatien 0:632c9925f013 1407 * Send persist timer zero-window probes to keep a connection active
donatien 0:632c9925f013 1408 * when a window update is lost.
donatien 0:632c9925f013 1409 *
donatien 0:632c9925f013 1410 * Called by tcp_slowtmr()
donatien 0:632c9925f013 1411 *
donatien 0:632c9925f013 1412 * @param pcb the tcp_pcb for which to send a zero-window probe packet
donatien 0:632c9925f013 1413 */
donatien 0:632c9925f013 1414 void
donatien 0:632c9925f013 1415 tcp_zero_window_probe(struct tcp_pcb *pcb)
donatien 0:632c9925f013 1416 {
donatien 0:632c9925f013 1417 struct pbuf *p;
donatien 0:632c9925f013 1418 struct tcp_hdr *tcphdr;
donatien 0:632c9925f013 1419 struct tcp_seg *seg;
donatien 0:632c9925f013 1420 u16_t len;
donatien 0:632c9925f013 1421 u8_t is_fin;
donatien 0:632c9925f013 1422
donatien 0:632c9925f013 1423 LWIP_DEBUGF(TCP_DEBUG,
donatien 0:632c9925f013 1424 ("tcp_zero_window_probe: sending ZERO WINDOW probe to %"
donatien 0:632c9925f013 1425 U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
donatien 0:632c9925f013 1426 ip4_addr1_16(&pcb->remote_ip), ip4_addr2_16(&pcb->remote_ip),
donatien 0:632c9925f013 1427 ip4_addr3_16(&pcb->remote_ip), ip4_addr4_16(&pcb->remote_ip)));
donatien 0:632c9925f013 1428
donatien 0:632c9925f013 1429 LWIP_DEBUGF(TCP_DEBUG,
donatien 0:632c9925f013 1430 ("tcp_zero_window_probe: tcp_ticks %"U32_F
donatien 0:632c9925f013 1431 " pcb->tmr %"U32_F" pcb->keep_cnt_sent %"U16_F"\n",
donatien 0:632c9925f013 1432 tcp_ticks, pcb->tmr, pcb->keep_cnt_sent));
donatien 0:632c9925f013 1433
donatien 0:632c9925f013 1434 seg = pcb->unacked;
donatien 0:632c9925f013 1435
donatien 0:632c9925f013 1436 if(seg == NULL) {
donatien 0:632c9925f013 1437 seg = pcb->unsent;
donatien 0:632c9925f013 1438 }
donatien 0:632c9925f013 1439 if(seg == NULL) {
donatien 0:632c9925f013 1440 return;
donatien 0:632c9925f013 1441 }
donatien 0:632c9925f013 1442
donatien 0:632c9925f013 1443 is_fin = ((TCPH_FLAGS(seg->tcphdr) & TCP_FIN) != 0) && (seg->len == 0);
donatien 0:632c9925f013 1444 /* we want to send one seqno: either FIN or data (no options) */
donatien 0:632c9925f013 1445 len = is_fin ? 0 : 1;
donatien 0:632c9925f013 1446
donatien 0:632c9925f013 1447 p = tcp_output_alloc_header(pcb, 0, len, seg->tcphdr->seqno);
donatien 0:632c9925f013 1448 if(p == NULL) {
donatien 0:632c9925f013 1449 LWIP_DEBUGF(TCP_DEBUG, ("tcp_zero_window_probe: no memory for pbuf\n"));
donatien 0:632c9925f013 1450 return;
donatien 0:632c9925f013 1451 }
donatien 0:632c9925f013 1452 tcphdr = (struct tcp_hdr *)p->payload;
donatien 0:632c9925f013 1453
donatien 0:632c9925f013 1454 if (is_fin) {
donatien 0:632c9925f013 1455 /* FIN segment, no data */
donatien 0:632c9925f013 1456 TCPH_FLAGS_SET(tcphdr, TCP_ACK | TCP_FIN);
donatien 0:632c9925f013 1457 } else {
donatien 0:632c9925f013 1458 /* Data segment, copy in one byte from the head of the unacked queue */
donatien 0:632c9925f013 1459 *((char *)p->payload + TCP_HLEN) = *(char *)seg->dataptr;
donatien 0:632c9925f013 1460 }
donatien 0:632c9925f013 1461
donatien 0:632c9925f013 1462 #if CHECKSUM_GEN_TCP
donatien 0:632c9925f013 1463 tcphdr->chksum = inet_chksum_pseudo(p, &pcb->local_ip, &pcb->remote_ip,
donatien 0:632c9925f013 1464 IP_PROTO_TCP, p->tot_len);
donatien 0:632c9925f013 1465 #endif
donatien 0:632c9925f013 1466 TCP_STATS_INC(tcp.xmit);
donatien 0:632c9925f013 1467
donatien 0:632c9925f013 1468 /* Send output to IP */
donatien 0:632c9925f013 1469 #if LWIP_NETIF_HWADDRHINT
donatien 0:632c9925f013 1470 ip_output_hinted(p, &pcb->local_ip, &pcb->remote_ip, pcb->ttl, 0, IP_PROTO_TCP,
donatien 0:632c9925f013 1471 &(pcb->addr_hint));
donatien 0:632c9925f013 1472 #else /* LWIP_NETIF_HWADDRHINT*/
donatien 0:632c9925f013 1473 ip_output(p, &pcb->local_ip, &pcb->remote_ip, pcb->ttl, 0, IP_PROTO_TCP);
donatien 0:632c9925f013 1474 #endif /* LWIP_NETIF_HWADDRHINT*/
donatien 0:632c9925f013 1475
donatien 0:632c9925f013 1476 pbuf_free(p);
donatien 0:632c9925f013 1477
donatien 0:632c9925f013 1478 LWIP_DEBUGF(TCP_DEBUG, ("tcp_zero_window_probe: seqno %"U32_F
donatien 0:632c9925f013 1479 " ackno %"U32_F".\n",
donatien 0:632c9925f013 1480 pcb->snd_nxt - 1, pcb->rcv_nxt));
donatien 0:632c9925f013 1481 }
donatien 0:632c9925f013 1482 #endif /* LWIP_TCP */