Subversion Repositories HelenOS

Rev

Rev 4350 | Rev 4505 | 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"
4307 mejdrech 56
#include "../../include/netif_interface.h"
4332 mejdrech 57
#include "../../include/net_interface.h"
4307 mejdrech 58
#include "../../include/nil_interface.h"
59
#include "../../include/il_interface.h"
3992 mejdrech 60
 
61
#include "../../structures/measured_strings.h"
62
#include "../../structures/packet/packet_client.h"
63
 
4261 mejdrech 64
#include "../nil_module.h"
65
 
3992 mejdrech 66
#include "eth.h"
67
#include "eth_header.h"
68
 
4350 mejdrech 69
/** Reserved packet prefix length.
70
 */
3992 mejdrech 71
#define ETH_PREFIX      ( sizeof( eth_header_t ) + sizeof( eth_header_lsap_t ) + sizeof( eth_header_snap_t ))
4350 mejdrech 72
 
73
/** Reserved packet suffix length.
74
 */
4075 mejdrech 75
#define ETH_SUFFIX      sizeof( eth_fcs_t )
4350 mejdrech 76
 
77
/** Maximum packet content length.
78
 */
4075 mejdrech 79
#define ETH_MAX_CONTENT 1500
4350 mejdrech 80
 
81
/** Minimum packet content length.
82
 */
3992 mejdrech 83
#define ETH_MIN_CONTENT 46
4350 mejdrech 84
 
85
/** Maximum tagged packet content length.
86
 */
4332 mejdrech 87
#define ETH_MAX_TAGGED_CONTENT( flags ) ( ETH_MAX_CONTENT - (( IS_8023_2_LSAP( flags ) || IS_8023_2_SNAP( flags )) ? sizeof( eth_header_lsap_t ) : 0 ) - ( IS_8023_2_SNAP( flags ) ? sizeof( eth_header_snap_t ) : 0 ))
4350 mejdrech 88
 
89
/** Minimum tagged packet content length.
90
 */
4332 mejdrech 91
#define ETH_MIN_TAGGED_CONTENT( flags ) ( ETH_MIN_CONTENT - (( IS_8023_2_LSAP( flags ) || IS_8023_2_SNAP( flags )) ? sizeof( eth_header_lsap_t ) : 0 ) - ( IS_8023_2_SNAP( flags ) ? sizeof( eth_header_snap_t ) : 0 ))
3992 mejdrech 92
 
4350 mejdrech 93
/** Dummy flag shift value.
94
 */
4332 mejdrech 95
#define ETH_DUMMY_SHIFT 0
4350 mejdrech 96
 
97
/** Mode flag shift value.
98
 */
4332 mejdrech 99
#define ETH_MODE_SHIFT  1
100
 
101
/** Dummy device flag.
102
 *  Preamble and FCS are mandatory part of the packets.
103
 */
104
#define ETH_DUMMY               ( 1 << ETH_DUMMY_SHIFT )
4350 mejdrech 105
 
106
/** Returns the dummy flag.
107
 *  @see ETH_DUMMY
108
 */
4332 mejdrech 109
#define IS_DUMMY( flags )       (( flags ) & ETH_DUMMY )
110
 
111
/** Device mode flags.
112
 *  @see ETH_DIX
113
 *  @see ETH_8023_2_LSAP
114
 *  @see ETH_8023_2_SNAP
115
 */
116
#define ETH_MODE_MASK           ( 3 << ETH_MODE_SHIFT )
4350 mejdrech 117
 
118
/** DIX Ethernet mode flag.
119
 */
4332 mejdrech 120
#define ETH_DIX                 ( 1 << ETH_MODE_SHIFT )
4350 mejdrech 121
 
122
/** Returns whether the DIX Ethernet mode flag is set.
123
 *  @param flags The ethernet flags. Input parameter.
124
 *  @see ETH_DIX
125
 */
4332 mejdrech 126
#define IS_DIX( flags )         ((( flags ) & ETH_MODE_MASK ) == ETH_DIX )
4350 mejdrech 127
 
128
/** 802.3 + 802.2 + LSAP mode flag.
129
 */
4332 mejdrech 130
#define ETH_8023_2_LSAP         ( 2 << ETH_MODE_SHIFT )
4350 mejdrech 131
 
132
/** Returns whether the 802.3 + 802.2 + LSAP mode flag is set.
133
 *  @param flags The ethernet flags. Input parameter.
134
 *  @see ETH_8023_2_LSAP
135
 */
