Subversion Repositories HelenOS

Rev

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

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