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 50... | Line 50... | ||
50 | #include "../../include/byteorder.h" |
50 | #include "../../include/byteorder.h" |
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 "../../netif/device.h" |
55 | #include "../../include/device.h" |
- | 56 | #include "../../include/netif_messages.h" |
|
56 | 57 | ||
57 | #include "../../structures/measured_strings.h" |
58 | #include "../../structures/measured_strings.h" |
58 | #include "../../structures/packet/packet.h" |
59 | #include "../../structures/packet/packet.h" |
59 | #include "../../structures/packet/packet_client.h" |
60 | #include "../../structures/packet/packet_client.h" |
60 | 61 | ||
Line 65... | Line 66... | ||
65 | 66 | ||
66 | #define ETH_PREFIX ( sizeof( eth_header_t ) + sizeof( eth_header_lsap_t ) + sizeof( eth_header_snap_t )) |
67 | #define ETH_PREFIX ( sizeof( eth_header_t ) + sizeof( eth_header_lsap_t ) + sizeof( eth_header_snap_t )) |
67 | #define ETH_SUFFIX sizeof( eth_fcs_t ) |
68 | #define ETH_SUFFIX sizeof( eth_fcs_t ) |
68 | #define ETH_MAX_CONTENT 1500 |
69 | #define ETH_MAX_CONTENT 1500 |
69 | #define ETH_MIN_CONTENT 46 |
70 | #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_MIN_TAGGED_CONTENT ( ETH_MIN_CONTENT - sizeof( eth_header_lsap_t ) - sizeof( eth_header_snap_t )) |
|
70 | 73 | ||
71 | /** Returns the device identifier message parameter. |
74 | /** Returns the device identifier message parameter. |
72 | */ |
75 | */ |
73 | #define IPC_GET_DEVICE( call ) ( device_id_t ) IPC_GET_ARG1( * call ) |
76 | #define IPC_GET_DEVICE( call ) ( device_id_t ) IPC_GET_ARG1( * call ) |
74 | 77 | ||
Line 82... | Line 85... | ||
82 | */ |
85 | */ |
83 | #define IPC_GET_PROTO( call ) ( services_t ) IPC_GET_ARG2( * call ) |
86 | #define IPC_GET_PROTO( call ) ( services_t ) IPC_GET_ARG2( * call ) |
84 | 87 | ||
85 | /** Returns the device driver service message parameter. |
88 | /** Returns the device driver service message parameter. |
86 | */ |
89 | */ |
87 | #define IPC_GET_SERVICE( call ) ( services_t ) IPC_GET_ARG2( * call ) |
90 | #define IPC_GET_SERVICE( call ) ( services_t ) IPC_GET_ARG3( * call ) |
88 | 91 | ||
89 | #define IPC_GET_MTU( call ) ( size_t ) IPC_GET_ARG3( * call ) |
92 | #define IPC_GET_MTU( call ) ( size_t ) IPC_GET_ARG2( * call ) |
90 | 93 | ||
91 | #define IPC_GET_PHONE( call ) ( int ) IPC_GET_ARG5( * call ) |
94 | #define IPC_GET_PHONE( call ) ( int ) IPC_GET_ARG5( * call ) |
92 | 95 | ||
93 | #define IPC_SET_ADDR( answer ) (( size_t * ) & IPC_GET_ARG1( * answer )) |
96 | #define IPC_SET_ADDR( answer ) (( size_t * ) & IPC_GET_ARG1( * answer )) |
94 | #define IPC_SET_PREFIX( answer ) (( size_t * ) & IPC_GET_ARG2( * answer )) |
97 | #define IPC_SET_PREFIX( answer ) (( size_t * ) & IPC_GET_ARG2( * answer )) |
Line 121... | Line 124... | ||
121 | int eth_receive_message( device_id_t device_id, packet_t packet ); |
124 | int eth_receive_message( device_id_t device_id, packet_t packet ); |
122 | int eth_packet_space_message( device_id_t device_id, size_t * addr_len, size_t * prefix, size_t * content, size_t * suffix ); |
125 | int eth_packet_space_message( device_id_t device_id, size_t * addr_len, size_t * prefix, size_t * content, size_t * suffix ); |
123 | int eth_addr_message( device_id_t device_id, eth_addr_type_t type, measured_string_ref * address ); |
126 | int eth_addr_message( device_id_t device_id, eth_addr_type_t type, measured_string_ref * address ); |
124 | int eth_register_message( services_t service, int phone ); |
127 | int eth_register_message( services_t service, int phone ); |
125 | int eth_send_message( device_id_t device_id, packet_t packet, services_t sender ); |
128 | int eth_send_message( device_id_t device_id, packet_t packet, services_t sender ); |
126 | int eth_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ); |
- | |
127 | void eth_receiver( ipc_callid_t iid, ipc_call_t * icall ); |
129 | void eth_receiver( ipc_callid_t iid, ipc_call_t * icall ); |
128 | eth_proto_ref eth_process_packet( int dummy, packet_t packet ); |
130 | eth_proto_ref eth_process_packet( int dummy, packet_t packet ); |
129 | int eth_prepare_packet( int dummy, packet_t packet, uint8_t * src_addr, int ethertype ); |
131 | int eth_prepare_packet( int dummy, packet_t packet, uint8_t * src_addr, int ethertype ); |
130 | 132 | ||
131 | int eth_initialize( void ){ |
133 | int eth_initialize( void ){ |
Line 148... | Line 150... | ||
148 | } |
150 | } |
149 | 151 | ||
150 | int eth_device_message( device_id_t device_id, services_t service, size_t mtu ){ |
152 | int eth_device_message( device_id_t device_id, services_t service, size_t mtu ){ |
151 | ERROR_DECLARE; |
153 | ERROR_DECLARE; |
152 | 154 | ||
153 | aid_t message; |
- | |
154 | ipc_call_t answer; |
- | |
155 | eth_device_ref device; |
155 | eth_device_ref device; |
156 | ipcarg_t result; |
- | |
157 | int index; |
156 | int index; |
158 | 157 | ||
159 | rwlock_write_lock( & eth_globals.devices_lock ); |
158 | rwlock_write_lock( & eth_globals.devices_lock ); |
160 | // an existing device? |
159 | // an existing device? |
161 | device = eth_devices_find( & eth_globals.devices, device_id ); |
160 | device = eth_devices_find( & eth_globals.devices, device_id ); |
Line 173... | Line 172... | ||
173 | // create a new device |
172 | // create a new device |
174 | device = ( eth_device_ref ) malloc( sizeof( eth_device_t )); |
173 | device = ( eth_device_ref ) malloc( sizeof( eth_device_t )); |
175 | if( ! device ) return ENOMEM; |
174 | if( ! device ) return ENOMEM; |
176 | device->device_id = device_id; |
175 | device->device_id = device_id; |
177 | device->service = service; |
176 | device->service = service; |
178 | device->mtu = ( mtu > 0 ) ? mtu : ETH_MAX_CONTENT; |
177 | device->mtu = (( mtu > 0 ) && ( mtu <= ETH_MAX_TAGGED_CONTENT )) ? mtu : ETH_MAX_TAGGED_CONTENT; |
179 | // TODO get dummy setting |
178 | // TODO get dummy setting |
180 | device->dummy = 0; |
179 | device->dummy = 0; |
181 | // bind the device driver |
180 | // bind the device driver |
182 | device->phone = bind_service( device->service, device->device_id, SERVICE_ETHERNET, 0, eth_receiver ); |
181 | device->phone = bind_service( device->service, device->device_id, SERVICE_ETHERNET, 0, eth_receiver ); |
183 | if( device->phone < 0 ){ |
182 | if( device->phone < 0 ){ |
184 | rwlock_write_unlock( & eth_globals.devices_lock ); |
183 | rwlock_write_unlock( & eth_globals.devices_lock ); |
185 | free( device ); |
184 | free( device ); |
186 | return device->phone; |
185 | return device->phone; |
187 | } |
186 | } |
188 | // get hardware address |
187 | // get hardware address |
189 | message = async_send_1( device->phone, NET_NETIF_GET_ADDR, device->device_id, & answer ); |
- | |
190 | if( ERROR_OCCURRED( measured_strings_return( device->phone, & device->addr, & device->addr_data, 1 ))){ |
188 | if( ERROR_OCCURRED( netif_get_addr( device->phone, device->device_id, & device->addr, & device->addr_data ))){ |
191 | rwlock_write_unlock( & eth_globals.devices_lock ); |
189 | rwlock_write_unlock( & eth_globals.devices_lock ); |
192 | free( device ); |
190 | free( device ); |
193 | async_wait_for( message, NULL ); |
- | |
194 | return ERROR_CODE; |
- | |
195 | } |
- | |
196 | async_wait_for( message, & result ); |
- | |
197 | if( ERROR_OCCURRED( result )){ |
- | |
198 | rwlock_write_unlock( & eth_globals.devices_lock ); |
- | |
199 | free( device->addr ); |
- | |
200 | free( device->addr_data ); |
- | |
201 | free( device ); |
- | |
202 | return ERROR_CODE; |
191 | return ERROR_CODE; |
203 | } |
192 | } |
204 | // add to the cache |
193 | // add to the cache |
205 | index = eth_devices_add( & eth_globals.devices, device->device_id, device ); |
194 | index = eth_devices_add( & eth_globals.devices, device->device_id, device ); |
206 | if( index < 0 ){ |
195 | if( index < 0 ){ |
Line 246... | Line 235... | ||
246 | return NULL; |
235 | return NULL; |
247 | }else if(( header->lsap.dsap == ETH_LSAP_SNAP ) && ( header->lsap.ssap == ETH_LSAP_SNAP )){ |
236 | }else if(( header->lsap.dsap == ETH_LSAP_SNAP ) && ( header->lsap.ssap == ETH_LSAP_SNAP )){ |
248 | // IEEE 802.3 + 802.2 + LSAP + SNAP |
237 | // IEEE 802.3 + 802.2 + LSAP + SNAP |
249 | // organization code not supported |
238 | // organization code not supported |
250 | type = ntohs( header->snap.ethertype ); |
239 | type = ntohs( header->snap.ethertype ); |
251 | prefix = sizeof( eth_header_t ) + sizeof( eth_header_lsap_t) + sizeof( eth_header_snap_t); |
240 | prefix = sizeof( eth_header_t ) + sizeof( eth_header_lsap_t ) + sizeof( eth_header_snap_t ); |
252 | }else{ |
241 | }else{ |
253 | // IEEE 802.3 + 802.2 LSAP |
242 | // IEEE 802.3 + 802.2 LSAP |
254 | type = lsap_map( header->lsap.dsap ); |
243 | type = lsap_map( header->lsap.dsap ); |
255 | prefix = sizeof( eth_header_t ) + sizeof( eth_header_lsap_t); |
244 | prefix = sizeof( eth_header_t ) + sizeof( eth_header_lsap_t); |
256 | } |
245 | } |
Line 311... | Line 300... | ||
311 | device = eth_devices_find( & eth_globals.devices, device_id ); |
300 | device = eth_devices_find( & eth_globals.devices, device_id ); |
312 | if( ! device ){ |
301 | if( ! device ){ |
313 | rwlock_read_unlock( & eth_globals.devices_lock ); |
302 | rwlock_read_unlock( & eth_globals.devices_lock ); |
314 | return ENOENT; |
303 | return ENOENT; |
315 | } |
304 | } |
316 | * content = ( ETH_MAX_CONTENT > device->mtu ) ? device->mtu : ETH_MAX_CONTENT; |
305 | * content = device->mtu; |
317 | rwlock_read_unlock( & eth_globals.devices_lock ); |
306 | rwlock_read_unlock( & eth_globals.devices_lock ); |
318 | * addr_len = ETH_ADDR; |
307 | * addr_len = ETH_ADDR; |
319 | * prefix = ETH_PREFIX; |
308 | * prefix = ETH_PREFIX; |
320 | * suffix = ETH_MIN_CONTENT + ETH_SUFFIX; |
309 | * suffix = ETH_MIN_CONTENT + ETH_SUFFIX; |
321 | return EOK; |
310 | return EOK; |
Line 382... | Line 371... | ||
382 | int length; |
371 | int length; |
383 | int i; |
372 | int i; |
384 | void * padding; |
373 | void * padding; |
385 | eth_preamble_ref preamble; |
374 | eth_preamble_ref preamble; |
386 | 375 | ||
- | 376 | length = packet_get_data_length( packet ); |
|
- | 377 | if( length > ETH_MAX_TAGGED_CONTENT ) return EINVAL; |
|
- | 378 | if( length < ETH_MIN_TAGGED_CONTENT ){ |
|
- | 379 | padding = packet_suffix( packet, ETH_MIN_TAGGED_CONTENT - length ); |
|
- | 380 | if( ! padding ) return ENOMEM; |
|
- | 381 | bzero( padding, ETH_MIN_TAGGED_CONTENT - length ); |
|
- | 382 | } |
|
387 | if( dummy ){ |
383 | if( dummy ){ |
388 | preamble = PACKET_PREFIX( packet, eth_preamble_t ); |
384 | preamble = PACKET_PREFIX( packet, eth_preamble_t ); |
389 | if( ! preamble ) return ENOMEM; |
385 | if( ! preamble ) return ENOMEM; |
390 | for( i = 0; i < 7; ++ i ) preamble->preamble[ i ] = ETH_PREAMBLE; |
386 | for( i = 0; i < 7; ++ i ) preamble->preamble[ i ] = ETH_PREAMBLE; |
391 | preamble->sfd = ETH_SFD; |
387 | preamble->sfd = ETH_SFD; |
392 | } |
388 | } |
393 | header = PACKET_PREFIX( packet, eth_header_ex_t ); |
389 | header = PACKET_PREFIX( packet, eth_header_ex_t ); |
394 | if( ! header ) return ENOMEM; |
390 | if( ! header ) return ENOMEM; |
395 | length = packet_get_addr( packet, & src, & dest ); |
- | |
396 | if( length < 0 ) return length; |
- | |
397 | if( length < ETH_ADDR ) return EINVAL; |
- | |
398 | memcpy( header->header.src, src_addr, ETH_ADDR ); |
- | |
399 | memcpy( & header->header.dest, dest, ETH_ADDR ); |
- | |
400 | length = packet_get_data_length( packet ); |
- | |
401 | if( length > ETH_MAX_CONTENT ) return EINVAL; |
- | |
402 | if( length < ETH_MIN_CONTENT ){ |
- | |
403 | padding = packet_suffix( packet, ETH_MIN_CONTENT - length ); |
- | |
404 | if( ! padding ) return ENOMEM; |
- | |
405 | bzero( padding, ETH_MIN_CONTENT - length ); |
- | |
406 | } |
- | |
407 | header->header.ethertype = htons( length ); |
391 | header->header.ethertype = htons( length + sizeof( eth_header_lsap_t ) + sizeof( eth_header_snap_t )); |
408 | header->lsap.dsap = 0xAA; |
392 | header->lsap.dsap = 0xAA; |
409 | header->lsap.ssap = header->lsap.dsap; |
393 | header->lsap.ssap = header->lsap.dsap; |
410 | header->lsap.ctrl = 0; |
394 | header->lsap.ctrl = 0; |
411 | for( i = 0; i < 3; ++ i ) header->snap.proto[ i ] = 0; |
395 | for( i = 0; i < 3; ++ i ) header->snap.proto[ i ] = 0; |
412 | header->snap.ethertype = ethertype; |
396 | header->snap.ethertype = ethertype; |
- | 397 | length = packet_get_addr( packet, & src, & dest ); |
|
- | 398 | if( length < 0 ) return length; |
|
- | 399 | if( length < ETH_ADDR ) return EINVAL; |
|
- | 400 | memcpy( header->header.src, src_addr, ETH_ADDR ); |
|
- | 401 | memcpy( header->header.dest, dest, ETH_ADDR ); |
|
413 | if( dummy ){ |
402 | if( dummy ){ |
414 | fcs = PACKET_SUFFIX( packet, eth_fcs_t ); |
403 | fcs = PACKET_SUFFIX( packet, eth_fcs_t ); |
415 | if( ! fcs ) return ENOMEM; |
404 | if( ! fcs ) return ENOMEM; |
416 | * fcs = htonl( ~ compute_crc32( ~ 0, & header->header.dest, ((( void * ) fcs ) - (( void * ) & header->header.dest )) * 8 )); |
405 | * fcs = htonl( ~ compute_crc32( ~ 0, & header->header.dest, ((( void * ) fcs ) - (( void * ) & header->header.dest )) * 8 )); |
417 | } |
406 | } |
Line 448... | Line 437... | ||
448 | }else{ |
437 | }else{ |
449 | next = pq_next( next ); |
438 | next = pq_next( next ); |
450 | } |
439 | } |
451 | }while( next ); |
440 | }while( next ); |
452 | // send packet queue |
441 | // send packet queue |
453 | async_msg_2( device->phone, NET_NETIF_SEND, device_id, packet_get_id( packet )); |
442 | netif_send_msg( device->phone, device_id, packet ); |
454 | rwlock_read_unlock( & eth_globals.devices_lock ); |
443 | rwlock_read_unlock( & eth_globals.devices_lock ); |
455 | return EOK; |
444 | return EOK; |
456 | } |
445 | } |
457 | 446 | ||
458 | int eth_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){ |
447 | int eth_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){ |
459 | ERROR_DECLARE; |
448 | ERROR_DECLARE; |
460 | 449 | ||
461 | measured_string_ref address; |
450 | measured_string_ref address; |
462 | packet_t packet; |
451 | packet_t packet; |
463 | 452 | ||
- | 453 | // printf( "\nmessage %d - %d", IPC_GET_METHOD( * call ), NET_NIL_FIRST ); |
|
464 | * answer_count = 0; |
454 | * answer_count = 0; |
465 | switch( IPC_GET_METHOD( * call )){ |
455 | switch( IPC_GET_METHOD( * call )){ |
466 | case IPC_M_PHONE_HUNGUP: |
456 | case IPC_M_PHONE_HUNGUP: |
467 | return EOK; |
457 | return EOK; |
468 | case NET_NIL_DEVICE: |
458 | case NET_NIL_DEVICE: |
Line 478... | Line 468... | ||
478 | ERROR_PROPAGATE( eth_addr_message( IPC_GET_DEVICE( call ), ETH_LOCAL_ADDR, & address )); |
468 | ERROR_PROPAGATE( eth_addr_message( IPC_GET_DEVICE( call ), ETH_LOCAL_ADDR, & address )); |
479 | return measured_strings_reply( address, 1 ); |
469 | return measured_strings_reply( address, 1 ); |
480 | case NET_NIL_BROADCAST_ADDR: |
470 | case NET_NIL_BROADCAST_ADDR: |
481 | ERROR_PROPAGATE( eth_addr_message( IPC_GET_DEVICE( call ), ETH_BROADCAST_ADDR, & address )); |
471 | ERROR_PROPAGATE( eth_addr_message( IPC_GET_DEVICE( call ), ETH_BROADCAST_ADDR, & address )); |
482 | return measured_strings_reply( address, 1 ); |
472 | return measured_strings_reply( address, 1 ); |
483 | return ERROR_CODE; |
- | |
484 | case IPC_M_CONNECT_TO_ME: |
473 | case IPC_M_CONNECT_TO_ME: |
485 | return eth_register_message( IPC_GET_PROTO( call ), IPC_GET_PHONE( call )); |
474 | return eth_register_message( IPC_GET_PROTO( call ), IPC_GET_PHONE( call )); |
486 | } |
475 | } |
487 | return ENOTSUP; |
476 | return ENOTSUP; |
488 | } |
477 | } |