4 * Transmission Control Protocol, outgoing traffic
6 * The output functions of TCP.
11 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
12 * All rights reserved.
14 * Redistribution and use in source and binary forms, with or without modification,
15 * are permitted provided that the following conditions are met:
17 * 1. Redistributions of source code must retain the above copyright notice,
18 * this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright notice,
20 * this list of conditions and the following disclaimer in the documentation
21 * and/or other materials provided with the distribution.
22 * 3. The name of the author may not be used to endorse or promote products
23 * derived from this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
28 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
30 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
33 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
36 * This file is part of the lwIP TCP/IP stack.
38 * Author: Adam Dunkels <adam@sics.se>
46 #include "lwip/memp.h"
49 #include "lwip/ip_addr.h"
50 #include "lwip/netif.h"
52 #include "lwip/inet.h"
55 #include "lwip/stats.h"
59 /* Forward declarations.*/
60 static void tcp_output_segment(struct tcp_seg *seg, struct tcp_pcb *pcb);
63 tcp_send_ctrl(struct tcp_pcb *pcb, u8_t flags)
65 return tcp_enqueue(pcb, NULL, 0, flags, 1, NULL, 0);
70 * NB. tcp_write() enqueues data for sending, but does not send it
71 * straight away. It waits in the expectation of more data being sent
72 * soon (as it can send them more efficiently by combining them
73 * together). To prompt the system to send data now, call
74 * tcp_output() after calling tcp_write().
78 tcp_write(struct tcp_pcb *pcb, const void *arg, u16_t len, u8_t copy)
80 LWIP_DEBUGF(TCP_OUTPUT_DEBUG, ("tcp_write(pcb=%p, arg=%p, len=%u, copy=%d)\n", (void *)pcb,
81 arg, len, (unsigned int)copy));
82 if (pcb->state == SYN_SENT ||
83 pcb->state == SYN_RCVD ||
84 pcb->state == ESTABLISHED ||
85 pcb->state == CLOSE_WAIT) {
87 return tcp_enqueue(pcb, (void *)arg, len, 0, copy, NULL, 0);
91 LWIP_DEBUGF(TCP_OUTPUT_DEBUG | DBG_STATE | 3, ("tcp_write() called in invalid state\n"));
97 tcp_enqueue(struct tcp_pcb *pcb, void *arg, u16_t len,
98 u8_t flags, u8_t copy,
99 u8_t *optdata, u8_t optlen)
102 struct tcp_seg *seg, *useg, *queue;
108 LWIP_DEBUGF(TCP_OUTPUT_DEBUG, ("tcp_enqueue(pcb=%p, arg=%p, len=%u, flags=%x, copy=%u)\n",
109 (void *)pcb, arg, len, (unsigned int)flags, (unsigned int)copy));
112 /* fail on too much data */
113 if (len > pcb->snd_buf) {
114 LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 3, ("tcp_enqueue: too much data (len=%u > snd_buf=%u)\n", len, pcb->snd_buf));
118 /* seqno will be the sequence number of the first segment enqueued
119 * by the call to this function. */
120 seqno = pcb->snd_lbb;
123 LWIP_DEBUGF(TCP_QLEN_DEBUG, ("tcp_enqueue: queuelen: %u\n", (unsigned int)pcb->snd_queuelen));
125 /* Check if the queue length exceeds the configured maximum queue
126 * length. If so, we return an error. */
127 queuelen = pcb->snd_queuelen;
128 if (queuelen >= TCP_SND_QUEUELEN) {
129 LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 3, ("tcp_enqueue: too long queue %u (max %u)\n", queuelen, TCP_SND_QUEUELEN));
133 if (pcb->snd_queuelen != 0) {
134 LWIP_ASSERT("tcp_enqueue: valid queue length", pcb->unacked != NULL ||
135 pcb->unsent != NULL);
141 /* First, break up the data into segments and tuck them together in
142 * the local "queue" variable. */
143 while (queue == NULL || left > 0) {
145 /* The segment length should be the MSS if the data to be enqueued
146 * is larger than the MSS. */
147 seglen = left > pcb->mss? pcb->mss: left;
149 /* Allocate memory for tcp_seg, and fill in fields. */
150 seg = memp_malloc(MEMP_TCP_SEG);
152 LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 2, ("tcp_enqueue: could not allocate memory for tcp_seg\n"));
162 /* Attach the segment to the end of the queued segments. */
163 LWIP_ASSERT("useg != NULL", useg != NULL);
168 /* If copy is set, memory should be allocated
169 * and data copied into pbuf, otherwise data comes from
170 * ROM or other static memory, and need not be copied. If
171 * optdata is != NULL, we have options instead of data. */
172 if (optdata != NULL) {
173 if ((seg->p = pbuf_alloc(PBUF_TRANSPORT, optlen, PBUF_RAM)) == NULL) {
177 seg->dataptr = seg->p->payload;
180 if ((seg->p = pbuf_alloc(PBUF_TRANSPORT, seglen, PBUF_RAM)) == NULL) {
181 LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 2, ("tcp_enqueue : could not allocate memory for pbuf copy size %u\n", seglen));
186 memcpy(seg->p->payload, ptr, seglen);
188 seg->dataptr = seg->p->payload;
190 /* do not copy data */
193 /* first, allocate a pbuf for holding the data.
194 * since the referenced data is available at least until it is sent out on the
195 * link (as it has to be ACKed by the remote party) we can safely use PBUF_ROM
196 * instead of PBUF_REF here.
198 if ((p = pbuf_alloc(PBUF_TRANSPORT, seglen, PBUF_ROM)) == NULL) {
199 LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 2, ("tcp_enqueue: could not allocate memory for zero-copy pbuf\n"));
206 /* Second, allocate a pbuf for the headers. */
207 if ((seg->p = pbuf_alloc(PBUF_TRANSPORT, 0, PBUF_RAM)) == NULL) {
208 /* If allocation fails, we have to deallocate the data pbuf as
211 LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 2, ("tcp_enqueue: could not allocate memory for header pbuf\n"));
216 /* Concatenate the headers and data pbufs together. */
221 /* Now that there are more segments queued, we check again if the
222 length of the queue exceeds the configured maximum. */
223 if (queuelen > TCP_SND_QUEUELEN) {
224 LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 2, ("tcp_enqueue: queue too long %u (%u)\n", queuelen, TCP_SND_QUEUELEN));
229 #if 0 /* Was commented out. TODO: can someone say why this is here? */
230 if ((flags & TCP_SYN) || (flags & TCP_FIN)) {
234 /* Build TCP header. */
235 if (pbuf_header(seg->p, TCP_HLEN)) {
237 LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 2, ("tcp_enqueue: no room for TCP header in pbuf.\n"));
239 TCP_STATS_INC(tcp.err);
242 seg->tcphdr = seg->p->payload;
243 seg->tcphdr->src = htons(pcb->local_port);
244 seg->tcphdr->dest = htons(pcb->remote_port);
245 seg->tcphdr->seqno = htonl(seqno);
246 seg->tcphdr->urgp = 0;
247 TCPH_FLAGS_SET(seg->tcphdr, flags);
248 /* don't fill in tcphdr->ackno and tcphdr->wnd until later */
250 /* Copy the options into the header, if they are present. */
251 if (optdata == NULL) {
252 TCPH_HDRLEN_SET(seg->tcphdr, 5);
255 TCPH_HDRLEN_SET(seg->tcphdr, (5 + optlen / 4));
256 /* Copy options into data portion of segment.
257 Options can thus only be sent in non data carrying
258 segments such as SYN|ACK. */
259 memcpy(seg->dataptr, optdata, optlen);
261 LWIP_DEBUGF(TCP_OUTPUT_DEBUG | DBG_TRACE, ("tcp_enqueue: queueing %lu:%lu (0x%x)\n",
262 ntohl(seg->tcphdr->seqno),
263 ntohl(seg->tcphdr->seqno) + TCP_TCPLEN(seg),
268 ptr = (void *)((char *)ptr + seglen);
272 /* Now that the data to be enqueued has been broken up into TCP
273 segments in the queue variable, we add them to the end of the
274 pcb->unsent queue. */
275 if (pcb->unsent == NULL) {
279 for (useg = pcb->unsent; useg->next != NULL; useg = useg->next);
282 /* If there is room in the last pbuf on the unsent queue,
283 chain the first pbuf on the queue together with that. */
285 TCP_TCPLEN(useg) != 0 &&
286 !(TCPH_FLAGS(useg->tcphdr) & (TCP_SYN | TCP_FIN)) &&
287 !(flags & (TCP_SYN | TCP_FIN)) &&
288 useg->len + queue->len <= pcb->mss) {
289 /* Remove TCP header from first segment. */
290 pbuf_header(queue->p, -TCP_HLEN);
291 pbuf_cat(useg->p, queue->p);
292 useg->len += queue->len;
293 useg->next = queue->next;
295 LWIP_DEBUGF(TCP_OUTPUT_DEBUG | DBG_TRACE | DBG_STATE, ("tcp_enqueue: chaining, new len %u\n", useg->len));
299 memp_free(MEMP_TCP_SEG, queue);
310 if ((flags & TCP_SYN) || (flags & TCP_FIN)) {
315 pcb->snd_queuelen = queuelen;
316 LWIP_DEBUGF(TCP_QLEN_DEBUG, ("tcp_enqueue: %d (after enqueued)\n", pcb->snd_queuelen));
317 if (pcb->snd_queuelen != 0) {
318 LWIP_ASSERT("tcp_enqueue: valid queue length", pcb->unacked != NULL ||
319 pcb->unsent != NULL);
323 /* Set the PSH flag in the last segment that we enqueued, but only
324 if the segment has data (indicated by seglen > 0). */
325 if (seg != NULL && seglen > 0 && seg->tcphdr != NULL) {
326 TCPH_SET_FLAG(seg->tcphdr, TCP_PSH);
331 TCP_STATS_INC(tcp.memerr);
334 tcp_segs_free(queue);
336 if (pcb->snd_queuelen != 0) {
337 LWIP_ASSERT("tcp_enqueue: valid queue length", pcb->unacked != NULL ||
338 pcb->unsent != NULL);
341 LWIP_DEBUGF(TCP_QLEN_DEBUG | DBG_STATE, ("tcp_enqueue: %d (with mem err)\n", pcb->snd_queuelen));
345 /* find out what we can send and send it */
347 tcp_output(struct tcp_pcb *pcb)
350 struct tcp_hdr *tcphdr;
351 struct tcp_seg *seg, *useg;
355 #endif /* TCP_CWND_DEBUG */
357 /* First, check if we are invoked by the TCP input processing
358 code. If so, we do not output anything. Instead, we rely on the
359 input processing code to call us when input processing is done
361 if (tcp_input_pcb == pcb) {
365 wnd = LWIP_MIN(pcb->snd_wnd, pcb->cwnd);
370 /* useg should point to last segment on unacked queue */
373 for (; useg->next != NULL; useg = useg->next);
377 /* If the TF_ACK_NOW flag is set, we check if there is data that is
378 to be sent. If data is to be sent out, we'll just piggyback our
379 acknowledgement with the outgoing segment. If no data will be
380 sent (either because the ->unsent queue is empty or because the
381 window doesn't allow it) we'll have to construct an empty ACK
382 segment and send it. */
383 if (pcb->flags & TF_ACK_NOW &&
385 ntohl(seg->tcphdr->seqno) - pcb->lastack + seg->len > wnd)) {
386 pcb->flags &= ~(TF_ACK_DELAY | TF_ACK_NOW);
387 p = pbuf_alloc(PBUF_IP, TCP_HLEN, PBUF_RAM);
389 LWIP_DEBUGF(TCP_OUTPUT_DEBUG, ("tcp_output: (ACK) could not allocate pbuf\n"));
392 LWIP_DEBUGF(TCP_OUTPUT_DEBUG, ("tcp_output: sending ACK for %lu\n", pcb->rcv_nxt));
395 tcphdr->src = htons(pcb->local_port);
396 tcphdr->dest = htons(pcb->remote_port);
397 tcphdr->seqno = htonl(pcb->snd_nxt);
398 tcphdr->ackno = htonl(pcb->rcv_nxt);
399 TCPH_FLAGS_SET(tcphdr, TCP_ACK);
400 tcphdr->wnd = htons(pcb->rcv_wnd);
402 TCPH_HDRLEN_SET(tcphdr, 5);
406 tcphdr->chksum = inet_chksum_pseudo(p, &(pcb->local_ip), &(pcb->remote_ip),
407 IP_PROTO_TCP, p->tot_len);
410 ip_output(p, &(pcb->local_ip), &(pcb->remote_ip), pcb->ttl, pcb->tos,
419 LWIP_DEBUGF(TCP_OUTPUT_DEBUG, ("tcp_output: nothing to send (%p)\n", pcb->unsent));
421 #endif /* TCP_OUTPUT_DEBUG */
424 LWIP_DEBUGF(TCP_CWND_DEBUG, ("tcp_output: snd_wnd %lu, cwnd %lu, wnd %lu, seg == NULL, ack %lu\n",
425 pcb->snd_wnd, pcb->cwnd, wnd,
428 LWIP_DEBUGF(TCP_CWND_DEBUG, ("tcp_output: snd_wnd %lu, cwnd %lu, wnd %lu, effwnd %lu, seq %lu, ack %lu\n",
429 pcb->snd_wnd, pcb->cwnd, wnd,
430 ntohl(seg->tcphdr->seqno) - pcb->lastack + seg->len,
431 ntohl(seg->tcphdr->seqno), pcb->lastack));
433 #endif /* TCP_CWND_DEBUG */
435 while (seg != NULL &&
436 ntohl(seg->tcphdr->seqno) - pcb->lastack + seg->len <= wnd) {
438 LWIP_DEBUGF(TCP_CWND_DEBUG, ("tcp_output: snd_wnd %lu, cwnd %lu, wnd %lu, effwnd %lu, seq %lu, ack %lu, i%d\n",
439 pcb->snd_wnd, pcb->cwnd, wnd,
440 ntohl(seg->tcphdr->seqno) + seg->len -
442 ntohl(seg->tcphdr->seqno), pcb->lastack, i));
444 #endif /* TCP_CWND_DEBUG */
446 pcb->unsent = seg->next;
448 if (pcb->state != SYN_SENT) {
449 TCPH_SET_FLAG(seg->tcphdr, TCP_ACK);
450 pcb->flags &= ~(TF_ACK_DELAY | TF_ACK_NOW);
453 tcp_output_segment(seg, pcb);
454 pcb->snd_nxt = ntohl(seg->tcphdr->seqno) + TCP_TCPLEN(seg);
455 if (TCP_SEQ_LT(pcb->snd_max, pcb->snd_nxt)) {
456 pcb->snd_max = pcb->snd_nxt;
458 /* put segment on unacknowledged list if length > 0 */
459 if (TCP_TCPLEN(seg) > 0) {
461 if (pcb->unacked == NULL) {
477 tcp_output_segment(struct tcp_seg *seg, struct tcp_pcb *pcb)
482 /* The TCP header has already been constructed, but the ackno and
483 wnd fields remain. */
484 seg->tcphdr->ackno = htonl(pcb->rcv_nxt);
486 /* silly window avoidance */
487 if (pcb->rcv_wnd < pcb->mss) {
488 seg->tcphdr->wnd = 0;
490 /* advertise our receive window size in this TCP segment */
491 seg->tcphdr->wnd = htons(pcb->rcv_wnd);
494 /* If we don't have a local IP address, we get one by
495 calling ip_route(). */
496 if (ip_addr_isany(&(pcb->local_ip))) {
497 netif = ip_route(&(pcb->remote_ip));
501 ip_addr_set(&(pcb->local_ip), &(netif->ip_addr));
506 if (pcb->rttest == 0) {
507 pcb->rttest = tcp_ticks;
508 pcb->rtseq = ntohl(seg->tcphdr->seqno);
510 LWIP_DEBUGF(TCP_RTO_DEBUG, ("tcp_output_segment: rtseq %lu\n", pcb->rtseq));
512 LWIP_DEBUGF(TCP_OUTPUT_DEBUG, ("tcp_output_segment: %lu:%lu\n",
513 htonl(seg->tcphdr->seqno), htonl(seg->tcphdr->seqno) +
516 len = (u16_t)((u8_t *)seg->tcphdr - (u8_t *)seg->p->payload);
519 seg->p->tot_len -= len;
521 seg->p->payload = seg->tcphdr;
523 seg->tcphdr->chksum = 0;
525 seg->tcphdr->chksum = inet_chksum_pseudo(seg->p,
528 IP_PROTO_TCP, seg->p->tot_len);
530 TCP_STATS_INC(tcp.xmit);
532 ip_output(seg->p, &(pcb->local_ip), &(pcb->remote_ip), pcb->ttl, pcb->tos,
537 tcp_rst(u32_t seqno, u32_t ackno,
538 struct ip_addr *local_ip, struct ip_addr *remote_ip,
539 u16_t local_port, u16_t remote_port)
542 struct tcp_hdr *tcphdr;
543 p = pbuf_alloc(PBUF_IP, TCP_HLEN, PBUF_RAM);
545 LWIP_DEBUGF(TCP_DEBUG, ("tcp_rst: could not allocate memory for pbuf\n"));
550 tcphdr->src = htons(local_port);
551 tcphdr->dest = htons(remote_port);
552 tcphdr->seqno = htonl(seqno);
553 tcphdr->ackno = htonl(ackno);
554 TCPH_FLAGS_SET(tcphdr, TCP_RST | TCP_ACK);
555 tcphdr->wnd = htons(TCP_WND);
557 TCPH_HDRLEN_SET(tcphdr, 5);
561 tcphdr->chksum = inet_chksum_pseudo(p, local_ip, remote_ip,
562 IP_PROTO_TCP, p->tot_len);
564 TCP_STATS_INC(tcp.xmit);
565 /* Send output with hardcoded TTL since we have no access to the pcb */
566 ip_output(p, local_ip, remote_ip, TCP_TTL, 0, IP_PROTO_TCP);
568 LWIP_DEBUGF(TCP_RST_DEBUG, ("tcp_rst: seqno %lu ackno %lu.\n", seqno, ackno));
572 tcp_rexmit(struct tcp_pcb *pcb)
576 if (pcb->unacked == NULL) {
580 /* Move all unacked segments to the unsent queue. */
581 for (seg = pcb->unacked; seg->next != NULL; seg = seg->next);
583 seg->next = pcb->unsent;
584 pcb->unsent = pcb->unacked;
589 pcb->snd_nxt = ntohl(pcb->unsent->tcphdr->seqno);
593 /* Don't take any rtt measurements after retransmitting. */
596 /* Do the actual retransmission. */
602 tcp_keepalive(struct tcp_pcb *pcb)
605 struct tcp_hdr *tcphdr;
607 LWIP_DEBUGF(TCP_DEBUG, ("tcp_keepalive: sending KEEPALIVE probe to %u.%u.%u.%u\n",
608 ip4_addr1(&pcb->remote_ip), ip4_addr2(&pcb->remote_ip),
609 ip4_addr3(&pcb->remote_ip), ip4_addr4(&pcb->remote_ip)));
611 LWIP_DEBUGF(TCP_DEBUG, ("tcp_keepalive: tcp_ticks %ld pcb->tmr %ld pcb->keep_cnt %ld\n", tcp_ticks, pcb->tmr, pcb->keep_cnt));
613 p = pbuf_alloc(PBUF_IP, TCP_HLEN, PBUF_RAM);
616 LWIP_DEBUGF(TCP_DEBUG, ("tcp_keepalive: could not allocate memory for pbuf\n"));
621 tcphdr->src = htons(pcb->local_port);
622 tcphdr->dest = htons(pcb->remote_port);
623 tcphdr->seqno = htonl(pcb->snd_nxt - 1);
624 tcphdr->ackno = htonl(pcb->rcv_nxt);
625 tcphdr->wnd = htons(pcb->rcv_wnd);
627 TCPH_HDRLEN_SET(tcphdr, 5);
631 tcphdr->chksum = inet_chksum_pseudo(p, &pcb->local_ip, &pcb->remote_ip, IP_PROTO_TCP, p->tot_len);
633 TCP_STATS_INC(tcp.xmit);
635 /* Send output to IP */
636 ip_output(p, &pcb->local_ip, &pcb->remote_ip, pcb->ttl, 0, IP_PROTO_TCP);
640 LWIP_DEBUGF(TCP_RST_DEBUG, ("tcp_keepalive: seqno %lu ackno %lu.\n", pcb->snd_nxt - 1, pcb->rcv_nxt));
643 #endif /* LWIP_TCP */