3 * Transmission Control Protocol, outgoing traffic
5 * The output functions of TCP.
10 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
11 * All rights reserved.
13 * Redistribution and use in source and binary forms, with or without modification,
14 * are permitted provided that the following conditions are met:
16 * 1. Redistributions of source code must retain the above copyright notice,
17 * this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright notice,
19 * this list of conditions and the following disclaimer in the documentation
20 * and/or other materials provided with the distribution.
21 * 3. The name of the author may not be used to endorse or promote products
22 * derived from this software without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
25 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
27 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
29 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
32 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
35 * This file is part of the lwIP TCP/IP stack.
37 * Author: Adam Dunkels <adam@sics.se>
43 #if LWIP_TCP /* don't build if not configured for use in lwipopts.h */
45 #include "lwip/priv/tcp_priv.h"
48 #include "lwip/memp.h"
49 #include "lwip/ip_addr.h"
50 #include "lwip/netif.h"
51 #include "lwip/inet_chksum.h"
52 #include "lwip/stats.h"
54 #include "lwip/ip6_addr.h"
55 #if LWIP_TCP_TIMESTAMPS
61 /* Define some copy-macros for checksum-on-copy so that the code looks
62 nicer by preventing too many ifdef's. */
63 #if TCP_CHECKSUM_ON_COPY
64 #define TCP_DATA_COPY(dst, src, len, seg) do { \
65 tcp_seg_add_chksum(LWIP_CHKSUM_COPY(dst, src, len), \
66 len, &seg->chksum, &seg->chksum_swapped); \
67 seg->flags |= TF_SEG_DATA_CHECKSUMMED; } while(0)
68 #define TCP_DATA_COPY2(dst, src, len, chksum, chksum_swapped) \
69 tcp_seg_add_chksum(LWIP_CHKSUM_COPY(dst, src, len), len, chksum, chksum_swapped);
70 #else /* TCP_CHECKSUM_ON_COPY*/
71 #define TCP_DATA_COPY(dst, src, len, seg) MEMCPY(dst, src, len)
72 #define TCP_DATA_COPY2(dst, src, len, chksum, chksum_swapped) MEMCPY(dst, src, len)
73 #endif /* TCP_CHECKSUM_ON_COPY*/
75 /** Define this to 1 for an extra check that the output checksum is valid
76 * (usefule when the checksum is generated by the application, not the stack) */
77 #ifndef TCP_CHECKSUM_ON_COPY_SANITY_CHECK
78 #define TCP_CHECKSUM_ON_COPY_SANITY_CHECK 0
80 /* Allow to override the failure of sanity check from warning to e.g. hard failure */
81 #if TCP_CHECKSUM_ON_COPY_SANITY_CHECK
82 #ifndef TCP_CHECKSUM_ON_COPY_SANITY_CHECK_FAIL
83 #define TCP_CHECKSUM_ON_COPY_SANITY_CHECK_FAIL(msg) LWIP_DEBUGF(TCP_DEBUG | LWIP_DBG_LEVEL_WARNING, msg)
88 /** The size of segment pbufs created when TCP_OVERSIZE is enabled */
89 #ifndef TCP_OVERSIZE_CALC_LENGTH
90 #define TCP_OVERSIZE_CALC_LENGTH(length) ((length) + TCP_OVERSIZE)
94 /* Forward declarations.*/
95 static err_t tcp_output_segment(struct tcp_seg *seg, struct tcp_pcb *pcb, struct netif *netif);
97 /** Allocate a pbuf and create a tcphdr at p->payload, used for output
98 * functions other than the default tcp_output -> tcp_output_segment
99 * (e.g. tcp_send_empty_ack, etc.)
101 * @param pcb tcp pcb for which to send a packet (used to initialize tcp_hdr)
102 * @param optlen length of header-options
103 * @param datalen length of tcp data to reserve in pbuf
104 * @param seqno_be seqno in network byte order (big-endian)
105 * @return pbuf with p->payload being the tcp_hdr
108 tcp_output_alloc_header(struct tcp_pcb *pcb, u16_t optlen, u16_t datalen,
109 u32_t seqno_be /* already in network byte order */)
111 struct tcp_hdr *tcphdr;
112 struct pbuf *p = pbuf_alloc(PBUF_IP, TCP_HLEN + optlen + datalen, PBUF_RAM);
114 LWIP_ASSERT("check that first pbuf can hold struct tcp_hdr",
115 (p->len >= TCP_HLEN + optlen));
116 tcphdr = (struct tcp_hdr *)p->payload;
117 tcphdr->src = lwip_htons(pcb->local_port);
118 tcphdr->dest = lwip_htons(pcb->remote_port);
119 tcphdr->seqno = seqno_be;
120 tcphdr->ackno = lwip_htonl(pcb->rcv_nxt);
121 TCPH_HDRLEN_FLAGS_SET(tcphdr, (5 + optlen / 4), TCP_ACK);
122 tcphdr->wnd = lwip_htons(TCPWND_MIN16(RCV_WND_SCALE(pcb, pcb->rcv_ann_wnd)));
126 /* If we're sending a packet, update the announced right window edge */
127 pcb->rcv_ann_right_edge = pcb->rcv_nxt + pcb->rcv_ann_wnd;
133 * Called by tcp_close() to send a segment including FIN flag but not data.
135 * @param pcb the tcp_pcb over which to send a segment
136 * @return ERR_OK if sent, another err_t otherwise
139 tcp_send_fin(struct tcp_pcb *pcb)
141 /* first, try to add the fin to the last unsent segment */
142 if (pcb->unsent != NULL) {
143 struct tcp_seg *last_unsent;
144 for (last_unsent = pcb->unsent; last_unsent->next != NULL;
145 last_unsent = last_unsent->next);
147 if ((TCPH_FLAGS(last_unsent->tcphdr) & (TCP_SYN | TCP_FIN | TCP_RST)) == 0) {
148 /* no SYN/FIN/RST flag in the header, we can add the FIN flag */
149 TCPH_SET_FLAG(last_unsent->tcphdr, TCP_FIN);
150 pcb->flags |= TF_FIN;
154 /* no data, no length, flags, copy=1, no optdata */
155 return tcp_enqueue_flags(pcb, TCP_FIN);
159 * Create a TCP segment with prefilled header.
161 * Called by tcp_write and tcp_enqueue_flags.
163 * @param pcb Protocol control block for the TCP connection.
164 * @param p pbuf that is used to hold the TCP header.
165 * @param flags TCP flags for header.
166 * @param seqno TCP sequence number of this packet
167 * @param optflags options to include in TCP header
168 * @return a new tcp_seg pointing to p, or NULL.
169 * The TCP header is filled in except ackno and wnd.
170 * p is freed on failure.
172 static struct tcp_seg *
173 tcp_create_segment(struct tcp_pcb *pcb, struct pbuf *p, u8_t flags, u32_t seqno, u8_t optflags)
176 u8_t optlen = LWIP_TCP_OPT_LENGTH(optflags);
178 if ((seg = (struct tcp_seg *)memp_malloc(MEMP_TCP_SEG)) == NULL) {
179 LWIP_DEBUGF(TCP_OUTPUT_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("tcp_create_segment: no memory.\n"));
183 seg->flags = optflags;
186 LWIP_ASSERT("p->tot_len >= optlen", p->tot_len >= optlen);
187 seg->len = p->tot_len - optlen;
188 #if TCP_OVERSIZE_DBGCHECK
189 seg->oversize_left = 0;
190 #endif /* TCP_OVERSIZE_DBGCHECK */
191 #if TCP_CHECKSUM_ON_COPY
193 seg->chksum_swapped = 0;
195 LWIP_ASSERT("invalid optflags passed: TF_SEG_DATA_CHECKSUMMED",
196 (optflags & TF_SEG_DATA_CHECKSUMMED) == 0);
197 #endif /* TCP_CHECKSUM_ON_COPY */
199 /* build TCP header */
200 if (pbuf_header(p, TCP_HLEN)) {
201 LWIP_DEBUGF(TCP_OUTPUT_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("tcp_create_segment: no room for TCP header in pbuf.\n"));
202 TCP_STATS_INC(tcp.err);
206 seg->tcphdr = (struct tcp_hdr *)seg->p->payload;
207 seg->tcphdr->src = lwip_htons(pcb->local_port);
208 seg->tcphdr->dest = lwip_htons(pcb->remote_port);
209 seg->tcphdr->seqno = lwip_htonl(seqno);
210 /* ackno is set in tcp_output */
211 TCPH_HDRLEN_FLAGS_SET(seg->tcphdr, (5 + optlen / 4), flags);
212 /* wnd and chksum are set in tcp_output */
213 seg->tcphdr->urgp = 0;
218 * Allocate a PBUF_RAM pbuf, perhaps with extra space at the end.
220 * This function is like pbuf_alloc(layer, length, PBUF_RAM) except
221 * there may be extra bytes available at the end.
223 * @param layer flag to define header size.
224 * @param length size of the pbuf's payload.
225 * @param max_length maximum usable size of payload+oversize.
226 * @param oversize pointer to a u16_t that will receive the number of usable tail bytes.
227 * @param pcb The TCP connection that will enqueue the pbuf.
228 * @param apiflags API flags given to tcp_write.
229 * @param first_seg true when this pbuf will be used in the first enqueued segment.
233 tcp_pbuf_prealloc(pbuf_layer layer, u16_t length, u16_t max_length,
234 u16_t *oversize, struct tcp_pcb *pcb, u8_t apiflags,
238 u16_t alloc = length;
240 #if LWIP_NETIF_TX_SINGLE_PBUF
241 LWIP_UNUSED_ARG(max_length);
242 LWIP_UNUSED_ARG(pcb);
243 LWIP_UNUSED_ARG(apiflags);
244 LWIP_UNUSED_ARG(first_seg);
246 #else /* LWIP_NETIF_TX_SINGLE_PBUF */
247 if (length < max_length) {
248 /* Should we allocate an oversized pbuf, or just the minimum
249 * length required? If tcp_write is going to be called again
250 * before this segment is transmitted, we want the oversized
251 * buffer. If the segment will be transmitted immediately, we can
252 * save memory by allocating only length. We use a simple
253 * heuristic based on the following information:
255 * Did the user set TCP_WRITE_FLAG_MORE?
257 * Will the Nagle algorithm defer transmission of this segment?
259 if ((apiflags & TCP_WRITE_FLAG_MORE) ||
260 (!(pcb->flags & TF_NODELAY) &&
262 pcb->unsent != NULL ||
263 pcb->unacked != NULL))) {
264 alloc = LWIP_MIN(max_length, LWIP_MEM_ALIGN_SIZE(TCP_OVERSIZE_CALC_LENGTH(length)));
267 #endif /* LWIP_NETIF_TX_SINGLE_PBUF */
268 p = pbuf_alloc(layer, alloc, PBUF_RAM);
272 LWIP_ASSERT("need unchained pbuf", p->next == NULL);
273 *oversize = p->len - length;
274 /* trim p->len to the currently used size */
275 p->len = p->tot_len = length;
278 #else /* TCP_OVERSIZE */
279 #define tcp_pbuf_prealloc(layer, length, mx, os, pcb, api, fst) pbuf_alloc((layer), (length), PBUF_RAM)
280 #endif /* TCP_OVERSIZE */
282 #if TCP_CHECKSUM_ON_COPY
283 /** Add a checksum of newly added data to the segment */
285 tcp_seg_add_chksum(u16_t chksum, u16_t len, u16_t *seg_chksum,
286 u8_t *seg_chksum_swapped)
289 /* add chksum to old chksum and fold to u16_t */
290 helper = chksum + *seg_chksum;
291 chksum = FOLD_U32T(helper);
292 if ((len & 1) != 0) {
293 *seg_chksum_swapped = 1 - *seg_chksum_swapped;
294 chksum = SWAP_BYTES_IN_WORD(chksum);
296 *seg_chksum = chksum;
298 #endif /* TCP_CHECKSUM_ON_COPY */
300 /** Checks if tcp_write is allowed or not (checks state, snd_buf and snd_queuelen).
302 * @param pcb the tcp pcb to check for
303 * @param len length of data to send (checked agains snd_buf)
304 * @return ERR_OK if tcp_write is allowed to proceed, another err_t otherwise
307 tcp_write_checks(struct tcp_pcb *pcb, u16_t len)
309 /* connection is in invalid state for data transmission? */
310 if ((pcb->state != ESTABLISHED) &&
311 (pcb->state != CLOSE_WAIT) &&
312 (pcb->state != SYN_SENT) &&
313 (pcb->state != SYN_RCVD)) {
314 LWIP_DEBUGF(TCP_OUTPUT_DEBUG | LWIP_DBG_STATE | LWIP_DBG_LEVEL_SEVERE, ("tcp_write() called in invalid state\n"));
316 } else if (len == 0) {
320 /* fail on too much data */
321 if (len > pcb->snd_buf) {
322 LWIP_DEBUGF(TCP_OUTPUT_DEBUG | LWIP_DBG_LEVEL_SEVERE, ("tcp_write: too much data (len=%"U16_F" > snd_buf=%"TCPWNDSIZE_F")\n",
324 pcb->flags |= TF_NAGLEMEMERR;
328 LWIP_DEBUGF(TCP_QLEN_DEBUG, ("tcp_write: queuelen: %"TCPWNDSIZE_F"\n", (tcpwnd_size_t)pcb->snd_queuelen));
330 /* If total number of pbufs on the unsent/unacked queues exceeds the
331 * configured maximum, return an error */
332 /* check for configured max queuelen and possible overflow */
333 if ((pcb->snd_queuelen >= TCP_SND_QUEUELEN) || (pcb->snd_queuelen > TCP_SNDQUEUELEN_OVERFLOW)) {
334 LWIP_DEBUGF(TCP_OUTPUT_DEBUG | LWIP_DBG_LEVEL_SEVERE, ("tcp_write: too long queue %"U16_F" (max %"U16_F")\n",
335 pcb->snd_queuelen, (u16_t)TCP_SND_QUEUELEN));
336 TCP_STATS_INC(tcp.memerr);
337 pcb->flags |= TF_NAGLEMEMERR;
340 if (pcb->snd_queuelen != 0) {
341 LWIP_ASSERT("tcp_write: pbufs on queue => at least one queue non-empty",
342 pcb->unacked != NULL || pcb->unsent != NULL);
344 LWIP_ASSERT("tcp_write: no pbufs on queue => both queues empty",
345 pcb->unacked == NULL && pcb->unsent == NULL);
352 * Write data for sending (but does not send it immediately).
354 * It waits in the expectation of more data being sent soon (as
355 * it can send them more efficiently by combining them together).
356 * To prompt the system to send data now, call tcp_output() after
357 * calling tcp_write().
359 * @param pcb Protocol control block for the TCP connection to enqueue data for.
360 * @param arg Pointer to the data to be enqueued for sending.
361 * @param len Data length in bytes
362 * @param apiflags combination of following flags :
363 * - TCP_WRITE_FLAG_COPY (0x01) data will be copied into memory belonging to the stack
364 * - TCP_WRITE_FLAG_MORE (0x02) for TCP connection, PSH flag will not be set on last segment sent,
365 * @return ERR_OK if enqueued, another err_t on error
368 tcp_write(struct tcp_pcb *pcb, const void *arg, u16_t len, u8_t apiflags)
370 struct pbuf *concat_p = NULL;
371 struct tcp_seg *last_unsent = NULL, *seg = NULL, *prev_seg = NULL, *queue = NULL;
372 u16_t pos = 0; /* position in 'arg' data */
378 u16_t oversize_used = 0;
379 #if TCP_OVERSIZE_DBGCHECK
380 u16_t oversize_add = 0;
381 #endif /* TCP_OVERSIZE_DBGCHECK*/
382 #endif /* TCP_OVERSIZE */
384 #if TCP_CHECKSUM_ON_COPY
385 u16_t concat_chksum = 0;
386 u8_t concat_chksum_swapped = 0;
387 u16_t concat_chksummed = 0;
388 #endif /* TCP_CHECKSUM_ON_COPY */
390 /* don't allocate segments bigger than half the maximum window we ever received */
391 u16_t mss_local = LWIP_MIN(pcb->mss, TCPWND_MIN16(pcb->snd_wnd_max/2));
392 mss_local = mss_local ? mss_local : pcb->mss;
394 LWIP_ASSERT_CORE_LOCKED();
396 #if LWIP_NETIF_TX_SINGLE_PBUF
397 /* Always copy to try to create single pbufs for TX */
398 apiflags |= TCP_WRITE_FLAG_COPY;
399 #endif /* LWIP_NETIF_TX_SINGLE_PBUF */
401 LWIP_DEBUGF(TCP_OUTPUT_DEBUG, ("tcp_write(pcb=%p, data=%p, len=%"U16_F", apiflags=%"U16_F")\n",
402 (void *)pcb, arg, len, (u16_t)apiflags));
403 LWIP_ERROR("tcp_write: arg == NULL (programmer violates API)",
404 arg != NULL, return ERR_ARG;);
406 err = tcp_write_checks(pcb, len);
410 queuelen = pcb->snd_queuelen;
412 #if LWIP_TCP_TIMESTAMPS
413 if ((pcb->flags & TF_TIMESTAMP)) {
414 /* Make sure the timestamp option is only included in data segments if we
415 agreed about it with the remote host. */
416 optflags = TF_SEG_OPTS_TS;
417 optlen = LWIP_TCP_OPT_LENGTH(TF_SEG_OPTS_TS);
418 /* ensure that segments can hold at least one data byte... */
419 mss_local = LWIP_MAX(mss_local, LWIP_TCP_OPT_LEN_TS + 1);
421 #endif /* LWIP_TCP_TIMESTAMPS */
425 * TCP segmentation is done in three phases with increasing complexity:
427 * 1. Copy data directly into an oversized pbuf.
428 * 2. Chain a new pbuf to the end of pcb->unsent.
429 * 3. Create new segments.
431 * We may run out of memory at any point. In that case we must
432 * return ERR_MEM and not change anything in pcb. Therefore, all
433 * changes are recorded in local variables and committed at the end
434 * of the function. Some pcb fields are maintained in local copies:
436 * queuelen = pcb->snd_queuelen
437 * oversize = pcb->unsent_oversize
439 * These variables are set consistently by the phases:
441 * seg points to the last segment tampered with.
443 * pos records progress as data is segmented.
446 /* Find the tail of the unsent queue. */
447 if (pcb->unsent != NULL) {
451 /* @todo: this could be sped up by keeping last_unsent in the pcb */
452 for (last_unsent = pcb->unsent; last_unsent->next != NULL;
453 last_unsent = last_unsent->next);
455 /* Usable space at the end of the last unsent segment */
456 unsent_optlen = LWIP_TCP_OPT_LENGTH(last_unsent->flags);
457 LWIP_ASSERT("mss_local is too small", mss_local >= last_unsent->len + unsent_optlen);
458 space = mss_local - (last_unsent->len + unsent_optlen);
461 * Phase 1: Copy data directly into an oversized pbuf.
463 * The number of bytes copied is recorded in the oversize_used
464 * variable. The actual copying is done at the bottom of the
468 #if TCP_OVERSIZE_DBGCHECK
469 /* check that pcb->unsent_oversize matches last_unsent->oversize_left */
470 LWIP_ASSERT("unsent_oversize mismatch (pcb vs. last_unsent)",
471 pcb->unsent_oversize == last_unsent->oversize_left);
472 #endif /* TCP_OVERSIZE_DBGCHECK */
473 oversize = pcb->unsent_oversize;
475 LWIP_ASSERT("inconsistent oversize vs. space", oversize <= space);
477 oversize_used = LWIP_MIN(space, LWIP_MIN(oversize, len));
478 pos += oversize_used;
479 oversize -= oversize_used;
480 space -= oversize_used;
482 /* now we are either finished or oversize is zero */
483 LWIP_ASSERT("inconsistent oversize vs. len", (oversize == 0) || (pos == len));
484 #endif /* TCP_OVERSIZE */
487 * Phase 2: Chain a new pbuf to the end of pcb->unsent.
489 * As an exception when NOT copying the data, if the given data buffer
490 * directly follows the last unsent data buffer in memory, extend the last
491 * ROM pbuf reference to the buffer, thus saving a ROM pbuf allocation.
493 * We don't extend segments containing SYN/FIN flags or options
494 * (len==0). The new pbuf is kept in concat_p and pbuf_cat'ed at
497 if ((pos < len) && (space > 0) && (last_unsent->len > 0)) {
498 u16_t seglen = LWIP_MIN(space, len - pos);
501 /* Create a pbuf with a copy or reference to seglen bytes. We
502 * can use PBUF_RAW here since the data appears in the middle of
503 * a segment. A header will never be prepended. */
504 if (apiflags & TCP_WRITE_FLAG_COPY) {
506 if ((concat_p = tcp_pbuf_prealloc(PBUF_RAW, seglen, space, &oversize, pcb, apiflags, 1)) == NULL) {
507 LWIP_DEBUGF(TCP_OUTPUT_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
508 ("tcp_write : could not allocate memory for pbuf copy size %"U16_F"\n",
512 #if TCP_OVERSIZE_DBGCHECK
513 oversize_add = oversize;
514 #endif /* TCP_OVERSIZE_DBGCHECK */
515 TCP_DATA_COPY2(concat_p->payload, (const u8_t*)arg + pos, seglen, &concat_chksum, &concat_chksum_swapped);
516 #if TCP_CHECKSUM_ON_COPY
517 concat_chksummed += seglen;
518 #endif /* TCP_CHECKSUM_ON_COPY */
519 queuelen += pbuf_clen(concat_p);
521 /* Data is not copied */
522 /* If the last unsent pbuf is of type PBUF_ROM, try to extend it. */
524 for (p = last_unsent->p; p->next != NULL; p = p->next);
525 if (p->type == PBUF_ROM && (const u8_t *)p->payload + p->len == (const u8_t *)arg) {
526 LWIP_ASSERT("tcp_write: ROM pbufs cannot be oversized", pos == 0);
529 if ((concat_p = pbuf_alloc(PBUF_RAW, seglen, PBUF_ROM)) == NULL) {
530 LWIP_DEBUGF(TCP_OUTPUT_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
531 ("tcp_write: could not allocate memory for zero-copy pbuf\n"));
534 /* reference the non-volatile payload data */
535 ((struct pbuf_rom*)concat_p)->payload = (const u8_t*)arg + pos;
536 queuelen += pbuf_clen(concat_p);
538 #if TCP_CHECKSUM_ON_COPY
539 /* calculate the checksum of nocopy-data */
540 tcp_seg_add_chksum(~inet_chksum((const u8_t*)arg + pos, seglen), seglen,
541 &concat_chksum, &concat_chksum_swapped);
542 concat_chksummed += seglen;
543 #endif /* TCP_CHECKSUM_ON_COPY */
550 LWIP_ASSERT("unsent_oversize mismatch (pcb->unsent is NULL)",
551 pcb->unsent_oversize == 0);
552 #endif /* TCP_OVERSIZE */
556 * Phase 3: Create new segments.
558 * The new segments are chained together in the local 'queue'
559 * variable, ready to be appended to pcb->unsent.
563 u16_t left = len - pos;
564 u16_t max_len = mss_local - optlen;
565 u16_t seglen = LWIP_MIN(left, max_len);
566 #if TCP_CHECKSUM_ON_COPY
568 u8_t chksum_swapped = 0;
569 #endif /* TCP_CHECKSUM_ON_COPY */
571 if (apiflags & TCP_WRITE_FLAG_COPY) {
572 /* If copy is set, memory should be allocated and data copied
574 if ((p = tcp_pbuf_prealloc(PBUF_TRANSPORT, seglen + optlen, mss_local, &oversize, pcb, apiflags, queue == NULL)) == NULL) {
575 LWIP_DEBUGF(TCP_OUTPUT_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("tcp_write : could not allocate memory for pbuf copy size %"U16_F"\n", seglen));
578 LWIP_ASSERT("tcp_write: check that first pbuf can hold the complete seglen",
580 TCP_DATA_COPY2((char *)p->payload + optlen, (const u8_t*)arg + pos, seglen, &chksum, &chksum_swapped);
582 /* Copy is not set: First allocate a pbuf for holding the data.
583 * Since the referenced data is available at least until it is
584 * sent out on the link (as it has to be ACKed by the remote
585 * party) we can safely use PBUF_ROM instead of PBUF_REF here.
589 LWIP_ASSERT("oversize == 0", oversize == 0);
590 #endif /* TCP_OVERSIZE */
591 if ((p2 = pbuf_alloc(PBUF_TRANSPORT, seglen, PBUF_ROM)) == NULL) {
592 LWIP_DEBUGF(TCP_OUTPUT_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("tcp_write: could not allocate memory for zero-copy pbuf\n"));
595 #if TCP_CHECKSUM_ON_COPY
596 /* calculate the checksum of nocopy-data */
597 chksum = ~inet_chksum((const u8_t*)arg + pos, seglen);
600 chksum = SWAP_BYTES_IN_WORD(chksum);
602 #endif /* TCP_CHECKSUM_ON_COPY */
603 /* reference the non-volatile payload data */
604 ((struct pbuf_rom*)p2)->payload = (const u8_t*)arg + pos;
606 /* Second, allocate a pbuf for the headers. */
607 if ((p = pbuf_alloc(PBUF_TRANSPORT, optlen, PBUF_RAM)) == NULL) {
608 /* If allocation fails, we have to deallocate the data pbuf as
611 LWIP_DEBUGF(TCP_OUTPUT_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("tcp_write: could not allocate memory for header pbuf\n"));
614 /* Concatenate the headers and data pbufs together. */
615 pbuf_cat(p/*header*/, p2/*data*/);
618 queuelen += pbuf_clen(p);
620 /* Now that there are more segments queued, we check again if the
621 * length of the queue exceeds the configured maximum or
623 if ((queuelen > TCP_SND_QUEUELEN) || (queuelen > TCP_SNDQUEUELEN_OVERFLOW)) {
624 LWIP_DEBUGF(TCP_OUTPUT_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("tcp_write: queue too long %"U16_F" (%d)\n",
625 queuelen, (int)TCP_SND_QUEUELEN));
630 if ((seg = tcp_create_segment(pcb, p, 0, pcb->snd_lbb + pos, optflags)) == NULL) {
633 #if TCP_OVERSIZE_DBGCHECK
634 seg->oversize_left = oversize;
635 #endif /* TCP_OVERSIZE_DBGCHECK */
636 #if TCP_CHECKSUM_ON_COPY
637 seg->chksum = chksum;
638 seg->chksum_swapped = chksum_swapped;
639 seg->flags |= TF_SEG_DATA_CHECKSUMMED;
640 #endif /* TCP_CHECKSUM_ON_COPY */
642 /* first segment of to-be-queued data? */
646 /* Attach the segment to the end of the queued segments */
647 LWIP_ASSERT("prev_seg != NULL", prev_seg != NULL);
648 prev_seg->next = seg;
650 /* remember last segment of to-be-queued data for next iteration */
653 LWIP_DEBUGF(TCP_OUTPUT_DEBUG | LWIP_DBG_TRACE, ("tcp_write: queueing %"U32_F":%"U32_F"\n",
654 lwip_ntohl(seg->tcphdr->seqno),
655 lwip_ntohl(seg->tcphdr->seqno) + TCP_TCPLEN(seg)));
661 * All three segmentation phases were successful. We can commit the
664 #if TCP_OVERSIZE_DBGCHECK
665 if ((last_unsent != NULL) && (oversize_add != 0)) {
666 last_unsent->oversize_left += oversize_add;
668 #endif /* TCP_OVERSIZE_DBGCHECK */
671 * Phase 1: If data has been added to the preallocated tail of
672 * last_unsent, we update the length fields of the pbuf chain.
675 if (oversize_used > 0) {
677 /* Bump tot_len of whole chain, len of tail */
678 for (p = last_unsent->p; p; p = p->next) {
679 p->tot_len += oversize_used;
680 if (p->next == NULL) {
681 TCP_DATA_COPY((char *)p->payload + p->len, arg, oversize_used, last_unsent);
682 p->len += oversize_used;
685 last_unsent->len += oversize_used;
686 #if TCP_OVERSIZE_DBGCHECK
687 LWIP_ASSERT("last_unsent->oversize_left >= oversize_used",
688 last_unsent->oversize_left >= oversize_used);
689 last_unsent->oversize_left -= oversize_used;
690 #endif /* TCP_OVERSIZE_DBGCHECK */
692 pcb->unsent_oversize = oversize;
693 #endif /* TCP_OVERSIZE */
696 * Phase 2: concat_p can be concatenated onto last_unsent->p, unless we
697 * determined that the last ROM pbuf can be extended to include the new data.
699 if (concat_p != NULL) {
700 LWIP_ASSERT("tcp_write: cannot concatenate when pcb->unsent is empty",
701 (last_unsent != NULL));
702 pbuf_cat(last_unsent->p, concat_p);
703 last_unsent->len += concat_p->tot_len;
704 } else if (extendlen > 0) {
706 LWIP_ASSERT("tcp_write: extension of reference requires reference",
707 last_unsent != NULL && last_unsent->p != NULL);
708 for (p = last_unsent->p; p->next != NULL; p = p->next) {
709 p->tot_len += extendlen;
711 p->tot_len += extendlen;
713 last_unsent->len += extendlen;
716 #if TCP_CHECKSUM_ON_COPY
717 if (concat_chksummed) {
718 LWIP_ASSERT("tcp_write: concat checksum needs concatenated data",
719 concat_p != NULL || extendlen > 0);
720 /*if concat checksumm swapped - swap it back */
721 if (concat_chksum_swapped) {
722 concat_chksum = SWAP_BYTES_IN_WORD(concat_chksum);
724 tcp_seg_add_chksum(concat_chksum, concat_chksummed, &last_unsent->chksum,
725 &last_unsent->chksum_swapped);
726 last_unsent->flags |= TF_SEG_DATA_CHECKSUMMED;
728 #endif /* TCP_CHECKSUM_ON_COPY */
731 * Phase 3: Append queue to pcb->unsent. Queue may be NULL, but that
734 if (last_unsent == NULL) {
737 last_unsent->next = queue;
741 * Finally update the pcb state.
745 pcb->snd_queuelen = queuelen;
747 LWIP_DEBUGF(TCP_QLEN_DEBUG, ("tcp_write: %"S16_F" (after enqueued)\n",
749 if (pcb->snd_queuelen != 0) {
750 LWIP_ASSERT("tcp_write: valid queue length",
751 pcb->unacked != NULL || pcb->unsent != NULL);
754 /* Set the PSH flag in the last segment that we enqueued. */
755 if (seg != NULL && seg->tcphdr != NULL && ((apiflags & TCP_WRITE_FLAG_MORE)==0)) {
756 TCPH_SET_FLAG(seg->tcphdr, TCP_PSH);
761 pcb->flags |= TF_NAGLEMEMERR;
762 TCP_STATS_INC(tcp.memerr);
764 if (concat_p != NULL) {
768 tcp_segs_free(queue);
770 if (pcb->snd_queuelen != 0) {
771 LWIP_ASSERT("tcp_write: valid queue length", pcb->unacked != NULL ||
772 pcb->unsent != NULL);
774 LWIP_DEBUGF(TCP_QLEN_DEBUG | LWIP_DBG_STATE, ("tcp_write: %"S16_F" (with mem err)\n", pcb->snd_queuelen));
779 * Enqueue TCP options for transmission.
781 * Called by tcp_connect(), tcp_listen_input(), and tcp_send_ctrl().
783 * @param pcb Protocol control block for the TCP connection.
784 * @param flags TCP header flags to set in the outgoing segment.
787 tcp_enqueue_flags(struct tcp_pcb *pcb, u8_t flags)
794 LWIP_DEBUGF(TCP_QLEN_DEBUG, ("tcp_enqueue_flags: queuelen: %"U16_F"\n", (u16_t)pcb->snd_queuelen));
796 LWIP_ASSERT("tcp_enqueue_flags: need either TCP_SYN or TCP_FIN in flags (programmer violates API)",
797 (flags & (TCP_SYN | TCP_FIN)) != 0);
799 /* check for configured max queuelen and possible overflow (FIN flag should always come through!) */
800 if (((pcb->snd_queuelen >= TCP_SND_QUEUELEN) || (pcb->snd_queuelen > TCP_SNDQUEUELEN_OVERFLOW)) &&
801 ((flags & TCP_FIN) == 0)) {
802 LWIP_DEBUGF(TCP_OUTPUT_DEBUG | LWIP_DBG_LEVEL_SEVERE, ("tcp_enqueue_flags: too long queue %"U16_F" (max %"U16_F")\n",
803 pcb->snd_queuelen, (u16_t)TCP_SND_QUEUELEN));
804 TCP_STATS_INC(tcp.memerr);
805 pcb->flags |= TF_NAGLEMEMERR;
809 if (flags & TCP_SYN) {
810 optflags = TF_SEG_OPTS_MSS;
812 if ((pcb->state != SYN_RCVD) || (pcb->flags & TF_WND_SCALE)) {
813 /* In a <SYN,ACK> (sent in state SYN_RCVD), the window scale option may only
814 be sent if we received a window scale option from the remote host. */
815 optflags |= TF_SEG_OPTS_WND_SCALE;
817 #endif /* LWIP_WND_SCALE */
819 #if LWIP_TCP_TIMESTAMPS
820 if ((pcb->flags & TF_TIMESTAMP)) {
821 /* Make sure the timestamp option is only included in data segments if we
822 agreed about it with the remote host. */
823 optflags |= TF_SEG_OPTS_TS;
825 #endif /* LWIP_TCP_TIMESTAMPS */
826 optlen = LWIP_TCP_OPT_LENGTH(optflags);
828 /* Allocate pbuf with room for TCP header + options */
829 if ((p = pbuf_alloc(PBUF_TRANSPORT, optlen, PBUF_RAM)) == NULL) {
830 pcb->flags |= TF_NAGLEMEMERR;
831 TCP_STATS_INC(tcp.memerr);
834 LWIP_ASSERT("tcp_enqueue_flags: check that first pbuf can hold optlen",
837 /* Allocate memory for tcp_seg, and fill in fields. */
838 if ((seg = tcp_create_segment(pcb, p, flags, pcb->snd_lbb, optflags)) == NULL) {
839 pcb->flags |= TF_NAGLEMEMERR;
840 TCP_STATS_INC(tcp.memerr);
843 LWIP_ASSERT("seg->tcphdr not aligned", ((mem_ptr_t)seg->tcphdr % LWIP_MIN(MEM_ALIGNMENT, 4)) == 0);
844 LWIP_ASSERT("tcp_enqueue_flags: invalid segment length", seg->len == 0);
846 LWIP_DEBUGF(TCP_OUTPUT_DEBUG | LWIP_DBG_TRACE,
847 ("tcp_enqueue_flags: queueing %"U32_F":%"U32_F" (0x%"X16_F")\n",
848 lwip_ntohl(seg->tcphdr->seqno),
849 lwip_ntohl(seg->tcphdr->seqno) + TCP_TCPLEN(seg),
852 /* Now append seg to pcb->unsent queue */
853 if (pcb->unsent == NULL) {
856 struct tcp_seg *useg;
857 for (useg = pcb->unsent; useg->next != NULL; useg = useg->next);
861 /* The new unsent tail has no space */
862 pcb->unsent_oversize = 0;
863 #endif /* TCP_OVERSIZE */
865 /* SYN and FIN bump the sequence number */
866 if ((flags & TCP_SYN) || (flags & TCP_FIN)) {
868 /* optlen does not influence snd_buf */
870 if (flags & TCP_FIN) {
871 pcb->flags |= TF_FIN;
874 /* update number of segments on the queues */
875 pcb->snd_queuelen += pbuf_clen(seg->p);
876 LWIP_DEBUGF(TCP_QLEN_DEBUG, ("tcp_enqueue_flags: %"S16_F" (after enqueued)\n", pcb->snd_queuelen));
877 if (pcb->snd_queuelen != 0) {
878 LWIP_ASSERT("tcp_enqueue_flags: invalid queue length",
879 pcb->unacked != NULL || pcb->unsent != NULL);
885 #if LWIP_TCP_TIMESTAMPS
886 /* Build a timestamp option (12 bytes long) at the specified options pointer)
889 * @param opts option pointer where to store the timestamp option
892 tcp_build_timestamp_option(struct tcp_pcb *pcb, u32_t *opts)
894 /* Pad with two NOP options to make everything nicely aligned */
895 opts[0] = PP_HTONL(0x0101080A);
896 opts[1] = lwip_htonl(sys_now());
897 opts[2] = lwip_htonl(pcb->ts_recent);
902 /** Build a window scale option (3 bytes long) at the specified options pointer)
904 * @param opts option pointer where to store the window scale option
907 tcp_build_wnd_scale_option(u32_t *opts)
909 /* Pad with one NOP option to make everything nicely aligned */
910 opts[0] = PP_HTONL(0x01030300 | TCP_RCV_SCALE);
915 * Send an ACK without data.
917 * @param pcb Protocol control block for the TCP connection to send the ACK
920 tcp_send_empty_ack(struct tcp_pcb *pcb)
926 #if LWIP_TCP_TIMESTAMPS || CHECKSUM_GEN_TCP
927 struct tcp_hdr *tcphdr;
928 #endif /* LWIP_TCP_TIMESTAMPS || CHECKSUM_GEN_TCP */
930 #if LWIP_TCP_TIMESTAMPS
931 if (pcb->flags & TF_TIMESTAMP) {
932 optlen = LWIP_TCP_OPT_LENGTH(TF_SEG_OPTS_TS);
936 p = tcp_output_alloc_header(pcb, optlen, 0, lwip_htonl(pcb->snd_nxt));
938 /* let tcp_fasttmr retry sending this ACK */
939 pcb->flags |= (TF_ACK_DELAY | TF_ACK_NOW);
940 LWIP_DEBUGF(TCP_OUTPUT_DEBUG, ("tcp_output: (ACK) could not allocate pbuf\n"));
943 #if LWIP_TCP_TIMESTAMPS || CHECKSUM_GEN_TCP
944 tcphdr = (struct tcp_hdr *)p->payload;
945 #endif /* LWIP_TCP_TIMESTAMPS || CHECKSUM_GEN_TCP */
946 LWIP_DEBUGF(TCP_OUTPUT_DEBUG,
947 ("tcp_output: sending ACK for %"U32_F"\n", pcb->rcv_nxt));
949 /* NB. MSS and window scale options are only sent on SYNs, so ignore them here */
950 #if LWIP_TCP_TIMESTAMPS
951 pcb->ts_lastacksent = pcb->rcv_nxt;
953 if (pcb->flags & TF_TIMESTAMP) {
954 tcp_build_timestamp_option(pcb, (u32_t *)(tcphdr + 1));
958 netif = ip_route(&pcb->local_ip, &pcb->remote_ip);
963 IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_TCP) {
964 tcphdr->chksum = ip_chksum_pseudo(p, IP_PROTO_TCP, p->tot_len,
965 &pcb->local_ip, &pcb->remote_ip);
968 netif_apply_pcb(netif, (struct ip_pcb *)pcb);
969 err = ip_output_if(p, &pcb->local_ip, &pcb->remote_ip,
970 pcb->ttl, pcb->tos, IP_PROTO_TCP, netif);
971 netif_apply_pcb(netif, NULL);
976 /* let tcp_fasttmr retry sending this ACK */
977 pcb->flags |= (TF_ACK_DELAY | TF_ACK_NOW);
979 /* remove ACK flags from the PCB, as we sent an empty ACK now */
980 pcb->flags &= ~(TF_ACK_DELAY | TF_ACK_NOW);
988 * Find out what we can send and send it
990 * @param pcb Protocol control block for the TCP connection to send data
991 * @return ERR_OK if data has been sent or nothing to send
992 * another err_t on error
995 tcp_output(struct tcp_pcb *pcb)
997 struct tcp_seg *seg, *useg;
1000 struct netif *netif;
1003 #endif /* TCP_CWND_DEBUG */
1005 LWIP_ASSERT_CORE_LOCKED();
1006 /* pcb->state LISTEN not allowed here */
1007 LWIP_ASSERT("don't call tcp_output for listen-pcbs",
1008 pcb->state != LISTEN);
1010 /* First, check if we are invoked by the TCP input processing
1011 code. If so, we do not output anything. Instead, we rely on the
1012 input processing code to call us when input processing is done
1014 if (tcp_input_pcb == pcb) {
1018 wnd = LWIP_MIN(pcb->snd_wnd, pcb->cwnd);
1022 /* If the TF_ACK_NOW flag is set and no data will be sent (either
1023 * because the ->unsent queue is empty or because the window does
1024 * not allow it), construct an empty ACK segment and send it.
1026 * If data is to be sent, we will just piggyback the ACK (see below).
1028 if (pcb->flags & TF_ACK_NOW &&
1030 lwip_ntohl(seg->tcphdr->seqno) - pcb->lastack + seg->len > wnd)) {
1031 return tcp_send_empty_ack(pcb);
1034 /* useg should point to last segment on unacked queue */
1035 useg = pcb->unacked;
1037 for (; useg->next != NULL; useg = useg->next);
1040 netif = ip_route(&pcb->local_ip, &pcb->remote_ip);
1041 if (netif == NULL) {
1045 /* If we don't have a local IP address, we get one from netif */
1046 if (ip_addr_isany(&pcb->local_ip)) {
1047 const ip_addr_t *local_ip = ip_netif_get_local_ip(netif, &pcb->remote_ip);
1048 if (local_ip == NULL) {
1051 ip_addr_copy(pcb->local_ip, *local_ip);
1054 #if TCP_OUTPUT_DEBUG
1056 LWIP_DEBUGF(TCP_OUTPUT_DEBUG, ("tcp_output: nothing to send (%p)\n",
1057 (void*)pcb->unsent));
1059 #endif /* TCP_OUTPUT_DEBUG */
1062 LWIP_DEBUGF(TCP_CWND_DEBUG, ("tcp_output: snd_wnd %"TCPWNDSIZE_F
1063 ", cwnd %"TCPWNDSIZE_F", wnd %"U32_F
1064 ", seg == NULL, ack %"U32_F"\n",
1065 pcb->snd_wnd, pcb->cwnd, wnd, pcb->lastack));
1067 LWIP_DEBUGF(TCP_CWND_DEBUG,
1068 ("tcp_output: snd_wnd %"TCPWNDSIZE_F", cwnd %"TCPWNDSIZE_F", wnd %"U32_F
1069 ", effwnd %"U32_F", seq %"U32_F", ack %"U32_F"\n",
1070 pcb->snd_wnd, pcb->cwnd, wnd,
1071 lwip_ntohl(seg->tcphdr->seqno) - pcb->lastack + seg->len,
1072 lwip_ntohl(seg->tcphdr->seqno), pcb->lastack));
1074 #endif /* TCP_CWND_DEBUG */
1075 /* Check if we need to start the persistent timer when the next unsent segment
1076 * does not fit within the remaining send window and RTO timer is not running (we
1077 * have no in-flight data). A traditional approach would fill the remaining window
1078 * with part of the unsent segment (which will engage zero-window probing upon
1079 * reception of the zero window update from the receiver). This ensures the
1080 * subsequent window update is reliably received. With the goal of being lightweight,
1081 * we avoid splitting the unsent segment and treat the window as already zero.
1084 lwip_ntohl(seg->tcphdr->seqno) - pcb->lastack + seg->len > wnd &&
1085 wnd > 0 && wnd == pcb->snd_wnd && pcb->unacked == NULL) {
1086 /* Start the persist timer */
1087 if (pcb->persist_backoff == 0) {
1088 pcb->persist_cnt = 0;
1089 pcb->persist_backoff = 1;
1093 /* data available and window allows it to be sent? */
1094 while (seg != NULL &&
1095 lwip_ntohl(seg->tcphdr->seqno) - pcb->lastack + seg->len <= wnd) {
1096 LWIP_ASSERT("RST not expected here!",
1097 (TCPH_FLAGS(seg->tcphdr) & TCP_RST) == 0);
1098 /* Stop sending if the nagle algorithm would prevent it
1100 * - if tcp_write had a memory error before (prevent delayed ACK timeout) or
1101 * - if FIN was already enqueued for this PCB (SYN is always alone in a segment -
1102 * either seg->next != NULL or pcb->unacked == NULL;
1103 * RST is no sent using tcp_write/tcp_output.
1105 if ((tcp_do_output_nagle(pcb) == 0) &&
1106 ((pcb->flags & (TF_NAGLEMEMERR | TF_FIN)) == 0)) {
1110 LWIP_DEBUGF(TCP_CWND_DEBUG, ("tcp_output: snd_wnd %"TCPWNDSIZE_F", cwnd %"TCPWNDSIZE_F", wnd %"U32_F", effwnd %"U32_F", seq %"U32_F", ack %"U32_F", i %"S16_F"\n",
1111 pcb->snd_wnd, pcb->cwnd, wnd,
1112 lwip_ntohl(seg->tcphdr->seqno) + seg->len -
1114 lwip_ntohl(seg->tcphdr->seqno), pcb->lastack, i));
1116 #endif /* TCP_CWND_DEBUG */
1118 if (pcb->state != SYN_SENT) {
1119 TCPH_SET_FLAG(seg->tcphdr, TCP_ACK);
1122 #if TCP_OVERSIZE_DBGCHECK
1123 seg->oversize_left = 0;
1124 #endif /* TCP_OVERSIZE_DBGCHECK */
1125 err = tcp_output_segment(seg, pcb, netif);
1126 if (err != ERR_OK) {
1127 /* segment could not be sent, for whatever reason */
1128 pcb->flags |= TF_NAGLEMEMERR;
1131 pcb->unsent = seg->next;
1132 if (pcb->state != SYN_SENT) {
1133 pcb->flags &= ~(TF_ACK_DELAY | TF_ACK_NOW);
1135 snd_nxt = lwip_ntohl(seg->tcphdr->seqno) + TCP_TCPLEN(seg);
1136 if (TCP_SEQ_LT(pcb->snd_nxt, snd_nxt)) {
1137 pcb->snd_nxt = snd_nxt;
1139 /* put segment on unacknowledged list if length > 0 */
1140 if (TCP_TCPLEN(seg) > 0) {
1142 /* unacked list is empty? */
1143 if (pcb->unacked == NULL) {
1146 /* unacked list is not empty? */
1148 /* In the case of fast retransmit, the packet should not go to the tail
1149 * of the unacked queue, but rather somewhere before it. We need to check for
1150 * this case. -STJ Jul 27, 2004 */
1151 if (TCP_SEQ_LT(lwip_ntohl(seg->tcphdr->seqno), lwip_ntohl(useg->tcphdr->seqno))) {
1152 /* add segment to before tail of unacked list, keeping the list sorted */
1153 struct tcp_seg **cur_seg = &(pcb->unacked);
1155 TCP_SEQ_LT(lwip_ntohl((*cur_seg)->tcphdr->seqno), lwip_ntohl(seg->tcphdr->seqno))) {
1156 cur_seg = &((*cur_seg)->next );
1158 seg->next = (*cur_seg);
1161 /* add segment to tail of unacked list */
1166 /* do not queue empty segments on the unacked list */
1174 if (pcb->unsent == NULL) {
1175 /* last unsent has been removed, reset unsent_oversize */
1176 pcb->unsent_oversize = 0;
1178 #endif /* TCP_OVERSIZE */
1180 pcb->flags &= ~TF_NAGLEMEMERR;
1185 * Called by tcp_output() to actually send a TCP segment over IP.
1187 * @param seg the tcp_seg to send
1188 * @param pcb the tcp_pcb for the TCP connection used to send the segment
1189 * @param netif the netif used to send the segment
1192 tcp_output_segment(struct tcp_seg *seg, struct tcp_pcb *pcb, struct netif *netif)
1198 if (seg->p->ref != 1) {
1199 /* This can happen if the pbuf of this segment is still referenced by the
1200 netif driver due to deferred transmission. Since this function modifies
1201 p->len, we must not continue in this case. */
1205 /* The TCP header has already been constructed, but the ackno and
1206 wnd fields remain. */
1207 seg->tcphdr->ackno = lwip_htonl(pcb->rcv_nxt);
1209 /* advertise our receive window size in this TCP segment */
1211 if (seg->flags & TF_SEG_OPTS_WND_SCALE) {
1212 /* The Window field in a SYN segment itself (the only type where we send
1213 the window scale option) is never scaled. */
1214 seg->tcphdr->wnd = lwip_htons(TCPWND_MIN16(pcb->rcv_ann_wnd));
1216 #endif /* LWIP_WND_SCALE */
1218 seg->tcphdr->wnd = lwip_htons(TCPWND_MIN16(RCV_WND_SCALE(pcb, pcb->rcv_ann_wnd)));
1221 pcb->rcv_ann_right_edge = pcb->rcv_nxt + pcb->rcv_ann_wnd;
1223 /* Add any requested options. NB MSS option is only set on SYN
1224 packets, so ignore it here */
1225 /* cast through void* to get rid of alignment warnings */
1226 opts = (u32_t *)(void *)(seg->tcphdr + 1);
1227 if (seg->flags & TF_SEG_OPTS_MSS) {
1229 #if TCP_CALCULATE_EFF_SEND_MSS
1230 mss = tcp_eff_send_mss(TCP_MSS, &pcb->local_ip, &pcb->remote_ip);
1231 #else /* TCP_CALCULATE_EFF_SEND_MSS */
1233 #endif /* TCP_CALCULATE_EFF_SEND_MSS */
1234 *opts = TCP_BUILD_MSS_OPTION(mss);
1237 #if LWIP_TCP_TIMESTAMPS
1238 pcb->ts_lastacksent = pcb->rcv_nxt;
1240 if (seg->flags & TF_SEG_OPTS_TS) {
1241 tcp_build_timestamp_option(pcb, opts);
1246 if (seg->flags & TF_SEG_OPTS_WND_SCALE) {
1247 tcp_build_wnd_scale_option(opts);
1252 /* Set retransmission timer running if it is not currently enabled
1253 This must be set before checking the route. */
1254 if (pcb->rtime < 0) {
1258 if (pcb->rttest == 0) {
1259 pcb->rttest = tcp_ticks;
1260 pcb->rtseq = lwip_ntohl(seg->tcphdr->seqno);
1262 LWIP_DEBUGF(TCP_RTO_DEBUG, ("tcp_output_segment: rtseq %"U32_F"\n", pcb->rtseq));
1264 LWIP_DEBUGF(TCP_OUTPUT_DEBUG, ("tcp_output_segment: %"U32_F":%"U32_F"\n",
1265 lwip_htonl(seg->tcphdr->seqno), lwip_htonl(seg->tcphdr->seqno) +
1268 len = (u16_t)((u8_t *)seg->tcphdr - (u8_t *)seg->p->payload);
1270 /** Exclude retransmitted segments from this count. */
1271 MIB2_STATS_INC(mib2.tcpoutsegs);
1275 seg->p->tot_len -= len;
1277 seg->p->payload = seg->tcphdr;
1279 seg->tcphdr->chksum = 0;
1280 #if CHECKSUM_GEN_TCP
1281 IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_TCP) {
1282 #if TCP_CHECKSUM_ON_COPY
1284 #if TCP_CHECKSUM_ON_COPY_SANITY_CHECK
1285 u16_t chksum_slow = ip_chksum_pseudo(seg->p, IP_PROTO_TCP,
1286 seg->p->tot_len, &pcb->local_ip, &pcb->remote_ip);
1287 #endif /* TCP_CHECKSUM_ON_COPY_SANITY_CHECK */
1288 if ((seg->flags & TF_SEG_DATA_CHECKSUMMED) == 0) {
1289 LWIP_ASSERT("data included but not checksummed",
1290 seg->p->tot_len == (TCPH_HDRLEN(seg->tcphdr) * 4));
1293 /* rebuild TCP header checksum (TCP header changes for retransmissions!) */
1294 acc = ip_chksum_pseudo_partial(seg->p, IP_PROTO_TCP,
1295 seg->p->tot_len, TCPH_HDRLEN(seg->tcphdr) * 4, &pcb->local_ip, &pcb->remote_ip);
1296 /* add payload checksum */
1297 if (seg->chksum_swapped) {
1298 seg->chksum = SWAP_BYTES_IN_WORD(seg->chksum);
1299 seg->chksum_swapped = 0;
1301 acc += (u16_t)~(seg->chksum);
1302 seg->tcphdr->chksum = FOLD_U32T(acc);
1303 #if TCP_CHECKSUM_ON_COPY_SANITY_CHECK
1304 if (chksum_slow != seg->tcphdr->chksum) {
1305 TCP_CHECKSUM_ON_COPY_SANITY_CHECK_FAIL(
1306 ("tcp_output_segment: calculated checksum is %"X16_F" instead of %"X16_F"\n",
1307 seg->tcphdr->chksum, chksum_slow));
1308 seg->tcphdr->chksum = chksum_slow;
1310 #endif /* TCP_CHECKSUM_ON_COPY_SANITY_CHECK */
1311 #else /* TCP_CHECKSUM_ON_COPY */
1312 seg->tcphdr->chksum = ip_chksum_pseudo(seg->p, IP_PROTO_TCP,
1313 seg->p->tot_len, &pcb->local_ip, &pcb->remote_ip);
1314 #endif /* TCP_CHECKSUM_ON_COPY */
1316 #endif /* CHECKSUM_GEN_TCP */
1317 TCP_STATS_INC(tcp.xmit);
1319 #if TCP_OUTPUT_DEBUG
1320 #if LWIP_IP_DEBUG_TARGET
1321 if (debug_target_match(PCB_ISIPV6(pcb), &pcb->local_ip, &pcb->remote_ip))
1324 tcp_debug_print(seg->tcphdr);
1325 #if LWIP_IP_DEBUG_TARGET
1330 netif_apply_pcb(netif, (struct ip_pcb *)pcb);
1331 err = ip_output_if(seg->p, &pcb->local_ip, &pcb->remote_ip, pcb->ttl,
1332 pcb->tos, IP_PROTO_TCP, netif);
1333 netif_apply_pcb(netif, NULL);
1338 * Send a TCP RESET packet (empty segment with RST flag set) either to
1339 * abort a connection or to show that there is no matching local connection
1340 * for a received segment.
1342 * Called by tcp_abort() (to abort a local connection), tcp_input() (if no
1343 * matching local pcb was found), tcp_listen_input() (if incoming segment
1344 * has ACK flag set) and tcp_process() (received segment in the wrong state)
1346 * Since a RST segment is in most cases not sent for an active connection,
1347 * tcp_rst() has a number of arguments that are taken from a tcp_pcb for
1348 * most other segment output functions.
1350 * @param seqno the sequence number to use for the outgoing segment
1351 * @param ackno the acknowledge number to use for the outgoing segment
1352 * @param local_ip the local IP address to send the segment from
1353 * @param remote_ip the remote IP address to send the segment to
1354 * @param local_port the local TCP port to send the segment from
1355 * @param remote_port the remote TCP port to send the segment to
1358 tcp_rst(u32_t seqno, u32_t ackno,
1359 const ip_addr_t *local_ip, const ip_addr_t *remote_ip,
1360 u16_t local_port, u16_t remote_port)
1363 struct tcp_hdr *tcphdr;
1364 struct netif *netif;
1365 p = pbuf_alloc(PBUF_IP, TCP_HLEN, PBUF_RAM);
1367 LWIP_DEBUGF(TCP_DEBUG, ("tcp_rst: could not allocate memory for pbuf\n"));
1370 LWIP_ASSERT("check that first pbuf can hold struct tcp_hdr",
1371 (p->len >= sizeof(struct tcp_hdr)));
1373 tcphdr = (struct tcp_hdr *)p->payload;
1374 tcphdr->src = lwip_htons(local_port);
1375 tcphdr->dest = lwip_htons(remote_port);
1376 tcphdr->seqno = lwip_htonl(seqno);
1377 tcphdr->ackno = lwip_htonl(ackno);
1378 TCPH_HDRLEN_FLAGS_SET(tcphdr, TCP_HLEN/4, TCP_RST | TCP_ACK);
1380 tcphdr->wnd = PP_HTONS(((TCP_WND >> TCP_RCV_SCALE) & 0xFFFF));
1382 tcphdr->wnd = PP_HTONS(TCP_WND);
1387 TCP_STATS_INC(tcp.xmit);
1388 MIB2_STATS_INC(mib2.tcpoutrsts);
1390 netif = ip_route(local_ip, remote_ip);
1391 if (netif != NULL) {
1392 #if CHECKSUM_GEN_TCP
1393 IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_TCP) {
1394 tcphdr->chksum = ip_chksum_pseudo(p, IP_PROTO_TCP, p->tot_len,
1395 local_ip, remote_ip);
1398 /* Send output with hardcoded TTL/HL since we have no access to the pcb */
1399 ip_output_if(p, local_ip, remote_ip, TCP_TTL, 0, IP_PROTO_TCP, netif);
1402 LWIP_DEBUGF(TCP_RST_DEBUG, ("tcp_rst: seqno %"U32_F" ackno %"U32_F".\n", seqno, ackno));
1406 * Requeue all unacked segments for retransmission
1408 * Called by tcp_slowtmr() for slow retransmission.
1410 * @param pcb the tcp_pcb for which to re-enqueue all unacked segments
1413 tcp_rexmit_rto(struct tcp_pcb *pcb)
1415 struct tcp_seg *seg;
1417 if (pcb->unacked == NULL) {
1421 /* Move all unacked segments to the head of the unsent queue */
1422 for (seg = pcb->unacked; seg->next != NULL; seg = seg->next);
1423 /* concatenate unsent queue after unacked queue */
1424 seg->next = pcb->unsent;
1425 #if TCP_OVERSIZE_DBGCHECK
1426 /* if last unsent changed, we need to update unsent_oversize */
1427 if (pcb->unsent == NULL) {
1428 pcb->unsent_oversize = seg->oversize_left;
1430 #endif /* TCP_OVERSIZE_DBGCHECK */
1431 /* unsent queue is the concatenated queue (of unacked, unsent) */
1432 pcb->unsent = pcb->unacked;
1433 /* unacked queue is now empty */
1434 pcb->unacked = NULL;
1436 /* increment number of retransmissions */
1437 if (pcb->nrtx < 0xFF) {
1441 /* Don't take any RTT measurements after retransmitting. */
1444 /* Do the actual retransmission */
1449 * Requeue the first unacked segment for retransmission
1451 * Called by tcp_receive() for fast retransmit.
1453 * @param pcb the tcp_pcb for which to retransmit the first unacked segment
1456 tcp_rexmit(struct tcp_pcb *pcb)
1458 struct tcp_seg *seg;
1459 struct tcp_seg **cur_seg;
1461 if (pcb->unacked == NULL) {
1465 /* Move the first unacked segment to the unsent queue */
1466 /* Keep the unsent queue sorted. */
1468 pcb->unacked = seg->next;
1470 cur_seg = &(pcb->unsent);
1472 TCP_SEQ_LT(lwip_ntohl((*cur_seg)->tcphdr->seqno), lwip_ntohl(seg->tcphdr->seqno))) {
1473 cur_seg = &((*cur_seg)->next );
1475 seg->next = *cur_seg;
1478 if (seg->next == NULL) {
1479 /* the retransmitted segment is last in unsent, so reset unsent_oversize */
1480 pcb->unsent_oversize = 0;
1482 #endif /* TCP_OVERSIZE */
1484 if (pcb->nrtx < 0xFF) {
1488 /* Don't take any rtt measurements after retransmitting. */
1491 /* Do the actual retransmission. */
1492 MIB2_STATS_INC(mib2.tcpretranssegs);
1493 /* No need to call tcp_output: we are always called from tcp_input()
1494 and thus tcp_output directly returns. */
1499 * Handle retransmission after three dupacks received
1501 * @param pcb the tcp_pcb for which to retransmit the first unacked segment
1504 tcp_rexmit_fast(struct tcp_pcb *pcb)
1506 if (pcb->unacked != NULL && !(pcb->flags & TF_INFR)) {
1507 /* This is fast retransmit. Retransmit the first unacked segment. */
1508 LWIP_DEBUGF(TCP_FR_DEBUG,
1509 ("tcp_receive: dupacks %"U16_F" (%"U32_F
1510 "), fast retransmit %"U32_F"\n",
1511 (u16_t)pcb->dupacks, pcb->lastack,
1512 lwip_ntohl(pcb->unacked->tcphdr->seqno)));
1515 /* Set ssthresh to half of the minimum of the current
1516 * cwnd and the advertised window */
1517 pcb->ssthresh = LWIP_MIN(pcb->cwnd, pcb->snd_wnd) / 2;
1519 /* The minimum value for ssthresh should be 2 MSS */
1520 if (pcb->ssthresh < (2U * pcb->mss)) {
1521 LWIP_DEBUGF(TCP_FR_DEBUG,
1522 ("tcp_receive: The minimum value for ssthresh %"TCPWNDSIZE_F
1523 " should be min 2 mss %"U16_F"...\n",
1524 pcb->ssthresh, (u16_t)(2*pcb->mss)));
1525 pcb->ssthresh = 2*pcb->mss;
1528 pcb->cwnd = pcb->ssthresh + 3 * pcb->mss;
1529 pcb->flags |= TF_INFR;
1531 /* Reset the retransmission timer to prevent immediate rto retransmissions */
1538 * Send keepalive packets to keep a connection active although
1539 * no data is sent over it.
1541 * Called by tcp_slowtmr()
1543 * @param pcb the tcp_pcb for which to send a keepalive packet
1546 tcp_keepalive(struct tcp_pcb *pcb)
1550 struct netif *netif;
1552 LWIP_DEBUGF(TCP_DEBUG, ("tcp_keepalive: sending KEEPALIVE probe to "));
1553 ip_addr_debug_print(TCP_DEBUG, &pcb->remote_ip);
1554 LWIP_DEBUGF(TCP_DEBUG, ("\n"));
1556 LWIP_DEBUGF(TCP_DEBUG, ("tcp_keepalive: tcp_ticks %"U32_F" pcb->tmr %"U32_F" pcb->keep_cnt_sent %"U16_F"\n",
1557 tcp_ticks, pcb->tmr, (u16_t)pcb->keep_cnt_sent));
1559 p = tcp_output_alloc_header(pcb, 0, 0, lwip_htonl(pcb->snd_nxt - 1));
1561 LWIP_DEBUGF(TCP_DEBUG,
1562 ("tcp_keepalive: could not allocate memory for pbuf\n"));
1565 netif = ip_route(&pcb->local_ip, &pcb->remote_ip);
1566 if (netif == NULL) {
1569 #if CHECKSUM_GEN_TCP
1570 IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_TCP) {
1571 struct tcp_hdr *tcphdr = (struct tcp_hdr *)p->payload;
1572 tcphdr->chksum = ip_chksum_pseudo(p, IP_PROTO_TCP, p->tot_len,
1573 &pcb->local_ip, &pcb->remote_ip);
1575 #endif /* CHECKSUM_GEN_TCP */
1576 TCP_STATS_INC(tcp.xmit);
1578 /* Send output to IP */
1579 netif_apply_pcb(netif, (struct ip_pcb *)pcb);
1580 err = ip_output_if(p, &pcb->local_ip, &pcb->remote_ip, pcb->ttl, 0, IP_PROTO_TCP, netif);
1581 netif_apply_pcb(netif, NULL);
1585 LWIP_DEBUGF(TCP_DEBUG, ("tcp_keepalive: seqno %"U32_F" ackno %"U32_F" err %d.\n",
1586 pcb->snd_nxt - 1, pcb->rcv_nxt, (int)err));
1592 * Send persist timer zero-window probes to keep a connection active
1593 * when a window update is lost.
1595 * Called by tcp_slowtmr()
1597 * @param pcb the tcp_pcb for which to send a zero-window probe packet
1600 tcp_zero_window_probe(struct tcp_pcb *pcb)
1604 struct tcp_hdr *tcphdr;
1605 struct tcp_seg *seg;
1609 struct netif *netif;
1611 LWIP_DEBUGF(TCP_DEBUG, ("tcp_zero_window_probe: sending ZERO WINDOW probe to "));
1612 ip_addr_debug_print(TCP_DEBUG, &pcb->remote_ip);
1613 LWIP_DEBUGF(TCP_DEBUG, ("\n"));
1615 LWIP_DEBUGF(TCP_DEBUG,
1616 ("tcp_zero_window_probe: tcp_ticks %"U32_F
1617 " pcb->tmr %"U32_F" pcb->keep_cnt_sent %"U16_F"\n",
1618 tcp_ticks, pcb->tmr, (u16_t)pcb->keep_cnt_sent));
1626 /* nothing to send, zero window probe not needed */
1630 is_fin = ((TCPH_FLAGS(seg->tcphdr) & TCP_FIN) != 0) && (seg->len == 0);
1631 /* we want to send one seqno: either FIN or data (no options) */
1632 len = is_fin ? 0 : 1;
1634 p = tcp_output_alloc_header(pcb, 0, len, seg->tcphdr->seqno);
1636 LWIP_DEBUGF(TCP_DEBUG, ("tcp_zero_window_probe: no memory for pbuf\n"));
1639 tcphdr = (struct tcp_hdr *)p->payload;
1642 /* FIN segment, no data */
1643 TCPH_FLAGS_SET(tcphdr, TCP_ACK | TCP_FIN);
1645 /* Data segment, copy in one byte from the head of the unacked queue */
1646 char *d = ((char *)p->payload + TCP_HLEN);
1647 /* Depending on whether the segment has already been sent (unacked) or not
1648 (unsent), seg->p->payload points to the IP header or TCP header.
1649 Ensure we copy the first TCP data byte: */
1650 pbuf_copy_partial(seg->p, d, 1, seg->p->tot_len - seg->len);
1653 /* The byte may be acknowledged without the window being opened. */
1654 snd_nxt = lwip_ntohl(seg->tcphdr->seqno) + 1;
1655 if (TCP_SEQ_LT(pcb->snd_nxt, snd_nxt)) {
1656 pcb->snd_nxt = snd_nxt;
1659 netif = ip_route(&pcb->local_ip, &pcb->remote_ip);
1660 if (netif == NULL) {
1663 #if CHECKSUM_GEN_TCP
1664 IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_TCP) {
1665 tcphdr->chksum = ip_chksum_pseudo(p, IP_PROTO_TCP, p->tot_len,
1666 &pcb->local_ip, &pcb->remote_ip);
1669 TCP_STATS_INC(tcp.xmit);
1671 /* Send output to IP */
1672 netif_apply_pcb(netif, (struct ip_pcb *)pcb);
1673 err = ip_output_if(p, &pcb->local_ip, &pcb->remote_ip, pcb->ttl,
1674 0, IP_PROTO_TCP, netif);
1675 netif_apply_pcb(netif, NULL);
1680 LWIP_DEBUGF(TCP_DEBUG, ("tcp_zero_window_probe: seqno %"U32_F
1681 " ackno %"U32_F" err %d.\n",
1682 pcb->snd_nxt - 1, pcb->rcv_nxt, (int)err));
1685 #endif /* LWIP_TCP */