Subversion Repositories HelenOS

Rev

Rev 4153 | Rev 4192 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4153 Rev 4163
1
/*
1
/*
2
 * Copyright (c) 2009 Lukas Mejdrech
2
 * Copyright (c) 2009 Lukas Mejdrech
3
 * All rights reserved.
3
 * All rights reserved.
4
 *
4
 *
5
 * Redistribution and use in source and binary forms, with or without
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
6
 * modification, are permitted provided that the following conditions
7
 * are met:
7
 * are met:
8
 *
8
 *
9
 * - Redistributions of source code must retain the above copyright
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
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
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.
15
 *   derived from this software without specific prior written permission.
16
 *
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
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
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
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
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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
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.
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
27
 */
28
 
28
 
29
/** @addtogroup eth
29
/** @addtogroup eth
30
 *  @{
30
 *  @{
31
 */
31
 */
32
 
32
 
33
/** @file
33
/** @file
34
 *  Ethernet module implementation.
34
 *  Ethernet module implementation.
35
 *  @see eth.h
35
 *  @see eth.h
36
 */
36
 */
37
 
37
 
38
#include <async.h>
38
#include <async.h>
39
#include <malloc.h>
39
#include <malloc.h>
40
#include <stdio.h>
40
#include <stdio.h>
41
#include <string.h>
41
#include <string.h>
42
 
42
 
43
#include <ipc/ipc.h>
43
#include <ipc/ipc.h>
44
#include <ipc/services.h>
44
#include <ipc/services.h>
45
 
45
 
46
#include "../../err.h"
46
#include "../../err.h"
47
#include "../../messages.h"
47
#include "../../messages.h"
48
#include "../../modules.h"
48
#include "../../modules.h"
49
 
49
 
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 "../../netif/device.h"
56
 
56
 
57
#include "../../structures/measured_strings.h"
57
#include "../../structures/measured_strings.h"
58
#include "../../structures/packet/packet.h"
58
#include "../../structures/packet/packet.h"
59
#include "../../structures/packet/packet_client.h"
59
#include "../../structures/packet/packet_client.h"
60
 
60
 
61
#include "eth.h"
61
#include "eth.h"
62
#include "eth_header.h"
62
#include "eth_header.h"
63
//#include "eth_messages.h"
63
//#include "eth_messages.h"
64
#include "eth_module.h"
64
#include "eth_module.h"
65
 
65
 
66
#define ETH_PREFIX      ( sizeof( eth_header_t ) + sizeof( eth_header_lsap_t ) + sizeof( eth_header_snap_t ))
66
#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 )
67
#define ETH_SUFFIX      sizeof( eth_fcs_t )
68
#define ETH_MAX_CONTENT 1500
68
#define ETH_MAX_CONTENT 1500
69
#define ETH_MIN_CONTENT 46
69
#define ETH_MIN_CONTENT 46
70
 
70
 
71
/** Returns the device identifier message parameter.
71
/** Returns the device identifier message parameter.
72
 */
72
 */
73
#define IPC_GET_DEVICE( call )      ( device_id_t ) IPC_GET_ARG1( * call )
73
#define IPC_GET_DEVICE( call )      ( device_id_t ) IPC_GET_ARG1( * call )
74
 
74
 
75
/** Returns the packet identifier message parameter.
75
/** Returns the packet identifier message parameter.
76
 */
76
 */
77
#define IPC_GET_PACKET( call )      ( packet_id_t ) IPC_GET_ARG2( * call )
77
#define IPC_GET_PACKET( call )      ( packet_id_t ) IPC_GET_ARG2( * call )
78
 
78
 
79
/** Returns the protocol service message parameter.
79
/** Returns the protocol service message parameter.
80
 */
80
 */
81
#define IPC_GET_PROTO( call )       ( services_t ) IPC_GET_ARG1( * call )
81
#define IPC_GET_PROTO( call )       ( services_t ) IPC_GET_ARG1( * call )
82
 
82
 
83
/** Returns the device driver service message parameter.
83
/** Returns the device driver service message parameter.
84
 */
84
 */
85
#define IPC_GET_SERVICE( call )     ( services_t ) IPC_GET_ARG2( * call )
85
#define IPC_GET_SERVICE( call )     ( services_t ) IPC_GET_ARG2( * call )
86
 
86
 
87
#define IPC_GET_MTU( call )         ( size_t ) IPC_GET_ARG3( * call )
87
#define IPC_GET_MTU( call )         ( size_t ) IPC_GET_ARG3( * call )
88
 
88
 
89
#define IPC_GET_PHONE( call )       ( int ) IPC_GET_ARG5( * call )
89
#define IPC_GET_PHONE( call )       ( int ) IPC_GET_ARG5( * call )
90
 
90
 
91
#define IPC_SET_ADDR( answer )      (( size_t * ) & IPC_GET_ARG1( * answer ))
91
#define IPC_SET_ADDR( answer )      (( size_t * ) & IPC_GET_ARG1( * answer ))
92
#define IPC_SET_PREFIX( answer )    (( size_t * ) & IPC_GET_ARG2( * answer ))
92
#define IPC_SET_PREFIX( answer )    (( size_t * ) & IPC_GET_ARG2( * answer ))
93
#define IPC_SET_CONTENT( answer )   (( size_t * ) & IPC_GET_ARG3( * answer ))
93
#define IPC_SET_CONTENT( answer )   (( size_t * ) & IPC_GET_ARG3( * answer ))
94
#define IPC_SET_SUFFIX( answer )    (( size_t * ) & IPC_GET_ARG4( * answer ))
94
#define IPC_SET_SUFFIX( answer )    (( size_t * ) & IPC_GET_ARG4( * answer ))
95
 
