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