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