95
 
96
typedef enum eth_addr_type  eth_addr_type_t;
96
typedef enum eth_addr_type  eth_addr_type_t;
97
typedef eth_addr_type_t *   eth_addr_type_ref;
97
typedef eth_addr_type_t *   eth_addr_type_ref;
98
 
98
 
99
enum eth_addr_type{
99
enum eth_addr_type{
100
    ETH_LOCAL_ADDR,
100
    ETH_LOCAL_ADDR,
101
    ETH_BROADCAST_ADDR
101
    ETH_BROADCAST_ADDR
102
};
102
};
103
 
103
 
104
/** Ethernet global data.
104
/** Ethernet global data.
105
 */
105
 */
106
eth_globals_t   eth_globals;
106
eth_globals_t   eth_globals;
107
 
107
 
108
/** Processes IPC messages from the registered device driver modules in an infinite loop.
108
/** Processes IPC messages from the registered device driver modules in an infinite loop.
109
 *  @param iid The message identifier. Input parameter.
109
 *  @param iid The message identifier. Input parameter.
110
 *  @param icall The message parameters. Input/output parameter.
110
 *  @param icall The message parameters. Input/output parameter.
111
 */
111
 */
112
void    eth_receiver( ipc_callid_t iid, ipc_call_t * icall );
112
void    eth_receiver( ipc_callid_t iid, ipc_call_t * icall );
113
 
113
 
114
DEVICE_MAP_IMPLEMENT( eth_devices, eth_device_t )
114
DEVICE_MAP_IMPLEMENT( eth_devices, eth_device_t )
115
 
115
 
116
INT_MAP_IMPLEMENT( eth_protos, eth_proto_t )
116
INT_MAP_IMPLEMENT( eth_protos, eth_proto_t )
117
 
117
 
118
int eth_device_message( device_id_t device_id, services_t service, size_t mtu );
118
int eth_device_message( device_id_t device_id, services_t service, size_t mtu );
119
int eth_receive_message( device_id_t device_id, packet_t packet );
119
int eth_receive_message( device_id_t device_id, packet_t packet );
120
int eth_packet_space_message( device_id_t device_id, size_t * addr_len, size_t * prefix, size_t * content, size_t * suffix );
120
int eth_packet_space_message( device_id_t device_id, size_t * addr_len, size_t * prefix, size_t * content, size_t * suffix );
121
int eth_addr_message( device_id_t device_id, eth_addr_type_t type, measured_string_ref * address );
121
int eth_addr_message( device_id_t device_id, eth_addr_type_t type, measured_string_ref * address );
122
int eth_register_message( services_t service, int phone );
122
int eth_register_message( services_t service, int phone );
123
int eth_send_message( device_id_t device_id, packet_t packet, services_t sender );
123
int eth_send_message( device_id_t device_id, packet_t packet, services_t sender );
124
int eth_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count );
124
int eth_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count );
125
void    eth_receiver( ipc_callid_t iid, ipc_call_t * icall );
125
void    eth_receiver( ipc_callid_t iid, ipc_call_t * icall );
126
eth_proto_ref   eth_proccess_packet( int dummy, packet_t packet );
126
eth_proto_ref   eth_proccess_packet( int dummy, packet_t packet );
127
int eth_prepare_packet( int dummy, packet_t packet, uint8_t * src_addr, int ethertype );
127
int eth_prepare_packet( int dummy, packet_t packet, uint8_t * src_addr, int ethertype );
128
 
128
 
129
int eth_initialize( void ){
129
int eth_initialize( void ){
130
    ERROR_DECLARE;
130
    ERROR_DECLARE;
131
 
131
 
132
    rwlock_initialize( & eth_globals.devices_lock );
132
    rwlock_initialize( & eth_globals.devices_lock );
133
    rwlock_initialize( & eth_globals.protos_lock );
133
    rwlock_initialize( & eth_globals.protos_lock );
134
    rwlock_write_lock( & eth_globals.devices_lock );
134
    rwlock_write_lock( & eth_globals.devices_lock );
135
    rwlock_write_lock( & eth_globals.protos_lock );
135
    rwlock_write_lock( & eth_globals.protos_lock );
136
    eth_globals.broadcast_addr = measured_string_create_bulk( "\xFF\xFF\xFF\xFF\xFF\xFF", CONVERT_SIZE( uint8_t, char, ETH_ADDR ));
136
    eth_globals.broadcast_addr = measured_string_create_bulk( "\xFF\xFF\xFF\xFF\xFF\xFF", CONVERT_SIZE( uint8_t, char, ETH_ADDR ));
137
    if( ! eth_globals.broadcast_addr ) return ENOMEM;
137
    if( ! eth_globals.broadcast_addr ) return ENOMEM;
138
    ERROR_PROPAGATE( eth_devices_initialize( & eth_globals.devices ));
138
    ERROR_PROPAGATE( eth_devices_initialize( & eth_globals.devices ));
139
    if( ERROR_OCCURRED( eth_protos_initialize( & eth_globals.protos ))){
139
    if( ERROR_OCCURRED( eth_protos_initialize( & eth_globals.protos ))){
140
        eth_devices_destroy( & eth_globals.devices );
140
        eth_devices_destroy( & eth_globals.devices );
141
        return ERROR_CODE;
141
        return ERROR_CODE;
142
    }
142
    }
143
    rwlock_write_unlock( & eth_globals.protos_lock );
143
    rwlock_write_unlock( & eth_globals.protos_lock );
144
    rwlock_write_unlock( & eth_globals.devices_lock );
144
    rwlock_write_unlock( & eth_globals.devices_lock );
145
    return EOK;
145
    return EOK;
146
}
146
}
147
 
