Subversion Repositories HelenOS

Rev

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

  1. /*
  2.  * Copyright (c) 2009 Lukas Mejdrech
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  *
  9.  * - Redistributions of source code must retain the above copyright
  10.  *   notice, this list of conditions and the following disclaimer.
  11.  * - Redistributions in binary form must reproduce the above copyright
  12.  *   notice, this list of conditions and the following disclaimer in the
  13.  *   documentation and/or other materials provided with the distribution.
  14.  * - The name of the author may not be used to endorse or promote products
  15.  *   derived from this software without specific prior written permission.
  16.  *
  17.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  18.  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  19.  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  20.  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  21.  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  22.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26.  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27.  */
  28.  
  29. /** @addtogroup eth
  30.  *  @{
  31.  */
  32.  
  33. /** @file
  34.  *  Ethernet module implementation.
  35.  *  @see eth.h
  36.  */
  37.  
  38. #include <async.h>
  39. #include <malloc.h>
  40. #include <mem.h>
  41. #include <stdio.h>
  42. #include <string.h>
  43.  
  44. #include <ipc/ipc.h>
  45. #include <ipc/services.h>
  46.  
  47. #include "../../err.h"
  48. #include "../../messages.h"
  49. #include "../../modules.h"
  50.  
  51. #include "../../include/byteorder.h"
  52. #include "../../include/crc.h"
  53. #include "../../include/ethernet_lsap.h"
  54. #include "../../include/ethernet_protocols.h"
  55. #include "../../include/protocol_map.h"
  56. #include "../../include/device.h"
  57. #include "../../include/netif_interface.h"
  58. #include "../../include/net_interface.h"
  59. #include "../../include/nil_interface.h"
  60. #include "../../include/il_interface.h"
  61.  
  62. #include "../../structures/measured_strings.h"
  63. #include "../../structures/packet/packet_client.h"
  64.  
  65. #include "../nil_module.h"
  66.  
  67. #include "eth.h"
  68. #include "eth_header.h"
  69.  
  70. /** Reserved packet prefix length.
  71.  */
  72. #define ETH_PREFIX      ( sizeof( eth_header_t ) + sizeof( eth_header_lsap_t ) + sizeof( eth_header_snap_t ))
  73.  
  74. /** Reserved packet suffix length.
  75.  */
  76. #define ETH_SUFFIX      sizeof( eth_fcs_t )
  77.  
  78. /** Maximum packet content length.
  79.  */
  80. #define ETH_MAX_CONTENT 1500u
  81.  
  82. /** Minimum packet content length.
  83.  */
  84. #define ETH_MIN_CONTENT 46u
  85.  
  86. /** Maximum tagged packet content length.
  87.  */
  88. #define ETH_MAX_TAGGED_CONTENT( flags ) ( ETH_MAX_CONTENT - (( IS_8023_2_LSAP( flags ) || IS_8023_2_SNAP( flags )) ? sizeof( eth_header_lsap_t ) : 0 ) - ( IS_8023_2_SNAP( flags ) ? sizeof( eth_header_snap_t ) : 0 ))
  89.  
  90. /** Minimum tagged packet content length.
  91.  */
  92. #define ETH_MIN_TAGGED_CONTENT( flags ) ( ETH_MIN_CONTENT - (( IS_8023_2_LSAP( flags ) || IS_8023_2_SNAP( flags )) ? sizeof( eth_header_lsap_t ) : 0 ) - ( IS_8023_2_SNAP( flags ) ? sizeof( eth_header_snap_t ) : 0 ))
  93.  
  94. /** Dummy flag shift value.
  95.  */
  96. #define ETH_DUMMY_SHIFT 0
  97.  
  98. /** Mode flag shift value.
  99.  */
  100. #define ETH_MODE_SHIFT  1
  101.  
  102. /** Dummy device flag.
  103.  *  Preamble and FCS are mandatory part of the packets.
  104.  */
  105. #define ETH_DUMMY               ( 1 << ETH_DUMMY_SHIFT )
  106.  
  107. /** Returns the dummy flag.
  108.  *  @see ETH_DUMMY
  109.  */
  110. #define IS_DUMMY( flags )       (( flags ) & ETH_DUMMY )
  111.  
  112. /** Device mode flags.
  113.  *  @see ETH_DIX
  114.  *  @see ETH_8023_2_LSAP
  115.  *  @see ETH_8023_2_SNAP
  116.  */
  117. #define ETH_MODE_MASK           ( 3 << ETH_MODE_SHIFT )
  118.  
  119. /** DIX Ethernet mode flag.
  120.  */
  121. #define ETH_DIX                 ( 1 << ETH_MODE_SHIFT )
  122.  
  123. /** Returns whether the DIX Ethernet mode flag is set.
  124.  *  @param flags The ethernet flags. Input parameter.
  125.  *  @see ETH_DIX
  126.  */
  127. #define IS_DIX( flags )         ((( flags ) & ETH_MODE_MASK ) == ETH_DIX )
  128.  
  129. /** 802.3 + 802.2 + LSAP mode flag.
  130.  */
  131. #define ETH_8023_2_LSAP         ( 2 << ETH_MODE_SHIFT )
  132.  
  133. /** Returns whether the 802.3 + 802.2 + LSAP mode flag is set.
  134.  *  @param flags The ethernet flags. Input parameter.
  135.  *  @see ETH_8023_2_LSAP
  136.  */
  137. #define IS_8023_2_LSAP( flags ) ((( flags ) & ETH_MODE_MASK ) == ETH_8023_2_LSAP )
  138.  
  139. /** 802.3 + 802.2 + LSAP + SNAP mode flag.
  140.  */
  141. #define ETH_8023_2_SNAP         ( 3 << ETH_MODE_SHIFT )
  142.  
  143. /** Returns whether the 802.3 + 802.2 + LSAP + SNAP mode flag is set.
  144.  *  @param flags The ethernet flags. Input parameter.
  145.  *  @see ETH_8023_2_SNAP
  146.  */
  147. #define IS_8023_2_SNAP( flags ) ((( flags ) & ETH_MODE_MASK ) == ETH_8023_2_SNAP )
  148.  
  149. /** Type definition of the ethernet address type.
  150.  *  @see eth_addr_type
  151.  */
  152. typedef enum eth_addr_type  eth_addr_type_t;
  153.  
  154. /** Type definition of the ethernet address type pointer.
  155.  *  @see eth_addr_type
  156.  */
  157. typedef eth_addr_type_t *   eth_addr_type_ref;
  158.  
  159. /** Ethernet address type.
  160.  */
  161. enum eth_addr_type{
  162.     /** Local address.
  163.      */
  164.     ETH_LOCAL_ADDR,
  165.     /** Broadcast address.
  166.      */
  167.     ETH_BROADCAST_ADDR
  168. };
  169.  
  170. /** Ethernet module global data.
  171.  */
  172. eth_globals_t   eth_globals;
  173.  
  174. /** @name Message processing functions
  175.  */
  176. /*@{*/
  177.  
  178. /** Processes IPC messages from the registered device driver modules in an infinite loop.
  179.  *  @param iid The message identifier. Input parameter.
  180.  *  @param icall The message parameters. Input/output parameter.
  181.  */
  182. void    eth_receiver( ipc_callid_t iid, ipc_call_t * icall );
  183.  
  184. /** Registers new device or updates the MTU of an existing one.
  185.  *  Determines the device local hardware address.
  186.  *  @param device_id The new device identifier. Input parameter.
  187.  *  @param service The device driver service. Input parameter.
  188.  *  @param mtu The device maximum transmission unit. Input parameter.
  189.  *  @returns EOK on success.
  190.  *  @returns EEXIST if the device with the different service exists.
  191.  *  @returns ENOMEM if there is not enough memory left.
  192.  *  @returns Other error codes as defined for the net_get_device_conf_req() function.
  193.  *  @returns Other error codes as defined for the netif_bind_service() function.
  194.  *  @returns Other error codes as defined for the netif_get_addr() function.
  195.  */
  196. int eth_device_message( device_id_t device_id, services_t service, size_t mtu );
  197.  
  198. /** Registers receiving module service.
  199.  *  Passes received packets for this service.
  200.  *  @param service The module service. Input parameter.
  201.  *  @param phone The service phone. Input parameter.
  202.  *  @returns EOK on success.
  203.  *  @returns ENOENT if the service is not known.
  204.  *  @returns ENOMEM if there is not enough memory left.
  205.  */
  206. int eth_register_message( services_t service, int phone );
  207.  
  208. /** Returns the device packet dimensions for sending.
  209.  *  @param device_id The device identifier. Input parameter.
  210.  *  @param addr_len The minimum reserved address length. Output parameter.
  211.  *  @param prefix The minimum reserved prefix size. Output parameter.
  212.  *  @param content The maximum content size. Output parameter.
  213.  *  @param suffix The minimum reserved suffix size. Output parameter.
  214.  *  @returns EOK on success.
  215.  *  @returns EBADMEM if either one of the parameters is NULL.
  216.  *  @returns ENOENT if there is no such device.
  217.  */
  218. int eth_packet_space_message( device_id_t device_id, size_t * addr_len, size_t * prefix, size_t * content, size_t * suffix );
  219.  
  220. /** Returns the device hardware address.
  221.  *  @param device_id The device identifier. Input parameter.
  222.  *  @param type Type of the desired address. Input parameter
  223.  *  @param address The device hardware address. Output parameter.
  224.  *  @returns EOK on success.
  225.  *  @returns EBADMEM if the address parameter is NULL.
  226.  *  @returns ENOENT if there no such device.
  227.  */
  228. int eth_addr_message( device_id_t device_id, eth_addr_type_t type, measured_string_ref * address );
  229.  
  230. /** Sends the packet queue.
  231.  *  Sends only packet successfully processed by the eth_prepare_packet() function.
  232.  *  @param device_id The device identifier. Input parameter.
  233.  *  @param packet The packet queue. Input parameter.
  234.  *  @param sender The sending module service. Input parameter.
  235.  *  @returns EOK on success.
  236.  *  @returns ENOENT if there no such device.
  237.  *  @returns EINVAL if the service parameter is not known.
  238.  */
  239. int eth_send_message( device_id_t device_id, packet_t packet, services_t sender );
  240.  
  241. /*@}*/
  242.  
  243. /** Processes the received packet and chooses the target registered module.
  244.  *  @param flags The device flags. Input parameter.
  245.  *  @param packet The packet. Input parameter.
  246.  *  @returns The target registered module.
  247.  *  @returns NULL if the packet is not long enough.
  248.  *  @returns NULL if the packet is too long.
  249.  *  @returns NULL if the raw ethernet protocol is used.
  250.  *  @returns NULL if the dummy device FCS checksum is invalid.
  251.  *  @returns NULL if the packet address length is not big enough.
  252.  */
  253. eth_proto_ref   eth_process_packet( int flags, packet_t packet );
  254.  
  255. /** Prepares the packet for sending.
  256.  *  @param flags The device flags. Input parameter.
  257.  *  @param packet The packet. Input parameter.
  258.  *  @param src_addr The source hardware address. Input parameter.
  259.  *  @param ethertype The ethernet protocol type. Input parameter.
  260.  *  @param mtu The device maximum transmission unit. Input parameter.
  261.  *  @returns EOK on success.
  262.  *  @returns EINVAL if the packet addresses length is not long enough.
  263.  *  @returns EINVAL if the packet is bigger than the device MTU.
  264.  *  @returns ENOMEM if there is not enough memory in the packet.
  265.  */
  266. int eth_prepare_packet( int flags, packet_t packet, uint8_t * src_addr, int ethertype, size_t mtu );
  267.  
  268. DEVICE_MAP_IMPLEMENT( eth_devices, eth_device_t )
  269.  
  270. INT_MAP_IMPLEMENT( eth_protos, eth_proto_t )
  271.  
  272. int nil_device_state_msg( int nil_phone, device_id_t device_id, int state ){
  273.     int             index;
  274.     eth_proto_ref   proto;
  275.  
  276.     fibril_rwlock_read_lock( & eth_globals.protos_lock );
  277.     for( index = eth_protos_count( & eth_globals.protos ) - 1; index >= 0; -- index ){
  278.         proto = eth_protos_get_index( & eth_globals.protos, index );
  279.         if( proto && proto->phone ) il_device_state_msg( proto->phone, device_id, state, proto->service );
  280.     }
  281.     fibril_rwlock_read_unlock( & eth_globals.protos_lock );
  282.     return EOK;
  283. }
  284.  
  285. int nil_initialize( int net_phone ){
  286.     ERROR_DECLARE;
  287.  
  288.     fibril_rwlock_initialize( & eth_globals.devices_lock );
  289.     fibril_rwlock_initialize( & eth_globals.protos_lock );
  290.     fibril_rwlock_write_lock( & eth_globals.devices_lock );
  291.     fibril_rwlock_write_lock( & eth_globals.protos_lock );
  292.     eth_globals.net_phone = net_phone;
  293.     eth_globals.broadcast_addr = measured_string_create_bulk( "\xFF\xFF\xFF\xFF\xFF\xFF", CONVERT_SIZE( uint8_t, char, ETH_ADDR ));
  294.     if( ! eth_globals.broadcast_addr ) return ENOMEM;
  295.     ERROR_PROPAGATE( eth_devices_initialize( & eth_globals.devices ));
  296.     if( ERROR_OCCURRED( eth_protos_initialize( & eth_globals.protos ))){
  297.         eth_devices_destroy( & eth_globals.devices );
  298.         return ERROR_CODE;
  299.     }
  300.     fibril_rwlock_write_unlock( & eth_globals.protos_lock );
  301.     fibril_rwlock_write_unlock( & eth_globals.devices_lock );
  302.     return EOK;
  303. }
  304.  
  305. int eth_device_message( device_id_t device_id, services_t service, size_t mtu ){
  306.     ERROR_DECLARE;
  307.  
  308.     eth_device_ref  device;
  309.     int             index;
  310.     measured_string_t   names[ 2 ] = {{ "ETH_MODE", 8 }, { "ETH_DUMMY", 9 }};
  311.     measured_string_ref configuration;
  312.     size_t              count = sizeof( names ) / sizeof( measured_string_t );
  313.     char *              data;
  314.     eth_proto_ref       proto;
  315.  
  316.     fibril_rwlock_write_lock( & eth_globals.devices_lock );
  317.     // an existing device?
  318.     device = eth_devices_find( & eth_globals.devices, device_id );
  319.     if( device ){
  320.         if( device->service != service ){
  321.             printf( "Device %d already exists\n", device->device_id );
  322.             fibril_rwlock_write_unlock( & eth_globals.devices_lock );
  323.             return EEXIST;
  324.         }else{
  325.             // update mtu
  326.             if(( mtu > 0 ) && ( mtu <= ETH_MAX_TAGGED_CONTENT( device->flags ))){
  327.                 device->mtu = mtu;
  328.             }else{
  329.                  device->mtu = ETH_MAX_TAGGED_CONTENT( device->flags );
  330.             }
  331.             printf( "Device %d already exists:\tMTU\t= %d\n", device->device_id, device->mtu );
  332.             fibril_rwlock_write_unlock( & eth_globals.devices_lock );
  333.             // notify all upper layer modules
  334.             fibril_rwlock_read_lock( & eth_globals.protos_lock );
  335.             for( index = 0; index < eth_protos_count( & eth_globals.protos ); ++ index ){
  336.                 proto = eth_protos_get_index( & eth_globals.protos, index );
  337.                 if ( proto->phone ){
  338.                     il_mtu_changed_msg( proto->phone, device->device_id, device->mtu, proto->service );
  339.                 }
  340.             }
  341.             fibril_rwlock_read_unlock( & eth_globals.protos_lock );
  342.             return EOK;
  343.         }
  344.     }else{
  345.         // create a new device
  346.         device = ( eth_device_ref ) malloc( sizeof( eth_device_t ));
  347.         if( ! device ) return ENOMEM;
  348.         device->device_id = device_id;
  349.         device->service = service;
  350.         device->flags = 0;
  351.         if(( mtu > 0 ) && ( mtu <= ETH_MAX_TAGGED_CONTENT( device->flags ))){
  352.             device->mtu = mtu;
  353.         }else{
  354.              device->mtu = ETH_MAX_TAGGED_CONTENT( device->flags );
  355.         }
  356.         configuration = & names[ 0 ];
  357.         if( ERROR_OCCURRED( net_get_device_conf_req( eth_globals.net_phone, device->device_id, & configuration, count, & data ))){
  358.             fibril_rwlock_write_unlock( & eth_globals.devices_lock );
  359.             free( device );
  360.             return ERROR_CODE;
  361.         }
  362.         if( configuration ){
  363.             if( ! str_lcmp( configuration[ 0 ].value, "DIX", configuration[ 0 ].length )){
  364.                 device->flags |= ETH_DIX;
  365.             }else if( ! str_lcmp( configuration[ 0 ].value, "8023_2_LSAP", configuration[ 0 ].length )){
  366.                 // TODO 8023_2_LSAP
  367.                 printf( "8023_2_LSAP is not supported (yet?), DIX used instead\n" );
  368.                 device->flags |= ETH_DIX;
  369.             }else device->flags |= ETH_8023_2_SNAP;
  370.             if(( configuration[ 1 ].value ) && ( configuration[ 1 ].value[ 0 ] == 'y' )){
  371.                 device->flags |= ETH_DUMMY;
  372.             }
  373.             net_free_settings( configuration, data );
  374.         }else{
  375.             device->flags |= ETH_8023_2_SNAP;
  376.         }
  377.         // bind the device driver
  378.         device->phone = netif_bind_service( device->service, device->device_id, SERVICE_ETHERNET, eth_receiver );
  379.         if( device->phone < 0 ){
  380.             fibril_rwlock_write_unlock( & eth_globals.devices_lock );
  381.             free( device );
  382.             return device->phone;
  383.         }
  384.         // get hardware address
  385.         if( ERROR_OCCURRED( netif_get_addr( device->phone, device->device_id, & device->addr, & device->addr_data ))){
  386.             fibril_rwlock_write_unlock( & eth_globals.devices_lock );
  387.             free( device );
  388.             return ERROR_CODE;
  389.         }
  390.         // add to the cache
  391.         index = eth_devices_add( & eth_globals.devices, device->device_id, device );
  392.         if( index < 0 ){
  393.             fibril_rwlock_write_unlock( & eth_globals.devices_lock );
  394.             free( device->addr );
  395.             free( device->addr_data );
  396.             free( device );
  397.             return index;
  398.         }
  399.         printf( "New device registered:\n\tid\t= %d\n\tservice\t= %d\n\tMTU\t= %d\n\taddress\t= %X:%X:%X:%X:%X:%X\n\tflags\t= 0x%x\n", 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 ], device->flags );
  400.     }
  401.     fibril_rwlock_write_unlock( & eth_globals.devices_lock );
  402.     return EOK;
  403. }
  404.  
  405. eth_proto_ref eth_process_packet( int flags, packet_t packet ){
  406.     ERROR_DECLARE;
  407.  
  408.     eth_header_ex_ref   header;
  409.     size_t              length;
  410.     eth_type_t          type;
  411.     size_t              prefix;
  412.     size_t              suffix;
  413.     eth_fcs_ref         fcs;
  414.     uint8_t *           data;
  415.  
  416.     length = packet_get_data_length( packet );
  417.     if( IS_DUMMY( flags )){
  418.         packet_trim( packet, sizeof( eth_preamble_t ), 0 );
  419.     }
  420.     if( length < sizeof( eth_header_t ) + ETH_MIN_CONTENT + ( IS_DUMMY( flags ) ? ETH_SUFFIX : 0 )) return NULL;
  421.     data = packet_get_data( packet );
  422.     header = ( eth_header_ex_ref ) data;
  423.     type = ntohs( header->header.ethertype );
  424.     if( type >= ETH_MIN_PROTO ){
  425.         // DIX Ethernet
  426.         prefix = sizeof( eth_header_t );
  427.         suffix = 0;
  428.         fcs = ( eth_fcs_ref ) data + length - sizeof( eth_fcs_t );
  429.         length -= sizeof( eth_fcs_t );
  430.     }else if( type <= ETH_MAX_CONTENT ){
  431.         // translate "LSAP" values
  432.         if(( header->lsap.dsap == ETH_LSAP_GLSAP ) && ( header->lsap.ssap == ETH_LSAP_GLSAP )){
  433.             // raw packet
  434.             // discard
  435.             return NULL;
  436.         }else if(( header->lsap.dsap == ETH_LSAP_SNAP ) && ( header->lsap.ssap == ETH_LSAP_SNAP )){
  437.             // IEEE 802.3 + 802.2 + LSAP + SNAP
  438.             // organization code not supported
  439.             type = ntohs( header->snap.ethertype );
  440.             prefix = sizeof( eth_header_t ) + sizeof( eth_header_lsap_t ) + sizeof( eth_header_snap_t );
  441.         }else{
  442.             // IEEE 802.3 + 802.2 LSAP
  443.             type = lsap_map( header->lsap.dsap );
  444.             prefix = sizeof( eth_header_t ) + sizeof( eth_header_lsap_t);
  445.         }
  446.         suffix = ( type < ETH_MIN_CONTENT ) ? ETH_MIN_CONTENT - type : 0u;
  447.         fcs = ( eth_fcs_ref ) data + prefix + type + suffix;
  448.         suffix += length - prefix - type;
  449.         length = prefix + type + suffix;
  450.     }else{
  451.         // invalid length/type, should not occurr
  452.         return NULL;
  453.     }
  454.     if( IS_DUMMY( flags )){
  455.         if(( ~ compute_crc32( ~ 0u, data, length * 8 )) != ntohl( * fcs )){
  456.             return NULL;
  457.         }
  458.         suffix += sizeof( eth_fcs_t );
  459.     }
  460.     if( ERROR_OCCURRED( packet_set_addr( packet, header->header.source_address, header->header.destination_address, ETH_ADDR ))
  461.     || ERROR_OCCURRED( packet_trim( packet, prefix, suffix ))){
  462.         return NULL;
  463.     }
  464.     return eth_protos_find( & eth_globals.protos, type );
  465. }
  466.  
  467. int nil_received_msg( int nil_phone, device_id_t device_id, packet_t packet, services_t target ){
  468.     eth_proto_ref   proto;
  469.     packet_t        next;
  470.     eth_device_ref  device;
  471.     int             flags;
  472.  
  473.     fibril_rwlock_read_lock( & eth_globals.devices_lock );
  474.     device = eth_devices_find( & eth_globals.devices, device_id );
  475.     if( ! device ){
  476.         fibril_rwlock_read_unlock( & eth_globals.devices_lock );
  477.         return ENOENT;
  478.     }
  479.     flags = device->flags;
  480.     fibril_rwlock_read_unlock( & eth_globals.devices_lock );
  481.     fibril_rwlock_read_lock( & eth_globals.protos_lock );
  482.     do{
  483.         next = pq_detach( packet );
  484.         proto = eth_process_packet( flags, packet );
  485.         if( proto ){
  486.             il_received_msg( proto->phone, device_id, packet, proto->service );
  487.         }else{
  488.             // drop invalid/unknown
  489.             pq_release( eth_globals.net_phone, packet_get_id( packet ));
  490.         }
  491.         packet = next;
  492.     }while( packet );
  493.     fibril_rwlock_read_unlock( & eth_globals.protos_lock );
  494.     return EOK;
  495. }
  496.  
  497. int eth_packet_space_message( device_id_t device_id, size_t * addr_len, size_t * prefix, size_t * content, size_t * suffix ){
  498.     eth_device_ref  device;
  499.  
  500.     if( !( addr_len && prefix && content && suffix )) return EBADMEM;
  501.     fibril_rwlock_read_lock( & eth_globals.devices_lock );
  502.     device = eth_devices_find( & eth_globals.devices, device_id );
  503.     if( ! device ){
  504.         fibril_rwlock_read_unlock( & eth_globals.devices_lock );
  505.         return ENOENT;
  506.     }
  507.     * content = device->mtu;
  508.     fibril_rwlock_read_unlock( & eth_globals.devices_lock );
  509.     * addr_len = ETH_ADDR;
  510.     * prefix = ETH_PREFIX;
  511.     * suffix = ETH_MIN_CONTENT + ETH_SUFFIX;
  512.     return EOK;
  513. }
  514.  
  515. int eth_addr_message( device_id_t device_id, eth_addr_type_t type, measured_string_ref * address ){
  516.     eth_device_ref  device;
  517.  
  518.     if( ! address ) return EBADMEM;
  519.     if( type == ETH_BROADCAST_ADDR ){
  520.         * address = eth_globals.broadcast_addr;
  521.     }else{
  522.         fibril_rwlock_read_lock( & eth_globals.devices_lock );
  523.         device = eth_devices_find( & eth_globals.devices, device_id );
  524.         if( ! device ){
  525.             fibril_rwlock_read_unlock( & eth_globals.devices_lock );
  526.             return ENOENT;
  527.         }
  528.         * address = device->addr;
  529.         fibril_rwlock_read_unlock( & eth_globals.devices_lock );
  530.     }
  531.     return ( * address ) ? EOK : ENOENT;
  532. }
  533.  
  534. int eth_register_message( services_t service, int phone ){
  535.     eth_proto_ref   proto;
  536.     int             protocol;
  537.     int             index;
  538.  
  539.     protocol = protocol_map( SERVICE_ETHERNET, service );
  540.     if( ! protocol ) return ENOENT;
  541.     fibril_rwlock_write_lock( & eth_globals.protos_lock );
  542.     proto = eth_protos_find( & eth_globals.protos, protocol );
  543.     if( proto ){
  544.         proto->phone = phone;
  545.         fibril_rwlock_write_unlock( & eth_globals.protos_lock );
  546.         return EOK;
  547.     }else{
  548.         proto = ( eth_proto_ref ) malloc( sizeof( eth_proto_t ));
  549.         if( ! proto ){
  550.             fibril_rwlock_write_unlock( & eth_globals.protos_lock );
  551.             return ENOMEM;
  552.         }
  553.         proto->service = service;
  554.         proto->protocol = protocol;
  555.         proto->phone = phone;
  556.         index = eth_protos_add( & eth_globals.protos, protocol, proto );
  557.         if( index < 0 ){
  558.             fibril_rwlock_write_unlock( & eth_globals.protos_lock );
  559.             free( proto );
  560.             return index;
  561.         }
  562.     }
  563.     printf( "New protocol registered:\n\tprotocol\t= 0x%x\n\tservice\t= %d\n\tphone\t= %d\n", proto->protocol, proto->service, proto->phone );
  564.     fibril_rwlock_write_unlock( & eth_globals.protos_lock );
  565.     return EOK;
  566. }
  567.  
  568. int eth_prepare_packet( int flags, packet_t packet, uint8_t * src_addr, int ethertype, size_t mtu ){
  569.     eth_header_ex_ref   header;
  570.     eth_header_ref      header_dix;
  571.     eth_fcs_ref         fcs;
  572.     uint8_t *           src;
  573.     uint8_t *           dest;
  574.     size_t              length;
  575.     int                 i;
  576.     void *              padding;
  577.     eth_preamble_ref    preamble;
  578.  
  579.     i = packet_get_addr( packet, & src, & dest );
  580.     if( i < 0 ) return i;
  581.     if( i != ETH_ADDR ) return EINVAL;
  582.     length = packet_get_data_length( packet );
  583.     if( length > mtu ) return EINVAL;
  584.     if( length < ETH_MIN_TAGGED_CONTENT( flags )){
  585.         padding = packet_suffix( packet, ETH_MIN_TAGGED_CONTENT( flags ) - length );
  586.         if( ! padding ) return ENOMEM;
  587.         bzero( padding, ETH_MIN_TAGGED_CONTENT( flags ) - length );
  588.     }
  589.     if( IS_DUMMY( flags )){
  590.         preamble = PACKET_PREFIX( packet, eth_preamble_t );
  591.         if( ! preamble ) return ENOMEM;
  592.         for( i = 0; i < 7; ++ i ) preamble->preamble[ i ] = ETH_PREAMBLE;
  593.         preamble->sfd = ETH_SFD;
  594.     }
  595.     // TODO LSAP only device
  596.     if( IS_DIX( flags ) || IS_8023_2_LSAP( flags )){
  597.         header_dix = PACKET_PREFIX( packet, eth_header_t );
  598.         if( ! header_dix ) return ENOMEM;
  599.         header_dix->ethertype = ( uint16_t ) ethertype;
  600.         memcpy( header_dix->source_address, src_addr, ETH_ADDR );
  601.         memcpy( header_dix->destination_address, dest, ETH_ADDR );
  602.         src = & header_dix->destination_address[ 0 ];
  603.     }else if( IS_8023_2_SNAP( flags )){
  604.         header = PACKET_PREFIX( packet, eth_header_ex_t );
  605.         if( ! header ) return ENOMEM;
  606.         header->header.ethertype = htons( length + sizeof( eth_header_lsap_t ) + sizeof( eth_header_snap_t ));
  607.         header->lsap.dsap = ( uint16_t ) ETH_LSAP_SNAP;
  608.         header->lsap.ssap = header->lsap.dsap;
  609.         header->lsap.ctrl = 0;
  610.         for( i = 0; i < 3; ++ i ) header->snap.protocol[ i ] = 0;
  611.         header->snap.ethertype = ( uint16_t ) ethertype;
  612.         memcpy( header->header.source_address, src_addr, ETH_ADDR );
  613.         memcpy( header->header.destination_address, dest, ETH_ADDR );
  614.         src = & header->header.destination_address[ 0 ];
  615.     }
  616.     if( IS_DUMMY( flags )){
  617.         fcs = PACKET_SUFFIX( packet, eth_fcs_t );
  618.         if( ! fcs ) return ENOMEM;
  619.         * fcs = htonl( ~ compute_crc32( ~ 0u, src, length * 8 ));
  620.     }
  621.     return EOK;
  622. }
  623.  
  624. int eth_send_message( device_id_t device_id, packet_t packet, services_t sender ){
  625.     ERROR_DECLARE;
  626.  
  627.     eth_device_ref      device;
  628.     packet_t            next;
  629.     packet_t            tmp;
  630.     int                 ethertype;
  631.  
  632.     ethertype = htons( protocol_map( SERVICE_ETHERNET, sender ));
  633.     if( ! ethertype ){
  634.         pq_release( eth_globals.net_phone, packet_get_id( packet ));
  635.         return EINVAL;
  636.     }
  637.     fibril_rwlock_read_lock( & eth_globals.devices_lock );
  638.     device = eth_devices_find( & eth_globals.devices, device_id );
  639.     if( ! device ){
  640.         fibril_rwlock_read_unlock( & eth_globals.devices_lock );
  641.         return ENOENT;
  642.     }
  643.     // process packet queue
  644.     next = packet;
  645.     do{
  646.         if( ERROR_OCCURRED( eth_prepare_packet( device->flags, next, ( uint8_t * ) device->addr->value, ethertype, device->mtu ))){
  647.             // release invalid packet
  648.             tmp = pq_detach( next );
  649.             if( next == packet ) packet = tmp;
  650.             pq_release( eth_globals.net_phone, packet_get_id( next ));
  651.             next = tmp;
  652.         }else{
  653.             next = pq_next( next );
  654.         }
  655.     }while( next );
  656.     // send packet queue
  657.     if( packet ){
  658.         netif_send_msg( device->phone, device_id, packet, SERVICE_ETHERNET );
  659.     }
  660.     fibril_rwlock_read_unlock( & eth_globals.devices_lock );
  661.     return EOK;
  662. }
  663.  
  664. int nil_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
  665.     ERROR_DECLARE;
  666.  
  667.     measured_string_ref address;
  668.     packet_t            packet;
  669.  
  670. //  printf( "message %d - %d\n", IPC_GET_METHOD( * call ), NET_NIL_FIRST );
  671.     * answer_count = 0;
  672.     switch( IPC_GET_METHOD( * call )){
  673.         case IPC_M_PHONE_HUNGUP:
  674.             return EOK;
  675.         case NET_NIL_DEVICE:
  676.             return eth_device_message( IPC_GET_DEVICE( call ), IPC_GET_SERVICE( call ), IPC_GET_MTU( call ));
  677.         case NET_NIL_SEND:
  678.             ERROR_PROPAGATE( packet_translate( eth_globals.net_phone, & packet, IPC_GET_PACKET( call )));
  679.             return eth_send_message( IPC_GET_DEVICE( call ), packet, IPC_GET_SERVICE( call ));
  680.         case NET_NIL_PACKET_SPACE:
  681.             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 )));
  682.             * answer_count = 4;
  683.             return EOK;
  684.         case NET_NIL_ADDR:
  685.             ERROR_PROPAGATE( eth_addr_message( IPC_GET_DEVICE( call ), ETH_LOCAL_ADDR, & address ));
  686.             return measured_strings_reply( address, 1 );
  687.         case NET_NIL_BROADCAST_ADDR:
  688.             ERROR_PROPAGATE( eth_addr_message( IPC_GET_DEVICE( call ), ETH_BROADCAST_ADDR, & address ));
  689.             return measured_strings_reply( address, 1 );
  690.         case IPC_M_CONNECT_TO_ME:
  691.             return eth_register_message( NIL_GET_PROTO( call ), IPC_GET_PHONE( call ));
  692.     }
  693.     return ENOTSUP;
  694. }
  695.  
  696. void eth_receiver( ipc_callid_t iid, ipc_call_t * icall ){
  697.     ERROR_DECLARE;
  698.  
  699.     packet_t        packet;
  700.  
  701.     while( true ){
  702. //      printf( "message %d - %d\n", IPC_GET_METHOD( * icall ), NET_NIL_FIRST );
  703.         switch( IPC_GET_METHOD( * icall )){
  704.             case NET_NIL_DEVICE_STATE:
  705.                 nil_device_state_msg( 0, IPC_GET_DEVICE( icall ), IPC_GET_STATE( icall ));
  706.                 ipc_answer_0( iid, EOK );
  707.                 break;
  708.             case NET_NIL_RECEIVED:
  709.                 if( ! ERROR_OCCURRED( packet_translate( eth_globals.net_phone, & packet, IPC_GET_PACKET( icall )))){
  710.                     ERROR_CODE = nil_received_msg( 0, IPC_GET_DEVICE( icall ), packet, 0 );
  711.                 }
  712.                 ipc_answer_0( iid, ( ipcarg_t ) ERROR_CODE );
  713.                 break;
  714.             default:
  715.                 ipc_answer_0( iid, ( ipcarg_t ) ENOTSUP );
  716.         }
  717.         iid = async_get_call( icall );
  718.     }
  719. }
  720.  
  721. /** @}
  722.  */
  723.