Subversion Repositories HelenOS

Compare Revisions

No changes between revisions

Ignore whitespace Rev 4706 → Rev 4707

/branches/network/uspace/srv/net/il/icmp/icmp.c
0,0 → 1,273
/*
* Copyright (c) 2008 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 icmp
* @{
*/
 
/** @file
* ICMP module implementation.
* @see icmp.h
*/
 
#include <async.h>
#include <fibril_sync.h>
 
#include <ipc/ipc.h>
#include <ipc/services.h>
 
#include "../../err.h"
#include "../../messages.h"
#include "../../modules.h"
 
#include "../../structures/packet/packet_client.h"
 
#include "../../include/crc.h"
#include "../../include/icmp_codes.h"
#include "../../include/icmp_client.h"
#include "../../include/icmp_interface.h"
#include "../../include/ip_client.h"
#include "../../include/ip_interface.h"
#include "../../include/il_interface.h"
#include "../../include/ip_protocols.h"
 
#include "../../tl/tl_messages.h"
 
#include "icmp.h"
#include "icmp_header.h"
#include "icmp_messages.h"
#include "icmp_module.h"
 
#define ICMP_KEEP_LENGTH 8
 
#define ICMP_HEADER_CHECKSUM( header ) compact_checksum(compute_checksum( 0, ( uint8_t * ) header, sizeof( icmp_header_t )))
 
/** Processes the received ICMP packet.
* Is used as an entry point from the underlying IP module.
* Releases the packet on error.
* @param device_id The device identifier. Ignored parameter.
* @param packet The received packet. Input/output parameter.
* @param receiver The target service. Ignored parameter.
* @param error The packet error reporting service. Prefixes the received packet. Input parameter.
* @returns EOK on success.
* @returns Other error codes as defined for the icmp_process_packet() function.
*/
int icmp_received_msg( device_id_t device_id, packet_t packet, services_t receiver, services_t error );
 
/** Processes the received ICMP packet.
* Notifies the destination socket application.
* @param packet The received packet. Input/output parameter.
* @returns EOK on success.
* @returns EINVAL if the packet is not valid.
* @returns EINVAL if the stored packet address is not the an_addr_t.
* @returns EINVAL if the packet does not contain any data.
* @returns NO_DATA if the packet content is shorter than the user datagram header.
* @returns ENOMEM if there is not enough memory left.
* @returns EADDRNOTAVAIL if the destination socket does not exist.
* @returns Other error codes as defined for the ip_client_process_packet() function.
*/
int icmp_process_packet( packet_t packet );
 
icmp_header_ref icmp_prepare_packet( packet_t packet );
int icmp_send_packet( icmp_type_t type, icmp_code_t code, packet_t packet, icmp_header_ref header );
 
/** ICMP global data.
*/
icmp_globals_t icmp_globals;
 
int icmp_echo_msg( int icmp_phone, icmp_param_t identifier, icmp_param_t sequence_number, const void * data, size_t length ){
// TODO echo - with timeout
// waits for the reply up to the timeout
return ENOTSUP;
}
 
int icmp_destination_unreachable_msg( int icmp_phone, icmp_code_t code, icmp_param_t mtu, packet_t packet ){
icmp_header_ref header;
 
header = icmp_prepare_packet( packet );
if( ! header ) return ENOMEM;
if( mtu ){
header->un.frag.mtu = mtu;
}
return icmp_send_packet( ICMP_DEST_UNREACH, code, packet, header );
}
 
int icmp_source_quench_msg( int icmp_phone, packet_t packet ){
icmp_header_ref header;
 
header = icmp_prepare_packet( packet );
if( ! header ) return ENOMEM;
return icmp_send_packet( ICMP_SOURCE_QUENCH, ICMP_SOURCE_QUENCH, packet, header );
}
 
int icmp_time_exceeded_msg( int icmp_phone, icmp_code_t code, packet_t packet ){
icmp_header_ref header;
 
header = icmp_prepare_packet( packet );
if( ! header ) return ENOMEM;
return icmp_send_packet( ICMP_TIME_EXCEEDED, code, packet, header );
}
 
int icmp_parameter_problem_msg( int icmp_phone, icmp_code_t code, icmp_param_t pointer, packet_t packet ){
icmp_header_ref header;
 
header = icmp_prepare_packet( packet );
if( ! header ) return ENOMEM;
header->un.param.pointer = pointer;
return icmp_send_packet( ICMP_PARAMETERPROB, code, packet, header );
}
 
icmp_header_ref icmp_prepare_packet( packet_t packet ){
icmp_header_ref header;
int header_length;
size_t total_length;
 
total_length = packet_get_data_length( packet );
if( total_length <= 0 ) return NULL;
header_length = ip_client_header_length( packet );
if( header_length <= 0 ) return NULL;
// truncate if longer than 64 bits (without the IP header)
if( total_length - header_length > ICMP_KEEP_LENGTH ){
if( packet_trim( packet, 0, total_length - header_length - ICMP_KEEP_LENGTH ) != EOK ) return NULL;
}
header = PACKET_PREFIX( packet, icmp_header_t );
if( ! header ){
pq_release( icmp_globals.net_phone, packet_get_id( packet ));
return NULL;
}
bzero( header, sizeof( * header ));
return header;
}
 
