modded version Dirk-Willem van Gulik's Bonjour/Zerconf library http://mbed.org/users/dirkx/code/Bonjour/

Dependents:   OSCtoCVConverter

Fork of Bonjour by Dirk-Willem van Gulik (NXP/mbed)

Committer:
casiotone401
Date:
Thu Oct 16 14:13:21 2014 +0000
Revision:
8:275256b5d807
Parent:
7:105191e07767
minor change

Who changed what in which revision?

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