Subversion Repositories HelenOS

Rev

Rev 4728 | Rev 4731 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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