2006-06-28 17:43:08 +02:00
|
|
|
#include <stdint.h>
|
2013-08-06 16:56:54 +02:00
|
|
|
#include <stdlib.h>
|
2006-06-28 17:43:08 +02:00
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <byteswap.h>
|
2010-04-19 21:16:01 +02:00
|
|
|
#include <ipxe/iobuf.h>
|
|
|
|
#include <ipxe/tables.h>
|
2014-03-02 21:33:35 +01:00
|
|
|
#include <ipxe/ipstat.h>
|
2014-03-04 14:10:07 +01:00
|
|
|
#include <ipxe/netdevice.h>
|
2010-04-19 21:16:01 +02:00
|
|
|
#include <ipxe/tcpip.h>
|
2006-06-28 17:43:08 +02:00
|
|
|
|
|
|
|
/** @file
|
|
|
|
*
|
|
|
|
* Transport-network layer interface
|
|
|
|
*
|
2006-08-02 02:02:21 +02:00
|
|
|
* This file contains functions and utilities for the
|
|
|
|
* TCP/IP transport-network layer interface
|
2006-06-28 17:43:08 +02:00
|
|
|
*/
|
|
|
|
|
2015-03-02 14:29:46 +01:00
|
|
|
FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
|
2009-05-01 16:41:06 +02:00
|
|
|
|
2014-03-04 13:54:21 +01:00
|
|
|
/**
|
|
|
|
* Process a received TCP/IP packet
|
2006-06-28 17:43:08 +02:00
|
|
|
*
|
2007-05-19 20:39:40 +02:00
|
|
|
* @v iobuf I/O buffer
|
2013-09-01 21:55:18 +02:00
|
|
|
* @v netdev Network device
|
2006-08-02 02:02:21 +02: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 21:48:52 +01:00
|
|
|
* @v pshdr_csum Pseudo-header checksum
|
2014-03-02 21:33:35 +01:00
|
|
|
* @v stats IP statistics
|
2006-08-02 02:02:21 +02:00
|
|
|
* @ret rc Return status code
|
2006-06-28 17:43:08 +02:00
|
|
|
*
|
2006-08-02 02:02:21 +02: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 17:43:08 +02:00
|
|
|
*/
|
2013-09-01 21:55:18 +02:00
|
|
|
int tcpip_rx ( struct io_buffer *iobuf, struct net_device *netdev,
|
|
|
|
uint8_t tcpip_proto, struct sockaddr_tcpip *st_src,
|
2014-03-02 21:33:35 +01:00
|
|
|
struct sockaddr_tcpip *st_dest, uint16_t pshdr_csum,
|
|
|
|
struct ip_statistics *stats ) {
|
2006-06-28 17:43:08 +02:00
|
|
|
struct tcpip_protocol *tcpip;
|
|
|
|
|
2006-08-02 02:02:21 +02:00
|
|
|
/* Hand off packet to the appropriate transport-layer protocol */
|
2009-03-12 20:41:40 +01:00
|
|
|
for_each_table_entry ( tcpip, TCPIP_PROTOCOLS ) {
|
2006-08-02 02:02:21 +02:00
|
|
|
if ( tcpip->tcpip_proto == tcpip_proto ) {
|
|
|
|
DBG ( "TCP/IP received %s packet\n", tcpip->name );
|
2014-03-02 21:33:35 +01:00
|
|
|
stats->in_delivers++;
|
2013-09-01 21:55:18 +02:00
|
|
|
return tcpip->rx ( iobuf, netdev, st_src, st_dest,
|
|
|
|
pshdr_csum );
|
2006-06-28 17:43:08 +02:00
|
|
|
}
|
|
|
|
}
|
2006-08-02 02:02:21 +02:00
|
|
|
|
|
|
|
DBG ( "Unrecognised TCP/IP protocol %d\n", tcpip_proto );
|
2014-03-02 21:33:35 +01:00
|
|
|
stats->in_unknown_protos++;
|
2007-05-19 20:39:40 +02:00
|
|
|
free_iob ( iobuf );
|
2006-08-02 02:02:21 +02:00
|
|
|
return -EPROTONOSUPPORT;
|
2006-06-28 17:43:08 +02:00
|
|
|
}
|
|
|
|
|
2014-03-04 13:54:21 +01:00
|
|
|
/**
|
|
|
|
* Find TCP/IP network-layer protocol
|
|
|
|
*
|
2015-09-01 17:18:32 +02:00
|
|
|
* @v sa_family Address family
|
2014-03-04 13:54:21 +01:00
|
|
|
* @ret tcpip_net TCP/IP network-layer protocol, or NULL if not found
|
|
|
|
*/
|
2015-09-01 17:18:32 +02:00
|
|
|
struct tcpip_net_protocol * tcpip_net_protocol ( sa_family_t sa_family ) {
|
2014-03-04 13:54:21 +01:00
|
|
|
struct tcpip_net_protocol *tcpip_net;
|
|
|
|
|
|
|
|
for_each_table_entry ( tcpip_net, TCPIP_NET_PROTOCOLS ) {
|
2015-09-01 17:18:32 +02:00
|
|
|
if ( tcpip_net->sa_family == sa_family )
|
2014-03-04 13:54:21 +01:00
|
|
|
return tcpip_net;
|
|
|
|
}
|
|
|
|
|
2015-09-01 17:18:32 +02:00
|
|
|
DBG ( "Unrecognised TCP/IP address family %d\n", sa_family );
|
2014-03-04 13:54:21 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Transmit a TCP/IP packet
|
2006-06-28 17:43:08 +02:00
|
|
|
*
|
2007-05-19 20:39:40 +02:00
|
|
|
* @v iobuf I/O buffer
|
2006-08-02 02:02:21 +02:00
|
|
|
* @v tcpip_protocol Transport-layer protocol
|
2009-01-21 04:40:39 +01:00
|
|
|
* @v st_src Source address, or NULL to use route default
|
2006-08-02 02:02:21 +02:00
|
|
|
* @v st_dest Destination address
|
2007-01-16 05:09:13 +01:00
|
|
|
* @v netdev Network device to use if no route found, or NULL
|
2007-01-03 21:48:52 +01:00
|
|
|
* @v trans_csum Transport-layer checksum to complete, or NULL
|
2006-08-02 02:02:21 +02:00
|
|
|
* @ret rc Return status code
|
2006-06-28 17:43:08 +02:00
|
|
|
*/
|
2007-05-19 20:39:40 +02:00
|
|
|
int tcpip_tx ( struct io_buffer *iobuf, struct tcpip_protocol *tcpip_protocol,
|
2009-01-21 04:40:39 +01:00
|
|
|
struct sockaddr_tcpip *st_src, struct sockaddr_tcpip *st_dest,
|
|
|
|
struct net_device *netdev, uint16_t *trans_csum ) {
|
2006-08-01 22:27:26 +02:00
|
|
|
struct tcpip_net_protocol *tcpip_net;
|
|
|
|
|
2006-08-02 02:02:21 +02:00
|
|
|
/* Hand off packet to the appropriate network-layer protocol */
|
2015-09-01 17:18:32 +02:00
|
|
|
tcpip_net = tcpip_net_protocol ( st_dest->st_family );
|
2014-03-04 13:54:21 +01:00
|
|
|
if ( tcpip_net ) {
|
|
|
|
DBG ( "TCP/IP sending %s packet\n", tcpip_net->name );
|
|
|
|
return tcpip_net->tx ( iobuf, tcpip_protocol, st_src, st_dest,
|
|
|
|
netdev, trans_csum );
|
2006-08-01 22:27:26 +02:00
|
|
|
}
|
2014-03-04 13:54:21 +01:00
|
|
|
|
2007-05-19 20:39:40 +02:00
|
|
|
free_iob ( iobuf );
|
2006-08-01 22:27:26 +02:00
|
|
|
return -EAFNOSUPPORT;
|
|
|
|
}
|
|
|
|
|
2014-03-04 13:54:21 +01:00
|
|
|
/**
|
|
|
|
* Determine transmitting network device
|
|
|
|
*
|
|
|
|
* @v st_dest Destination address
|
|
|
|
* @ret netdev Network device, or NULL
|
|
|
|
*/
|
|
|
|
struct net_device * tcpip_netdev ( struct sockaddr_tcpip *st_dest ) {
|
|
|
|
struct tcpip_net_protocol *tcpip_net;
|
|
|
|
|
|
|
|
/* Hand off to the appropriate network-layer protocol */
|
2015-09-01 17:18:32 +02:00
|
|
|
tcpip_net = tcpip_net_protocol ( st_dest->st_family );
|
2014-03-04 13:54:21 +01:00
|
|
|
if ( tcpip_net )
|
|
|
|
return tcpip_net->netdev ( st_dest );
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-03-04 14:10:07 +01:00
|
|
|
/**
|
|
|
|
* Determine maximum transmission unit
|
|
|
|
*
|
|
|
|
* @v st_dest Destination address
|
|
|
|
* @ret mtu Maximum transmission unit
|
|
|
|
*/
|
|
|
|
size_t tcpip_mtu ( struct sockaddr_tcpip *st_dest ) {
|
|
|
|
struct tcpip_net_protocol *tcpip_net;
|
|
|
|
struct net_device *netdev;
|
|
|
|
size_t mtu;
|
|
|
|
|
|
|
|
/* Find appropriate network-layer protocol */
|
2015-09-01 17:18:32 +02:00
|
|
|
tcpip_net = tcpip_net_protocol ( st_dest->st_family );
|
2014-03-04 14:10:07 +01:00
|
|
|
if ( ! tcpip_net )
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Find transmitting network device */
|
|
|
|
netdev = tcpip_net->netdev ( st_dest );
|
|
|
|
if ( ! netdev )
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Calculate MTU */
|
|
|
|
mtu = ( netdev->max_pkt_len - netdev->ll_protocol->ll_header_len -
|
|
|
|
tcpip_net->header_len );
|
|
|
|
|
|
|
|
return mtu;
|
|
|
|
}
|
|
|
|
|
2006-06-28 17:43:08 +02:00
|
|
|
/**
|
2006-07-20 01:38:05 +02:00
|
|
|
* Calculate continued TCP/IP checkum
|
2006-06-28 17:43:08 +02:00
|
|
|
*
|
2006-07-20 01:38:05 +02: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.
|
|
|
|
*/
|
2012-06-27 13:02:58 +02:00
|
|
|
uint16_t generic_tcpip_continue_chksum ( uint16_t partial,
|
|
|
|
const void *data, size_t len ) {
|
2006-07-20 01:38:05 +02: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 21:48:52 +01:00
|
|
|
return ( ~cksum );
|
2006-07-20 01:38:05 +02: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 17:43:08 +02:00
|
|
|
*/
|
2007-01-03 21:48:52 +01:00
|
|
|
uint16_t tcpip_chksum ( const void *data, size_t len ) {
|
|
|
|
return tcpip_continue_chksum ( TCPIP_EMPTY_CSUM, data, len );
|
2006-06-28 17:43:08 +02:00
|
|
|
}
|
2013-08-06 16:56:54 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Bind to local TCP/IP port
|
|
|
|
*
|
|
|
|
* @v st_local Local TCP/IP socket address, or NULL
|
|
|
|
* @v available Function to check port availability
|
|
|
|
* @ret port Local port number, or negative error
|
|
|
|
*/
|
|
|
|
int tcpip_bind ( struct sockaddr_tcpip *st_local,
|
|
|
|
int ( * available ) ( int port ) ) {
|
|
|
|
uint16_t flags = 0;
|
|
|
|
uint16_t try_port = 0;
|
|
|
|
uint16_t min_port;
|
|
|
|
uint16_t max_port;
|
|
|
|
unsigned int offset;
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
/* Extract parameters from local socket address */
|
|
|
|
if ( st_local ) {
|
|
|
|
flags = st_local->st_flags;
|
|
|
|
try_port = ntohs ( st_local->st_port );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If an explicit port is specified, check its availability */
|
|
|
|
if ( try_port )
|
|
|
|
return available ( try_port );
|
|
|
|
|
|
|
|
/* Otherwise, find an available port in the range [1,1023] or
|
|
|
|
* [1025,65535] as appropriate.
|
|
|
|
*/
|
2015-03-13 11:19:44 +01:00
|
|
|
min_port = ( ( ( ~flags ) & TCPIP_BIND_PRIVILEGED ) + 1 );
|
2013-08-06 16:56:54 +02:00
|
|
|
max_port = ( ( flags & TCPIP_BIND_PRIVILEGED ) - 1 );
|
|
|
|
offset = random();
|
|
|
|
for ( i = 0 ; i <= max_port ; i++ ) {
|
|
|
|
try_port = ( ( i + offset ) & max_port );
|
|
|
|
if ( try_port < min_port )
|
|
|
|
continue;
|
|
|
|
if ( available ( try_port ) < 0 )
|
|
|
|
continue;
|
|
|
|
return try_port;
|
|
|
|
}
|
|
|
|
return -EADDRINUSE;
|
|
|
|
}
|