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_H__
33 #define __LWIP_TCP_H__
38 #include "lwip/pbuf.h"
41 #include "lwip/icmp.h"
47 /* Functions for interfacing with TCP: */
49 /* Lower layer interface to TCP: */
50 void tcp_init (void); /* Must be called first to
52 void tcp_tmr (void); /* Must be called every
54 ms. (Typically 250 ms). */
55 /* Application program's interface: */
56 struct tcp_pcb * tcp_new (void);
57 struct tcp_pcb * tcp_alloc (u8_t prio);
59 void tcp_arg (struct tcp_pcb *pcb, void *arg);
60 void tcp_accept (struct tcp_pcb *pcb,
61 err_t (* accept)(void *arg, struct tcp_pcb *newpcb,
63 void tcp_recv (struct tcp_pcb *pcb,
64 err_t (* recv)(void *arg, struct tcp_pcb *tpcb,
65 struct pbuf *p, err_t err));
66 void tcp_sent (struct tcp_pcb *pcb,
67 err_t (* sent)(void *arg, struct tcp_pcb *tpcb,
69 void tcp_poll (struct tcp_pcb *pcb,
70 err_t (* poll)(void *arg, struct tcp_pcb *tpcb),
72 void tcp_err (struct tcp_pcb *pcb,
73 void (* err)(void *arg, err_t err));
75 #define tcp_mss(pcb) ((pcb)->mss)
76 #define tcp_sndbuf(pcb) ((pcb)->snd_buf)
78 void tcp_recved (struct tcp_pcb *pcb, u16_t len);
79 err_t tcp_bind (struct tcp_pcb *pcb, struct ip_addr *ipaddr,
81 err_t tcp_connect (struct tcp_pcb *pcb, struct ip_addr *ipaddr,
82 u16_t port, err_t (* connected)(void *arg,
85 struct tcp_pcb * tcp_listen (struct tcp_pcb *pcb);
86 void tcp_abort (struct tcp_pcb *pcb);
87 err_t tcp_close (struct tcp_pcb *pcb);
88 err_t tcp_write (struct tcp_pcb *pcb, const void *dataptr, u16_t len,
91 void tcp_setprio (struct tcp_pcb *pcb, u8_t prio);
93 #define TCP_PRIO_MIN 1
94 #define TCP_PRIO_NORMAL 64
95 #define TCP_PRIO_MAX 127
97 /* It is also possible to call these two functions at the right
98 intervals (instead of calling tcp_tmr()). */
99 void tcp_slowtmr (void);
100 void tcp_fasttmr (void);
103 /* Only used by IP to pass a TCP segment to TCP: */
104 void tcp_input (struct pbuf *p, struct netif *inp);
105 /* Used within the TCP code only: */
106 err_t tcp_output (struct tcp_pcb *pcb);
107 void tcp_rexmit (struct tcp_pcb *pcb);
108 void tcp_rexmit_rto (struct tcp_pcb *pcb);
112 #define TCP_SEQ_LT(a,b) ((s32_t)((a)-(b)) < 0)
113 #define TCP_SEQ_LEQ(a,b) ((s32_t)((a)-(b)) <= 0)
114 #define TCP_SEQ_GT(a,b) ((s32_t)((a)-(b)) > 0)
115 #define TCP_SEQ_GEQ(a,b) ((s32_t)((a)-(b)) >= 0)
117 #if 0 /* see bug #10548 */
118 #define TCP_SEQ_BETWEEN(a,b,c) ((c)-(b) >= (a)-(b))
120 #define TCP_SEQ_BETWEEN(a,b,c) (TCP_SEQ_GEQ(a,b) && TCP_SEQ_LEQ(a,c))
121 #define TCP_FIN 0x01U
122 #define TCP_SYN 0x02U
123 #define TCP_RST 0x04U
124 #define TCP_PSH 0x08U
125 #define TCP_ACK 0x10U
126 #define TCP_URG 0x20U
127 #define TCP_ECE 0x40U
128 #define TCP_CWR 0x80U
130 #define TCP_FLAGS 0x3fU
132 /* Length of the TCP header, excluding options. */
135 #ifndef TCP_TMR_INTERVAL
136 #define TCP_TMR_INTERVAL 250 /* The TCP timer interval in
138 #endif /* TCP_TMR_INTERVAL */
140 #ifndef TCP_FAST_INTERVAL
141 #define TCP_FAST_INTERVAL TCP_TMR_INTERVAL /* the fine grained timeout in
143 #endif /* TCP_FAST_INTERVAL */
145 #ifndef TCP_SLOW_INTERVAL
146 #define TCP_SLOW_INTERVAL (2*TCP_TMR_INTERVAL) /* the coarse grained timeout in
148 #endif /* TCP_SLOW_INTERVAL */
150 #define TCP_FIN_WAIT_TIMEOUT 20000 /* milliseconds */
151 #define TCP_SYN_RCVD_TIMEOUT 20000 /* milliseconds */
153 #define TCP_OOSEQ_TIMEOUT 6 /* x RTO */
155 #define TCP_MSL 60000 /* The maximum segment lifetime in microseconds */
158 * User-settable options (used with setsockopt).
160 #define TCP_NODELAY 0x01 /* don't delay send to coalesce packets */
161 #define TCP_KEEPALIVE 0x02 /* send KEEPALIVE probes when idle for pcb->keepalive miliseconds */
163 /* Keepalive values */
164 #define TCP_KEEPDEFAULT 7200000 /* KEEPALIVE timer in miliseconds */
165 #define TCP_KEEPINTVL 75000 /* Time between KEEPALIVE probes in miliseconds */
166 #define TCP_KEEPCNT 9 /* Counter for KEEPALIVE probes */
167 #define TCP_MAXIDLE TCP_KEEPCNT * TCP_KEEPINTVL /* Maximum KEEPALIVE probe time */
170 #ifdef PACK_STRUCT_USE_INCLUDES
171 # include "arch/bpstruct.h"
175 PACK_STRUCT_FIELD(u16_t src);
176 PACK_STRUCT_FIELD(u16_t dest);
177 PACK_STRUCT_FIELD(u32_t seqno);
178 PACK_STRUCT_FIELD(u32_t ackno);
179 PACK_STRUCT_FIELD(u16_t _hdrlen_rsvd_flags);
180 PACK_STRUCT_FIELD(u16_t wnd);
181 PACK_STRUCT_FIELD(u16_t chksum);
182 PACK_STRUCT_FIELD(u16_t urgp);
183 } PACK_STRUCT_STRUCT;
185 #ifdef PACK_STRUCT_USE_INCLUDES
186 # include "arch/epstruct.h"
189 #define TCPH_OFFSET(phdr) (ntohs((phdr)->_hdrlen_rsvd_flags) >> 8)
190 #define TCPH_HDRLEN(phdr) (ntohs((phdr)->_hdrlen_rsvd_flags) >> 12)
191 #define TCPH_FLAGS(phdr) (ntohs((phdr)->_hdrlen_rsvd_flags) & TCP_FLAGS)
193 #define TCPH_OFFSET_SET(phdr, offset) (phdr)->_hdrlen_rsvd_flags = htons(((offset) << 8) | TCPH_FLAGS(phdr))
194 #define TCPH_HDRLEN_SET(phdr, len) (phdr)->_hdrlen_rsvd_flags = htons(((len) << 12) | TCPH_FLAGS(phdr))
195 #define TCPH_FLAGS_SET(phdr, flags) (phdr)->_hdrlen_rsvd_flags = htons((ntohs((phdr)->_hdrlen_rsvd_flags) & ~TCP_FLAGS) | (flags))
196 #define TCPH_SET_FLAG(phdr, flags ) (phdr)->_hdrlen_rsvd_flags = htons(ntohs((phdr)->_hdrlen_rsvd_flags) | (flags))
197 #define TCPH_UNSET_FLAG(phdr, flags) (phdr)->_hdrlen_rsvd_flags = htons(ntohs((phdr)->_hdrlen_rsvd_flags) | (TCPH_FLAGS(phdr) & ~(flags)) )
199 #define TCP_TCPLEN(seg) ((seg)->len + ((TCPH_FLAGS((seg)->tcphdr) & TCP_FIN || \
200 TCPH_FLAGS((seg)->tcphdr) & TCP_SYN)? 1: 0))
216 /* the TCP protocol control block */
218 /** common PCB members */
220 /** protocol specific PCB members */
221 struct tcp_pcb *next; /* for the linked list */
222 enum tcp_state state; /* TCP state */
230 #define TF_ACK_DELAY (u8_t)0x01U /* Delayed ACK. */
231 #define TF_ACK_NOW (u8_t)0x02U /* Immediate ACK. */
232 #define TF_INFR (u8_t)0x04U /* In fast recovery. */
233 #define TF_RESET (u8_t)0x08U /* Connection was reset. */
234 #define TF_CLOSED (u8_t)0x10U /* Connection was sucessfully closed. */
235 #define TF_GOT_FIN (u8_t)0x20U /* Connection was closed by the remote end. */
236 #define TF_NODELAY (u8_t)0x40U /* Disable Nagle algorithm */
238 /* receiver variables */
239 u32_t rcv_nxt; /* next seqno expected */
240 u16_t rcv_wnd; /* receiver window */
244 u8_t polltmr, pollinterval;
246 /* Retransmission timer. */
249 u16_t mss; /* maximum segment size */
251 /* RTT (round trip time) estimation variables */
252 u32_t rttest; /* RTT estimate in 500ms ticks */
253 u32_t rtseq; /* sequence number being timed */
254 s16_t sa, sv; /* @todo document this */
256 u16_t rto; /* retransmission time-out */
257 u8_t nrtx; /* number of retransmissions */
259 /* fast retransmit/recovery */
260 u32_t lastack; /* Highest acknowledged seqno. */
263 /* congestion avoidance/control variables */
267 /* sender variables */
268 u32_t snd_nxt, /* next seqno to be sent */
269 snd_max, /* Highest seqno sent. */
270 snd_wnd, /* sender window */
271 snd_wl1, snd_wl2, /* Sequence and acknowledgement numbers of last
273 snd_lbb; /* Sequence number of next byte to be buffered. */
277 u16_t snd_buf; /* Available buffer space for sending (in bytes). */
278 u8_t snd_queuelen; /* Available buffer space for sending (in tcp_segs). */
281 /* These are ordered by sequence number: */
282 struct tcp_seg *unsent; /* Unsent (queued) segments. */
283 struct tcp_seg *unacked; /* Sent but unacknowledged segments. */
285 struct tcp_seg *ooseq; /* Received out of sequence segments. */
286 #endif /* TCP_QUEUE_OOSEQ */
288 #if LWIP_CALLBACK_API
289 /* Function to be called when more send buffer space is available. */
290 err_t (* sent)(void *arg, struct tcp_pcb *pcb, u16_t space);
292 /* Function to be called when (in-sequence) data has arrived. */
293 err_t (* recv)(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err);
295 /* Function to be called when a connection has been set up. */
296 err_t (* connected)(void *arg, struct tcp_pcb *pcb, err_t err);
298 /* Function to call when a listener has been connected. */
299 err_t (* accept)(void *arg, struct tcp_pcb *newpcb, err_t err);
301 /* Function which is called periodically. */
302 err_t (* poll)(void *arg, struct tcp_pcb *pcb);
304 /* Function to be called whenever a fatal error occurs. */
305 void (* errf)(void *arg, err_t err);
306 #endif /* LWIP_CALLBACK_API */
308 /* idle time before KEEPALIVE is sent */
311 /* KEEPALIVE counter */
315 struct tcp_pcb_listen {
316 /* Common members of all PCB types */
319 /* Protocol specific PCB members */
320 struct tcp_pcb_listen *next; /* for the linked list */
322 /* Even if state is obviously LISTEN this is here for
323 * field compatibility with tpc_pcb to which it is cast sometimes
324 * Until a cleaner solution emerges this is here.FIXME
326 enum tcp_state state; /* TCP state */
333 #if LWIP_CALLBACK_API
334 /* Function to call when a listener has been connected. */
335 err_t (* accept)(void *arg, struct tcp_pcb *newpcb, err_t err);
336 #endif /* LWIP_CALLBACK_API */
345 LWIP_EVENT_CONNECTED,
350 err_t lwip_tcp_event(void *arg, struct tcp_pcb *pcb,
356 #define TCP_EVENT_ACCEPT(pcb,err,ret) ret = lwip_tcp_event((pcb)->callback_arg, (pcb),\
357 LWIP_EVENT_ACCEPT, NULL, 0, err)
358 #define TCP_EVENT_SENT(pcb,space,ret) ret = lwip_tcp_event((pcb)->callback_arg, (pcb),\
359 LWIP_EVENT_SENT, NULL, space, ERR_OK)
360 #define TCP_EVENT_RECV(pcb,p,err,ret) ret = lwip_tcp_event((pcb)->callback_arg, (pcb),\
361 LWIP_EVENT_RECV, (p), 0, (err))
362 #define TCP_EVENT_CONNECTED(pcb,err,ret) ret = lwip_tcp_event((pcb)->callback_arg, (pcb),\
363 LWIP_EVENT_CONNECTED, NULL, 0, (err))
364 #define TCP_EVENT_POLL(pcb,ret) ret = lwip_tcp_event((pcb)->callback_arg, (pcb),\
365 LWIP_EVENT_POLL, NULL, 0, ERR_OK)
366 #define TCP_EVENT_ERR(errf,arg,err) lwip_tcp_event((arg), NULL, \
367 LWIP_EVENT_ERR, NULL, 0, (err))
368 #else /* LWIP_EVENT_API */
369 #define TCP_EVENT_ACCEPT(pcb,err,ret) \
370 if((pcb)->accept != NULL) \
371 (ret = (pcb)->accept((pcb)->callback_arg,(pcb),(err)))
372 #define TCP_EVENT_SENT(pcb,space,ret) \
373 if((pcb)->sent != NULL) \
374 (ret = (pcb)->sent((pcb)->callback_arg,(pcb),(space)))
375 #define TCP_EVENT_RECV(pcb,p,err,ret) \
376 if((pcb)->recv != NULL) \
377 { ret = (pcb)->recv((pcb)->callback_arg,(pcb),(p),(err)); } else { \
378 if (p) pbuf_free(p); }
379 #define TCP_EVENT_CONNECTED(pcb,err,ret) \
380 if((pcb)->connected != NULL) \
381 (ret = (pcb)->connected((pcb)->callback_arg,(pcb),(err)))
382 #define TCP_EVENT_POLL(pcb,ret) \
383 if((pcb)->poll != NULL) \
384 (ret = (pcb)->poll((pcb)->callback_arg,(pcb)))
385 #define TCP_EVENT_ERR(errf,arg,err) \
388 #endif /* LWIP_EVENT_API */
390 /* This structure represents a TCP segment on the unsent and unacked queues */
392 struct tcp_seg *next; /* used when putting segements on a queue */
393 struct pbuf *p; /* buffer containing data + TCP header */
394 void *dataptr; /* pointer to the TCP data in the pbuf */
395 u16_t len; /* the TCP length of this segment */
396 struct tcp_hdr *tcphdr; /* the TCP header */
399 /* Internal functions and global variables: */
400 struct tcp_pcb *tcp_pcb_copy(struct tcp_pcb *pcb);
401 void tcp_pcb_purge(struct tcp_pcb *pcb);
402 void tcp_pcb_remove(struct tcp_pcb **pcblist, struct tcp_pcb *pcb);
404 u8_t tcp_segs_free(struct tcp_seg *seg);
405 u8_t tcp_seg_free(struct tcp_seg *seg);
406 struct tcp_seg *tcp_seg_copy(struct tcp_seg *seg);
408 #define tcp_ack(pcb) if((pcb)->flags & TF_ACK_DELAY) { \
409 (pcb)->flags &= ~TF_ACK_DELAY; \
410 (pcb)->flags |= TF_ACK_NOW; \
413 (pcb)->flags |= TF_ACK_DELAY; \
416 #define tcp_ack_now(pcb) (pcb)->flags |= TF_ACK_NOW; \
419 err_t tcp_send_ctrl(struct tcp_pcb *pcb, u8_t flags);
420 err_t tcp_enqueue(struct tcp_pcb *pcb, void *dataptr, u16_t len,
421 u8_t flags, u8_t copy,
422 u8_t *optdata, u8_t optlen);
424 void tcp_rexmit_seg(struct tcp_pcb *pcb, struct tcp_seg *seg);
426 void tcp_rst(u32_t seqno, u32_t ackno,
427 struct ip_addr *local_ip, struct ip_addr *remote_ip,
428 u16_t local_port, u16_t remote_port);
430 u32_t tcp_next_iss(void);
432 void tcp_keepalive(struct tcp_pcb *pcb);
434 extern struct tcp_pcb *tcp_input_pcb;
435 extern u32_t tcp_ticks;
437 #if TCP_DEBUG || TCP_INPUT_DEBUG || TCP_OUTPUT_DEBUG
438 void tcp_debug_print(struct tcp_hdr *tcphdr);
439 void tcp_debug_print_flags(u8_t flags);
440 void tcp_debug_print_state(enum tcp_state s);
441 void tcp_debug_print_pcbs(void);
442 s16_t tcp_pcbs_sane(void);
444 # define tcp_debug_print(tcphdr)
445 # define tcp_debug_print_flags(flags)
446 # define tcp_debug_print_state(s)
447 # define tcp_debug_print_pcbs()
448 # define tcp_pcbs_sane() 1
449 #endif /* TCP_DEBUG */
452 #define tcp_timer_needed()
454 void tcp_timer_needed(void);
457 /* The TCP PCB lists. */
458 union tcp_listen_pcbs_t { /* List of all TCP PCBs in LISTEN state. */
459 struct tcp_pcb_listen *listen_pcbs;
460 struct tcp_pcb *pcbs;
462 extern union tcp_listen_pcbs_t tcp_listen_pcbs;
463 extern struct tcp_pcb *tcp_active_pcbs; /* List of all TCP PCBs that are in a
464 state in which they accept or send
466 extern struct tcp_pcb *tcp_tw_pcbs; /* List of all TCP PCBs in TIME-WAIT. */
468 extern struct tcp_pcb *tcp_tmp_pcb; /* Only used for temporary storage. */
470 /* Axioms about the above lists:
471 1) Every TCP PCB that is not CLOSED is in one of the lists.
472 2) A PCB is only in one of the lists.
473 3) All PCBs in the tcp_listen_pcbs list is in LISTEN state.
474 4) All PCBs in the tcp_tw_pcbs list is in TIME-WAIT state.
477 /* Define two macros, TCP_REG and TCP_RMV that registers a TCP PCB
478 with a PCB list or removes a PCB from a list, respectively. */
480 #define TCP_REG(pcbs, npcb) do {\
481 LWIP_DEBUGF(TCP_DEBUG, ("TCP_REG %p local port %d\n", npcb, npcb->local_port)); \
482 for(tcp_tmp_pcb = *pcbs; \
483 tcp_tmp_pcb != NULL; \
484 tcp_tmp_pcb = tcp_tmp_pcb->next) { \
485 LWIP_ASSERT("TCP_REG: already registered\n", tcp_tmp_pcb != npcb); \
487 LWIP_ASSERT("TCP_REG: pcb->state != CLOSED", npcb->state != CLOSED); \
488 npcb->next = *pcbs; \
489 LWIP_ASSERT("TCP_REG: npcb->next != npcb", npcb->next != npcb); \
491 LWIP_ASSERT("TCP_RMV: tcp_pcbs sane", tcp_pcbs_sane()); \
492 tcp_timer_needed(); \
494 #define TCP_RMV(pcbs, npcb) do { \
495 LWIP_ASSERT("TCP_RMV: pcbs != NULL", *pcbs != NULL); \
496 LWIP_DEBUGF(TCP_DEBUG, ("TCP_RMV: removing %p from %p\n", npcb, *pcbs)); \
497 if(*pcbs == npcb) { \
498 *pcbs = (*pcbs)->next; \
499 } else for(tcp_tmp_pcb = *pcbs; tcp_tmp_pcb != NULL; tcp_tmp_pcb = tcp_tmp_pcb->next) { \
500 if(tcp_tmp_pcb->next != NULL && tcp_tmp_pcb->next == npcb) { \
501 tcp_tmp_pcb->next = npcb->next; \
506 LWIP_ASSERT("TCP_RMV: tcp_pcbs sane", tcp_pcbs_sane()); \
507 LWIP_DEBUGF(TCP_DEBUG, ("TCP_RMV: removed %p from %p\n", npcb, *pcbs)); \
510 #else /* LWIP_DEBUG */
511 #define TCP_REG(pcbs, npcb) do { \
512 npcb->next = *pcbs; \
514 tcp_timer_needed(); \
516 #define TCP_RMV(pcbs, npcb) do { \
517 if(*(pcbs) == npcb) { \
518 (*(pcbs)) = (*pcbs)->next; \
519 } else for(tcp_tmp_pcb = *pcbs; tcp_tmp_pcb != NULL; tcp_tmp_pcb = tcp_tmp_pcb->next) { \
520 if(tcp_tmp_pcb->next != NULL && tcp_tmp_pcb->next == npcb) { \
521 tcp_tmp_pcb->next = npcb->next; \
527 #endif /* LWIP_DEBUG */
528 #endif /* __LWIP_TCP_H__ */