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