1/**
2 * @file
3 * Transmission Control Protocol, outgoing traffic
4 *
5 * The output functions of TCP.
6 *
7 */
8
9/*
10 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
11 * All rights reserved.
12 *
13 * Redistribution and use in source and binary forms, with or without modification,
14 * are permitted provided that the following conditions are met:
15 *
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.
23 *
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
33 * OF SUCH DAMAGE.
34 *
35 * This file is part of the lwIP TCP/IP stack.
36 *
37 * Author: Adam Dunkels <adam@sics.se>
38 *
39 */
40
41#include "lwip/opt.h"
42
43#if LWIP_TCP                    /* don't build if not configured for use in lwipopts.h */
44
45#include "lwip/tcp.h"
46#include "lwip/def.h"
47#include "lwip/mem.h"
48#include "lwip/memp.h"
49#include "lwip/sys.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"
56#include "lwip/init.h"
57
58#include <string.h>
59
60/* Forward declarations.*/
61static void tcp_output_segment(struct tcp_seg *seg, struct tcp_pcb *pcb);
62
63static struct tcp_hdr *tcp_output_set_header(struct tcp_pcb *pcb,
64                                             struct pbuf *p, int optlen,
65                                             u32_t seqno_be
66                                             /* already in network byte order */
67                                             )
68{
69    struct tcp_hdr *tcphdr = p->payload;
70
71    tcphdr->src = htons(pcb->local_port);
72    tcphdr->dest = htons(pcb->remote_port);
73    tcphdr->seqno = seqno_be;
74    tcphdr->ackno = htonl(pcb->rcv_nxt);
75    TCPH_FLAGS_SET(tcphdr, TCP_ACK);
76    tcphdr->wnd = htons(pcb->rcv_ann_wnd);
77    tcphdr->urgp = 0;
78    TCPH_HDRLEN_SET(tcphdr, (5 + optlen / 4));
79    tcphdr->chksum = 0;
80
81    /* If we're sending a packet, update the announced right window edge */
82    pcb->rcv_ann_right_edge = pcb->rcv_nxt + pcb->rcv_ann_wnd;
83
84    return tcphdr;
85}
86
87/**
88 * Called by tcp_close() to send a segment including flags but not data.
89 *
90 * @param pcb the tcp_pcb over which to send a segment
91 * @param flags the flags to set in the segment header
92 * @return ERR_OK if sent, another err_t otherwise
93 */
94err_t tcp_send_ctrl(struct tcp_pcb * pcb, u8_t flags)
95{
96    /* no data, no length, flags, copy=1, no optdata */
97    return tcp_enqueue(pcb, NULL, 0, flags, TCP_WRITE_FLAG_COPY, 0);
98}
99
100/**
101 * Write data for sending (but does not send it immediately).
102 *
103 * It waits in the expectation of more data being sent soon (as
104 * it can send them more efficiently by combining them together).
105 * To prompt the system to send data now, call tcp_output() after
106 * calling tcp_write().
107 *
108 * @param pcb Protocol control block of the TCP connection to enqueue data for.
109 * @param data pointer to the data to send
110 * @param len length (in bytes) of the data to send
111 * @param apiflags combination of following flags :
112 * - TCP_WRITE_FLAG_COPY (0x01) data will be copied into memory belonging to the stack
113 * - TCP_WRITE_FLAG_MORE (0x02) for TCP connection, PSH flag will be set on last segment sent,
114 * @return ERR_OK if enqueued, another err_t on error
115 *
116 * @see tcp_write()
117 */
118err_t
119tcp_write(struct tcp_pcb * pcb, const void *data, u16_t len, u8_t apiflags)
120{
121    LWIP_DEBUGF(TCP_OUTPUT_DEBUG,
122                ("tcp_write(pcb=%p, data=%p, len=%" U16_F ", apiflags=%" U16_F
123                 ")\n", (void *) pcb, data, len, (u16_t) apiflags));
124    /* connection is in valid state for data transmission? */
125    if (pcb->state == ESTABLISHED ||
126        pcb->state == CLOSE_WAIT ||
127        pcb->state == SYN_SENT || pcb->state == SYN_RCVD) {
128        if (len > 0) {
129#if LWIP_TCP_TIMESTAMPS
130            return tcp_enqueue(pcb, (void *) data, len, 0, apiflags,
131                               pcb->flags & TF_TIMESTAMP ? TF_SEG_OPTS_TS : 0);
132#else
133            return tcp_enqueue(pcb, (void *) data, len, 0, apiflags, 0);
134#endif
135        }
136        return ERR_OK;
137    } else {
138        LWIP_DEBUGF(TCP_OUTPUT_DEBUG | LWIP_DBG_STATE | 3,
139                    ("tcp_write() called in invalid state\n"));
140        return ERR_CONN;
141    }
142}
143
144/**
145 * Enqueue data and/or TCP options for transmission
146 *
147 * Called by tcp_connect(), tcp_listen_input(), tcp_send_ctrl() and tcp_write().
148 *
149 * @param pcb Protocol control block for the TCP connection to enqueue data for.
150 * @param arg Pointer to the data to be enqueued for sending.
151 * @param len Data length in bytes
152 * @param flags tcp header flags to set in the outgoing segment
153 * @param apiflags combination of following flags :
154 * - TCP_WRITE_FLAG_COPY (0x01) data will be copied into memory belonging to the stack
155 * - TCP_WRITE_FLAG_MORE (0x02) for TCP connection, PSH flag will be set on last segment sent,
156 * @param optflags options to include in segment later on (see definition of struct tcp_seg)
157 */
158err_t
159tcp_enqueue(struct tcp_pcb * pcb, void *arg, u16_t len,
160            u8_t flags, u8_t apiflags, u8_t optflags)
161{
162//  struct pbuf *p;
163    struct tcp_seg *seg, *useg, *queue;
164    u32_t seqno;
165    u16_t left, seglen;
166    void *ptr;
167    u16_t queuelen;
168    u8_t optlen;
169
170    LWIP_DEBUGF(TCP_OUTPUT_DEBUG,
171                ("tcp_enqueue(pcb=%p, arg=%p, len=%" U16_F ", flags=%" X16_F
172                 ", apiflags=%" U16_F ")\n", (void *) pcb, arg, len,
173                 (u16_t) flags, (u16_t) apiflags));
174    LWIP_ERROR
175      ("tcp_enqueue: packet needs payload, options, or SYN/FIN (programmer violates API)",
176       ((len != 0) || (optflags != 0)
177        || ((flags & (TCP_SYN | TCP_FIN)) != 0)), return ERR_ARG;
178      );
179    LWIP_ERROR("tcp_enqueue: len != 0 || arg == NULL (programmer violates API)",
180               ((len != 0) || (arg == NULL)), return ERR_ARG;
181      );
182
183    /* fail on too much data */
184    if (len > pcb->snd_buf) {
185        LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 3,
186                    ("tcp_enqueue: too much data (len=%" U16_F " > snd_buf=%"
187                     U16_F ")\n", len, pcb->snd_buf));
188        pcb->flags |= TF_NAGLEMEMERR;
189        return ERR_MEM;
190    }
191    left = len;
192    ptr = arg;
193
194    optlen = LWIP_TCP_OPT_LENGTH(optflags);
195
196    /* seqno will be the sequence number of the first segment enqueued
197     * by the call to this function. */
198    seqno = pcb->snd_lbb;
199
200    LWIP_DEBUGF(TCP_QLEN_DEBUG,
201                ("tcp_enqueue: queuelen: %" U16_F "\n",
202                 (u16_t) pcb->snd_queuelen));
203
204    /* If total number of pbufs on the unsent/unacked queues exceeds the
205     * configured maximum, return an error */
206    queuelen = pcb->snd_queuelen;
207    /* check for configured max queuelen and possible overflow */
208    if ((queuelen >= TCP_SND_QUEUELEN) || (queuelen > TCP_SNDQUEUELEN_OVERFLOW)) {
209        LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 3,
210                    ("tcp_enqueue: too long queue %" U16_F " (max %" U16_F
211                     ")\n", queuelen, TCP_SND_QUEUELEN));
212        TCP_STATS_INC(tcp.memerr);
213        pcb->flags |= TF_NAGLEMEMERR;
214        return ERR_MEM;
215    }
216    if (queuelen != 0) {
217        LWIP_ASSERT
218          ("tcp_enqueue: pbufs on queue => at least one queue non-empty",
219           pcb->unacked != NULL || pcb->unsent != NULL);
220    } else {
221        LWIP_ASSERT("tcp_enqueue: no pbufs on queue => both queues empty",
222                    pcb->unacked == NULL && pcb->unsent == NULL);
223    }
224
225    /* First, break up the data into segments and tuck them together in
226     * the local "queue" variable. */
227    useg = queue = seg = NULL;
228    seglen = 0;
229    while (queue == NULL || left > 0) {
230        /* The segment length (including options) should be at most the MSS */
231        seglen = left > (pcb->mss - optlen) ? (pcb->mss - optlen) : left;
232
233        /* Allocate memory for tcp_seg, and fill in fields. */
234#if TRACE_ONLY_SUB_NNET
235        trace_event(TRACE_SUBSYS_NNET, TRACE_EVENT_NNET_TX_MEMP, 0);
236#endif
237        seg = memp_malloc(MEMP_TCP_SEG);
238        if (seg == NULL) {
239            LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 2,
240                        ("tcp_enqueue: could not allocate memory for tcp_seg\n"));
241            goto memerr;
242        }
243        seg->next = NULL;
244        seg->p = NULL;
245
246#if TRACE_ONLY_SUB_NNET
247        trace_event(TRACE_SUBSYS_NNET, TRACE_EVENT_NNET_TX_MEMP_D, 0);
248#endif
249        /* first segment of to-be-queued data? */
250        if (queue == NULL) {
251            queue = seg;
252        }
253        /* subsequent segments of to-be-queued data */
254        else {
255            /* Attach the segment to the end of the queued segments */
256            LWIP_ASSERT("useg != NULL", useg != NULL);
257            useg->next = seg;
258        }
259        /* remember last segment of to-be-queued data for next iteration */
260        useg = seg;
261
262        /* If copy is set, memory should be allocated
263         * and data copied into pbuf, otherwise data comes from
264         * ROM or other static memory, and need not be copied.  */
265
266        /* NOTE: modified the code to avoid the use of PBUF_ROM : -- PS */
267//    if (apiflags & TCP_WRITE_FLAG_COPY) {
268        if ((seg->p =
269             pbuf_alloc(PBUF_TRANSPORT, seglen + optlen, PBUF_RAM)) == NULL) {
270            LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 2,
271                        ("tcp_enqueue : could not allocate memory for pbuf copy size %"
272                         U16_F "\n", seglen));
273            goto memerr;
274        }
275        LWIP_ASSERT("check that first pbuf can hold the complete seglen",
276                    (seg->p->len >= seglen + optlen));
277        queuelen += pbuf_clen(seg->p);
278        if (arg != NULL) {
279            MEMCPY((char *) seg->p->payload + optlen, ptr, seglen);
280        }
281        seg->dataptr = seg->p->payload;
282//    }
283
284#if 0
285        /* do not copy data */
286        else {
287            /* First, allocate a pbuf for the headers. */
288            if ((seg->p = pbuf_alloc(PBUF_TRANSPORT, optlen, PBUF_RAM)) == NULL) {
289                LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 2,
290                            ("tcp_enqueue: could not allocate memory for header pbuf\n"));
291                goto memerr;
292            }
293            queuelen += pbuf_clen(seg->p);
294
295            /* Second, allocate a pbuf for holding the data.
296             * since the referenced data is available at least until it is sent out on the
297             * link (as it has to be ACKed by the remote party) we can safely use PBUF_ROM
298             * instead of PBUF_REF here.
299             */
300            if (left > 0) {
301                if ((p = pbuf_alloc(PBUF_RAW, seglen, PBUF_ROM)) == NULL) {
302                    /* If allocation fails, we have to deallocate the header pbuf as well. */
303                    pbuf_free(seg->p);
304                    seg->p = NULL;
305                    LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 2,
306                                ("tcp_enqueue: could not allocate memory for zero-copy pbuf\n"));
307                    goto memerr;
308                }
309                ++queuelen;
310                /* reference the non-volatile payload data */
311                p->payload = ptr;
312                seg->dataptr = ptr;
313
314                /* Concatenate the headers and data pbufs together. */
315                pbuf_cat(seg->p /*header */ , p /*data */ );
316                p = NULL;
317            }
318        }
319#endif                          // 0
320
321        /* Now that there are more segments queued, we check again if the
322           length of the queue exceeds the configured maximum or overflows. */
323        if ((queuelen > TCP_SND_QUEUELEN)
324            || (queuelen > TCP_SNDQUEUELEN_OVERFLOW)) {
325            LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 2,
326                        ("tcp_enqueue: queue too long %" U16_F " (%" U16_F
327                         ")\n", queuelen, TCP_SND_QUEUELEN));
328            goto memerr;
329        }
330
331        seg->len = seglen;
332
333        /* build TCP header */
334        if (pbuf_header(seg->p, TCP_HLEN)) {
335            LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 2,
336                        ("tcp_enqueue: no room for TCP header in pbuf.\n"));
337            TCP_STATS_INC(tcp.err);
338            goto memerr;
339        }
340        seg->tcphdr = seg->p->payload;
341        seg->tcphdr->src = htons(pcb->local_port);
342        seg->tcphdr->dest = htons(pcb->remote_port);
343        seg->tcphdr->seqno = htonl(seqno);
344        seg->tcphdr->urgp = 0;
345        TCPH_FLAGS_SET(seg->tcphdr, flags);
346        /* don't fill in tcphdr->ackno and tcphdr->wnd until later */
347
348        seg->flags = optflags;
349
350        /* Set the length of the header */
351        TCPH_HDRLEN_SET(seg->tcphdr, (5 + optlen / 4));
352        LWIP_DEBUGF(TCP_OUTPUT_DEBUG | LWIP_DBG_TRACE,
353                    ("tcp_enqueue: queueing %" U32_F ":%" U32_F " (0x%" X16_F
354                     ")\n", ntohl(seg->tcphdr->seqno),
355                     ntohl(seg->tcphdr->seqno) + TCP_TCPLEN(seg),
356                     (u16_t) flags));
357
358        left -= seglen;
359        seqno += seglen;
360        ptr = (void *) ((u8_t *) ptr + seglen);
361    }
362
363    /* Now that the data to be enqueued has been broken up into TCP
364       segments in the queue variable, we add them to the end of the
365       pcb->unsent queue. */
366    if (pcb->unsent == NULL) {
367        useg = NULL;
368    } else {
369        for (useg = pcb->unsent; useg->next != NULL; useg = useg->next);
370    }
371    /* { useg is last segment on the unsent queue, NULL if list is empty } */
372
373    /* If there is room in the last pbuf on the unsent queue,
374       chain the first pbuf on the queue together with that. */
375    if (useg != NULL &&
376        TCP_TCPLEN(useg) != 0 &&
377        !(TCPH_FLAGS(useg->tcphdr) & (TCP_SYN | TCP_FIN)) &&
378        !(flags & (TCP_SYN | TCP_FIN)) &&
379        /* fit within max seg size */
380        (useg->len + queue->len <= pcb->mss) &&
381        /* only concatenate segments with the same options */
382        (useg->flags == queue->flags)) {
383        /* Remove TCP header from first segment of our to-be-queued list */
384        if (pbuf_header(queue->p, -(TCP_HLEN + optlen))) {
385            /* Can we cope with this failing?  Just assert for now */
386            LWIP_ASSERT("pbuf_header failed\n", 0);
387            TCP_STATS_INC(tcp.err);
388            goto memerr;
389        }
390        if (queue->p->len == 0) {
391            /* free the first (header-only) pbuf if it is now empty (contained only headers) */
392            struct pbuf *old_q = queue->p;
393
394            queue->p = queue->p->next;
395            old_q->next = NULL;
396            queuelen--;
397            pbuf_free(old_q);
398        }
399        LWIP_ASSERT("zero-length pbuf", (queue->p != NULL)
400                    && (queue->p->len > 0));
401        pbuf_cat(useg->p, queue->p);
402        useg->len += queue->len;
403        useg->next = queue->next;
404
405        LWIP_DEBUGF(TCP_OUTPUT_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE,
406                    ("tcp_enqueue: chaining segments, new len %" U16_F "\n",
407                     useg->len));
408        if (seg == queue) {
409            seg = useg;
410            seglen = useg->len;
411        }
412        memp_free(MEMP_TCP_SEG, queue);
413    } else {
414        /* empty list */
415        if (useg == NULL) {
416            /* initialize list with this segment */
417            pcb->unsent = queue;
418        }
419        /* enqueue segment */
420        else {
421            useg->next = queue;
422        }
423    }
424    if ((flags & TCP_SYN) || (flags & TCP_FIN)) {
425        ++len;
426    }
427    if (flags & TCP_FIN) {
428        pcb->flags |= TF_FIN;
429    }
430    pcb->snd_lbb += len;
431
432    pcb->snd_buf -= len;
433
434    /* update number of segments on the queues */
435    pcb->snd_queuelen = queuelen;
436    LWIP_DEBUGF(TCP_QLEN_DEBUG,
437                ("tcp_enqueue: %" S16_F " (after enqueued)\n",
438                 pcb->snd_queuelen));
439    if (pcb->snd_queuelen != 0) {
440        LWIP_ASSERT("tcp_enqueue: valid queue length",
441                    pcb->unacked != NULL || pcb->unsent != NULL);
442    }
443
444    /* Set the PSH flag in the last segment that we enqueued, but only
445       if the segment has data (indicated by seglen > 0). */
446    if (seg != NULL && seglen > 0 && seg->tcphdr != NULL
447        && ((apiflags & TCP_WRITE_FLAG_MORE) == 0)) {
448        TCPH_SET_FLAG(seg->tcphdr, TCP_PSH);
449    }
450
451    return ERR_OK;
452  memerr:
453    pcb->flags |= TF_NAGLEMEMERR;
454    TCP_STATS_INC(tcp.memerr);
455
456    if (queue != NULL) {
457        tcp_segs_free(queue);
458    }
459    if (pcb->snd_queuelen != 0) {
460        LWIP_ASSERT("tcp_enqueue: valid queue length", pcb->unacked != NULL ||
461                    pcb->unsent != NULL);
462    }
463    LWIP_DEBUGF(TCP_QLEN_DEBUG | LWIP_DBG_STATE,
464                ("tcp_enqueue: %" S16_F " (with mem err)\n",
465                 pcb->snd_queuelen));
466    return ERR_MEM;
467}
468
469
470#if LWIP_TCP_TIMESTAMPS
471/* Build a timestamp option (12 bytes long) at the specified options pointer)
472 *
473 * @param pcb tcp_pcb
474 * @param opts option pointer where to store the timestamp option
475 */
476static void tcp_build_timestamp_option(struct tcp_pcb *pcb, u32_t * opts)
477{
478    /* Pad with two NOP options to make everything nicely aligned */
479    opts[0] = htonl(0x0101080A);
480    opts[1] = htonl(sys_now());
481    opts[2] = htonl(pcb->ts_recent);
482}
483#endif
484
485
486/**
487 * Find out what we can send and send it
488 *
489 * @param pcb Protocol control block for the TCP connection to send data
490 * @return ERR_OK if data has been sent or nothing to send
491 *         another err_t on error
492 */
493err_t tcp_output(struct tcp_pcb *pcb)
494{
495    struct pbuf *p;
496    struct tcp_hdr *tcphdr;
497    struct tcp_seg *seg, *useg;
498    u32_t wnd, snd_nxt;
499
500#if TCP_CWND_DEBUG
501    s16_t i = 0;
502#endif                          /* TCP_CWND_DEBUG */
503    u8_t optlen = 0;
504
505    /* First, check if we are invoked by the TCP input processing
506       code. If so, we do not output anything. Instead, we rely on the
507       input processing code to call us when input processing is done
508       with. */
509    if (tcp_input_pcb == pcb) {
510        return ERR_OK;
511    }
512
513    wnd = LWIP_MIN(pcb->snd_wnd, pcb->cwnd);
514
515    seg = pcb->unsent;
516
517    /* useg should point to last segment on unacked queue */
518    useg = pcb->unacked;
519    if (useg != NULL) {
520        for (; useg->next != NULL; useg = useg->next);
521    }
522
523    /* If the TF_ACK_NOW flag is set and no data will be sent (either
524     * because the ->unsent queue is empty or because the window does
525     * not allow it), construct an empty ACK segment and send it.
526     *
527     * If data is to be sent, we will just piggyback the ACK (see below).
528     */
529    if (pcb->flags & TF_ACK_NOW &&
530        (seg == NULL ||
531         ntohl(seg->tcphdr->seqno) - pcb->lastack + seg->len > wnd)) {
532#if LWIP_TCP_TIMESTAMPS
533        if (pcb->flags & TF_TIMESTAMP)
534            optlen = LWIP_TCP_OPT_LENGTH(TF_SEG_OPTS_TS);
535#endif
536        p = pbuf_alloc(PBUF_IP, TCP_HLEN + optlen, PBUF_RAM);
537        if (p == NULL) {
538            LWIP_DEBUGF(TCP_OUTPUT_DEBUG,
539                        ("tcp_output: (ACK) could not allocate pbuf\n"));
540            return ERR_BUF;
541        }
542        LWIP_DEBUGF(TCP_OUTPUT_DEBUG,
543                    ("tcp_output: sending ACK for %" U32_F "\n", pcb->rcv_nxt));
544        /* remove ACK flags from the PCB, as we send an empty ACK now */
545        pcb->flags &= ~(TF_ACK_DELAY | TF_ACK_NOW);
546
547        tcphdr = tcp_output_set_header(pcb, p, optlen, htonl(pcb->snd_nxt));
548
549        /* NB. MSS option is only sent on SYNs, so ignore it here */
550#if LWIP_TCP_TIMESTAMPS
551        pcb->ts_lastacksent = pcb->rcv_nxt;
552
553        if (pcb->flags & TF_TIMESTAMP)
554            tcp_build_timestamp_option(pcb, (u32_t *) (tcphdr + 1));
555#endif
556
557#if CHECKSUM_GEN_TCP
558        if (is_hw_feature_enabled(TCP_IPV4_CHECKSUM_HW)) {
559            p->nicflags |= NETIF_TXFLAG_TCPCHECKSUM;
560            p->nicflags |= TCPH_HDRLEN(tcphdr) << NETIF_TXFLAG_TCPHDRLEN_SHIFT;
561            tcphdr->chksum =
562                (~inet_chksum_pseudo_partial(p, &(pcb->local_ip), &(pcb->remote_ip),
563                                             IP_PROTO_TCP, p->tot_len, 0)) & 0xffff;
564        } else {
565            tcphdr->chksum =
566                inet_chksum_pseudo(p, &(pcb->local_ip), &(pcb->remote_ip),
567                        IP_PROTO_TCP, p->tot_len);
568        }
569#endif
570#if LWIP_NETIF_HWADDRHINT
571        ip_output_hinted(p, &(pcb->local_ip), &(pcb->remote_ip), pcb->ttl,
572                         pcb->tos, IP_PROTO_TCP, &(pcb->addr_hint));
573#else                           /* LWIP_NETIF_HWADDRHINT */
574        ip_output(p, &(pcb->local_ip), &(pcb->remote_ip), pcb->ttl, pcb->tos,
575                  IP_PROTO_TCP);
576#endif                          /* LWIP_NETIF_HWADDRHINT */
577        pbuf_free(p);
578
579        return ERR_OK;
580    }
581#if TCP_OUTPUT_DEBUG
582    if (seg == NULL) {
583        LWIP_DEBUGF(TCP_OUTPUT_DEBUG, ("tcp_output: nothing to send (%p)\n",
584                                       (void *) pcb->unsent));
585    }
586#endif                          /* TCP_OUTPUT_DEBUG */
587#if TCP_CWND_DEBUG
588    if (seg == NULL) {
589        LWIP_DEBUGF(TCP_CWND_DEBUG, ("tcp_output: snd_wnd %" U16_F
590                                     ", cwnd %" U16_F ", wnd %" U32_F
591                                     ", seg == NULL, ack %" U32_F "\n",
592                                     pcb->snd_wnd, pcb->cwnd, wnd,
593                                     pcb->lastack));
594    } else {
595        LWIP_DEBUGF(TCP_CWND_DEBUG,
596                    ("tcp_output: snd_wnd %" U16_F ", cwnd %" U16_F ", wnd %"
597                     U32_F ", effwnd %" U32_F ", seq %" U32_F ", ack %" U32_F
598                     "\n", pcb->snd_wnd, pcb->cwnd, wnd,
599                     ntohl(seg->tcphdr->seqno) - pcb->lastack + seg->len,
600                     ntohl(seg->tcphdr->seqno), pcb->lastack));
601    }
602#endif                          /* TCP_CWND_DEBUG */
603    /* data available and window allows it to be sent? */
604    while (seg != NULL &&
605           ntohl(seg->tcphdr->seqno) - pcb->lastack + seg->len <= wnd) {
606        LWIP_ASSERT("RST not expected here!",
607                    (TCPH_FLAGS(seg->tcphdr) & TCP_RST) == 0);
608        /* Stop sending if the nagle algorithm would prevent it
609         * Don't stop:
610         * - if tcp_enqueue had a memory error before (prevent delayed ACK timeout) or
611         * - if FIN was already enqueued for this PCB (SYN is always alone in a segment -
612         *   either seg->next != NULL or pcb->unacked == NULL;
613         *   RST is no sent using tcp_enqueue/tcp_output.
614         */
615        if ((tcp_do_output_nagle(pcb) == 0) &&
616            ((pcb->flags & (TF_NAGLEMEMERR | TF_FIN)) == 0)) {
617            break;
618        }
619#if TCP_CWND_DEBUG
620        LWIP_DEBUGF(TCP_CWND_DEBUG,
621                    ("tcp_output: snd_wnd %" U16_F ", cwnd %" U16_F ", wnd %"
622                     U32_F ", effwnd %" U32_F ", seq %" U32_F ", ack %" U32_F
623                     ", i %" S16_F "\n", pcb->snd_wnd, pcb->cwnd, wnd,
624                     ntohl(seg->tcphdr->seqno) + seg->len - pcb->lastack,
625                     ntohl(seg->tcphdr->seqno), pcb->lastack, i));
626        ++i;
627#endif                          /* TCP_CWND_DEBUG */
628
629        pcb->unsent = seg->next;
630
631        if (pcb->state != SYN_SENT) {
632            TCPH_SET_FLAG(seg->tcphdr, TCP_ACK);
633            pcb->flags &= ~(TF_ACK_DELAY | TF_ACK_NOW);
634        }
635
636        tcp_output_segment(seg, pcb);
637        snd_nxt = ntohl(seg->tcphdr->seqno) + TCP_TCPLEN(seg);
638        if (TCP_SEQ_LT(pcb->snd_nxt, snd_nxt)) {
639            pcb->snd_nxt = snd_nxt;
640        }
641        /* put segment on unacknowledged list if length > 0 */
642        if (TCP_TCPLEN(seg) > 0) {
643            seg->next = NULL;
644            /* unacked list is empty? */
645            if (pcb->unacked == NULL) {
646                pcb->unacked = seg;
647                useg = seg;
648                /* unacked list is not empty? */
649            } else {
650                /* In the case of fast retransmit, the packet should not go to the tail
651                 * of the unacked queue, but rather somewhere before it. We need to check for
652                 * this case. -STJ Jul 27, 2004 */
653                if (TCP_SEQ_LT
654                    (ntohl(seg->tcphdr->seqno), ntohl(useg->tcphdr->seqno))) {
655                    /* add segment to before tail of unacked list, keeping the list sorted */
656                    struct tcp_seg **cur_seg = &(pcb->unacked);
657
658                    while (*cur_seg &&
659                           TCP_SEQ_LT(ntohl((*cur_seg)->tcphdr->seqno),
660                                      ntohl(seg->tcphdr->seqno))) {
661                        cur_seg = &((*cur_seg)->next);
662                    }
663                    seg->next = (*cur_seg);
664                    (*cur_seg) = seg;
665                } else {
666                    /* add segment to tail of unacked list */
667                    useg->next = seg;
668                    useg = useg->next;
669                }
670            }
671            /* do not queue empty segments on the unacked list */
672        } else {
673            tcp_seg_free(seg);
674        }
675        seg = pcb->unsent;
676    }
677
678    if (seg != NULL && pcb->persist_backoff == 0 &&
679        ntohl(seg->tcphdr->seqno) - pcb->lastack + seg->len > pcb->snd_wnd) {
680        /* prepare for persist timer */
681        pcb->persist_cnt = 0;
682        pcb->persist_backoff = 1;
683    }
684
685    pcb->flags &= ~TF_NAGLEMEMERR;
686    return ERR_OK;
687}
688
689/**
690 * Called by tcp_output() to actually send a TCP segment over IP.
691 *
692 * @param seg the tcp_seg to send
693 * @param pcb the tcp_pcb for the TCP connection used to send the segment
694 */
695static void tcp_output_segment(struct tcp_seg *seg, struct tcp_pcb *pcb)
696{
697    u16_t len;
698    struct netif *netif;
699    u32_t *opts;
700
701  /** @bug Exclude retransmitted segments from this count. */
702    snmp_inc_tcpoutsegs();
703
704    /* The TCP header has already been constructed, but the ackno and
705       wnd fields remain. */
706    seg->tcphdr->ackno = htonl(pcb->rcv_nxt);
707
708    /* advertise our receive window size in this TCP segment */
709    seg->tcphdr->wnd = htons(pcb->rcv_ann_wnd);
710
711    pcb->rcv_ann_right_edge = pcb->rcv_nxt + pcb->rcv_ann_wnd;
712
713    /* Add any requested options.  NB MSS option is only set on SYN
714       packets, so ignore it here */
715    opts = (u32_t *) (seg->tcphdr + 1);
716    if (seg->flags & TF_SEG_OPTS_MSS) {
717        TCP_BUILD_MSS_OPTION(*opts);
718        opts += 1;
719    }
720#if LWIP_TCP_TIMESTAMPS
721    pcb->ts_lastacksent = pcb->rcv_nxt;
722
723    if (seg->flags & TF_SEG_OPTS_TS) {
724        tcp_build_timestamp_option(pcb, opts);
725        opts += 3;
726    }
727#endif
728
729    /* If we don't have a local IP address, we get one by
730       calling ip_route(). */
731    if (ip_addr_isany(&(pcb->local_ip))) {
732        netif = ip_route(&(pcb->remote_ip));
733        if (netif == NULL) {
734            return;
735        }
736        ip_addr_set(&(pcb->local_ip), &(netif->ip_addr));
737    }
738
739    /* Set retransmission timer running if it is not currently enabled */
740    if (pcb->rtime == -1)
741        pcb->rtime = 0;
742
743    if (pcb->rttest == 0) {
744        pcb->rttest = tcp_ticks;
745        pcb->rtseq = ntohl(seg->tcphdr->seqno);
746
747        LWIP_DEBUGF(TCP_RTO_DEBUG,
748                    ("tcp_output_segment: rtseq %" U32_F "\n", pcb->rtseq));
749    }
750    LWIP_DEBUGF(TCP_OUTPUT_DEBUG,
751                ("tcp_output_segment: %" U32_F ":%" U32_F "\n",
752                 htonl(seg->tcphdr->seqno),
753                 htonl(seg->tcphdr->seqno) + seg->len));
754
755    len = (u16_t) ((u8_t *) seg->tcphdr - (u8_t *) seg->p->payload);
756
757    seg->p->len -= len;
758    seg->p->tot_len -= len;
759
760    seg->p->payload = seg->tcphdr;
761
762    seg->tcphdr->chksum = 0;
763#if CHECKSUM_GEN_TCP
764
765    if (is_hw_feature_enabled(TCP_IPV4_CHECKSUM_HW)) {
766        seg->p->nicflags |= NETIF_TXFLAG_TCPCHECKSUM;
767        seg->p->nicflags |=
768            TCPH_HDRLEN(seg->tcphdr) << NETIF_TXFLAG_TCPHDRLEN_SHIFT;
769        seg->tcphdr->chksum = (~inet_chksum_pseudo_partial(seg->p,
770                    &(pcb->local_ip),
771                    &(pcb->remote_ip),
772                    IP_PROTO_TCP, seg->p->tot_len,
773                    0)) & 0xffff;
774    } else {
775        seg->tcphdr->chksum = inet_chksum_pseudo(seg->p,
776                    &(pcb->local_ip),
777                    &(pcb->remote_ip),
778                    IP_PROTO_TCP, seg->p->tot_len);
779    }
780#endif
781    TCP_STATS_INC(tcp.xmit);
782
783#if LWIP_NETIF_HWADDRHINT
784    ip_output_hinted(seg->p, &(pcb->local_ip), &(pcb->remote_ip), pcb->ttl,
785                     pcb->tos, IP_PROTO_TCP, &(pcb->addr_hint));
786#else                           /* LWIP_NETIF_HWADDRHINT */
787    ip_output(seg->p, &(pcb->local_ip), &(pcb->remote_ip), pcb->ttl, pcb->tos,
788              IP_PROTO_TCP);
789#endif                          /* LWIP_NETIF_HWADDRHINT */
790}
791
792/**
793 * Send a TCP RESET packet (empty segment with RST flag set) either to
794 * abort a connection or to show that there is no matching local connection
795 * for a received segment.
796 *
797 * Called by tcp_abort() (to abort a local connection), tcp_input() (if no
798 * matching local pcb was found), tcp_listen_input() (if incoming segment
799 * has ACK flag set) and tcp_process() (received segment in the wrong state)
800 *
801 * Since a RST segment is in most cases not sent for an active connection,
802 * tcp_rst() has a number of arguments that are taken from a tcp_pcb for
803 * most other segment output functions.
804 *
805 * @param seqno the sequence number to use for the outgoing segment
806 * @param ackno the acknowledge number to use for the outgoing segment
807 * @param local_ip the local IP address to send the segment from
808 * @param remote_ip the remote IP address to send the segment to
809 * @param local_port the local TCP port to send the segment from
810 * @param remote_port the remote TCP port to send the segment to
811 */
812void
813tcp_rst(u32_t seqno, u32_t ackno,
814        struct ip_addr *local_ip, struct ip_addr *remote_ip,
815        u16_t local_port, u16_t remote_port)
816{
817    struct pbuf *p;
818    struct tcp_hdr *tcphdr;
819
820    p = pbuf_alloc(PBUF_IP, TCP_HLEN, PBUF_RAM);
821    if (p == NULL) {
822        LWIP_DEBUGF(TCP_DEBUG,
823                    ("tcp_rst: could not allocate memory for pbuf\n"));
824        return;
825    }
826    LWIP_ASSERT("check that first pbuf can hold struct tcp_hdr",
827                (p->len >= sizeof(struct tcp_hdr)));
828
829    tcphdr = p->payload;
830    tcphdr->src = htons(local_port);
831    tcphdr->dest = htons(remote_port);
832    tcphdr->seqno = htonl(seqno);
833    tcphdr->ackno = htonl(ackno);
834    TCPH_FLAGS_SET(tcphdr, TCP_RST | TCP_ACK);
835    tcphdr->wnd = htons(TCP_WND);
836    tcphdr->urgp = 0;
837    TCPH_HDRLEN_SET(tcphdr, 5);
838
839    tcphdr->chksum = 0;
840#if CHECKSUM_GEN_TCP
841    tcphdr->chksum = inet_chksum_pseudo(p, local_ip, remote_ip,
842                                        IP_PROTO_TCP, p->tot_len);
843#endif
844    TCP_STATS_INC(tcp.xmit);
845    snmp_inc_tcpoutrsts();
846    /* Send output with hardcoded TTL since we have no access to the pcb */
847    ip_output(p, local_ip, remote_ip, TCP_TTL, 0, IP_PROTO_TCP);
848    pbuf_free(p);
849    LWIP_DEBUGF(TCP_RST_DEBUG,
850                ("tcp_rst: seqno %" U32_F " ackno %" U32_F ".\n", seqno,
851                 ackno));
852}
853
854/**
855 * Requeue all unacked segments for retransmission
856 *
857 * Called by tcp_slowtmr() for slow retransmission.
858 *
859 * @param pcb the tcp_pcb for which to re-enqueue all unacked segments
860 */
861void tcp_rexmit_rto(struct tcp_pcb *pcb)
862{
863    struct tcp_seg *seg;
864
865    if (pcb->unacked == NULL) {
866        return;
867    }
868
869    /* Move all unacked segments to the head of the unsent queue */
870    for (seg = pcb->unacked; seg->next != NULL; seg = seg->next);
871    /* concatenate unsent queue after unacked queue */
872    seg->next = pcb->unsent;
873    /* unsent queue is the concatenated queue (of unacked, unsent) */
874    pcb->unsent = pcb->unacked;
875    /* unacked queue is now empty */
876    pcb->unacked = NULL;
877
878    /* increment number of retransmissions */
879    ++pcb->nrtx;
880
881    /* Don't take any RTT measurements after retransmitting. */
882    pcb->rttest = 0;
883
884    /* Do the actual retransmission */
885    tcp_output(pcb);
886}
887
888/**
889 * Requeue the first unacked segment for retransmission
890 *
891 * Called by tcp_receive() for fast retramsmit.
892 *
893 * @param pcb the tcp_pcb for which to retransmit the first unacked segment
894 */
895void tcp_rexmit(struct tcp_pcb *pcb)
896{
897    struct tcp_seg *seg;
898    struct tcp_seg **cur_seg;
899
900    if (pcb->unacked == NULL) {
901        return;
902    }
903
904    /* Move the first unacked segment to the unsent queue */
905    /* Keep the unsent queue sorted. */
906    seg = pcb->unacked;
907    pcb->unacked = seg->next;
908
909    cur_seg = &(pcb->unsent);
910    while (*cur_seg &&
911           TCP_SEQ_LT(ntohl((*cur_seg)->tcphdr->seqno),
912                      ntohl(seg->tcphdr->seqno))) {
913        cur_seg = &((*cur_seg)->next);
914    }
915    seg->next = *cur_seg;
916    *cur_seg = seg;
917
918    ++pcb->nrtx;
919
920    /* Don't take any rtt measurements after retransmitting. */
921    pcb->rttest = 0;
922
923    /* Do the actual retransmission. */
924    snmp_inc_tcpretranssegs();
925    tcp_output(pcb);
926}
927
928/**
929 * Send keepalive packets to keep a connection active although
930 * no data is sent over it.
931 *
932 * Called by tcp_slowtmr()
933 *
934 * @param pcb the tcp_pcb for which to send a keepalive packet
935 */
936void tcp_keepalive(struct tcp_pcb *pcb)
937{
938    struct pbuf *p;
939    struct tcp_hdr *tcphdr;
940
941    LWIP_DEBUGF(TCP_DEBUG,
942                ("tcp_keepalive: sending KEEPALIVE probe to %" U16_F ".%" U16_F
943                 ".%" U16_F ".%" U16_F "\n", ip4_addr1(&pcb->remote_ip),
944                 ip4_addr2(&pcb->remote_ip), ip4_addr3(&pcb->remote_ip),
945                 ip4_addr4(&pcb->remote_ip)));
946
947    LWIP_DEBUGF(TCP_DEBUG,
948                ("tcp_keepalive: tcp_ticks %" U32_F "   pcb->tmr %" U32_F
949                 " pcb->keep_cnt_sent %" U16_F "\n", tcp_ticks, pcb->tmr,
950                 pcb->keep_cnt_sent));
951
952    p = pbuf_alloc(PBUF_IP, TCP_HLEN, PBUF_RAM);
953
954    if (p == NULL) {
955        LWIP_DEBUGF(TCP_DEBUG,
956                    ("tcp_keepalive: could not allocate memory for pbuf\n"));
957        return;
958    }
959    LWIP_ASSERT("check that first pbuf can hold struct tcp_hdr",
960                (p->len >= sizeof(struct tcp_hdr)));
961
962    tcphdr = tcp_output_set_header(pcb, p, 0, htonl(pcb->snd_nxt - 1));
963
964#if CHECKSUM_GEN_TCP
965    tcphdr->chksum = inet_chksum_pseudo(p, &pcb->local_ip, &pcb->remote_ip,
966                                        IP_PROTO_TCP, p->tot_len);
967#endif
968    TCP_STATS_INC(tcp.xmit);
969
970    /* Send output to IP */
971#if LWIP_NETIF_HWADDRHINT
972    ip_output_hinted(p, &pcb->local_ip, &pcb->remote_ip, pcb->ttl, 0,
973                     IP_PROTO_TCP, &(pcb->addr_hint));
974#else                           /* LWIP_NETIF_HWADDRHINT */
975    ip_output(p, &pcb->local_ip, &pcb->remote_ip, pcb->ttl, 0, IP_PROTO_TCP);
976#endif                          /* LWIP_NETIF_HWADDRHINT */
977
978    pbuf_free(p);
979
980    LWIP_DEBUGF(TCP_DEBUG,
981                ("tcp_keepalive: seqno %" U32_F " ackno %" U32_F ".\n",
982                 pcb->snd_nxt - 1, pcb->rcv_nxt));
983}
984
985
986/**
987 * Send persist timer zero-window probes to keep a connection active
988 * when a window update is lost.
989 *
990 * Called by tcp_slowtmr()
991 *
992 * @param pcb the tcp_pcb for which to send a zero-window probe packet
993 */
994void tcp_zero_window_probe(struct tcp_pcb *pcb)
995{
996    struct pbuf *p;
997    struct tcp_hdr *tcphdr;
998    struct tcp_seg *seg;
999
1000    LWIP_DEBUGF(TCP_DEBUG,
1001                ("tcp_zero_window_probe: sending ZERO WINDOW probe to %"
1002                 U16_F ".%" U16_F ".%" U16_F ".%" U16_F "\n",
1003                 ip4_addr1(&pcb->remote_ip), ip4_addr2(&pcb->remote_ip),
1004                 ip4_addr3(&pcb->remote_ip), ip4_addr4(&pcb->remote_ip)));
1005
1006    LWIP_DEBUGF(TCP_DEBUG,
1007                ("tcp_zero_window_probe: tcp_ticks %" U32_F
1008                 "   pcb->tmr %" U32_F " pcb->keep_cnt_sent %" U16_F "\n",
1009                 tcp_ticks, pcb->tmr, pcb->keep_cnt_sent));
1010
1011    seg = pcb->unacked;
1012
1013    if (seg == NULL)
1014        seg = pcb->unsent;
1015
1016    if (seg == NULL)
1017        return;
1018
1019    p = pbuf_alloc(PBUF_IP, TCP_HLEN + 1, PBUF_RAM);
1020
1021    if (p == NULL) {
1022        LWIP_DEBUGF(TCP_DEBUG, ("tcp_zero_window_probe: no memory for pbuf\n"));
1023        return;
1024    }
1025    LWIP_ASSERT("check that first pbuf can hold struct tcp_hdr",
1026                (p->len >= sizeof(struct tcp_hdr)));
1027
1028    tcphdr = tcp_output_set_header(pcb, p, 0, seg->tcphdr->seqno);
1029
1030    /* Copy in one byte from the head of the unacked queue */
1031    *((char *) p->payload + sizeof(struct tcp_hdr)) = *(char *) seg->dataptr;
1032
1033#if CHECKSUM_GEN_TCP
1034    tcphdr->chksum = inet_chksum_pseudo(p, &pcb->local_ip, &pcb->remote_ip,
1035                                        IP_PROTO_TCP, p->tot_len);
1036#endif
1037    TCP_STATS_INC(tcp.xmit);
1038
1039    /* Send output to IP */
1040#if LWIP_NETIF_HWADDRHINT
1041    ip_output_hinted(p, &pcb->local_ip, &pcb->remote_ip, pcb->ttl, 0,
1042                     IP_PROTO_TCP, &(pcb->addr_hint));
1043#else                           /* LWIP_NETIF_HWADDRHINT */
1044    ip_output(p, &pcb->local_ip, &pcb->remote_ip, pcb->ttl, 0, IP_PROTO_TCP);
1045#endif                          /* LWIP_NETIF_HWADDRHINT */
1046
1047    pbuf_free(p);
1048
1049    LWIP_DEBUGF(TCP_DEBUG, ("tcp_zero_window_probe: seqno %" U32_F
1050                            " ackno %" U32_F ".\n",
1051                            pcb->snd_nxt - 1, pcb->rcv_nxt));
1052}
1053#endif                          /* LWIP_TCP */
1054