4332 mejdrech 136
#define IS_8023_2_LSAP( flags ) ((( flags ) & ETH_MODE_MASK ) == ETH_8023_2_LSAP )
4350 mejdrech 137
 
138
/** 802.3 + 802.2 + LSAP + SNAP mode flag.
139
 */
4332 mejdrech 140
#define ETH_8023_2_SNAP         ( 3 << ETH_MODE_SHIFT )
4350 mejdrech 141
 
142
/** Returns whether the 802.3 + 802.2 + LSAP + SNAP mode flag is set.
143
 *  @param flags The ethernet flags. Input parameter.
144
 *  @see ETH_8023_2_SNAP
145
 */
4332 mejdrech 146
#define IS_8023_2_SNAP( flags ) ((( flags ) & ETH_MODE_MASK ) == ETH_8023_2_SNAP )
147
 
4350 mejdrech 148
/** Type definition of the ethernet address type.
149
 *  @see eth_addr_type
150
 */
3992 mejdrech 151
typedef enum eth_addr_type  eth_addr_type_t;
4350 mejdrech 152
 
153
/** Type definition of the ethernet address type pointer.
154
 *  @see eth_addr_type
155
 */
3992 mejdrech 156
typedef eth_addr_type_t *   eth_addr_type_ref;
157
 
4350 mejdrech 158
/** Ethernet address type.
159
 */
3992 mejdrech 160
enum eth_addr_type{
4350 mejdrech 161
    /** Local address.
162
     */
3992 mejdrech 163
    ETH_LOCAL_ADDR,
4350 mejdrech 164
    /** Broadcast address.
165
     */
3992 mejdrech 166
    ETH_BROADCAST_ADDR
167
};
168
 
4350 mejdrech 169
/** Ethernet module global data.
3992 mejdrech 170
 */
171
eth_globals_t   eth_globals;
172
 
173
/** Processes IPC messages from the registered device driver modules in an infinite loop.
174
 *  @param iid The message identifier. Input parameter.
175
 *  @param icall The message parameters. Input/output parameter.
176
 */
177
void    eth_receiver( ipc_callid_t iid, ipc_call_t * icall );
178
 
4350 mejdrech 179
/** Registers new device or updates the MTU of an existing one.
180
 *  Determines the device local hardware address.
181
 *  @param device_id The new device identifier. Input parameter.
182
 *  @param service The device driver service. Input parameter.
183
 *  @param mtu The device maximum transmission unit. Input parameter.
184
 *  @returns EOK on success.
185
 *  @returns EEXIST if the device with the different service exists.
186
 *  @returns ENOMEM if there is not enough memory left.
187
 *  @returns Other error codes as defined for the net_get_device_conf_req() function.
188
 *  @returns Other error codes as defined for the netif_bind_service() function.
189
 *  @returns Other error codes as defined for the netif_get_addr() function.
190
 */
191
int eth_device_message( device_id_t device_id, services_t service, size_t mtu );
3992 mejdrech 192
 
4350 mejdrech 193
/** Registers receiving module service.
194
 *  Passes received packets for this service.
195
 *  @param service The module service. Input parameter.
196
 *  @param phone The service phone. Input parameter.
197
 *  @returns EOK on success.
198
 *  @returns ENOENT if the service is not known.
199
 *  @returns ENOMEM if there is not enough memory left.
200
 */
201
int nil_register_message( services_t service, int phone );
3992 mejdrech 202
 
4350 mejdrech 203
/** Returns the device packet dimensions for sending.
204
 *  @param device_id The device identifier. Input parameter.
205
 *  @param addr_len The minimum reserved address length. Output parameter.
206
 *  @param prefix The minimum reserved prefix size. Output parameter.
207
 *  @param content The maximum content size. Output parameter.
208
 *  @param suffix The minimum reserved suffix size. Output parameter.
209
 *  @returns EOK on success.
210
 *  @returns EBADMEM if either one of the parameters is NULL.
211
 *  @returns ENOENT if there is no such device.
212
 */
3992 mejdrech 213
int eth_packet_space_message( device_id_t device_id, size_t * addr_len, size_t * prefix, size_t * content, size_t * suffix );
4350 mejdrech 214
 
215
/** Returns the device hardware address.
216
 *  @param device_id The device identifier. Input parameter.
217
 *  @param type Type of the desired address. Input parameter
218
 *  @param address The device hardware address. Output parameter.
219
 *  @returns EOK on success.
220
 *  @returns EBADMEM if the address parameter is NULL.
221
 *  @returns ENOENT if there no such device.
222
 */
