36,11 → 36,15 |
*/ |
|
#include <async.h> |
#include <atomic.h> |
#include <fibril.h> |
#include <fibril_sync.h> |
|
#include <ipc/ipc.h> |
#include <ipc/services.h> |
|
#include <sys/types.h> |
|
#include "../../err.h" |
#include "../../messages.h" |
#include "../../modules.h" |
47,14 → 51,20 |
|
#include "../../structures/packet/packet_client.h" |
|
#include "../../include/byteorder.h" |
#include "../../include/crc.h" |
#include "../../include/icmp_api.h" |
#include "../../include/icmp_client.h" |
#include "../../include/icmp_codes.h" |
#include "../../include/icmp_client.h" |
#include "../../include/icmp_common.h" |
#include "../../include/icmp_interface.h" |
#include "../../include/il_interface.h" |
#include "../../include/inet.h" |
#include "../../include/ip_client.h" |
#include "../../include/ip_interface.h" |
#include "../../include/il_interface.h" |
#include "../../include/ip_protocols.h" |
#include "../../include/socket_codes.h" |
#include "../../include/socket_errno.h" |
|
#include "../../tl/tl_messages.h" |
|
65,8 → 75,12 |
|
#define ICMP_KEEP_LENGTH 8 |
|
#define ICMP_HEADER_CHECKSUM( header ) compact_checksum(compute_checksum( 0, ( uint8_t * ) header, sizeof( icmp_header_t ))) |
#define ICMP_CHECKSUM( header, length ) htons( ip_checksum(( uint8_t * ) ( header ), ( length ))) |
|
#define ICMP_ECHO_TEXT "Hello from HelenOS." |
|
#define ICMP_GET_LOCK_KEY( id, sequence ) ((( id ) << 16 ) | ( sequence & 0xFFFF )) |
|
/** Processes the received ICMP packet. |
* Is used as an entry point from the underlying IP module. |
* Releases the packet on error. |
82,6 → 96,7 |
/** Processes the received ICMP packet. |
* Notifies the destination socket application. |
* @param packet The received packet. Input/output parameter. |
* @param error The packet error reporting service. Prefixes the received packet. Input 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. |
91,21 → 106,148 |
* @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 ); |
int icmp_process_packet( packet_t packet, services_t error ); |
|
/** Processes the client messages. |
* Remenbers the assigned identifier and sequence numbers. |
* Runs until the client module disconnects. |
* @param callid The message identifier. Input parameter. |
* @param call The message parameters. Input parameter. |
* @returns EOK on success. |
* @see icmp_interface.h |
*/ |
int icmp_process_client_messages( ipc_callid_t callid, ipc_call_t call ); |
|
/** Releases the packet and returns the result. |
* @param packet The packet queue to be released. Input parameter. |
* @param result The result to be returned. Input parameter. |
* @return The result parameter. |
*/ |
int icmp_release_and_return( packet_t packet, int result ); |
|
int icmp_echo( icmp_param_t id, icmp_param_t sequence, size_t size, suseconds_t timeout, ip_ttl_t ttl, ip_tos_t tos, int dont_fragment, const struct sockaddr * addr, socklen_t addrlen ); |
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 ); |
int icmp_send_packet( icmp_type_t type, icmp_code_t code, packet_t packet, icmp_header_ref header, services_t error ); |
int process_echo_reply( packet_t packet, icmp_header_ref header, icmp_type_t type, icmp_code_t code ); |
|
/** 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_MAP_IMPLEMENT( time_locks, atomic_t ); |
|
GENERIC_FIELD_IMPLEMENT( echo_data, icmp_echo_t ); |
|
int icmp_echo_msg( int icmp_phone, size_t size, suseconds_t timeout, ip_ttl_t ttl, ip_tos_t tos, int dont_fragment, const struct sockaddr * addr, socklen_t addrlen ){ |
icmp_echo_ref echo_data; |
int res; |
|
fibril_rwlock_write_lock( & icmp_globals.lock ); |
// use the phone as the echo data index |
echo_data = echo_data_get_index( & icmp_globals.echo_data, icmp_phone ); |
if( ! echo_data ){ |
res = ENOENT; |
}else{ |
res = icmp_echo( echo_data->id, echo_data->sequence, size, timeout, ttl, tos, dont_fragment, addr, addrlen ); |
++ echo_data->sequence; |
} |
fibril_rwlock_write_unlock( & icmp_globals.lock ); |
return res; |
} |
|
int icmp_echo( icmp_param_t id, icmp_param_t sequence, size_t size, suseconds_t timeout, ip_ttl_t ttl, ip_tos_t tos, int dont_fragment, const struct sockaddr * addr, socklen_t addrlen ){ |
ERROR_DECLARE; |
|
icmp_header_ref header; |
packet_t packet; |
size_t offset; |
uint8_t * data; |
atomic_t * lock; |
int result; |
int index; |
int lock_key; |
struct sockaddr_in * address_in; |
struct timeval time_before; |
struct timeval time_after; |
|
// TODO do not ask all the time |
ERROR_PROPAGATE( ip_packet_size_req( icmp_globals.ip_phone, -1, & icmp_globals.addr_len, & icmp_globals.prefix, & icmp_globals.content, & icmp_globals.suffix )); |
packet = packet_get_4( icmp_globals.net_phone, size, icmp_globals.addr_len, sizeof( icmp_header_t ) + icmp_globals.prefix, icmp_globals.suffix ); |
if( ! packet ) return ENOMEM; |
// set the destination address |
if( addrlen < sizeof( struct sockaddr )){ |
return icmp_release_and_return( packet, EINVAL ); |
} |
switch( addr->sa_family ){ |
case AF_INET: |
if( addrlen != sizeof( struct sockaddr_in )){ |
return icmp_release_and_return( packet, EINVAL ); |
} |
address_in = ( struct sockaddr_in * ) addr; |
if( ERROR_OCCURRED( packet_set_addr( packet, NULL, ( uint8_t * ) & address_in->sin_addr.s_addr, sizeof( address_in->sin_addr.s_addr )))){ |
return icmp_release_and_return( packet, ERROR_CODE ); |
} |
break; |
default: |
return icmp_release_and_return( packet, EAFNOSUPPORT ); |
} |
// allocate space in the packet |
data = ( uint8_t * ) packet_suffix( packet, size ); |
if( ! data ){ |
return icmp_release_and_return( packet, ENOMEM ); |
} |
offset = 0; |
while( size > offset + sizeof( ICMP_ECHO_TEXT )){ |
memcpy( data + offset, ICMP_ECHO_TEXT, sizeof( ICMP_ECHO_TEXT )); |
offset += sizeof( ICMP_ECHO_TEXT ); |
} |
memcpy( data + offset, ICMP_ECHO_TEXT, size - offset ); |
header = icmp_prepare_packet( packet ); |
if( ! header ){ |
return icmp_release_and_return( packet, ENOMEM ); |
} |
header->un.echo.id = id; |
header->un.echo.sequence = sequence; |
lock_key = ICMP_GET_LOCK_KEY( header->un.echo.id, header->un.echo.sequence ); |
// create a locked fuxed |
lock = malloc( sizeof( * lock )); |
if( ! lock ){ |
return icmp_release_and_return( packet, ENOMEM ); |
} |
atomic_set( lock, 0 ); |
index = time_locks_add( & icmp_globals.time_locks, lock_key, lock ); |
if( index < 0 ){ |
free( lock ); |
return icmp_release_and_return( packet, index ); |
} |
if( ERROR_OCCURRED( icmp_send_packet( ICMP_ECHO, 0, packet, header, 0 ))){ |
free( lock ); |
return icmp_release_and_return( packet, ERROR_CODE ); |
} |
// unlock the global to allow unlocking and other fibrils to work |
// try to lock again - may be unlocked by the reply |
ERROR_PROPAGATE( gettimeofday( & time_before, NULL )); |
do{ |
result = atomic_get( lock ); |
if( result ){ |
break; |
}else{ |
fibril_rwlock_write_unlock( & icmp_globals.lock ); |
// TODO does not yield? |
//printf( "y %d\n", fibril_yield()); |
fibril_yield(); |
fibril_rwlock_write_lock( & icmp_globals.lock ); |
ERROR_PROPAGATE( gettimeofday( & time_after, NULL )); |
} |
}while( tv_sub( & time_after, & time_before ) <= timeout ); |
if( ! result ){ |
result = ELIMIT; |
} |
// destroy the lock |
time_locks_exclude_index( & icmp_globals.time_locks, index ); |
return result; |
} |
|
int icmp_destination_unreachable_msg( int icmp_phone, icmp_code_t code, icmp_param_t mtu, packet_t packet ){ |
icmp_header_ref header; |
|
114,7 → 256,7 |
if( mtu ){ |
header->un.frag.mtu = mtu; |
} |
return icmp_send_packet( ICMP_DEST_UNREACH, code, packet, header ); |
return icmp_send_packet( ICMP_DEST_UNREACH, code, packet, header, SERVICE_ICMP ); |
} |
|
int icmp_source_quench_msg( int icmp_phone, packet_t packet ){ |
122,7 → 264,7 |
|
header = icmp_prepare_packet( packet ); |
if( ! header ) return ENOMEM; |
return icmp_send_packet( ICMP_SOURCE_QUENCH, ICMP_SOURCE_QUENCH, packet, header ); |
return icmp_send_packet( ICMP_SOURCE_QUENCH, 0, packet, header, SERVICE_ICMP ); |
} |
|
int icmp_time_exceeded_msg( int icmp_phone, icmp_code_t code, packet_t packet ){ |
130,7 → 272,7 |
|
header = icmp_prepare_packet( packet ); |
if( ! header ) return ENOMEM; |
return icmp_send_packet( ICMP_TIME_EXCEEDED, code, packet, header ); |
return icmp_send_packet( ICMP_TIME_EXCEEDED, code, packet, header, SERVICE_ICMP ); |
} |
|
int icmp_parameter_problem_msg( int icmp_phone, icmp_code_t code, icmp_param_t pointer, packet_t packet ){ |
139,7 → 281,7 |
header = icmp_prepare_packet( packet ); |
if( ! header ) return ENOMEM; |
header->un.param.pointer = pointer; |
return icmp_send_packet( ICMP_PARAMETERPROB, code, packet, header ); |
return icmp_send_packet( ICMP_PARAMETERPROB, code, packet, header, SERVICE_ICMP ); |
} |
|
icmp_header_ref icmp_prepare_packet( packet_t packet ){ |
164,22 → 306,39 |
return header; |
} |
|
int icmp_send_packet( icmp_type_t type, icmp_code_t code, packet_t packet, icmp_header_ref header ){ |
int icmp_send_packet( icmp_type_t type, icmp_code_t code, packet_t packet, icmp_header_ref header, services_t error ){ |
ERROR_DECLARE; |
|
header->type = type; |
header->code = code; |
header->checksum = 0; |
header->checksum = ICMP_HEADER_CHECKSUM( header ); |
header->checksum = ICMP_CHECKSUM( header, packet_get_data_length( packet )); |
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 ); |
return ip_send_msg( icmp_globals.ip_phone, -1, packet, SERVICE_ICMP, error ); |
} |
|
int icmp_connect_module( services_t service ){ |
return EOK; |
icmp_echo_ref echo_data; |
int index; |
|
echo_data = ( icmp_echo_ref ) malloc( sizeof( * echo_data )); |
if( ! echo_data ) return ENOMEM; |
// assign a new identifier |
fibril_rwlock_write_lock( & icmp_globals.lock ); |
++ icmp_globals.last_used_id; |
echo_data->id = icmp_globals.last_used_id; |
echo_data->sequence = 0; |
// remember the assigned echo data |
index = echo_data_add( & icmp_globals.echo_data, echo_data ); |
if( index < 0 ){ |
free( echo_data ); |
} |
fibril_rwlock_write_unlock( & icmp_globals.lock ); |
// return the echo data index as the ICMP phone |
return index; |
} |
|
int icmp_initialize( async_client_conn_t client_connection ){ |
187,6 → 346,8 |
|
fibril_rwlock_initialize( & icmp_globals.lock ); |
fibril_rwlock_write_lock( & icmp_globals.lock ); |
time_locks_initialize( & icmp_globals.time_locks ); |
echo_data_initialize( & icmp_globals.echo_data ); |
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; |
201,12 → 362,8 |
int icmp_received_msg( device_id_t device_id, packet_t packet, services_t receiver, services_t error ){ |
ERROR_DECLARE; |
|
if( error ){ |
if( ERROR_OCCURRED( icmp_process_packet( packet, 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; |
} |
|
213,12 → 370,34 |
return EOK; |
} |
|
int icmp_process_packet( packet_t packet ){ |
int icmp_process_packet( packet_t packet, services_t error ){ |
ERROR_DECLARE; |
|
size_t length; |
uint8_t * src; |
int addrlen; |
int result; |
void * data; |
icmp_header_ref header; |
icmp_type_t type; |
icmp_code_t code; |
|
if( error ){ |
switch( error ){ |
case SERVICE_ICMP: |
// process error |
// TODO remove debug dump |
// length = icmp_client_header_length( packet ); |
result = icmp_client_process_packet( packet, & type, & code, NULL, NULL ); |
if( result < 0 ) return result; |
length = ( size_t ) result; |
printf( "ICMP error %d (%d) in packet %d\n", type, code, packet_get_id( packet ) ); |
ERROR_PROPAGATE( packet_trim( packet, length, 0 )); |
break; |
default: |
return ENOTSUP; |
} |
} |
// get rid of the ip header |
result = ip_client_process_packet( packet, NULL, NULL, NULL, NULL, NULL ); |
if( result < 0 ) return result; |
232,11 → 411,60 |
// get icmp header |
header = ( icmp_header_ref ) data; |
// checksum |
if(( header->checksum ) && ( ICMP_HEADER_CHECKSUM( header ))){ |
if(( header->checksum ) && ( ICMP_CHECKSUM( header, length ))){ |
return EINVAL; |
} |
// TODO process requests and replies |
ip_received_error_msg( icmp_globals.ip_phone, -1, packet, SERVICE_IP, SERVICE_ICMP ); |
switch( header->type ){ |
case ICMP_ECHOREPLY: |
return process_echo_reply( packet, header, ICMP_ECHO, 0 ); |
case ICMP_ECHO: |
if( error ){ |
return process_echo_reply( packet, header, type, code ); |
}else{ |
addrlen = packet_get_addr( packet, & src, NULL ); |
if(( addrlen > 0 ) |
// set both addresses to the source one (avoids the source address deletion before setting the destination one) |
&& ( packet_set_addr( packet, src, src, ( size_t ) addrlen ) == EOK )){ |
// send the reply |
return icmp_send_packet( ICMP_ECHOREPLY, 0, packet, header, 0 ); |
}else{ |
return icmp_release_and_return( packet, EINVAL ); |
} |
} |
case ICMP_DEST_UNREACH: |
case ICMP_SOURCE_QUENCH: |
case ICMP_REDIRECT: |
case ICMP_ALTERNATE_ADDR: |
case ICMP_ROUTER_ADV: |
case ICMP_ROUTER_SOL: |
case ICMP_TIME_EXCEEDED: |
case ICMP_PARAMETERPROB: |
case ICMP_CONVERSION_ERROR: |
case ICMP_REDIRECT_MOBILE: |
case ICMP_SKIP: |
case ICMP_PHOTURIS: |
fibril_rwlock_read_lock( & icmp_globals.lock ); |
ip_received_error_msg( icmp_globals.ip_phone, -1, packet, SERVICE_IP, SERVICE_ICMP ); |
fibril_rwlock_read_unlock( & icmp_globals.lock ); |
return EOK; |
default: |
return icmp_release_and_return( packet, ENOTSUP ); |
} |
} |
|
int process_echo_reply( packet_t packet, icmp_header_ref header, icmp_type_t type, icmp_code_t code ){ |
int lock_key; |
atomic_t * lock; |
|
lock_key = ICMP_GET_LOCK_KEY( header->un.echo.id, header->un.echo.sequence ); |
fibril_rwlock_write_lock( & icmp_globals.lock ); |
lock = time_locks_find( & icmp_globals.time_locks, lock_key ); |
if( lock ){ |
// unlock the lock for the waiting fibril |
atomic_set( lock, type ); |
} |
pq_release( icmp_globals.net_phone, packet_get_id( packet )); |
fibril_rwlock_write_unlock( & icmp_globals.lock ); |
return EOK; |
} |
|
243,32 → 471,111 |
int icmp_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){ |
ERROR_DECLARE; |
|
packet_t packet; |
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 ); |
case NET_ICMP_INIT: |
return icmp_process_client_messages( callid, * call ); |
} |
return ENOTSUP; |
} |
|
int icmp_process_client_messages( ipc_callid_t callid, ipc_call_t call ){ |
ERROR_DECLARE; |
|
bool keep_on_going = true; |
fibril_rwlock_t lock; |
ipc_call_t answer; |
int answer_count; |
packet_t packet; |
size_t addrlen; |
struct sockaddr * addr; |
icmp_param_t id; |
icmp_param_t sequence = 0; |
ipc_callid_t data_callid; |
|
/* |
* Accept the connection |
* - Answer the first NET_ICMP_INIT call. |
*/ |
ipc_answer_0( callid, EOK ); |
|
fibril_rwlock_initialize( & lock ); |
|
// assign a new identifier |
fibril_rwlock_write_lock( & icmp_globals.lock ); |
++ icmp_globals.last_used_id; |
id = icmp_globals.last_used_id; |
fibril_rwlock_write_unlock( & icmp_globals.lock ); |
|
while( keep_on_going ){ |
refresh_answer( & answer, & answer_count ); |
|
callid = async_get_call( & call ); |
|
switch( IPC_GET_METHOD( call )){ |
case IPC_M_PHONE_HUNGUP: |
keep_on_going = false; |
ERROR_CODE = EOK; |
break; |
case NET_ICMP_ECHO: |
fibril_rwlock_write_lock( & lock ); |
if( ! ipc_data_write_receive( & data_callid, & addrlen )){ |
ERROR_CODE = EINVAL; |
}else{ |
addr = malloc( addrlen ); |
if( ! addr ){ |
ERROR_CODE = ENOMEM; |
}else{ |
if( ! ERROR_OCCURRED( ipc_data_write_finalize( data_callid, addr, addrlen ))){ |
fibril_rwlock_write_lock( & icmp_globals.lock ); |
ERROR_CODE = icmp_echo( id, sequence, ICMP_GET_SIZE( call ), ICMP_GET_TIMEOUT( call ), ICMP_GET_TTL( call ), ICMP_GET_TOS( call ), ICMP_GET_DONT_FRAGMENT( call ), addr, addrlen ); |
fibril_rwlock_write_unlock( & icmp_globals.lock ); |
free( addr ); |
++ sequence; |
} |
} |
} |
fibril_rwlock_write_unlock( & lock ); |
break; |
case NET_ICMP_DEST_UNREACH: |
if( ! ERROR_OCCURRED( packet_translate( icmp_globals.net_phone, & packet, IPC_GET_PACKET( & call )))){ |
ERROR_CODE = icmp_destination_unreachable_msg( 0, ICMP_GET_CODE( call ), ICMP_GET_MTU( call ), packet ); |
} |
break; |
case NET_ICMP_SOURCE_QUENCH: |
if( ! ERROR_OCCURRED( packet_translate( icmp_globals.net_phone, & packet, IPC_GET_PACKET( & call )))){ |
ERROR_CODE = icmp_source_quench_msg( 0, packet ); |
} |
case NET_ICMP_TIME_EXCEEDED: |
if( ! ERROR_OCCURRED( packet_translate( icmp_globals.net_phone, & packet, IPC_GET_PACKET( & call )))){ |
ERROR_CODE = icmp_time_exceeded_msg( 0, ICMP_GET_CODE( call ), packet ); |
} |
break; |
case NET_ICMP_PARAMETERPROB: |
if( ! ERROR_OCCURRED( packet_translate( icmp_globals.net_phone, & packet, IPC_GET_PACKET( & call )))){ |
ERROR_CODE = icmp_parameter_problem_msg( 0, ICMP_GET_CODE( call ), ICMP_GET_POINTER( call ), packet ); |
} |
default: |
ERROR_CODE = ENOTSUP; |
} |
|
answer_call( callid, ERROR_CODE, & answer, answer_count ); |
} |
|
return EOK; |
} |
|
int icmp_release_and_return( packet_t packet, int result ){ |
pq_release( icmp_globals.net_phone, packet_get_id( packet )); |
return result; |
} |
|
/** @} |
*/ |