Changes in uspace/srv/net/il/arp/arp.c [aadf01e:91478aa] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/srv/net/il/arp/arp.c
raadf01e r91478aa 81 81 * @returns ENOMEM if there is not enough memory left. 82 82 */ 83 int arp_proto_create(arp_proto_ref * proto, services_t service, measured_string_ref address);83 int arp_proto_create( arp_proto_ref * proto, services_t service, measured_string_ref address ); 84 84 85 85 /** Clears the device specific data. 86 86 * @param[in] device The device specific data. 87 87 */ 88 void arp_clear_device(arp_device_ref device);88 void arp_clear_device( arp_device_ref device ); 89 89 90 90 /** @name Message processing functions … … 103 103 * @returns Other error codes as defined for the measured_strings_return() function. 104 104 */ 105 int arp_device_message(device_id_t device_id, services_t service, services_t protocol, measured_string_ref address);105 int arp_device_message( device_id_t device_id, services_t service, services_t protocol, measured_string_ref address ); 106 106 107 107 /** Returns the hardware address for the given protocol address. … … 116 116 * @returns NULL if the hardware address is not found in the cache. 117 117 */ 118 measured_string_ref arp_translate_message(device_id_t device_id, services_t protocol, measured_string_ref target);118 measured_string_ref arp_translate_message( device_id_t device_id, services_t protocol, measured_string_ref target ); 119 119 120 120 /** Processes the received ARP packet. … … 131 131 * @returns ENOMEM if there is not enough memory left. 132 132 */ 133 int arp_receive_message(device_id_t device_id, packet_t packet);133 int arp_receive_message( device_id_t device_id, packet_t packet ); 134 134 135 135 /** Updates the device content length according to the new MTU value. … … 139 139 * @returns EOK on success. 140 140 */ 141 int arp_mtu_changed_message(device_id_t device_id, size_t mtu);141 int arp_mtu_changed_message( device_id_t device_id, size_t mtu ); 142 142 143 143 /*@}*/ 144 144 145 DEVICE_MAP_IMPLEMENT( arp_cache, arp_device_t)146 147 INT_MAP_IMPLEMENT( arp_protos, arp_proto_t)148 149 GENERIC_CHAR_MAP_IMPLEMENT( arp_addr, measured_string_t)150 151 task_id_t arp_task_get_id( void){145 DEVICE_MAP_IMPLEMENT( arp_cache, arp_device_t ) 146 147 INT_MAP_IMPLEMENT( arp_protos, arp_proto_t ) 148 149 GENERIC_CHAR_MAP_IMPLEMENT( arp_addr, measured_string_t ) 150 151 task_id_t arp_task_get_id( void ){ 152 152 return task_get_id(); 153 153 } 154 154 155 int arp_clear_device_req( int arp_phone, device_id_t device_id){156 arp_device_ref 157 158 fibril_rwlock_write_lock( &arp_globals.lock);159 device = arp_cache_find( &arp_globals.cache, device_id);160 if( ! device){161 fibril_rwlock_write_unlock( &arp_globals.lock);155 int arp_clear_device_req( int arp_phone, device_id_t device_id ){ 156 arp_device_ref device; 157 158 fibril_rwlock_write_lock( & arp_globals.lock ); 159 device = arp_cache_find( & arp_globals.cache, device_id ); 160 if( ! device ){ 161 fibril_rwlock_write_unlock( & arp_globals.lock ); 162 162 return ENOENT; 163 163 } 164 arp_clear_device( device);165 printf( "Device %d cleared\n", device_id);166 fibril_rwlock_write_unlock( &arp_globals.lock);167 return EOK; 168 } 169 170 int arp_clear_address_req( int arp_phone, device_id_t device_id, services_t protocol, measured_string_ref address){171 arp_device_ref 172 arp_proto_ref 173 174 fibril_rwlock_write_lock( &arp_globals.lock);175 device = arp_cache_find( &arp_globals.cache, device_id);176 if( ! device){177 fibril_rwlock_write_unlock( &arp_globals.lock);164 arp_clear_device( device ); 165 printf( "Device %d cleared\n", device_id ); 166 fibril_rwlock_write_unlock( & arp_globals.lock ); 167 return EOK; 168 } 169 170 int arp_clear_address_req( int arp_phone, device_id_t device_id, services_t protocol, measured_string_ref address ){ 171 arp_device_ref device; 172 arp_proto_ref proto; 173 174 fibril_rwlock_write_lock( & arp_globals.lock ); 175 device = arp_cache_find( & arp_globals.cache, device_id ); 176 if( ! device ){ 177 fibril_rwlock_write_unlock( & arp_globals.lock ); 178 178 return ENOENT; 179 179 } 180 proto = arp_protos_find( &device->protos, protocol);181 if( ! proto){182 fibril_rwlock_write_unlock( &arp_globals.lock);180 proto = arp_protos_find( & device->protos, protocol ); 181 if( ! proto ){ 182 fibril_rwlock_write_unlock( & arp_globals.lock ); 183 183 return ENOENT; 184 184 } 185 arp_addr_exclude(&proto->addresses, address->value, address->length); 186 fibril_rwlock_write_unlock(&arp_globals.lock); 187 return EOK; 188 } 189 190 int arp_clean_cache_req(int arp_phone){ 191 int count; 192 arp_device_ref device; 193 194 fibril_rwlock_write_lock(&arp_globals.lock); 195 for(count = arp_cache_count(&arp_globals.cache) - 1; count >= 0; -- count){ 196 device = arp_cache_get_index(&arp_globals.cache, count); 197 if(device){ 198 arp_clear_device(device); 199 if(device->addr_data){ 200 free(device->addr_data); 201 } 202 if(device->broadcast_data){ 203 free(device->broadcast_data); 204 } 205 } 206 } 207 arp_cache_clear(&arp_globals.cache); 208 fibril_rwlock_write_unlock(&arp_globals.lock); 209 printf("Cache cleaned\n"); 210 return EOK; 211 } 212 213 int arp_device_req(int arp_phone, device_id_t device_id, services_t protocol, services_t netif, measured_string_ref address){ 185 arp_addr_exclude( & proto->addresses, address->value, address->length ); 186 fibril_rwlock_write_unlock( & arp_globals.lock ); 187 return EOK; 188 } 189 190 int arp_clean_cache_req( int arp_phone ){ 191 int count; 192 arp_device_ref device; 193 194 fibril_rwlock_write_lock( & arp_globals.lock ); 195 for( count = arp_cache_count( & arp_globals.cache ) - 1; count >= 0; -- count ){ 196 device = arp_cache_get_index( & arp_globals.cache, count ); 197 if( device ){ 198 arp_clear_device( device ); 199 if( device->addr_data ) free( device->addr_data ); 200 if( device->broadcast_data ) free( device->broadcast_data ); 201 } 202 } 203 arp_cache_clear( & arp_globals.cache ); 204 fibril_rwlock_write_unlock( & arp_globals.lock ); 205 printf( "Cache cleaned\n" ); 206 return EOK; 207 } 208 209 int arp_device_req( int arp_phone, device_id_t device_id, services_t protocol, services_t netif, measured_string_ref address ){ 214 210 ERROR_DECLARE; 215 211 … … 217 213 218 214 // copy the given address for exclusive use 219 tmp = measured_string_copy( address);220 if( ERROR_OCCURRED(arp_device_message(device_id, netif, protocol, tmp))){221 free( tmp->value);222 free( tmp);215 tmp = measured_string_copy( address ); 216 if( ERROR_OCCURRED( arp_device_message( device_id, netif, protocol, tmp ))){ 217 free( tmp->value ); 218 free( tmp ); 223 219 } 224 220 return ERROR_CODE; 225 221 } 226 222 227 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){228 measured_string_ref 229 230 fibril_rwlock_read_lock( &arp_globals.lock);231 tmp = arp_translate_message( device_id, protocol, address);232 if( tmp){233 * translation = measured_string_copy(tmp);234 fibril_rwlock_read_unlock( &arp_globals.lock);235 if( *translation){236 * data = (** translation).value;223 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 ){ 224 measured_string_ref tmp; 225 226 fibril_rwlock_read_lock( & arp_globals.lock ); 227 tmp = arp_translate_message( device_id, protocol, address ); 228 if( tmp ){ 229 * translation = measured_string_copy( tmp ); 230 fibril_rwlock_read_unlock( & arp_globals.lock ); 231 if( * translation ){ 232 * data = ( ** translation ).value; 237 233 return EOK; 238 234 }else{ … … 240 236 } 241 237 }else{ 242 fibril_rwlock_read_unlock( &arp_globals.lock);238 fibril_rwlock_read_unlock( & arp_globals.lock ); 243 239 return ENOENT; 244 240 } 245 241 } 246 242 247 int arp_initialize( async_client_conn_t client_connection){243 int arp_initialize( async_client_conn_t client_connection ){ 248 244 ERROR_DECLARE; 249 245 250 fibril_rwlock_initialize( &arp_globals.lock);251 fibril_rwlock_write_lock( &arp_globals.lock);246 fibril_rwlock_initialize( & arp_globals.lock ); 247 fibril_rwlock_write_lock( & arp_globals.lock ); 252 248 arp_globals.client_connection = client_connection; 253 ERROR_PROPAGATE( arp_cache_initialize(&arp_globals.cache));254 fibril_rwlock_write_unlock( &arp_globals.lock);255 return EOK; 256 } 257 258 int arp_proto_create( arp_proto_ref * proto, services_t service, measured_string_ref address){249 ERROR_PROPAGATE( arp_cache_initialize( & arp_globals.cache )); 250 fibril_rwlock_write_unlock( & arp_globals.lock ); 251 return EOK; 252 } 253 254 int arp_proto_create( arp_proto_ref * proto, services_t service, measured_string_ref address ){ 259 255 ERROR_DECLARE; 260 256 261 *proto = (arp_proto_ref) malloc(sizeof(arp_proto_t)); 262 if(!(*proto)){ 263 return ENOMEM; 264 } 265 (** proto).service = service; 266 (** proto).addr = address; 267 (** proto).addr_data = address->value; 268 if(ERROR_OCCURRED(arp_addr_initialize(&(** proto).addresses))){ 269 free(*proto); 257 * proto = ( arp_proto_ref ) malloc( sizeof( arp_proto_t )); 258 if( !( * proto )) return ENOMEM; 259 ( ** proto ).service = service; 260 ( ** proto ).addr = address; 261 ( ** proto ).addr_data = address->value; 262 if( ERROR_OCCURRED( arp_addr_initialize( &( ** proto).addresses ))){ 263 free( * proto ); 270 264 return ERROR_CODE; 271 265 } … … 273 267 } 274 268 275 int arp_device_message( device_id_t device_id, services_t service, services_t protocol, measured_string_ref address){269 int arp_device_message( device_id_t device_id, services_t service, services_t protocol, measured_string_ref address ){ 276 270 ERROR_DECLARE; 277 271 278 arp_device_ref 279 arp_proto_ref 280 int 281 hw_type_t 282 283 fibril_rwlock_write_lock( &arp_globals.lock);272 arp_device_ref device; 273 arp_proto_ref proto; 274 int index; 275 hw_type_t hardware; 276 277 fibril_rwlock_write_lock( & arp_globals.lock ); 284 278 // an existing device? 285 device = arp_cache_find( &arp_globals.cache, device_id);286 if( device){287 if( device->service != service){288 printf( "Device %d already exists\n", device->device_id);289 fibril_rwlock_write_unlock( &arp_globals.lock);279 device = arp_cache_find( & arp_globals.cache, device_id ); 280 if( device ){ 281 if( device->service != service ){ 282 printf( "Device %d already exists\n", device->device_id ); 283 fibril_rwlock_write_unlock( & arp_globals.lock ); 290 284 return EEXIST; 291 285 } 292 proto = arp_protos_find( &device->protos, protocol);293 if( proto){294 free( proto->addr);295 free( proto->addr_data);286 proto = arp_protos_find( & device->protos, protocol ); 287 if( proto ){ 288 free( proto->addr ); 289 free( proto->addr_data ); 296 290 proto->addr = address; 297 291 proto->addr_data = address->value; 298 292 }else{ 299 if( ERROR_OCCURRED(arp_proto_create(&proto, protocol, address))){300 fibril_rwlock_write_unlock( &arp_globals.lock);293 if( ERROR_OCCURRED( arp_proto_create( & proto, protocol, address ))){ 294 fibril_rwlock_write_unlock( & arp_globals.lock ); 301 295 return ERROR_CODE; 302 296 } 303 index = arp_protos_add( &device->protos, proto->service, proto);304 if( index < 0){305 fibril_rwlock_write_unlock( &arp_globals.lock);306 free( proto);297 index = arp_protos_add( & device->protos, proto->service, proto ); 298 if( index < 0 ){ 299 fibril_rwlock_write_unlock( & arp_globals.lock ); 300 free( proto ); 307 301 return index; 308 302 } 309 printf( "New protocol added:\n\tdevice id\t= %d\n\tproto\t= %d", device_id, protocol);303 printf( "New protocol added:\n\tdevice id\t= %d\n\tproto\t= %d", device_id, protocol ); 310 304 } 311 305 }else{ 312 hardware = hardware_map(service); 313 if(! hardware){ 314 return ENOENT; 315 } 306 hardware = hardware_map( service ); 307 if( ! hardware ) return ENOENT; 316 308 // create a new device 317 device = ( arp_device_ref) malloc(sizeof(arp_device_t));318 if( ! device){319 fibril_rwlock_write_unlock( &arp_globals.lock);309 device = ( arp_device_ref ) malloc( sizeof( arp_device_t )); 310 if( ! device ){ 311 fibril_rwlock_write_unlock( & arp_globals.lock ); 320 312 return ENOMEM; 321 313 } 322 314 device->hardware = hardware; 323 315 device->device_id = device_id; 324 if( ERROR_OCCURRED(arp_protos_initialize(&device->protos))325 || ERROR_OCCURRED(arp_proto_create(&proto, protocol, address))){326 fibril_rwlock_write_unlock( &arp_globals.lock);327 free( device);328 return ERROR_CODE; 329 } 330 index = arp_protos_add( &device->protos, proto->service, proto);331 if( index < 0){332 fibril_rwlock_write_unlock( &arp_globals.lock);333 arp_protos_destroy( &device->protos);334 free( device);316 if( ERROR_OCCURRED( arp_protos_initialize( & device->protos )) 317 || ERROR_OCCURRED( arp_proto_create( & proto, protocol, address ))){ 318 fibril_rwlock_write_unlock( & arp_globals.lock ); 319 free( device ); 320 return ERROR_CODE; 321 } 322 index = arp_protos_add( & device->protos, proto->service, proto ); 323 if( index < 0 ){ 324 fibril_rwlock_write_unlock( & arp_globals.lock ); 325 arp_protos_destroy( & device->protos ); 326 free( device ); 335 327 return index; 336 328 } 337 329 device->service = service; 338 330 // bind the new one 339 device->phone = nil_bind_service( device->service, (ipcarg_t) device->device_id, SERVICE_ARP, arp_globals.client_connection);340 if( device->phone < 0){341 fibril_rwlock_write_unlock( &arp_globals.lock);342 arp_protos_destroy( &device->protos);343 free( device);331 device->phone = nil_bind_service( device->service, ( ipcarg_t ) device->device_id, SERVICE_ARP, arp_globals.client_connection ); 332 if( device->phone < 0 ){ 333 fibril_rwlock_write_unlock( & arp_globals.lock ); 334 arp_protos_destroy( & device->protos ); 335 free( device ); 344 336 return EREFUSED; 345 337 } 346 338 // get packet dimensions 347 if( ERROR_OCCURRED(nil_packet_size_req(device->phone, device_id, &device->packet_dimension))){348 fibril_rwlock_write_unlock( &arp_globals.lock);349 arp_protos_destroy( &device->protos);350 free( device);339 if( ERROR_OCCURRED( nil_packet_size_req( device->phone, device_id, & device->packet_dimension ))){ 340 fibril_rwlock_write_unlock( & arp_globals.lock ); 341 arp_protos_destroy( & device->protos ); 342 free( device ); 351 343 return ERROR_CODE; 352 344 } 353 345 // get hardware address 354 if( ERROR_OCCURRED(nil_get_addr_req(device->phone, device_id, &device->addr, &device->addr_data))){355 fibril_rwlock_write_unlock( &arp_globals.lock);356 arp_protos_destroy( &device->protos);357 free( device);346 if( ERROR_OCCURRED( nil_get_addr_req( device->phone, device_id, & device->addr, & device->addr_data ))){ 347 fibril_rwlock_write_unlock( & arp_globals.lock ); 348 arp_protos_destroy( & device->protos ); 349 free( device ); 358 350 return ERROR_CODE; 359 351 } 360 352 // get broadcast address 361 if(ERROR_OCCURRED(nil_get_broadcast_addr_req(device->phone, device_id, &device->broadcast_addr, &device->broadcast_data))){ 362 fibril_rwlock_write_unlock(&arp_globals.lock); 363 free(device->addr); 364 free(device->addr_data); 365 arp_protos_destroy(&device->protos); 366 free(device); 367 return ERROR_CODE; 368 } 369 if(ERROR_OCCURRED(arp_cache_add(&arp_globals.cache, device->device_id, device))){ 370 fibril_rwlock_write_unlock(&arp_globals.lock); 371 free(device->addr); 372 free(device->addr_data); 373 free(device->broadcast_addr); 374 free(device->broadcast_data); 375 arp_protos_destroy(&device->protos); 376 free(device); 377 return ERROR_CODE; 378 } 379 printf("New device registered:\n\tid\t= %d\n\ttype\t= 0x%x\n\tservice\t= %d\n\tproto\t= %d\n", device->device_id, device->hardware, device->service, protocol); 380 } 381 fibril_rwlock_write_unlock(&arp_globals.lock); 382 return EOK; 383 } 384 385 measured_string_ref arp_translate_message(device_id_t device_id, services_t protocol, measured_string_ref target){ 386 arp_device_ref device; 387 arp_proto_ref proto; 388 measured_string_ref addr; 389 size_t length; 390 packet_t packet; 391 arp_header_ref header; 392 393 if(! target){ 353 if( ERROR_OCCURRED( nil_get_broadcast_addr_req( device->phone, device_id, & device->broadcast_addr, & device->broadcast_data ))){ 354 fibril_rwlock_write_unlock( & arp_globals.lock ); 355 free( device->addr ); 356 free( device->addr_data ); 357 arp_protos_destroy( & device->protos ); 358 free( device ); 359 return ERROR_CODE; 360 } 361 if( ERROR_OCCURRED( arp_cache_add( & arp_globals.cache, device->device_id, device ))){ 362 fibril_rwlock_write_unlock( & arp_globals.lock ); 363 free( device->addr ); 364 free( device->addr_data ); 365 free( device->broadcast_addr ); 366 free( device->broadcast_data ); 367 arp_protos_destroy( & device->protos ); 368 free( device ); 369 return ERROR_CODE; 370 } 371 printf( "New device registered:\n\tid\t= %d\n\ttype\t= 0x%x\n\tservice\t= %d\n\tproto\t= %d\n", device->device_id, device->hardware, device->service, protocol ); 372 } 373 fibril_rwlock_write_unlock( & arp_globals.lock ); 374 return EOK; 375 } 376 377 measured_string_ref arp_translate_message( device_id_t device_id, services_t protocol, measured_string_ref target ){ 378 arp_device_ref device; 379 arp_proto_ref proto; 380 measured_string_ref addr; 381 size_t length; 382 packet_t packet; 383 arp_header_ref header; 384 385 if( ! target ) return NULL; 386 device = arp_cache_find( & arp_globals.cache, device_id ); 387 if( ! device ) return NULL; 388 proto = arp_protos_find( & device->protos, protocol ); 389 if(( ! proto ) || ( proto->addr->length != target->length )) return NULL; 390 addr = arp_addr_find( & proto->addresses, target->value, target->length ); 391 if( addr ) return addr; 392 // ARP packet content size = header + ( address + translation ) * 2 393 length = 8 + ( CONVERT_SIZE( char, uint8_t, proto->addr->length ) + CONVERT_SIZE( char, uint8_t, device->addr->length )) * 2; 394 if( length > device->packet_dimension.content ) return NULL; 395 packet = packet_get_4( arp_globals.net_phone, device->packet_dimension.addr_len, device->packet_dimension.prefix, length, device->packet_dimension.suffix ); 396 if( ! packet ) return NULL; 397 header = ( arp_header_ref ) packet_suffix( packet, length ); 398 if( ! header ){ 399 pq_release( arp_globals.net_phone, packet_get_id( packet )); 394 400 return NULL; 395 401 } 396 device = arp_cache_find(&arp_globals.cache, device_id); 397 if(! device){ 402 header->hardware = htons( device->hardware ); 403 header->hardware_length = ( uint8_t ) device->addr->length; 404 header->protocol = htons( protocol_map( device->service, protocol )); 405 header->protocol_length = ( uint8_t ) proto->addr->length; 406 header->operation = htons( ARPOP_REQUEST ); 407 length = sizeof( arp_header_t ); 408 memcpy((( uint8_t * ) header ) + length, device->addr->value, device->addr->length ); 409 length += device->addr->length; 410 memcpy((( uint8_t * ) header ) + length, proto->addr->value, proto->addr->length ); 411 length += proto->addr->length; 412 bzero((( uint8_t * ) header ) + length, device->addr->length ); 413 length += device->addr->length; 414 memcpy((( uint8_t * ) header ) + length, target->value, target->length ); 415 if( packet_set_addr( packet, ( uint8_t * ) device->addr->value, ( uint8_t * ) device->broadcast_addr->value, CONVERT_SIZE( char, uint8_t, device->addr->length )) != EOK ){ 416 pq_release( arp_globals.net_phone, packet_get_id( packet )); 398 417 return NULL; 399 418 } 400 proto = arp_protos_find(&device->protos, protocol); 401 if((! proto) || (proto->addr->length != target->length)){ 402 return NULL; 403 } 404 addr = arp_addr_find(&proto->addresses, target->value, target->length); 405 if(addr){ 406 return addr; 407 } 408 // ARP packet content size = header + (address + translation) * 2 409 length = 8 + (CONVERT_SIZE(char, uint8_t, proto->addr->length) + CONVERT_SIZE(char, uint8_t, device->addr->length)) * 2; 410 if(length > device->packet_dimension.content){ 411 return NULL; 412 } 413 packet = packet_get_4(arp_globals.net_phone, device->packet_dimension.addr_len, device->packet_dimension.prefix, length, device->packet_dimension.suffix); 414 if(! packet){ 415 return NULL; 416 } 417 header = (arp_header_ref) packet_suffix(packet, length); 418 if(! header){ 419 pq_release(arp_globals.net_phone, packet_get_id(packet)); 420 return NULL; 421 } 422 header->hardware = htons(device->hardware); 423 header->hardware_length = (uint8_t) device->addr->length; 424 header->protocol = htons(protocol_map(device->service, protocol)); 425 header->protocol_length = (uint8_t) proto->addr->length; 426 header->operation = htons(ARPOP_REQUEST); 427 length = sizeof(arp_header_t); 428 memcpy(((uint8_t *) header) + length, device->addr->value, device->addr->length); 429 length += device->addr->length; 430 memcpy(((uint8_t *) header) + length, proto->addr->value, proto->addr->length); 431 length += proto->addr->length; 432 bzero(((uint8_t *) header) + length, device->addr->length); 433 length += device->addr->length; 434 memcpy(((uint8_t *) header) + length, target->value, target->length); 435 if(packet_set_addr(packet, (uint8_t *) device->addr->value, (uint8_t *) device->broadcast_addr->value, CONVERT_SIZE(char, uint8_t, device->addr->length)) != EOK){ 436 pq_release(arp_globals.net_phone, packet_get_id(packet)); 437 return NULL; 438 } 439 nil_send_msg(device->phone, device_id, packet, SERVICE_ARP); 419 nil_send_msg( device->phone, device_id, packet, SERVICE_ARP ); 440 420 return NULL; 441 421 } 442 422 443 int arp_receive_message( device_id_t device_id, packet_t packet){423 int arp_receive_message( device_id_t device_id, packet_t packet ){ 444 424 ERROR_DECLARE; 445 425 446 size_t length; 447 arp_header_ref header; 448 arp_device_ref device; 449 arp_proto_ref proto; 450 measured_string_ref hw_source; 451 uint8_t * src_hw; 452 uint8_t * src_proto; 453 uint8_t * des_hw; 454 uint8_t * des_proto; 455 456 length = packet_get_data_length(packet); 457 if(length <= sizeof(arp_header_t)){ 426 size_t length; 427 arp_header_ref header; 428 arp_device_ref device; 429 arp_proto_ref proto; 430 measured_string_ref hw_source; 431 uint8_t * src_hw; 432 uint8_t * src_proto; 433 uint8_t * des_hw; 434 uint8_t * des_proto; 435 436 length = packet_get_data_length( packet ); 437 if( length <= sizeof( arp_header_t )) return EINVAL; 438 device = arp_cache_find( & arp_globals.cache, device_id ); 439 if( ! device ) return ENOENT; 440 header = ( arp_header_ref ) packet_get_data( packet ); 441 if(( ntohs( header->hardware ) != device->hardware ) 442 || ( length < sizeof( arp_header_t ) + header->hardware_length * 2u + header->protocol_length * 2u )){ 458 443 return EINVAL; 459 444 } 460 device = arp_cache_find(&arp_globals.cache, device_id); 461 if(! device){ 462 return ENOENT; 463 } 464 header = (arp_header_ref) packet_get_data(packet); 465 if((ntohs(header->hardware) != device->hardware) 466 || (length < sizeof(arp_header_t) + header->hardware_length * 2u + header->protocol_length * 2u)){ 467 return EINVAL; 468 } 469 proto = arp_protos_find(&device->protos, protocol_unmap(device->service, ntohs(header->protocol))); 470 if(! proto){ 471 return ENOENT; 472 } 473 src_hw = ((uint8_t *) header) + sizeof(arp_header_t); 445 proto = arp_protos_find( & device->protos, protocol_unmap( device->service, ntohs( header->protocol ))); 446 if( ! proto ) return ENOENT; 447 src_hw = (( uint8_t * ) header ) + sizeof( arp_header_t ); 474 448 src_proto = src_hw + header->hardware_length; 475 449 des_hw = src_proto + header->protocol_length; 476 450 des_proto = des_hw + header->hardware_length; 477 hw_source = arp_addr_find( &proto->addresses, (char *) src_proto, CONVERT_SIZE(uint8_t, char, header->protocol_length));451 hw_source = arp_addr_find( & proto->addresses, ( char * ) src_proto, CONVERT_SIZE( uint8_t, char, header->protocol_length )); 478 452 // exists? 479 if( hw_source){480 if( hw_source->length != CONVERT_SIZE(uint8_t, char, header->hardware_length)){453 if( hw_source ){ 454 if( hw_source->length != CONVERT_SIZE( uint8_t, char, header->hardware_length )){ 481 455 return EINVAL; 482 456 } 483 memcpy( hw_source->value, src_hw, hw_source->length);457 memcpy( hw_source->value, src_hw, hw_source->length ); 484 458 } 485 459 // is my protocol address? 486 if( proto->addr->length != CONVERT_SIZE(uint8_t, char, header->protocol_length)){460 if( proto->addr->length != CONVERT_SIZE( uint8_t, char, header->protocol_length )){ 487 461 return EINVAL; 488 462 } 489 if( ! str_lcmp(proto->addr->value, (char *) des_proto, proto->addr->length)){463 if( ! str_lcmp( proto->addr->value, ( char * ) des_proto, proto->addr->length )){ 490 464 // not already upadted? 491 if(! hw_source){ 492 hw_source = measured_string_create_bulk((char *) src_hw, CONVERT_SIZE(uint8_t, char, header->hardware_length)); 493 if(! hw_source){ 494 return ENOMEM; 495 } 496 ERROR_PROPAGATE(arp_addr_add(&proto->addresses, (char *) src_proto, CONVERT_SIZE(uint8_t, char, header->protocol_length), hw_source)); 497 } 498 if(ntohs(header->operation) == ARPOP_REQUEST){ 499 header->operation = htons(ARPOP_REPLY); 500 memcpy(des_proto, src_proto, header->protocol_length); 501 memcpy(src_proto, proto->addr->value, header->protocol_length); 502 memcpy(src_hw, device->addr->value, device->packet_dimension.addr_len); 503 memcpy(des_hw, hw_source->value, header->hardware_length); 504 ERROR_PROPAGATE(packet_set_addr(packet, src_hw, des_hw, header->hardware_length)); 505 nil_send_msg(device->phone, device_id, packet, SERVICE_ARP); 465 if( ! hw_source ){ 466 hw_source = measured_string_create_bulk(( char * ) src_hw, CONVERT_SIZE( uint8_t, char, header->hardware_length )); 467 if( ! hw_source ) return ENOMEM; 468 ERROR_PROPAGATE( arp_addr_add( & proto->addresses, ( char * ) src_proto, CONVERT_SIZE( uint8_t, char, header->protocol_length ), hw_source )); 469 } 470 if( ntohs( header->operation ) == ARPOP_REQUEST ){ 471 header->operation = htons( ARPOP_REPLY ); 472 memcpy( des_proto, src_proto, header->protocol_length ); 473 memcpy( src_proto, proto->addr->value, header->protocol_length ); 474 memcpy( src_hw, device->addr->value, device->packet_dimension.addr_len ); 475 memcpy( des_hw, hw_source->value, header->hardware_length ); 476 ERROR_PROPAGATE( packet_set_addr( packet, src_hw, des_hw, header->hardware_length )); 477 nil_send_msg( device->phone, device_id, packet, SERVICE_ARP ); 506 478 return 1; 507 479 } … … 510 482 } 511 483 512 void arp_clear_device(arp_device_ref device){ 513 int count; 514 arp_proto_ref proto; 515 516 for(count = arp_protos_count(&device->protos) - 1; count >= 0; -- count){ 517 proto = arp_protos_get_index(&device->protos, count); 518 if(proto){ 519 if(proto->addr){ 520 free(proto->addr); 521 } 522 if(proto->addr_data){ 523 free(proto->addr_data); 524 } 525 arp_addr_destroy(&proto->addresses); 526 } 527 } 528 arp_protos_clear(&device->protos); 529 } 530 531 int arp_connect_module(services_t service){ 532 if(service != SERVICE_ARP){ 533 return EINVAL; 534 } 535 return EOK; 536 } 537 538 int arp_mtu_changed_message(device_id_t device_id, size_t mtu){ 539 arp_device_ref device; 540 541 fibril_rwlock_write_lock(&arp_globals.lock); 542 device = arp_cache_find(&arp_globals.cache, device_id); 543 if(! device){ 544 fibril_rwlock_write_unlock(&arp_globals.lock); 484 void arp_clear_device( arp_device_ref device ){ 485 int count; 486 arp_proto_ref proto; 487 488 for( count = arp_protos_count( & device->protos ) - 1; count >= 0; -- count ){ 489 proto = arp_protos_get_index( & device->protos, count ); 490 if( proto ){ 491 if( proto->addr ) free( proto->addr ); 492 if( proto->addr_data ) free( proto->addr_data ); 493 arp_addr_destroy( & proto->addresses ); 494 } 495 } 496 arp_protos_clear( & device->protos ); 497 } 498 499 int arp_connect_module( services_t service ){ 500 if( service != SERVICE_ARP ) return EINVAL; 501 return EOK; 502 } 503 504 int arp_mtu_changed_message( device_id_t device_id, size_t mtu ){ 505 arp_device_ref device; 506 507 fibril_rwlock_write_lock( & arp_globals.lock ); 508 device = arp_cache_find( & arp_globals.cache, device_id ); 509 if( ! device ){ 510 fibril_rwlock_write_unlock( & arp_globals.lock ); 545 511 return ENOENT; 546 512 } 547 513 device->packet_dimension.content = mtu; 548 printf( "arp - device %d changed mtu to %d\n\n", device_id, mtu);549 fibril_rwlock_write_unlock( &arp_globals.lock);550 return EOK; 551 } 552 553 int arp_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count){514 printf( "arp - device %d changed mtu to %d\n\n", device_id, mtu ); 515 fibril_rwlock_write_unlock( & arp_globals.lock ); 516 return EOK; 517 } 518 519 int arp_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){ 554 520 ERROR_DECLARE; 555 521 556 measured_string_ref 557 measured_string_ref 558 char * 559 packet_t 560 packet_t 561 562 // printf( "message %d - %d\n", IPC_GET_METHOD(*call), NET_ARP_FIRST);563 * answer_count = 0;564 switch( IPC_GET_METHOD(*call)){522 measured_string_ref address; 523 measured_string_ref translation; 524 char * data; 525 packet_t packet; 526 packet_t next; 527 528 // printf( "message %d - %d\n", IPC_GET_METHOD( * call ), NET_ARP_FIRST ); 529 * answer_count = 0; 530 switch( IPC_GET_METHOD( * call )){ 565 531 case IPC_M_PHONE_HUNGUP: 566 532 return EOK; 567 533 case NET_ARP_DEVICE: 568 ERROR_PROPAGATE( measured_strings_receive(&address, &data, 1));569 if( ERROR_OCCURRED(arp_device_message(IPC_GET_DEVICE(call), IPC_GET_SERVICE(call), ARP_GET_NETIF(call), address))){570 free( address);571 free( data);534 ERROR_PROPAGATE( measured_strings_receive( & address, & data, 1 )); 535 if( ERROR_OCCURRED( arp_device_message( IPC_GET_DEVICE( call ), IPC_GET_SERVICE( call ), ARP_GET_NETIF( call ), address ))){ 536 free( address ); 537 free( data ); 572 538 } 573 539 return ERROR_CODE; 574 540 case NET_ARP_TRANSLATE: 575 ERROR_PROPAGATE( measured_strings_receive(&address, &data, 1));576 fibril_rwlock_read_lock( &arp_globals.lock);577 translation = arp_translate_message( IPC_GET_DEVICE(call), IPC_GET_SERVICE(call), address);578 free( address);579 free( data);580 if( ! translation){581 fibril_rwlock_read_unlock( &arp_globals.lock);541 ERROR_PROPAGATE( measured_strings_receive( & address, & data, 1 )); 542 fibril_rwlock_read_lock( & arp_globals.lock ); 543 translation = arp_translate_message( IPC_GET_DEVICE( call ), IPC_GET_SERVICE( call ), address ); 544 free( address ); 545 free( data ); 546 if( ! translation ){ 547 fibril_rwlock_read_unlock( & arp_globals.lock ); 582 548 return ENOENT; 583 549 } 584 ERROR_CODE = measured_strings_reply( translation, 1);585 fibril_rwlock_read_unlock( &arp_globals.lock);550 ERROR_CODE = measured_strings_reply( translation, 1 ); 551 fibril_rwlock_read_unlock( & arp_globals.lock ); 586 552 return ERROR_CODE; 587 553 case NET_ARP_CLEAR_DEVICE: 588 return arp_clear_device_req( 0, IPC_GET_DEVICE(call));554 return arp_clear_device_req( 0, IPC_GET_DEVICE( call )); 589 555 case NET_ARP_CLEAR_ADDRESS: 590 ERROR_PROPAGATE( measured_strings_receive(&address, &data, 1));591 arp_clear_address_req( 0, IPC_GET_DEVICE(call), IPC_GET_SERVICE(call), address);592 free( address);593 free( data);556 ERROR_PROPAGATE( measured_strings_receive( & address, & data, 1 )); 557 arp_clear_address_req( 0, IPC_GET_DEVICE( call ), IPC_GET_SERVICE( call ), address ); 558 free( address ); 559 free( data ); 594 560 return EOK; 595 561 case NET_ARP_CLEAN_CACHE: 596 return arp_clean_cache_req( 0);562 return arp_clean_cache_req( 0 ); 597 563 case NET_IL_DEVICE_STATE: 598 564 // do nothing - keep the cache 599 565 return EOK; 600 566 case NET_IL_RECEIVED: 601 if( ! ERROR_OCCURRED(packet_translate(arp_globals.net_phone, &packet, IPC_GET_PACKET(call)))){602 fibril_rwlock_read_lock( &arp_globals.lock);567 if( ! ERROR_OCCURRED( packet_translate( arp_globals.net_phone, & packet, IPC_GET_PACKET( call )))){ 568 fibril_rwlock_read_lock( & arp_globals.lock ); 603 569 do{ 604 next = pq_detach(packet); 605 ERROR_CODE = arp_receive_message(IPC_GET_DEVICE(call), packet); 606 if(ERROR_CODE != 1){ 607 pq_release(arp_globals.net_phone, packet_get_id(packet)); 608 } 570 next = pq_detach( packet ); 571 ERROR_CODE = arp_receive_message( IPC_GET_DEVICE( call ), packet ); 572 if( ERROR_CODE != 1 ) pq_release( arp_globals.net_phone, packet_get_id( packet )); 609 573 packet = next; 610 }while( packet);611 fibril_rwlock_read_unlock( &arp_globals.lock);574 }while( packet ); 575 fibril_rwlock_read_unlock( & arp_globals.lock ); 612 576 } 613 577 return ERROR_CODE; 614 578 case NET_IL_MTU_CHANGED: 615 return arp_mtu_changed_message( IPC_GET_DEVICE(call), IPC_GET_MTU(call));579 return arp_mtu_changed_message( IPC_GET_DEVICE( call ), IPC_GET_MTU( call )); 616 580 } 617 581 return ENOTSUP;
Note:
See TracChangeset
for help on using the changeset viewer.