Subversion Repositories HelenOS

Rev

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

Rev 4261 Rev 4307
Line 51... Line 51...
51
#include "../../include/crc.h"
51
#include "../../include/crc.h"
52
#include "../../include/ethernet_lsap.h"
52
#include "../../include/ethernet_lsap.h"
53
#include "../../include/ethernet_protocols.h"
53
#include "../../include/ethernet_protocols.h"
54
#include "../../include/protocol_map.h"
54
#include "../../include/protocol_map.h"
55
#include "../../include/device.h"
55
#include "../../include/device.h"
56
#include "../../include/netif_messages.h"
56
#include "../../include/netif_interface.h"
57
#include "../../include/nil_messages.h"
57
#include "../../include/nil_interface.h"
-
 
58
#include "../../include/il_interface.h"
58
 
59
 
59
#include "../../structures/measured_strings.h"
60
#include "../../structures/measured_strings.h"
60
#include "../../structures/packet/packet_client.h"
61
#include "../../structures/packet/packet_client.h"
61
 
62
 
62
#include "../nil_module.h"
63
#include "../nil_module.h"
63
#include "../nil_wrappers.h"
-
 
64
 
64
 
65
#include "eth.h"
65
#include "eth.h"
66
#include "eth_header.h"
66
#include "eth_header.h"
67
 
67
 
68
#define ETH_PREFIX      ( sizeof( eth_header_t ) + sizeof( eth_header_lsap_t ) + sizeof( eth_header_snap_t ))
68
#define ETH_PREFIX      ( sizeof( eth_header_t ) + sizeof( eth_header_lsap_t ) + sizeof( eth_header_snap_t ))
Line 93... Line 93...
93
DEVICE_MAP_IMPLEMENT( eth_devices, eth_device_t )
93
DEVICE_MAP_IMPLEMENT( eth_devices, eth_device_t )
94
 
94
 
95
INT_MAP_IMPLEMENT( eth_protos, eth_proto_t )
95
INT_MAP_IMPLEMENT( eth_protos, eth_proto_t )
96
 
96
 
97
int eth_device_message( device_id_t device_id, services_t service, size_t mtu );
97
int eth_device_message( device_id_t device_id, services_t service, size_t mtu );
98
int eth_receive_message( device_id_t device_id, packet_t packet );
98
int nil_receive_msg( int nil_phone, device_id_t device_id, packet_t packet );
-
 
99
int nil_register_message( services_t service, int phone );
99
int eth_packet_space_message( device_id_t device_id, size_t * addr_len, size_t * prefix, size_t * content, size_t * suffix );
100
int eth_packet_space_message( device_id_t device_id, size_t * addr_len, size_t * prefix, size_t * content, size_t * suffix );
100
int eth_addr_message( device_id_t device_id, eth_addr_type_t type, measured_string_ref * address );
101
int eth_addr_message( device_id_t device_id, eth_addr_type_t type, measured_string_ref * address );
101
int eth_send_message( device_id_t device_id, packet_t packet, services_t sender );
102
int eth_send_message( device_id_t device_id, packet_t packet, services_t sender );
102
eth_proto_ref   eth_process_packet( int dummy, packet_t packet );
103
eth_proto_ref   eth_process_packet( int dummy, packet_t packet );
103
int eth_prepare_packet( int dummy, packet_t packet, uint8_t * src_addr, int ethertype );
104
int eth_prepare_packet( int dummy, packet_t packet, uint8_t * src_addr, int ethertype );
104
 
105
 
105
void    nil_device_state_wrapper( device_id_t device_id, int state ){
106
int nil_device_state_msg( int nil_phone, device_id_t device_id, int state ){
106
    int             index;
107
    int             index;
107
    eth_proto_ref   proto;
108
    eth_proto_ref   proto;
108
 
109
 
109
    //TODO clear device if off?
110
    //TODO clear device if off?
110
    rwlock_read_lock( & eth_globals.protos_lock );
111
    rwlock_read_lock( & eth_globals.protos_lock );
111
    for( index = eth_protos_count( & eth_globals.protos ) - 1; index >= 0; -- index ){
112
    for( index = eth_protos_count( & eth_globals.protos ) - 1; index >= 0; -- index ){
112
        proto = eth_protos_get_index( & eth_globals.protos, index );
113
        proto = eth_protos_get_index( & eth_globals.protos, index );
113
        if( proto && proto->phone ) async_msg_2( proto->phone, NET_IL_DEVICE_STATE, device_id, state );
114
        if( proto && proto->phone ) il_device_state_msg( proto->phone, device_id, state );
114
    }
115
    }
115
    rwlock_read_unlock( & eth_globals.protos_lock );
116
    rwlock_read_unlock( & eth_globals.protos_lock );
-
 
117
    return EOK;
116
}
118
}
117
 