3992 mejdrech 223
int eth_addr_message( device_id_t device_id, eth_addr_type_t type, measured_string_ref * address );
4350 mejdrech 224
 
225
/** Sends the packet queue.
226
 *  Sends only packet successfully processed by the eth_prepare_packet() function.
227
 *  @param device_id The device identifier. Input parameter.
228
 *  @param packet The packet queue. Input parameter.
229
 *  @param sender The sending module service. Input parameter.
230
 *  @returns EOK on success.
231
 *  @returns ENOENT if there no such device.
232
 *  @returns EINVAL if the service parameter is not known.
233
 */
3992 mejdrech 234
int eth_send_message( device_id_t device_id, packet_t packet, services_t sender );
4350 mejdrech 235
 
236
/** Processes the received packet and chooses the target registered module.
237
 *  @param flags The device flags. Input parameter.
238
 *  @param packet The packet. Input parameter.
239
 *  @returns The target registered module.
240
 *  @returns NULL if the packet is not long enough.
241
 *  @returns NULL if the packet is too long.
242
 *  @returns NULL if the raw ethernet protocol is used.
243
 *  @returns NULL if the dummy device FCS checksum is invalid.
244
 *  @returns NULL if the packet address length is not big enough.
245
 */
4332 mejdrech 246
eth_proto_ref   eth_process_packet( int flags, packet_t packet );
4350 mejdrech 247
 
248
/** Prepares the packet for sending.
249
 *  @param flags The device flags. Input parameter.
250
 *  @param packet The packet. Input parameter.
251
 *  @param src_addr The source hardware address. Input parameter.
252
 *  @param ethertype The ethernet protocol type. Input parameter.
253
 *  @param mtu The device maximum transmission unit. Input parameter.
254
 *  @returns EOK on success.
255
 *  @returns EINVAL if the packet addresses length is not long enough.
256
 *  @returns EINVAL if the packet is bigger than the device MTU.
257
 *  @returns ENOMEM if there is not enough memory in the packet.
258
 */
4351 mejdrech 259
int eth_prepare_packet( int flags, packet_t packet, uint8_t * src_addr, int ethertype, size_t mtu );
3992 mejdrech 260
 
4350 mejdrech 261
DEVICE_MAP_IMPLEMENT( eth_devices, eth_device_t )
262
 
263
INT_MAP_IMPLEMENT( eth_protos, eth_proto_t )
264
 
4307 mejdrech 265
int nil_device_state_msg( int nil_phone, device_id_t device_id, int state ){
4261 mejdrech 266
    int             index;
267
    eth_proto_ref   proto;
268
 
269
    //TODO clear device if off?
270
    rwlock_read_lock( & eth_globals.protos_lock );
271
    for( index = eth_protos_count( & eth_globals.protos ) - 1; index >= 0; -- index ){
272
        proto = eth_protos_get_index( & eth_globals.protos, index );
4351 mejdrech 273
        if( proto && proto->phone ) il_device_state_msg( proto->phone, device_id, state, proto->service );
4261 mejdrech 274
    }
275
    rwlock_read_unlock( & eth_globals.protos_lock );
4307 mejdrech 276
    return EOK;
4261 mejdrech 277
}
278
 
4307 mejdrech 279
int nil_initialize( int net_phone ){
3992 mejdrech 280
    ERROR_DECLARE;
281
 
282
    rwlock_initialize( & eth_globals.devices_lock );
283
    rwlock_initialize( & eth_globals.protos_lock );
284
    rwlock_write_lock( & eth_globals.devices_lock );
285
    rwlock_write_lock( & eth_globals.protos_lock );
4307 mejdrech 286
    eth_globals.net_phone = net_phone;
3992 mejdrech 287
    eth_globals.broadcast_addr = measured_string_create_bulk( "\xFF\xFF\xFF\xFF\xFF\xFF", CONVERT_SIZE( uint8_t, char, ETH_ADDR ));
288
    if( ! eth_globals.broadcast_addr ) return ENOMEM;
289
    ERROR_PROPAGATE( eth_devices_initialize( & eth_globals.devices ));
290
    if( ERROR_OCCURRED( eth_protos_initialize( & eth_globals.protos ))){
291
        eth_devices_destroy( & eth_globals.devices );
292
        return ERROR_CODE;
293
    }
294
    rwlock_write_unlock( & eth_globals.protos_lock );
295
    rwlock_write_unlock( & eth_globals.devices_lock );
296
    return EOK;
297
}
298
 
