2006-04-30 03:16:37 +02:00
|
|
|
#include <string.h>
|
2006-08-01 22:46:50 +02:00
|
|
|
#include <stdlib.h>
|
2006-04-30 03:16:37 +02:00
|
|
|
#include <assert.h>
|
|
|
|
#include <byteswap.h>
|
2006-04-30 04:08:42 +02:00
|
|
|
#include <latch.h>
|
2006-04-30 18:59:45 +02:00
|
|
|
#include <errno.h>
|
2006-04-30 04:08:42 +02:00
|
|
|
#include <gpxe/process.h>
|
|
|
|
#include <gpxe/init.h>
|
|
|
|
#include <gpxe/netdevice.h>
|
|
|
|
#include <gpxe/pkbuff.h>
|
|
|
|
#include <gpxe/ip.h>
|
2006-04-30 03:16:37 +02:00
|
|
|
#include <gpxe/tcp.h>
|
2006-08-01 22:46:50 +02:00
|
|
|
#include <gpxe/tcpip.h>
|
2006-08-07 20:52:26 +02:00
|
|
|
#include <gpxe/retry.h>
|
2006-04-30 03:16:37 +02:00
|
|
|
#include "uip/uip.h"
|
|
|
|
|
|
|
|
/** @file
|
|
|
|
*
|
|
|
|
* TCP protocol
|
|
|
|
*
|
|
|
|
* The gPXE TCP stack is currently implemented on top of the uIP
|
|
|
|
* protocol stack. This file provides wrappers around uIP so that
|
|
|
|
* higher-level protocol implementations do not need to talk directly
|
|
|
|
* to uIP (which has a somewhat baroque API).
|
|
|
|
*
|
|
|
|
* Basic operation is to create a #tcp_connection structure, call
|
|
|
|
* tcp_connect() and then call run_tcpip() in a loop until the
|
|
|
|
* operation has completed. The TCP stack will call the various
|
|
|
|
* methods defined in the #tcp_operations structure in order to send
|
|
|
|
* and receive data.
|
|
|
|
*
|
|
|
|
* See hello.c for a trivial example of a TCP protocol using this
|
|
|
|
* API.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2006-08-01 22:46:50 +02:00
|
|
|
#if USE_UIP
|
|
|
|
|
2006-04-30 03:16:37 +02:00
|
|
|
/**
|
|
|
|
* TCP transmit buffer
|
|
|
|
*
|
|
|
|
* When a tcp_operations::senddata() method is called, it is
|
|
|
|
* guaranteed to be able to use this buffer as temporary space for
|
|
|
|
* constructing the data to be sent. For example, code such as
|
|
|
|
*
|
|
|
|
* @code
|
|
|
|
*
|
2006-06-18 01:20:54 +02:00
|
|
|
* static void my_senddata ( struct tcp_connection *conn, void *buf,
|
|
|
|
* size_t len ) {
|
|
|
|
* len = snprintf ( buf, len, "FETCH %s\r\n", filename );
|
|
|
|
* tcp_send ( conn, buf + already_sent, len - already_sent );
|
2006-04-30 03:16:37 +02:00
|
|
|
* }
|
|
|
|
*
|
|
|
|
* @endcode
|
|
|
|
*
|
|
|
|
* is allowed, and is probably the best way to deal with
|
|
|
|
* variably-sized data.
|
|
|
|
*
|
|
|
|
* Note that you cannot use this simple mechanism if you want to be
|
2006-06-18 01:20:54 +02:00
|
|
|
* able to construct single data blocks of more than #len bytes.
|
2006-04-30 03:16:37 +02:00
|
|
|
*/
|
2006-06-18 01:20:54 +02:00
|
|
|
static void *tcp_buffer = uip_buf + ( 40 + UIP_LLH_LEN );
|
2006-04-30 03:16:37 +02:00
|
|
|
|
|
|
|
/** Size of #tcp_buffer */
|
2006-06-18 01:20:54 +02:00
|
|
|
static size_t tcp_buflen = UIP_BUFSIZE - ( 40 + UIP_LLH_LEN );
|
2006-04-30 03:16:37 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Open a TCP connection
|
|
|
|
*
|
|
|
|
* @v conn TCP connection
|
|
|
|
*
|
|
|
|
* This sets up a new TCP connection to the remote host specified in
|
2006-04-30 04:13:52 +02:00
|
|
|
* tcp_connection::sin.
|
2006-04-30 03:16:37 +02:00
|
|
|
*/
|
2006-04-30 04:13:52 +02:00
|
|
|
void tcp_connect ( struct tcp_connection *conn ) {
|
2006-04-30 03:16:37 +02:00
|
|
|
struct uip_conn *uip_conn;
|
|
|
|
u16_t ipaddr[2];
|
|
|
|
|
|
|
|
assert ( conn->sin.sin_addr.s_addr != 0 );
|
|
|
|
assert ( conn->sin.sin_port != 0 );
|
|
|
|
assert ( conn->tcp_op != NULL );
|
|
|
|
assert ( sizeof ( uip_conn->appstate ) == sizeof ( conn ) );
|
|
|
|
|
|
|
|
* ( ( uint32_t * ) ipaddr ) = conn->sin.sin_addr.s_addr;
|
|
|
|
uip_conn = uip_connect ( ipaddr, conn->sin.sin_port );
|
2006-04-30 04:13:52 +02:00
|
|
|
#warning "Use linked lists so that uip_connect() cannot fail"
|
|
|
|
assert ( uip_conn != NULL );
|
2006-04-30 03:16:37 +02:00
|
|
|
*( ( void ** ) uip_conn->appstate ) = conn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send data via a TCP connection
|
|
|
|
*
|
|
|
|
* @v conn TCP connection
|
|
|
|
* @v data Data to send
|
|
|
|
* @v len Length of data
|
|
|
|
*
|
|
|
|
* Data will be automatically limited to the current TCP window size.
|
|
|
|
*
|
|
|
|
* If retransmission is required, the connection's
|
|
|
|
* tcp_operations::senddata() method will be called again in order to
|
|
|
|
* regenerate the data.
|
|
|
|
*/
|
|
|
|
void tcp_send ( struct tcp_connection *conn __unused,
|
|
|
|
const void *data, size_t len ) {
|
|
|
|
|
|
|
|
assert ( conn = *( ( void ** ) uip_conn->appstate ) );
|
|
|
|
|
|
|
|
if ( len > tcp_buflen )
|
|
|
|
len = tcp_buflen;
|
|
|
|
memmove ( tcp_buffer, data, len );
|
|
|
|
|
|
|
|
uip_send ( tcp_buffer, len );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Close a TCP connection
|
|
|
|
*
|
|
|
|
* @v conn TCP connection
|
|
|
|
*/
|
|
|
|
void tcp_close ( struct tcp_connection *conn __unused ) {
|
|
|
|
assert ( conn = *( ( void ** ) uip_conn->appstate ) );
|
|
|
|
uip_close();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* uIP TCP application call interface
|
|
|
|
*
|
|
|
|
* This is the entry point of gPXE from the point of view of the uIP
|
|
|
|
* protocol stack. This function calls the appropriate methods from
|
|
|
|
* the connection's @tcp_operations table in order to process received
|
|
|
|
* data, transmit new data etc.
|
|
|
|
*/
|
|
|
|
void uip_tcp_appcall ( void ) {
|
|
|
|
struct tcp_connection *conn = *( ( void ** ) uip_conn->appstate );
|
|
|
|
struct tcp_operations *op = conn->tcp_op;
|
|
|
|
|
2006-04-30 18:59:45 +02:00
|
|
|
if ( op->closed ) {
|
|
|
|
if ( uip_aborted() )
|
|
|
|
op->closed ( conn, -ECONNABORTED );
|
|
|
|
if ( uip_timedout() )
|
|
|
|
op->closed ( conn, -ETIMEDOUT );
|
|
|
|
if ( uip_closed() )
|
|
|
|
op->closed ( conn, 0 );
|
|
|
|
}
|
2006-04-30 15:20:40 +02:00
|
|
|
if ( uip_connected() && op->connected )
|
2006-04-30 03:16:37 +02:00
|
|
|
op->connected ( conn );
|
2006-04-30 15:20:40 +02:00
|
|
|
if ( uip_acked() && op->acked )
|
2006-04-30 03:16:37 +02:00
|
|
|
op->acked ( conn, uip_conn->len );
|
2006-04-30 15:20:40 +02:00
|
|
|
if ( uip_newdata() && op->newdata )
|
2006-04-30 03:16:37 +02:00
|
|
|
op->newdata ( conn, ( void * ) uip_appdata, uip_len );
|
2006-04-30 15:20:40 +02:00
|
|
|
if ( ( uip_rexmit() || uip_newdata() || uip_acked() ||
|
|
|
|
uip_connected() || uip_poll() ) && op->senddata )
|
2006-06-18 01:20:54 +02:00
|
|
|
op->senddata ( conn, tcp_buffer, tcp_buflen );
|
2006-04-30 03:16:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Present here to allow everything to link. Will go into separate
|
|
|
|
* udp.c file
|
|
|
|
*/
|
|
|
|
void uip_udp_appcall ( void ) {
|
|
|
|
}
|
2006-04-30 04:08:42 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Perform periodic processing of all TCP connections
|
|
|
|
*
|
|
|
|
* This allows TCP connections to retransmit data if necessary.
|
|
|
|
*/
|
|
|
|
static void tcp_periodic ( void ) {
|
|
|
|
struct pk_buff *pkb;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for ( i = 0 ; i < UIP_CONNS ; i++ ) {
|
|
|
|
uip_periodic ( i );
|
|
|
|
if ( uip_len > 0 ) {
|
|
|
|
pkb = alloc_pkb ( uip_len + MAX_LL_HEADER_LEN);
|
|
|
|
if ( ! pkb )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
pkb_reserve ( pkb, MAX_LL_HEADER_LEN );
|
|
|
|
pkb_put ( pkb, uip_len );
|
|
|
|
memcpy ( pkb->data, uip_buf, uip_len );
|
2006-06-16 02:19:46 +02:00
|
|
|
|
2006-06-18 00:36:27 +02:00
|
|
|
ipv4_uip_tx ( pkb );
|
2006-04-30 04:08:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-05-27 21:01:20 +02:00
|
|
|
/**
|
|
|
|
* Kick a connection into life
|
|
|
|
*
|
|
|
|
* @v conn TCP connection
|
|
|
|
*
|
|
|
|
* Call this function when you have new data to send and are not
|
|
|
|
* already being called as part of TCP processing.
|
|
|
|
*/
|
|
|
|
void tcp_kick ( struct tcp_connection *conn __unused ) {
|
|
|
|
/* Just kick all the connections; this will work for now */
|
|
|
|
tcp_periodic();
|
|
|
|
}
|
|
|
|
|
2006-04-30 04:08:42 +02:00
|
|
|
/**
|
|
|
|
* Single-step the TCP stack
|
|
|
|
*
|
|
|
|
* @v process TCP process
|
|
|
|
*
|
|
|
|
* This calls tcp_periodic() at regular intervals.
|
|
|
|
*/
|
|
|
|
static void tcp_step ( struct process *process ) {
|
2006-04-30 15:20:40 +02:00
|
|
|
static unsigned long timeout = 0;
|
2006-04-30 04:08:42 +02:00
|
|
|
|
|
|
|
if ( currticks() > timeout ) {
|
|
|
|
timeout = currticks() + ( TICKS_PER_SEC / 10 );
|
|
|
|
tcp_periodic ();
|
|
|
|
}
|
|
|
|
|
|
|
|
schedule ( process );
|
|
|
|
}
|
|
|
|
|
|
|
|
/** TCP stack process */
|
|
|
|
static struct process tcp_process = {
|
|
|
|
.step = tcp_step,
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Initialise the TCP stack */
|
|
|
|
static void init_tcp ( void ) {
|
|
|
|
schedule ( &tcp_process );
|
|
|
|
}
|
|
|
|
|
|
|
|
INIT_FN ( INIT_PROCESS, init_tcp, NULL, NULL );
|
2006-08-01 22:46:50 +02:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
/**
|
|
|
|
* List of registered TCP connections
|
|
|
|
*/
|
|
|
|
static LIST_HEAD ( tcp_conns );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* List of TCP states
|
|
|
|
*/
|
|
|
|
static const char *tcp_states[] = {
|
|
|
|
"CLOSED",
|
|
|
|
"LISTEN",
|
|
|
|
"SYN_SENT",
|
|
|
|
"SYN_RCVD",
|
|
|
|
"ESTABLISHED",
|
|
|
|
"FIN_WAIT_1",
|
|
|
|
"FIN_WAIT_2",
|
|
|
|
"CLOSING",
|
|
|
|
"TIME_WAIT",
|
|
|
|
"CLOSE_WAIT",
|
|
|
|
"LAST_ACK",
|
|
|
|
"INVALID" };
|
|
|
|
|
|
|
|
/**
|
|
|
|
* TCP state transition function
|
|
|
|
*
|
|
|
|
* @v conn TCP connection
|
|
|
|
* @v nxt_state Next TCP state
|
|
|
|
*/
|
2006-08-08 05:42:30 +02:00
|
|
|
void tcp_set_flags ( struct tcp_connection *conn ) {
|
2006-08-01 22:46:50 +02:00
|
|
|
|
|
|
|
/* Set the TCP flags */
|
|
|
|
switch ( conn->tcp_state ) {
|
|
|
|
case TCP_CLOSED:
|
|
|
|
if ( conn->tcp_lstate == TCP_SYN_RCVD ) {
|
|
|
|
conn->tcp_flags |= TCP_RST;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case TCP_LISTEN:
|
|
|
|
break;
|
|
|
|
case TCP_SYN_SENT:
|
|
|
|
if ( conn->tcp_lstate == TCP_LISTEN ||
|
|
|
|
conn->tcp_lstate == TCP_CLOSED ) {
|
|
|
|
conn->tcp_flags |= TCP_SYN;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case TCP_SYN_RCVD:
|
|
|
|
if ( conn->tcp_lstate == TCP_LISTEN ||
|
|
|
|
conn->tcp_lstate == TCP_SYN_SENT ) {
|
|
|
|
conn->tcp_flags |= ( TCP_SYN | TCP_ACK );
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case TCP_ESTABLISHED:
|
|
|
|
if ( conn->tcp_lstate == TCP_SYN_SENT ) {
|
|
|
|
conn->tcp_flags |= TCP_ACK;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case TCP_FIN_WAIT_1:
|
|
|
|
if ( conn->tcp_lstate == TCP_SYN_RCVD ||
|
|
|
|
conn->tcp_lstate == TCP_ESTABLISHED ) {
|
|
|
|
conn->tcp_flags |= TCP_FIN;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case TCP_FIN_WAIT_2:
|
|
|
|
break;
|
|
|
|
case TCP_CLOSING:
|
|
|
|
if ( conn->tcp_lstate == TCP_FIN_WAIT_1 ) {
|
|
|
|
conn->tcp_flags |= TCP_ACK;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case TCP_TIME_WAIT:
|
|
|
|
if ( conn->tcp_lstate == TCP_FIN_WAIT_1 ||
|
|
|
|
conn->tcp_lstate == TCP_FIN_WAIT_2 ) {
|
|
|
|
conn->tcp_flags |= TCP_ACK;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case TCP_CLOSE_WAIT:
|
|
|
|
if ( conn->tcp_lstate == TCP_ESTABLISHED ) {
|
|
|
|
conn->tcp_flags |= TCP_ACK;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case TCP_LAST_ACK:
|
|
|
|
if ( conn->tcp_lstate == TCP_CLOSE_WAIT ) {
|
|
|
|
conn->tcp_flags |= TCP_FIN;
|
|
|
|
}
|
2006-08-09 17:04:26 +02:00
|
|
|
if ( conn->tcp_lstate == TCP_ESTABLISHED ) {
|
|
|
|
conn->tcp_flags |= ( TCP_FIN | TCP_ACK );
|
|
|
|
}
|
2006-08-01 22:46:50 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
DBG ( "TCP_INVALID state %d\n", conn->tcp_state );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-08-08 05:42:30 +02:00
|
|
|
void tcp_trans ( struct tcp_connection *conn, int nxt_state ) {
|
|
|
|
/* Remember the last state */
|
|
|
|
conn->tcp_lstate = conn->tcp_state;
|
|
|
|
conn->tcp_state = nxt_state;
|
|
|
|
|
|
|
|
/* TODO: Check if this check is required */
|
|
|
|
if ( conn->tcp_lstate == conn->tcp_state ||
|
|
|
|
conn->tcp_state == TCP_INVALID ) {
|
|
|
|
conn->tcp_flags = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
tcp_set_flags ( conn );
|
|
|
|
}
|
|
|
|
|
2006-08-01 22:46:50 +02:00
|
|
|
/**
|
|
|
|
* Dump TCP header
|
|
|
|
*
|
|
|
|
* @v tcphdr TCP header
|
|
|
|
*/
|
|
|
|
void tcp_dump ( struct tcp_header *tcphdr ) {
|
2006-08-09 14:01:09 +02:00
|
|
|
DBG ( "TCP %p src:%d dest:%d seq:%lx ack:%lx hlen:%hd flags:%#hx\n",
|
2006-08-09 00:25:20 +02:00
|
|
|
tcphdr, ntohs ( tcphdr->src ), ntohs ( tcphdr->dest ), ntohl ( tcphdr->seq ),
|
2006-08-08 05:42:30 +02:00
|
|
|
ntohl ( tcphdr->ack ), ( ( tcphdr->hlen & TCP_MASK_HLEN ) / 16 ), ( tcphdr->flags & TCP_MASK_FLAGS ) );
|
2006-08-01 22:46:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize a TCP connection
|
|
|
|
*
|
|
|
|
* @v conn TCP connection
|
|
|
|
*
|
|
|
|
* This function assigns initial values to some fields in the connection
|
|
|
|
* structure. The application should call tcp_init_conn after creating a new
|
|
|
|
* connection before calling any other "tcp_*" function.
|
|
|
|
*
|
|
|
|
* struct tcp_connection my_conn;
|
|
|
|
* tcp_init_conn ( &my_conn );
|
|
|
|
* ...
|
|
|
|
*/
|
|
|
|
void tcp_init_conn ( struct tcp_connection *conn ) {
|
|
|
|
conn->local_port = 0;
|
|
|
|
conn->tcp_state = TCP_CLOSED;
|
|
|
|
conn->tcp_lstate = TCP_INVALID;
|
|
|
|
conn->tx_pkb = NULL;
|
|
|
|
conn->tcp_op = NULL;
|
|
|
|
}
|
|
|
|
|
2006-08-07 20:52:26 +02:00
|
|
|
/** Retry timer
|
|
|
|
*
|
|
|
|
* @v timer Retry timer
|
|
|
|
* @v over Failure indicator
|
|
|
|
*/
|
|
|
|
void tcp_expired ( struct retry_timer *timer, int over ) {
|
|
|
|
struct tcp_connection *conn;
|
2006-08-08 05:42:30 +02:00
|
|
|
conn = ( struct tcp_connection * ) container_of ( timer,
|
|
|
|
struct tcp_connection, timer );
|
|
|
|
DBG ( "Timer expired in %s\n", tcp_states[conn->tcp_state] );
|
|
|
|
switch ( conn->tcp_state ) {
|
|
|
|
case TCP_SYN_SENT:
|
|
|
|
if ( over ) {
|
2006-08-07 20:52:26 +02:00
|
|
|
tcp_trans ( conn, TCP_CLOSED );
|
2006-08-08 05:42:30 +02:00
|
|
|
DBG ( "Timeout! Connection closed\n" );
|
2006-08-07 20:52:26 +02:00
|
|
|
return;
|
2006-08-08 05:42:30 +02:00
|
|
|
}
|
|
|
|
goto send_tcp_nomsg;
|
|
|
|
case TCP_SYN_RCVD:
|
|
|
|
if ( over ) {
|
2006-08-07 20:52:26 +02:00
|
|
|
tcp_trans ( conn, TCP_CLOSED );
|
2006-08-08 05:42:30 +02:00
|
|
|
goto send_tcp_nomsg;
|
|
|
|
}
|
|
|
|
goto send_tcp_nomsg;
|
|
|
|
case TCP_ESTABLISHED:
|
|
|
|
if ( conn->tcp_lstate == TCP_SYN_SENT ) {
|
|
|
|
goto send_tcp_nomsg;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case TCP_CLOSE_WAIT:
|
|
|
|
if ( conn->tcp_lstate == TCP_ESTABLISHED ) {
|
|
|
|
goto send_tcp_nomsg;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case TCP_FIN_WAIT_1:
|
|
|
|
case TCP_FIN_WAIT_2:
|
|
|
|
goto send_tcp_nomsg;
|
|
|
|
case TCP_CLOSING:
|
|
|
|
case TCP_LAST_ACK:
|
|
|
|
if ( conn->tcp_lstate == TCP_CLOSE_WAIT ) {
|
|
|
|
goto send_tcp_nomsg;
|
2006-08-07 20:52:26 +02:00
|
|
|
}
|
|
|
|
return;
|
2006-08-08 05:42:30 +02:00
|
|
|
case TCP_TIME_WAIT:
|
|
|
|
tcp_trans ( conn, TCP_CLOSED );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* Retransmit the data */
|
|
|
|
tcp_set_flags ( conn );
|
|
|
|
tcp_senddata ( conn );
|
|
|
|
return;
|
2006-08-07 20:52:26 +02:00
|
|
|
|
|
|
|
send_tcp_nomsg:
|
2006-08-09 00:25:20 +02:00
|
|
|
free_pkb ( conn->tx_pkb );
|
2006-08-08 05:42:30 +02:00
|
|
|
conn->tx_pkb = alloc_pkb ( MIN_PKB_LEN );
|
|
|
|
pkb_reserve ( conn->tx_pkb, MAX_HDR_LEN );
|
|
|
|
tcp_set_flags ( conn );
|
|
|
|
int rc;
|
|
|
|
if ( ( rc = tcp_send ( conn, TCP_NOMSG, TCP_NOMSG_LEN ) ) != 0 ) {
|
|
|
|
DBG ( "Error sending TCP message (rc = %d)\n", rc );
|
2006-08-07 20:52:26 +02:00
|
|
|
}
|
2006-08-08 05:42:30 +02:00
|
|
|
return;
|
2006-08-07 20:52:26 +02:00
|
|
|
}
|
|
|
|
|
2006-08-01 22:46:50 +02:00
|
|
|
/**
|
|
|
|
* Connect to a remote server
|
|
|
|
*
|
|
|
|
* @v conn TCP connection
|
|
|
|
* @v peer Remote socket address
|
|
|
|
*
|
|
|
|
* This function initiates a TCP connection to the socket address specified in
|
|
|
|
* peer. It sends a SYN packet to peer. When the connection is established, the
|
|
|
|
* TCP stack calls the connected() callback function.
|
|
|
|
*/
|
2006-08-02 02:02:21 +02:00
|
|
|
int tcp_connectto ( struct tcp_connection *conn,
|
|
|
|
struct sockaddr_tcpip *peer ) {
|
2006-08-01 22:46:50 +02:00
|
|
|
int rc;
|
|
|
|
|
|
|
|
/* A connection can only be established from the CLOSED state */
|
|
|
|
if ( conn->tcp_state != TCP_CLOSED ) {
|
|
|
|
DBG ( "Error opening connection: Invalid state %s\n",
|
|
|
|
tcp_states[conn->tcp_state] );
|
|
|
|
return -EISCONN;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add the connection to the set of listening connections */
|
|
|
|
if ( ( rc = tcp_listen ( conn, conn->local_port ) ) != 0 ) {
|
|
|
|
return rc;
|
|
|
|
}
|
2006-08-02 02:02:21 +02:00
|
|
|
memcpy ( &conn->peer, peer, sizeof ( conn->peer ) );
|
2006-08-01 22:46:50 +02:00
|
|
|
|
2006-08-07 20:52:26 +02:00
|
|
|
/* Initialize the TCP timer */
|
|
|
|
conn->timer.expired = tcp_expired;
|
|
|
|
|
2006-08-01 22:46:50 +02:00
|
|
|
/* Send a SYN packet and transition to TCP_SYN_SENT */
|
2006-08-09 15:35:03 +02:00
|
|
|
conn->snd_una = random();
|
2006-08-01 22:46:50 +02:00
|
|
|
tcp_trans ( conn, TCP_SYN_SENT );
|
|
|
|
/* Allocate space for the packet */
|
|
|
|
free_pkb ( conn->tx_pkb );
|
|
|
|
conn->tx_pkb = alloc_pkb ( MIN_PKB_LEN );
|
|
|
|
pkb_reserve ( conn->tx_pkb, MAX_HDR_LEN );
|
|
|
|
conn->rcv_win = MAX_PKB_LEN - MAX_HDR_LEN; /* TODO: Is this OK? */
|
|
|
|
return tcp_send ( conn, TCP_NOMSG, TCP_NOMSG_LEN );
|
|
|
|
}
|
|
|
|
|
|
|
|
int tcp_connect ( struct tcp_connection *conn ) {
|
2006-08-02 02:02:21 +02:00
|
|
|
return tcp_connectto ( conn, &conn->peer );
|
2006-08-01 22:46:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Close the connection
|
|
|
|
*
|
|
|
|
* @v conn
|
|
|
|
*
|
|
|
|
* This function sends a FIN packet to the remote end of the connection. When
|
|
|
|
* the remote end of the connection ACKs the FIN (FIN consumes one byte on the
|
|
|
|
* snd stream), the stack invokes the closed() callback function.
|
|
|
|
*/
|
|
|
|
int tcp_close ( struct tcp_connection *conn ) {
|
|
|
|
/* A connection can only be closed if it is a connected state */
|
|
|
|
switch ( conn->tcp_state ) {
|
|
|
|
case TCP_SYN_RCVD:
|
|
|
|
case TCP_ESTABLISHED:
|
|
|
|
tcp_trans ( conn, TCP_FIN_WAIT_1 );
|
|
|
|
conn->tcp_op->closed ( conn, CONN_SNDCLOSE ); /* TODO: Check! */
|
|
|
|
/* FIN consumes one byte on the snd stream */
|
|
|
|
// conn->snd_una++;
|
|
|
|
goto send_tcp_nomsg;
|
|
|
|
case TCP_SYN_SENT:
|
|
|
|
case TCP_LISTEN:
|
|
|
|
/**
|
|
|
|
* Since the connection does not expect any packets from the
|
|
|
|
* remote end, it can be removed from the set of listening
|
|
|
|
* connections.
|
|
|
|
*/
|
|
|
|
list_del ( &conn->list );
|
|
|
|
tcp_trans ( conn, TCP_CLOSED );
|
|
|
|
conn->tcp_op->closed ( conn, CONN_SNDCLOSE );
|
|
|
|
return 0;
|
|
|
|
case TCP_CLOSE_WAIT:
|
|
|
|
tcp_trans ( conn, TCP_LAST_ACK );
|
|
|
|
conn->tcp_op->closed ( conn, CONN_SNDCLOSE ); /* TODO: Check! */
|
|
|
|
/* FIN consumes one byte on the snd stream */
|
|
|
|
// conn->snd_una++;
|
|
|
|
goto send_tcp_nomsg;
|
|
|
|
default:
|
|
|
|
DBG ( "tcp_close(): Invalid state %s\n",
|
|
|
|
tcp_states[conn->tcp_state] );
|
|
|
|
return -EPROTO;
|
|
|
|
}
|
|
|
|
|
|
|
|
send_tcp_nomsg:
|
|
|
|
free_pkb ( conn->tx_pkb );
|
|
|
|
conn->tx_pkb = alloc_pkb ( MIN_PKB_LEN );
|
|
|
|
conn->tcp_flags = TCP_FIN;
|
|
|
|
pkb_reserve ( conn->tx_pkb, MAX_HDR_LEN );
|
|
|
|
return tcp_send ( conn, TCP_NOMSG, TCP_NOMSG_LEN );
|
|
|
|
}
|
|
|
|
|
2006-08-09 14:08:20 +02:00
|
|
|
/**
|
|
|
|
* Bind TCP connection to local port
|
|
|
|
*
|
|
|
|
* @v conn TCP connection
|
|
|
|
* @v local_port Local port, in network byte order
|
|
|
|
* @ret rc Return status code
|
|
|
|
*/
|
|
|
|
int tcp_bind ( struct tcp_connection *conn, uint16_t local_port ) {
|
|
|
|
struct tcp_connection *existing;
|
|
|
|
|
|
|
|
list_for_each_entry ( existing, &tcp_conns, list ) {
|
|
|
|
if ( existing->local_port == local_port )
|
|
|
|
return -EADDRINUSE;
|
|
|
|
}
|
|
|
|
conn->local_port = local_port;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-08-01 22:46:50 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Listen for a packet
|
|
|
|
*
|
2006-08-09 14:08:20 +02:00
|
|
|
* @v conn TCP connection
|
|
|
|
* @v local_port Local port, in network byte order
|
2006-08-01 22:46:50 +02:00
|
|
|
*
|
2006-08-09 14:08:20 +02:00
|
|
|
* This function adds the connection to a list of registered tcp
|
|
|
|
* connections. If the local port is 0, the connection is assigned an
|
|
|
|
* available port between MIN_TCP_PORT and 65535.
|
2006-08-01 22:46:50 +02:00
|
|
|
*/
|
2006-08-09 14:08:20 +02:00
|
|
|
int tcp_listen ( struct tcp_connection *conn, uint16_t local_port ) {
|
|
|
|
static uint16_t try_port = 1024;
|
|
|
|
int rc;
|
|
|
|
|
2006-08-09 16:44:58 +02:00
|
|
|
#warning "Fix the port re-use bug"
|
|
|
|
/* If we re-use the same port, the connection should be reset
|
|
|
|
* and a new connection set up. This doesn't happen yet, so
|
|
|
|
* randomise the port to avoid hitting the problem.
|
|
|
|
*/
|
|
|
|
try_port = random();
|
|
|
|
|
2006-08-09 14:08:20 +02:00
|
|
|
/* If no port specified, find the first available port */
|
|
|
|
if ( ! local_port ) {
|
|
|
|
for ( ; try_port ; try_port++ ) {
|
|
|
|
if ( try_port < 1024 )
|
|
|
|
continue;
|
|
|
|
if ( tcp_listen ( conn, htons ( try_port ) ) == 0 )
|
|
|
|
return 0;
|
2006-08-01 22:46:50 +02:00
|
|
|
}
|
2006-08-09 14:08:20 +02:00
|
|
|
return -EADDRINUSE;
|
2006-08-01 22:46:50 +02:00
|
|
|
}
|
2006-08-09 14:08:20 +02:00
|
|
|
|
|
|
|
/* Attempt bind to local port */
|
|
|
|
if ( ( rc = tcp_bind ( conn, local_port ) ) != 0 )
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
/* Add to TCP connection list */
|
|
|
|
list_add ( &conn->list, &tcp_conns );
|
|
|
|
DBG ( "TCP opened %p on port %d\n", conn, ntohs ( local_port ) );
|
|
|
|
|
|
|
|
return 0;
|
2006-08-01 22:46:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send data
|
|
|
|
*
|
|
|
|
* @v conn TCP connection
|
|
|
|
*
|
|
|
|
* This function allocates space to the transmit buffer and invokes the
|
|
|
|
* senddata() callback function. It passes the allocated buffer to senddata().
|
|
|
|
* The applicaion may use this space to write it's data.
|
|
|
|
*/
|
|
|
|
int tcp_senddata ( struct tcp_connection *conn ) {
|
|
|
|
/* The connection must be in a state in which the user can send data */
|
|
|
|
switch ( conn->tcp_state ) {
|
|
|
|
case TCP_LISTEN:
|
|
|
|
tcp_trans ( conn, TCP_SYN_SENT );
|
2006-08-09 15:35:03 +02:00
|
|
|
conn->snd_una = random();
|
2006-08-01 22:46:50 +02:00
|
|
|
break;
|
|
|
|
case TCP_ESTABLISHED:
|
|
|
|
case TCP_CLOSE_WAIT:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
DBG ( "tcp_senddata: Invalid state %s\n",
|
|
|
|
tcp_states[conn->tcp_state] );
|
|
|
|
return -EPROTO;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate space to the TX buffer */
|
|
|
|
free_pkb ( conn->tx_pkb );
|
|
|
|
conn->tx_pkb = alloc_pkb ( MAX_PKB_LEN );
|
|
|
|
if ( !conn->tx_pkb ) {
|
|
|
|
DBG ( "Insufficient memory\n" );
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
pkb_reserve ( conn->tx_pkb, MAX_HDR_LEN );
|
|
|
|
/* Set the advertised window */
|
|
|
|
conn->rcv_win = pkb_available ( conn->tx_pkb );
|
|
|
|
/* Call the senddata() call back function */
|
|
|
|
conn->tcp_op->senddata ( conn, conn->tx_pkb->data,
|
|
|
|
pkb_available ( conn->tx_pkb ) );
|
2006-08-09 17:50:20 +02:00
|
|
|
/* Send pure ACK if senddata() didn't call tcp_send() */
|
|
|
|
if ( conn->tx_pkb ) {
|
|
|
|
tcp_send ( conn, TCP_NOMSG, TCP_NOMSG_LEN );
|
|
|
|
}
|
2006-08-01 22:46:50 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Transmit data
|
|
|
|
*
|
|
|
|
* @v conn TCP connection
|
|
|
|
* @v data Data to be sent
|
|
|
|
* @v len Length of the data
|
|
|
|
*
|
|
|
|
* This function sends data to the peer socket address
|
|
|
|
*/
|
|
|
|
int tcp_send ( struct tcp_connection *conn, const void *data, size_t len ) {
|
2006-08-02 02:02:21 +02:00
|
|
|
struct sockaddr_tcpip *peer = &conn->peer;
|
2006-08-09 16:27:07 +02:00
|
|
|
struct pk_buff *pkb;
|
2006-08-01 22:46:50 +02:00
|
|
|
int slen;
|
|
|
|
|
2006-08-09 16:27:07 +02:00
|
|
|
/* Take ownership of the TX buffer from the connection */
|
|
|
|
pkb = conn->tx_pkb;
|
|
|
|
conn->tx_pkb = NULL;
|
|
|
|
|
2006-08-01 22:46:50 +02:00
|
|
|
/* Determine the amount of data to be sent */
|
|
|
|
slen = len < conn->snd_win ? len : conn->snd_win;
|
|
|
|
/* Copy payload */
|
|
|
|
memmove ( pkb_put ( pkb, slen ), data, slen );
|
|
|
|
|
|
|
|
/* Fill up the TCP header */
|
|
|
|
struct tcp_header *tcphdr = pkb_push ( pkb, sizeof ( *tcphdr ) );
|
|
|
|
|
|
|
|
/* Source port, assumed to be in network byte order in conn */
|
|
|
|
tcphdr->src = conn->local_port;
|
|
|
|
/* Destination port, assumed to be in network byte order in peer */
|
2006-08-02 02:02:21 +02:00
|
|
|
tcphdr->dest = peer->st_port;
|
2006-08-01 22:46:50 +02:00
|
|
|
tcphdr->seq = htonl ( conn->snd_una );
|
|
|
|
tcphdr->ack = htonl ( conn->rcv_nxt );
|
|
|
|
/* Header length, = 0x50 (without TCP options) */
|
|
|
|
tcphdr->hlen = ( uint8_t ) ( ( sizeof ( *tcphdr ) / 4 ) << 4 );
|
|
|
|
/* Copy TCP flags, and then reset the variable */
|
|
|
|
tcphdr->flags = conn->tcp_flags;
|
|
|
|
conn->tcp_flags = 0;
|
|
|
|
/* Advertised window, in network byte order */
|
|
|
|
tcphdr->win = htons ( conn->rcv_win );
|
|
|
|
/* Set urgent pointer to 0 */
|
|
|
|
tcphdr->urg = 0;
|
2006-08-07 09:37:23 +02:00
|
|
|
/* Calculate and store partial checksum, in host byte order */
|
2006-08-01 22:46:50 +02:00
|
|
|
tcphdr->csum = 0;
|
|
|
|
tcphdr->csum = tcpip_chksum ( pkb->data, pkb_len ( pkb ) );
|
|
|
|
|
|
|
|
/* Dump the TCP header */
|
|
|
|
|
2006-08-07 20:52:26 +02:00
|
|
|
/* Start the timer */
|
2006-08-08 05:42:30 +02:00
|
|
|
if ( ( conn->tcp_state == TCP_ESTABLISHED && conn->tcp_lstate == TCP_SYN_SENT ) ||
|
|
|
|
( conn->tcp_state == TCP_LISTEN && conn->tcp_lstate == TCP_SYN_RCVD ) ||
|
2006-08-09 17:50:20 +02:00
|
|
|
( conn->tcp_state == TCP_CLOSED && conn->tcp_lstate == TCP_SYN_RCVD ) ||
|
|
|
|
( conn->tcp_state == TCP_ESTABLISHED && ( len == 0 ) ) ) {
|
2006-08-08 05:42:30 +02:00
|
|
|
// Don't start the timer
|
|
|
|
} else {
|
|
|
|
start_timer ( &conn->timer );
|
|
|
|
}
|
2006-08-07 20:52:26 +02:00
|
|
|
|
2006-08-01 22:46:50 +02:00
|
|
|
/* Transmit packet */
|
|
|
|
return tcpip_tx ( pkb, &tcp_protocol, peer );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Process received packet
|
|
|
|
*
|
|
|
|
* @v pkb Packet buffer
|
|
|
|
* @v partial Partial checksum
|
|
|
|
*/
|
2006-08-02 02:02:21 +02:00
|
|
|
static int tcp_rx ( struct pk_buff *pkb,
|
|
|
|
struct sockaddr_tcpip *st_src __unused,
|
|
|
|
struct sockaddr_tcpip *st_dest __unused ) {
|
2006-08-01 22:46:50 +02:00
|
|
|
struct tcp_connection *conn;
|
|
|
|
struct tcp_header *tcphdr;
|
|
|
|
uint32_t acked, toack;
|
|
|
|
int hlen;
|
2006-08-09 16:27:07 +02:00
|
|
|
int rc;
|
2006-08-01 22:46:50 +02:00
|
|
|
|
|
|
|
/* Sanity check */
|
|
|
|
if ( pkb_len ( pkb ) < sizeof ( *tcphdr ) ) {
|
|
|
|
DBG ( "Packet too short (%d bytes)\n", pkb_len ( pkb ) );
|
2006-08-09 16:27:07 +02:00
|
|
|
rc = -EINVAL;
|
|
|
|
goto done;
|
2006-08-01 22:46:50 +02:00
|
|
|
}
|
|
|
|
|
2006-08-08 05:42:30 +02:00
|
|
|
|
2006-08-01 22:46:50 +02:00
|
|
|
/* Process TCP header */
|
|
|
|
tcphdr = pkb->data;
|
2006-08-08 05:42:30 +02:00
|
|
|
tcp_dump ( tcphdr );
|
2006-08-01 22:46:50 +02:00
|
|
|
|
|
|
|
/* Verify header length */
|
|
|
|
hlen = ( ( tcphdr->hlen & TCP_MASK_HLEN ) / 16 ) * 4;
|
|
|
|
if ( hlen != sizeof ( *tcphdr ) ) {
|
2006-08-08 05:42:30 +02:00
|
|
|
if ( hlen == sizeof ( *tcphdr ) + 4 ) {
|
|
|
|
DBG ( "TCP options sent\n" );
|
|
|
|
} else {
|
|
|
|
DBG ( "Bad header length (%d bytes)\n", hlen );
|
2006-08-09 16:27:07 +02:00
|
|
|
rc = -EINVAL;
|
|
|
|
goto done;
|
2006-08-08 05:42:30 +02:00
|
|
|
}
|
2006-08-01 22:46:50 +02:00
|
|
|
}
|
2006-08-09 00:25:20 +02:00
|
|
|
|
|
|
|
|
2006-08-01 22:46:50 +02:00
|
|
|
/* TODO: Verify checksum */
|
|
|
|
|
|
|
|
/* Demux TCP connection */
|
|
|
|
list_for_each_entry ( conn, &tcp_conns, list ) {
|
|
|
|
if ( tcphdr->dest == conn->local_port ) {
|
|
|
|
goto found_conn;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DBG ( "No connection found on port %d\n", ntohs ( tcphdr->dest ) );
|
2006-08-09 16:27:07 +02:00
|
|
|
rc = 0;
|
|
|
|
goto done;
|
2006-08-01 22:46:50 +02:00
|
|
|
|
|
|
|
found_conn:
|
2006-08-07 20:52:26 +02:00
|
|
|
/* Stop the timer */
|
|
|
|
stop_timer ( &conn->timer );
|
|
|
|
|
2006-08-01 22:46:50 +02:00
|
|
|
/* Set the advertised window */
|
|
|
|
conn->snd_win = tcphdr->win;
|
|
|
|
|
|
|
|
/* TCP State Machine */
|
|
|
|
conn->tcp_lstate = conn->tcp_state;
|
|
|
|
switch ( conn->tcp_state ) {
|
|
|
|
case TCP_CLOSED:
|
|
|
|
DBG ( "tcp_rx(): Invalid state %s\n",
|
|
|
|
tcp_states[conn->tcp_state] );
|
2006-08-09 16:27:07 +02:00
|
|
|
rc = -EINVAL;
|
|
|
|
goto done;
|
2006-08-01 22:46:50 +02:00
|
|
|
case TCP_LISTEN:
|
|
|
|
if ( tcphdr->flags & TCP_SYN ) {
|
|
|
|
tcp_trans ( conn, TCP_SYN_RCVD );
|
|
|
|
/* Synchronize the sequence numbers */
|
|
|
|
conn->rcv_nxt = ntohl ( tcphdr->seq ) + 1;
|
2006-08-09 15:35:03 +02:00
|
|
|
conn->tcp_flags |= TCP_ACK;
|
2006-08-01 22:46:50 +02:00
|
|
|
|
|
|
|
/* Set the sequence number for the snd stream */
|
2006-08-09 15:35:03 +02:00
|
|
|
conn->snd_una = random();
|
|
|
|
conn->tcp_flags |= TCP_SYN;
|
2006-08-01 22:46:50 +02:00
|
|
|
|
|
|
|
/* Send a SYN,ACK packet */
|
|
|
|
goto send_tcp_nomsg;
|
|
|
|
}
|
|
|
|
/* Unexpected packet */
|
|
|
|
goto unexpected;
|
|
|
|
case TCP_SYN_SENT:
|
|
|
|
if ( tcphdr->flags & TCP_SYN ) {
|
|
|
|
/* Synchronize the sequence number in rcv stream */
|
|
|
|
conn->rcv_nxt = ntohl ( tcphdr->seq ) + 1;
|
2006-08-09 15:35:03 +02:00
|
|
|
conn->tcp_flags |= TCP_ACK;
|
2006-08-01 22:46:50 +02:00
|
|
|
|
|
|
|
if ( tcphdr->flags & TCP_ACK ) {
|
|
|
|
tcp_trans ( conn, TCP_ESTABLISHED );
|
|
|
|
/**
|
|
|
|
* Process ACK of SYN. This does not invoke the
|
|
|
|
* acked() callback function.
|
|
|
|
*/
|
|
|
|
conn->snd_una = ntohl ( tcphdr->ack );
|
|
|
|
conn->tcp_op->connected ( conn );
|
2006-08-09 15:35:03 +02:00
|
|
|
conn->tcp_flags |= TCP_ACK;
|
2006-08-08 05:42:30 +02:00
|
|
|
tcp_senddata ( conn );
|
2006-08-09 16:27:07 +02:00
|
|
|
rc = 0;
|
|
|
|
goto done;
|
2006-08-01 22:46:50 +02:00
|
|
|
} else {
|
|
|
|
tcp_trans ( conn, TCP_SYN_RCVD );
|
2006-08-09 15:35:03 +02:00
|
|
|
conn->tcp_flags |= TCP_SYN;
|
2006-08-09 00:25:20 +02:00
|
|
|
goto send_tcp_nomsg;
|
2006-08-01 22:46:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Unexpected packet */
|
|
|
|
goto unexpected;
|
|
|
|
case TCP_SYN_RCVD:
|
|
|
|
if ( tcphdr->flags & TCP_RST ) {
|
|
|
|
tcp_trans ( conn, TCP_LISTEN );
|
|
|
|
conn->tcp_op->closed ( conn, CONN_RESTART );
|
2006-08-09 16:27:07 +02:00
|
|
|
rc = 0;
|
|
|
|
goto done;
|
2006-08-01 22:46:50 +02:00
|
|
|
}
|
|
|
|
if ( tcphdr->flags & TCP_ACK ) {
|
|
|
|
tcp_trans ( conn, TCP_ESTABLISHED );
|
|
|
|
/**
|
|
|
|
* Process ACK of SYN. It neither invokes the callback
|
|
|
|
* function nor does it send an ACK.
|
|
|
|
*/
|
|
|
|
conn->snd_una = tcphdr->ack - 1;
|
|
|
|
conn->tcp_op->connected ( conn );
|
2006-08-09 16:27:07 +02:00
|
|
|
rc = 0;
|
|
|
|
goto done;
|
2006-08-01 22:46:50 +02:00
|
|
|
}
|
|
|
|
/* Unexpected packet */
|
|
|
|
goto unexpected;
|
|
|
|
case TCP_ESTABLISHED:
|
2006-08-09 00:25:20 +02:00
|
|
|
#if 0
|
2006-08-01 22:46:50 +02:00
|
|
|
if ( tcphdr->flags & TCP_FIN ) {
|
|
|
|
tcp_trans ( conn, TCP_CLOSE_WAIT );
|
|
|
|
/* FIN consumes one byte */
|
|
|
|
conn->rcv_nxt++;
|
2006-08-09 15:35:03 +02:00
|
|
|
conn->tcp_flags |= TCP_ACK;
|
2006-08-01 22:46:50 +02:00
|
|
|
/* Send an acknowledgement */
|
|
|
|
goto send_tcp_nomsg;
|
|
|
|
}
|
2006-08-09 00:25:20 +02:00
|
|
|
#endif
|
2006-08-01 22:46:50 +02:00
|
|
|
/* Packet might contain data */
|
|
|
|
break;
|
|
|
|
case TCP_FIN_WAIT_1:
|
|
|
|
if ( tcphdr->flags & TCP_FIN ) {
|
|
|
|
conn->rcv_nxt++;
|
2006-08-09 15:35:03 +02:00
|
|
|
conn->tcp_flags |= TCP_ACK;
|
2006-08-01 22:46:50 +02:00
|
|
|
conn->tcp_op->closed ( conn, CONN_SNDCLOSE );
|
|
|
|
|
|
|
|
if ( tcphdr->flags & TCP_ACK ) {
|
|
|
|
tcp_trans ( conn, TCP_TIME_WAIT );
|
|
|
|
} else {
|
|
|
|
tcp_trans ( conn, TCP_CLOSING );
|
|
|
|
}
|
|
|
|
/* Send an acknowledgement */
|
|
|
|
goto send_tcp_nomsg;
|
|
|
|
}
|
|
|
|
if ( tcphdr->flags & TCP_ACK ) {
|
|
|
|
tcp_trans ( conn, TCP_FIN_WAIT_2 );
|
|
|
|
}
|
|
|
|
/* Packet might contain data */
|
|
|
|
break;
|
|
|
|
case TCP_FIN_WAIT_2:
|
|
|
|
if ( tcphdr->flags & TCP_FIN ) {
|
|
|
|
tcp_trans ( conn, TCP_TIME_WAIT );
|
|
|
|
/* FIN consumes one byte */
|
|
|
|
conn->rcv_nxt++;
|
2006-08-09 15:35:03 +02:00
|
|
|
conn->tcp_flags |= TCP_ACK;
|
2006-08-01 22:46:50 +02:00
|
|
|
goto send_tcp_nomsg;
|
|
|
|
}
|
|
|
|
/* Packet might contain data */
|
|
|
|
break;
|
|
|
|
case TCP_CLOSING:
|
|
|
|
if ( tcphdr->flags & TCP_ACK ) {
|
|
|
|
tcp_trans ( conn, TCP_TIME_WAIT );
|
2006-08-08 05:42:30 +02:00
|
|
|
start_timer ( &conn->timer );
|
2006-08-09 16:27:07 +02:00
|
|
|
rc = 0;
|
|
|
|
goto done;
|
2006-08-01 22:46:50 +02:00
|
|
|
}
|
|
|
|
/* Unexpected packet */
|
|
|
|
goto unexpected;
|
|
|
|
case TCP_TIME_WAIT:
|
|
|
|
/* Unexpected packet */
|
|
|
|
goto unexpected;
|
|
|
|
case TCP_CLOSE_WAIT:
|
|
|
|
/* Packet could acknowledge data */
|
|
|
|
break;
|
|
|
|
case TCP_LAST_ACK:
|
|
|
|
if ( tcphdr->flags & TCP_ACK ) {
|
|
|
|
tcp_trans ( conn, TCP_CLOSED );
|
2006-08-09 16:27:07 +02:00
|
|
|
rc = 0;
|
|
|
|
goto done;
|
2006-08-01 22:46:50 +02:00
|
|
|
}
|
|
|
|
/* Unexpected packet */
|
|
|
|
goto unexpected;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Any packet reaching this point either contains new data or
|
|
|
|
* acknowledges previously transmitted data.
|
|
|
|
*/
|
|
|
|
assert ( ( tcphdr->flags & TCP_ACK ) ||
|
|
|
|
pkb_len ( pkb ) > sizeof ( *tcphdr ) );
|
|
|
|
|
|
|
|
/* Check for new data */
|
|
|
|
toack = pkb_len ( pkb ) - hlen;
|
|
|
|
if ( toack > 0 ) {
|
|
|
|
/* Check if expected sequence number */
|
|
|
|
if ( conn->rcv_nxt == ntohl ( tcphdr->seq ) ) {
|
|
|
|
conn->rcv_nxt += toack;
|
2006-08-09 14:01:09 +02:00
|
|
|
conn->tcp_op->newdata ( conn, pkb->data + hlen,
|
|
|
|
toack );
|
2006-08-09 00:25:20 +02:00
|
|
|
} else {
|
2006-08-09 15:35:03 +02:00
|
|
|
DBG ( "Unexpected sequence number %lx (wanted %lx)\n",
|
2006-08-09 14:01:09 +02:00
|
|
|
ntohl ( tcphdr->seq ), conn->rcv_nxt );
|
2006-08-01 22:46:50 +02:00
|
|
|
}
|
|
|
|
|
2006-08-09 17:50:20 +02:00
|
|
|
/* Acknowledge new data */
|
|
|
|
conn->tcp_flags |= TCP_ACK;
|
|
|
|
if ( !( tcphdr->flags & TCP_ACK ) ) {
|
|
|
|
goto send_tcp_nomsg;
|
2006-08-01 22:46:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Process ACK */
|
|
|
|
if ( tcphdr->flags & TCP_ACK ) {
|
|
|
|
acked = ntohl ( tcphdr->ack ) - conn->snd_una;
|
|
|
|
if ( acked < 0 ) { /* TODO: Replace all uint32_t arith */
|
|
|
|
DBG ( "Previously ACKed (%d)\n", tcphdr->ack );
|
2006-08-09 16:27:07 +02:00
|
|
|
rc = 0;
|
|
|
|
goto done;
|
2006-08-01 22:46:50 +02:00
|
|
|
}
|
|
|
|
/* Advance snd stream */
|
|
|
|
conn->snd_una += acked;
|
|
|
|
/* Invoke the acked() callback function */
|
|
|
|
conn->tcp_op->acked ( conn, acked );
|
|
|
|
/* Invoke the senddata() callback function */
|
|
|
|
tcp_senddata ( conn );
|
|
|
|
}
|
2006-08-09 00:25:20 +02:00
|
|
|
|
|
|
|
/* If the connection is in ESTABLISHED and it receives a FIN */
|
|
|
|
if ( conn->tcp_state == ESTABLISHED && ( tcphdr->flags & TCP_FIN ) ) {
|
2006-08-09 17:04:26 +02:00
|
|
|
if ( tcphdr->flags & TCP_ACK ) {
|
|
|
|
tcp_trans ( conn, TCP_LAST_ACK );
|
|
|
|
goto send_tcp_nomsg;
|
|
|
|
}
|
2006-08-09 00:25:20 +02:00
|
|
|
tcp_trans ( conn, TCP_CLOSE_WAIT );
|
2006-08-09 17:04:26 +02:00
|
|
|
// conn->tcp_op->closed ( conn, CONN_SNDCLOSE );
|
2006-08-09 00:25:20 +02:00
|
|
|
conn->rcv_nxt++;
|
|
|
|
if ( ! ( tcphdr->flags & TCP_ACK ) ) {
|
2006-08-09 15:35:03 +02:00
|
|
|
conn->tcp_flags |= TCP_ACK;
|
2006-08-09 00:25:20 +02:00
|
|
|
/* Send an acknowledgement */
|
|
|
|
goto send_tcp_nomsg;
|
|
|
|
}
|
|
|
|
/* Otherwise, the packet has been ACKed already */
|
|
|
|
}
|
2006-08-09 16:27:07 +02:00
|
|
|
rc = 0;
|
|
|
|
goto done;
|
2006-08-01 22:46:50 +02:00
|
|
|
|
|
|
|
send_tcp_nomsg:
|
|
|
|
free_pkb ( conn->tx_pkb );
|
|
|
|
conn->tx_pkb = alloc_pkb ( MIN_PKB_LEN );
|
|
|
|
pkb_reserve ( conn->tx_pkb, MAX_HDR_LEN );
|
|
|
|
if ( ( rc = tcp_send ( conn, TCP_NOMSG, TCP_NOMSG_LEN ) ) != 0 ) {
|
|
|
|
DBG ( "Error sending TCP message (rc = %d)\n", rc );
|
|
|
|
}
|
2006-08-09 16:27:07 +02:00
|
|
|
goto done;
|
2006-08-01 22:46:50 +02:00
|
|
|
|
|
|
|
unexpected:
|
2006-08-08 05:42:30 +02:00
|
|
|
DBG ( "Unexpected packet received in %s with flags = %#hx\n",
|
|
|
|
tcp_states[conn->tcp_state], tcphdr->flags & TCP_MASK_FLAGS );
|
|
|
|
tcp_close ( conn );
|
2006-08-01 22:46:50 +02:00
|
|
|
free_pkb ( conn->tx_pkb );
|
2006-08-09 16:27:07 +02:00
|
|
|
conn->tx_pkb = NULL;
|
|
|
|
rc = -EINVAL;
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
done:
|
|
|
|
free_pkb ( pkb );
|
|
|
|
return rc;
|
2006-08-01 22:46:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** TCP protocol */
|
2006-08-09 06:42:14 +02:00
|
|
|
struct tcpip_protocol tcp_protocol __tcpip_protocol = {
|
2006-08-01 22:46:50 +02:00
|
|
|
.name = "TCP",
|
|
|
|
.rx = tcp_rx,
|
2006-08-02 02:02:21 +02:00
|
|
|
.tcpip_proto = IP_TCP,
|
2006-08-01 22:46:50 +02:00
|
|
|
.csum_offset = 16,
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* USE_UIP */
|