I have a problem getting this to work. Server only recieves half of the data being sent. Whats wrong

Dependencies:   mbed

Committer:
tax
Date:
Tue Mar 29 13:20:15 2011 +0000
Revision:
0:66300c77c6e9

        

Who changed what in which revision?

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