119
 
118
int nil_receive_wrapper( device_id_t device_id, packet_t packet ){
-
 
119
    return eth_receive_message( device_id, packet );
-
 
120
}
-
 
121
 
-
 
122
int nil_initialize( int networking_phone ){
120
int nil_initialize( int net_phone ){
123
    ERROR_DECLARE;
121
    ERROR_DECLARE;
124
 
122
 
125
    rwlock_initialize( & eth_globals.devices_lock );
123
    rwlock_initialize( & eth_globals.devices_lock );
126
    rwlock_initialize( & eth_globals.protos_lock );
124
    rwlock_initialize( & eth_globals.protos_lock );
127
    rwlock_write_lock( & eth_globals.devices_lock );
125
    rwlock_write_lock( & eth_globals.devices_lock );
128
    rwlock_write_lock( & eth_globals.protos_lock );
126
    rwlock_write_lock( & eth_globals.protos_lock );
129
    eth_globals.networking_phone = networking_phone;
127
    eth_globals.net_phone = net_phone;
130
    eth_globals.broadcast_addr = measured_string_create_bulk( "\xFF\xFF\xFF\xFF\xFF\xFF", CONVERT_SIZE( uint8_t, char, ETH_ADDR ));
128
    eth_globals.broadcast_addr = measured_string_create_bulk( "\xFF\xFF\xFF\xFF\xFF\xFF", CONVERT_SIZE( uint8_t, char, ETH_ADDR ));
131
    if( ! eth_globals.broadcast_addr ) return ENOMEM;
129
    if( ! eth_globals.broadcast_addr ) return ENOMEM;
132
    ERROR_PROPAGATE( eth_devices_initialize( & eth_globals.devices ));
130
    ERROR_PROPAGATE( eth_devices_initialize( & eth_globals.devices ));
133
    if( ERROR_OCCURRED( eth_protos_initialize( & eth_globals.protos ))){
131
    if( ERROR_OCCURRED( eth_protos_initialize( & eth_globals.protos ))){
134
        eth_devices_destroy( & eth_globals.devices );
132
        eth_devices_destroy( & eth_globals.devices );
Line 148... Line 146...
148
    rwlock_write_lock( & eth_globals.devices_lock );
146
    rwlock_write_lock( & eth_globals.devices_lock );
149
    // an existing device?
147
    // an existing device?
150
    device = eth_devices_find( & eth_globals.devices, device_id );
148
    device = eth_devices_find( & eth_globals.devices, device_id );
151
    if( device ){
149
    if( device ){
152
        if( device->service != service ){
150
        if( device->service != service ){
153
            printf( "\nDevice %d already exists", device->device_id );
151
            printf( "Device %d already exists\n", device->device_id );
154
            rwlock_write_unlock( & eth_globals.devices_lock );
152
            rwlock_write_unlock( & eth_globals.devices_lock );
155
            return EEXIST;
153
            return EEXIST;
156
        }else{
154
        }else{
157
            // update mtu
155
            // update mtu
158
            device->mtu = mtu;
156
            device->mtu = mtu;
159
            printf( "\nDevice %d already exists:\tMTU\t= %d", device->device_id, device->mtu );
157
            printf( "Device %d already exists:\tMTU\t= %d\n", device->device_id, device->mtu );
160
        }
158
        }
161
    }else{
159
    }else{
162
        // create a new device
160
        // create a new device
163
        device = ( eth_device_ref ) malloc( sizeof( eth_device_t ));
161
        device = ( eth_device_ref ) malloc( sizeof( eth_device_t ));
164
        if( ! device ) return ENOMEM;
162
        if( ! device ) return ENOMEM;
Line 166... Line 164...
166
        device->service = service;
164
        device->service = service;
167
        device->mtu = (( mtu > 0 ) && ( mtu <= ETH_MAX_TAGGED_CONTENT )) ? mtu : ETH_MAX_TAGGED_CONTENT;
165
        device->mtu = (( mtu > 0 ) && ( mtu <= ETH_MAX_TAGGED_CONTENT )) ? mtu : ETH_MAX_TAGGED_CONTENT;
168
        // TODO get dummy setting
166
        // TODO get dummy setting
169
        device->dummy = 0;
167
        device->dummy = 0;
170
        // bind the device driver
168
        // bind the device driver
171
#if ! NETIF_BUNDLE
-
 
172
        device->phone = bind_service( device->service, device->device_id, SERVICE_ETHERNET, 0, eth_receiver );
169
        device->phone = netif_bind_service( device->service, device->device_id, SERVICE_ETHERNET, eth_receiver );
173
        if( device->phone < 0 ){
170
        if( device->phone < 0 ){
174
            rwlock_write_unlock( & eth_globals.devices_lock );
171
            rwlock_write_unlock( & eth_globals.devices_lock );
175
            free( device );
172
            free( device );
176
            return device->phone;
173
            return device->phone;
177
        }
174
        }
178
#endif
-
 
179
        // get hardware address
175
        // get hardware address
180
        if( ERROR_OCCURRED( netif_get_addr( device->phone, device->device_id, & device->addr, & device->addr_data ))){
176
        if( ERROR_OCCURRED( netif_get_addr( device->phone, device->device_id, & device->addr, & device->addr_data ))){
181
            rwlock_write_unlock( & eth_globals.devices_lock );
177
            rwlock_write_unlock( & eth_globals.devices_lock );
182
            free( device );
178
            free( device );
183
            return ERROR_CODE;
179
            return ERROR_CODE;
Line 189... Line 185...
189
            free( device->addr );
185
            free( device->addr );
190
            free( device->addr_data );
186
            free( device->addr_data );
191
            free( device );
187
            free( device );
192
            return index;
188
            return index;
193
        }
189
        }
194
        printf( "\nNew device registered:\n\tid\t= %d\n\tservice\t= %d\n\tMTU\t= %d\n\taddress\t= %X:%X:%X:%X:%X:%X", device->device_id, device->service, device->mtu, device->addr_data[ 0 ], device->addr_data[ 1 ], device->addr_data[ 2 ], device->addr_data[ 3 ], device->addr_data[ 4 ], device->addr_data[ 5 ] );
190
        printf( "New device registered:\n\tid\t= %d\n\tservice\t= %d\n\tMTU\t= %d\n\taddress\t= %X:%X:%X:%X:%X:%X\n", device->device_id, device->service, device->mtu, device->addr_data[ 0 ], device->addr_data[ 1 ], device->addr_data[ 2 ], device->addr_data[ 3 ], device->addr_data[ 4 ], device->addr_data[ 5 ] );
195
    }
191
    }
196
    rwlock_write_unlock( & eth_globals.devices_lock );
192
    rwlock_write_unlock( & eth_globals.devices_lock );
197
    return EOK;
193
    return EOK;
198
}
194
}
199
 
195
 
Line 252... Line 248...
252
        return NULL;
248
        return NULL;
253
    }
249
    }