147
 
148
int eth_device_message( device_id_t device_id, services_t service, size_t mtu ){
148
int eth_device_message( device_id_t device_id, services_t service, size_t mtu ){
149
    ERROR_DECLARE;
149
    ERROR_DECLARE;
150
 
150
 
151
    aid_t           message;
151
    aid_t           message;
152
    ipc_call_t      answer;
152
    ipc_call_t      answer;
153
    eth_device_ref  device;
153
    eth_device_ref  device;
154
    int             result;
154
    int             result;
155
 
155
 
156
    rwlock_write_lock( & eth_globals.devices_lock );
156
    rwlock_write_lock( & eth_globals.devices_lock );
157
    // an existing device?
157
    // an existing device?
158
    device = eth_devices_find( & eth_globals.devices, device_id );
158
    device = eth_devices_find( & eth_globals.devices, device_id );
159
    if( device ){
159
    if( device ){
160
        if( device->service == service ){
160
        if( device->service != service ){
161
            // update mtu
-
 
162
            device->mtu = mtu;
-
 
163
            rwlock_write_unlock( & eth_globals.devices_lock );
161
            printf( "\nDevice %d already exists", device->device_id );
164
            return EOK;
-
 
165
        }else{
-
 
166
            rwlock_write_unlock( & eth_globals.devices_lock );
162
            rwlock_write_unlock( & eth_globals.devices_lock );
167
            return EEXIST;
163
            return EEXIST;
-
 
164
        }else{
-
 
165
            // update mtu
-
 
166
            device->mtu = mtu;
-
 
167
            printf( "\nDevice %d already exists:\tMTU\t= %d", device->device_id, device->mtu );
168
        }
168
        }
169
    }else{
169
    }else{
170
        // create a new device
170
        // create a new device
171
        device = ( eth_device_ref ) malloc( sizeof( eth_device_t ));
171
        device = ( eth_device_ref ) malloc( sizeof( eth_device_t ));
172
        if( ! device ) return ENOMEM;
172
        if( ! device ) return ENOMEM;
173
        device->device_id = device_id;
173
        device->device_id = device_id;
174
        device->service = service;
174
        device->service = service;
175
        device->mtu = mtu;
175
        device->mtu = ( mtu > 0 ) ? mtu : ETH_MAX_CONTENT;
176
        // TODO get dummy setting
176
        // TODO get dummy setting
177
        device->dummy = 0;
177
        device->dummy = 0;
178
        // bind the device driver
178
        // bind the device driver
179
        device->phone = bind_service( device->service, device->device_id, SERVICE_ETHERNET, 0, eth_receiver );
179
        device->phone = bind_service( device->service, device->device_id, SERVICE_ETHERNET, 0, eth_receiver );
-
 
180
        if( device->phone < 0 ){
-
 
181
            rwlock_write_unlock( & eth_globals.devices_lock );
-
 
182
            free( device );
-
 
183
            return device->phone;
-
 
184
        }
180
        // get hardware address
185
        // get hardware address
181
        message = async_send_1( device->phone, NET_NETIF_GET_ADDR, device->device_id, & answer );
186
        message = async_send_1( device->phone, NET_NETIF_GET_ADDR, device->device_id, & answer );
182
        if( ERROR_OCCURRED( measured_strings_return( device->phone, & device->addr, & device->addr_data, 1 ))){
187
        if( ERROR_OCCURRED( measured_strings_return( device->phone, & device->addr, & device->addr_data, 1 ))){
183
            rwlock_write_unlock( & eth_globals.devices_lock );
188
            rwlock_write_unlock( & eth_globals.devices_lock );
184
            free( device );
189
            free( device );
185
            async_wait_for( message, NULL );
190
            async_wait_for( message, NULL );
186
            return ERROR_CODE;
191
            return ERROR_CODE;
187
        }
192
        }
188
        async_wait_for( message, ( ipcarg_t * ) & result );
193
        async_wait_for( message, ( ipcarg_t * ) & result );
189
        if( ERROR_OCCURRED( result )){
194
        if( ERROR_OCCURRED( result )){
190
            rwlock_write_unlock( & eth_globals.devices_lock );
195
            rwlock_write_unlock( & eth_globals.devices_lock );
191
            free( device->addr );
196
            free( device->addr );
192
            free( device->addr_data );
197
            free( device->addr_data );
193
            free( device );
198
            free( device );
194
            return ERROR_CODE;
199
            return ERROR_CODE;
195
        }
200
        }
196
        // add to the cache
201
        // add to the cache
197
        if( ERROR_OCCURRED( eth_devices_add( & eth_globals.devices, device->device_id, device ))){
202
        if( ERROR_OCCURRED( eth_devices_add( & eth_globals.devices, device->device_id, device ))){
198
            rwlock_write_unlock( & eth_globals.devices_lock );
203
            rwlock_write_unlock( & eth_globals.devices_lock );
199
            free( device->addr );
204
            free( device->addr );
200
            free( device->addr_data );
205
            free( device->addr_data );
201
            free( device );
206
            free( device );
202
            return ERROR_CODE;
207
            return ERROR_CODE;
203
        }
208
        }
-
 
209
        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 ] );
204
    }
210
    }
205
    rwlock_write_unlock( & eth_globals.devices_lock );
211
    rwlock_write_unlock( & eth_globals.devices_lock );
206
    return EOK;
212
    return EOK;
207
}
213
}
208
 
214
 
