Microchip® Advanced Software Framework

tcp_in.c File Reference

Transmission Control Protocol, incoming traffic.

The input processing functions of the TCP layer.

These functions are generally called in the order (ip_input() ->) tcp_input() -> * tcp_process() -> tcp_receive() (-> application).

#include "lwip/opt.h"
#include "lwip/tcp_impl.h"
#include "lwip/def.h"
#include "lwip/ip_addr.h"
#include "lwip/netif.h"
#include "lwip/mem.h"
#include "lwip/memp.h"
#include "lwip/inet_chksum.h"
#include "lwip/stats.h"
#include "lwip/snmp.h"
#include "arch/perf.h"
#include "lwip/ip6.h"
#include "lwip/ip6_addr.h"
#include "lwip/nd6.h"

Macros

#define LWIP_TCP_CALC_INITIAL_CWND(mss)   LWIP_MIN((4U * (mss)), LWIP_MAX((2U * (mss)), 4380U));
 

Functions

static u8_t tcp_getoptbyte (void)
 
void tcp_input (struct pbuf *p, struct netif *inp)
 The initial input processing of TCP. More...
 
static err_t tcp_listen_input (struct tcp_pcb_listen *pcb)
 Called by tcp_input() when a segment arrives for a listening connection (from tcp_input()). More...
 
static void tcp_oos_insert_segment (struct tcp_seg *cseg, struct tcp_seg *next)
 Insert segment into the list (segments covered with new one will be deleted) More...
 
static void tcp_parseopt (struct tcp_pcb *pcb)
 Parses the options contained in the incoming segment. More...
 
static err_t tcp_process (struct tcp_pcb *pcb)
 Implements the TCP state machine. More...
 
static void tcp_receive (struct tcp_pcb *pcb)
 Called by tcp_process. More...
 
static err_t tcp_timewait_input (struct tcp_pcb *pcb)
 Called by tcp_input() when a segment arrives for a connection in TIME_WAIT. More...
 

Variables

static u32_t ackno
 
static u8_t flags
 
static struct tcp_seg inseg
 
static struct pbufrecv_data
 
static u8_t recv_flags
 
static u32_t seqno
 
struct tcp_pcbtcp_input_pcb
 
static u16_t tcp_optidx
 
static struct tcp_hdrtcphdr
 
static u16_t tcphdr_opt1len
 
static u8_ttcphdr_opt2
 
static u16_t tcplen
 

#define LWIP_TCP_CALC_INITIAL_CWND (   mss)    LWIP_MIN((4U * (mss)), LWIP_MAX((2U * (mss)), 4380U));

Referenced by tcp_process().

static u8_t tcp_getoptbyte ( void  )
static

References NULL, TCP_HLEN, tcp_optidx, tcphdr_opt1len, and tcphdr_opt2.

Referenced by tcp_parseopt().

void tcp_input ( struct pbuf p,
struct netif inp 
)

The initial input processing of TCP.

It verifies the TCP header, demultiplexes the segment between the PCBs and passes it on to tcp_process(), which implements the TCP finite state machine. This function is called by the IP layer (in ip_input()).

Parameters
preceived TCP segment to process (p->payload pointing to the TCP header)
inpnetwork interface on which this segment was received

