Rev 4243 | Rev 4307 | 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" |
||
| 4243 | mejdrech | 55 | #include "../../include/device.h" |
| 56 | #include "../../include/netif_messages.h" |
||
| 4261 | mejdrech | 57 | #include "../../include/nil_messages.h" |
| 3992 | mejdrech | 58 | |
| 59 | #include "../../structures/measured_strings.h" |
||
| 60 | #include "../../structures/packet/packet_client.h" |
||
| 61 | |||
| 4261 | mejdrech | 62 | #include "../nil_module.h" |
| 63 | #include "../nil_wrappers.h" |
||
| 64 | |||
| 3992 | mejdrech | 65 | #include "eth.h" |
| 66 | #include "eth_header.h" |
||
| 67 | |||
| 68 | #define ETH_PREFIX ( sizeof( eth_header_t ) + sizeof( eth_header_lsap_t ) + sizeof( eth_header_snap_t )) |
||
| 4075 | mejdrech | 69 | #define ETH_SUFFIX sizeof( eth_fcs_t ) |
| 70 | #define ETH_MAX_CONTENT 1500 |
||
| 3992 | mejdrech | 71 | #define ETH_MIN_CONTENT 46 |
| 4243 | mejdrech | 72 | #define ETH_MAX_TAGGED_CONTENT ( ETH_MAX_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 )) |
||
| 3992 | mejdrech | 74 | |
| 75 | typedef enum eth_addr_type eth_addr_type_t; |
||
| 76 | typedef eth_addr_type_t * eth_addr_type_ref; |
||
| 77 | |||
| 78 | enum eth_addr_type{ |
||
| 79 | ETH_LOCAL_ADDR, |
||
| 80 | ETH_BROADCAST_ADDR |
||
| 81 | }; |
||
| 82 | |||
| 83 | /** Ethernet global data. |
||
| 84 | */ |
||
| 85 | eth_globals_t eth_globals; |
||
| 86 | |||
| 87 | /** Processes IPC messages from the registered device driver modules in an infinite loop. |
||
| 88 | * @param iid The message identifier. Input parameter. |
||
| 89 | * @param icall The message parameters. Input/output parameter. |
||
| 90 | */ |
||
| 91 | void eth_receiver( ipc_callid_t iid, ipc_call_t * icall ); |
||
| 92 | |||
| 93 | DEVICE_MAP_IMPLEMENT( eth_devices, eth_device_t ) |
||
| 94 | |||
| 95 | INT_MAP_IMPLEMENT( eth_protos, eth_proto_t ) |
||
| 96 | |||
| 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 ); |
||
| 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_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 ); |
||
| 4192 | mejdrech | 102 | eth_proto_ref eth_process_packet( int dummy, packet_t packet ); |
| 4153 | mejdrech | 103 | int eth_prepare_packet( int dummy, packet_t packet, uint8_t * src_addr, int ethertype ); |
| 3992 | mejdrech | 104 | |
| 4261 | mejdrech | 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 | |||
| 122 | int nil_initialize( int networking_phone ){ |
||
| 3992 | mejdrech | 123 | ERROR_DECLARE; |
| 124 | |||
| 125 | rwlock_initialize( & eth_globals.devices_lock ); |
||
| 126 | rwlock_initialize( & eth_globals.protos_lock ); |
||
| 127 | rwlock_write_lock( & eth_globals.devices_lock ); |
||
| 128 | rwlock_write_lock( & eth_globals.protos_lock ); |
||
| 4261 | mejdrech | 129 | eth_globals.networking_phone = networking_phone; |
| 3992 | mejdrech | 130 | 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; |
||
| 132 | ERROR_PROPAGATE( eth_devices_initialize( & eth_globals.devices )); |
||
| 133 | if( ERROR_OCCURRED( eth_protos_initialize( & eth_globals.protos ))){ |
||
| 134 | eth_devices_destroy( & eth_globals.devices ); |
||
| 135 | return ERROR_CODE; |
||
| 136 | } |
||
| 137 | rwlock_write_unlock( & eth_globals.protos_lock ); |
||
| 138 | rwlock_write_unlock( & eth_globals.devices_lock ); |
||
| 139 | return EOK; |
||
| 140 | } |
||
| 141 | |||
| 142 | int eth_device_message( device_id_t device_id, services_t service, size_t mtu ){ |
||
| 143 | ERROR_DECLARE; |
||
| 144 | |||
| 145 | eth_device_ref device; |
||
| 4192 | mejdrech | 146 | int index; |
| 3992 | mejdrech | 147 | |
| 148 | rwlock_write_lock( & eth_globals.devices_lock ); |
||
| 149 | // an existing device? |
||
| 150 | device = eth_devices_find( & eth_globals.devices, device_id ); |
||
| 151 | if( device ){ |
||
| 4163 | mejdrech | 152 | if( device->service != service ){ |
| 153 | printf( "\nDevice %d already exists", device->device_id ); |
||
| 154 | rwlock_write_unlock( & eth_globals.devices_lock ); |
||
| 155 | return EEXIST; |
||
| 156 | }else{ |
||
| 3992 | mejdrech | 157 | // update mtu |
| 158 | device->mtu = mtu; |
||
| 4163 | mejdrech | 159 | printf( "\nDevice %d already exists:\tMTU\t= %d", device->device_id, device->mtu ); |
| 3992 | mejdrech | 160 | } |
| 161 | }else{ |
||
| 162 | // create a new device |
||
| 163 | device = ( eth_device_ref ) malloc( sizeof( eth_device_t )); |
||
| 164 | if( ! device ) return ENOMEM; |
||
| 165 | device->device_id = device_id; |
||
| 166 | device->service = service; |
||
| 4243 | mejdrech | 167 | device->mtu = (( mtu > 0 ) && ( mtu <= ETH_MAX_TAGGED_CONTENT )) ? mtu : ETH_MAX_TAGGED_CONTENT; |
| 4153 | mejdrech | 168 | // TODO get dummy setting |
| 169 | device->dummy = 0; |
||
| 3992 | mejdrech | 170 | // bind the device driver |
| 4261 | mejdrech | 171 | #if ! NETIF_BUNDLE |
| 3992 | mejdrech | 172 | device->phone = bind_service( device->service, device->device_id, SERVICE_ETHERNET, 0, eth_receiver ); |
| 4163 | mejdrech | 173 | if( device->phone < 0 ){ |
| 174 | rwlock_write_unlock( & eth_globals.devices_lock ); |
||
| 175 | free( device ); |
||
| 176 | return device->phone; |
||
| 177 | } |
||
| 4261 | mejdrech | 178 | #endif |
| 3992 | mejdrech | 179 | // get hardware address |
| 4243 | mejdrech | 180 | if( ERROR_OCCURRED( netif_get_addr( device->phone, device->device_id, & device->addr, & device->addr_data ))){ |
| 3992 | mejdrech | 181 | rwlock_write_unlock( & eth_globals.devices_lock ); |
| 182 | free( device ); |
||
| 183 | return ERROR_CODE; |
||
| 184 | } |
||
| 185 | // add to the cache |
||
| 4192 | mejdrech | 186 | index = eth_devices_add( & eth_globals.devices, device->device_id, device ); |
| 187 | if( index < 0 ){ |
||
| 3992 | mejdrech | 188 | rwlock_write_unlock( & eth_globals.devices_lock ); |
| 189 | free( device->addr ); |
||
| 190 | free( device->addr_data ); |
||
| 191 | free( device ); |
||
| 4192 | mejdrech | 192 | return index; |
| 3992 | mejdrech | 193 | } |
| 4163 | mejdrech | 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 ] ); |
| 3992 | mejdrech | 195 | } |
| 196 | rwlock_write_unlock( & eth_globals.devices_lock ); |
||
| 197 | return EOK; |
||
| 198 | } |
||
| 199 | |||
| 4192 | mejdrech | 200 | eth_proto_ref eth_process_packet( int dummy, packet_t packet ){ |
| 3992 | mejdrech | 201 | ERROR_DECLARE; |
| 202 | |||
| 203 | eth_header_ex_ref header; |
||
| 204 | size_t length; |
||
| 205 | int type; |
||
| 4075 | mejdrech | 206 | size_t prefix; |
| 207 | size_t suffix; |
||
| 208 | eth_fcs_ref fcs; |
||
| 3992 | mejdrech | 209 | |
| 210 | length = packet_get_data_length( packet ); |
||
| 4153 | mejdrech | 211 | if( dummy ){ |
| 212 | packet_trim( packet, sizeof( eth_preamble_t ), 0 ); |
||
| 213 | } |
||
| 4075 | mejdrech | 214 | if( length <= sizeof( eth_header_t ) + ETH_MIN_CONTENT + ETH_SUFFIX ) return NULL; |
| 3992 | mejdrech | 215 | header = ( eth_header_ex_ref ) packet_get_data( packet ); |
| 216 | type = ntohs( header->header.ethertype ); |
||
| 217 | if( type >= ETH_MIN_PROTO ){ |
||
| 218 | // DIX Ethernet |
||
| 4075 | mejdrech | 219 | prefix = sizeof( eth_header_t ); |
| 220 | suffix = sizeof( eth_fcs_t ); |
||
| 221 | fcs = (( void * ) header ) + length - suffix; |
||
| 222 | }else if( type <= ETH_MAX_CONTENT ){ |
||
| 3992 | mejdrech | 223 | // translate "LSAP" values |
| 4075 | mejdrech | 224 | if(( header->lsap.dsap == ETH_LSAP_GLSAP ) && ( header->lsap.ssap == ETH_LSAP_GLSAP )){ |
| 3992 | mejdrech | 225 | // raw packet |
| 226 | // discard |
||
| 4075 | mejdrech | 227 | return NULL; |
| 3992 | mejdrech | 228 | }else if(( header->lsap.dsap == ETH_LSAP_SNAP ) && ( header->lsap.ssap == ETH_LSAP_SNAP )){ |
| 4075 | mejdrech | 229 | // IEEE 802.3 + 802.2 + LSAP + SNAP |
| 3992 | mejdrech | 230 | // organization code not supported |
| 231 | type = ntohs( header->snap.ethertype ); |
||
| 4243 | mejdrech | 232 | prefix = sizeof( eth_header_t ) + sizeof( eth_header_lsap_t ) + sizeof( eth_header_snap_t ); |
| 3992 | mejdrech | 233 | }else{ |
| 234 | // IEEE 802.3 + 802.2 LSAP |
||
| 4075 | mejdrech | 235 | type = lsap_map( header->lsap.dsap ); |
| 236 | prefix = sizeof( eth_header_t ) + sizeof( eth_header_lsap_t); |
||
| 3992 | mejdrech | 237 | } |
| 4075 | mejdrech | 238 | suffix = ( type < ETH_MIN_CONTENT ) ? ETH_MIN_CONTENT - type : 0; |
| 239 | fcs = (( void * ) header ) + prefix + type + suffix; |
||
| 240 | suffix += length - prefix - type; |
||
| 241 | }else{ |
||
| 242 | // invalid length/type, should not occurr |
||
| 243 | return NULL; |
||
| 3992 | mejdrech | 244 | } |
| 4153 | mejdrech | 245 | if( dummy ){ |
| 246 | if(( ~ compute_crc32( ~ 0, & header->header.dest, ((( void * ) fcs ) - (( void * ) & header->header.dest )) * 8 )) != ntohl( * fcs )){ |
||
| 247 | return NULL; |
||
| 248 | } |
||
| 3992 | mejdrech | 249 | } |
| 250 | if( ERROR_OCCURRED( packet_set_addr( packet, header->header.src, header->header.dest, ETH_ADDR )) |
||
| 4075 | mejdrech | 251 | || ERROR_OCCURRED( packet_trim( packet, prefix, suffix ))){ |
| 252 | return NULL; |
||
| 3992 | mejdrech | 253 | } |
| 4075 | mejdrech | 254 | return eth_protos_find( & eth_globals.protos, type ); |
| 255 | } |
||
| 256 | |||
| 257 | int eth_receive_message( device_id_t device_id, packet_t packet ){ |
||
| 258 | eth_proto_ref proto; |
||
| 259 | packet_t next; |
||
| 4153 | mejdrech | 260 | eth_device_ref device; |
| 261 | int dummy; |
||
| 4075 | mejdrech | 262 | |
| 4153 | mejdrech | 263 | rwlock_read_lock( & eth_globals.devices_lock ); |
| 264 | device = eth_devices_find( & eth_globals.devices, device_id ); |
||
| 265 | if( ! device ){ |
||
| 266 | rwlock_read_unlock( & eth_globals.devices_lock ); |
||
| 267 | return ENOENT; |
||
| 268 | } |
||
| 269 | dummy = device->dummy; |
||
| 270 | rwlock_read_unlock( & eth_globals.devices_lock ); |
||
| 4075 | mejdrech | 271 | rwlock_read_lock( & eth_globals.protos_lock ); |
| 272 | do{ |
||
| 273 | next = pq_detach( packet ); |
||
| 4192 | mejdrech | 274 | proto = eth_process_packet( dummy, packet ); |
| 4075 | mejdrech | 275 | if( proto ){ |
| 4261 | mejdrech | 276 | async_msg_3( proto->phone, NET_IL_RECEIVED, device_id, packet_get_id( packet ), proto->service ); |
| 4075 | mejdrech | 277 | }else{ |
| 278 | // drop invalid/unknown |
||
| 4192 | mejdrech | 279 | pq_release( eth_globals.networking_phone, packet_get_id( packet )); |
| 4075 | mejdrech | 280 | } |
| 281 | packet = next; |
||
| 282 | }while( packet ); |
||
| 283 | rwlock_read_unlock( & eth_globals.protos_lock ); |
||
| 3992 | mejdrech | 284 | return EOK; |
| 285 | } |
||
| 286 | |||
| 287 | int eth_packet_space_message( device_id_t device_id, size_t * addr_len, size_t * prefix, size_t * content, size_t * suffix ){ |
||
| 288 | eth_device_ref device; |
||
| 289 | |||
| 290 | if( !( addr_len && prefix && content && suffix )) return EINVAL; |
||
| 4192 | mejdrech | 291 | rwlock_read_lock( & eth_globals.devices_lock ); |
| 3992 | mejdrech | 292 | device = eth_devices_find( & eth_globals.devices, device_id ); |
| 293 | if( ! device ){ |
||
| 4192 | mejdrech | 294 | rwlock_read_unlock( & eth_globals.devices_lock ); |
| 3992 | mejdrech | 295 | return ENOENT; |
| 296 | } |
||
| 4243 | mejdrech | 297 | * content = device->mtu; |
| 4192 | mejdrech | 298 | rwlock_read_unlock( & eth_globals.devices_lock ); |
| 3992 | mejdrech | 299 | * addr_len = ETH_ADDR; |
| 300 | * prefix = ETH_PREFIX; |
||
| 4075 | mejdrech | 301 | * suffix = ETH_MIN_CONTENT + ETH_SUFFIX; |
| 3992 | mejdrech | 302 | return EOK; |
| 303 | } |
||
| 304 | |||
| 305 | int eth_addr_message( device_id_t device_id, eth_addr_type_t type, measured_string_ref * address ){ |
||
| 306 | eth_device_ref device; |
||
| 307 | |||
| 308 | if( ! address ) return EINVAL; |
||
| 309 | if( type == ETH_BROADCAST_ADDR ){ |
||
| 310 | * address = eth_globals.broadcast_addr; |
||
| 311 | }else{ |
||
| 4192 | mejdrech | 312 | rwlock_read_lock( & eth_globals.devices_lock ); |
| 3992 | mejdrech | 313 | device = eth_devices_find( & eth_globals.devices, device_id ); |
| 314 | if( ! device ){ |
||
| 4192 | mejdrech | 315 | rwlock_read_unlock( & eth_globals.devices_lock ); |
| 3992 | mejdrech | 316 | return ENOENT; |
| 317 | } |
||
| 318 | * address = device->addr; |
||
| 4192 | mejdrech | 319 | rwlock_read_unlock( & eth_globals.devices_lock ); |
| 3992 | mejdrech | 320 | } |
| 321 | return ( * address ) ? EOK : ENOENT; |
||
| 322 | } |
||
| 323 | |||
| 4261 | mejdrech | 324 | int nil_register_message( services_t service, int phone ){ |
| 3992 | mejdrech | 325 | eth_proto_ref proto; |
| 326 | int protocol; |
||
| 4192 | mejdrech | 327 | int index; |
| 3992 | mejdrech | 328 | |
| 329 | protocol = protocol_map( SERVICE_ETHERNET, service ); |
||
| 330 | if( ! protocol ) return ENOENT; |
||
| 331 | rwlock_write_lock( & eth_globals.protos_lock ); |
||
| 332 | proto = eth_protos_find( & eth_globals.protos, protocol ); |
||
| 333 | if( proto ){ |
||
| 334 | proto->phone = phone; |
||
| 335 | rwlock_write_unlock( & eth_globals.protos_lock ); |
||
| 336 | return EOK; |
||
| 337 | }else{ |
||
| 338 | proto = ( eth_proto_ref ) malloc( sizeof( eth_proto_t )); |
||
| 339 | if( ! proto ){ |
||
| 340 | rwlock_write_unlock( & eth_globals.protos_lock ); |
||
| 341 | return ENOMEM; |
||
| 342 | } |
||
| 343 | proto->service = service; |
||
| 344 | proto->protocol = protocol; |
||
| 345 | proto->phone = phone; |
||
| 4192 | mejdrech | 346 | index = eth_protos_add( & eth_globals.protos, protocol, proto ); |
| 347 | if( index < 0 ){ |
||
| 3992 | mejdrech | 348 | rwlock_write_unlock( & eth_globals.protos_lock ); |
| 349 | free( proto ); |
||
| 4192 | mejdrech | 350 | return index; |
| 3992 | mejdrech | 351 | } |
| 352 | } |
||
| 4192 | mejdrech | 353 | 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 | 354 | rwlock_write_unlock( & eth_globals.protos_lock ); |
| 355 | return EOK; |
||
| 356 | } |
||
| 357 | |||
| 4153 | mejdrech | 358 | int eth_prepare_packet( int dummy, packet_t packet, uint8_t * src_addr, int ethertype ){ |
| 3992 | mejdrech | 359 | eth_header_ex_ref header; |
| 4075 | mejdrech | 360 | eth_fcs_ref fcs; |
| 3992 | mejdrech | 361 | uint8_t * src; |
| 362 | uint8_t * dest; |
||
| 363 | int length; |
||
| 364 | int i; |
||
| 4075 | mejdrech | 365 | void * padding; |
| 4153 | mejdrech | 366 | eth_preamble_ref preamble; |
| 3992 | mejdrech | 367 | |
| 4243 | mejdrech | 368 | length = packet_get_data_length( packet ); |
| 369 | if( length > ETH_MAX_TAGGED_CONTENT ) return EINVAL; |
||
| 370 | if( length < ETH_MIN_TAGGED_CONTENT ){ |
||
| 371 | padding = packet_suffix( packet, ETH_MIN_TAGGED_CONTENT - length ); |
||
| 372 | if( ! padding ) return ENOMEM; |
||
| 373 | bzero( padding, ETH_MIN_TAGGED_CONTENT - length ); |
||
| 374 | } |
||
| 4153 | mejdrech | 375 | if( dummy ){ |
| 376 | preamble = PACKET_PREFIX( packet, eth_preamble_t ); |
||
| 377 | if( ! preamble ) return ENOMEM; |
||
| 378 | for( i = 0; i < 7; ++ i ) preamble->preamble[ i ] = ETH_PREAMBLE; |
||
| 379 | preamble->sfd = ETH_SFD; |
||
| 380 | } |
||
| 3992 | mejdrech | 381 | header = PACKET_PREFIX( packet, eth_header_ex_t ); |
| 382 | if( ! header ) return ENOMEM; |
||
| 4243 | mejdrech | 383 | header->header.ethertype = htons( length + sizeof( eth_header_lsap_t ) + sizeof( eth_header_snap_t )); |
| 4261 | mejdrech | 384 | header->lsap.dsap = ( uint16_t ) ETH_LSAP_SNAP; |
| 3992 | mejdrech | 385 | header->lsap.ssap = header->lsap.dsap; |
| 386 | header->lsap.ctrl = 0; |
||
| 387 | for( i = 0; i < 3; ++ i ) header->snap.proto[ i ] = 0; |
||
| 4261 | mejdrech | 388 | header->snap.ethertype = ( uint16_t ) ethertype; |
| 4243 | mejdrech | 389 | length = packet_get_addr( packet, & src, & dest ); |
| 390 | if( length < 0 ) return length; |
||
| 391 | if( length < ETH_ADDR ) return EINVAL; |
||
| 392 | memcpy( header->header.src, src_addr, ETH_ADDR ); |
||
| 393 | memcpy( header->header.dest, dest, ETH_ADDR ); |
||
| 4153 | mejdrech | 394 | if( dummy ){ |
| 395 | fcs = PACKET_SUFFIX( packet, eth_fcs_t ); |
||
| 396 | if( ! fcs ) return ENOMEM; |
||
| 397 | * fcs = htonl( ~ compute_crc32( ~ 0, & header->header.dest, ((( void * ) fcs ) - (( void * ) & header->header.dest )) * 8 )); |
||
| 398 | } |
||
| 4075 | mejdrech | 399 | return EOK; |
| 400 | } |
||
| 401 | |||
| 402 | int eth_send_message( device_id_t device_id, packet_t packet, services_t sender ){ |
||
| 403 | ERROR_DECLARE; |
||
| 404 | |||
| 405 | eth_device_ref device; |
||
| 406 | packet_t next; |
||
| 407 | packet_t tmp; |
||
| 408 | int ethertype; |
||
| 409 | |||
| 410 | ethertype = htons( protocol_map( SERVICE_ETHERNET, sender )); |
||
| 411 | if( ! ethertype ){ |
||
| 4192 | mejdrech | 412 | pq_release( eth_globals.networking_phone, packet_get_id( packet )); |
| 4075 | mejdrech | 413 | return EINVAL; |
| 3992 | mejdrech | 414 | } |
| 4075 | mejdrech | 415 | rwlock_read_lock( & eth_globals.devices_lock ); |
| 416 | device = eth_devices_find( & eth_globals.devices, device_id ); |
||
| 417 | if( ! device ){ |
||
| 418 | rwlock_read_unlock( & eth_globals.devices_lock ); |
||
| 419 | return ENOENT; |
||
| 420 | } |
||
| 4192 | mejdrech | 421 | // process packet queue |
| 4075 | mejdrech | 422 | next = packet; |
| 423 | do{ |
||
| 4153 | mejdrech | 424 | if( ERROR_OCCURRED( eth_prepare_packet( device->dummy, next, ( uint8_t * ) device->addr->value, ethertype ))){ |
| 4075 | mejdrech | 425 | // release invalid packet |
| 426 | tmp = pq_detach( next ); |
||
| 4192 | mejdrech | 427 | pq_release( eth_globals.networking_phone, packet_get_id( next )); |
| 4075 | mejdrech | 428 | next = tmp; |
| 429 | }else{ |
||
| 430 | next = pq_next( next ); |
||
| 431 | } |
||
| 432 | }while( next ); |
||
| 433 | // send packet queue |
||
| 4261 | mejdrech | 434 | netif_send_msg( device->phone, device_id, packet, SERVICE_ETHERNET ); |
| 4075 | mejdrech | 435 | rwlock_read_unlock( & eth_globals.devices_lock ); |
| 3992 | mejdrech | 436 | return EOK; |
| 437 | } |
||
| 438 | |||
| 4261 | mejdrech | 439 | int nil_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){ |
| 3992 | mejdrech | 440 | ERROR_DECLARE; |
| 441 | |||
| 442 | measured_string_ref address; |
||
| 443 | packet_t packet; |
||
| 444 | |||
| 4243 | mejdrech | 445 | // printf( "\nmessage %d - %d", IPC_GET_METHOD( * call ), NET_NIL_FIRST ); |
| 3992 | mejdrech | 446 | * answer_count = 0; |
| 447 | switch( IPC_GET_METHOD( * call )){ |
||
| 448 | case IPC_M_PHONE_HUNGUP: |
||
| 449 | return EOK; |
||
| 450 | case NET_NIL_DEVICE: |
||
| 4261 | mejdrech | 451 | return eth_device_message( IPC_GET_DEVICE( call ), IPC_GET_SERVICE( call ), NIL_GET_MTU( call )); |
| 3992 | mejdrech | 452 | case NET_NIL_SEND: |
| 453 | ERROR_PROPAGATE( packet_translate( eth_globals.networking_phone, & packet, IPC_GET_PACKET( call ))); |
||
| 454 | return eth_send_message( IPC_GET_DEVICE( call ), packet, IPC_GET_SERVICE( call )); |
||
| 455 | 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 ))); |
||
| 457 | * answer_count = 3; |
||
| 458 | return EOK; |
||
| 459 | case NET_NIL_ADDR: |
||
| 4192 | mejdrech | 460 | ERROR_PROPAGATE( eth_addr_message( IPC_GET_DEVICE( call ), ETH_LOCAL_ADDR, & address )); |
| 461 | return measured_strings_reply( address, 1 ); |
||
| 3992 | mejdrech | 462 | case NET_NIL_BROADCAST_ADDR: |
| 4192 | mejdrech | 463 | ERROR_PROPAGATE( eth_addr_message( IPC_GET_DEVICE( call ), ETH_BROADCAST_ADDR, & address )); |
| 464 | return measured_strings_reply( address, 1 ); |
||
| 3992 | mejdrech | 465 | case IPC_M_CONNECT_TO_ME: |
| 4261 | mejdrech | 466 | return nil_register_message( NIL_GET_PROTO( call ), IPC_GET_PHONE( call )); |
| 3992 | mejdrech | 467 | } |
| 468 | return ENOTSUP; |
||
| 469 | } |
||
| 470 | |||
| 471 | void eth_receiver( ipc_callid_t iid, ipc_call_t * icall ){ |
||
| 472 | ERROR_DECLARE; |
||
| 473 | |||
| 474 | packet_t packet; |
||
| 475 | |||
| 476 | while( true ){ |
||
| 477 | switch( IPC_GET_METHOD( * icall )){ |
||
| 478 | case NET_NIL_DEVICE_STATE: |
||
| 4261 | mejdrech | 479 | nil_device_state_wrapper( IPC_GET_DEVICE( icall ), IPC_GET_STATE( icall )); |
| 4192 | mejdrech | 480 | ipc_answer_0( iid, EOK ); |
| 3992 | mejdrech | 481 | break; |
| 482 | case NET_NIL_RECEIVED: |
||
| 483 | if( ! ERROR_OCCURRED( packet_translate( eth_globals.networking_phone, & packet, IPC_GET_PACKET( icall )))){ |
||
| 4261 | mejdrech | 484 | ERROR_CODE = nil_receive_wrapper( IPC_GET_DEVICE( icall ), packet ); |
| 3992 | mejdrech | 485 | } |
| 486 | ipc_answer_0( iid, ERROR_CODE ); |
||
| 487 | break; |
||
| 488 | default: |
||
| 489 | ipc_answer_0( iid, ENOTSUP ); |
||
| 490 | } |
||
| 491 | iid = async_get_call( icall ); |
||
| 492 | } |
||
| 493 | } |
||
| 494 | |||
| 495 | /** @} |
||
| 496 | */ |