Rev 4271 | Rev 4327 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
3846 | mejdrech | 1 | /* |
3912 | mejdrech | 2 | * Copyright (c) 2009 Lukas Mejdrech |
3846 | 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 arp |
||
30 | * @{ |
||
31 | */ |
||
32 | |||
33 | /** @file |
||
3912 | mejdrech | 34 | * ARP module implementation. |
35 | * @see arp.h |
||
3846 | mejdrech | 36 | */ |
37 | |||
38 | #include <async.h> |
||
39 | #include <malloc.h> |
||
4192 | mejdrech | 40 | #include <mem.h> |
3991 | mejdrech | 41 | #include <rwlock.h> |
3846 | mejdrech | 42 | #include <stdio.h> |
4307 | mejdrech | 43 | #include <task.h> |
3846 | mejdrech | 44 | |
45 | #include <ipc/ipc.h> |
||
46 | #include <ipc/services.h> |
||
47 | |||
3886 | mejdrech | 48 | #include "../../err.h" |
49 | #include "../../messages.h" |
||
50 | #include "../../modules.h" |
||
3846 | mejdrech | 51 | |
4243 | mejdrech | 52 | #include "../../include/byteorder.h" |
53 | #include "../../include/device.h" |
||
4307 | mejdrech | 54 | #include "../../include/arp_interface.h" |
55 | #include "../../include/nil_interface.h" |
||
3886 | mejdrech | 56 | #include "../../include/protocol_map.h" |
3846 | mejdrech | 57 | |
3886 | mejdrech | 58 | #include "../../structures/measured_strings.h" |
59 | #include "../../structures/packet/packet.h" |
||
3901 | mejdrech | 60 | #include "../../structures/packet/packet_client.h" |
3886 | mejdrech | 61 | |
4307 | mejdrech | 62 | #include "../il_messages.h" |
63 | |||
3846 | mejdrech | 64 | #include "arp.h" |
65 | #include "arp_header.h" |
||
66 | #include "arp_oc.h" |
||
67 | #include "arp_module.h" |
||
4307 | mejdrech | 68 | #include "arp_messages.h" |
3846 | mejdrech | 69 | |
3912 | mejdrech | 70 | /** ARP global data. |
71 | */ |
||
3846 | mejdrech | 72 | arp_globals_t arp_globals; |
73 | |||
4307 | mejdrech | 74 | int arp_device_req( int arp_phone, device_id_t device_id, services_t protocol, services_t netif, measured_string_ref address ); |
75 | int arp_translate_req( int arp_phone, device_id_t device_id, services_t protocol, measured_string_ref address, measured_string_ref * translation, char ** data ); |
||
76 | |||
77 | /** Clears the whole cache. |
||
78 | * @returns EOK on success. |
||
79 | */ |
||
80 | int arp_clean_cache_req( int arp_phone ); |
||
81 | |||
82 | /** Clears the device specific data from the cache. |
||
83 | * @param device_id The device identifier. Input parameter. |
||
84 | * @returns EOK on success. |
||
85 | * @returns ENOENT if the device is not found in the cache. |
||
86 | */ |
||
87 | int arp_clear_device_req( int arp_phone, device_id_t device_id ); |
||
88 | |||
3912 | mejdrech | 89 | /** Creates new protocol specific data. |
90 | * @param proto Protocol specific data. Output parameter. |
||
91 | * @param service Protocol module service. Input parameter. |
||
92 | * @param address Actual protocol device address. Input parameter. |
||
93 | * @returns EOK on success. |
||
94 | * @returns ENOMEM if there is not enough memory left. |
||
95 | */ |
||
96 | int arp_proto_create( arp_proto_ref * proto, services_t service, measured_string_ref address ); |
||
97 | |||
98 | /** Registers the device. |
||
99 | * Creates new device entry in the cache or updates the protocol address if the device with the device identifier and the driver service exists. |
||
100 | * @param device_id The device identifier. Input parameter. |
||
101 | * @param service The device driver service. Input parameter. |
||
102 | * @param protocol The protocol service. Input parameter. |
||
103 | * @param address The actual device protocol address. |
||
104 | * @returns EOK on success. |
||
105 | * @returns EEXIST if another device with the same device identifier and different driver service exists. |
||
106 | * @returns ENOMEM if there is not enough memory left. |
||
107 | * @returns Other error codes as defined for the measured_strings_return() function. |
||
108 | */ |
||
109 | int arp_device_message( device_id_t device_id, services_t service, services_t protocol, measured_string_ref address ); |
||
110 | |||
111 | /** Returns the hardware address for the given protocol address. |
||
112 | * Sends the ARP request packet if the hardware address is not found in the cache. |
||
113 | * @param device_id The device identifier. Input parameter. |
||
114 | * @param protocol The protocol service. Input parameter. |
||
115 | * @param target The target protocol address. Input parameter. |
||
116 | * @returns The hardware address of the target. |
||
117 | * @returns NULL if the target parameter is NULL. |
||
118 | * @returns NULL if the device is not found. |
||
119 | * @returns NULL if the device packet is too small to send a request. |
||
120 | * @returns NULL if the hardware address is not found in the cache. |
||
121 | */ |
||
122 | measured_string_ref arp_translate_message( device_id_t device_id, services_t protocol, measured_string_ref target ); |
||
123 | |||
124 | /** Processes the received ARP packet. |
||
125 | * Updates the source hardware address if the source entry exists or the packet is targeted to my protocol address. |
||
126 | * Responses to the ARP request if the packet is the ARP request and is targeted to my address. |
||
127 | * @param device_id The source device identifier. Input parameter. |
||
128 | * @param packet The received packet. Input/output parameter. |
||
129 | * @returns EOK on success. |
||
130 | * @returns EINVAL if the packet is too small to carry the ARP packet. |
||
131 | * @returns EINVAL if the received address lengths differs from the registered values. |
||
132 | * @returns ENOENT if the device is not found in the cache. |
||
133 | * @returns ENOENT if the protocol for the device is not found in the cache. |
||
134 | * @returns ENOMEM if there is not enough memory left. |
||
135 | */ |
||
136 | int arp_receive_message( device_id_t device_id, packet_t packet ); |
||
137 | |||
138 | /** Clears the device specific data. |
||
139 | * @param device The device specific data. |
||
140 | */ |
||
141 | void clear_device( arp_device_ref device ); |
||
142 | |||
143 | /** Processes IPC messages from the registered device driver modules in an infinite loop. |
||
144 | * @param iid The message identifier. Input parameter. |
||
145 | * @param icall The message parameters. Input/output parameter. |
||
146 | */ |
||
147 | void arp_receiver( ipc_callid_t iid, ipc_call_t * icall ); |
||
148 | |||
3846 | mejdrech | 149 | DEVICE_MAP_IMPLEMENT( arp_cache, arp_device_t ) |
150 | |||
151 | INT_MAP_IMPLEMENT( arp_protos, arp_proto_t ) |
||
152 | |||
153 | GENERIC_CHAR_MAP_IMPLEMENT( arp_addr, measured_string_t ) |
||
154 | |||
4307 | mejdrech | 155 | int arp_task_get_id( void ){ |
156 | return task_get_id(); |
||
157 | } |
||
158 | |||
159 | int arp_clear_device_req( int arp_phone, device_id_t device_id ){ |
||
4271 | mejdrech | 160 | arp_device_ref device; |
161 | |||
162 | rwlock_write_lock( & arp_globals.lock ); |
||
163 | device = arp_cache_find( & arp_globals.cache, device_id ); |
||
164 | if( ! device ){ |
||
165 | rwlock_write_unlock( & arp_globals.lock ); |
||
166 | return ENOENT; |
||
167 | } |
||
168 | clear_device( device ); |
||
4307 | mejdrech | 169 | printf( "Device %d cleared\n", device_id ); |
4271 | mejdrech | 170 | rwlock_write_unlock( & arp_globals.lock ); |
171 | return EOK; |
||
172 | } |
||
173 | |||
4307 | mejdrech | 174 | int arp_clean_cache_req( int arp_phone ){ |
4271 | mejdrech | 175 | int count; |
176 | arp_device_ref device; |
||
177 | |||
178 | rwlock_write_lock( & arp_globals.lock ); |
||
179 | for( count = arp_cache_count( & arp_globals.cache ) - 1; count >= 0; -- count ){ |
||
180 | device = arp_cache_get_index( & arp_globals.cache, count ); |
||
181 | if( device ){ |
||
182 | clear_device( device ); |
||
183 | if( device->addr_data ) free( device->addr_data ); |
||
184 | if( device->broadcast_data ) free( device->broadcast_data ); |
||
185 | } |
||
186 | } |
||
187 | arp_cache_clear( & arp_globals.cache ); |
||
188 | rwlock_write_unlock( & arp_globals.lock ); |
||
4307 | mejdrech | 189 | printf( "Cache cleaned\n" ); |
4271 | mejdrech | 190 | return EOK; |
191 | } |
||
192 | |||
4307 | mejdrech | 193 | int arp_device_req( int arp_phone, device_id_t device_id, services_t protocol, services_t netif, measured_string_ref address ){ |
4271 | mejdrech | 194 | ERROR_DECLARE; |
195 | |||
196 | measured_string_ref tmp; |
||
197 | |||
198 | tmp = measured_string_copy( address ); |
||
199 | if( ERROR_OCCURRED( arp_device_message( device_id, netif, protocol, tmp ))){ |
||
200 | free( tmp->value ); |
||
201 | free( tmp ); |
||
202 | } |
||
203 | return ERROR_CODE; |
||
204 | } |
||
205 | |||
4307 | mejdrech | 206 | int arp_translate_req( int arp_phone, device_id_t device_id, services_t protocol, measured_string_ref address, measured_string_ref * translation, char ** data ){ |
4271 | mejdrech | 207 | measured_string_ref tmp; |
208 | |||
209 | rwlock_read_lock( & arp_globals.lock ); |
||
210 | tmp = arp_translate_message( device_id, protocol, address ); |
||
211 | if( tmp ){ |
||
212 | * translation = measured_string_copy( tmp ); |
||
213 | rwlock_read_unlock( & arp_globals.lock ); |
||
214 | if( * translation ){ |
||
215 | * data = ( ** translation ).value; |
||
216 | return EOK; |
||
217 | }else{ |
||
218 | return ENOMEM; |
||
219 | } |
||
220 | }else{ |
||
221 | rwlock_read_unlock( & arp_globals.lock ); |
||
222 | return ENOENT; |
||
223 | } |
||
224 | } |
||
225 | |||
3846 | mejdrech | 226 | int arp_initialize( void ){ |
3991 | mejdrech | 227 | ERROR_DECLARE; |
228 | |||
229 | rwlock_initialize( & arp_globals.lock ); |
||
230 | rwlock_write_lock( & arp_globals.lock ); |
||
231 | ERROR_PROPAGATE( arp_cache_initialize( & arp_globals.cache )); |
||
232 | rwlock_write_unlock( & arp_globals.lock ); |
||
233 | return EOK; |
||
3846 | mejdrech | 234 | } |
235 | |||
236 | int arp_proto_create( arp_proto_ref * proto, services_t service, measured_string_ref address ){ |
||
237 | ERROR_DECLARE; |
||
238 | |||
239 | * proto = ( arp_proto_ref ) malloc( sizeof( arp_proto_t )); |
||
240 | if( !( * proto )) return ENOMEM; |
||
241 | ( ** proto ).service = service; |
||
242 | ( ** proto ).addr = address; |
||
243 | ( ** proto ).addr_data = address->value; |
||
3912 | mejdrech | 244 | if( ERROR_OCCURRED( arp_addr_initialize( &( ** proto).addresses ))){ |
3846 | mejdrech | 245 | free( * proto ); |
246 | return ERROR_CODE; |
||
247 | } |
||
248 | return EOK; |
||
249 | } |
||
250 | |||
251 | int arp_device_message( device_id_t device_id, services_t service, services_t protocol, measured_string_ref address ){ |
||
252 | ERROR_DECLARE; |
||
253 | |||
254 | arp_device_ref device; |
||
255 | arp_proto_ref proto; |
||
4192 | mejdrech | 256 | int index; |
3846 | mejdrech | 257 | |
3991 | mejdrech | 258 | rwlock_write_lock( & arp_globals.lock ); |
3846 | mejdrech | 259 | // an existing device? |
260 | device = arp_cache_find( & arp_globals.cache, device_id ); |
||
261 | if( device ){ |
||
3991 | mejdrech | 262 | if( device->service != service ){ |
4307 | mejdrech | 263 | printf( "Device %d already exists\n", device->device_id ); |
3991 | mejdrech | 264 | rwlock_write_unlock( & arp_globals.lock ); |
265 | return EEXIST; |
||
266 | } |
||
3846 | mejdrech | 267 | proto = arp_protos_find( & device->protos, protocol ); |
268 | if( proto ){ |
||
269 | free( proto->addr ); |
||
270 | free( proto->addr_data ); |
||
271 | proto->addr = address; |
||
272 | proto->addr_data = address->value; |
||
273 | }else{ |
||
3991 | mejdrech | 274 | if( ERROR_OCCURRED( arp_proto_create( & proto, protocol, address ))){ |
275 | rwlock_write_unlock( & arp_globals.lock ); |
||
276 | return ERROR_CODE; |
||
277 | } |
||
4192 | mejdrech | 278 | index = arp_protos_add( & device->protos, proto->service, proto ); |
279 | if( index < 0 ){ |
||
3991 | mejdrech | 280 | rwlock_write_unlock( & arp_globals.lock ); |
3846 | mejdrech | 281 | free( proto ); |
4261 | mejdrech | 282 | return index; |
3846 | mejdrech | 283 | } |
284 | } |
||
4307 | mejdrech | 285 | printf( "Cache of the existing device %d cleaned\n", device->device_id ); |
3846 | mejdrech | 286 | }else{ |
4243 | mejdrech | 287 | index = hardware_map( service ); |
4192 | mejdrech | 288 | if( ! index ) return ENOENT; |
3846 | mejdrech | 289 | // create a new device |
290 | device = ( arp_device_ref ) malloc( sizeof( arp_device_t )); |
||
3991 | mejdrech | 291 | if( ! device ){ |
292 | rwlock_write_unlock( & arp_globals.lock ); |
||
293 | return ENOMEM; |
||
294 | } |
||
4192 | mejdrech | 295 | device->hardware = index; |
3846 | mejdrech | 296 | device->device_id = device_id; |
3912 | mejdrech | 297 | if( ERROR_OCCURRED( arp_protos_initialize( & device->protos )) |
298 | || ERROR_OCCURRED( arp_proto_create( & proto, protocol, address ))){ |
||
3991 | mejdrech | 299 | rwlock_write_unlock( & arp_globals.lock ); |
3846 | mejdrech | 300 | free( device ); |
301 | return ERROR_CODE; |
||
302 | } |
||
4192 | mejdrech | 303 | index = arp_protos_add( & device->protos, proto->service, proto ); |
304 | if( index < 0 ){ |
||
3991 | mejdrech | 305 | rwlock_write_unlock( & arp_globals.lock ); |
3846 | mejdrech | 306 | arp_protos_destroy( & device->protos ); |
307 | free( device ); |
||
4192 | mejdrech | 308 | return index; |
3846 | mejdrech | 309 | } |
310 | device->service = service; |
||
311 | // bind the new one |
||
312 | device->phone = bind_service( device->service, device->device_id, SERVICE_ARP, 0, arp_receiver ); |
||
4192 | mejdrech | 313 | if( device->phone < 0 ){ |
314 | rwlock_write_unlock( & arp_globals.lock ); |
||
315 | arp_protos_destroy( & device->protos ); |
||
316 | free( device ); |
||
317 | return EREFUSED; |
||
318 | } |
||
3846 | mejdrech | 319 | // get packet dimensions |
4261 | mejdrech | 320 | if( ERROR_OCCURRED( nil_packet_size_req( device->phone, device_id, & device->addr_len, & device->prefix, & device->content, & device->suffix ))){ |
3991 | mejdrech | 321 | rwlock_write_unlock( & arp_globals.lock ); |
3846 | mejdrech | 322 | arp_protos_destroy( & device->protos ); |
323 | free( device ); |
||
324 | return ERROR_CODE; |
||
325 | } |
||
326 | // get hardware address |
||
4261 | mejdrech | 327 | if( ERROR_OCCURRED( nil_get_addr( device->phone, device_id, & device->addr, & device->addr_data ))){ |
3991 | mejdrech | 328 | rwlock_write_unlock( & arp_globals.lock ); |
3846 | mejdrech | 329 | arp_protos_destroy( & device->protos ); |
330 | free( device ); |
||
331 | return ERROR_CODE; |
||
332 | } |
||
333 | // get broadcast address |
||
4261 | mejdrech | 334 | if( ERROR_OCCURRED( nil_get_broadcast_addr( device->phone, device_id, & device->broadcast_addr, & device->broadcast_data ))){ |
3991 | mejdrech | 335 | rwlock_write_unlock( & arp_globals.lock ); |
3846 | mejdrech | 336 | free( device->addr ); |
337 | free( device->addr_data ); |
||
338 | arp_protos_destroy( & device->protos ); |
||
339 | free( device ); |
||
340 | return ERROR_CODE; |
||
341 | } |
||
4261 | mejdrech | 342 | if( ERROR_OCCURRED( arp_cache_add( & arp_globals.cache, device->device_id, device ))){ |
3991 | mejdrech | 343 | rwlock_write_unlock( & arp_globals.lock ); |
3846 | mejdrech | 344 | free( device->addr ); |
345 | free( device->addr_data ); |
||
346 | free( device->broadcast_addr ); |
||
347 | free( device->broadcast_data ); |
||
348 | arp_protos_destroy( & device->protos ); |
||
349 | free( device ); |
||
350 | return ERROR_CODE; |
||
351 | } |
||
4307 | mejdrech | 352 | printf( "New device registered:\n\tid\t= %d\n\ttype\t= 0x%x\n\tservice\t= %d\n", device->device_id, device->hardware, device->service ); |
3846 | mejdrech | 353 | } |
3991 | mejdrech | 354 | rwlock_write_unlock( & arp_globals.lock ); |
3846 | mejdrech | 355 | return EOK; |
356 | } |
||
357 | |||
358 | measured_string_ref arp_translate_message( device_id_t device_id, services_t protocol, measured_string_ref target ){ |
||
359 | arp_device_ref device; |
||
360 | arp_proto_ref proto; |
||
361 | measured_string_ref addr; |
||
362 | size_t length; |
||
363 | packet_t packet; |
||
364 | arp_header_ref header; |
||
365 | |||
366 | if( ! target ) return NULL; |
||
367 | device = arp_cache_find( & arp_globals.cache, device_id ); |
||
4243 | mejdrech | 368 | if( ! device ) return NULL; |
3846 | mejdrech | 369 | proto = arp_protos_find( & device->protos, protocol ); |
4243 | mejdrech | 370 | if(( ! proto ) || ( proto->addr->length != target->length )) return NULL; |
3846 | mejdrech | 371 | addr = arp_addr_find( & proto->addresses, target->value, target->length ); |
4243 | mejdrech | 372 | if( addr ) return addr; |
3846 | mejdrech | 373 | // ARP packet content size = header + ( address + translation ) * 2 |
374 | length = 8 + ( CONVERT_SIZE( char, uint8_t, proto->addr->length ) + CONVERT_SIZE( char, uint8_t, device->addr->length )) * 2; |
||
4243 | mejdrech | 375 | if( length > device->content ) return NULL; |
4307 | mejdrech | 376 | packet = packet_get_4( arp_globals.net_phone, device->addr_len, device->prefix, length, device->suffix ); |
4243 | mejdrech | 377 | if( ! packet ) return NULL; |
3912 | mejdrech | 378 | header = ( arp_header_ref ) packet_suffix( packet, length ); |
4243 | mejdrech | 379 | header->hardware = htons( device->hardware ); |
3846 | mejdrech | 380 | header->hardware_length = device->addr->length; |
4243 | mejdrech | 381 | header->protocol = htons( protocol_map( device->service, protocol )); |
3846 | mejdrech | 382 | header->protocol_length = proto->addr->length; |
4243 | mejdrech | 383 | header->operation = htons( ARPOP_REQUEST ); |
3846 | mejdrech | 384 | length = sizeof( arp_header_t ); |
385 | memcpy((( uint8_t * ) header ) + length, device->addr->value, device->addr->length ); |
||
386 | length += device->addr->length; |
||
387 | memcpy((( uint8_t * ) header ) + length, proto->addr->value, proto->addr->length ); |
||
388 | length += proto->addr->length; |
||
4192 | mejdrech | 389 | bzero((( uint8_t * ) header ) + length, device->addr->length ); |
3846 | mejdrech | 390 | length += device->addr->length; |
391 | memcpy((( uint8_t * ) header ) + length, target->value, target->length ); |
||
3912 | mejdrech | 392 | packet_set_addr( packet, ( uint8_t * ) device->addr->value, ( uint8_t * ) device->broadcast_addr->value, CONVERT_SIZE( char, uint8_t, device->addr->length )); |
4261 | mejdrech | 393 | nil_send_msg( device->phone, device_id, packet, SERVICE_ARP ); |
3846 | mejdrech | 394 | return NULL; |
395 | } |
||
396 | |||
397 | int arp_receive_message( device_id_t device_id, packet_t packet ){ |
||
398 | ERROR_DECLARE; |
||
399 | |||
400 | size_t length; |
||
401 | arp_header_ref header; |
||
402 | arp_device_ref device; |
||
403 | arp_proto_ref proto; |
||
404 | measured_string_ref hw_source; |
||
3991 | mejdrech | 405 | uint8_t * src_hw; |
3912 | mejdrech | 406 | uint8_t * src_proto; |
407 | uint8_t * des_hw; |
||
408 | uint8_t * des_proto; |
||
3846 | mejdrech | 409 | |
410 | length = packet_get_data_length( packet ); |
||
411 | if( length <= sizeof( arp_header_t )) return EINVAL; |
||
412 | device = arp_cache_find( & arp_globals.cache, device_id ); |
||
4243 | mejdrech | 413 | if( ! device ) return ENOENT; |
3846 | mejdrech | 414 | header = ( arp_header_ref ) packet_get_data( packet ); |
4243 | mejdrech | 415 | if(( ntohs( header->hardware ) != device->hardware ) |
3991 | mejdrech | 416 | || ( length < sizeof( arp_header_t ) + ( header->hardware_length + header->protocol_length ) * 2 )){ |
417 | return EINVAL; |
||
418 | } |
||
4243 | mejdrech | 419 | proto = arp_protos_find( & device->protos, protocol_unmap( device->service, ntohs( header->protocol ))); |
420 | if( ! proto ) return ENOENT; |
||
3912 | mejdrech | 421 | src_hw = (( uint8_t * ) header ) + sizeof( arp_header_t ); |
3846 | mejdrech | 422 | src_proto = src_hw + header->hardware_length; |
423 | des_hw = src_proto + header->protocol_length; |
||
424 | des_proto = des_hw + header->hardware_length; |
||
3912 | mejdrech | 425 | hw_source = arp_addr_find( & proto->addresses, ( char * ) src_proto, CONVERT_SIZE( uint8_t, char, header->protocol_length )); |
3846 | mejdrech | 426 | // exists? |
427 | if( hw_source ){ |
||
3991 | mejdrech | 428 | if( hw_source->length != CONVERT_SIZE( uint8_t, char, header->hardware_length )){ |
429 | return EINVAL; |
||
430 | } |
||
3912 | mejdrech | 431 | memcpy( hw_source->value, src_hw, hw_source->length ); |
3846 | mejdrech | 432 | } |
433 | // is my protocol address? |
||
3991 | mejdrech | 434 | if( proto->addr->length != CONVERT_SIZE( uint8_t, char, header->hardware_length )){ |
435 | return EINVAL; |
||
3846 | mejdrech | 436 | } |
3912 | mejdrech | 437 | if( ! strncmp( proto->addr->value, ( char * ) des_proto, proto->addr->length )){ |
3846 | mejdrech | 438 | // not already upadted? |
439 | if( ! hw_source ){ |
||
3912 | mejdrech | 440 | hw_source = measured_string_create_bulk(( char * ) src_hw, CONVERT_SIZE( uint8_t, char, header->hardware_length )); |
4243 | mejdrech | 441 | if( ! hw_source ) return ENOMEM; |
442 | ERROR_PROPAGATE( arp_addr_add( & proto->addresses, ( char * ) src_proto, CONVERT_SIZE( uint8_t, char, header->protocol_length ), hw_source )); |
||
3846 | mejdrech | 443 | } |
4243 | mejdrech | 444 | if( ntohs( header->operation ) == ARPOP_REQUEST ){ |
445 | header->operation = htons( ARPOP_REPLY ); |
||
3991 | mejdrech | 446 | memcpy( des_proto, src_proto, header->protocol_length ); |
3846 | mejdrech | 447 | memcpy( src_proto, proto->addr->value, header->protocol_length ); |
448 | memcpy( src_hw, des_hw, header->hardware_length ); |
||
3912 | mejdrech | 449 | memcpy( des_hw, hw_source->value, header->hardware_length ); |
450 | packet_set_addr( packet, src_hw, des_hw, header->hardware_length ); |
||
4261 | mejdrech | 451 | nil_send_msg( device->phone, device_id, packet, SERVICE_ARP ); |
3901 | mejdrech | 452 | }else{ |
4307 | mejdrech | 453 | pq_release( arp_globals.net_phone, packet_get_id( packet )); |
3846 | mejdrech | 454 | } |
455 | } |
||
456 | return EOK; |
||
457 | } |
||
458 | |||
459 | void clear_device( arp_device_ref device ){ |
||
460 | int count; |
||
461 | arp_proto_ref proto; |
||
462 | |||
4192 | mejdrech | 463 | for( count = arp_protos_count( & device->protos ) - 1; count >= 0; -- count ){ |
3846 | mejdrech | 464 | proto = arp_protos_get_index( & device->protos, count ); |
4192 | mejdrech | 465 | if( proto ){ |
466 | if( proto->addr ) free( proto->addr ); |
||
467 | if( proto->addr_data ) free( proto->addr_data ); |
||
468 | arp_addr_destroy( & proto->addresses ); |
||
469 | } |
||
3846 | mejdrech | 470 | } |
471 | arp_protos_clear( & device->protos ); |
||
472 | } |
||
473 | |||
4307 | mejdrech | 474 | int arp_connect_module( services_t service ){ |
475 | return EOK; |
||
476 | } |
||
477 | |||
3846 | mejdrech | 478 | int arp_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){ |
479 | ERROR_DECLARE; |
||
480 | |||
481 | measured_string_ref address; |
||
482 | measured_string_ref translation; |
||
483 | char * data; |
||
484 | |||
4307 | mejdrech | 485 | // printf( "message %d - %d\n", IPC_GET_METHOD( * call ), NET_ARP_FIRST ); |
3846 | mejdrech | 486 | * answer_count = 0; |
487 | switch( IPC_GET_METHOD( * call )){ |
||
488 | case IPC_M_PHONE_HUNGUP: |
||
489 | return EOK; |
||
490 | case NET_ARP_DEVICE: |
||
491 | ERROR_PROPAGATE( measured_strings_receive( & address, & data, 1 )); |
||
4307 | mejdrech | 492 | if( ERROR_OCCURRED( arp_device_message( IPC_GET_DEVICE( call ), IPC_GET_SERVICE( call ), ARP_GET_NETIF( call ), address ))){ |
3846 | mejdrech | 493 | free( address ); |
494 | free( data ); |
||
495 | } |
||
496 | return ERROR_CODE; |
||
497 | case NET_ARP_TRANSLATE: |
||
498 | ERROR_PROPAGATE( measured_strings_receive( & address, & data, 1 )); |
||
4243 | mejdrech | 499 | rwlock_read_lock( & arp_globals.lock ); |
4307 | mejdrech | 500 | translation = arp_translate_message( IPC_GET_DEVICE( call ), IPC_GET_SERVICE( call ), address ); |
3846 | mejdrech | 501 | free( address ); |
502 | free( data ); |
||
4243 | mejdrech | 503 | if( ! translation ){ |
504 | rwlock_read_unlock( & arp_globals.lock ); |
||
505 | return ENOENT; |
||
506 | } |
||
507 | ERROR_CODE = measured_strings_reply( translation, 1 ); |
||
508 | rwlock_read_unlock( & arp_globals.lock ); |
||
509 | return ERROR_CODE; |
||
3846 | mejdrech | 510 | case NET_ARP_CLEAR_DEVICE: |
4307 | mejdrech | 511 | return arp_clear_device_req( 0, IPC_GET_DEVICE( call )); |
3846 | mejdrech | 512 | case NET_ARP_CLEAN_CACHE: |
4307 | mejdrech | 513 | return arp_clean_cache_req( 0 ); |
3846 | mejdrech | 514 | } |
515 | return ENOTSUP; |
||
516 | } |
||
517 | |||
518 | void arp_receiver( ipc_callid_t iid, ipc_call_t * icall ){ |
||
519 | ERROR_DECLARE; |
||
520 | |||
521 | packet_t packet; |
||
522 | |||
523 | while( true ){ |
||
3912 | mejdrech | 524 | switch( IPC_GET_METHOD( * icall )){ |
3846 | mejdrech | 525 | case NET_IL_DEVICE_STATE: |
3991 | mejdrech | 526 | // do nothing - keep the cache |
527 | ipc_answer_0( iid, EOK ); |
||
3846 | mejdrech | 528 | break; |
529 | case NET_IL_RECEIVED: |
||
4307 | mejdrech | 530 | if( ! ERROR_OCCURRED( packet_translate( arp_globals.net_phone, & packet, IPC_GET_PACKET( icall )))){ |
4243 | mejdrech | 531 | rwlock_read_lock( & arp_globals.lock ); |
3912 | mejdrech | 532 | ERROR_CODE = arp_receive_message( IPC_GET_DEVICE( icall ), packet ); |
4243 | mejdrech | 533 | rwlock_read_unlock( & arp_globals.lock ); |
3846 | mejdrech | 534 | } |
3912 | mejdrech | 535 | ipc_answer_0( iid, ERROR_CODE ); |
3846 | mejdrech | 536 | break; |
537 | default: |
||
3912 | mejdrech | 538 | ipc_answer_0( iid, ENOTSUP ); |
3846 | mejdrech | 539 | } |
3912 | mejdrech | 540 | iid = async_get_call( icall ); |
3846 | mejdrech | 541 | } |
542 | } |
||
543 | |||
544 | /** @} |
||
545 | */ |