Subversion Repositories HelenOS

Rev

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

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