int icmp_send_packet( icmp_type_t type, icmp_code_t code, packet_t packet, icmp_header_ref header ){
ERROR_DECLARE;
 
header->type = type;
header->code = code;
header->checksum = 0;
header->checksum = ICMP_HEADER_CHECKSUM( header );
if( ERROR_OCCURRED( ip_client_prepare_packet( packet, IPPROTO_ICMP, 0, 0, 0, 0 ))){
pq_release( icmp_globals.net_phone, packet_get_id( packet ));
return ERROR_CODE;
}
return ip_send_msg( icmp_globals.ip_phone, -1, packet, SERVICE_ICMP, SERVICE_ICMP );
}
 
int icmp_connect_module( services_t service ){
return EOK;
}
 
int icmp_initialize( async_client_conn_t client_connection ){
ERROR_DECLARE;
 
fibril_rwlock_initialize( & icmp_globals.lock );
fibril_rwlock_write_lock( & icmp_globals.lock );
icmp_globals.ip_phone = ip_bind_service( SERVICE_IP, IPPROTO_ICMP, SERVICE_ICMP, client_connection, icmp_received_msg );
if( icmp_globals.ip_phone < 0 ){
return icmp_globals.ip_phone;
}
ERROR_PROPAGATE( ip_packet_size_req( icmp_globals.ip_phone, -1, & icmp_globals.addr_len, & icmp_globals.prefix, & icmp_globals.content, & icmp_globals.suffix ));
icmp_globals.prefix += sizeof( icmp_header_t );
icmp_globals.content -= sizeof( icmp_header_t );
fibril_rwlock_write_unlock( & icmp_globals.lock );
return EOK;
}
 
int icmp_received_msg( device_id_t device_id, packet_t packet, services_t receiver, services_t error ){
ERROR_DECLARE;
 
if( error ){
pq_release( icmp_globals.net_phone, packet_get_id( packet ));
return EINVAL;
}
if( ERROR_OCCURRED( icmp_process_packet( packet ))){
pq_release( icmp_globals.net_phone, packet_get_id( packet ));
return ERROR_CODE;
}
 
return EOK;
}
 
int icmp_process_packet( packet_t packet ){
int length;
void * data;
icmp_header_ref header;
 
// get rid of the ip header
length = ip_client_process_packet( packet, NULL, NULL, NULL, NULL, NULL );
if( length < 0 ) return length;
packet_trim( packet, length, 0 );
 
length = packet_get_data_length( packet );
if( length <= 0 ) return EINVAL;
if( length < sizeof( icmp_header_t )) return EINVAL;
data = packet_get_data( packet );
if( ! data ) return EINVAL;
// get icmp header
header = ( icmp_header_ref ) data;
// checksum
if(( header->checksum ) && ( ICMP_HEADER_CHECKSUM( header ))){
return EINVAL;
}
// TODO process requests and replies
ip_received_error_msg( icmp_globals.ip_phone, -1, packet, SERVICE_IP, SERVICE_ICMP );
return EOK;
}
 
int icmp_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
ERROR_DECLARE;
 
packet_t packet;
 
* answer_count = 0;
switch( IPC_GET_METHOD( * call )){
case NET_TL_RECEIVED:
fibril_rwlock_read_lock( & icmp_globals.lock );
if( ! ERROR_OCCURRED( packet_translate( icmp_globals.net_phone, & packet, IPC_GET_PACKET( call )))){
ERROR_CODE = icmp_received_msg( IPC_GET_DEVICE( call ), packet, SERVICE_ICMP, IPC_GET_ERROR( call ));
}
fibril_rwlock_read_unlock( & icmp_globals.lock );
return ERROR_CODE;
case NET_ICMP_DEST_UNREACH:
ERROR_PROPAGATE( packet_translate( icmp_globals.net_phone, & packet, IPC_GET_PACKET( call )));
return icmp_destination_unreachable_msg( 0, ICMP_GET_CODE( call ), ICMP_GET_MTU( call ), packet );
case NET_ICMP_SOURCE_QUENCH:
ERROR_PROPAGATE( packet_translate( icmp_globals.net_phone, & packet, IPC_GET_PACKET( call )));
return icmp_source_quench_msg( 0, packet );
case NET_ICMP_TIME_EXCEEDED:
ERROR_PROPAGATE( packet_translate( icmp_globals.net_phone, & packet, IPC_GET_PACKET( call )));
return icmp_time_exceeded_msg( 0, ICMP_GET_CODE( call ), packet );
case NET_ICMP_PARAMETERPROB:
ERROR_PROPAGATE( packet_translate( icmp_globals.net_phone, & packet, IPC_GET_PACKET( call )));
return icmp_parameter_problem_msg( 0, ICMP_GET_CODE( call ), ICMP_GET_POINTER( call ), packet );
}
return ENOTSUP;
}
 
/** @}
*/
Property changes:
Added: svn:eol-style
+native
\ No newline at end of property