References tcp_pcb_listen::accept_any_ip_version, tcp_pcb::acked, ackno, CLOSED, ERR_ABRT, ERR_CLSD, ERR_OK, ERR_RST, tcp_pcb::errf, flags, pbuf::flags, tcp_pcb::flags, inseg, ip_addr_isbroadcast, ip_current_dest_addr, ip_current_is_v6, IP_PCB_IPVER_INPUT_MATCH, IP_PROTO_TCP, ipX_addr_cmp, ipX_addr_isany, ipX_addr_ismulticast, ipX_chksum_pseudo, ipX_current_dest_addr, ipX_current_src_addr, pbuf::len, tcp_seg::len, LISTEN, tcp_listen_pcbs_t::listen_pcbs, LWIP_ASSERT, LWIP_DEBUGF, LWIP_MIN, memp_free(), pbuf::next, tcp_seg::next, ntohl, ntohs, NULL, tcp_seg::p, pbuf::payload, pbuf_cat(), PBUF_FLAG_PUSH, PBUF_FLAG_TCP_FIN, pbuf_free(), pbuf_header(), PERF_START, PERF_STOP, tcp_pcb::rcv_wnd, recv_data, recv_flags, tcp_pcb::refused_data, tcp_pcb::remote_port, seqno, snmp_inc_tcpinerrs, snmp_inc_tcpinsegs, tcp_abort(), tcp_active_pcbs, tcp_debug_print, tcp_debug_print_flags, tcp_debug_print_state, TCP_EVENT_CLOSED, TCP_EVENT_ERR, TCP_EVENT_RECV, TCP_EVENT_SENT, TCP_FIN, TCP_HLEN, TCP_INPUT_DEBUG, tcp_listen_input(), tcp_listen_pcbs, tcp_output(), tcp_pcb_remove(), tcp_pcbs_sane, tcp_process(), tcp_process_refused_data(), TCP_PSH, TCP_RST, tcp_rst, TCP_RST_DEBUG, TCP_STATS_INC, TCP_SYN, tcp_timewait_input(), tcp_tw_pcbs, TCP_WND, TCPH_FLAGS, TCPH_HDRLEN, tcphdr, tcp_seg::tcphdr, tcphdr_opt1len, tcphdr_opt2, tcplen, TF_CLOSED, TF_GOT_FIN, TF_RESET, TF_RXCLOSED, TIME_WAIT, pbuf::tot_len, U16_F, and X16_F.

Referenced by ip6_input(), and ip_input().

static err_t tcp_listen_input ( struct tcp_pcb_listen pcb)
static

Called by tcp_input() when a segment arrives for a listening connection (from tcp_input()).

Parameters
pcbthe tcp_pcb_listen for which a segment arrived
Returns
ERR_OK if the segment was processed another err_t on error
Note
the return value is not (yet?) used in tcp_input()
the segment which arrived is saved in global variables, therefore only the pcb involved is passed as a parameter to this function

References ackno, ERR_ABRT, ERR_MEM, ERR_OK, flags, ip_current_is_v6, ipX_addr_copy, ipX_current_dest_addr, ipX_current_src_addr, LWIP_DEBUGF, tcp_pcb::mss, NULL, PCB_ISIPV6, tcp_pcb::rcv_ann_right_edge, tcp_pcb::rcv_nxt, tcp_pcb::remote_port, seqno, tcp_pcb::snd_wl1, tcp_pcb::snd_wnd, tcp_pcb::snd_wnd_max, SND_WND_SCALE, snmp_inc_tcppassiveopens, SOF_INHERITED, tcp_pcb::ssthresh, SYN_RCVD, tcp_abandon(), TCP_ACK, tcp_alloc(), TCP_DEBUG, tcp_eff_send_mss, tcp_enqueue_flags(), tcp_output(), tcp_parseopt(), TCP_REG_ACTIVE, TCP_RST, tcp_rst, TCP_RST_DEBUG, TCP_STATS_INC, TCP_SYN, tcplen, and U16_F.

Referenced by tcp_input().

static void tcp_oos_insert_segment ( struct tcp_seg cseg,
struct tcp_seg next 
)
static

Insert segment into the list (segments covered with new one will be deleted)

Called from tcp_receive()

References tcp_seg::len, tcp_seg::next, next, NULL, tcp_seg::p, pbuf_realloc(), seqno, TCP_FIN, tcp_seg_free(), tcp_segs_free(), TCP_SEQ_GEQ, TCP_SEQ_GT, TCPH_FLAGS, TCPH_SET_FLAG, and tcp_seg::tcphdr.

Referenced by tcp_receive().

static void tcp_parseopt ( struct tcp_pcb pcb)
static

Parses the options contained in the incoming segment.

Called from tcp_listen_input() and tcp_process(). Currently, only the MSS option is supported!