209
eth_proto_ref eth_proccess_packet( int dummy, packet_t packet ){
215
eth_proto_ref eth_proccess_packet( int dummy, packet_t packet ){
210
    ERROR_DECLARE;
216
    ERROR_DECLARE;
211
 
217
 
212
    eth_header_ex_ref   header;
218
    eth_header_ex_ref   header;
213
    size_t              length;
219
    size_t              length;
214
    int                 type;
220
    int                 type;
215
    size_t              prefix;
221
    size_t              prefix;
216
    size_t              suffix;
222
    size_t              suffix;
217
    eth_fcs_ref         fcs;
223
    eth_fcs_ref         fcs;
218
 
224
 
219
    length = packet_get_data_length( packet );
225
    length = packet_get_data_length( packet );
220
    if( dummy ){
226
    if( dummy ){
221
        packet_trim( packet, sizeof( eth_preamble_t ), 0 );
227
        packet_trim( packet, sizeof( eth_preamble_t ), 0 );
222
    }
228
    }
223
    if( length <= sizeof( eth_header_t ) + ETH_MIN_CONTENT + ETH_SUFFIX ) return NULL;
229
    if( length <= sizeof( eth_header_t ) + ETH_MIN_CONTENT + ETH_SUFFIX ) return NULL;
224
    header = ( eth_header_ex_ref ) packet_get_data( packet );
230
    header = ( eth_header_ex_ref ) packet_get_data( packet );
225
    type = ntohs( header->header.ethertype );
231
    type = ntohs( header->header.ethertype );
226
    if( type >= ETH_MIN_PROTO ){
232
    if( type >= ETH_MIN_PROTO ){
227
        // DIX Ethernet
233
        // DIX Ethernet
228
        prefix = sizeof( eth_header_t );
234
        prefix = sizeof( eth_header_t );
229
        suffix = sizeof( eth_fcs_t );
235
        suffix = sizeof( eth_fcs_t );
230
        fcs = (( void * ) header ) + length - suffix;
236
        fcs = (( void * ) header ) + length - suffix;
231
    }else if( type <= ETH_MAX_CONTENT ){
237
    }else if( type <= ETH_MAX_CONTENT ){
232
        // translate "LSAP" values
238
        // translate "LSAP" values
233
        if(( header->lsap.dsap == ETH_LSAP_GLSAP ) && ( header->lsap.ssap == ETH_LSAP_GLSAP )){
239
        if(( header->lsap.dsap == ETH_LSAP_GLSAP ) && ( header->lsap.ssap == ETH_LSAP_GLSAP )){
234
            // raw packet
240
            // raw packet
235
            // discard
241
            // discard
236
            return NULL;
242
            return NULL;
237
        }else if(( header->lsap.dsap == ETH_LSAP_SNAP ) && ( header->lsap.ssap == ETH_LSAP_SNAP )){
243
        }else if(( header->lsap.dsap == ETH_LSAP_SNAP ) && ( header->lsap.ssap == ETH_LSAP_SNAP )){
238
            // IEEE 802.3 + 802.2 + LSAP + SNAP
244
            // IEEE 802.3 + 802.2 + LSAP + SNAP
239
            // organization code not supported
245
            // organization code not supported
240
            type = ntohs( header->snap.ethertype );
246
            type = ntohs( header->snap.ethertype );
241
            prefix = sizeof( eth_header_t ) + sizeof( eth_header_lsap_t) + sizeof( eth_header_snap_t);
247
            prefix = sizeof( eth_header_t ) + sizeof( eth_header_lsap_t) + sizeof( eth_header_snap_t);
242
        }else{
248
        }else{
243
            // IEEE 802.3 + 802.2 LSAP
249
            // IEEE 802.3 + 802.2 LSAP
244
            type = lsap_map( header->lsap.dsap );
250
            type = lsap_map( header->lsap.dsap );
245
            prefix = sizeof( eth_header_t ) + sizeof( eth_header_lsap_t);
251
            prefix = sizeof( eth_header_t ) + sizeof( eth_header_lsap_t);
246
        }
252
        }
247
        suffix = ( type < ETH_MIN_CONTENT ) ? ETH_MIN_CONTENT - type : 0;
253
        suffix = ( type < ETH_MIN_CONTENT ) ? ETH_MIN_CONTENT - type : 0;
248
        fcs = (( void * ) header ) + prefix + type + suffix;
254
        fcs = (( void * ) header ) + prefix + type + suffix;
249
        suffix += length - prefix - type;
255
        suffix += length - prefix - type;
250
    }else{
256
    }else{
251
        // invalid length/type, should not occurr
257
        // invalid length/type, should not occurr
252
        return NULL;
258
        return NULL;
253
    }
259
    }
254
    if( dummy ){
260
    if( dummy ){
255
        if(( ~ compute_crc32( ~ 0, & header->header.dest, ((( void * ) fcs ) - (( void * ) & header->header.dest )) * 8 )) != ntohl( * fcs )){
261
        if(( ~ compute_crc32( ~ 0, & header->header.dest, ((( void * ) fcs ) - (( void * ) & header->header.dest )) * 8 )) != ntohl( * fcs )){
256
            return NULL;
262
            return NULL;
257
        }
263
        }
258
    }
264
    }
259
    if( ERROR_OCCURRED( packet_set_addr( packet, header->header.src, header->header.dest, ETH_ADDR ))
265
    if( ERROR_OCCURRED( packet_set_addr( packet, header->header.src, header->header.dest, ETH_ADDR ))
260
    || ERROR_OCCURRED( packet_trim( packet, prefix, suffix ))){
266
    || ERROR_OCCURRED( packet_trim( packet, prefix, suffix ))){
261
        return NULL;
267
        return NULL;
262
    }
268
    }
263
    return eth_protos_find( & eth_globals.protos, type );
269
    return eth_protos_find( & eth_globals.protos, type );
264
}
270
}
265
 
