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