299
int eth_device_message( device_id_t device_id, services_t service, size_t mtu ){
300
    ERROR_DECLARE;
301
 
302
    eth_device_ref  device;
4192 mejdrech 303
    int             index;
4332 mejdrech 304
    measured_string_t   names[ 2 ] = {{ "ETH_MODE", 8 }, { "ETH_DUMMY", 9 }};
305
    measured_string_ref configuration;
306
    int                 count = 2;
307
    char *              data;
3992 mejdrech 308
 
309
    rwlock_write_lock( & eth_globals.devices_lock );
310
    // an existing device?
311
    device = eth_devices_find( & eth_globals.devices, device_id );
312
    if( device ){
4163 mejdrech 313
        if( device->service != service ){
4307 mejdrech 314
            printf( "Device %d already exists\n", device->device_id );
4163 mejdrech 315
            rwlock_write_unlock( & eth_globals.devices_lock );
316
            return EEXIST;
317
        }else{
3992 mejdrech 318
            // update mtu
319
            device->mtu = mtu;
4307 mejdrech 320
            printf( "Device %d already exists:\tMTU\t= %d\n", device->device_id, device->mtu );
3992 mejdrech 321
        }
322
    }else{
323
        // create a new device
324
        device = ( eth_device_ref ) malloc( sizeof( eth_device_t ));
325
        if( ! device ) return ENOMEM;
326
        device->device_id = device_id;
327
        device->service = service;
4332 mejdrech 328
        device->flags = 0;
329
        device->mtu = (( mtu > 0 ) && ( mtu <= ETH_MAX_TAGGED_CONTENT( device->flags ))) ? mtu : ETH_MAX_TAGGED_CONTENT( device->flags );
330
        configuration = & names[ 0 ];
331
        if( ERROR_OCCURRED( net_get_device_conf_req( eth_globals.net_phone, device->device_id, & configuration, count, & data ))){
332
            rwlock_write_unlock( & eth_globals.devices_lock );
333
            free( device );
334
            return ERROR_CODE;
335
        }
336
        if( configuration ){
337
            if( ! str_lcmp( configuration[ 0 ].value, "DIX", configuration[ 0 ].length )){
338
                device->flags |= ETH_DIX;
339
            }else if( ! str_lcmp( configuration[ 0 ].value, "8023_2_LSAP", configuration[ 0 ].length )){
340
                // TODO 8023_2_LSAP
341
                printf( "8023_2_LSAP is not supported (yet?), DIX used instead\n" );
342
                device->flags |= ETH_DIX;
343
            }else device->flags |= ETH_8023_2_SNAP;
344
            if(( configuration[ 1 ].value ) && ( configuration[ 1 ].value[ 0 ] == 'y' )){
345
                device->flags |= ETH_DUMMY;
346
            }
347
            net_free_settings( configuration, data );
348
        }else{
349
            device->flags |= ETH_8023_2_SNAP;
350
        }
3992 mejdrech 351
        // bind the device driver
4307 mejdrech 352
        device->phone = netif_bind_service( device->service, device->device_id, SERVICE_ETHERNET, eth_receiver );
4163 mejdrech 353
        if( device->phone < 0 ){
354
            rwlock_write_unlock( & eth_globals.devices_lock );
355
            free( device );
356
            return device->phone;
357
        }
3992 mejdrech 358
        // get hardware address
4243 mejdrech 359
        if( ERROR_OCCURRED( netif_get_addr( device->phone, device->device_id, & device->addr, & device->addr_data ))){
3992 mejdrech 360
            rwlock_write_unlock( & eth_globals.devices_lock );
361
            free( device );
362
            return ERROR_CODE;
363
        }
364
        // add to the cache
4192 mejdrech 365
        index = eth_devices_add( & eth_globals.devices, device->device_id, device );
366
        if( index < 0 ){
3992 mejdrech 367
            rwlock_write_unlock( & eth_globals.devices_lock );
368
            free( device->addr );
369
            free( device->addr_data );
370
            free( device );
4192 mejdrech 371
            return index;
3992 mejdrech 372
        }
4332 mejdrech 373
        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\tflags\t= 0x%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 ], device->flags );
3992 mejdrech 374
    }
375
    rwlock_write_unlock( & eth_globals.devices_lock );
376
    return EOK;
377
}
378
 