271
 
266
int eth_receive_message( device_id_t device_id, packet_t packet ){
272
int eth_receive_message( device_id_t device_id, packet_t packet ){
267
    eth_proto_ref   proto;
273
    eth_proto_ref   proto;
268
    packet_t        next;
274
    packet_t        next;
269
    eth_device_ref  device;
275
    eth_device_ref  device;
270
    int             dummy;
276
    int             dummy;
271
 
277
 
272
    rwlock_read_lock( & eth_globals.devices_lock );
278
    rwlock_read_lock( & eth_globals.devices_lock );
273
    device = eth_devices_find( & eth_globals.devices, device_id );
279
    device = eth_devices_find( & eth_globals.devices, device_id );
274
    if( ! device ){
280
    if( ! device ){
275
        rwlock_read_unlock( & eth_globals.devices_lock );
281
        rwlock_read_unlock( & eth_globals.devices_lock );
276
        return ENOENT;
282
        return ENOENT;
277
    }
283
    }
278
    dummy = device->dummy;
284
    dummy = device->dummy;
279
    rwlock_read_unlock( & eth_globals.devices_lock );
285
    rwlock_read_unlock( & eth_globals.devices_lock );
280
    rwlock_read_lock( & eth_globals.protos_lock );
286
    rwlock_read_lock( & eth_globals.protos_lock );
281
    do{
287
    do{
282
        next = pq_detach( packet );
288
        next = pq_detach( packet );
283
        proto = eth_proccess_packet( dummy, packet );
289
        proto = eth_proccess_packet( dummy, packet );
284
        if( proto ){
290
        if( proto ){
285
            async_msg_2( proto->phone, NET_IL_RECEIVED, device_id, packet_get_id( packet ));
291
            async_msg_2( proto->phone, NET_IL_RECEIVED, device_id, packet_get_id( packet ));
286
        }else{
292
        }else{
287
            // drop invalid/unknown
293
            // drop invalid/unknown
288
            packet_release( eth_globals.networking_phone, packet_get_id( packet ));
294
            packet_release( eth_globals.networking_phone, packet_get_id( packet ));
289
        }
295
        }
290
        packet = next;
296
        packet = next;
291
    }while( packet );
297
    }while( packet );
292
    rwlock_read_unlock( & eth_globals.protos_lock );
298
    rwlock_read_unlock( & eth_globals.protos_lock );
293
    return EOK;
299
    return EOK;
294
}
300
}
295
 
301
 
296
int eth_packet_space_message( device_id_t device_id, size_t * addr_len, size_t * prefix, size_t * content, size_t * suffix ){
302
int eth_packet_space_message( device_id_t device_id, size_t * addr_len, size_t * prefix, size_t * content, size_t * suffix ){
297
    eth_device_ref  device;
303
    eth_device_ref  device;
298
 
304
 
299
    if( !( addr_len && prefix && content && suffix )) return EINVAL;
305
    if( !( addr_len && prefix && content && suffix )) return EINVAL;
300
    rwlock_write_lock( & eth_globals.devices_lock );
306
    rwlock_write_lock( & eth_globals.devices_lock );
301
    device = eth_devices_find( & eth_globals.devices, device_id );
307
    device = eth_devices_find( & eth_globals.devices, device_id );
302
    if( ! device ){
308
    if( ! device ){
303
        rwlock_write_unlock( & eth_globals.devices_lock );
309
        rwlock_write_unlock( & eth_globals.devices_lock );
304
        return ENOENT;
310
        return ENOENT;
305
    }
311
    }
306
    * content = (  ETH_MAX_CONTENT > device->mtu ) ? device->mtu : ETH_MAX_CONTENT;
312
    * content = ( ETH_MAX_CONTENT > device->mtu ) ? device->mtu : ETH_MAX_CONTENT;
307
    rwlock_write_unlock( & eth_globals.devices_lock );
313
    rwlock_write_unlock( & eth_globals.devices_lock );
308
    * addr_len = ETH_ADDR;
314
    * addr_len = ETH_ADDR;
309
    * prefix = ETH_PREFIX;
315
    * prefix = ETH_PREFIX;
310
    * suffix = ETH_MIN_CONTENT + ETH_SUFFIX;
316
    * suffix = ETH_MIN_CONTENT + ETH_SUFFIX;
311
    return EOK;
317
    return EOK;
312
}
318
}
313
 
319
 
314
int eth_addr_message( device_id_t device_id, eth_addr_type_t type, measured_string_ref * address ){
320
int eth_addr_message( device_id_t device_id, eth_addr_type_t type, measured_string_ref * address ){
315
    eth_device_ref  device;
321
    eth_device_ref  device;
316
 
322
 
317
    if( ! address ) return EINVAL;
323
    if( ! address ) return EINVAL;
318
    if( type == ETH_BROADCAST_ADDR ){
324
    if( type == ETH_BROADCAST_ADDR ){
319
        * address = eth_globals.broadcast_addr;
325
        * address = eth_globals.broadcast_addr;
320
    }else{
326
    }else{
321
        rwlock_write_lock( & eth_globals.devices_lock );
327
        rwlock_write_lock( & eth_globals.devices_lock );
322
        device = eth_devices_find( & eth_globals.devices, device_id );
328
        device = eth_devices_find( & eth_globals.devices, device_id );
323
        if( ! device ){
329
        if( ! device ){
324
            rwlock_write_unlock( & eth_globals.devices_lock );
330
            rwlock_write_unlock( & eth_globals.devices_lock );
325
            return ENOENT;
331
            return ENOENT;
326
        }
332
        }
327
        * address = device->addr;
333
        * address = device->addr;
328
        rwlock_write_unlock( & eth_globals.devices_lock );
334
        rwlock_write_unlock( & eth_globals.devices_lock );
329
    }
335
    }
330
    return ( * address ) ? EOK : ENOENT;
336
    return ( * address ) ? EOK : ENOENT;
331
}
337
}
332
 
