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