4332 mejdrech 379
eth_proto_ref eth_process_packet( int flags, packet_t packet ){
3992 mejdrech 380
    ERROR_DECLARE;
381
 
382
    eth_header_ex_ref   header;
383
    size_t              length;
384
    int                 type;
4075 mejdrech 385
    size_t              prefix;
386
    size_t              suffix;
387
    eth_fcs_ref         fcs;
3992 mejdrech 388
 
389
    length = packet_get_data_length( packet );
4332 mejdrech 390
    if( IS_DUMMY( flags )){
4153 mejdrech 391
        packet_trim( packet, sizeof( eth_preamble_t ), 0 );
392
    }
4332 mejdrech 393
    if( length < sizeof( eth_header_t ) + ETH_MIN_CONTENT + ( IS_DUMMY( flags ) ? ETH_SUFFIX : 0 )) return NULL;
3992 mejdrech 394
    header = ( eth_header_ex_ref ) packet_get_data( packet );
395
    type = ntohs( header->header.ethertype );
396
    if( type >= ETH_MIN_PROTO ){
397
        // DIX Ethernet
4075 mejdrech 398
        prefix = sizeof( eth_header_t );
4332 mejdrech 399
        suffix = 0;
400
        fcs = (( void * ) header ) + length - sizeof( eth_fcs_t );
4075 mejdrech 401
    }else if( type <= ETH_MAX_CONTENT ){
3992 mejdrech 402
        // translate "LSAP" values
4075 mejdrech 403
        if(( header->lsap.dsap == ETH_LSAP_GLSAP ) && ( header->lsap.ssap == ETH_LSAP_GLSAP )){
3992 mejdrech 404
            // raw packet
405
            // discard
4075 mejdrech 406
            return NULL;
3992 mejdrech 407
        }else if(( header->lsap.dsap == ETH_LSAP_SNAP ) && ( header->lsap.ssap == ETH_LSAP_SNAP )){
4075 mejdrech 408
            // IEEE 802.3 + 802.2 + LSAP + SNAP
3992 mejdrech 409
            // organization code not supported
410
            type = ntohs( header->snap.ethertype );
4243 mejdrech 411
            prefix = sizeof( eth_header_t ) + sizeof( eth_header_lsap_t ) + sizeof( eth_header_snap_t );
3992 mejdrech 412
        }else{
413
            // IEEE 802.3 + 802.2 LSAP
4075 mejdrech 414
            type = lsap_map( header->lsap.dsap );
415
            prefix = sizeof( eth_header_t ) + sizeof( eth_header_lsap_t);
3992 mejdrech 416
        }
4075 mejdrech 417
        suffix = ( type < ETH_MIN_CONTENT ) ? ETH_MIN_CONTENT - type : 0;
418
        fcs = (( void * ) header ) + prefix + type + suffix;
419
        suffix += length - prefix - type;
420
    }else{
421
        // invalid length/type, should not occurr
422
        return NULL;
3992 mejdrech 423
    }
4332 mejdrech 424
    if( IS_DUMMY( flags )){
4153 mejdrech 425
        if(( ~ compute_crc32( ~ 0, & header->header.dest, ((( void * ) fcs ) - (( void * ) & header->header.dest )) * 8 )) != ntohl( * fcs )){
426
            return NULL;
427
        }
4332 mejdrech 428
        suffix += sizeof( eth_fcs_t );
3992 mejdrech 429
    }
430
    if( ERROR_OCCURRED( packet_set_addr( packet, header->header.src, header->header.dest, ETH_ADDR ))
4075 mejdrech 431
    || ERROR_OCCURRED( packet_trim( packet, prefix, suffix ))){
432
        return NULL;
3992 mejdrech 433
    }
4075 mejdrech 434
    return eth_protos_find( & eth_globals.protos, type );
435
}
436
 
4307 mejdrech 437
int nil_received_msg( int nil_phone, device_id_t device_id, packet_t packet, services_t target ){
4075 mejdrech 438
    eth_proto_ref   proto;
439
    packet_t        next;
4153 mejdrech 440
    eth_device_ref  device;
4332 mejdrech 441
    int             flags;
4075 mejdrech 442
 
4153 mejdrech 443
    rwlock_read_lock( & eth_globals.devices_lock );
444
    device = eth_devices_find( & eth_globals.devices, device_id );
445
    if( ! device ){
446
        rwlock_read_unlock( & eth_globals.devices_lock );
447
        return ENOENT;
448
    }
4332 mejdrech 449
    flags = device->flags;
4153 mejdrech 450
    rwlock_read_unlock( & eth_globals.devices_lock );
4075 mejdrech 451
    rwlock_read_lock( & eth_globals.protos_lock );
452
    do{
453
        next = pq_detach( packet );
4332 mejdrech 454
        proto = eth_process_packet( flags, packet );
4075 mejdrech 455
        if( proto ){
4307 mejdrech 456
            il_received_msg( proto->phone, device_id, packet, proto->service );
4075 mejdrech 457
        }else{
458
            // drop invalid/unknown
4307 mejdrech 459
            pq_release( eth_globals.net_phone, packet_get_id( packet ));
4075 mejdrech 460
        }
461
        packet = next;
462
    }while( packet );
463
    rwlock_read_unlock( & eth_globals.protos_lock );
3992 mejdrech 464
    return EOK;
465
}
466
 