338
 
333
int eth_register_message( services_t service, int phone ){
339
int eth_register_message( services_t service, int phone ){
334
    ERROR_DECLARE;
340
    ERROR_DECLARE;
335
 
341
 
336
    eth_proto_ref   proto;
342
    eth_proto_ref   proto;
337
    int             protocol;
343
    int             protocol;
338
 
344
 
339
    protocol = protocol_map( SERVICE_ETHERNET, service );
345
    protocol = protocol_map( SERVICE_ETHERNET, service );
340
    if( ! protocol ) return ENOENT;
346
    if( ! protocol ) return ENOENT;
341
    rwlock_write_lock( & eth_globals.protos_lock );
347
    rwlock_write_lock( & eth_globals.protos_lock );
342
    proto = eth_protos_find( & eth_globals.protos, protocol );
348
    proto = eth_protos_find( & eth_globals.protos, protocol );
343
    if( proto ){
349
    if( proto ){
344
        proto->phone = phone;
350
        proto->phone = phone;
345
        rwlock_write_unlock( & eth_globals.protos_lock );
351
        rwlock_write_unlock( & eth_globals.protos_lock );
346
        return EOK;
352
        return EOK;
347
    }else{
353
    }else{
348
        proto = ( eth_proto_ref ) malloc( sizeof( eth_proto_t ));
354
        proto = ( eth_proto_ref ) malloc( sizeof( eth_proto_t ));
349
        if( ! proto ){
355
        if( ! proto ){
350
            rwlock_write_unlock( & eth_globals.protos_lock );
356
            rwlock_write_unlock( & eth_globals.protos_lock );
351
            return ENOMEM;
357
            return ENOMEM;
352
        }
358
        }
353
        proto->service = service;
359
        proto->service = service;
354
        proto->protocol = protocol;
360
        proto->protocol = protocol;
355
        proto->phone = phone;
361
        proto->phone = phone;
356
        if( ERROR_OCCURRED( eth_protos_add( & eth_globals.protos, protocol, proto ))){
362
        if( ERROR_OCCURRED( eth_protos_add( & eth_globals.protos, protocol, proto ))){
357
            rwlock_write_unlock( & eth_globals.protos_lock );
363
            rwlock_write_unlock( & eth_globals.protos_lock );
358
            free( proto );
364
            free( proto );
359
            return ERROR_CODE;
365
            return ERROR_CODE;
360
        }
366
        }
361
    }
367
    }
-
 
368
    printf( "\nNew protocol registered:\n\tprotocol\t= %d\n\tservice\t= %d\n\tphone\t= %d", proto->protocol, proto->service, proto->phone );
362
    rwlock_write_unlock( & eth_globals.protos_lock );
369
    rwlock_write_unlock( & eth_globals.protos_lock );
363
    return EOK;
370
    return EOK;
364
}
371
}
365
 
372
 
366
int eth_prepare_packet( int dummy, packet_t packet, uint8_t * src_addr, int ethertype ){
373
int eth_prepare_packet( int dummy, packet_t packet, uint8_t * src_addr, int ethertype ){
367
    eth_header_ex_ref   header;
374
    eth_header_ex_ref   header;
368
    eth_fcs_ref         fcs;
375
    eth_fcs_ref         fcs;
369
    uint8_t *           src;
376
    uint8_t *           src;
370
    uint8_t *           dest;
377
    uint8_t *           dest;
371
    int                 length;
378
    int                 length;
372
    int                 i;
379
    int                 i;
373
    void *              padding;
380
    void *              padding;
374
    eth_preamble_ref    preamble;
381
    eth_preamble_ref    preamble;
375
 
382
 
376
    if( dummy ){
383
    if( dummy ){
377
        preamble = PACKET_PREFIX( packet, eth_preamble_t );
384
        preamble = PACKET_PREFIX( packet, eth_preamble_t );
378
        if( ! preamble ) return ENOMEM;
385
        if( ! preamble ) return ENOMEM;
379
        for( i = 0; i < 7; ++ i ) preamble->preamble[ i ] = ETH_PREAMBLE;
386
        for( i = 0; i < 7; ++ i ) preamble->preamble[ i ] = ETH_PREAMBLE;
380
        preamble->sfd = ETH_SFD;
387
        preamble->sfd = ETH_SFD;
381
    }
388
    }
382
    header = PACKET_PREFIX( packet, eth_header_ex_t );
389
    header = PACKET_PREFIX( packet, eth_header_ex_t );
383
    if( ! header ) return ENOMEM;
390
    if( ! header ) return ENOMEM;
384
    length = packet_get_addr( packet, & src, & dest );
391
    length = packet_get_addr( packet, & src, & dest );
385
    if( length < 0 ) return length;
392
    if( length < 0 ) return length;
386
    if( length < ETH_ADDR ) return EINVAL;
393
    if( length < ETH_ADDR ) return EINVAL;
387
    memcpy( header->header.src, src_addr, ETH_ADDR );
394
    memcpy( header->header.src, src_addr, ETH_ADDR );
388
    memcpy( & header->header.dest, dest, ETH_ADDR );
395
    memcpy( & header->header.dest, dest, ETH_ADDR );
389
    length = packet_get_data_length( packet );
396
    length = packet_get_data_length( packet );
390
    if( length > ETH_MAX_CONTENT ) return EINVAL;
397
    if( length > ETH_MAX_CONTENT ) return EINVAL;
391
    if( length < ETH_MIN_CONTENT ){
398
    if( length < ETH_MIN_CONTENT ){
392
        padding = packet_suffix( packet, ETH_MIN_CONTENT - length );
399
        padding = packet_suffix( packet, ETH_MIN_CONTENT - length );
393
        if( ! padding ) return ENOMEM;
400
        if( ! padding ) return ENOMEM;
394
        memset( padding, 0, ETH_MIN_CONTENT - length );
401
        memset( padding, 0, ETH_MIN_CONTENT - length );
395
    }
402
    }
396
    header->header.ethertype = htons( length );
403
    header->header.ethertype = htons( length );
397
    header->lsap.dsap = 0xAA;
404
    header->lsap.dsap = 0xAA;
398
    header->lsap.ssap = header->lsap.dsap;
405
    header->lsap.ssap = header->lsap.dsap;
399
    header->lsap.ctrl = 0;
406
    header->lsap.ctrl = 0;
400
    for( i = 0; i < 3; ++ i ) header->snap.proto[ i ] = 0;
407
    for( i = 0; i < 3; ++ i ) header->snap.proto[ i ] = 0;
401
    header->snap.ethertype = ethertype;
408
    header->snap.ethertype = ethertype;
402
    if( dummy ){
409
    if( dummy ){
403
        fcs = PACKET_SUFFIX( packet, eth_fcs_t );
410
        fcs = PACKET_SUFFIX( packet, eth_fcs_t );
404
        if( ! fcs ) return ENOMEM;
411
        if( ! fcs ) return ENOMEM;
405
        * fcs = htonl( ~ compute_crc32( ~ 0, & header->header.dest, ((( void * ) fcs ) - (( void * ) & header->header.dest )) * 8 ));
412
        * fcs = htonl( ~ compute_crc32( ~ 0, & header->header.dest, ((( void * ) fcs ) - (( void * ) & header->header.dest )) * 8 ));
406
    }
413
    }
407
    return EOK;
414
    return EOK;
408
}
415
}
409
 