Parameters
pcbthe tcp_pcb for which a segment arrived

References flags, tcp_pcb::flags, LWIP_DEBUGF, LWIP_TCP_OPT_EOL, LWIP_TCP_OPT_LEN_MSS, LWIP_TCP_OPT_MSS, LWIP_TCP_OPT_NOP, LWIP_TCP_OPT_TS, LWIP_TCP_OPT_WS, tcp_pcb::mss, ntohl, seqno, tcp_getoptbyte(), TCP_INPUT_DEBUG, TCP_MSS, tcp_optidx, TCP_SEQ_BETWEEN, TCP_SYN, TCPH_HDRLEN, tcplen, and TF_TIMESTAMP.

Referenced by tcp_listen_input(), and tcp_process().

static err_t tcp_process ( struct tcp_pcb pcb)
static

Implements the TCP state machine.

Called by tcp_input. In some states tcp_receive() is called to receive data. The tcp_seg argument will be freed by the caller (tcp_input()) unless the recv_data pointer in the pcb is set.

Parameters
pcbthe tcp_pcb for which a segment arrived
Note
the segment which arrived is saved in global variables, therefore only the pcb involved is passed as a parameter to this function

References tcp_pcb::acked, ackno, CLOSE_WAIT, CLOSED, CLOSING, tcp_pcb::cwnd, ERR_ABRT, ERR_OK, ERR_RST, ESTABLISHED, FIN_WAIT_1, FIN_WAIT_2, flags, tcp_pcb::flags, inseg, ip_current_is_v6, ipX_current_dest_addr, ipX_current_src_addr, tcp_pcb::keep_cnt_sent, LAST_ACK, tcp_pcb::lastack, LWIP_ASSERT, LWIP_DEBUGF, LWIP_TCP_CALC_INITIAL_CWND, tcp_pcb::mss, tcp_seg::next, tcp_pcb::nrtx, ntohl, NULL, PCB_ISIPV6, tcp_pcb::rcv_ann_right_edge, tcp_pcb::rcv_nxt, tcp_pcb::rcv_wnd, recv_flags, tcp_pcb::rtime, seqno, tcp_pcb::snd_buf, tcp_pcb::snd_nxt, tcp_pcb::snd_queuelen, tcp_pcb::snd_wl1, tcp_pcb::snd_wnd, tcp_pcb::snd_wnd_max, SND_WND_SCALE, tcp_pcb::ssthresh, SYN_RCVD, SYN_SENT, tcp_abort(), TCP_ACK, tcp_ack_now, TCP_CWND_DEBUG, TCP_DEBUG, tcp_eff_send_mss, TCP_EVENT_ACCEPT, TCP_EVENT_CONNECTED, TCP_INPUT_DEBUG, tcp_parseopt(), tcp_pcb_purge(), TCP_QLEN_DEBUG, tcp_receive(), TCP_REG, tcp_rexmit(), TCP_RMV_ACTIVE, TCP_RST, tcp_rst, tcp_seg_free(), TCP_SEQ_BETWEEN, TCP_SYN, tcp_ticks, tcp_tw_pcbs, tcp_seg::tcphdr, tcplen, TCPWNDSIZE_F, TF_ACK_DELAY, TF_CLOSED, TF_GOT_FIN, TF_RESET, TF_RXCLOSED, TIME_WAIT, tcp_pcb::tmr, U16_F, U32_F, and tcp_pcb::unacked.

Referenced by tcp_input().

static void tcp_receive ( struct tcp_pcb pcb)
static

Called by tcp_process.

Checks if the given segment is an ACK for outstanding data, and if so frees the memory of the buffered data. Next, is places the segment on any of the receive queues (pcb->recved or pcb->ooseq). If the segment is buffered, the pbuf is referenced by pbuf_ref so that it will not be freed until it has been removed from the buffer.

If the incoming segment constitutes an ACK for a segment that was used for RTT estimation, the RTT is estimated here as well.

Called from tcp_process().

