2006-04-19 13:32:24 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2006 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
|
2012-07-20 20:55:45 +02:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
|
|
|
* 02110-1301, USA.
|
2015-03-02 14:29:46 +01:00
|
|
|
*
|
|
|
|
* You can also choose to distribute this program under the terms of
|
|
|
|
* the Unmodified Binary Distribution Licence (as given in the file
|
|
|
|
* COPYING.UBDL), provided that you have satisfied its requirements.
|
2006-04-19 13:32:24 +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
|
|
|
|
2006-04-19 13:32:24 +02:00
|
|
|
#include <stdint.h>
|
2014-06-02 00:26:20 +02:00
|
|
|
#include <stdlib.h>
|
2007-01-19 02:13:12 +01:00
|
|
|
#include <stdio.h>
|
2006-04-19 13:32:24 +02:00
|
|
|
#include <string.h>
|
|
|
|
#include <byteswap.h>
|
2006-08-09 03:24:32 +02:00
|
|
|
#include <errno.h>
|
2006-04-19 13:32:24 +02:00
|
|
|
#include <assert.h>
|
2010-04-19 21:16:01 +02:00
|
|
|
#include <ipxe/if_arp.h>
|
|
|
|
#include <ipxe/if_ether.h>
|
|
|
|
#include <ipxe/in.h>
|
|
|
|
#include <ipxe/netdevice.h>
|
|
|
|
#include <ipxe/iobuf.h>
|
|
|
|
#include <ipxe/ethernet.h>
|
2006-04-19 13:32:24 +02:00
|
|
|
|
|
|
|
/** @file
|
|
|
|
*
|
|
|
|
* Ethernet protocol
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** Ethernet broadcast MAC address */
|
2012-08-30 16:58:58 +02:00
|
|
|
uint8_t eth_broadcast[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
|
2006-04-19 13:32:24 +02:00
|
|
|
|
2015-06-25 15:37:18 +02:00
|
|
|
/**
|
|
|
|
* Check if Ethernet packet has an 802.3 LLC header
|
|
|
|
*
|
|
|
|
* @v ethhdr Ethernet header
|
|
|
|
* @ret is_llc Packet has 802.3 LLC header
|
|
|
|
*/
|
|
|
|
static inline int eth_is_llc_packet ( struct ethhdr *ethhdr ) {
|
|
|
|
uint8_t len_msb;
|
|
|
|
|
|
|
|
/* Check if the protocol field contains a value short enough
|
|
|
|
* to be a frame length. The slightly convoluted form of the
|
|
|
|
* comparison is designed to reduce to a single x86
|
|
|
|
* instruction.
|
|
|
|
*/
|
|
|
|
len_msb = *( ( uint8_t * ) ðhdr->h_protocol );
|
|
|
|
return ( len_msb < 0x06 );
|
|
|
|
}
|
|
|
|
|
2006-04-19 13:32:24 +02:00
|
|
|
/**
|
2008-08-20 04:21:37 +02:00
|
|
|
* Add Ethernet link-layer header
|
2006-04-19 13:32:24 +02:00
|
|
|
*
|
2009-06-15 20:37:43 +02:00
|
|
|
* @v netdev Network device
|
2007-05-19 20:39:40 +02:00
|
|
|
* @v iobuf I/O buffer
|
2006-06-16 02:19:46 +02:00
|
|
|
* @v ll_dest Link-layer destination address
|
2008-10-15 05:17:48 +02:00
|
|
|
* @v ll_source Source link-layer address
|
|
|
|
* @v net_proto Network-layer protocol, in network-byte order
|
|
|
|
* @ret rc Return status code
|
2006-04-24 17:33:06 +02:00
|
|
|
*/
|
2012-03-02 19:02:03 +01:00
|
|
|
int eth_push ( struct net_device *netdev __unused, struct io_buffer *iobuf,
|
|
|
|
const void *ll_dest, const void *ll_source,
|
|
|
|
uint16_t net_proto ) {
|
2007-05-19 20:39:40 +02:00
|
|
|
struct ethhdr *ethhdr = iob_push ( iobuf, sizeof ( *ethhdr ) );
|
2006-06-16 02:19:46 +02:00
|
|
|
|
2006-06-18 00:36:27 +02:00
|
|
|
/* Build Ethernet header */
|
2006-06-16 02:19:46 +02:00
|
|
|
memcpy ( ethhdr->h_dest, ll_dest, ETH_ALEN );
|
2008-10-15 05:17:48 +02:00
|
|
|
memcpy ( ethhdr->h_source, ll_source, ETH_ALEN );
|
|
|
|
ethhdr->h_protocol = net_proto;
|
2006-06-18 00:36:27 +02:00
|
|
|
|
2008-08-20 04:21:37 +02:00
|
|
|
return 0;
|
2006-04-24 17:33:06 +02:00
|
|
|
}
|
|
|
|
|
2006-04-19 13:32:24 +02:00
|
|
|
/**
|
2008-08-20 04:21:37 +02:00
|
|
|
* Remove Ethernet link-layer header
|
2006-04-19 13:32:24 +02:00
|
|
|
*
|
2009-06-15 20:37:43 +02:00
|
|
|
* @v netdev Network device
|
2008-08-20 04:21:37 +02:00
|
|
|
* @v iobuf I/O buffer
|
2008-10-15 05:17:48 +02:00
|
|
|
* @ret ll_dest Link-layer destination address
|
|
|
|
* @ret ll_source Source link-layer address
|
|
|
|
* @ret net_proto Network-layer protocol, in network-byte order
|
2011-07-15 19:48:46 +02:00
|
|
|
* @ret flags Packet flags
|
2008-08-20 04:21:37 +02:00
|
|
|
* @ret rc Return status code
|
2006-04-19 13:32:24 +02:00
|
|
|
*/
|
2012-03-02 19:02:03 +01:00
|
|
|
int eth_pull ( struct net_device *netdev __unused, struct io_buffer *iobuf,
|
|
|
|
const void **ll_dest, const void **ll_source,
|
|
|
|
uint16_t *net_proto, unsigned int *flags ) {
|
2007-05-19 20:39:40 +02:00
|
|
|
struct ethhdr *ethhdr = iobuf->data;
|
2015-06-25 15:37:18 +02:00
|
|
|
uint16_t *llc_proto;
|
2006-04-19 13:32:24 +02:00
|
|
|
|
2015-06-25 15:37:18 +02:00
|
|
|
/* Sanity check. While in theory we could receive a one-byte
|
|
|
|
* packet, this will never happen in practice and performing
|
|
|
|
* the combined length check here avoids the need for an
|
|
|
|
* additional comparison if we detect an LLC frame.
|
|
|
|
*/
|
|
|
|
if ( iob_len ( iobuf ) < ( sizeof ( *ethhdr ) + sizeof ( *llc_proto ))){
|
2007-12-07 01:11:43 +01:00
|
|
|
DBG ( "Ethernet packet too short (%zd bytes)\n",
|
2007-05-19 20:39:40 +02:00
|
|
|
iob_len ( iobuf ) );
|
2006-08-09 03:24:32 +02:00
|
|
|
return -EINVAL;
|
2006-04-19 13:32:24 +02:00
|
|
|
}
|
2006-06-18 00:36:27 +02:00
|
|
|
|
|
|
|
/* Strip off Ethernet header */
|
2007-05-19 20:39:40 +02:00
|
|
|
iob_pull ( iobuf, sizeof ( *ethhdr ) );
|
2006-06-18 00:36:27 +02:00
|
|
|
|
2008-08-20 04:21:37 +02:00
|
|
|
/* Fill in required fields */
|
2008-10-15 05:17:48 +02:00
|
|
|
*ll_dest = ethhdr->h_dest;
|
2008-08-20 04:21:37 +02:00
|
|
|
*ll_source = ethhdr->h_source;
|
2008-10-15 05:17:48 +02:00
|
|
|
*net_proto = ethhdr->h_protocol;
|
2011-07-15 19:48:46 +02:00
|
|
|
*flags = ( ( is_multicast_ether_addr ( ethhdr->h_dest ) ?
|
|
|
|
LL_MULTICAST : 0 ) |
|
|
|
|
( is_broadcast_ether_addr ( ethhdr->h_dest ) ?
|
|
|
|
LL_BROADCAST : 0 ) );
|
2008-08-20 04:21:37 +02:00
|
|
|
|
2015-06-25 15:37:18 +02:00
|
|
|
/* If this is an LLC frame (with a length in place of the
|
|
|
|
* protocol field), then use the next two bytes (which happen
|
|
|
|
* to be the LLC DSAP and SSAP) as the protocol. This allows
|
|
|
|
* for minimal-overhead support for receiving (rare) LLC
|
|
|
|
* frames, without requiring a full LLC protocol layer.
|
|
|
|
*/
|
|
|
|
if ( eth_is_llc_packet ( ethhdr ) ) {
|
|
|
|
llc_proto = ( ðhdr->h_protocol + 1 );
|
|
|
|
*net_proto = *llc_proto;
|
|
|
|
}
|
|
|
|
|
2008-08-20 04:21:37 +02:00
|
|
|
return 0;
|
2006-04-19 13:32:24 +02:00
|
|
|
}
|
2006-04-24 17:33:06 +02:00
|
|
|
|
2009-08-12 00:40:27 +02:00
|
|
|
/**
|
|
|
|
* Initialise Ethernet address
|
|
|
|
*
|
|
|
|
* @v hw_addr Hardware address
|
|
|
|
* @v ll_addr Link-layer address
|
|
|
|
*/
|
|
|
|
void eth_init_addr ( const void *hw_addr, void *ll_addr ) {
|
|
|
|
memcpy ( ll_addr, hw_addr, ETH_ALEN );
|
|
|
|
}
|
|
|
|
|
2014-06-02 00:26:20 +02:00
|
|
|
/**
|
|
|
|
* Generate random Ethernet address
|
|
|
|
*
|
|
|
|
* @v hw_addr Generated hardware address
|
|
|
|
*/
|
|
|
|
void eth_random_addr ( void *hw_addr ) {
|
|
|
|
uint8_t *addr = hw_addr;
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
for ( i = 0 ; i < ETH_ALEN ; i++ )
|
|
|
|
addr[i] = random();
|
|
|
|
addr[0] &= ~0x01; /* Clear multicast bit */
|
|
|
|
addr[0] |= 0x02; /* Set locally-assigned bit */
|
|
|
|
}
|
|
|
|
|
2006-04-28 16:13:50 +02:00
|
|
|
/**
|
|
|
|
* Transcribe Ethernet address
|
|
|
|
*
|
2008-10-15 03:00:44 +02:00
|
|
|
* @v ll_addr Link-layer address
|
|
|
|
* @ret string Link-layer address in human-readable format
|
2006-04-28 16:13:50 +02:00
|
|
|
*/
|
2006-09-15 14:34:34 +02:00
|
|
|
const char * eth_ntoa ( const void *ll_addr ) {
|
2006-04-28 16:13:50 +02:00
|
|
|
static char buf[18]; /* "00:00:00:00:00:00" */
|
2006-06-16 02:19:46 +02:00
|
|
|
const uint8_t *eth_addr = ll_addr;
|
2006-04-28 16:13:50 +02:00
|
|
|
|
2006-05-27 15:38:49 +02:00
|
|
|
sprintf ( buf, "%02x:%02x:%02x:%02x:%02x:%02x",
|
2006-04-28 16:13:50 +02:00
|
|
|
eth_addr[0], eth_addr[1], eth_addr[2],
|
|
|
|
eth_addr[3], eth_addr[4], eth_addr[5] );
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2008-10-15 03:00:44 +02:00
|
|
|
/**
|
|
|
|
* Hash multicast address
|
|
|
|
*
|
|
|
|
* @v af Address family
|
|
|
|
* @v net_addr Network-layer address
|
|
|
|
* @v ll_addr Link-layer address to fill in
|
|
|
|
* @ret rc Return status code
|
|
|
|
*/
|
2009-08-12 01:40:26 +02:00
|
|
|
int eth_mc_hash ( unsigned int af, const void *net_addr, void *ll_addr ) {
|
2008-10-15 03:00:44 +02:00
|
|
|
const uint8_t *net_addr_bytes = net_addr;
|
|
|
|
uint8_t *ll_addr_bytes = ll_addr;
|
|
|
|
|
|
|
|
switch ( af ) {
|
|
|
|
case AF_INET:
|
|
|
|
ll_addr_bytes[0] = 0x01;
|
|
|
|
ll_addr_bytes[1] = 0x00;
|
|
|
|
ll_addr_bytes[2] = 0x5e;
|
|
|
|
ll_addr_bytes[3] = net_addr_bytes[1] & 0x7f;
|
|
|
|
ll_addr_bytes[4] = net_addr_bytes[2];
|
|
|
|
ll_addr_bytes[5] = net_addr_bytes[3];
|
2009-01-21 04:37:36 +01:00
|
|
|
return 0;
|
2013-08-31 04:23:24 +02:00
|
|
|
case AF_INET6:
|
|
|
|
ll_addr_bytes[0] = 0x33;
|
|
|
|
ll_addr_bytes[1] = 0x33;
|
|
|
|
memcpy ( &ll_addr_bytes[2], &net_addr_bytes[12], 4 );
|
|
|
|
return 0;
|
2008-10-15 03:00:44 +02:00
|
|
|
default:
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-23 23:14:05 +02:00
|
|
|
/**
|
|
|
|
* Generate Ethernet-compatible compressed link-layer address
|
|
|
|
*
|
|
|
|
* @v ll_addr Link-layer address
|
|
|
|
* @v eth_addr Ethernet-compatible address to fill in
|
|
|
|
*/
|
|
|
|
int eth_eth_addr ( const void *ll_addr, void *eth_addr ) {
|
|
|
|
memcpy ( eth_addr, ll_addr, ETH_ALEN );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-08-30 20:05:03 +02:00
|
|
|
/**
|
|
|
|
* Generate EUI-64 address
|
|
|
|
*
|
|
|
|
* @v ll_addr Link-layer address
|
|
|
|
* @v eui64 EUI-64 address to fill in
|
|
|
|
* @ret rc Return status code
|
|
|
|
*/
|
|
|
|
int eth_eui64 ( const void *ll_addr, void *eui64 ) {
|
|
|
|
|
|
|
|
memcpy ( ( eui64 + 0 ), ( ll_addr + 0 ), 3 );
|
|
|
|
memcpy ( ( eui64 + 5 ), ( ll_addr + 3 ), 3 );
|
|
|
|
*( ( uint16_t * ) ( eui64 + 3 ) ) = htons ( 0xfffe );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-04-24 17:33:06 +02:00
|
|
|
/** Ethernet protocol */
|
2006-08-09 06:42:14 +02:00
|
|
|
struct ll_protocol ethernet_protocol __ll_protocol = {
|
2006-06-16 02:19:46 +02:00
|
|
|
.name = "Ethernet",
|
|
|
|
.ll_proto = htons ( ARPHRD_ETHER ),
|
2009-08-12 00:40:27 +02:00
|
|
|
.hw_addr_len = ETH_ALEN,
|
2006-06-16 02:19:46 +02:00
|
|
|
.ll_addr_len = ETH_ALEN,
|
2007-07-02 21:05:58 +02:00
|
|
|
.ll_header_len = ETH_HLEN,
|
2008-08-20 04:21:37 +02:00
|
|
|
.push = eth_push,
|
|
|
|
.pull = eth_pull,
|
2009-08-12 00:40:27 +02:00
|
|
|
.init_addr = eth_init_addr,
|
2006-06-16 02:19:46 +02:00
|
|
|
.ntoa = eth_ntoa,
|
2008-10-15 03:00:44 +02:00
|
|
|
.mc_hash = eth_mc_hash,
|
2009-10-23 23:14:05 +02:00
|
|
|
.eth_addr = eth_eth_addr,
|
2013-08-30 20:05:03 +02:00
|
|
|
.eui64 = eth_eui64,
|
2006-04-24 17:33:06 +02:00
|
|
|
};
|
2009-07-17 23:48:31 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Allocate Ethernet device
|
|
|
|
*
|
|
|
|
* @v priv_size Size of driver private data
|
|
|
|
* @ret netdev Network device, or NULL
|
|
|
|
*/
|
|
|
|
struct net_device * alloc_etherdev ( size_t priv_size ) {
|
|
|
|
struct net_device *netdev;
|
|
|
|
|
|
|
|
netdev = alloc_netdev ( priv_size );
|
|
|
|
if ( netdev ) {
|
|
|
|
netdev->ll_protocol = ðernet_protocol;
|
|
|
|
netdev->ll_broadcast = eth_broadcast;
|
|
|
|
netdev->max_pkt_len = ETH_FRAME_LEN;
|
|
|
|
}
|
|
|
|
return netdev;
|
|
|
|
}
|
2010-05-10 17:30:10 +02:00
|
|
|
|
2015-03-04 19:48:19 +01:00
|
|
|
/* Drag in objects via ethernet_protocol */
|
|
|
|
REQUIRING_SYMBOL ( ethernet_protocol );
|
|
|
|
|
2015-03-04 18:22:07 +01:00
|
|
|
/* Drag in Ethernet configuration */
|
|
|
|
REQUIRE_OBJECT ( config_ethernet );
|
|
|
|
|
2010-05-10 17:30:10 +02:00
|
|
|
/* Drag in Ethernet slow protocols */
|
|
|
|
REQUIRE_OBJECT ( eth_slow );
|