ProjetoBB

Dependencies:   F7_Ethernet WebSocketClient mbed mcp3008

Fork of Nucleo_F746ZG_Ethernet by Dieter Graef

Committer:
DieterGraef
Date:
Sat Jun 18 10:49:12 2016 +0000
Revision:
0:f9b6112278fe
Ethernet for the NUCLEO STM32F746 Board Testprogram uses DHCP and NTP to set the clock

Who changed what in which revision?

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