2006-04-30 01:16:37 +00:00
|
|
|
#include <string.h>
|
2006-08-01 20:46:50 +00:00
|
|
|
#include <stdlib.h>
|
2007-01-19 01:13:12 +00:00
|
|
|
#include <stdio.h>
|
2006-04-30 01:16:37 +00:00
|
|
|
#include <assert.h>
|
2006-04-30 16:59:45 +00:00
|
|
|
#include <errno.h>
|
2006-12-27 23:09:46 +00:00
|
|
|
#include <byteswap.h>
|
|
|
|
#include <timer.h>
|
2007-05-19 18:39:40 +00:00
|
|
|
#include <gpxe/iobuf.h>
|
2007-01-18 20:39:17 +00:00
|
|
|
#include <gpxe/malloc.h>
|
2006-08-07 18:52:26 +00:00
|
|
|
#include <gpxe/retry.h>
|
2007-05-25 15:58:42 +00:00
|
|
|
#include <gpxe/refcnt.h>
|
|
|
|
#include <gpxe/xfer.h>
|
|
|
|
#include <gpxe/open.h>
|
2006-12-27 23:09:46 +00:00
|
|
|
#include <gpxe/tcpip.h>
|
|
|
|
#include <gpxe/tcp.h>
|
2006-04-30 01:16:37 +00:00
|
|
|
|
|
|
|
/** @file
|
|
|
|
*
|
|
|
|
* TCP protocol
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2007-05-25 15:58:42 +00:00
|
|
|
/** A TCP connection */
|
2006-12-27 23:09:46 +00:00
|
|
|
struct tcp_connection {
|
2007-05-25 15:58:42 +00:00
|
|
|
/** Reference counter */
|
|
|
|
struct refcnt refcnt;
|
2006-12-27 23:09:46 +00:00
|
|
|
/** List of TCP connections */
|
|
|
|
struct list_head list;
|
|
|
|
|
2007-05-25 15:58:42 +00:00
|
|
|
/** Data transfer interface */
|
|
|
|
struct xfer_interface xfer;
|
|
|
|
/** Data transfer interface closed flag */
|
|
|
|
int xfer_closed;
|
|
|
|
|
2006-12-27 23:09:46 +00:00
|
|
|
/** Remote socket address */
|
|
|
|
struct sockaddr_tcpip peer;
|
|
|
|
/** Local port, in network byte order */
|
2007-05-25 15:58:42 +00:00
|
|
|
unsigned int local_port;
|
2006-12-27 23:09:46 +00:00
|
|
|
|
|
|
|
/** Current TCP state */
|
|
|
|
unsigned int tcp_state;
|
|
|
|
/** Previous TCP state
|
|
|
|
*
|
|
|
|
* Maintained only for debug messages
|
|
|
|
*/
|
|
|
|
unsigned int prev_tcp_state;
|
|
|
|
/** Current sequence number
|
|
|
|
*
|
|
|
|
* Equivalent to SND.UNA in RFC 793 terminology.
|
|
|
|
*/
|
|
|
|
uint32_t snd_seq;
|
|
|
|
/** Unacknowledged sequence count
|
|
|
|
*
|
|
|
|
* Equivalent to (SND.NXT-SND.UNA) in RFC 793 terminology.
|
|
|
|
*/
|
|
|
|
uint32_t snd_sent;
|
|
|
|
/** Send window
|
|
|
|
*
|
|
|
|
* Equivalent to SND.WND in RFC 793 terminology
|
|
|
|
*/
|
|
|
|
uint32_t snd_win;
|
|
|
|
/** Current acknowledgement number
|
|
|
|
*
|
|
|
|
* Equivalent to RCV.NXT in RFC 793 terminology.
|
|
|
|
*/
|
|
|
|
uint32_t rcv_ack;
|
2006-04-30 01:16:37 +00:00
|
|
|
|
2007-05-25 15:58:42 +00:00
|
|
|
/** Transmit queue */
|
|
|
|
struct list_head queue;
|
2006-12-27 23:09:46 +00:00
|
|
|
/** Retransmission timer */
|
|
|
|
struct retry_timer timer;
|
|
|
|
};
|
2006-04-30 01:16:37 +00:00
|
|
|
|
|
|
|
/**
|
2006-12-27 23:09:46 +00:00
|
|
|
* List of registered TCP connections
|
|
|
|
*/
|
|
|
|
static LIST_HEAD ( tcp_conns );
|
|
|
|
|
2007-05-25 15:58:42 +00:00
|
|
|
/* Forward declarations */
|
|
|
|
static struct xfer_interface_operations tcp_xfer_operations;
|
|
|
|
static void tcp_expired ( struct retry_timer *timer, int over );
|
|
|
|
static int tcp_rx_ack ( struct tcp_connection *tcp, uint32_t ack,
|
|
|
|
uint32_t win );
|
|
|
|
|
2006-12-27 23:09:46 +00:00
|
|
|
/**
|
|
|
|
* Name TCP state
|
2006-04-30 01:16:37 +00:00
|
|
|
*
|
2006-12-27 23:09:46 +00:00
|
|
|
* @v state TCP state
|
|
|
|
* @ret name Name of TCP state
|
2006-04-30 01:16:37 +00:00
|
|
|
*/
|
2006-12-27 23:09:46 +00:00
|
|
|
static inline __attribute__ (( always_inline )) const char *
|
|
|
|
tcp_state ( int state ) {
|
|
|
|
switch ( state ) {
|
|
|
|
case TCP_CLOSED: return "CLOSED";
|
|
|
|
case TCP_LISTEN: return "LISTEN";
|
|
|
|
case TCP_SYN_SENT: return "SYN_SENT";
|
|
|
|
case TCP_SYN_RCVD: return "SYN_RCVD";
|
|
|
|
case TCP_ESTABLISHED: return "ESTABLISHED";
|
|
|
|
case TCP_FIN_WAIT_1: return "FIN_WAIT_1";
|
|
|
|
case TCP_FIN_WAIT_2: return "FIN_WAIT_2";
|
|
|
|
case TCP_CLOSING_OR_LAST_ACK: return "CLOSING/LAST_ACK";
|
|
|
|
case TCP_TIME_WAIT: return "TIME_WAIT";
|
|
|
|
case TCP_CLOSE_WAIT: return "CLOSE_WAIT";
|
|
|
|
default: return "INVALID";
|
|
|
|
}
|
2006-04-30 01:16:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2006-12-27 23:09:46 +00:00
|
|
|
* Dump TCP state transition
|
2006-04-30 01:16:37 +00:00
|
|
|
*
|
2007-01-31 02:09:13 +00:00
|
|
|
* @v tcp TCP connection
|
2006-04-30 01:16:37 +00:00
|
|
|
*/
|
2006-12-27 23:09:46 +00:00
|
|
|
static inline __attribute__ (( always_inline )) void
|
2007-01-31 02:09:13 +00:00
|
|
|
tcp_dump_state ( struct tcp_connection *tcp ) {
|
2006-04-30 01:16:37 +00:00
|
|
|
|
2007-01-31 02:09:13 +00:00
|
|
|
if ( tcp->tcp_state != tcp->prev_tcp_state ) {
|
|
|
|
DBGC ( tcp, "TCP %p transitioned from %s to %s\n", tcp,
|
|
|
|
tcp_state ( tcp->prev_tcp_state ),
|
|
|
|
tcp_state ( tcp->tcp_state ) );
|
2006-12-27 23:09:46 +00:00
|
|
|
}
|
2007-01-31 02:09:13 +00:00
|
|
|
tcp->prev_tcp_state = tcp->tcp_state;
|
2006-04-30 01:16:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2006-12-27 23:09:46 +00:00
|
|
|
* Dump TCP flags
|
2006-04-30 01:16:37 +00:00
|
|
|
*
|
2006-12-27 23:09:46 +00:00
|
|
|
* @v flags TCP flags
|
2006-04-30 01:16:37 +00:00
|
|
|
*/
|
2006-12-27 23:09:46 +00:00
|
|
|
static inline __attribute__ (( always_inline )) void
|
2007-01-31 02:09:13 +00:00
|
|
|
tcp_dump_flags ( struct tcp_connection *tcp, unsigned int flags ) {
|
2006-12-27 23:09:46 +00:00
|
|
|
if ( flags & TCP_RST )
|
2007-01-31 02:09:13 +00:00
|
|
|
DBGC ( tcp, " RST" );
|
2006-12-27 23:09:46 +00:00
|
|
|
if ( flags & TCP_SYN )
|
2007-01-31 02:09:13 +00:00
|
|
|
DBGC ( tcp, " SYN" );
|
2006-12-27 23:09:46 +00:00
|
|
|
if ( flags & TCP_PSH )
|
2007-01-31 02:09:13 +00:00
|
|
|
DBGC ( tcp, " PSH" );
|
2006-12-27 23:09:46 +00:00
|
|
|
if ( flags & TCP_FIN )
|
2007-01-31 02:09:13 +00:00
|
|
|
DBGC ( tcp, " FIN" );
|
2006-12-27 23:09:46 +00:00
|
|
|
if ( flags & TCP_ACK )
|
2007-01-31 02:09:13 +00:00
|
|
|
DBGC ( tcp, " ACK" );
|
2006-04-30 01:16:37 +00:00
|
|
|
}
|
|
|
|
|
2007-05-25 15:58:42 +00:00
|
|
|
/***************************************************************************
|
|
|
|
*
|
|
|
|
* Open and close
|
|
|
|
*
|
|
|
|
***************************************************************************
|
|
|
|
*/
|
|
|
|
|
2006-04-30 01:16:37 +00:00
|
|
|
/**
|
2007-05-25 15:58:42 +00:00
|
|
|
* Bind TCP connection to local port
|
2006-12-27 23:09:46 +00:00
|
|
|
*
|
2007-05-25 15:58:42 +00:00
|
|
|
* @v tcp TCP connection
|
|
|
|
* @v port Local port number, in network-endian order
|
|
|
|
* @ret rc Return status code
|
2006-04-30 01:16:37 +00:00
|
|
|
*
|
2007-05-25 15:58:42 +00:00
|
|
|
* If the port is 0, the connection is assigned an available port
|
|
|
|
* between 1024 and 65535.
|
2006-04-30 01:16:37 +00:00
|
|
|
*/
|
2007-05-25 15:58:42 +00:00
|
|
|
static int tcp_bind ( struct tcp_connection *tcp, unsigned int port ) {
|
|
|
|
struct tcp_connection *existing;
|
|
|
|
static uint16_t try_port = 1024;
|
2007-01-31 02:09:13 +00:00
|
|
|
|
2007-05-25 15:58:42 +00:00
|
|
|
/* If no port specified, find the first available port */
|
|
|
|
if ( ! port ) {
|
|
|
|
for ( ; try_port ; try_port++ ) {
|
|
|
|
if ( try_port < 1024 )
|
|
|
|
continue;
|
|
|
|
if ( tcp_bind ( tcp, htons ( try_port ) ) == 0 )
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
DBGC ( tcp, "TCP %p could not bind: no free ports\n", tcp );
|
|
|
|
return -EADDRINUSE;
|
2007-01-31 02:09:13 +00:00
|
|
|
}
|
2007-05-25 15:58:42 +00:00
|
|
|
|
|
|
|
/* Attempt bind to local port */
|
|
|
|
list_for_each_entry ( existing, &tcp_conns, list ) {
|
|
|
|
if ( existing->local_port == port ) {
|
|
|
|
DBGC ( tcp, "TCP %p could not bind: port %d in use\n",
|
|
|
|
tcp, ntohs ( port ) );
|
|
|
|
return -EADDRINUSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tcp->local_port = port;
|
|
|
|
|
|
|
|
DBGC ( tcp, "TCP %p bound to port %d\n", tcp, ntohs ( port ) );
|
|
|
|
return 0;
|
2006-04-30 01:16:37 +00:00
|
|
|
}
|
2006-04-30 02:08:42 +00:00
|
|
|
|
|
|
|
/**
|
2007-05-25 15:58:42 +00:00
|
|
|
* Open a TCP connection
|
2006-12-27 23:09:46 +00:00
|
|
|
*
|
2007-05-25 15:58:42 +00:00
|
|
|
* @v xfer Data transfer interface
|
|
|
|
* @v peer Peer socket address
|
|
|
|
* @v local Local socket address, or NULL
|
|
|
|
* @ret rc Return status code
|
2006-04-30 02:08:42 +00:00
|
|
|
*/
|
2007-05-25 15:58:42 +00:00
|
|
|
static int tcp_open ( struct xfer_interface *xfer, struct sockaddr *peer,
|
|
|
|
struct sockaddr *local ) {
|
|
|
|
struct sockaddr_tcpip *st_peer = ( struct sockaddr_tcpip * ) peer;
|
|
|
|
struct sockaddr_tcpip *st_local = ( struct sockaddr_tcpip * ) local;
|
|
|
|
struct tcp_connection *tcp;
|
|
|
|
unsigned int bind_port;
|
|
|
|
int rc;
|
2006-04-30 02:08:42 +00:00
|
|
|
|
2007-05-25 15:58:42 +00:00
|
|
|
/* Allocate and initialise structure */
|
|
|
|
tcp = malloc ( sizeof ( *tcp ) );
|
|
|
|
if ( ! tcp )
|
|
|
|
return -ENOMEM;
|
|
|
|
DBGC ( tcp, "TCP %p allocated\n", tcp );
|
|
|
|
memset ( tcp, 0, sizeof ( *tcp ) );
|
|
|
|
xfer_init ( &tcp->xfer, &tcp_xfer_operations, &tcp->refcnt );
|
2007-05-26 15:04:36 +00:00
|
|
|
tcp->prev_tcp_state = TCP_CLOSED;
|
|
|
|
tcp->tcp_state = TCP_STATE_SENT ( TCP_SYN );
|
|
|
|
tcp_dump_state ( tcp );
|
2007-05-25 15:58:42 +00:00
|
|
|
tcp->snd_seq = random();
|
|
|
|
INIT_LIST_HEAD ( &tcp->queue );
|
|
|
|
tcp->timer.expired = tcp_expired;
|
|
|
|
memcpy ( &tcp->peer, st_peer, sizeof ( tcp->peer ) );
|
|
|
|
|
|
|
|
/* Bind to local port */
|
|
|
|
bind_port = ( st_local ? st_local->st_port : 0 );
|
|
|
|
if ( ( rc = tcp_bind ( tcp, bind_port ) ) != 0 )
|
|
|
|
goto err;
|
|
|
|
|
2007-05-26 15:04:36 +00:00
|
|
|
/* Start timer to initiate SYN */
|
|
|
|
start_timer ( &tcp->timer );
|
|
|
|
|
2007-05-25 15:58:42 +00:00
|
|
|
/* Attach parent interface, transfer reference to connection
|
|
|
|
* list and return
|
|
|
|
*/
|
|
|
|
xfer_plug_plug ( &tcp->xfer, xfer );
|
|
|
|
list_add ( &tcp->list, &tcp_conns );
|
|
|
|
return 0;
|
2006-06-16 00:19:46 +00:00
|
|
|
|
2007-05-25 15:58:42 +00:00
|
|
|
err:
|
|
|
|
ref_put ( &tcp->refcnt );
|
|
|
|
return rc;
|
2006-05-27 19:01:20 +00:00
|
|
|
}
|
|
|
|
|
2007-01-14 16:47:03 +00:00
|
|
|
/**
|
2007-05-25 15:58:42 +00:00
|
|
|
* Close TCP connection
|
2007-01-14 16:47:03 +00:00
|
|
|
*
|
2007-01-31 02:09:13 +00:00
|
|
|
* @v tcp TCP connection
|
2007-05-25 15:58:42 +00:00
|
|
|
* @v rc Reason for close
|
|
|
|
*
|
|
|
|
* Closes the data transfer interface. If the TCP state machine is in
|
|
|
|
* a suitable state, the connection will be deleted.
|
2007-01-14 16:47:03 +00:00
|
|
|
*/
|
2007-05-25 15:58:42 +00:00
|
|
|
static void tcp_close ( struct tcp_connection *tcp, int rc ) {
|
|
|
|
struct io_buffer *iobuf;
|
|
|
|
struct io_buffer *tmp;
|
2007-01-14 16:47:03 +00:00
|
|
|
|
2007-05-25 15:58:42 +00:00
|
|
|
/* Close data transfer interface */
|
|
|
|
xfer_nullify ( &tcp->xfer );
|
|
|
|
xfer_close ( &tcp->xfer, rc );
|
|
|
|
tcp->xfer_closed = 1;
|
|
|
|
|
|
|
|
/* If we are in CLOSED, or have otherwise not yet received a
|
|
|
|
* SYN (i.e. we are in LISTEN or SYN_SENT), just delete the
|
|
|
|
* connection.
|
|
|
|
*/
|
|
|
|
if ( ! ( tcp->tcp_state & TCP_STATE_RCVD ( TCP_SYN ) ) ) {
|
|
|
|
|
|
|
|
/* Transition to CLOSED for the sake of debugging messages */
|
|
|
|
tcp->tcp_state = TCP_CLOSED;
|
|
|
|
tcp_dump_state ( tcp );
|
|
|
|
|
|
|
|
/* Free any unsent I/O buffers */
|
|
|
|
list_for_each_entry_safe ( iobuf, tmp, &tcp->queue, list ) {
|
|
|
|
list_del ( &iobuf->list );
|
|
|
|
free_iob ( iobuf );
|
|
|
|
}
|
2007-01-14 16:47:03 +00:00
|
|
|
|
2007-05-25 15:58:42 +00:00
|
|
|
/* Remove from list and drop reference */
|
|
|
|
stop_timer ( &tcp->timer );
|
|
|
|
list_del ( &tcp->list );
|
|
|
|
ref_put ( &tcp->refcnt );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we have not had our SYN acknowledged (i.e. we are in
|
|
|
|
* SYN_RCVD), pretend that it has been acknowledged so that we
|
|
|
|
* can send a FIN without breaking things.
|
|
|
|
*/
|
|
|
|
if ( ! ( tcp->tcp_state & TCP_STATE_ACKED ( TCP_SYN ) ) )
|
|
|
|
tcp_rx_ack ( tcp, ( tcp->snd_seq + 1 ), 0 );
|
|
|
|
|
|
|
|
/* If we have no data remaining to send, start sending FIN */
|
|
|
|
if ( list_empty ( &tcp->queue ) ) {
|
|
|
|
tcp->tcp_state |= TCP_STATE_SENT ( TCP_FIN );
|
|
|
|
tcp_dump_state ( tcp );
|
|
|
|
}
|
|
|
|
}
|
2007-01-14 16:47:03 +00:00
|
|
|
|
2007-05-25 15:58:42 +00:00
|
|
|
/***************************************************************************
|
|
|
|
*
|
|
|
|
* Transmit data path
|
|
|
|
*
|
|
|
|
***************************************************************************
|
|
|
|
*/
|
2007-01-14 16:47:03 +00:00
|
|
|
|
2007-05-25 15:58:42 +00:00
|
|
|
/**
|
|
|
|
* Process TCP transmit queue
|
|
|
|
*
|
|
|
|
* @v tcp TCP connection
|
|
|
|
* @v max_len Maximum length to process
|
|
|
|
* @v dest I/O buffer to fill with data, or NULL
|
|
|
|
* @v remove Remove data from queue
|
|
|
|
* @ret len Length of data processed
|
|
|
|
*
|
|
|
|
* This processes at most @c max_len bytes from the TCP connection's
|
|
|
|
* transmit queue. Data will be copied into the @c dest I/O buffer
|
|
|
|
* (if provided) and, if @c remove is true, removed from the transmit
|
|
|
|
* queue.
|
|
|
|
*/
|
|
|
|
static size_t tcp_process_queue ( struct tcp_connection *tcp, size_t max_len,
|
|
|
|
struct io_buffer *dest, int remove ) {
|
|
|
|
struct io_buffer *iobuf;
|
|
|
|
struct io_buffer *tmp;
|
|
|
|
size_t frag_len;
|
|
|
|
size_t len = 0;
|
|
|
|
|
|
|
|
list_for_each_entry_safe ( iobuf, tmp, &tcp->queue, list ) {
|
|
|
|
frag_len = iob_len ( iobuf );
|
|
|
|
if ( frag_len > max_len )
|
|
|
|
frag_len = max_len;
|
|
|
|
if ( dest ) {
|
|
|
|
memcpy ( iob_put ( dest, frag_len ), iobuf->data,
|
|
|
|
frag_len );
|
|
|
|
}
|
|
|
|
if ( remove ) {
|
|
|
|
iob_pull ( iobuf, frag_len );
|
|
|
|
if ( ! iob_len ( iobuf ) ) {
|
|
|
|
list_del ( &iobuf->list );
|
|
|
|
free_iob ( iobuf );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
len += frag_len;
|
|
|
|
max_len -= frag_len;
|
|
|
|
}
|
|
|
|
return len;
|
2007-01-14 16:47:03 +00:00
|
|
|
}
|
|
|
|
|
2006-04-30 02:08:42 +00:00
|
|
|
/**
|
2006-12-27 23:09:46 +00:00
|
|
|
* Transmit any outstanding data
|
2006-04-30 02:08:42 +00:00
|
|
|
*
|
2007-01-31 02:09:13 +00:00
|
|
|
* @v tcp TCP connection
|
2006-12-27 23:09:46 +00:00
|
|
|
* @v force_send Force sending of packet
|
|
|
|
*
|
2007-05-25 15:58:42 +00:00
|
|
|
* Transmits any outstanding data on the connection.
|
2006-04-30 02:08:42 +00:00
|
|
|
*
|
2006-12-27 23:09:46 +00:00
|
|
|
* Note that even if an error is returned, the retransmission timer
|
|
|
|
* will have been started if necessary, and so the stack will
|
|
|
|
* eventually attempt to retransmit the failed packet.
|
2006-04-30 02:08:42 +00:00
|
|
|
*/
|
2007-05-25 15:58:42 +00:00
|
|
|
static int tcp_xmit ( struct tcp_connection *tcp, int force_send ) {
|
2007-05-19 18:39:40 +00:00
|
|
|
struct io_buffer *iobuf;
|
2006-12-27 23:09:46 +00:00
|
|
|
struct tcp_header *tcphdr;
|
2007-01-13 17:36:17 +00:00
|
|
|
struct tcp_mss_option *mssopt;
|
|
|
|
void *payload;
|
2006-12-29 00:44:31 +00:00
|
|
|
unsigned int flags;
|
2007-05-25 15:58:42 +00:00
|
|
|
size_t len = 0;
|
2006-12-27 23:09:46 +00:00
|
|
|
size_t seq_len;
|
2007-01-18 20:39:17 +00:00
|
|
|
size_t window;
|
2007-01-14 16:47:03 +00:00
|
|
|
int rc;
|
2006-04-30 02:08:42 +00:00
|
|
|
|
2007-05-26 15:04:36 +00:00
|
|
|
/* If retransmission timer is already running, do nothing */
|
|
|
|
if ( timer_running ( &tcp->timer ) )
|
|
|
|
return 0;
|
|
|
|
|
2007-05-25 15:58:42 +00:00
|
|
|
/* Calculate both the actual (payload) and sequence space
|
|
|
|
* lengths that we wish to transmit.
|
2006-12-27 23:09:46 +00:00
|
|
|
*/
|
2007-01-31 02:09:13 +00:00
|
|
|
if ( TCP_CAN_SEND_DATA ( tcp->tcp_state ) ) {
|
2007-05-25 15:58:42 +00:00
|
|
|
len = tcp_process_queue ( tcp, tcp->snd_win, NULL, 0 );
|
2006-12-27 23:09:46 +00:00
|
|
|
}
|
|
|
|
seq_len = len;
|
2007-01-31 02:09:13 +00:00
|
|
|
flags = TCP_FLAGS_SENDING ( tcp->tcp_state );
|
2007-05-25 15:58:42 +00:00
|
|
|
if ( flags & ( TCP_SYN | TCP_FIN ) ) {
|
|
|
|
/* SYN or FIN consume one byte, and we can never send both */
|
|
|
|
assert ( ! ( ( flags & TCP_SYN ) && ( flags & TCP_FIN ) ) );
|
2006-12-27 23:09:46 +00:00
|
|
|
seq_len++;
|
2007-05-25 15:58:42 +00:00
|
|
|
}
|
2007-01-31 02:09:13 +00:00
|
|
|
tcp->snd_sent = seq_len;
|
2006-12-27 23:09:46 +00:00
|
|
|
|
2007-05-25 15:58:42 +00:00
|
|
|
/* If we have nothing to transmit, stop now */
|
|
|
|
if ( ( seq_len == 0 ) && ! force_send )
|
2006-12-27 23:09:46 +00:00
|
|
|
return 0;
|
2006-04-30 02:08:42 +00:00
|
|
|
|
2006-12-27 23:09:46 +00:00
|
|
|
/* If we are transmitting anything that requires
|
|
|
|
* acknowledgement (i.e. consumes sequence space), start the
|
2007-05-25 15:58:42 +00:00
|
|
|
* retransmission timer. Do this before attempting to
|
|
|
|
* allocate the I/O buffer, in case allocation itself fails.
|
2006-12-27 23:09:46 +00:00
|
|
|
*/
|
|
|
|
if ( seq_len )
|
2007-01-31 02:09:13 +00:00
|
|
|
start_timer ( &tcp->timer );
|
2006-08-01 20:46:50 +00:00
|
|
|
|
2007-05-25 15:58:42 +00:00
|
|
|
/* Allocate I/O buffer */
|
|
|
|
iobuf = alloc_iob ( len + MAX_HDR_LEN );
|
|
|
|
if ( ! iobuf ) {
|
|
|
|
DBGC ( tcp, "TCP %p could not allocate data buffer\n", tcp );
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
iob_reserve ( iobuf, MAX_HDR_LEN );
|
|
|
|
|
|
|
|
/* Fill data payload from transmit queue */
|
|
|
|
tcp_process_queue ( tcp, len, iobuf, 0 );
|
|
|
|
|
2007-01-18 20:39:17 +00:00
|
|
|
/* Estimate window size */
|
2007-02-01 09:38:16 +00:00
|
|
|
window = ( ( freemem * 3 ) / 4 );
|
2007-01-18 20:39:17 +00:00
|
|
|
if ( window > TCP_MAX_WINDOW_SIZE )
|
|
|
|
window = TCP_MAX_WINDOW_SIZE;
|
|
|
|
window &= ~0x03; /* Keep everything dword-aligned */
|
|
|
|
|
2006-12-27 23:09:46 +00:00
|
|
|
/* Fill up the TCP header */
|
2007-05-19 18:39:40 +00:00
|
|
|
payload = iobuf->data;
|
2007-01-13 17:36:17 +00:00
|
|
|
if ( flags & TCP_SYN ) {
|
2007-05-19 18:39:40 +00:00
|
|
|
mssopt = iob_push ( iobuf, sizeof ( *mssopt ) );
|
2007-01-13 17:36:17 +00:00
|
|
|
mssopt->kind = TCP_OPTION_MSS;
|
|
|
|
mssopt->length = sizeof ( *mssopt );
|
|
|
|
mssopt->mss = htons ( TCP_MSS );
|
|
|
|
}
|
2007-05-19 18:39:40 +00:00
|
|
|
tcphdr = iob_push ( iobuf, sizeof ( *tcphdr ) );
|
2006-12-27 23:09:46 +00:00
|
|
|
memset ( tcphdr, 0, sizeof ( *tcphdr ) );
|
2007-01-31 02:09:13 +00:00
|
|
|
tcphdr->src = tcp->local_port;
|
|
|
|
tcphdr->dest = tcp->peer.st_port;
|
|
|
|
tcphdr->seq = htonl ( tcp->snd_seq );
|
|
|
|
tcphdr->ack = htonl ( tcp->rcv_ack );
|
2007-05-19 18:39:40 +00:00
|
|
|
tcphdr->hlen = ( ( payload - iobuf->data ) << 2 );
|
2006-12-29 00:44:31 +00:00
|
|
|
tcphdr->flags = flags;
|
2007-01-18 20:39:17 +00:00
|
|
|
tcphdr->win = htons ( window );
|
2007-05-19 18:39:40 +00:00
|
|
|
tcphdr->csum = tcpip_chksum ( iobuf->data, iob_len ( iobuf ) );
|
2006-08-01 20:46:50 +00:00
|
|
|
|
2006-12-27 23:09:46 +00:00
|
|
|
/* Dump header */
|
2007-01-31 02:09:13 +00:00
|
|
|
DBGC ( tcp, "TCP %p TX %d->%d %08lx..%08lx %08lx %4zd",
|
|
|
|
tcp, ntohs ( tcphdr->src ), ntohs ( tcphdr->dest ),
|
2006-12-29 14:03:03 +00:00
|
|
|
ntohl ( tcphdr->seq ), ( ntohl ( tcphdr->seq ) + seq_len ),
|
|
|
|
ntohl ( tcphdr->ack ), len );
|
2007-01-31 02:09:13 +00:00
|
|
|
tcp_dump_flags ( tcp, tcphdr->flags );
|
|
|
|
DBGC ( tcp, "\n" );
|
2006-08-01 20:46:50 +00:00
|
|
|
|
2006-12-27 23:09:46 +00:00
|
|
|
/* Transmit packet */
|
2007-05-19 18:39:40 +00:00
|
|
|
rc = tcpip_tx ( iobuf, &tcp_protocol, &tcp->peer, NULL, &tcphdr->csum );
|
2007-01-14 16:47:03 +00:00
|
|
|
|
|
|
|
/* If we got -ENETUNREACH, kill the connection immediately
|
|
|
|
* because there is no point retrying. This isn't strictly
|
|
|
|
* necessary (since we will eventually time out anyway), but
|
|
|
|
* it avoids irritating needless delays. Don't do this for
|
|
|
|
* RST packets transmitted on connection abort, to avoid a
|
|
|
|
* potential infinite loop.
|
|
|
|
*/
|
2007-01-31 02:09:13 +00:00
|
|
|
if ( ( ! ( tcp->tcp_state & TCP_STATE_SENT ( TCP_RST ) ) ) &&
|
2007-01-14 16:47:03 +00:00
|
|
|
( rc == -ENETUNREACH ) ) {
|
2007-01-31 02:09:13 +00:00
|
|
|
DBGC ( tcp, "TCP %p aborting after TX failed: %s\n",
|
|
|
|
tcp, strerror ( rc ) );
|
2007-05-25 15:58:42 +00:00
|
|
|
tcp->tcp_state = TCP_CLOSED;
|
|
|
|
tcp_dump_state ( tcp );
|
|
|
|
tcp_close ( tcp, rc );
|
2007-01-14 16:47:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
2006-12-27 23:09:46 +00:00
|
|
|
}
|
2006-08-01 20:46:50 +00:00
|
|
|
|
2006-12-27 23:09:46 +00:00
|
|
|
/**
|
|
|
|
* Retransmission timer expired
|
2006-08-07 18:52:26 +00:00
|
|
|
*
|
|
|
|
* @v timer Retry timer
|
|
|
|
* @v over Failure indicator
|
|
|
|
*/
|
2006-12-27 23:09:46 +00:00
|
|
|
static void tcp_expired ( struct retry_timer *timer, int over ) {
|
2007-01-31 02:09:13 +00:00
|
|
|
struct tcp_connection *tcp =
|
2006-12-05 22:53:28 +00:00
|
|
|
container_of ( timer, struct tcp_connection, timer );
|
2007-01-31 02:09:13 +00:00
|
|
|
int graceful_close = TCP_CLOSED_GRACEFULLY ( tcp->tcp_state );
|
2006-12-27 23:09:46 +00:00
|
|
|
|
2007-01-31 02:09:13 +00:00
|
|
|
DBGC ( tcp, "TCP %p timer %s in %s\n", tcp,
|
|
|
|
( over ? "expired" : "fired" ), tcp_state ( tcp->tcp_state ) );
|
2006-12-27 23:09:46 +00:00
|
|
|
|
2007-01-31 02:09:13 +00:00
|
|
|
assert ( ( tcp->tcp_state == TCP_SYN_SENT ) ||
|
|
|
|
( tcp->tcp_state == TCP_SYN_RCVD ) ||
|
|
|
|
( tcp->tcp_state == TCP_ESTABLISHED ) ||
|
|
|
|
( tcp->tcp_state == TCP_FIN_WAIT_1 ) ||
|
|
|
|
( tcp->tcp_state == TCP_TIME_WAIT ) ||
|
|
|
|
( tcp->tcp_state == TCP_CLOSE_WAIT ) ||
|
|
|
|
( tcp->tcp_state == TCP_CLOSING_OR_LAST_ACK ) );
|
2006-12-27 23:09:46 +00:00
|
|
|
|
|
|
|
if ( over || graceful_close ) {
|
2007-01-14 16:47:03 +00:00
|
|
|
/* If we have finally timed out and given up, or if
|
|
|
|
* this is the result of a graceful close, terminate
|
|
|
|
* the connection
|
|
|
|
*/
|
2007-05-25 15:58:42 +00:00
|
|
|
tcp->tcp_state = TCP_CLOSED;
|
|
|
|
tcp_dump_state ( tcp );
|
|
|
|
tcp_close ( tcp, -ETIMEDOUT );
|
2006-12-27 23:09:46 +00:00
|
|
|
} else {
|
|
|
|
/* Otherwise, retransmit the packet */
|
2007-05-25 15:58:42 +00:00
|
|
|
tcp_xmit ( tcp, 0 );
|
2006-12-27 23:09:46 +00:00
|
|
|
}
|
2006-08-01 20:46:50 +00:00
|
|
|
}
|
|
|
|
|
2007-01-03 22:21:59 +00:00
|
|
|
/**
|
|
|
|
* Send RST response to incoming packet
|
|
|
|
*
|
|
|
|
* @v in_tcphdr TCP header of incoming packet
|
|
|
|
* @ret rc Return status code
|
|
|
|
*/
|
2007-05-25 15:58:42 +00:00
|
|
|
static int tcp_xmit_reset ( struct tcp_connection *tcp,
|
|
|
|
struct sockaddr_tcpip *st_dest,
|
2007-01-03 22:21:59 +00:00
|
|
|
struct tcp_header *in_tcphdr ) {
|
2007-05-19 18:39:40 +00:00
|
|
|
struct io_buffer *iobuf;
|
2007-01-03 22:21:59 +00:00
|
|
|
struct tcp_header *tcphdr;
|
|
|
|
|
|
|
|
/* Allocate space for dataless TX buffer */
|
2007-05-19 18:39:40 +00:00
|
|
|
iobuf = alloc_iob ( MAX_HDR_LEN );
|
|
|
|
if ( ! iobuf ) {
|
2007-01-31 02:09:13 +00:00
|
|
|
DBGC ( tcp, "TCP %p could not allocate data buffer\n", tcp );
|
2007-01-03 22:21:59 +00:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
2007-05-19 18:39:40 +00:00
|
|
|
iob_reserve ( iobuf, MAX_HDR_LEN );
|
2007-01-03 22:21:59 +00:00
|
|
|
|
|
|
|
/* Construct RST response */
|
2007-05-19 18:39:40 +00:00
|
|
|
tcphdr = iob_push ( iobuf, sizeof ( *tcphdr ) );
|
2007-01-03 22:21:59 +00:00
|
|
|
memset ( tcphdr, 0, sizeof ( *tcphdr ) );
|
|
|
|
tcphdr->src = in_tcphdr->dest;
|
|
|
|
tcphdr->dest = in_tcphdr->src;
|
|
|
|
tcphdr->seq = in_tcphdr->ack;
|
|
|
|
tcphdr->ack = in_tcphdr->seq;
|
|
|
|
tcphdr->hlen = ( ( sizeof ( *tcphdr ) / 4 ) << 4 );
|
|
|
|
tcphdr->flags = ( TCP_RST | TCP_ACK );
|
2007-01-18 20:39:17 +00:00
|
|
|
tcphdr->win = htons ( TCP_MAX_WINDOW_SIZE );
|
2007-05-19 18:39:40 +00:00
|
|
|
tcphdr->csum = tcpip_chksum ( iobuf->data, iob_len ( iobuf ) );
|
2007-01-03 22:21:59 +00:00
|
|
|
|
|
|
|
/* Dump header */
|
2007-01-31 02:09:13 +00:00
|
|
|
DBGC ( tcp, "TCP %p TX %d->%d %08lx..%08lx %08lx %4zd",
|
|
|
|
tcp, ntohs ( tcphdr->src ), ntohs ( tcphdr->dest ),
|
2007-01-03 22:21:59 +00:00
|
|
|
ntohl ( tcphdr->seq ), ( ntohl ( tcphdr->seq ) ),
|
|
|
|
ntohl ( tcphdr->ack ), 0 );
|
2007-01-31 02:09:13 +00:00
|
|
|
tcp_dump_flags ( tcp, tcphdr->flags );
|
|
|
|
DBGC ( tcp, "\n" );
|
2007-01-03 22:21:59 +00:00
|
|
|
|
|
|
|
/* Transmit packet */
|
2007-05-25 15:58:42 +00:00
|
|
|
return tcpip_tx ( iobuf, &tcp_protocol, st_dest,
|
2007-01-10 02:25:11 +00:00
|
|
|
NULL, &tcphdr->csum );
|
2007-01-03 22:21:59 +00:00
|
|
|
}
|
|
|
|
|
2007-05-25 15:58:42 +00:00
|
|
|
/***************************************************************************
|
|
|
|
*
|
|
|
|
* Receive data path
|
|
|
|
*
|
|
|
|
***************************************************************************
|
|
|
|
*/
|
|
|
|
|
2006-08-01 20:46:50 +00:00
|
|
|
/**
|
2006-12-27 23:09:46 +00:00
|
|
|
* Identify TCP connection by local port number
|
2006-08-01 20:46:50 +00:00
|
|
|
*
|
2006-12-27 23:09:46 +00:00
|
|
|
* @v local_port Local port (in network-endian order)
|
2007-01-31 02:09:13 +00:00
|
|
|
* @ret tcp TCP connection, or NULL
|
2006-08-01 20:46:50 +00:00
|
|
|
*/
|
2007-05-25 15:58:42 +00:00
|
|
|
static struct tcp_connection * tcp_demux ( unsigned int local_port ) {
|
2007-01-31 02:09:13 +00:00
|
|
|
struct tcp_connection *tcp;
|
2006-08-01 20:46:50 +00:00
|
|
|
|
2007-01-31 02:09:13 +00:00
|
|
|
list_for_each_entry ( tcp, &tcp_conns, list ) {
|
|
|
|
if ( tcp->local_port == local_port )
|
|
|
|
return tcp;
|
2006-12-27 23:09:46 +00:00
|
|
|
}
|
|
|
|
return NULL;
|
2006-08-01 20:46:50 +00:00
|
|
|
}
|
|
|
|
|
2006-08-09 12:08:20 +00:00
|
|
|
/**
|
2006-12-27 23:09:46 +00:00
|
|
|
* Handle TCP received SYN
|
2006-08-09 12:08:20 +00:00
|
|
|
*
|
2007-01-31 02:09:13 +00:00
|
|
|
* @v tcp TCP connection
|
2006-12-27 23:09:46 +00:00
|
|
|
* @v seq SEQ value (in host-endian order)
|
2006-08-09 12:08:20 +00:00
|
|
|
* @ret rc Return status code
|
|
|
|
*/
|
2007-01-31 02:09:13 +00:00
|
|
|
static int tcp_rx_syn ( struct tcp_connection *tcp, uint32_t seq ) {
|
2006-12-27 23:09:46 +00:00
|
|
|
|
|
|
|
/* Synchronise sequence numbers on first SYN */
|
2007-01-31 02:09:13 +00:00
|
|
|
if ( ! ( tcp->tcp_state & TCP_STATE_RCVD ( TCP_SYN ) ) )
|
|
|
|
tcp->rcv_ack = seq;
|
2006-12-27 23:09:46 +00:00
|
|
|
|
|
|
|
/* Ignore duplicate SYN */
|
2007-01-31 02:09:13 +00:00
|
|
|
if ( ( tcp->rcv_ack - seq ) > 0 )
|
2006-12-27 23:09:46 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Mark SYN as received and start sending ACKs with each packet */
|
2007-01-31 02:09:13 +00:00
|
|
|
tcp->tcp_state |= ( TCP_STATE_SENT ( TCP_ACK ) |
|
2007-05-25 15:58:42 +00:00
|
|
|
TCP_STATE_RCVD ( TCP_SYN ) );
|
2006-12-27 23:09:46 +00:00
|
|
|
|
|
|
|
/* Acknowledge SYN */
|
2007-01-31 02:09:13 +00:00
|
|
|
tcp->rcv_ack++;
|
2006-08-09 12:08:20 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-08-01 20:46:50 +00:00
|
|
|
/**
|
2006-12-27 23:09:46 +00:00
|
|
|
* Handle TCP received ACK
|
2006-08-01 20:46:50 +00:00
|
|
|
*
|
2007-01-31 02:09:13 +00:00
|
|
|
* @v tcp TCP connection
|
2006-12-27 23:09:46 +00:00
|
|
|
* @v ack ACK value (in host-endian order)
|
|
|
|
* @v win WIN value (in host-endian order)
|
|
|
|
* @ret rc Return status code
|
2006-08-01 20:46:50 +00:00
|
|
|
*/
|
2007-01-31 02:09:13 +00:00
|
|
|
static int tcp_rx_ack ( struct tcp_connection *tcp, uint32_t ack,
|
2006-12-27 23:09:46 +00:00
|
|
|
uint32_t win ) {
|
2007-01-31 02:09:13 +00:00
|
|
|
size_t ack_len = ( ack - tcp->snd_seq );
|
2006-12-27 23:09:46 +00:00
|
|
|
size_t len;
|
2007-05-25 15:58:42 +00:00
|
|
|
unsigned int acked_flags;
|
2006-12-27 23:09:46 +00:00
|
|
|
|
|
|
|
/* Ignore duplicate or out-of-range ACK */
|
2007-01-31 02:09:13 +00:00
|
|
|
if ( ack_len > tcp->snd_sent ) {
|
|
|
|
DBGC ( tcp, "TCP %p received ACK for [%08lx,%08lx), "
|
|
|
|
"sent only [%08lx,%08lx)\n", tcp, tcp->snd_seq,
|
|
|
|
( tcp->snd_seq + ack_len ), tcp->snd_seq,
|
|
|
|
( tcp->snd_seq + tcp->snd_sent ) );
|
2006-12-27 23:09:46 +00:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
2006-08-09 12:08:20 +00:00
|
|
|
|
2007-05-25 15:58:42 +00:00
|
|
|
/* Acknowledge any flags being sent */
|
2006-12-27 23:09:46 +00:00
|
|
|
len = ack_len;
|
2007-05-25 15:58:42 +00:00
|
|
|
acked_flags = ( TCP_FLAGS_SENDING ( tcp->tcp_state ) &
|
|
|
|
( TCP_SYN | TCP_FIN ) );
|
|
|
|
if ( acked_flags )
|
|
|
|
len--;
|
2006-08-09 12:08:20 +00:00
|
|
|
|
2006-12-27 23:09:46 +00:00
|
|
|
/* Update SEQ and sent counters, and window size */
|
2007-01-31 02:09:13 +00:00
|
|
|
tcp->snd_seq = ack;
|
|
|
|
tcp->snd_sent = 0;
|
|
|
|
tcp->snd_win = win;
|
2006-08-09 12:08:20 +00:00
|
|
|
|
2006-12-27 23:09:46 +00:00
|
|
|
/* Stop the retransmission timer */
|
2007-01-31 02:09:13 +00:00
|
|
|
stop_timer ( &tcp->timer );
|
2006-12-27 23:09:46 +00:00
|
|
|
|
2007-05-25 15:58:42 +00:00
|
|
|
/* Remove any acknowledged data from transmit queue */
|
|
|
|
tcp_process_queue ( tcp, len, NULL, 1 );
|
|
|
|
|
2006-12-27 23:09:46 +00:00
|
|
|
/* Mark SYN/FIN as acknowledged if applicable. */
|
2006-12-29 00:44:31 +00:00
|
|
|
if ( acked_flags )
|
2007-01-31 02:09:13 +00:00
|
|
|
tcp->tcp_state |= TCP_STATE_ACKED ( acked_flags );
|
2006-12-27 23:09:46 +00:00
|
|
|
|
2007-05-25 15:58:42 +00:00
|
|
|
/* Start sending FIN if we've had all possible data ACKed */
|
|
|
|
if ( list_empty ( &tcp->queue ) && tcp->xfer_closed )
|
|
|
|
tcp->tcp_state |= TCP_STATE_SENT ( TCP_FIN );
|
2006-08-09 12:08:20 +00:00
|
|
|
|
|
|
|
return 0;
|
2006-08-01 20:46:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2006-12-27 23:09:46 +00:00
|
|
|
* Handle TCP received data
|
2006-08-01 20:46:50 +00:00
|
|
|
*
|
2007-01-31 02:09:13 +00:00
|
|
|
* @v tcp TCP connection
|
2006-12-27 23:09:46 +00:00
|
|
|
* @v seq SEQ value (in host-endian order)
|
2007-05-25 15:58:42 +00:00
|
|
|
* @v iobuf I/O buffer
|
2006-12-27 23:09:46 +00:00
|
|
|
* @ret rc Return status code
|
2007-05-25 15:58:42 +00:00
|
|
|
*
|
|
|
|
* This function takes ownership of the I/O buffer.
|
2006-08-01 20:46:50 +00:00
|
|
|
*/
|
2007-01-31 02:09:13 +00:00
|
|
|
static int tcp_rx_data ( struct tcp_connection *tcp, uint32_t seq,
|
2007-05-25 15:58:42 +00:00
|
|
|
struct io_buffer *iobuf ) {
|
2006-12-27 23:09:46 +00:00
|
|
|
size_t already_rcvd;
|
2007-05-25 15:58:42 +00:00
|
|
|
size_t len;
|
|
|
|
int rc;
|
2006-12-27 23:09:46 +00:00
|
|
|
|
|
|
|
/* Ignore duplicate data */
|
2007-01-31 02:09:13 +00:00
|
|
|
already_rcvd = ( tcp->rcv_ack - seq );
|
2007-05-25 15:58:42 +00:00
|
|
|
len = iob_len ( iobuf );
|
|
|
|
if ( already_rcvd >= len ) {
|
|
|
|
free_iob ( iobuf );
|
2006-12-27 23:09:46 +00:00
|
|
|
return 0;
|
2007-05-25 15:58:42 +00:00
|
|
|
}
|
|
|
|
iob_pull ( iobuf, already_rcvd );
|
|
|
|
|
|
|
|
/* Deliver data to application */
|
|
|
|
if ( ( rc = xfer_deliver_iob ( &tcp->xfer, iobuf ) ) != 0 )
|
|
|
|
return rc;
|
2006-12-27 23:09:46 +00:00
|
|
|
|
|
|
|
/* Acknowledge new data */
|
2007-01-31 02:09:13 +00:00
|
|
|
tcp->rcv_ack += len;
|
2006-08-01 20:46:50 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-01-03 21:51:36 +00:00
|
|
|
/**
|
|
|
|
* Handle TCP received FIN
|
2006-08-01 20:46:50 +00:00
|
|
|
*
|
2007-01-31 02:09:13 +00:00
|
|
|
* @v tcp TCP connection
|
2006-12-27 23:09:46 +00:00
|
|
|
* @v seq SEQ value (in host-endian order)
|
|
|
|
* @ret rc Return status code
|
2006-08-01 20:46:50 +00:00
|
|
|
*/
|
2007-01-31 02:09:13 +00:00
|
|
|
static int tcp_rx_fin ( struct tcp_connection *tcp, uint32_t seq ) {
|
2006-08-01 20:46:50 +00:00
|
|
|
|
2006-12-27 23:09:46 +00:00
|
|
|
/* Ignore duplicate FIN */
|
2007-01-31 02:09:13 +00:00
|
|
|
if ( ( tcp->rcv_ack - seq ) > 0 )
|
2006-12-27 23:09:46 +00:00
|
|
|
return 0;
|
2006-08-09 14:27:07 +00:00
|
|
|
|
2007-05-25 15:58:42 +00:00
|
|
|
/* Mark FIN as received and acknowledge it */
|
|
|
|
tcp->tcp_state |= TCP_STATE_RCVD ( TCP_FIN );
|
2007-01-31 02:09:13 +00:00
|
|
|
tcp->rcv_ack++;
|
2006-08-07 18:52:26 +00:00
|
|
|
|
2007-05-25 15:58:42 +00:00
|
|
|
/* Close connection */
|
|
|
|
tcp_close ( tcp, 0 );
|
2006-12-27 23:09:46 +00:00
|
|
|
|
|
|
|
return 0;
|
2006-08-01 20:46:50 +00:00
|
|
|
}
|
|
|
|
|
2007-01-03 21:51:36 +00:00
|
|
|
/**
|
|
|
|
* Handle TCP received RST
|
|
|
|
*
|
2007-01-31 02:09:13 +00:00
|
|
|
* @v tcp TCP connection
|
2007-01-03 21:51:36 +00:00
|
|
|
* @v seq SEQ value (in host-endian order)
|
|
|
|
* @ret rc Return status code
|
|
|
|
*/
|
2007-01-31 02:09:13 +00:00
|
|
|
static int tcp_rx_rst ( struct tcp_connection *tcp, uint32_t seq ) {
|
2007-01-03 21:51:36 +00:00
|
|
|
|
|
|
|
/* Accept RST only if it falls within the window. If we have
|
|
|
|
* not yet received a SYN, then we have no window to test
|
|
|
|
* against, so fall back to checking that our SYN has been
|
|
|
|
* ACKed.
|
|
|
|
*/
|
2007-01-31 02:09:13 +00:00
|
|
|
if ( tcp->tcp_state & TCP_STATE_RCVD ( TCP_SYN ) ) {
|
|
|
|
if ( ( tcp->rcv_ack - seq ) > 0 )
|
2007-01-03 21:51:36 +00:00
|
|
|
return 0;
|
|
|
|
} else {
|
2007-01-31 02:09:13 +00:00
|
|
|
if ( ! ( tcp->tcp_state & TCP_STATE_ACKED ( TCP_SYN ) ) )
|
2007-01-03 21:51:36 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-05-25 15:58:42 +00:00
|
|
|
/* Abort connection */
|
|
|
|
tcp->tcp_state = TCP_CLOSED;
|
|
|
|
tcp_dump_state ( tcp );
|
|
|
|
tcp_close ( tcp, -ECONNRESET );
|
2007-01-03 21:51:36 +00:00
|
|
|
|
|
|
|
return -ECONNRESET;
|
|
|
|
}
|
|
|
|
|
2006-08-01 20:46:50 +00:00
|
|
|
/**
|
|
|
|
* Process received packet
|
|
|
|
*
|
2007-05-19 18:39:40 +00:00
|
|
|
* @v iobuf I/O buffer
|
2007-01-03 20:48:52 +00:00
|
|
|
* @v st_src Partially-filled source address
|
|
|
|
* @v st_dest Partially-filled destination address
|
|
|
|
* @v pshdr_csum Pseudo-header checksum
|
|
|
|
* @ret rc Return status code
|
|
|
|
*/
|
2007-05-19 18:39:40 +00:00
|
|
|
static int tcp_rx ( struct io_buffer *iobuf,
|
2007-05-25 15:58:42 +00:00
|
|
|
struct sockaddr_tcpip *st_src,
|
2007-01-03 20:48:52 +00:00
|
|
|
struct sockaddr_tcpip *st_dest __unused,
|
|
|
|
uint16_t pshdr_csum ) {
|
2007-05-19 18:39:40 +00:00
|
|
|
struct tcp_header *tcphdr = iobuf->data;
|
2007-01-31 02:09:13 +00:00
|
|
|
struct tcp_connection *tcp;
|
2006-12-05 22:53:28 +00:00
|
|
|
unsigned int hlen;
|
2007-01-03 20:48:52 +00:00
|
|
|
uint16_t csum;
|
2006-12-27 23:09:46 +00:00
|
|
|
uint32_t start_seq;
|
|
|
|
uint32_t seq;
|
|
|
|
uint32_t ack;
|
|
|
|
uint32_t win;
|
|
|
|
unsigned int flags;
|
|
|
|
size_t len;
|
2007-01-03 21:13:11 +00:00
|
|
|
int rc;
|
2006-12-27 23:09:46 +00:00
|
|
|
|
2007-01-03 20:48:52 +00:00
|
|
|
/* Sanity check packet */
|
2007-05-19 18:39:40 +00:00
|
|
|
if ( iob_len ( iobuf ) < sizeof ( *tcphdr ) ) {
|
2006-12-27 23:09:46 +00:00
|
|
|
DBG ( "TCP packet too short at %d bytes (min %d bytes)\n",
|
2007-05-19 18:39:40 +00:00
|
|
|
iob_len ( iobuf ), sizeof ( *tcphdr ) );
|
2006-08-09 14:27:07 +00:00
|
|
|
rc = -EINVAL;
|
2007-05-25 15:58:42 +00:00
|
|
|
goto discard;
|
2006-08-01 20:46:50 +00:00
|
|
|
}
|
|
|
|
hlen = ( ( tcphdr->hlen & TCP_MASK_HLEN ) / 16 ) * 4;
|
2006-08-19 16:14:53 +00:00
|
|
|
if ( hlen < sizeof ( *tcphdr ) ) {
|
2006-12-27 23:09:46 +00:00
|
|
|
DBG ( "TCP header too short at %d bytes (min %d bytes)\n",
|
|
|
|
hlen, sizeof ( *tcphdr ) );
|
2006-08-19 16:14:53 +00:00
|
|
|
rc = -EINVAL;
|
2007-05-25 15:58:42 +00:00
|
|
|
goto discard;
|
2006-08-19 16:14:53 +00:00
|
|
|
}
|
2007-05-19 18:39:40 +00:00
|
|
|
if ( hlen > iob_len ( iobuf ) ) {
|
2006-12-27 23:09:46 +00:00
|
|
|
DBG ( "TCP header too long at %d bytes (max %d bytes)\n",
|
2007-05-19 18:39:40 +00:00
|
|
|
hlen, iob_len ( iobuf ) );
|
2006-12-27 23:09:46 +00:00
|
|
|
rc = -EINVAL;
|
2007-05-25 15:58:42 +00:00
|
|
|
goto discard;
|
2006-08-01 20:46:50 +00:00
|
|
|
}
|
2007-05-19 18:39:40 +00:00
|
|
|
csum = tcpip_continue_chksum ( pshdr_csum, iobuf->data, iob_len ( iobuf ));
|
2007-01-03 20:48:52 +00:00
|
|
|
if ( csum != 0 ) {
|
|
|
|
DBG ( "TCP checksum incorrect (is %04x including checksum "
|
|
|
|
"field, should be 0000)\n", csum );
|
2007-01-03 21:13:11 +00:00
|
|
|
rc = -EINVAL;
|
2007-05-25 15:58:42 +00:00
|
|
|
goto discard;
|
2007-01-03 20:48:52 +00:00
|
|
|
}
|
2006-08-01 20:46:50 +00:00
|
|
|
|
2006-12-27 23:09:46 +00:00
|
|
|
/* Parse parameters from header and strip header */
|
2007-01-31 02:09:13 +00:00
|
|
|
tcp = tcp_demux ( tcphdr->dest );
|
2006-12-27 23:09:46 +00:00
|
|
|
start_seq = seq = ntohl ( tcphdr->seq );
|
|
|
|
ack = ntohl ( tcphdr->ack );
|
|
|
|
win = ntohs ( tcphdr->win );
|
|
|
|
flags = tcphdr->flags;
|
2007-05-25 15:58:42 +00:00
|
|
|
iob_pull ( iobuf, hlen );
|
2007-05-19 18:39:40 +00:00
|
|
|
len = iob_len ( iobuf );
|
2006-12-27 23:09:46 +00:00
|
|
|
|
|
|
|
/* Dump header */
|
2007-01-31 02:09:13 +00:00
|
|
|
DBGC ( tcp, "TCP %p RX %d<-%d %08lx %08lx..%08lx %4zd",
|
|
|
|
tcp, ntohs ( tcphdr->dest ), ntohs ( tcphdr->src ),
|
2006-12-29 14:03:03 +00:00
|
|
|
ntohl ( tcphdr->ack ), ntohl ( tcphdr->seq ),
|
|
|
|
( ntohl ( tcphdr->seq ) + len +
|
|
|
|
( ( tcphdr->flags & ( TCP_SYN | TCP_FIN ) ) ? 1 : 0 ) ), len);
|
2007-01-31 02:09:13 +00:00
|
|
|
tcp_dump_flags ( tcp, tcphdr->flags );
|
|
|
|
DBGC ( tcp, "\n" );
|
2006-12-27 23:09:46 +00:00
|
|
|
|
2007-01-03 22:21:59 +00:00
|
|
|
/* If no connection was found, send RST */
|
2007-01-31 02:09:13 +00:00
|
|
|
if ( ! tcp ) {
|
2007-05-25 15:58:42 +00:00
|
|
|
tcp_xmit_reset ( tcp, st_src, tcphdr );
|
2007-01-03 21:13:11 +00:00
|
|
|
rc = -ENOTCONN;
|
2007-05-25 15:58:42 +00:00
|
|
|
goto discard;
|
2007-01-03 21:13:11 +00:00
|
|
|
}
|
2006-12-27 23:09:46 +00:00
|
|
|
|
|
|
|
/* Handle ACK, if present */
|
2007-01-03 22:21:59 +00:00
|
|
|
if ( flags & TCP_ACK ) {
|
2007-01-31 02:09:13 +00:00
|
|
|
if ( ( rc = tcp_rx_ack ( tcp, ack, win ) ) != 0 ) {
|
2007-05-25 15:58:42 +00:00
|
|
|
tcp_xmit_reset ( tcp, st_src, tcphdr );
|
|
|
|
goto discard;
|
2007-01-03 22:21:59 +00:00
|
|
|
}
|
|
|
|
}
|
2006-12-27 23:09:46 +00:00
|
|
|
|
|
|
|
/* Handle SYN, if present */
|
|
|
|
if ( flags & TCP_SYN ) {
|
2007-01-31 02:09:13 +00:00
|
|
|
tcp_rx_syn ( tcp, seq );
|
2006-12-27 23:09:46 +00:00
|
|
|
seq++;
|
2006-08-01 20:46:50 +00:00
|
|
|
}
|
|
|
|
|
2007-01-03 21:51:36 +00:00
|
|
|
/* Handle RST, if present */
|
|
|
|
if ( flags & TCP_RST ) {
|
2007-01-31 02:09:13 +00:00
|
|
|
if ( ( rc = tcp_rx_rst ( tcp, seq ) ) != 0 )
|
2007-05-25 15:58:42 +00:00
|
|
|
goto discard;
|
2007-01-03 21:51:36 +00:00
|
|
|
}
|
|
|
|
|
2006-12-27 23:09:46 +00:00
|
|
|
/* Handle new data, if any */
|
2007-05-25 15:58:42 +00:00
|
|
|
tcp_rx_data ( tcp, seq, iobuf );
|
2006-12-27 23:09:46 +00:00
|
|
|
seq += len;
|
2006-08-07 18:52:26 +00:00
|
|
|
|
2006-12-27 23:09:46 +00:00
|
|
|
/* Handle FIN, if present */
|
|
|
|
if ( flags & TCP_FIN ) {
|
2007-01-31 02:09:13 +00:00
|
|
|
tcp_rx_fin ( tcp, seq );
|
2006-12-27 23:09:46 +00:00
|
|
|
seq++;
|
2006-08-01 20:46:50 +00:00
|
|
|
}
|
|
|
|
|
2007-01-03 21:51:36 +00:00
|
|
|
/* Dump out any state change as a result of the received packet */
|
2007-01-31 02:09:13 +00:00
|
|
|
tcp_dump_state ( tcp );
|
2006-12-27 23:09:46 +00:00
|
|
|
|
|
|
|
/* Send out any pending data. If peer is expecting an ACK for
|
|
|
|
* this packet then force sending a reply.
|
2006-08-01 20:46:50 +00:00
|
|
|
*/
|
2007-05-25 15:58:42 +00:00
|
|
|
tcp_xmit ( tcp, ( start_seq != seq ) );
|
2006-08-01 20:46:50 +00:00
|
|
|
|
2006-12-27 23:09:46 +00:00
|
|
|
/* If this packet was the last we expect to receive, set up
|
|
|
|
* timer to expire and cause the connection to be freed.
|
2006-08-19 16:14:53 +00:00
|
|
|
*/
|
2007-01-31 02:09:13 +00:00
|
|
|
if ( TCP_CLOSED_GRACEFULLY ( tcp->tcp_state ) ) {
|
|
|
|
tcp->timer.timeout = ( 2 * TCP_MSL );
|
|
|
|
start_timer ( &tcp->timer );
|
2006-12-27 23:09:46 +00:00
|
|
|
}
|
|
|
|
|
2007-05-25 15:58:42 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
discard:
|
2006-12-27 23:09:46 +00:00
|
|
|
/* Free received packet */
|
2007-05-19 18:39:40 +00:00
|
|
|
free_iob ( iobuf );
|
2006-12-27 23:09:46 +00:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2007-05-25 15:58:42 +00:00
|
|
|
/** TCP protocol */
|
|
|
|
struct tcpip_protocol tcp_protocol __tcpip_protocol = {
|
|
|
|
.name = "TCP",
|
|
|
|
.rx = tcp_rx,
|
|
|
|
.tcpip_proto = IP_TCP,
|
|
|
|
};
|
|
|
|
|
|
|
|
/***************************************************************************
|
2006-12-27 23:09:46 +00:00
|
|
|
*
|
2007-05-25 15:58:42 +00:00
|
|
|
* Data transfer interface
|
2006-12-27 23:09:46 +00:00
|
|
|
*
|
2007-05-25 15:58:42 +00:00
|
|
|
***************************************************************************
|
2006-12-27 23:09:46 +00:00
|
|
|
*/
|
|
|
|
|
2007-05-25 15:58:42 +00:00
|
|
|
/**
|
|
|
|
* Close interface
|
|
|
|
*
|
|
|
|
* @v xfer Data transfer interface
|
|
|
|
* @v rc Reason for close
|
|
|
|
*/
|
|
|
|
static void tcp_xfer_close ( struct xfer_interface *xfer, int rc ) {
|
|
|
|
struct tcp_connection *tcp =
|
|
|
|
container_of ( xfer, struct tcp_connection, xfer );
|
2006-12-27 23:09:46 +00:00
|
|
|
|
2007-05-25 15:58:42 +00:00
|
|
|
/* Close data transfer interface */
|
|
|
|
tcp_close ( tcp, rc );
|
|
|
|
|
|
|
|
/* Transmit FIN, if possible */
|
|
|
|
tcp_xmit ( tcp, 0 );
|
2006-12-27 23:09:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2007-05-25 15:58:42 +00:00
|
|
|
* Seek to position
|
2006-12-27 23:09:46 +00:00
|
|
|
*
|
2007-05-25 15:58:42 +00:00
|
|
|
* @v xfer Data transfer interface
|
|
|
|
* @v offset Offset to new position
|
|
|
|
* @v whence Basis for new position
|
|
|
|
* @ret rc Return status code
|
2006-12-27 23:09:46 +00:00
|
|
|
*/
|
2007-05-25 15:58:42 +00:00
|
|
|
static int tcp_xfer_seek ( struct xfer_interface *xfer, off_t offset,
|
|
|
|
int whence ) {
|
2007-01-31 02:09:13 +00:00
|
|
|
struct tcp_connection *tcp =
|
2007-05-25 15:58:42 +00:00
|
|
|
container_of ( xfer, struct tcp_connection, xfer );
|
2006-12-27 23:09:46 +00:00
|
|
|
|
2007-05-25 15:58:42 +00:00
|
|
|
/* TCP doesn't support seeking to arbitrary positions */
|
|
|
|
if ( ( whence != SEEK_CUR ) || ( offset != 0 ) )
|
|
|
|
return -EINVAL;
|
2006-12-27 23:09:46 +00:00
|
|
|
|
2007-05-25 15:58:42 +00:00
|
|
|
/* Not ready if we're not in a suitable connection state */
|
|
|
|
if ( ! TCP_CAN_SEND_DATA ( tcp->tcp_state ) )
|
|
|
|
return -EAGAIN;
|
|
|
|
|
|
|
|
/* Not ready if data queue is non-empty */
|
|
|
|
if ( ! list_empty ( &tcp->queue ) )
|
|
|
|
return -EAGAIN;
|
|
|
|
|
|
|
|
return 0;
|
2006-08-01 20:46:50 +00:00
|
|
|
}
|
|
|
|
|
2007-01-31 02:09:13 +00:00
|
|
|
/**
|
2007-05-25 15:58:42 +00:00
|
|
|
* Deliver datagram as I/O buffer
|
2007-01-31 02:09:13 +00:00
|
|
|
*
|
2007-05-25 15:58:42 +00:00
|
|
|
* @v xfer Data transfer interface
|
|
|
|
* @v iobuf Datagram I/O buffer
|
2007-01-31 02:09:13 +00:00
|
|
|
* @ret rc Return status code
|
|
|
|
*/
|
2007-05-25 15:58:42 +00:00
|
|
|
static int tcp_xfer_deliver_iob ( struct xfer_interface *xfer,
|
|
|
|
struct io_buffer *iobuf ) {
|
|
|
|
struct tcp_connection *tcp =
|
|
|
|
container_of ( xfer, struct tcp_connection, xfer );
|
2007-01-31 02:09:13 +00:00
|
|
|
|
2007-05-25 15:58:42 +00:00
|
|
|
/* Enqueue packet */
|
|
|
|
list_add_tail ( &iobuf->list, &tcp->queue );
|
2007-01-31 02:09:13 +00:00
|
|
|
|
2007-05-25 15:58:42 +00:00
|
|
|
/* Transmit data, if possible */
|
|
|
|
tcp_xmit ( tcp, 0 );
|
2007-01-31 02:09:13 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-05-25 15:58:42 +00:00
|
|
|
/** TCP data transfer interface operations */
|
|
|
|
static struct xfer_interface_operations tcp_xfer_operations = {
|
|
|
|
.close = tcp_xfer_close,
|
|
|
|
.vredirect = ignore_xfer_vredirect,
|
2007-05-26 15:04:36 +00:00
|
|
|
.request = ignore_xfer_request,
|
2007-05-25 15:58:42 +00:00
|
|
|
.seek = tcp_xfer_seek,
|
|
|
|
.alloc_iob = default_xfer_alloc_iob,
|
|
|
|
.deliver_iob = tcp_xfer_deliver_iob,
|
|
|
|
.deliver_raw = xfer_deliver_as_iob,
|
2007-01-31 02:09:13 +00:00
|
|
|
};
|
|
|
|
|
2007-05-25 15:58:42 +00:00
|
|
|
/***************************************************************************
|
|
|
|
*
|
|
|
|
* Openers
|
|
|
|
*
|
|
|
|
***************************************************************************
|
|
|
|
*/
|
|
|
|
|
|
|
|
#warning "Placeholder URI opener"
|
|
|
|
static int tcp_open_uri ( struct xfer_interface *xfer, struct uri *uri ) {
|
|
|
|
struct sockaddr_in peer;
|
|
|
|
|
|
|
|
memset ( &peer, 0, sizeof ( peer ) );
|
|
|
|
peer.sin_family = AF_INET;
|
|
|
|
peer.sin_addr.s_addr = htonl ( 0x0afefe02 );
|
|
|
|
peer.sin_port = htons ( 12345 );
|
|
|
|
return tcp_open ( xfer, &peer, NULL );
|
|
|
|
}
|
|
|
|
|
|
|
|
/** TCP URI opener */
|
|
|
|
struct uri_opener tcp_uri_opener __uri_opener = {
|
|
|
|
.scheme = "tcp",
|
|
|
|
.open = tcp_open_uri,
|
|
|
|
};
|
|
|
|
|
|
|
|
/** TCP socket opener */
|
|
|
|
struct socket_opener tcp_socket_opener __socket_opener = {
|
|
|
|
.domain = PF_INET,
|
|
|
|
.type = SOCK_STREAM,
|
|
|
|
.open = tcp_open,
|
2006-08-01 20:46:50 +00:00
|
|
|
};
|