2013-08-26 15:23:54 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2013 Michael Brown <mbrown@fensystems.co.uk>.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2 of the
|
|
|
|
* License, or any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
|
|
|
* 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
FILE_LICENCE ( GPL2_OR_LATER );
|
|
|
|
|
2006-08-19 17:58:22 +02:00
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
2013-08-26 15:23:54 +02:00
|
|
|
#include <byteswap.h>
|
|
|
|
#include <ipxe/in.h>
|
2010-04-19 21:16:01 +02:00
|
|
|
#include <ipxe/iobuf.h>
|
2013-08-26 15:23:54 +02:00
|
|
|
#include <ipxe/tcpip.h>
|
|
|
|
#include <ipxe/ipv6.h>
|
|
|
|
#include <ipxe/icmpv6.h>
|
|
|
|
#include <ipxe/neighbour.h>
|
2010-04-19 21:16:01 +02:00
|
|
|
#include <ipxe/ndp.h>
|
2006-08-19 17:58:22 +02:00
|
|
|
|
|
|
|
/** @file
|
|
|
|
*
|
2013-08-26 15:23:54 +02:00
|
|
|
* IPv6 neighbour discovery protocol
|
2006-08-19 17:58:22 +02:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2013-10-24 15:06:33 +02:00
|
|
|
* Transmit NDP packet with link-layer address option
|
2006-08-19 17:58:22 +02:00
|
|
|
*
|
2013-08-26 15:23:54 +02:00
|
|
|
* @v netdev Network device
|
|
|
|
* @v sin6_src Source socket address
|
|
|
|
* @v sin6_dest Destination socket address
|
2013-10-24 15:06:33 +02:00
|
|
|
* @v data NDP header
|
|
|
|
* @v len Size of NDP header
|
2013-08-26 15:23:54 +02:00
|
|
|
* @v option_type NDP option type
|
|
|
|
* @ret rc Return status code
|
2006-08-19 17:58:22 +02:00
|
|
|
*/
|
2013-10-24 15:06:33 +02:00
|
|
|
static int ndp_tx_ll_addr ( struct net_device *netdev,
|
|
|
|
struct sockaddr_in6 *sin6_src,
|
|
|
|
struct sockaddr_in6 *sin6_dest,
|
|
|
|
const void *data, size_t len,
|
|
|
|
unsigned int option_type ) {
|
2013-08-26 15:23:54 +02:00
|
|
|
struct sockaddr_tcpip *st_src =
|
|
|
|
( ( struct sockaddr_tcpip * ) sin6_src );
|
|
|
|
struct sockaddr_tcpip *st_dest =
|
|
|
|
( ( struct sockaddr_tcpip * ) sin6_dest );
|
|
|
|
struct ll_protocol *ll_protocol = netdev->ll_protocol;
|
|
|
|
struct io_buffer *iobuf;
|
2013-10-23 15:00:12 +02:00
|
|
|
struct ndp_ll_addr_option *ll_addr_opt;
|
2013-10-24 15:06:33 +02:00
|
|
|
union ndp_header *ndp;
|
2013-08-26 15:23:54 +02:00
|
|
|
size_t option_len;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
/* Allocate and populate buffer */
|
2013-10-23 15:00:12 +02:00
|
|
|
option_len = ( ( sizeof ( *ll_addr_opt ) +
|
2013-10-23 00:39:29 +02:00
|
|
|
ll_protocol->ll_addr_len + NDP_OPTION_BLKSZ - 1 ) &
|
2013-08-26 15:23:54 +02:00
|
|
|
~( NDP_OPTION_BLKSZ - 1 ) );
|
2013-10-24 15:06:33 +02:00
|
|
|
iobuf = alloc_iob ( MAX_LL_NET_HEADER_LEN + len + option_len );
|
2013-08-26 15:23:54 +02:00
|
|
|
if ( ! iobuf )
|
|
|
|
return -ENOMEM;
|
|
|
|
iob_reserve ( iobuf, MAX_LL_NET_HEADER_LEN );
|
2013-10-24 15:06:33 +02:00
|
|
|
memcpy ( iob_put ( iobuf, len ), data, len );
|
|
|
|
ll_addr_opt = iob_put ( iobuf, option_len );
|
2013-10-23 15:00:12 +02:00
|
|
|
ll_addr_opt->header.type = option_type;
|
|
|
|
ll_addr_opt->header.blocks = ( option_len / NDP_OPTION_BLKSZ );
|
|
|
|
memcpy ( ll_addr_opt->ll_addr, netdev->ll_addr,
|
2013-08-26 15:23:54 +02:00
|
|
|
ll_protocol->ll_addr_len );
|
2013-10-24 15:06:33 +02:00
|
|
|
ndp = iobuf->data;
|
|
|
|
ndp->icmp.chksum = tcpip_chksum ( ndp, ( len + option_len ) );
|
2013-08-26 15:23:54 +02:00
|
|
|
|
|
|
|
/* Transmit packet */
|
|
|
|
if ( ( rc = tcpip_tx ( iobuf, &icmpv6_protocol, st_src, st_dest,
|
2013-10-24 15:06:33 +02:00
|
|
|
netdev, &ndp->icmp.chksum ) ) != 0 ) {
|
2013-08-26 15:23:54 +02:00
|
|
|
DBGC ( netdev, "NDP could not transmit packet: %s\n",
|
|
|
|
strerror ( rc ) );
|
|
|
|
return rc;
|
2006-08-19 17:58:22 +02:00
|
|
|
}
|
2013-08-26 15:23:54 +02:00
|
|
|
|
|
|
|
return 0;
|
2006-08-19 17:58:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-08-26 15:23:54 +02:00
|
|
|
* Transmit NDP neighbour discovery request
|
|
|
|
*
|
|
|
|
* @v netdev Network device
|
|
|
|
* @v net_protocol Network-layer protocol
|
|
|
|
* @v net_dest Destination network-layer address
|
|
|
|
* @v net_source Source network-layer address
|
|
|
|
* @ret rc Return status code
|
2006-08-19 17:58:22 +02:00
|
|
|
*/
|
2013-08-26 15:23:54 +02:00
|
|
|
static int ndp_tx_request ( struct net_device *netdev,
|
|
|
|
struct net_protocol *net_protocol __unused,
|
|
|
|
const void *net_dest, const void *net_source ) {
|
|
|
|
struct sockaddr_in6 sin6_src;
|
|
|
|
struct sockaddr_in6 sin6_dest;
|
2013-10-24 15:06:33 +02:00
|
|
|
struct ndp_neighbour_header neigh;
|
|
|
|
int rc;
|
2013-08-26 15:23:54 +02:00
|
|
|
|
|
|
|
/* Construct source address */
|
|
|
|
memset ( &sin6_src, 0, sizeof ( sin6_src ) );
|
|
|
|
sin6_src.sin6_family = AF_INET6;
|
|
|
|
memcpy ( &sin6_src.sin6_addr, net_source,
|
|
|
|
sizeof ( sin6_src.sin6_addr ) );
|
2013-10-25 14:48:18 +02:00
|
|
|
sin6_src.sin6_scope_id = netdev->index;
|
2013-08-26 15:23:54 +02:00
|
|
|
|
|
|
|
/* Construct multicast destination address */
|
|
|
|
memset ( &sin6_dest, 0, sizeof ( sin6_dest ) );
|
|
|
|
sin6_dest.sin6_family = AF_INET6;
|
2013-10-25 14:48:18 +02:00
|
|
|
sin6_dest.sin6_scope_id = netdev->index;
|
2013-08-26 15:23:54 +02:00
|
|
|
ipv6_solicited_node ( &sin6_dest.sin6_addr, net_dest );
|
|
|
|
|
2013-10-24 15:06:33 +02:00
|
|
|
/* Construct neighbour header */
|
|
|
|
memset ( &neigh, 0, sizeof ( neigh ) );
|
|
|
|
neigh.icmp.type = ICMPV6_NEIGHBOUR_SOLICITATION;
|
|
|
|
memcpy ( &neigh.target, net_dest, sizeof ( neigh.target ) );
|
|
|
|
|
2013-08-26 15:23:54 +02:00
|
|
|
/* Transmit neighbour discovery packet */
|
2013-10-24 15:06:33 +02:00
|
|
|
if ( ( rc = ndp_tx_ll_addr ( netdev, &sin6_src, &sin6_dest, &neigh,
|
|
|
|
sizeof ( neigh ),
|
|
|
|
NDP_OPT_LL_SOURCE ) ) != 0 )
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
return 0;
|
2006-08-19 17:58:22 +02:00
|
|
|
}
|
|
|
|
|
2013-08-26 15:23:54 +02:00
|
|
|
/** NDP neighbour discovery protocol */
|
|
|
|
struct neighbour_discovery ndp_discovery = {
|
|
|
|
.name = "NDP",
|
|
|
|
.tx_request = ndp_tx_request,
|
|
|
|
};
|
|
|
|
|
2013-10-24 15:06:33 +02:00
|
|
|
/**
|
|
|
|
* Transmit NDP router solicitation
|
|
|
|
*
|
|
|
|
* @v netdev Network device
|
|
|
|
* @ret rc Return status code
|
|
|
|
*/
|
|
|
|
int ndp_tx_router_solicitation ( struct net_device *netdev ) {
|
|
|
|
struct ndp_router_solicitation_header rsol;
|
|
|
|
struct sockaddr_in6 sin6_dest;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
/* Construct multicast destination address */
|
|
|
|
memset ( &sin6_dest, 0, sizeof ( sin6_dest ) );
|
|
|
|
sin6_dest.sin6_family = AF_INET6;
|
|
|
|
sin6_dest.sin6_scope_id = netdev->index;
|
|
|
|
ipv6_all_routers ( &sin6_dest.sin6_addr );
|
|
|
|
|
|
|
|
/* Construct router solicitation */
|
|
|
|
memset ( &rsol, 0, sizeof ( rsol ) );
|
|
|
|
rsol.icmp.type = ICMPV6_ROUTER_SOLICITATION;
|
|
|
|
|
|
|
|
/* Transmit packet */
|
|
|
|
if ( ( rc = ndp_tx_ll_addr ( netdev, NULL, &sin6_dest, &rsol,
|
|
|
|
sizeof ( rsol ), NDP_OPT_LL_SOURCE ) ) !=0)
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-08-19 17:58:22 +02:00
|
|
|
/**
|
2013-08-26 15:23:54 +02:00
|
|
|
* Process NDP neighbour solicitation source link-layer address option
|
2006-08-19 17:58:22 +02:00
|
|
|
*
|
|
|
|
* @v netdev Network device
|
2013-08-26 15:23:54 +02:00
|
|
|
* @v sin6_src Source socket address
|
|
|
|
* @v ndp NDP packet
|
2013-10-23 15:00:12 +02:00
|
|
|
* @v option NDP option
|
|
|
|
* @v len NDP option length
|
2013-08-26 15:23:54 +02:00
|
|
|
* @ret rc Return status code
|
2006-08-19 17:58:22 +02:00
|
|
|
*/
|
2013-10-23 00:39:29 +02:00
|
|
|
static int
|
|
|
|
ndp_rx_neighbour_solicitation_ll_source ( struct net_device *netdev,
|
|
|
|
struct sockaddr_in6 *sin6_src,
|
|
|
|
union ndp_header *ndp,
|
2013-10-23 15:00:12 +02:00
|
|
|
union ndp_option *option,
|
|
|
|
size_t len ) {
|
2013-10-23 00:39:29 +02:00
|
|
|
struct ndp_neighbour_header *neigh = &ndp->neigh;
|
2013-10-23 15:00:12 +02:00
|
|
|
struct ndp_ll_addr_option *ll_addr_opt = &option->ll_addr;
|
2006-08-19 17:58:22 +02:00
|
|
|
struct ll_protocol *ll_protocol = netdev->ll_protocol;
|
|
|
|
int rc;
|
|
|
|
|
2013-08-26 15:23:54 +02:00
|
|
|
/* Silently ignore neighbour solicitations for addresses we do
|
|
|
|
* not own.
|
|
|
|
*/
|
2013-10-23 00:39:29 +02:00
|
|
|
if ( ! ipv6_has_addr ( netdev, &neigh->target ) )
|
2006-08-19 17:58:22 +02:00
|
|
|
return 0;
|
|
|
|
|
2013-08-26 15:23:54 +02:00
|
|
|
/* Sanity check */
|
2013-10-23 15:00:12 +02:00
|
|
|
if ( offsetof ( typeof ( *ll_addr_opt ),
|
|
|
|
ll_addr[ll_protocol->ll_addr_len] ) > len ) {
|
2013-08-26 15:23:54 +02:00
|
|
|
DBGC ( netdev, "NDP neighbour solicitation link-layer address "
|
2013-10-23 15:00:12 +02:00
|
|
|
"option too short at %zd bytes\n", len );
|
2013-08-26 15:23:54 +02:00
|
|
|
return -EINVAL;
|
2006-08-19 17:58:22 +02:00
|
|
|
}
|
|
|
|
|
2013-08-26 15:23:54 +02:00
|
|
|
/* Create or update neighbour cache entry */
|
|
|
|
if ( ( rc = neighbour_define ( netdev, &ipv6_protocol,
|
|
|
|
&sin6_src->sin6_addr,
|
2013-10-23 15:00:12 +02:00
|
|
|
ll_addr_opt->ll_addr ) ) != 0 ) {
|
2013-08-26 15:23:54 +02:00
|
|
|
DBGC ( netdev, "NDP could not define %s => %s: %s\n",
|
|
|
|
inet6_ntoa ( &sin6_src->sin6_addr ),
|
2013-10-23 15:00:12 +02:00
|
|
|
ll_protocol->ntoa ( ll_addr_opt->ll_addr ),
|
|
|
|
strerror ( rc ) );
|
2013-08-26 15:23:54 +02:00
|
|
|
return rc;
|
|
|
|
}
|
2006-08-19 17:58:22 +02:00
|
|
|
|
2013-10-24 15:06:33 +02:00
|
|
|
/* Convert neighbour header to advertisement */
|
|
|
|
memset ( neigh, 0, offsetof ( typeof ( *neigh ), target ) );
|
|
|
|
neigh->icmp.type = ICMPV6_NEIGHBOUR_ADVERTISEMENT;
|
|
|
|
neigh->flags = ( NDP_NEIGHBOUR_SOLICITED | NDP_NEIGHBOUR_OVERRIDE );
|
|
|
|
|
2013-08-26 15:23:54 +02:00
|
|
|
/* Send neighbour advertisement */
|
2013-10-24 15:06:33 +02:00
|
|
|
if ( ( rc = ndp_tx_ll_addr ( netdev, NULL, sin6_src, neigh,
|
|
|
|
sizeof ( *neigh ),
|
|
|
|
NDP_OPT_LL_TARGET ) ) != 0 )
|
2006-08-19 17:58:22 +02:00
|
|
|
return rc;
|
2013-08-26 15:23:54 +02:00
|
|
|
|
|
|
|
return 0;
|
2006-08-19 17:58:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-08-26 15:23:54 +02:00
|
|
|
* Process NDP neighbour advertisement target link-layer address option
|
2006-08-19 17:58:22 +02:00
|
|
|
*
|
2013-08-26 15:23:54 +02:00
|
|
|
* @v netdev Network device
|
|
|
|
* @v sin6_src Source socket address
|
|
|
|
* @v ndp NDP packet
|
2013-10-23 15:00:12 +02:00
|
|
|
* @v option NDP option
|
|
|
|
* @v len NDP option length
|
2013-08-26 15:23:54 +02:00
|
|
|
* @ret rc Return status code
|
2006-08-19 17:58:22 +02:00
|
|
|
*/
|
2013-08-26 15:23:54 +02:00
|
|
|
static int
|
2013-10-23 00:39:29 +02:00
|
|
|
ndp_rx_neighbour_advertisement_ll_target ( struct net_device *netdev,
|
|
|
|
struct sockaddr_in6 *sin6_src
|
|
|
|
__unused,
|
|
|
|
union ndp_header *ndp,
|
2013-10-23 15:00:12 +02:00
|
|
|
union ndp_option *option,
|
|
|
|
size_t len ) {
|
2013-10-23 00:39:29 +02:00
|
|
|
struct ndp_neighbour_header *neigh = &ndp->neigh;
|
2013-10-23 15:00:12 +02:00
|
|
|
struct ndp_ll_addr_option *ll_addr_opt = &option->ll_addr;
|
2013-08-26 15:23:54 +02:00
|
|
|
struct ll_protocol *ll_protocol = netdev->ll_protocol;
|
|
|
|
int rc;
|
2006-08-19 17:58:22 +02:00
|
|
|
|
|
|
|
/* Sanity check */
|
2013-10-23 15:00:12 +02:00
|
|
|
if ( offsetof ( typeof ( *ll_addr_opt ),
|
|
|
|
ll_addr[ll_protocol->ll_addr_len] ) > len ) {
|
2013-08-26 15:23:54 +02:00
|
|
|
DBGC ( netdev, "NDP neighbour advertisement link-layer address "
|
2013-10-23 15:00:12 +02:00
|
|
|
"option too short at %zd bytes\n", len );
|
2006-08-19 17:58:22 +02:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2013-08-26 15:23:54 +02:00
|
|
|
/* Update neighbour cache entry, if any */
|
2013-10-23 00:39:29 +02:00
|
|
|
if ( ( rc = neighbour_update ( netdev, &ipv6_protocol, &neigh->target,
|
2013-10-23 15:00:12 +02:00
|
|
|
ll_addr_opt->ll_addr ) ) != 0 ) {
|
2013-08-26 15:23:54 +02:00
|
|
|
DBGC ( netdev, "NDP could not update %s => %s: %s\n",
|
2013-10-23 00:39:29 +02:00
|
|
|
inet6_ntoa ( &neigh->target ),
|
2013-10-23 15:00:12 +02:00
|
|
|
ll_protocol->ntoa ( ll_addr_opt->ll_addr ),
|
|
|
|
strerror ( rc ) );
|
2013-10-23 00:39:29 +02:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Process NDP router advertisement source link-layer address option
|
|
|
|
*
|
|
|
|
* @v netdev Network device
|
|
|
|
* @v sin6_src Source socket address
|
|
|
|
* @v ndp NDP packet
|
2013-10-23 15:00:12 +02:00
|
|
|
* @v option NDP option
|
|
|
|
* @v len NDP option length
|
2013-10-23 00:39:29 +02:00
|
|
|
* @ret rc Return status code
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
ndp_rx_router_advertisement_ll_source ( struct net_device *netdev,
|
|
|
|
struct sockaddr_in6 *sin6_src,
|
|
|
|
union ndp_header *ndp __unused,
|
2013-10-23 15:00:12 +02:00
|
|
|
union ndp_option *option, size_t len ) {
|
|
|
|
struct ndp_ll_addr_option *ll_addr_opt = &option->ll_addr;
|
2013-10-23 00:39:29 +02:00
|
|
|
struct ll_protocol *ll_protocol = netdev->ll_protocol;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
/* Sanity check */
|
2013-10-23 15:00:12 +02:00
|
|
|
if ( offsetof ( typeof ( *ll_addr_opt ),
|
|
|
|
ll_addr[ll_protocol->ll_addr_len] ) > len ) {
|
2013-10-23 00:39:29 +02:00
|
|
|
DBGC ( netdev, "NDP router advertisement link-layer address "
|
2013-10-23 15:00:12 +02:00
|
|
|
"option too short at %zd bytes\n", len );
|
2013-10-23 00:39:29 +02:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Define neighbour cache entry */
|
|
|
|
if ( ( rc = neighbour_define ( netdev, &ipv6_protocol,
|
|
|
|
&sin6_src->sin6_addr,
|
2013-10-23 15:00:12 +02:00
|
|
|
ll_addr_opt->ll_addr ) ) != 0 ) {
|
2013-10-23 00:39:29 +02:00
|
|
|
DBGC ( netdev, "NDP could not define %s => %s: %s\n",
|
|
|
|
inet6_ntoa ( &sin6_src->sin6_addr ),
|
2013-10-23 15:00:12 +02:00
|
|
|
ll_protocol->ntoa ( ll_addr_opt->ll_addr ),
|
|
|
|
strerror ( rc ) );
|
2013-08-26 15:23:54 +02:00
|
|
|
return rc;
|
|
|
|
}
|
2006-08-19 17:58:22 +02:00
|
|
|
|
2013-08-26 15:23:54 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2006-08-19 17:58:22 +02:00
|
|
|
|
2013-10-23 15:00:12 +02:00
|
|
|
/**
|
|
|
|
* Process NDP router advertisement prefix information option
|
|
|
|
*
|
|
|
|
* @v netdev Network device
|
|
|
|
* @v sin6_src Source socket address
|
|
|
|
* @v ndp NDP packet
|
|
|
|
* @v option NDP option
|
|
|
|
* @v len NDP option length
|
|
|
|
* @ret rc Return status code
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
ndp_rx_router_advertisement_prefix ( struct net_device *netdev,
|
|
|
|
struct sockaddr_in6 *sin6_src,
|
|
|
|
union ndp_header *ndp,
|
|
|
|
union ndp_option *option, size_t len ) {
|
|
|
|
struct ndp_router_advertisement_header *radv = &ndp->radv;
|
|
|
|
struct ndp_prefix_information_option *prefix_opt = &option->prefix;
|
|
|
|
struct in6_addr *router = &sin6_src->sin6_addr;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
/* Sanity check */
|
|
|
|
if ( sizeof ( *prefix_opt ) > len ) {
|
|
|
|
DBGC ( netdev, "NDP router advertisement prefix option too "
|
|
|
|
"short at %zd bytes\n", len );
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
DBGC ( netdev, "NDP found %sdefault router %s ",
|
|
|
|
( radv->lifetime ? "" : "non-" ),
|
|
|
|
inet6_ntoa ( &sin6_src->sin6_addr ) );
|
|
|
|
DBGC ( netdev, "for %s-link %sautonomous prefix %s/%d\n",
|
|
|
|
( ( prefix_opt->flags & NDP_PREFIX_ON_LINK ) ? "on" : "off" ),
|
|
|
|
( ( prefix_opt->flags & NDP_PREFIX_AUTONOMOUS ) ? "" : "non-" ),
|
|
|
|
inet6_ntoa ( &prefix_opt->prefix ),
|
|
|
|
prefix_opt->prefix_len );
|
|
|
|
|
|
|
|
/* Perform stateless address autoconfiguration, if applicable */
|
|
|
|
if ( ( prefix_opt->flags &
|
|
|
|
( NDP_PREFIX_ON_LINK | NDP_PREFIX_AUTONOMOUS ) ) ==
|
|
|
|
( NDP_PREFIX_ON_LINK | NDP_PREFIX_AUTONOMOUS ) ) {
|
|
|
|
if ( ( rc = ipv6_slaac ( netdev, &prefix_opt->prefix,
|
|
|
|
prefix_opt->prefix_len,
|
|
|
|
( radv->lifetime ?
|
|
|
|
router : NULL ) ) ) != 0 ) {
|
|
|
|
DBGC ( netdev, "NDP could not autoconfigure prefix %s/"
|
|
|
|
"%d: %s\n", inet6_ntoa ( &prefix_opt->prefix ),
|
|
|
|
prefix_opt->prefix_len, strerror ( rc ) );
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-08-26 15:23:54 +02:00
|
|
|
/** An NDP option handler */
|
|
|
|
struct ndp_option_handler {
|
|
|
|
/** ICMPv6 type */
|
|
|
|
uint8_t icmp_type;
|
|
|
|
/** Option type */
|
|
|
|
uint8_t option_type;
|
|
|
|
/**
|
|
|
|
* Handle received option
|
|
|
|
*
|
|
|
|
* @v netdev Network device
|
|
|
|
* @v sin6_src Source socket address
|
|
|
|
* @v ndp NDP packet
|
2013-10-23 15:00:12 +02:00
|
|
|
* @v option NDP option
|
2013-08-26 15:23:54 +02:00
|
|
|
* @ret rc Return status code
|
|
|
|
*/
|
|
|
|
int ( * rx ) ( struct net_device *netdev, struct sockaddr_in6 *sin6_src,
|
2013-10-23 15:00:12 +02:00
|
|
|
union ndp_header *ndp, union ndp_option *option,
|
|
|
|
size_t len );
|
2013-08-26 15:23:54 +02:00
|
|
|
};
|
2006-08-19 17:58:22 +02:00
|
|
|
|
2013-08-26 15:23:54 +02:00
|
|
|
/** NDP option handlers */
|
|
|
|
static struct ndp_option_handler ndp_option_handlers[] = {
|
|
|
|
{
|
2013-10-23 00:39:29 +02:00
|
|
|
.icmp_type = ICMPV6_NEIGHBOUR_SOLICITATION,
|
2013-08-26 15:23:54 +02:00
|
|
|
.option_type = NDP_OPT_LL_SOURCE,
|
2013-10-23 00:39:29 +02:00
|
|
|
.rx = ndp_rx_neighbour_solicitation_ll_source,
|
2013-08-26 15:23:54 +02:00
|
|
|
},
|
|
|
|
{
|
2013-10-23 00:39:29 +02:00
|
|
|
.icmp_type = ICMPV6_NEIGHBOUR_ADVERTISEMENT,
|
2013-08-26 15:23:54 +02:00
|
|
|
.option_type = NDP_OPT_LL_TARGET,
|
2013-10-23 00:39:29 +02:00
|
|
|
.rx = ndp_rx_neighbour_advertisement_ll_target,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.icmp_type = ICMPV6_ROUTER_ADVERTISEMENT,
|
|
|
|
.option_type = NDP_OPT_LL_SOURCE,
|
|
|
|
.rx = ndp_rx_router_advertisement_ll_source,
|
2013-08-26 15:23:54 +02:00
|
|
|
},
|
2013-10-23 15:00:12 +02:00
|
|
|
{
|
|
|
|
.icmp_type = ICMPV6_ROUTER_ADVERTISEMENT,
|
|
|
|
.option_type = NDP_OPT_PREFIX,
|
|
|
|
.rx = ndp_rx_router_advertisement_prefix,
|
|
|
|
},
|
2013-08-26 15:23:54 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Process received NDP option
|
|
|
|
*
|
|
|
|
* @v netdev Network device
|
|
|
|
* @v sin6_src Source socket address
|
|
|
|
* @v ndp NDP packet
|
2013-10-23 15:00:12 +02:00
|
|
|
* @v option NDP option
|
2013-08-26 15:23:54 +02:00
|
|
|
* @v len Option length
|
|
|
|
* @ret rc Return status code
|
|
|
|
*/
|
|
|
|
static int ndp_rx_option ( struct net_device *netdev,
|
2013-10-23 15:00:12 +02:00
|
|
|
struct sockaddr_in6 *sin6_src, union ndp_header *ndp,
|
|
|
|
union ndp_option *option, size_t len ) {
|
2013-08-26 15:23:54 +02:00
|
|
|
struct ndp_option_handler *handler;
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
/* Locate a suitable option handler, if any */
|
|
|
|
for ( i = 0 ; i < ( sizeof ( ndp_option_handlers ) /
|
|
|
|
sizeof ( ndp_option_handlers[0] ) ) ; i++ ) {
|
|
|
|
handler = &ndp_option_handlers[i];
|
|
|
|
if ( ( handler->icmp_type == ndp->icmp.type ) &&
|
2013-10-23 15:00:12 +02:00
|
|
|
( handler->option_type == option->header.type ) ) {
|
2013-08-26 15:23:54 +02:00
|
|
|
return handler->rx ( netdev, sin6_src, ndp,
|
2013-10-23 15:00:12 +02:00
|
|
|
option, len );
|
2006-08-19 17:58:22 +02:00
|
|
|
}
|
|
|
|
}
|
2013-08-26 15:23:54 +02:00
|
|
|
|
|
|
|
/* Silently ignore unknown options as per RFC 4861 */
|
2006-08-19 17:58:22 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2013-08-26 15:23:54 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Process received NDP packet
|
|
|
|
*
|
|
|
|
* @v iobuf I/O buffer
|
|
|
|
* @v netdev Network device
|
|
|
|
* @v sin6_src Source socket address
|
2013-10-23 00:39:29 +02:00
|
|
|
* @v offset Offset to NDP options
|
2013-08-26 15:23:54 +02:00
|
|
|
* @ret rc Return status code
|
|
|
|
*/
|
|
|
|
static int ndp_rx ( struct io_buffer *iobuf,
|
|
|
|
struct net_device *netdev,
|
|
|
|
struct sockaddr_in6 *sin6_src,
|
2013-10-23 00:39:29 +02:00
|
|
|
size_t offset ) {
|
|
|
|
union ndp_header *ndp = iobuf->data;
|
2013-10-23 15:00:12 +02:00
|
|
|
union ndp_option *option;
|
2013-08-26 15:23:54 +02:00
|
|
|
size_t remaining;
|
|
|
|
size_t option_len;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
/* Sanity check */
|
2013-10-23 00:39:29 +02:00
|
|
|
if ( iob_len ( iobuf ) < offset ) {
|
2013-08-26 15:23:54 +02:00
|
|
|
DBGC ( netdev, "NDP packet too short at %zd bytes (min %zd "
|
2013-10-23 00:39:29 +02:00
|
|
|
"bytes)\n", iob_len ( iobuf ), offset );
|
2013-08-26 15:23:54 +02:00
|
|
|
rc = -EINVAL;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Search for option */
|
2013-10-23 00:39:29 +02:00
|
|
|
option = ( ( ( void * ) ndp ) + offset );
|
|
|
|
remaining = ( iob_len ( iobuf ) - offset );
|
2013-08-26 15:23:54 +02:00
|
|
|
while ( remaining ) {
|
|
|
|
|
|
|
|
/* Sanity check */
|
2013-10-23 15:00:12 +02:00
|
|
|
if ( ( remaining < sizeof ( option->header ) ) ||
|
|
|
|
( option->header.blocks == 0 ) ||
|
|
|
|
( remaining < ( option->header.blocks *
|
|
|
|
NDP_OPTION_BLKSZ ) ) ) {
|
2013-08-26 15:23:54 +02:00
|
|
|
DBGC ( netdev, "NDP bad option length:\n" );
|
|
|
|
DBGC_HDA ( netdev, 0, option, remaining );
|
|
|
|
rc = -EINVAL;
|
|
|
|
goto done;
|
|
|
|
}
|
2013-10-23 15:00:12 +02:00
|
|
|
option_len = ( option->header.blocks * NDP_OPTION_BLKSZ );
|
2013-08-26 15:23:54 +02:00
|
|
|
|
|
|
|
/* Handle option */
|
2013-10-23 15:00:12 +02:00
|
|
|
if ( ( rc = ndp_rx_option ( netdev, sin6_src, ndp, option,
|
|
|
|
option_len ) ) != 0 )
|
2013-08-26 15:23:54 +02:00
|
|
|
goto done;
|
|
|
|
|
|
|
|
/* Move to next option */
|
|
|
|
option = ( ( ( void * ) option ) + option_len );
|
|
|
|
remaining -= option_len;
|
|
|
|
}
|
|
|
|
|
2013-09-03 21:01:17 +02:00
|
|
|
/* Success */
|
|
|
|
rc = 0;
|
|
|
|
|
2013-08-26 15:23:54 +02:00
|
|
|
done:
|
|
|
|
free_iob ( iobuf );
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2013-10-23 00:39:29 +02:00
|
|
|
/**
|
|
|
|
* Process received NDP neighbour solicitation or advertisement
|
|
|
|
*
|
|
|
|
* @v iobuf I/O buffer
|
|
|
|
* @v netdev Network device
|
|
|
|
* @v sin6_src Source socket address
|
|
|
|
* @v sin6_dest Destination socket address
|
|
|
|
* @ret rc Return status code
|
|
|
|
*/
|
|
|
|
static int ndp_rx_neighbour ( struct io_buffer *iobuf,
|
|
|
|
struct net_device *netdev,
|
|
|
|
struct sockaddr_in6 *sin6_src,
|
|
|
|
struct sockaddr_in6 *sin6_dest __unused ) {
|
|
|
|
union ndp_header *ndp = iobuf->data;
|
|
|
|
struct ndp_neighbour_header *neigh = &ndp->neigh;
|
|
|
|
|
|
|
|
return ndp_rx ( iobuf, netdev, sin6_src,
|
|
|
|
offsetof ( typeof ( *neigh ), option ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Process received NDP router advertisement
|
|
|
|
*
|
|
|
|
* @v iobuf I/O buffer
|
|
|
|
* @v netdev Network device
|
|
|
|
* @v sin6_src Source socket address
|
|
|
|
* @v sin6_dest Destination socket address
|
|
|
|
* @ret rc Return status code
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
ndp_rx_router_advertisement ( struct io_buffer *iobuf,
|
|
|
|
struct net_device *netdev,
|
|
|
|
struct sockaddr_in6 *sin6_src,
|
|
|
|
struct sockaddr_in6 *sin6_dest __unused ) {
|
|
|
|
union ndp_header *ndp = iobuf->data;
|
|
|
|
struct ndp_router_advertisement_header *radv = &ndp->radv;
|
|
|
|
|
|
|
|
return ndp_rx ( iobuf, netdev, sin6_src,
|
|
|
|
offsetof ( typeof ( *radv ), option ) );
|
|
|
|
}
|
|
|
|
|
2013-08-26 15:23:54 +02:00
|
|
|
/** NDP ICMPv6 handlers */
|
|
|
|
struct icmpv6_handler ndp_handlers[] __icmpv6_handler = {
|
|
|
|
{
|
2013-10-23 00:39:29 +02:00
|
|
|
.type = ICMPV6_NEIGHBOUR_SOLICITATION,
|
|
|
|
.rx = ndp_rx_neighbour,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.type = ICMPV6_NEIGHBOUR_ADVERTISEMENT,
|
|
|
|
.rx = ndp_rx_neighbour,
|
2013-08-26 15:23:54 +02:00
|
|
|
},
|
|
|
|
{
|
2013-10-23 00:39:29 +02:00
|
|
|
.type = ICMPV6_ROUTER_ADVERTISEMENT,
|
|
|
|
.rx = ndp_rx_router_advertisement,
|
2013-08-26 15:23:54 +02:00
|
|
|
},
|
|
|
|
};
|