2006-06-25 05:13:17 +00:00
|
|
|
#include <stdint.h>
|
2007-06-20 00:13:35 +00:00
|
|
|
#include <stdlib.h>
|
2006-06-25 05:13:17 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <byteswap.h>
|
|
|
|
#include <errno.h>
|
2006-08-02 00:02:21 +00:00
|
|
|
#include <gpxe/tcpip.h>
|
2007-05-19 18:39:40 +00:00
|
|
|
#include <gpxe/iobuf.h>
|
2007-06-20 00:13:35 +00:00
|
|
|
#include <gpxe/xfer.h>
|
|
|
|
#include <gpxe/open.h>
|
|
|
|
#include <gpxe/uri.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-07-19 16:25:23 +00:00
|
|
|
/**
|
2007-06-20 00:13:35 +00:00
|
|
|
* A UDP connection
|
|
|
|
*
|
2006-07-19 16:25:23 +00:00
|
|
|
*/
|
2007-06-20 00:13:35 +00:00
|
|
|
struct udp_connection {
|
|
|
|
/** Reference counter */
|
|
|
|
struct refcnt refcnt;
|
|
|
|
/** List of UDP connections */
|
|
|
|
struct list_head list;
|
|
|
|
|
|
|
|
/** Data transfer interface */
|
|
|
|
struct xfer_interface xfer;
|
|
|
|
|
|
|
|
/** Remote socket address */
|
|
|
|
struct sockaddr_tcpip peer;
|
|
|
|
/** Local port on which the connection receives packets */
|
|
|
|
unsigned int local_port;
|
|
|
|
};
|
2006-07-19 16:25:23 +00:00
|
|
|
|
|
|
|
/**
|
2007-06-20 00:13:35 +00:00
|
|
|
* List of registered UDP connections
|
2006-07-19 16:25:23 +00:00
|
|
|
*/
|
2007-06-20 00:13:35 +00:00
|
|
|
static LIST_HEAD ( udp_conns );
|
2006-06-28 07:46:28 +00:00
|
|
|
|
2007-06-20 00:13:35 +00:00
|
|
|
/* Forward declatations */
|
|
|
|
static struct xfer_interface_operations udp_xfer_operations;
|
|
|
|
struct tcpip_protocol udp_protocol;
|
2006-06-28 07:46:28 +00:00
|
|
|
|
2006-06-25 05:13:17 +00:00
|
|
|
/**
|
2007-06-20 00:13:35 +00:00
|
|
|
* Bind UDP connection to local port
|
2006-08-02 00:02:21 +00:00
|
|
|
*
|
2007-06-20 00:13:35 +00:00
|
|
|
* @v udp UDP connection
|
|
|
|
* @v port Local port, in network byte order, or zero
|
2006-08-02 00:02:21 +00:00
|
|
|
* @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
|
|
|
*/
|
2007-06-20 00:13:35 +00:00
|
|
|
static int udp_bind ( struct udp_connection *udp, unsigned int port ) {
|
|
|
|
struct udp_connection *existing;
|
2006-08-02 00:02:21 +00:00
|
|
|
static uint16_t try_port = 1024;
|
|
|
|
|
|
|
|
/* If no port specified, find the first available port */
|
2007-06-20 00:13:35 +00:00
|
|
|
if ( ! port ) {
|
2006-08-02 00:02:21 +00:00
|
|
|
for ( ; try_port ; try_port++ ) {
|
|
|
|
if ( try_port < 1024 )
|
|
|
|
continue;
|
2007-06-20 00:13:35 +00:00
|
|
|
if ( udp_bind ( udp, htons ( try_port ) ) == 0 )
|
2006-08-02 00:02:21 +00:00
|
|
|
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 */
|
2007-06-20 00:13:35 +00:00
|
|
|
list_for_each_entry ( existing, &udp_conns, list ) {
|
|
|
|
if ( existing->local_port == port ) {
|
|
|
|
DBGC ( udp, "UDP %p could not bind: port %d in use\n",
|
|
|
|
udp, ntohs ( port ) );
|
|
|
|
return -EADDRINUSE;
|
|
|
|
}
|
2007-01-16 03:19:40 +00:00
|
|
|
}
|
2007-06-20 00:13:35 +00:00
|
|
|
udp->local_port = port;
|
2006-08-02 00:02:21 +00:00
|
|
|
|
|
|
|
/* Add to UDP connection list */
|
2007-06-20 00:13:35 +00:00
|
|
|
DBGC ( udp, "UDP %p bound to port %d\n", udp, ntohs ( port ) );
|
2006-08-02 00:02:21 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2007-06-20 00:13:35 +00:00
|
|
|
* Open a UDP connection
|
2006-08-02 00:02:21 +00:00
|
|
|
*
|
2007-06-20 00:13:35 +00:00
|
|
|
* @v xfer Data transfer interface
|
|
|
|
* @v peer Peer socket address
|
|
|
|
* @v local Local socket address, or NULL
|
|
|
|
* @v promisc Socket is promiscuous
|
|
|
|
* @ret rc Return status code
|
2006-08-02 00:02:21 +00:00
|
|
|
*/
|
2007-06-20 00:13:35 +00:00
|
|
|
static int udp_open_common ( struct xfer_interface *xfer,
|
|
|
|
struct sockaddr *peer, struct sockaddr *local,
|
|
|
|
int promisc ) {
|
|
|
|
struct sockaddr_tcpip *st_peer = ( struct sockaddr_tcpip * ) peer;
|
|
|
|
struct sockaddr_tcpip *st_local = ( struct sockaddr_tcpip * ) local;
|
|
|
|
struct udp_connection *udp;
|
|
|
|
unsigned int bind_port;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
/* Allocate and initialise structure */
|
|
|
|
udp = malloc ( sizeof ( *udp ) );
|
|
|
|
if ( ! udp )
|
|
|
|
return -ENOMEM;
|
|
|
|
DBGC ( udp, "UDP %p allocated\n", udp );
|
|
|
|
memset ( udp, 0, sizeof ( *udp ) );
|
|
|
|
xfer_init ( &udp->xfer, &udp_xfer_operations, &udp->refcnt );
|
|
|
|
memcpy ( &udp->peer, st_peer, sizeof ( udp->peer ) );
|
|
|
|
|
|
|
|
/* Bind to local port */
|
|
|
|
if ( ! promisc ) {
|
|
|
|
bind_port = ( st_local ? st_local->st_port : 0 );
|
|
|
|
if ( ( rc = udp_bind ( udp, bind_port ) ) != 0 )
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Attach parent interface, transfer reference to connection
|
|
|
|
* list and return
|
|
|
|
*/
|
|
|
|
xfer_plug_plug ( &udp->xfer, xfer );
|
|
|
|
list_add ( &udp->list, &udp_conns );
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
err:
|
|
|
|
ref_put ( &udp->refcnt );
|
|
|
|
return rc;
|
2006-06-25 05:13:17 +00:00
|
|
|
}
|
|
|
|
|
2007-01-10 02:46:39 +00:00
|
|
|
/**
|
2007-06-20 00:13:35 +00:00
|
|
|
* Open a UDP connection
|
2007-01-10 02:46:39 +00:00
|
|
|
*
|
2007-06-20 00:13:35 +00:00
|
|
|
* @v xfer Data transfer interface
|
|
|
|
* @v peer Peer socket address
|
|
|
|
* @v local Local socket address, or NULL
|
|
|
|
* @ret rc Return status code
|
2007-01-10 02:46:39 +00:00
|
|
|
*/
|
2007-06-20 00:13:35 +00:00
|
|
|
int udp_open ( struct xfer_interface *xfer, struct sockaddr *peer,
|
|
|
|
struct sockaddr *local ) {
|
|
|
|
return udp_open_common ( xfer, peer, local, 0 );
|
2007-01-10 02:46:39 +00:00
|
|
|
}
|
|
|
|
|
2006-06-25 05:13:17 +00:00
|
|
|
/**
|
2007-06-20 00:13:35 +00:00
|
|
|
* Open a promiscuous UDP connection
|
2006-07-19 16:25:23 +00:00
|
|
|
*
|
2007-06-20 00:13:35 +00:00
|
|
|
* @v xfer Data transfer interface
|
|
|
|
* @ret rc Return status code
|
2006-07-19 16:25:23 +00:00
|
|
|
*
|
2007-06-20 00:13:35 +00:00
|
|
|
* Promiscuous UDP connections are required in order to support the
|
|
|
|
* PXE API.
|
2006-07-19 16:25:23 +00:00
|
|
|
*/
|
2007-06-20 00:13:35 +00:00
|
|
|
int udp_open_promisc ( struct xfer_interface *xfer ) {
|
|
|
|
return udp_open_common ( xfer, NULL, NULL, 1 );
|
|
|
|
}
|
2006-08-09 01:24:32 +00:00
|
|
|
|
2007-06-20 00:13:35 +00:00
|
|
|
/**
|
|
|
|
* Close a UDP connection
|
|
|
|
*
|
|
|
|
* @v udp UDP connection
|
|
|
|
* @v rc Reason for close
|
|
|
|
*/
|
|
|
|
static void udp_close ( struct udp_connection *udp, int rc ) {
|
2007-01-10 02:46:39 +00:00
|
|
|
|
2007-06-20 00:13:35 +00:00
|
|
|
/* Close data transfer interface */
|
|
|
|
xfer_nullify ( &udp->xfer );
|
|
|
|
xfer_close ( &udp->xfer, rc );
|
2007-01-10 02:46:39 +00:00
|
|
|
|
2007-06-20 00:13:35 +00:00
|
|
|
/* Remove from list of connections and drop list's reference */
|
|
|
|
list_del ( &udp->list );
|
|
|
|
ref_put ( &udp->refcnt );
|
2007-01-10 02:46:39 +00:00
|
|
|
|
2007-06-20 00:13:35 +00:00
|
|
|
DBGC ( udp, "UDP %p closed\n", udp );
|
2006-07-19 16:25:23 +00:00
|
|
|
}
|
2007-06-20 00:13:35 +00:00
|
|
|
|
2006-07-19 16:25:23 +00:00
|
|
|
/**
|
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
|
|
|
*
|
2007-06-20 00:13:35 +00:00
|
|
|
* @v udp UDP connection
|
|
|
|
* @v iobuf I/O buffer
|
|
|
|
* @v src_port Source port, or 0 to use default
|
|
|
|
* @v dest Destination address, or NULL to use default
|
2006-08-02 00:02:21 +00:00
|
|
|
* @ret rc Return status code
|
2006-06-25 05:13:17 +00:00
|
|
|
*/
|
2007-06-20 00:13:35 +00:00
|
|
|
static int udp_tx ( struct udp_connection *udp, struct io_buffer *iobuf,
|
|
|
|
unsigned int src_port, struct sockaddr_tcpip *dest ) {
|
2006-08-02 00:02:21 +00:00
|
|
|
struct udp_header *udphdr;
|
2007-06-20 00:13:35 +00:00
|
|
|
struct net_device *netdev = NULL;
|
|
|
|
size_t len;
|
2007-01-16 03:19:40 +00:00
|
|
|
int rc;
|
2006-08-09 01:24:32 +00:00
|
|
|
|
2007-06-20 00:13:35 +00:00
|
|
|
#warning "netdev?"
|
2006-08-02 00:02:21 +00:00
|
|
|
|
2007-06-20 00:13:35 +00:00
|
|
|
/* Check we can accommodate the header */
|
|
|
|
if ( ( rc = iob_ensure_headroom ( iobuf, UDP_MAX_HLEN ) ) != 0 ) {
|
|
|
|
free_iob ( iobuf );
|
|
|
|
return rc;
|
|
|
|
}
|
2006-06-28 07:46:28 +00:00
|
|
|
|
2007-06-20 00:13:35 +00:00
|
|
|
/* Fill in default values if not explicitly provided */
|
|
|
|
if ( ! src_port )
|
|
|
|
src_port = udp->local_port;
|
|
|
|
if ( ! dest )
|
|
|
|
dest = &udp->peer;
|
2006-06-25 05:13:17 +00:00
|
|
|
|
2007-06-20 00:13:35 +00:00
|
|
|
/* Add the UDP header */
|
2007-05-19 18:39:40 +00:00
|
|
|
udphdr = iob_push ( iobuf, sizeof ( *udphdr ) );
|
2007-06-20 00:13:35 +00:00
|
|
|
len = iob_len ( iobuf );
|
|
|
|
udphdr->dest = dest->st_port;
|
|
|
|
udphdr->src = src_port;
|
|
|
|
udphdr->len = htons ( len );
|
2006-07-19 23:38:05 +00:00
|
|
|
udphdr->chksum = 0;
|
2007-06-20 00:13:35 +00:00
|
|
|
udphdr->chksum = tcpip_chksum ( udphdr, len );
|
2006-06-28 07:46:28 +00:00
|
|
|
|
2006-08-02 00:02:21 +00:00
|
|
|
/* Dump debugging information */
|
2007-06-20 00:13:35 +00:00
|
|
|
DBGC ( udp, "UDP %p TX %d->%d len %zd\n", udp,
|
|
|
|
ntohs ( udphdr->src ), ntohs ( udphdr->dest ),
|
2007-01-03 21:13:11 +00:00
|
|
|
ntohs ( udphdr->len ) );
|
2006-06-28 07:46:28 +00:00
|
|
|
|
|
|
|
/* Send it to the next layer for processing */
|
2007-06-20 00:13:35 +00:00
|
|
|
if ( ( rc = tcpip_tx ( iobuf, &udp_protocol, dest, netdev,
|
2007-01-16 03:19:40 +00:00
|
|
|
&udphdr->chksum ) ) != 0 ) {
|
2007-06-20 00:13:35 +00:00
|
|
|
DBGC ( udp, "UDP %p could not transmit packet: %s\n",
|
|
|
|
udp, strerror ( rc ) );
|
2007-01-16 03:19:40 +00:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2007-01-10 02:31:38 +00:00
|
|
|
}
|
|
|
|
|
2007-01-03 21:13:11 +00:00
|
|
|
/**
|
|
|
|
* Identify UDP connection by local port number
|
|
|
|
*
|
|
|
|
* @v local_port Local port (in network-endian order)
|
2007-06-20 00:13:35 +00:00
|
|
|
* @ret udp UDP connection, or NULL
|
2007-01-03 21:13:11 +00:00
|
|
|
*/
|
2007-06-20 00:13:35 +00:00
|
|
|
static struct udp_connection * udp_demux ( unsigned int local_port ) {
|
|
|
|
struct udp_connection *udp;
|
2007-01-03 21:13:11 +00:00
|
|
|
|
2007-06-20 00:13:35 +00:00
|
|
|
list_for_each_entry ( udp, &udp_conns, list ) {
|
|
|
|
if ( ( udp->local_port == local_port ) ||
|
|
|
|
( udp->local_port == 0 ) ) {
|
|
|
|
return udp;
|
2007-01-03 21:13:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2006-06-25 05:13:17 +00:00
|
|
|
/**
|
|
|
|
* Process a received packet
|
|
|
|
*
|
2007-05-19 18:39:40 +00:00
|
|
|
* @v iobuf I/O buffer
|
2006-08-02 00:02:21 +00:00
|
|
|
* @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-25 05:13:17 +00:00
|
|
|
*/
|
2007-05-19 18:39:40 +00:00
|
|
|
static int udp_rx ( struct io_buffer *iobuf, struct sockaddr_tcpip *st_src,
|
2007-01-03 20:48:52 +00:00
|
|
|
struct sockaddr_tcpip *st_dest, uint16_t pshdr_csum ) {
|
2007-05-19 18:39:40 +00:00
|
|
|
struct udp_header *udphdr = iobuf->data;
|
2007-06-20 00:13:35 +00:00
|
|
|
struct udp_connection *udp;
|
|
|
|
struct xfer_metadata meta;
|
2007-01-03 21:13:11 +00:00
|
|
|
size_t ulen;
|
2007-06-20 00:13:35 +00:00
|
|
|
unsigned int csum;
|
2007-04-09 17:34:10 +00:00
|
|
|
int rc = 0;
|
2006-06-28 07:46:28 +00:00
|
|
|
|
2007-01-03 21:13:11 +00:00
|
|
|
/* Sanity check packet */
|
2007-05-19 18:39:40 +00:00
|
|
|
if ( iob_len ( iobuf ) < sizeof ( *udphdr ) ) {
|
2007-01-03 21:13:11 +00:00
|
|
|
DBG ( "UDP packet too short at %d bytes (min %d bytes)\n",
|
2007-05-19 18:39:40 +00:00
|
|
|
iob_len ( iobuf ), sizeof ( *udphdr ) );
|
2007-01-03 21:13:11 +00:00
|
|
|
|
2006-08-09 01:24:32 +00:00
|
|
|
rc = -EINVAL;
|
|
|
|
goto done;
|
2006-06-28 07:46:28 +00:00
|
|
|
}
|
|
|
|
ulen = ntohs ( udphdr->len );
|
2007-01-03 21:13:11 +00:00
|
|
|
if ( ulen < sizeof ( *udphdr ) ) {
|
|
|
|
DBG ( "UDP length too short at %d bytes "
|
|
|
|
"(header is %d bytes)\n", ulen, sizeof ( *udphdr ) );
|
|
|
|
rc = -EINVAL;
|
|
|
|
goto done;
|
|
|
|
}
|
2007-05-19 18:39:40 +00:00
|
|
|
if ( ulen > iob_len ( iobuf ) ) {
|
2007-01-03 21:13:11 +00:00
|
|
|
DBG ( "UDP length too long at %d bytes (packet is %d bytes)\n",
|
2007-05-19 18:39:40 +00:00
|
|
|
ulen, iob_len ( iobuf ) );
|
2006-08-09 01:24:32 +00:00
|
|
|
rc = -EINVAL;
|
|
|
|
goto done;
|
2006-06-28 07:46:28 +00:00
|
|
|
}
|
2007-01-04 05:17:28 +00:00
|
|
|
if ( udphdr->chksum ) {
|
2007-05-19 18:39:40 +00:00
|
|
|
csum = tcpip_continue_chksum ( pshdr_csum, iobuf->data, ulen );
|
2007-01-04 05:17:28 +00:00
|
|
|
if ( csum != 0 ) {
|
|
|
|
DBG ( "UDP checksum incorrect (is %04x including "
|
|
|
|
"checksum field, should be 0000)\n", csum );
|
|
|
|
rc = -EINVAL;
|
|
|
|
goto done;
|
|
|
|
}
|
2006-06-28 07:46:28 +00:00
|
|
|
}
|
|
|
|
|
2007-01-03 21:13:11 +00:00
|
|
|
/* Parse parameters from header and strip header */
|
2007-06-20 00:13:35 +00:00
|
|
|
st_src->st_port = udphdr->src;
|
|
|
|
st_dest->st_port = udphdr->dest;
|
|
|
|
udp = udp_demux ( udphdr->dest );
|
2007-05-19 18:39:40 +00:00
|
|
|
iob_unput ( iobuf, ( iob_len ( iobuf ) - ulen ) );
|
|
|
|
iob_pull ( iobuf, sizeof ( *udphdr ) );
|
2006-06-28 07:46:28 +00:00
|
|
|
|
2007-01-03 21:13:11 +00:00
|
|
|
/* Dump debugging information */
|
2007-06-20 00:13:35 +00:00
|
|
|
DBGC ( udp, "UDP %p RX %d<-%d len %zd\n", udp,
|
|
|
|
ntohs ( udphdr->dest ), ntohs ( udphdr->src ), ulen );
|
2006-06-28 07:46:28 +00:00
|
|
|
|
2007-01-03 21:13:11 +00:00
|
|
|
/* Ignore if no matching connection found */
|
2007-06-20 00:13:35 +00:00
|
|
|
if ( ! udp ) {
|
2007-01-03 21:13:11 +00:00
|
|
|
DBG ( "No UDP connection listening on port %d\n",
|
2007-06-20 00:13:35 +00:00
|
|
|
ntohs ( udphdr->dest ) );
|
2007-01-03 21:13:11 +00:00
|
|
|
rc = -ENOTCONN;
|
2006-08-09 01:24:32 +00:00
|
|
|
goto done;
|
2006-08-02 00:02:21 +00:00
|
|
|
}
|
2006-06-28 07:46:28 +00:00
|
|
|
|
2007-01-03 21:13:11 +00:00
|
|
|
/* Pass data to application */
|
2007-06-20 00:13:35 +00:00
|
|
|
memset ( &meta, 0, sizeof ( meta ) );
|
|
|
|
meta.src = ( struct sockaddr * ) st_src;
|
|
|
|
meta.dest = ( struct sockaddr * ) st_dest;
|
|
|
|
rc = xfer_deliver_iob_meta ( &udp->xfer, iobuf, &meta );
|
|
|
|
iobuf = NULL;
|
2006-08-09 01:24:32 +00:00
|
|
|
|
|
|
|
done:
|
2007-05-19 18:39:40 +00:00
|
|
|
free_iob ( iobuf );
|
2006-08-09 01:24:32 +00:00
|
|
|
return rc;
|
2006-06-25 05:13:17 +00:00
|
|
|
}
|
|
|
|
|
2006-08-09 04:42:14 +00:00
|
|
|
struct tcpip_protocol udp_protocol __tcpip_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-25 05:13:17 +00:00
|
|
|
};
|
2007-06-20 00:13:35 +00:00
|
|
|
|
|
|
|
/***************************************************************************
|
|
|
|
*
|
|
|
|
* Data transfer interface
|
|
|
|
*
|
|
|
|
***************************************************************************
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Close interface
|
|
|
|
*
|
|
|
|
* @v xfer Data transfer interface
|
|
|
|
* @v rc Reason for close
|
|
|
|
*/
|
|
|
|
static void udp_xfer_close ( struct xfer_interface *xfer, int rc ) {
|
|
|
|
struct udp_connection *udp =
|
|
|
|
container_of ( xfer, struct udp_connection, xfer );
|
|
|
|
|
|
|
|
/* Close connection */
|
|
|
|
udp_close ( udp, rc );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Allocate I/O buffer for UDP
|
|
|
|
*
|
|
|
|
* @v xfer Data transfer interface
|
|
|
|
* @v len Payload size
|
|
|
|
* @ret iobuf I/O buffer, or NULL
|
|
|
|
*/
|
|
|
|
static struct io_buffer * udp_alloc_iob ( struct xfer_interface *xfer,
|
|
|
|
size_t len ) {
|
|
|
|
struct udp_connection *udp =
|
|
|
|
container_of ( xfer, struct udp_connection, xfer );
|
|
|
|
struct io_buffer *iobuf;
|
|
|
|
|
|
|
|
iobuf = alloc_iob ( UDP_MAX_HLEN + len );
|
|
|
|
if ( ! iobuf ) {
|
|
|
|
DBGC ( udp, "UDP %p cannot allocate buffer of length %d\n",
|
|
|
|
udp, len );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
iob_reserve ( iobuf, UDP_MAX_HLEN );
|
|
|
|
return iobuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deliver datagram as I/O buffer
|
|
|
|
*
|
|
|
|
* @v xfer Data transfer interface
|
|
|
|
* @v iobuf Datagram I/O buffer
|
|
|
|
* @v meta Data transfer metadata, or NULL
|
|
|
|
* @ret rc Return status code
|
|
|
|
*/
|
|
|
|
static int udp_xfer_deliver_iob ( struct xfer_interface *xfer,
|
|
|
|
struct io_buffer *iobuf,
|
|
|
|
struct xfer_metadata *meta ) {
|
|
|
|
struct udp_connection *udp =
|
|
|
|
container_of ( xfer, struct udp_connection, xfer );
|
|
|
|
struct sockaddr_tcpip *src;
|
|
|
|
struct sockaddr_tcpip *dest = NULL;
|
|
|
|
unsigned int src_port = 0;
|
|
|
|
|
|
|
|
/* Apply xfer metadata */
|
|
|
|
if ( meta ) {
|
|
|
|
src = ( struct sockaddr_tcpip * ) meta->src;
|
|
|
|
if ( src )
|
|
|
|
src_port = src->st_port;
|
|
|
|
dest = ( struct sockaddr_tcpip * ) meta->dest;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Transmit data, if possible */
|
|
|
|
udp_tx ( udp, iobuf, src_port, dest );
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** UDP data transfer interface operations */
|
|
|
|
static struct xfer_interface_operations udp_xfer_operations = {
|
|
|
|
.close = udp_xfer_close,
|
|
|
|
.vredirect = ignore_xfer_vredirect,
|
|
|
|
.request = ignore_xfer_request,
|
|
|
|
.seek = ignore_xfer_seek,
|
|
|
|
.alloc_iob = udp_alloc_iob,
|
|
|
|
.deliver_iob = udp_xfer_deliver_iob,
|
|
|
|
.deliver_raw = xfer_deliver_as_iob,
|
|
|
|
};
|
|
|
|
|
|
|
|
/***************************************************************************
|
|
|
|
*
|
|
|
|
* Openers
|
|
|
|
*
|
|
|
|
***************************************************************************
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** UDP socket opener */
|
|
|
|
struct socket_opener udp_socket_opener __socket_opener = {
|
|
|
|
.semantics = SOCK_DGRAM,
|
|
|
|
.family = AF_INET,
|
|
|
|
.open = udp_open,
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Open UDP URI
|
|
|
|
*
|
|
|
|
* @v xfer Data transfer interface
|
|
|
|
* @v uri URI
|
|
|
|
* @ret rc Return status code
|
|
|
|
*/
|
|
|
|
static int udp_open_uri ( struct xfer_interface *xfer, struct uri *uri ) {
|
|
|
|
struct sockaddr_tcpip peer;
|
|
|
|
|
|
|
|
/* Sanity check */
|
|
|
|
if ( ! uri->host )
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
memset ( &peer, 0, sizeof ( peer ) );
|
|
|
|
peer.st_port = htons ( uri_port ( uri, 0 ) );
|
|
|
|
return xfer_open_named_socket ( xfer, SOCK_DGRAM,
|
|
|
|
( struct sockaddr * ) &peer,
|
|
|
|
uri->host, NULL );
|
|
|
|
}
|
|
|
|
|
|
|
|
/** UDP URI opener */
|
|
|
|
struct uri_opener udp_uri_opener __uri_opener = {
|
|
|
|
.scheme = "udp",
|
|
|
|
.open = udp_open_uri,
|
|
|
|
};
|