Subversion Repositories HelenOS

Rev

Rev 4192 | Rev 4261 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4192 Rev 4243
Line 46... Line 46...
46
 
46
 
47
#include "../../err.h"
47
#include "../../err.h"
48
#include "../../messages.h"
48
#include "../../messages.h"
49
#include "../../modules.h"
49
#include "../../modules.h"
50
 
50
 
-
 
51
#include "../../include/byteorder.h"
-
 
52
#include "../../include/device.h"
51
#include "../../include/protocol_map.h"
53
#include "../../include/protocol_map.h"
52
#include "../../netif/device.h"
-
 
53
 
54
 
54
#include "../../structures/measured_strings.h"
55
#include "../../structures/measured_strings.h"
55
#include "../../structures/packet/packet.h"
56
#include "../../structures/packet/packet.h"
56
#include "../../structures/packet/packet_client.h"
57
#include "../../structures/packet/packet_client.h"
57
 
58
 
Line 221... Line 222...
221
                return result;
222
                return result;
222
            }
223
            }
223
        }
224
        }
224
        printf( "\nCache of the existing device %d cleaned", device->device_id );
225
        printf( "\nCache of the existing device %d cleaned", device->device_id );
225
    }else{
226
    }else{
226
        index = protocol_map( service, protocol );
227
        index = hardware_map( service );
227
        if( ! index ) return ENOENT;
228
        if( ! index ) return ENOENT;
228
        // create a new device
229
        // create a new device
229
        device = ( arp_device_ref ) malloc( sizeof( arp_device_t ));
230
        device = ( arp_device_ref ) malloc( sizeof( arp_device_t ));
230
        if( ! device ){
231
        if( ! device ){
231
            rwlock_write_unlock( & arp_globals.lock );
232
            rwlock_write_unlock( & arp_globals.lock );
Line 317... Line 318...
317
    size_t              length;
318
    size_t              length;
318
    packet_t            packet;
319
    packet_t            packet;
319
    arp_header_ref      header;
320
    arp_header_ref      header;
320
 
321
 
321
    if( ! target ) return NULL;
322
    if( ! target ) return NULL;
322
    rwlock_read_lock( & arp_globals.lock );
-
 
323
    device = arp_cache_find( & arp_globals.cache, device_id );
323
    device = arp_cache_find( & arp_globals.cache, device_id );
324
    if( ! device ){
324
    if( ! device ) return NULL;
325
        rwlock_read_unlock( & arp_globals.lock );
-
 
326
        return NULL;
-
 
327
    }
-
 
328
    proto = arp_protos_find( & device->protos, protocol );
325
    proto = arp_protos_find( & device->protos, protocol );
329
    if(( ! proto ) || ( proto->addr->length != target->length )){
326
    if(( ! proto ) || ( proto->addr->length != target->length )) return NULL;
330
        rwlock_read_unlock( & arp_globals.lock );
-
 
331
        return NULL;
-
 
332
    }
-
 
333
    addr = arp_addr_find( & proto->addresses, target->value, target->length );
327
    addr = arp_addr_find( & proto->addresses, target->value, target->length );
334
    if( addr ){
-
 
335
        rwlock_read_unlock( & arp_globals.lock );
-
 
336
        return addr;
328
    if( addr ) return addr;
337
    }
-
 
338
    // ARP packet content size = header + ( address + translation ) * 2
329
    // ARP packet content size = header + ( address + translation ) * 2
339
    length = 8 + ( CONVERT_SIZE( char, uint8_t, proto->addr->length ) + CONVERT_SIZE( char, uint8_t, device->addr->length )) * 2;
330
    length = 8 + ( CONVERT_SIZE( char, uint8_t, proto->addr->length ) + CONVERT_SIZE( char, uint8_t, device->addr->length )) * 2;
340
    if( length > device->content ){
331
    if( length > device->content ) return NULL;
341
        rwlock_read_unlock( & arp_globals.lock );
-
 
342
        return NULL;
-
 
343
    }
-
 
344
    packet = packet_get_4( arp_globals.networking_phone, device->addr_len, device->prefix, length, device->suffix );
332
    packet = packet_get_4( arp_globals.networking_phone, device->addr_len, device->prefix, length, device->suffix );
345
    if( ! packet ){
333
    if( ! packet ) return NULL;
346
        rwlock_read_unlock( & arp_globals.lock );
-
 
347
        return NULL;
-
 
348
    }
-
 
349
    header = ( arp_header_ref ) packet_suffix( packet, length );
334
    header = ( arp_header_ref ) packet_suffix( packet, length );
350
    header->hardware = device->hardware;
335
    header->hardware = htons( device->hardware );
351
    header->hardware_length = device->addr->length;
336
    header->hardware_length = device->addr->length;
352
    header->protocol = protocol_map( device->service, protocol );
337
    header->protocol = htons( protocol_map( device->service, protocol ));
353
    header->protocol_length = proto->addr->length;
338
    header->protocol_length = proto->addr->length;
354
    header->operation = ARPOP_REQUEST;
339
    header->operation = htons( ARPOP_REQUEST );
355
    length = sizeof( arp_header_t );
340
    length = sizeof( arp_header_t );
356
    memcpy((( uint8_t * ) header ) + length, device->addr->value, device->addr->length );
341
    memcpy((( uint8_t * ) header ) + length, device->addr->value, device->addr->length );
357
    length += device->addr->length;
342
    length += device->addr->length;
358
    memcpy((( uint8_t * ) header ) + length, proto->addr->value, proto->addr->length );
343
    memcpy((( uint8_t * ) header ) + length, proto->addr->value, proto->addr->length );
359
    length += proto->addr->length;
344
    length += proto->addr->length;
360
    bzero((( uint8_t * ) header ) + length, device->addr->length );
345
    bzero((( uint8_t * ) header ) + length, device->addr->length );
361
    length += device->addr->length;
346
    length += device->addr->length;
362
    memcpy((( uint8_t * ) header ) + length, target->value, target->length );
347
    memcpy((( uint8_t * ) header ) + length, target->value, target->length );
363
    packet_set_addr( packet, ( uint8_t * ) device->addr->value, ( uint8_t * ) device->broadcast_addr->value, CONVERT_SIZE( char, uint8_t, device->addr->length ));
348
    packet_set_addr( packet, ( uint8_t * ) device->addr->value, ( uint8_t * ) device->broadcast_addr->value, CONVERT_SIZE( char, uint8_t, device->addr->length ));
364
    async_msg_3( device->phone, NET_NETIF_SEND, device_id, SERVICE_ARP, packet_get_id( packet ));
349
    async_msg_3( device->phone, NET_NIL_SEND, device_id, packet_get_id( packet ), SERVICE_ARP );
365
    rwlock_read_unlock( & arp_globals.lock );
-
 
366
    return NULL;
350
    return NULL;
367
}
351
}
368
 
352
 
369
int arp_receive_message( device_id_t device_id, packet_t packet ){
353
int arp_receive_message( device_id_t device_id, packet_t packet ){
370
    ERROR_DECLARE;
354
    ERROR_DECLARE;
Line 379... Line 363...
379
    uint8_t *           des_hw;
363
    uint8_t *           des_hw;
380
    uint8_t *           des_proto;
364
    uint8_t *           des_proto;
381
 
365
 
382
    length = packet_get_data_length( packet );
366
    length = packet_get_data_length( packet );
383
    if( length <= sizeof( arp_header_t )) return EINVAL;
367
    if( length <= sizeof( arp_header_t )) return EINVAL;
384
    rwlock_read_lock( & arp_globals.lock );
-
 
385
    device = arp_cache_find( & arp_globals.cache, device_id );
368
    device = arp_cache_find( & arp_globals.cache, device_id );
386
    if( ! device ){
-
 
387
        rwlock_read_unlock( & arp_globals.lock );
-
 
388
        return ENOENT;
369
    if( ! device ) return ENOENT;
389
    }
-
 
390
    header = ( arp_header_ref ) packet_get_data( packet );
370
    header = ( arp_header_ref ) packet_get_data( packet );
391
    if(( header->hardware != device->hardware )
371
    if(( ntohs( header->hardware ) != device->hardware )
392
    || ( length < sizeof( arp_header_t ) + ( header->hardware_length + header->protocol_length ) * 2 )){
372
    || ( length < sizeof( arp_header_t ) + ( header->hardware_length + header->protocol_length ) * 2 )){
393
        rwlock_read_unlock( & arp_globals.lock );
-
 
394
        return EINVAL;
373
        return EINVAL;
395
    }
374
    }
396
    proto = arp_protos_find( & device->protos, protocol_unmap( device->service, header->protocol ));
375
    proto = arp_protos_find( & device->protos, protocol_unmap( device->service, ntohs( header->protocol )));
397
    if( ! proto ){
-
 
398
        rwlock_read_unlock( & arp_globals.lock );
-
 
399
        return ENOENT;
376
    if( ! proto ) return ENOENT;
400
    }
-
 
401
    src_hw = (( uint8_t * ) header ) + sizeof( arp_header_t );
377
    src_hw = (( uint8_t * ) header ) + sizeof( arp_header_t );
402
    src_proto = src_hw + header->hardware_length;
378
    src_proto = src_hw + header->hardware_length;
403
    des_hw = src_proto + header->protocol_length;
379
    des_hw = src_proto + header->protocol_length;
404
    des_proto = des_hw + header->hardware_length;
380
    des_proto = des_hw + header->hardware_length;
405
    hw_source = arp_addr_find( & proto->addresses, ( char * ) src_proto, CONVERT_SIZE( uint8_t, char, header->protocol_length ));
381
    hw_source = arp_addr_find( & proto->addresses, ( char * ) src_proto, CONVERT_SIZE( uint8_t, char, header->protocol_length ));
406
    // exists?
382
    // exists?
407
    if( hw_source ){
383
    if( hw_source ){
408
        if( hw_source->length != CONVERT_SIZE( uint8_t, char, header->hardware_length )){
384
        if( hw_source->length != CONVERT_SIZE( uint8_t, char, header->hardware_length )){
409
            rwlock_read_unlock( & arp_globals.lock );
-
 
410
            return EINVAL;
385
            return EINVAL;
411
        }
386
        }
412
        memcpy( hw_source->value, src_hw, hw_source->length );
387
        memcpy( hw_source->value, src_hw, hw_source->length );
413
    }
388
    }
414
    // is my protocol address?
389
    // is my protocol address?
415
    if( proto->addr->length != CONVERT_SIZE( uint8_t, char, header->hardware_length )){
390
    if( proto->addr->length != CONVERT_SIZE( uint8_t, char, header->hardware_length )){
416
        rwlock_read_unlock( & arp_globals.lock );
-
 
417
        return EINVAL;
391
        return EINVAL;
418
    }
392
    }
419
    if( ! strncmp( proto->addr->value, ( char * ) des_proto, proto->addr->length )){
393
    if( ! strncmp( proto->addr->value, ( char * ) des_proto, proto->addr->length )){
420
        // not already upadted?
394
        // not already upadted?
421
        if( ! hw_source ){
395
        if( ! hw_source ){
422
            hw_source = measured_string_create_bulk(( char * ) src_hw, CONVERT_SIZE( uint8_t, char, header->hardware_length ));
396
            hw_source = measured_string_create_bulk(( char * ) src_hw, CONVERT_SIZE( uint8_t, char, header->hardware_length ));
423
            if( ! hw_source ){
397
            if( ! hw_source ) return ENOMEM;
424
                rwlock_read_unlock( & arp_globals.lock );
-
 
425
                return ENOMEM;
-
 
426
            }
-
 
427
            if( ERROR_OCCURRED( arp_addr_add( & proto->addresses, ( char * ) src_proto, CONVERT_SIZE( uint8_t, char, header->protocol_length ), hw_source ))){
398
            ERROR_PROPAGATE( arp_addr_add( & proto->addresses, ( char * ) src_proto, CONVERT_SIZE( uint8_t, char, header->protocol_length ), hw_source ));
428
                rwlock_read_unlock( & arp_globals.lock );
-
 
429
                return ERROR_CODE;
-
 
430
            }
-
 
431
        }
399
        }
432
        if( header->operation == ARPOP_REQUEST ){
400
        if( ntohs( header->operation ) == ARPOP_REQUEST ){
433
            header->operation = ARPOP_REPLY;
401
            header->operation = htons( ARPOP_REPLY );
434
            memcpy( des_proto, src_proto, header->protocol_length );
402
            memcpy( des_proto, src_proto, header->protocol_length );
435
            memcpy( src_proto, proto->addr->value, header->protocol_length );
403
            memcpy( src_proto, proto->addr->value, header->protocol_length );
436
            memcpy( src_hw, des_hw, header->hardware_length );
404
            memcpy( src_hw, des_hw, header->hardware_length );
437
            memcpy( des_hw, hw_source->value, header->hardware_length );
405
            memcpy( des_hw, hw_source->value, header->hardware_length );
438
            packet_set_addr( packet, src_hw, des_hw, header->hardware_length );
406
            packet_set_addr( packet, src_hw, des_hw, header->hardware_length );
439
            async_msg_3( device->phone, NET_NETIF_SEND, device_id, SERVICE_ARP, packet_get_id( packet ));
407
            async_msg_3( device->phone, NET_NIL_SEND, device_id, packet_get_id( packet ), SERVICE_ARP );
440
            rwlock_read_unlock( & arp_globals.lock );
-
 
441
        }else{
408
        }else{
442
            rwlock_read_unlock( & arp_globals.lock );
-
 
443
            pq_release( arp_globals.networking_phone, packet_get_id( packet ));
409
            pq_release( arp_globals.networking_phone, packet_get_id( packet ));
444
        }
410
        }
445
    }
411
    }
446
    return EOK;
412
    return EOK;
447
}
413
}
Line 500... Line 466...
500
 
466
 
501
    measured_string_ref address;
467
    measured_string_ref address;
502
    measured_string_ref translation;
468
    measured_string_ref translation;
503
    char *              data;
469
    char *              data;
504
 
470
 
-
 
471
//  printf( "\nmessage %d - %d", IPC_GET_METHOD( * call ), NET_ARP_FIRST );
505
    * answer_count = 0;
472
    * answer_count = 0;
506
    switch( IPC_GET_METHOD( * call )){
473
    switch( IPC_GET_METHOD( * call )){
507
        case IPC_M_PHONE_HUNGUP:
474
        case IPC_M_PHONE_HUNGUP:
508
            return EOK;
475
            return EOK;
509
        case NET_ARP_DEVICE:
476
        case NET_ARP_DEVICE:
Line 513... Line 480...
513
                free( data );
480
                free( data );
514
            }
481
            }
515
            return ERROR_CODE;
482
            return ERROR_CODE;
516
        case NET_ARP_TRANSLATE:
483
        case NET_ARP_TRANSLATE:
517
            ERROR_PROPAGATE( measured_strings_receive( & address, & data, 1 ));
484
            ERROR_PROPAGATE( measured_strings_receive( & address, & data, 1 ));
-
 
485
            rwlock_read_lock( & arp_globals.lock );
518
            translation = arp_translate_message( IPC_GET_DEVICE( call ), IPC_GET_PROTO( call ), address );
486
            translation = arp_translate_message( IPC_GET_DEVICE( call ), IPC_GET_PROTO( call ), address );
519
            free( address );
487
            free( address );
520
            free( data );
488
            free( data );
521
            if( ! translation ) return ENOENT;
489
            if( ! translation ){
-
 
490
                rwlock_read_unlock( & arp_globals.lock );
-
 
491
                return ENOENT;
-
 
492
            }
522
            return measured_strings_reply( translation, 1 );
493
            ERROR_CODE = measured_strings_reply( translation, 1 );
-
 
494
            rwlock_read_unlock( & arp_globals.lock );
-
 
495
            return ERROR_CODE;
523
        case NET_ARP_CLEAR_DEVICE:
496
        case NET_ARP_CLEAR_DEVICE:
524
            return arp_clear_device_message( IPC_GET_DEVICE( call ));
497
            return arp_clear_device_message( IPC_GET_DEVICE( call ));
525
        case NET_ARP_CLEAN_CACHE:
498
        case NET_ARP_CLEAN_CACHE:
526
            return arp_clean_cache_message();
499
            return arp_clean_cache_message();
527
    }
