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 */
48 #include "lwip/memp.h"
50 #include "lwip/ip_addr.h"
51 #include "lwip/netif.h"
52 #include "lwip/inet.h"
53 #include "lwip/inet_chksum.h"
54 #include "lwip/stats.h"
55 #include "lwip/snmp.h"
59 /* Forward declarations.*/
60 static void tcp_output_segment(struct tcp_seg *seg, struct tcp_pcb *pcb);
63 * Called by tcp_close() to send a segment including flags but not data.
65 * @param pcb the tcp_pcb over which to send a segment
66 * @param flags the flags to set in the segment header
67 * @return ERR_OK if sent, another err_t otherwise
70 tcp_send_ctrl(struct tcp_pcb *pcb, u8_t flags)
72 /* no data, no length, flags, copy=1, no optdata, no optdatalen */
73 return tcp_enqueue(pcb, NULL, 0, flags, TCP_WRITE_FLAG_COPY, NULL, 0);
77 * Write data for sending (but does not send it immediately).
79 * It waits in the expectation of more data being sent soon (as
80 * it can send them more efficiently by combining them together).
81 * To prompt the system to send data now, call tcp_output() after
82 * calling tcp_write().
84 * @param pcb Protocol control block of the TCP connection to enqueue data for.
85 * @param data pointer to the data to send
86 * @param len length (in bytes) of the data to send
87 * @param apiflags combination of following flags :
88 * - TCP_WRITE_FLAG_COPY (0x01) data will be copied into memory belonging to the stack
89 * - TCP_WRITE_FLAG_MORE (0x02) for TCP connection, PSH flag will be set on last segment sent,
90 * @return ERR_OK if enqueued, another err_t on error
95 tcp_write(struct tcp_pcb *pcb, const void *data, u16_t len, u8_t apiflags)
97 LWIP_DEBUGF(TCP_OUTPUT_DEBUG, ("tcp_write(pcb=%p, data=%p, len=%"U16_F", apiflags=%"U16_F")\n", (void *)pcb,
98 data, len, (u16_t)apiflags));
99 /* connection is in valid state for data transmission? */
100 if (pcb->state == ESTABLISHED ||
101 pcb->state == CLOSE_WAIT ||
102 pcb->state == SYN_SENT ||
103 pcb->state == SYN_RCVD) {
105 return tcp_enqueue(pcb, (void *)data, len, 0, apiflags, NULL, 0);
109 LWIP_DEBUGF(TCP_OUTPUT_DEBUG | LWIP_DBG_STATE | 3, ("tcp_write() called in invalid state\n"));
115 * Enqueue either data or TCP options (but not both) for tranmission
117 * Called by tcp_connect(), tcp_listen_input(), tcp_send_ctrl() and tcp_write().
119 * @param pcb Protocol control block for the TCP connection to enqueue data for.
120 * @param arg Pointer to the data to be enqueued for sending.
121 * @param len Data length in bytes
122 * @param flags tcp header flags to set in the outgoing segment
123 * @param apiflags combination of following flags :
124 * - TCP_WRITE_FLAG_COPY (0x01) data will be copied into memory belonging to the stack
125 * - TCP_WRITE_FLAG_MORE (0x02) for TCP connection, PSH flag will be set on last segment sent,
130 tcp_enqueue(struct tcp_pcb *pcb, void *arg, u16_t len,
131 u8_t flags, u8_t apiflags,
132 u8_t *optdata, u8_t optlen)
135 struct tcp_seg *seg, *useg, *queue;
141 LWIP_DEBUGF(TCP_OUTPUT_DEBUG, ("tcp_enqueue(pcb=%p, arg=%p, len=%"U16_F", flags=%"X16_F", apiflags=%"U16_F")\n",
142 (void *)pcb, arg, len, (u16_t)flags, (u16_t)apiflags));
143 LWIP_ERROR("tcp_enqueue: len == 0 || optlen == 0 (programmer violates API)",
144 ((len == 0) || (optlen == 0)), return ERR_ARG;);
145 LWIP_ERROR("tcp_enqueue: arg == NULL || optdata == NULL (programmer violates API)",
146 ((arg == NULL) || (optdata == NULL)), return ERR_ARG;);
147 /* fail on too much data */
148 if (len > pcb->snd_buf) {
149 LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 3, ("tcp_enqueue: too much data (len=%"U16_F" > snd_buf=%"U16_F")\n", len, pcb->snd_buf));
150 pcb->flags |= TF_NAGLEMEMERR;
156 /* seqno will be the sequence number of the first segment enqueued
157 * by the call to this function. */
158 seqno = pcb->snd_lbb;
160 LWIP_DEBUGF(TCP_QLEN_DEBUG, ("tcp_enqueue: queuelen: %"U16_F"\n", (u16_t)pcb->snd_queuelen));
162 /* If total number of pbufs on the unsent/unacked queues exceeds the
163 * configured maximum, return an error */
164 queuelen = pcb->snd_queuelen;
165 /* check for configured max queuelen and possible overflow */
166 if ((queuelen >= TCP_SND_QUEUELEN) || (queuelen > TCP_SNDQUEUELEN_OVERFLOW)) {
167 LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 3, ("tcp_enqueue: too long queue %"U16_F" (max %"U16_F")\n", queuelen, TCP_SND_QUEUELEN));
168 TCP_STATS_INC(tcp.memerr);
169 pcb->flags |= TF_NAGLEMEMERR;
173 LWIP_ASSERT("tcp_enqueue: pbufs on queue => at least one queue non-empty",
174 pcb->unacked != NULL || pcb->unsent != NULL);
176 LWIP_ASSERT("tcp_enqueue: no pbufs on queue => both queues empty",
177 pcb->unacked == NULL && pcb->unsent == NULL);
180 /* First, break up the data into segments and tuck them together in
181 * the local "queue" variable. */
182 useg = queue = seg = NULL;
184 while (queue == NULL || left > 0) {
186 /* The segment length should be the MSS if the data to be enqueued
187 * is larger than the MSS. */
188 seglen = left > pcb->mss? pcb->mss: left;
190 /* Allocate memory for tcp_seg, and fill in fields. */
191 seg = memp_malloc(MEMP_TCP_SEG);
193 LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 2, ("tcp_enqueue: could not allocate memory for tcp_seg\n"));
199 /* first segment of to-be-queued data? */
203 /* subsequent segments of to-be-queued data */
205 /* Attach the segment to the end of the queued segments */
206 LWIP_ASSERT("useg != NULL", useg != NULL);
209 /* remember last segment of to-be-queued data for next iteration */
212 /* If copy is set, memory should be allocated
213 * and data copied into pbuf, otherwise data comes from
214 * ROM or other static memory, and need not be copied. If
215 * optdata is != NULL, we have options instead of data. */
218 if (optdata != NULL) {
219 if ((seg->p = pbuf_alloc(PBUF_TRANSPORT, optlen, PBUF_RAM)) == NULL) {
222 LWIP_ASSERT("check that first pbuf can hold optlen",
223 (seg->p->len >= optlen));
224 queuelen += pbuf_clen(seg->p);
225 seg->dataptr = seg->p->payload;
227 /* copy from volatile memory? */
228 else if (apiflags & TCP_WRITE_FLAG_COPY) {
229 if ((seg->p = pbuf_alloc(PBUF_TRANSPORT, seglen, PBUF_RAM)) == NULL) {
230 LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 2, ("tcp_enqueue : could not allocate memory for pbuf copy size %"U16_F"\n", seglen));
233 LWIP_ASSERT("check that first pbuf can hold the complete seglen",
234 (seg->p->len >= seglen));
235 queuelen += pbuf_clen(seg->p);
237 MEMCPY(seg->p->payload, ptr, seglen);
239 seg->dataptr = seg->p->payload;
241 /* do not copy data */
243 /* First, allocate a pbuf for holding the data.
244 * since the referenced data is available at least until it is sent out on the
245 * link (as it has to be ACKed by the remote party) we can safely use PBUF_ROM
246 * instead of PBUF_REF here.
248 if ((p = pbuf_alloc(PBUF_TRANSPORT, seglen, PBUF_ROM)) == NULL) {
249 LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 2, ("tcp_enqueue: could not allocate memory for zero-copy pbuf\n"));
253 /* reference the non-volatile payload data */
257 /* Second, allocate a pbuf for the headers. */
258 if ((seg->p = pbuf_alloc(PBUF_TRANSPORT, 0, PBUF_RAM)) == NULL) {
259 /* If allocation fails, we have to deallocate the data pbuf as
262 LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 2, ("tcp_enqueue: could not allocate memory for header pbuf\n"));
265 queuelen += pbuf_clen(seg->p);
267 /* Concatenate the headers and data pbufs together. */
268 pbuf_cat(seg->p/*header*/, p/*data*/);
272 /* Now that there are more segments queued, we check again if the
273 length of the queue exceeds the configured maximum or overflows. */
274 if ((queuelen > TCP_SND_QUEUELEN) || (queuelen > TCP_SNDQUEUELEN_OVERFLOW)) {
275 LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 2, ("tcp_enqueue: queue too long %"U16_F" (%"U16_F")\n", queuelen, TCP_SND_QUEUELEN));
281 /* build TCP header */
282 if (pbuf_header(seg->p, TCP_HLEN)) {
283 LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 2, ("tcp_enqueue: no room for TCP header in pbuf.\n"));
284 TCP_STATS_INC(tcp.err);
287 seg->tcphdr = seg->p->payload;
288 seg->tcphdr->src = htons(pcb->local_port);
289 seg->tcphdr->dest = htons(pcb->remote_port);
290 seg->tcphdr->seqno = htonl(seqno);
291 seg->tcphdr->urgp = 0;
292 TCPH_FLAGS_SET(seg->tcphdr, flags);
293 /* don't fill in tcphdr->ackno and tcphdr->wnd until later */
295 /* Copy the options into the header, if they are present. */
296 if (optdata == NULL) {
297 TCPH_HDRLEN_SET(seg->tcphdr, 5);
300 TCPH_HDRLEN_SET(seg->tcphdr, (5 + optlen / 4));
301 /* Copy options into data portion of segment.
302 Options can thus only be sent in non data carrying
303 segments such as SYN|ACK. */
304 SMEMCPY(seg->dataptr, optdata, optlen);
306 LWIP_DEBUGF(TCP_OUTPUT_DEBUG | LWIP_DBG_TRACE, ("tcp_enqueue: queueing %"U32_F":%"U32_F" (0x%"X16_F")\n",
307 ntohl(seg->tcphdr->seqno),
308 ntohl(seg->tcphdr->seqno) + TCP_TCPLEN(seg),
313 ptr = (void *)((u8_t *)ptr + seglen);
316 /* Now that the data to be enqueued has been broken up into TCP
317 segments in the queue variable, we add them to the end of the
318 pcb->unsent queue. */
319 if (pcb->unsent == NULL) {
323 for (useg = pcb->unsent; useg->next != NULL; useg = useg->next);
325 /* { useg is last segment on the unsent queue, NULL if list is empty } */
327 /* If there is room in the last pbuf on the unsent queue,
328 chain the first pbuf on the queue together with that. */
330 TCP_TCPLEN(useg) != 0 &&
331 !(TCPH_FLAGS(useg->tcphdr) & (TCP_SYN | TCP_FIN)) &&
332 !(flags & (TCP_SYN | TCP_FIN)) &&
333 /* fit within max seg size */
334 useg->len + queue->len <= pcb->mss) {
335 /* Remove TCP header from first segment of our to-be-queued list */
336 if(pbuf_header(queue->p, -TCP_HLEN)) {
337 /* Can we cope with this failing? Just assert for now */
338 LWIP_ASSERT("pbuf_header failed\n", 0);
339 TCP_STATS_INC(tcp.err);
342 pbuf_cat(useg->p, queue->p);
343 useg->len += queue->len;
344 useg->next = queue->next;
346 LWIP_DEBUGF(TCP_OUTPUT_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("tcp_enqueue: chaining segments, new len %"U16_F"\n", useg->len));
350 memp_free(MEMP_TCP_SEG, queue);
355 /* initialize list with this segment */
358 /* enqueue segment */
363 if ((flags & TCP_SYN) || (flags & TCP_FIN)) {
366 if (flags & TCP_FIN) {
367 pcb->flags |= TF_FIN;
373 /* update number of segments on the queues */
374 pcb->snd_queuelen = queuelen;
375 LWIP_DEBUGF(TCP_QLEN_DEBUG, ("tcp_enqueue: %"S16_F" (after enqueued)\n", pcb->snd_queuelen));
376 if (pcb->snd_queuelen != 0) {
377 LWIP_ASSERT("tcp_enqueue: valid queue length",
378 pcb->unacked != NULL || pcb->unsent != NULL);
381 /* Set the PSH flag in the last segment that we enqueued, but only
382 if the segment has data (indicated by seglen > 0). */
383 if (seg != NULL && seglen > 0 && seg->tcphdr != NULL && ((apiflags & TCP_WRITE_FLAG_MORE)==0)) {
384 TCPH_SET_FLAG(seg->tcphdr, TCP_PSH);
389 pcb->flags |= TF_NAGLEMEMERR;
390 TCP_STATS_INC(tcp.memerr);
393 tcp_segs_free(queue);
395 if (pcb->snd_queuelen != 0) {
396 LWIP_ASSERT("tcp_enqueue: valid queue length", pcb->unacked != NULL ||
397 pcb->unsent != NULL);
399 LWIP_DEBUGF(TCP_QLEN_DEBUG | LWIP_DBG_STATE, ("tcp_enqueue: %"S16_F" (with mem err)\n", pcb->snd_queuelen));
404 * Find out what we can send and send it
406 * @param pcb Protocol control block for the TCP connection to send data
407 * @return ERR_OK if data has been sent or nothing to send
408 * another err_t on error
411 tcp_output(struct tcp_pcb *pcb)
414 struct tcp_hdr *tcphdr;
415 struct tcp_seg *seg, *useg;
419 #endif /* TCP_CWND_DEBUG */
421 /* First, check if we are invoked by the TCP input processing
422 code. If so, we do not output anything. Instead, we rely on the
423 input processing code to call us when input processing is done
425 if (tcp_input_pcb == pcb) {
429 wnd = LWIP_MIN(pcb->snd_wnd, pcb->cwnd);
433 /* useg should point to last segment on unacked queue */
436 for (; useg->next != NULL; useg = useg->next);
439 /* If the TF_ACK_NOW flag is set and no data will be sent (either
440 * because the ->unsent queue is empty or because the window does
441 * not allow it), construct an empty ACK segment and send it.
443 * If data is to be sent, we will just piggyback the ACK (see below).
445 if (pcb->flags & TF_ACK_NOW &&
447 ntohl(seg->tcphdr->seqno) - pcb->lastack + seg->len > wnd)) {
448 p = pbuf_alloc(PBUF_IP, TCP_HLEN, PBUF_RAM);
450 LWIP_DEBUGF(TCP_OUTPUT_DEBUG, ("tcp_output: (ACK) could not allocate pbuf\n"));
453 LWIP_DEBUGF(TCP_OUTPUT_DEBUG, ("tcp_output: sending ACK for %"U32_F"\n", pcb->rcv_nxt));
454 /* remove ACK flags from the PCB, as we send an empty ACK now */
455 pcb->flags &= ~(TF_ACK_DELAY | TF_ACK_NOW);
458 tcphdr->src = htons(pcb->local_port);
459 tcphdr->dest = htons(pcb->remote_port);
460 tcphdr->seqno = htonl(pcb->snd_nxt);
461 tcphdr->ackno = htonl(pcb->rcv_nxt);
462 TCPH_FLAGS_SET(tcphdr, TCP_ACK);
463 tcphdr->wnd = htons(pcb->rcv_ann_wnd);
465 TCPH_HDRLEN_SET(tcphdr, 5);
469 tcphdr->chksum = inet_chksum_pseudo(p, &(pcb->local_ip), &(pcb->remote_ip),
470 IP_PROTO_TCP, p->tot_len);
472 #if LWIP_NETIF_HWADDRHINT
475 netif = ip_route(&pcb->remote_ip);
477 netif->addr_hint = &(pcb->addr_hint);
478 ip_output_if(p, &(pcb->local_ip), &(pcb->remote_ip), pcb->ttl,
479 pcb->tos, IP_PROTO_TCP, netif);
480 netif->addr_hint = NULL;
483 #else /* LWIP_NETIF_HWADDRHINT*/
484 ip_output(p, &(pcb->local_ip), &(pcb->remote_ip), pcb->ttl, pcb->tos,
486 #endif /* LWIP_NETIF_HWADDRHINT*/
494 LWIP_DEBUGF(TCP_OUTPUT_DEBUG, ("tcp_output: nothing to send (%p)\n",
495 (void*)pcb->unsent));
497 #endif /* TCP_OUTPUT_DEBUG */
500 LWIP_DEBUGF(TCP_CWND_DEBUG, ("tcp_output: snd_wnd %"U16_F
501 ", cwnd %"U16_F", wnd %"U32_F
502 ", seg == NULL, ack %"U32_F"\n",
503 pcb->snd_wnd, pcb->cwnd, wnd, pcb->lastack));
505 LWIP_DEBUGF(TCP_CWND_DEBUG,
506 ("tcp_output: snd_wnd %"U16_F", cwnd %"U16_F", wnd %"U32_F
507 ", effwnd %"U32_F", seq %"U32_F", ack %"U32_F"\n",
508 pcb->snd_wnd, pcb->cwnd, wnd,
509 ntohl(seg->tcphdr->seqno) - pcb->lastack + seg->len,
510 ntohl(seg->tcphdr->seqno), pcb->lastack));
512 #endif /* TCP_CWND_DEBUG */
513 /* data available and window allows it to be sent? */
514 while (seg != NULL &&
515 ntohl(seg->tcphdr->seqno) - pcb->lastack + seg->len <= wnd) {
516 LWIP_ASSERT("RST not expected here!",
517 (TCPH_FLAGS(seg->tcphdr) & TCP_RST) == 0);
518 /* Stop sending if the nagle algorithm would prevent it
520 * - if tcp_enqueue had a memory error before (prevent delayed ACK timeout) or
521 * - if FIN was already enqueued for this PCB (SYN is always alone in a segment -
522 * either seg->next != NULL or pcb->unacked == NULL;
523 * RST is no sent using tcp_enqueue/tcp_output.
525 if((tcp_do_output_nagle(pcb) == 0) &&
526 ((pcb->flags & (TF_NAGLEMEMERR | TF_FIN)) == 0)){
530 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",
531 pcb->snd_wnd, pcb->cwnd, wnd,
532 ntohl(seg->tcphdr->seqno) + seg->len -
534 ntohl(seg->tcphdr->seqno), pcb->lastack, i));
536 #endif /* TCP_CWND_DEBUG */
538 pcb->unsent = seg->next;
540 if (pcb->state != SYN_SENT) {
541 TCPH_SET_FLAG(seg->tcphdr, TCP_ACK);
542 pcb->flags &= ~(TF_ACK_DELAY | TF_ACK_NOW);
545 tcp_output_segment(seg, pcb);
546 pcb->snd_nxt = ntohl(seg->tcphdr->seqno) + TCP_TCPLEN(seg);
547 if (TCP_SEQ_LT(pcb->snd_max, pcb->snd_nxt)) {
548 pcb->snd_max = pcb->snd_nxt;
550 /* put segment on unacknowledged list if length > 0 */
551 if (TCP_TCPLEN(seg) > 0) {
553 /* unacked list is empty? */
554 if (pcb->unacked == NULL) {
557 /* unacked list is not empty? */
559 /* In the case of fast retransmit, the packet should not go to the tail
560 * of the unacked queue, but rather at the head. We need to check for
561 * this case. -STJ Jul 27, 2004 */
562 if (TCP_SEQ_LT(ntohl(seg->tcphdr->seqno), ntohl(useg->tcphdr->seqno))){
563 /* add segment to head of unacked list */
564 seg->next = pcb->unacked;
567 /* add segment to tail of unacked list */
572 /* do not queue empty segments on the unacked list */
579 if (seg != NULL && pcb->persist_backoff == 0 &&
580 ntohl(seg->tcphdr->seqno) - pcb->lastack + seg->len > pcb->snd_wnd) {
581 /* prepare for persist timer */
582 pcb->persist_cnt = 0;
583 pcb->persist_backoff = 1;
586 pcb->flags &= ~TF_NAGLEMEMERR;
591 * Called by tcp_output() to actually send a TCP segment over IP.
593 * @param seg the tcp_seg to send
594 * @param pcb the tcp_pcb for the TCP connection used to send the segment
597 tcp_output_segment(struct tcp_seg *seg, struct tcp_pcb *pcb)
602 /** @bug Exclude retransmitted segments from this count. */
603 snmp_inc_tcpoutsegs();
605 /* The TCP header has already been constructed, but the ackno and
606 wnd fields remain. */
607 seg->tcphdr->ackno = htonl(pcb->rcv_nxt);
609 /* advertise our receive window size in this TCP segment */
610 seg->tcphdr->wnd = htons(pcb->rcv_ann_wnd);
612 /* If we don't have a local IP address, we get one by
613 calling ip_route(). */
614 if (ip_addr_isany(&(pcb->local_ip))) {
615 netif = ip_route(&(pcb->remote_ip));
619 ip_addr_set(&(pcb->local_ip), &(netif->ip_addr));
622 /* Set retransmission timer running if it is not currently enabled */
626 if (pcb->rttest == 0) {
627 pcb->rttest = tcp_ticks;
628 pcb->rtseq = ntohl(seg->tcphdr->seqno);
630 LWIP_DEBUGF(TCP_RTO_DEBUG, ("tcp_output_segment: rtseq %"U32_F"\n", pcb->rtseq));
632 LWIP_DEBUGF(TCP_OUTPUT_DEBUG, ("tcp_output_segment: %"U32_F":%"U32_F"\n",
633 htonl(seg->tcphdr->seqno), htonl(seg->tcphdr->seqno) +
636 len = (u16_t)((u8_t *)seg->tcphdr - (u8_t *)seg->p->payload);
639 seg->p->tot_len -= len;
641 seg->p->payload = seg->tcphdr;
643 seg->tcphdr->chksum = 0;
645 seg->tcphdr->chksum = inet_chksum_pseudo(seg->p,
648 IP_PROTO_TCP, seg->p->tot_len);
650 TCP_STATS_INC(tcp.xmit);
652 #if LWIP_NETIF_HWADDRHINT
655 netif = ip_route(&pcb->remote_ip);
657 netif->addr_hint = &(pcb->addr_hint);
658 ip_output_if(seg->p, &(pcb->local_ip), &(pcb->remote_ip), pcb->ttl,
659 pcb->tos, IP_PROTO_TCP, netif);
660 netif->addr_hint = NULL;
663 #else /* LWIP_NETIF_HWADDRHINT*/
664 ip_output(seg->p, &(pcb->local_ip), &(pcb->remote_ip), pcb->ttl, pcb->tos,
666 #endif /* LWIP_NETIF_HWADDRHINT*/
670 * Send a TCP RESET packet (empty segment with RST flag set) either to
671 * abort a connection or to show that there is no matching local connection
672 * for a received segment.
674 * Called by tcp_abort() (to abort a local connection), tcp_input() (if no
675 * matching local pcb was found), tcp_listen_input() (if incoming segment
676 * has ACK flag set) and tcp_process() (received segment in the wrong state)
678 * Since a RST segment is in most cases not sent for an active connection,
679 * tcp_rst() has a number of arguments that are taken from a tcp_pcb for
680 * most other segment output functions.
682 * @param seqno the sequence number to use for the outgoing segment
683 * @param ackno the acknowledge number to use for the outgoing segment
684 * @param local_ip the local IP address to send the segment from
685 * @param remote_ip the remote IP address to send the segment to
686 * @param local_port the local TCP port to send the segment from
687 * @param remote_port the remote TCP port to send the segment to
690 tcp_rst(u32_t seqno, u32_t ackno,
691 struct ip_addr *local_ip, struct ip_addr *remote_ip,
692 u16_t local_port, u16_t remote_port)
695 struct tcp_hdr *tcphdr;
696 p = pbuf_alloc(PBUF_IP, TCP_HLEN, PBUF_RAM);
698 LWIP_DEBUGF(TCP_DEBUG, ("tcp_rst: could not allocate memory for pbuf\n"));
701 LWIP_ASSERT("check that first pbuf can hold struct tcp_hdr",
702 (p->len >= sizeof(struct tcp_hdr)));
705 tcphdr->src = htons(local_port);
706 tcphdr->dest = htons(remote_port);
707 tcphdr->seqno = htonl(seqno);
708 tcphdr->ackno = htonl(ackno);
709 TCPH_FLAGS_SET(tcphdr, TCP_RST | TCP_ACK);
710 tcphdr->wnd = htons(TCP_WND);
712 TCPH_HDRLEN_SET(tcphdr, 5);
716 tcphdr->chksum = inet_chksum_pseudo(p, local_ip, remote_ip,
717 IP_PROTO_TCP, p->tot_len);
719 TCP_STATS_INC(tcp.xmit);
720 snmp_inc_tcpoutrsts();
721 /* Send output with hardcoded TTL since we have no access to the pcb */
722 ip_output(p, local_ip, remote_ip, TCP_TTL, 0, IP_PROTO_TCP);
724 LWIP_DEBUGF(TCP_RST_DEBUG, ("tcp_rst: seqno %"U32_F" ackno %"U32_F".\n", seqno, ackno));
728 * Requeue all unacked segments for retransmission
730 * Called by tcp_slowtmr() for slow retransmission.
732 * @param pcb the tcp_pcb for which to re-enqueue all unacked segments
735 tcp_rexmit_rto(struct tcp_pcb *pcb)
739 if (pcb->unacked == NULL) {
743 /* Move all unacked segments to the head of the unsent queue */
744 for (seg = pcb->unacked; seg->next != NULL; seg = seg->next);
745 /* concatenate unsent queue after unacked queue */
746 seg->next = pcb->unsent;
747 /* unsent queue is the concatenated queue (of unacked, unsent) */
748 pcb->unsent = pcb->unacked;
749 /* unacked queue is now empty */
752 pcb->snd_nxt = ntohl(pcb->unsent->tcphdr->seqno);
753 /* increment number of retransmissions */
756 /* Don't take any RTT measurements after retransmitting. */
759 /* Do the actual retransmission */
764 * Requeue the first unacked segment for retransmission
766 * Called by tcp_receive() for fast retramsmit.
768 * @param pcb the tcp_pcb for which to retransmit the first unacked segment
771 tcp_rexmit(struct tcp_pcb *pcb)
775 if (pcb->unacked == NULL) {
779 /* Move the first unacked segment to the unsent queue */
780 seg = pcb->unacked->next;
781 pcb->unacked->next = pcb->unsent;
782 pcb->unsent = pcb->unacked;
785 pcb->snd_nxt = ntohl(pcb->unsent->tcphdr->seqno);
789 /* Don't take any rtt measurements after retransmitting. */
792 /* Do the actual retransmission. */
793 snmp_inc_tcpretranssegs();
798 * Send keepalive packets to keep a connection active although
799 * no data is sent over it.
801 * Called by tcp_slowtmr()
803 * @param pcb the tcp_pcb for which to send a keepalive packet
806 tcp_keepalive(struct tcp_pcb *pcb)
809 struct tcp_hdr *tcphdr;
811 LWIP_DEBUGF(TCP_DEBUG, ("tcp_keepalive: sending KEEPALIVE probe to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
812 ip4_addr1(&pcb->remote_ip), ip4_addr2(&pcb->remote_ip),
813 ip4_addr3(&pcb->remote_ip), ip4_addr4(&pcb->remote_ip)));
815 LWIP_DEBUGF(TCP_DEBUG, ("tcp_keepalive: tcp_ticks %"U32_F" pcb->tmr %"U32_F" pcb->keep_cnt_sent %"U16_F"\n",
816 tcp_ticks, pcb->tmr, pcb->keep_cnt_sent));
818 p = pbuf_alloc(PBUF_IP, TCP_HLEN, PBUF_RAM);
821 LWIP_DEBUGF(TCP_DEBUG,
822 ("tcp_keepalive: could not allocate memory for pbuf\n"));
825 LWIP_ASSERT("check that first pbuf can hold struct tcp_hdr",
826 (p->len >= sizeof(struct tcp_hdr)));
829 tcphdr->src = htons(pcb->local_port);
830 tcphdr->dest = htons(pcb->remote_port);
831 tcphdr->seqno = htonl(pcb->snd_nxt - 1);
832 tcphdr->ackno = htonl(pcb->rcv_nxt);
833 TCPH_FLAGS_SET(tcphdr, 0);
834 tcphdr->wnd = htons(pcb->rcv_ann_wnd);
836 TCPH_HDRLEN_SET(tcphdr, 5);
840 tcphdr->chksum = inet_chksum_pseudo(p, &pcb->local_ip, &pcb->remote_ip,
841 IP_PROTO_TCP, p->tot_len);
843 TCP_STATS_INC(tcp.xmit);
845 /* Send output to IP */
846 #if LWIP_NETIF_HWADDRHINT
849 netif = ip_route(&pcb->remote_ip);
851 netif->addr_hint = &(pcb->addr_hint);
852 ip_output_if(p, &(pcb->local_ip), &(pcb->remote_ip), pcb->ttl,
853 0, IP_PROTO_TCP, netif);
854 netif->addr_hint = NULL;
857 #else /* LWIP_NETIF_HWADDRHINT*/
858 ip_output(p, &pcb->local_ip, &pcb->remote_ip, pcb->ttl, 0, IP_PROTO_TCP);
859 #endif /* LWIP_NETIF_HWADDRHINT*/
863 LWIP_DEBUGF(TCP_DEBUG, ("tcp_keepalive: seqno %"U32_F" ackno %"U32_F".\n",
864 pcb->snd_nxt - 1, pcb->rcv_nxt));
869 * Send persist timer zero-window probes to keep a connection active
870 * when a window update is lost.
872 * Called by tcp_slowtmr()
874 * @param pcb the tcp_pcb for which to send a zero-window probe packet
877 tcp_zero_window_probe(struct tcp_pcb *pcb)
880 struct tcp_hdr *tcphdr;
883 LWIP_DEBUGF(TCP_DEBUG,
884 ("tcp_zero_window_probe: sending ZERO WINDOW probe to %"
885 U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
886 ip4_addr1(&pcb->remote_ip), ip4_addr2(&pcb->remote_ip),
887 ip4_addr3(&pcb->remote_ip), ip4_addr4(&pcb->remote_ip)));
889 LWIP_DEBUGF(TCP_DEBUG,
890 ("tcp_zero_window_probe: tcp_ticks %"U32_F
891 " pcb->tmr %"U32_F" pcb->keep_cnt_sent %"U16_F"\n",
892 tcp_ticks, pcb->tmr, pcb->keep_cnt_sent));
902 p = pbuf_alloc(PBUF_IP, TCP_HLEN + 1, PBUF_RAM);
905 LWIP_DEBUGF(TCP_DEBUG, ("tcp_zero_window_probe: no memory for pbuf\n"));
908 LWIP_ASSERT("check that first pbuf can hold struct tcp_hdr",
909 (p->len >= sizeof(struct tcp_hdr)));
912 tcphdr->src = htons(pcb->local_port);
913 tcphdr->dest = htons(pcb->remote_port);
914 tcphdr->seqno = seg->tcphdr->seqno;
915 tcphdr->ackno = htonl(pcb->rcv_nxt);
916 TCPH_FLAGS_SET(tcphdr, 0);
917 tcphdr->wnd = htons(pcb->rcv_ann_wnd);
919 TCPH_HDRLEN_SET(tcphdr, 5);
921 /* Copy in one byte from the head of the unacked queue */
922 *((char *)p->payload + sizeof(struct tcp_hdr)) = *(char *)seg->dataptr;
926 tcphdr->chksum = inet_chksum_pseudo(p, &pcb->local_ip, &pcb->remote_ip,
927 IP_PROTO_TCP, p->tot_len);
929 TCP_STATS_INC(tcp.xmit);
931 /* Send output to IP */
932 #if LWIP_NETIF_HWADDRHINT
935 netif = ip_route(&pcb->remote_ip);
937 netif->addr_hint = &(pcb->addr_hint);
938 ip_output_if(p, &(pcb->local_ip), &(pcb->remote_ip), pcb->ttl,
939 0, IP_PROTO_TCP, netif);
940 netif->addr_hint = NULL;
943 #else /* LWIP_NETIF_HWADDRHINT*/
944 ip_output(p, &pcb->local_ip, &pcb->remote_ip, pcb->ttl, 0, IP_PROTO_TCP);
945 #endif /* LWIP_NETIF_HWADDRHINT*/
949 LWIP_DEBUGF(TCP_DEBUG, ("tcp_zero_window_probe: seqno %"U32_F
950 " ackno %"U32_F".\n",
951 pcb->snd_nxt - 1, pcb->rcv_nxt));
953 #endif /* LWIP_TCP */