2006-06-28 15:43:08 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <byteswap.h>
|
|
|
|
#include <gpxe/pkbuff.h>
|
|
|
|
#include <gpxe/tables.h>
|
2006-08-01 14:18:09 +00:00
|
|
|
#include <gpxe/tcpip.h>
|
2006-06-28 15:43:08 +00:00
|
|
|
|
|
|
|
/** @file
|
|
|
|
*
|
|
|
|
* Transport-network layer interface
|
|
|
|
*
|
2006-08-02 00:02:21 +00:00
|
|
|
* This file contains functions and utilities for the
|
|
|
|
* TCP/IP transport-network layer interface
|
2006-06-28 15:43:08 +00:00
|
|
|
*/
|
|
|
|
|
2006-08-02 00:02:21 +00:00
|
|
|
/** Registered network-layer protocols that support TCP/IP */
|
|
|
|
static struct tcpip_net_protocol
|
|
|
|
tcpip_net_protocols[0] __table_start ( tcpip_net_protocols );
|
|
|
|
static struct tcpip_net_protocol
|
|
|
|
tcpip_net_protocols_end[0] __table_end ( tcpip_net_protocols );
|
2006-06-28 15:43:08 +00:00
|
|
|
|
2006-08-02 00:02:21 +00:00
|
|
|
/** Registered transport-layer protocols that support TCP/IP */
|
|
|
|
static struct tcpip_protocol
|
|
|
|
tcpip_protocols[0]__table_start ( tcpip_protocols );
|
|
|
|
static struct tcpip_protocol
|
|
|
|
tcpip_protocols_end[0] __table_end ( tcpip_protocols );
|
2006-06-28 15:43:08 +00:00
|
|
|
|
2006-08-02 00:02:21 +00:00
|
|
|
/** Process a received TCP/IP packet
|
2006-06-28 15:43:08 +00:00
|
|
|
*
|
|
|
|
* @v pkb Packet buffer
|
2006-08-02 00:02:21 +00:00
|
|
|
* @v tcpip_proto Transport-layer protocol number
|
|
|
|
* @v st_src Partially-filled source address
|
|
|
|
* @v st_dest Partially-filled destination address
|
2007-01-03 20:48:52 +00:00
|
|
|
* @v pshdr_csum Pseudo-header checksum
|
2006-08-02 00:02:21 +00:00
|
|
|
* @ret rc Return status code
|
2006-06-28 15:43:08 +00:00
|
|
|
*
|
2006-08-02 00:02:21 +00:00
|
|
|
* This function expects a transport-layer segment from the network
|
|
|
|
* layer. The network layer should fill in as much as it can of the
|
|
|
|
* source and destination addresses (i.e. it should fill in the
|
|
|
|
* address family and the network-layer addresses, but leave the ports
|
|
|
|
* and the rest of the structures as zero).
|
2006-06-28 15:43:08 +00:00
|
|
|
*/
|
2006-08-02 00:02:21 +00:00
|
|
|
int tcpip_rx ( struct pk_buff *pkb, uint8_t tcpip_proto,
|
|
|
|
struct sockaddr_tcpip *st_src,
|
2007-01-03 20:48:52 +00:00
|
|
|
struct sockaddr_tcpip *st_dest,
|
|
|
|
uint16_t pshdr_csum ) {
|
2006-06-28 15:43:08 +00:00
|
|
|
struct tcpip_protocol *tcpip;
|
|
|
|
|
2006-08-02 00:02:21 +00:00
|
|
|
/* Hand off packet to the appropriate transport-layer protocol */
|
|
|
|
for ( tcpip = tcpip_protocols; tcpip < tcpip_protocols_end; tcpip++ ) {
|
|
|
|
if ( tcpip->tcpip_proto == tcpip_proto ) {
|
|
|
|
DBG ( "TCP/IP received %s packet\n", tcpip->name );
|
2007-01-03 20:48:52 +00:00
|
|
|
return tcpip->rx ( pkb, st_src, st_dest, pshdr_csum );
|
2006-06-28 15:43:08 +00:00
|
|
|
}
|
|
|
|
}
|
2006-08-02 00:02:21 +00:00
|
|
|
|
|
|
|
DBG ( "Unrecognised TCP/IP protocol %d\n", tcpip_proto );
|
2006-08-09 01:24:32 +00:00
|
|
|
free_pkb ( pkb );
|
2006-08-02 00:02:21 +00:00
|
|
|
return -EPROTONOSUPPORT;
|
2006-06-28 15:43:08 +00:00
|
|
|
}
|
|
|
|
|
2006-08-02 00:02:21 +00:00
|
|
|
/** Transmit a TCP/IP packet
|
2006-06-28 15:43:08 +00:00
|
|
|
*
|
|
|
|
* @v pkb Packet buffer
|
2006-08-02 00:02:21 +00:00
|
|
|
* @v tcpip_protocol Transport-layer protocol
|
|
|
|
* @v st_dest Destination address
|
2007-01-10 02:25:11 +00:00
|
|
|
* @v netdev Network device (or NULL to route automatically)
|
2007-01-03 20:48:52 +00:00
|
|
|
* @v trans_csum Transport-layer checksum to complete, or NULL
|
2006-08-02 00:02:21 +00:00
|
|
|
* @ret rc Return status code
|
2006-06-28 15:43:08 +00:00
|
|
|
*/
|
2006-08-02 00:02:21 +00:00
|
|
|
int tcpip_tx ( struct pk_buff *pkb, struct tcpip_protocol *tcpip_protocol,
|
2007-01-10 02:25:11 +00:00
|
|
|
struct sockaddr_tcpip *st_dest, struct net_device *netdev,
|
|
|
|
uint16_t *trans_csum ) {
|
2006-08-01 20:27:26 +00:00
|
|
|
struct tcpip_net_protocol *tcpip_net;
|
|
|
|
|
2006-08-02 00:02:21 +00:00
|
|
|
/* Hand off packet to the appropriate network-layer protocol */
|
|
|
|
for ( tcpip_net = tcpip_net_protocols ;
|
|
|
|
tcpip_net < tcpip_net_protocols_end ; tcpip_net++ ) {
|
|
|
|
if ( tcpip_net->sa_family == st_dest->st_family ) {
|
|
|
|
DBG ( "TCP/IP sending %s packet\n", tcpip_net->name );
|
2007-01-03 20:48:52 +00:00
|
|
|
return tcpip_net->tx ( pkb, tcpip_protocol, st_dest,
|
2007-01-10 02:25:11 +00:00
|
|
|
netdev, trans_csum );
|
2006-08-01 20:27:26 +00:00
|
|
|
}
|
|
|
|
}
|
2006-08-02 00:02:21 +00:00
|
|
|
|
|
|
|
DBG ( "Unrecognised TCP/IP address family %d\n", st_dest->st_family );
|
2006-08-09 01:24:32 +00:00
|
|
|
free_pkb ( pkb );
|
2006-08-01 20:27:26 +00:00
|
|
|
return -EAFNOSUPPORT;
|
|
|
|
}
|
|
|
|
|
2006-06-28 15:43:08 +00:00
|
|
|
/**
|
2006-07-19 23:38:05 +00:00
|
|
|
* Calculate continued TCP/IP checkum
|
2006-06-28 15:43:08 +00:00
|
|
|
*
|
2006-07-19 23:38:05 +00:00
|
|
|
* @v partial Checksum of already-summed data, in network byte order
|
|
|
|
* @v data Data buffer
|
|
|
|
* @v len Length of data buffer
|
|
|
|
* @ret cksum Updated checksum, in network byte order
|
|
|
|
*
|
|
|
|
* Calculates a TCP/IP-style 16-bit checksum over the data block. The
|
|
|
|
* checksum is returned in network byte order.
|
|
|
|
*
|
|
|
|
* This function may be used to add new data to an existing checksum.
|
|
|
|
* The function assumes that both the old data and the new data start
|
|
|
|
* on even byte offsets; if this is not the case then you will need to
|
|
|
|
* byte-swap either the input partial checksum, the output checksum,
|
|
|
|
* or both. Deciding which to swap is left as an exercise for the
|
|
|
|
* interested reader.
|
|
|
|
*/
|
2007-01-03 20:48:52 +00:00
|
|
|
uint16_t tcpip_continue_chksum ( uint16_t partial, const void *data,
|
|
|
|
size_t len ) {
|
2006-07-19 23:38:05 +00:00
|
|
|
unsigned int cksum = ( ( ~partial ) & 0xffff );
|
|
|
|
unsigned int value;
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
for ( i = 0 ; i < len ; i++ ) {
|
|
|
|
value = * ( ( uint8_t * ) data + i );
|
|
|
|
if ( i & 1 ) {
|
|
|
|
/* Odd bytes: swap on little-endian systems */
|
|
|
|
value = be16_to_cpu ( value );
|
|
|
|
} else {
|
|
|
|
/* Even bytes: swap on big-endian systems */
|
|
|
|
value = le16_to_cpu ( value );
|
|
|
|
}
|
|
|
|
cksum += value;
|
|
|
|
if ( cksum > 0xffff )
|
|
|
|
cksum -= 0xffff;
|
|
|
|
}
|
|
|
|
|
2007-01-03 20:48:52 +00:00
|
|
|
return ( ~cksum );
|
2006-07-19 23:38:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculate TCP/IP checkum
|
|
|
|
*
|
|
|
|
* @v data Data buffer
|
|
|
|
* @v len Length of data buffer
|
|
|
|
* @ret cksum Checksum, in network byte order
|
|
|
|
*
|
|
|
|
* Calculates a TCP/IP-style 16-bit checksum over the data block. The
|
|
|
|
* checksum is returned in network byte order.
|
2006-06-28 15:43:08 +00:00
|
|
|
*/
|
2007-01-03 20:48:52 +00:00
|
|
|
uint16_t tcpip_chksum ( const void *data, size_t len ) {
|
|
|
|
return tcpip_continue_chksum ( TCPIP_EMPTY_CSUM, data, len );
|
2006-06-28 15:43:08 +00:00
|
|
|
}
|