467
int eth_packet_space_message( device_id_t device_id, size_t * addr_len, size_t * prefix, size_t * content, size_t * suffix ){
468
    eth_device_ref  device;
469
 
4350 mejdrech 470
    if( !( addr_len && prefix && content && suffix )) return EBADMEM;
4192 mejdrech 471
    rwlock_read_lock( & eth_globals.devices_lock );
3992 mejdrech 472
    device = eth_devices_find( & eth_globals.devices, device_id );
473
    if( ! device ){
4192 mejdrech 474
        rwlock_read_unlock( & eth_globals.devices_lock );
3992 mejdrech 475
        return ENOENT;
476
    }
4243 mejdrech 477
    * content = device->mtu;
4192 mejdrech 478
    rwlock_read_unlock( & eth_globals.devices_lock );
3992 mejdrech 479
    * addr_len = ETH_ADDR;
480
    * prefix = ETH_PREFIX;
4075 mejdrech 481
    * suffix = ETH_MIN_CONTENT + ETH_SUFFIX;
3992 mejdrech 482
    return EOK;
483
}
484
 
485
int eth_addr_message( device_id_t device_id, eth_addr_type_t type, measured_string_ref * address ){
486
    eth_device_ref  device;
487
 
4350 mejdrech 488
    if( ! address ) return EBADMEM;
3992 mejdrech 489
    if( type == ETH_BROADCAST_ADDR ){
490
        * address = eth_globals.broadcast_addr;
491
    }else{
4192 mejdrech 492
        rwlock_read_lock( & eth_globals.devices_lock );
3992 mejdrech 493
        device = eth_devices_find( & eth_globals.devices, device_id );
494
        if( ! device ){
4192 mejdrech 495
            rwlock_read_unlock( & eth_globals.devices_lock );
3992 mejdrech 496
            return ENOENT;
497
        }
498
        * address = device->addr;
4192 mejdrech 499
        rwlock_read_unlock( & eth_globals.devices_lock );
3992 mejdrech 500
    }
501
    return ( * address ) ? EOK : ENOENT;
502
}
503
 
4261 mejdrech 504
int nil_register_message( services_t service, int phone ){
3992 mejdrech 505
    eth_proto_ref   proto;
506
    int             protocol;
4192 mejdrech 507
    int             index;
3992 mejdrech 508
 
509
    protocol = protocol_map( SERVICE_ETHERNET, service );
510
    if( ! protocol ) return ENOENT;
511
    rwlock_write_lock( & eth_globals.protos_lock );
512
    proto = eth_protos_find( & eth_globals.protos, protocol );
513
    if( proto ){
514
        proto->phone = phone;
515
        rwlock_write_unlock( & eth_globals.protos_lock );
516
        return EOK;
517
    }else{
518
        proto = ( eth_proto_ref ) malloc( sizeof( eth_proto_t ));
519
        if( ! proto ){
520
            rwlock_write_unlock( & eth_globals.protos_lock );
521
            return ENOMEM;
522
        }
523
        proto->service = service;
524
        proto->protocol = protocol;
525
        proto->phone = phone;
4192 mejdrech 526
        index = eth_protos_add( & eth_globals.protos, protocol, proto );
527
        if( index < 0 ){
3992 mejdrech 528
            rwlock_write_unlock( & eth_globals.protos_lock );
529
            free( proto );
4192 mejdrech 530
            return index;
3992 mejdrech 531
        }
532
    }
4307 mejdrech 533
    printf( "New protocol registered:\n\tprotocol\t= 0x%x\n\tservice\t= %d\n\tphone\t= %d\n", proto->protocol, proto->service, proto->phone );
3992 mejdrech 534
    rwlock_write_unlock( & eth_globals.protos_lock );
535
    return EOK;
536
}
537
 
