Rev 4163 | Rev 4243 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 3992 | mejdrech | 1 | /* |
| 2 | * Copyright (c) 2009 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 eth |
||
| 30 | * @{ |
||
| 31 | */ |
||
| 32 | |||
| 33 | /** @file |
||
| 34 | * Ethernet module implementation. |
||
| 35 | * @see eth.h |
||
| 36 | */ |
||
| 37 | |||
| 38 | #include <async.h> |
||
| 39 | #include <malloc.h> |
||
| 4192 | mejdrech | 40 | #include <mem.h> |
| 3992 | mejdrech | 41 | #include <stdio.h> |
| 42 | |||
| 43 | #include <ipc/ipc.h> |
||
| 44 | #include <ipc/services.h> |
||
| 45 | |||
| 46 | #include "../../err.h" |
||
| 47 | #include "../../messages.h" |
||
| 48 | #include "../../modules.h" |
||
| 49 | |||
| 50 | #include "../../include/byteorder.h" |
||
| 4075 | mejdrech | 51 | #include "../../include/crc.h" |
| 52 | #include "../../include/ethernet_lsap.h" |
||
| 3992 | mejdrech | 53 | #include "../../include/ethernet_protocols.h" |
| 54 | #include "../../include/protocol_map.h" |
||
| 55 | #include "../../netif/device.h" |
||
| 56 | |||
| 57 | #include "../../structures/measured_strings.h" |
||
| 58 | #include "../../structures/packet/packet.h" |
||
| 59 | #include "../../structures/packet/packet_client.h" |
||
| 60 | |||
| 61 | #include "eth.h" |
||
| 62 | #include "eth_header.h" |
||
| 63 | //#include "eth_messages.h" |
||
| 64 | #include "eth_module.h" |
||
| 65 | |||
| 66 | #define ETH_PREFIX ( sizeof( eth_header_t ) + sizeof( eth_header_lsap_t ) + sizeof( eth_header_snap_t )) |
||
| 4075 | mejdrech | 67 | #define ETH_SUFFIX sizeof( eth_fcs_t ) |
| 68 | #define ETH_MAX_CONTENT 1500 |
||
| 3992 | mejdrech | 69 | #define ETH_MIN_CONTENT 46 |
| 70 | |||
| 71 | /** Returns the device identifier message parameter. |
||
| 72 | */ |
||
| 73 | #define IPC_GET_DEVICE( call ) ( device_id_t ) IPC_GET_ARG1( * call ) |
||
| 74 | |||
| 75 | /** Returns the packet identifier message parameter. |
||
| 76 | */ |
||
| 77 | #define IPC_GET_PACKET( call ) ( packet_id_t ) IPC_GET_ARG2( * call ) |
||
| 78 | |||
| 4192 | mejdrech | 79 | #define IPC_GET_STATE( call ) ( device_state_t ) IPC_GET_ARG2( * call ) |
| 80 | |||
| 3992 | mejdrech | 81 | /** Returns the protocol service message parameter. |
| 82 | */ |
||
| 4192 | mejdrech | 83 | #define IPC_GET_PROTO( call ) ( services_t ) IPC_GET_ARG2( * call ) |
| 3992 | mejdrech | 84 | |
| 85 | /** Returns the device driver service message parameter. |
||
| 86 | */ |
||
| 87 | #define IPC_GET_SERVICE( call ) ( services_t ) IPC_GET_ARG2( * call ) |
||
| 88 | |||
| 89 | #define IPC_GET_MTU( call ) ( size_t ) IPC_GET_ARG3( * call ) |
||
| 90 | |||
| 91 | #define IPC_GET_PHONE( call ) ( int ) IPC_GET_ARG5( * call ) |
||
| 92 | |||
| 93 | #define IPC_SET_ADDR( answer ) (( size_t * ) & IPC_GET_ARG1( * answer )) |
||
| 94 | #define IPC_SET_PREFIX( answer ) (( size_t * ) & IPC_GET_ARG2( * answer )) |
||
| 95 | #define IPC_SET_CONTENT( answer ) (( size_t * ) & IPC_GET_ARG3( * answer )) |
||
| 96 | #define IPC_SET_SUFFIX( answer ) (( size_t * ) & IPC_GET_ARG4( * answer )) |
||
| 97 | |||
| 98 | typedef enum eth_addr_type eth_addr_type_t; |
||
| 99 | typedef eth_addr_type_t * eth_addr_type_ref; |
||
| 100 | |||
| 101 | enum eth_addr_type{ |
||
| 102 | ETH_LOCAL_ADDR, |
||
| 103 | ETH_BROADCAST_ADDR |
||
| 104 | }; |
||
| 105 | |||
| 106 | /** Ethernet global data. |
||
| 107 | */ |
||
| 108 | eth_globals_t eth_globals; |
||
| 109 | |||
| 110 | /** Processes IPC messages from the registered device driver modules in an infinite loop. |
||
| 111 | * @param iid The message identifier. Input parameter. |
||
| 112 | * @param icall The message parameters. Input/output parameter. |
||
| 113 | */ |
||
| 114 | void eth_receiver( ipc_callid_t iid, ipc_call_t * icall ); |
||
| 115 | |||
| 116 | DEVICE_MAP_IMPLEMENT( eth_devices, eth_device_t ) |
||
| 117 | |||
| 118 | INT_MAP_IMPLEMENT( eth_protos, eth_proto_t ) |
||
| 119 | |||
| 120 | int eth_device_message( device_id_t device_id, services_t service, size_t mtu ); |
||
| 121 | 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 ); |
||
| 123 | 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 ); |
||
| 125 | 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 ); |
||
| 4192 | mejdrech | 128 | eth_proto_ref eth_process_packet( int dummy, packet_t packet ); |
| 4153 | mejdrech | 129 | int eth_prepare_packet( int dummy, packet_t packet, uint8_t * src_addr, int ethertype ); |
| 3992 | mejdrech | 130 | |
| 131 | int eth_initialize( void ){ |
||
| 132 | ERROR_DECLARE; |
||
| 133 | |||
| 134 | rwlock_initialize( & eth_globals.devices_lock ); |
||
| 135 | rwlock_initialize( & eth_globals.protos_lock ); |
||
| 136 | rwlock_write_lock( & eth_globals.devices_lock ); |
||
| 137 | rwlock_write_lock( & eth_globals.protos_lock ); |
||
| 138 | eth_globals.broadcast_addr = measured_string_create_bulk( "\xFF\xFF\xFF\xFF\xFF\xFF", CONVERT_SIZE( uint8_t, char, ETH_ADDR )); |
||
| 139 | if( ! eth_globals.broadcast_addr ) return ENOMEM; |
||
| 140 | ERROR_PROPAGATE( eth_devices_initialize( & eth_globals.devices )); |
||
| 141 | if( ERROR_OCCURRED( eth_protos_initialize( & eth_globals.protos ))){ |
||
| 142 | eth_devices_destroy( & eth_globals.devices ); |
||
| 143 | return ERROR_CODE; |
||
| 144 | } |
||
| 145 | rwlock_write_unlock( & eth_globals.protos_lock ); |
||
| 146 | rwlock_write_unlock( & eth_globals.devices_lock ); |
||
| 147 | return EOK; |
||
| 148 | } |
||
| 149 | |||
| 150 | int eth_device_message( device_id_t device_id, services_t service, size_t mtu ){ |
||
| 151 | ERROR_DECLARE; |
||
| 152 | |||
| 153 | aid_t message; |
||
| 154 | ipc_call_t answer; |
||
| 155 | eth_device_ref device; |
||
| 4192 | mejdrech | 156 | ipcarg_t result; |
| 157 | int index; |
||
| 3992 | mejdrech | 158 | |
| 159 | rwlock_write_lock( & eth_globals.devices_lock ); |
||
| 160 | // an existing device? |
||
| 161 | device = eth_devices_find( & eth_globals.devices, device_id ); |
||
| 162 | if( device ){ |
||
| 4163 | mejdrech | 163 | if( device->service != service ){ |
| 164 | printf( "\nDevice %d already exists", device->device_id ); |
||
| 165 | rwlock_write_unlock( & eth_globals.devices_lock ); |
||
| 166 | return EEXIST; |
||
| 167 | }else{ |
||
| 3992 | mejdrech | 168 | // update mtu |
| 169 | device->mtu = mtu; |
||
| 4163 | mejdrech | 170 | printf( "\nDevice %d already exists:\tMTU\t= %d", device->device_id, device->mtu ); |
| 3992 | mejdrech | 171 | } |
| 172 | }else{ |
||
| 173 | // create a new device |
||
| 174 | device = ( eth_device_ref ) malloc( sizeof( eth_device_t )); |
||
| 175 | if( ! device ) return ENOMEM; |
||
| 176 | device->device_id = device_id; |
||
| 177 | device->service = service; |
||
| 4163 | mejdrech | 178 | device->mtu = ( mtu > 0 ) ? mtu : ETH_MAX_CONTENT; |
| 4153 | mejdrech | 179 | // TODO get dummy setting |
| 180 | device->dummy = 0; |
||
| 3992 | mejdrech | 181 | // bind the device driver |
| 182 | device->phone = bind_service( device->service, device->device_id, SERVICE_ETHERNET, 0, eth_receiver ); |
||
| 4163 | mejdrech | 183 | if( device->phone < 0 ){ |
| 184 | rwlock_write_unlock( & eth_globals.devices_lock ); |
||
| 185 | free( device ); |
||
| 186 | return device->phone; |
||
| 187 | } |
||
| 3992 | mejdrech | 188 | // 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 ))){ |
||
| 191 | rwlock_write_unlock( & eth_globals.devices_lock ); |
||
| 192 | free( device ); |
||
| 193 | async_wait_for( message, NULL ); |
||
| 194 | return ERROR_CODE; |
||
| 195 | } |
||
| 4192 | mejdrech | 196 | async_wait_for( message, & result ); |
| 3992 | mejdrech | 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; |
||
| 203 | } |
||
| 204 | // add to the cache |
||
| 4192 | mejdrech | 205 | index = eth_devices_add( & eth_globals.devices, device->device_id, device ); |
| 206 | if( index < 0 ){ |
||
| 3992 | mejdrech | 207 | rwlock_write_unlock( & eth_globals.devices_lock ); |
| 208 | free( device->addr ); |
||
| 209 | free( device->addr_data ); |
||
| 210 | free( device ); |
||
| 4192 | mejdrech | 211 | return index; |
| 3992 | mejdrech | 212 | } |
| 4163 | mejdrech | 213 | 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 ] ); |
| 3992 | mejdrech | 214 | } |
| 215 | rwlock_write_unlock( & eth_globals.devices_lock ); |
||
| 216 | return EOK; |
||
| 217 | } |
||
| 218 | |||
| 4192 | mejdrech | 219 | eth_proto_ref eth_process_packet( int dummy, packet_t packet ){ |
| 3992 | mejdrech | 220 | ERROR_DECLARE; |
| 221 | |||
| 222 | eth_header_ex_ref header; |
||
| 223 | size_t length; |
||
| 224 | int type; |
||
| 4075 | mejdrech | 225 | size_t prefix; |
| 226 | size_t suffix; |
||
| 227 | eth_fcs_ref fcs; |
||
| 3992 | mejdrech | 228 | |
| 229 | length = packet_get_data_length( packet ); |
||
| 4153 | mejdrech | 230 | if( dummy ){ |
| 231 | packet_trim( packet, sizeof( eth_preamble_t ), 0 ); |
||
| 232 | } |
||
| 4075 | mejdrech | 233 | if( length <= sizeof( eth_header_t ) + ETH_MIN_CONTENT + ETH_SUFFIX ) return NULL; |
| 3992 | mejdrech | 234 | header = ( eth_header_ex_ref ) packet_get_data( packet ); |
| 235 | type = ntohs( header->header.ethertype ); |
||
| 236 | if( type >= ETH_MIN_PROTO ){ |
||
| 237 | // DIX Ethernet |
||
| 4075 | mejdrech | 238 | prefix = sizeof( eth_header_t ); |
| 239 | suffix = sizeof( eth_fcs_t ); |
||
| 240 | fcs = (( void * ) header ) + length - suffix; |
||
| 241 | }else if( type <= ETH_MAX_CONTENT ){ |
||
| 3992 | mejdrech | 242 | // translate "LSAP" values |
| 4075 | mejdrech | 243 | if(( header->lsap.dsap == ETH_LSAP_GLSAP ) && ( header->lsap.ssap == ETH_LSAP_GLSAP )){ |
| 3992 | mejdrech | 244 | // raw packet |
| 245 | // discard |
||
| 4075 | mejdrech | 246 | return NULL; |
| 3992 | mejdrech | 247 | }else if(( header->lsap.dsap == ETH_LSAP_SNAP ) && ( header->lsap.ssap == ETH_LSAP_SNAP )){ |
| 4075 | mejdrech | 248 | // IEEE 802.3 + 802.2 + LSAP + SNAP |
| 3992 | mejdrech | 249 | // organization code not supported |
| 250 | type = ntohs( header->snap.ethertype ); |
||
| 4075 | mejdrech | 251 | prefix = sizeof( eth_header_t ) + sizeof( eth_header_lsap_t) + sizeof( eth_header_snap_t); |
| 3992 | mejdrech | 252 | }else{ |
| 253 | // IEEE 802.3 + 802.2 LSAP |
||
| 4075 | mejdrech | 254 | type = lsap_map( header->lsap.dsap ); |
| 255 | prefix = sizeof( eth_header_t ) + sizeof( eth_header_lsap_t); |
||
| 3992 | mejdrech | 256 | } |
| 4075 | mejdrech | 257 | suffix = ( type < ETH_MIN_CONTENT ) ? ETH_MIN_CONTENT - type : 0; |
| 258 | fcs = (( void * ) header ) + prefix + type + suffix; |
||
| 259 | suffix += length - prefix - type; |
||
| 260 | }else{ |
||
| 261 | // invalid length/type, should not occurr |
||
| 262 | return NULL; |
||
| 3992 | mejdrech | 263 | } |
| 4153 | mejdrech | 264 | if( dummy ){ |
| 265 | if(( ~ compute_crc32( ~ 0, & header->header.dest, ((( void * ) fcs ) - (( void * ) & header->header.dest )) * 8 )) != ntohl( * fcs )){ |
||
| 266 | return NULL; |
||
| 267 | } |
||
| 3992 | mejdrech | 268 | } |
| 269 | if( ERROR_OCCURRED( packet_set_addr( packet, header->header.src, header->header.dest, ETH_ADDR )) |
||
| 4075 | mejdrech | 270 | || ERROR_OCCURRED( packet_trim( packet, prefix, suffix ))){ |
| 271 | return NULL; |
||
| 3992 | mejdrech | 272 | } |
| 4075 | mejdrech | 273 | return eth_protos_find( & eth_globals.protos, type ); |
| 274 | } |
||
| 275 | |||
| 276 | int eth_receive_message( device_id_t device_id, packet_t packet ){ |
||
| 277 | eth_proto_ref proto; |
||
| 278 | packet_t next; |
||
| 4153 | mejdrech | 279 | eth_device_ref device; |
| 280 | int dummy; |
||
| 4075 | mejdrech | 281 | |
| 4153 | mejdrech | 282 | rwlock_read_lock( & eth_globals.devices_lock ); |
| 283 | device = eth_devices_find( & eth_globals.devices, device_id ); |
||
| 284 | if( ! device ){ |
||
| 285 | rwlock_read_unlock( & eth_globals.devices_lock ); |
||
| 286 | return ENOENT; |
||
| 287 | } |
||
| 288 | dummy = device->dummy; |
||
| 289 | rwlock_read_unlock( & eth_globals.devices_lock ); |
||
| 4075 | mejdrech | 290 | rwlock_read_lock( & eth_globals.protos_lock ); |
| 291 | do{ |
||
| 292 | next = pq_detach( packet ); |
||
| 4192 | mejdrech | 293 | proto = eth_process_packet( dummy, packet ); |
| 4075 | mejdrech | 294 | if( proto ){ |
| 295 | async_msg_2( proto->phone, NET_IL_RECEIVED, device_id, packet_get_id( packet )); |
||
| 296 | }else{ |
||
| 297 | // drop invalid/unknown |
||
| 4192 | mejdrech | 298 | pq_release( eth_globals.networking_phone, packet_get_id( packet )); |
| 4075 | mejdrech | 299 | } |
| 300 | packet = next; |
||
| 301 | }while( packet ); |
||
| 302 | rwlock_read_unlock( & eth_globals.protos_lock ); |
||
| 3992 | mejdrech | 303 | return EOK; |
| 304 | } |
||
| 305 | |||
| 306 | int eth_packet_space_message( device_id_t device_id, size_t * addr_len, size_t * prefix, size_t * content, size_t * suffix ){ |
||
| 307 | eth_device_ref device; |
||
| 308 | |||
| 309 | if( !( addr_len && prefix && content && suffix )) return EINVAL; |
||
| 4192 | mejdrech | 310 | rwlock_read_lock( & eth_globals.devices_lock ); |
| 3992 | mejdrech | 311 | device = eth_devices_find( & eth_globals.devices, device_id ); |
| 312 | if( ! device ){ |
||
| 4192 | mejdrech | 313 | rwlock_read_unlock( & eth_globals.devices_lock ); |
| 3992 | mejdrech | 314 | return ENOENT; |
| 315 | } |
||
| 4163 | mejdrech | 316 | * content = ( ETH_MAX_CONTENT > device->mtu ) ? device->mtu : ETH_MAX_CONTENT; |
| 4192 | mejdrech | 317 | rwlock_read_unlock( & eth_globals.devices_lock ); |
| 3992 | mejdrech | 318 | * addr_len = ETH_ADDR; |
| 319 | * prefix = ETH_PREFIX; |
||
| 4075 | mejdrech | 320 | * suffix = ETH_MIN_CONTENT + ETH_SUFFIX; |
| 3992 | mejdrech | 321 | return EOK; |
| 322 | } |
||
| 323 | |||
| 324 | int eth_addr_message( device_id_t device_id, eth_addr_type_t type, measured_string_ref * address ){ |
||
| 325 | eth_device_ref device; |
||
| 326 | |||
| 327 | if( ! address ) return EINVAL; |
||
| 328 | if( type == ETH_BROADCAST_ADDR ){ |
||
| 329 | * address = eth_globals.broadcast_addr; |
||
| 330 | }else{ |
||
| 4192 | mejdrech | 331 | rwlock_read_lock( & eth_globals.devices_lock ); |
| 3992 | mejdrech | 332 | device = eth_devices_find( & eth_globals.devices, device_id ); |
| 333 | if( ! device ){ |
||
| 4192 | mejdrech | 334 | rwlock_read_unlock( & eth_globals.devices_lock ); |
| 3992 | mejdrech | 335 | return ENOENT; |
| 336 | } |
||
| 337 | * address = device->addr; |
||
| 4192 | mejdrech | 338 | rwlock_read_unlock( & eth_globals.devices_lock ); |
| 3992 | mejdrech | 339 | } |
| 340 | return ( * address ) ? EOK : ENOENT; |
||
| 341 | } |
||
| 342 | |||
| 343 | int eth_register_message( services_t service, int phone ){ |
||
| 344 | eth_proto_ref proto; |
||
| 345 | int protocol; |
||
| 4192 | mejdrech | 346 | int index; |
| 3992 | mejdrech | 347 | |
| 348 | protocol = protocol_map( SERVICE_ETHERNET, service ); |
||
| 349 | if( ! protocol ) return ENOENT; |
||
| 350 | rwlock_write_lock( & eth_globals.protos_lock ); |
||
| 351 | proto = eth_protos_find( & eth_globals.protos, protocol ); |
||
| 352 | if( proto ){ |
||
| 353 | proto->phone = phone; |
||
| 354 | rwlock_write_unlock( & eth_globals.protos_lock ); |
||
| 355 | return EOK; |
||
| 356 | }else{ |
||
| 357 | proto = ( eth_proto_ref ) malloc( sizeof( eth_proto_t )); |
||
| 358 | if( ! proto ){ |
||
| 359 | rwlock_write_unlock( & eth_globals.protos_lock ); |
||
| 360 | return ENOMEM; |
||
| 361 | } |
||
| 362 | proto->service = service; |
||
| 363 | proto->protocol = protocol; |
||
| 364 | proto->phone = phone; |
||
| 4192 | mejdrech | 365 | index = eth_protos_add( & eth_globals.protos, protocol, proto ); |
| 366 | if( index < 0 ){ |
||
| 3992 | mejdrech | 367 | rwlock_write_unlock( & eth_globals.protos_lock ); |
| 368 | free( proto ); |
||
| 4192 | mejdrech | 369 | return index; |
| 3992 | mejdrech | 370 | } |
| 371 | } |
||
| 4192 | mejdrech | 372 | printf( "\nNew protocol registered:\n\tprotocol\t= 0x%x\n\tservice\t= %d\n\tphone\t= %d", proto->protocol, proto->service, proto->phone ); |
| 3992 | mejdrech | 373 | rwlock_write_unlock( & eth_globals.protos_lock ); |
| 374 | return EOK; |
||
| 375 | } |
||
| 376 | |||
| 4153 | mejdrech | 377 | int eth_prepare_packet( int dummy, packet_t packet, uint8_t * src_addr, int ethertype ){ |
| 3992 | mejdrech | 378 | eth_header_ex_ref header; |
| 4075 | mejdrech | 379 | eth_fcs_ref fcs; |
| 3992 | mejdrech | 380 | uint8_t * src; |
| 381 | uint8_t * dest; |
||
| 382 | int length; |
||
| 383 | int i; |
||
| 4075 | mejdrech | 384 | void * padding; |
| 4153 | mejdrech | 385 | eth_preamble_ref preamble; |
| 3992 | mejdrech | 386 | |
| 4153 | mejdrech | 387 | if( dummy ){ |
| 388 | preamble = PACKET_PREFIX( packet, eth_preamble_t ); |
||
| 389 | if( ! preamble ) return ENOMEM; |
||
| 390 | for( i = 0; i < 7; ++ i ) preamble->preamble[ i ] = ETH_PREAMBLE; |
||
| 391 | preamble->sfd = ETH_SFD; |
||
| 392 | } |
||
| 3992 | mejdrech | 393 | header = PACKET_PREFIX( packet, eth_header_ex_t ); |
| 394 | 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; |
||
| 4075 | mejdrech | 398 | memcpy( header->header.src, src_addr, ETH_ADDR ); |
| 399 | memcpy( & header->header.dest, dest, ETH_ADDR ); |
||
| 3992 | mejdrech | 400 | length = packet_get_data_length( packet ); |
| 401 | if( length > ETH_MAX_CONTENT ) return EINVAL; |
||
| 402 | if( length < ETH_MIN_CONTENT ){ |
||
| 4075 | mejdrech | 403 | padding = packet_suffix( packet, ETH_MIN_CONTENT - length ); |
| 404 | if( ! padding ) return ENOMEM; |
||
| 4192 | mejdrech | 405 | bzero( padding, ETH_MIN_CONTENT - length ); |
| 3992 | mejdrech | 406 | } |
| 407 | header->header.ethertype = htons( length ); |
||
| 408 | header->lsap.dsap = 0xAA; |
||
| 409 | header->lsap.ssap = header->lsap.dsap; |
||
| 410 | header->lsap.ctrl = 0; |
||
| 411 | for( i = 0; i < 3; ++ i ) header->snap.proto[ i ] = 0; |
||
| 4075 | mejdrech | 412 | header->snap.ethertype = ethertype; |
| 4153 | mejdrech | 413 | if( dummy ){ |
| 414 | fcs = PACKET_SUFFIX( packet, eth_fcs_t ); |
||
| 415 | if( ! fcs ) return ENOMEM; |
||
| 416 | * fcs = htonl( ~ compute_crc32( ~ 0, & header->header.dest, ((( void * ) fcs ) - (( void * ) & header->header.dest )) * 8 )); |
||
| 417 | } |
||
| 4075 | mejdrech | 418 | return EOK; |
| 419 | } |
||
| 420 | |||
| 421 | int eth_send_message( device_id_t device_id, packet_t packet, services_t sender ){ |
||
| 422 | ERROR_DECLARE; |
||
| 423 | |||
| 424 | eth_device_ref device; |
||
| 425 | packet_t next; |
||
| 426 | packet_t tmp; |
||
| 427 | int ethertype; |
||
| 428 | |||
| 429 | ethertype = htons( protocol_map( SERVICE_ETHERNET, sender )); |
||
| 430 | if( ! ethertype ){ |
||
| 4192 | mejdrech | 431 | pq_release( eth_globals.networking_phone, packet_get_id( packet )); |
| 4075 | mejdrech | 432 | return EINVAL; |
| 3992 | mejdrech | 433 | } |
| 4075 | mejdrech | 434 | rwlock_read_lock( & eth_globals.devices_lock ); |
| 435 | device = eth_devices_find( & eth_globals.devices, device_id ); |
||
| 436 | if( ! device ){ |
||
| 437 | rwlock_read_unlock( & eth_globals.devices_lock ); |
||
| 438 | return ENOENT; |
||
| 439 | } |
||
| 4192 | mejdrech | 440 | // process packet queue |
| 4075 | mejdrech | 441 | next = packet; |
| 442 | do{ |
||
| 4153 | mejdrech | 443 | if( ERROR_OCCURRED( eth_prepare_packet( device->dummy, next, ( uint8_t * ) device->addr->value, ethertype ))){ |
| 4075 | mejdrech | 444 | // release invalid packet |
| 445 | tmp = pq_detach( next ); |
||
| 4192 | mejdrech | 446 | pq_release( eth_globals.networking_phone, packet_get_id( next )); |
| 4075 | mejdrech | 447 | next = tmp; |
| 448 | }else{ |
||
| 449 | next = pq_next( next ); |
||
| 450 | } |
||
| 451 | }while( next ); |
||
| 452 | // send packet queue |
||
| 453 | async_msg_2( device->phone, NET_NETIF_SEND, device_id, packet_get_id( packet )); |
||
| 454 | rwlock_read_unlock( & eth_globals.devices_lock ); |
||
| 3992 | mejdrech | 455 | return EOK; |
| 456 | } |
||
| 457 | |||
| 458 | int eth_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){ |
||
| 459 | ERROR_DECLARE; |
||
| 460 | |||
| 461 | measured_string_ref address; |
||
| 462 | packet_t packet; |
||
| 463 | |||
| 464 | * answer_count = 0; |
||
| 465 | switch( IPC_GET_METHOD( * call )){ |
||
| 466 | case IPC_M_PHONE_HUNGUP: |
||
| 467 | return EOK; |
||
| 468 | case NET_NIL_DEVICE: |
||
| 469 | return eth_device_message( IPC_GET_DEVICE( call ), IPC_GET_SERVICE( call ), IPC_GET_MTU( call )); |
||
| 470 | case NET_NIL_SEND: |
||
| 471 | ERROR_PROPAGATE( packet_translate( eth_globals.networking_phone, & packet, IPC_GET_PACKET( call ))); |
||
| 472 | return eth_send_message( IPC_GET_DEVICE( call ), packet, IPC_GET_SERVICE( call )); |
||
| 473 | case NET_NIL_PACKET_SPACE: |
||
| 474 | 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 ))); |
||
| 475 | * answer_count = 3; |
||
| 476 | return EOK; |
||
| 477 | case NET_NIL_ADDR: |
||
| 4192 | mejdrech | 478 | ERROR_PROPAGATE( eth_addr_message( IPC_GET_DEVICE( call ), ETH_LOCAL_ADDR, & address )); |
| 479 | return measured_strings_reply( address, 1 ); |
||
| 3992 | mejdrech | 480 | case NET_NIL_BROADCAST_ADDR: |
| 4192 | mejdrech | 481 | ERROR_PROPAGATE( eth_addr_message( IPC_GET_DEVICE( call ), ETH_BROADCAST_ADDR, & address )); |
| 482 | return measured_strings_reply( address, 1 ); |
||
| 3992 | mejdrech | 483 | return ERROR_CODE; |
| 484 | case IPC_M_CONNECT_TO_ME: |
||
| 485 | return eth_register_message( IPC_GET_PROTO( call ), IPC_GET_PHONE( call )); |
||
| 486 | } |
||
| 487 | return ENOTSUP; |
||
| 488 | } |
||
| 489 | |||
| 490 | void eth_receiver( ipc_callid_t iid, ipc_call_t * icall ){ |
||
| 491 | ERROR_DECLARE; |
||
| 492 | |||
| 493 | packet_t packet; |
||
| 4192 | mejdrech | 494 | int index; |
| 495 | eth_proto_ref proto; |
||
| 3992 | mejdrech | 496 | |
| 497 | while( true ){ |
||
| 498 | switch( IPC_GET_METHOD( * icall )){ |
||
| 499 | case NET_NIL_DEVICE_STATE: |
||
| 500 | //TODO clear device if off? |
||
| 4192 | mejdrech | 501 | rwlock_read_lock( & eth_globals.protos_lock ); |
| 502 | for( index = eth_protos_count( & eth_globals.protos ) - 1; index >= 0; -- index ){ |
||
| 503 | proto = eth_protos_get_index( & eth_globals.protos, index ); |
||
| 504 | if( proto && proto->phone ) async_msg_2( proto->phone, NET_IL_DEVICE_STATE, IPC_GET_DEVICE( icall ), IPC_GET_STATE( icall )); |
||
| 505 | } |
||
| 506 | rwlock_read_unlock( & eth_globals.protos_lock ); |
||
| 507 | ipc_answer_0( iid, EOK ); |
||
| 3992 | mejdrech | 508 | break; |
| 509 | case NET_NIL_RECEIVED: |
||
| 510 | if( ! ERROR_OCCURRED( packet_translate( eth_globals.networking_phone, & packet, IPC_GET_PACKET( icall )))){ |
||
| 511 | ERROR_CODE = eth_receive_message( IPC_GET_DEVICE( icall ), packet ); |
||
| 512 | } |
||
| 513 | ipc_answer_0( iid, ERROR_CODE ); |
||
| 514 | break; |
||
| 515 | default: |
||
| 516 | ipc_answer_0( iid, ENOTSUP ); |
||
| 517 | } |
||
| 518 | iid = async_get_call( icall ); |
||
| 519 | } |
||
| 520 | } |
||
| 521 | |||
| 522 | /** @} |
||
| 523 | */ |