Subversion Repositories HelenOS

Rev

Rev 4743 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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