Rev 4695 | Rev 4707 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
/*
* Copyright (c) 2009 Lukas Mejdrech
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* - The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/** @addtogroup ip
* @{
*/
/** @file
* IP module implementation.
* @see arp.h
* \todo
*/
#include <async.h>
#include <errno.h>
#include <fibril_sync.h>
#include <stdio.h>
#include <string.h>
#include <ipc/ipc.h>
#include <ipc/services.h>
#include <sys/types.h>
#include "../../err.h"
#include "../../messages.h"
#include "../../modules.h"
#include "../../include/net_interface.h"
#include "../../include/inet.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"
#include "../../nil/nil_messages.h"
#include "../il_messages.h"
#include "ip.h"
#include "ip_header.h"
#include "ip_messages.h"
#include "ip_module.h"
/** Default IP version.
*/
#define DEFAULT_IPV 4
/** Minimum IP packet content.
*/
#define IP_MIN_CONTENT 576
/** ARP module name.
*/
#define ARP_NAME "arp"
/** ARP module filename.
*/
#define ARP_FILENAME "/srv/arp"
/** IP packet address length.
*/
#define IP_ADDR sizeof( in_addr_t )
/** IP packet prefix length.
*/
#define IP_PREFIX sizeof( ip_header_t )
/** IP packet suffix length.
*/
#define IP_SUFFIX 0
/** IP packet maximum content length.
*/
#define IP_MAX_CONTENT 65535
/** Returns the actual IP header length.
* @param header The IP packet header. Input parameter.
*/
#define IP_HEADER_LENGTH( header ) (( header )->ihl * 4u )
/** Returns the actual IP packet total length.
* @param header The IP packet header. Input parameter.
*/
#define IP_TOTAL_LENGTH( header ) ntohs(( header )->total_length )
/** Returns the actual IP packet data length.
* @param header The IP packet header. Input parameter.
*/
#define IP_HEADER_DATA_LENGTH( header ) ( IP_TOTAL_LENGTH( header ) - IP_HEADER_LENGTH( header ))
/** Returns the IP packet header checksum.
* @param header The IP packet header. Input parameter.
*/
#define IP_HEADER_CHECKSUM( header ) ( htons( ip_checksum(( uint8_t * )( header ), IP_HEADER_LENGTH( header ))))
/** IP header checksum value for computed zero checksum.
* Zero is returned as 0xFFFF (not flipped)
*/
#define IP_HEADER_CHECKSUM_ZERO 0xFFFFu
/** IP global data.
*/
ip_globals_t ip_globals;
DEVICE_MAP_IMPLEMENT( ip_netifs, ip_netif_t )
INT_MAP_IMPLEMENT( ip_protos, ip_proto_t )
GENERIC_FIELD_IMPLEMENT( ip_routes, ip_route_t )
/** Updates the device content length according to the new MTU value.
* @param device_id The device identifier. Input parameter.
* @param mtu The new mtu value. Input parameter.
* @returns EOK on success.
* @returns ENOENT if device is not found.
*/
int ip_mtu_changed_message( device_id_t device_id, size_t mtu );
/** Updates the device state.
* @param device_id The device identifier. Input parameter.
* @param state The new state value. Input parameter.
* @returns EOK on success.
* @returns ENOENT if device is not found.
*/
int ip_device_state_message( device_id_t device_id, device_state_t state );
int ip_register( int protocol, services_t service, int phone, tl_received_msg_t tl_received_msg );
/** Initializes a new network interface specific data.
* Connects to the network interface layer module, reads the netif configuration, starts an ARP module if needed and sets the netif routing table.
* The device identifier and the nil service has to be set.
* @param ip_netif Network interface specific data. Input/output parameter.
* @returns EOK on success.
* @returns ENOTSUP if DHCP is configured.
* @returns ENOTSUP if IPv6 is configured.
* @returns EINVAL if any of the addresses is invalid.
* @returns EINVAL if the used ARP module is not known.
* @returns ENOMEM if there is not enough memory left.
* @returns Other error codes as defined for the net_get_device_conf_req() function.
* @returns Other error codes as defined for the bind_service() function.
* @returns Other error codes as defined for the specific arp_device_req() function.
* @returns Other error codes as defined for the nil_packet_size_req() function.
*/
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, in_addr_t dest, 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 );
void ip_create_last_header( ip_header_ref last, 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 );
/** Processes the received IP packet.
* @param device_id The source device identifier. Input parameter.
* @param packet The received packet. Input/output parameter.
* @returns EOK on success and the packet is no longer needed.
* @returns EINVAL if the packet is too small to carry the IP packet.
* @returns EINVAL if the received address lengths differs from the registered values.
* @returns ENOENT if the device is not found in the cache.
* @returns ENOENT if the protocol for the device is not found in the cache.
* @returns ENOMEM if there is not enough memory left.
*/
int ip_receive_message( 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 0xFFFF if the computed checksum is zero.
*/
uint16_t ip_checksum( uint8_t * data, size_t length );
uint16_t ip_checksum( uint8_t * data, size_t 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;
}
int ip_initialize( async_client_conn_t client_connection ){
ERROR_DECLARE;
fibril_rwlock_initialize( & ip_globals.lock );
fibril_rwlock_write_lock( & ip_globals.lock );
fibril_rwlock_initialize( & ip_globals.protos_lock );
fibril_rwlock_initialize( & ip_globals.netifs_lock );
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;
ERROR_PROPAGATE( modules_initialize( & ip_globals.modules ));
ERROR_PROPAGATE( add_module( NULL, & ip_globals.modules, ARP_NAME, ARP_FILENAME, SERVICE_ARP, arp_task_get_id(), arp_connect_module ));
fibril_rwlock_write_unlock( & ip_globals.lock );
return EOK;
}
int ip_device_req( int il_phone, device_id_t device_id, services_t netif ){
ERROR_DECLARE;
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;
fibril_rwlock_write_lock( & ip_globals.netifs_lock );
if( ERROR_OCCURRED( ip_netif_initialize( ip_netif ))){
fibril_rwlock_write_unlock( & ip_globals.netifs_lock );
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 );
}
fibril_rwlock_write_unlock( & ip_globals.netifs_lock );
return EOK;
}
int ip_netif_initialize( ip_netif_ref ip_netif ){
ERROR_DECLARE;
measured_string_t names[] = {{ "IPV", 3 }, { "IP_CONFIG", 9 }, { "IP_ADDR", 7 }, { "NETMASK", 7 }, { "GATEWAY", 7 }, { "BROADCAST", 9 }, { "DNS1", 4 }, { "DNS2", 4 }, { "ARP", 3 }, { "IP_ROUTING", 10 }};
measured_string_ref configuration;
size_t count = sizeof( names ) / sizeof( measured_string_t );
char * data;
int index;
ip_route_ref route;
in_addr_t gateway;
ip_netif->arp = NULL;
route = NULL;
configuration = & names[ 0 ];
// get configuration
ERROR_PROPAGATE( net_get_device_conf_req( ip_globals.net_phone, ip_netif->device_id, & configuration, count, & data ));
if( configuration ){
if( configuration[ 0 ].value ){
ip_netif->ipv = strtol( configuration[ 0 ].value, NULL, 0 );
}else{
ip_netif->ipv = DEFAULT_IPV;
}
ip_netif->dhcp = ! str_lcmp( configuration[ 1 ].value, "dhcp", configuration[ 1 ].length );
if( ip_netif->dhcp ){
// TODO dhcp
net_free_settings( configuration, data );
return ENOTSUP;
}else if( ip_netif->ipv == 4 ){
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 );
return EINVAL;
}
}else{
// TODO ipv6 in separate module
net_free_settings( configuration, data );
return ENOTSUP;
}
if( configuration[ 8 ].value ){
ip_netif->arp = get_running_module( & ip_globals.modules, configuration[ 8 ].value );
if( ! ip_netif->arp ){
printf( "Failed to start the arp %s\n", configuration[ 8 ].value );
net_free_settings( configuration, data );
return EINVAL;
}
}else{
ip_netif->arp = NULL;
}
ip_netif->routing = configuration[ 9 ].value && ( configuration[ 9 ].value[ 0 ] == 'y' );
net_free_settings( configuration, data );
}
ip_netif->phone = bind_service( ip_netif->service, ( ipcarg_t ) 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", ip_netif->service );
return ip_netif->phone;
}
// MUST BE AFTER the bind_service up there!
if( ip_netif->arp ){
if( route ){
configuration[ 0 ].value = ( char * ) & route->address.s_addr;
configuration[ 0 ].length = CONVERT_SIZE( in_addr_t, char, 1 );
ERROR_PROPAGATE( arp_device_req( ip_netif->arp->phone, ip_netif->device_id, SERVICE_IP, ip_netif->service, & configuration[ 0 ] ));
}else{
ip_netif->arp = 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 ) 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;
}
return EOK;
}
int ip_mtu_changed_message( device_id_t device_id, size_t mtu ){
ip_netif_ref netif;
fibril_rwlock_write_lock( & ip_globals.netifs_lock );
netif = ip_netifs_find( & ip_globals.netifs, device_id );
if( ! netif ){
fibril_rwlock_write_unlock( & ip_globals.netifs_lock );
return ENOENT;
}
netif->content = mtu;
printf( "ip - device %d changed mtu to %d\n\n", device_id, mtu );
fibril_rwlock_write_unlock( & ip_globals.netifs_lock );
return EOK;
}
int ip_device_state_message( device_id_t device_id, device_state_t state ){
// ERROR_DECLARE;
/* measured_string_t address;
measured_string_ref translation;
char * data;
*/
/* packet_t packet;
in_addr_t destination;
*/
ip_netif_ref netif;
fibril_rwlock_write_lock( & ip_globals.netifs_lock );
netif = ip_netifs_find( & ip_globals.netifs, device_id );
if( ! netif ){
fibril_rwlock_write_unlock( & ip_globals.netifs_lock );
return ENOENT;
}
netif->state = state;
// TODO state
printf( "ip - device %d changed state to %d\n\n", device_id, state );
fibril_rwlock_write_unlock( & ip_globals.netifs_lock );
// 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 ));
}
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_connect_module( services_t service ){
return EOK;
}
int ip_bind_service( services_t service, int protocol, services_t me, async_client_conn_t receiver, tl_received_msg_t received_msg ){
return ip_register( protocol, me, 0, received_msg );
}
int ip_register( int protocol, services_t service, int phone, tl_received_msg_t received_msg ){
ip_proto_ref proto;
int index;
if( !( protocol && service && (( phone > 0 ) || ( 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->received_msg = received_msg;
fibril_rwlock_write_lock( & ip_globals.protos_lock );
index = ip_protos_add( & ip_globals.protos, proto->protocol, proto );
if( index < 0 ){
fibril_rwlock_write_unlock( & ip_globals.protos_lock );
free( proto );
return index;
}
printf( "New protocol registered:\n\tprotocol\t= %d\n\tphone\t= %d\n", proto->protocol, proto->phone );
fibril_rwlock_write_unlock( & ip_globals.protos_lock );
return EOK;
}
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, ( uint8_t ** ) & dest );
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;
}
fibril_rwlock_read_lock( & ip_globals.netifs_lock );
// device specified?
// dest.s_addr = ntohl( dest.s_addr );
if( device_id > 0 ){
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 )){
fibril_rwlock_read_unlock( & ip_globals.netifs_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
fibril_rwlock_read_unlock( & ip_globals.netifs_lock );
return ip_deliver_local( -1, packet, ( ip_header_ref ) packet_get_data( packet ));
}
src = ip_netif_addr( netif );
if( ! src ){
fibril_rwlock_read_unlock( & ip_globals.netifs_lock );
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 ));
}
fibril_rwlock_read_unlock( & ip_globals.netifs_lock );
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;
measured_string_t destination;
measured_string_ref translation;
char * data;
// get destination hardware address
if( netif->arp ){
destination.value = route->gateway.s_addr ? ( char * ) & route->gateway.s_addr : ( 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 ))){
sleep( 1 );
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;
if( ERROR_OCCURRED( ip_prepare_packet( src, dest, packet, translation ))){
pq_release( ip_globals.net_phone, packet_get_id( packet ));
}else{
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 );
}
}
if( translation ){
free( translation );
free( data );
}
return ERROR_CODE;
}
int ip_prepare_packet( in_addr_t * source, in_addr_t dest, packet_t packet, measured_string_ref destination ){
ERROR_DECLARE;
size_t length;
ip_header_ref header;
ip_header_ref last_header;
ip_header_ref middle_header;
packet_t next;
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->fragment_offset = 0;
header->header_checksum = 0;
if( source ) header->source_address = source->s_addr;
header->destination_address = dest.s_addr;
fibril_rwlock_write_lock( & ip_globals.lock );
++ ip_globals.packet_counter;
header->identification = htons( ip_globals.packet_counter );
fibril_rwlock_write_unlock( & ip_globals.lock );
length = packet_get_data_length( packet );
if( pq_next( packet )){
last_header = ( ip_header_ref ) malloc( IP_HEADER_LENGTH( header ));
if( ! last_header ) return ENOMEM;
ip_create_last_header( last_header, header );
next = pq_next( packet );
while( pq_next( next )){
middle_header = ( ip_header_ref ) packet_prefix( next, IP_HEADER_LENGTH( last_header ));
if( ! middle_header ) return ENOMEM;
memcpy( middle_header, last_header, IP_HEADER_LENGTH( last_header ));
header->flags |= IPFLAG_MORE_FRAGMENTS;
middle_header->total_length = htons( packet_get_data_length( next ));
middle_header->fragment_offset = length / 8;
middle_header->header_checksum = IP_HEADER_CHECKSUM( middle_header );
if( destination ){
ERROR_PROPAGATE( packet_set_addr( next, NULL, ( uint8_t * ) destination->value, CONVERT_SIZE( char, uint8_t, destination->length )));
}
length += packet_get_data_length( next );
next = pq_next( next );
}
middle_header = ( ip_header_ref ) packet_prefix( next, IP_HEADER_LENGTH( last_header ));
if( ! middle_header ) return ENOMEM;
memcpy( middle_header, last_header, IP_HEADER_LENGTH( last_header ));
middle_header->total_length = htons( packet_get_data_length( next ));
middle_header->fragment_offset = length / 8;
middle_header->header_checksum = IP_HEADER_CHECKSUM( middle_header );
if( destination ){
ERROR_PROPAGATE( packet_set_addr( next, NULL, ( uint8_t * ) destination->value, CONVERT_SIZE( char, uint8_t, destination->length )));
}
length += packet_get_data_length( next );
free( last_header );
header->flags |= IPFLAG_MORE_FRAGMENTS;
}
header->total_length = htons( length );
// 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;
packet_t packet;
* answer_count = 0;
switch( IPC_GET_METHOD( * call )){
case IPC_M_PHONE_HUNGUP:
return EOK;
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( 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 );
case NET_IL_DEVICE_STATE:
return ip_device_state_message( IPC_GET_DEVICE( call ), IPC_GET_STATE( call ));
case NET_IL_RECEIVED:
ERROR_PROPAGATE( packet_translate( ip_globals.net_phone, & packet, IPC_GET_PACKET( call )));
return ip_receive_message( 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;
case NET_IL_MTU_CHANGED:
return ip_mtu_changed_message( IPC_GET_DEVICE( call ), IPC_GET_MTU( call ));
}
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;
int index;
if( !( addr_len && prefix && content && suffix )) return EBADMEM;
* content = IP_MAX_CONTENT - IP_PREFIX;
fibril_rwlock_read_lock( & ip_globals.netifs_lock );
if( device_id < 0 ){
* addr_len = IP_ADDR;
* prefix = 0;
* suffix = 0;
for( index = ip_netifs_count( & ip_globals.netifs ) - 1; index >= 0; -- index ){
netif = ip_netifs_get_index( & ip_globals.netifs, index );
if( netif ){
if( netif->addr_len > * addr_len ) * addr_len = netif->addr_len;
if( netif->prefix > * prefix ) * prefix = netif->prefix;
if( netif->suffix > * suffix ) * suffix = netif->suffix;
}
}
* prefix = * prefix + IP_PREFIX;
* suffix = * suffix + IP_SUFFIX;
}else{
netif = ip_netifs_find( & ip_globals.netifs, device_id );
if( ! netif ){
fibril_rwlock_read_unlock( & ip_globals.netifs_lock );
return ENOENT;
}
* addr_len = ( netif->addr_len > IP_ADDR ) ? netif->addr_len : IP_ADDR;
* prefix = netif->prefix + IP_PREFIX;
* suffix = netif->suffix + IP_SUFFIX;
}
fibril_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;
fibril_rwlock_write_lock( & ip_globals.netifs_lock );
netif = ip_netifs_find( & ip_globals.netifs, device_id );
if( ! netif ){
fibril_rwlock_write_unlock( & ip_globals.netifs_lock );
return ENOENT;
}
route = ( ip_route_ref ) malloc( sizeof( ip_route_t ));
if( ! route ){
fibril_rwlock_write_unlock( & ip_globals.netifs_lock );
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 );
fibril_rwlock_write_unlock( & ip_globals.netifs_lock );
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;
fibril_rwlock_write_lock( & ip_globals.netifs_lock );
netif = ip_netifs_find( & ip_globals.netifs, device_id );
if( ! netif ){
fibril_rwlock_write_unlock( & ip_globals.netifs_lock );
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;
fibril_rwlock_write_unlock( & ip_globals.netifs_lock );
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, ((( size_t ) address_length > addr_len ) ? ( size_t ) address_length : addr_len ));
if( ! new_packet ) return ENOMEM;
// allocate as much as originally
last_header = ( ip_header_ref ) packet_suffix( new_packet, IP_HEADER_LENGTH( header ));
if( ! last_header ){
pq_release( ip_globals.net_phone, packet_get_id( new_packet ));
return ENOMEM;
}
ip_create_last_header( last_header, header );
// trim the unused space
if( ERROR_OCCURRED( packet_trim( new_packet, 0, IP_HEADER_LENGTH( header ) - IP_HEADER_LENGTH( last_header )))){
pq_release( ip_globals.net_phone, packet_get_id( new_packet ));
return ERROR_CODE;
}
// 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;
}
void ip_create_last_header( ip_header_ref last, ip_header_ref first ){
ip_option_ref option;
size_t next;
size_t length;
// 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;
}
last->header_checksum = 0;
}
int ip_receive_message( 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{
// only if routing enabled
if( route->netif->routing ){
return ip_send_route( packet, route->netif, route, NULL, dest );
}
else
{
// TODO icmp unreachable?
return ENOENT;
}
}
}
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{
ERROR_PROPAGATE( packet_set_addr( packet, ( uint8_t * ) & header->source_address, ( uint8_t * ) & header->destination_address, IP_ADDR ));
fibril_rwlock_read_lock( & ip_globals.protos_lock );
proto = ip_protos_find( & ip_globals.protos, header->protocol );
if( ! proto ){
fibril_rwlock_read_unlock( & ip_globals.protos_lock );
return ENOENT;
}
if( proto->received_msg ){
ERROR_CODE = proto->received_msg( device_id, packet, SERVICE_IP );
}else{
ERROR_CODE = tl_received_msg( proto->phone, device_id, packet, proto->service );
}
fibril_rwlock_read_unlock( & ip_globals.protos_lock );
return ERROR_CODE;
}
}
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;
}
/** @}
*/