Subversion Repositories HelenOS

Rev

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

  1. /*
  2.  * Copyright (c) 2008 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 arp
  30.  *  @{
  31.  */
  32.  
  33. /** @file
  34.  */
  35.  
  36. #include <as.h>
  37. #include <async.h>
  38. #include <malloc.h>
  39. #include <stdio.h>
  40. #include <string.h>
  41.  
  42. #include <ipc/ipc.h>
  43. #include <ipc/services.h>
  44.  
  45. #include "../../err.h"
  46. #include "../../messages.h"
  47. #include "../../modules.h"
  48.  
  49. #include "../../include/protocol_map.h"
  50. #include "../../netif/device.h"
  51.  
  52. #include "../../structures/measured_strings.h"
  53. #include "../../structures/packet/packet.h"
  54.  
  55. #include "arp.h"
  56. #include "arp_header.h"
  57. #include "arp_oc.h"
  58. //#include "arp_messages.h"
  59. #include "arp_module.h"
  60.  
  61. #define IPC_GET_DEVICE( call )      ( device_id_t ) IPC_GET_ARG1( * call )
  62. #define IPC_GET_PROTO( call )       ( services_t ) IPC_GET_ARG2( * call )
  63. #define IPC_GET_SERVICE( call )     ( services_t ) IPC_GET_ARG3( * call )
  64.  
  65. arp_globals_t   arp_globals;
  66.  
  67. DEVICE_MAP_IMPLEMENT( arp_cache, arp_device_t )
  68.  
  69. INT_MAP_IMPLEMENT( arp_protos, arp_proto_t )
  70.  
  71. GENERIC_CHAR_MAP_IMPLEMENT( arp_addr, measured_string_t )
  72.  
  73. int arp_proto_create( arp_proto_ref * proto, services_t service, measured_string_ref address );
  74. int arp_device_message( device_id_t device_id, services_t service, services_t protocol, measured_string_ref address );
  75. measured_string_ref arp_translate_message( device_id_t device, services_t protocol, measured_string_ref target );
  76. int arp_receive_message( device_id_t device_id, packet_t packet );
  77. int arp_clear_device_message( device_id_t device_id );
  78. void clear_device( arp_device_ref device );
  79. int arp_clean_cache_message( void );
  80. void arp_receiver( ipc_callid_t iid, ipc_call_t * icall );
  81.  
  82. /** Initializes the ARP module.
  83.  */
  84. int arp_initialize( void ){
  85.     arp_cache_initialize( & arp_globals.cache );
  86.     return EOK;
  87. }
  88.  
  89. int arp_proto_create( arp_proto_ref * proto, services_t service, measured_string_ref address ){
  90.     ERROR_DECLARE;
  91.  
  92.     * proto = ( arp_proto_ref ) malloc( sizeof( arp_proto_t ));
  93.     if( !( * proto )) return ENOMEM;
  94.     ( ** proto ).service = service;
  95.     ( ** proto ).addr = address;
  96.     ( ** proto ).addr_data = address->value;
  97.     if( ERROR_OCCURED( arp_addr_initialize( &( ** proto).addresses ))){
  98.         free( * proto );
  99.         return ERROR_CODE;
  100.     }
  101.     return EOK;
  102. }
  103.  
  104. int arp_device_message( device_id_t device_id, services_t service, services_t protocol, measured_string_ref address ){
  105.     ERROR_DECLARE;
  106.  
  107.     arp_device_ref  device;
  108.     aid_t           message;
  109.     ipc_call_t      answer;
  110.     ipcarg_t        result;
  111.     arp_proto_ref   proto;
  112.  
  113.     // an existing device?
  114.     device = arp_cache_find( & arp_globals.cache, device_id );
  115.     if( device ){
  116.         if( device->service != service ) return EEXIST;
  117.         proto = arp_protos_find( & device->protos, protocol );
  118.         if( proto ){
  119.             free( proto->addr );
  120.             free( proto->addr_data );
  121.             proto->addr = address;
  122.             proto->addr_data = address->value;
  123.         }else{
  124.             ERROR_PROPAGATE( arp_proto_create( & proto, protocol, address ));
  125.             if( ERROR_OCCURED( arp_protos_add( & device->protos, proto->service, proto ))){
  126.                 free( proto );
  127.                 return ERROR_CODE;
  128.             }
  129.         }
  130.         return EOK;
  131.     }else{
  132.         // create a new device
  133.         device = ( arp_device_ref ) malloc( sizeof( arp_device_t ));
  134.         if( ! device ) return ENOMEM;
  135.         device->device_id = device_id;
  136.         if( ERROR_OCCURED( arp_protos_initialize( & device->protos ))
  137.         || ERROR_OCCURED( arp_proto_create( & proto, protocol, address ))){
  138.             free( device );
  139.             return ERROR_CODE;
  140.         }
  141.         if( ERROR_OCCURED( arp_protos_add( & device->protos, proto->service, proto ))){
  142.             arp_protos_destroy( & device->protos );
  143.             free( device );
  144.             return ERROR_CODE;
  145.         }
  146.         device->service = service;
  147.         // bind the new one
  148.         device->phone = bind_service( device->service, device->device_id, SERVICE_ARP, 0, arp_receiver );
  149.         // get packet dimensions
  150.         if( ERROR_OCCURED( async_req_1_3( device->phone, NET_NIL_PACKET_SPACE, device_id, & device->prefix, & device->content, & device->sufix ))){
  151.             arp_protos_destroy( & device->protos );
  152.             free( device );
  153.             return ERROR_CODE;
  154.         }
  155.         // get hardware address
  156.         message = async_send_1( device->phone, NET_NIL_ADDR, device->device_id, & answer );
  157.         if( ERROR_OCCURED( measured_strings_return( device->phone, & device->addr, & device->addr_data, 1 ))){
  158.             arp_protos_destroy( & device->protos );
  159.             free( device );
  160.             async_wait_for( message, NULL );
  161.             return ERROR_CODE;
  162.         }
  163.         async_wait_for( message, & result );
  164.         if( ERROR_OCCURED( result )){
  165.             free( device->addr );
  166.             free( device->addr_data );
  167.             arp_protos_destroy( & device->protos );
  168.             free( device );
  169.             return ERROR_CODE;
  170.         }
  171.         // get broadcast address
  172.         message = async_send_1( device->phone, NET_NIL_BROADCAST_ADDR, device->device_id, & answer );
  173.         if( ERROR_OCCURED( measured_strings_return( device->phone, & device->broadcast_addr, & device->broadcast_data, 1 ))){
  174.             free( device->addr );
  175.             free( device->addr_data );
  176.             arp_protos_destroy( & device->protos );
  177.             free( device );
  178.             async_wait_for( message, NULL );
  179.             return ERROR_CODE;
  180.         }
  181.         async_wait_for( message, & result );
  182.         // add to the cache
  183.         if( ERROR_OCCURED( result )
  184.         || ERROR_OCCURED( arp_cache_add( & arp_globals.cache, device->device_id, device ))){
  185.             free( device->addr );
  186.             free( device->addr_data );
  187.             free( device->broadcast_addr );
  188.             free( device->broadcast_data );
  189.             arp_protos_destroy( & device->protos );
  190.             free( device );
  191.             return ERROR_CODE;
  192.         }
  193.     }
  194.     return EOK;
  195. }
  196.  
  197. measured_string_ref arp_translate_message( device_id_t device_id, services_t protocol, measured_string_ref target ){
  198. //  ERROR_DECLARE;
  199.  
  200.     arp_device_ref      device;
  201.     arp_proto_ref       proto;
  202.     measured_string_ref addr;
  203.     size_t              length;
  204.     packet_t            packet;
  205.     arp_header_ref      header;
  206.  
  207.     if( ! target ) return NULL;
  208.     device = arp_cache_find( & arp_globals.cache, device_id );
  209.     if( ! device ) return NULL;
  210.     proto = arp_protos_find( & device->protos, protocol );
  211.     if(( ! proto ) || ( proto->addr->length != target->length )) return NULL;
  212.     addr = arp_addr_find( & proto->addresses, target->value, target->length );
  213.     if( addr ) return addr;
  214.     // ARP packet content size = header + ( address + translation ) * 2
  215.     length = 8 + ( CONVERT_SIZE( char, uint8_t, proto->addr->length ) + CONVERT_SIZE( char, uint8_t, device->addr->length )) * 2;
  216.     if( length > device->content ){
  217.         return NULL;
  218.     }
  219.     packet = packet_create( device->prefix, length, device->sufix );
  220.     if( ! packet ) return NULL;
  221.     header = ( arp_header_ref ) packet_append( packet, length );
  222.     header->hardware = device->hardware;
  223.     header->hardware_length = device->addr->length;
  224.     header->protocol = protocol_map( device->service, protocol );
  225.     header->protocol_length = proto->addr->length;
  226.     header->operation = ARPOP_REQUEST;
  227.     length = sizeof( arp_header_t );
  228.     memcpy((( uint8_t * ) header ) + length, device->addr->value, device->addr->length );
  229.     length += device->addr->length;
  230.     memcpy((( uint8_t * ) header ) + length, proto->addr->value, proto->addr->length );
  231.     length += proto->addr->length;
  232.     memset((( uint8_t * ) header ) + length, 0, device->addr->length );
  233.     length += device->addr->length;
  234.     memcpy((( uint8_t * ) header ) + length, target->value, target->length );
  235.     // TODO send to the device->broadcast_addr as arp protocol
  236.     packet_send( packet, device->phone );
  237.     return NULL;
  238. }
  239.  
  240. int arp_receive_message( device_id_t device_id, packet_t packet ){
  241.     ERROR_DECLARE;
  242.  
  243.     size_t              length;
  244.     arp_header_ref      header;
  245.     arp_device_ref      device;
  246.     arp_proto_ref       proto;
  247. //  arp_addr_ref        addr;
  248.     measured_string_ref hw_source;
  249. /*  measured_string_t   proto_target;
  250.     aid_t               message;
  251.     ipcarg_t            result;
  252.     int                 index;
  253.     ipc_call_t          answer;
  254. */  int8_t *            src_hw;
  255.     int8_t *            src_proto;
  256.     int8_t *            des_hw;
  257.     int8_t *            des_proto;
  258.  
  259.     length = packet_get_data_length( packet );
  260.     if( length <= sizeof( arp_header_t )) return EINVAL;
  261.     device = arp_cache_find( & arp_globals.cache, device_id );
  262.     if( ! device ) return ENOENT;
  263.     header = ( arp_header_ref ) packet_get_data( packet );
  264.     if( header->hardware != device->hardware ) return EINVAL;
  265.     if( length < sizeof( arp_header_t ) + ( header->hardware_length + header->protocol_length ) * 2 ) return EINVAL;
  266.     proto = arp_protos_find( & device->protos, protocol_unmap( device->service, header->protocol ));
  267.     if( ! proto ) return ENOENT;
  268.     src_hw = (( int8_t * ) header ) + sizeof( arp_header_t );
  269.     src_proto = src_hw + header->hardware_length;
  270.     des_hw = src_proto + header->protocol_length;
  271.     des_proto = des_hw + header->hardware_length;
  272.     hw_source = arp_addr_find( & proto->addresses, src_proto, header->protocol_length );
  273.     // exists?
  274.     if( hw_source ){
  275.         if( hw_source->length != header->hardware_length ) return EINVAL;
  276.         memcpy( hw_source->value, src_hw, header->hardware_length );
  277.     }
  278.     // is my protocol address?
  279. /*  proto_target.value = des_proto;
  280.     proto_target.length = header->protocol_length;
  281.     // TODO send necessary?
  282.     message = async_send_0( proto->phone, NET_IL_MY_ADDR, & answer );
  283.     if( ERROR_OCCURED( measured_strings_send( device->phone, & proto_target, 1 ))){
  284.         async_wait_for( message, NULL );
  285.         return ERROR_CODE;
  286.     }
  287.     async_wait_for( message, & result );
  288.     if( result == EOK ){
  289. */  if( proto->addr->length != header->hardware_length ) return EINVAL;
  290.     if( ! strncmp( proto->addr->value, des_proto, proto->addr->length )){
  291.         // not already upadted?
  292.         if( ! hw_source ){
  293.             hw_source = measured_string_create_bulk( src_hw, header->hardware_length );
  294.             if( ! hw_source ) return ENOMEM;
  295.             ERROR_PROPAGATE( arp_addr_add( & proto->addresses, src_proto, header->protocol_length, hw_source ));
  296.         }
  297.         if( header->operation == ARPOP_REQUEST ){
  298.             header->operation = ARPOP_REPLY;
  299. /*          for( index = 0; index + header->hardware_length < header->protocol_length; index += header->hardware_length ){
  300.                 memcpy( src_hw, src_proto + index, header->hardware_length );
  301.                 memcpy( src_proto + index, des_proto + index, header->hardware_length );
  302.                 memcpy( des_proto + index, src_hw, header->hardware_length );
  303.             }
  304.             memcpy( src_hw, src_proto + index, header->hardware_length - header->protocol_length );
  305.             memcpy( src_proto + index, des_proto + index, header->hardware_length - header->protocol_length );
  306.             memcpy( des_proto + index, src_hw, header->hardware_length - header->protocol_length );
  307.             memcpy( src_hw, des_hw, header->hardware_length );
  308.             memcpy( des_hw, hw_source->value, hw_source->length );
  309. */          memcpy( des_proto, src_proto, header->protocol_length );
  310.             memcpy( src_proto, proto->addr->value, header->protocol_length );
  311.             memcpy( src_hw, des_hw, header->hardware_length );
  312.             memcpy( des_hw, hw_source->value, hw_source->length );
  313.             // TODO send to the hw_source as arp protocol
  314.             packet_send( packet, device->phone );
  315.         }
  316.     }
  317.     return EOK;
  318. }
  319.  
  320. int arp_clear_device_message( device_id_t device_id ){
  321.     arp_device_ref  device;
  322.  
  323.     device = arp_cache_find( & arp_globals.cache, device_id );
  324.     if( ! device ) return ENOENT;
  325.     clear_device( device );
  326.     return EOK;
  327. }
  328.  
  329. void clear_device( arp_device_ref device ){
  330.     int             count;
  331.     arp_proto_ref   proto;
  332.  
  333.     count = arp_protos_count( & device->protos );
  334.     while( count > 0 ){
  335.         proto = arp_protos_get_index( & device->protos, count );
  336.         if( proto->addr ) free( proto->addr );
  337.         if( proto->addr_data ) free( proto->addr_data );
  338.         arp_addr_destroy( & proto->addresses );
  339.         -- count;
  340.     }
  341.     arp_protos_clear( & device->protos );
  342. }
  343.  
  344. int arp_clean_cache_message( void ){
  345.     int             count;
  346.     arp_device_ref  device;
  347.  
  348.     count = arp_cache_count( & arp_globals.cache );
  349.     while( count > 0 ){
  350.         device = arp_cache_get_index( & arp_globals.cache, count );
  351.         if( device ){
  352.             clear_device( device );
  353.             if( device->broadcast_addr ) free( device->broadcast_addr );
  354.             if( device->broadcast_data ) free( device->broadcast_data );
  355.         }
  356.     }
  357.     arp_cache_clear( & arp_globals.cache );
  358.     return EOK;
  359. }
  360.  
  361. int arp_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
  362.     ERROR_DECLARE;
  363.  
  364. //  packet_t            packet;
  365.     measured_string_ref address;
  366.     measured_string_ref translation;
  367.     char *              data;
  368.  
  369.     * answer_count = 0;
  370.     switch( IPC_GET_METHOD( * call )){
  371.         case IPC_M_PHONE_HUNGUP:
  372.             return EOK;
  373.         case NET_ARP_DEVICE:
  374.             ERROR_PROPAGATE( measured_strings_receive( & address, & data, 1 ));
  375.             if( ERROR_OCCURED( arp_device_message( IPC_GET_DEVICE( call ), IPC_GET_SERVICE( call ), IPC_GET_PROTO( call ), address ))){
  376.                 free( address );
  377.                 free( data );
  378.             }
  379.             return ERROR_CODE;
  380.         case NET_ARP_TRANSLATE:
  381.             ERROR_PROPAGATE( measured_strings_receive( & address, & data, 1 ));
  382.             translation = arp_translate_message( IPC_GET_DEVICE( call ), IPC_GET_PROTO( call ), address );
  383.             free( address );
  384.             free( data );
  385.             if( ! translation ) return ENOENT;
  386.             return measured_strings_reply( translation, 1 );
  387.         case NET_ARP_CLEAR_DEVICE:
  388.             return arp_clear_device_message( IPC_GET_DEVICE( call ));
  389.         case NET_ARP_CLEAN_CACHE:
  390.             return arp_clean_cache_message();
  391.     }
  392.     return ENOTSUP;
  393. }
  394.  
  395. void arp_receiver( ipc_callid_t iid, ipc_call_t * icall ){
  396.     ERROR_DECLARE;
  397.  
  398.     ipc_callid_t    callid;
  399.     ipc_call_t      call;
  400. //  int             result;
  401.     packet_t        packet;
  402.  
  403.     /*
  404.      * Accept the connection
  405.      *  - Answer the first IPC_M_CONNECT_ME_TO call.
  406.      */
  407.     //TODO auto accept?
  408.     //ipc_answer_0( iid, EOK );
  409.  
  410.     while( true ){
  411.         callid = async_get_call( & call );
  412.         switch( IPC_GET_METHOD( call )){
  413.             case NET_IL_DEVICE_STATE:
  414.                 //TODO clear device if off?
  415.                 break;
  416.             case NET_IL_RECEIVED:
  417.                 if( ! ERROR_OCCURED( packet_receive( & packet ))){
  418.                     ERROR_CODE = arp_receive_message( IPC_GET_DEVICE( & call ), packet );
  419.                 }
  420.                 ipc_answer_0( callid, ERROR_CODE );
  421.                 break;
  422.             default:
  423.                 ipc_answer_0( callid, ENOTSUP );
  424.         }
  425.     }
  426. }
  427.  
  428. /** @}
  429.  */
  430.