Subversion Repositories HelenOS

Rev

Rev 4075 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

/*
 * 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 <stdio.h>
#include <string.h>

#include <ipc/ipc.h>
#include <ipc/services.h>

#include "../../err.h"
#include "../../messages.h"
#include "../../modules.h"

#include "../../include/byteorder.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      4
#define ETH_MAX_CONTENT ( ETH_MIN_PROTO - 1 )
#define ETH_MIN_CONTENT 46
#define ETH_PADDING     ETH_SUFFIX

/** 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 )

/** Returns the protocol service message parameter.
 */
#define IPC_GET_PROTO( call )       ( services_t ) IPC_GET_ARG1( * 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 );

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;
    int             result;

    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 ){
            // update mtu
            device->mtu = mtu;
            rwlock_write_unlock( & eth_globals.devices_lock );
            return EOK;
        }else{
            rwlock_write_unlock( & eth_globals.devices_lock );
            return EEXIST;
        }
    }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;
        // bind the device driver
        device->phone = bind_service( device->service, device->device_id, SERVICE_ETHERNET, 0, eth_receiver );
        // 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 );
            free( device );
            async_wait_for( message, NULL );
            return ERROR_CODE;
        }
        async_wait_for( message, ( ipcarg_t * ) & result );
        if( ERROR_OCCURRED( result )){
            rwlock_write_unlock( & eth_globals.devices_lock );
            free( device->addr );
            free( device->addr_data );
            free( device );
            return ERROR_CODE;
        }
        // add to the cache
        if( ERROR_OCCURRED( eth_devices_add( & eth_globals.devices, device->device_id, device ))){
            rwlock_write_unlock( & eth_globals.devices_lock );
            free( device->addr );
            free( device->addr_data );
            free( device );
            return ERROR_CODE;
        }
    }
    rwlock_write_unlock( & eth_globals.devices_lock );
    return EOK;
}

int eth_receive_message( device_id_t device_id, packet_t packet ){
    ERROR_DECLARE;

    eth_header_ex_ref   header;
    size_t              length;
    int                 type;
    eth_proto_ref       proto;
    size_t              size;

    length = packet_get_data_length( packet );
    if( length <= sizeof( eth_header_t ) + ETH_MIN_CONTENT + ETH_PADDING ) return EINVAL;
    // TODO check CRC padding?
    header = ( eth_header_ex_ref ) packet_get_data( packet );
    type = ntohs( header->header.ethertype );
    if( type >= ETH_MIN_PROTO ){
        // DIX Ethernet
        size = sizeof( eth_header_t );
    }else{
        // TODO check length?
        // translate "LSAP" values
        if(( header->lsap.dsap == ETH_RAW ) && ( header->lsap.ssap == ETH_RAW )){
            // raw packet
            // discard
            return EINVAL;
        }else if(( header->lsap.dsap == ETH_LSAP_SNAP ) && ( header->lsap.ssap == ETH_LSAP_SNAP )){
            // IEEE 802.3 SNAP
            // organization code not supported
            type = ntohs( header->snap.ethertype );
            size = sizeof( eth_header_t ) + sizeof( eth_header_lsap_t) + sizeof( eth_header_snap_t);
        }else{
            // IEEE 802.3 + 802.2 LSAP
            // TODO lsap numbers
            type = header->lsap.dsap;
            size = sizeof( eth_header_t ) + sizeof( eth_header_lsap_t);
        }
    }
    rwlock_write_lock( & eth_globals.protos_lock );
    proto = eth_protos_find( & eth_globals.protos, type );
    if( ! proto ){
        rwlock_write_unlock( & eth_globals.protos_lock );
        return ENOENT;
    }
    if( ERROR_OCCURRED( packet_set_addr( packet, header->header.src, header->header.dest, ETH_ADDR ))
    || ERROR_OCCURRED( packet_trim( packet, size, ETH_PADDING ))){
        rwlock_write_unlock( & eth_globals.protos_lock );
        return ERROR_CODE;
    }
    async_msg_2( proto->phone, NET_IL_RECEIVED, device_id, packet_get_id( packet ));
    rwlock_write_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_write_lock( & eth_globals.devices_lock );
    device = eth_devices_find( & eth_globals.devices, device_id );
    if( ! device ){
        rwlock_write_unlock( & eth_globals.devices_lock );
        return ENOENT;
    }
    * content = (  ETH_MAX_CONTENT > device->mtu ) ? device->mtu : ETH_MAX_CONTENT;
    rwlock_write_unlock( & eth_globals.devices_lock );
    * addr_len = ETH_ADDR;
    * prefix = ETH_PREFIX;
    * suffix = 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_write_lock( & eth_globals.devices_lock );
        device = eth_devices_find( & eth_globals.devices, device_id );
        if( ! device ){
            rwlock_write_unlock( & eth_globals.devices_lock );
            return ENOENT;
        }
        * address = device->addr;
        rwlock_write_unlock( & eth_globals.devices_lock );
    }
    return ( * address ) ? EOK : ENOENT;
}

