Subversion Repositories HelenOS

Compare Revisions

Ignore whitespace Rev 4504 → Rev 4505

/branches/network/uspace/srv/net/il/ip/ip.c
41,6 → 41,8
#include <ipc/ipc.h>
#include <ipc/services.h>
 
#include <sys/types.h>
 
#include "../../err.h"
#include "../../messages.h"
#include "../../modules.h"
48,11 → 50,15
#include "../../include/net_interface.h"
#include "../../include/sockaddr.h"
#include "../../include/socket.h"
#include "../../include/byteorder.h"
#include "../../include/crc.h"
#include "../../include/device.h"
#include "../../include/arp_interface.h"
#include "../../include/nil_interface.h"
#include "../../include/il_interface.h"
#include "../../include/ip_client.h"
#include "../../include/ip_interface.h"
#include "../../include/tl_interface.h"
#include "../../structures/measured_strings.h"
#include "../../structures/module_map.h"
#include "../../structures/packet/packet_client.h"
62,13 → 68,28
#include "../il_messages.h"
 
#include "ip.h"
#include "ip_header.h"
#include "ip_messages.h"
#include "ip_module.h"
 
#define DEFAULT_IPV 4
#define IP_MIN_CONTENT 576
 
#define ARP_NAME "arp"
#define ARP_FILENAME "/srv/arp"
 
#define IP_ADDR sizeof( in_addr_t )
#define IP_PREFIX sizeof( ip_header_t )
#define IP_SUFFIX 0
#define IP_MAX_CONTENT 65535
#define IP_HEADER_LENGTH( header ) (( header )->ihl * 4 )
#define IP_TOTAL_LENGTH( header ) ntohs(( header )->total_length )
#define IP_HEADER_DATA_LENGTH( header ) ( IP_TOTAL_LENGTH( header ) - IP_HEADER_LENGTH( header ))
#define IP_HEADER_CHECKSUM( header ) ( htons( ip_checksum(( uint8_t * )( header ), IP_HEADER_LENGTH( header ))))
 
//zero is returned as 0xFFFF (not flipped)
#define IP_HEADER_CHECKSUM_ZERO 0xFFFF
 
ip_globals_t ip_globals;
 
DEVICE_MAP_IMPLEMENT( ip_netifs, ip_netif_t )
75,14 → 96,60
 
INT_MAP_IMPLEMENT( ip_protos, ip_proto_t )
 
GENERIC_FIELD_IMPLEMENT( ip_routes, ip_route_t )
 
int ip_device_state_msg( int il_phone, device_id_t device_id, device_state_t state );
int ip_register( int il_phone, int protocol, int phone );
int ip_register( int protocol, services_t service, int phone, tl_received_msg_t tl_received_msg );
int ip_netif_initialize( ip_netif_ref ip_netif );
 
int ip_send_route( packet_t packet, ip_netif_ref netif, ip_route_ref route, in_addr_t * src, in_addr_t dest );
int ip_prepare_packet( in_addr_t * source, packet_t packet, measured_string_ref destination );
 
packet_t ip_split_packet( packet_t packet, size_t prefix, size_t content, size_t suffix, size_t addr_len );
int ip_fragment_packet( packet_t packet, size_t length, size_t prefix, size_t suffix, size_t addr_len );
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 );
ip_header_ref ip_create_middle_header( packet_t packet, ip_header_ref last );
ip_header_ref ip_create_last_header( packet_t packet, ip_header_ref first );
 
in_addr_t * ip_netif_addr( ip_netif_ref netif );
ip_route_ref ip_find_route( in_addr_t destination );
ip_route_ref ip_netif_find_route( ip_netif_ref netif, in_addr_t destination );
 
int ip_received_msg( device_id_t device_id, packet_t packet );
int ip_process_packet( device_id_t device_id, packet_t packet );
in_addr_t ip_get_destination( ip_header_ref header );
int ip_deliver_local( device_id_t device_id, packet_t packet, ip_header_ref header );
 
/** Computes the ip header checksum.
* To compute the checksum of a new packet, the checksum header field must be zero.
* To check the checksum of a received packet, the checksum may be left set.
* The zero (0) value will returned in this case if valid.
* @param data The header data. Input parameter.
* @param length The header length in bytes. Input parameter.
* @returns The internet protocol header checksum.
* @returns IP_HEADER_CHECKSUM_ZERO if the computed checksum is zero.
*/
uint16_t ip_checksum( uint8_t * data, int length );
 
uint16_t ip_checksum( uint8_t * data, int length ){
uint16_t checksum;
 
checksum = compact_checksum(compute_checksum( 0, data, length ));
 
// flip, zero is returned as 0xFFFF (not flipped)
return ( ~ checksum ) ? ~ checksum : IP_HEADER_CHECKSUM_ZERO;
}
 
