2006-06-25 05:13:17 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <byteswap.h>
|
|
|
|
#include <errno.h>
|
2006-08-02 00:02:21 +00:00
|
|
|
#include <gpxe/tcpip.h>
|
2006-06-25 05:13:17 +00:00
|
|
|
#include <gpxe/pkbuff.h>
|
|
|
|
#include <gpxe/netdevice.h>
|
2006-08-02 00:02:21 +00:00
|
|
|
#include <gpxe/udp.h>
|
2006-06-25 05:13:17 +00:00
|
|
|
|
|
|
|
/** @file
|
|
|
|
*
|
|
|
|
* UDP protocol
|
|
|
|
*/
|
|
|
|
|
2006-08-02 00:02:21 +00:00
|
|
|
struct tcpip_protocol udp_protocol;
|
|
|
|
|
2006-07-19 16:25:23 +00:00
|
|
|
/**
|
|
|
|
* List of registered UDP connections
|
|
|
|
*/
|
|
|
|
static LIST_HEAD ( udp_conns );
|
|
|
|
|
|
|
|
/**
|
2006-08-02 00:02:21 +00:00
|
|
|
* Bind UDP connection to local port
|
|
|
|
*
|
|
|
|
* @v conn UDP connection
|
|
|
|
* @v local_port Local port, in network byte order
|
|
|
|
* @ret rc Return status code
|
2006-07-19 16:25:23 +00:00
|
|
|
*/
|
2006-08-02 00:02:21 +00:00
|
|
|
int udp_bind ( struct udp_connection *conn, uint16_t local_port ) {
|
|
|
|
struct udp_connection *existing;
|
2006-06-28 07:46:28 +00:00
|
|
|
|
2006-08-02 00:02:21 +00:00
|
|
|
list_for_each_entry ( existing, &udp_conns, list ) {
|
|
|
|
if ( existing->local_port == local_port )
|
|
|
|
return -EADDRINUSE;
|
2006-06-28 07:46:28 +00:00
|
|
|
}
|
2006-08-02 00:02:21 +00:00
|
|
|
conn->local_port = local_port;
|
|
|
|
return 0;
|
2006-06-28 07:46:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2006-08-02 00:02:21 +00:00
|
|
|
* Bind UDP connection to all local ports
|
2006-06-28 07:46:28 +00:00
|
|
|
*
|
2006-08-02 00:02:21 +00:00
|
|
|
* @v conn UDP connection
|
|
|
|
*
|
|
|
|
* A promiscuous UDP connection will receive packets with any
|
|
|
|
* destination UDP port. This is required in order to support the PXE
|
|
|
|
* UDP API.
|
|
|
|
*
|
|
|
|
* If the promiscuous connection is not the only UDP connection, the
|
|
|
|
* behaviour is undefined.
|
2006-06-28 07:46:28 +00:00
|
|
|
*/
|
2006-08-02 00:02:21 +00:00
|
|
|
void udp_bind_promisc ( struct udp_connection *conn ) {
|
|
|
|
conn->local_port = 0;
|
2006-06-28 07:46:28 +00:00
|
|
|
}
|
2006-06-25 05:13:17 +00:00
|
|
|
|
|
|
|
/**
|
2006-08-02 00:02:21 +00:00
|
|
|
* Connect UDP connection to remote host and port
|
2006-06-25 05:13:17 +00:00
|
|
|
*
|
2006-08-02 00:02:21 +00:00
|
|
|
* @v conn UDP connection
|
|
|
|
* @v peer Destination socket address
|
2006-06-25 05:13:17 +00:00
|
|
|
*
|
|
|
|
* This function stores the socket address within the connection
|
|
|
|
*/
|
2006-08-02 00:02:21 +00:00
|
|
|
void udp_connect ( struct udp_connection *conn, struct sockaddr_tcpip *peer ) {
|
|
|
|
memcpy ( &conn->peer, peer, sizeof ( conn->peer ) );
|
2006-06-25 05:13:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2006-08-02 00:02:21 +00:00
|
|
|
* Open a local port
|
|
|
|
*
|
|
|
|
* @v conn UDP connection
|
|
|
|
* @v local_port Local port, in network byte order, or zero
|
|
|
|
* @ret rc Return status code
|
2006-06-25 05:13:17 +00:00
|
|
|
*
|
2006-08-02 00:02:21 +00:00
|
|
|
* Opens the UDP connection and binds to a local port. If no local
|
|
|
|
* port is specified, the first available port will be used.
|
2006-06-25 05:13:17 +00:00
|
|
|
*/
|
2006-08-02 00:02:21 +00:00
|
|
|
int udp_open ( struct udp_connection *conn, uint16_t local_port ) {
|
|
|
|
static uint16_t try_port = 1024;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
/* If no port specified, find the first available port */
|
|
|
|
if ( ! local_port ) {
|
|
|
|
for ( ; try_port ; try_port++ ) {
|
|
|
|
if ( try_port < 1024 )
|
|
|
|
continue;
|
|
|
|
if ( udp_open ( conn, htons ( try_port ) ) == 0 )
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return -EADDRINUSE;
|
2006-06-28 07:46:28 +00:00
|
|
|
}
|
2006-08-02 00:02:21 +00:00
|
|
|
|
|
|
|
/* Attempt bind to local port */
|
|
|
|
if ( ( rc = udp_bind ( conn, local_port ) ) != 0 )
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
/* Add to UDP connection list */
|
|
|
|
list_add ( &conn->list, &udp_conns );
|
|
|
|
DBG ( "UDP opened %p on port %d\n", conn, ntohs ( local_port ) );
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Close a UDP connection
|
|
|
|
*
|
|
|
|
* @v conn UDP connection
|
|
|
|
*/
|
|
|
|
void udp_close ( struct udp_connection *conn ) {
|
|
|
|
list_del ( &conn->list );
|
|
|
|
DBG ( "UDP closed %p\n", conn );
|
2006-06-25 05:13:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2006-07-19 16:25:23 +00:00
|
|
|
* User request to send data via a UDP connection
|
|
|
|
*
|
2006-08-02 00:02:21 +00:00
|
|
|
* @v conn UDP connection
|
2006-07-19 16:25:23 +00:00
|
|
|
*
|
|
|
|
* This function allocates buffer space and invokes the function's senddata()
|
|
|
|
* callback. The callback may use the buffer space
|
|
|
|
*/
|
|
|
|
int udp_senddata ( struct udp_connection *conn ) {
|
2006-07-19 16:27:54 +00:00
|
|
|
conn->tx_pkb = alloc_pkb ( UDP_MAX_TXPKB );
|
2006-07-19 16:25:23 +00:00
|
|
|
if ( conn->tx_pkb == NULL ) {
|
2006-08-02 00:02:21 +00:00
|
|
|
DBG ( "UDP %p cannot allocate packet buffer of length %d\n",
|
|
|
|
conn, UDP_MAX_TXPKB );
|
2006-07-19 16:25:23 +00:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
2006-07-19 18:21:30 +00:00
|
|
|
pkb_reserve ( conn->tx_pkb, UDP_MAX_HLEN );
|
2006-07-19 20:16:46 +00:00
|
|
|
conn->udp_op->senddata ( conn, conn->tx_pkb->data,
|
2006-07-19 18:21:30 +00:00
|
|
|
pkb_available ( conn->tx_pkb ) );
|
2006-07-19 16:25:23 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2006-07-19 20:38:49 +00:00
|
|
|
* Transmit data via a UDP connection to a specified address
|
2006-06-25 05:13:17 +00:00
|
|
|
*
|
2006-08-02 00:02:21 +00:00
|
|
|
* @v conn UDP connection
|
|
|
|
* @v peer Destination address
|
|
|
|
* @v data Data to send
|
|
|
|
* @v len Length of data
|
|
|
|
* @ret rc Return status code
|
2006-06-25 05:13:17 +00:00
|
|
|
*
|
2006-08-02 00:02:21 +00:00
|
|
|
* This function fills up the UDP headers and sends the data. It may
|
|
|
|
* be called only from within the context of an application's
|
|
|
|
* senddata() method; if the application wishes to send data it must
|
|
|
|
* call udp_senddata() and wait for its senddata() method to be
|
|
|
|
* called.
|
2006-06-25 05:13:17 +00:00
|
|
|
*/
|
2006-08-02 00:02:21 +00:00
|
|
|
int udp_sendto ( struct udp_connection *conn, struct sockaddr_tcpip *peer,
|
2006-07-19 20:38:49 +00:00
|
|
|
const void *data, size_t len ) {
|
2006-08-02 00:02:21 +00:00
|
|
|
struct udp_header *udphdr;
|
|
|
|
|
|
|
|
/* Avoid overflowing TX buffer */
|
|
|
|
if ( len > pkb_available ( conn->tx_pkb ) )
|
|
|
|
len = pkb_available ( conn->tx_pkb );
|
2006-06-28 07:46:28 +00:00
|
|
|
|
2006-07-19 18:21:30 +00:00
|
|
|
/* Copy payload */
|
|
|
|
memmove ( pkb_put ( conn->tx_pkb, len ), data, len );
|
2006-06-25 05:13:17 +00:00
|
|
|
|
2006-06-28 07:46:28 +00:00
|
|
|
/*
|
|
|
|
* Add the UDP header
|
|
|
|
*
|
|
|
|
* Covert all 16- and 32- bit integers into network btye order before
|
|
|
|
* sending it over the network
|
|
|
|
*/
|
|
|
|
udphdr = pkb_push ( conn->tx_pkb, sizeof ( *udphdr ) );
|
2006-08-02 00:02:21 +00:00
|
|
|
udphdr->dest_port = peer->st_port;
|
2006-06-30 08:52:03 +00:00
|
|
|
udphdr->source_port = conn->local_port;
|
2006-06-28 07:46:28 +00:00
|
|
|
udphdr->len = htons ( pkb_len ( conn->tx_pkb ) );
|
2006-07-19 23:38:05 +00:00
|
|
|
udphdr->chksum = 0;
|
|
|
|
udphdr->chksum = tcpip_chksum ( udphdr, sizeof ( *udphdr ) + len );
|
2006-06-28 07:46:28 +00:00
|
|
|
|
2006-08-02 00:02:21 +00:00
|
|
|
/* Dump debugging information */
|
|
|
|
DBG ( "UDP %p transmitting %p+%#zx len %#x src %d dest %d "
|
|
|
|
"chksum %#04x\n", conn, conn->tx_pkb->data,
|
|
|
|
pkb_len ( conn->tx_pkb ), ntohs ( udphdr->len ),
|
|
|
|
ntohs ( udphdr->source_port ), ntohs ( udphdr->dest_port ),
|
|
|
|
ntohs ( udphdr->chksum ) );
|
2006-06-28 07:46:28 +00:00
|
|
|
|
|
|
|
/* Send it to the next layer for processing */
|
2006-08-01 14:22:04 +00:00
|
|
|
return tcpip_tx ( conn->tx_pkb, &udp_protocol, peer );
|
2006-06-25 05:13:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2006-08-02 00:02:21 +00:00
|
|
|
* Transmit data via a UDP connection
|
2006-06-25 05:13:17 +00:00
|
|
|
*
|
|
|
|
* @v conn UDP connection
|
2006-08-02 00:02:21 +00:00
|
|
|
* @v data Data to send
|
|
|
|
* @v len Length of data
|
|
|
|
* @ret rc Return status code
|
2006-06-25 05:13:17 +00:00
|
|
|
*
|
2006-08-02 00:02:21 +00:00
|
|
|
* This function fills up the UDP headers and sends the data. It may
|
|
|
|
* be called only from within the context of an application's
|
|
|
|
* senddata() method; if the application wishes to send data it must
|
|
|
|
* call udp_senddata() and wait for its senddata() method to be
|
|
|
|
* called.
|
2006-06-25 05:13:17 +00:00
|
|
|
*/
|
2006-08-02 00:02:21 +00:00
|
|
|
int udp_send ( struct udp_connection *conn, const void *data, size_t len ) {
|
|
|
|
return udp_sendto ( conn, &conn->peer, data, len );
|
2006-06-25 05:13:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Process a received packet
|
|
|
|
*
|
2006-08-02 00:02:21 +00:00
|
|
|
* @v pkb Packet buffer
|
|
|
|
* @v st_src Partially-filled source address
|
|
|
|
* @v st_dest Partially-filled destination address
|
|
|
|
* @ret rc Return status code
|
2006-06-25 05:13:17 +00:00
|
|
|
*/
|
2006-08-02 00:02:21 +00:00
|
|
|
static int udp_rx ( struct pk_buff *pkb, struct sockaddr_tcpip *st_src,
|
|
|
|
struct sockaddr_tcpip *st_dest ) {
|
2006-06-28 07:46:28 +00:00
|
|
|
struct udp_header *udphdr = pkb->data;
|
|
|
|
struct udp_connection *conn;
|
2006-08-02 00:02:21 +00:00
|
|
|
unsigned int ulen;
|
2006-06-28 07:46:28 +00:00
|
|
|
uint16_t chksum;
|
|
|
|
|
2006-08-02 00:02:21 +00:00
|
|
|
/* Sanity check */
|
2006-06-28 07:46:28 +00:00
|
|
|
if ( pkb_len ( pkb ) < sizeof ( *udphdr ) ) {
|
2006-08-02 00:02:21 +00:00
|
|
|
DBG ( "UDP received underlength packet %p+%#zx\n",
|
|
|
|
pkb->data, pkb_len ( pkb ) );
|
|
|
|
return -EINVAL;
|
2006-06-28 07:46:28 +00:00
|
|
|
}
|
|
|
|
|
2006-08-02 00:02:21 +00:00
|
|
|
/* Dump debugging information */
|
|
|
|
DBG ( "UDP received %p+%#zx len %#x src %d dest %d chksum %#04x\n",
|
|
|
|
pkb->data, pkb_len ( pkb ), ntohs ( udphdr->len ),
|
|
|
|
ntohs ( udphdr->source_port ), ntohs ( udphdr->dest_port ),
|
|
|
|
ntohs ( udphdr->chksum ) );
|
|
|
|
|
|
|
|
/* Check length and trim any excess */
|
2006-06-28 07:46:28 +00:00
|
|
|
ulen = ntohs ( udphdr->len );
|
2006-08-02 00:02:21 +00:00
|
|
|
if ( ulen > pkb_len ( pkb ) ) {
|
|
|
|
DBG ( "UDP received truncated packet %p+%#zx\n",
|
|
|
|
pkb->data, pkb_len ( pkb ) );
|
|
|
|
return -EINVAL;
|
2006-06-28 07:46:28 +00:00
|
|
|
}
|
2006-08-02 00:02:21 +00:00
|
|
|
pkb_unput ( pkb, ( pkb_len ( pkb ) - ulen ) );
|
2006-06-28 07:46:28 +00:00
|
|
|
|
|
|
|
/* Verify the checksum */
|
2006-07-20 00:03:01 +00:00
|
|
|
#warning "Don't we need to take the pseudo-header into account here?"
|
|
|
|
#if 0
|
2006-07-19 23:38:05 +00:00
|
|
|
chksum = tcpip_chksum ( pkb->data, pkb_len ( pkb ) );
|
2006-06-28 07:46:28 +00:00
|
|
|
if ( chksum != 0xffff ) {
|
2006-07-20 00:03:01 +00:00
|
|
|
DBG ( "Bad checksum %#x\n", chksum );
|
2006-08-02 00:02:21 +00:00
|
|
|
return -EINVAL;
|
2006-06-28 07:46:28 +00:00
|
|
|
}
|
2006-07-20 00:03:01 +00:00
|
|
|
#endif
|
2006-06-28 07:46:28 +00:00
|
|
|
|
2006-08-02 00:02:21 +00:00
|
|
|
/* Complete the socket addresses */
|
|
|
|
st_src->st_port = udphdr->source_port;
|
|
|
|
st_dest->st_port = udphdr->dest_port;
|
2006-06-28 07:46:28 +00:00
|
|
|
|
|
|
|
/* Demux the connection */
|
|
|
|
list_for_each_entry ( conn, &udp_conns, list ) {
|
2006-08-02 00:02:21 +00:00
|
|
|
if ( conn->local_port &&
|
|
|
|
( conn->local_port != udphdr->dest_port ) ) {
|
|
|
|
/* Bound to local port and local port doesn't match */
|
|
|
|
continue;
|
2006-06-28 07:46:28 +00:00
|
|
|
}
|
2006-08-02 00:02:21 +00:00
|
|
|
if ( conn->peer.st_family &&
|
|
|
|
( memcmp ( &conn->peer, st_src,
|
|
|
|
sizeof ( conn->peer ) ) != 0 ) ) {
|
|
|
|
/* Connected to remote port and remote port
|
|
|
|
* doesn't match
|
|
|
|
*/
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Strip off the UDP header */
|
|
|
|
pkb_pull ( pkb, sizeof ( *udphdr ) );
|
2006-06-28 07:46:28 +00:00
|
|
|
|
2006-08-02 00:02:21 +00:00
|
|
|
DBG ( "UDP delivering to %p\n", conn );
|
|
|
|
|
|
|
|
/* Call the application's callback */
|
|
|
|
return conn->udp_op->newdata ( conn, pkb->data, pkb_len( pkb ),
|
|
|
|
st_src, st_dest );
|
|
|
|
}
|
2006-06-28 07:46:28 +00:00
|
|
|
|
2006-08-02 00:02:21 +00:00
|
|
|
DBG ( "No UDP connection listening on port %d\n",
|
|
|
|
ntohs ( udphdr->dest_port ) );
|
|
|
|
return 0;
|
2006-06-25 05:13:17 +00:00
|
|
|
}
|
|
|
|
|
2006-06-28 15:43:08 +00:00
|
|
|
struct tcpip_protocol udp_protocol = {
|
2006-06-28 07:46:28 +00:00
|
|
|
.name = "UDP",
|
|
|
|
.rx = udp_rx,
|
2006-08-02 00:02:21 +00:00
|
|
|
.tcpip_proto = IP_UDP,
|
2006-06-28 15:43:08 +00:00
|
|
|
.csum_offset = 6,
|
2006-06-25 05:13:17 +00:00
|
|
|
};
|
|
|
|
|
2006-06-28 15:43:08 +00:00
|
|
|
TCPIP_PROTOCOL ( udp_protocol );
|