4351 mejdrech 538
int eth_prepare_packet( int flags, packet_t packet, uint8_t * src_addr, int ethertype, size_t mtu ){
3992 mejdrech 539
    eth_header_ex_ref   header;
4332 mejdrech 540
    eth_header_ref      header_dix;
4075 mejdrech 541
    eth_fcs_ref         fcs;
3992 mejdrech 542
    uint8_t *           src;
543
    uint8_t *           dest;
544
    int                 length;
545
    int                 i;
4075 mejdrech 546
    void *              padding;
4153 mejdrech 547
    eth_preamble_ref    preamble;
3992 mejdrech 548
 
4332 mejdrech 549
    length = packet_get_addr( packet, & src, & dest );
550
    if( length < 0 ) return length;
551
    if( length < ETH_ADDR ) return EINVAL;
4243 mejdrech 552
    length = packet_get_data_length( packet );
4351 mejdrech 553
    if( length > mtu ) return EINVAL;
4332 mejdrech 554
    if( length < ETH_MIN_TAGGED_CONTENT( flags )){
555
        padding = packet_suffix( packet, ETH_MIN_TAGGED_CONTENT( flags ) - length );
4243 mejdrech 556
        if( ! padding ) return ENOMEM;
4332 mejdrech 557
        bzero( padding, ETH_MIN_TAGGED_CONTENT( flags ) - length );
4243 mejdrech 558
    }
4332 mejdrech 559
    if( IS_DUMMY( flags )){
4153 mejdrech 560
        preamble = PACKET_PREFIX( packet, eth_preamble_t );
561
        if( ! preamble ) return ENOMEM;
562
        for( i = 0; i < 7; ++ i ) preamble->preamble[ i ] = ETH_PREAMBLE;
563
        preamble->sfd = ETH_SFD;
564
    }
4332 mejdrech 565
    // TODO LSAP only device
566
    if( IS_DIX( flags ) || IS_8023_2_LSAP( flags )){
567
        header_dix = PACKET_PREFIX( packet, eth_header_t );
568
        header_dix->ethertype = ( uint16_t ) ethertype;
569
        memcpy( header_dix->src, src_addr, ETH_ADDR );
570
        memcpy( header_dix->dest, dest, ETH_ADDR );
571
        src = & header_dix->dest[ 0 ];
572
    }else if( IS_8023_2_SNAP( flags )){
573
        header = PACKET_PREFIX( packet, eth_header_ex_t );
574
        if( ! header ) return ENOMEM;
575
        header->header.ethertype = htons( length + sizeof( eth_header_lsap_t ) + sizeof( eth_header_snap_t ));
576
        header->lsap.dsap = ( uint16_t ) ETH_LSAP_SNAP;
577
        header->lsap.ssap = header->lsap.dsap;
578
        header->lsap.ctrl = 0;
579
        for( i = 0; i < 3; ++ i ) header->snap.proto[ i ] = 0;
580
        header->snap.ethertype = ( uint16_t ) ethertype;
581
        memcpy( header->header.src, src_addr, ETH_ADDR );
582
        memcpy( header->header.dest, dest, ETH_ADDR );
583
        src = & header->header.dest[ 0 ];
584
    }
585
    if( IS_DUMMY( flags )){
4153 mejdrech 586
        fcs = PACKET_SUFFIX( packet, eth_fcs_t );
587
        if( ! fcs ) return ENOMEM;
4332 mejdrech 588
        * fcs = htonl( ~ compute_crc32( ~ 0, src, ((( void * ) fcs ) - (( void * ) src )) * 8 ));
4153 mejdrech 589
    }
4075 mejdrech 590
    return EOK;
591
}
592
 
