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>
|
2006-04-30 02:08:42 +00:00
|
|
|
#include <gpxe/pkbuff.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-01-31 02:09:13 +00:00
|
|
|
#include <gpxe/stream.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-01-31 02:09:13 +00:00
|
|
|
struct tcp_connection;
|
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
|
|
|
static int tcp_senddata_conn ( struct tcp_connection *tcp, int force_send );
|
|
|
|
static struct stream_connection_operations tcp_op;
|
2006-08-01 20:46:50 +00:00
|
|
|
|
2006-04-30 01:16:37 +00:00
|
|
|
/**
|
2006-12-27 23:09:46 +00:00
|
|
|
* A TCP connection
|
2006-04-30 01:16:37 +00:00
|
|
|
*
|
2006-12-27 23:09:46 +00:00
|
|
|
* This data structure represents the internal state of a TCP
|
2007-01-31 02:09:13 +00:00
|
|
|
* connection.
|
2006-04-30 01:16:37 +00:00
|
|
|
*/
|
2006-12-27 23:09:46 +00:00
|
|
|
struct tcp_connection {
|
2007-01-31 02:09:13 +00:00
|
|
|
/** The stream connection */
|
|
|
|
struct stream_connection stream;
|
2006-12-27 23:09:46 +00:00
|
|
|
/** List of TCP connections */
|
|
|
|
struct list_head list;
|
|
|
|
|
|
|
|
/** Remote socket address */
|
|
|
|
struct sockaddr_tcpip peer;
|
|
|
|
/** Local port, in network byte order */
|
|
|
|
uint16_t local_port;
|
|
|
|
|
|
|
|
/** 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
|
|
|
|
2006-12-27 23:09:46 +00:00
|
|
|
/** Transmit packet buffer
|
|
|
|
*
|
|
|
|
* This buffer is allocated prior to calling the application's
|
|
|
|
* senddata() method, to provide temporary storage space.
|
|
|
|
*/
|
|
|
|
struct pk_buff *tx_pkb;
|
|
|
|
/** 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 );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2006-12-27 23:09:46 +00:00
|
|
|
* Allocate TCP connection
|
|
|
|
*
|
|
|
|
* @ret conn TCP connection, or NULL
|
2006-04-30 01:16:37 +00:00
|
|
|
*
|
2006-12-27 23:09:46 +00:00
|
|
|
* Allocates TCP connection and adds it to the TCP connection list.
|
2006-04-30 01:16:37 +00:00
|
|
|
*/
|
2006-12-27 23:09:46 +00:00
|
|
|
static struct tcp_connection * alloc_tcp ( void ) {
|
2007-01-31 02:09:13 +00:00
|
|
|
struct tcp_connection *tcp;
|
|
|
|
|
|
|
|
tcp = malloc ( sizeof ( *tcp ) );
|
|
|
|
if ( tcp ) {
|
|
|
|
DBGC ( tcp, "TCP %p allocated\n", tcp );
|
|
|
|
memset ( tcp, 0, sizeof ( *tcp ) );
|
|
|
|
tcp->tcp_state = tcp->prev_tcp_state = TCP_CLOSED;
|
|
|
|
tcp->snd_seq = random();
|
|
|
|
tcp->timer.expired = tcp_expired;
|
|
|
|
tcp->stream.op = &tcp_op;
|
|
|
|
list_add ( &tcp->list, &tcp_conns );
|
|
|
|
}
|
|
|
|
return tcp;
|
2006-04-30 01:16:37 +00:00
|
|
|
}
|
2006-04-30 02:08:42 +00:00
|
|
|
|
|
|
|
/**
|
2006-12-27 23:09:46 +00:00
|
|
|
* Free TCP connection
|
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
|
|
|
*
|
|
|
|
* Removes connection from TCP connection list and frees the data
|
|
|
|
* structure.
|
2006-04-30 02:08:42 +00:00
|
|
|
*/
|
2007-01-31 02:09:13 +00:00
|
|
|
static void free_tcp ( struct tcp_connection *tcp ) {
|
2006-04-30 02:08:42 +00:00
|
|
|
|
2007-01-31 02:09:13 +00:00
|
|
|
assert ( tcp );
|
|
|
|
assert ( tcp->tcp_state == TCP_CLOSED );
|
2006-06-16 00:19:46 +00:00
|
|
|
|
2007-01-31 02:09:13 +00:00
|
|
|
stop_timer ( &tcp->timer );
|
|
|
|
list_del ( &tcp->list );
|
|
|
|
free ( tcp );
|
|
|
|
DBGC ( tcp, "TCP %p freed\n", tcp );
|
2006-05-27 19:01:20 +00:00
|
|
|
}
|
|
|
|
|
2007-01-14 16:47:03 +00:00
|
|
|
/**
|
|
|
|
* Abort TCP connection
|
|
|
|
*
|
2007-01-31 02:09:13 +00:00
|
|
|
* @v tcp TCP connection
|
2007-01-14 16:47:03 +00:00
|
|
|
* @v send_rst Send a RST after closing
|
|
|
|
* @v rc Reason code
|
|
|
|
*/
|
2007-01-31 02:09:13 +00:00
|
|
|
static void tcp_abort ( struct tcp_connection *tcp, int send_rst, int rc ) {
|
2007-01-14 16:47:03 +00:00
|
|
|
|
|
|
|
/* Transition to CLOSED */
|
2007-01-31 02:09:13 +00:00
|
|
|
tcp->tcp_state = TCP_CLOSED;
|
|
|
|
tcp_dump_state ( tcp );
|
2007-01-14 16:47:03 +00:00
|
|
|
|
|
|
|
/* Send RST if requested to do so */
|
|
|
|
if ( send_rst )
|
2007-01-31 02:09:13 +00:00
|
|
|
tcp_senddata_conn ( tcp, 1 );
|
2007-01-14 16:47:03 +00:00
|
|
|
|
2007-01-31 02:09:13 +00:00
|
|
|
/* Close stream */
|
|
|
|
stream_closed ( &tcp->stream, rc );
|
2007-01-14 16:47:03 +00:00
|
|
|
|
|
|
|
/* Free the connection */
|
2007-01-31 02:09:13 +00:00
|
|
|
free_tcp ( tcp );
|
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
|
|
|
|
*
|
|
|
|
* Transmits any outstanding data on the connection. If the
|
|
|
|
* connection is in a connected state, the application's senddata()
|
|
|
|
* method will be called to generate the data payload, if any.
|
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-01-31 02:09:13 +00:00
|
|
|
static int tcp_senddata_conn ( struct tcp_connection *tcp, int force_send ) {
|
2006-12-27 23:09:46 +00:00
|
|
|
struct pk_buff *pkb;
|
|
|
|
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;
|
2006-12-27 23:09:46 +00:00
|
|
|
size_t len;
|
|
|
|
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
|
|
|
|
2006-12-27 23:09:46 +00:00
|
|
|
/* Allocate space to the TX buffer */
|
|
|
|
pkb = alloc_pkb ( MAX_PKB_LEN );
|
|
|
|
if ( ! pkb ) {
|
2007-01-31 02:09:13 +00:00
|
|
|
DBGC ( tcp, "TCP %p could not allocate data buffer\n", tcp );
|
2006-12-27 23:09:46 +00:00
|
|
|
/* Start the retry timer so that we attempt to
|
|
|
|
* retransmit this packet later. (Start it
|
|
|
|
* unconditionally, since without a packet buffer we
|
2006-12-29 14:03:03 +00:00
|
|
|
* can't call the senddata() callback, and so may not
|
2006-12-27 23:09:46 +00:00
|
|
|
* be able to tell whether or not we have something
|
|
|
|
* that actually needs to be retransmitted).
|
|
|
|
*/
|
2007-01-31 02:09:13 +00:00
|
|
|
start_timer ( &tcp->timer );
|
2006-12-27 23:09:46 +00:00
|
|
|
return -ENOMEM;
|
2006-04-30 02:08:42 +00:00
|
|
|
}
|
2006-12-27 23:09:46 +00:00
|
|
|
pkb_reserve ( pkb, MAX_HDR_LEN );
|
2006-04-30 02:08:42 +00:00
|
|
|
|
2006-12-27 23:09:46 +00:00
|
|
|
/* If we are connected, call the senddata() method, which may
|
|
|
|
* call tcp_send() to queue up a data payload.
|
|
|
|
*/
|
2007-01-31 02:09:13 +00:00
|
|
|
if ( TCP_CAN_SEND_DATA ( tcp->tcp_state ) ) {
|
|
|
|
tcp->tx_pkb = pkb;
|
|
|
|
stream_senddata ( &tcp->stream, pkb->data,
|
|
|
|
pkb_tailroom ( pkb ) );
|
|
|
|
tcp->tx_pkb = NULL;
|
2006-12-27 23:09:46 +00:00
|
|
|
}
|
2006-04-30 02:08:42 +00:00
|
|
|
|
2006-12-29 14:03:03 +00:00
|
|
|
/* Truncate payload length to fit transmit window */
|
|
|
|
len = pkb_len ( pkb );
|
2007-01-31 02:09:13 +00:00
|
|
|
if ( len > tcp->snd_win )
|
|
|
|
len = tcp->snd_win;
|
2006-12-29 14:03:03 +00:00
|
|
|
|
2006-12-27 23:09:46 +00:00
|
|
|
/* Calculate amount of sequence space that this transmission
|
|
|
|
* consumes. (SYN or FIN consume one byte, and we can never
|
|
|
|
* send both at once).
|
|
|
|
*/
|
|
|
|
seq_len = len;
|
2007-01-31 02:09:13 +00:00
|
|
|
flags = TCP_FLAGS_SENDING ( tcp->tcp_state );
|
2006-12-29 00:44:31 +00:00
|
|
|
assert ( ! ( ( flags & TCP_SYN ) && ( flags & TCP_FIN ) ) );
|
|
|
|
if ( flags & ( TCP_SYN | TCP_FIN ) )
|
2006-12-27 23:09:46 +00:00
|
|
|
seq_len++;
|
2007-01-31 02:09:13 +00:00
|
|
|
tcp->snd_sent = seq_len;
|
2006-12-27 23:09:46 +00:00
|
|
|
|
|
|
|
/* If we have nothing to transmit, drop the packet */
|
|
|
|
if ( ( seq_len == 0 ) && ! force_send ) {
|
|
|
|
free_pkb ( pkb );
|
|
|
|
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
|
|
|
|
* retransmission timer.
|
|
|
|
*/
|
|
|
|
if ( seq_len )
|
2007-01-31 02:09:13 +00:00
|
|
|
start_timer ( &tcp->timer );
|
2006-08-01 20:46:50 +00:00
|
|
|
|
2007-01-18 20:39:17 +00:00
|
|
|
/* Estimate window size */
|
|
|
|
window = freemem;
|
|
|
|
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-01-13 17:36:17 +00:00
|
|
|
payload = pkb->data;
|
|
|
|
if ( flags & TCP_SYN ) {
|
|
|
|
mssopt = pkb_push ( pkb, sizeof ( *mssopt ) );
|
|
|
|
mssopt->kind = TCP_OPTION_MSS;
|
|
|
|
mssopt->length = sizeof ( *mssopt );
|
|
|
|
mssopt->mss = htons ( TCP_MSS );
|
|
|
|
}
|
2006-12-27 23:09:46 +00:00
|
|
|
tcphdr = pkb_push ( pkb, sizeof ( *tcphdr ) );
|
|
|
|
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-01-13 17:36:17 +00:00
|
|
|
tcphdr->hlen = ( ( payload - pkb->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 );
|
2006-12-27 23:09:46 +00:00
|
|
|
tcphdr->csum = tcpip_chksum ( pkb->data, pkb_len ( pkb ) );
|
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-01-31 02:09:13 +00:00
|
|
|
rc = tcpip_tx ( pkb, &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 ) );
|
|
|
|
tcp_abort ( tcp, 0, 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
|
|
|
* Transmit any outstanding data
|
2006-08-01 20:46:50 +00:00
|
|
|
*
|
2007-01-31 02:09:13 +00:00
|
|
|
* @v stream TCP stream
|
2006-12-27 23:09:46 +00:00
|
|
|
*
|
|
|
|
* This function allocates space to the transmit buffer and invokes
|
|
|
|
* the senddata() callback function, to allow the application to
|
|
|
|
* transmit new data.
|
2006-08-01 20:46:50 +00:00
|
|
|
*/
|
2007-01-31 02:09:13 +00:00
|
|
|
static int tcp_kick ( struct stream_connection *stream ) {
|
|
|
|
struct tcp_connection *tcp =
|
|
|
|
container_of ( stream, struct tcp_connection, stream );
|
2006-08-19 16:14:53 +00:00
|
|
|
|
2007-01-31 02:09:13 +00:00
|
|
|
return tcp_senddata_conn ( tcp, 0 );
|
2006-08-08 03:42:30 +00:00
|
|
|
}
|
|
|
|
|
2006-08-01 20:46:50 +00:00
|
|
|
/**
|
2006-12-27 23:09:46 +00:00
|
|
|
* Transmit data
|
2006-08-01 20:46:50 +00:00
|
|
|
*
|
2007-01-31 02:09:13 +00:00
|
|
|
* @v stream TCP stream
|
2006-12-27 23:09:46 +00:00
|
|
|
* @v data Data to be sent
|
|
|
|
* @v len Length of the data
|
|
|
|
* @ret rc Return status code
|
2006-08-01 20:46:50 +00:00
|
|
|
*
|
2006-12-27 23:09:46 +00:00
|
|
|
* This function queues data to be sent via the TCP connection. It
|
|
|
|
* can be called only in the context of an application's senddata()
|
|
|
|
* method.
|
2006-08-01 20:46:50 +00:00
|
|
|
*/
|
2007-01-31 02:09:13 +00:00
|
|
|
static int tcp_send ( struct stream_connection *stream,
|
|
|
|
const void *data, size_t len ) {
|
|
|
|
struct tcp_connection *tcp =
|
|
|
|
container_of ( stream, struct tcp_connection, stream );
|
2006-12-27 23:09:46 +00:00
|
|
|
struct pk_buff *pkb;
|
|
|
|
|
|
|
|
/* Check that we have a packet buffer to fill */
|
2007-01-31 02:09:13 +00:00
|
|
|
pkb = tcp->tx_pkb;
|
2006-12-27 23:09:46 +00:00
|
|
|
if ( ! pkb ) {
|
2007-01-31 02:09:13 +00:00
|
|
|
DBGC ( tcp, "TCP %p tried to send data outside of the "
|
|
|
|
"senddata() method\n", tcp );
|
2006-12-27 23:09:46 +00:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Truncate length to fit packet buffer */
|
2007-01-09 20:56:31 +00:00
|
|
|
if ( len > pkb_tailroom ( pkb ) )
|
|
|
|
len = pkb_tailroom ( pkb );
|
2006-12-27 23:09:46 +00:00
|
|
|
|
|
|
|
/* Copy payload */
|
|
|
|
memmove ( pkb_put ( pkb, len ), data, len );
|
|
|
|
|
|
|
|
return 0;
|
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-01-31 02:09:13 +00:00
|
|
|
tcp_abort ( tcp, 1, -ETIMEDOUT );
|
2006-12-27 23:09:46 +00:00
|
|
|
} else {
|
|
|
|
/* Otherwise, retransmit the packet */
|
2007-01-31 02:09:13 +00:00
|
|
|
tcp_senddata_conn ( 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-01-31 02:09:13 +00:00
|
|
|
static int tcp_send_reset ( struct tcp_connection *tcp,
|
2007-01-03 22:21:59 +00:00
|
|
|
struct tcp_header *in_tcphdr ) {
|
|
|
|
struct pk_buff *pkb;
|
|
|
|
struct tcp_header *tcphdr;
|
|
|
|
|
|
|
|
/* Allocate space for dataless TX buffer */
|
|
|
|
pkb = alloc_pkb ( MAX_HDR_LEN );
|
|
|
|
if ( ! pkb ) {
|
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;
|
|
|
|
}
|
|
|
|
pkb_reserve ( pkb, MAX_HDR_LEN );
|
|
|
|
|
|
|
|
/* Construct RST response */
|
|
|
|
tcphdr = pkb_push ( pkb, sizeof ( *tcphdr ) );
|
|
|
|
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-01-03 22:21:59 +00:00
|
|
|
tcphdr->csum = tcpip_chksum ( pkb->data, pkb_len ( pkb ) );
|
|
|
|
|
|
|
|
/* 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-01-31 02:09:13 +00:00
|
|
|
return tcpip_tx ( pkb, &tcp_protocol, &tcp->peer,
|
2007-01-10 02:25:11 +00:00
|
|
|
NULL, &tcphdr->csum );
|
2007-01-03 22:21:59 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
*/
|
2006-12-27 23:09:46 +00:00
|
|
|
static struct tcp_connection * tcp_demux ( uint16_t 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 ) |
|
2006-12-27 23:09:46 +00:00
|
|
|
TCP_STATE_RCVD ( TCP_SYN ) );
|
|
|
|
|
|
|
|
/* Acknowledge SYN */
|
2007-01-31 02:09:13 +00:00
|
|
|
tcp->rcv_ack++;
|
2006-08-09 12:08:20 +00:00
|
|
|
|
2007-01-09 05:01:22 +00:00
|
|
|
/* Notify application of established connection, if applicable */
|
2007-01-31 02:09:13 +00:00
|
|
|
if ( ( tcp->tcp_state & TCP_STATE_ACKED ( TCP_SYN ) ) )
|
|
|
|
stream_connected ( &tcp->stream );
|
2007-01-09 05:01:22 +00:00
|
|
|
|
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;
|
|
|
|
unsigned int acked_flags = 0;
|
|
|
|
|
|
|
|
/* 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
|
|
|
|
2006-12-27 23:09:46 +00:00
|
|
|
/* If we are sending flags and this ACK acknowledges all
|
|
|
|
* outstanding sequence points, then it acknowledges the
|
|
|
|
* flags. (This works since both SYN and FIN will always be
|
|
|
|
* the last outstanding sequence point.)
|
2006-08-09 14:44:58 +00:00
|
|
|
*/
|
2006-12-27 23:09:46 +00:00
|
|
|
len = ack_len;
|
2007-01-31 02:09:13 +00:00
|
|
|
if ( ack_len == tcp->snd_sent ) {
|
|
|
|
acked_flags = ( TCP_FLAGS_SENDING ( tcp->tcp_state ) &
|
2006-12-27 23:09:46 +00:00
|
|
|
( TCP_SYN | TCP_FIN ) );
|
|
|
|
if ( acked_flags )
|
|
|
|
len--;
|
2006-08-01 20:46:50 +00:00
|
|
|
}
|
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
|
|
|
|
|
|
|
/* Notify application of acknowledged data, if any */
|
2007-01-31 02:09:13 +00:00
|
|
|
if ( len )
|
|
|
|
stream_acked ( &tcp->stream, len );
|
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
|
|
|
|
|
|
|
/* Notify application of established connection, if applicable */
|
2007-01-09 05:01:22 +00:00
|
|
|
if ( ( acked_flags & TCP_SYN ) &&
|
2007-01-31 02:09:13 +00:00
|
|
|
( tcp->tcp_state & TCP_STATE_RCVD ( TCP_SYN ) ) ) {
|
|
|
|
stream_connected ( &tcp->stream );
|
|
|
|
}
|
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)
|
|
|
|
* @v data Data buffer
|
|
|
|
* @v len Length of data buffer
|
|
|
|
* @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_data ( struct tcp_connection *tcp, uint32_t seq,
|
2006-12-27 23:09:46 +00:00
|
|
|
void *data, size_t len ) {
|
|
|
|
size_t already_rcvd;
|
|
|
|
|
|
|
|
/* Ignore duplicate data */
|
2007-01-31 02:09:13 +00:00
|
|
|
already_rcvd = ( tcp->rcv_ack - seq );
|
2006-12-27 23:09:46 +00:00
|
|
|
if ( already_rcvd >= len )
|
|
|
|
return 0;
|
|
|
|
data += already_rcvd;
|
|
|
|
len -= already_rcvd;
|
|
|
|
|
|
|
|
/* Acknowledge new data */
|
2007-01-31 02:09:13 +00:00
|
|
|
tcp->rcv_ack += len;
|
2006-12-27 23:09:46 +00:00
|
|
|
|
|
|
|
/* Notify application */
|
2007-01-31 02:09:13 +00:00
|
|
|
stream_newdata ( &tcp->stream, data, 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
|
|
|
|
2006-12-29 00:44:31 +00:00
|
|
|
/* Mark FIN as received, acknowledge it, and send our own FIN */
|
2007-01-31 02:09:13 +00:00
|
|
|
tcp->tcp_state |= ( TCP_STATE_RCVD ( TCP_FIN ) |
|
2006-12-29 00:44:31 +00:00
|
|
|
TCP_STATE_SENT ( TCP_FIN ) );
|
2007-01-31 02:09:13 +00:00
|
|
|
tcp->rcv_ack++;
|
2006-08-07 18:52:26 +00:00
|
|
|
|
2007-01-31 02:09:13 +00:00
|
|
|
/* Close stream */
|
|
|
|
stream_closed ( &tcp->stream, 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-01-14 16:47:03 +00:00
|
|
|
/* Abort connection without sending a RST */
|
2007-01-31 02:09:13 +00:00
|
|
|
tcp_abort ( tcp, 0, -ECONNRESET );
|
2007-01-03 21:51:36 +00:00
|
|
|
|
|
|
|
return -ECONNRESET;
|
|
|
|
}
|
|
|
|
|
2006-08-01 20:46:50 +00:00
|
|
|
/**
|
|
|
|
* Process received packet
|
|
|
|
*
|
2006-12-27 23:09:46 +00:00
|
|
|
* @v pkb Packet 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
|
|
|
|
*/
|
2006-08-02 00:02:21 +00:00
|
|
|
static int tcp_rx ( struct pk_buff *pkb,
|
|
|
|
struct sockaddr_tcpip *st_src __unused,
|
2007-01-03 20:48:52 +00:00
|
|
|
struct sockaddr_tcpip *st_dest __unused,
|
|
|
|
uint16_t pshdr_csum ) {
|
2007-01-03 21:13:11 +00:00
|
|
|
struct tcp_header *tcphdr = pkb->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;
|
|
|
|
void *data;
|
|
|
|
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 */
|
2006-08-01 20:46:50 +00:00
|
|
|
if ( pkb_len ( pkb ) < sizeof ( *tcphdr ) ) {
|
2006-12-27 23:09:46 +00:00
|
|
|
DBG ( "TCP packet too short at %d bytes (min %d bytes)\n",
|
|
|
|
pkb_len ( pkb ), sizeof ( *tcphdr ) );
|
2006-08-09 14:27:07 +00:00
|
|
|
rc = -EINVAL;
|
2007-01-03 21:13:11 +00:00
|
|
|
goto done;
|
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-01-03 21:13:11 +00:00
|
|
|
goto done;
|
2006-08-19 16:14:53 +00:00
|
|
|
}
|
2006-12-27 23:09:46 +00:00
|
|
|
if ( hlen > pkb_len ( pkb ) ) {
|
|
|
|
DBG ( "TCP header too long at %d bytes (max %d bytes)\n",
|
|
|
|
hlen, pkb_len ( pkb ) );
|
|
|
|
rc = -EINVAL;
|
2007-01-03 21:13:11 +00:00
|
|
|
goto done;
|
2006-08-01 20:46:50 +00:00
|
|
|
}
|
2007-01-03 20:48:52 +00:00
|
|
|
csum = tcpip_continue_chksum ( pshdr_csum, pkb->data, pkb_len ( pkb ));
|
|
|
|
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;
|
|
|
|
goto done;
|
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;
|
|
|
|
data = pkb_pull ( pkb, hlen );
|
|
|
|
len = pkb_len ( pkb );
|
|
|
|
|
|
|
|
/* 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 ) {
|
|
|
|
tcp_send_reset ( tcp, tcphdr );
|
2007-01-03 21:13:11 +00:00
|
|
|
rc = -ENOTCONN;
|
|
|
|
goto done;
|
|
|
|
}
|
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 ) {
|
|
|
|
tcp_send_reset ( tcp, tcphdr );
|
2007-01-03 22:21:59 +00:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
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-01-03 21:51:36 +00:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2006-12-27 23:09:46 +00:00
|
|
|
/* Handle new data, if any */
|
2007-01-31 02:09:13 +00:00
|
|
|
tcp_rx_data ( tcp, seq, data, len );
|
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-01-31 02:09:13 +00:00
|
|
|
tcp_senddata_conn ( 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-01-03 21:13:11 +00:00
|
|
|
rc = 0;
|
|
|
|
done:
|
2006-12-27 23:09:46 +00:00
|
|
|
/* Free received packet */
|
|
|
|
free_pkb ( pkb );
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Bind TCP connection to local port
|
|
|
|
*
|
2007-01-31 02:09:13 +00:00
|
|
|
* @v stream TCP stream
|
|
|
|
* @v local Local address
|
2006-12-27 23:09:46 +00:00
|
|
|
* @ret rc Return status code
|
|
|
|
*
|
2007-01-31 02:09:13 +00:00
|
|
|
* Only the port portion of the local address is used. If the local
|
|
|
|
* port is 0, the connection is assigned an available port between
|
|
|
|
* 1024 and 65535.
|
2006-12-27 23:09:46 +00:00
|
|
|
*/
|
2007-01-31 02:09:13 +00:00
|
|
|
static int tcp_bind ( struct stream_connection *stream,
|
|
|
|
struct sockaddr *local ) {
|
|
|
|
struct tcp_connection *tcp =
|
|
|
|
container_of ( stream, struct tcp_connection, stream );
|
|
|
|
struct sockaddr_tcpip *st = ( ( struct sockaddr_tcpip * ) local );
|
2006-12-27 23:09:46 +00:00
|
|
|
struct tcp_connection *existing;
|
2007-01-31 02:09:13 +00:00
|
|
|
struct sockaddr_tcpip try;
|
2006-12-27 23:09:46 +00:00
|
|
|
static uint16_t try_port = 1024;
|
2007-01-31 02:09:13 +00:00
|
|
|
uint16_t local_port = st->st_port;
|
2006-12-27 23:09:46 +00:00
|
|
|
|
|
|
|
/* If no port specified, find the first available port */
|
|
|
|
if ( ! local_port ) {
|
|
|
|
for ( ; try_port ; try_port++ ) {
|
|
|
|
if ( try_port < 1024 )
|
|
|
|
continue;
|
2007-01-31 02:09:13 +00:00
|
|
|
try.st_port = htons ( try_port );
|
|
|
|
if ( tcp_bind ( stream,
|
|
|
|
( struct sockaddr * ) &try ) == 0 ) {
|
2006-12-27 23:09:46 +00:00
|
|
|
return 0;
|
2007-01-31 02:09:13 +00:00
|
|
|
}
|
2006-08-01 20:46:50 +00:00
|
|
|
}
|
2007-01-31 02:09:13 +00:00
|
|
|
DBGC ( tcp, "TCP %p could not bind: no free ports\n", tcp );
|
2006-12-27 23:09:46 +00:00
|
|
|
return -EADDRINUSE;
|
2006-08-01 20:46:50 +00:00
|
|
|
}
|
2006-12-27 23:09:46 +00:00
|
|
|
|
|
|
|
/* Attempt bind to local port */
|
|
|
|
list_for_each_entry ( existing, &tcp_conns, list ) {
|
|
|
|
if ( existing->local_port == local_port ) {
|
2007-01-31 02:09:13 +00:00
|
|
|
DBGC ( tcp, "TCP %p could not bind: port %d in use\n",
|
|
|
|
tcp, ntohs ( local_port ) );
|
2006-12-27 23:09:46 +00:00
|
|
|
return -EADDRINUSE;
|
2006-08-08 22:25:20 +00:00
|
|
|
}
|
|
|
|
}
|
2007-01-31 02:09:13 +00:00
|
|
|
tcp->local_port = local_port;
|
2006-12-27 23:09:46 +00:00
|
|
|
|
2007-01-31 02:09:13 +00:00
|
|
|
DBGC ( tcp, "TCP %p bound to port %d\n", tcp, ntohs ( local_port ) );
|
2006-12-27 23:09:46 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Connect to a remote server
|
|
|
|
*
|
2007-01-31 02:09:13 +00:00
|
|
|
* @v stream TCP stream
|
2006-12-27 23:09:46 +00:00
|
|
|
* @v peer Remote socket address
|
|
|
|
* @ret rc Return status code
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2007-01-31 02:09:13 +00:00
|
|
|
static int tcp_connect ( struct stream_connection *stream,
|
|
|
|
struct sockaddr *peer ) {
|
|
|
|
struct tcp_connection *tcp =
|
|
|
|
container_of ( stream, struct tcp_connection, stream );
|
|
|
|
struct sockaddr_tcpip *st = ( ( struct sockaddr_tcpip * ) peer );
|
|
|
|
struct sockaddr_tcpip local;
|
2006-12-27 23:09:46 +00:00
|
|
|
int rc;
|
|
|
|
|
2007-01-31 02:09:13 +00:00
|
|
|
/* Bind to local port if not already bound */
|
|
|
|
if ( ! tcp->local_port ) {
|
|
|
|
local.st_port = 0;
|
|
|
|
if ( ( rc = tcp_bind ( stream,
|
|
|
|
( struct sockaddr * ) &local ) ) != 0 ){
|
|
|
|
return rc;
|
|
|
|
}
|
2006-12-27 23:09:46 +00:00
|
|
|
}
|
|
|
|
|
2007-01-31 02:09:13 +00:00
|
|
|
/* Bind to peer */
|
|
|
|
memcpy ( &tcp->peer, st, sizeof ( tcp->peer ) );
|
2006-12-27 23:09:46 +00:00
|
|
|
|
|
|
|
/* Transition to TCP_SYN_SENT and send the SYN */
|
2007-01-31 02:09:13 +00:00
|
|
|
tcp->tcp_state = TCP_SYN_SENT;
|
|
|
|
tcp_dump_state ( tcp );
|
|
|
|
tcp_senddata_conn ( tcp, 0 );
|
2006-12-27 23:09:46 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Close the connection
|
|
|
|
*
|
2007-01-31 02:09:13 +00:00
|
|
|
* @v stream TCP stream
|
2006-12-27 23:09:46 +00:00
|
|
|
*
|
2007-01-31 02:09:13 +00:00
|
|
|
* The TCP connection will persist until the state machine has
|
|
|
|
* returned to the TCP_CLOSED state.
|
2006-12-27 23:09:46 +00:00
|
|
|
*/
|
2007-01-31 02:09:13 +00:00
|
|
|
static void tcp_close ( struct stream_connection *stream ) {
|
|
|
|
struct tcp_connection *tcp =
|
|
|
|
container_of ( stream, struct tcp_connection, stream );
|
2006-12-27 23:09:46 +00:00
|
|
|
|
|
|
|
/* If we have not yet received a SYN (i.e. we are in CLOSED,
|
|
|
|
* LISTEN or SYN_SENT), just delete the connection
|
2006-08-19 16:14:53 +00:00
|
|
|
*/
|
2007-01-31 02:09:13 +00:00
|
|
|
if ( ! ( tcp->tcp_state & TCP_STATE_RCVD ( TCP_SYN ) ) ) {
|
|
|
|
tcp->tcp_state = TCP_CLOSED;
|
|
|
|
tcp_dump_state ( tcp );
|
|
|
|
free_tcp ( tcp );
|
2006-12-27 23:09:46 +00:00
|
|
|
return;
|
2006-08-01 20:46:50 +00:00
|
|
|
}
|
2006-12-27 23:09:46 +00:00
|
|
|
|
2006-12-29 00:44:31 +00:00
|
|
|
/* 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.
|
2006-12-27 23:09:46 +00:00
|
|
|
*/
|
2007-01-31 02:09:13 +00:00
|
|
|
if ( ! ( tcp->tcp_state & TCP_STATE_ACKED ( TCP_SYN ) ) )
|
|
|
|
tcp_rx_ack ( tcp, ( tcp->snd_seq + 1 ), 0 );
|
2006-12-27 23:09:46 +00:00
|
|
|
|
|
|
|
/* Send a FIN to initiate the close */
|
2007-01-31 02:09:13 +00:00
|
|
|
tcp->tcp_state |= TCP_STATE_SENT ( TCP_FIN );
|
|
|
|
tcp_dump_state ( tcp );
|
|
|
|
tcp_senddata_conn ( tcp, 0 );
|
2006-08-01 20:46:50 +00:00
|
|
|
}
|
|
|
|
|
2007-01-31 02:09:13 +00:00
|
|
|
/**
|
|
|
|
* Open TCP connection
|
|
|
|
*
|
|
|
|
* @v app Stream application
|
|
|
|
* @ret rc Return status code
|
|
|
|
*/
|
|
|
|
int tcp_open ( struct stream_application *app ) {
|
|
|
|
struct tcp_connection *tcp;
|
|
|
|
|
|
|
|
/* Application must not already have an open connection */
|
|
|
|
if ( app->conn ) {
|
|
|
|
DBG ( "TCP app %p already open on %p\n", app, app->conn );
|
|
|
|
return -EISCONN;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate connection state storage and add to connection list */
|
|
|
|
tcp = alloc_tcp();
|
|
|
|
if ( ! tcp ) {
|
|
|
|
DBG ( "TCP app %p could not allocate connection\n", app );
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Associate application with connection */
|
|
|
|
stream_associate ( app, &tcp->stream );
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** TCP stream operations */
|
|
|
|
static struct stream_connection_operations tcp_op = {
|
|
|
|
.bind = tcp_bind,
|
|
|
|
.connect = tcp_connect,
|
|
|
|
.close = tcp_close,
|
|
|
|
.send = tcp_send,
|
|
|
|
.kick = tcp_kick,
|
|
|
|
};
|
|
|
|
|
2006-08-01 20:46:50 +00:00
|
|
|
/** TCP protocol */
|
2006-08-09 04:42:14 +00:00
|
|
|
struct tcpip_protocol tcp_protocol __tcpip_protocol = {
|
2006-08-01 20:46:50 +00:00
|
|
|
.name = "TCP",
|
|
|
|
.rx = tcp_rx,
|
2006-08-02 00:02:21 +00:00
|
|
|
.tcpip_proto = IP_TCP,
|
2006-08-01 20:46:50 +00:00
|
|
|
};
|