References tcp_pcb::acked, ackno, CLOSE_WAIT, tcp_pcb::cwnd, tcp_pcb::dupacks, ESTABLISHED, flags, tcp_pcb::flags, inseg, ip6_current_src_addr, tcp_pcb::lastack, pbuf::len, tcp_seg::len, LWIP_ASSERT, LWIP_DEBUGF, tcp_pcb::mss, nd6_reachability_hint(), pbuf::next, tcp_seg::next, next, tcp_pcb::nrtx, ntohl, NULL, tcp_pcb::ooseq, tcp_seg::p, pbuf_cat(), pbuf_clen(), pbuf_header(), pbuf_realloc(), PCB_ISIPV6, tcp_pcb::persist_backoff, tcp_pcb::persist_cnt, tcp_pcb::polltmr, tcp_pcb::rcv_nxt, tcp_pcb::rcv_wnd, recv_flags, tcp_pcb::rtime, tcp_pcb::rto, tcp_pcb::rtseq, tcp_pcb::rttest, tcp_pcb::sa, seqno, tcp_pcb::snd_buf, tcp_pcb::snd_nxt, tcp_pcb::snd_queuelen, tcp_pcb::snd_wl1, tcp_pcb::snd_wl2, tcp_pcb::snd_wnd, tcp_pcb::snd_wnd_max, SND_WND_SCALE, tcp_pcb::ssthresh, tcp_pcb::sv, TCP_ACK, tcp_ack, tcp_ack_now, TCP_CWND_DEBUG, TCP_FIN, TCP_INPUT_DEBUG, tcp_oos_insert_segment(), TCP_QLEN_DEBUG, tcp_rexmit_fast(), TCP_RTO_DEBUG, tcp_seg_copy(), tcp_seg_free(), tcp_segs_free(), tcp_send_empty_ack(), TCP_SEQ_BETWEEN, TCP_SEQ_GEQ, TCP_SEQ_GT, TCP_SEQ_LEQ, TCP_SEQ_LT, TCP_SLOW_INTERVAL, TCP_SYN, TCP_TCPLEN, tcp_ticks, tcp_update_rcv_ann_wnd(), TCP_WND_DEBUG, TCPH_FLAGS, TCPH_FLAGS_SET, TCPH_SET_FLAG, tcp_seg::tcphdr, tcplen, TCPWNDSIZE_F, TF_GOT_FIN, TF_INFR, pbuf::tot_len, U16_F, U32_F, tcp_pcb::unacked, tcp_pcb::unsent, and tcp_pcb::unsent_oversize.

Referenced by tcp_process().

static err_t tcp_timewait_input ( struct tcp_pcb pcb)
static

Called by tcp_input() when a segment arrives for a connection in TIME_WAIT.

Parameters
pcbthe tcp_pcb for which a segment arrived
Note
the segment which arrived is saved in global variables, therefore only the pcb involved is passed as a parameter to this function

References ackno, ERR_OK, flags, tcp_pcb::flags, ip_current_is_v6, ipX_current_dest_addr, ipX_current_src_addr, tcp_pcb::rcv_nxt, tcp_pcb::rcv_wnd, seqno, TCP_FIN, tcp_output(), TCP_RST, tcp_rst, TCP_SEQ_BETWEEN, TCP_SYN, tcp_ticks, tcplen, TF_ACK_NOW, and tcp_pcb::tmr.

Referenced by tcp_input().

struct tcp_seg inseg
static

Referenced by tcp_input(), tcp_process(), and tcp_receive().

struct pbuf* recv_data
static

Referenced by tcp_input().

u8_t recv_flags
static

Referenced by tcp_input(), tcp_process(), and tcp_receive().

struct tcp_pcb* tcp_input_pcb

Referenced by tcp_output().

u16_t tcp_optidx
static

Referenced by tcp_getoptbyte(), and tcp_parseopt().

u16_t tcphdr_opt1len
static

Referenced by tcp_getoptbyte(), and tcp_input().

u8_t* tcphdr_opt2
static

Referenced by tcp_getoptbyte(), and tcp_input().