]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/ethernet/lwIP_130/src/core/tcp_out.c
Add FreeRTOS-Plus directory.
[freertos] / FreeRTOS / Demo / Common / ethernet / lwIP_130 / src / core / tcp_out.c
1 /**\r
2  * @file\r
3  * Transmission Control Protocol, outgoing traffic\r
4  *\r
5  * The output functions of TCP.\r
6  *\r
7  */\r
8 \r
9 /*\r
10  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.\r
11  * All rights reserved.\r
12  *\r
13  * Redistribution and use in source and binary forms, with or without modification,\r
14  * are permitted provided that the following conditions are met:\r
15  *\r
16  * 1. Redistributions of source code must retain the above copyright notice,\r
17  *    this list of conditions and the following disclaimer.\r
18  * 2. Redistributions in binary form must reproduce the above copyright notice,\r
19  *    this list of conditions and the following disclaimer in the documentation\r
20  *    and/or other materials provided with the distribution.\r
21  * 3. The name of the author may not be used to endorse or promote products\r
22  *    derived from this software without specific prior written permission.\r
23  *\r
24  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED\r
25  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\r
26  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT\r
27  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,\r
28  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT\r
29  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING\r
32  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY\r
33  * OF SUCH DAMAGE.\r
34  *\r
35  * This file is part of the lwIP TCP/IP stack.\r
36  *\r
37  * Author: Adam Dunkels <adam@sics.se>\r
38  *\r
39  */\r
40 \r
41 #include "lwip/opt.h"\r
42 \r
43 #if LWIP_TCP /* don't build if not configured for use in lwipopts.h */\r
44 \r
45 #include "lwip/tcp.h"\r
46 #include "lwip/def.h"\r
47 #include "lwip/mem.h"\r
48 #include "lwip/memp.h"\r
49 #include "lwip/sys.h"\r
50 #include "lwip/ip_addr.h"\r
51 #include "lwip/netif.h"\r
52 #include "lwip/inet.h"\r
53 #include "lwip/inet_chksum.h"\r
54 #include "lwip/stats.h"\r
55 #include "lwip/snmp.h"\r
56 \r
57 #include <string.h>\r
58 \r
59 /* Forward declarations.*/\r
60 static void tcp_output_segment(struct tcp_seg *seg, struct tcp_pcb *pcb);\r
61 \r
62 /**\r
63  * Called by tcp_close() to send a segment including flags but not data.\r
64  *\r
65  * @param pcb the tcp_pcb over which to send a segment\r
66  * @param flags the flags to set in the segment header\r
67  * @return ERR_OK if sent, another err_t otherwise\r
68  */\r
69 err_t\r
70 tcp_send_ctrl(struct tcp_pcb *pcb, u8_t flags)\r
71 {\r
72   /* no data, no length, flags, copy=1, no optdata, no optdatalen */\r
73   return tcp_enqueue(pcb, NULL, 0, flags, TCP_WRITE_FLAG_COPY, NULL, 0);\r
74 }\r
75 \r
76 /**\r
77  * Write data for sending (but does not send it immediately).\r
78  *\r
79  * It waits in the expectation of more data being sent soon (as\r
80  * it can send them more efficiently by combining them together).\r
81  * To prompt the system to send data now, call tcp_output() after\r
82  * calling tcp_write().\r
83  *\r
84  * @param pcb Protocol control block of the TCP connection to enqueue data for.\r
85  * @param data pointer to the data to send\r
86  * @param len length (in bytes) of the data to send\r
87  * @param apiflags combination of following flags :\r
88  * - TCP_WRITE_FLAG_COPY (0x01) data will be copied into memory belonging to the stack\r
89  * - TCP_WRITE_FLAG_MORE (0x02) for TCP connection, PSH flag will be set on last segment sent,\r
90  * @return ERR_OK if enqueued, another err_t on error\r
91  *\r
92  * @see tcp_write()\r
93  */\r
94 err_t\r
95 tcp_write(struct tcp_pcb *pcb, const void *data, u16_t len, u8_t apiflags)\r
96 {\r
97   LWIP_DEBUGF(TCP_OUTPUT_DEBUG, ("tcp_write(pcb=%p, data=%p, len=%"U16_F", apiflags=%"U16_F")\n", (void *)pcb,\r
98     data, len, (u16_t)apiflags));\r
99   /* connection is in valid state for data transmission? */\r
100   if (pcb->state == ESTABLISHED ||\r
101      pcb->state == CLOSE_WAIT ||\r
102      pcb->state == SYN_SENT ||\r
103      pcb->state == SYN_RCVD) {\r
104     if (len > 0) {\r
105       return tcp_enqueue(pcb, (void *)data, len, 0, apiflags, NULL, 0);\r
106     }\r
107     return ERR_OK;\r
108   } else {\r
109     LWIP_DEBUGF(TCP_OUTPUT_DEBUG | LWIP_DBG_STATE | 3, ("tcp_write() called in invalid state\n"));\r
110     return ERR_CONN;\r
111   }\r
112 }\r
113 \r
114 /**\r
115  * Enqueue either data or TCP options (but not both) for tranmission\r
116  *\r
117  * Called by tcp_connect(), tcp_listen_input(), tcp_send_ctrl() and tcp_write().\r
118  *\r
119  * @param pcb Protocol control block for the TCP connection to enqueue data for.\r
120  * @param arg Pointer to the data to be enqueued for sending.\r
121  * @param len Data length in bytes\r
122  * @param flags tcp header flags to set in the outgoing segment\r
123  * @param apiflags combination of following flags :\r
124  * - TCP_WRITE_FLAG_COPY (0x01) data will be copied into memory belonging to the stack\r
125  * - TCP_WRITE_FLAG_MORE (0x02) for TCP connection, PSH flag will be set on last segment sent,\r
126  * @param optdata\r
127  * @param optlen\r
128  */\r
129 err_t\r
130 tcp_enqueue(struct tcp_pcb *pcb, void *arg, u16_t len,\r
131   u8_t flags, u8_t apiflags,\r
132   u8_t *optdata, u8_t optlen)\r
133 {\r
134   struct pbuf *p;\r
135   struct tcp_seg *seg, *useg, *queue;\r
136   u32_t seqno;\r
137   u16_t left, seglen;\r
138   void *ptr;\r
139   u16_t queuelen;\r
140 \r
141   LWIP_DEBUGF(TCP_OUTPUT_DEBUG, ("tcp_enqueue(pcb=%p, arg=%p, len=%"U16_F", flags=%"X16_F", apiflags=%"U16_F")\n",\r
142     (void *)pcb, arg, len, (u16_t)flags, (u16_t)apiflags));\r
143   LWIP_ERROR("tcp_enqueue: len == 0 || optlen == 0 (programmer violates API)",\r
144       ((len == 0) || (optlen == 0)), return ERR_ARG;);\r
145   LWIP_ERROR("tcp_enqueue: arg == NULL || optdata == NULL (programmer violates API)",\r
146       ((arg == NULL) || (optdata == NULL)), return ERR_ARG;);\r
147   /* fail on too much data */\r
148   if (len > pcb->snd_buf) {\r
149     LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 3, ("tcp_enqueue: too much data (len=%"U16_F" > snd_buf=%"U16_F")\n", len, pcb->snd_buf));\r
150     pcb->flags |= TF_NAGLEMEMERR;\r
151     return ERR_MEM;\r
152   }\r
153   left = len;\r
154   ptr = arg;\r
155 \r
156   /* seqno will be the sequence number of the first segment enqueued\r
157    * by the call to this function. */\r
158   seqno = pcb->snd_lbb;\r
159 \r
160   LWIP_DEBUGF(TCP_QLEN_DEBUG, ("tcp_enqueue: queuelen: %"U16_F"\n", (u16_t)pcb->snd_queuelen));\r
161 \r
162   /* If total number of pbufs on the unsent/unacked queues exceeds the\r
163    * configured maximum, return an error */\r
164   queuelen = pcb->snd_queuelen;\r
165   /* check for configured max queuelen and possible overflow */\r
166   if ((queuelen >= TCP_SND_QUEUELEN) || (queuelen > TCP_SNDQUEUELEN_OVERFLOW)) {\r
167     LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 3, ("tcp_enqueue: too long queue %"U16_F" (max %"U16_F")\n", queuelen, TCP_SND_QUEUELEN));\r
168     TCP_STATS_INC(tcp.memerr);\r
169     pcb->flags |= TF_NAGLEMEMERR;\r
170     return ERR_MEM;\r
171   }\r
172   if (queuelen != 0) {\r
173     LWIP_ASSERT("tcp_enqueue: pbufs on queue => at least one queue non-empty",\r
174       pcb->unacked != NULL || pcb->unsent != NULL);\r
175   } else {\r
176     LWIP_ASSERT("tcp_enqueue: no pbufs on queue => both queues empty",\r
177       pcb->unacked == NULL && pcb->unsent == NULL);\r
178   }\r
179 \r
180   /* First, break up the data into segments and tuck them together in\r
181    * the local "queue" variable. */\r
182   useg = queue = seg = NULL;\r
183   seglen = 0;\r
184   while (queue == NULL || left > 0) {\r
185 \r
186     /* The segment length should be the MSS if the data to be enqueued\r
187      * is larger than the MSS. */\r
188     seglen = left > pcb->mss? pcb->mss: left;\r
189 \r
190     /* Allocate memory for tcp_seg, and fill in fields. */\r
191     seg = memp_malloc(MEMP_TCP_SEG);\r
192     if (seg == NULL) {\r
193       LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 2, ("tcp_enqueue: could not allocate memory for tcp_seg\n"));\r
194       goto memerr;\r
195     }\r
196     seg->next = NULL;\r
197     seg->p = NULL;\r
198 \r
199     /* first segment of to-be-queued data? */\r
200     if (queue == NULL) {\r
201       queue = seg;\r
202     }\r
203     /* subsequent segments of to-be-queued data */\r
204     else {\r
205       /* Attach the segment to the end of the queued segments */\r
206       LWIP_ASSERT("useg != NULL", useg != NULL);\r
207       useg->next = seg;\r
208     }\r
209     /* remember last segment of to-be-queued data for next iteration */\r
210     useg = seg;\r
211 \r
212     /* If copy is set, memory should be allocated\r
213      * and data copied into pbuf, otherwise data comes from\r
214      * ROM or other static memory, and need not be copied. If\r
215      * optdata is != NULL, we have options instead of data. */\r
216 \r
217     /* options? */\r
218     if (optdata != NULL) {\r
219       if ((seg->p = pbuf_alloc(PBUF_TRANSPORT, optlen, PBUF_RAM)) == NULL) {\r
220         goto memerr;\r
221       }\r
222       LWIP_ASSERT("check that first pbuf can hold optlen",\r
223                   (seg->p->len >= optlen));\r
224       queuelen += pbuf_clen(seg->p);\r
225       seg->dataptr = seg->p->payload;\r
226     }\r
227     /* copy from volatile memory? */\r
228     else if (apiflags & TCP_WRITE_FLAG_COPY) {\r
229       if ((seg->p = pbuf_alloc(PBUF_TRANSPORT, seglen, PBUF_RAM)) == NULL) {\r
230         LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 2, ("tcp_enqueue : could not allocate memory for pbuf copy size %"U16_F"\n", seglen));\r
231         goto memerr;\r
232       }\r
233       LWIP_ASSERT("check that first pbuf can hold the complete seglen",\r
234                   (seg->p->len >= seglen));\r
235       queuelen += pbuf_clen(seg->p);\r
236       if (arg != NULL) {\r
237         MEMCPY(seg->p->payload, ptr, seglen);\r
238       }\r
239       seg->dataptr = seg->p->payload;\r
240     }\r
241     /* do not copy data */\r
242     else {\r
243       /* First, allocate a pbuf for holding the data.\r
244        * since the referenced data is available at least until it is sent out on the\r
245        * link (as it has to be ACKed by the remote party) we can safely use PBUF_ROM\r
246        * instead of PBUF_REF here.\r
247        */\r
248       if ((p = pbuf_alloc(PBUF_TRANSPORT, seglen, PBUF_ROM)) == NULL) {\r
249         LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 2, ("tcp_enqueue: could not allocate memory for zero-copy pbuf\n"));\r
250         goto memerr;\r
251       }\r
252       ++queuelen;\r
253       /* reference the non-volatile payload data */\r
254       p->payload = ptr;\r
255       seg->dataptr = ptr;\r
256 \r
257       /* Second, allocate a pbuf for the headers. */\r
258       if ((seg->p = pbuf_alloc(PBUF_TRANSPORT, 0, PBUF_RAM)) == NULL) {\r
259         /* If allocation fails, we have to deallocate the data pbuf as\r
260          * well. */\r
261         pbuf_free(p);\r
262         LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 2, ("tcp_enqueue: could not allocate memory for header pbuf\n"));\r
263         goto memerr;\r
264       }\r
265       queuelen += pbuf_clen(seg->p);\r
266 \r
267       /* Concatenate the headers and data pbufs together. */\r
268       pbuf_cat(seg->p/*header*/, p/*data*/);\r
269       p = NULL;\r
270     }\r
271 \r
272     /* Now that there are more segments queued, we check again if the\r
273     length of the queue exceeds the configured maximum or overflows. */\r
274     if ((queuelen > TCP_SND_QUEUELEN) || (queuelen > TCP_SNDQUEUELEN_OVERFLOW)) {\r
275       LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 2, ("tcp_enqueue: queue too long %"U16_F" (%"U16_F")\n", queuelen, TCP_SND_QUEUELEN));\r
276       goto memerr;\r
277     }\r
278 \r
279     seg->len = seglen;\r
280 \r
281     /* build TCP header */\r
282     if (pbuf_header(seg->p, TCP_HLEN)) {\r
283       LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 2, ("tcp_enqueue: no room for TCP header in pbuf.\n"));\r
284       TCP_STATS_INC(tcp.err);\r
285       goto memerr;\r
286     }\r
287     seg->tcphdr = seg->p->payload;\r
288     seg->tcphdr->src = htons(pcb->local_port);\r
289     seg->tcphdr->dest = htons(pcb->remote_port);\r
290     seg->tcphdr->seqno = htonl(seqno);\r
291     seg->tcphdr->urgp = 0;\r
292     TCPH_FLAGS_SET(seg->tcphdr, flags);\r
293     /* don't fill in tcphdr->ackno and tcphdr->wnd until later */\r
294 \r
295     /* Copy the options into the header, if they are present. */\r
296     if (optdata == NULL) {\r
297       TCPH_HDRLEN_SET(seg->tcphdr, 5);\r
298     }\r
299     else {\r
300       TCPH_HDRLEN_SET(seg->tcphdr, (5 + optlen / 4));\r
301       /* Copy options into data portion of segment.\r
302        Options can thus only be sent in non data carrying\r
303        segments such as SYN|ACK. */\r
304       SMEMCPY(seg->dataptr, optdata, optlen);\r
305     }\r
306     LWIP_DEBUGF(TCP_OUTPUT_DEBUG | LWIP_DBG_TRACE, ("tcp_enqueue: queueing %"U32_F":%"U32_F" (0x%"X16_F")\n",\r
307       ntohl(seg->tcphdr->seqno),\r
308       ntohl(seg->tcphdr->seqno) + TCP_TCPLEN(seg),\r
309       (u16_t)flags));\r
310 \r
311     left -= seglen;\r
312     seqno += seglen;\r
313     ptr = (void *)((u8_t *)ptr + seglen);\r
314   }\r
315 \r
316   /* Now that the data to be enqueued has been broken up into TCP\r
317   segments in the queue variable, we add them to the end of the\r
318   pcb->unsent queue. */\r
319   if (pcb->unsent == NULL) {\r
320     useg = NULL;\r
321   }\r
322   else {\r
323     for (useg = pcb->unsent; useg->next != NULL; useg = useg->next){}\r
324   }\r
325   /* { useg is last segment on the unsent queue, NULL if list is empty } */\r
326 \r
327   /* If there is room in the last pbuf on the unsent queue,\r
328   chain the first pbuf on the queue together with that. */\r
329   if (useg != NULL &&\r
330     TCP_TCPLEN(useg) != 0 &&\r
331     !(TCPH_FLAGS(useg->tcphdr) & (TCP_SYN | TCP_FIN)) &&\r
332     !(flags & (TCP_SYN | TCP_FIN)) &&\r
333     /* fit within max seg size */\r
334     useg->len + queue->len <= pcb->mss) {\r
335     /* Remove TCP header from first segment of our to-be-queued list */\r
336     if(pbuf_header(queue->p, -TCP_HLEN)) {\r
337       /* Can we cope with this failing?  Just assert for now */\r
338       LWIP_ASSERT("pbuf_header failed\n", 0);\r
339       TCP_STATS_INC(tcp.err);\r
340       goto memerr;\r
341     }\r
342     pbuf_cat(useg->p, queue->p);\r
343     useg->len += queue->len;\r
344     useg->next = queue->next;\r
345 \r
346     LWIP_DEBUGF(TCP_OUTPUT_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("tcp_enqueue: chaining segments, new len %"U16_F"\n", useg->len));\r
347     if (seg == queue) {\r
348       seg = NULL;\r
349     }\r
350     memp_free(MEMP_TCP_SEG, queue);\r
351   }\r
352   else {\r
353     /* empty list */\r
354     if (useg == NULL) {\r
355       /* initialize list with this segment */\r
356       pcb->unsent = queue;\r
357     }\r
358     /* enqueue segment */\r
359     else {\r
360       useg->next = queue;\r
361     }\r
362   }\r
363   if ((flags & TCP_SYN) || (flags & TCP_FIN)) {\r
364     ++len;\r
365   }\r
366   if (flags & TCP_FIN) {\r
367     pcb->flags |= TF_FIN;\r
368   }\r
369   pcb->snd_lbb += len;\r
370 \r
371   pcb->snd_buf -= len;\r
372 \r
373   /* update number of segments on the queues */\r
374   pcb->snd_queuelen = queuelen;\r
375   LWIP_DEBUGF(TCP_QLEN_DEBUG, ("tcp_enqueue: %"S16_F" (after enqueued)\n", pcb->snd_queuelen));\r
376   if (pcb->snd_queuelen != 0) {\r
377     LWIP_ASSERT("tcp_enqueue: valid queue length",\r
378       pcb->unacked != NULL || pcb->unsent != NULL);\r
379   }\r
380 \r
381   /* Set the PSH flag in the last segment that we enqueued, but only\r
382   if the segment has data (indicated by seglen > 0). */\r
383   if (seg != NULL && seglen > 0 && seg->tcphdr != NULL && ((apiflags & TCP_WRITE_FLAG_MORE)==0)) {\r
384     TCPH_SET_FLAG(seg->tcphdr, TCP_PSH);\r
385   }\r
386 \r
387   return ERR_OK;\r
388 memerr:\r
389   pcb->flags |= TF_NAGLEMEMERR;\r
390   TCP_STATS_INC(tcp.memerr);\r
391 \r
392   if (queue != NULL) {\r
393     tcp_segs_free(queue);\r
394   }\r
395   if (pcb->snd_queuelen != 0) {\r
396     LWIP_ASSERT("tcp_enqueue: valid queue length", pcb->unacked != NULL ||\r
397       pcb->unsent != NULL);\r
398   }\r
399   LWIP_DEBUGF(TCP_QLEN_DEBUG | LWIP_DBG_STATE, ("tcp_enqueue: %"S16_F" (with mem err)\n", pcb->snd_queuelen));\r
400   return ERR_MEM;\r
401 }\r
402 \r
403 /**\r
404  * Find out what we can send and send it\r
405  *\r
406  * @param pcb Protocol control block for the TCP connection to send data\r
407  * @return ERR_OK if data has been sent or nothing to send\r
408  *         another err_t on error\r
409  */\r
410 err_t\r
411 tcp_output(struct tcp_pcb *pcb)\r
412 {\r
413   struct pbuf *p;\r
414   struct tcp_hdr *tcphdr;\r
415   struct tcp_seg *seg, *useg;\r
416   u32_t wnd;\r
417 #if TCP_CWND_DEBUG\r
418   s16_t i = 0;\r
419 #endif /* TCP_CWND_DEBUG */\r
420 \r
421   /* First, check if we are invoked by the TCP input processing\r
422      code. If so, we do not output anything. Instead, we rely on the\r
423      input processing code to call us when input processing is done\r
424      with. */\r
425   if (tcp_input_pcb == pcb) {\r
426     return ERR_OK;\r
427   }\r
428 \r
429   wnd = LWIP_MIN(pcb->snd_wnd, pcb->cwnd);\r
430 \r
431   seg = pcb->unsent;\r
432 \r
433   /* useg should point to last segment on unacked queue */\r
434   useg = pcb->unacked;\r
435   if (useg != NULL) {\r
436     for (; useg->next != NULL; useg = useg->next){}\r
437   }\r
438 \r
439   /* If the TF_ACK_NOW flag is set and no data will be sent (either\r
440    * because the ->unsent queue is empty or because the window does\r
441    * not allow it), construct an empty ACK segment and send it.\r
442    *\r
443    * If data is to be sent, we will just piggyback the ACK (see below).\r
444    */\r
445   if (pcb->flags & TF_ACK_NOW &&\r
446      (seg == NULL ||\r
447       ntohl(seg->tcphdr->seqno) - pcb->lastack + seg->len > wnd)) {\r
448     p = pbuf_alloc(PBUF_IP, TCP_HLEN, PBUF_RAM);\r
449     if (p == NULL) {\r
450       LWIP_DEBUGF(TCP_OUTPUT_DEBUG, ("tcp_output: (ACK) could not allocate pbuf\n"));\r
451       return ERR_BUF;\r
452     }\r
453     LWIP_DEBUGF(TCP_OUTPUT_DEBUG, ("tcp_output: sending ACK for %"U32_F"\n", pcb->rcv_nxt));\r
454     /* remove ACK flags from the PCB, as we send an empty ACK now */\r
455     pcb->flags &= ~(TF_ACK_DELAY | TF_ACK_NOW);\r
456 \r
457     tcphdr = p->payload;\r
458     tcphdr->src = htons(pcb->local_port);\r
459     tcphdr->dest = htons(pcb->remote_port);\r
460     tcphdr->seqno = htonl(pcb->snd_nxt);\r
461     tcphdr->ackno = htonl(pcb->rcv_nxt);\r
462     TCPH_FLAGS_SET(tcphdr, TCP_ACK);\r
463     tcphdr->wnd = htons(pcb->rcv_ann_wnd);\r
464     tcphdr->urgp = 0;\r
465     TCPH_HDRLEN_SET(tcphdr, 5);\r
466 \r
467     tcphdr->chksum = 0;\r
468 #if CHECKSUM_GEN_TCP\r
469     tcphdr->chksum = inet_chksum_pseudo(p, &(pcb->local_ip), &(pcb->remote_ip),\r
470           IP_PROTO_TCP, p->tot_len);\r
471 #endif\r
472 #if LWIP_NETIF_HWADDRHINT\r
473     {\r
474       struct netif *netif;\r
475       netif = ip_route(&pcb->remote_ip);\r
476       if(netif != NULL){\r
477         netif->addr_hint = &(pcb->addr_hint);\r
478         ip_output_if(p, &(pcb->local_ip), &(pcb->remote_ip), pcb->ttl,\r
479                      pcb->tos, IP_PROTO_TCP, netif);\r
480         netif->addr_hint = NULL;\r
481       }\r
482     }\r
483 #else /* LWIP_NETIF_HWADDRHINT*/\r
484     ip_output(p, &(pcb->local_ip), &(pcb->remote_ip), pcb->ttl, pcb->tos,\r
485         IP_PROTO_TCP);\r
486 #endif /* LWIP_NETIF_HWADDRHINT*/\r
487     pbuf_free(p);\r
488 \r
489     return ERR_OK;\r
490   }\r
491 \r
492 #if TCP_OUTPUT_DEBUG\r
493   if (seg == NULL) {\r
494     LWIP_DEBUGF(TCP_OUTPUT_DEBUG, ("tcp_output: nothing to send (%p)\n",\r
495                                    (void*)pcb->unsent));\r
496   }\r
497 #endif /* TCP_OUTPUT_DEBUG */\r
498 #if TCP_CWND_DEBUG\r
499   if (seg == NULL) {\r
500     LWIP_DEBUGF(TCP_CWND_DEBUG, ("tcp_output: snd_wnd %"U16_F\r
501                                  ", cwnd %"U16_F", wnd %"U32_F\r
502                                  ", seg == NULL, ack %"U32_F"\n",\r
503                                  pcb->snd_wnd, pcb->cwnd, wnd, pcb->lastack));\r
504   } else {\r
505     LWIP_DEBUGF(TCP_CWND_DEBUG,\r
506                 ("tcp_output: snd_wnd %"U16_F", cwnd %"U16_F", wnd %"U32_F\r
507                  ", effwnd %"U32_F", seq %"U32_F", ack %"U32_F"\n",\r
508                  pcb->snd_wnd, pcb->cwnd, wnd,\r
509                  ntohl(seg->tcphdr->seqno) - pcb->lastack + seg->len,\r
510                  ntohl(seg->tcphdr->seqno), pcb->lastack));\r
511   }\r
512 #endif /* TCP_CWND_DEBUG */\r
513   /* data available and window allows it to be sent? */\r
514   while (seg != NULL &&\r
515          ntohl(seg->tcphdr->seqno) - pcb->lastack + seg->len <= wnd) {\r
516     LWIP_ASSERT("RST not expected here!",\r
517                 (TCPH_FLAGS(seg->tcphdr) & TCP_RST) == 0);\r
518     /* Stop sending if the nagle algorithm would prevent it\r
519      * Don't stop:\r
520      * - if tcp_enqueue had a memory error before (prevent delayed ACK timeout) or\r
521      * - if FIN was already enqueued for this PCB (SYN is always alone in a segment -\r
522      *   either seg->next != NULL or pcb->unacked == NULL;\r
523      *   RST is no sent using tcp_enqueue/tcp_output.\r
524      */\r
525     if((tcp_do_output_nagle(pcb) == 0) &&\r
526       ((pcb->flags & (TF_NAGLEMEMERR | TF_FIN)) == 0)){\r
527       break;\r
528     }\r
529 #if TCP_CWND_DEBUG\r
530     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",\r
531                             pcb->snd_wnd, pcb->cwnd, wnd,\r
532                             ntohl(seg->tcphdr->seqno) + seg->len -\r
533                             pcb->lastack,\r
534                             ntohl(seg->tcphdr->seqno), pcb->lastack, i));\r
535     ++i;\r
536 #endif /* TCP_CWND_DEBUG */\r
537 \r
538     pcb->unsent = seg->next;\r
539 \r
540     if (pcb->state != SYN_SENT) {\r
541       TCPH_SET_FLAG(seg->tcphdr, TCP_ACK);\r
542       pcb->flags &= ~(TF_ACK_DELAY | TF_ACK_NOW);\r
543     }\r
544 \r
545     tcp_output_segment(seg, pcb);\r
546     pcb->snd_nxt = ntohl(seg->tcphdr->seqno) + TCP_TCPLEN(seg);\r
547     if (TCP_SEQ_LT(pcb->snd_max, pcb->snd_nxt)) {\r
548       pcb->snd_max = pcb->snd_nxt;\r
549     }\r
550     /* put segment on unacknowledged list if length > 0 */\r
551     if (TCP_TCPLEN(seg) > 0) {\r
552       seg->next = NULL;\r
553       /* unacked list is empty? */\r
554       if (pcb->unacked == NULL) {\r
555         pcb->unacked = seg;\r
556         useg = seg;\r
557       /* unacked list is not empty? */\r
558       } else {\r
559         /* In the case of fast retransmit, the packet should not go to the tail\r
560          * of the unacked queue, but rather at the head. We need to check for\r
561          * this case. -STJ Jul 27, 2004 */\r
562         if (TCP_SEQ_LT(ntohl(seg->tcphdr->seqno), ntohl(useg->tcphdr->seqno))){\r
563           /* add segment to head of unacked list */\r
564           seg->next = pcb->unacked;\r
565           pcb->unacked = seg;\r
566         } else {\r
567           /* add segment to tail of unacked list */\r
568           useg->next = seg;\r
569           useg = useg->next;\r
570         }\r
571       }\r
572     /* do not queue empty segments on the unacked list */\r
573     } else {\r
574       tcp_seg_free(seg);\r
575     }\r
576     seg = pcb->unsent;\r
577   }\r
578 \r
579   if (seg != NULL && pcb->persist_backoff == 0 &&\r
580       ntohl(seg->tcphdr->seqno) - pcb->lastack + seg->len > pcb->snd_wnd) {\r
581     /* prepare for persist timer */\r
582     pcb->persist_cnt = 0;\r
583     pcb->persist_backoff = 1;\r
584   }\r
585 \r
586   pcb->flags &= ~TF_NAGLEMEMERR;\r
587   return ERR_OK;\r
588 }\r
589 \r
590 /**\r
591  * Called by tcp_output() to actually send a TCP segment over IP.\r
592  *\r
593  * @param seg the tcp_seg to send\r
594  * @param pcb the tcp_pcb for the TCP connection used to send the segment\r
595  */\r
596 static void\r
597 tcp_output_segment(struct tcp_seg *seg, struct tcp_pcb *pcb)\r
598 {\r
599   u16_t len;\r
600   struct netif *netif;\r
601 \r
602   /** @bug Exclude retransmitted segments from this count. */\r
603   snmp_inc_tcpoutsegs();\r
604 \r
605   /* The TCP header has already been constructed, but the ackno and\r
606    wnd fields remain. */\r
607   seg->tcphdr->ackno = htonl(pcb->rcv_nxt);\r
608 \r
609   /* advertise our receive window size in this TCP segment */\r
610   seg->tcphdr->wnd = htons(pcb->rcv_ann_wnd);\r
611 \r
612   /* If we don't have a local IP address, we get one by\r
613      calling ip_route(). */\r
614   if (ip_addr_isany(&(pcb->local_ip))) {\r
615     netif = ip_route(&(pcb->remote_ip));\r
616     if (netif == NULL) {\r
617       return;\r
618     }\r
619     ip_addr_set(&(pcb->local_ip), &(netif->ip_addr));\r
620   }\r
621 \r
622   /* Set retransmission timer running if it is not currently enabled */\r
623   if(pcb->rtime == -1)\r
624     pcb->rtime = 0;\r
625 \r
626   if (pcb->rttest == 0) {\r
627     pcb->rttest = tcp_ticks;\r
628     pcb->rtseq = ntohl(seg->tcphdr->seqno);\r
629 \r
630     LWIP_DEBUGF(TCP_RTO_DEBUG, ("tcp_output_segment: rtseq %"U32_F"\n", pcb->rtseq));\r
631   }\r
632   LWIP_DEBUGF(TCP_OUTPUT_DEBUG, ("tcp_output_segment: %"U32_F":%"U32_F"\n",\r
633           htonl(seg->tcphdr->seqno), htonl(seg->tcphdr->seqno) +\r
634           seg->len));\r
635 \r
636   len = (u16_t)((u8_t *)seg->tcphdr - (u8_t *)seg->p->payload);\r
637 \r
638   seg->p->len -= len;\r
639   seg->p->tot_len -= len;\r
640 \r
641   seg->p->payload = seg->tcphdr;\r
642 \r
643   seg->tcphdr->chksum = 0;\r
644 #if CHECKSUM_GEN_TCP\r
645   seg->tcphdr->chksum = inet_chksum_pseudo(seg->p,\r
646              &(pcb->local_ip),\r
647              &(pcb->remote_ip),\r
648              IP_PROTO_TCP, seg->p->tot_len);\r
649 #endif\r
650   TCP_STATS_INC(tcp.xmit);\r
651 \r
652 #if LWIP_NETIF_HWADDRHINT\r
653   {\r
654     struct netif *netif;\r
655     netif = ip_route(&pcb->remote_ip);\r
656     if(netif != NULL){\r
657       netif->addr_hint = &(pcb->addr_hint);\r
658       ip_output_if(seg->p, &(pcb->local_ip), &(pcb->remote_ip), pcb->ttl,\r
659                    pcb->tos, IP_PROTO_TCP, netif);\r
660       netif->addr_hint = NULL;\r
661     }\r
662   }\r
663 #else /* LWIP_NETIF_HWADDRHINT*/\r
664   ip_output(seg->p, &(pcb->local_ip), &(pcb->remote_ip), pcb->ttl, pcb->tos,\r
665       IP_PROTO_TCP);\r
666 #endif /* LWIP_NETIF_HWADDRHINT*/\r
667 }\r
668 \r
669 /**\r
670  * Send a TCP RESET packet (empty segment with RST flag set) either to\r
671  * abort a connection or to show that there is no matching local connection\r
672  * for a received segment.\r
673  *\r
674  * Called by tcp_abort() (to abort a local connection), tcp_input() (if no\r
675  * matching local pcb was found), tcp_listen_input() (if incoming segment\r
676  * has ACK flag set) and tcp_process() (received segment in the wrong state)\r
677  *\r
678  * Since a RST segment is in most cases not sent for an active connection,\r
679  * tcp_rst() has a number of arguments that are taken from a tcp_pcb for\r
680  * most other segment output functions.\r
681  *\r
682  * @param seqno the sequence number to use for the outgoing segment\r
683  * @param ackno the acknowledge number to use for the outgoing segment\r
684  * @param local_ip the local IP address to send the segment from\r
685  * @param remote_ip the remote IP address to send the segment to\r
686  * @param local_port the local TCP port to send the segment from\r
687  * @param remote_port the remote TCP port to send the segment to\r
688  */\r
689 void\r
690 tcp_rst(u32_t seqno, u32_t ackno,\r
691   struct ip_addr *local_ip, struct ip_addr *remote_ip,\r
692   u16_t local_port, u16_t remote_port)\r
693 {\r
694   struct pbuf *p;\r
695   struct tcp_hdr *tcphdr;\r
696   p = pbuf_alloc(PBUF_IP, TCP_HLEN, PBUF_RAM);\r
697   if (p == NULL) {\r
698       LWIP_DEBUGF(TCP_DEBUG, ("tcp_rst: could not allocate memory for pbuf\n"));\r
699       return;\r
700   }\r
701   LWIP_ASSERT("check that first pbuf can hold struct tcp_hdr",\r
702               (p->len >= sizeof(struct tcp_hdr)));\r
703 \r
704   tcphdr = p->payload;\r
705   tcphdr->src = htons(local_port);\r
706   tcphdr->dest = htons(remote_port);\r
707   tcphdr->seqno = htonl(seqno);\r
708   tcphdr->ackno = htonl(ackno);\r
709   TCPH_FLAGS_SET(tcphdr, TCP_RST | TCP_ACK);\r
710   tcphdr->wnd = htons(TCP_WND);\r
711   tcphdr->urgp = 0;\r
712   TCPH_HDRLEN_SET(tcphdr, 5);\r
713 \r
714   tcphdr->chksum = 0;\r
715 #if CHECKSUM_GEN_TCP\r
716   tcphdr->chksum = inet_chksum_pseudo(p, local_ip, remote_ip,\r
717               IP_PROTO_TCP, p->tot_len);\r
718 #endif\r
719   TCP_STATS_INC(tcp.xmit);\r
720   snmp_inc_tcpoutrsts();\r
721    /* Send output with hardcoded TTL since we have no access to the pcb */\r
722   ip_output(p, local_ip, remote_ip, TCP_TTL, 0, IP_PROTO_TCP);\r
723   pbuf_free(p);\r
724   LWIP_DEBUGF(TCP_RST_DEBUG, ("tcp_rst: seqno %"U32_F" ackno %"U32_F".\n", seqno, ackno));\r
725 }\r
726 \r
727 /**\r
728  * Requeue all unacked segments for retransmission\r
729  *\r
730  * Called by tcp_slowtmr() for slow retransmission.\r
731  *\r
732  * @param pcb the tcp_pcb for which to re-enqueue all unacked segments\r
733  */\r
734 void\r
735 tcp_rexmit_rto(struct tcp_pcb *pcb)\r
736 {\r
737   struct tcp_seg *seg;\r
738 \r
739   if (pcb->unacked == NULL) {\r
740     return;\r
741   }\r
742 \r
743   /* Move all unacked segments to the head of the unsent queue */\r
744   for (seg = pcb->unacked; seg->next != NULL; seg = seg->next){}\r
745   /* concatenate unsent queue after unacked queue */\r
746   seg->next = pcb->unsent;\r
747   /* unsent queue is the concatenated queue (of unacked, unsent) */\r
748   pcb->unsent = pcb->unacked;\r
749   /* unacked queue is now empty */\r
750   pcb->unacked = NULL;\r
751 \r
752   pcb->snd_nxt = ntohl(pcb->unsent->tcphdr->seqno);\r
753   /* increment number of retransmissions */\r
754   ++pcb->nrtx;\r
755 \r
756   /* Don't take any RTT measurements after retransmitting. */\r
757   pcb->rttest = 0;\r
758 \r
759   /* Do the actual retransmission */\r
760   tcp_output(pcb);\r
761 }\r
762 \r
763 /**\r
764  * Requeue the first unacked segment for retransmission\r
765  *\r
766  * Called by tcp_receive() for fast retramsmit.\r
767  *\r
768  * @param pcb the tcp_pcb for which to retransmit the first unacked segment\r
769  */\r
770 void\r
771 tcp_rexmit(struct tcp_pcb *pcb)\r
772 {\r
773   struct tcp_seg *seg;\r
774 \r
775   if (pcb->unacked == NULL) {\r
776     return;\r
777   }\r
778 \r
779   /* Move the first unacked segment to the unsent queue */\r
780   seg = pcb->unacked->next;\r
781   pcb->unacked->next = pcb->unsent;\r
782   pcb->unsent = pcb->unacked;\r
783   pcb->unacked = seg;\r
784 \r
785   pcb->snd_nxt = ntohl(pcb->unsent->tcphdr->seqno);\r
786 \r
787   ++pcb->nrtx;\r
788 \r
789   /* Don't take any rtt measurements after retransmitting. */\r
790   pcb->rttest = 0;\r
791 \r
792   /* Do the actual retransmission. */\r
793   snmp_inc_tcpretranssegs();\r
794   tcp_output(pcb);\r
795 }\r
796 \r
797 /**\r
798  * Send keepalive packets to keep a connection active although\r
799  * no data is sent over it.\r
800  *\r
801  * Called by tcp_slowtmr()\r
802  *\r
803  * @param pcb the tcp_pcb for which to send a keepalive packet\r
804  */\r
805 void\r
806 tcp_keepalive(struct tcp_pcb *pcb)\r
807 {\r
808   struct pbuf *p;\r
809   struct tcp_hdr *tcphdr;\r
810 \r
811   LWIP_DEBUGF(TCP_DEBUG, ("tcp_keepalive: sending KEEPALIVE probe to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",\r
812                           ip4_addr1(&pcb->remote_ip), ip4_addr2(&pcb->remote_ip),\r
813                           ip4_addr3(&pcb->remote_ip), ip4_addr4(&pcb->remote_ip)));\r
814 \r
815   LWIP_DEBUGF(TCP_DEBUG, ("tcp_keepalive: tcp_ticks %"U32_F"   pcb->tmr %"U32_F" pcb->keep_cnt_sent %"U16_F"\n",\r
816                           tcp_ticks, pcb->tmr, pcb->keep_cnt_sent));\r
817 \r
818   p = pbuf_alloc(PBUF_IP, TCP_HLEN, PBUF_RAM);\r
819 \r
820   if(p == NULL) {\r
821     LWIP_DEBUGF(TCP_DEBUG,\r
822                 ("tcp_keepalive: could not allocate memory for pbuf\n"));\r
823     return;\r
824   }\r
825   LWIP_ASSERT("check that first pbuf can hold struct tcp_hdr",\r
826               (p->len >= sizeof(struct tcp_hdr)));\r
827 \r
828   tcphdr = p->payload;\r
829   tcphdr->src = htons(pcb->local_port);\r
830   tcphdr->dest = htons(pcb->remote_port);\r
831   tcphdr->seqno = htonl(pcb->snd_nxt - 1);\r
832   tcphdr->ackno = htonl(pcb->rcv_nxt);\r
833   TCPH_FLAGS_SET(tcphdr, 0);\r
834   tcphdr->wnd = htons(pcb->rcv_ann_wnd);\r
835   tcphdr->urgp = 0;\r
836   TCPH_HDRLEN_SET(tcphdr, 5);\r
837 \r
838   tcphdr->chksum = 0;\r
839 #if CHECKSUM_GEN_TCP\r
840   tcphdr->chksum = inet_chksum_pseudo(p, &pcb->local_ip, &pcb->remote_ip,\r
841                                       IP_PROTO_TCP, p->tot_len);\r
842 #endif\r
843   TCP_STATS_INC(tcp.xmit);\r
844 \r
845   /* Send output to IP */\r
846 #if LWIP_NETIF_HWADDRHINT\r
847   {\r
848     struct netif *netif;\r
849     netif = ip_route(&pcb->remote_ip);\r
850     if(netif != NULL){\r
851       netif->addr_hint = &(pcb->addr_hint);\r
852       ip_output_if(p, &(pcb->local_ip), &(pcb->remote_ip), pcb->ttl,\r
853                    0, IP_PROTO_TCP, netif);\r
854       netif->addr_hint = NULL;\r
855     }\r
856   }\r
857 #else /* LWIP_NETIF_HWADDRHINT*/\r
858   ip_output(p, &pcb->local_ip, &pcb->remote_ip, pcb->ttl, 0, IP_PROTO_TCP);\r
859 #endif /* LWIP_NETIF_HWADDRHINT*/\r
860 \r
861   pbuf_free(p);\r
862 \r
863   LWIP_DEBUGF(TCP_DEBUG, ("tcp_keepalive: seqno %"U32_F" ackno %"U32_F".\n",\r
864                           pcb->snd_nxt - 1, pcb->rcv_nxt));\r
865 }\r
866 \r
867 \r
868 /**\r
869  * Send persist timer zero-window probes to keep a connection active\r
870  * when a window update is lost.\r
871  *\r
872  * Called by tcp_slowtmr()\r
873  *\r
874  * @param pcb the tcp_pcb for which to send a zero-window probe packet\r
875  */\r
876 void\r
877 tcp_zero_window_probe(struct tcp_pcb *pcb)\r
878 {\r
879   struct pbuf *p;\r
880   struct tcp_hdr *tcphdr;\r
881   struct tcp_seg *seg;\r
882 \r
883   LWIP_DEBUGF(TCP_DEBUG,\r
884               ("tcp_zero_window_probe: sending ZERO WINDOW probe to %"\r
885                U16_F".%"U16_F".%"U16_F".%"U16_F"\n",\r
886                ip4_addr1(&pcb->remote_ip), ip4_addr2(&pcb->remote_ip),\r
887                ip4_addr3(&pcb->remote_ip), ip4_addr4(&pcb->remote_ip)));\r
888 \r
889   LWIP_DEBUGF(TCP_DEBUG,\r
890               ("tcp_zero_window_probe: tcp_ticks %"U32_F\r
891                "   pcb->tmr %"U32_F" pcb->keep_cnt_sent %"U16_F"\n",\r
892                tcp_ticks, pcb->tmr, pcb->keep_cnt_sent));\r
893 \r
894   seg = pcb->unacked;\r
895 \r
896   if(seg == NULL)\r
897     seg = pcb->unsent;\r
898 \r
899   if(seg == NULL)\r
900     return;\r
901 \r
902   p = pbuf_alloc(PBUF_IP, TCP_HLEN + 1, PBUF_RAM);\r
903 \r
904   if(p == NULL) {\r
905     LWIP_DEBUGF(TCP_DEBUG, ("tcp_zero_window_probe: no memory for pbuf\n"));\r
906     return;\r
907   }\r
908   LWIP_ASSERT("check that first pbuf can hold struct tcp_hdr",\r
909               (p->len >= sizeof(struct tcp_hdr)));\r
910 \r
911   tcphdr = p->payload;\r
912   tcphdr->src = htons(pcb->local_port);\r
913   tcphdr->dest = htons(pcb->remote_port);\r
914   tcphdr->seqno = seg->tcphdr->seqno;\r
915   tcphdr->ackno = htonl(pcb->rcv_nxt);\r
916   TCPH_FLAGS_SET(tcphdr, 0);\r
917   tcphdr->wnd = htons(pcb->rcv_ann_wnd);\r
918   tcphdr->urgp = 0;\r
919   TCPH_HDRLEN_SET(tcphdr, 5);\r
920 \r
921   /* Copy in one byte from the head of the unacked queue */\r
922   *((char *)p->payload + sizeof(struct tcp_hdr)) = *(char *)seg->dataptr;\r
923 \r
924   tcphdr->chksum = 0;\r
925 #if CHECKSUM_GEN_TCP\r
926   tcphdr->chksum = inet_chksum_pseudo(p, &pcb->local_ip, &pcb->remote_ip,\r
927                                       IP_PROTO_TCP, p->tot_len);\r
928 #endif\r
929   TCP_STATS_INC(tcp.xmit);\r
930 \r
931   /* Send output to IP */\r
932 #if LWIP_NETIF_HWADDRHINT\r
933   {\r
934     struct netif *netif;\r
935     netif = ip_route(&pcb->remote_ip);\r
936     if(netif != NULL){\r
937       netif->addr_hint = &(pcb->addr_hint);\r
938       ip_output_if(p, &(pcb->local_ip), &(pcb->remote_ip), pcb->ttl,\r
939                    0, IP_PROTO_TCP, netif);\r
940       netif->addr_hint = NULL;\r
941     }\r
942   }\r
943 #else /* LWIP_NETIF_HWADDRHINT*/\r
944   ip_output(p, &pcb->local_ip, &pcb->remote_ip, pcb->ttl, 0, IP_PROTO_TCP);\r
945 #endif /* LWIP_NETIF_HWADDRHINT*/\r
946 \r
947   pbuf_free(p);\r
948 \r
949   LWIP_DEBUGF(TCP_DEBUG, ("tcp_zero_window_probe: seqno %"U32_F\r
950                           " ackno %"U32_F".\n",\r
951                           pcb->snd_nxt - 1, pcb->rcv_nxt));\r
952 }\r
953 #endif /* LWIP_TCP */\r