source: mainline/uspace/srv/net/tl/icmp/icmp.c@ b5cbff4

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since b5cbff4 was b5cbff4, checked in by Jakub Jermar <jakub@…>, 15 years ago

Do not pass icmp_globals.lock ownership between fibrils.
Fix some typos and comments in the ICMP module.

  • Property mode set to 100644
File size: 26.5 KB
Line 
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 icmp
30 * @{
31 */
32
33/** @file
34 * ICMP module implementation.
35 * @see icmp.h
36 */
37
38#include <async.h>
39#include <atomic.h>
40#include <fibril.h>
41#include <fibril_synch.h>
42#include <stdint.h>
43
44#include <ipc/ipc.h>
45#include <ipc/services.h>
46
47#include <sys/time.h>
48#include <sys/types.h>
49
50#include "../../err.h"
51#include "../../messages.h"
52#include "../../modules.h"
53
54#include "../../structures/packet/packet_client.h"
55
56#include "../../include/byteorder.h"
57#include "../../include/checksum.h"
58#include "../../include/icmp_api.h"
59#include "../../include/icmp_client.h"
60#include "../../include/icmp_codes.h"
61#include "../../include/icmp_common.h"
62#include "../../include/icmp_interface.h"
63#include "../../include/il_interface.h"
64#include "../../include/inet.h"
65#include "../../include/ip_client.h"
66#include "../../include/ip_interface.h"
67#include "../../include/ip_protocols.h"
68#include "../../include/net_interface.h"
69#include "../../include/socket_codes.h"
70#include "../../include/socket_errno.h"
71
72#include "../../tl/tl_messages.h"
73
74#include "icmp.h"
75#include "icmp_header.h"
76#include "icmp_messages.h"
77#include "icmp_module.h"
78
79/** Default ICMP error reporting.
80 */
81#define NET_DEFAULT_ICMP_ERROR_REPORTING true
82
83/** Default ICMP echo replying.
84 */
85#define NET_DEFAULT_ICMP_ECHO_REPLYING true
86
87/** Original datagram length in bytes transfered to the error notification message.
88 */
89#define ICMP_KEEP_LENGTH 8
90
91/** Free identifier numbers pool start.
92 */
93#define ICMP_FREE_IDS_START 1
94
95/** Free identifier numbers pool end.
96 */
97#define ICMP_FREE_IDS_END MAX_UINT16
98
99/** Computes the ICMP datagram checksum.
100 * @param[in,out] header The ICMP datagram header.
101 * @param[in] length The total datagram length.
102 * @returns The computed checksum.
103 */
104#define ICMP_CHECKSUM( header, length ) htons( ip_checksum(( uint8_t * ) ( header ), ( length )))
105
106/** An echo request datagrams pattern.
107 */
108#define ICMP_ECHO_TEXT "Hello from HelenOS."
109
110/** Computes an ICMP reply data key.
111 * @param[in] id The message identifier.
112 * @param[in] sequence The message sequence number.
113 * @returns The computed ICMP reply data key.
114 */
115#define ICMP_GET_REPLY_KEY( id, sequence ) ((( id ) << 16 ) | ( sequence & 0xFFFF ))
116
117/** Processes the received ICMP packet.
118 * Is used as an entry point from the underlying IP module.
119 * Releases the packet on error.
120 * @param device_id The device identifier. Ignored parameter.
121 * @param[in,out] packet The received packet.
122 * @param receiver The target service. Ignored parameter.
123 * @param[in] error The packet error reporting service. Prefixes the received packet.
124 * @returns EOK on success.
125 * @returns Other error codes as defined for the icmp_process_packet() function.
126 */
127int icmp_received_msg( device_id_t device_id, packet_t packet, services_t receiver, services_t error );
128
129/** Processes the received ICMP packet.
130 * Notifies the destination socket application.
131 * @param[in,out] packet The received packet.
132 * @param[in] error The packet error reporting service. Prefixes the received packet.
133 * @returns EOK on success.
134 * @returns EINVAL if the packet is not valid.
135 * @returns EINVAL if the stored packet address is not the an_addr_t.
136 * @returns EINVAL if the packet does not contain any data.
137 * @returns NO_DATA if the packet content is shorter than the user datagram header.
138 * @returns ENOMEM if there is not enough memory left.
139 * @returns EADDRNOTAVAIL if the destination socket does not exist.
140 * @returns Other error codes as defined for the ip_client_process_packet() function.
141 */
142int icmp_process_packet( packet_t packet, services_t error );
143
144/** Processes the client messages.
145 * Remembers the assigned identifier and sequence numbers.
146 * Runs until the client module disconnects.
147 * @param[in] callid The message identifier.
148 * @param[in] call The message parameters.
149 * @returns EOK.
150 * @see icmp_interface.h
151 * @see icmp_api.h
152 */
153int icmp_process_client_messages( ipc_callid_t callid, ipc_call_t call );
154
155/** Processes the generic client messages.
156 * @param[in] call The message parameters.
157 * @returns EOK on success.
158 * @returns ENOTSUP if the message is not known.
159 * @returns Other error codes as defined for the packet_translate() function.
160 * @returns Other error codes as defined for the icmp_destination_unreachable_msg() function.
161 * @returns Other error codes as defined for the icmp_source_quench_msg() function.
162 * @returns Other error codes as defined for the icmp_time_exceeded_msg() function.
163 * @returns Other error codes as defined for the icmp_parameter_problem_msg() function.
164 * @see icmp_interface.h
165 */
166int icmp_process_message( ipc_call_t * call );
167
168/** Releases the packet and returns the result.
169 * @param[in] packet The packet queue to be released.
170 * @param[in] result The result to be returned.
171 * @returns The result parameter.
172 */
173int icmp_release_and_return( packet_t packet, int result );
174
175/** Requests an echo message.
176 * Sends a packet with specified parameters to the target host and waits for the reply upto the given timeout.
177 * Blocks the caller until the reply or the timeout occurs.
178 * @param[in] id The message identifier.
179 * @param[in] sequence The message sequence parameter.
180 * @param[in] size The message data length in bytes.
181 * @param[in] timeout The timeout in miliseconds.
182 * @param[in] ttl The time to live.
183 * @param[in] tos The type of service.
184 * @param[in] dont_fragment The value indicating whether the datagram must not be fragmented. Is used as a MTU discovery.
185 * @param[in] addr The target host address.
186 * @param[in] addrlen The torget host address length.
187 * @returns ICMP_ECHO on success.
188 * @returns ETIMEOUT if the reply has not arrived before the timeout.
189 * @returns ICMP type of the received error notification.
190 * @returns EINVAL if the addrlen parameter is less or equal to zero (<=0).
191 * @returns ENOMEM if there is not enough memory left.
192 * @returns EPARTY if there was an internal error.
193 */
194int icmp_echo( icmp_param_t id, icmp_param_t sequence, size_t size, mseconds_t timeout, ip_ttl_t ttl, ip_tos_t tos, int dont_fragment, const struct sockaddr * addr, socklen_t addrlen );
195
196/** Prepares the ICMP error packet.
197 * Truncates the original packet if longer than ICMP_KEEP_LENGTH bytes.
198 * Prefixes and returns the ICMP header.
199 * @param[in,out] packet The original packet.
200 * @returns The prefixed ICMP header.
201 * @returns NULL on errors.
202 */
203icmp_header_ref icmp_prepare_packet( packet_t packet );
204
205/** Sends the ICMP message.
206 * Sets the message type and code and computes the checksum.
207 * Error messages are sent only if allowed in the configuration.
208 * Releases the packet on errors.
209 * @param[in] type The message type.
210 * @param[in] code The message code.
211 * @param[in] packet The message packet to be sent.
212 * @param[in] header The ICMP header.
213 * @param[in] error The error service to be announced. Should be SERVICE_ICMP or zero (0).
214 * @param[in] ttl The time to live.
215 * @param[in] tos The type of service.
216 * @param[in] dont_fragment The value indicating whether the datagram must not be fragmented. Is used as a MTU discovery.
217 * @returns EOK on success.
218 * @returns EPERM if the error message is not allowed.
219 */
220int icmp_send_packet( icmp_type_t type, icmp_code_t code, packet_t packet, icmp_header_ref header, services_t error, ip_ttl_t ttl, ip_tos_t tos, int dont_fragment );
221
222/** Tries to set the pending reply result as the received message type.
223 * If the reply data is not present, the reply timed out and the other fibril
224 * is already awake.
225 * Releases the packet.
226 * @param[in] packet The received reply message.
227 * @param[in] header The ICMP message header.
228 * @param[in] type The received reply message type.
229 * @param[in] code The received reply message code.
230 * @returns EOK.
231 */
232int icmp_process_echo_reply( packet_t packet, icmp_header_ref header, icmp_type_t type, icmp_code_t code );
233
234/** Assigns a new identifier for the connection.
235 * Fills the echo data parameter with the assigned values.
236 * @param[in,out] echo_data The echo data to be bound.
237 * @returns Index of the inserted echo data.
238 * @returns EBADMEM if the echo_data parameter is NULL.
239 * @returns ENOTCONN if no free identifier have been found.
240 */
241int icmp_bind_free_id( icmp_echo_ref echo_data );
242
243/** ICMP global data.
244 */
245icmp_globals_t icmp_globals;
246
247INT_MAP_IMPLEMENT( icmp_replies, icmp_reply_t );
248
249INT_MAP_IMPLEMENT( icmp_echo_data, icmp_echo_t );
250
251int icmp_echo_msg( int icmp_phone, size_t size, mseconds_t timeout, ip_ttl_t ttl, ip_tos_t tos, int dont_fragment, const struct sockaddr * addr, socklen_t addrlen ){
252 icmp_echo_ref echo_data;
253 int res;
254
255 fibril_rwlock_write_lock( & icmp_globals.lock );
256 // use the phone as the echo data index
257 echo_data = icmp_echo_data_find( & icmp_globals.echo_data, icmp_phone );
258 if( ! echo_data ){
259 res = ENOENT;
260 }else{
261 res = icmp_echo( echo_data->identifier, echo_data->sequence_number, size, timeout, ttl, tos, dont_fragment, addr, addrlen );
262 if( echo_data->sequence_number < MAX_UINT16 ){
263 ++ echo_data->sequence_number;
264 }else{
265 echo_data->sequence_number = 0;
266 }
267 }
268 fibril_rwlock_write_unlock( & icmp_globals.lock );
269 return res;
270}
271
272int icmp_echo( icmp_param_t id, icmp_param_t sequence, size_t size, mseconds_t timeout, ip_ttl_t ttl, ip_tos_t tos, int dont_fragment, const struct sockaddr * addr, socklen_t addrlen ){
273 ERROR_DECLARE;
274
275 icmp_header_ref header;
276 packet_t packet;
277 size_t length;
278 uint8_t * data;
279 icmp_reply_ref reply;
280 int reply_key;
281 int result;
282 int index;
283
284 if( addrlen <= 0 ){
285 return EINVAL;
286 }
287 length = ( size_t ) addrlen;
288 // TODO do not ask all the time
289 ERROR_PROPAGATE( ip_packet_size_req( icmp_globals.ip_phone, -1, & icmp_globals.packet_dimension ));
290 packet = packet_get_4( icmp_globals.net_phone, size, icmp_globals.packet_dimension.addr_len, ICMP_HEADER_SIZE + icmp_globals.packet_dimension.prefix, icmp_globals.packet_dimension.suffix );
291 if( ! packet ) return ENOMEM;
292
293 // prepare the requesting packet
294 // set the destination address
295 if( ERROR_OCCURRED( packet_set_addr( packet, NULL, ( const uint8_t * ) addr, length ))){
296 return icmp_release_and_return( packet, ERROR_CODE );
297 }
298 // allocate space in the packet
299 data = ( uint8_t * ) packet_suffix( packet, size );
300 if( ! data ){
301 return icmp_release_and_return( packet, ENOMEM );
302 }
303 // fill the data
304 length = 0;
305 while( size > length + sizeof( ICMP_ECHO_TEXT )){
306 memcpy( data + length, ICMP_ECHO_TEXT, sizeof( ICMP_ECHO_TEXT ));
307 length += sizeof( ICMP_ECHO_TEXT );
308 }
309 memcpy( data + length, ICMP_ECHO_TEXT, size - length );
310 // prefix the header
311 header = PACKET_PREFIX( packet, icmp_header_t );
312 if( ! header ){
313 return icmp_release_and_return( packet, ENOMEM );
314 }
315 bzero( header, sizeof( * header ));
316 header->un.echo.identifier = id;
317 header->un.echo.sequence_number = sequence;
318
319 // prepare the reply structure
320 reply = malloc( sizeof( * reply ));
321 if( ! reply ){
322 return icmp_release_and_return( packet, ENOMEM );
323 }
324 fibril_mutex_initialize( & reply->mutex );
325 fibril_mutex_lock( & reply->mutex );
326 fibril_condvar_initialize( & reply->condvar );
327 reply_key = ICMP_GET_REPLY_KEY( header->un.echo.identifier, header->un.echo.sequence_number );
328 index = icmp_replies_add( & icmp_globals.replies, reply_key, reply );
329 if( index < 0 ){
330 free( reply );
331 return icmp_release_and_return( packet, index );
332 }
333
334 // unlock the globals so that we can wait for the reply
335 fibril_rwlock_write_unlock( & icmp_globals.lock );
336
337 // send the request
338 icmp_send_packet( ICMP_ECHO, 0, packet, header, 0, ttl, tos, dont_fragment );
339
340 // wait for the reply
341 // timeout in microseconds
342 if( ERROR_OCCURRED( fibril_condvar_wait_timeout( & reply->condvar, & reply->mutex, timeout * 1000 ))){
343 result = ERROR_CODE;
344 }else{
345 // read the result
346 result = reply->result;
347 }
348
349 // drop the reply mutex before locking the globals again
350 fibril_mutex_unlock( & reply->mutex );
351 fibril_rwlock_write_lock( & icmp_globals.lock );
352
353 // destroy the reply structure
354 icmp_replies_exclude_index( & icmp_globals.replies, index );
355 return result;
356}
357
358int icmp_destination_unreachable_msg( int icmp_phone, icmp_code_t code, icmp_param_t mtu, packet_t packet ){
359 icmp_header_ref header;
360
361 header = icmp_prepare_packet( packet );
362 if( ! header ){
363 return icmp_release_and_return( packet, ENOMEM );
364 }
365 if( mtu ){
366 header->un.frag.mtu = mtu;
367 }
368 return icmp_send_packet( ICMP_DEST_UNREACH, code, packet, header, SERVICE_ICMP, 0, 0, 0 );
369}
370
371int icmp_source_quench_msg( int icmp_phone, packet_t packet ){
372 icmp_header_ref header;
373
374 header = icmp_prepare_packet( packet );
375 if( ! header ){
376 return icmp_release_and_return( packet, ENOMEM );
377 }
378 return icmp_send_packet( ICMP_SOURCE_QUENCH, 0, packet, header, SERVICE_ICMP, 0, 0, 0 );
379}
380
381int icmp_time_exceeded_msg( int icmp_phone, icmp_code_t code, packet_t packet ){
382 icmp_header_ref header;
383
384 header = icmp_prepare_packet( packet );
385 if( ! header ){
386 return icmp_release_and_return( packet, ENOMEM );
387 }
388 return icmp_send_packet( ICMP_TIME_EXCEEDED, code, packet, header, SERVICE_ICMP, 0, 0, 0 );
389}
390
391int icmp_parameter_problem_msg( int icmp_phone, icmp_code_t code, icmp_param_t pointer, packet_t packet ){
392 icmp_header_ref header;
393
394 header = icmp_prepare_packet( packet );
395 if( ! header ){
396 return icmp_release_and_return( packet, ENOMEM );
397 }
398 header->un.param.pointer = pointer;
399 return icmp_send_packet( ICMP_PARAMETERPROB, code, packet, header, SERVICE_ICMP, 0, 0, 0 );
400}
401
402icmp_header_ref icmp_prepare_packet( packet_t packet ){
403 icmp_header_ref header;
404 size_t header_length;
405 size_t total_length;
406
407 total_length = packet_get_data_length( packet );
408 if( total_length <= 0 ) return NULL;
409 header_length = ip_client_header_length( packet );
410 if( header_length <= 0 ) return NULL;
411 // truncate if longer than 64 bits (without the IP header)
412 if(( total_length > header_length + ICMP_KEEP_LENGTH )
413 && ( packet_trim( packet, 0, total_length - header_length - ICMP_KEEP_LENGTH ) != EOK )){
414 return NULL;
415 }
416 header = PACKET_PREFIX( packet, icmp_header_t );
417 if( ! header ) return NULL;
418 bzero( header, sizeof( * header ));
419 return header;
420}
421
422int icmp_send_packet( icmp_type_t type, icmp_code_t code, packet_t packet, icmp_header_ref header, services_t error, ip_ttl_t ttl, ip_tos_t tos, int dont_fragment ){
423 ERROR_DECLARE;
424
425 // do not send an error if disabled
426 if( error && ( ! icmp_globals.error_reporting )){
427 return icmp_release_and_return( packet, EPERM );
428 }
429 header->type = type;
430 header->code = code;
431 header->checksum = 0;
432 header->checksum = ICMP_CHECKSUM( header, packet_get_data_length( packet ));
433 if( ERROR_OCCURRED( ip_client_prepare_packet( packet, IPPROTO_ICMP, ttl, tos, dont_fragment, 0 ))){
434 return icmp_release_and_return( packet, ERROR_CODE );
435 }
436 return ip_send_msg( icmp_globals.ip_phone, -1, packet, SERVICE_ICMP, error );
437}
438
439int icmp_connect_module( services_t service, suseconds_t timeout ){
440 icmp_echo_ref echo_data;
441 icmp_param_t id;
442 int index;
443
444 echo_data = ( icmp_echo_ref ) malloc( sizeof( * echo_data ));
445 if( ! echo_data ) return ENOMEM;
446 // assign a new identifier
447 fibril_rwlock_write_lock( & icmp_globals.lock );
448 index = icmp_bind_free_id( echo_data );
449 if( index < 0 ){
450 free( echo_data );
451 fibril_rwlock_write_unlock( & icmp_globals.lock );
452 return index;
453 }else{
454 id = echo_data->identifier;
455 fibril_rwlock_write_unlock( & icmp_globals.lock );
456 // return the echo data identifier as the ICMP phone
457 return id;
458 }
459}
460
461int icmp_initialize( async_client_conn_t client_connection ){
462 ERROR_DECLARE;
463
464 measured_string_t names[] = {{ str_dup("ICMP_ERROR_REPORTING"), 20 }, { str_dup("ICMP_ECHO_REPLYING"), 18 }};
465 measured_string_ref configuration;
466 size_t count = sizeof( names ) / sizeof( measured_string_t );
467 char * data;
468
469 fibril_rwlock_initialize( & icmp_globals.lock );
470 fibril_rwlock_write_lock( & icmp_globals.lock );
471 icmp_replies_initialize( & icmp_globals.replies );
472 icmp_echo_data_initialize( & icmp_globals.echo_data );
473 icmp_globals.ip_phone = ip_bind_service( SERVICE_IP, IPPROTO_ICMP, SERVICE_ICMP, client_connection, icmp_received_msg );
474 if( icmp_globals.ip_phone < 0 ){
475 return icmp_globals.ip_phone;
476 }
477 ERROR_PROPAGATE( ip_packet_size_req( icmp_globals.ip_phone, -1, & icmp_globals.packet_dimension ));
478 icmp_globals.packet_dimension.prefix += ICMP_HEADER_SIZE;
479 icmp_globals.packet_dimension.content -= ICMP_HEADER_SIZE;
480 // get configuration
481 icmp_globals.error_reporting = NET_DEFAULT_ICMP_ERROR_REPORTING;
482 icmp_globals.echo_replying = NET_DEFAULT_ICMP_ECHO_REPLYING;
483 configuration = & names[ 0 ];
484 ERROR_PROPAGATE( net_get_conf_req( icmp_globals.net_phone, & configuration, count, & data ));
485 if( configuration ){
486 if( configuration[ 0 ].value ){
487 icmp_globals.error_reporting = ( configuration[ 0 ].value[ 0 ] == 'y' );
488 }
489 if( configuration[ 1 ].value ){
490 icmp_globals.echo_replying = ( configuration[ 1 ].value[ 0 ] == 'y' );
491 }
492 net_free_settings( configuration, data );
493 }
494 fibril_rwlock_write_unlock( & icmp_globals.lock );
495 return EOK;
496}
497
498int icmp_received_msg( device_id_t device_id, packet_t packet, services_t receiver, services_t error ){
499 ERROR_DECLARE;
500
501 if( ERROR_OCCURRED( icmp_process_packet( packet, error ))){
502 return icmp_release_and_return( packet, ERROR_CODE );
503 }
504
505 return EOK;
506}
507
508int icmp_process_packet( packet_t packet, services_t error ){
509 ERROR_DECLARE;
510
511 size_t length;
512 uint8_t * src;
513 int addrlen;
514 int result;
515 void * data;
516 icmp_header_ref header;
517 icmp_type_t type;
518 icmp_code_t code;
519
520 if( error ){
521 switch( error ){
522 case SERVICE_ICMP:
523 // process error
524 result = icmp_client_process_packet( packet, & type, & code, NULL, NULL );
525 if( result < 0 ) return result;
526 length = ( size_t ) result;
527 // remove the error header
528 ERROR_PROPAGATE( packet_trim( packet, length, 0 ));
529 break;
530 default:
531 return ENOTSUP;
532 }
533 }
534 // get rid of the ip header
535 length = ip_client_header_length( packet );
536 ERROR_PROPAGATE( packet_trim( packet, length, 0 ));
537
538 length = packet_get_data_length( packet );
539 if( length <= 0 ) return EINVAL;
540 if( length < ICMP_HEADER_SIZE) return EINVAL;
541 data = packet_get_data( packet );
542 if( ! data ) return EINVAL;
543 // get icmp header
544 header = ( icmp_header_ref ) data;
545 // checksum
546 if( header->checksum ){
547 while( ICMP_CHECKSUM( header, length ) != IP_CHECKSUM_ZERO ){
548 // set the original message type on error notification
549 // type swap observed in Qemu
550 if( error ){
551 switch( header->type ){
552 case ICMP_ECHOREPLY:
553 header->type = ICMP_ECHO;
554 continue;
555 }
556 }
557 return EINVAL;
558 }
559 }
560 switch( header->type ){
561 case ICMP_ECHOREPLY:
562 if( error ){
563 return icmp_process_echo_reply( packet, header, type, code );
564 }else{
565 return icmp_process_echo_reply( packet, header, ICMP_ECHO, 0 );
566 }
567 case ICMP_ECHO:
568 if( error ){
569 return icmp_process_echo_reply( packet, header, type, code );
570 // do not send a reply if disabled
571 }else if( icmp_globals.echo_replying ){
572 addrlen = packet_get_addr( packet, & src, NULL );
573 if(( addrlen > 0 )
574 // set both addresses to the source one (avoids the source address deletion before setting the destination one)
575 && ( packet_set_addr( packet, src, src, ( size_t ) addrlen ) == EOK )){
576 // send the reply
577 icmp_send_packet( ICMP_ECHOREPLY, 0, packet, header, 0, 0, 0, 0 );
578 return EOK;
579 }else{
580 return EINVAL;
581 }
582 }else{
583 return EPERM;
584 }
585 case ICMP_DEST_UNREACH:
586 case ICMP_SOURCE_QUENCH:
587 case ICMP_REDIRECT:
588 case ICMP_ALTERNATE_ADDR:
589 case ICMP_ROUTER_ADV:
590 case ICMP_ROUTER_SOL:
591 case ICMP_TIME_EXCEEDED:
592 case ICMP_PARAMETERPROB:
593 case ICMP_CONVERSION_ERROR:
594 case ICMP_REDIRECT_MOBILE:
595 case ICMP_SKIP:
596 case ICMP_PHOTURIS:
597 ip_received_error_msg( icmp_globals.ip_phone, -1, packet, SERVICE_IP, SERVICE_ICMP );
598 return EOK;
599 default:
600 return ENOTSUP;
601 }
602}
603
604int icmp_process_echo_reply( packet_t packet, icmp_header_ref header, icmp_type_t type, icmp_code_t code ){
605 int reply_key;
606 icmp_reply_ref reply;
607
608 // compute the reply key
609 reply_key = ICMP_GET_REPLY_KEY( header->un.echo.identifier, header->un.echo.sequence_number );
610 pq_release( icmp_globals.net_phone, packet_get_id( packet ));
611 // lock the globals
612 fibril_rwlock_write_lock( & icmp_globals.lock );
613 // find the pending reply
614 reply = icmp_replies_find( & icmp_globals.replies, reply_key );
615 if( reply ){
616 // set the result
617 reply->result = type;
618 // notify the waiting fibril
619 fibril_condvar_signal( & reply->condvar );
620 }
621 fibril_rwlock_write_unlock( & icmp_globals.lock );
622 return EOK;
623}
624
625int icmp_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
626 ERROR_DECLARE;
627
628 packet_t packet;
629
630 * answer_count = 0;
631 switch( IPC_GET_METHOD( * call )){
632 case NET_TL_RECEIVED:
633 if( ! ERROR_OCCURRED( packet_translate( icmp_globals.net_phone, & packet, IPC_GET_PACKET( call )))){
634 ERROR_CODE = icmp_received_msg( IPC_GET_DEVICE( call ), packet, SERVICE_ICMP, IPC_GET_ERROR( call ));
635 }
636 return ERROR_CODE;
637 case NET_ICMP_INIT:
638 return icmp_process_client_messages( callid, * call );
639 default:
640 return icmp_process_message( call );
641 }
642 return ENOTSUP;
643}
644
645int icmp_process_client_messages( ipc_callid_t callid, ipc_call_t call ){
646 ERROR_DECLARE;
647
648 bool keep_on_going = true;
649// fibril_rwlock_t lock;
650 ipc_call_t answer;
651 int answer_count;
652 size_t length;
653 struct sockaddr * addr;
654 ipc_callid_t data_callid;
655 icmp_echo_ref echo_data;
656
657 /*
658 * Accept the connection
659 * - Answer the first NET_ICMP_INIT call.
660 */
661 ipc_answer_0( callid, EOK );
662
663// fibril_rwlock_initialize( & lock );
664
665 echo_data = ( icmp_echo_ref ) malloc( sizeof( * echo_data ));
666 if( ! echo_data ) return ENOMEM;
667 // assign a new identifier
668 fibril_rwlock_write_lock( & icmp_globals.lock );
669 ERROR_CODE = icmp_bind_free_id( echo_data );
670 fibril_rwlock_write_unlock( & icmp_globals.lock );
671 if( ERROR_CODE < 0 ){
672 free( echo_data );
673 return ERROR_CODE;
674 }
675
676 while( keep_on_going ){
677 refresh_answer( & answer, & answer_count );
678
679 callid = async_get_call( & call );
680
681 switch( IPC_GET_METHOD( call )){
682 case IPC_M_PHONE_HUNGUP:
683 keep_on_going = false;
684 ERROR_CODE = EOK;
685 break;
686 case NET_ICMP_ECHO:
687// fibril_rwlock_write_lock( & lock );
688 if( ! async_data_write_receive( & data_callid, & length )){
689 ERROR_CODE = EINVAL;
690 }else{
691 addr = malloc( length );
692 if( ! addr ){
693 ERROR_CODE = ENOMEM;
694 }else{
695 if( ! ERROR_OCCURRED( async_data_write_finalize( data_callid, addr, length ))){
696 fibril_rwlock_write_lock( & icmp_globals.lock );
697 ERROR_CODE = icmp_echo( echo_data->identifier, echo_data->sequence_number, ICMP_GET_SIZE( call ), ICMP_GET_TIMEOUT( call ), ICMP_GET_TTL( call ), ICMP_GET_TOS( call ), ICMP_GET_DONT_FRAGMENT( call ), addr, ( socklen_t ) length );
698 fibril_rwlock_write_unlock( & icmp_globals.lock );
699 free( addr );
700 if( echo_data->sequence_number < MAX_UINT16 ){
701 ++ echo_data->sequence_number;
702 }else{
703 echo_data->sequence_number = 0;
704 }
705 }
706 }
707 }
708// fibril_rwlock_write_unlock( & lock );
709 break;
710 default:
711 ERROR_CODE = icmp_process_message( & call );
712 }
713
714 answer_call( callid, ERROR_CODE, & answer, answer_count );
715 }
716
717 // release the identifier
718 fibril_rwlock_write_lock( & icmp_globals.lock );
719 icmp_echo_data_exclude( & icmp_globals.echo_data, echo_data->identifier );
720 fibril_rwlock_write_unlock( & icmp_globals.lock );
721 return EOK;
722}
723
724int icmp_process_message( ipc_call_t * call ){
725 ERROR_DECLARE;
726
727 packet_t packet;
728
729 switch( IPC_GET_METHOD( * call )){
730 case NET_ICMP_DEST_UNREACH:
731 if( ! ERROR_OCCURRED( packet_translate( icmp_globals.net_phone, & packet, IPC_GET_PACKET( call )))){
732 ERROR_CODE = icmp_destination_unreachable_msg( 0, ICMP_GET_CODE( call ), ICMP_GET_MTU( call ), packet );
733 }
734 return ERROR_CODE;
735 case NET_ICMP_SOURCE_QUENCH:
736 if( ! ERROR_OCCURRED( packet_translate( icmp_globals.net_phone, & packet, IPC_GET_PACKET( call )))){
737 ERROR_CODE = icmp_source_quench_msg( 0, packet );
738 }
739 return ERROR_CODE;
740 case NET_ICMP_TIME_EXCEEDED:
741 if( ! ERROR_OCCURRED( packet_translate( icmp_globals.net_phone, & packet, IPC_GET_PACKET( call )))){
742 ERROR_CODE = icmp_time_exceeded_msg( 0, ICMP_GET_CODE( call ), packet );
743 }
744 return ERROR_CODE;
745 case NET_ICMP_PARAMETERPROB:
746 if( ! ERROR_OCCURRED( packet_translate( icmp_globals.net_phone, & packet, IPC_GET_PACKET( call )))){
747 ERROR_CODE = icmp_parameter_problem_msg( 0, ICMP_GET_CODE( call ), ICMP_GET_POINTER( call ), packet );
748 }
749 return ERROR_CODE;
750 default:
751 return ENOTSUP;
752 }
753}
754
755int icmp_release_and_return( packet_t packet, int result ){
756 pq_release( icmp_globals.net_phone, packet_get_id( packet ));
757 return result;
758}
759
760int icmp_bind_free_id( icmp_echo_ref echo_data ){
761 icmp_param_t index;
762
763 if( ! echo_data ) return EBADMEM;
764 // from the last used one
765 index = icmp_globals.last_used_id;
766 do{
767 ++ index;
768 // til the range end
769 if( index >= ICMP_FREE_IDS_END ){
770 // start from the range beginning
771 index = ICMP_FREE_IDS_START - 1;
772 do{
773 ++ index;
774 // til the last used one
775 if( index >= icmp_globals.last_used_id ){
776 // none found
777 return ENOTCONN;
778 }
779 }while( icmp_echo_data_find( & icmp_globals.echo_data, index ) != NULL );
780 // found, break immediately
781 break;
782 }
783 }while( icmp_echo_data_find( & icmp_globals.echo_data, index ) != NULL );
784 echo_data->identifier = index;
785 echo_data->sequence_number = 0;
786 return icmp_echo_data_add( & icmp_globals.echo_data, index, echo_data );
787}
788
789/** @}
790 */
Note: See TracBrowser for help on using the repository browser.