416
 
410
int eth_send_message( device_id_t device_id, packet_t packet, services_t sender ){
417
int eth_send_message( device_id_t device_id, packet_t packet, services_t sender ){
411
    ERROR_DECLARE;
418
    ERROR_DECLARE;
412
 
419
 
413
    eth_device_ref      device;
420
    eth_device_ref      device;
414
    packet_t            next;
421
    packet_t            next;
415
    packet_t            tmp;
422
    packet_t            tmp;
416
    int                 ethertype;
423
    int                 ethertype;
417
 
424
 
418
    ethertype = htons( protocol_map( SERVICE_ETHERNET, sender ));
425
    ethertype = htons( protocol_map( SERVICE_ETHERNET, sender ));
419
    if( ! ethertype ){
426
    if( ! ethertype ){
420
        packet_release( eth_globals.networking_phone, packet_get_id( packet ));
427
        packet_release( eth_globals.networking_phone, packet_get_id( packet ));
421
        return EINVAL;
428
        return EINVAL;
422
    }
429
    }
423
    rwlock_read_lock( & eth_globals.devices_lock );
430
    rwlock_read_lock( & eth_globals.devices_lock );
424
    device = eth_devices_find( & eth_globals.devices, device_id );
431
    device = eth_devices_find( & eth_globals.devices, device_id );
425
    if( ! device ){
432
    if( ! device ){
426
        rwlock_read_unlock( & eth_globals.devices_lock );
433
        rwlock_read_unlock( & eth_globals.devices_lock );
427
        return ENOENT;
434
        return ENOENT;
428
    }
435
    }
429
    // proccess packet queue
436
    // proccess packet queue
430
    next = packet;
437
    next = packet;
431
    do{
438
    do{
432
        if( ERROR_OCCURRED( eth_prepare_packet( device->dummy, next, ( uint8_t * ) device->addr->value, ethertype ))){
439
        if( ERROR_OCCURRED( eth_prepare_packet( device->dummy, next, ( uint8_t * ) device->addr->value, ethertype ))){
433
            // release invalid packet
440
            // release invalid packet
434
            tmp = pq_detach( next );
441
            tmp = pq_detach( next );
435
            packet_release( eth_globals.networking_phone, packet_get_id( next ));
442
            packet_release( eth_globals.networking_phone, packet_get_id( next ));
436
            next = tmp;
443
            next = tmp;
437
        }else{
444
        }else{
438
            next = pq_next( next );
445
            next = pq_next( next );
439
        }
446
        }
440
    }while( next );
447
    }while( next );
441
    // send packet queue
448
    // send packet queue
442
    async_msg_2( device->phone, NET_NETIF_SEND, device_id, packet_get_id( packet ));
449
    async_msg_2( device->phone, NET_NETIF_SEND, device_id, packet_get_id( packet ));
443
    rwlock_read_unlock( & eth_globals.devices_lock );
450
    rwlock_read_unlock( & eth_globals.devices_lock );
444
    return EOK;
451
    return EOK;
445
}
452
}
446
 
453
 
447
int eth_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
454
int eth_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
448
    ERROR_DECLARE;
455
    ERROR_DECLARE;
449
 
456
 
450
    measured_string_ref address;
457
    measured_string_ref address;
451
    packet_t            packet;
458
    packet_t            packet;
452
 
459
 
453
    * answer_count = 0;
460
    * answer_count = 0;
