2006-04-19 11:32:24 +00: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
|
|
|
|
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <byteswap.h>
|
2006-08-09 01:24:32 +00:00
|
|
|
#include <errno.h>
|
2006-04-19 11:32:24 +00:00
|
|
|
#include <assert.h>
|
2006-05-14 02:51:55 +00:00
|
|
|
#include <vsprintf.h>
|
2006-04-24 15:33:06 +00:00
|
|
|
#include <gpxe/if_arp.h>
|
2006-04-19 11:32:24 +00:00
|
|
|
#include <gpxe/if_ether.h>
|
|
|
|
#include <gpxe/netdevice.h>
|
|
|
|
#include <gpxe/pkbuff.h>
|
2006-04-24 15:33:06 +00:00
|
|
|
#include <gpxe/ethernet.h>
|
2006-04-19 11:32:24 +00:00
|
|
|
|
|
|
|
/** @file
|
|
|
|
*
|
|
|
|
* Ethernet protocol
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** Ethernet broadcast MAC address */
|
2006-04-24 15:33:06 +00:00
|
|
|
static uint8_t eth_broadcast[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
|
2006-04-19 11:32:24 +00:00
|
|
|
|
|
|
|
/**
|
2006-06-16 00:19:46 +00:00
|
|
|
* Transmit Ethernet packet
|
2006-04-19 11:32:24 +00:00
|
|
|
*
|
2006-06-16 00:19:46 +00:00
|
|
|
* @v pkb Packet buffer
|
|
|
|
* @v netdev Network device
|
|
|
|
* @v net_protocol Network-layer protocol
|
|
|
|
* @v ll_dest Link-layer destination address
|
2006-04-19 11:32:24 +00:00
|
|
|
*
|
2006-06-16 00:19:46 +00:00
|
|
|
* Prepends the Ethernet link-layer header and transmits the packet.
|
2006-04-24 15:33:06 +00:00
|
|
|
*/
|
2006-06-17 22:36:27 +00:00
|
|
|
static int eth_tx ( struct pk_buff *pkb, struct net_device *netdev,
|
|
|
|
struct net_protocol *net_protocol, const void *ll_dest ) {
|
2006-06-16 13:17:40 +00:00
|
|
|
struct ethhdr *ethhdr = pkb_push ( pkb, sizeof ( *ethhdr ) );
|
2006-06-16 00:19:46 +00:00
|
|
|
|
2006-06-17 22:36:27 +00:00
|
|
|
/* Build Ethernet header */
|
2006-06-16 00:19:46 +00:00
|
|
|
memcpy ( ethhdr->h_dest, ll_dest, ETH_ALEN );
|
|
|
|
memcpy ( ethhdr->h_source, netdev->ll_addr, ETH_ALEN );
|
|
|
|
ethhdr->h_protocol = net_protocol->net_proto;
|
2006-06-17 22:36:27 +00:00
|
|
|
|
|
|
|
/* Hand off to network device */
|
|
|
|
return netdev_tx ( netdev, pkb );
|
2006-04-24 15:33:06 +00:00
|
|
|
}
|
|
|
|
|
2006-04-19 11:32:24 +00:00
|
|
|
/**
|
2006-06-17 22:36:27 +00:00
|
|
|
* Process received Ethernet packet
|
2006-04-19 11:32:24 +00:00
|
|
|
*
|
|
|
|
* @v pkb Packet buffer
|
2006-06-17 22:36:27 +00:00
|
|
|
* @v netdev Network device
|
2006-04-19 11:32:24 +00:00
|
|
|
*
|
2006-06-17 22:36:27 +00:00
|
|
|
* Strips off the Ethernet link-layer header and passes up to the
|
|
|
|
* network-layer protocol.
|
2006-04-19 11:32:24 +00:00
|
|
|
*/
|
2006-08-09 01:24:32 +00:00
|
|
|
static int eth_rx ( struct pk_buff *pkb, struct net_device *netdev ) {
|
2006-04-19 11:32:24 +00:00
|
|
|
struct ethhdr *ethhdr = pkb->data;
|
|
|
|
|
2006-06-17 22:36:27 +00:00
|
|
|
/* Sanity check */
|
|
|
|
if ( pkb_len ( pkb ) < sizeof ( *ethhdr ) ) {
|
|
|
|
DBG ( "Ethernet packet too short (%d bytes)\n",
|
|
|
|
pkb_len ( pkb ) );
|
2006-08-09 01:24:32 +00:00
|
|
|
free_pkb ( pkb );
|
|
|
|
return -EINVAL;
|
2006-04-19 11:32:24 +00:00
|
|
|
}
|
2006-06-17 22:36:27 +00:00
|
|
|
|
|
|
|
/* Strip off Ethernet header */
|
|
|
|
pkb_pull ( pkb, sizeof ( *ethhdr ) );
|
|
|
|
|
|
|
|
/* Hand off to network-layer protocol */
|
2006-08-09 01:24:32 +00:00
|
|
|
return net_rx ( pkb, netdev, ethhdr->h_protocol, ethhdr->h_source );
|
2006-04-19 11:32:24 +00:00
|
|
|
}
|
2006-04-24 15:33:06 +00:00
|
|
|
|
2006-04-28 14:13:50 +00:00
|
|
|
/**
|
|
|
|
* Transcribe Ethernet address
|
|
|
|
*
|
|
|
|
* @v ll_addr Link-layer address
|
|
|
|
* @ret string Link-layer address in human-readable format
|
|
|
|
*/
|
|
|
|
static const char * eth_ntoa ( const void *ll_addr ) {
|
|
|
|
static char buf[18]; /* "00:00:00:00:00:00" */
|
2006-06-16 00:19:46 +00:00
|
|
|
const uint8_t *eth_addr = ll_addr;
|
2006-04-28 14:13:50 +00:00
|
|
|
|
2006-05-27 13:38:49 +00:00
|
|
|
sprintf ( buf, "%02x:%02x:%02x:%02x:%02x:%02x",
|
2006-04-28 14:13:50 +00:00
|
|
|
eth_addr[0], eth_addr[1], eth_addr[2],
|
|
|
|
eth_addr[3], eth_addr[4], eth_addr[5] );
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2006-04-24 15:33:06 +00:00
|
|
|
/** Ethernet protocol */
|
2006-08-09 04:42:14 +00:00
|
|
|
struct ll_protocol ethernet_protocol __ll_protocol = {
|
2006-06-16 00:19:46 +00:00
|
|
|
.name = "Ethernet",
|
|
|
|
.ll_proto = htons ( ARPHRD_ETHER ),
|
|
|
|
.ll_addr_len = ETH_ALEN,
|
|
|
|
.ll_broadcast = eth_broadcast,
|
2006-06-17 22:36:27 +00:00
|
|
|
.tx = eth_tx,
|
|
|
|
.rx = eth_rx,
|
2006-06-16 00:19:46 +00:00
|
|
|
.ntoa = eth_ntoa,
|
2006-04-24 15:33:06 +00:00
|
|
|
};
|