Rev 4243 | Rev 4307 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
| Rev 4243 | Rev 4261 | ||
|---|---|---|---|
| Line 52... | Line 52... | ||
| 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_messages.h" |
| - | 57 | #include "../../include/nil_messages.h" |
|
| 57 | 58 | ||
| 58 | #include "../../structures/measured_strings.h" |
59 | #include "../../structures/measured_strings.h" |
| 59 | #include "../../structures/packet/packet.h" |
- | |
| 60 | #include "../../structures/packet/packet_client.h" |
60 | #include "../../structures/packet/packet_client.h" |
| 61 | 61 | ||
| - | 62 | #include "../nil_module.h" |
|
| - | 63 | #include "../nil_wrappers.h" |
|
| - | 64 | ||
| 62 | #include "eth.h" |
65 | #include "eth.h" |
| 63 | #include "eth_header.h" |
66 | #include "eth_header.h" |
| 64 | //#include "eth_messages.h" |
- | |
| 65 | #include "eth_module.h" |
- | |
| 66 | 67 | ||
| 67 | #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 )) |
| 68 | #define ETH_SUFFIX sizeof( eth_fcs_t ) |
69 | #define ETH_SUFFIX sizeof( eth_fcs_t ) |
| 69 | #define ETH_MAX_CONTENT 1500 |
70 | #define ETH_MAX_CONTENT 1500 |
| 70 | #define ETH_MIN_CONTENT 46 |
71 | #define ETH_MIN_CONTENT 46 |
| 71 | #define ETH_MAX_TAGGED_CONTENT ( ETH_MAX_CONTENT - sizeof( eth_header_lsap_t ) - sizeof( eth_header_snap_t )) |
72 | #define ETH_MAX_TAGGED_CONTENT ( ETH_MAX_CONTENT - sizeof( eth_header_lsap_t ) - sizeof( eth_header_snap_t )) |
| 72 | #define ETH_MIN_TAGGED_CONTENT ( ETH_MIN_CONTENT - sizeof( eth_header_lsap_t ) - sizeof( eth_header_snap_t )) |
73 | #define ETH_MIN_TAGGED_CONTENT ( ETH_MIN_CONTENT - sizeof( eth_header_lsap_t ) - sizeof( eth_header_snap_t )) |
| 73 | 74 | ||
| 74 | /** Returns the device identifier message parameter. |
- | |
| 75 | */ |
- | |
| 76 | #define IPC_GET_DEVICE( call ) ( device_id_t ) IPC_GET_ARG1( * call ) |
- | |
| 77 | - | ||
| 78 | /** Returns the packet identifier message parameter. |
- | |
| 79 | */ |
- | |
| 80 | #define IPC_GET_PACKET( call ) ( packet_id_t ) IPC_GET_ARG2( * call ) |
- | |
| 81 | - | ||
| 82 | #define IPC_GET_STATE( call ) ( device_state_t ) IPC_GET_ARG2( * call ) |
- | |
| 83 | - | ||
| 84 | /** Returns the protocol service message parameter. |
- | |
| 85 | */ |
- | |
| 86 | #define IPC_GET_PROTO( call ) ( services_t ) IPC_GET_ARG2( * call ) |
- | |
| 87 | - | ||
| 88 | /** Returns the device driver service message parameter. |
- | |
| 89 | */ |
- | |
| 90 | #define IPC_GET_SERVICE( call ) ( services_t ) IPC_GET_ARG3( * call ) |
- | |
| 91 | - | ||
| 92 | #define IPC_GET_MTU( call ) ( size_t ) IPC_GET_ARG2( * call ) |
- | |
| 93 | - | ||
| 94 | #define IPC_GET_PHONE( call ) ( int ) IPC_GET_ARG5( * call ) |
- | |
| 95 | - | ||
| 96 | #define IPC_SET_ADDR( answer ) (( size_t * ) & IPC_GET_ARG1( * answer )) |
- | |
| 97 | #define IPC_SET_PREFIX( answer ) (( size_t * ) & IPC_GET_ARG2( * answer )) |
- | |
| 98 | #define IPC_SET_CONTENT( answer ) (( size_t * ) & IPC_GET_ARG3( * answer )) |
- | |
| 99 | #define IPC_SET_SUFFIX( answer ) (( size_t * ) & IPC_GET_ARG4( * answer )) |
- | |
| 100 | - | ||
| 101 | typedef enum eth_addr_type eth_addr_type_t; |
75 | typedef enum eth_addr_type eth_addr_type_t; |
| 102 | typedef eth_addr_type_t * eth_addr_type_ref; |
76 | typedef eth_addr_type_t * eth_addr_type_ref; |
| 103 | 77 | ||
| 104 | enum eth_addr_type{ |
78 | enum eth_addr_type{ |
| 105 | ETH_LOCAL_ADDR, |
79 | ETH_LOCAL_ADDR, |
| Line 122... | Line 96... | ||
| 122 | 96 | ||
| 123 | 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 ); |
| 124 | int eth_receive_message( device_id_t device_id, packet_t packet ); |
98 | int eth_receive_message( device_id_t device_id, packet_t packet ); |
| 125 | int eth_packet_space_message( device_id_t device_id, size_t * addr_len, size_t * prefix, size_t * content, size_t * suffix ); |
99 | int eth_packet_space_message( device_id_t device_id, size_t * addr_len, size_t * prefix, size_t * content, size_t * suffix ); |
| 126 | int eth_addr_message( device_id_t device_id, eth_addr_type_t type, measured_string_ref * address ); |
100 | int eth_addr_message( device_id_t device_id, eth_addr_type_t type, measured_string_ref * address ); |
| 127 | int eth_register_message( services_t service, int phone ); |
- | |
| 128 | int eth_send_message( device_id_t device_id, packet_t packet, services_t sender ); |
101 | int eth_send_message( device_id_t device_id, packet_t packet, services_t sender ); |
| 129 | void eth_receiver( ipc_callid_t iid, ipc_call_t * icall ); |
- | |
| 130 | eth_proto_ref eth_process_packet( int dummy, packet_t packet ); |
102 | eth_proto_ref eth_process_packet( int dummy, packet_t packet ); |
| 131 | int eth_prepare_packet( int dummy, packet_t packet, uint8_t * src_addr, int ethertype ); |
103 | int eth_prepare_packet( int dummy, packet_t packet, uint8_t * src_addr, int ethertype ); |
| 132 | 104 | ||
| - | 105 | void nil_device_state_wrapper( device_id_t device_id, int state ){ |
|
| - | 106 | int index; |
|
| - | 107 | eth_proto_ref proto; |
|
| - | 108 | ||
| - | 109 | //TODO clear device if off? |
|
| - | 110 | rwlock_read_lock( & eth_globals.protos_lock ); |
|
| - | 111 | for( index = eth_protos_count( & eth_globals.protos ) - 1; index >= 0; -- index ){ |
|
| - | 112 | 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 | } |
|
| - | 115 | rwlock_read_unlock( & eth_globals.protos_lock ); |
|
| - | 116 | } |
|
| - | 117 | ||
| - | 118 | int nil_receive_wrapper( device_id_t device_id, packet_t packet ){ |
|
| - | 119 | return eth_receive_message( device_id, packet ); |
|
| - | 120 | } |
|
| - | 121 | ||
| 133 | int eth_initialize( void ){ |
122 | int nil_initialize( int networking_phone ){ |
| 134 | ERROR_DECLARE; |
123 | ERROR_DECLARE; |
| 135 | 124 | ||
| 136 | rwlock_initialize( & eth_globals.devices_lock ); |
125 | rwlock_initialize( & eth_globals.devices_lock ); |
| 137 | rwlock_initialize( & eth_globals.protos_lock ); |
126 | rwlock_initialize( & eth_globals.protos_lock ); |
| 138 | rwlock_write_lock( & eth_globals.devices_lock ); |
127 | rwlock_write_lock( & eth_globals.devices_lock ); |
| 139 | rwlock_write_lock( & eth_globals.protos_lock ); |
128 | rwlock_write_lock( & eth_globals.protos_lock ); |
| - | 129 | eth_globals.networking_phone = networking_phone; |
|
| 140 | eth_globals.broadcast_addr = measured_string_create_bulk( "\xFF\xFF\xFF\xFF\xFF\xFF", CONVERT_SIZE( uint8_t, char, ETH_ADDR )); |
130 | eth_globals.broadcast_addr = measured_string_create_bulk( "\xFF\xFF\xFF\xFF\xFF\xFF", CONVERT_SIZE( uint8_t, char, ETH_ADDR )); |
| 141 | if( ! eth_globals.broadcast_addr ) return ENOMEM; |
131 | if( ! eth_globals.broadcast_addr ) return ENOMEM; |
| 142 | ERROR_PROPAGATE( eth_devices_initialize( & eth_globals.devices )); |
132 | ERROR_PROPAGATE( eth_devices_initialize( & eth_globals.devices )); |
| 143 | if( ERROR_OCCURRED( eth_protos_initialize( & eth_globals.protos ))){ |
133 | if( ERROR_OCCURRED( eth_protos_initialize( & eth_globals.protos ))){ |
| 144 | eth_devices_destroy( & eth_globals.devices ); |
134 | eth_devices_destroy( & eth_globals.devices ); |
| Line 176... | Line 166... | ||
| 176 | device->service = service; |
166 | device->service = service; |
| 177 | device->mtu = (( mtu > 0 ) && ( mtu <= ETH_MAX_TAGGED_CONTENT )) ? mtu : ETH_MAX_TAGGED_CONTENT; |
167 | device->mtu = (( mtu > 0 ) && ( mtu <= ETH_MAX_TAGGED_CONTENT )) ? mtu : ETH_MAX_TAGGED_CONTENT; |
| 178 | // TODO get dummy setting |
168 | // TODO get dummy setting |
| 179 | device->dummy = 0; |
169 | device->dummy = 0; |
| 180 | // bind the device driver |
170 | // bind the device driver |
| - | 171 | #if ! NETIF_BUNDLE |
|
| 181 | device->phone = bind_service( device->service, device->device_id, SERVICE_ETHERNET, 0, eth_receiver ); |
172 | device->phone = bind_service( device->service, device->device_id, SERVICE_ETHERNET, 0, eth_receiver ); |
| 182 | if( device->phone < 0 ){ |
173 | if( device->phone < 0 ){ |
| 183 | rwlock_write_unlock( & eth_globals.devices_lock ); |
174 | rwlock_write_unlock( & eth_globals.devices_lock ); |
| 184 | free( device ); |
175 | free( device ); |
| 185 | return device->phone; |
176 | return device->phone; |
| 186 | } |
177 | } |
| - | 178 | #endif |
|
| 187 | // get hardware address |
179 | // get hardware address |
| 188 | if( ERROR_OCCURRED( netif_get_addr( device->phone, device->device_id, & device->addr, & device->addr_data ))){ |
180 | if( ERROR_OCCURRED( netif_get_addr( device->phone, device->device_id, & device->addr, & device->addr_data ))){ |
| 189 | rwlock_write_unlock( & eth_globals.devices_lock ); |
181 | rwlock_write_unlock( & eth_globals.devices_lock ); |
| 190 | free( device ); |
182 | free( device ); |
| 191 | return ERROR_CODE; |
183 | return ERROR_CODE; |
| Line 279... | Line 271... | ||
| 279 | rwlock_read_lock( & eth_globals.protos_lock ); |
271 | rwlock_read_lock( & eth_globals.protos_lock ); |
| 280 | do{ |
272 | do{ |
| 281 | next = pq_detach( packet ); |
273 | next = pq_detach( packet ); |
| 282 | proto = eth_process_packet( dummy, packet ); |
274 | proto = eth_process_packet( dummy, packet ); |
| 283 | if( proto ){ |
275 | if( proto ){ |
| 284 | async_msg_2( proto->phone, NET_IL_RECEIVED, device_id, packet_get_id( packet )); |
276 | async_msg_3( proto->phone, NET_IL_RECEIVED, device_id, packet_get_id( packet ), proto->service ); |
| 285 | }else{ |
277 | }else{ |
| 286 | // drop invalid/unknown |
278 | // drop invalid/unknown |
| 287 | pq_release( eth_globals.networking_phone, packet_get_id( packet )); |
279 | pq_release( eth_globals.networking_phone, packet_get_id( packet )); |
| 288 | } |
280 | } |
| 289 | packet = next; |
281 | packet = next; |
| Line 327... | Line 319... | ||
| 327 | rwlock_read_unlock( & eth_globals.devices_lock ); |
319 | rwlock_read_unlock( & eth_globals.devices_lock ); |
| 328 | } |
320 | } |
| 329 | return ( * address ) ? EOK : ENOENT; |
321 | return ( * address ) ? EOK : ENOENT; |
| 330 | } |
322 | } |
| 331 | 323 | ||
| 332 | int eth_register_message( services_t service, int phone ){ |
324 | int nil_register_message( services_t service, int phone ){ |
| 333 | eth_proto_ref proto; |
325 | eth_proto_ref proto; |
| 334 | int protocol; |
326 | int protocol; |
| 335 | int index; |
327 | int index; |
| 336 | 328 | ||
| 337 | protocol = protocol_map( SERVICE_ETHERNET, service ); |
329 | protocol = protocol_map( SERVICE_ETHERNET, service ); |
| Line 387... | Line 379... | ||
| 387 | preamble->sfd = ETH_SFD; |
379 | preamble->sfd = ETH_SFD; |
| 388 | } |
380 | } |
| 389 | header = PACKET_PREFIX( packet, eth_header_ex_t ); |
381 | header = PACKET_PREFIX( packet, eth_header_ex_t ); |
| 390 | if( ! header ) return ENOMEM; |
382 | if( ! header ) return ENOMEM; |
| 391 | header->header.ethertype = htons( length + sizeof( eth_header_lsap_t ) + sizeof( eth_header_snap_t )); |
383 | header->header.ethertype = htons( length + sizeof( eth_header_lsap_t ) + sizeof( eth_header_snap_t )); |
| 392 | header->lsap.dsap = 0xAA; |
384 | header->lsap.dsap = ( uint16_t ) ETH_LSAP_SNAP; |
| 393 | header->lsap.ssap = header->lsap.dsap; |
385 | header->lsap.ssap = header->lsap.dsap; |
| 394 | header->lsap.ctrl = 0; |
386 | header->lsap.ctrl = 0; |
| 395 | for( i = 0; i < 3; ++ i ) header->snap.proto[ i ] = 0; |
387 | for( i = 0; i < 3; ++ i ) header->snap.proto[ i ] = 0; |
| 396 | header->snap.ethertype = ethertype; |
388 | header->snap.ethertype = ( uint16_t ) ethertype; |
| 397 | length = packet_get_addr( packet, & src, & dest ); |
389 | length = packet_get_addr( packet, & src, & dest ); |
| 398 | if( length < 0 ) return length; |
390 | if( length < 0 ) return length; |
| 399 | if( length < ETH_ADDR ) return EINVAL; |
391 | if( length < ETH_ADDR ) return EINVAL; |
| 400 | memcpy( header->header.src, src_addr, ETH_ADDR ); |
392 | memcpy( header->header.src, src_addr, ETH_ADDR ); |
| 401 | memcpy( header->header.dest, dest, ETH_ADDR ); |
393 | memcpy( header->header.dest, dest, ETH_ADDR ); |
| Line 437... | Line 429... | ||
| 437 | }else{ |
429 | }else{ |
| 438 | next = pq_next( next ); |
430 | next = pq_next( next ); |
| 439 | } |
431 | } |
| 440 | }while( next ); |
432 | }while( next ); |
| 441 | // send packet queue |
433 | // send packet queue |
| 442 | netif_send_msg( device->phone, device_id, packet ); |
434 | netif_send_msg( device->phone, device_id, packet, SERVICE_ETHERNET ); |
| 443 | rwlock_read_unlock( & eth_globals.devices_lock ); |
435 | rwlock_read_unlock( & eth_globals.devices_lock ); |
| 444 | return EOK; |
436 | return EOK; |
| 445 | } |
437 | } |
| 446 | 438 | ||
| 447 | int eth_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){ |
439 | int nil_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){ |
| 448 | ERROR_DECLARE; |
440 | ERROR_DECLARE; |
| 449 | 441 | ||
| 450 | measured_string_ref address; |
442 | measured_string_ref address; |
| 451 | packet_t packet; |
443 | packet_t packet; |
| 452 | 444 | ||
| Line 454... | Line 446... | ||
| 454 | * answer_count = 0; |
446 | * answer_count = 0; |
| 455 | switch( IPC_GET_METHOD( * call )){ |
447 | switch( IPC_GET_METHOD( * call )){ |
| 456 | case IPC_M_PHONE_HUNGUP: |
448 | case IPC_M_PHONE_HUNGUP: |
| 457 | return EOK; |
449 | return EOK; |
| 458 | case NET_NIL_DEVICE: |
450 | case NET_NIL_DEVICE: |
| 459 | return eth_device_message( IPC_GET_DEVICE( call ), IPC_GET_SERVICE( call ), IPC_GET_MTU( call )); |
451 | return eth_device_message( IPC_GET_DEVICE( call ), IPC_GET_SERVICE( call ), NIL_GET_MTU( call )); |
| 460 | case NET_NIL_SEND: |
452 | case NET_NIL_SEND: |
| 461 | ERROR_PROPAGATE( packet_translate( eth_globals.networking_phone, & packet, IPC_GET_PACKET( call ))); |
453 | ERROR_PROPAGATE( packet_translate( eth_globals.networking_phone, & packet, IPC_GET_PACKET( call ))); |
| 462 | return eth_send_message( IPC_GET_DEVICE( call ), packet, IPC_GET_SERVICE( call )); |
454 | return eth_send_message( IPC_GET_DEVICE( call ), packet, IPC_GET_SERVICE( call )); |
| 463 | case NET_NIL_PACKET_SPACE: |
455 | case NET_NIL_PACKET_SPACE: |
| 464 | 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 ))); |
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 ))); |
| Line 469... | Line 461... | ||
| 469 | return measured_strings_reply( address, 1 ); |
461 | return measured_strings_reply( address, 1 ); |
| 470 | case NET_NIL_BROADCAST_ADDR: |
462 | case NET_NIL_BROADCAST_ADDR: |
| 471 | ERROR_PROPAGATE( eth_addr_message( IPC_GET_DEVICE( call ), ETH_BROADCAST_ADDR, & address )); |
463 | ERROR_PROPAGATE( eth_addr_message( IPC_GET_DEVICE( call ), ETH_BROADCAST_ADDR, & address )); |
| 472 | return measured_strings_reply( address, 1 ); |
464 | return measured_strings_reply( address, 1 ); |
| 473 | case IPC_M_CONNECT_TO_ME: |
465 | case IPC_M_CONNECT_TO_ME: |
| 474 | return eth_register_message( IPC_GET_PROTO( call ), IPC_GET_PHONE( call )); |
466 | return nil_register_message( NIL_GET_PROTO( call ), IPC_GET_PHONE( call )); |
| 475 | } |
467 | } |
| 476 | return ENOTSUP; |
468 | return ENOTSUP; |
| 477 | } |
469 | } |
| 478 | 470 | ||
| 479 | void eth_receiver( ipc_callid_t iid, ipc_call_t * icall ){ |
471 | void eth_receiver( ipc_callid_t iid, ipc_call_t * icall ){ |
| 480 | ERROR_DECLARE; |
472 | ERROR_DECLARE; |
| 481 | 473 | ||
| 482 | packet_t packet; |
474 | packet_t packet; |
| 483 | int index; |
- | |
| 484 | eth_proto_ref proto; |
- | |
| 485 | 475 | ||
| 486 | while( true ){ |
476 | while( true ){ |
| 487 | switch( IPC_GET_METHOD( * icall )){ |
477 | switch( IPC_GET_METHOD( * icall )){ |
| 488 | case NET_NIL_DEVICE_STATE: |
478 | case NET_NIL_DEVICE_STATE: |
| 489 | //TODO clear device if off? |
- | |
| 490 | rwlock_read_lock( & eth_globals.protos_lock ); |
- | |
| 491 | for( index = eth_protos_count( & eth_globals.protos ) - 1; index >= 0; -- index ){ |
- | |
| 492 | proto = eth_protos_get_index( & eth_globals.protos, index ); |
- | |
| 493 | if( proto && proto->phone ) async_msg_2( proto->phone, NET_IL_DEVICE_STATE, IPC_GET_DEVICE( icall ), IPC_GET_STATE( icall )); |
479 | nil_device_state_wrapper( IPC_GET_DEVICE( icall ), IPC_GET_STATE( icall )); |
| 494 | } |
- | |
| 495 | rwlock_read_unlock( & eth_globals.protos_lock ); |
- | |
| 496 | ipc_answer_0( iid, EOK ); |
480 | ipc_answer_0( iid, EOK ); |
| 497 | break; |
481 | break; |
| 498 | case NET_NIL_RECEIVED: |
482 | case NET_NIL_RECEIVED: |
| 499 | if( ! ERROR_OCCURRED( packet_translate( eth_globals.networking_phone, & packet, IPC_GET_PACKET( icall )))){ |
483 | if( ! ERROR_OCCURRED( packet_translate( eth_globals.networking_phone, & packet, IPC_GET_PACKET( icall )))){ |
| 500 | ERROR_CODE = eth_receive_message( IPC_GET_DEVICE( icall ), packet ); |
484 | ERROR_CODE = nil_receive_wrapper( IPC_GET_DEVICE( icall ), packet ); |
| 501 | } |
485 | } |
| 502 | ipc_answer_0( iid, ERROR_CODE ); |
486 | ipc_answer_0( iid, ERROR_CODE ); |
| 503 | break; |
487 | break; |
| 504 | default: |
488 | default: |
| 505 | ipc_answer_0( iid, ENOTSUP ); |
489 | ipc_answer_0( iid, ENOTSUP ); |