2 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
19 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
21 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
24 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
27 * This file is part of the lwIP TCP/IP stack.
29 * Author: Adam Dunkels <adam@sics.se>
32 #ifndef __LWIP_TCP_IMPL_H__
33 #define __LWIP_TCP_IMPL_H__
37 #if LWIP_TCP /* don't build if not configured for use in lwipopts.h */
41 #include "lwip/pbuf.h"
43 #include "lwip/icmp.h"
46 #include "lwip/ip6_addr.h"
52 /* Functions for interfacing with TCP: */
54 /* Lower layer interface to TCP: */
55 void tcp_init (void); /* Initialize this module. */
56 void tcp_tmr (void); /* Must be called every
58 ms. (Typically 250 ms). */
59 /* It is also possible to call these two functions at the right
60 intervals (instead of calling tcp_tmr()). */
61 void tcp_slowtmr (void);
62 void tcp_fasttmr (void);
65 /* Only used by IP to pass a TCP segment to TCP: */
66 void tcp_input (struct pbuf *p, struct netif *inp);
67 /* Used within the TCP code only: */
68 struct tcp_pcb * tcp_alloc (u8_t prio);
69 void tcp_abandon (struct tcp_pcb *pcb, int reset);
70 err_t tcp_send_empty_ack(struct tcp_pcb *pcb);
71 void tcp_rexmit (struct tcp_pcb *pcb);
72 void tcp_rexmit_rto (struct tcp_pcb *pcb);
73 void tcp_rexmit_fast (struct tcp_pcb *pcb);
74 u32_t tcp_update_rcv_ann_wnd(struct tcp_pcb *pcb);
77 * This is the Nagle algorithm: try to combine user data to send as few TCP
78 * segments as possible. Only send if
79 * - no previously transmitted data on the connection remains unacknowledged or
80 * - the TF_NODELAY flag is set (nagle algorithm turned off for this pcb) or
81 * - the only unsent segment is at least pcb->mss bytes long (or there is more
82 * than one unsent segment - with lwIP, this can happen although unsent->len < mss)
83 * - or if we are in fast-retransmit (TF_INFR)
85 #define tcp_do_output_nagle(tpcb) ((((tpcb)->unacked == NULL) || \
86 ((tpcb)->flags & (TF_NODELAY | TF_INFR)) || \
87 (((tpcb)->unsent != NULL) && (((tpcb)->unsent->next != NULL) || \
88 ((tpcb)->unsent->len >= (tpcb)->mss))) || \
89 ((tcp_sndbuf(tpcb) == 0) || (tcp_sndqueuelen(tpcb) >= TCP_SND_QUEUELEN)) \
91 #define tcp_output_nagle(tpcb) (tcp_do_output_nagle(tpcb) ? tcp_output(tpcb) : ERR_OK)
94 #define TCP_SEQ_LT(a,b) ((s32_t)((a)-(b)) < 0)
95 #define TCP_SEQ_LEQ(a,b) ((s32_t)((a)-(b)) <= 0)
96 #define TCP_SEQ_GT(a,b) ((s32_t)((a)-(b)) > 0)
97 #define TCP_SEQ_GEQ(a,b) ((s32_t)((a)-(b)) >= 0)
99 #if 0 /* see bug #10548 */
100 #define TCP_SEQ_BETWEEN(a,b,c) ((c)-(b) >= (a)-(b))
102 #define TCP_SEQ_BETWEEN(a,b,c) (TCP_SEQ_GEQ(a,b) && TCP_SEQ_LEQ(a,c))
103 #define TCP_FIN 0x01U
104 #define TCP_SYN 0x02U
105 #define TCP_RST 0x04U
106 #define TCP_PSH 0x08U
107 #define TCP_ACK 0x10U
108 #define TCP_URG 0x20U
109 #define TCP_ECE 0x40U
110 #define TCP_CWR 0x80U
112 #define TCP_FLAGS 0x3fU
114 /* Length of the TCP header, excluding options. */
117 #ifndef TCP_TMR_INTERVAL
118 #define TCP_TMR_INTERVAL 250 /* The TCP timer interval in milliseconds. */
119 #endif /* TCP_TMR_INTERVAL */
121 #ifndef TCP_FAST_INTERVAL
122 #define TCP_FAST_INTERVAL TCP_TMR_INTERVAL /* the fine grained timeout in milliseconds */
123 #endif /* TCP_FAST_INTERVAL */
125 #ifndef TCP_SLOW_INTERVAL
126 #define TCP_SLOW_INTERVAL (2*TCP_TMR_INTERVAL) /* the coarse grained timeout in milliseconds */
127 #endif /* TCP_SLOW_INTERVAL */
129 #define TCP_FIN_WAIT_TIMEOUT 20000 /* milliseconds */
130 #define TCP_SYN_RCVD_TIMEOUT 20000 /* milliseconds */
132 #define TCP_OOSEQ_TIMEOUT 6U /* x RTO */
135 #define TCP_MSL 60000UL /* The maximum segment lifetime in milliseconds */
138 /* Keepalive values, compliant with RFC 1122. Don't change this unless you know what you're doing */
139 #ifndef TCP_KEEPIDLE_DEFAULT
140 #define TCP_KEEPIDLE_DEFAULT 7200000UL /* Default KEEPALIVE timer in milliseconds */
143 #ifndef TCP_KEEPINTVL_DEFAULT
144 #define TCP_KEEPINTVL_DEFAULT 75000UL /* Default Time between KEEPALIVE probes in milliseconds */
147 #ifndef TCP_KEEPCNT_DEFAULT
148 #define TCP_KEEPCNT_DEFAULT 9U /* Default Counter for KEEPALIVE probes */
151 #define TCP_MAXIDLE TCP_KEEPCNT_DEFAULT * TCP_KEEPINTVL_DEFAULT /* Maximum KEEPALIVE probe time */
153 /* Fields are (of course) in network byte order.
154 * Some fields are converted to host byte order in tcp_input().
156 #ifdef PACK_STRUCT_USE_INCLUDES
157 # include "arch/bpstruct.h"
161 PACK_STRUCT_FIELD(u16_t src);
162 PACK_STRUCT_FIELD(u16_t dest);
163 PACK_STRUCT_FIELD(u32_t seqno);
164 PACK_STRUCT_FIELD(u32_t ackno);
165 PACK_STRUCT_FIELD(u16_t _hdrlen_rsvd_flags);
166 PACK_STRUCT_FIELD(u16_t wnd);
167 PACK_STRUCT_FIELD(u16_t chksum);
168 PACK_STRUCT_FIELD(u16_t urgp);
169 } PACK_STRUCT_STRUCT;
171 #ifdef PACK_STRUCT_USE_INCLUDES
172 # include "arch/epstruct.h"
175 #define TCPH_OFFSET(phdr) (ntohs((phdr)->_hdrlen_rsvd_flags) >> 8)
176 #define TCPH_HDRLEN(phdr) (ntohs((phdr)->_hdrlen_rsvd_flags) >> 12)
177 #define TCPH_FLAGS(phdr) (ntohs((phdr)->_hdrlen_rsvd_flags) & TCP_FLAGS)
179 #define TCPH_OFFSET_SET(phdr, offset) (phdr)->_hdrlen_rsvd_flags = htons(((offset) << 8) | TCPH_FLAGS(phdr))
180 #define TCPH_HDRLEN_SET(phdr, len) (phdr)->_hdrlen_rsvd_flags = htons(((len) << 12) | TCPH_FLAGS(phdr))
181 #define TCPH_FLAGS_SET(phdr, flags) (phdr)->_hdrlen_rsvd_flags = (((phdr)->_hdrlen_rsvd_flags & PP_HTONS((u16_t)(~(u16_t)(TCP_FLAGS)))) | htons(flags))
182 #define TCPH_HDRLEN_FLAGS_SET(phdr, len, flags) (phdr)->_hdrlen_rsvd_flags = htons(((len) << 12) | (flags))
184 #define TCPH_SET_FLAG(phdr, flags ) (phdr)->_hdrlen_rsvd_flags = ((phdr)->_hdrlen_rsvd_flags | htons(flags))
185 #define TCPH_UNSET_FLAG(phdr, flags) (phdr)->_hdrlen_rsvd_flags = htons(ntohs((phdr)->_hdrlen_rsvd_flags) | (TCPH_FLAGS(phdr) & ~(flags)) )
187 #define TCP_TCPLEN(seg) ((seg)->len + ((TCPH_FLAGS((seg)->tcphdr) & (TCP_FIN | TCP_SYN)) != 0))
189 /** Flags used on input processing, not on pcb->flags
191 #define TF_RESET (u8_t)0x08U /* Connection was reset. */
192 #define TF_CLOSED (u8_t)0x10U /* Connection was sucessfully closed. */
193 #define TF_GOT_FIN (u8_t)0x20U /* Connection was closed by the remote end. */
198 #define TCP_EVENT_ACCEPT(pcb,err,ret) ret = lwip_tcp_event((pcb)->callback_arg, (pcb),\
199 LWIP_EVENT_ACCEPT, NULL, 0, err)
200 #define TCP_EVENT_SENT(pcb,space,ret) ret = lwip_tcp_event((pcb)->callback_arg, (pcb),\
201 LWIP_EVENT_SENT, NULL, space, ERR_OK)
202 #define TCP_EVENT_RECV(pcb,p,err,ret) ret = lwip_tcp_event((pcb)->callback_arg, (pcb),\
203 LWIP_EVENT_RECV, (p), 0, (err))
204 #define TCP_EVENT_CLOSED(pcb,ret) ret = lwip_tcp_event((pcb)->callback_arg, (pcb),\
205 LWIP_EVENT_RECV, NULL, 0, ERR_OK)
206 #define TCP_EVENT_CONNECTED(pcb,err,ret) ret = lwip_tcp_event((pcb)->callback_arg, (pcb),\
207 LWIP_EVENT_CONNECTED, NULL, 0, (err))
208 #define TCP_EVENT_POLL(pcb,ret) ret = lwip_tcp_event((pcb)->callback_arg, (pcb),\
209 LWIP_EVENT_POLL, NULL, 0, ERR_OK)
210 #define TCP_EVENT_ERR(errf,arg,err) lwip_tcp_event((arg), NULL, \
211 LWIP_EVENT_ERR, NULL, 0, (err))
213 #else /* LWIP_EVENT_API */
215 #define TCP_EVENT_ACCEPT(pcb,err,ret) \
217 if((pcb)->accept != NULL) \
218 (ret) = (pcb)->accept((pcb)->callback_arg,(pcb),(err)); \
219 else (ret) = ERR_ARG; \
222 #define TCP_EVENT_SENT(pcb,space,ret) \
224 if((pcb)->sent != NULL) \
225 (ret) = (pcb)->sent((pcb)->callback_arg,(pcb),(space)); \
226 else (ret) = ERR_OK; \
229 #define TCP_EVENT_RECV(pcb,p,err,ret) \
231 if((pcb)->recv != NULL) { \
232 (ret) = (pcb)->recv((pcb)->callback_arg,(pcb),(p),(err));\
234 (ret) = tcp_recv_null(NULL, (pcb), (p), (err)); \
238 #define TCP_EVENT_CLOSED(pcb,ret) \
240 if(((pcb)->recv != NULL)) { \
241 (ret) = (pcb)->recv((pcb)->callback_arg,(pcb),NULL,ERR_OK);\
247 #define TCP_EVENT_CONNECTED(pcb,err,ret) \
249 if((pcb)->connected != NULL) \
250 (ret) = (pcb)->connected((pcb)->callback_arg,(pcb),(err)); \
251 else (ret) = ERR_OK; \
254 #define TCP_EVENT_POLL(pcb,ret) \
256 if((pcb)->poll != NULL) \
257 (ret) = (pcb)->poll((pcb)->callback_arg,(pcb)); \
258 else (ret) = ERR_OK; \
261 #define TCP_EVENT_ERR(errf,arg,err) \
264 (errf)((arg),(err)); \
267 #endif /* LWIP_EVENT_API */
269 /** Enabled extra-check for TCP_OVERSIZE if LWIP_DEBUG is enabled */
270 #if TCP_OVERSIZE && defined(LWIP_DEBUG)
271 #define TCP_OVERSIZE_DBGCHECK 1
273 #define TCP_OVERSIZE_DBGCHECK 0
276 /** Don't generate checksum on copy if CHECKSUM_GEN_TCP is disabled */
277 #define TCP_CHECKSUM_ON_COPY (LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_TCP)
279 /* This structure represents a TCP segment on the unsent, unacked and ooseq queues */
281 struct tcp_seg *next; /* used when putting segements on a queue */
282 struct pbuf *p; /* buffer containing data + TCP header */
283 u16_t len; /* the TCP length of this segment */
284 #if TCP_OVERSIZE_DBGCHECK
285 u16_t oversize_left; /* Extra bytes available at the end of the last
286 pbuf in unsent (used for asserting vs.
287 tcp_pcb.unsent_oversized only) */
288 #endif /* TCP_OVERSIZE_DBGCHECK */
289 #if TCP_CHECKSUM_ON_COPY
292 #endif /* TCP_CHECKSUM_ON_COPY */
294 #define TF_SEG_OPTS_MSS (u8_t)0x01U /* Include MSS option. */
295 #define TF_SEG_OPTS_TS (u8_t)0x02U /* Include timestamp option. */
296 #define TF_SEG_DATA_CHECKSUMMED (u8_t)0x04U /* ALL data (not the header) is
297 checksummed into 'chksum' */
298 struct tcp_hdr *tcphdr; /* the TCP header */
301 #define LWIP_TCP_OPT_LENGTH(flags) \
302 (flags & TF_SEG_OPTS_MSS ? 4 : 0) + \
303 (flags & TF_SEG_OPTS_TS ? 12 : 0)
305 /** This returns a TCP header option for MSS in an u32_t */
306 #define TCP_BUILD_MSS_OPTION(mss) htonl(0x02040000 | ((mss) & 0xFFFF))
308 /* Global variables: */
309 extern struct tcp_pcb *tcp_input_pcb;
310 extern u32_t tcp_ticks;
312 /* The TCP PCB lists. */
313 union tcp_listen_pcbs_t { /* List of all TCP PCBs in LISTEN state. */
314 struct tcp_pcb_listen *listen_pcbs;
315 struct tcp_pcb *pcbs;
317 extern struct tcp_pcb *tcp_bound_pcbs;
318 extern union tcp_listen_pcbs_t tcp_listen_pcbs;
319 extern struct tcp_pcb *tcp_active_pcbs; /* List of all TCP PCBs that are in a
320 state in which they accept or send
322 extern struct tcp_pcb *tcp_tw_pcbs; /* List of all TCP PCBs in TIME-WAIT. */
324 extern struct tcp_pcb *tcp_tmp_pcb; /* Only used for temporary storage. */
326 /* Axioms about the above lists:
327 1) Every TCP PCB that is not CLOSED is in one of the lists.
328 2) A PCB is only in one of the lists.
329 3) All PCBs in the tcp_listen_pcbs list is in LISTEN state.
330 4) All PCBs in the tcp_tw_pcbs list is in TIME-WAIT state.
332 /* Define two macros, TCP_REG and TCP_RMV that registers a TCP PCB
333 with a PCB list or removes a PCB from a list, respectively. */
334 #ifndef TCP_DEBUG_PCB_LISTS
335 #define TCP_DEBUG_PCB_LISTS 0
337 #if TCP_DEBUG_PCB_LISTS
338 #define TCP_REG(pcbs, npcb) do {\
339 LWIP_DEBUGF(TCP_DEBUG, ("TCP_REG %p local port %d\n", (npcb), (npcb)->local_port)); \
340 for(tcp_tmp_pcb = *(pcbs); \
341 tcp_tmp_pcb != NULL; \
342 tcp_tmp_pcb = tcp_tmp_pcb->next) { \
343 LWIP_ASSERT("TCP_REG: already registered\n", tcp_tmp_pcb != (npcb)); \
345 LWIP_ASSERT("TCP_REG: pcb->state != CLOSED", ((pcbs) == &tcp_bound_pcbs) || ((npcb)->state != CLOSED)); \
346 (npcb)->next = *(pcbs); \
347 LWIP_ASSERT("TCP_REG: npcb->next != npcb", (npcb)->next != (npcb)); \
349 LWIP_ASSERT("TCP_RMV: tcp_pcbs sane", tcp_pcbs_sane()); \
350 tcp_timer_needed(); \
352 #define TCP_RMV(pcbs, npcb) do { \
353 LWIP_ASSERT("TCP_RMV: pcbs != NULL", *(pcbs) != NULL); \
354 LWIP_DEBUGF(TCP_DEBUG, ("TCP_RMV: removing %p from %p\n", (npcb), *(pcbs))); \
355 if(*(pcbs) == (npcb)) { \
356 *(pcbs) = (*pcbs)->next; \
357 } else for(tcp_tmp_pcb = *(pcbs); tcp_tmp_pcb != NULL; tcp_tmp_pcb = tcp_tmp_pcb->next) { \
358 if(tcp_tmp_pcb->next == (npcb)) { \
359 tcp_tmp_pcb->next = (npcb)->next; \
363 (npcb)->next = NULL; \
364 LWIP_ASSERT("TCP_RMV: tcp_pcbs sane", tcp_pcbs_sane()); \
365 LWIP_DEBUGF(TCP_DEBUG, ("TCP_RMV: removed %p from %p\n", (npcb), *(pcbs))); \
368 #else /* LWIP_DEBUG */
370 #define TCP_REG(pcbs, npcb) \
372 (npcb)->next = *pcbs; \
374 tcp_timer_needed(); \
377 #define TCP_RMV(pcbs, npcb) \
379 if(*(pcbs) == (npcb)) { \
380 (*(pcbs)) = (*pcbs)->next; \
383 for(tcp_tmp_pcb = *pcbs; \
384 tcp_tmp_pcb != NULL; \
385 tcp_tmp_pcb = tcp_tmp_pcb->next) { \
386 if(tcp_tmp_pcb->next == (npcb)) { \
387 tcp_tmp_pcb->next = (npcb)->next; \
392 (npcb)->next = NULL; \
395 #endif /* LWIP_DEBUG */
398 /* Internal functions: */
399 struct tcp_pcb *tcp_pcb_copy(struct tcp_pcb *pcb);
400 void tcp_pcb_purge(struct tcp_pcb *pcb);
401 void tcp_pcb_remove(struct tcp_pcb **pcblist, struct tcp_pcb *pcb);
403 void tcp_segs_free(struct tcp_seg *seg);
404 void tcp_seg_free(struct tcp_seg *seg);
405 struct tcp_seg *tcp_seg_copy(struct tcp_seg *seg);
407 #define tcp_ack(pcb) \
409 if((pcb)->flags & TF_ACK_DELAY) { \
410 (pcb)->flags &= ~TF_ACK_DELAY; \
411 (pcb)->flags |= TF_ACK_NOW; \
414 (pcb)->flags |= TF_ACK_DELAY; \
418 #define tcp_ack_now(pcb) \
420 (pcb)->flags |= TF_ACK_NOW; \
423 err_t tcp_send_fin(struct tcp_pcb *pcb);
424 err_t tcp_enqueue_flags(struct tcp_pcb *pcb, u8_t flags);
426 void tcp_rexmit_seg(struct tcp_pcb *pcb, struct tcp_seg *seg);
428 void tcp_rst_impl(u32_t seqno, u32_t ackno,
429 ipX_addr_t *local_ip, ipX_addr_t *remote_ip,
430 u16_t local_port, u16_t remote_port
433 #endif /* LWIP_IPV6 */
436 #define tcp_rst(seqno, ackno, local_ip, remote_ip, local_port, remote_port, isipv6) \
437 tcp_rst_impl(seqno, ackno, local_ip, remote_ip, local_port, remote_port, isipv6)
438 #else /* LWIP_IPV6 */
439 #define tcp_rst(seqno, ackno, local_ip, remote_ip, local_port, remote_port, isipv6) \
440 tcp_rst_impl(seqno, ackno, local_ip, remote_ip, local_port, remote_port)
441 #endif /* LWIP_IPV6 */
443 u32_t tcp_next_iss(void);
445 void tcp_keepalive(struct tcp_pcb *pcb);
446 void tcp_zero_window_probe(struct tcp_pcb *pcb);
448 #if TCP_CALCULATE_EFF_SEND_MSS
449 u16_t tcp_eff_send_mss_impl(u16_t sendmss, ipX_addr_t *dest
451 , ipX_addr_t *src, u8_t isipv6
452 #endif /* LWIP_IPV6 */
455 #define tcp_eff_send_mss(sendmss, src, dest, isipv6) tcp_eff_send_mss_impl(sendmss, dest, src, isipv6)
456 #else /* LWIP_IPV6 */
457 #define tcp_eff_send_mss(sendmss, src, dest, isipv6) tcp_eff_send_mss_impl(sendmss, dest)
458 #endif /* LWIP_IPV6 */
459 #endif /* TCP_CALCULATE_EFF_SEND_MSS */
461 #if LWIP_CALLBACK_API
462 err_t tcp_recv_null(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err);
463 #endif /* LWIP_CALLBACK_API */
465 #if TCP_DEBUG || TCP_INPUT_DEBUG || TCP_OUTPUT_DEBUG
466 void tcp_debug_print(struct tcp_hdr *tcphdr);
467 void tcp_debug_print_flags(u8_t flags);
468 void tcp_debug_print_state(enum tcp_state s);
469 void tcp_debug_print_pcbs(void);
470 s16_t tcp_pcbs_sane(void);
472 # define tcp_debug_print(tcphdr)
473 # define tcp_debug_print_flags(flags)
474 # define tcp_debug_print_state(s)
475 # define tcp_debug_print_pcbs()
476 # define tcp_pcbs_sane() 1
477 #endif /* TCP_DEBUG */
479 /** External function (implemented in timers.c), called when TCP detects
480 * that a timer is needed (i.e. active- or time-wait-pcb found). */
481 void tcp_timer_needed(void);
488 #endif /* LWIP_TCP */
490 #endif /* __LWIP_TCP_H__ */