/** Initializes the module.
*/
int ip_initialize( async_client_conn_t client_connection ){
ERROR_DECLARE;
 
ip_globals.packet_counter = 0;
ip_globals.gateway.address.s_addr = 0;
ip_globals.gateway.netmask.s_addr = 0;
ip_globals.gateway.gateway.s_addr = 0;
ip_globals.gateway.netif = NULL;
ERROR_PROPAGATE( ip_netifs_initialize( & ip_globals.netifs ));
ERROR_PROPAGATE( ip_protos_initialize( & ip_globals.protos ));
ip_globals.client_connection = client_connection;
94,17 → 161,67
int ip_device_req( int il_phone, device_id_t device_id, services_t netif ){
ERROR_DECLARE;
 
ip_netif_ref ip_netif;
ip_netif_ref ip_netif;
ip_route_ref route;
int index;
char * data;
 
ip_netif = ( ip_netif_ref ) malloc( sizeof( ip_netif_t ));
if( ! ip_netif ) return ENOMEM;
if( ERROR_OCCURRED( ip_routes_initialize( & ip_netif->routes ))){
free( ip_netif );
return ERROR_CODE;
}
ip_netif->device_id = device_id;
ip_netif->service = netif;
ip_netif->state = NETIF_STOPPED;
if( ERROR_OCCURRED( ip_netif_initialize( ip_netif ))){
ip_routes_destroy( & ip_netif->routes );
free( ip_netif );
return ERROR_CODE;
}
if( ip_netif->arp ) ++ ip_netif->arp->usage;
// print the settings
printf( "New device registered:\n\tid\t= %d\n\tphone\t= %d\n\tIPV\t= %d\n", ip_netif->device_id, ip_netif->phone, ip_netif->ipv );
printf( "\tconfiguration\t= %s\n", ip_netif->dhcp ? "dhcp" : "static" );
// TODO ipv6 addresses
data = ( char * ) malloc( INET_ADDRSTRLEN );
if( data ){
for( index = 0; index < ip_routes_count( & ip_netif->routes ); ++ index ){
route = ip_routes_get_index( & ip_netif->routes, index );
if( route ){
printf( "\tRouting %d:\n", index );
inet_ntop( AF_INET, ( uint8_t * ) & route->address.s_addr, data, INET_ADDRSTRLEN );
printf( "\t\taddress\t= %s\n", data );
inet_ntop( AF_INET, ( uint8_t * ) & route->netmask.s_addr, data, INET_ADDRSTRLEN );
printf( "\t\tnetmask\t= %s\n", data );
inet_ntop( AF_INET, ( uint8_t * ) & route->gateway.s_addr, data, INET_ADDRSTRLEN );
printf( "\t\tgateway\t= %s\n", data );
}
}
inet_ntop( AF_INET, ( uint8_t * ) & ip_netif->broadcast.s_addr, data, INET_ADDRSTRLEN );
printf( "\tbroadcast\t= %s\n", data );
inet_ntop( AF_INET, ( uint8_t * ) & ip_netif->dns1, data, INET_ADDRSTRLEN );
printf( "\tdns1\t= %s\n", data );
inet_ntop( AF_INET, ( uint8_t * ) & ip_netif->dns2, data, INET_ADDRSTRLEN );
printf( "\tdns2\t= %s\n", data );
free( data );
}
return EOK;
}
 
int ip_netif_initialize( ip_netif_ref ip_netif ){
ERROR_DECLARE;
 
measured_string_t names[ 9 ] = {{ "IPV", 3 }, { "IP_CONFIG", 9 }, { "IP_ADDR", 7 }, { "NETMASK", 7 }, { "GATEWAY", 7 }, { "BROADCAST", 9 }, { "DNS1", 4 }, { "DNS2", 4 }, { "ARP", 3 }};
measured_string_ref configuration;
int count = 9;
size_t count = sizeof( names ) / sizeof( measured_string_t );
char * data;
int index;
ip_route_ref route;
in_addr_t gateway;
 
configuration = & names[ 0 ];
ip_netif = ( ip_netif_ref ) malloc( sizeof( ip_netif_t ));
if( ! ip_netif ) return ENOMEM;
ip_netif->device_id = device_id;
// get configuration
ERROR_PROPAGATE( net_get_device_conf_req( ip_globals.net_phone, ip_netif->device_id, & configuration, count, & data ));
if( configuration ){
117,23 → 234,35
if( ip_netif->dhcp ){
// TODO dhcp
net_free_settings( configuration, data );
free( ip_netif );
return ENOTSUP;
}else if( ip_netif->ipv == 4 ){
if( ERROR_OCCURRED( inet_pton( AF_INET, configuration[ 2 ].value, ( uint8_t * ) & ip_netif->address ))
|| ERROR_OCCURRED( inet_pton( AF_INET, configuration[ 3 ].value, ( uint8_t * ) & ip_netif->netmask ))
|| ( inet_pton( AF_INET, configuration[ 4 ].value, ( uint8_t * ) & ip_netif->gateway ) == EINVAL )
|| ( inet_pton( AF_INET, configuration[ 5 ].value, ( uint8_t * ) & ip_netif->broadcast ) == EINVAL )
route = ( ip_route_ref ) malloc( sizeof( ip_route_t ));
if( ! route ){
net_free_settings( configuration, data );
return ENOMEM;
}
route->address.s_addr = 0;
route->netmask.s_addr = 0;
route->gateway.s_addr = 0;
route->netif = ip_netif;
index = ip_routes_add( & ip_netif->routes, route );
if( index < 0 ){
net_free_settings( configuration, data );
free( route );
return index;
}
if( ERROR_OCCURRED( inet_pton( AF_INET, configuration[ 2 ].value, ( uint8_t * ) & route->address.s_addr ))
|| ERROR_OCCURRED( inet_pton( AF_INET, configuration[ 3 ].value, ( uint8_t * ) & route->netmask.s_addr ))
|| ( inet_pton( AF_INET, configuration[ 4 ].value, ( uint8_t * ) & gateway.s_addr ) == EINVAL )
|| ( inet_pton( AF_INET, configuration[ 5 ].value, ( uint8_t * ) & ip_netif->broadcast.s_addr ) == EINVAL )
|| ( inet_pton( AF_INET, configuration[ 6 ].value, ( uint8_t * ) & ip_netif->dns1 ) == EINVAL )
|| ( inet_pton( AF_INET, configuration[ 7 ].value, ( uint8_t * ) & ip_netif->dns2 ) == EINVAL )){
net_free_settings( configuration, data );
free( ip_netif );
return EINVAL;
}
}else{
// TODO ipv6
// TODO ipv6 in separate module
net_free_settings( configuration, data );
free( ip_netif );
return ENOTSUP;
}
if( configuration[ 8 ].value ){
141,7 → 270,6
if( ! ip_netif->arp ){
printf( "Failed to start the arp %s\n", configuration[ 8 ].value );
net_free_settings( configuration, data );
free( ip_netif );
return EINVAL;
}
}else{
149,66 → 277,64
}
net_free_settings( configuration, data );
}
ip_netif->phone = bind_service( netif, ip_netif->device_id, SERVICE_IP, 0, ip_globals.client_connection );
ip_netif->phone = bind_service( ip_netif->service, ip_netif->device_id, SERVICE_IP, 0, ip_globals.client_connection );
if( ip_netif->phone < 0 ){
printf( "Failed to contact the nil service %d\n", netif );
free( ip_netif );
printf( "Failed to contact the nil service %d\n", ip_netif->service );
return ip_netif->phone;
}
// MUST BE AFTER the bind_service up there!
if( ip_netif->arp ){
configuration[ 0 ].value = ( char * ) & ip_netif->address;
configuration[ 0 ].value = ( char * ) & route->address.s_addr;
configuration[ 0 ].length = CONVERT_SIZE( in_addr_t, char, 1 );
if( ERROR_OCCURRED( arp_device_req( ip_netif->arp->phone, ip_netif->device_id, SERVICE_IP, netif, & configuration[ 0 ] ))){
free( ip_netif );
return ERROR_CODE;
}
ERROR_PROPAGATE( arp_device_req( ip_netif->arp->phone, ip_netif->device_id, SERVICE_IP, ip_netif->service, & configuration[ 0 ] ));
}
// get packet dimensions
ERROR_PROPAGATE( nil_packet_size_req( ip_netif->phone, ip_netif->device_id, & ip_netif->addr_len, & ip_netif->prefix, & ip_netif->content, & ip_netif->suffix ));
if( ip_netif->content < IP_MIN_CONTENT ){
printf( "Maximum transmission unit %d bytes is too small, at least %d bytes are needed\n", ip_netif->content, IP_MIN_CONTENT );
ip_netif->content = IP_MIN_CONTENT;
}
index = ip_netifs_add( & ip_globals.netifs, ip_netif->device_id, ip_netif );
if( index < 0 ){
free( ip_netif );
return index;
if( index < 0 ) return index;
if( gateway.s_addr ){
// the default gateway
ip_globals.gateway.address.s_addr = 0;
ip_globals.gateway.netmask.s_addr = 0;
ip_globals.gateway.gateway.s_addr = gateway.s_addr;
ip_globals.gateway.netif = ip_netif;
}
if( ip_netif->arp ) ++ ip_netif->arp->usage;
// print the settings
printf( "New device registered:\n\tid\t= %d\n\tphone\t= %d\n\tIPV\t= %d\n", ip_netif->device_id, ip_netif->phone, ip_netif->ipv );
printf( "\tconfiguration\t= %s\n", ip_netif->dhcp ? "dhcp" : "static" );
// TODO ipv6 addresses
data = malloc( INET_ADDRSTRLEN );
if( data ){
inet_ntop( AF_INET, ( uint8_t * ) & ip_netif->address, data, INET_ADDRSTRLEN );
printf( "\taddress\t= %s\n", data );
inet_ntop( AF_INET, ( uint8_t * ) & ip_netif->netmask, data, INET_ADDRSTRLEN );
printf( "\tnetmask\t= %s\n", data );
inet_ntop( AF_INET, ( uint8_t * ) & ip_netif->gateway, data, INET_ADDRSTRLEN );
printf( "\tgateway\t= %s\n", data );
inet_ntop( AF_INET, ( uint8_t * ) & ip_netif->broadcast, data, INET_ADDRSTRLEN );
printf( "\tbroadcast\t= %s\n", data );
inet_ntop( AF_INET, ( uint8_t * ) & ip_netif->dns1, data, INET_ADDRSTRLEN );
printf( "\tdns1\t= %s\n", data );
inet_ntop( AF_INET, ( uint8_t * ) & ip_netif->dns2, data, INET_ADDRSTRLEN );
printf( "\tdns2\t= %s\n", data );
free( data );
}
return EOK;
}
 
int ip_device_state_msg( int il_phone, device_id_t device_id, device_state_t state ){
// ERROR_DECLARE;
ERROR_DECLARE;
 
/* measured_string_t address;
measured_string_ref translation;
char * data;
*/
packet_t packet;
in_addr_t destination;
 
ip_netif_ref netif;
 
/* measured_string_t address;
measured_string_ref translation;
char * data;
*/
netif = ip_netifs_find( & ip_globals.netifs, device_id );
if( ! netif ) return ENOENT;
netif->state = state;
// TODO state
printf( "ip - device %d changed state to %d\n\n", device_id, state );
/* if( netif->arp ){
address.value = ( char * ) & netif->gateway;
address.length = CONVERT_SIZE( in_addr_t, char, 1 );
if( netif->arp ){
/* address.value = ( char * ) & ip_globals.gateway.gateway.s_addr;
address.length = CONVERT_SIZE( ip_globals.gateway.gateway.s_addr, char, 1 );
if( ERROR_OCCURRED( arp_translate_req( netif->arp->phone, netif->device_id, SERVICE_IP, & address, & translation, & data ))){
ERROR_PROPAGATE( arp_translate_req( netif->arp->phone, netif->device_id, SERVICE_IP, & address, & translation, & data ));
}
printf( "\tgateway translated to\t= %X:%X:%X:%X:%X:%X\n", data[ 0 ], data[ 1 ], data[ 2 ], data[ 3 ], data[ 4 ], data[ 5 ] );
free( translation );
free( data );
address.value = ( char * ) & ip_globals.gateway.gateway.s_addr;
address.length = CONVERT_SIZE( ip_globals.gateway.gateway.s_addr, char, 1 );
if( ERROR_OCCURRED( arp_translate_req( netif->arp->phone, netif->device_id, SERVICE_IP, & address, & translation, & data ))){
sleep( 2 );
ERROR_PROPAGATE( arp_translate_req( netif->arp->phone, netif->device_id, SERVICE_IP, & address, & translation, & data ));
}
215,12 → 341,29
printf( "\tgateway translated to\t= %X:%X:%X:%X:%X:%X\n", data[ 0 ], data[ 1 ], data[ 2 ], data[ 3 ], data[ 4 ], data[ 5 ] );
free( translation );
free( data );
*/ printf( "IP - testing to send packet:\n" );
ERROR_PROPAGATE( inet_pton( AF_INET, "90.182.101.18", ( uint8_t * ) & destination.s_addr ));
packet = packet_get_4( ip_globals.net_phone, 30, netif->addr_len, netif->prefix + sizeof( ip_header_t ), netif->suffix );
if( ! packet ) return ENOMEM;
pq_release( ip_globals.net_phone, packet_get_id( packet ));
packet = packet_get_4( ip_globals.net_phone, 30, netif->addr_len, netif->prefix + sizeof( ip_header_t ), netif->suffix );
if( ! packet ) return ENOMEM;
pq_release( ip_globals.net_phone, packet_get_id( packet ));
packet = packet_get_4( ip_globals.net_phone, 30, netif->addr_len, netif->prefix + sizeof( ip_header_t ), netif->suffix );
if( ! packet ) return ENOMEM;
pq_release( ip_globals.net_phone, packet_get_id( packet ));
packet = packet_get_4( ip_globals.net_phone, 1500, netif->addr_len, netif->prefix + sizeof( ip_header_t ), netif->suffix );
if( ! packet ) return ENOMEM;
// try this long version
// if( ERROR_OCCURRED( packet_copy_data( packet, "Hi, this is IP, wery long version 1, wery long version 2, wery long version 3, wery long version 4, wery long version 5, wery long version 6, wery long version 7, wery long version 8, wery long version 9, wery long version 10, wery long version 11, wery long version 12, wery long version 13, wery long version 14, wery long version 15, wery long version 16, wery long version 17, wery long version 18, wery long version 19, wery long version 20, wery long version 21, wery long version 22, wery long version 23, wery long version 24, wery long version 25, wery long version 26, wery long version 27, wery long version 28, wery long version 29, wery long version 30Hi, this is IP, wery long version 1, wery long version 2, wery long version 3, wery long version 4, wery long version 5, wery long version 6, wery long version 7, wery long version 8, wery long version 9, wery long version 10, wery long version 11, wery long version 12, wery long version 13, wery long version 14, wery long version 15, wery long version 16, wery long version 17, wery long version 18, wery long version 19, wery long version 20, wery long version 21, wery long version 22, wery long version 23, wery long version 24, wery long version 25, wery long version 26, wery long version 27, wery long version 28, wery long version 29, wery long version 30", 1330 ))
if( ERROR_OCCURRED( packet_copy_data( packet, "Hi, this is IP", 14 ))
|| ERROR_OCCURRED( packet_set_addr( packet, NULL, ( uint8_t * ) & destination.s_addr, 4 ))
|| ERROR_OCCURRED( ip_client_prepare_packet( packet, 0, 0, 0, 0, 0 ))){
pq_release( ip_globals.net_phone, packet_get_id( packet ));
}
ERROR_CODE = ip_send_msg( 0, 0, packet, SERVICE_IP );
printf( "send returned %d\n", ERROR_CODE );
}
*/ return EOK;
}
 
int ip_bind_service( services_t service, services_t me, async_client_conn_t receiver ){
//TODO receive function
return EOK;
}
 
228,14 → 371,21
return EOK;
}
 
int ip_register( int il_phone, int protocol, int phone ){
int ip_bind_service( services_t service, int protocol, services_t me, async_client_conn_t receiver, tl_received_msg_t tl_received_msg ){
return ip_register( protocol, me, 0, tl_received_msg );
}
 
int ip_register( int protocol, services_t service, int phone, tl_received_msg_t tl_received_msg ){
ip_proto_ref proto;
int index;
 
if( !( protocol && service && (( phone > 0 ) || ( tl_received_msg )))) return EINVAL;
proto = ( ip_proto_ref ) malloc( sizeof( ip_protos_t ));
if( ! proto ) return ENOMEM;
proto->protocol = protocol;
proto->service = service;
proto->phone = phone;
proto->tl_received_msg = tl_received_msg;
index = ip_protos_add( & ip_globals.protos, proto->protocol, proto );
if( index < 0 ){
free( proto );
245,13 → 395,147
return EOK;
}
 
int ip_send_msg( int il_phone, device_id_t device_id, packet_t packet, services_t target ){
// TODO send packet
printf( "Packet to send via %d: %s\n", device_id, packet_get_data( packet ));
pq_release( ip_globals.net_phone, packet_get_id( packet ));
int ip_send_msg( int il_phone, device_id_t device_id, packet_t packet, services_t sender ){
ERROR_DECLARE;
 
int length;
ip_netif_ref netif;
ip_route_ref route;
in_addr_t dest;
in_addr_t * src;
 
// addresses in the host byte order
// should be the next hop address or the target destination address
length = packet_get_addr( packet, NULL, ( void * )( & dest.s_addr ));
if( length < 0 ){
pq_release( ip_globals.net_phone, packet_get_id( packet ));
return length;
}
// TODO IPv6
if( length != IP_ADDR ){
pq_release( ip_globals.net_phone, packet_get_id( packet ));
return EINVAL;
}
// rwlock_read_lock( & ip_globals.devices_lock );
// device specified?
// dest.s_addr = ntohl( dest.s_addr );
if( device_id ){
netif = ip_netifs_find( & ip_globals.netifs, device_id );
route = ip_netif_find_route( netif, dest );
}else{
// TODO IPv6
route = ip_find_route( dest );
netif = route ? route->netif : NULL;
}
if( !( netif && route )){
// rwlock_read_unlock( & ip_globals.devices_lock );
pq_release( ip_globals.net_phone, packet_get_id( packet ));
return ENOENT;
}
// to me?
if( route->address.s_addr == dest.s_addr ){
// TODO loopback deliver
}
 
src = ip_netif_addr( netif );
if( ! src ){
pq_release( ip_globals.net_phone, packet_get_id( packet ));
return ENOENT;
}
if( ERROR_OCCURRED( ip_send_route( packet, netif, route, src, dest ))){
pq_release( ip_globals.net_phone, packet_get_id( packet ));
}
return ERROR_CODE;
}
 
in_addr_t * ip_netif_addr( ip_netif_ref netif ){
ip_route_ref route;
 
route = ip_routes_get_index( & netif->routes, 0 );
return route ? & route->address : NULL;
}
 
int ip_send_route( packet_t packet, ip_netif_ref netif, ip_route_ref route, in_addr_t * src, in_addr_t dest ){
ERROR_DECLARE;
 
packet_t next;
packet_t tmp;
measured_string_t destination;
measured_string_ref translation;
char * data;
 
if( route->gateway.s_addr ){
dest.s_addr = route->gateway.s_addr;
}
// get destination hardware address
if( netif->arp ){
destination.value = ( char * ) & dest.s_addr;
destination.length = CONVERT_SIZE( dest.s_addr, char, 1 );
if( ERROR_OCCURRED( arp_translate_req( netif->arp->phone, netif->device_id, SERVICE_IP, & destination, & translation, & data ))){
usleep( 200000 );
ERROR_PROPAGATE( arp_translate_req( netif->arp->phone, netif->device_id, SERVICE_IP, & destination, & translation, & data ));
}
// TODO unreachable
if( ! translation ) return EINVAL;
if( ! translation->value ){
// TODO unreachable
free( translation );
free( data );
return EINVAL;
}
}else translation = NULL;
// process packet queue
next = packet;
do{
if( ERROR_OCCURRED( ip_prepare_packet( src, next, translation ))){
// release invalid packet
tmp = pq_detach( next );
if( next == packet ) packet = tmp;
pq_release( ip_globals.net_phone, packet_get_id( next ));
next = tmp;
}else{
next = pq_next( next );
}
}while( next );
if( translation ){
free( translation );
free( data );
}
// send packet queue
if( packet ){
packet = ip_split_packet( packet, netif->prefix, netif->content, netif->suffix, netif->addr_len );
if( packet ){
nil_send_msg( netif->phone, netif->device_id, packet, SERVICE_IP );
}
}
// rwlock_read_unlock( & ip_globals.netifs_lock );
return EOK;
}
 
int ip_prepare_packet( in_addr_t * source, packet_t packet, measured_string_ref destination ){
ERROR_DECLARE;
 
int length;
ip_header_ref header;
 
length = packet_get_data_length( packet );
if(( length < sizeof( ip_header_t )) || ( length > IP_MAX_CONTENT )) return EINVAL;
header = ( ip_header_ref ) packet_get_data( packet );
if( destination ){
ERROR_PROPAGATE( packet_set_addr( packet, NULL, ( uint8_t * ) destination->value, CONVERT_SIZE( char, uint8_t, destination->length )));
}
header->version = 4;
header->total_length = htons( length );
header->fragment_offset = 0;
if( source ) header->source_address = source->s_addr;//htonl( source.s_addr );
++ ip_globals.packet_counter;
header->identification = htons( ip_globals.packet_counter );
header->header_checksum = 0;
// unnecessary for all protocols
header->header_checksum = IP_HEADER_CHECKSUM( header );
return EOK;
}
 
int ip_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
ERROR_DECLARE;
 
264,7 → 548,7
case NET_IL_DEVICE:
return ip_device_req( 0, IPC_GET_DEVICE( call ), IPC_GET_SERVICE( call ));
case IPC_M_CONNECT_TO_ME:
return ip_register( 0, IL_GET_PROTO( call ), IPC_GET_PHONE( call ));
return ip_register( IL_GET_PROTO( call ), IL_GET_SERVICE( call ), IPC_GET_PHONE( call ), NULL );
case NET_IL_SEND:
ERROR_PROPAGATE( packet_translate( ip_globals.net_phone, & packet, IPC_GET_PACKET( call )));
return ip_send_msg( 0, IPC_GET_DEVICE( call ), packet, 0 );
271,14 → 555,327
case NET_IL_DEVICE_STATE:
case NET_NIL_DEVICE_STATE:
return ip_device_state_msg( 0, IPC_GET_DEVICE( call ), IPC_GET_STATE( call ));
// TODO packet received
case NET_IL_RECEIVED:
case NET_NIL_RECEIVED:
ERROR_PROPAGATE( packet_translate( ip_globals.net_phone, & packet, IPC_GET_PACKET( call )));
//return il_receive_msg( 0, IPC_GET_DEVICE( call ), packet );
return ip_received_msg( IPC_GET_DEVICE( call ), packet );
case NET_IP_ADD_ROUTE:
return ip_add_route_req( 0, IPC_GET_DEVICE( call ), IP_GET_ADDRESS( call ), IP_GET_NETMASK( call ), IP_GET_GATEWAY( call ));
case NET_IP_SET_GATEWAY:
return ip_set_gateway_req( 0, IPC_GET_DEVICE( call ), IP_GET_GATEWAY( call ));
case NET_IL_PACKET_SPACE:
ERROR_PROPAGATE( ip_packet_size_req( 0, IPC_GET_DEVICE( call ), IPC_SET_ADDR( answer ), IPC_SET_PREFIX( answer ), IPC_SET_CONTENT( answer ), IPC_SET_SUFFIX( answer )));
* answer_count = 3;
return EOK;
}
return ENOTSUP;
}
 
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 ){
ip_netif_ref netif;
 
if( !( addr_len && prefix && content && suffix )) return EBADMEM;
// rwlock_read_lock( & ip_globals.netifs_lock );
netif = ip_netifs_find( & ip_globals.netifs, device_id );
if( ! netif ){
// rwlock_read_unlock( & ip_globals.netifs_lock );
return ENOENT;
}
* content = IP_MAX_CONTENT - IP_PREFIX;
* addr_len = ( netif->addr_len > IP_ADDR ) ? netif->addr_len : IP_ADDR;
* prefix = netif->prefix + IP_PREFIX;
* suffix = netif->suffix + IP_SUFFIX;
// rwlock_read_unlock( & ip_globals.netifs_lock );
return EOK;
}
 
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 ){
ip_route_ref route;
ip_netif_ref netif;
int index;
 
netif = ip_netifs_find( & ip_globals.netifs, device_id );
if( ! netif ) return ENOENT;
route = ( ip_route_ref ) malloc( sizeof( ip_route_t ));
if( ! route ) return ENOMEM;
route->address.s_addr = address.s_addr;
route->netmask.s_addr = netmask.s_addr;
route->gateway.s_addr = gateway.s_addr;
route->netif = netif;
index = ip_routes_add( & netif->routes, route );
if( index < 0 ) free( route );
return index;
}
 
