Ethernetwebsoc

Dependencies:   C12832_lcd LM75B WebSocketClient mbed-rtos mbed Socket lwip-eth lwip-sys lwip

Committer:
GordonSin
Date:
Fri May 31 04:09:54 2013 +0000
Revision:
0:0ed2a7c7190c
31/5/2013;

Who changed what in which revision?

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