]> rtime.felk.cvut.cz Git - pes-rpp/rpp-lwip.git/blob - src/core/tcp_out.c
1a6ff53b6e93bedbd71d59575b27ac9e1335c6c3
[pes-rpp/rpp-lwip.git] / src / core / tcp_out.c
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_impl.h"
46 #include "lwip/def.h"
47 #include "lwip/mem.h"
48 #include "lwip/memp.h"
49 #include "lwip/ip_addr.h"
50 #include "lwip/netif.h"
51 #include "lwip/inet_chksum.h"
52 #include "lwip/stats.h"
53 #include "lwip/snmp.h"
54 #include "lwip/ip6.h"
55 #include "lwip/ip6_addr.h"
56 #include "lwip/inet_chksum.h"
57 #if LWIP_TCP_TIMESTAMPS
58 #include "lwip/sys.h"
59 #endif
60
61 #include <string.h>
62
63 /* Define some copy-macros for checksum-on-copy so that the code looks
64    nicer by preventing too many ifdef's. */
65 #if TCP_CHECKSUM_ON_COPY
66 #define TCP_DATA_COPY(dst, src, len, seg) do { \
67   tcp_seg_add_chksum(LWIP_CHKSUM_COPY(dst, src, len), \
68                      len, &seg->chksum, &seg->chksum_swapped); \
69   seg->flags |= TF_SEG_DATA_CHECKSUMMED; } while(0)
70 #define TCP_DATA_COPY2(dst, src, len, chksum, chksum_swapped)  \
71   tcp_seg_add_chksum(LWIP_CHKSUM_COPY(dst, src, len), len, chksum, chksum_swapped);
72 #else /* TCP_CHECKSUM_ON_COPY*/
73 #define TCP_DATA_COPY(dst, src, len, seg)                     MEMCPY(dst, src, len)
74 #define TCP_DATA_COPY2(dst, src, len, chksum, chksum_swapped) MEMCPY(dst, src, len)
75 #endif /* TCP_CHECKSUM_ON_COPY*/
76
77 /** Define this to 1 for an extra check that the output checksum is valid
78  * (usefule when the checksum is generated by the application, not the stack) */
79 #ifndef TCP_CHECKSUM_ON_COPY_SANITY_CHECK
80 #define TCP_CHECKSUM_ON_COPY_SANITY_CHECK   0
81 #endif
82
83 /* Forward declarations.*/
84 static void tcp_output_segment(struct tcp_seg *seg, struct tcp_pcb *pcb);
85
86 /** Allocate a pbuf and create a tcphdr at p->payload, used for output
87  * functions other than the default tcp_output -> tcp_output_segment
88  * (e.g. tcp_send_empty_ack, etc.)
89  *
90  * @param pcb tcp pcb for which to send a packet (used to initialize tcp_hdr)
91  * @param optlen length of header-options
92  * @param datalen length of tcp data to reserve in pbuf
93  * @param seqno_be seqno in network byte order (big-endian)
94  * @return pbuf with p->payload being the tcp_hdr
95  */
96 static struct pbuf *
97 tcp_output_alloc_header(struct tcp_pcb *pcb, u16_t optlen, u16_t datalen,
98                       u32_t seqno_be /* already in network byte order */)
99 {
100   struct tcp_hdr *tcphdr;
101   struct pbuf *p = pbuf_alloc(PBUF_IP, TCP_HLEN + optlen + datalen, PBUF_RAM);
102   if (p != NULL) {
103     LWIP_ASSERT("check that first pbuf can hold struct tcp_hdr",
104                  (p->len >= TCP_HLEN + optlen));
105     tcphdr = (struct tcp_hdr *)p->payload;
106     tcphdr->src = htons(pcb->local_port);
107     tcphdr->dest = htons(pcb->remote_port);
108     tcphdr->seqno = seqno_be;
109     tcphdr->ackno = htonl(pcb->rcv_nxt);
110     TCPH_HDRLEN_FLAGS_SET(tcphdr, (5 + optlen / 4), TCP_ACK);
111     tcphdr->wnd = htons(pcb->rcv_ann_wnd);
112     tcphdr->chksum = 0;
113     tcphdr->urgp = 0;
114
115     /* If we're sending a packet, update the announced right window edge */
116     pcb->rcv_ann_right_edge = pcb->rcv_nxt + pcb->rcv_ann_wnd;
117   }
118   return p;
119 }
120
121 /**
122  * Called by tcp_close() to send a segment including FIN flag but not data.
123  *
124  * @param pcb the tcp_pcb over which to send a segment
125  * @return ERR_OK if sent, another err_t otherwise
126  */
127 err_t
128 tcp_send_fin(struct tcp_pcb *pcb)
129 {
130   /* first, try to add the fin to the last unsent segment */
131   if (pcb->unsent != NULL) {
132     struct tcp_seg *last_unsent;
133     for (last_unsent = pcb->unsent; last_unsent->next != NULL;
134          last_unsent = last_unsent->next);
135
136     if ((TCPH_FLAGS(last_unsent->tcphdr) & (TCP_SYN | TCP_FIN | TCP_RST)) == 0) {
137       /* no SYN/FIN/RST flag in the header, we can add the FIN flag */
138       TCPH_SET_FLAG(last_unsent->tcphdr, TCP_FIN);
139       pcb->flags |= TF_FIN;
140       return ERR_OK;
141     }
142   }
143   /* no data, no length, flags, copy=1, no optdata */
144   return tcp_enqueue_flags(pcb, TCP_FIN);
145 }
146
147 /**
148  * Create a TCP segment with prefilled header.
149  *
150  * Called by tcp_write and tcp_enqueue_flags.
151  *
152  * @param pcb Protocol control block for the TCP connection.
153  * @param p pbuf that is used to hold the TCP header.
154  * @param flags TCP flags for header.
155  * @param seqno TCP sequence number of this packet
156  * @param optflags options to include in TCP header
157  * @return a new tcp_seg pointing to p, or NULL.
158  * The TCP header is filled in except ackno and wnd.
159  * p is freed on failure.
160  */
161 static struct tcp_seg *
162 tcp_create_segment(struct tcp_pcb *pcb, struct pbuf *p, u8_t flags, u32_t seqno, u8_t optflags)
163 {
164   struct tcp_seg *seg;
165   u8_t optlen = LWIP_TCP_OPT_LENGTH(optflags);
166
167   if ((seg = (struct tcp_seg *)memp_malloc(MEMP_TCP_SEG)) == NULL) {
168     LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 2, ("tcp_create_segment: no memory.\n"));
169     pbuf_free(p);
170     return NULL;
171   }
172   seg->flags = optflags;
173   seg->next = NULL;
174   seg->p = p;
175   seg->len = p->tot_len - optlen;
176 #if TCP_OVERSIZE_DBGCHECK
177   seg->oversize_left = 0;
178 #endif /* TCP_OVERSIZE_DBGCHECK */
179 #if TCP_CHECKSUM_ON_COPY
180   seg->chksum = 0;
181   seg->chksum_swapped = 0;
182   /* check optflags */
183   LWIP_ASSERT("invalid optflags passed: TF_SEG_DATA_CHECKSUMMED",
184               (optflags & TF_SEG_DATA_CHECKSUMMED) == 0);
185 #endif /* TCP_CHECKSUM_ON_COPY */
186
187   /* build TCP header */
188   if (pbuf_header(p, TCP_HLEN)) {
189     LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 2, ("tcp_create_segment: no room for TCP header in pbuf.\n"));
190     TCP_STATS_INC(tcp.err);
191     tcp_seg_free(seg);
192     return NULL;
193   }
194   seg->tcphdr = (struct tcp_hdr *)seg->p->payload;
195   seg->tcphdr->src = htons(pcb->local_port);
196   seg->tcphdr->dest = htons(pcb->remote_port);
197   seg->tcphdr->seqno = htonl(seqno);
198   /* ackno is set in tcp_output */
199   TCPH_HDRLEN_FLAGS_SET(seg->tcphdr, (5 + optlen / 4), flags);
200   /* wnd and chksum are set in tcp_output */
201   seg->tcphdr->urgp = 0;
202   return seg;
203
204
205 /**
206  * Allocate a PBUF_RAM pbuf, perhaps with extra space at the end.
207  *
208  * This function is like pbuf_alloc(layer, length, PBUF_RAM) except
209  * there may be extra bytes available at the end.
210  *
211  * @param layer flag to define header size.
212  * @param length size of the pbuf's payload.
213  * @param max_length maximum usable size of payload+oversize.
214  * @param oversize pointer to a u16_t that will receive the number of usable tail bytes.
215  * @param pcb The TCP connection that willo enqueue the pbuf.
216  * @param apiflags API flags given to tcp_write.
217  * @param first_seg true when this pbuf will be used in the first enqueued segment.
218  * @param 
219  */
220 #if TCP_OVERSIZE
221 static struct pbuf *
222 tcp_pbuf_prealloc(pbuf_layer layer, u16_t length, u16_t max_length,
223                   u16_t *oversize, struct tcp_pcb *pcb, u8_t apiflags,
224                   u8_t first_seg)
225 {
226   struct pbuf *p;
227   u16_t alloc = length;
228
229 #if LWIP_NETIF_TX_SINGLE_PBUF
230   LWIP_UNUSED_ARG(max_length);
231   LWIP_UNUSED_ARG(pcb);
232   LWIP_UNUSED_ARG(apiflags);
233   LWIP_UNUSED_ARG(first_seg);
234   /* always create MSS-sized pbufs */
235   alloc = pcb->mss;
236 #else /* LWIP_NETIF_TX_SINGLE_PBUF */
237   if (length < max_length) {
238     /* Should we allocate an oversized pbuf, or just the minimum
239      * length required? If tcp_write is going to be called again
240      * before this segment is transmitted, we want the oversized
241      * buffer. If the segment will be transmitted immediately, we can
242      * save memory by allocating only length. We use a simple
243      * heuristic based on the following information:
244      *
245      * Did the user set TCP_WRITE_FLAG_MORE?
246      *
247      * Will the Nagle algorithm defer transmission of this segment?
248      */
249     if ((apiflags & TCP_WRITE_FLAG_MORE) ||
250         (!(pcb->flags & TF_NODELAY) &&
251          (!first_seg ||
252           pcb->unsent != NULL ||
253           pcb->unacked != NULL))) {
254       alloc = LWIP_MIN(max_length, LWIP_MEM_ALIGN_SIZE(length + TCP_OVERSIZE));
255     }
256   }
257 #endif /* LWIP_NETIF_TX_SINGLE_PBUF */
258   p = pbuf_alloc(layer, alloc, PBUF_RAM);
259   if (p == NULL) {
260     return NULL;
261   }
262   LWIP_ASSERT("need unchained pbuf", p->next == NULL);
263   *oversize = p->len - length;
264   /* trim p->len to the currently used size */
265   p->len = p->tot_len = length;
266   return p;
267 }
268 #else /* TCP_OVERSIZE */
269 #define tcp_pbuf_prealloc(layer, length, mx, os, pcb, api, fst) pbuf_alloc((layer), (length), PBUF_RAM)
270 #endif /* TCP_OVERSIZE */
271
272 #if TCP_CHECKSUM_ON_COPY
273 /** Add a checksum of newly added data to the segment */
274 static void
275 tcp_seg_add_chksum(u16_t chksum, u16_t len, u16_t *seg_chksum,
276                    u8_t *seg_chksum_swapped)
277 {
278   u32_t helper;
279   /* add chksum to old chksum and fold to u16_t */
280   helper = chksum + *seg_chksum;
281   chksum = FOLD_U32T(helper);
282   if ((len & 1) != 0) {
283     *seg_chksum_swapped = 1 - *seg_chksum_swapped;
284     chksum = SWAP_BYTES_IN_WORD(chksum);
285   }
286   *seg_chksum = chksum;
287 }
288 #endif /* TCP_CHECKSUM_ON_COPY */
289
290 /** Checks if tcp_write is allowed or not (checks state, snd_buf and snd_queuelen).
291  *
292  * @param pcb the tcp pcb to check for
293  * @param len length of data to send (checked agains snd_buf)
294  * @return ERR_OK if tcp_write is allowed to proceed, another err_t otherwise
295  */
296 static err_t
297 tcp_write_checks(struct tcp_pcb *pcb, u16_t len)
298 {
299   /* connection is in invalid state for data transmission? */
300   if ((pcb->state != ESTABLISHED) &&
301       (pcb->state != CLOSE_WAIT) &&
302       (pcb->state != SYN_SENT) &&
303       (pcb->state != SYN_RCVD)) {
304     LWIP_DEBUGF(TCP_OUTPUT_DEBUG | LWIP_DBG_STATE | LWIP_DBG_LEVEL_SEVERE, ("tcp_write() called in invalid state\n"));
305     return ERR_CONN;
306   } else if (len == 0) {
307     return ERR_OK;
308   }
309
310   /* fail on too much data */
311   if (len > pcb->snd_buf) {
312     LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 3, ("tcp_write: too much data (len=%"U16_F" > snd_buf=%"U16_F")\n",
313       len, pcb->snd_buf));
314     pcb->flags |= TF_NAGLEMEMERR;
315     return ERR_MEM;
316   }
317
318   LWIP_DEBUGF(TCP_QLEN_DEBUG, ("tcp_write: queuelen: %"U16_F"\n", (u16_t)pcb->snd_queuelen));
319
320   /* If total number of pbufs on the unsent/unacked queues exceeds the
321    * configured maximum, return an error */
322   /* check for configured max queuelen and possible overflow */
323   if ((pcb->snd_queuelen >= TCP_SND_QUEUELEN) || (pcb->snd_queuelen > TCP_SNDQUEUELEN_OVERFLOW)) {
324     LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 3, ("tcp_write: too long queue %"U16_F" (max %"U16_F")\n",
325       pcb->snd_queuelen, TCP_SND_QUEUELEN));
326     TCP_STATS_INC(tcp.memerr);
327     pcb->flags |= TF_NAGLEMEMERR;
328     return ERR_MEM;
329   }
330   if (pcb->snd_queuelen != 0) {
331     LWIP_ASSERT("tcp_write: pbufs on queue => at least one queue non-empty",
332       pcb->unacked != NULL || pcb->unsent != NULL);
333   } else {
334     LWIP_ASSERT("tcp_write: no pbufs on queue => both queues empty",
335       pcb->unacked == NULL && pcb->unsent == NULL);
336   }
337   return ERR_OK;
338 }
339
340 /**
341  * Write data for sending (but does not send it immediately).
342  *
343  * It waits in the expectation of more data being sent soon (as
344  * it can send them more efficiently by combining them together).
345  * To prompt the system to send data now, call tcp_output() after
346  * calling tcp_write().
347  *
348  * @param pcb Protocol control block for the TCP connection to enqueue data for.
349  * @param arg Pointer to the data to be enqueued for sending.
350  * @param len Data length in bytes
351  * @param apiflags combination of following flags :
352  * - TCP_WRITE_FLAG_COPY (0x01) data will be copied into memory belonging to the stack
353  * - TCP_WRITE_FLAG_MORE (0x02) for TCP connection, PSH flag will be set on last segment sent,
354  * @return ERR_OK if enqueued, another err_t on error
355  */
356 err_t
357 tcp_write(struct tcp_pcb *pcb, const void *arg, u16_t len, u8_t apiflags)
358 {
359   struct pbuf *concat_p = NULL;
360   struct tcp_seg *last_unsent = NULL, *seg = NULL, *prev_seg = NULL, *queue = NULL;
361   u16_t pos = 0; /* position in 'arg' data */
362   u16_t queuelen;
363   u8_t optlen = 0;
364   u8_t optflags = 0;
365 #if TCP_OVERSIZE
366   u16_t oversize = 0;
367   u16_t oversize_used = 0;
368 #endif /* TCP_OVERSIZE */
369 #if TCP_CHECKSUM_ON_COPY
370   u16_t concat_chksum = 0;
371   u8_t concat_chksum_swapped = 0;
372   u16_t concat_chksummed = 0;
373 #endif /* TCP_CHECKSUM_ON_COPY */
374   err_t err;
375
376 #if LWIP_NETIF_TX_SINGLE_PBUF
377   /* Always copy to try to create single pbufs for TX */
378   apiflags |= TCP_WRITE_FLAG_COPY;
379 #endif /* LWIP_NETIF_TX_SINGLE_PBUF */
380
381   LWIP_DEBUGF(TCP_OUTPUT_DEBUG, ("tcp_write(pcb=%p, data=%p, len=%"U16_F", apiflags=%"U16_F")\n",
382     (void *)pcb, arg, len, (u16_t)apiflags));
383   LWIP_ERROR("tcp_write: arg == NULL (programmer violates API)", 
384              arg != NULL, return ERR_ARG;);
385
386   err = tcp_write_checks(pcb, len);
387   if (err != ERR_OK) {
388     return err;
389   }
390   queuelen = pcb->snd_queuelen;
391
392 #if LWIP_TCP_TIMESTAMPS
393   if ((pcb->flags & TF_TIMESTAMP)) {
394     optflags = TF_SEG_OPTS_TS;
395     optlen = LWIP_TCP_OPT_LENGTH(TF_SEG_OPTS_TS);
396   }
397 #endif /* LWIP_TCP_TIMESTAMPS */
398
399
400   /*
401    * TCP segmentation is done in three phases with increasing complexity:
402    *
403    * 1. Copy data directly into an oversized pbuf.
404    * 2. Chain a new pbuf to the end of pcb->unsent.
405    * 3. Create new segments.
406    *
407    * We may run out of memory at any point. In that case we must
408    * return ERR_MEM and not change anything in pcb. Therefore, all
409    * changes are recorded in local variables and committed at the end
410    * of the function. Some pcb fields are maintained in local copies:
411    *
412    * queuelen = pcb->snd_queuelen
413    * oversize = pcb->unsent_oversize
414    *
415    * These variables are set consistently by the phases:
416    *
417    * seg points to the last segment tampered with.
418    *
419    * pos records progress as data is segmented.
420    */
421
422   /* Find the tail of the unsent queue. */
423   if (pcb->unsent != NULL) {
424     u16_t space;
425     u16_t unsent_optlen;
426
427     /* @todo: this could be sped up by keeping last_unsent in the pcb */
428     for (last_unsent = pcb->unsent; last_unsent->next != NULL;
429          last_unsent = last_unsent->next);
430
431     /* Usable space at the end of the last unsent segment */
432     unsent_optlen = LWIP_TCP_OPT_LENGTH(last_unsent->flags);
433     space = pcb->mss - (last_unsent->len + unsent_optlen);
434
435     /*
436      * Phase 1: Copy data directly into an oversized pbuf.
437      *
438      * The number of bytes copied is recorded in the oversize_used
439      * variable. The actual copying is done at the bottom of the
440      * function.
441      */
442 #if TCP_OVERSIZE
443 #if TCP_OVERSIZE_DBGCHECK
444     /* check that pcb->unsent_oversize matches last_unsent->unsent_oversize */
445     LWIP_ASSERT("unsent_oversize mismatch (pcb vs. last_unsent)",
446                 pcb->unsent_oversize == last_unsent->oversize_left);
447 #endif /* TCP_OVERSIZE_DBGCHECK */
448     oversize = pcb->unsent_oversize;
449     if (oversize > 0) {
450       LWIP_ASSERT("inconsistent oversize vs. space", oversize_used <= space);
451       seg = last_unsent;
452       oversize_used = oversize < len ? oversize : len;
453       pos += oversize_used;
454       oversize -= oversize_used;
455       space -= oversize_used;
456     }
457     /* now we are either finished or oversize is zero */
458     LWIP_ASSERT("inconsistend oversize vs. len", (oversize == 0) || (pos == len));
459 #endif /* TCP_OVERSIZE */
460
461     /*
462      * Phase 2: Chain a new pbuf to the end of pcb->unsent.
463      *
464      * We don't extend segments containing SYN/FIN flags or options
465      * (len==0). The new pbuf is kept in concat_p and pbuf_cat'ed at
466      * the end.
467      */
468     if ((pos < len) && (space > 0) && (last_unsent->len > 0)) {
469       u16_t seglen = space < len - pos ? space : len - pos;
470       seg = last_unsent;
471
472       /* Create a pbuf with a copy or reference to seglen bytes. We
473        * can use PBUF_RAW here since the data appears in the middle of
474        * a segment. A header will never be prepended. */
475       if (apiflags & TCP_WRITE_FLAG_COPY) {
476         /* Data is copied */
477         if ((concat_p = tcp_pbuf_prealloc(PBUF_RAW, seglen, space, &oversize, pcb, apiflags, 1)) == NULL) {
478           LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 2,
479                       ("tcp_write : could not allocate memory for pbuf copy size %"U16_F"\n",
480                        seglen));
481           goto memerr;
482         }
483 #if TCP_OVERSIZE_DBGCHECK
484         last_unsent->oversize_left = oversize;
485 #endif /* TCP_OVERSIZE_DBGCHECK */
486         TCP_DATA_COPY2(concat_p->payload, (u8_t*)arg + pos, seglen, &concat_chksum, &concat_chksum_swapped);
487 #if TCP_CHECKSUM_ON_COPY
488         concat_chksummed += seglen;
489 #endif /* TCP_CHECKSUM_ON_COPY */
490       } else {
491         /* Data is not copied */
492         if ((concat_p = pbuf_alloc(PBUF_RAW, seglen, PBUF_ROM)) == NULL) {
493           LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 2,
494                       ("tcp_write: could not allocate memory for zero-copy pbuf\n"));
495           goto memerr;
496         }
497 #if TCP_CHECKSUM_ON_COPY
498         /* calculate the checksum of nocopy-data */
499         tcp_seg_add_chksum(~inet_chksum((u8_t*)arg + pos, seglen), seglen,
500           &concat_chksum, &concat_chksum_swapped);
501         concat_chksummed += seglen;
502 #endif /* TCP_CHECKSUM_ON_COPY */
503         /* reference the non-volatile payload data */
504         concat_p->payload = (u8_t*)arg + pos;
505       }
506
507       pos += seglen;
508       queuelen += pbuf_clen(concat_p);
509     }
510   } else {
511 #if TCP_OVERSIZE
512     LWIP_ASSERT("unsent_oversize mismatch (pcb->unsent is NULL)",
513                 pcb->unsent_oversize == 0);
514 #endif /* TCP_OVERSIZE */
515   }
516
517   /*
518    * Phase 3: Create new segments.
519    *
520    * The new segments are chained together in the local 'queue'
521    * variable, ready to be appended to pcb->unsent.
522    */
523   while (pos < len) {
524     struct pbuf *p;
525     u16_t left = len - pos;
526     u16_t max_len = pcb->mss - optlen;
527     u16_t seglen = left > max_len ? max_len : left;
528 #if TCP_CHECKSUM_ON_COPY
529     u16_t chksum = 0;
530     u8_t chksum_swapped = 0;
531 #endif /* TCP_CHECKSUM_ON_COPY */
532
533     if (apiflags & TCP_WRITE_FLAG_COPY) {
534       /* If copy is set, memory should be allocated and data copied
535        * into pbuf */
536       if ((p = tcp_pbuf_prealloc(PBUF_TRANSPORT, seglen + optlen, pcb->mss, &oversize, pcb, apiflags, queue == NULL)) == NULL) {
537         LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 2, ("tcp_write : could not allocate memory for pbuf copy size %"U16_F"\n", seglen));
538         goto memerr;
539       }
540       LWIP_ASSERT("tcp_write: check that first pbuf can hold the complete seglen",
541                   (p->len >= seglen));
542       TCP_DATA_COPY2((char *)p->payload + optlen, (u8_t*)arg + pos, seglen, &chksum, &chksum_swapped);
543     } else {
544       /* Copy is not set: First allocate a pbuf for holding the data.
545        * Since the referenced data is available at least until it is
546        * sent out on the link (as it has to be ACKed by the remote
547        * party) we can safely use PBUF_ROM instead of PBUF_REF here.
548        */
549       struct pbuf *p2;
550 #if TCP_OVERSIZE
551       LWIP_ASSERT("oversize == 0", oversize == 0);
552 #endif /* TCP_OVERSIZE */
553       if ((p2 = pbuf_alloc(PBUF_TRANSPORT, seglen, PBUF_ROM)) == NULL) {
554         LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 2, ("tcp_write: could not allocate memory for zero-copy pbuf\n"));
555         goto memerr;
556       }
557 #if TCP_CHECKSUM_ON_COPY
558       /* calculate the checksum of nocopy-data */
559       chksum = ~inet_chksum((u8_t*)arg + pos, seglen);
560 #endif /* TCP_CHECKSUM_ON_COPY */
561       /* reference the non-volatile payload data */
562       p2->payload = (u8_t*)arg + pos;
563
564       /* Second, allocate a pbuf for the headers. */
565       if ((p = pbuf_alloc(PBUF_TRANSPORT, optlen, PBUF_RAM)) == NULL) {
566         /* If allocation fails, we have to deallocate the data pbuf as
567          * well. */
568         pbuf_free(p2);
569         LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 2, ("tcp_write: could not allocate memory for header pbuf\n"));
570         goto memerr;
571       }
572       /* Concatenate the headers and data pbufs together. */
573       pbuf_cat(p/*header*/, p2/*data*/);
574     }
575
576     queuelen += pbuf_clen(p);
577
578     /* Now that there are more segments queued, we check again if the
579      * length of the queue exceeds the configured maximum or
580      * overflows. */
581     if ((queuelen > TCP_SND_QUEUELEN) || (queuelen > TCP_SNDQUEUELEN_OVERFLOW)) {
582       LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 2, ("tcp_write: queue too long %"U16_F" (%"U16_F")\n", queuelen, TCP_SND_QUEUELEN));
583       pbuf_free(p);
584       goto memerr;
585     }
586
587     if ((seg = tcp_create_segment(pcb, p, 0, pcb->snd_lbb + pos, optflags)) == NULL) {
588       goto memerr;
589     }
590 #if TCP_OVERSIZE_DBGCHECK
591     seg->oversize_left = oversize;
592 #endif /* TCP_OVERSIZE_DBGCHECK */
593 #if TCP_CHECKSUM_ON_COPY
594     seg->chksum = chksum;
595     seg->chksum_swapped = chksum_swapped;
596     seg->flags |= TF_SEG_DATA_CHECKSUMMED;
597 #endif /* TCP_CHECKSUM_ON_COPY */
598
599     /* first segment of to-be-queued data? */
600     if (queue == NULL) {
601       queue = seg;
602     } else {
603       /* Attach the segment to the end of the queued segments */
604       LWIP_ASSERT("prev_seg != NULL", prev_seg != NULL);
605       prev_seg->next = seg;
606     }
607     /* remember last segment of to-be-queued data for next iteration */
608     prev_seg = seg;
609
610     LWIP_DEBUGF(TCP_OUTPUT_DEBUG | LWIP_DBG_TRACE, ("tcp_write: queueing %"U32_F":%"U32_F"\n",
611       ntohl(seg->tcphdr->seqno),
612       ntohl(seg->tcphdr->seqno) + TCP_TCPLEN(seg)));
613
614     pos += seglen;
615   }
616
617   /*
618    * All three segmentation phases were successful. We can commit the
619    * transaction.
620    */
621
622   /*
623    * Phase 1: If data has been added to the preallocated tail of
624    * last_unsent, we update the length fields of the pbuf chain.
625    */
626 #if TCP_OVERSIZE
627   if (oversize_used > 0) {
628     struct pbuf *p;
629     /* Bump tot_len of whole chain, len of tail */
630     for (p = last_unsent->p; p; p = p->next) {
631       p->tot_len += oversize_used;
632       if (p->next == NULL) {
633         TCP_DATA_COPY((char *)p->payload + p->len, arg, oversize_used, last_unsent);
634         p->len += oversize_used;
635       }
636     }
637     last_unsent->len += oversize_used;
638 #if TCP_OVERSIZE_DBGCHECK
639     last_unsent->oversize_left -= oversize_used;
640 #endif /* TCP_OVERSIZE_DBGCHECK */
641   }
642   pcb->unsent_oversize = oversize;
643 #endif /* TCP_OVERSIZE */
644
645   /*
646    * Phase 2: concat_p can be concatenated onto last_unsent->p
647    */
648   if (concat_p != NULL) {
649     LWIP_ASSERT("tcp_write: cannot concatenate when pcb->unsent is empty",
650       (last_unsent != NULL));
651     pbuf_cat(last_unsent->p, concat_p);
652     last_unsent->len += concat_p->tot_len;
653 #if TCP_CHECKSUM_ON_COPY
654     if (concat_chksummed) {
655       tcp_seg_add_chksum(concat_chksum, concat_chksummed, &last_unsent->chksum,
656         &last_unsent->chksum_swapped);
657       last_unsent->flags |= TF_SEG_DATA_CHECKSUMMED;
658     }
659 #endif /* TCP_CHECKSUM_ON_COPY */
660   }
661
662   /*
663    * Phase 3: Append queue to pcb->unsent. Queue may be NULL, but that
664    * is harmless
665    */
666   if (last_unsent == NULL) {
667     pcb->unsent = queue;
668   } else {
669     last_unsent->next = queue;
670   }
671
672   /*
673    * Finally update the pcb state.
674    */
675   pcb->snd_lbb += len;
676   pcb->snd_buf -= len;
677   pcb->snd_queuelen = queuelen;
678
679   LWIP_DEBUGF(TCP_QLEN_DEBUG, ("tcp_write: %"S16_F" (after enqueued)\n",
680     pcb->snd_queuelen));
681   if (pcb->snd_queuelen != 0) {
682     LWIP_ASSERT("tcp_write: valid queue length",
683                 pcb->unacked != NULL || pcb->unsent != NULL);
684   }
685
686   /* Set the PSH flag in the last segment that we enqueued. */
687   if (seg != NULL && seg->tcphdr != NULL && ((apiflags & TCP_WRITE_FLAG_MORE)==0)) {
688     TCPH_SET_FLAG(seg->tcphdr, TCP_PSH);
689   }
690
691   return ERR_OK;
692 memerr:
693   pcb->flags |= TF_NAGLEMEMERR;
694   TCP_STATS_INC(tcp.memerr);
695
696   if (concat_p != NULL) {
697     pbuf_free(concat_p);
698   }
699   if (queue != NULL) {
700     tcp_segs_free(queue);
701   }
702   if (pcb->snd_queuelen != 0) {
703     LWIP_ASSERT("tcp_write: valid queue length", pcb->unacked != NULL ||
704       pcb->unsent != NULL);
705   }
706   LWIP_DEBUGF(TCP_QLEN_DEBUG | LWIP_DBG_STATE, ("tcp_write: %"S16_F" (with mem err)\n", pcb->snd_queuelen));
707   return ERR_MEM;
708 }
709
710 /**
711  * Enqueue TCP options for transmission.
712  *
713  * Called by tcp_connect(), tcp_listen_input(), and tcp_send_ctrl().
714  *
715  * @param pcb Protocol control block for the TCP connection.
716  * @param flags TCP header flags to set in the outgoing segment.
717  * @param optdata pointer to TCP options, or NULL.
718  * @param optlen length of TCP options in bytes.
719  */
720 err_t
721 tcp_enqueue_flags(struct tcp_pcb *pcb, u8_t flags)
722 {
723   struct pbuf *p;
724   struct tcp_seg *seg;
725   u8_t optflags = 0;
726   u8_t optlen = 0;
727
728   LWIP_DEBUGF(TCP_QLEN_DEBUG, ("tcp_enqueue_flags: queuelen: %"U16_F"\n", (u16_t)pcb->snd_queuelen));
729
730   LWIP_ASSERT("tcp_enqueue_flags: need either TCP_SYN or TCP_FIN in flags (programmer violates API)",
731               (flags & (TCP_SYN | TCP_FIN)) != 0);
732
733   /* check for configured max queuelen and possible overflow */
734   if ((pcb->snd_queuelen >= TCP_SND_QUEUELEN) || (pcb->snd_queuelen > TCP_SNDQUEUELEN_OVERFLOW)) {
735     LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 3, ("tcp_enqueue_flags: too long queue %"U16_F" (max %"U16_F")\n",
736                                        pcb->snd_queuelen, TCP_SND_QUEUELEN));
737     TCP_STATS_INC(tcp.memerr);
738     pcb->flags |= TF_NAGLEMEMERR;
739     return ERR_MEM;
740   }
741
742   if (flags & TCP_SYN) {
743     optflags = TF_SEG_OPTS_MSS;
744   }
745 #if LWIP_TCP_TIMESTAMPS
746   if ((pcb->flags & TF_TIMESTAMP)) {
747     optflags |= TF_SEG_OPTS_TS;
748   }
749 #endif /* LWIP_TCP_TIMESTAMPS */
750   optlen = LWIP_TCP_OPT_LENGTH(optflags);
751
752   /* tcp_enqueue_flags is always called with either SYN or FIN in flags.
753    * We need one available snd_buf byte to do that.
754    * This means we can't send FIN while snd_buf==0. A better fix would be to
755    * not include SYN and FIN sequence numbers in the snd_buf count. */
756   if (pcb->snd_buf == 0) {
757     LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 3, ("tcp_enqueue_flags: no send buffer available\n"));
758     TCP_STATS_INC(tcp.memerr);
759     return ERR_MEM;
760   }
761
762   /* Allocate pbuf with room for TCP header + options */
763   if ((p = pbuf_alloc(PBUF_TRANSPORT, optlen, PBUF_RAM)) == NULL) {
764     pcb->flags |= TF_NAGLEMEMERR;
765     TCP_STATS_INC(tcp.memerr);
766     return ERR_MEM;
767   }
768   LWIP_ASSERT("tcp_enqueue_flags: check that first pbuf can hold optlen",
769               (p->len >= optlen));
770
771   /* Allocate memory for tcp_seg, and fill in fields. */
772   if ((seg = tcp_create_segment(pcb, p, flags, pcb->snd_lbb, optflags)) == NULL) {
773     pcb->flags |= TF_NAGLEMEMERR;
774     TCP_STATS_INC(tcp.memerr);
775     return ERR_MEM;
776   }
777   LWIP_ASSERT("seg->tcphdr not aligned", ((mem_ptr_t)seg->tcphdr % MEM_ALIGNMENT) == 0);
778   LWIP_ASSERT("tcp_enqueue_flags: invalid segment length", seg->len == 0);
779
780   LWIP_DEBUGF(TCP_OUTPUT_DEBUG | LWIP_DBG_TRACE,
781               ("tcp_enqueue_flags: queueing %"U32_F":%"U32_F" (0x%"X16_F")\n",
782                ntohl(seg->tcphdr->seqno),
783                ntohl(seg->tcphdr->seqno) + TCP_TCPLEN(seg),
784                (u16_t)flags));
785
786   /* Now append seg to pcb->unsent queue */
787   if (pcb->unsent == NULL) {
788     pcb->unsent = seg;
789   } else {
790     struct tcp_seg *useg;
791     for (useg = pcb->unsent; useg->next != NULL; useg = useg->next);
792     useg->next = seg;
793   }
794 #if TCP_OVERSIZE
795   /* The new unsent tail has no space */
796   pcb->unsent_oversize = 0;
797 #endif /* TCP_OVERSIZE */
798
799   /* SYN and FIN bump the sequence number */
800   if ((flags & TCP_SYN) || (flags & TCP_FIN)) {
801     pcb->snd_lbb++;
802     /* optlen does not influence snd_buf */
803     pcb->snd_buf--;
804   }
805   if (flags & TCP_FIN) {
806     pcb->flags |= TF_FIN;
807   }
808
809   /* update number of segments on the queues */
810   pcb->snd_queuelen += pbuf_clen(seg->p);
811   LWIP_DEBUGF(TCP_QLEN_DEBUG, ("tcp_enqueue_flags: %"S16_F" (after enqueued)\n", pcb->snd_queuelen));
812   if (pcb->snd_queuelen != 0) {
813     LWIP_ASSERT("tcp_enqueue_flags: invalid queue length",
814       pcb->unacked != NULL || pcb->unsent != NULL);
815   }
816
817   return ERR_OK;
818 }
819
820 #if LWIP_TCP_TIMESTAMPS
821 /* Build a timestamp option (12 bytes long) at the specified options pointer)
822  *
823  * @param pcb tcp_pcb
824  * @param opts option pointer where to store the timestamp option
825  */
826 static void
827 tcp_build_timestamp_option(struct tcp_pcb *pcb, u32_t *opts)
828 {
829   /* Pad with two NOP options to make everything nicely aligned */
830   opts[0] = PP_HTONL(0x0101080A);
831   opts[1] = htonl(sys_now());
832   opts[2] = htonl(pcb->ts_recent);
833 }
834 #endif
835
836 /** Send an ACK without data.
837  *
838  * @param pcb Protocol control block for the TCP connection to send the ACK
839  */
840 err_t
841 tcp_send_empty_ack(struct tcp_pcb *pcb)
842 {
843   struct pbuf *p;
844   struct tcp_hdr *tcphdr;
845   u8_t optlen = 0;
846
847 #if LWIP_TCP_TIMESTAMPS
848   if (pcb->flags & TF_TIMESTAMP) {
849     optlen = LWIP_TCP_OPT_LENGTH(TF_SEG_OPTS_TS);
850   }
851 #endif
852
853   p = tcp_output_alloc_header(pcb, optlen, 0, htonl(pcb->snd_nxt));
854   if (p == NULL) {
855     LWIP_DEBUGF(TCP_OUTPUT_DEBUG, ("tcp_output: (ACK) could not allocate pbuf\n"));
856     return ERR_BUF;
857   }
858   tcphdr = (struct tcp_hdr *)p->payload;
859   LWIP_DEBUGF(TCP_OUTPUT_DEBUG, 
860               ("tcp_output: sending ACK for %"U32_F"\n", pcb->rcv_nxt));
861   /* remove ACK flags from the PCB, as we send an empty ACK now */
862   pcb->flags &= ~(TF_ACK_DELAY | TF_ACK_NOW);
863
864   /* NB. MSS option is only sent on SYNs, so ignore it here */
865 #if LWIP_TCP_TIMESTAMPS
866   pcb->ts_lastacksent = pcb->rcv_nxt;
867
868   if (pcb->flags & TF_TIMESTAMP) {
869     tcp_build_timestamp_option(pcb, (u32_t *)(tcphdr + 1));
870   }
871 #endif 
872
873 #if CHECKSUM_GEN_TCP
874   tcphdr->chksum = ipX_chksum_pseudo(PCB_ISIPV6(pcb), p, IP_PROTO_TCP, p->tot_len,
875     &pcb->local_ip, &pcb->remote_ip);
876 #endif
877 #if LWIP_NETIF_HWADDRHINT
878   ipX_output_hinted(PCB_ISIPV6(pcb), p, &pcb->local_ip, &pcb->remote_ip, pcb->ttl, pcb->tos,
879       IP_PROTO_TCP, &pcb->addr_hint);
880 #else /* LWIP_NETIF_HWADDRHINT*/
881   ipX_output(PCB_ISIPV6(pcb), p, &pcb->local_ip, &pcb->remote_ip, pcb->ttl, pcb->tos,
882       IP_PROTO_TCP);
883 #endif /* LWIP_NETIF_HWADDRHINT*/
884   pbuf_free(p);
885
886   return ERR_OK;
887 }
888
889 /**
890  * Find out what we can send and send it
891  *
892  * @param pcb Protocol control block for the TCP connection to send data
893  * @return ERR_OK if data has been sent or nothing to send
894  *         another err_t on error
895  */
896 err_t
897 tcp_output(struct tcp_pcb *pcb)
898 {
899   struct tcp_seg *seg, *useg;
900   u32_t wnd, snd_nxt;
901 #if TCP_CWND_DEBUG
902   s16_t i = 0;
903 #endif /* TCP_CWND_DEBUG */
904
905   /* pcb->state LISTEN not allowed here */
906   LWIP_ASSERT("don't call tcp_output for listen-pcbs",
907     pcb->state != LISTEN);
908
909   /* First, check if we are invoked by the TCP input processing
910      code. If so, we do not output anything. Instead, we rely on the
911      input processing code to call us when input processing is done
912      with. */
913   if (tcp_input_pcb == pcb) {
914     return ERR_OK;
915   }
916
917   wnd = LWIP_MIN(pcb->snd_wnd, pcb->cwnd);
918
919   seg = pcb->unsent;
920
921   /* If the TF_ACK_NOW flag is set and no data will be sent (either
922    * because the ->unsent queue is empty or because the window does
923    * not allow it), construct an empty ACK segment and send it.
924    *
925    * If data is to be sent, we will just piggyback the ACK (see below).
926    */
927   if (pcb->flags & TF_ACK_NOW &&
928      (seg == NULL ||
929       ntohl(seg->tcphdr->seqno) - pcb->lastack + seg->len > wnd)) {
930      return tcp_send_empty_ack(pcb);
931   }
932
933   /* useg should point to last segment on unacked queue */
934   useg = pcb->unacked;
935   if (useg != NULL) {
936     for (; useg->next != NULL; useg = useg->next);
937   }
938
939 #if TCP_OUTPUT_DEBUG
940   if (seg == NULL) {
941     LWIP_DEBUGF(TCP_OUTPUT_DEBUG, ("tcp_output: nothing to send (%p)\n",
942                                    (void*)pcb->unsent));
943   }
944 #endif /* TCP_OUTPUT_DEBUG */
945 #if TCP_CWND_DEBUG
946   if (seg == NULL) {
947     LWIP_DEBUGF(TCP_CWND_DEBUG, ("tcp_output: snd_wnd %"U16_F
948                                  ", cwnd %"U16_F", wnd %"U32_F
949                                  ", seg == NULL, ack %"U32_F"\n",
950                                  pcb->snd_wnd, pcb->cwnd, wnd, pcb->lastack));
951   } else {
952     LWIP_DEBUGF(TCP_CWND_DEBUG, 
953                 ("tcp_output: snd_wnd %"U16_F", cwnd %"U16_F", wnd %"U32_F
954                  ", effwnd %"U32_F", seq %"U32_F", ack %"U32_F"\n",
955                  pcb->snd_wnd, pcb->cwnd, wnd,
956                  ntohl(seg->tcphdr->seqno) - pcb->lastack + seg->len,
957                  ntohl(seg->tcphdr->seqno), pcb->lastack));
958   }
959 #endif /* TCP_CWND_DEBUG */
960   /* data available and window allows it to be sent? */
961   while (seg != NULL &&
962          ntohl(seg->tcphdr->seqno) - pcb->lastack + seg->len <= wnd) {
963     LWIP_ASSERT("RST not expected here!", 
964                 (TCPH_FLAGS(seg->tcphdr) & TCP_RST) == 0);
965     /* Stop sending if the nagle algorithm would prevent it
966      * Don't stop:
967      * - if tcp_write had a memory error before (prevent delayed ACK timeout) or
968      * - if FIN was already enqueued for this PCB (SYN is always alone in a segment -
969      *   either seg->next != NULL or pcb->unacked == NULL;
970      *   RST is no sent using tcp_write/tcp_output.
971      */
972     if((tcp_do_output_nagle(pcb) == 0) &&
973       ((pcb->flags & (TF_NAGLEMEMERR | TF_FIN)) == 0)){
974       break;
975     }
976 #if TCP_CWND_DEBUG
977     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",
978                             pcb->snd_wnd, pcb->cwnd, wnd,
979                             ntohl(seg->tcphdr->seqno) + seg->len -
980                             pcb->lastack,
981                             ntohl(seg->tcphdr->seqno), pcb->lastack, i));
982     ++i;
983 #endif /* TCP_CWND_DEBUG */
984
985     pcb->unsent = seg->next;
986
987     if (pcb->state != SYN_SENT) {
988       TCPH_SET_FLAG(seg->tcphdr, TCP_ACK);
989       pcb->flags &= ~(TF_ACK_DELAY | TF_ACK_NOW);
990     }
991
992     tcp_output_segment(seg, pcb);
993     snd_nxt = ntohl(seg->tcphdr->seqno) + TCP_TCPLEN(seg);
994     if (TCP_SEQ_LT(pcb->snd_nxt, snd_nxt)) {
995       pcb->snd_nxt = snd_nxt;
996     }
997     /* put segment on unacknowledged list if length > 0 */
998     if (TCP_TCPLEN(seg) > 0) {
999       seg->next = NULL;
1000       /* unacked list is empty? */
1001       if (pcb->unacked == NULL) {
1002         pcb->unacked = seg;
1003         useg = seg;
1004       /* unacked list is not empty? */
1005       } else {
1006         /* In the case of fast retransmit, the packet should not go to the tail
1007          * of the unacked queue, but rather somewhere before it. We need to check for
1008          * this case. -STJ Jul 27, 2004 */
1009         if (TCP_SEQ_LT(ntohl(seg->tcphdr->seqno), ntohl(useg->tcphdr->seqno))) {
1010           /* add segment to before tail of unacked list, keeping the list sorted */
1011           struct tcp_seg **cur_seg = &(pcb->unacked);
1012           while (*cur_seg &&
1013             TCP_SEQ_LT(ntohl((*cur_seg)->tcphdr->seqno), ntohl(seg->tcphdr->seqno))) {
1014               cur_seg = &((*cur_seg)->next );
1015           }
1016           seg->next = (*cur_seg);
1017           (*cur_seg) = seg;
1018         } else {
1019           /* add segment to tail of unacked list */
1020           useg->next = seg;
1021           useg = useg->next;
1022         }
1023       }
1024     /* do not queue empty segments on the unacked list */
1025     } else {
1026       tcp_seg_free(seg);
1027     }
1028     seg = pcb->unsent;
1029   }
1030 #if TCP_OVERSIZE
1031   if (pcb->unsent == NULL) {
1032     /* last unsent has been removed, reset unsent_oversize */
1033     pcb->unsent_oversize = 0;
1034   }
1035 #endif /* TCP_OVERSIZE */
1036
1037   if (seg != NULL && pcb->persist_backoff == 0 && 
1038       ntohl(seg->tcphdr->seqno) - pcb->lastack + seg->len > pcb->snd_wnd) {
1039     /* prepare for persist timer */
1040     pcb->persist_cnt = 0;
1041     pcb->persist_backoff = 1;
1042   }
1043
1044   pcb->flags &= ~TF_NAGLEMEMERR;
1045   return ERR_OK;
1046 }
1047
1048 /**
1049  * Called by tcp_output() to actually send a TCP segment over IP.
1050  *
1051  * @param seg the tcp_seg to send
1052  * @param pcb the tcp_pcb for the TCP connection used to send the segment
1053  */
1054 static void
1055 tcp_output_segment(struct tcp_seg *seg, struct tcp_pcb *pcb)
1056 {
1057   u16_t len;
1058   u32_t *opts;
1059
1060   /** @bug Exclude retransmitted segments from this count. */
1061   snmp_inc_tcpoutsegs();
1062
1063   /* The TCP header has already been constructed, but the ackno and
1064    wnd fields remain. */
1065   seg->tcphdr->ackno = htonl(pcb->rcv_nxt);
1066
1067   /* advertise our receive window size in this TCP segment */
1068   seg->tcphdr->wnd = htons(pcb->rcv_ann_wnd);
1069
1070   pcb->rcv_ann_right_edge = pcb->rcv_nxt + pcb->rcv_ann_wnd;
1071
1072   /* Add any requested options.  NB MSS option is only set on SYN
1073      packets, so ignore it here */
1074   LWIP_ASSERT("seg->tcphdr not aligned", ((mem_ptr_t)seg->tcphdr % MEM_ALIGNMENT) == 0);
1075   opts = (u32_t *)(void *)(seg->tcphdr + 1);
1076   if (seg->flags & TF_SEG_OPTS_MSS) {
1077     TCP_BUILD_MSS_OPTION(*opts);
1078     opts += 1;
1079   }
1080 #if LWIP_TCP_TIMESTAMPS
1081   pcb->ts_lastacksent = pcb->rcv_nxt;
1082
1083   if (seg->flags & TF_SEG_OPTS_TS) {
1084     tcp_build_timestamp_option(pcb, opts);
1085     opts += 3;
1086   }
1087 #endif
1088
1089   /* Set retransmission timer running if it is not currently enabled 
1090      This must be set before checking the route. */
1091   if (pcb->rtime == -1) {
1092     pcb->rtime = 0;
1093   }
1094
1095   /* If we don't have a local IP address, we get one by
1096      calling ip_route(). */
1097   if (ipX_addr_isany(PCB_ISIPV6(pcb), &pcb->local_ip)) {
1098     struct netif *netif;
1099     ipX_addr_t *local_ip;
1100     ipX_route_get_local_ipX(PCB_ISIPV6(pcb), &pcb->local_ip, &pcb->remote_ip, netif, local_ip);
1101     if ((netif == NULL) || (local_ip == NULL)) {
1102       return;
1103     }
1104     ipX_addr_copy(PCB_ISIPV6(pcb), pcb->local_ip, *local_ip);
1105   }
1106
1107   if (pcb->rttest == 0) {
1108     pcb->rttest = tcp_ticks;
1109     pcb->rtseq = ntohl(seg->tcphdr->seqno);
1110
1111     LWIP_DEBUGF(TCP_RTO_DEBUG, ("tcp_output_segment: rtseq %"U32_F"\n", pcb->rtseq));
1112   }
1113   LWIP_DEBUGF(TCP_OUTPUT_DEBUG, ("tcp_output_segment: %"U32_F":%"U32_F"\n",
1114           htonl(seg->tcphdr->seqno), htonl(seg->tcphdr->seqno) +
1115           seg->len));
1116
1117   len = (u16_t)((u8_t *)seg->tcphdr - (u8_t *)seg->p->payload);
1118
1119   seg->p->len -= len;
1120   seg->p->tot_len -= len;
1121
1122   seg->p->payload = seg->tcphdr;
1123
1124   seg->tcphdr->chksum = 0;
1125 #if TCP_CHECKSUM_ON_COPY
1126   {
1127     u32_t acc;
1128 #if TCP_CHECKSUM_ON_COPY_SANITY_CHECK
1129     u16_t chksum_slow = ipX_chksum_pseudo(PCB_ISIPV6(pcb), seg->p, IP_PROTO_TCP,
1130       seg->p->tot_len, &pcb->local_ip, &pcb->remote_ip);
1131 #endif /* TCP_CHECKSUM_ON_COPY_SANITY_CHECK */
1132     if ((seg->flags & TF_SEG_DATA_CHECKSUMMED) == 0) {
1133       LWIP_ASSERT("data included but not checksummed",
1134         seg->p->tot_len == (TCPH_HDRLEN(seg->tcphdr) * 4));
1135     }
1136
1137     /* rebuild TCP header checksum (TCP header changes for retransmissions!) */
1138     acc = ipX_chksum_pseudo_partial(PCB_ISIPV6(pcb), seg->p, IP_PROTO_TCP,
1139       seg->p->tot_len, TCPH_HDRLEN(seg->tcphdr) * 4, &pcb->local_ip, &pcb->remote_ip);
1140     /* add payload checksum */
1141     if (seg->chksum_swapped) {
1142       seg->chksum = SWAP_BYTES_IN_WORD(seg->chksum);
1143       seg->chksum_swapped = 0;
1144     }
1145     acc += (u16_t)~(seg->chksum);
1146     seg->tcphdr->chksum = FOLD_U32T(acc);
1147 #if TCP_CHECKSUM_ON_COPY_SANITY_CHECK
1148     if (chksum_slow != seg->tcphdr->chksum) {
1149       LWIP_DEBUGF(TCP_DEBUG | LWIP_DBG_LEVEL_WARNING,
1150                   ("tcp_output_segment: calculated checksum is %"X16_F" instead of %"X16_F"\n",
1151                   seg->tcphdr->chksum, chksum_slow));
1152       seg->tcphdr->chksum = chksum_slow;
1153     }
1154 #endif /* TCP_CHECKSUM_ON_COPY_SANITY_CHECK */
1155   }
1156 #else /* TCP_CHECKSUM_ON_COPY */
1157 #if CHECKSUM_GEN_TCP
1158   seg->tcphdr->chksum = ipX_chksum_pseudo(PCB_ISIPV6(pcb), seg->p, IP_PROTO_TCP,
1159     seg->p->tot_len, &pcb->local_ip, &pcb->remote_ip);
1160 #endif /* CHECKSUM_GEN_TCP */
1161 #endif /* TCP_CHECKSUM_ON_COPY */
1162   TCP_STATS_INC(tcp.xmit);
1163
1164 #if LWIP_NETIF_HWADDRHINT
1165   ipX_output_hinted(PCB_ISIPV6(pcb), seg->p, &pcb->local_ip, &pcb->remote_ip,
1166     pcb->ttl, pcb->tos, IP_PROTO_TCP, &pcb->addr_hint);
1167 #else /* LWIP_NETIF_HWADDRHINT*/
1168   ipX_output(PCB_ISIPV6(pcb), seg->p, &pcb->local_ip, &pcb->remote_ip, pcb->ttl,
1169     pcb->tos, IP_PROTO_TCP);
1170 #endif /* LWIP_NETIF_HWADDRHINT*/
1171 }
1172
1173 /**
1174  * Send a TCP RESET packet (empty segment with RST flag set) either to
1175  * abort a connection or to show that there is no matching local connection
1176  * for a received segment.
1177  *
1178  * Called by tcp_abort() (to abort a local connection), tcp_input() (if no
1179  * matching local pcb was found), tcp_listen_input() (if incoming segment
1180  * has ACK flag set) and tcp_process() (received segment in the wrong state)
1181  *
1182  * Since a RST segment is in most cases not sent for an active connection,
1183  * tcp_rst() has a number of arguments that are taken from a tcp_pcb for
1184  * most other segment output functions.
1185  *
1186  * @param seqno the sequence number to use for the outgoing segment
1187  * @param ackno the acknowledge number to use for the outgoing segment
1188  * @param local_ip the local IP address to send the segment from
1189  * @param remote_ip the remote IP address to send the segment to
1190  * @param local_port the local TCP port to send the segment from
1191  * @param remote_port the remote TCP port to send the segment to
1192  */
1193 void
1194 tcp_rst_impl(u32_t seqno, u32_t ackno,
1195   ipX_addr_t *local_ip, ipX_addr_t *remote_ip,
1196   u16_t local_port, u16_t remote_port
1197 #if LWIP_IPV6
1198   , u8_t isipv6
1199 #endif /* LWIP_IPV6 */
1200   )
1201 {
1202   struct pbuf *p;
1203   struct tcp_hdr *tcphdr;
1204   p = pbuf_alloc(PBUF_IP, TCP_HLEN, PBUF_RAM);
1205   if (p == NULL) {
1206       LWIP_DEBUGF(TCP_DEBUG, ("tcp_rst: could not allocate memory for pbuf\n"));
1207       return;
1208   }
1209   LWIP_ASSERT("check that first pbuf can hold struct tcp_hdr",
1210               (p->len >= sizeof(struct tcp_hdr)));
1211
1212   tcphdr = (struct tcp_hdr *)p->payload;
1213   tcphdr->src = htons(local_port);
1214   tcphdr->dest = htons(remote_port);
1215   tcphdr->seqno = htonl(seqno);
1216   tcphdr->ackno = htonl(ackno);
1217   TCPH_HDRLEN_FLAGS_SET(tcphdr, TCP_HLEN/4, TCP_RST | TCP_ACK);
1218   tcphdr->wnd = PP_HTONS(TCP_WND);
1219   tcphdr->chksum = 0;
1220   tcphdr->urgp = 0;
1221
1222   TCP_STATS_INC(tcp.xmit);
1223   snmp_inc_tcpoutrsts();
1224
1225 #if CHECKSUM_GEN_TCP
1226   tcphdr->chksum = ipX_chksum_pseudo(isipv6, p, IP_PROTO_TCP, p->tot_len,
1227                                      local_ip, remote_ip);
1228 #endif
1229   /* Send output with hardcoded TTL/HL since we have no access to the pcb */
1230   ipX_output(isipv6, p, local_ip, remote_ip, TCP_TTL, 0, IP_PROTO_TCP);
1231   pbuf_free(p);
1232   LWIP_DEBUGF(TCP_RST_DEBUG, ("tcp_rst: seqno %"U32_F" ackno %"U32_F".\n", seqno, ackno));
1233 }
1234
1235 /**
1236  * Requeue all unacked segments for retransmission
1237  *
1238  * Called by tcp_slowtmr() for slow retransmission.
1239  *
1240  * @param pcb the tcp_pcb for which to re-enqueue all unacked segments
1241  */
1242 void
1243 tcp_rexmit_rto(struct tcp_pcb *pcb)
1244 {
1245   struct tcp_seg *seg;
1246
1247   if (pcb->unacked == NULL) {
1248     return;
1249   }
1250
1251   /* Move all unacked segments to the head of the unsent queue */
1252   for (seg = pcb->unacked; seg->next != NULL; seg = seg->next);
1253   /* concatenate unsent queue after unacked queue */
1254   seg->next = pcb->unsent;
1255   /* unsent queue is the concatenated queue (of unacked, unsent) */
1256   pcb->unsent = pcb->unacked;
1257   /* unacked queue is now empty */
1258   pcb->unacked = NULL;
1259   /* last unsent hasn't changed, no need to reset unsent_oversize */
1260
1261   /* increment number of retransmissions */
1262   ++pcb->nrtx;
1263
1264   /* Don't take any RTT measurements after retransmitting. */
1265   pcb->rttest = 0;
1266
1267   /* Do the actual retransmission */
1268   tcp_output(pcb);
1269 }
1270
1271 /**
1272  * Requeue the first unacked segment for retransmission
1273  *
1274  * Called by tcp_receive() for fast retramsmit.
1275  *
1276  * @param pcb the tcp_pcb for which to retransmit the first unacked segment
1277  */
1278 void
1279 tcp_rexmit(struct tcp_pcb *pcb)
1280 {
1281   struct tcp_seg *seg;
1282   struct tcp_seg **cur_seg;
1283
1284   if (pcb->unacked == NULL) {
1285     return;
1286   }
1287
1288   /* Move the first unacked segment to the unsent queue */
1289   /* Keep the unsent queue sorted. */
1290   seg = pcb->unacked;
1291   pcb->unacked = seg->next;
1292
1293   cur_seg = &(pcb->unsent);
1294   while (*cur_seg &&
1295     TCP_SEQ_LT(ntohl((*cur_seg)->tcphdr->seqno), ntohl(seg->tcphdr->seqno))) {
1296       cur_seg = &((*cur_seg)->next );
1297   }
1298   seg->next = *cur_seg;
1299   *cur_seg = seg;
1300 #if TCP_OVERSIZE
1301   if (seg->next == NULL) {
1302     /* the retransmitted segment is last in unsent, so reset unsent_oversize */
1303     pcb->unsent_oversize = 0;
1304   }
1305 #endif /* TCP_OVERSIZE */
1306
1307   ++pcb->nrtx;
1308
1309   /* Don't take any rtt measurements after retransmitting. */
1310   pcb->rttest = 0;
1311
1312   /* Do the actual retransmission. */
1313   snmp_inc_tcpretranssegs();
1314   /* No need to call tcp_output: we are always called from tcp_input()
1315      and thus tcp_output directly returns. */
1316 }
1317
1318
1319 /**
1320  * Handle retransmission after three dupacks received
1321  *
1322  * @param pcb the tcp_pcb for which to retransmit the first unacked segment
1323  */
1324 void 
1325 tcp_rexmit_fast(struct tcp_pcb *pcb)
1326 {
1327   if (pcb->unacked != NULL && !(pcb->flags & TF_INFR)) {
1328     /* This is fast retransmit. Retransmit the first unacked segment. */
1329     LWIP_DEBUGF(TCP_FR_DEBUG, 
1330                 ("tcp_receive: dupacks %"U16_F" (%"U32_F
1331                  "), fast retransmit %"U32_F"\n",
1332                  (u16_t)pcb->dupacks, pcb->lastack,
1333                  ntohl(pcb->unacked->tcphdr->seqno)));
1334     tcp_rexmit(pcb);
1335
1336     /* Set ssthresh to half of the minimum of the current
1337      * cwnd and the advertised window */
1338     if (pcb->cwnd > pcb->snd_wnd) {
1339       pcb->ssthresh = pcb->snd_wnd / 2;
1340     } else {
1341       pcb->ssthresh = pcb->cwnd / 2;
1342     }
1343     
1344     /* The minimum value for ssthresh should be 2 MSS */
1345     if (pcb->ssthresh < 2*pcb->mss) {
1346       LWIP_DEBUGF(TCP_FR_DEBUG, 
1347                   ("tcp_receive: The minimum value for ssthresh %"U16_F
1348                    " should be min 2 mss %"U16_F"...\n",
1349                    pcb->ssthresh, 2*pcb->mss));
1350       pcb->ssthresh = 2*pcb->mss;
1351     }
1352     
1353     pcb->cwnd = pcb->ssthresh + 3 * pcb->mss;
1354     pcb->flags |= TF_INFR;
1355   } 
1356 }
1357
1358
1359 /**
1360  * Send keepalive packets to keep a connection active although
1361  * no data is sent over it.
1362  *
1363  * Called by tcp_slowtmr()
1364  *
1365  * @param pcb the tcp_pcb for which to send a keepalive packet
1366  */
1367 void
1368 tcp_keepalive(struct tcp_pcb *pcb)
1369 {
1370   struct pbuf *p;
1371   struct tcp_hdr *tcphdr;
1372
1373   LWIP_DEBUGF(TCP_DEBUG, ("tcp_keepalive: sending KEEPALIVE probe to "));
1374   ipX_addr_debug_print(PCB_ISIPV6(pcb), TCP_DEBUG, &pcb->remote_ip);
1375   LWIP_DEBUGF(TCP_DEBUG, ("\n"));
1376
1377   LWIP_DEBUGF(TCP_DEBUG, ("tcp_keepalive: tcp_ticks %"U32_F"   pcb->tmr %"U32_F" pcb->keep_cnt_sent %"U16_F"\n", 
1378                           tcp_ticks, pcb->tmr, pcb->keep_cnt_sent));
1379    
1380   p = tcp_output_alloc_header(pcb, 0, 0, htonl(pcb->snd_nxt - 1));
1381   if(p == NULL) {
1382     LWIP_DEBUGF(TCP_DEBUG, 
1383                 ("tcp_keepalive: could not allocate memory for pbuf\n"));
1384     return;
1385   }
1386   tcphdr = (struct tcp_hdr *)p->payload;
1387
1388   tcphdr->chksum = ipX_chksum_pseudo(PCB_ISIPV6(pcb), p, IP_PROTO_TCP, p->tot_len,
1389       &pcb->local_ip, &pcb->remote_ip);
1390   TCP_STATS_INC(tcp.xmit);
1391
1392   /* Send output to IP */
1393 #if LWIP_NETIF_HWADDRHINT
1394   ipX_output_hinted(PCB_ISIPV6(pcb), p, &pcb->local_ip, &pcb->remote_ip,
1395     pcb->ttl, 0, IP_PROTO_TCP, &pcb->addr_hint);
1396 #else /* LWIP_NETIF_HWADDRHINT*/
1397   ipX_output(PCB_ISIPV6(pcb), p, &pcb->local_ip, &pcb->remote_ip, pcb->ttl,
1398     0, IP_PROTO_TCP);
1399 #endif /* LWIP_NETIF_HWADDRHINT*/
1400
1401   pbuf_free(p);
1402
1403   LWIP_DEBUGF(TCP_DEBUG, ("tcp_keepalive: seqno %"U32_F" ackno %"U32_F".\n",
1404                           pcb->snd_nxt - 1, pcb->rcv_nxt));
1405 }
1406
1407
1408 /**
1409  * Send persist timer zero-window probes to keep a connection active
1410  * when a window update is lost.
1411  *
1412  * Called by tcp_slowtmr()
1413  *
1414  * @param pcb the tcp_pcb for which to send a zero-window probe packet
1415  */
1416 void
1417 tcp_zero_window_probe(struct tcp_pcb *pcb)
1418 {
1419   struct pbuf *p;
1420   struct tcp_hdr *tcphdr;
1421   struct tcp_seg *seg;
1422   u16_t len;
1423   u8_t is_fin;
1424
1425   LWIP_DEBUGF(TCP_DEBUG, ("tcp_zero_window_probe: sending ZERO WINDOW probe to "));
1426   ipX_addr_debug_print(PCB_ISIPV6(pcb), TCP_DEBUG, &pcb->remote_ip);
1427   LWIP_DEBUGF(TCP_DEBUG, ("\n"));
1428
1429   LWIP_DEBUGF(TCP_DEBUG, 
1430               ("tcp_zero_window_probe: tcp_ticks %"U32_F
1431                "   pcb->tmr %"U32_F" pcb->keep_cnt_sent %"U16_F"\n", 
1432                tcp_ticks, pcb->tmr, pcb->keep_cnt_sent));
1433
1434   seg = pcb->unacked;
1435
1436   if(seg == NULL) {
1437     seg = pcb->unsent;
1438   }
1439   if(seg == NULL) {
1440     return;
1441   }
1442
1443   is_fin = ((TCPH_FLAGS(seg->tcphdr) & TCP_FIN) != 0) && (seg->len == 0);
1444   /* we want to send one seqno: either FIN or data (no options) */
1445   len = is_fin ? 0 : 1;
1446
1447   p = tcp_output_alloc_header(pcb, 0, len, seg->tcphdr->seqno);
1448   if(p == NULL) {
1449     LWIP_DEBUGF(TCP_DEBUG, ("tcp_zero_window_probe: no memory for pbuf\n"));
1450     return;
1451   }
1452   tcphdr = (struct tcp_hdr *)p->payload;
1453
1454   if (is_fin) {
1455     /* FIN segment, no data */
1456     TCPH_FLAGS_SET(tcphdr, TCP_ACK | TCP_FIN);
1457   } else {
1458     /* Data segment, copy in one byte from the head of the unacked queue */
1459     struct tcp_hdr *thdr = (struct tcp_hdr *)seg->p->payload;
1460     char *d = ((char *)p->payload + TCP_HLEN);
1461     pbuf_copy_partial(seg->p, d, 1, TCPH_HDRLEN(thdr) * 4);
1462   }
1463
1464 #if CHECKSUM_GEN_TCP
1465   tcphdr->chksum = ipX_chksum_pseudo(PCB_ISIPV6(pcb), p, IP_PROTO_TCP, p->tot_len,
1466       &pcb->local_ip, &pcb->remote_ip);
1467 #endif
1468   TCP_STATS_INC(tcp.xmit);
1469
1470   /* Send output to IP */
1471 #if LWIP_NETIF_HWADDRHINT
1472   ipX_output_hinted(PCB_ISIPV6(pcb), p, &pcb->local_ip, &pcb->remote_ip, pcb->ttl,
1473     0, IP_PROTO_TCP, &pcb->addr_hint);
1474 #else /* LWIP_NETIF_HWADDRHINT*/
1475   ipX_output(PCB_ISIPV6(pcb), p, &pcb->local_ip, &pcb->remote_ip, pcb->ttl, 0, IP_PROTO_TCP);
1476 #endif /* LWIP_NETIF_HWADDRHINT*/
1477
1478   pbuf_free(p);
1479
1480   LWIP_DEBUGF(TCP_DEBUG, ("tcp_zero_window_probe: seqno %"U32_F
1481                           " ackno %"U32_F".\n",
1482                           pcb->snd_nxt - 1, pcb->rcv_nxt));
1483 }
1484 #endif /* LWIP_TCP */