ip_route_ref ip_find_route( in_addr_t destination ){
int index;
ip_route_ref route;
ip_netif_ref netif;
 
// start with the last netif - the newest one
index = ip_netifs_count( & ip_globals.netifs ) - 1;
while( index >= 0 ){
netif = ip_netifs_get_index( & ip_globals.netifs, index );
if( netif && ( netif->state == NETIF_ACTIVE )){
route = ip_netif_find_route( netif, destination );
if( route ) return route;
}
-- index;
}
return & ip_globals.gateway;
}
 
ip_route_ref ip_netif_find_route( ip_netif_ref netif, in_addr_t destination ){
int index;
ip_route_ref route;
 
if( netif ){
// start with the first one - the direct route
for( index = 0; index < ip_routes_count( & netif->routes ); ++ index ){
route = ip_routes_get_index( & netif->routes, index );
if( route && (( route->address.s_addr & route->netmask.s_addr ) == ( destination.s_addr & route->netmask.s_addr ))){
return route;
}
}
}
return NULL;
}
 
int ip_set_gateway_req( int ip_phone, device_id_t device_id, in_addr_t gateway ){
ip_netif_ref netif;
 
netif = ip_netifs_find( & ip_globals.netifs, device_id );
if( ! netif ) return ENOENT;
ip_globals.gateway.address.s_addr = 0;
ip_globals.gateway.netmask.s_addr = 0;
ip_globals.gateway.gateway.s_addr = gateway.s_addr;
ip_globals.gateway.netif = netif;
return EOK;
}
 
