NetTribute library with debug turned on in FShandler Donatien Garner -> Segundo Equipo -> this version

Committer:
hexley
Date:
Fri Nov 19 01:54:45 2010 +0000
Revision:
0:281d6ff68967

        

Who changed what in which revision?

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