254
    return eth_protos_find( & eth_globals.protos, type );
250
    return eth_protos_find( & eth_globals.protos, type );
255
}
251
}
256
 
252
 
257
int eth_receive_message( device_id_t device_id, packet_t packet ){
253
int nil_received_msg( int nil_phone, device_id_t device_id, packet_t packet, services_t target ){
258
    eth_proto_ref   proto;
254
    eth_proto_ref   proto;
259
    packet_t        next;
255
    packet_t        next;
260
    eth_device_ref  device;
256
    eth_device_ref  device;
261
    int             dummy;
257
    int             dummy;
262
 
258
 
Line 271... Line 267...
271
    rwlock_read_lock( & eth_globals.protos_lock );
267
    rwlock_read_lock( & eth_globals.protos_lock );
272
    do{
268
    do{
273
        next = pq_detach( packet );
269
        next = pq_detach( packet );
274
        proto = eth_process_packet( dummy, packet );
270
        proto = eth_process_packet( dummy, packet );
275
        if( proto ){
271
        if( proto ){
276
            async_msg_3( proto->phone, NET_IL_RECEIVED, device_id, packet_get_id( packet ), proto->service );
272
            il_received_msg( proto->phone, device_id, packet, proto->service );
277
        }else{
273
        }else{
278
            // drop invalid/unknown
274
            // drop invalid/unknown
279
            pq_release( eth_globals.networking_phone, packet_get_id( packet ));
275
            pq_release( eth_globals.net_phone, packet_get_id( packet ));
280
        }
276
        }
281
        packet = next;
277
        packet = next;
282
    }while( packet );
278
    }while( packet );
283
    rwlock_read_unlock( & eth_globals.protos_lock );
279
    rwlock_read_unlock( & eth_globals.protos_lock );
284
    return EOK;
280
    return EOK;
Line 348... Line 344...
348
            rwlock_write_unlock( & eth_globals.protos_lock );
344
            rwlock_write_unlock( & eth_globals.protos_lock );
349
            free( proto );
345
            free( proto );
350
            return index;
346
            return index;
351
        }
347
        }