packet_t ip_split_packet( packet_t packet, size_t prefix, size_t content, size_t suffix, size_t addr_len ){
size_t length;
packet_t next;
packet_t new_packet;
 
next = packet;
// check all packets
while( next ){
length = packet_get_data_length( next );
// too long?
if( length > content ){
if( ip_fragment_packet( next, content, prefix, suffix, addr_len ) != EOK ){
new_packet = pq_detach( next );
if( next == packet ){
packet = new_packet;
}
pq_release( ip_globals.net_phone, packet_get_id( next ));
next = new_packet;
continue;
}
}
next = pq_next( next );
}
return packet;
}
 
int ip_fragment_packet( packet_t packet, size_t length, size_t prefix, size_t suffix, size_t addr_len ){
ERROR_DECLARE;
 
packet_t new_packet;
ip_header_ref header;
ip_header_ref middle_header;
ip_header_ref last_header;
uint8_t * src;
uint8_t * dest;
int address_length;
 
address_length = packet_get_addr( packet, & src, & dest );
if( address_length <= 0 ) return EINVAL;
if( packet_get_data_length( packet ) <= sizeof( ip_header_t )) return ENOMEM;
// get header
header = ( ip_header_ref ) packet_get_data( packet );
if( ! header ) return EINVAL;
// fragmentation forbidden?
if( header->flags & IPFLAG_DONT_FRAGMENT ){
// TODO fragmentation necessary ICMP
return EPERM;
}
// create the last fragment
new_packet = packet_get_4( ip_globals.net_phone, prefix, length, suffix, (( address_length > addr_len ) ? address_length : addr_len ));
if( ! new_packet ) return ENOMEM;
last_header = ip_create_last_header( new_packet, header );
if( ! last_header ){
pq_release( ip_globals.net_phone, packet_get_id( new_packet ));
return ENOMEM;
}
// biggest multiple of 8 lower than content
// TODO even fragmentation?
length = length & ( ~ 0x7 );// ( content / 8 ) * 8
if( ERROR_OCCURRED( ip_fragment_packet_data( packet, new_packet, header, last_header, (( IP_TOTAL_LENGTH( header ) - length ) % ( length - IP_HEADER_LENGTH( last_header ))), src, dest, address_length ))){
pq_release( ip_globals.net_phone, packet_get_id( new_packet ));
return ERROR_CODE;
}
// mark the first as fragmented
header->flags |= IPFLAG_MORE_FRAGMENTS;
// create middle framgents
while( IP_TOTAL_LENGTH( header ) > length ){
new_packet = packet_get_4( ip_globals.net_phone, prefix, length, suffix, (( address_length >= addr_len ) ? address_length : addr_len ));
if( ! new_packet ) return ENOMEM;
middle_header = ip_create_middle_header( new_packet, last_header );
if( ! middle_header ){
pq_release( ip_globals.net_phone, packet_get_id( new_packet ));
return ENOMEM;
}
if( ERROR_OCCURRED( ip_fragment_packet_data( packet, new_packet, header, middle_header, length - IP_HEADER_LENGTH( middle_header ), src, dest, address_length ))){
pq_release( ip_globals.net_phone, packet_get_id( new_packet ));
return ERROR_CODE;
}
}
// finish the first fragment
header->header_checksum = IP_HEADER_CHECKSUM( header );
printf( "ok\n" );
return EOK;
}
 
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 ){
ERROR_DECLARE;
 
void * data;
 
data = packet_suffix( new_packet, length );
if( ! data ) return ENOMEM;
memcpy( data, (( void * ) header ) + IP_TOTAL_LENGTH( header ) - length, length );
ERROR_PROPAGATE( packet_trim( packet, 0, length ));
header->total_length = htons( IP_TOTAL_LENGTH( header ) - length );
new_header->total_length = htons( IP_HEADER_LENGTH( new_header ) + length );
new_header->fragment_offset = header->fragment_offset + IP_HEADER_DATA_LENGTH( header ) / 8;
new_header->header_checksum = IP_HEADER_CHECKSUM( new_header );
ERROR_PROPAGATE( packet_set_addr( new_packet, src, dest, address_length ));
return pq_insert_after( packet, new_packet );
}
 