593
int eth_send_message( device_id_t device_id, packet_t packet, services_t sender ){
594
    ERROR_DECLARE;
595
 
596
    eth_device_ref      device;
597
    packet_t            next;
598
    packet_t            tmp;
599
    int                 ethertype;
600
 
601
    ethertype = htons( protocol_map( SERVICE_ETHERNET, sender ));
602
    if( ! ethertype ){
4307 mejdrech 603
        pq_release( eth_globals.net_phone, packet_get_id( packet ));
4075 mejdrech 604
        return EINVAL;
3992 mejdrech 605
    }
4075 mejdrech 606
    rwlock_read_lock( & eth_globals.devices_lock );
607
    device = eth_devices_find( & eth_globals.devices, device_id );
608
    if( ! device ){
609
        rwlock_read_unlock( & eth_globals.devices_lock );
610
        return ENOENT;
611
    }
4192 mejdrech 612
    // process packet queue
4075 mejdrech 613
    next = packet;
614
    do{
4351 mejdrech 615
        if( ERROR_OCCURRED( eth_prepare_packet( device->flags, next, ( uint8_t * ) device->addr->value, ethertype, device->mtu ))){
4075 mejdrech 616
            // release invalid packet
617
            tmp = pq_detach( next );
4307 mejdrech 618
            pq_release( eth_globals.net_phone, packet_get_id( next ));
4075 mejdrech 619
            next = tmp;
620
        }else{
621
            next = pq_next( next );
622
        }
623
    }while( next );
624
    // send packet queue
4261 mejdrech 625
    netif_send_msg( device->phone, device_id, packet, SERVICE_ETHERNET );
4075 mejdrech 626
    rwlock_read_unlock( & eth_globals.devices_lock );
3992 mejdrech 627
    return EOK;
628
}
629
 
4261 mejdrech 630
int nil_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
3992 mejdrech 631
    ERROR_DECLARE;
632
 
633
    measured_string_ref address;
634
    packet_t            packet;
635
 
4307 mejdrech 636
//  printf( "message %d - %d\n", IPC_GET_METHOD( * call ), NET_NIL_FIRST );
3992 mejdrech 637
    * answer_count = 0;
638
    switch( IPC_GET_METHOD( * call )){
639
        case IPC_M_PHONE_HUNGUP:
640
            return EOK;
641
        case NET_NIL_DEVICE:
4261 mejdrech 642
            return eth_device_message( IPC_GET_DEVICE( call ), IPC_GET_SERVICE( call ), NIL_GET_MTU( call ));
3992 mejdrech 643
        case NET_NIL_SEND:
4307 mejdrech 644
            ERROR_PROPAGATE( packet_translate( eth_globals.net_phone, & packet, IPC_GET_PACKET( call )));
3992 mejdrech 645
            return eth_send_message( IPC_GET_DEVICE( call ), packet, IPC_GET_SERVICE( call ));
646
        case NET_NIL_PACKET_SPACE:
647
            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 )));
648
            * answer_count = 3;
649
            return EOK;
650
        case NET_NIL_ADDR:
4192 mejdrech 651
            ERROR_PROPAGATE( eth_addr_message( IPC_GET_DEVICE( call ), ETH_LOCAL_ADDR, & address ));
652
            return measured_strings_reply( address, 1 );
3992 mejdrech 653
        case NET_NIL_BROADCAST_ADDR:
4192 mejdrech 654
            ERROR_PROPAGATE( eth_addr_message( IPC_GET_DEVICE( call ), ETH_BROADCAST_ADDR, & address ));
655
            return measured_strings_reply( address, 1 );
3992 mejdrech 656
        case IPC_M_CONNECT_TO_ME:
4261 mejdrech 657
            return nil_register_message( NIL_GET_PROTO( call ), IPC_GET_PHONE( call ));
3992 mejdrech 658
    }
659
    return ENOTSUP;
660
}
661
 
662
void eth_receiver( ipc_callid_t iid, ipc_call_t * icall ){
663
    ERROR_DECLARE;
664
 
665
    packet_t        packet;
666
 
667
    while( true ){
4332 mejdrech 668
//      printf( "message %d - %d\n", IPC_GET_METHOD( * icall ), NET_NIL_FIRST );
3992 mejdrech 669
        switch( IPC_GET_METHOD( * icall )){
670
            case NET_NIL_DEVICE_STATE:
4307 mejdrech 671
                nil_device_state_msg( 0, IPC_GET_DEVICE( icall ), IPC_GET_STATE( icall ));
4192 mejdrech 672
                ipc_answer_0( iid, EOK );
3992 mejdrech 673
                break;
674
            case NET_NIL_RECEIVED:
4307 mejdrech 675
                if( ! ERROR_OCCURRED( packet_translate( eth_globals.net_phone, & packet, IPC_GET_PACKET( icall )))){
676
                    ERROR_CODE = nil_received_msg( 0, IPC_GET_DEVICE( icall ), packet, 0 );
3992 mejdrech 677
                }
678
                ipc_answer_0( iid, ERROR_CODE );
679
                break;
680
            default:
681
                ipc_answer_0( iid, ENOTSUP );
682
        }
683
        iid = async_get_call( icall );
684
    }
685
}
686
 
687
/** @}
688
 */