int eth_register_message( services_t service, int phone ){
    ERROR_DECLARE;

    eth_proto_ref   proto;
    int             protocol;

    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;
        if( ERROR_OCCURRED( eth_protos_add( & eth_globals.protos, protocol, proto ))){
            rwlock_write_unlock( & eth_globals.protos_lock );
            free( proto );
            return ERROR_CODE;
        }
    }
    rwlock_write_unlock( & eth_globals.protos_lock );
    return EOK;
}

int eth_send_message( device_id_t device_id, packet_t packet, services_t sender ){
    ERROR_DECLARE;

    eth_device_ref      device;
    eth_header_ex_ref   header;
    uint8_t *           src;
    uint8_t *           dest;
    int                 length;
    int                 i;

    header = PACKET_PREFIX( packet, eth_header_ex_t );
    if( ! header ) return ENOMEM;
    for( i = 0; i < 7; ++ i ) header->header.preamble[ i ] = ETH_PREAMBLE;
    header->header.sfd = ETH_SFD;
    length = packet_get_addr( packet, & src, & dest );
    if( length < 0 ) return length;
    if( length < ETH_ADDR ) return EINVAL;
    // TODO src set?
    // TODO set addresses
    length = packet_get_data_length( packet );
    if( length > ETH_MAX_CONTENT ) return EINVAL;
    if( length < ETH_MIN_CONTENT ){
        // TODO pad zeros
    }
    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 = htons( protocol_map( SERVICE_ETHERNET, sender ));
    if( ! header ) return ENOENT;
    // TODO eth padding
    if( ! packet_suffix( packet, ETH_PADDING )){
        return ENOMEM;
    }
    
    rwlock_write_lock( & eth_globals.devices_lock );
    rwlock_write_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:
            rwlock_read_lock( & eth_globals.devices_lock );
            if( ! ERROR_OCCURRED( eth_addr_message( IPC_GET_DEVICE( call ), ETH_LOCAL_ADDR, & address ))){
                 ERROR_CODE = measured_strings_reply( address, 1 );
            }
            rwlock_read_unlock( & eth_globals.devices_lock );
            return ERROR_CODE;
        case NET_NIL_BROADCAST_ADDR:
            rwlock_read_lock( & eth_globals.devices_lock );
            if( ! ERROR_OCCURRED( eth_addr_message( IPC_GET_DEVICE( call ), ETH_BROADCAST_ADDR, & address ))){
                 ERROR_CODE = measured_strings_reply( address, 1 );
            }
            rwlock_read_unlock( & eth_globals.devices_lock );
            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;

    while( true ){
        switch( IPC_GET_METHOD( * icall )){
            case NET_NIL_DEVICE_STATE:
                //TODO clear device if off?
                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 );
    }
}

/** @}
 */