/*
* Copyright (c) 2009 Lukas Mejdrech
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* - The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/** @addtogroup eth
* @{
*/
/** @file
* Ethernet module implementation.
* @see eth.h
*/
#include <async.h>
#include <malloc.h>
#include <mem.h>
#include <stdio.h>
#include <ipc/ipc.h>
#include <ipc/services.h>
#include "../../err.h"
#include "../../messages.h"
#include "../../modules.h"
#include "../../include/byteorder.h"
#include "../../include/crc.h"
#include "../../include/ethernet_lsap.h"
#include "../../include/ethernet_protocols.h"
#include "../../include/protocol_map.h"
#include "../../netif/device.h"
#include "../../structures/measured_strings.h"
#include "../../structures/packet/packet.h"
#include "../../structures/packet/packet_client.h"
#include "eth.h"
#include "eth_header.h"
//#include "eth_messages.h"
#include "eth_module.h"
#define ETH_PREFIX ( sizeof( eth_header_t ) + sizeof( eth_header_lsap_t ) + sizeof( eth_header_snap_t ))
#define ETH_SUFFIX sizeof( eth_fcs_t )
#define ETH_MAX_CONTENT 1500
#define ETH_MIN_CONTENT 46
/** Returns the device identifier message parameter.
*/
#define IPC_GET_DEVICE( call ) ( device_id_t ) IPC_GET_ARG1( * call )
/** Returns the packet identifier message parameter.
*/
#define IPC_GET_PACKET( call ) ( packet_id_t ) IPC_GET_ARG2( * call )
#define IPC_GET_STATE( call ) ( device_state_t ) IPC_GET_ARG2( * call )
/** Returns the protocol service message parameter.
*/
#define IPC_GET_PROTO( call ) ( services_t ) IPC_GET_ARG2( * call )
/** Returns the device driver service message parameter.
*/
#define IPC_GET_SERVICE( call ) ( services_t ) IPC_GET_ARG2( * call )
#define IPC_GET_MTU( call ) ( size_t ) IPC_GET_ARG3( * call )
#define IPC_GET_PHONE( call ) ( int ) IPC_GET_ARG5( * call )
#define IPC_SET_ADDR( answer ) (( size_t * ) & IPC_GET_ARG1( * answer ))
#define IPC_SET_PREFIX( answer ) (( size_t * ) & IPC_GET_ARG2( * answer ))
#define IPC_SET_CONTENT( answer ) (( size_t * ) & IPC_GET_ARG3( * answer ))
#define IPC_SET_SUFFIX( answer ) (( size_t * ) & IPC_GET_ARG4( * answer ))
typedef enum eth_addr_type eth_addr_type_t;
typedef eth_addr_type_t * eth_addr_type_ref;
enum eth_addr_type{
ETH_LOCAL_ADDR,
ETH_BROADCAST_ADDR
};
/** Ethernet global data.
*/
eth_globals_t eth_globals;
/** Processes IPC messages from the registered device driver modules in an infinite loop.
* @param iid The message identifier. Input parameter.
* @param icall The message parameters. Input/output parameter.
*/
void eth_receiver( ipc_callid_t iid, ipc_call_t * icall );
DEVICE_MAP_IMPLEMENT( eth_devices, eth_device_t )
INT_MAP_IMPLEMENT( eth_protos, eth_proto_t )
int eth_device_message( device_id_t device_id, services_t service, size_t mtu );
int eth_receive_message( device_id_t device_id, packet_t packet );
int eth_packet_space_message( device_id_t device_id, size_t * addr_len, size_t * prefix, size_t * content, size_t * suffix );
int eth_addr_message( device_id_t device_id, eth_addr_type_t type, measured_string_ref * address );
int eth_register_message( services_t service, int phone );
int eth_send_message( device_id_t device_id, packet_t packet, services_t sender );
int eth_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count );
void eth_receiver( ipc_callid_t iid, ipc_call_t * icall );
eth_proto_ref eth_process_packet( int dummy, packet_t packet );
int eth_prepare_packet( int dummy, packet_t packet, uint8_t * src_addr, int ethertype );
int eth_initialize( void ){
ERROR_DECLARE;
rwlock_initialize( & eth_globals.devices_lock );
rwlock_initialize( & eth_globals.protos_lock );
rwlock_write_lock( & eth_globals.devices_lock );
rwlock_write_lock( & eth_globals.protos_lock );
eth_globals.broadcast_addr = measured_string_create_bulk( "\xFF\xFF\xFF\xFF\xFF\xFF", CONVERT_SIZE( uint8_t, char, ETH_ADDR ));
if( ! eth_globals.broadcast_addr ) return ENOMEM;
ERROR_PROPAGATE( eth_devices_initialize( & eth_globals.devices ));
if( ERROR_OCCURRED( eth_protos_initialize( & eth_globals.protos ))){
eth_devices_destroy( & eth_globals.devices );
return ERROR_CODE;
}
rwlock_write_unlock( & eth_globals.protos_lock );
rwlock_write_unlock( & eth_globals.devices_lock );
return EOK;
}
int eth_device_message( device_id_t device_id, services_t service, size_t mtu ){
ERROR_DECLARE;
aid_t message;
ipc_call_t answer;
eth_device_ref device;
ipcarg_t result;
int index;
rwlock_write_lock( & eth_globals.devices_lock );
// an existing device?
device = eth_devices_find( & eth_globals.devices, device_id );
if( device ){
if( device->service != service ){
printf( "\nDevice %d already exists", device
->device_id
);
rwlock_write_unlock( & eth_globals.devices_lock );
return EEXIST;
}else{
// update mtu
device->mtu = mtu;
printf( "\nDevice %d already exists:\tMTU\t= %d", device
->device_id
, device
->mtu
);
}
}else{
// create a new device
device
= ( eth_device_ref
) malloc( sizeof( eth_device_t
));
if( ! device ) return ENOMEM;
device->device_id = device_id;
device->service = service;
device->mtu = ( mtu > 0 ) ? mtu : ETH_MAX_CONTENT;
// TODO get dummy setting
device->dummy = 0;
// bind the device driver
device->phone = bind_service( device->service, device->device_id, SERVICE_ETHERNET, 0, eth_receiver );
if( device->phone < 0 ){
rwlock_write_unlock( & eth_globals.devices_lock );
return device->phone;
}
// get hardware address
message = async_send_1( device->phone, NET_NETIF_GET_ADDR, device->device_id, & answer );
if( ERROR_OCCURRED( measured_strings_return( device->phone, & device->addr, & device->addr_data, 1 ))){
rwlock_write_unlock( & eth_globals.devices_lock );
async_wait_for( message, NULL );
return ERROR_CODE;
}
async_wait_for( message, & result );
if( ERROR_OCCURRED( result )){
rwlock_write_unlock( & eth_globals.devices_lock );
free( device
->addr_data
);
return ERROR_CODE;
}
// add to the cache
index = eth_devices_add( & eth_globals.devices, device->device_id, device );
if( index < 0 ){
rwlock_write_unlock( & eth_globals.devices_lock );
free( device
->addr_data
);
return index;
}
printf( "\nNew device registered:\n\tid\t= %d\n\tservice\t= %d\n\tMTU\t= %d\n\taddress\t= %X:%X:%X:%X:%X:%X", device
->device_id
, device
->service
, device
->mtu
, device
->addr_data
[ 0 ], device
->addr_data
[ 1 ], device
->addr_data
[ 2 ], device
->addr_data
[ 3 ], device
->addr_data
[ 4 ], device
->addr_data
[ 5 ] );
}
rwlock_write_unlock( & eth_globals.devices_lock );
return EOK;
}
eth_proto_ref eth_process_packet( int dummy, packet_t packet ){
ERROR_DECLARE;
eth_header_ex_ref header;
size_t length;
int type;
size_t prefix;
size_t suffix;
eth_fcs_ref fcs;
length = packet_get_data_length( packet );
if( dummy ){
packet_trim( packet, sizeof( eth_preamble_t ), 0 );
}
if( length <= sizeof( eth_header_t ) + ETH_MIN_CONTENT + ETH_SUFFIX ) return NULL;
header = ( eth_header_ex_ref ) packet_get_data( packet );
type = ntohs( header->header.ethertype );
if( type >= ETH_MIN_PROTO ){
// DIX Ethernet
prefix = sizeof( eth_header_t );
suffix = sizeof( eth_fcs_t );
fcs = (( void * ) header ) + length - suffix;
}else if( type <= ETH_MAX_CONTENT ){
// translate "LSAP" values
if(( header->lsap.dsap == ETH_LSAP_GLSAP ) && ( header->lsap.ssap == ETH_LSAP_GLSAP )){
// raw packet
// discard
return NULL;
}else if(( header->lsap.dsap == ETH_LSAP_SNAP ) && ( header->lsap.ssap == ETH_LSAP_SNAP )){
// IEEE 802.3 + 802.2 + LSAP + SNAP
// organization code not supported
type = ntohs( header->snap.ethertype );
prefix = sizeof( eth_header_t ) + sizeof( eth_header_lsap_t) + sizeof( eth_header_snap_t);
}else{
// IEEE 802.3 + 802.2 LSAP
type = lsap_map( header->lsap.dsap );
prefix = sizeof( eth_header_t ) + sizeof( eth_header_lsap_t);
}
suffix = ( type < ETH_MIN_CONTENT ) ? ETH_MIN_CONTENT - type : 0;
fcs = (( void * ) header ) + prefix + type + suffix;
suffix += length - prefix - type;
}else{
// invalid length/type, should not occurr
return NULL;
}
if( dummy ){
if(( ~ compute_crc32( ~ 0, & header->header.dest, ((( void * ) fcs ) - (( void * ) & header->header.dest )) * 8 )) != ntohl( * fcs )){
return NULL;
}
}
if( ERROR_OCCURRED( packet_set_addr( packet, header->header.src, header->header.dest, ETH_ADDR ))
|| ERROR_OCCURRED( packet_trim( packet, prefix, suffix ))){
return NULL;
}
return eth_protos_find( & eth_globals.protos, type );
}
int eth_receive_message( device_id_t device_id, packet_t packet ){
eth_proto_ref proto;
packet_t next;
eth_device_ref device;
int dummy;
rwlock_read_lock( & eth_globals.devices_lock );
device = eth_devices_find( & eth_globals.devices, device_id );
if( ! device ){
rwlock_read_unlock( & eth_globals.devices_lock );
return ENOENT;
}
dummy = device->dummy;
rwlock_read_unlock( & eth_globals.devices_lock );
rwlock_read_lock( & eth_globals.protos_lock );
do{
next = pq_detach( packet );
proto = eth_process_packet( dummy, packet );
if( proto ){
async_msg_2( proto->phone, NET_IL_RECEIVED, device_id, packet_get_id( packet ));
}else{
// drop invalid/unknown
pq_release( eth_globals.networking_phone, packet_get_id( packet ));
}
packet = next;
}while( packet );
rwlock_read_unlock( & eth_globals.protos_lock );
return EOK;
}
int eth_packet_space_message( device_id_t device_id, size_t * addr_len, size_t * prefix, size_t * content, size_t * suffix ){
eth_device_ref device;
if( !( addr_len && prefix && content && suffix )) return EINVAL;
rwlock_read_lock( & eth_globals.devices_lock );
device = eth_devices_find( & eth_globals.devices, device_id );
if( ! device ){
rwlock_read_unlock( & eth_globals.devices_lock );
return ENOENT;
}
* content = ( ETH_MAX_CONTENT > device->mtu ) ? device->mtu : ETH_MAX_CONTENT;
rwlock_read_unlock( & eth_globals.devices_lock );
* addr_len = ETH_ADDR;
* prefix = ETH_PREFIX;
* suffix = ETH_MIN_CONTENT + ETH_SUFFIX;
return EOK;
}
int eth_addr_message( device_id_t device_id, eth_addr_type_t type, measured_string_ref * address ){
eth_device_ref device;
if( ! address ) return EINVAL;
if( type == ETH_BROADCAST_ADDR ){
* address = eth_globals.broadcast_addr;
}else{
rwlock_read_lock( & eth_globals.devices_lock );
device = eth_devices_find( & eth_globals.devices, device_id );
if( ! device ){
rwlock_read_unlock( & eth_globals.devices_lock );
return ENOENT;
}
* address = device->addr;
rwlock_read_unlock( & eth_globals.devices_lock );
}
return ( * address ) ? EOK : ENOENT;
}
int eth_register_message( services_t service, int phone ){
eth_proto_ref proto;
int protocol;
int index;
protocol = protocol_map( SERVICE_ETHERNET, service );
if( ! protocol ) return ENOENT;
rwlock_write_lock( & eth_globals.protos_lock );
proto = eth_protos_find( & eth_globals.protos, protocol );
if( proto ){
proto->phone = phone;
rwlock_write_unlock( & eth_globals.protos_lock );
return EOK;
}else{
proto
= ( eth_proto_ref
) malloc( sizeof( eth_proto_t
));
if( ! proto ){
rwlock_write_unlock( & eth_globals.protos_lock );
return ENOMEM;
}
proto->service = service;
proto->protocol = protocol;
proto->phone = phone;
index = eth_protos_add( & eth_globals.protos, protocol, proto );
if( index < 0 ){
rwlock_write_unlock( & eth_globals.protos_lock );
return index;
}
}
printf( "\nNew protocol registered:\n\tprotocol\t= 0x%x\n\tservice\t= %d\n\tphone\t= %d", proto
->protocol
, proto
->service
, proto
->phone
);
rwlock_write_unlock( & eth_globals.protos_lock );
return EOK;
}
int eth_prepare_packet( int dummy, packet_t packet, uint8_t * src_addr, int ethertype ){
eth_header_ex_ref header;
eth_fcs_ref fcs;
uint8_t * src;
uint8_t * dest;
int length;
int i;
void * padding;
eth_preamble_ref preamble;
if( dummy ){
preamble = PACKET_PREFIX( packet, eth_preamble_t );
if( ! preamble ) return ENOMEM;
for( i = 0; i < 7; ++ i ) preamble->preamble[ i ] = ETH_PREAMBLE;
preamble->sfd = ETH_SFD;
}
header = PACKET_PREFIX( packet, eth_header_ex_t );
if( ! header ) return ENOMEM;
length = packet_get_addr( packet, & src, & dest );
if( length < 0 ) return length;
if( length < ETH_ADDR ) return EINVAL;
memcpy( header
->header.
src, src_addr
, ETH_ADDR
);
memcpy( & header
->header.
dest, dest
, ETH_ADDR
);
length = packet_get_data_length( packet );
if( length > ETH_MAX_CONTENT ) return EINVAL;
if( length < ETH_MIN_CONTENT ){
padding = packet_suffix( packet, ETH_MIN_CONTENT - length );
if( ! padding ) return ENOMEM;
bzero( padding, ETH_MIN_CONTENT - length );
}
header->header.ethertype = htons( length );
header->lsap.dsap = 0xAA;
header->lsap.ssap = header->lsap.dsap;
header->lsap.ctrl = 0;
for( i = 0; i < 3; ++ i ) header->snap.proto[ i ] = 0;
header->snap.ethertype = ethertype;
if( dummy ){
fcs = PACKET_SUFFIX( packet, eth_fcs_t );
if( ! fcs ) return ENOMEM;
* fcs = htonl( ~ compute_crc32( ~ 0, & header->header.dest, ((( void * ) fcs ) - (( void * ) & header->header.dest )) * 8 ));
}
return EOK;
}
int eth_send_message( device_id_t device_id, packet_t packet, services_t sender ){
ERROR_DECLARE;
eth_device_ref device;
packet_t next;
packet_t tmp;
int ethertype;
ethertype = htons( protocol_map( SERVICE_ETHERNET, sender ));
if( ! ethertype ){
pq_release( eth_globals.networking_phone, packet_get_id( packet ));
return EINVAL;
}
rwlock_read_lock( & eth_globals.devices_lock );
device = eth_devices_find( & eth_globals.devices, device_id );
if( ! device ){
rwlock_read_unlock( & eth_globals.devices_lock );
return ENOENT;
}
// process packet queue
next = packet;
do{
if( ERROR_OCCURRED( eth_prepare_packet( device->dummy, next, ( uint8_t * ) device->addr->value, ethertype ))){
// release invalid packet
tmp = pq_detach( next );
pq_release( eth_globals.networking_phone, packet_get_id( next ));
next = tmp;
}else{
next = pq_next( next );
}
}while( next );
// send packet queue
async_msg_2( device->phone, NET_NETIF_SEND, device_id, packet_get_id( packet ));
rwlock_read_unlock( & eth_globals.devices_lock );
return EOK;
}
int eth_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
ERROR_DECLARE;
measured_string_ref address;
packet_t packet;
* answer_count = 0;
switch( IPC_GET_METHOD( * call )){
case IPC_M_PHONE_HUNGUP:
return EOK;
case NET_NIL_DEVICE:
return eth_device_message( IPC_GET_DEVICE( call ), IPC_GET_SERVICE( call ), IPC_GET_MTU( call ));
case NET_NIL_SEND:
ERROR_PROPAGATE( packet_translate( eth_globals.networking_phone, & packet, IPC_GET_PACKET( call )));
return eth_send_message( IPC_GET_DEVICE( call ), packet, IPC_GET_SERVICE( call ));
case NET_NIL_PACKET_SPACE:
ERROR_PROPAGATE( eth_packet_space_message( IPC_GET_DEVICE( call ), IPC_SET_ADDR( answer ), IPC_SET_PREFIX( answer ), IPC_SET_CONTENT( answer ), IPC_SET_SUFFIX( answer )));
* answer_count = 3;
return EOK;
case NET_NIL_ADDR:
ERROR_PROPAGATE( eth_addr_message( IPC_GET_DEVICE( call ), ETH_LOCAL_ADDR, & address ));
return measured_strings_reply( address, 1 );
case NET_NIL_BROADCAST_ADDR:
ERROR_PROPAGATE( eth_addr_message( IPC_GET_DEVICE( call ), ETH_BROADCAST_ADDR, & address ));
return measured_strings_reply( address, 1 );
return ERROR_CODE;
case IPC_M_CONNECT_TO_ME:
return eth_register_message( IPC_GET_PROTO( call ), IPC_GET_PHONE( call ));
}
return ENOTSUP;
}
void eth_receiver( ipc_callid_t iid, ipc_call_t * icall ){
ERROR_DECLARE;
packet_t packet;
int index;
eth_proto_ref proto;
while( true ){
switch( IPC_GET_METHOD( * icall )){
case NET_NIL_DEVICE_STATE:
//TODO clear device if off?
rwlock_read_lock( & eth_globals.protos_lock );
for( index = eth_protos_count( & eth_globals.protos ) - 1; index >= 0; -- index ){
proto = eth_protos_get_index( & eth_globals.protos, index );
if( proto && proto->phone ) async_msg_2( proto->phone, NET_IL_DEVICE_STATE, IPC_GET_DEVICE( icall ), IPC_GET_STATE( icall ));
}
rwlock_read_unlock( & eth_globals.protos_lock );
ipc_answer_0( iid, EOK );
break;
case NET_NIL_RECEIVED:
if( ! ERROR_OCCURRED( packet_translate( eth_globals.networking_phone, & packet, IPC_GET_PACKET( icall )))){
ERROR_CODE = eth_receive_message( IPC_GET_DEVICE( icall ), packet );
}
ipc_answer_0( iid, ERROR_CODE );
break;
default:
ipc_answer_0( iid, ENOTSUP );
}
iid = async_get_call( icall );
}
}
/** @}
*/