SNMP agent attached to SPI slave

Dependencies:   mbed

Committer:
lorcansmith
Date:
Mon Aug 13 15:07:40 2012 +0000
Revision:
0:2a53a4c3238c
v1.1 release includes ioAlarm traps

Who changed what in which revision?

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