500
    }
Line 539... Line 512...
539
                // do nothing - keep the cache
512
                // do nothing - keep the cache
540
                ipc_answer_0( iid, EOK );
513
                ipc_answer_0( iid, EOK );
541
                break;
514
                break;
542
            case NET_IL_RECEIVED:
515
            case NET_IL_RECEIVED:
543
                if( ! ERROR_OCCURRED( packet_translate( arp_globals.networking_phone, & packet, IPC_GET_PACKET( icall )))){
516
                if( ! ERROR_OCCURRED( packet_translate( arp_globals.networking_phone, & packet, IPC_GET_PACKET( icall )))){
-
 
517
                    rwlock_read_lock( & arp_globals.lock );
544
                    ERROR_CODE = arp_receive_message( IPC_GET_DEVICE( icall ), packet );
518
                    ERROR_CODE = arp_receive_message( IPC_GET_DEVICE( icall ), packet );
-
 
519
                    rwlock_read_unlock( & arp_globals.lock );
545
                }
520
                }
546
                ipc_answer_0( iid, ERROR_CODE );
521
                ipc_answer_0( iid, ERROR_CODE );
547
                break;
522
                break;
548
            default:
523
            default:
549
                ipc_answer_0( iid, ENOTSUP );
524
                ipc_answer_0( iid, ENOTSUP );