ip_header_ref ip_create_middle_header( packet_t packet, ip_header_ref last ){
ip_header_ref middle;
 
middle = ( ip_header_ref ) packet_suffix( packet, IP_HEADER_LENGTH( last ));
if( ! middle ) return NULL;
memcpy( middle, last, IP_HEADER_LENGTH( last ));
middle->flags |= IPFLAG_MORE_FRAGMENTS;
return middle;
}
 
ip_header_ref ip_create_last_header( packet_t packet, ip_header_ref first ){
ip_header_ref last;
ip_option_ref option;
size_t next;
size_t length;
 
// allocate as much as originally
last = ( ip_header_ref ) packet_suffix( packet, IP_HEADER_LENGTH( first ));
if( ! last ) return NULL;
// copy first itself
memcpy( last, first, sizeof( ip_header_t ));
length = sizeof( ip_header_t );
next = sizeof( ip_header_t );
// process all ip options
while( next < first->ihl ){
option = ( ip_option_ref ) ((( void * ) first ) + next );
// skip end or noop
if(( option->type == IPOPT_END ) || ( option->type == IPOPT_NOOP )){
++ next;
}else{
// copy if said so or skip
if( IPOPT_COPIED( option->type )){
memcpy((( void * ) last ) + length, (( void * ) first ) + next, option->length );
length += option->length;
}
// next option
next += option->length;
}
}
// align 4 byte boundary
if( length % 4 ){
bzero((( void * ) last ) + length, 4 - ( length % 4 ));
last->ihl = length / 4 + 1;
}else{
last->ihl = length / 4;
}
// trim the unused space
if( packet_trim( packet, 0, IP_HEADER_LENGTH( first ) - IP_HEADER_LENGTH( last )) != EOK ) return NULL;
return last;
}
 
