2006-06-26 15:33:46 +00:00
|
|
|
#include <errno.h>
|
2006-08-01 20:27:26 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
2006-08-19 15:58:22 +00:00
|
|
|
#include <stdlib.h>
|
2007-01-19 01:13:12 +00:00
|
|
|
#include <stdio.h>
|
2006-08-01 20:27:26 +00:00
|
|
|
#include <byteswap.h>
|
2006-08-19 15:58:22 +00:00
|
|
|
#include <gpxe/in.h>
|
|
|
|
#include <gpxe/ip6.h>
|
|
|
|
#include <gpxe/ndp.h>
|
|
|
|
#include <gpxe/list.h>
|
|
|
|
#include <gpxe/icmp6.h>
|
|
|
|
#include <gpxe/tcpip.h>
|
|
|
|
#include <gpxe/socket.h>
|
2007-05-19 18:39:40 +00:00
|
|
|
#include <gpxe/iobuf.h>
|
2006-06-26 15:33:46 +00:00
|
|
|
#include <gpxe/netdevice.h>
|
2006-08-01 20:27:26 +00:00
|
|
|
#include <gpxe/if_ether.h>
|
2006-08-19 15:58:22 +00:00
|
|
|
|
|
|
|
struct net_protocol ipv6_protocol;
|
|
|
|
|
|
|
|
/* Unspecified IP6 address */
|
|
|
|
static struct in6_addr ip6_none = {
|
|
|
|
.in6_u.u6_addr32[0] = 0,
|
|
|
|
.in6_u.u6_addr32[1] = 0,
|
|
|
|
.in6_u.u6_addr32[2] = 0,
|
|
|
|
.in6_u.u6_addr32[3] = 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
/** An IPv6 routing table entry */
|
|
|
|
struct ipv6_miniroute {
|
|
|
|
/* List of miniroutes */
|
|
|
|
struct list_head list;
|
2007-01-04 03:28:30 +00:00
|
|
|
|
2006-08-19 15:58:22 +00:00
|
|
|
/* Network device */
|
|
|
|
struct net_device *netdev;
|
2007-01-04 03:28:30 +00:00
|
|
|
/** Reference to network device */
|
|
|
|
struct reference netdev_ref;
|
|
|
|
|
2006-08-19 15:58:22 +00:00
|
|
|
/* Destination prefix */
|
|
|
|
struct in6_addr prefix;
|
|
|
|
/* Prefix length */
|
|
|
|
int prefix_len;
|
|
|
|
/* IPv6 address of interface */
|
|
|
|
struct in6_addr address;
|
|
|
|
/* Gateway address */
|
|
|
|
struct in6_addr gateway;
|
|
|
|
};
|
|
|
|
|
|
|
|
/** List of IPv6 miniroutes */
|
|
|
|
static LIST_HEAD ( miniroutes );
|
2006-06-26 15:33:46 +00:00
|
|
|
|
2007-01-04 03:28:30 +00:00
|
|
|
static void ipv6_forget_netdev ( struct reference *ref );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add IPv6 minirouting table entry
|
|
|
|
*
|
|
|
|
* @v netdev Network device
|
|
|
|
* @v prefix Destination prefix
|
|
|
|
* @v address Address of the interface
|
|
|
|
* @v gateway Gateway address (or ::0 for no gateway)
|
|
|
|
* @ret miniroute Routing table entry, or NULL
|
|
|
|
*/
|
|
|
|
static struct ipv6_miniroute * add_ipv6_miniroute ( struct net_device *netdev,
|
|
|
|
struct in6_addr prefix,
|
|
|
|
int prefix_len,
|
|
|
|
struct in6_addr address,
|
|
|
|
struct in6_addr gateway ) {
|
|
|
|
struct ipv6_miniroute *miniroute;
|
|
|
|
|
|
|
|
miniroute = malloc ( sizeof ( *miniroute ) );
|
|
|
|
if ( miniroute ) {
|
|
|
|
/* Record routing information */
|
|
|
|
miniroute->netdev = netdev;
|
|
|
|
miniroute->prefix = prefix;
|
|
|
|
miniroute->prefix_len = prefix_len;
|
|
|
|
miniroute->address = address;
|
|
|
|
miniroute->gateway = gateway;
|
|
|
|
|
|
|
|
/* Add miniroute to list of miniroutes */
|
|
|
|
if ( !IP6_EQUAL ( gateway, ip6_none ) ) {
|
|
|
|
list_add_tail ( &miniroute->list, &miniroutes );
|
|
|
|
} else {
|
|
|
|
list_add ( &miniroute->list, &miniroutes );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Record reference to net_device */
|
|
|
|
miniroute->netdev_ref.forget = ipv6_forget_netdev;
|
|
|
|
ref_add ( &miniroute->netdev_ref, &netdev->references );
|
|
|
|
}
|
|
|
|
|
|
|
|
return miniroute;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete IPv6 minirouting table entry
|
|
|
|
*
|
|
|
|
* @v miniroute Routing table entry
|
|
|
|
*/
|
|
|
|
static void del_ipv6_miniroute ( struct ipv6_miniroute *miniroute ) {
|
|
|
|
ref_del ( &miniroute->netdev_ref );
|
|
|
|
list_del ( &miniroute->list );
|
|
|
|
free ( miniroute );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Forget reference to net_device
|
|
|
|
*
|
|
|
|
* @v ref Persistent reference
|
|
|
|
*/
|
|
|
|
static void ipv6_forget_netdev ( struct reference *ref ) {
|
|
|
|
struct ipv6_miniroute *miniroute
|
|
|
|
= container_of ( ref, struct ipv6_miniroute, netdev_ref );
|
|
|
|
|
|
|
|
del_ipv6_miniroute ( miniroute );
|
|
|
|
}
|
|
|
|
|
2006-06-26 15:33:46 +00:00
|
|
|
/**
|
2006-08-19 15:58:22 +00:00
|
|
|
* Add IPv6 interface
|
|
|
|
*
|
|
|
|
* @v netdev Network device
|
|
|
|
* @v prefix Destination prefix
|
|
|
|
* @v address Address of the interface
|
|
|
|
* @v gateway Gateway address (or ::0 for no gateway)
|
|
|
|
*/
|
|
|
|
int add_ipv6_address ( struct net_device *netdev, struct in6_addr prefix,
|
|
|
|
int prefix_len, struct in6_addr address,
|
|
|
|
struct in6_addr gateway ) {
|
|
|
|
struct ipv6_miniroute *miniroute;
|
|
|
|
|
2007-01-04 03:28:30 +00:00
|
|
|
/* Clear any existing address for this net device */
|
|
|
|
del_ipv6_address ( netdev );
|
|
|
|
|
|
|
|
/* Add new miniroute */
|
|
|
|
miniroute = add_ipv6_miniroute ( netdev, prefix, prefix_len, address,
|
|
|
|
gateway );
|
|
|
|
if ( ! miniroute )
|
2006-08-19 15:58:22 +00:00
|
|
|
return -ENOMEM;
|
2007-01-04 03:28:30 +00:00
|
|
|
|
2006-08-19 15:58:22 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove IPv6 interface
|
|
|
|
*
|
|
|
|
* @v netdev Network device
|
|
|
|
*/
|
|
|
|
void del_ipv6_address ( struct net_device *netdev ) {
|
|
|
|
struct ipv6_miniroute *miniroute;
|
|
|
|
|
|
|
|
list_for_each_entry ( miniroute, &miniroutes, list ) {
|
|
|
|
if ( miniroute->netdev == netdev ) {
|
2007-01-04 03:28:30 +00:00
|
|
|
del_ipv6_miniroute ( miniroute );
|
2006-08-19 15:58:22 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculate TCPIP checksum
|
|
|
|
*
|
2007-05-19 18:39:40 +00:00
|
|
|
* @v iobuf I/O buffer
|
2006-08-19 15:58:22 +00:00
|
|
|
* @v tcpip TCP/IP protocol
|
|
|
|
*
|
|
|
|
* This function constructs the pseudo header and completes the checksum in the
|
|
|
|
* upper layer header.
|
|
|
|
*/
|
2007-05-19 18:39:40 +00:00
|
|
|
static uint16_t ipv6_tx_csum ( struct io_buffer *iobuf, uint16_t csum ) {
|
|
|
|
struct ip6_header *ip6hdr = iobuf->data;
|
2006-08-19 15:58:22 +00:00
|
|
|
struct ipv6_pseudo_header pshdr;
|
|
|
|
|
|
|
|
/* Calculate pseudo header */
|
|
|
|
memset ( &pshdr, 0, sizeof ( pshdr ) );
|
|
|
|
pshdr.src = ip6hdr->src;
|
|
|
|
pshdr.dest = ip6hdr->dest;
|
2007-05-19 18:39:40 +00:00
|
|
|
pshdr.len = htons ( iob_len ( iobuf ) - sizeof ( *ip6hdr ) );
|
2006-08-19 15:58:22 +00:00
|
|
|
pshdr.nxt_hdr = ip6hdr->nxt_hdr;
|
|
|
|
|
|
|
|
/* Update checksum value */
|
2007-01-03 20:48:52 +00:00
|
|
|
return tcpip_continue_chksum ( csum, &pshdr, sizeof ( pshdr ) );
|
2006-08-19 15:58:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dump IP6 header for debugging
|
|
|
|
*
|
|
|
|
* ip6hdr IPv6 header
|
|
|
|
*/
|
|
|
|
void ipv6_dump ( struct ip6_header *ip6hdr ) {
|
|
|
|
DBG ( "IP6 %p src %s dest %s nxt_hdr %d len %d\n", ip6hdr,
|
|
|
|
inet6_ntoa ( ip6hdr->src ), inet6_ntoa ( ip6hdr->dest ),
|
|
|
|
ip6hdr->nxt_hdr, ntohs ( ip6hdr->payload_len ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Transmit IP6 packet
|
|
|
|
*
|
2007-05-19 18:39:40 +00:00
|
|
|
* iobuf I/O buffer
|
2006-08-19 15:58:22 +00:00
|
|
|
* tcpip TCP/IP protocol
|
|
|
|
* st_dest Destination socket address
|
|
|
|
*
|
|
|
|
* This function prepends the IPv6 headers to the payload an transmits it.
|
2006-06-26 15:33:46 +00:00
|
|
|
*/
|
2007-05-19 18:39:40 +00:00
|
|
|
static int ipv6_tx ( struct io_buffer *iobuf,
|
2006-08-19 15:58:22 +00:00
|
|
|
struct tcpip_protocol *tcpip,
|
2007-01-03 20:48:52 +00:00
|
|
|
struct sockaddr_tcpip *st_dest,
|
2007-01-10 02:25:11 +00:00
|
|
|
struct net_device *netdev,
|
2007-01-03 20:48:52 +00:00
|
|
|
uint16_t *trans_csum ) {
|
2006-08-19 15:58:22 +00:00
|
|
|
struct sockaddr_in6 *dest = ( struct sockaddr_in6* ) st_dest;
|
|
|
|
struct in6_addr next_hop;
|
|
|
|
struct ipv6_miniroute *miniroute;
|
|
|
|
uint8_t ll_dest_buf[MAX_LL_ADDR_LEN];
|
|
|
|
const uint8_t *ll_dest = ll_dest_buf;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
/* Construct the IPv6 packet */
|
2007-05-19 18:39:40 +00:00
|
|
|
struct ip6_header *ip6hdr = iob_push ( iobuf, sizeof ( *ip6hdr ) );
|
2006-08-19 15:58:22 +00:00
|
|
|
memset ( ip6hdr, 0, sizeof ( *ip6hdr) );
|
|
|
|
ip6hdr->ver_traffic_class_flow_label = htonl ( 0x60000000 );//IP6_VERSION;
|
2007-05-19 18:39:40 +00:00
|
|
|
ip6hdr->payload_len = htons ( iob_len ( iobuf ) - sizeof ( *ip6hdr ) );
|
2006-08-19 15:58:22 +00:00
|
|
|
ip6hdr->nxt_hdr = tcpip->tcpip_proto;
|
|
|
|
ip6hdr->hop_limit = IP6_HOP_LIMIT; // 255
|
|
|
|
|
|
|
|
/* Determine the next hop address and interface
|
|
|
|
*
|
|
|
|
* TODO: Implement the routing table.
|
|
|
|
*/
|
|
|
|
next_hop = dest->sin6_addr;
|
|
|
|
list_for_each_entry ( miniroute, &miniroutes, list ) {
|
|
|
|
if ( ( strncmp ( &ip6hdr->dest, &miniroute->prefix,
|
|
|
|
miniroute->prefix_len ) == 0 ) ||
|
|
|
|
( IP6_EQUAL ( miniroute->gateway, ip6_none ) ) ) {
|
|
|
|
netdev = miniroute->netdev;
|
|
|
|
ip6hdr->src = miniroute->address;
|
|
|
|
if ( ! ( IS_UNSPECIFIED ( miniroute->gateway ) ) ) {
|
|
|
|
next_hop = miniroute->gateway;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* No network interface identified */
|
|
|
|
if ( !netdev ) {
|
|
|
|
DBG ( "No route to host %s\n", inet6_ntoa ( ip6hdr->dest ) );
|
2007-01-14 16:22:10 +00:00
|
|
|
rc = -ENETUNREACH;
|
2006-08-19 15:58:22 +00:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Complete the transport layer checksum */
|
2007-01-03 20:48:52 +00:00
|
|
|
if ( trans_csum )
|
2007-05-19 18:39:40 +00:00
|
|
|
*trans_csum = ipv6_tx_csum ( iobuf, *trans_csum );
|
2006-08-19 15:58:22 +00:00
|
|
|
|
|
|
|
/* Print IPv6 header */
|
|
|
|
ipv6_dump ( ip6hdr );
|
|
|
|
|
|
|
|
/* Resolve link layer address */
|
|
|
|
if ( next_hop.in6_u.u6_addr8[0] == 0xff ) {
|
|
|
|
ll_dest_buf[0] = 0x33;
|
|
|
|
ll_dest_buf[1] = 0x33;
|
|
|
|
ll_dest_buf[2] = next_hop.in6_u.u6_addr8[12];
|
|
|
|
ll_dest_buf[3] = next_hop.in6_u.u6_addr8[13];
|
|
|
|
ll_dest_buf[4] = next_hop.in6_u.u6_addr8[14];
|
|
|
|
ll_dest_buf[5] = next_hop.in6_u.u6_addr8[15];
|
|
|
|
} else {
|
|
|
|
/* Unicast address needs to be resolved by NDP */
|
|
|
|
if ( ( rc = ndp_resolve ( netdev, &next_hop, &ip6hdr->src,
|
|
|
|
ll_dest_buf ) ) != 0 ) {
|
|
|
|
DBG ( "No entry for %s\n", inet6_ntoa ( next_hop ) );
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Transmit packet */
|
2007-05-19 18:39:40 +00:00
|
|
|
return net_tx ( iobuf, netdev, &ipv6_protocol, ll_dest );
|
2006-08-19 15:58:22 +00:00
|
|
|
|
|
|
|
err:
|
2007-05-19 18:39:40 +00:00
|
|
|
free_iob ( iobuf );
|
2006-08-19 15:58:22 +00:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Process next IP6 header
|
|
|
|
*
|
2007-05-19 18:39:40 +00:00
|
|
|
* @v iobuf I/O buffer
|
2006-08-19 15:58:22 +00:00
|
|
|
* @v nxt_hdr Next header number
|
|
|
|
* @v src Source socket address
|
|
|
|
* @v dest Destination socket address
|
|
|
|
*
|
|
|
|
* Refer http://www.iana.org/assignments/ipv6-parameters for the numbers
|
|
|
|
*/
|
2007-05-19 18:39:40 +00:00
|
|
|
static int ipv6_process_nxt_hdr ( struct io_buffer *iobuf, uint8_t nxt_hdr,
|
2006-08-19 15:58:22 +00:00
|
|
|
struct sockaddr_tcpip *src, struct sockaddr_tcpip *dest ) {
|
|
|
|
switch ( nxt_hdr ) {
|
|
|
|
case IP6_HOPBYHOP:
|
|
|
|
case IP6_ROUTING:
|
|
|
|
case IP6_FRAGMENT:
|
|
|
|
case IP6_AUTHENTICATION:
|
|
|
|
case IP6_DEST_OPTS:
|
|
|
|
case IP6_ESP:
|
|
|
|
DBG ( "Function not implemented for header %d\n", nxt_hdr );
|
|
|
|
return -ENOSYS;
|
|
|
|
case IP6_ICMP6:
|
|
|
|
break;
|
|
|
|
case IP6_NO_HEADER:
|
|
|
|
DBG ( "No next header\n" );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* Next header is not a IPv6 extension header */
|
2007-05-19 18:39:40 +00:00
|
|
|
return tcpip_rx ( iobuf, nxt_hdr, src, dest, 0 /* fixme */ );
|
2006-06-26 15:33:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Process incoming IP6 packets
|
|
|
|
*
|
2007-05-19 18:39:40 +00:00
|
|
|
* @v iobuf I/O buffer
|
2006-08-19 15:58:22 +00:00
|
|
|
* @v netdev Network device
|
|
|
|
* @v ll_source Link-layer source address
|
|
|
|
*
|
|
|
|
* This function processes a IPv6 packet
|
2006-06-26 15:33:46 +00:00
|
|
|
*/
|
2007-05-19 18:39:40 +00:00
|
|
|
static int ipv6_rx ( struct io_buffer *iobuf,
|
2006-08-19 15:58:22 +00:00
|
|
|
struct net_device *netdev,
|
|
|
|
const void *ll_source ) {
|
|
|
|
|
2007-05-19 18:39:40 +00:00
|
|
|
struct ip6_header *ip6hdr = iobuf->data;
|
2006-08-19 15:58:22 +00:00
|
|
|
union {
|
|
|
|
struct sockaddr_in6 sin6;
|
|
|
|
struct sockaddr_tcpip st;
|
|
|
|
} src, dest;
|
|
|
|
|
|
|
|
/* Sanity check */
|
2007-05-19 18:39:40 +00:00
|
|
|
if ( iob_len ( iobuf ) < sizeof ( *ip6hdr ) ) {
|
|
|
|
DBG ( "Packet too short (%d bytes)\n", iob_len ( iobuf ) );
|
2006-08-19 15:58:22 +00:00
|
|
|
goto drop;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* TODO: Verify checksum */
|
|
|
|
|
|
|
|
/* Print IP6 header for debugging */
|
|
|
|
ipv6_dump ( ip6hdr );
|
|
|
|
|
|
|
|
/* Check header version */
|
|
|
|
if ( ip6hdr->ver_traffic_class_flow_label & 0xf0000000 != 0x60000000 ) {
|
|
|
|
DBG ( "Invalid protocol version\n" );
|
|
|
|
goto drop;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check the payload length */
|
2007-05-19 18:39:40 +00:00
|
|
|
if ( ntohs ( ip6hdr->payload_len ) > iob_len ( iobuf ) ) {
|
2006-08-19 15:58:22 +00:00
|
|
|
DBG ( "Inconsistent packet length (%d bytes)\n",
|
|
|
|
ip6hdr->payload_len );
|
|
|
|
goto drop;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Ignore the traffic class and flow control values */
|
|
|
|
|
|
|
|
/* Construct socket address */
|
|
|
|
memset ( &src, 0, sizeof ( src ) );
|
|
|
|
src.sin6.sin_family = AF_INET6;
|
|
|
|
src.sin6.sin6_addr = ip6hdr->src;
|
|
|
|
memset ( &dest, 0, sizeof ( dest ) );
|
|
|
|
dest.sin6.sin_family = AF_INET6;
|
|
|
|
dest.sin6.sin6_addr = ip6hdr->dest;
|
|
|
|
|
|
|
|
/* Strip header */
|
2007-05-19 18:39:40 +00:00
|
|
|
iob_unput ( iobuf, iob_len ( iobuf ) - ntohs ( ip6hdr->payload_len ) -
|
2006-08-19 15:58:22 +00:00
|
|
|
sizeof ( *ip6hdr ) );
|
2007-05-19 18:39:40 +00:00
|
|
|
iob_pull ( iobuf, sizeof ( *ip6hdr ) );
|
2006-08-19 15:58:22 +00:00
|
|
|
|
|
|
|
/* Send it to the transport layer */
|
2007-05-19 18:39:40 +00:00
|
|
|
return ipv6_process_nxt_hdr ( iobuf, ip6hdr->nxt_hdr, &src.st, &dest.st );
|
2006-08-19 15:58:22 +00:00
|
|
|
|
|
|
|
drop:
|
|
|
|
DBG ( "Packet dropped\n" );
|
2007-05-19 18:39:40 +00:00
|
|
|
free_iob ( iobuf );
|
2006-08-19 15:58:22 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Print a IP6 address as xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx
|
|
|
|
*/
|
|
|
|
char * inet6_ntoa ( struct in6_addr in6 ) {
|
|
|
|
static char buf[40];
|
|
|
|
uint16_t *bytes = ( uint16_t* ) &in6;
|
|
|
|
sprintf ( buf, "%x:%x:%x:%x:%x:%x:%x:%x", bytes[0], bytes[1], bytes[2],
|
|
|
|
bytes[3], bytes[4], bytes[5], bytes[6], bytes[7] );
|
|
|
|
return buf;
|
2006-08-01 20:27:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const char * ipv6_ntoa ( const void *net_addr ) {
|
2006-08-19 15:58:22 +00:00
|
|
|
return inet6_ntoa ( * ( ( struct in6_addr * ) net_addr ) );
|
2006-08-01 20:27:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** IPv6 protocol */
|
2006-08-09 04:42:14 +00:00
|
|
|
struct net_protocol ipv6_protocol __net_protocol = {
|
2006-08-19 15:58:22 +00:00
|
|
|
.name = "IPv6",
|
2006-08-01 20:27:26 +00:00
|
|
|
.net_proto = htons ( ETH_P_IPV6 ),
|
|
|
|
.net_addr_len = sizeof ( struct in6_addr ),
|
|
|
|
.rx = ipv6_rx,
|
|
|
|
.ntoa = ipv6_ntoa,
|
|
|
|
};
|
|
|
|
|
|
|
|
/** IPv6 TCPIP net protocol */
|
2006-08-09 04:42:14 +00:00
|
|
|
struct tcpip_net_protocol ipv6_tcpip_protocol __tcpip_net_protocol = {
|
2006-08-02 00:02:21 +00:00
|
|
|
.name = "IPv6",
|
2006-08-01 20:27:26 +00:00
|
|
|
.sa_family = AF_INET6,
|
|
|
|
.tx = ipv6_tx,
|
|
|
|
};
|