Ethernet for Nucleo and Disco board STM32F746 works with gcc and arm. IAC is untested

Dependencies:   mbed-rtos

Dependents:   IMU_ethernet

Fork of F7_Ethernet by Dieter Graef

Committer:
rctaduio
Date:
Thu Oct 06 16:55:16 2016 +0000
Revision:
2:e0a4035b5cd1
Parent:
0:d26c1b55cfca
Ethernet library for F7

Who changed what in which revision?

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