352
    }
348
    }
353
    printf( "\nNew protocol registered:\n\tprotocol\t= 0x%x\n\tservice\t= %d\n\tphone\t= %d", proto->protocol, proto->service, proto->phone );
349
    printf( "New protocol registered:\n\tprotocol\t= 0x%x\n\tservice\t= %d\n\tphone\t= %d\n", proto->protocol, proto->service, proto->phone );
354
    rwlock_write_unlock( & eth_globals.protos_lock );
350
    rwlock_write_unlock( & eth_globals.protos_lock );
355
    return EOK;
351
    return EOK;
356
}
352
}
357
 
353
 
358
int eth_prepare_packet( int dummy, packet_t packet, uint8_t * src_addr, int ethertype ){
354
int eth_prepare_packet( int dummy, packet_t packet, uint8_t * src_addr, int ethertype ){
Line 407... Line 403...
407
    packet_t            tmp;
403
    packet_t            tmp;
408
    int                 ethertype;
404
    int                 ethertype;
409
 
405
 
410
    ethertype = htons( protocol_map( SERVICE_ETHERNET, sender ));
406
    ethertype = htons( protocol_map( SERVICE_ETHERNET, sender ));
411
    if( ! ethertype ){
407
    if( ! ethertype ){
412
        pq_release( eth_globals.networking_phone, packet_get_id( packet ));
408
        pq_release( eth_globals.net_phone, packet_get_id( packet ));
413
        return EINVAL;
409
        return EINVAL;
414
    }
410
    }
415
    rwlock_read_lock( & eth_globals.devices_lock );
411
    rwlock_read_lock( & eth_globals.devices_lock );
416
    device = eth_devices_find( & eth_globals.devices, device_id );
412
    device = eth_devices_find( & eth_globals.devices, device_id );
417
    if( ! device ){
413
    if( ! device ){
Line 422... Line 418...
422
    next = packet;
418
    next = packet;
423
    do{
419
    do{
424
        if( ERROR_OCCURRED( eth_prepare_packet( device->dummy, next, ( uint8_t * ) device->addr->value, ethertype ))){
420
        if( ERROR_OCCURRED( eth_prepare_packet( device->dummy, next, ( uint8_t * ) device->addr->value, ethertype ))){
425
            // release invalid packet
421
            // release invalid packet
426
            tmp = pq_detach( next );
422
            tmp = pq_detach( next );
427
            pq_release( eth_globals.networking_phone, packet_get_id( next ));
423
            pq_release( eth_globals.net_phone, packet_get_id( next ));
428
            next = tmp;
424
            next = tmp;
429
        }else{
425
        }else{
430
            next = pq_next( next );
426
            next = pq_next( next );
431
        }
427
        }
432
    }while( next );
428
    }while( next );
Line 440... Line 436...
440
    ERROR_DECLARE;
436
    ERROR_DECLARE;
441
 
437
 
442
    measured_string_ref address;
438
    measured_string_ref address;
443
    packet_t            packet;
439
    packet_t            packet;
444
 
440
 
445
//  printf( "\nmessage %d - %d", IPC_GET_METHOD( * call ), NET_NIL_FIRST );
441
//  printf( "message %d - %d\n", IPC_GET_METHOD( * call ), NET_NIL_FIRST );
446
    * answer_count = 0;
442
    * answer_count = 0;
447
    switch( IPC_GET_METHOD( * call )){
443
    switch( IPC_GET_METHOD( * call )){
448
        case IPC_M_PHONE_HUNGUP:
444
        case IPC_M_PHONE_HUNGUP:
449
            return EOK;
445
            return EOK;
450
        case NET_NIL_DEVICE:
446
        case NET_NIL_DEVICE:
451
            return eth_device_message( IPC_GET_DEVICE( call ), IPC_GET_SERVICE( call ), NIL_GET_MTU( call ));
447
            return eth_device_message( IPC_GET_DEVICE( call ), IPC_GET_SERVICE( call ), NIL_GET_MTU( call ));
452
        case NET_NIL_SEND:
448
        case NET_NIL_SEND:
453
            ERROR_PROPAGATE( packet_translate( eth_globals.networking_phone, & packet, IPC_GET_PACKET( call )));
449
            ERROR_PROPAGATE( packet_translate( eth_globals.net_phone, & packet, IPC_GET_PACKET( call )));
454
            return eth_send_message( IPC_GET_DEVICE( call ), packet, IPC_GET_SERVICE( call ));
450
            return eth_send_message( IPC_GET_DEVICE( call ), packet, IPC_GET_SERVICE( call ));
455
        case NET_NIL_PACKET_SPACE:
451
        case NET_NIL_PACKET_SPACE:
456
            ERROR_PROPAGATE( eth_packet_space_message( IPC_GET_DEVICE( call ), IPC_SET_ADDR( answer ), IPC_SET_PREFIX( answer ), IPC_SET_CONTENT( answer ), IPC_SET_SUFFIX( answer )));
452
            ERROR_PROPAGATE( eth_packet_space_message( IPC_GET_DEVICE( call ), IPC_SET_ADDR( answer ), IPC_SET_PREFIX( answer ), IPC_SET_CONTENT( answer ), IPC_SET_SUFFIX( answer )));
457
            * answer_count = 3;
453
            * answer_count = 3;
458
            return EOK;
454
            return EOK;
Line 474... Line 470...
474
    packet_t        packet;
470
    packet_t        packet;
475
 
471
 
476
    while( true ){
472
    while( true ){
477
        switch( IPC_GET_METHOD( * icall )){
473
        switch( IPC_GET_METHOD( * icall )){
478
            case NET_NIL_DEVICE_STATE:
474
            case NET_NIL_DEVICE_STATE:
479
                nil_device_state_wrapper( IPC_GET_DEVICE( icall ), IPC_GET_STATE( icall ));
475
                nil_device_state_msg( 0, IPC_GET_DEVICE( icall ), IPC_GET_STATE( icall ));
480
                ipc_answer_0( iid, EOK );
476
                ipc_answer_0( iid, EOK );
481
                break;
477
                break;
482
            case NET_NIL_RECEIVED:
478
            case NET_NIL_RECEIVED:
483
                if( ! ERROR_OCCURRED( packet_translate( eth_globals.networking_phone, & packet, IPC_GET_PACKET( icall )))){
479
                if( ! ERROR_OCCURRED( packet_translate( eth_globals.net_phone, & packet, IPC_GET_PACKET( icall )))){
484
                    ERROR_CODE = nil_receive_wrapper( IPC_GET_DEVICE( icall ), packet );
480
                    ERROR_CODE = nil_received_msg( 0, IPC_GET_DEVICE( icall ), packet, 0 );
485
                }
481
                }
486
                ipc_answer_0( iid, ERROR_CODE );
482
                ipc_answer_0( iid, ERROR_CODE );
487
                break;
483
                break;
488
            default:
484
            default:
489
                ipc_answer_0( iid, ENOTSUP );
485
                ipc_answer_0( iid, ENOTSUP );