• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * @file
3  * Transmission Control Protocol, outgoing traffic
4  *
5  * The output functions of TCP.
6  *
7  * There are two distinct ways for TCP segments to get sent:
8  * - queued data: these are segments transferring data or segments containing
9  *   SYN or FIN (which both count as one sequence number). They are created as
10  *   struct @ref pbuf together with a struct tcp_seg and enqueue to the
11  *   unsent list of the pcb. They are sent by tcp_output:
12  *   - @ref tcp_write : creates data segments
13  *   - @ref tcp_split_unsent_seg : splits a data segment
14  *   - @ref tcp_enqueue_flags : creates SYN-only or FIN-only segments
15  *   - @ref tcp_output / tcp_output_segment : finalize the tcp header
16  *      (e.g. sequence numbers, options, checksum) and output to IP
17  *   - the various tcp_rexmit functions shuffle around segments between the
18  *     unsent an unacked lists to retransmit them
19  *   - tcp_create_segment and tcp_pbuf_prealloc allocate pbuf and
20  *     segment for these functions
21  * - direct send: these segments don't contain data but control the connection
22  *   behaviour. They are created as pbuf only and sent directly without
23  *   enqueueing them:
24  *   - @ref tcp_send_empty_ack sends an ACK-only segment
25  *   - @ref tcp_rst sends a RST segment
26  *   - @ref tcp_keepalive sends a keepalive segment
27  *   - @ref tcp_zero_window_probe sends a window probe segment
28  *   - tcp_output_alloc_header allocates a header-only pbuf for these functions
29  */
30 
31 /*
32  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
33  * All rights reserved.
34  *
35  * Redistribution and use in source and binary forms, with or without modification,
36  * are permitted provided that the following conditions are met:
37  *
38  * 1. Redistributions of source code must retain the above copyright notice,
39  *    this list of conditions and the following disclaimer.
40  * 2. Redistributions in binary form must reproduce the above copyright notice,
41  *    this list of conditions and the following disclaimer in the documentation
42  *    and/or other materials provided with the distribution.
43  * 3. The name of the author may not be used to endorse or promote products
44  *    derived from this software without specific prior written permission.
45  *
46  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
47  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
48  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
49  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
50  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
51  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
52  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
53  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
54  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
55  * OF SUCH DAMAGE.
56  *
57  * This file is part of the lwIP TCP/IP stack.
58  *
59  * Author: Adam Dunkels <adam@sics.se>
60  *
61  */
62 
63 #include "lwip/opt.h"
64 
65 #if LWIP_TCP /* don't build if not configured for use in lwipopts.h */
66 
67 #include "lwip/priv/tcp_priv.h"
68 #include "lwip/def.h"
69 #include "lwip/mem.h"
70 #include "lwip/memp.h"
71 #include "lwip/ip_addr.h"
72 #include "lwip/netif.h"
73 #include "lwip/inet_chksum.h"
74 #include "lwip/stats.h"
75 #include "lwip/ip6.h"
76 #include "lwip/ip6_addr.h"
77 #if LWIP_TCP_TIMESTAMPS
78 #include "lwip/sys.h"
79 #endif
80 
81 #include <string.h>
82 
83 #ifdef LWIP_HOOK_FILENAME
84 #include LWIP_HOOK_FILENAME
85 #endif
86 
87 /* Allow to add custom TCP header options by defining this hook */
88 #ifdef LWIP_HOOK_TCP_OUT_TCPOPT_LENGTH
89 #define LWIP_TCP_OPT_LENGTH_SEGMENT(flags, pcb) LWIP_HOOK_TCP_OUT_TCPOPT_LENGTH(pcb, LWIP_TCP_OPT_LENGTH(flags))
90 #else
91 #define LWIP_TCP_OPT_LENGTH_SEGMENT(flags, pcb) LWIP_TCP_OPT_LENGTH(flags)
92 #endif
93 
94 /* Define some copy-macros for checksum-on-copy so that the code looks
95    nicer by preventing too many ifdef's. */
96 #if TCP_CHECKSUM_ON_COPY
97 #define TCP_DATA_COPY(dst, src, len, seg) do { \
98   tcp_seg_add_chksum(LWIP_CHKSUM_COPY(dst, src, len), \
99                      len, &seg->chksum, &seg->chksum_swapped); \
100   seg->flags |= TF_SEG_DATA_CHECKSUMMED; } while(0)
101 #define TCP_DATA_COPY2(dst, src, len, chksum, chksum_swapped)  \
102   tcp_seg_add_chksum(LWIP_CHKSUM_COPY(dst, src, len), len, chksum, chksum_swapped);
103 #else /* TCP_CHECKSUM_ON_COPY*/
104 #define TCP_DATA_COPY(dst, src, len, seg)                     MEMCPY(dst, src, len)
105 #define TCP_DATA_COPY2(dst, src, len, chksum, chksum_swapped) MEMCPY(dst, src, len)
106 #endif /* TCP_CHECKSUM_ON_COPY*/
107 
108 /** Define this to 1 for an extra check that the output checksum is valid
109  * (useful when the checksum is generated by the application, not the stack) */
110 #ifndef TCP_CHECKSUM_ON_COPY_SANITY_CHECK
111 #define TCP_CHECKSUM_ON_COPY_SANITY_CHECK   0
112 #endif
113 /* Allow to override the failure of sanity check from warning to e.g. hard failure */
114 #if TCP_CHECKSUM_ON_COPY_SANITY_CHECK
115 #ifndef TCP_CHECKSUM_ON_COPY_SANITY_CHECK_FAIL
116 #define TCP_CHECKSUM_ON_COPY_SANITY_CHECK_FAIL(msg) LWIP_DEBUGF(TCP_DEBUG | LWIP_DBG_LEVEL_WARNING, msg)
117 #endif
118 #endif
119 
120 #if TCP_OVERSIZE
121 /** The size of segment pbufs created when TCP_OVERSIZE is enabled */
122 #ifndef TCP_OVERSIZE_CALC_LENGTH
123 #define TCP_OVERSIZE_CALC_LENGTH(length) ((length) + TCP_OVERSIZE)
124 #endif
125 #endif
126 
127 /* Forward declarations.*/
128 static err_t tcp_output_segment(struct tcp_seg *seg, struct tcp_pcb *pcb, struct netif *netif);
129 static err_t tcp_output_control_segment_netif(const struct tcp_pcb *pcb, struct pbuf *p,
130                                               const ip_addr_t *src, const ip_addr_t *dst,
131                                               struct netif *netif);
132 
133 /* tcp_route: common code that returns a fixed bound netif or calls ip_route */
134 static struct netif *
tcp_route(const struct tcp_pcb * pcb,const ip_addr_t * src,const ip_addr_t * dst)135 tcp_route(const struct tcp_pcb *pcb, const ip_addr_t *src, const ip_addr_t *dst)
136 {
137   LWIP_UNUSED_ARG(src); /* in case IPv4-only and source-based routing is disabled */
138 #ifdef LOSCFG_NET_CONTAINER
139   struct net_group *group = get_net_group_from_tcp_pcb(pcb);
140   LWIP_ERROR("tcp_route: invalid net group", group != NULL, return NULL);
141 #endif
142 
143   if ((pcb != NULL) && (pcb->netif_idx != NETIF_NO_INDEX)) {
144 #ifdef LOSCFG_NET_CONTAINER
145     return netif_get_by_index(pcb->netif_idx, group);
146 #else
147     return netif_get_by_index(pcb->netif_idx);
148 #endif
149   } else {
150 #ifdef LOSCFG_NET_CONTAINER
151     return ip_route(src, dst, group);
152 #else
153     return ip_route(src, dst);
154 #endif
155   }
156 }
157 
158 /**
159  * Create a TCP segment with prefilled header.
160  *
161  * Called by @ref tcp_write, @ref tcp_enqueue_flags and @ref tcp_split_unsent_seg
162  *
163  * @param pcb Protocol control block for the TCP connection.
164  * @param p pbuf that is used to hold the TCP header.
165  * @param hdrflags TCP flags for header.
166  * @param seqno TCP sequence number of this packet
167  * @param optflags options to include in TCP header
168  * @return a new tcp_seg pointing to p, or NULL.
169  * The TCP header is filled in except ackno and wnd.
170  * p is freed on failure.
171  */
172 static struct tcp_seg *
tcp_create_segment(const struct tcp_pcb * pcb,struct pbuf * p,u8_t hdrflags,u32_t seqno,u8_t optflags)173 tcp_create_segment(const struct tcp_pcb *pcb, struct pbuf *p, u8_t hdrflags, u32_t seqno, u8_t optflags)
174 {
175   struct tcp_seg *seg;
176   u8_t optlen;
177 
178   LWIP_ASSERT("tcp_create_segment: invalid pcb", pcb != NULL);
179   LWIP_ASSERT("tcp_create_segment: invalid pbuf", p != NULL);
180 
181   optlen = LWIP_TCP_OPT_LENGTH_SEGMENT(optflags, pcb);
182 
183   if ((seg = (struct tcp_seg *)memp_malloc(MEMP_TCP_SEG)) == NULL) {
184     LWIP_DEBUGF(TCP_OUTPUT_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("tcp_create_segment: no memory.\n"));
185     pbuf_free(p);
186     return NULL;
187   }
188   seg->flags = optflags;
189   seg->next = NULL;
190   seg->p = p;
191   LWIP_ASSERT("p->tot_len >= optlen", p->tot_len >= optlen);
192   seg->len = p->tot_len - optlen;
193 #if TCP_OVERSIZE_DBGCHECK
194   seg->oversize_left = 0;
195 #endif /* TCP_OVERSIZE_DBGCHECK */
196 #if TCP_CHECKSUM_ON_COPY
197   seg->chksum = 0;
198   seg->chksum_swapped = 0;
199   /* check optflags */
200   LWIP_ASSERT("invalid optflags passed: TF_SEG_DATA_CHECKSUMMED",
201               (optflags & TF_SEG_DATA_CHECKSUMMED) == 0);
202 #endif /* TCP_CHECKSUM_ON_COPY */
203 
204   /* build TCP header */
205   if (pbuf_add_header(p, TCP_HLEN)) {
206     LWIP_DEBUGF(TCP_OUTPUT_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("tcp_create_segment: no room for TCP header in pbuf.\n"));
207     TCP_STATS_INC(tcp.err);
208     tcp_seg_free(seg);
209     return NULL;
210   }
211   seg->tcphdr = (struct tcp_hdr *)seg->p->payload;
212   seg->tcphdr->src = lwip_htons(pcb->local_port);
213   seg->tcphdr->dest = lwip_htons(pcb->remote_port);
214   seg->tcphdr->seqno = lwip_htonl(seqno);
215   /* ackno is set in tcp_output */
216   TCPH_HDRLEN_FLAGS_SET(seg->tcphdr, (5 + optlen / 4), hdrflags);
217   /* wnd and chksum are set in tcp_output */
218   seg->tcphdr->urgp = 0;
219   return seg;
220 }
221 
222 /**
223  * Allocate a PBUF_RAM pbuf, perhaps with extra space at the end.
224  *
225  * This function is like pbuf_alloc(layer, length, PBUF_RAM) except
226  * there may be extra bytes available at the end.
227  *
228  * Called by @ref tcp_write
229  *
230  * @param layer flag to define header size.
231  * @param length size of the pbuf's payload.
232  * @param max_length maximum usable size of payload+oversize.
233  * @param oversize pointer to a u16_t that will receive the number of usable tail bytes.
234  * @param pcb The TCP connection that will enqueue the pbuf.
235  * @param apiflags API flags given to tcp_write.
236  * @param first_seg true when this pbuf will be used in the first enqueued segment.
237  */
238 #if TCP_OVERSIZE
239 static struct pbuf *
tcp_pbuf_prealloc(pbuf_layer layer,u16_t length,u16_t max_length,u16_t * oversize,const struct tcp_pcb * pcb,u8_t apiflags,u8_t first_seg)240 tcp_pbuf_prealloc(pbuf_layer layer, u16_t length, u16_t max_length,
241                   u16_t *oversize, const struct tcp_pcb *pcb, u8_t apiflags,
242                   u8_t first_seg)
243 {
244   struct pbuf *p;
245   u16_t alloc = length;
246 
247   LWIP_ASSERT("tcp_pbuf_prealloc: invalid oversize", oversize != NULL);
248   LWIP_ASSERT("tcp_pbuf_prealloc: invalid pcb", pcb != NULL);
249 
250 #if LWIP_NETIF_TX_SINGLE_PBUF
251   LWIP_UNUSED_ARG(max_length);
252   LWIP_UNUSED_ARG(pcb);
253   LWIP_UNUSED_ARG(apiflags);
254   LWIP_UNUSED_ARG(first_seg);
255   alloc = max_length;
256 #else /* LWIP_NETIF_TX_SINGLE_PBUF */
257   if (length < max_length) {
258     /* Should we allocate an oversized pbuf, or just the minimum
259      * length required? If tcp_write is going to be called again
260      * before this segment is transmitted, we want the oversized
261      * buffer. If the segment will be transmitted immediately, we can
262      * save memory by allocating only length. We use a simple
263      * heuristic based on the following information:
264      *
265      * Did the user set TCP_WRITE_FLAG_MORE?
266      *
267      * Will the Nagle algorithm defer transmission of this segment?
268      */
269     if ((apiflags & TCP_WRITE_FLAG_MORE) ||
270         (!(pcb->flags & TF_NODELAY) &&
271          (!first_seg ||
272           pcb->unsent != NULL ||
273           pcb->unacked != NULL))) {
274       alloc = LWIP_MIN(max_length, LWIP_MEM_ALIGN_SIZE(TCP_OVERSIZE_CALC_LENGTH(length)));
275     }
276   }
277 #endif /* LWIP_NETIF_TX_SINGLE_PBUF */
278   p = pbuf_alloc(layer, alloc, PBUF_RAM);
279   if (p == NULL) {
280     return NULL;
281   }
282   LWIP_ASSERT("need unchained pbuf", p->next == NULL);
283   *oversize = p->len - length;
284   /* trim p->len to the currently used size */
285   p->len = p->tot_len = length;
286   return p;
287 }
288 #else /* TCP_OVERSIZE */
289 #define tcp_pbuf_prealloc(layer, length, mx, os, pcb, api, fst) pbuf_alloc((layer), (length), PBUF_RAM)
290 #endif /* TCP_OVERSIZE */
291 
292 #if TCP_CHECKSUM_ON_COPY
293 /** Add a checksum of newly added data to the segment.
294  *
295  * Called by tcp_write and tcp_split_unsent_seg.
296  */
297 static void
tcp_seg_add_chksum(u16_t chksum,u16_t len,u16_t * seg_chksum,u8_t * seg_chksum_swapped)298 tcp_seg_add_chksum(u16_t chksum, u16_t len, u16_t *seg_chksum,
299                    u8_t *seg_chksum_swapped)
300 {
301   u32_t helper;
302   /* add chksum to old chksum and fold to u16_t */
303   helper = chksum + *seg_chksum;
304   chksum = FOLD_U32T(helper);
305   if ((len & 1) != 0) {
306     *seg_chksum_swapped = 1 - *seg_chksum_swapped;
307     chksum = SWAP_BYTES_IN_WORD(chksum);
308   }
309   *seg_chksum = chksum;
310 }
311 #endif /* TCP_CHECKSUM_ON_COPY */
312 
313 /** Checks if tcp_write is allowed or not (checks state, snd_buf and snd_queuelen).
314  *
315  * @param pcb the tcp pcb to check for
316  * @param len length of data to send (checked against snd_buf)
317  * @return ERR_OK if tcp_write is allowed to proceed, another err_t otherwise
318  */
319 static err_t
tcp_write_checks(struct tcp_pcb * pcb,u16_t len)320 tcp_write_checks(struct tcp_pcb *pcb, u16_t len)
321 {
322   LWIP_ASSERT("tcp_write_checks: invalid pcb", pcb != NULL);
323 
324   /* connection is in invalid state for data transmission? */
325   if ((pcb->state != ESTABLISHED) &&
326       (pcb->state != CLOSE_WAIT) &&
327       (pcb->state != SYN_SENT) &&
328       (pcb->state != SYN_RCVD)) {
329     LWIP_DEBUGF(TCP_OUTPUT_DEBUG | LWIP_DBG_STATE | LWIP_DBG_LEVEL_SEVERE, ("tcp_write() called in invalid state\n"));
330     return ERR_CONN;
331   } else if (len == 0) {
332     return ERR_OK;
333   }
334 
335   /* fail on too much data */
336   if (len > pcb->snd_buf) {
337     LWIP_DEBUGF(TCP_OUTPUT_DEBUG | LWIP_DBG_LEVEL_SEVERE, ("tcp_write: too much data (len=%"U16_F" > snd_buf=%"TCPWNDSIZE_F")\n",
338                 len, pcb->snd_buf));
339     tcp_set_flags(pcb, TF_NAGLEMEMERR);
340     return ERR_MEM;
341   }
342 
343   LWIP_DEBUGF(TCP_QLEN_DEBUG, ("tcp_write: queuelen: %"TCPWNDSIZE_F"\n", (tcpwnd_size_t)pcb->snd_queuelen));
344 
345   /* If total number of pbufs on the unsent/unacked queues exceeds the
346    * configured maximum, return an error */
347   /* check for configured max queuelen and possible overflow */
348   if (pcb->snd_queuelen >= LWIP_MIN(TCP_SND_QUEUELEN, (TCP_SNDQUEUELEN_OVERFLOW + 1))) {
349     LWIP_DEBUGF(TCP_OUTPUT_DEBUG | LWIP_DBG_LEVEL_SEVERE, ("tcp_write: too long queue %"U16_F" (max %"U16_F")\n",
350                 pcb->snd_queuelen, (u16_t)TCP_SND_QUEUELEN));
351     TCP_STATS_INC(tcp.memerr);
352     tcp_set_flags(pcb, TF_NAGLEMEMERR);
353     return ERR_MEM;
354   }
355   if (pcb->snd_queuelen != 0) {
356     LWIP_ASSERT("tcp_write: pbufs on queue => at least one queue non-empty",
357                 pcb->unacked != NULL || pcb->unsent != NULL);
358   } else {
359     LWIP_ASSERT("tcp_write: no pbufs on queue => both queues empty",
360                 pcb->unacked == NULL && pcb->unsent == NULL);
361   }
362   return ERR_OK;
363 }
364 
365 /**
366  * @ingroup tcp_raw
367  * Write data for sending (but does not send it immediately).
368  *
369  * It waits in the expectation of more data being sent soon (as
370  * it can send them more efficiently by combining them together).
371  * To prompt the system to send data now, call tcp_output() after
372  * calling tcp_write().
373  *
374  * This function enqueues the data pointed to by the argument dataptr. The length of
375  * the data is passed as the len parameter. The apiflags can be one or more of:
376  * - TCP_WRITE_FLAG_COPY: indicates whether the new memory should be allocated
377  *   for the data to be copied into. If this flag is not given, no new memory
378  *   should be allocated and the data should only be referenced by pointer. This
379  *   also means that the memory behind dataptr must not change until the data is
380  *   ACKed by the remote host
381  * - TCP_WRITE_FLAG_MORE: indicates that more data follows. If this is omitted,
382  *   the PSH flag is set in the last segment created by this call to tcp_write.
383  *   If this flag is given, the PSH flag is not set.
384  *
385  * The tcp_write() function will fail and return ERR_MEM if the length
386  * of the data exceeds the current send buffer size or if the length of
387  * the queue of outgoing segment is larger than the upper limit defined
388  * in lwipopts.h. The number of bytes available in the output queue can
389  * be retrieved with the tcp_sndbuf() function.
390  *
391  * The proper way to use this function is to call the function with at
392  * most tcp_sndbuf() bytes of data. If the function returns ERR_MEM,
393  * the application should wait until some of the currently enqueued
394  * data has been successfully received by the other host and try again.
395  *
396  * @param pcb Protocol control block for the TCP connection to enqueue data for.
397  * @param arg Pointer to the data to be enqueued for sending.
398  * @param len Data length in bytes
399  * @param apiflags combination of following flags :
400  * - TCP_WRITE_FLAG_COPY (0x01) data will be copied into memory belonging to the stack
401  * - TCP_WRITE_FLAG_MORE (0x02) for TCP connection, PSH flag will not be set on last segment sent,
402  * @return ERR_OK if enqueued, another err_t on error
403  */
404 err_t
tcp_write(struct tcp_pcb * pcb,const void * arg,u16_t len,u8_t apiflags)405 tcp_write(struct tcp_pcb *pcb, const void *arg, u16_t len, u8_t apiflags)
406 {
407   struct pbuf *concat_p = NULL;
408   struct tcp_seg *last_unsent = NULL, *seg = NULL, *prev_seg = NULL, *queue = NULL;
409   u16_t pos = 0; /* position in 'arg' data */
410   u16_t queuelen;
411   u8_t optlen;
412   u8_t optflags = 0;
413 #if TCP_OVERSIZE
414   u16_t oversize = 0;
415   u16_t oversize_used = 0;
416 #if TCP_OVERSIZE_DBGCHECK
417   u16_t oversize_add = 0;
418 #endif /* TCP_OVERSIZE_DBGCHECK*/
419 #endif /* TCP_OVERSIZE */
420   u16_t extendlen = 0;
421 #if TCP_CHECKSUM_ON_COPY
422   u16_t concat_chksum = 0;
423   u8_t concat_chksum_swapped = 0;
424   u16_t concat_chksummed = 0;
425 #endif /* TCP_CHECKSUM_ON_COPY */
426   err_t err;
427   u16_t mss_local;
428 
429   LWIP_ERROR("tcp_write: invalid pcb", pcb != NULL, return ERR_ARG);
430 
431   /* don't allocate segments bigger than half the maximum window we ever received */
432   mss_local = LWIP_MIN(pcb->mss, TCPWND_MIN16(pcb->snd_wnd_max / 2));
433   mss_local = mss_local ? mss_local : pcb->mss;
434 
435   LWIP_ASSERT_CORE_LOCKED();
436 
437 #if LWIP_NETIF_TX_SINGLE_PBUF
438   /* Always copy to try to create single pbufs for TX */
439   apiflags |= TCP_WRITE_FLAG_COPY;
440 #endif /* LWIP_NETIF_TX_SINGLE_PBUF */
441 
442   LWIP_DEBUGF(TCP_OUTPUT_DEBUG, ("tcp_write(pcb=%p, data=%p, len=%"U16_F", apiflags=%"U16_F")\n",
443                                  (void *)pcb, arg, len, (u16_t)apiflags));
444   LWIP_ERROR("tcp_write: arg == NULL (programmer violates API)",
445              arg != NULL, return ERR_ARG;);
446 
447   err = tcp_write_checks(pcb, len);
448   if (err != ERR_OK) {
449     return err;
450   }
451   queuelen = pcb->snd_queuelen;
452 
453 #if LWIP_TCP_TIMESTAMPS
454   if ((pcb->flags & TF_TIMESTAMP)) {
455     /* Make sure the timestamp option is only included in data segments if we
456        agreed about it with the remote host. */
457     optflags = TF_SEG_OPTS_TS;
458     optlen = LWIP_TCP_OPT_LENGTH_SEGMENT(TF_SEG_OPTS_TS, pcb);
459     /* ensure that segments can hold at least one data byte... */
460     mss_local = LWIP_MAX(mss_local, LWIP_TCP_OPT_LEN_TS + 1);
461   } else
462 #endif /* LWIP_TCP_TIMESTAMPS */
463   {
464     optlen = LWIP_TCP_OPT_LENGTH_SEGMENT(0, pcb);
465   }
466 
467 
468   /*
469    * TCP segmentation is done in three phases with increasing complexity:
470    *
471    * 1. Copy data directly into an oversized pbuf.
472    * 2. Chain a new pbuf to the end of pcb->unsent.
473    * 3. Create new segments.
474    *
475    * We may run out of memory at any point. In that case we must
476    * return ERR_MEM and not change anything in pcb. Therefore, all
477    * changes are recorded in local variables and committed at the end
478    * of the function. Some pcb fields are maintained in local copies:
479    *
480    * queuelen = pcb->snd_queuelen
481    * oversize = pcb->unsent_oversize
482    *
483    * These variables are set consistently by the phases:
484    *
485    * seg points to the last segment tampered with.
486    *
487    * pos records progress as data is segmented.
488    */
489 
490   /* Find the tail of the unsent queue. */
491   if (pcb->unsent != NULL) {
492     u16_t space;
493     u16_t unsent_optlen;
494 
495     /* @todo: this could be sped up by keeping last_unsent in the pcb */
496     for (last_unsent = pcb->unsent; last_unsent->next != NULL;
497          last_unsent = last_unsent->next);
498 
499     /* Usable space at the end of the last unsent segment */
500     unsent_optlen = LWIP_TCP_OPT_LENGTH_SEGMENT(last_unsent->flags, pcb);
501     LWIP_ASSERT("mss_local is too small", mss_local >= last_unsent->len + unsent_optlen);
502     space = mss_local - (last_unsent->len + unsent_optlen);
503 
504     /*
505      * Phase 1: Copy data directly into an oversized pbuf.
506      *
507      * The number of bytes copied is recorded in the oversize_used
508      * variable. The actual copying is done at the bottom of the
509      * function.
510      */
511 #if TCP_OVERSIZE
512 #if TCP_OVERSIZE_DBGCHECK
513     /* check that pcb->unsent_oversize matches last_unsent->oversize_left */
514     LWIP_ASSERT("unsent_oversize mismatch (pcb vs. last_unsent)",
515                 pcb->unsent_oversize == last_unsent->oversize_left);
516 #endif /* TCP_OVERSIZE_DBGCHECK */
517     oversize = pcb->unsent_oversize;
518     if (oversize > 0) {
519       LWIP_ASSERT("inconsistent oversize vs. space", oversize <= space);
520       seg = last_unsent;
521       oversize_used = LWIP_MIN(space, LWIP_MIN(oversize, len));
522       pos += oversize_used;
523       oversize -= oversize_used;
524       space -= oversize_used;
525     }
526     /* now we are either finished or oversize is zero */
527     LWIP_ASSERT("inconsistent oversize vs. len", (oversize == 0) || (pos == len));
528 #endif /* TCP_OVERSIZE */
529 
530 #if !LWIP_NETIF_TX_SINGLE_PBUF
531     /*
532      * Phase 2: Chain a new pbuf to the end of pcb->unsent.
533      *
534      * As an exception when NOT copying the data, if the given data buffer
535      * directly follows the last unsent data buffer in memory, extend the last
536      * ROM pbuf reference to the buffer, thus saving a ROM pbuf allocation.
537      *
538      * We don't extend segments containing SYN/FIN flags or options
539      * (len==0). The new pbuf is kept in concat_p and pbuf_cat'ed at
540      * the end.
541      *
542      * This phase is skipped for LWIP_NETIF_TX_SINGLE_PBUF as we could only execute
543      * it after rexmit puts a segment from unacked to unsent and at this point,
544      * oversize info is lost.
545      */
546     if ((pos < len) && (space > 0) && (last_unsent->len > 0)) {
547       u16_t seglen = LWIP_MIN(space, len - pos);
548       seg = last_unsent;
549 
550       /* Create a pbuf with a copy or reference to seglen bytes. We
551        * can use PBUF_RAW here since the data appears in the middle of
552        * a segment. A header will never be prepended. */
553       if (apiflags & TCP_WRITE_FLAG_COPY) {
554         /* Data is copied */
555         if ((concat_p = tcp_pbuf_prealloc(PBUF_RAW, seglen, space, &oversize, pcb, apiflags, 1)) == NULL) {
556           LWIP_DEBUGF(TCP_OUTPUT_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
557                       ("tcp_write : could not allocate memory for pbuf copy size %"U16_F"\n",
558                        seglen));
559           goto memerr;
560         }
561 #if TCP_OVERSIZE_DBGCHECK
562         oversize_add = oversize;
563 #endif /* TCP_OVERSIZE_DBGCHECK */
564         TCP_DATA_COPY2(concat_p->payload, (const u8_t *)arg + pos, seglen, &concat_chksum, &concat_chksum_swapped);
565 #if TCP_CHECKSUM_ON_COPY
566         concat_chksummed += seglen;
567 #endif /* TCP_CHECKSUM_ON_COPY */
568         queuelen += pbuf_clen(concat_p);
569       } else {
570         /* Data is not copied */
571         /* If the last unsent pbuf is of type PBUF_ROM, try to extend it. */
572         struct pbuf *p;
573         for (p = last_unsent->p; p->next != NULL; p = p->next);
574         if (((p->type_internal & (PBUF_TYPE_FLAG_STRUCT_DATA_CONTIGUOUS | PBUF_TYPE_FLAG_DATA_VOLATILE)) == 0) &&
575             (const u8_t *)p->payload + p->len == (const u8_t *)arg) {
576           LWIP_ASSERT("tcp_write: ROM pbufs cannot be oversized", pos == 0);
577           extendlen = seglen;
578         } else {
579           if ((concat_p = pbuf_alloc(PBUF_RAW, seglen, PBUF_ROM)) == NULL) {
580             LWIP_DEBUGF(TCP_OUTPUT_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
581                         ("tcp_write: could not allocate memory for zero-copy pbuf\n"));
582             goto memerr;
583           }
584           /* reference the non-volatile payload data */
585           ((struct pbuf_rom *)concat_p)->payload = (const u8_t *)arg + pos;
586           queuelen += pbuf_clen(concat_p);
587         }
588 #if TCP_CHECKSUM_ON_COPY
589         /* calculate the checksum of nocopy-data */
590         tcp_seg_add_chksum(~inet_chksum((const u8_t *)arg + pos, seglen), seglen,
591                            &concat_chksum, &concat_chksum_swapped);
592         concat_chksummed += seglen;
593 #endif /* TCP_CHECKSUM_ON_COPY */
594       }
595 
596       pos += seglen;
597     }
598 #endif /* !LWIP_NETIF_TX_SINGLE_PBUF */
599   } else {
600 #if TCP_OVERSIZE
601     LWIP_ASSERT("unsent_oversize mismatch (pcb->unsent is NULL)",
602                 pcb->unsent_oversize == 0);
603 #endif /* TCP_OVERSIZE */
604   }
605 
606   /*
607    * Phase 3: Create new segments.
608    *
609    * The new segments are chained together in the local 'queue'
610    * variable, ready to be appended to pcb->unsent.
611    */
612   while (pos < len) {
613     struct pbuf *p;
614     u16_t left = len - pos;
615     u16_t max_len = mss_local - optlen;
616     u16_t seglen = LWIP_MIN(left, max_len);
617 #if TCP_CHECKSUM_ON_COPY
618     u16_t chksum = 0;
619     u8_t chksum_swapped = 0;
620 #endif /* TCP_CHECKSUM_ON_COPY */
621 
622     if (apiflags & TCP_WRITE_FLAG_COPY) {
623       /* If copy is set, memory should be allocated and data copied
624        * into pbuf */
625       if ((p = tcp_pbuf_prealloc(PBUF_TRANSPORT, seglen + optlen, mss_local, &oversize, pcb, apiflags, queue == NULL)) == NULL) {
626         LWIP_DEBUGF(TCP_OUTPUT_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("tcp_write : could not allocate memory for pbuf copy size %"U16_F"\n", seglen));
627         goto memerr;
628       }
629       LWIP_ASSERT("tcp_write: check that first pbuf can hold the complete seglen",
630                   (p->len >= seglen));
631       TCP_DATA_COPY2((char *)p->payload + optlen, (const u8_t *)arg + pos, seglen, &chksum, &chksum_swapped);
632     } else {
633       /* Copy is not set: First allocate a pbuf for holding the data.
634        * Since the referenced data is available at least until it is
635        * sent out on the link (as it has to be ACKed by the remote
636        * party) we can safely use PBUF_ROM instead of PBUF_REF here.
637        */
638       struct pbuf *p2;
639 #if TCP_OVERSIZE
640       LWIP_ASSERT("oversize == 0", oversize == 0);
641 #endif /* TCP_OVERSIZE */
642       if ((p2 = pbuf_alloc(PBUF_TRANSPORT, seglen, PBUF_ROM)) == NULL) {
643         LWIP_DEBUGF(TCP_OUTPUT_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("tcp_write: could not allocate memory for zero-copy pbuf\n"));
644         goto memerr;
645       }
646 #if TCP_CHECKSUM_ON_COPY
647       /* calculate the checksum of nocopy-data */
648       chksum = ~inet_chksum((const u8_t *)arg + pos, seglen);
649       if (seglen & 1) {
650         chksum_swapped = 1;
651         chksum = SWAP_BYTES_IN_WORD(chksum);
652       }
653 #endif /* TCP_CHECKSUM_ON_COPY */
654       /* reference the non-volatile payload data */
655       ((struct pbuf_rom *)p2)->payload = (const u8_t *)arg + pos;
656 
657       /* Second, allocate a pbuf for the headers. */
658       if ((p = pbuf_alloc(PBUF_TRANSPORT, optlen, PBUF_RAM)) == NULL) {
659         /* If allocation fails, we have to deallocate the data pbuf as
660          * well. */
661         pbuf_free(p2);
662         LWIP_DEBUGF(TCP_OUTPUT_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("tcp_write: could not allocate memory for header pbuf\n"));
663         goto memerr;
664       }
665       /* Concatenate the headers and data pbufs together. */
666       pbuf_cat(p/*header*/, p2/*data*/);
667     }
668 
669     queuelen += pbuf_clen(p);
670 
671     /* Now that there are more segments queued, we check again if the
672      * length of the queue exceeds the configured maximum or
673      * overflows. */
674     if (queuelen > LWIP_MIN(TCP_SND_QUEUELEN, TCP_SNDQUEUELEN_OVERFLOW)) {
675       LWIP_DEBUGF(TCP_OUTPUT_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("tcp_write: queue too long %"U16_F" (%d)\n",
676                   queuelen, (int)TCP_SND_QUEUELEN));
677       pbuf_free(p);
678       goto memerr;
679     }
680 
681     if ((seg = tcp_create_segment(pcb, p, 0, pcb->snd_lbb + pos, optflags)) == NULL) {
682       goto memerr;
683     }
684 #if TCP_OVERSIZE_DBGCHECK
685     seg->oversize_left = oversize;
686 #endif /* TCP_OVERSIZE_DBGCHECK */
687 #if TCP_CHECKSUM_ON_COPY
688     seg->chksum = chksum;
689     seg->chksum_swapped = chksum_swapped;
690     seg->flags |= TF_SEG_DATA_CHECKSUMMED;
691 #endif /* TCP_CHECKSUM_ON_COPY */
692 
693     /* first segment of to-be-queued data? */
694     if (queue == NULL) {
695       queue = seg;
696     } else {
697       /* Attach the segment to the end of the queued segments */
698       LWIP_ASSERT("prev_seg != NULL", prev_seg != NULL);
699       prev_seg->next = seg;
700     }
701     /* remember last segment of to-be-queued data for next iteration */
702     prev_seg = seg;
703 
704     LWIP_DEBUGF(TCP_OUTPUT_DEBUG | LWIP_DBG_TRACE, ("tcp_write: queueing %"U32_F":%"U32_F"\n",
705                 lwip_ntohl(seg->tcphdr->seqno),
706                 lwip_ntohl(seg->tcphdr->seqno) + TCP_TCPLEN(seg)));
707 
708     pos += seglen;
709   }
710 
711   /*
712    * All three segmentation phases were successful. We can commit the
713    * transaction.
714    */
715 #if TCP_OVERSIZE_DBGCHECK
716   if ((last_unsent != NULL) && (oversize_add != 0)) {
717     last_unsent->oversize_left += oversize_add;
718   }
719 #endif /* TCP_OVERSIZE_DBGCHECK */
720 
721   /*
722    * Phase 1: If data has been added to the preallocated tail of
723    * last_unsent, we update the length fields of the pbuf chain.
724    */
725 #if TCP_OVERSIZE
726   if (oversize_used > 0) {
727     struct pbuf *p;
728     /* Bump tot_len of whole chain, len of tail */
729     for (p = last_unsent->p; p; p = p->next) {
730       p->tot_len += oversize_used;
731       if (p->next == NULL) {
732         TCP_DATA_COPY((char *)p->payload + p->len, arg, oversize_used, last_unsent);
733         p->len += oversize_used;
734       }
735     }
736     last_unsent->len += oversize_used;
737 #if TCP_OVERSIZE_DBGCHECK
738     LWIP_ASSERT("last_unsent->oversize_left >= oversize_used",
739                 last_unsent->oversize_left >= oversize_used);
740     last_unsent->oversize_left -= oversize_used;
741 #endif /* TCP_OVERSIZE_DBGCHECK */
742   }
743   pcb->unsent_oversize = oversize;
744 #endif /* TCP_OVERSIZE */
745 
746   /*
747    * Phase 2: concat_p can be concatenated onto last_unsent->p, unless we
748    * determined that the last ROM pbuf can be extended to include the new data.
749    */
750   if (concat_p != NULL) {
751     LWIP_ASSERT("tcp_write: cannot concatenate when pcb->unsent is empty",
752                 (last_unsent != NULL));
753     pbuf_cat(last_unsent->p, concat_p);
754     last_unsent->len += concat_p->tot_len;
755   } else if (extendlen > 0) {
756     struct pbuf *p;
757     LWIP_ASSERT("tcp_write: extension of reference requires reference",
758                 last_unsent != NULL && last_unsent->p != NULL);
759     for (p = last_unsent->p; p->next != NULL; p = p->next) {
760       p->tot_len += extendlen;
761     }
762     p->tot_len += extendlen;
763     p->len += extendlen;
764     last_unsent->len += extendlen;
765   }
766 
767 #if TCP_CHECKSUM_ON_COPY
768   if (concat_chksummed) {
769     LWIP_ASSERT("tcp_write: concat checksum needs concatenated data",
770                 concat_p != NULL || extendlen > 0);
771     /*if concat checksumm swapped - swap it back */
772     if (concat_chksum_swapped) {
773       concat_chksum = SWAP_BYTES_IN_WORD(concat_chksum);
774     }
775     tcp_seg_add_chksum(concat_chksum, concat_chksummed, &last_unsent->chksum,
776                        &last_unsent->chksum_swapped);
777     last_unsent->flags |= TF_SEG_DATA_CHECKSUMMED;
778   }
779 #endif /* TCP_CHECKSUM_ON_COPY */
780 
781   /*
782    * Phase 3: Append queue to pcb->unsent. Queue may be NULL, but that
783    * is harmless
784    */
785   if (last_unsent == NULL) {
786     pcb->unsent = queue;
787   } else {
788     last_unsent->next = queue;
789   }
790 
791   /*
792    * Finally update the pcb state.
793    */
794   pcb->snd_lbb += len;
795   pcb->snd_buf -= len;
796   pcb->snd_queuelen = queuelen;
797 
798   LWIP_DEBUGF(TCP_QLEN_DEBUG, ("tcp_write: %"S16_F" (after enqueued)\n",
799                                pcb->snd_queuelen));
800   if (pcb->snd_queuelen != 0) {
801     LWIP_ASSERT("tcp_write: valid queue length",
802                 pcb->unacked != NULL || pcb->unsent != NULL);
803   }
804 
805   /* Set the PSH flag in the last segment that we enqueued. */
806   if (seg != NULL && seg->tcphdr != NULL && ((apiflags & TCP_WRITE_FLAG_MORE) == 0)) {
807     TCPH_SET_FLAG(seg->tcphdr, TCP_PSH);
808   }
809 
810   return ERR_OK;
811 memerr:
812   tcp_set_flags(pcb, TF_NAGLEMEMERR);
813   TCP_STATS_INC(tcp.memerr);
814 
815   if (concat_p != NULL) {
816     pbuf_free(concat_p);
817   }
818   if (queue != NULL) {
819     tcp_segs_free(queue);
820   }
821   if (pcb->snd_queuelen != 0) {
822     LWIP_ASSERT("tcp_write: valid queue length", pcb->unacked != NULL ||
823                 pcb->unsent != NULL);
824   }
825   LWIP_DEBUGF(TCP_QLEN_DEBUG | LWIP_DBG_STATE, ("tcp_write: %"S16_F" (with mem err)\n", pcb->snd_queuelen));
826   return ERR_MEM;
827 }
828 
829 /**
830  * Split segment on the head of the unsent queue.  If return is not
831  * ERR_OK, existing head remains intact
832  *
833  * The split is accomplished by creating a new TCP segment and pbuf
834  * which holds the remainder payload after the split.  The original
835  * pbuf is trimmed to new length.  This allows splitting of read-only
836  * pbufs
837  *
838  * @param pcb the tcp_pcb for which to split the unsent head
839  * @param split the amount of payload to remain in the head
840  */
841 err_t
tcp_split_unsent_seg(struct tcp_pcb * pcb,u16_t split)842 tcp_split_unsent_seg(struct tcp_pcb *pcb, u16_t split)
843 {
844   struct tcp_seg *seg = NULL, *useg = NULL;
845   struct pbuf *p = NULL;
846   u8_t optlen;
847   u8_t optflags;
848   u8_t split_flags;
849   u8_t remainder_flags;
850   u16_t remainder;
851   u16_t offset;
852 #if TCP_CHECKSUM_ON_COPY
853   u16_t chksum = 0;
854   u8_t chksum_swapped = 0;
855   struct pbuf *q;
856 #endif /* TCP_CHECKSUM_ON_COPY */
857 
858   LWIP_ASSERT("tcp_split_unsent_seg: invalid pcb", pcb != NULL);
859 
860   useg = pcb->unsent;
861   if (useg == NULL) {
862     return ERR_MEM;
863   }
864 
865   if (split == 0) {
866     LWIP_ASSERT("Can't split segment into length 0", 0);
867     return ERR_VAL;
868   }
869 
870   if (useg->len <= split) {
871     return ERR_OK;
872   }
873 
874   LWIP_ASSERT("split <= mss", split <= pcb->mss);
875   LWIP_ASSERT("useg->len > 0", useg->len > 0);
876 
877   /* We should check that we don't exceed TCP_SND_QUEUELEN but we need
878    * to split this packet so we may actually exceed the max value by
879    * one!
880    */
881   LWIP_DEBUGF(TCP_QLEN_DEBUG, ("tcp_enqueue: split_unsent_seg: %u\n", (unsigned int)pcb->snd_queuelen));
882 
883   optflags = useg->flags;
884 #if TCP_CHECKSUM_ON_COPY
885   /* Remove since checksum is not stored until after tcp_create_segment() */
886   optflags &= ~TF_SEG_DATA_CHECKSUMMED;
887 #endif /* TCP_CHECKSUM_ON_COPY */
888   optlen = LWIP_TCP_OPT_LENGTH(optflags);
889   remainder = useg->len - split;
890 
891   /* Create new pbuf for the remainder of the split */
892   p = pbuf_alloc(PBUF_TRANSPORT, remainder + optlen, PBUF_RAM);
893   if (p == NULL) {
894     LWIP_DEBUGF(TCP_OUTPUT_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
895                 ("tcp_split_unsent_seg: could not allocate memory for pbuf remainder %u\n", remainder));
896     goto memerr;
897   }
898 
899   /* Offset into the original pbuf is past TCP/IP headers, options, and split amount */
900   offset = useg->p->tot_len - useg->len + split;
901   /* Copy remainder into new pbuf, headers and options will not be filled out */
902   if (pbuf_copy_partial(useg->p, (u8_t *)p->payload + optlen, remainder, offset ) != remainder) {
903     LWIP_DEBUGF(TCP_OUTPUT_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
904                 ("tcp_split_unsent_seg: could not copy pbuf remainder %u\n", remainder));
905     goto memerr;
906   }
907 #if TCP_CHECKSUM_ON_COPY
908   /* calculate the checksum on remainder data */
909   tcp_seg_add_chksum(~inet_chksum((const u8_t *)p->payload + optlen, remainder), remainder,
910                      &chksum, &chksum_swapped);
911 #endif /* TCP_CHECKSUM_ON_COPY */
912 
913   /* Options are created when calling tcp_output() */
914 
915   /* Migrate flags from original segment */
916   split_flags = TCPH_FLAGS(useg->tcphdr);
917   remainder_flags = 0; /* ACK added in tcp_output() */
918 
919   if (split_flags & TCP_PSH) {
920     split_flags &= ~TCP_PSH;
921     remainder_flags |= TCP_PSH;
922   }
923   if (split_flags & TCP_FIN) {
924     split_flags &= ~TCP_FIN;
925     remainder_flags |= TCP_FIN;
926   }
927   /* SYN should be left on split, RST should not be present with data */
928 
929   seg = tcp_create_segment(pcb, p, remainder_flags, lwip_ntohl(useg->tcphdr->seqno) + split, optflags);
930   if (seg == NULL) {
931     p = NULL; /* Freed by tcp_create_segment */
932     LWIP_DEBUGF(TCP_OUTPUT_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
933                 ("tcp_split_unsent_seg: could not create new TCP segment\n"));
934     goto memerr;
935   }
936 
937 #if TCP_CHECKSUM_ON_COPY
938   seg->chksum = chksum;
939   seg->chksum_swapped = chksum_swapped;
940   seg->flags |= TF_SEG_DATA_CHECKSUMMED;
941 #endif /* TCP_CHECKSUM_ON_COPY */
942 
943   /* Remove this segment from the queue since trimming it may free pbufs */
944   pcb->snd_queuelen -= pbuf_clen(useg->p);
945 
946   /* Trim the original pbuf into our split size.  At this point our remainder segment must be setup
947   successfully because we are modifying the original segment */
948   pbuf_realloc(useg->p, useg->p->tot_len - remainder);
949   useg->len -= remainder;
950   TCPH_SET_FLAG(useg->tcphdr, split_flags);
951 #if TCP_OVERSIZE_DBGCHECK
952   /* By trimming, realloc may have actually shrunk the pbuf, so clear oversize_left */
953   useg->oversize_left = 0;
954 #endif /* TCP_OVERSIZE_DBGCHECK */
955 
956   /* Add back to the queue with new trimmed pbuf */
957   pcb->snd_queuelen += pbuf_clen(useg->p);
958 
959 #if TCP_CHECKSUM_ON_COPY
960   /* The checksum on the split segment is now incorrect. We need to re-run it over the split */
961   useg->chksum = 0;
962   useg->chksum_swapped = 0;
963   q = useg->p;
964   offset = q->tot_len - useg->len; /* Offset due to exposed headers */
965 
966   /* Advance to the pbuf where the offset ends */
967   while (q != NULL && offset > q->len) {
968     offset -= q->len;
969     q = q->next;
970   }
971   LWIP_ASSERT("Found start of payload pbuf", q != NULL);
972   /* Checksum the first payload pbuf accounting for offset, then other pbufs are all payload */
973   for (; q != NULL; offset = 0, q = q->next) {
974     tcp_seg_add_chksum(~inet_chksum((const u8_t *)q->payload + offset, q->len - offset), q->len - offset,
975                        &useg->chksum, &useg->chksum_swapped);
976   }
977 #endif /* TCP_CHECKSUM_ON_COPY */
978 
979   /* Update number of segments on the queues. Note that length now may
980    * exceed TCP_SND_QUEUELEN! We don't have to touch pcb->snd_buf
981    * because the total amount of data is constant when packet is split */
982   pcb->snd_queuelen += pbuf_clen(seg->p);
983 
984   /* Finally insert remainder into queue after split (which stays head) */
985   seg->next = useg->next;
986   useg->next = seg;
987 
988 #if TCP_OVERSIZE
989   /* If remainder is last segment on the unsent, ensure we clear the oversize amount
990    * because the remainder is always sized to the exact remaining amount */
991   if (seg->next == NULL) {
992     pcb->unsent_oversize = 0;
993   }
994 #endif /* TCP_OVERSIZE */
995 
996   return ERR_OK;
997 memerr:
998   TCP_STATS_INC(tcp.memerr);
999 
1000   LWIP_ASSERT("seg == NULL", seg == NULL);
1001   if (p != NULL) {
1002     pbuf_free(p);
1003   }
1004 
1005   return ERR_MEM;
1006 }
1007 
1008 /**
1009  * Called by tcp_close() to send a segment including FIN flag but not data.
1010  * This FIN may be added to an existing segment or a new, otherwise empty
1011  * segment is enqueued.
1012  *
1013  * @param pcb the tcp_pcb over which to send a segment
1014  * @return ERR_OK if sent, another err_t otherwise
1015  */
1016 err_t
tcp_send_fin(struct tcp_pcb * pcb)1017 tcp_send_fin(struct tcp_pcb *pcb)
1018 {
1019   LWIP_ASSERT("tcp_send_fin: invalid pcb", pcb != NULL);
1020 
1021   /* first, try to add the fin to the last unsent segment */
1022   if (pcb->unsent != NULL) {
1023     struct tcp_seg *last_unsent;
1024     for (last_unsent = pcb->unsent; last_unsent->next != NULL;
1025          last_unsent = last_unsent->next);
1026 
1027     if ((TCPH_FLAGS(last_unsent->tcphdr) & (TCP_SYN | TCP_FIN | TCP_RST)) == 0) {
1028       /* no SYN/FIN/RST flag in the header, we can add the FIN flag */
1029       TCPH_SET_FLAG(last_unsent->tcphdr, TCP_FIN);
1030       tcp_set_flags(pcb, TF_FIN);
1031       return ERR_OK;
1032     }
1033   }
1034   /* no data, no length, flags, copy=1, no optdata */
1035   return tcp_enqueue_flags(pcb, TCP_FIN);
1036 }
1037 
1038 /**
1039  * Enqueue SYN or FIN for transmission.
1040  *
1041  * Called by @ref tcp_connect, tcp_listen_input, and @ref tcp_close
1042  * (via @ref tcp_send_fin)
1043  *
1044  * @param pcb Protocol control block for the TCP connection.
1045  * @param flags TCP header flags to set in the outgoing segment.
1046  */
1047 err_t
tcp_enqueue_flags(struct tcp_pcb * pcb,u8_t flags)1048 tcp_enqueue_flags(struct tcp_pcb *pcb, u8_t flags)
1049 {
1050   struct pbuf *p;
1051   struct tcp_seg *seg;
1052   u8_t optflags = 0;
1053   u8_t optlen = 0;
1054 
1055   LWIP_ASSERT("tcp_enqueue_flags: need either TCP_SYN or TCP_FIN in flags (programmer violates API)",
1056               (flags & (TCP_SYN | TCP_FIN)) != 0);
1057   LWIP_ASSERT("tcp_enqueue_flags: invalid pcb", pcb != NULL);
1058 
1059   LWIP_DEBUGF(TCP_QLEN_DEBUG, ("tcp_enqueue_flags: queuelen: %"U16_F"\n", (u16_t)pcb->snd_queuelen));
1060 
1061   /* No need to check pcb->snd_queuelen if only SYN or FIN are allowed! */
1062 
1063   /* Get options for this segment. This is a special case since this is the
1064      only place where a SYN can be sent. */
1065   if (flags & TCP_SYN) {
1066     optflags = TF_SEG_OPTS_MSS;
1067 #if LWIP_WND_SCALE
1068     if ((pcb->state != SYN_RCVD) || (pcb->flags & TF_WND_SCALE)) {
1069       /* In a <SYN,ACK> (sent in state SYN_RCVD), the window scale option may only
1070          be sent if we received a window scale option from the remote host. */
1071       optflags |= TF_SEG_OPTS_WND_SCALE;
1072     }
1073 #endif /* LWIP_WND_SCALE */
1074 #if LWIP_TCP_SACK_OUT
1075     if ((pcb->state != SYN_RCVD) || (pcb->flags & TF_SACK)) {
1076       /* In a <SYN,ACK> (sent in state SYN_RCVD), the SACK_PERM option may only
1077          be sent if we received a SACK_PERM option from the remote host. */
1078       optflags |= TF_SEG_OPTS_SACK_PERM;
1079     }
1080 #endif /* LWIP_TCP_SACK_OUT */
1081   }
1082 #if LWIP_TCP_TIMESTAMPS
1083   if ((pcb->flags & TF_TIMESTAMP) || ((flags & TCP_SYN) && (pcb->state != SYN_RCVD))) {
1084     /* Make sure the timestamp option is only included in data segments if we
1085        agreed about it with the remote host (and in active open SYN segments). */
1086     optflags |= TF_SEG_OPTS_TS;
1087   }
1088 #endif /* LWIP_TCP_TIMESTAMPS */
1089   optlen = LWIP_TCP_OPT_LENGTH_SEGMENT(optflags, pcb);
1090 
1091   /* Allocate pbuf with room for TCP header + options */
1092   if ((p = pbuf_alloc(PBUF_TRANSPORT, optlen, PBUF_RAM)) == NULL) {
1093     tcp_set_flags(pcb, TF_NAGLEMEMERR);
1094     TCP_STATS_INC(tcp.memerr);
1095     return ERR_MEM;
1096   }
1097   LWIP_ASSERT("tcp_enqueue_flags: check that first pbuf can hold optlen",
1098               (p->len >= optlen));
1099 
1100   /* Allocate memory for tcp_seg, and fill in fields. */
1101   if ((seg = tcp_create_segment(pcb, p, flags, pcb->snd_lbb, optflags)) == NULL) {
1102     tcp_set_flags(pcb, TF_NAGLEMEMERR);
1103     TCP_STATS_INC(tcp.memerr);
1104     return ERR_MEM;
1105   }
1106   LWIP_ASSERT("seg->tcphdr not aligned", ((mem_ptr_t)seg->tcphdr % LWIP_MIN(MEM_ALIGNMENT, 4)) == 0);
1107   LWIP_ASSERT("tcp_enqueue_flags: invalid segment length", seg->len == 0);
1108 
1109   LWIP_DEBUGF(TCP_OUTPUT_DEBUG | LWIP_DBG_TRACE,
1110               ("tcp_enqueue_flags: queueing %"U32_F":%"U32_F" (0x%"X16_F")\n",
1111                lwip_ntohl(seg->tcphdr->seqno),
1112                lwip_ntohl(seg->tcphdr->seqno) + TCP_TCPLEN(seg),
1113                (u16_t)flags));
1114 
1115   /* Now append seg to pcb->unsent queue */
1116   if (pcb->unsent == NULL) {
1117     pcb->unsent = seg;
1118   } else {
1119     struct tcp_seg *useg;
1120     for (useg = pcb->unsent; useg->next != NULL; useg = useg->next);
1121     useg->next = seg;
1122   }
1123 #if TCP_OVERSIZE
1124   /* The new unsent tail has no space */
1125   pcb->unsent_oversize = 0;
1126 #endif /* TCP_OVERSIZE */
1127 
1128   /* SYN and FIN bump the sequence number */
1129   if ((flags & TCP_SYN) || (flags & TCP_FIN)) {
1130     pcb->snd_lbb++;
1131     /* optlen does not influence snd_buf */
1132   }
1133   if (flags & TCP_FIN) {
1134     tcp_set_flags(pcb, TF_FIN);
1135   }
1136 
1137   /* update number of segments on the queues */
1138   pcb->snd_queuelen += pbuf_clen(seg->p);
1139   LWIP_DEBUGF(TCP_QLEN_DEBUG, ("tcp_enqueue_flags: %"S16_F" (after enqueued)\n", pcb->snd_queuelen));
1140   if (pcb->snd_queuelen != 0) {
1141     LWIP_ASSERT("tcp_enqueue_flags: invalid queue length",
1142                 pcb->unacked != NULL || pcb->unsent != NULL);
1143   }
1144 
1145   return ERR_OK;
1146 }
1147 
1148 #if LWIP_TCP_TIMESTAMPS
1149 /* Build a timestamp option (12 bytes long) at the specified options pointer)
1150  *
1151  * @param pcb tcp_pcb
1152  * @param opts option pointer where to store the timestamp option
1153  */
1154 static void
tcp_build_timestamp_option(const struct tcp_pcb * pcb,u32_t * opts)1155 tcp_build_timestamp_option(const struct tcp_pcb *pcb, u32_t *opts)
1156 {
1157   LWIP_ASSERT("tcp_build_timestamp_option: invalid pcb", pcb != NULL);
1158 
1159   /* Pad with two NOP options to make everything nicely aligned */
1160   opts[0] = PP_HTONL(0x0101080A);
1161   opts[1] = lwip_htonl(sys_now());
1162   opts[2] = lwip_htonl(pcb->ts_recent);
1163 }
1164 #endif
1165 
1166 #if LWIP_TCP_SACK_OUT
1167 /**
1168  * Calculates the number of SACK entries that should be generated.
1169  * It takes into account whether TF_SACK flag is set,
1170  * the number of SACK entries in tcp_pcb that are valid,
1171  * as well as the available options size.
1172  *
1173  * @param pcb tcp_pcb
1174  * @param optlen the length of other TCP options (in bytes)
1175  * @return the number of SACK ranges that can be used
1176  */
1177 static u8_t
tcp_get_num_sacks(const struct tcp_pcb * pcb,u8_t optlen)1178 tcp_get_num_sacks(const struct tcp_pcb *pcb, u8_t optlen)
1179 {
1180   u8_t num_sacks = 0;
1181 
1182   LWIP_ASSERT("tcp_get_num_sacks: invalid pcb", pcb != NULL);
1183 
1184   if (pcb->flags & TF_SACK) {
1185     u8_t i;
1186 
1187     /* The first SACK takes up 12 bytes (it includes SACK header and two NOP options),
1188        each additional one - 8 bytes. */
1189     optlen += 12;
1190 
1191     /* Max options size = 40, number of SACK array entries = LWIP_TCP_MAX_SACK_NUM */
1192     for (i = 0; (i < LWIP_TCP_MAX_SACK_NUM) && (optlen <= TCP_MAX_OPTION_BYTES) &&
1193          LWIP_TCP_SACK_VALID(pcb, i); ++i) {
1194       ++num_sacks;
1195       optlen += 8;
1196     }
1197   }
1198 
1199   return num_sacks;
1200 }
1201 
1202 /** Build a SACK option (12 or more bytes long) at the specified options pointer)
1203  *
1204  * @param pcb tcp_pcb
1205  * @param opts option pointer where to store the SACK option
1206  * @param num_sacks the number of SACKs to store
1207  */
1208 static void
tcp_build_sack_option(const struct tcp_pcb * pcb,u32_t * opts,u8_t num_sacks)1209 tcp_build_sack_option(const struct tcp_pcb *pcb, u32_t *opts, u8_t num_sacks)
1210 {
1211   u8_t i;
1212 
1213   LWIP_ASSERT("tcp_build_sack_option: invalid pcb", pcb != NULL);
1214   LWIP_ASSERT("tcp_build_sack_option: invalid opts", opts != NULL);
1215 
1216   /* Pad with two NOP options to make everything nicely aligned.
1217      We add the length (of just the SACK option, not the NOPs in front of it),
1218      which is 2B of header, plus 8B for each SACK. */
1219   *(opts++) = PP_HTONL(0x01010500 + 2 + num_sacks * 8);
1220 
1221   for (i = 0; i < num_sacks; ++i) {
1222     *(opts++) = lwip_htonl(pcb->rcv_sacks[i].left);
1223     *(opts++) = lwip_htonl(pcb->rcv_sacks[i].right);
1224   }
1225 }
1226 
1227 #endif
1228 
1229 #if LWIP_WND_SCALE
1230 /** Build a window scale option (3 bytes long) at the specified options pointer)
1231  *
1232  * @param opts option pointer where to store the window scale option
1233  */
1234 static void
tcp_build_wnd_scale_option(u32_t * opts)1235 tcp_build_wnd_scale_option(u32_t *opts)
1236 {
1237   LWIP_ASSERT("tcp_build_wnd_scale_option: invalid opts", opts != NULL);
1238 
1239   /* Pad with one NOP option to make everything nicely aligned */
1240   opts[0] = PP_HTONL(0x01030300 | TCP_RCV_SCALE);
1241 }
1242 #endif
1243 
1244 /**
1245  * @ingroup tcp_raw
1246  * Find out what we can send and send it
1247  *
1248  * @param pcb Protocol control block for the TCP connection to send data
1249  * @return ERR_OK if data has been sent or nothing to send
1250  *         another err_t on error
1251  */
1252 err_t
tcp_output(struct tcp_pcb * pcb)1253 tcp_output(struct tcp_pcb *pcb)
1254 {
1255   struct tcp_seg *seg, *useg;
1256   u32_t wnd, snd_nxt;
1257   err_t err;
1258   struct netif *netif;
1259 #if TCP_CWND_DEBUG
1260   s16_t i = 0;
1261 #endif /* TCP_CWND_DEBUG */
1262 
1263   LWIP_ASSERT_CORE_LOCKED();
1264 
1265   LWIP_ASSERT("tcp_output: invalid pcb", pcb != NULL);
1266   /* pcb->state LISTEN not allowed here */
1267   LWIP_ASSERT("don't call tcp_output for listen-pcbs",
1268               pcb->state != LISTEN);
1269 
1270   /* First, check if we are invoked by the TCP input processing
1271      code. If so, we do not output anything. Instead, we rely on the
1272      input processing code to call us when input processing is done
1273      with. */
1274   if (tcp_input_pcb == pcb) {
1275     return ERR_OK;
1276   }
1277 
1278   wnd = LWIP_MIN(pcb->snd_wnd, pcb->cwnd);
1279 
1280   seg = pcb->unsent;
1281 
1282   if (seg == NULL) {
1283     LWIP_DEBUGF(TCP_OUTPUT_DEBUG, ("tcp_output: nothing to send (%p)\n",
1284                                    (void *)pcb->unsent));
1285     LWIP_DEBUGF(TCP_CWND_DEBUG, ("tcp_output: snd_wnd %"TCPWNDSIZE_F
1286                                  ", cwnd %"TCPWNDSIZE_F", wnd %"U32_F
1287                                  ", seg == NULL, ack %"U32_F"\n",
1288                                  pcb->snd_wnd, pcb->cwnd, wnd, pcb->lastack));
1289 
1290     /* If the TF_ACK_NOW flag is set and the ->unsent queue is empty, construct
1291      * an empty ACK segment and send it. */
1292     if (pcb->flags & TF_ACK_NOW) {
1293       return tcp_send_empty_ack(pcb);
1294     }
1295     /* nothing to send: shortcut out of here */
1296     goto output_done;
1297   } else {
1298     LWIP_DEBUGF(TCP_CWND_DEBUG,
1299                 ("tcp_output: snd_wnd %"TCPWNDSIZE_F", cwnd %"TCPWNDSIZE_F", wnd %"U32_F
1300                  ", effwnd %"U32_F", seq %"U32_F", ack %"U32_F"\n",
1301                  pcb->snd_wnd, pcb->cwnd, wnd,
1302                  lwip_ntohl(seg->tcphdr->seqno) - pcb->lastack + seg->len,
1303                  lwip_ntohl(seg->tcphdr->seqno), pcb->lastack));
1304   }
1305 
1306   netif = tcp_route(pcb, &pcb->local_ip, &pcb->remote_ip);
1307   if (netif == NULL) {
1308     return ERR_RTE;
1309   }
1310 
1311   /* If we don't have a local IP address, we get one from netif */
1312   if (ip_addr_isany(&pcb->local_ip)) {
1313     const ip_addr_t *local_ip = ip_netif_get_local_ip(netif, &pcb->remote_ip);
1314     if (local_ip == NULL) {
1315       return ERR_RTE;
1316     }
1317     ip_addr_copy(pcb->local_ip, *local_ip);
1318   }
1319 
1320   /* Handle the current segment not fitting within the window */
1321   if (lwip_ntohl(seg->tcphdr->seqno) - pcb->lastack + seg->len > wnd) {
1322     /* We need to start the persistent timer when the next unsent segment does not fit
1323      * within the remaining (could be 0) send window and RTO timer is not running (we
1324      * have no in-flight data). If window is still too small after persist timer fires,
1325      * then we split the segment. We don't consider the congestion window since a cwnd
1326      * smaller than 1 SMSS implies in-flight data
1327      */
1328     if (wnd == pcb->snd_wnd && pcb->unacked == NULL && pcb->persist_backoff == 0) {
1329       pcb->persist_cnt = 0;
1330       pcb->persist_backoff = 1;
1331       pcb->persist_probe = 0;
1332     }
1333     /* We need an ACK, but can't send data now, so send an empty ACK */
1334     if (pcb->flags & TF_ACK_NOW) {
1335       return tcp_send_empty_ack(pcb);
1336     }
1337     goto output_done;
1338   }
1339   /* Stop persist timer, above conditions are not active */
1340   pcb->persist_backoff = 0;
1341 
1342   /* useg should point to last segment on unacked queue */
1343   useg = pcb->unacked;
1344   if (useg != NULL) {
1345     for (; useg->next != NULL; useg = useg->next);
1346   }
1347   /* data available and window allows it to be sent? */
1348   while (seg != NULL &&
1349          lwip_ntohl(seg->tcphdr->seqno) - pcb->lastack + seg->len <= wnd) {
1350     LWIP_ASSERT("RST not expected here!",
1351                 (TCPH_FLAGS(seg->tcphdr) & TCP_RST) == 0);
1352     /* Stop sending if the nagle algorithm would prevent it
1353      * Don't stop:
1354      * - if tcp_write had a memory error before (prevent delayed ACK timeout) or
1355      * - if FIN was already enqueued for this PCB (SYN is always alone in a segment -
1356      *   either seg->next != NULL or pcb->unacked == NULL;
1357      *   RST is no sent using tcp_write/tcp_output.
1358      */
1359     if ((tcp_do_output_nagle(pcb) == 0) &&
1360         ((pcb->flags & (TF_NAGLEMEMERR | TF_FIN)) == 0)) {
1361       break;
1362     }
1363 #if TCP_CWND_DEBUG
1364     LWIP_DEBUGF(TCP_CWND_DEBUG, ("tcp_output: snd_wnd %"TCPWNDSIZE_F", cwnd %"TCPWNDSIZE_F", wnd %"U32_F", effwnd %"U32_F", seq %"U32_F", ack %"U32_F", i %"S16_F"\n",
1365                                  pcb->snd_wnd, pcb->cwnd, wnd,
1366                                  lwip_ntohl(seg->tcphdr->seqno) + seg->len -
1367                                  pcb->lastack,
1368                                  lwip_ntohl(seg->tcphdr->seqno), pcb->lastack, i));
1369     ++i;
1370 #endif /* TCP_CWND_DEBUG */
1371 
1372     if (pcb->state != SYN_SENT) {
1373       TCPH_SET_FLAG(seg->tcphdr, TCP_ACK);
1374     }
1375 
1376     err = tcp_output_segment(seg, pcb, netif);
1377     if (err != ERR_OK) {
1378       /* segment could not be sent, for whatever reason */
1379       tcp_set_flags(pcb, TF_NAGLEMEMERR);
1380       return err;
1381     }
1382 #if TCP_OVERSIZE_DBGCHECK
1383     seg->oversize_left = 0;
1384 #endif /* TCP_OVERSIZE_DBGCHECK */
1385     pcb->unsent = seg->next;
1386     if (pcb->state != SYN_SENT) {
1387       tcp_clear_flags(pcb, TF_ACK_DELAY | TF_ACK_NOW);
1388     }
1389     snd_nxt = lwip_ntohl(seg->tcphdr->seqno) + TCP_TCPLEN(seg);
1390     if (TCP_SEQ_LT(pcb->snd_nxt, snd_nxt)) {
1391       pcb->snd_nxt = snd_nxt;
1392     }
1393     /* put segment on unacknowledged list if length > 0 */
1394     if (TCP_TCPLEN(seg) > 0) {
1395       seg->next = NULL;
1396       /* unacked list is empty? */
1397       if (pcb->unacked == NULL) {
1398         pcb->unacked = seg;
1399         useg = seg;
1400         /* unacked list is not empty? */
1401       } else {
1402         /* In the case of fast retransmit, the packet should not go to the tail
1403          * of the unacked queue, but rather somewhere before it. We need to check for
1404          * this case. -STJ Jul 27, 2004 */
1405         if (TCP_SEQ_LT(lwip_ntohl(seg->tcphdr->seqno), lwip_ntohl(useg->tcphdr->seqno))) {
1406           /* add segment to before tail of unacked list, keeping the list sorted */
1407           struct tcp_seg **cur_seg = &(pcb->unacked);
1408           while (*cur_seg &&
1409                  TCP_SEQ_LT(lwip_ntohl((*cur_seg)->tcphdr->seqno), lwip_ntohl(seg->tcphdr->seqno))) {
1410             cur_seg = &((*cur_seg)->next );
1411           }
1412           seg->next = (*cur_seg);
1413           (*cur_seg) = seg;
1414         } else {
1415           /* add segment to tail of unacked list */
1416           useg->next = seg;
1417           useg = useg->next;
1418         }
1419       }
1420       /* do not queue empty segments on the unacked list */
1421     } else {
1422       tcp_seg_free(seg);
1423     }
1424     seg = pcb->unsent;
1425   }
1426 #if TCP_OVERSIZE
1427   if (pcb->unsent == NULL) {
1428     /* last unsent has been removed, reset unsent_oversize */
1429     pcb->unsent_oversize = 0;
1430   }
1431 #endif /* TCP_OVERSIZE */
1432 
1433 output_done:
1434   tcp_clear_flags(pcb, TF_NAGLEMEMERR);
1435   return ERR_OK;
1436 }
1437 
1438 /** Check if a segment's pbufs are used by someone else than TCP.
1439  * This can happen on retransmission if the pbuf of this segment is still
1440  * referenced by the netif driver due to deferred transmission.
1441  * This is the case (only!) if someone down the TX call path called
1442  * pbuf_ref() on one of the pbufs!
1443  *
1444  * @arg seg the tcp segment to check
1445  * @return 1 if ref != 1, 0 if ref == 1
1446  */
1447 static int
tcp_output_segment_busy(const struct tcp_seg * seg)1448 tcp_output_segment_busy(const struct tcp_seg *seg)
1449 {
1450   LWIP_ASSERT("tcp_output_segment_busy: invalid seg", seg != NULL);
1451 
1452   /* We only need to check the first pbuf here:
1453      If a pbuf is queued for transmission, a driver calls pbuf_ref(),
1454      which only changes the ref count of the first pbuf */
1455   if (seg->p->ref != 1) {
1456     /* other reference found */
1457     return 1;
1458   }
1459   /* no other references found */
1460   return 0;
1461 }
1462 
1463 /**
1464  * Called by tcp_output() to actually send a TCP segment over IP.
1465  *
1466  * @param seg the tcp_seg to send
1467  * @param pcb the tcp_pcb for the TCP connection used to send the segment
1468  * @param netif the netif used to send the segment
1469  */
1470 static err_t
tcp_output_segment(struct tcp_seg * seg,struct tcp_pcb * pcb,struct netif * netif)1471 tcp_output_segment(struct tcp_seg *seg, struct tcp_pcb *pcb, struct netif *netif)
1472 {
1473   err_t err;
1474   u16_t len;
1475   u32_t *opts;
1476 #if TCP_CHECKSUM_ON_COPY
1477   int seg_chksum_was_swapped = 0;
1478 #endif
1479 
1480   LWIP_ASSERT("tcp_output_segment: invalid seg", seg != NULL);
1481   LWIP_ASSERT("tcp_output_segment: invalid pcb", pcb != NULL);
1482   LWIP_ASSERT("tcp_output_segment: invalid netif", netif != NULL);
1483 
1484   if (tcp_output_segment_busy(seg)) {
1485     /* This should not happen: rexmit functions should have checked this.
1486        However, since this function modifies p->len, we must not continue in this case. */
1487     LWIP_DEBUGF(TCP_RTO_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("tcp_output_segment: segment busy\n"));
1488     return ERR_OK;
1489   }
1490 
1491   /* The TCP header has already been constructed, but the ackno and
1492    wnd fields remain. */
1493   seg->tcphdr->ackno = lwip_htonl(pcb->rcv_nxt);
1494 
1495   /* advertise our receive window size in this TCP segment */
1496 #if LWIP_WND_SCALE
1497   if (seg->flags & TF_SEG_OPTS_WND_SCALE) {
1498     /* The Window field in a SYN segment itself (the only type where we send
1499        the window scale option) is never scaled. */
1500     seg->tcphdr->wnd = lwip_htons(TCPWND_MIN16(pcb->rcv_ann_wnd));
1501   } else
1502 #endif /* LWIP_WND_SCALE */
1503   {
1504     seg->tcphdr->wnd = lwip_htons(TCPWND_MIN16(RCV_WND_SCALE(pcb, pcb->rcv_ann_wnd)));
1505   }
1506 
1507   pcb->rcv_ann_right_edge = pcb->rcv_nxt + pcb->rcv_ann_wnd;
1508 
1509   /* Add any requested options.  NB MSS option is only set on SYN
1510      packets, so ignore it here */
1511   /* cast through void* to get rid of alignment warnings */
1512   opts = (u32_t *)(void *)(seg->tcphdr + 1);
1513   if (seg->flags & TF_SEG_OPTS_MSS) {
1514     u16_t mss;
1515 #if TCP_CALCULATE_EFF_SEND_MSS
1516     mss = tcp_eff_send_mss_netif(TCP_MSS, netif, &pcb->remote_ip);
1517 #else /* TCP_CALCULATE_EFF_SEND_MSS */
1518     mss = TCP_MSS;
1519 #endif /* TCP_CALCULATE_EFF_SEND_MSS */
1520     *opts = TCP_BUILD_MSS_OPTION(mss);
1521     opts += 1;
1522   }
1523 #if LWIP_TCP_TIMESTAMPS
1524   pcb->ts_lastacksent = pcb->rcv_nxt;
1525 
1526   if (seg->flags & TF_SEG_OPTS_TS) {
1527     tcp_build_timestamp_option(pcb, opts);
1528     opts += 3;
1529   }
1530 #endif
1531 #if LWIP_WND_SCALE
1532   if (seg->flags & TF_SEG_OPTS_WND_SCALE) {
1533     tcp_build_wnd_scale_option(opts);
1534     opts += 1;
1535   }
1536 #endif
1537 #if LWIP_TCP_SACK_OUT
1538   if (seg->flags & TF_SEG_OPTS_SACK_PERM) {
1539     /* Pad with two NOP options to make everything nicely aligned
1540      * NOTE: When we send both timestamp and SACK_PERM options,
1541      * we could use the first two NOPs before the timestamp to store SACK_PERM option,
1542      * but that would complicate the code.
1543      */
1544     *(opts++) = PP_HTONL(0x01010402);
1545   }
1546 #endif
1547 
1548   /* Set retransmission timer running if it is not currently enabled
1549      This must be set before checking the route. */
1550   if (pcb->rtime < 0) {
1551     pcb->rtime = 0;
1552   }
1553 
1554   if (pcb->rttest == 0) {
1555     pcb->rttest = tcp_ticks;
1556     pcb->rtseq = lwip_ntohl(seg->tcphdr->seqno);
1557 
1558     LWIP_DEBUGF(TCP_RTO_DEBUG, ("tcp_output_segment: rtseq %"U32_F"\n", pcb->rtseq));
1559   }
1560   LWIP_DEBUGF(TCP_OUTPUT_DEBUG, ("tcp_output_segment: %"U32_F":%"U32_F"\n",
1561                                  lwip_htonl(seg->tcphdr->seqno), lwip_htonl(seg->tcphdr->seqno) +
1562                                  seg->len));
1563 
1564   len = (u16_t)((u8_t *)seg->tcphdr - (u8_t *)seg->p->payload);
1565   if (len == 0) {
1566     /** Exclude retransmitted segments from this count. */
1567     MIB2_STATS_INC(mib2.tcpoutsegs);
1568   }
1569 
1570   seg->p->len -= len;
1571   seg->p->tot_len -= len;
1572 
1573   seg->p->payload = seg->tcphdr;
1574 
1575   seg->tcphdr->chksum = 0;
1576 
1577 #ifdef LWIP_HOOK_TCP_OUT_ADD_TCPOPTS
1578   opts = LWIP_HOOK_TCP_OUT_ADD_TCPOPTS(seg->p, seg->tcphdr, pcb, opts);
1579 #endif
1580   LWIP_ASSERT("options not filled", (u8_t *)opts == ((u8_t *)(seg->tcphdr + 1)) + LWIP_TCP_OPT_LENGTH_SEGMENT(seg->flags, pcb));
1581 
1582 #if CHECKSUM_GEN_TCP
1583   IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_TCP) {
1584 #if TCP_CHECKSUM_ON_COPY
1585     u32_t acc;
1586 #if TCP_CHECKSUM_ON_COPY_SANITY_CHECK
1587     u16_t chksum_slow = ip_chksum_pseudo(seg->p, IP_PROTO_TCP,
1588                                          seg->p->tot_len, &pcb->local_ip, &pcb->remote_ip);
1589 #endif /* TCP_CHECKSUM_ON_COPY_SANITY_CHECK */
1590     if ((seg->flags & TF_SEG_DATA_CHECKSUMMED) == 0) {
1591       LWIP_ASSERT("data included but not checksummed",
1592                   seg->p->tot_len == TCPH_HDRLEN_BYTES(seg->tcphdr));
1593     }
1594 
1595     /* rebuild TCP header checksum (TCP header changes for retransmissions!) */
1596     acc = ip_chksum_pseudo_partial(seg->p, IP_PROTO_TCP,
1597                                    seg->p->tot_len, TCPH_HDRLEN_BYTES(seg->tcphdr), &pcb->local_ip, &pcb->remote_ip);
1598     /* add payload checksum */
1599     if (seg->chksum_swapped) {
1600       seg_chksum_was_swapped = 1;
1601       seg->chksum = SWAP_BYTES_IN_WORD(seg->chksum);
1602       seg->chksum_swapped = 0;
1603     }
1604     acc = (u16_t)~acc + seg->chksum;
1605     seg->tcphdr->chksum = (u16_t)~FOLD_U32T(acc);
1606 #if TCP_CHECKSUM_ON_COPY_SANITY_CHECK
1607     if (chksum_slow != seg->tcphdr->chksum) {
1608       TCP_CHECKSUM_ON_COPY_SANITY_CHECK_FAIL(
1609         ("tcp_output_segment: calculated checksum is %"X16_F" instead of %"X16_F"\n",
1610          seg->tcphdr->chksum, chksum_slow));
1611       seg->tcphdr->chksum = chksum_slow;
1612     }
1613 #endif /* TCP_CHECKSUM_ON_COPY_SANITY_CHECK */
1614 #else /* TCP_CHECKSUM_ON_COPY */
1615     seg->tcphdr->chksum = ip_chksum_pseudo(seg->p, IP_PROTO_TCP,
1616                                            seg->p->tot_len, &pcb->local_ip, &pcb->remote_ip);
1617 #endif /* TCP_CHECKSUM_ON_COPY */
1618   }
1619 #endif /* CHECKSUM_GEN_TCP */
1620   TCP_STATS_INC(tcp.xmit);
1621 
1622   NETIF_SET_HINTS(netif, &(pcb->netif_hints));
1623   err = ip_output_if(seg->p, &pcb->local_ip, &pcb->remote_ip, pcb->ttl,
1624                      pcb->tos, IP_PROTO_TCP, netif);
1625   NETIF_RESET_HINTS(netif);
1626 
1627 #if TCP_CHECKSUM_ON_COPY
1628   if (seg_chksum_was_swapped) {
1629     /* if data is added to this segment later, chksum needs to be swapped,
1630        so restore this now */
1631     seg->chksum = SWAP_BYTES_IN_WORD(seg->chksum);
1632     seg->chksum_swapped = 1;
1633   }
1634 #endif
1635 
1636   return err;
1637 }
1638 
1639 /**
1640  * Requeue all unacked segments for retransmission
1641  *
1642  * Called by tcp_slowtmr() for slow retransmission.
1643  *
1644  * @param pcb the tcp_pcb for which to re-enqueue all unacked segments
1645  */
1646 err_t
tcp_rexmit_rto_prepare(struct tcp_pcb * pcb)1647 tcp_rexmit_rto_prepare(struct tcp_pcb *pcb)
1648 {
1649   struct tcp_seg *seg;
1650 
1651   LWIP_ASSERT("tcp_rexmit_rto_prepare: invalid pcb", pcb != NULL);
1652 
1653   if (pcb->unacked == NULL) {
1654     return ERR_VAL;
1655   }
1656 
1657   /* Move all unacked segments to the head of the unsent queue.
1658      However, give up if any of the unsent pbufs are still referenced by the
1659      netif driver due to deferred transmission. No point loading the link further
1660      if it is struggling to flush its buffered writes. */
1661   for (seg = pcb->unacked; seg->next != NULL; seg = seg->next) {
1662     if (tcp_output_segment_busy(seg)) {
1663       LWIP_DEBUGF(TCP_RTO_DEBUG, ("tcp_rexmit_rto: segment busy\n"));
1664       return ERR_VAL;
1665     }
1666   }
1667   if (tcp_output_segment_busy(seg)) {
1668     LWIP_DEBUGF(TCP_RTO_DEBUG, ("tcp_rexmit_rto: segment busy\n"));
1669     return ERR_VAL;
1670   }
1671   /* concatenate unsent queue after unacked queue */
1672   seg->next = pcb->unsent;
1673 #if TCP_OVERSIZE_DBGCHECK
1674   /* if last unsent changed, we need to update unsent_oversize */
1675   if (pcb->unsent == NULL) {
1676     pcb->unsent_oversize = seg->oversize_left;
1677   }
1678 #endif /* TCP_OVERSIZE_DBGCHECK */
1679   /* unsent queue is the concatenated queue (of unacked, unsent) */
1680   pcb->unsent = pcb->unacked;
1681   /* unacked queue is now empty */
1682   pcb->unacked = NULL;
1683 
1684   /* Mark RTO in-progress */
1685   tcp_set_flags(pcb, TF_RTO);
1686   /* Record the next byte following retransmit */
1687   pcb->rto_end = lwip_ntohl(seg->tcphdr->seqno) + TCP_TCPLEN(seg);
1688   /* Don't take any RTT measurements after retransmitting. */
1689   pcb->rttest = 0;
1690 
1691   return ERR_OK;
1692 }
1693 
1694 /**
1695  * Requeue all unacked segments for retransmission
1696  *
1697  * Called by tcp_slowtmr() for slow retransmission.
1698  *
1699  * @param pcb the tcp_pcb for which to re-enqueue all unacked segments
1700  */
1701 void
tcp_rexmit_rto_commit(struct tcp_pcb * pcb)1702 tcp_rexmit_rto_commit(struct tcp_pcb *pcb)
1703 {
1704   LWIP_ASSERT("tcp_rexmit_rto_commit: invalid pcb", pcb != NULL);
1705 
1706   /* increment number of retransmissions */
1707   if (pcb->nrtx < 0xFF) {
1708     ++pcb->nrtx;
1709   }
1710   /* Do the actual retransmission */
1711   tcp_output(pcb);
1712 }
1713 
1714 /**
1715  * Requeue all unacked segments for retransmission
1716  *
1717  * Called by tcp_process() only, tcp_slowtmr() needs to do some things between
1718  * "prepare" and "commit".
1719  *
1720  * @param pcb the tcp_pcb for which to re-enqueue all unacked segments
1721  */
1722 void
tcp_rexmit_rto(struct tcp_pcb * pcb)1723 tcp_rexmit_rto(struct tcp_pcb *pcb)
1724 {
1725   LWIP_ASSERT("tcp_rexmit_rto: invalid pcb", pcb != NULL);
1726 
1727   if (tcp_rexmit_rto_prepare(pcb) == ERR_OK) {
1728     tcp_rexmit_rto_commit(pcb);
1729   }
1730 }
1731 
1732 /**
1733  * Requeue the first unacked segment for retransmission
1734  *
1735  * Called by tcp_receive() for fast retransmit.
1736  *
1737  * @param pcb the tcp_pcb for which to retransmit the first unacked segment
1738  */
1739 err_t
tcp_rexmit(struct tcp_pcb * pcb)1740 tcp_rexmit(struct tcp_pcb *pcb)
1741 {
1742   struct tcp_seg *seg;
1743   struct tcp_seg **cur_seg;
1744 
1745   LWIP_ASSERT("tcp_rexmit: invalid pcb", pcb != NULL);
1746 
1747   if (pcb->unacked == NULL) {
1748     return ERR_VAL;
1749   }
1750 
1751   seg = pcb->unacked;
1752 
1753   /* Give up if the segment is still referenced by the netif driver
1754      due to deferred transmission. */
1755   if (tcp_output_segment_busy(seg)) {
1756     LWIP_DEBUGF(TCP_RTO_DEBUG, ("tcp_rexmit busy\n"));
1757     return ERR_VAL;
1758   }
1759 
1760   /* Move the first unacked segment to the unsent queue */
1761   /* Keep the unsent queue sorted. */
1762   pcb->unacked = seg->next;
1763 
1764   cur_seg = &(pcb->unsent);
1765   while (*cur_seg &&
1766          TCP_SEQ_LT(lwip_ntohl((*cur_seg)->tcphdr->seqno), lwip_ntohl(seg->tcphdr->seqno))) {
1767     cur_seg = &((*cur_seg)->next );
1768   }
1769   seg->next = *cur_seg;
1770   *cur_seg = seg;
1771 #if TCP_OVERSIZE
1772   if (seg->next == NULL) {
1773     /* the retransmitted segment is last in unsent, so reset unsent_oversize */
1774     pcb->unsent_oversize = 0;
1775   }
1776 #endif /* TCP_OVERSIZE */
1777 
1778   if (pcb->nrtx < 0xFF) {
1779     ++pcb->nrtx;
1780   }
1781 
1782   /* Don't take any rtt measurements after retransmitting. */
1783   pcb->rttest = 0;
1784 
1785   /* Do the actual retransmission. */
1786   MIB2_STATS_INC(mib2.tcpretranssegs);
1787   /* No need to call tcp_output: we are always called from tcp_input()
1788      and thus tcp_output directly returns. */
1789   return ERR_OK;
1790 }
1791 
1792 
1793 /**
1794  * Handle retransmission after three dupacks received
1795  *
1796  * @param pcb the tcp_pcb for which to retransmit the first unacked segment
1797  */
1798 void
tcp_rexmit_fast(struct tcp_pcb * pcb)1799 tcp_rexmit_fast(struct tcp_pcb *pcb)
1800 {
1801   LWIP_ASSERT("tcp_rexmit_fast: invalid pcb", pcb != NULL);
1802 
1803   if (pcb->unacked != NULL && !(pcb->flags & TF_INFR)) {
1804     /* This is fast retransmit. Retransmit the first unacked segment. */
1805     LWIP_DEBUGF(TCP_FR_DEBUG,
1806                 ("tcp_receive: dupacks %"U16_F" (%"U32_F
1807                  "), fast retransmit %"U32_F"\n",
1808                  (u16_t)pcb->dupacks, pcb->lastack,
1809                  lwip_ntohl(pcb->unacked->tcphdr->seqno)));
1810     if (tcp_rexmit(pcb) == ERR_OK) {
1811       /* Set ssthresh to half of the minimum of the current
1812        * cwnd and the advertised window */
1813       pcb->ssthresh = LWIP_MIN(pcb->cwnd, pcb->snd_wnd) / 2;
1814 
1815       /* The minimum value for ssthresh should be 2 MSS */
1816       if (pcb->ssthresh < (2U * pcb->mss)) {
1817         LWIP_DEBUGF(TCP_FR_DEBUG,
1818                     ("tcp_receive: The minimum value for ssthresh %"TCPWNDSIZE_F
1819                      " should be min 2 mss %"U16_F"...\n",
1820                      pcb->ssthresh, (u16_t)(2 * pcb->mss)));
1821         pcb->ssthresh = 2 * pcb->mss;
1822       }
1823 
1824       pcb->cwnd = pcb->ssthresh + 3 * pcb->mss;
1825       tcp_set_flags(pcb, TF_INFR);
1826 
1827       /* Reset the retransmission timer to prevent immediate rto retransmissions */
1828       pcb->rtime = 0;
1829     }
1830   }
1831 }
1832 
1833 static struct pbuf *
tcp_output_alloc_header_common(u32_t ackno,u16_t optlen,u16_t datalen,u32_t seqno_be,u16_t src_port,u16_t dst_port,u8_t flags,u16_t wnd)1834 tcp_output_alloc_header_common(u32_t ackno, u16_t optlen, u16_t datalen,
1835                         u32_t seqno_be /* already in network byte order */,
1836                         u16_t src_port, u16_t dst_port, u8_t flags, u16_t wnd)
1837 {
1838   struct tcp_hdr *tcphdr;
1839   struct pbuf *p;
1840 
1841   p = pbuf_alloc(PBUF_IP, TCP_HLEN + optlen + datalen, PBUF_RAM);
1842   if (p != NULL) {
1843     LWIP_ASSERT("check that first pbuf can hold struct tcp_hdr",
1844                 (p->len >= TCP_HLEN + optlen));
1845     tcphdr = (struct tcp_hdr *)p->payload;
1846     tcphdr->src = lwip_htons(src_port);
1847     tcphdr->dest = lwip_htons(dst_port);
1848     tcphdr->seqno = seqno_be;
1849     tcphdr->ackno = lwip_htonl(ackno);
1850     TCPH_HDRLEN_FLAGS_SET(tcphdr, (5 + optlen / 4), flags);
1851     tcphdr->wnd = lwip_htons(wnd);
1852     tcphdr->chksum = 0;
1853     tcphdr->urgp = 0;
1854   }
1855   return p;
1856 }
1857 
1858 /** Allocate a pbuf and create a tcphdr at p->payload, used for output
1859  * functions other than the default tcp_output -> tcp_output_segment
1860  * (e.g. tcp_send_empty_ack, etc.)
1861  *
1862  * @param pcb tcp pcb for which to send a packet (used to initialize tcp_hdr)
1863  * @param optlen length of header-options
1864  * @param datalen length of tcp data to reserve in pbuf
1865  * @param seqno_be seqno in network byte order (big-endian)
1866  * @return pbuf with p->payload being the tcp_hdr
1867  */
1868 static struct pbuf *
tcp_output_alloc_header(struct tcp_pcb * pcb,u16_t optlen,u16_t datalen,u32_t seqno_be)1869 tcp_output_alloc_header(struct tcp_pcb *pcb, u16_t optlen, u16_t datalen,
1870                         u32_t seqno_be /* already in network byte order */)
1871 {
1872   struct pbuf *p;
1873 
1874   LWIP_ASSERT("tcp_output_alloc_header: invalid pcb", pcb != NULL);
1875 
1876   p = tcp_output_alloc_header_common(pcb->rcv_nxt, optlen, datalen,
1877     seqno_be, pcb->local_port, pcb->remote_port, TCP_ACK,
1878     TCPWND_MIN16(RCV_WND_SCALE(pcb, pcb->rcv_ann_wnd)));
1879   if (p != NULL) {
1880     /* If we're sending a packet, update the announced right window edge */
1881     pcb->rcv_ann_right_edge = pcb->rcv_nxt + pcb->rcv_ann_wnd;
1882   }
1883   return p;
1884 }
1885 
1886 /* Fill in options for control segments */
1887 static void
tcp_output_fill_options(const struct tcp_pcb * pcb,struct pbuf * p,u8_t optflags,u8_t num_sacks)1888 tcp_output_fill_options(const struct tcp_pcb *pcb, struct pbuf *p, u8_t optflags, u8_t num_sacks)
1889 {
1890   struct tcp_hdr *tcphdr;
1891   u32_t *opts;
1892   u16_t sacks_len = 0;
1893 
1894   LWIP_ASSERT("tcp_output_fill_options: invalid pbuf", p != NULL);
1895 
1896   tcphdr = (struct tcp_hdr *)p->payload;
1897   opts = (u32_t *)(void *)(tcphdr + 1);
1898 
1899   /* NB. MSS and window scale options are only sent on SYNs, so ignore them here */
1900 
1901 #if LWIP_TCP_TIMESTAMPS
1902   if (optflags & TF_SEG_OPTS_TS) {
1903     tcp_build_timestamp_option(pcb, opts);
1904     opts += 3;
1905   }
1906 #endif
1907 
1908 #if LWIP_TCP_SACK_OUT
1909   if (pcb && (num_sacks > 0)) {
1910     tcp_build_sack_option(pcb, opts, num_sacks);
1911     /* 1 word for SACKs header (including 2xNOP), and 2 words for each SACK */
1912     sacks_len = 1 + num_sacks * 2;
1913     opts += sacks_len;
1914   }
1915 #else
1916   LWIP_UNUSED_ARG(num_sacks);
1917 #endif
1918 
1919 #ifdef LWIP_HOOK_TCP_OUT_ADD_TCPOPTS
1920   opts = LWIP_HOOK_TCP_OUT_ADD_TCPOPTS(p, tcphdr, pcb, opts);
1921 #endif
1922 
1923   LWIP_UNUSED_ARG(pcb);
1924   LWIP_UNUSED_ARG(sacks_len);
1925   LWIP_ASSERT("options not filled", (u8_t *)opts == ((u8_t *)(tcphdr + 1)) + sacks_len * 4 + LWIP_TCP_OPT_LENGTH_SEGMENT(optflags, pcb));
1926   LWIP_UNUSED_ARG(optflags); /* for LWIP_NOASSERT */
1927   LWIP_UNUSED_ARG(opts); /* for LWIP_NOASSERT */
1928 }
1929 
1930 /** Output a control segment pbuf to IP.
1931  *
1932  * Called from tcp_rst, tcp_send_empty_ack, tcp_keepalive and tcp_zero_window_probe,
1933  * this function combines selecting a netif for transmission, generating the tcp
1934  * header checksum and calling ip_output_if while handling netif hints and stats.
1935  */
1936 static err_t
tcp_output_control_segment(const struct tcp_pcb * pcb,struct pbuf * p,const ip_addr_t * src,const ip_addr_t * dst)1937 tcp_output_control_segment(const struct tcp_pcb *pcb, struct pbuf *p,
1938                            const ip_addr_t *src, const ip_addr_t *dst)
1939 {
1940   struct netif *netif;
1941 
1942   LWIP_ASSERT("tcp_output_control_segment: invalid pbuf", p != NULL);
1943 
1944   netif = tcp_route(pcb, src, dst);
1945   if (netif == NULL) {
1946     pbuf_free(p);
1947     return ERR_RTE;
1948   }
1949   return tcp_output_control_segment_netif(pcb, p, src, dst, netif);
1950 }
1951 
1952 /** Output a control segment pbuf to IP.
1953  *
1954  * Called instead of tcp_output_control_segment when we don't have a pcb but we
1955  * do know the interface to send to.
1956  */
1957 static err_t
tcp_output_control_segment_netif(const struct tcp_pcb * pcb,struct pbuf * p,const ip_addr_t * src,const ip_addr_t * dst,struct netif * netif)1958 tcp_output_control_segment_netif(const struct tcp_pcb *pcb, struct pbuf *p,
1959                                  const ip_addr_t *src, const ip_addr_t *dst,
1960                                  struct netif *netif)
1961 {
1962   err_t err;
1963   u8_t ttl, tos;
1964 
1965   LWIP_ASSERT("tcp_output_control_segment_netif: no netif given", netif != NULL);
1966 
1967 #if CHECKSUM_GEN_TCP
1968   IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_TCP) {
1969     struct tcp_hdr *tcphdr = (struct tcp_hdr *)p->payload;
1970     tcphdr->chksum = ip_chksum_pseudo(p, IP_PROTO_TCP, p->tot_len,
1971                                       src, dst);
1972   }
1973 #endif
1974   if (pcb != NULL) {
1975     NETIF_SET_HINTS(netif, LWIP_CONST_CAST(struct netif_hint*, &(pcb->netif_hints)));
1976     ttl = pcb->ttl;
1977     tos = pcb->tos;
1978   } else {
1979     /* Send output with hardcoded TTL/HL since we have no access to the pcb */
1980     ttl = TCP_TTL;
1981     tos = 0;
1982   }
1983   TCP_STATS_INC(tcp.xmit);
1984   err = ip_output_if(p, src, dst, ttl, tos, IP_PROTO_TCP, netif);
1985   NETIF_RESET_HINTS(netif);
1986 
1987   pbuf_free(p);
1988   return err;
1989 }
1990 
1991 static struct pbuf *
tcp_rst_common(const struct tcp_pcb * pcb,u32_t seqno,u32_t ackno,const ip_addr_t * local_ip,const ip_addr_t * remote_ip,u16_t local_port,u16_t remote_port)1992 tcp_rst_common(const struct tcp_pcb *pcb, u32_t seqno, u32_t ackno,
1993                const ip_addr_t *local_ip, const ip_addr_t *remote_ip,
1994                u16_t local_port, u16_t remote_port)
1995 {
1996   struct pbuf *p;
1997   u16_t wnd;
1998   u8_t optlen;
1999 
2000   LWIP_ASSERT("tcp_rst: invalid local_ip", local_ip != NULL);
2001   LWIP_ASSERT("tcp_rst: invalid remote_ip", remote_ip != NULL);
2002   /* these two are passed only for checks, disable warnings without asserts */
2003   LWIP_UNUSED_ARG(local_ip);
2004   LWIP_UNUSED_ARG(remote_ip);
2005 
2006   optlen = LWIP_TCP_OPT_LENGTH_SEGMENT(0, pcb);
2007 
2008 #if LWIP_WND_SCALE
2009   wnd = PP_HTONS(((TCP_WND >> TCP_RCV_SCALE) & 0xFFFF));
2010 #else
2011   wnd = PP_HTONS(TCP_WND);
2012 #endif
2013 
2014   p = tcp_output_alloc_header_common(ackno, optlen, 0, lwip_htonl(seqno), local_port,
2015     remote_port, TCP_RST | TCP_ACK, wnd);
2016   if (p == NULL) {
2017     LWIP_DEBUGF(TCP_DEBUG, ("tcp_rst: could not allocate memory for pbuf\n"));
2018     return NULL;
2019   }
2020   tcp_output_fill_options(pcb, p, 0, 0);
2021 
2022   MIB2_STATS_INC(mib2.tcpoutrsts);
2023 
2024   LWIP_DEBUGF(TCP_RST_DEBUG, ("tcp_rst: seqno %"U32_F" ackno %"U32_F".\n", seqno, ackno));
2025   return p;
2026 }
2027 
2028 /**
2029  * Send a TCP RESET packet (empty segment with RST flag set) to abort a
2030  * connection.
2031  *
2032  * Called by tcp_abort() (to abort a local connection), tcp_closen() (if not
2033  * all data has been received by the application), tcp_timewait_input() (if a
2034  * SYN is received) and tcp_process() (received segment in the wrong state).
2035  *
2036  * Since a RST segment is in most cases not sent for an active connection,
2037  * tcp_rst() has a number of arguments that are taken from a tcp_pcb for
2038  * most other segment output functions.
2039  *
2040  * @param pcb TCP pcb (may be NULL if no pcb is available)
2041  * @param seqno the sequence number to use for the outgoing segment
2042  * @param ackno the acknowledge number to use for the outgoing segment
2043  * @param local_ip the local IP address to send the segment from
2044  * @param remote_ip the remote IP address to send the segment to
2045  * @param local_port the local TCP port to send the segment from
2046  * @param remote_port the remote TCP port to send the segment to
2047  */
2048 void
tcp_rst(const struct tcp_pcb * pcb,u32_t seqno,u32_t ackno,const ip_addr_t * local_ip,const ip_addr_t * remote_ip,u16_t local_port,u16_t remote_port)2049 tcp_rst(const struct tcp_pcb *pcb, u32_t seqno, u32_t ackno,
2050         const ip_addr_t *local_ip, const ip_addr_t *remote_ip,
2051         u16_t local_port, u16_t remote_port)
2052 {
2053   struct pbuf *p;
2054 
2055   p = tcp_rst_common(pcb, seqno, ackno, local_ip, remote_ip, local_port, remote_port);
2056   if (p != NULL) {
2057     tcp_output_control_segment(pcb, p, local_ip, remote_ip);
2058   }
2059 }
2060 
2061 /**
2062  * Send a TCP RESET packet (empty segment with RST flag set) to show that there
2063  * is no matching local connection for a received segment.
2064  *
2065  * Called by tcp_input() (if no matching local pcb was found) and
2066  * tcp_listen_input() (if incoming segment has ACK flag set).
2067  *
2068  * Since a RST segment is in most cases not sent for an active connection,
2069  * tcp_rst() has a number of arguments that are taken from a tcp_pcb for
2070  * most other segment output functions.
2071  *
2072  * @param netif the netif on which to send the RST (since we have no pcb)
2073  * @param seqno the sequence number to use for the outgoing segment
2074  * @param ackno the acknowledge number to use for the outgoing segment
2075  * @param local_ip the local IP address to send the segment from
2076  * @param remote_ip the remote IP address to send the segment to
2077  * @param local_port the local TCP port to send the segment from
2078  * @param remote_port the remote TCP port to send the segment to
2079  */
2080 void
tcp_rst_netif(struct netif * netif,u32_t seqno,u32_t ackno,const ip_addr_t * local_ip,const ip_addr_t * remote_ip,u16_t local_port,u16_t remote_port)2081 tcp_rst_netif(struct netif *netif, u32_t seqno, u32_t ackno,
2082               const ip_addr_t *local_ip, const ip_addr_t *remote_ip,
2083               u16_t local_port, u16_t remote_port)
2084 {
2085   if (netif) {
2086     struct pbuf *p = tcp_rst_common(NULL, seqno, ackno, local_ip, remote_ip, local_port, remote_port);
2087     if (p != NULL) {
2088       tcp_output_control_segment_netif(NULL, p, local_ip, remote_ip, netif);
2089     }
2090   } else {
2091     LWIP_DEBUGF(TCP_OUTPUT_DEBUG, ("tcp_rst_netif: no netif given\n"));
2092   }
2093 }
2094 
2095 /**
2096  * Send an ACK without data.
2097  *
2098  * @param pcb Protocol control block for the TCP connection to send the ACK
2099  */
2100 err_t
tcp_send_empty_ack(struct tcp_pcb * pcb)2101 tcp_send_empty_ack(struct tcp_pcb *pcb)
2102 {
2103   err_t err;
2104   struct pbuf *p;
2105   u8_t optlen, optflags = 0;
2106   u8_t num_sacks = 0;
2107 
2108   LWIP_ASSERT("tcp_send_empty_ack: invalid pcb", pcb != NULL);
2109 
2110 #if LWIP_TCP_TIMESTAMPS
2111   if (pcb->flags & TF_TIMESTAMP) {
2112     optflags = TF_SEG_OPTS_TS;
2113   }
2114 #endif
2115   optlen = LWIP_TCP_OPT_LENGTH_SEGMENT(optflags, pcb);
2116 
2117 #if LWIP_TCP_SACK_OUT
2118   /* For now, SACKs are only sent with empty ACKs */
2119   if ((num_sacks = tcp_get_num_sacks(pcb, optlen)) > 0) {
2120     optlen += 4 + num_sacks * 8; /* 4 bytes for header (including 2*NOP), plus 8B for each SACK */
2121   }
2122 #endif
2123 
2124   p = tcp_output_alloc_header(pcb, optlen, 0, lwip_htonl(pcb->snd_nxt));
2125   if (p == NULL) {
2126     /* let tcp_fasttmr retry sending this ACK */
2127     tcp_set_flags(pcb, TF_ACK_DELAY | TF_ACK_NOW);
2128     LWIP_DEBUGF(TCP_OUTPUT_DEBUG, ("tcp_output: (ACK) could not allocate pbuf\n"));
2129     return ERR_BUF;
2130   }
2131   tcp_output_fill_options(pcb, p, optflags, num_sacks);
2132 
2133 #if LWIP_TCP_TIMESTAMPS
2134   pcb->ts_lastacksent = pcb->rcv_nxt;
2135 #endif
2136 
2137   LWIP_DEBUGF(TCP_OUTPUT_DEBUG,
2138               ("tcp_output: sending ACK for %"U32_F"\n", pcb->rcv_nxt));
2139   err = tcp_output_control_segment(pcb, p, &pcb->local_ip, &pcb->remote_ip);
2140   if (err != ERR_OK) {
2141     /* let tcp_fasttmr retry sending this ACK */
2142     tcp_set_flags(pcb, TF_ACK_DELAY | TF_ACK_NOW);
2143   } else {
2144     /* remove ACK flags from the PCB, as we sent an empty ACK now */
2145     tcp_clear_flags(pcb, TF_ACK_DELAY | TF_ACK_NOW);
2146   }
2147 
2148   return err;
2149 }
2150 
2151 /**
2152  * Send keepalive packets to keep a connection active although
2153  * no data is sent over it.
2154  *
2155  * Called by tcp_slowtmr()
2156  *
2157  * @param pcb the tcp_pcb for which to send a keepalive packet
2158  */
2159 err_t
tcp_keepalive(struct tcp_pcb * pcb)2160 tcp_keepalive(struct tcp_pcb *pcb)
2161 {
2162   err_t err;
2163   struct pbuf *p;
2164   u8_t optlen = LWIP_TCP_OPT_LENGTH_SEGMENT(0, pcb);
2165 
2166   LWIP_ASSERT("tcp_keepalive: invalid pcb", pcb != NULL);
2167 
2168   LWIP_DEBUGF(TCP_DEBUG, ("tcp_keepalive: sending KEEPALIVE probe to "));
2169   ip_addr_debug_print_val(TCP_DEBUG, pcb->remote_ip);
2170   LWIP_DEBUGF(TCP_DEBUG, ("\n"));
2171 
2172   LWIP_DEBUGF(TCP_DEBUG, ("tcp_keepalive: tcp_ticks %"U32_F"   pcb->tmr %"U32_F" pcb->keep_cnt_sent %"U16_F"\n",
2173                           tcp_ticks, pcb->tmr, (u16_t)pcb->keep_cnt_sent));
2174 
2175   p = tcp_output_alloc_header(pcb, optlen, 0, lwip_htonl(pcb->snd_nxt - 1));
2176   if (p == NULL) {
2177     LWIP_DEBUGF(TCP_DEBUG,
2178                 ("tcp_keepalive: could not allocate memory for pbuf\n"));
2179     return ERR_MEM;
2180   }
2181   tcp_output_fill_options(pcb, p, 0, 0);
2182   err = tcp_output_control_segment(pcb, p, &pcb->local_ip, &pcb->remote_ip);
2183 
2184   LWIP_DEBUGF(TCP_DEBUG, ("tcp_keepalive: seqno %"U32_F" ackno %"U32_F" err %d.\n",
2185                           pcb->snd_nxt - 1, pcb->rcv_nxt, (int)err));
2186   return err;
2187 }
2188 
2189 /**
2190  * Send persist timer zero-window probes to keep a connection active
2191  * when a window update is lost.
2192  *
2193  * Called by tcp_slowtmr()
2194  *
2195  * @param pcb the tcp_pcb for which to send a zero-window probe packet
2196  */
2197 err_t
tcp_zero_window_probe(struct tcp_pcb * pcb)2198 tcp_zero_window_probe(struct tcp_pcb *pcb)
2199 {
2200   err_t err;
2201   struct pbuf *p;
2202   struct tcp_hdr *tcphdr;
2203   struct tcp_seg *seg;
2204   u16_t len;
2205   u8_t is_fin;
2206   u32_t snd_nxt;
2207   u8_t optlen = LWIP_TCP_OPT_LENGTH_SEGMENT(0, pcb);
2208 
2209   LWIP_ASSERT("tcp_zero_window_probe: invalid pcb", pcb != NULL);
2210 
2211   LWIP_DEBUGF(TCP_DEBUG, ("tcp_zero_window_probe: sending ZERO WINDOW probe to "));
2212   ip_addr_debug_print_val(TCP_DEBUG, pcb->remote_ip);
2213   LWIP_DEBUGF(TCP_DEBUG, ("\n"));
2214 
2215   LWIP_DEBUGF(TCP_DEBUG,
2216               ("tcp_zero_window_probe: tcp_ticks %"U32_F
2217                "   pcb->tmr %"U32_F" pcb->keep_cnt_sent %"U16_F"\n",
2218                tcp_ticks, pcb->tmr, (u16_t)pcb->keep_cnt_sent));
2219 
2220   /* Only consider unsent, persist timer should be off when there is data in-flight */
2221   seg = pcb->unsent;
2222   if (seg == NULL) {
2223     /* Not expected, persist timer should be off when the send buffer is empty */
2224     return ERR_OK;
2225   }
2226 
2227   /* increment probe count. NOTE: we record probe even if it fails
2228      to actually transmit due to an error. This ensures memory exhaustion/
2229      routing problem doesn't leave a zero-window pcb as an indefinite zombie.
2230      RTO mechanism has similar behavior, see pcb->nrtx */
2231   if (pcb->persist_probe < 0xFF) {
2232     ++pcb->persist_probe;
2233   }
2234 
2235   is_fin = ((TCPH_FLAGS(seg->tcphdr) & TCP_FIN) != 0) && (seg->len == 0);
2236   /* we want to send one seqno: either FIN or data (no options) */
2237   len = is_fin ? 0 : 1;
2238 
2239   p = tcp_output_alloc_header(pcb, optlen, len, seg->tcphdr->seqno);
2240   if (p == NULL) {
2241     LWIP_DEBUGF(TCP_DEBUG, ("tcp_zero_window_probe: no memory for pbuf\n"));
2242     return ERR_MEM;
2243   }
2244   tcphdr = (struct tcp_hdr *)p->payload;
2245 
2246   if (is_fin) {
2247     /* FIN segment, no data */
2248     TCPH_FLAGS_SET(tcphdr, TCP_ACK | TCP_FIN);
2249   } else {
2250     /* Data segment, copy in one byte from the head of the unacked queue */
2251     char *d = ((char *)p->payload + TCP_HLEN);
2252     /* Depending on whether the segment has already been sent (unacked) or not
2253        (unsent), seg->p->payload points to the IP header or TCP header.
2254        Ensure we copy the first TCP data byte: */
2255     pbuf_copy_partial(seg->p, d, 1, seg->p->tot_len - seg->len);
2256   }
2257 
2258   /* The byte may be acknowledged without the window being opened. */
2259   snd_nxt = lwip_ntohl(seg->tcphdr->seqno) + 1;
2260   if (TCP_SEQ_LT(pcb->snd_nxt, snd_nxt)) {
2261     pcb->snd_nxt = snd_nxt;
2262   }
2263   tcp_output_fill_options(pcb, p, 0, 0);
2264 
2265   err = tcp_output_control_segment(pcb, p, &pcb->local_ip, &pcb->remote_ip);
2266 
2267   LWIP_DEBUGF(TCP_DEBUG, ("tcp_zero_window_probe: seqno %"U32_F
2268                           " ackno %"U32_F" err %d.\n",
2269                           pcb->snd_nxt - 1, pcb->rcv_nxt, (int)err));
2270   return err;
2271 }
2272 #endif /* LWIP_TCP */
2273