1 /**
2  * @file
3  * Transmission Control Protocol, outgoing traffic
4  *
5  * The output functions of TCP.
6  *
7  */
8 
9 /*
10  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
11  * All rights reserved.
12  *
13  * Redistribution and use in source and binary forms, with or without modification,
14  * are permitted provided that the following conditions are met:
15  *
16  * 1. Redistributions of source code must retain the above copyright notice,
17  *    this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright notice,
19  *    this list of conditions and the following disclaimer in the documentation
20  *    and/or other materials provided with the distribution.
21  * 3. The name of the author may not be used to endorse or promote products
22  *    derived from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
25  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
27  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
29  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
32  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
33  * OF SUCH DAMAGE.
34  *
35  * This file is part of the lwIP TCP/IP stack.
36  *
37  * Author: Adam Dunkels <adam@sics.se>
38  *
39  */
40 
41 #include "lwip/opt.h"
42 
43 #if LWIP_TCP /* don't build if not configured for use in lwipopts.h */
44 
45 #include "lwip/priv/tcp_priv.h"
46 #include "lwip/def.h"
47 #include "lwip/mem.h"
48 #include "lwip/memp.h"
49 #include "lwip/ip_addr.h"
50 #include "lwip/netif.h"
51 #include "lwip/inet_chksum.h"
52 #include "lwip/stats.h"
53 #include "lwip/ip6.h"
54 #include "lwip/ip6_addr.h"
55 #if LWIP_TCP_TIMESTAMPS
56 #include "lwip/sys.h"
57 #endif
58 
59 #include <string.h>
60 
61 /* Define some copy-macros for checksum-on-copy so that the code looks
62    nicer by preventing too many ifdef's. */
63 #if TCP_CHECKSUM_ON_COPY
64 #define TCP_DATA_COPY(dst, src, len, seg) do { \
65   tcp_seg_add_chksum(LWIP_CHKSUM_COPY(dst, src, len), \
66                      len, &seg->chksum, &seg->chksum_swapped); \
67   seg->flags |= TF_SEG_DATA_CHECKSUMMED; } while(0)
68 #define TCP_DATA_COPY2(dst, src, len, chksum, chksum_swapped)  \
69   tcp_seg_add_chksum(LWIP_CHKSUM_COPY(dst, src, len), len, chksum, chksum_swapped);
70 #else /* TCP_CHECKSUM_ON_COPY*/
71 #define TCP_DATA_COPY(dst, src, len, seg)                     MEMCPY(dst, src, len)
72 #define TCP_DATA_COPY2(dst, src, len, chksum, chksum_swapped) MEMCPY(dst, src, len)
73 #endif /* TCP_CHECKSUM_ON_COPY*/
74 
75 /** Define this to 1 for an extra check that the output checksum is valid
76  * (usefule when the checksum is generated by the application, not the stack) */
77 #ifndef TCP_CHECKSUM_ON_COPY_SANITY_CHECK
78 #define TCP_CHECKSUM_ON_COPY_SANITY_CHECK   0
79 #endif
80 /* Allow to override the failure of sanity check from warning to e.g. hard failure */
81 #if TCP_CHECKSUM_ON_COPY_SANITY_CHECK
82 #ifndef TCP_CHECKSUM_ON_COPY_SANITY_CHECK_FAIL
83 #define TCP_CHECKSUM_ON_COPY_SANITY_CHECK_FAIL(msg) LWIP_DEBUGF(TCP_DEBUG | LWIP_DBG_LEVEL_WARNING, msg)
84 #endif
85 #endif
86 
87 #if TCP_OVERSIZE
88 /** The size of segment pbufs created when TCP_OVERSIZE is enabled */
89 #ifndef TCP_OVERSIZE_CALC_LENGTH
90 #define TCP_OVERSIZE_CALC_LENGTH(length) ((length) + TCP_OVERSIZE)
91 #endif
92 #endif
93 
94 /* Forward declarations.*/
95 static err_t tcp_output_segment(struct tcp_seg *seg, struct tcp_pcb *pcb, struct netif *netif);
96 
97 /** Allocate a pbuf and create a tcphdr at p->payload, used for output
98  * functions other than the default tcp_output -> tcp_output_segment
99  * (e.g. tcp_send_empty_ack, etc.)
100  *
101  * @param pcb tcp pcb for which to send a packet (used to initialize tcp_hdr)
102  * @param optlen length of header-options
103  * @param datalen length of tcp data to reserve in pbuf
104  * @param seqno_be seqno in network byte order (big-endian)
105  * @return pbuf with p->payload being the tcp_hdr
106  */
107 static struct pbuf *
tcp_output_alloc_header(struct tcp_pcb * pcb,u16_t optlen,u16_t datalen,u32_t seqno_be)108 tcp_output_alloc_header(struct tcp_pcb *pcb, u16_t optlen, u16_t datalen,
109                       u32_t seqno_be /* already in network byte order */)
110 {
111   struct tcp_hdr *tcphdr;
112   struct pbuf *p = pbuf_alloc(PBUF_IP, TCP_HLEN + optlen + datalen, PBUF_RAM);
113   if (p != NULL) {
114     LWIP_ASSERT("check that first pbuf can hold struct tcp_hdr",
115                  (p->len >= TCP_HLEN + optlen));
116     tcphdr = (struct tcp_hdr *)p->payload;
117     tcphdr->src = lwip_htons(pcb->local_port);
118     tcphdr->dest = lwip_htons(pcb->remote_port);
119     tcphdr->seqno = seqno_be;
120     tcphdr->ackno = lwip_htonl(pcb->rcv_nxt);
121     TCPH_HDRLEN_FLAGS_SET(tcphdr, (5 + optlen / 4), TCP_ACK);
122     tcphdr->wnd = lwip_htons(TCPWND_MIN16(RCV_WND_SCALE(pcb, pcb->rcv_ann_wnd)));
123     tcphdr->chksum = 0;
124     tcphdr->urgp = 0;
125 
126     /* If we're sending a packet, update the announced right window edge */
127     pcb->rcv_ann_right_edge = pcb->rcv_nxt + pcb->rcv_ann_wnd;
128   }
129   return p;
130 }
131 
132 /**
133  * Called by tcp_close() to send a segment including FIN flag but not data.
134  *
135  * @param pcb the tcp_pcb over which to send a segment
136  * @return ERR_OK if sent, another err_t otherwise
137  */
138 err_t
tcp_send_fin(struct tcp_pcb * pcb)139 tcp_send_fin(struct tcp_pcb *pcb)
140 {
141   /* first, try to add the fin to the last unsent segment */
142   if (pcb->unsent != NULL) {
143     struct tcp_seg *last_unsent;
144     for (last_unsent = pcb->unsent; last_unsent->next != NULL;
145          last_unsent = last_unsent->next);
146 
147     if ((TCPH_FLAGS(last_unsent->tcphdr) & (TCP_SYN | TCP_FIN | TCP_RST)) == 0) {
148       /* no SYN/FIN/RST flag in the header, we can add the FIN flag */
149       TCPH_SET_FLAG(last_unsent->tcphdr, TCP_FIN);
150       pcb->flags |= TF_FIN;
151       return ERR_OK;
152     }
153   }
154   /* no data, no length, flags, copy=1, no optdata */
155   return tcp_enqueue_flags(pcb, TCP_FIN);
156 }
157 
158 /**
159  * Create a TCP segment with prefilled header.
160  *
161  * Called by tcp_write and tcp_enqueue_flags.
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 flags 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(struct tcp_pcb * pcb,struct pbuf * p,u8_t flags,u32_t seqno,u8_t optflags)173 tcp_create_segment(struct tcp_pcb *pcb, struct pbuf *p, u8_t flags, u32_t seqno, u8_t optflags)
174 {
175   struct tcp_seg *seg;
176   u8_t optlen = LWIP_TCP_OPT_LENGTH(optflags);
177 
178   if ((seg = (struct tcp_seg *)memp_malloc(MEMP_TCP_SEG)) == NULL) {
179     LWIP_DEBUGF(TCP_OUTPUT_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("tcp_create_segment: no memory.\n"));
180     pbuf_free(p);
181     return NULL;
182   }
183   seg->flags = optflags;
184   seg->next = NULL;
185   seg->p = p;
186   LWIP_ASSERT("p->tot_len >= optlen", p->tot_len >= optlen);
187   seg->len = p->tot_len - optlen;
188 #if TCP_OVERSIZE_DBGCHECK
189   seg->oversize_left = 0;
190 #endif /* TCP_OVERSIZE_DBGCHECK */
191 #if TCP_CHECKSUM_ON_COPY
192   seg->chksum = 0;
193   seg->chksum_swapped = 0;
194   /* check optflags */
195   LWIP_ASSERT("invalid optflags passed: TF_SEG_DATA_CHECKSUMMED",
196               (optflags & TF_SEG_DATA_CHECKSUMMED) == 0);
197 #endif /* TCP_CHECKSUM_ON_COPY */
198 
199   /* build TCP header */
200   if (pbuf_header(p, TCP_HLEN)) {
201     LWIP_DEBUGF(TCP_OUTPUT_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("tcp_create_segment: no room for TCP header in pbuf.\n"));
202     TCP_STATS_INC(tcp.err);
203     tcp_seg_free(seg);
204     return NULL;
205   }
206   seg->tcphdr = (struct tcp_hdr *)seg->p->payload;
207   seg->tcphdr->src = lwip_htons(pcb->local_port);
208   seg->tcphdr->dest = lwip_htons(pcb->remote_port);
209   seg->tcphdr->seqno = lwip_htonl(seqno);
210   /* ackno is set in tcp_output */
211   TCPH_HDRLEN_FLAGS_SET(seg->tcphdr, (5 + optlen / 4), flags);
212   /* wnd and chksum are set in tcp_output */
213   seg->tcphdr->urgp = 0;
214   return seg;
215 }
216 
217 /**
218  * Allocate a PBUF_RAM pbuf, perhaps with extra space at the end.
219  *
220  * This function is like pbuf_alloc(layer, length, PBUF_RAM) except
221  * there may be extra bytes available at the end.
222  *
223  * @param layer flag to define header size.
224  * @param length size of the pbuf's payload.
225  * @param max_length maximum usable size of payload+oversize.
226  * @param oversize pointer to a u16_t that will receive the number of usable tail bytes.
227  * @param pcb The TCP connection that willo enqueue the pbuf.
228  * @param apiflags API flags given to tcp_write.
229  * @param first_seg true when this pbuf will be used in the first enqueued segment.
230  */
231 #if TCP_OVERSIZE
232 static struct pbuf *
tcp_pbuf_prealloc(pbuf_layer layer,u16_t length,u16_t max_length,u16_t * oversize,struct tcp_pcb * pcb,u8_t apiflags,u8_t first_seg)233 tcp_pbuf_prealloc(pbuf_layer layer, u16_t length, u16_t max_length,
234                   u16_t *oversize, struct tcp_pcb *pcb, u8_t apiflags,
235                   u8_t first_seg)
236 {
237   struct pbuf *p;
238   u16_t alloc = length;
239 
240 #if LWIP_NETIF_TX_SINGLE_PBUF
241   LWIP_UNUSED_ARG(max_length);
242   LWIP_UNUSED_ARG(pcb);
243   LWIP_UNUSED_ARG(apiflags);
244   LWIP_UNUSED_ARG(first_seg);
245   alloc = max_length;
246 #else /* LWIP_NETIF_TX_SINGLE_PBUF */
247   if (length < max_length) {
248     /* Should we allocate an oversized pbuf, or just the minimum
249      * length required? If tcp_write is going to be called again
250      * before this segment is transmitted, we want the oversized
251      * buffer. If the segment will be transmitted immediately, we can
252      * save memory by allocating only length. We use a simple
253      * heuristic based on the following information:
254      *
255      * Did the user set TCP_WRITE_FLAG_MORE?
256      *
257      * Will the Nagle algorithm defer transmission of this segment?
258      */
259     if ((apiflags & TCP_WRITE_FLAG_MORE) ||
260         (!(pcb->flags & TF_NODELAY) &&
261          (!first_seg ||
262           pcb->unsent != NULL ||
263           pcb->unacked != NULL))) {
264       alloc = LWIP_MIN(max_length, LWIP_MEM_ALIGN_SIZE(TCP_OVERSIZE_CALC_LENGTH(length)));
265     }
266   }
267 #endif /* LWIP_NETIF_TX_SINGLE_PBUF */
268   p = pbuf_alloc(layer, alloc, PBUF_RAM);
269   if (p == NULL) {
270     return NULL;
271   }
272   LWIP_ASSERT("need unchained pbuf", p->next == NULL);
273   *oversize = p->len - length;
274   /* trim p->len to the currently used size */
275   p->len = p->tot_len = length;
276   return p;
277 }
278 #else /* TCP_OVERSIZE */
279 #define tcp_pbuf_prealloc(layer, length, mx, os, pcb, api, fst) pbuf_alloc((layer), (length), PBUF_RAM)
280 #endif /* TCP_OVERSIZE */
281 
282 #if TCP_CHECKSUM_ON_COPY
283 /** Add a checksum of newly added data to the segment */
284 static void
tcp_seg_add_chksum(u16_t chksum,u16_t len,u16_t * seg_chksum,u8_t * seg_chksum_swapped)285 tcp_seg_add_chksum(u16_t chksum, u16_t len, u16_t *seg_chksum,
286                    u8_t *seg_chksum_swapped)
287 {
288   u32_t helper;
289   /* add chksum to old chksum and fold to u16_t */
290   helper = chksum + *seg_chksum;
291   chksum = FOLD_U32T(helper);
292   if ((len & 1) != 0) {
293     *seg_chksum_swapped = 1 - *seg_chksum_swapped;
294     chksum = SWAP_BYTES_IN_WORD(chksum);
295   }
296   *seg_chksum = chksum;
297 }
298 #endif /* TCP_CHECKSUM_ON_COPY */
299 
300 /** Checks if tcp_write is allowed or not (checks state, snd_buf and snd_queuelen).
301  *
302  * @param pcb the tcp pcb to check for
303  * @param len length of data to send (checked agains snd_buf)
304  * @return ERR_OK if tcp_write is allowed to proceed, another err_t otherwise
305  */
306 static err_t
tcp_write_checks(struct tcp_pcb * pcb,u16_t len)307 tcp_write_checks(struct tcp_pcb *pcb, u16_t len)
308 {
309   /* connection is in invalid state for data transmission? */
310   if ((pcb->state != ESTABLISHED) &&
311       (pcb->state != CLOSE_WAIT) &&
312       (pcb->state != SYN_SENT) &&
313       (pcb->state != SYN_RCVD)) {
314     LWIP_DEBUGF(TCP_OUTPUT_DEBUG | LWIP_DBG_STATE | LWIP_DBG_LEVEL_SEVERE, ("tcp_write() called in invalid state\n"));
315     return ERR_CONN;
316   } else if (len == 0) {
317     return ERR_OK;
318   }
319 
320   /* fail on too much data */
321   if (len > pcb->snd_buf) {
322     LWIP_DEBUGF(TCP_OUTPUT_DEBUG | LWIP_DBG_LEVEL_SEVERE, ("tcp_write: too much data (len=%"U16_F" > snd_buf=%"TCPWNDSIZE_F")\n",
323       len, pcb->snd_buf));
324     pcb->flags |= TF_NAGLEMEMERR;
325     return ERR_MEM;
326   }
327 
328   LWIP_DEBUGF(TCP_QLEN_DEBUG, ("tcp_write: queuelen: %"TCPWNDSIZE_F"\n", (tcpwnd_size_t)pcb->snd_queuelen));
329 
330   /* If total number of pbufs on the unsent/unacked queues exceeds the
331    * configured maximum, return an error */
332   /* check for configured max queuelen and possible overflow */
333   if ((pcb->snd_queuelen >= TCP_SND_QUEUELEN) || (pcb->snd_queuelen > TCP_SNDQUEUELEN_OVERFLOW)) {
334     LWIP_DEBUGF(TCP_OUTPUT_DEBUG | LWIP_DBG_LEVEL_SEVERE, ("tcp_write: too long queue %"U16_F" (max %"U16_F")\n",
335       pcb->snd_queuelen, (u16_t)TCP_SND_QUEUELEN));
336     TCP_STATS_INC(tcp.memerr);
337     pcb->flags |= TF_NAGLEMEMERR;
338     return ERR_MEM;
339   }
340   if (pcb->snd_queuelen != 0) {
341     LWIP_ASSERT("tcp_write: pbufs on queue => at least one queue non-empty",
342       pcb->unacked != NULL || pcb->unsent != NULL);
343   } else {
344     LWIP_ASSERT("tcp_write: no pbufs on queue => both queues empty",
345       pcb->unacked == NULL && pcb->unsent == NULL);
346   }
347   return ERR_OK;
348 }
349 
350 /**
351  * @ingroup tcp_raw
352  * Write data for sending (but does not send it immediately).
353  *
354  * It waits in the expectation of more data being sent soon (as
355  * it can send them more efficiently by combining them together).
356  * To prompt the system to send data now, call tcp_output() after
357  * calling tcp_write().
358  *
359  * @param pcb Protocol control block for the TCP connection to enqueue data for.
360  * @param arg Pointer to the data to be enqueued for sending.
361  * @param len Data length in bytes
362  * @param apiflags combination of following flags :
363  * - TCP_WRITE_FLAG_COPY (0x01) data will be copied into memory belonging to the stack
364  * - TCP_WRITE_FLAG_MORE (0x02) for TCP connection, PSH flag will not be set on last segment sent,
365  * @return ERR_OK if enqueued, another err_t on error
366  */
367 err_t
tcp_write(struct tcp_pcb * pcb,const void * arg,u16_t len,u8_t apiflags)368 tcp_write(struct tcp_pcb *pcb, const void *arg, u16_t len, u8_t apiflags)
369 {
370   struct pbuf *concat_p = NULL;
371   struct tcp_seg *last_unsent = NULL, *seg = NULL, *prev_seg = NULL, *queue = NULL;
372   u16_t pos = 0; /* position in 'arg' data */
373   u16_t queuelen;
374   u8_t optlen = 0;
375   u8_t optflags = 0;
376 #if TCP_OVERSIZE
377   u16_t oversize = 0;
378   u16_t oversize_used = 0;
379 #endif /* TCP_OVERSIZE */
380 #if TCP_CHECKSUM_ON_COPY
381   u16_t concat_chksum = 0;
382   u8_t concat_chksum_swapped = 0;
383   u16_t concat_chksummed = 0;
384 #endif /* TCP_CHECKSUM_ON_COPY */
385   err_t err;
386   /* don't allocate segments bigger than half the maximum window we ever received */
387   u16_t mss_local = LWIP_MIN(pcb->mss, TCPWND_MIN16(pcb->snd_wnd_max/2));
388   mss_local = mss_local ? mss_local : pcb->mss;
389 
390 #if LWIP_NETIF_TX_SINGLE_PBUF
391   /* Always copy to try to create single pbufs for TX */
392   apiflags |= TCP_WRITE_FLAG_COPY;
393 #endif /* LWIP_NETIF_TX_SINGLE_PBUF */
394 
395   LWIP_DEBUGF(TCP_OUTPUT_DEBUG, ("tcp_write(pcb=%p, data=%p, len=%"U16_F", apiflags=%"U16_F")\n",
396     (void *)pcb, arg, len, (u16_t)apiflags));
397   LWIP_ERROR("tcp_write: arg == NULL (programmer violates API)",
398              arg != NULL, return ERR_ARG;);
399 
400   err = tcp_write_checks(pcb, len);
401   if (err != ERR_OK) {
402     return err;
403   }
404   queuelen = pcb->snd_queuelen;
405 
406 #if LWIP_TCP_TIMESTAMPS
407   if ((pcb->flags & TF_TIMESTAMP)) {
408     /* Make sure the timestamp option is only included in data segments if we
409        agreed about it with the remote host. */
410     optflags = TF_SEG_OPTS_TS;
411     optlen = LWIP_TCP_OPT_LENGTH(TF_SEG_OPTS_TS);
412     /* ensure that segments can hold at least one data byte... */
413     mss_local = LWIP_MAX(mss_local, LWIP_TCP_OPT_LEN_TS + 1);
414   }
415 #endif /* LWIP_TCP_TIMESTAMPS */
416 
417 
418   /*
419    * TCP segmentation is done in three phases with increasing complexity:
420    *
421    * 1. Copy data directly into an oversized pbuf.
422    * 2. Chain a new pbuf to the end of pcb->unsent.
423    * 3. Create new segments.
424    *
425    * We may run out of memory at any point. In that case we must
426    * return ERR_MEM and not change anything in pcb. Therefore, all
427    * changes are recorded in local variables and committed at the end
428    * of the function. Some pcb fields are maintained in local copies:
429    *
430    * queuelen = pcb->snd_queuelen
431    * oversize = pcb->unsent_oversize
432    *
433    * These variables are set consistently by the phases:
434    *
435    * seg points to the last segment tampered with.
436    *
437    * pos records progress as data is segmented.
438    */
439 
440   /* Find the tail of the unsent queue. */
441   if (pcb->unsent != NULL) {
442     u16_t space;
443     u16_t unsent_optlen;
444 
445     /* @todo: this could be sped up by keeping last_unsent in the pcb */
446     for (last_unsent = pcb->unsent; last_unsent->next != NULL;
447          last_unsent = last_unsent->next);
448 
449     /* Usable space at the end of the last unsent segment */
450     unsent_optlen = LWIP_TCP_OPT_LENGTH(last_unsent->flags);
451     LWIP_ASSERT("mss_local is too small", mss_local >= last_unsent->len + unsent_optlen);
452     space = mss_local - (last_unsent->len + unsent_optlen);
453 
454     /*
455      * Phase 1: Copy data directly into an oversized pbuf.
456      *
457      * The number of bytes copied is recorded in the oversize_used
458      * variable. The actual copying is done at the bottom of the
459      * function.
460      */
461 #if TCP_OVERSIZE
462 #if TCP_OVERSIZE_DBGCHECK
463     /* check that pcb->unsent_oversize matches last_unsent->unsent_oversize */
464     LWIP_ASSERT("unsent_oversize mismatch (pcb vs. last_unsent)",
465                 pcb->unsent_oversize == last_unsent->oversize_left);
466 #endif /* TCP_OVERSIZE_DBGCHECK */
467     oversize = pcb->unsent_oversize;
468     if (oversize > 0) {
469       LWIP_ASSERT("inconsistent oversize vs. space", oversize_used <= space);
470       seg = last_unsent;
471       oversize_used = LWIP_MIN(space, LWIP_MIN(oversize, len));
472       pos += oversize_used;
473       oversize -= oversize_used;
474       space -= oversize_used;
475     }
476     /* now we are either finished or oversize is zero */
477     LWIP_ASSERT("inconsistend oversize vs. len", (oversize == 0) || (pos == len));
478 #endif /* TCP_OVERSIZE */
479 
480     /*
481      * Phase 2: Chain a new pbuf to the end of pcb->unsent.
482      *
483      * We don't extend segments containing SYN/FIN flags or options
484      * (len==0). The new pbuf is kept in concat_p and pbuf_cat'ed at
485      * the end.
486      */
487     if ((pos < len) && (space > 0) && (last_unsent->len > 0)) {
488       u16_t seglen = space < len - pos ? space : len - pos;
489       seg = last_unsent;
490 
491       /* Create a pbuf with a copy or reference to seglen bytes. We
492        * can use PBUF_RAW here since the data appears in the middle of
493        * a segment. A header will never be prepended. */
494       if (apiflags & TCP_WRITE_FLAG_COPY) {
495         /* Data is copied */
496         if ((concat_p = tcp_pbuf_prealloc(PBUF_RAW, seglen, space, &oversize, pcb, apiflags, 1)) == NULL) {
497           LWIP_DEBUGF(TCP_OUTPUT_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
498                       ("tcp_write : could not allocate memory for pbuf copy size %"U16_F"\n",
499                        seglen));
500           goto memerr;
501         }
502 #if TCP_OVERSIZE_DBGCHECK
503         last_unsent->oversize_left += oversize;
504 #endif /* TCP_OVERSIZE_DBGCHECK */
505         TCP_DATA_COPY2(concat_p->payload, (const u8_t*)arg + pos, seglen, &concat_chksum, &concat_chksum_swapped);
506 #if TCP_CHECKSUM_ON_COPY
507         concat_chksummed += seglen;
508 #endif /* TCP_CHECKSUM_ON_COPY */
509       } else {
510         /* Data is not copied */
511         if ((concat_p = pbuf_alloc(PBUF_RAW, seglen, PBUF_ROM)) == NULL) {
512           LWIP_DEBUGF(TCP_OUTPUT_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
513                       ("tcp_write: could not allocate memory for zero-copy pbuf\n"));
514           goto memerr;
515         }
516 #if TCP_CHECKSUM_ON_COPY
517         /* calculate the checksum of nocopy-data */
518         tcp_seg_add_chksum(~inet_chksum((const u8_t*)arg + pos, seglen), seglen,
519           &concat_chksum, &concat_chksum_swapped);
520         concat_chksummed += seglen;
521 #endif /* TCP_CHECKSUM_ON_COPY */
522         /* reference the non-volatile payload data */
523         ((struct pbuf_rom*)concat_p)->payload = (const u8_t*)arg + pos;
524       }
525 
526       pos += seglen;
527       queuelen += pbuf_clen(concat_p);
528     }
529   } else {
530 #if TCP_OVERSIZE
531     LWIP_ASSERT("unsent_oversize mismatch (pcb->unsent is NULL)",
532                 pcb->unsent_oversize == 0);
533 #endif /* TCP_OVERSIZE */
534   }
535 
536   /*
537    * Phase 3: Create new segments.
538    *
539    * The new segments are chained together in the local 'queue'
540    * variable, ready to be appended to pcb->unsent.
541    */
542   while (pos < len) {
543     struct pbuf *p;
544     u16_t left = len - pos;
545     u16_t max_len = mss_local - optlen;
546     u16_t seglen = left > max_len ? max_len : left;
547 #if TCP_CHECKSUM_ON_COPY
548     u16_t chksum = 0;
549     u8_t chksum_swapped = 0;
550 #endif /* TCP_CHECKSUM_ON_COPY */
551 
552     if (apiflags & TCP_WRITE_FLAG_COPY) {
553       /* If copy is set, memory should be allocated and data copied
554        * into pbuf */
555       if ((p = tcp_pbuf_prealloc(PBUF_TRANSPORT, seglen + optlen, mss_local, &oversize, pcb, apiflags, queue == NULL)) == NULL) {
556         LWIP_DEBUGF(TCP_OUTPUT_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("tcp_write : could not allocate memory for pbuf copy size %"U16_F"\n", seglen));
557         goto memerr;
558       }
559       LWIP_ASSERT("tcp_write: check that first pbuf can hold the complete seglen",
560                   (p->len >= seglen));
561       TCP_DATA_COPY2((char *)p->payload + optlen, (const u8_t*)arg + pos, seglen, &chksum, &chksum_swapped);
562     } else {
563       /* Copy is not set: First allocate a pbuf for holding the data.
564        * Since the referenced data is available at least until it is
565        * sent out on the link (as it has to be ACKed by the remote
566        * party) we can safely use PBUF_ROM instead of PBUF_REF here.
567        */
568       struct pbuf *p2;
569 #if TCP_OVERSIZE
570       LWIP_ASSERT("oversize == 0", oversize == 0);
571 #endif /* TCP_OVERSIZE */
572       if ((p2 = pbuf_alloc(PBUF_TRANSPORT, seglen, PBUF_ROM)) == NULL) {
573         LWIP_DEBUGF(TCP_OUTPUT_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("tcp_write: could not allocate memory for zero-copy pbuf\n"));
574         goto memerr;
575       }
576 #if TCP_CHECKSUM_ON_COPY
577       /* calculate the checksum of nocopy-data */
578       chksum = ~inet_chksum((const u8_t*)arg + pos, seglen);
579       if (seglen & 1) {
580         chksum_swapped = 1;
581         chksum = SWAP_BYTES_IN_WORD(chksum);
582       }
583 #endif /* TCP_CHECKSUM_ON_COPY */
584       /* reference the non-volatile payload data */
585       ((struct pbuf_rom*)p2)->payload = (const u8_t*)arg + pos;
586 
587       /* Second, allocate a pbuf for the headers. */
588       if ((p = pbuf_alloc(PBUF_TRANSPORT, optlen, PBUF_RAM)) == NULL) {
589         /* If allocation fails, we have to deallocate the data pbuf as
590          * well. */
591         pbuf_free(p2);
592         LWIP_DEBUGF(TCP_OUTPUT_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("tcp_write: could not allocate memory for header pbuf\n"));
593         goto memerr;
594       }
595       /* Concatenate the headers and data pbufs together. */
596       pbuf_cat(p/*header*/, p2/*data*/);
597     }
598 
599     queuelen += pbuf_clen(p);
600 
601     /* Now that there are more segments queued, we check again if the
602      * length of the queue exceeds the configured maximum or
603      * overflows. */
604     if ((queuelen > TCP_SND_QUEUELEN) || (queuelen > TCP_SNDQUEUELEN_OVERFLOW)) {
605       LWIP_DEBUGF(TCP_OUTPUT_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("tcp_write: queue too long %"U16_F" (%d)\n",
606         queuelen, (int)TCP_SND_QUEUELEN));
607       pbuf_free(p);
608       goto memerr;
609     }
610 
611     if ((seg = tcp_create_segment(pcb, p, 0, pcb->snd_lbb + pos, optflags)) == NULL) {
612       goto memerr;
613     }
614 #if TCP_OVERSIZE_DBGCHECK
615     seg->oversize_left = oversize;
616 #endif /* TCP_OVERSIZE_DBGCHECK */
617 #if TCP_CHECKSUM_ON_COPY
618     seg->chksum = chksum;
619     seg->chksum_swapped = chksum_swapped;
620     seg->flags |= TF_SEG_DATA_CHECKSUMMED;
621 #endif /* TCP_CHECKSUM_ON_COPY */
622 
623     /* first segment of to-be-queued data? */
624     if (queue == NULL) {
625       queue = seg;
626     } else {
627       /* Attach the segment to the end of the queued segments */
628       LWIP_ASSERT("prev_seg != NULL", prev_seg != NULL);
629       prev_seg->next = seg;
630     }
631     /* remember last segment of to-be-queued data for next iteration */
632     prev_seg = seg;
633 
634     LWIP_DEBUGF(TCP_OUTPUT_DEBUG | LWIP_DBG_TRACE, ("tcp_write: queueing %"U32_F":%"U32_F"\n",
635       lwip_ntohl(seg->tcphdr->seqno),
636       lwip_ntohl(seg->tcphdr->seqno) + TCP_TCPLEN(seg)));
637 
638     pos += seglen;
639   }
640 
641   /*
642    * All three segmentation phases were successful. We can commit the
643    * transaction.
644    */
645 
646   /*
647    * Phase 1: If data has been added to the preallocated tail of
648    * last_unsent, we update the length fields of the pbuf chain.
649    */
650 #if TCP_OVERSIZE
651   if (oversize_used > 0) {
652     struct pbuf *p;
653     /* Bump tot_len of whole chain, len of tail */
654     for (p = last_unsent->p; p; p = p->next) {
655       p->tot_len += oversize_used;
656       if (p->next == NULL) {
657         TCP_DATA_COPY((char *)p->payload + p->len, arg, oversize_used, last_unsent);
658         p->len += oversize_used;
659       }
660     }
661     last_unsent->len += oversize_used;
662 #if TCP_OVERSIZE_DBGCHECK
663     LWIP_ASSERT("last_unsent->oversize_left >= oversize_used",
664                 last_unsent->oversize_left >= oversize_used);
665     last_unsent->oversize_left -= oversize_used;
666 #endif /* TCP_OVERSIZE_DBGCHECK */
667   }
668   pcb->unsent_oversize = oversize;
669 #endif /* TCP_OVERSIZE */
670 
671   /*
672    * Phase 2: concat_p can be concatenated onto last_unsent->p
673    */
674   if (concat_p != NULL) {
675     LWIP_ASSERT("tcp_write: cannot concatenate when pcb->unsent is empty",
676       (last_unsent != NULL));
677     pbuf_cat(last_unsent->p, concat_p);
678     last_unsent->len += concat_p->tot_len;
679 #if TCP_CHECKSUM_ON_COPY
680     if (concat_chksummed) {
681       /*if concat checksumm swapped - swap it back */
682       if (concat_chksum_swapped) {
683         concat_chksum = SWAP_BYTES_IN_WORD(concat_chksum);
684       }
685       tcp_seg_add_chksum(concat_chksum, concat_chksummed, &last_unsent->chksum,
686         &last_unsent->chksum_swapped);
687       last_unsent->flags |= TF_SEG_DATA_CHECKSUMMED;
688     }
689 #endif /* TCP_CHECKSUM_ON_COPY */
690   }
691 
692   /*
693    * Phase 3: Append queue to pcb->unsent. Queue may be NULL, but that
694    * is harmless
695    */
696   if (last_unsent == NULL) {
697     pcb->unsent = queue;
698   } else {
699     last_unsent->next = queue;
700   }
701 
702   /*
703    * Finally update the pcb state.
704    */
705   pcb->snd_lbb += len;
706   pcb->snd_buf -= len;
707   pcb->snd_queuelen = queuelen;
708 
709   LWIP_DEBUGF(TCP_QLEN_DEBUG, ("tcp_write: %"S16_F" (after enqueued)\n",
710     pcb->snd_queuelen));
711   if (pcb->snd_queuelen != 0) {
712     LWIP_ASSERT("tcp_write: valid queue length",
713                 pcb->unacked != NULL || pcb->unsent != NULL);
714   }
715 
716   /* Set the PSH flag in the last segment that we enqueued. */
717   if (seg != NULL && seg->tcphdr != NULL && ((apiflags & TCP_WRITE_FLAG_MORE)==0)) {
718     TCPH_SET_FLAG(seg->tcphdr, TCP_PSH);
719   }
720 
721   return ERR_OK;
722 memerr:
723   pcb->flags |= TF_NAGLEMEMERR;
724   TCP_STATS_INC(tcp.memerr);
725 
726   if (concat_p != NULL) {
727     pbuf_free(concat_p);
728   }
729   if (queue != NULL) {
730     tcp_segs_free(queue);
731   }
732   if (pcb->snd_queuelen != 0) {
733     LWIP_ASSERT("tcp_write: valid queue length", pcb->unacked != NULL ||
734       pcb->unsent != NULL);
735   }
736   LWIP_DEBUGF(TCP_QLEN_DEBUG | LWIP_DBG_STATE, ("tcp_write: %"S16_F" (with mem err)\n", pcb->snd_queuelen));
737   return ERR_MEM;
738 }
739 
740 /**
741  * Enqueue TCP options for transmission.
742  *
743  * Called by tcp_connect(), tcp_listen_input(), and tcp_send_ctrl().
744  *
745  * @param pcb Protocol control block for the TCP connection.
746  * @param flags TCP header flags to set in the outgoing segment.
747  */
748 err_t
tcp_enqueue_flags(struct tcp_pcb * pcb,u8_t flags)749 tcp_enqueue_flags(struct tcp_pcb *pcb, u8_t flags)
750 {
751   struct pbuf *p;
752   struct tcp_seg *seg;
753   u8_t optflags = 0;
754   u8_t optlen = 0;
755 
756   LWIP_DEBUGF(TCP_QLEN_DEBUG, ("tcp_enqueue_flags: queuelen: %"U16_F"\n", (u16_t)pcb->snd_queuelen));
757 
758   LWIP_ASSERT("tcp_enqueue_flags: need either TCP_SYN or TCP_FIN in flags (programmer violates API)",
759               (flags & (TCP_SYN | TCP_FIN)) != 0);
760 
761   /* check for configured max queuelen and possible overflow (FIN flag should always come through!) */
762   if (((pcb->snd_queuelen >= TCP_SND_QUEUELEN) || (pcb->snd_queuelen > TCP_SNDQUEUELEN_OVERFLOW)) &&
763       ((flags & TCP_FIN) == 0)) {
764     LWIP_DEBUGF(TCP_OUTPUT_DEBUG | LWIP_DBG_LEVEL_SEVERE, ("tcp_enqueue_flags: too long queue %"U16_F" (max %"U16_F")\n",
765                                        pcb->snd_queuelen, (u16_t)TCP_SND_QUEUELEN));
766     TCP_STATS_INC(tcp.memerr);
767     pcb->flags |= TF_NAGLEMEMERR;
768     return ERR_MEM;
769   }
770 
771   if (flags & TCP_SYN) {
772     optflags = TF_SEG_OPTS_MSS;
773 #if LWIP_WND_SCALE
774     if ((pcb->state != SYN_RCVD) || (pcb->flags & TF_WND_SCALE)) {
775       /* In a <SYN,ACK> (sent in state SYN_RCVD), the window scale option may only
776          be sent if we received a window scale option from the remote host. */
777       optflags |= TF_SEG_OPTS_WND_SCALE;
778     }
779 #endif /* LWIP_WND_SCALE */
780 #if LWIP_TCP_SACK_OUT
781     if (ip_get_option(pcb, SOF_TCPSACK) && ((pcb->state != SYN_RCVD) || (pcb->flags & TF_SACK))) {
782       /* In a <SYN,ACK> (sent in state SYN_RCVD), the SACK_PERM option may only
783          be sent if we received a SACK_PERM option from the remote host. */
784       optflags |= TF_SEG_OPTS_SACK_PERM;
785     }
786 #endif /* LWIP_TCP_SACK_OUT */
787   }
788 #if LWIP_TCP_TIMESTAMPS
789   if ((pcb->flags & TF_TIMESTAMP)) {
790     /* Make sure the timestamp option is only included in data segments if we
791        agreed about it with the remote host. */
792     optflags |= TF_SEG_OPTS_TS;
793   }
794 #endif /* LWIP_TCP_TIMESTAMPS */
795   optlen = LWIP_TCP_OPT_LENGTH(optflags);
796 
797   /* Allocate pbuf with room for TCP header + options */
798   if ((p = pbuf_alloc(PBUF_TRANSPORT, optlen, PBUF_RAM)) == NULL) {
799     pcb->flags |= TF_NAGLEMEMERR;
800     TCP_STATS_INC(tcp.memerr);
801     return ERR_MEM;
802   }
803   LWIP_ASSERT("tcp_enqueue_flags: check that first pbuf can hold optlen",
804               (p->len >= optlen));
805 
806   /* Allocate memory for tcp_seg, and fill in fields. */
807   if ((seg = tcp_create_segment(pcb, p, flags, pcb->snd_lbb, optflags)) == NULL) {
808     pcb->flags |= TF_NAGLEMEMERR;
809     TCP_STATS_INC(tcp.memerr);
810     return ERR_MEM;
811   }
812   LWIP_ASSERT("seg->tcphdr not aligned", ((mem_ptr_t)seg->tcphdr % LWIP_MIN(MEM_ALIGNMENT, 4)) == 0);
813   LWIP_ASSERT("tcp_enqueue_flags: invalid segment length", seg->len == 0);
814 
815   LWIP_DEBUGF(TCP_OUTPUT_DEBUG | LWIP_DBG_TRACE,
816               ("tcp_enqueue_flags: queueing %"U32_F":%"U32_F" (0x%"X16_F")\n",
817                lwip_ntohl(seg->tcphdr->seqno),
818                lwip_ntohl(seg->tcphdr->seqno) + TCP_TCPLEN(seg),
819                (u16_t)flags));
820 
821   /* Now append seg to pcb->unsent queue */
822   if (pcb->unsent == NULL) {
823     pcb->unsent = seg;
824   } else {
825     struct tcp_seg *useg;
826     for (useg = pcb->unsent; useg->next != NULL; useg = useg->next);
827     useg->next = seg;
828   }
829 #if TCP_OVERSIZE
830   /* The new unsent tail has no space */
831   pcb->unsent_oversize = 0;
832 #endif /* TCP_OVERSIZE */
833 
834   /* SYN and FIN bump the sequence number */
835   if ((flags & TCP_SYN) || (flags & TCP_FIN)) {
836     pcb->snd_lbb++;
837     /* optlen does not influence snd_buf */
838   }
839   if (flags & TCP_FIN) {
840     pcb->flags |= TF_FIN;
841   }
842 
843   /* update number of segments on the queues */
844   pcb->snd_queuelen += pbuf_clen(seg->p);
845   LWIP_DEBUGF(TCP_QLEN_DEBUG, ("tcp_enqueue_flags: %"S16_F" (after enqueued)\n", pcb->snd_queuelen));
846   if (pcb->snd_queuelen != 0) {
847     LWIP_ASSERT("tcp_enqueue_flags: invalid queue length",
848       pcb->unacked != NULL || pcb->unsent != NULL);
849   }
850 
851   return ERR_OK;
852 }
853 
854 #if LWIP_TCP_TIMESTAMPS
855 /* Build a timestamp option (12 bytes long) at the specified options pointer)
856  *
857  * @param pcb tcp_pcb
858  * @param opts option pointer where to store the timestamp option
859  */
860 static void
tcp_build_timestamp_option(struct tcp_pcb * pcb,u32_t * opts)861 tcp_build_timestamp_option(struct tcp_pcb *pcb, u32_t *opts)
862 {
863   /* Pad with two NOP options to make everything nicely aligned */
864   opts[0] = PP_HTONL(0x0101080A);
865   opts[1] = lwip_htonl(sys_now());
866   opts[2] = lwip_htonl(pcb->ts_recent);
867 }
868 #endif
869 
870 #if LWIP_TCP_SACK_OUT
871 /**
872  * Calculates the number of SACK entries that should be generated.
873  * It takes into account whether TF_SACK flag is set,
874  * the number of SACK entries in tcp_pcb that are valid,
875  * as well as the available options size.
876  *
877  * @param pcb tcp_pcb
878  * @param optlen the length of other TCP options (in bytes)
879  * @return the number of SACK ranges that can be used
880  */
881 static u8_t
tcp_get_num_sacks(const struct tcp_pcb * pcb,u8_t optlen)882 tcp_get_num_sacks(const struct tcp_pcb *pcb, u8_t optlen)
883 {
884   u8_t num_sacks = 0;
885 
886   LWIP_ASSERT("tcp_get_num_sacks: invalid pcb", pcb != NULL);
887 
888   if (pcb->flags & TF_SACK) {
889     u8_t i;
890 
891     /* The first SACK takes up 12 bytes (it includes SACK header and two NOP options),
892        each additional one - 8 bytes. */
893     optlen += 12;
894 
895     /* Max options size = 40, number of SACK array entries = LWIP_TCP_MAX_SACK_NUM */
896     for (i = 0; (i < LWIP_TCP_MAX_SACK_NUM) && (optlen <= TCP_MAX_OPTION_BYTES) &&
897          LWIP_TCP_SACK_VALID(pcb, i); ++i) {
898       ++num_sacks;
899       optlen += 8;
900     }
901   }
902 
903   return num_sacks;
904 }
905 
906 /** Build a SACK option (12 or more bytes long) at the specified options pointer)
907  *
908  * @param pcb tcp_pcb
909  * @param opts option pointer where to store the SACK option
910  * @param num_sacks the number of SACKs to store
911  */
912 static void
tcp_build_sack_option(const struct tcp_pcb * pcb,u32_t * opts,u8_t num_sacks)913 tcp_build_sack_option(const struct tcp_pcb *pcb, u32_t *opts, u8_t num_sacks)
914 {
915   u8_t i;
916 
917   LWIP_ASSERT("tcp_build_sack_option: invalid pcb", pcb != NULL);
918   LWIP_ASSERT("tcp_build_sack_option: invalid opts", opts != NULL);
919 
920   /* Pad with two NOP options to make everything nicely aligned.
921      We add the length (of just the SACK option, not the NOPs in front of it),
922      which is 2B of header, plus 8B for each SACK. */
923   *(opts++) = PP_HTONL(0x01010500 + 2 + num_sacks * 8);
924 
925   for (i = 0; i < num_sacks; ++i) {
926     *(opts++) = lwip_htonl(pcb->rcv_sacks[i].left);
927     *(opts++) = lwip_htonl(pcb->rcv_sacks[i].right);
928   }
929 }
930 
931 #endif
932 
933 #if LWIP_WND_SCALE
934 /** Build a window scale option (3 bytes long) at the specified options pointer)
935  *
936  * @param opts option pointer where to store the window scale option
937  */
938 static void
tcp_build_wnd_scale_option(u32_t * opts)939 tcp_build_wnd_scale_option(u32_t *opts)
940 {
941   /* Pad with one NOP option to make everything nicely aligned */
942   opts[0] = PP_HTONL(0x01030300 | TCP_RCV_SCALE);
943 }
944 #endif
945 
946 /**
947  * Send an ACK without data.
948  *
949  * @param pcb Protocol control block for the TCP connection to send the ACK
950  */
951 err_t
tcp_send_empty_ack(struct tcp_pcb * pcb)952 tcp_send_empty_ack(struct tcp_pcb *pcb)
953 {
954   err_t err;
955   struct pbuf *p;
956   u8_t optlen = 0;
957   struct netif *netif;
958 #if LWIP_TCP_TIMESTAMPS || CHECKSUM_GEN_TCP || LWIP_TCP_SACK_OUT
959   struct tcp_hdr *tcphdr;
960 #endif /* LWIP_TCP_TIMESTAMPS || CHECKSUM_GEN_TCP */
961   u8_t num_sacks = 0;
962 #if LWIP_TCP_SACK_OUT
963   u32_t *opts;
964   u16_t sacks_len = 0;
965 #endif
966 
967 #if LWIP_TCP_TIMESTAMPS
968   if (pcb->flags & TF_TIMESTAMP) {
969     optlen = LWIP_TCP_OPT_LENGTH(TF_SEG_OPTS_TS);
970   }
971 #endif
972 
973 #if LWIP_TCP_SACK_OUT
974   /* For now, SACKs are only sent with empty ACKs */
975   if ((num_sacks = tcp_get_num_sacks(pcb, optlen)) > 0) {
976     optlen += 4 + num_sacks * 8; /* 4 bytes for header (including 2*NOP), plus 8B for each SACK */
977   }
978 #endif
979 
980   p = tcp_output_alloc_header(pcb, optlen, 0, lwip_htonl(pcb->snd_nxt));
981   if (p == NULL) {
982     /* let tcp_fasttmr retry sending this ACK */
983     pcb->flags |= (TF_ACK_DELAY | TF_ACK_NOW);
984     LWIP_DEBUGF(TCP_OUTPUT_DEBUG, ("tcp_output: (ACK) could not allocate pbuf\n"));
985     return ERR_BUF;
986   }
987 #if LWIP_TCP_TIMESTAMPS || CHECKSUM_GEN_TCP || LWIP_TCP_SACK_OUT
988   tcphdr = (struct tcp_hdr *)p->payload;
989 #endif /* LWIP_TCP_TIMESTAMPS || CHECKSUM_GEN_TCP */
990   LWIP_DEBUGF(TCP_OUTPUT_DEBUG,
991               ("tcp_output: sending ACK for %"U32_F"\n", pcb->rcv_nxt));
992 
993   /* NB. MSS and window scale options are only sent on SYNs, so ignore them here */
994 #if LWIP_TCP_TIMESTAMPS
995   pcb->ts_lastacksent = pcb->rcv_nxt;
996 
997   if (pcb->flags & TF_TIMESTAMP) {
998     tcp_build_timestamp_option(pcb, (u32_t *)(tcphdr + 1));
999   }
1000 #endif
1001 
1002 #if LWIP_TCP_SACK_OUT
1003   opts = (u32_t *)(void *)(tcphdr + 1);
1004 
1005   if (pcb && (num_sacks > 0)) {
1006     tcp_build_sack_option(pcb, opts, num_sacks);
1007     /* 1 word for SACKs header (including 2xNOP), and 2 words for each SACK */
1008     sacks_len = 1 + num_sacks * 2;
1009     opts += sacks_len;
1010   }
1011 #else
1012   LWIP_UNUSED_ARG(num_sacks);
1013 #endif
1014 
1015   netif = ip_route(&pcb->local_ip, &pcb->remote_ip);
1016   if (netif == NULL) {
1017     err = ERR_RTE;
1018   } else {
1019 #if CHECKSUM_GEN_TCP
1020     IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_TCP) {
1021       tcphdr->chksum = ip_chksum_pseudo(p, IP_PROTO_TCP, p->tot_len,
1022         &pcb->local_ip, &pcb->remote_ip);
1023     }
1024 #endif
1025     NETIF_SET_HWADDRHINT(netif, &(pcb->addr_hint));
1026     err = ip_output_if(p, &pcb->local_ip, &pcb->remote_ip,
1027       pcb->ttl, pcb->tos, IP_PROTO_TCP, netif);
1028     NETIF_SET_HWADDRHINT(netif, NULL);
1029   }
1030   pbuf_free(p);
1031 
1032   if (err != ERR_OK) {
1033     /* let tcp_fasttmr retry sending this ACK */
1034     pcb->flags |= (TF_ACK_DELAY | TF_ACK_NOW);
1035   } else {
1036     /* remove ACK flags from the PCB, as we sent an empty ACK now */
1037     pcb->flags &= ~(TF_ACK_DELAY | TF_ACK_NOW);
1038   }
1039 
1040   return err;
1041 }
1042 
1043 /**
1044  * @ingroup tcp_raw
1045  * Find out what we can send and send it
1046  *
1047  * @param pcb Protocol control block for the TCP connection to send data
1048  * @return ERR_OK if data has been sent or nothing to send
1049  *         another err_t on error
1050  */
1051 err_t
tcp_output(struct tcp_pcb * pcb)1052 tcp_output(struct tcp_pcb *pcb)
1053 {
1054   struct tcp_seg *seg, *useg;
1055   u32_t wnd, snd_nxt;
1056   err_t err;
1057   struct netif *netif;
1058 #if TCP_CWND_DEBUG
1059   s16_t i = 0;
1060 #endif /* TCP_CWND_DEBUG */
1061 
1062   /* pcb->state LISTEN not allowed here */
1063   LWIP_ASSERT("don't call tcp_output for listen-pcbs",
1064     pcb->state != LISTEN);
1065 
1066   /* First, check if we are invoked by the TCP input processing
1067      code. If so, we do not output anything. Instead, we rely on the
1068      input processing code to call us when input processing is done
1069      with. */
1070   if (tcp_input_pcb == pcb) {
1071     return ERR_OK;
1072   }
1073 
1074   wnd = LWIP_MIN(pcb->snd_wnd, pcb->cwnd);
1075 
1076   seg = pcb->unsent;
1077 
1078   /* If the TF_ACK_NOW flag is set and no data will be sent (either
1079    * because the ->unsent queue is empty or because the window does
1080    * not allow it), construct an empty ACK segment and send it.
1081    *
1082    * If data is to be sent, we will just piggyback the ACK (see below).
1083    */
1084   if (pcb->flags & TF_ACK_NOW &&
1085      (seg == NULL ||
1086       lwip_ntohl(seg->tcphdr->seqno) - pcb->lastack + seg->len > wnd)) {
1087      return tcp_send_empty_ack(pcb);
1088   }
1089 
1090   /* useg should point to last segment on unacked queue */
1091   useg = pcb->unacked;
1092   if (useg != NULL) {
1093     for (; useg->next != NULL; useg = useg->next);
1094   }
1095 
1096   netif = ip_route(&pcb->local_ip, &pcb->remote_ip);
1097   if (netif == NULL) {
1098     return ERR_RTE;
1099   }
1100 
1101   /* If we don't have a local IP address, we get one from netif */
1102   if (ip_addr_isany(&pcb->local_ip)) {
1103     const ip_addr_t *local_ip = ip_netif_get_local_ip(netif, &pcb->remote_ip);
1104     if (local_ip == NULL) {
1105       return ERR_RTE;
1106     }
1107     ip_addr_copy(pcb->local_ip, *local_ip);
1108   }
1109 
1110 #if TCP_OUTPUT_DEBUG
1111   if (seg == NULL) {
1112     LWIP_DEBUGF(TCP_OUTPUT_DEBUG, ("tcp_output: nothing to send (%p)\n",
1113                                    (void*)pcb->unsent));
1114   }
1115 #endif /* TCP_OUTPUT_DEBUG */
1116 #if TCP_CWND_DEBUG
1117   if (seg == NULL) {
1118     LWIP_DEBUGF(TCP_CWND_DEBUG, ("tcp_output: snd_wnd %"TCPWNDSIZE_F
1119                                  ", cwnd %"TCPWNDSIZE_F", wnd %"U32_F
1120                                  ", seg == NULL, ack %"U32_F"\n",
1121                                  pcb->snd_wnd, pcb->cwnd, wnd, pcb->lastack));
1122   } else {
1123     LWIP_DEBUGF(TCP_CWND_DEBUG,
1124                 ("tcp_output: snd_wnd %"TCPWNDSIZE_F", cwnd %"TCPWNDSIZE_F", wnd %"U32_F
1125                  ", effwnd %"U32_F", seq %"U32_F", ack %"U32_F"\n",
1126                  pcb->snd_wnd, pcb->cwnd, wnd,
1127                  lwip_ntohl(seg->tcphdr->seqno) - pcb->lastack + seg->len,
1128                  lwip_ntohl(seg->tcphdr->seqno), pcb->lastack));
1129   }
1130 #endif /* TCP_CWND_DEBUG */
1131   /* data available and window allows it to be sent? */
1132   while (seg != NULL &&
1133          lwip_ntohl(seg->tcphdr->seqno) - pcb->lastack + seg->len <= wnd) {
1134     LWIP_ASSERT("RST not expected here!",
1135                 (TCPH_FLAGS(seg->tcphdr) & TCP_RST) == 0);
1136     /* Stop sending if the nagle algorithm would prevent it
1137      * Don't stop:
1138      * - if tcp_write had a memory error before (prevent delayed ACK timeout) or
1139      * - if FIN was already enqueued for this PCB (SYN is always alone in a segment -
1140      *   either seg->next != NULL or pcb->unacked == NULL;
1141      *   RST is no sent using tcp_write/tcp_output.
1142      */
1143     if ((tcp_do_output_nagle(pcb) == 0) &&
1144       ((pcb->flags & (TF_NAGLEMEMERR | TF_FIN)) == 0)) {
1145       break;
1146     }
1147 #if TCP_CWND_DEBUG
1148     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",
1149                             pcb->snd_wnd, pcb->cwnd, wnd,
1150                             lwip_ntohl(seg->tcphdr->seqno) + seg->len -
1151                             pcb->lastack,
1152                             lwip_ntohl(seg->tcphdr->seqno), pcb->lastack, i));
1153     ++i;
1154 #endif /* TCP_CWND_DEBUG */
1155 
1156     if (pcb->state != SYN_SENT) {
1157       TCPH_SET_FLAG(seg->tcphdr, TCP_ACK);
1158     }
1159 
1160 #if TCP_OVERSIZE_DBGCHECK
1161     seg->oversize_left = 0;
1162 #endif /* TCP_OVERSIZE_DBGCHECK */
1163     err = tcp_output_segment(seg, pcb, netif);
1164     if (err != ERR_OK) {
1165       /* segment could not be sent, for whatever reason */
1166       pcb->flags |= TF_NAGLEMEMERR;
1167       return err;
1168     }
1169     pcb->unsent = seg->next;
1170     if (pcb->state != SYN_SENT) {
1171       pcb->flags &= ~(TF_ACK_DELAY | TF_ACK_NOW);
1172     }
1173     snd_nxt = lwip_ntohl(seg->tcphdr->seqno) + TCP_TCPLEN(seg);
1174     if (TCP_SEQ_LT(pcb->snd_nxt, snd_nxt)) {
1175       pcb->snd_nxt = snd_nxt;
1176     }
1177     /* put segment on unacknowledged list if length > 0 */
1178     if (TCP_TCPLEN(seg) > 0) {
1179       seg->next = NULL;
1180       /* unacked list is empty? */
1181       if (pcb->unacked == NULL) {
1182         pcb->unacked = seg;
1183         useg = seg;
1184       /* unacked list is not empty? */
1185       } else {
1186         /* In the case of fast retransmit, the packet should not go to the tail
1187          * of the unacked queue, but rather somewhere before it. We need to check for
1188          * this case. -STJ Jul 27, 2004 */
1189         if (TCP_SEQ_LT(lwip_ntohl(seg->tcphdr->seqno), lwip_ntohl(useg->tcphdr->seqno))) {
1190           /* add segment to before tail of unacked list, keeping the list sorted */
1191           struct tcp_seg **cur_seg = &(pcb->unacked);
1192           while (*cur_seg &&
1193             TCP_SEQ_LT(lwip_ntohl((*cur_seg)->tcphdr->seqno), lwip_ntohl(seg->tcphdr->seqno))) {
1194               cur_seg = &((*cur_seg)->next );
1195           }
1196           seg->next = (*cur_seg);
1197           (*cur_seg) = seg;
1198         } else {
1199           /* add segment to tail of unacked list */
1200           useg->next = seg;
1201           useg = useg->next;
1202         }
1203       }
1204     /* do not queue empty segments on the unacked list */
1205     } else {
1206       tcp_seg_free(seg);
1207     }
1208     seg = pcb->unsent;
1209   }
1210 #if TCP_OVERSIZE
1211   if (pcb->unsent == NULL) {
1212     /* last unsent has been removed, reset unsent_oversize */
1213     pcb->unsent_oversize = 0;
1214   }
1215 #endif /* TCP_OVERSIZE */
1216 
1217   pcb->flags &= ~TF_NAGLEMEMERR;
1218   return ERR_OK;
1219 }
1220 
1221 /**
1222  * Called by tcp_output() to actually send a TCP segment over IP.
1223  *
1224  * @param seg the tcp_seg to send
1225  * @param pcb the tcp_pcb for the TCP connection used to send the segment
1226  * @param netif the netif used to send the segment
1227  */
1228 static err_t
tcp_output_segment(struct tcp_seg * seg,struct tcp_pcb * pcb,struct netif * netif)1229 tcp_output_segment(struct tcp_seg *seg, struct tcp_pcb *pcb, struct netif *netif)
1230 {
1231   err_t err;
1232   u16_t len;
1233   u32_t *opts;
1234 
1235   if (seg->p->ref != 1) {
1236     /* This can happen if the pbuf of this segment is still referenced by the
1237        netif driver due to deferred transmission. Since this function modifies
1238        p->len, we must not continue in this case. */
1239     return ERR_OK;
1240   }
1241 
1242   /* The TCP header has already been constructed, but the ackno and
1243    wnd fields remain. */
1244   seg->tcphdr->ackno = lwip_htonl(pcb->rcv_nxt);
1245 
1246   /* advertise our receive window size in this TCP segment */
1247 #if LWIP_WND_SCALE
1248   if (seg->flags & TF_SEG_OPTS_WND_SCALE) {
1249     /* The Window field in a SYN segment itself (the only type where we send
1250        the window scale option) is never scaled. */
1251     seg->tcphdr->wnd = lwip_htons(TCPWND_MIN16(pcb->rcv_ann_wnd));
1252   } else
1253 #endif /* LWIP_WND_SCALE */
1254   {
1255     seg->tcphdr->wnd = lwip_htons(TCPWND_MIN16(RCV_WND_SCALE(pcb, pcb->rcv_ann_wnd)));
1256   }
1257 
1258   pcb->rcv_ann_right_edge = pcb->rcv_nxt + pcb->rcv_ann_wnd;
1259 
1260   /* Add any requested options.  NB MSS option is only set on SYN
1261      packets, so ignore it here */
1262   /* cast through void* to get rid of alignment warnings */
1263   opts = (u32_t *)(void *)(seg->tcphdr + 1);
1264   if (seg->flags & TF_SEG_OPTS_MSS) {
1265     u16_t mss;
1266 #if TCP_CALCULATE_EFF_SEND_MSS
1267     mss = tcp_eff_send_mss(TCP_MSS, &pcb->local_ip, &pcb->remote_ip);
1268 #else /* TCP_CALCULATE_EFF_SEND_MSS */
1269     mss = TCP_MSS;
1270 #endif /* TCP_CALCULATE_EFF_SEND_MSS */
1271     *opts = TCP_BUILD_MSS_OPTION(mss);
1272     opts += 1;
1273   }
1274 #if LWIP_TCP_TIMESTAMPS
1275   pcb->ts_lastacksent = pcb->rcv_nxt;
1276 
1277   if (seg->flags & TF_SEG_OPTS_TS) {
1278     tcp_build_timestamp_option(pcb, opts);
1279     opts += 3;
1280   }
1281 #endif
1282 #if LWIP_WND_SCALE
1283   if (seg->flags & TF_SEG_OPTS_WND_SCALE) {
1284     tcp_build_wnd_scale_option(opts);
1285     opts += 1;
1286   }
1287 #endif
1288 #if LWIP_TCP_SACK_OUT
1289   if (ip_get_option(pcb, SOF_TCPSACK) && (seg->flags & TF_SEG_OPTS_SACK_PERM)) {
1290     /* Pad with two NOP options to make everything nicely aligned
1291      * NOTE: When we send both timestamp and SACK_PERM options,
1292      * we could use the first two NOPs before the timestamp to store SACK_PERM option,
1293      * but that would complicate the code.
1294      */
1295     *(opts++) = PP_HTONL(0x01010402);
1296   }
1297 #endif
1298 
1299   /* Set retransmission timer running if it is not currently enabled
1300      This must be set before checking the route. */
1301   if (pcb->rtime < 0) {
1302     pcb->rtime = 0;
1303   }
1304 
1305   if (pcb->rttest == 0) {
1306     pcb->rttest = tcp_ticks;
1307     pcb->rtseq = lwip_ntohl(seg->tcphdr->seqno);
1308 
1309     LWIP_DEBUGF(TCP_RTO_DEBUG, ("tcp_output_segment: rtseq %"U32_F"\n", pcb->rtseq));
1310   }
1311   LWIP_DEBUGF(TCP_OUTPUT_DEBUG, ("tcp_output_segment: %"U32_F":%"U32_F"\n",
1312           lwip_htonl(seg->tcphdr->seqno), lwip_htonl(seg->tcphdr->seqno) +
1313           seg->len));
1314 
1315   len = (u16_t)((u8_t *)seg->tcphdr - (u8_t *)seg->p->payload);
1316   if (len == 0) {
1317     /** Exclude retransmitted segments from this count. */
1318     MIB2_STATS_INC(mib2.tcpoutsegs);
1319   }
1320 
1321   seg->p->len -= len;
1322   seg->p->tot_len -= len;
1323 
1324   seg->p->payload = seg->tcphdr;
1325 
1326   seg->tcphdr->chksum = 0;
1327 #if CHECKSUM_GEN_TCP
1328   IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_TCP) {
1329 #if TCP_CHECKSUM_ON_COPY
1330     u32_t acc;
1331 #if TCP_CHECKSUM_ON_COPY_SANITY_CHECK
1332     u16_t chksum_slow = ip_chksum_pseudo(seg->p, IP_PROTO_TCP,
1333       seg->p->tot_len, &pcb->local_ip, &pcb->remote_ip);
1334 #endif /* TCP_CHECKSUM_ON_COPY_SANITY_CHECK */
1335     if ((seg->flags & TF_SEG_DATA_CHECKSUMMED) == 0) {
1336       LWIP_ASSERT("data included but not checksummed",
1337         seg->p->tot_len == (TCPH_HDRLEN(seg->tcphdr) * 4));
1338     }
1339 
1340     /* rebuild TCP header checksum (TCP header changes for retransmissions!) */
1341     acc = ip_chksum_pseudo_partial(seg->p, IP_PROTO_TCP,
1342       seg->p->tot_len, TCPH_HDRLEN(seg->tcphdr) * 4, &pcb->local_ip, &pcb->remote_ip);
1343     /* add payload checksum */
1344     if (seg->chksum_swapped) {
1345       seg->chksum = SWAP_BYTES_IN_WORD(seg->chksum);
1346       seg->chksum_swapped = 0;
1347     }
1348     acc += (u16_t)~(seg->chksum);
1349     seg->tcphdr->chksum = FOLD_U32T(acc);
1350 #if TCP_CHECKSUM_ON_COPY_SANITY_CHECK
1351     if (chksum_slow != seg->tcphdr->chksum) {
1352       TCP_CHECKSUM_ON_COPY_SANITY_CHECK_FAIL(
1353                   ("tcp_output_segment: calculated checksum is %"X16_F" instead of %"X16_F"\n",
1354                   seg->tcphdr->chksum, chksum_slow));
1355       seg->tcphdr->chksum = chksum_slow;
1356     }
1357 #endif /* TCP_CHECKSUM_ON_COPY_SANITY_CHECK */
1358 #else /* TCP_CHECKSUM_ON_COPY */
1359     seg->tcphdr->chksum = ip_chksum_pseudo(seg->p, IP_PROTO_TCP,
1360       seg->p->tot_len, &pcb->local_ip, &pcb->remote_ip);
1361 #endif /* TCP_CHECKSUM_ON_COPY */
1362   }
1363 #endif /* CHECKSUM_GEN_TCP */
1364   TCP_STATS_INC(tcp.xmit);
1365 
1366   NETIF_SET_HWADDRHINT(netif, &(pcb->addr_hint));
1367   err = ip_output_if(seg->p, &pcb->local_ip, &pcb->remote_ip, pcb->ttl,
1368     pcb->tos, IP_PROTO_TCP, netif);
1369   NETIF_SET_HWADDRHINT(netif, NULL);
1370   return err;
1371 }
1372 
1373 /**
1374  * Send a TCP RESET packet (empty segment with RST flag set) either to
1375  * abort a connection or to show that there is no matching local connection
1376  * for a received segment.
1377  *
1378  * Called by tcp_abort() (to abort a local connection), tcp_input() (if no
1379  * matching local pcb was found), tcp_listen_input() (if incoming segment
1380  * has ACK flag set) and tcp_process() (received segment in the wrong state)
1381  *
1382  * Since a RST segment is in most cases not sent for an active connection,
1383  * tcp_rst() has a number of arguments that are taken from a tcp_pcb for
1384  * most other segment output functions.
1385  *
1386  * @param seqno the sequence number to use for the outgoing segment
1387  * @param ackno the acknowledge number to use for the outgoing segment
1388  * @param local_ip the local IP address to send the segment from
1389  * @param remote_ip the remote IP address to send the segment to
1390  * @param local_port the local TCP port to send the segment from
1391  * @param remote_port the remote TCP port to send the segment to
1392  */
1393 void
tcp_rst(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)1394 tcp_rst(u32_t seqno, u32_t ackno,
1395   const ip_addr_t *local_ip, const ip_addr_t *remote_ip,
1396   u16_t local_port, u16_t remote_port)
1397 {
1398   struct pbuf *p;
1399   struct tcp_hdr *tcphdr;
1400   struct netif *netif;
1401   p = pbuf_alloc(PBUF_IP, TCP_HLEN, PBUF_RAM);
1402   if (p == NULL) {
1403     LWIP_DEBUGF(TCP_DEBUG, ("tcp_rst: could not allocate memory for pbuf\n"));
1404     return;
1405   }
1406   LWIP_ASSERT("check that first pbuf can hold struct tcp_hdr",
1407               (p->len >= sizeof(struct tcp_hdr)));
1408 
1409   tcphdr = (struct tcp_hdr *)p->payload;
1410   tcphdr->src = lwip_htons(local_port);
1411   tcphdr->dest = lwip_htons(remote_port);
1412   tcphdr->seqno = lwip_htonl(seqno);
1413   tcphdr->ackno = lwip_htonl(ackno);
1414   TCPH_HDRLEN_FLAGS_SET(tcphdr, TCP_HLEN/4, TCP_RST | TCP_ACK);
1415 #if LWIP_WND_SCALE
1416   tcphdr->wnd = PP_HTONS(((TCP_WND >> TCP_RCV_SCALE) & 0xFFFF));
1417 #else
1418   tcphdr->wnd = PP_HTONS(TCP_WND);
1419 #endif
1420   tcphdr->chksum = 0;
1421   tcphdr->urgp = 0;
1422 
1423   TCP_STATS_INC(tcp.xmit);
1424   MIB2_STATS_INC(mib2.tcpoutrsts);
1425 
1426   netif = ip_route(local_ip, remote_ip);
1427   if (netif != NULL) {
1428 #if CHECKSUM_GEN_TCP
1429     IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_TCP) {
1430       tcphdr->chksum = ip_chksum_pseudo(p, IP_PROTO_TCP, p->tot_len,
1431                                         local_ip, remote_ip);
1432     }
1433 #endif
1434     /* Send output with hardcoded TTL/HL since we have no access to the pcb */
1435     ip_output_if(p, local_ip, remote_ip, TCP_TTL, 0, IP_PROTO_TCP, netif);
1436   }
1437   pbuf_free(p);
1438   LWIP_DEBUGF(TCP_RST_DEBUG, ("tcp_rst: seqno %"U32_F" ackno %"U32_F".\n", seqno, ackno));
1439 }
1440 
1441 /**
1442  * Requeue all unacked segments for retransmission
1443  *
1444  * Called by tcp_slowtmr() for slow retransmission.
1445  *
1446  * @param pcb the tcp_pcb for which to re-enqueue all unacked segments
1447  */
1448 void
tcp_rexmit_rto(struct tcp_pcb * pcb)1449 tcp_rexmit_rto(struct tcp_pcb *pcb)
1450 {
1451   struct tcp_seg *seg;
1452 
1453   if (pcb->unacked == NULL) {
1454     return;
1455   }
1456 
1457   /* Move all unacked segments to the head of the unsent queue */
1458   for (seg = pcb->unacked; seg->next != NULL; seg = seg->next);
1459   /* concatenate unsent queue after unacked queue */
1460   seg->next = pcb->unsent;
1461 #if TCP_OVERSIZE_DBGCHECK
1462   /* if last unsent changed, we need to update unsent_oversize */
1463   if (pcb->unsent == NULL) {
1464     pcb->unsent_oversize = seg->oversize_left;
1465   }
1466 #endif /* TCP_OVERSIZE_DBGCHECK */
1467   /* unsent queue is the concatenated queue (of unacked, unsent) */
1468   pcb->unsent = pcb->unacked;
1469   /* unacked queue is now empty */
1470   pcb->unacked = NULL;
1471 
1472   /* increment number of retransmissions */
1473   ++pcb->nrtx;
1474 
1475   /* Don't take any RTT measurements after retransmitting. */
1476   pcb->rttest = 0;
1477 
1478   /* Do the actual retransmission */
1479   tcp_output(pcb);
1480 }
1481 
1482 /**
1483  * Requeue the first unacked segment for retransmission
1484  *
1485  * Called by tcp_receive() for fast retransmit.
1486  *
1487  * @param pcb the tcp_pcb for which to retransmit the first unacked segment
1488  */
1489 void
tcp_rexmit(struct tcp_pcb * pcb)1490 tcp_rexmit(struct tcp_pcb *pcb)
1491 {
1492   struct tcp_seg *seg;
1493   struct tcp_seg **cur_seg;
1494 
1495   if (pcb->unacked == NULL) {
1496     return;
1497   }
1498 
1499   /* Move the first unacked segment to the unsent queue */
1500   /* Keep the unsent queue sorted. */
1501   seg = pcb->unacked;
1502   pcb->unacked = seg->next;
1503 
1504   cur_seg = &(pcb->unsent);
1505   while (*cur_seg &&
1506     TCP_SEQ_LT(lwip_ntohl((*cur_seg)->tcphdr->seqno), lwip_ntohl(seg->tcphdr->seqno))) {
1507       cur_seg = &((*cur_seg)->next );
1508   }
1509   seg->next = *cur_seg;
1510   *cur_seg = seg;
1511 #if TCP_OVERSIZE
1512   if (seg->next == NULL) {
1513     /* the retransmitted segment is last in unsent, so reset unsent_oversize */
1514     pcb->unsent_oversize = 0;
1515   }
1516 #endif /* TCP_OVERSIZE */
1517 
1518   ++pcb->nrtx;
1519 
1520   /* Don't take any rtt measurements after retransmitting. */
1521   pcb->rttest = 0;
1522 
1523   /* Do the actual retransmission. */
1524   MIB2_STATS_INC(mib2.tcpretranssegs);
1525   /* No need to call tcp_output: we are always called from tcp_input()
1526      and thus tcp_output directly returns. */
1527 }
1528 
1529 
1530 /**
1531  * Handle retransmission after three dupacks received
1532  *
1533  * @param pcb the tcp_pcb for which to retransmit the first unacked segment
1534  */
1535 void
tcp_rexmit_fast(struct tcp_pcb * pcb)1536 tcp_rexmit_fast(struct tcp_pcb *pcb)
1537 {
1538   if (pcb->unacked != NULL && !(pcb->flags & TF_INFR)) {
1539     /* This is fast retransmit. Retransmit the first unacked segment. */
1540     LWIP_DEBUGF(TCP_FR_DEBUG,
1541                 ("tcp_receive: dupacks %"U16_F" (%"U32_F
1542                  "), fast retransmit %"U32_F"\n",
1543                  (u16_t)pcb->dupacks, pcb->lastack,
1544                  lwip_ntohl(pcb->unacked->tcphdr->seqno)));
1545     tcp_rexmit(pcb);
1546 
1547     /* Set ssthresh to half of the minimum of the current
1548      * cwnd and the advertised window */
1549     if (pcb->cwnd > pcb->snd_wnd) {
1550       pcb->ssthresh = pcb->snd_wnd / 2;
1551     } else {
1552       pcb->ssthresh = pcb->cwnd / 2;
1553     }
1554 
1555     /* The minimum value for ssthresh should be 2 MSS */
1556     if (pcb->ssthresh < (2U * pcb->mss)) {
1557       LWIP_DEBUGF(TCP_FR_DEBUG,
1558                   ("tcp_receive: The minimum value for ssthresh %"TCPWNDSIZE_F
1559                    " should be min 2 mss %"U16_F"...\n",
1560                    pcb->ssthresh, (u16_t)(2*pcb->mss)));
1561       pcb->ssthresh = 2*pcb->mss;
1562     }
1563 
1564     pcb->cwnd = pcb->ssthresh + 3 * pcb->mss;
1565     pcb->flags |= TF_INFR;
1566 
1567     /* Reset the retransmission timer to prevent immediate rto retransmissions */
1568     pcb->rtime = 0;
1569   }
1570 }
1571 
1572 
1573 /**
1574  * Send keepalive packets to keep a connection active although
1575  * no data is sent over it.
1576  *
1577  * Called by tcp_slowtmr()
1578  *
1579  * @param pcb the tcp_pcb for which to send a keepalive packet
1580  */
1581 err_t
tcp_keepalive(struct tcp_pcb * pcb)1582 tcp_keepalive(struct tcp_pcb *pcb)
1583 {
1584   err_t err;
1585   struct pbuf *p;
1586   struct netif *netif;
1587 
1588   LWIP_DEBUGF(TCP_DEBUG, ("tcp_keepalive: sending KEEPALIVE probe to "));
1589   ip_addr_debug_print(TCP_DEBUG, &pcb->remote_ip);
1590   LWIP_DEBUGF(TCP_DEBUG, ("\n"));
1591 
1592   LWIP_DEBUGF(TCP_DEBUG, ("tcp_keepalive: tcp_ticks %"U32_F"   pcb->tmr %"U32_F" pcb->keep_cnt_sent %"U16_F"\n",
1593                           tcp_ticks, pcb->tmr, (u16_t)pcb->keep_cnt_sent));
1594 
1595   p = tcp_output_alloc_header(pcb, 0, 0, lwip_htonl(pcb->snd_nxt - 1));
1596   if (p == NULL) {
1597     LWIP_DEBUGF(TCP_DEBUG,
1598                 ("tcp_keepalive: could not allocate memory for pbuf\n"));
1599     return ERR_MEM;
1600   }
1601   netif = ip_route(&pcb->local_ip, &pcb->remote_ip);
1602   if (netif == NULL) {
1603     err = ERR_RTE;
1604   } else {
1605 #if CHECKSUM_GEN_TCP
1606     IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_TCP) {
1607       struct tcp_hdr *tcphdr = (struct tcp_hdr *)p->payload;
1608       tcphdr->chksum = ip_chksum_pseudo(p, IP_PROTO_TCP, p->tot_len,
1609         &pcb->local_ip, &pcb->remote_ip);
1610     }
1611 #endif /* CHECKSUM_GEN_TCP */
1612     TCP_STATS_INC(tcp.xmit);
1613 
1614     /* Send output to IP */
1615     NETIF_SET_HWADDRHINT(netif, &(pcb->addr_hint));
1616     err = ip_output_if(p, &pcb->local_ip, &pcb->remote_ip, pcb->ttl, 0, IP_PROTO_TCP, netif);
1617     NETIF_SET_HWADDRHINT(netif, NULL);
1618   }
1619   pbuf_free(p);
1620 
1621   LWIP_DEBUGF(TCP_DEBUG, ("tcp_keepalive: seqno %"U32_F" ackno %"U32_F" err %d.\n",
1622                           pcb->snd_nxt - 1, pcb->rcv_nxt, (int)err));
1623   return err;
1624 }
1625 
1626 
1627 /**
1628  * Send persist timer zero-window probes to keep a connection active
1629  * when a window update is lost.
1630  *
1631  * Called by tcp_slowtmr()
1632  *
1633  * @param pcb the tcp_pcb for which to send a zero-window probe packet
1634  */
1635 err_t
tcp_zero_window_probe(struct tcp_pcb * pcb)1636 tcp_zero_window_probe(struct tcp_pcb *pcb)
1637 {
1638   err_t err;
1639   struct pbuf *p;
1640   struct tcp_hdr *tcphdr;
1641   struct tcp_seg *seg;
1642   u16_t len;
1643   u8_t is_fin;
1644   u32_t snd_nxt;
1645   struct netif *netif;
1646 
1647   LWIP_DEBUGF(TCP_DEBUG, ("tcp_zero_window_probe: sending ZERO WINDOW probe to "));
1648   ip_addr_debug_print(TCP_DEBUG, &pcb->remote_ip);
1649   LWIP_DEBUGF(TCP_DEBUG, ("\n"));
1650 
1651   LWIP_DEBUGF(TCP_DEBUG,
1652               ("tcp_zero_window_probe: tcp_ticks %"U32_F
1653                "   pcb->tmr %"U32_F" pcb->keep_cnt_sent %"U16_F"\n",
1654                tcp_ticks, pcb->tmr, (u16_t)pcb->keep_cnt_sent));
1655 
1656   seg = pcb->unacked;
1657 
1658   if (seg == NULL) {
1659     seg = pcb->unsent;
1660   }
1661   if (seg == NULL) {
1662     /* nothing to send, zero window probe not needed */
1663     return ERR_OK;
1664   }
1665 
1666   is_fin = ((TCPH_FLAGS(seg->tcphdr) & TCP_FIN) != 0) && (seg->len == 0);
1667   /* we want to send one seqno: either FIN or data (no options) */
1668   len = is_fin ? 0 : 1;
1669 
1670   p = tcp_output_alloc_header(pcb, 0, len, seg->tcphdr->seqno);
1671   if (p == NULL) {
1672     LWIP_DEBUGF(TCP_DEBUG, ("tcp_zero_window_probe: no memory for pbuf\n"));
1673     return ERR_MEM;
1674   }
1675   tcphdr = (struct tcp_hdr *)p->payload;
1676 
1677   if (is_fin) {
1678     /* FIN segment, no data */
1679     TCPH_FLAGS_SET(tcphdr, TCP_ACK | TCP_FIN);
1680   } else {
1681     /* Data segment, copy in one byte from the head of the unacked queue */
1682     char *d = ((char *)p->payload + TCP_HLEN);
1683     /* Depending on whether the segment has already been sent (unacked) or not
1684        (unsent), seg->p->payload points to the IP header or TCP header.
1685        Ensure we copy the first TCP data byte: */
1686     pbuf_copy_partial(seg->p, d, 1, seg->p->tot_len - seg->len);
1687   }
1688 
1689   /* The byte may be acknowledged without the window being opened. */
1690   snd_nxt = lwip_ntohl(seg->tcphdr->seqno) + 1;
1691   if (TCP_SEQ_LT(pcb->snd_nxt, snd_nxt)) {
1692     pcb->snd_nxt = snd_nxt;
1693   }
1694 
1695   netif = ip_route(&pcb->local_ip, &pcb->remote_ip);
1696   if (netif == NULL) {
1697     err = ERR_RTE;
1698   } else {
1699 #if CHECKSUM_GEN_TCP
1700     IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_TCP) {
1701       tcphdr->chksum = ip_chksum_pseudo(p, IP_PROTO_TCP, p->tot_len,
1702         &pcb->local_ip, &pcb->remote_ip);
1703     }
1704 #endif
1705     TCP_STATS_INC(tcp.xmit);
1706 
1707     /* Send output to IP */
1708     NETIF_SET_HWADDRHINT(netif, &(pcb->addr_hint));
1709     err = ip_output_if(p, &pcb->local_ip, &pcb->remote_ip, pcb->ttl,
1710       0, IP_PROTO_TCP, netif);
1711     NETIF_SET_HWADDRHINT(netif, NULL);
1712   }
1713 
1714   pbuf_free(p);
1715 
1716   LWIP_DEBUGF(TCP_DEBUG, ("tcp_zero_window_probe: seqno %"U32_F
1717                           " ackno %"U32_F" err %d.\n",
1718                           pcb->snd_nxt - 1, pcb->rcv_nxt, (int)err));
1719   return err;
1720 }
1721 #endif /* LWIP_TCP */
1722