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