int ip_received_msg( device_id_t device_id, packet_t packet ){
packet_t next;
 
do{
next = pq_detach( packet );
if( ip_process_packet( device_id, packet ) != EOK ){
pq_release( ip_globals.net_phone, packet_get_id( packet ));
}
packet = next;
}while( packet );
return EOK;
}
 
int ip_process_packet( device_id_t device_id, packet_t packet ){
ERROR_DECLARE;
 
ip_header_ref header;
in_addr_t dest;
ip_route_ref route;
 
header = ( ip_header_ref ) packet_get_data( packet );
if( ! header ) return ENOMEM;
// checksum
if(( header->header_checksum ) && ( IP_HEADER_CHECKSUM( header ))){
// TODO checksum error ICMP?
return EINVAL;
}
// TODO ttl oxceeded ICMP?
if( !( -- header->ttl )) return EINVAL;
// process ipopt and get destination
dest = ip_get_destination( header );
ERROR_PROPAGATE( packet_set_addr( packet, NULL, ( uint8_t * ) & dest.s_addr, IP_ADDR ));
route = ip_find_route( dest );
// TODO unreachable ICMP?
if( ! route ) return ENOENT;
if( route->address.s_addr == dest.s_addr ){
// local delivery
return ip_deliver_local( device_id, packet, header );
}else{
return ip_send_route( packet, route->netif, route, NULL, dest );
}
}
 
int ip_deliver_local( device_id_t device_id, packet_t packet, ip_header_ref header ){
ERROR_DECLARE;
 
ip_proto_ref proto;
 
if(( header->flags & IPFLAG_MORE_FRAGMENTS ) || header->fragment_offset ){
// TODO fragmented
return ENOTSUP;
}else{
proto = ip_protos_find( & ip_globals.protos, header->protocol );
if( ! proto ) return ENOENT;
ERROR_PROPAGATE( packet_set_addr( packet, ( uint8_t * ) & header->source_address, ( uint8_t * ) & header->destination_address, IP_ADDR ));
if( proto->tl_received_msg ){
return proto->tl_received_msg( device_id, packet, SERVICE_IP );
}else{
return tl_received_msg( proto->phone, device_id, packet, proto->service );
}
}
}
 
in_addr_t ip_get_destination( ip_header_ref header ){
in_addr_t destination;
 
// TODO search set ipopt route?
destination.s_addr = header->destination_address; //ntohl( header->destination_address );
return destination;
}
 
/** @}
*/