Rev 4558 | Rev 4582 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
| Rev 4558 | Rev 4575 | ||
|---|---|---|---|
| Line 102... | Line 102... | ||
| 102 | int ip_device_state_msg( int il_phone, device_id_t device_id, device_state_t state ); |
102 | int ip_device_state_msg( int il_phone, device_id_t device_id, device_state_t state ); |
| 103 | int ip_register( int protocol, services_t service, int phone, tl_received_msg_t tl_received_msg ); |
103 | int ip_register( int protocol, services_t service, int phone, tl_received_msg_t tl_received_msg ); |
| 104 | int ip_netif_initialize( ip_netif_ref ip_netif ); |
104 | int ip_netif_initialize( ip_netif_ref ip_netif ); |
| 105 | 105 | ||
| 106 | int ip_send_route( packet_t packet, ip_netif_ref netif, ip_route_ref route, in_addr_t * src, in_addr_t dest ); |
106 | int ip_send_route( packet_t packet, ip_netif_ref netif, ip_route_ref route, in_addr_t * src, in_addr_t dest ); |
| 107 | int ip_prepare_packet( in_addr_t * source, packet_t packet, measured_string_ref destination ); |
107 | int ip_prepare_packet( in_addr_t * source, in_addr_t dest, packet_t packet, measured_string_ref destination ); |
| 108 | 108 | ||
| 109 | packet_t ip_split_packet( packet_t packet, size_t prefix, size_t content, size_t suffix, size_t addr_len ); |
109 | packet_t ip_split_packet( packet_t packet, size_t prefix, size_t content, size_t suffix, size_t addr_len ); |
| 110 | int ip_fragment_packet( packet_t packet, size_t length, size_t prefix, size_t suffix, size_t addr_len ); |
110 | int ip_fragment_packet( packet_t packet, size_t length, size_t prefix, size_t suffix, size_t addr_len ); |
| 111 | int ip_fragment_packet_data( packet_t packet, packet_t new_packet, ip_header_ref header, ip_header_ref new_header, size_t length, void * src, void * dest, size_t address_length ); |
111 | int ip_fragment_packet_data( packet_t packet, packet_t new_packet, ip_header_ref header, ip_header_ref new_header, size_t length, void * src, void * dest, size_t address_length ); |
| 112 | ip_header_ref ip_create_middle_header( packet_t packet, ip_header_ref last ); |
112 | ip_header_ref ip_create_middle_header( packet_t packet, ip_header_ref last ); |
| Line 422... | Line 422... | ||
| 422 | ERROR_DECLARE; |
422 | ERROR_DECLARE; |
| 423 | 423 | ||
| 424 | int length; |
424 | int length; |
| 425 | ip_netif_ref netif; |
425 | ip_netif_ref netif; |
| 426 | ip_route_ref route; |
426 | ip_route_ref route; |
| 427 | in_addr_t dest; |
427 | in_addr_t * dest; |
| 428 | in_addr_t * src; |
428 | in_addr_t * src; |
| 429 | 429 | ||
| 430 | // addresses in the host byte order |
430 | // addresses in the host byte order |
| 431 | // should be the next hop address or the target destination address |
431 | // should be the next hop address or the target destination address |
| 432 | length = packet_get_addr( packet, NULL, ( void * )( & dest.s_addr )); |
432 | length = packet_get_addr( packet, NULL, ( uint8_t ** ) & dest ); |
| 433 | if( length < 0 ){ |
433 | if( length < 0 ){ |
| 434 | pq_release( ip_globals.net_phone, packet_get_id( packet )); |
434 | pq_release( ip_globals.net_phone, packet_get_id( packet )); |
| 435 | return length; |
435 | return length; |
| 436 | } |
436 | } |
| 437 | // TODO IPv6 |
437 | // TODO IPv6 |
| Line 440... | Line 440... | ||
| 440 | return EINVAL; |
440 | return EINVAL; |
| 441 | } |
441 | } |
| 442 | rwlock_read_lock( & ip_globals.netifs_lock ); |
442 | rwlock_read_lock( & ip_globals.netifs_lock ); |
| 443 | // device specified? |
443 | // device specified? |
| 444 | // dest.s_addr = ntohl( dest.s_addr ); |
444 | // dest.s_addr = ntohl( dest.s_addr ); |
| 445 | if( device_id ){ |
445 | if( device_id > 0 ){ |
| 446 | netif = ip_netifs_find( & ip_globals.netifs, device_id ); |
446 | netif = ip_netifs_find( & ip_globals.netifs, device_id ); |
| 447 | route = ip_netif_find_route( netif, dest ); |
447 | route = ip_netif_find_route( netif, * dest ); |
| 448 | }else{ |
448 | }else{ |
| 449 | // TODO IPv6 |
449 | // TODO IPv6 |
| 450 | route = ip_find_route( dest ); |
450 | route = ip_find_route( * dest ); |
| 451 | netif = route ? route->netif : NULL; |
451 | netif = route ? route->netif : NULL; |
| 452 | } |
452 | } |
| 453 | if( !( netif && route )){ |
453 | if( !( netif && route )){ |
| 454 | rwlock_read_unlock( & ip_globals.netifs_lock ); |
454 | rwlock_read_unlock( & ip_globals.netifs_lock ); |
| 455 | pq_release( ip_globals.net_phone, packet_get_id( packet )); |
455 | pq_release( ip_globals.net_phone, packet_get_id( packet )); |
| 456 | return ENOENT; |
456 | return ENOENT; |
| 457 | } |
457 | } |
| 458 | // to me? |
458 | // to me? |
| 459 | if( route->address.s_addr == dest.s_addr ){ |
459 | if( route->address.s_addr == dest->s_addr ){ |
| 460 | // TODO loopback deliver |
460 | // TODO loopback deliver |
| 461 | rwlock_read_unlock( & ip_globals.netifs_lock ); |
461 | rwlock_read_unlock( & ip_globals.netifs_lock ); |
| 462 | return ip_deliver_local( -1, packet, ( ip_header_ref ) packet_get_data( packet )); |
462 | return ip_deliver_local( -1, packet, ( ip_header_ref ) packet_get_data( packet )); |
| 463 | } |
463 | } |
| 464 | 464 | ||
| Line 466... | Line 466... | ||
| 466 | if( ! src ){ |
466 | if( ! src ){ |
| 467 | rwlock_read_unlock( & ip_globals.netifs_lock ); |
467 | rwlock_read_unlock( & ip_globals.netifs_lock ); |
| 468 | pq_release( ip_globals.net_phone, packet_get_id( packet )); |
468 | pq_release( ip_globals.net_phone, packet_get_id( packet )); |
| 469 | return ENOENT; |
469 | return ENOENT; |
| 470 | } |
470 | } |
| 471 | if( ERROR_OCCURRED( ip_send_route( packet, netif, route, src, dest ))){ |
471 | if( ERROR_OCCURRED( ip_send_route( packet, netif, route, src, * dest ))){ |
| 472 | pq_release( ip_globals.net_phone, packet_get_id( packet )); |
472 | pq_release( ip_globals.net_phone, packet_get_id( packet )); |
| 473 | } |
473 | } |
| 474 | rwlock_read_unlock( & ip_globals.netifs_lock ); |
474 | rwlock_read_unlock( & ip_globals.netifs_lock ); |
| 475 | return ERROR_CODE; |
475 | return ERROR_CODE; |
| 476 | } |
476 | } |
| Line 489... | Line 489... | ||
| 489 | packet_t tmp; |
489 | packet_t tmp; |
| 490 | measured_string_t destination; |
490 | measured_string_t destination; |
| 491 | measured_string_ref translation; |
491 | measured_string_ref translation; |
| 492 | char * data; |
492 | char * data; |
| 493 | 493 | ||
| 494 | if( route->gateway.s_addr ){ |
- | |
| 495 | dest.s_addr = route->gateway.s_addr; |
- | |
| 496 | } |
- | |
| 497 | // get destination hardware address |
494 | // get destination hardware address |
| 498 | if( netif->arp ){ |
495 | if( netif->arp ){ |
| 499 | destination.value = ( char * ) & dest.s_addr; |
496 | destination.value = route->gateway.s_addr ? ( char * ) & route->gateway.s_addr : ( char * ) & dest.s_addr; |
| 500 | destination.length = CONVERT_SIZE( dest.s_addr, char, 1 ); |
497 | destination.length = CONVERT_SIZE( dest.s_addr, char, 1 ); |
| 501 | if( ERROR_OCCURRED( arp_translate_req( netif->arp->phone, netif->device_id, SERVICE_IP, & destination, & translation, & data ))){ |
498 | if( ERROR_OCCURRED( arp_translate_req( netif->arp->phone, netif->device_id, SERVICE_IP, & destination, & translation, & data ))){ |
| 502 | usleep( 200000 ); |
499 | usleep( 200000 ); |
| 503 | ERROR_PROPAGATE( arp_translate_req( netif->arp->phone, netif->device_id, SERVICE_IP, & destination, & translation, & data )); |
500 | ERROR_PROPAGATE( arp_translate_req( netif->arp->phone, netif->device_id, SERVICE_IP, & destination, & translation, & data )); |
| 504 | } |
501 | } |
| Line 512... | Line 509... | ||
| 512 | } |
509 | } |
| 513 | }else translation = NULL; |
510 | }else translation = NULL; |
| 514 | // process packet queue |
511 | // process packet queue |
| 515 | next = packet; |
512 | next = packet; |
| 516 | do{ |
513 | do{ |
| 517 | if( ERROR_OCCURRED( ip_prepare_packet( src, next, translation ))){ |
514 | if( ERROR_OCCURRED( ip_prepare_packet( src, dest, next, translation ))){ |
| 518 | // release invalid packet |
515 | // release invalid packet |
| 519 | tmp = pq_detach( next ); |
516 | tmp = pq_detach( next ); |
| 520 | if( next == packet ) packet = tmp; |
517 | if( next == packet ) packet = tmp; |
| 521 | pq_release( ip_globals.net_phone, packet_get_id( next )); |
518 | pq_release( ip_globals.net_phone, packet_get_id( next )); |
| 522 | next = tmp; |
519 | next = tmp; |
| Line 536... | Line 533... | ||
| 536 | } |
533 | } |
| 537 | } |
534 | } |
| 538 | return EOK; |
535 | return EOK; |
| 539 | } |
536 | } |
| 540 | 537 | ||
| 541 | int ip_prepare_packet( in_addr_t * source, packet_t packet, measured_string_ref destination ){ |
538 | int ip_prepare_packet( in_addr_t * source, in_addr_t dest, packet_t packet, measured_string_ref destination ){ |
| 542 | ERROR_DECLARE; |
539 | ERROR_DECLARE; |
| 543 | 540 | ||
| 544 | size_t length; |
541 | size_t length; |
| 545 | ip_header_ref header; |
542 | ip_header_ref header; |
| 546 | 543 | ||
| Line 551... | Line 548... | ||
| 551 | ERROR_PROPAGATE( packet_set_addr( packet, NULL, ( uint8_t * ) destination->value, CONVERT_SIZE( char, uint8_t, destination->length ))); |
548 | ERROR_PROPAGATE( packet_set_addr( packet, NULL, ( uint8_t * ) destination->value, CONVERT_SIZE( char, uint8_t, destination->length ))); |
| 552 | } |
549 | } |
| 553 | header->version = 4; |
550 | header->version = 4; |
| 554 | header->total_length = htons( length ); |
551 | header->total_length = htons( length ); |
| 555 | header->fragment_offset = 0; |
552 | header->fragment_offset = 0; |
| 556 | if( source ) header->source_address = source->s_addr;//htonl( source.s_addr ); |
553 | if( source ) header->source_address = source->s_addr; |
| - | 554 | header->destination_address = dest.s_addr; |
|
| 557 | rwlock_write_lock( & ip_globals.lock ); |
555 | rwlock_write_lock( & ip_globals.lock ); |
| 558 | ++ ip_globals.packet_counter; |
556 | ++ ip_globals.packet_counter; |
| 559 | header->identification = htons( ip_globals.packet_counter ); |
557 | header->identification = htons( ip_globals.packet_counter ); |
| 560 | rwlock_write_unlock( & ip_globals.lock ); |
558 | rwlock_write_unlock( & ip_globals.lock ); |
| 561 | header->header_checksum = 0; |
559 | header->header_checksum = 0; |
| Line 599... | Line 597... | ||
| 599 | return ENOTSUP; |
597 | return ENOTSUP; |
| 600 | } |
598 | } |
| 601 | 599 | ||
| 602 | int ip_packet_size_req( int ip_phone, device_id_t device_id, size_t * addr_len, size_t * prefix, size_t * content, size_t * suffix ){ |
600 | int ip_packet_size_req( int ip_phone, device_id_t device_id, size_t * addr_len, size_t * prefix, size_t * content, size_t * suffix ){ |
| 603 | ip_netif_ref netif; |
601 | ip_netif_ref netif; |
| - | 602 | int index; |
|
| 604 | 603 | ||
| 605 | if( !( addr_len && prefix && content && suffix )) return EBADMEM; |
604 | if( !( addr_len && prefix && content && suffix )) return EBADMEM; |
| - | 605 | * content = IP_MAX_CONTENT - IP_PREFIX; |
|
| 606 | rwlock_read_lock( & ip_globals.netifs_lock ); |
606 | rwlock_read_lock( & ip_globals.netifs_lock ); |
| - | 607 | if( device_id < 0 ){ |
|
| - | 608 | * addr_len = IP_ADDR; |
|
| - | 609 | * prefix = 0; |
|
| - | 610 | * suffix = 0; |
|
| - | 611 | for( index = ip_netifs_count( & ip_globals.netifs ) - 1; index >= 0; -- index ){ |
|
| - | 612 | netif = ip_netifs_get_index( & ip_globals.netifs, index ); |
|
| - | 613 | if( netif ){ |
|
| - | 614 | if( netif->addr_len > * addr_len ) * addr_len = netif->addr_len; |
|
| - | 615 | if( netif->prefix > * prefix ) * prefix = netif->prefix; |
|
| - | 616 | if( netif->suffix > * suffix ) * suffix = netif->suffix; |
|
| - | 617 | } |
|
| - | 618 | } |
|
| - | 619 | * prefix = * prefix + IP_PREFIX; |
|
| - | 620 | * suffix = * suffix + IP_SUFFIX; |
|
| - | 621 | }else{ |
|
| 607 | netif = ip_netifs_find( & ip_globals.netifs, device_id ); |
622 | netif = ip_netifs_find( & ip_globals.netifs, device_id ); |
| 608 | if( ! netif ){ |
623 | if( ! netif ){ |
| 609 | rwlock_read_unlock( & ip_globals.netifs_lock ); |
624 | rwlock_read_unlock( & ip_globals.netifs_lock ); |
| 610 | return ENOENT; |
625 | return ENOENT; |
| - | 626 | } |
|
| - | 627 | * addr_len = ( netif->addr_len > IP_ADDR ) ? netif->addr_len : IP_ADDR; |
|
| - | 628 | * prefix = netif->prefix + IP_PREFIX; |
|
| - | 629 | * suffix = netif->suffix + IP_SUFFIX; |
|
| 611 | } |
630 | } |
| 612 | * content = IP_MAX_CONTENT - IP_PREFIX; |
- | |
| 613 | * addr_len = ( netif->addr_len > IP_ADDR ) ? netif->addr_len : IP_ADDR; |
- | |
| 614 | * prefix = netif->prefix + IP_PREFIX; |
- | |
| 615 | * suffix = netif->suffix + IP_SUFFIX; |
- | |
| 616 | rwlock_read_unlock( & ip_globals.netifs_lock ); |
631 | rwlock_read_unlock( & ip_globals.netifs_lock ); |
| 617 | return EOK; |
632 | return EOK; |
| 618 | } |
633 | } |
| 619 | 634 | ||
| 620 | int ip_add_route_req( int ip_phone, device_id_t device_id, in_addr_t address, in_addr_t netmask, in_addr_t gateway ){ |
635 | int ip_add_route_req( int ip_phone, device_id_t device_id, in_addr_t address, in_addr_t netmask, in_addr_t gateway ){ |