454
    switch( IPC_GET_METHOD( * call )){
461
    switch( IPC_GET_METHOD( * call )){
455
        case IPC_M_PHONE_HUNGUP:
462
        case IPC_M_PHONE_HUNGUP:
456
            return EOK;
463
            return EOK;
457
        case NET_NIL_DEVICE:
464
        case NET_NIL_DEVICE:
458
            return eth_device_message( IPC_GET_DEVICE( call ), IPC_GET_SERVICE( call ), IPC_GET_MTU( call ));
465
            return eth_device_message( IPC_GET_DEVICE( call ), IPC_GET_SERVICE( call ), IPC_GET_MTU( call ));
459
        case NET_NIL_SEND:
466
        case NET_NIL_SEND:
460
            ERROR_PROPAGATE( packet_translate( eth_globals.networking_phone, & packet, IPC_GET_PACKET( call )));
467
            ERROR_PROPAGATE( packet_translate( eth_globals.networking_phone, & packet, IPC_GET_PACKET( call )));
461
            return eth_send_message( IPC_GET_DEVICE( call ), packet, IPC_GET_SERVICE( call ));
468
            return eth_send_message( IPC_GET_DEVICE( call ), packet, IPC_GET_SERVICE( call ));
462
        case NET_NIL_PACKET_SPACE:
469
        case NET_NIL_PACKET_SPACE:
463
            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 )));
470
            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 )));
464
            * answer_count = 3;
471
            * answer_count = 3;
465
            return EOK;
472
            return EOK;
466
        case NET_NIL_ADDR:
473
        case NET_NIL_ADDR:
467
            rwlock_read_lock( & eth_globals.devices_lock );
474
            rwlock_read_lock( & eth_globals.devices_lock );
468
            if( ! ERROR_OCCURRED( eth_addr_message( IPC_GET_DEVICE( call ), ETH_LOCAL_ADDR, & address ))){
475
            if( ! ERROR_OCCURRED( eth_addr_message( IPC_GET_DEVICE( call ), ETH_LOCAL_ADDR, & address ))){
469
                 ERROR_CODE = measured_strings_reply( address, 1 );
476
                 ERROR_CODE = measured_strings_reply( address, 1 );
470
            }
477
            }
471
            rwlock_read_unlock( & eth_globals.devices_lock );
478
            rwlock_read_unlock( & eth_globals.devices_lock );
472
            return ERROR_CODE;
479
            return ERROR_CODE;
473
        case NET_NIL_BROADCAST_ADDR:
480
        case NET_NIL_BROADCAST_ADDR:
474
            rwlock_read_lock( & eth_globals.devices_lock );
481
            rwlock_read_lock( & eth_globals.devices_lock );
475
            if( ! ERROR_OCCURRED( eth_addr_message( IPC_GET_DEVICE( call ), ETH_BROADCAST_ADDR, & address ))){
482
            if( ! ERROR_OCCURRED( eth_addr_message( IPC_GET_DEVICE( call ), ETH_BROADCAST_ADDR, & address ))){
476
                 ERROR_CODE = measured_strings_reply( address, 1 );
483
                 ERROR_CODE = measured_strings_reply( address, 1 );
477
            }
484
            }
478
            rwlock_read_unlock( & eth_globals.devices_lock );
485
            rwlock_read_unlock( & eth_globals.devices_lock );
479
            return ERROR_CODE;
486
            return ERROR_CODE;
480
        case IPC_M_CONNECT_TO_ME:
487
        case IPC_M_CONNECT_TO_ME:
481
            return eth_register_message( IPC_GET_PROTO( call ), IPC_GET_PHONE( call ));
488
            return eth_register_message( IPC_GET_PROTO( call ), IPC_GET_PHONE( call ));
482
    }
489
    }
483
    return ENOTSUP;
490
    return ENOTSUP;
484
}
491
}
485
 
492
 
486
void eth_receiver( ipc_callid_t iid, ipc_call_t * icall ){
493
void eth_receiver( ipc_callid_t iid, ipc_call_t * icall ){
487
    ERROR_DECLARE;
494
    ERROR_DECLARE;
488
 
495
 
489
    packet_t        packet;
496
    packet_t        packet;
490
 
497
 
491
    while( true ){
498
    while( true ){
492
        switch( IPC_GET_METHOD( * icall )){
499
        switch( IPC_GET_METHOD( * icall )){
493
            case NET_NIL_DEVICE_STATE:
500
            case NET_NIL_DEVICE_STATE:
494
                //TODO clear device if off?
501
                //TODO clear device if off?
495
                break;
502
                break;
496
            case NET_NIL_RECEIVED:
503
            case NET_NIL_RECEIVED:
497
                if( ! ERROR_OCCURRED( packet_translate( eth_globals.networking_phone, & packet, IPC_GET_PACKET( icall )))){
504
                if( ! ERROR_OCCURRED( packet_translate( eth_globals.networking_phone, & packet, IPC_GET_PACKET( icall )))){
498
                    ERROR_CODE = eth_receive_message( IPC_GET_DEVICE( icall ), packet );
505
                    ERROR_CODE = eth_receive_message( IPC_GET_DEVICE( icall ), packet );
499
                }
506
                }
500
                ipc_answer_0( iid, ERROR_CODE );
507
                ipc_answer_0( iid, ERROR_CODE );
501
                break;
508
                break;
502
            default:
509
            default:
503
                ipc_answer_0( iid, ENOTSUP );
510
                ipc_answer_0( iid, ENOTSUP );
504
        }
511
        }
505
        iid = async_get_call( icall );
512
        iid = async_get_call( icall );
506
    }
513
    }
507
}
514
}
508
 
515
 
509
/** @}
516
/** @}
510
 */
517
 */
511
 
518