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