/*
* Acess2 IP Stack
- * - Address Resolution Protocol
+ * - Stack Initialisation
*/
#define DEBUG 0
-#define VERSION ((0<<8)|10)
+#define VERSION VER2(0,10)
#include "ipstack.h"
+#include "link.h"
#include <modules.h>
#include <fs_devfs.h>
-#include <tpl_drv_common.h>
-#include <tpl_drv_network.h>
// === IMPORTS ===
- int ARP_Initialise();
+extern int ARP_Initialise();
+extern void UDP_Initialise();
+extern void TCP_Initialise();
+extern int IPv4_Initialise();
+extern int IPv6_Initialise();
+
+extern tAdapter *IPStack_GetAdapter(const char *Path);
+extern char *IPStack_Root_ReadDir(tVFS_Node *Node, int Pos);
+extern tVFS_Node *IPStack_Root_FindDir(tVFS_Node *Node, const char *Name);
+extern int IPStack_Root_IOCtl(tVFS_Node *Node, int ID, void *Data);
+extern tInterface gIP_LoopInterface;
+extern tInterface *IPStack_AddInterface(const char *Device, const char *Name);
+extern tRoute *IPStack_AddRoute(const char *Interface, void *Network, int SubnetBits, void *NextHop, int Metric);
// === PROTOTYPES ===
int IPStack_Install(char **Arguments);
-char *IPStack_ReadDir(tVFS_Node *Node, int Pos);
-tVFS_Node *IPStack_FindDir(tVFS_Node *Node, char *Name);
- int IPStack_IOCtl(tVFS_Node *Node, int ID, void *Data);
- int IPStack_AddInterface(char *Device);
-tAdapter *IPStack_GetAdapter(char *Path);
+ int IPStack_CompareAddress(int AddressType, const void *Address1, const void *Address2, int CheckBits);
// === GLOBALS ===
MODULE_DEFINE(0, VERSION, IPStack, IPStack_Install, NULL, NULL);
+tVFS_NodeType gIP_RootNodeType = {
+ .ReadDir = IPStack_Root_ReadDir,
+ .FindDir = IPStack_Root_FindDir,
+ .IOCtl = IPStack_Root_IOCtl
+};
tDevFS_Driver gIP_DriverInfo = {
NULL, "ip",
{
- .Size = 0, // Number of interfaces
+ .Size = -1, // Number of interfaces
.NumACLs = 1,
.ACLs = &gVFS_ACL_EveryoneRX,
.Flags = VFS_FFLAG_DIRECTORY,
- .ReadDir = IPStack_ReadDir,
- .FindDir = IPStack_FindDir
+ .Type = &gIP_RootNodeType
}
};
- int glIP_Interfaces = 0;
-tInterface *gIP_Interfaces = NULL;
-tInterface *gIP_Interfaces_Last = NULL;
- int glIP_Adapters = 0;
-tAdapter *gIP_Adapters = NULL;
// === CODE ===
/**
{
int i = 0;
- // Install Handlers
+ // Layer 3 - Network Layer Protocols
ARP_Initialise();
+ IPv4_Initialise();
+ IPv6_Initialise();
+ // Layer 4 - Transport Layer Protocols
+ TCP_Initialise();
+ UDP_Initialise();
if(Arguments)
{
// Parse module arguments
for( i = 0; Arguments[i]; i++ )
{
- //if(strcmp(Arguments[i], "Device") == '=') {
- //
- //}
- }
- }
-
- return 1;
-}
-
-/**
- * \brief Read from the IP Stack's Device Directory
- */
-char *IPStack_ReadDir(tVFS_Node *Node, int Pos)
-{
- tInterface *iface;
- char name[5] = "ip0\0\0";
-
- // Create the name
- if(Pos < 10)
- name[2] = '0' + Pos;
- else {
- name[2] = '0' + Pos/10;
- name[3] = '0' + Pos%10;
- }
-
- // Traverse the list
- for( iface = gIP_Interfaces; iface && Pos--; iface = iface->Next ) ;
-
- // Did we run off the end?
- if(!iface) return NULL;
-
- // Return the pre-generated name
- return strdup(name);
-}
+ // TODO:
+ // Define interfaces by <Device>:<Type>:<HexStreamAddress>:<Bits>
+ // Where:
+ // - <Device> is the device path (E.g. /Devices/ne2k/0)
+ // - <Type> is a number (e.g. 4) or symbol (e.g. AF_INET4)
+ // - <HexStreamAddress> is a condensed hexadecimal stream (in big endian)
+ // (E.g. 0A000201 for 10.0.2.1 IPv4)
+ // - <Bits> is the number of subnet bits (E.g. 24 for an IPv4 Class C)
+ // Example: /Devices/ne2k/0:4:0A00020A:24
+ // would define an interface with the address 10.0.2.10/24
+ if( Arguments[i][0] == '/' ) {
+ // Define Interface
+ char *dev, *type, *addr, *bits;
+
+ // Read definition
+ dev = Arguments[i];
+ type = strchr(dev, ':');
+ if( !type ) {
+ Log_Warning("IPStack", "<Device>:<Type>:<HexStreamAddress>:<Bits>");
+ continue;
+ }
+ *type = '\0'; type ++;
+
+ addr = strchr(type, ':');
+ if( !addr ) {
+ Log_Warning("IPStack", "<Device>:<Type>:<HexStreamAddress>:<Bits>");
+ continue;
+ }
+ *addr = '\0'; addr ++;
+
+ bits = strchr(addr, ':');
+ if( !bits ) {
+ Log_Warning("IPStack", "<Device>:<Type>:<HexStreamAddress>:<Bits>");
+ continue;
+ }
+ *bits = '\0'; bits ++;
+
+ // Define interface
+ {
+ int iType = atoi(type);
+ int size = IPStack_GetAddressSize(iType);
+ Uint8 addrData[size];
+ int iBits = atoi(bits);
+
+ UnHex(addrData, size, addr);
+
+ tInterface *iface = IPStack_AddInterface(dev, "");
+ if( !iface ) {
+ Log_Warning("IPStack", "Unable to add interface on '%s'", dev);
+ continue ;
+ }
+ iface->Type = iType;
+ memcpy(iface->Address, addrData, size);
+ iface->SubnetBits = iBits;
+
+ // Route for addrData/iBits, no next hop, default metric
+ IPStack_AddRoute(iface->Name, iface->Address, iBits, NULL, 0);
-/**
- * \brief Get the node of an interface
- */
-tVFS_Node *IPStack_FindDir(tVFS_Node *Node, char *Name)
-{
- int i;
- tInterface *iface;
-
- if(Name[0] != 'i' || Name[1] != 'p') return NULL;
- if(Name[2] < '0' || Name[2] > '9') return NULL;
-
- if(Name[3] == '\0') {
- i = Name[2] - '0';
- for( iface = gIP_Interfaces; iface && i--; iface = iface->Next ) ;
- if(!iface) return NULL;
- return &iface->Node;
+ Log_Notice("IPStack", "Boot interface %s/%i on %s",
+ IPStack_PrintAddress(iType, addrData), iBits,
+ dev);
+ }
+
+ continue;
+ }
+
+ // I could also define routes using <Interface>:<HexStreamNetwork>:<Bits>[:<HexStreamGateway>]
+ // Example: 1:00000000:0:0A000201
+ if( '0' <= Arguments[i][0] && Arguments[i][0] <= '9' )
+ {
+ // Define Interface
+ char *ifaceName, *network, *bits, *gateway;
+
+ // Read definition
+ ifaceName = Arguments[i];
+
+ network = strchr(ifaceName, ':');
+ if( !network ) {
+ Log_Warning("IPStack", "<iface>:<HexStreamNetwork>:<Bits>:<HexStreamGateway>");
+ continue;
+ }
+ *network = '\0'; network ++;
+
+ bits = strchr(network, ':');
+ if( !bits ) {
+ Log_Warning("IPStack", "<Device>:<Type>:<HexStreamAddress>:<Bits>");
+ continue;
+ }
+ *bits = '\0'; bits ++;
+
+ gateway = strchr(bits, ':');
+ if( gateway ) {
+ *gateway = '\0'; gateway ++;
+ }
+
+ // Define route
+ {
+ tVFS_Node *node = IPStack_Root_FindDir(NULL, ifaceName);
+ if( !node ) {
+ Log_Warning("IPStack", "Unknown interface '%s' in arg %i", ifaceName, i);
+ continue ;
+ }
+ tInterface *iface = node->ImplPtr;
+
+ int size = IPStack_GetAddressSize(iface->Type);
+ Uint8 netData[size];
+ Uint8 gwData[size];
+ int iBits = atoi(bits);
+
+ UnHex(netData, size, network);
+ if( gateway )
+ UnHex(gwData, size, gateway);
+ else
+ memset(gwData, 0, size);
+
+ IPStack_AddRoute(ifaceName, netData, iBits, gwData, 30);
+ }
+
+ continue;
+ }
+ }
}
- if(Name[3] < '0' || Name[3] > '9') return NULL;
+ // Initialise loopback interface
+ gIP_LoopInterface.Adapter = IPStack_GetAdapter("LOOPBACK");
- i = (Name[2] - '0')*10;
- i += Name[3] - '0';
+ DevFS_AddDevice( &gIP_DriverInfo );
- for( iface = gIP_Interfaces; iface && i--; iface = iface->Next ) ;
- if(!iface) return NULL;
- return &iface->Node;
+ return MODULE_ERR_OK;
}
-static const char *casIOCtls_Root[] = { DRV_IOCTLNAMES, "add_interface", NULL };
-static const char *casIOCtls_Iface[] = {
- DRV_IOCTLNAMES,
- "getset_type",
- "get_address", "set_address",
- "getset_subnet",
- "get_gateway", "set_gateway",
- NULL
- };
/**
- * \brief Handles IOCtls for the IPStack root/interfaces
+ * \brief Gets the size (in bytes) of a specified form of address
*/
-int IPStack_IOCtl(tVFS_Node *Node, int ID, void *Data)
+int IPStack_GetAddressSize(int AddressType)
{
- switch(ID)
+ switch(AddressType)
{
- // --- Standard IOCtls (0-3) ---
- case DRV_IOCTL_TYPE:
- return DRV_TYPE_MISC;
-
- case DRV_IOCTL_IDENT:
- if( !CheckMem( Data, 4 ) ) return -1;
- memcpy(Data, "IP\0\0", 4);
- return 1;
+ case -1: // -1 = maximum
+ return sizeof(tIPv6);
- case DRV_IOCTL_VERSION:
- return VERSION;
+ case AF_NULL:
+ return 0;
- case DRV_IOCTL_LOOKUP:
- if( !CheckString( Data ) ) return -1;
+ case AF_INET4:
+ return sizeof(tIPv4);
+ case AF_INET6:
+ return sizeof(tIPv6);
- if( Node == &gIP_DriverInfo.RootNode )
- return LookupString( (char**)casIOCtls_Root, (char*)Data );
- else
- return LookupString( (char**)casIOCtls_Iface, (char*)Data );
- }
-
- if(Node == &gIP_DriverInfo.RootNode)
- {
- switch(ID)
- {
- /*
- * add_interface
- * - Adds a new IP interface and binds it to a device
- */
- case 4:
- if( Threads_GetUID() != 0 ) return -1;
- if( !CheckString( Data ) ) return -1;
- return IPStack_AddInterface(Data);
- }
- return 0;
+ default:
+ return 0;
}
- else
- {
- tInterface *iface = (tInterface*)Node->ImplPtr;
- switch(ID)
- {
- /*
- * getset_type
- * - Get/Set the interface type
- */
- case 4:
- // Get Type?
- if( !Data ) return iface->Type;
- // Ok, it's set type
- if( Threads_GetUID() != 0 ) return -1;
- if( !CheckMem( Data, sizeof(int) ) ) return -1;
- switch( *(int*)Data )
- {
- case 0: // Disable
- iface->Type = 0;
- memset(&iface->IP6, 0, sizeof(tIPv6)); // Clear address
- break;
- case 4: // IPv4
- iface->Type = 4;
- memset(&iface->IP4, 0, sizeof(tIPv4));
- break;
- case 6: // IPv6
- iface->Type = 6;
- memset(&iface->IP6, 0, sizeof(tIPv6));
- break;
- default:
- return -1;
- }
- return 0;
-
- /*
- * get_address
- * - Get the interface's address
- */
- case 5:
- switch(iface->Type)
- {
- case 0: return 1;
- case 4:
- if( !CheckMem( Data, sizeof(tIPv4) ) ) return -1;
- memcpy( Data, &iface->IP4, sizeof(tIPv4) );
- return 1;
- case 6:
- if( !CheckMem( Data, sizeof(tIPv6) ) ) return -1;
- memcpy( Data, &iface->IP6, sizeof(tIPv6) );
- return 1;
- }
- return 0;
-
- /*
- * set_address
- * - Get the interface's address
- */
- case 6:
- if( Threads_GetUID() != 0 ) return -1;
- switch(iface->Type)
- {
- case 0: return 1;
- case 4:
- if( !CheckMem( Data, sizeof(tIPv4) ) ) return -1;
- iface->Type = 0; // One very hacky mutex/trash protector
- memcpy( &iface->IP4, Data, sizeof(tIPv4) );
- iface->Type = 4;
- return 1;
- case 6:
- if( !CheckMem( Data, sizeof(tIPv6) ) ) return -1;
- iface->Type = 0;
- memcpy( &iface->IP6, Data, sizeof(tIPv6) );
- iface->Type = 6;
- return 1;
- }
- return 0;
-
- /*
- * getset_subnet
- * - Get/Set the bits in the address subnet
- */
- case 7:
- // Get?
- if( Data == NULL )
- {
- switch( iface->Type )
- {
- case 4: return iface->IP4.SubnetBits;
- case 6: return iface->IP6.SubnetBits;
- default: return 0;
- }
- }
-
- // Ok, set.
- if( Threads_GetUID() != 0 ) return -1;
- if( !CheckMem(Data, sizeof(int)) ) return -1;
-
- // Check and set the subnet bits
- switch( iface->Type )
- {
- case 4:
- if( *(int*)Data < 0 || *(int*)Data > 31 ) return -1;
- iface->IP4.SubnetBits = *(int*)Data;
- return iface->IP4.SubnetBits;
- case 6:
- if( *(int*)Data < 0 || *(int*)Data > 127 ) return -1;
- iface->IP6.SubnetBits = *(int*)Data;
- return iface->IP6.SubnetBits;
- default:
- break;
- }
-
- return 0;
- }
- }
-
- return 0;
}
-// --- Internal ---
/**
- * \fn int IPStack_AddInterface(char *Device)
- * \brief Adds an interface to the list
+ * \brief Compare two IP Addresses masked by CheckBits
*/
-int IPStack_AddInterface(char *Device)
+int IPStack_CompareAddress(int AddressType, const void *Address1, const void *Address2, int CheckBits)
{
- tInterface *iface;
+ int size = IPStack_GetAddressSize(AddressType);
+ Uint8 mask;
+ const Uint8 *addr1 = Address1, *addr2 = Address2;
- iface = malloc(sizeof(tInterface));
- if(!iface) return -2; // Return ERR_MYBAD
+ // Sanity check size
+ if( CheckBits < 0 ) CheckBits = size*8;
+ if( CheckBits > size*8 ) CheckBits = size*8;
- iface->Next = NULL;
- iface->Type = 0; // Unset type
+ if( CheckBits == 0 ) return 1; // /0 matches anything
- // Create Node
- iface->Node.Flags = VFS_FFLAG_DIRECTORY;
- iface->Node.Size = 0;
- iface->Node.NumACLs = 1;
- iface->Node.ACLs = &gVFS_ACL_EveryoneRX;
- iface->Node.ReadDir = NULL;
- iface->Node.FindDir = NULL;
+ // Check first bits/8 bytes
+ if( memcmp(Address1, Address2, CheckBits/8) != 0 ) return 0;
- // Get adapter handle
- iface->Adapter = IPStack_GetAdapter(Device);
- if( !iface->Adapter ) {
- free( iface );
- return -1; // Return ERR_YOUFAIL
- }
+ // Check if the mask is a multiple of 8
+ if( CheckBits % 8 == 0 ) return 1;
- // Append to list
- LOCK( &glIP_Interfaces );
- if( gIP_Interfaces ) {
- gIP_Interfaces_Last->Next = iface;
- gIP_Interfaces_Last = iface;
- }
- else {
- gIP_Interfaces = iface;
- gIP_Interfaces_Last = iface;
- }
- RELEASE( &glIP_Interfaces );
+ // Check last bits
+ mask = 0xFF << (8 - (CheckBits % 8));
+ if( (addr1[CheckBits/8] & mask) == (addr2[CheckBits/8] & mask) )
+ return 1;
- // Success!
- return 1;
+ return 0;
}
-/**
- * \fn tAdapter *IPStack_GetAdapter(char *Path)
- * \brief Gets/opens an adapter given the path
- */
-tAdapter *IPStack_GetAdapter(char *Path)
+const char *IPStack_PrintAddress(int AddressType, const void *Address)
{
- tAdapter *dev;
-
- LOCK( &glIP_Adapters );
-
- // Check if this adapter is already open
- for( dev = gIP_Adapters; dev; dev = dev->Next )
+ switch( AddressType )
{
- if( strcmp(dev->Device, Path) == 0 ) {
- dev->NRef ++;
- RELEASE( &glIP_Adapters );
- return dev;
+ case 4: {
+ static char ret[4*3+3+1];
+ const Uint8 *addr = Address;
+ sprintf(ret, "%i.%i.%i.%i", addr[0], addr[1], addr[2], addr[3]);
+ return ret;
}
- }
-
- // Ok, so let's open it
- dev = malloc( sizeof(tAdapter) + strlen(Path) + 1 );
- if(!dev) {
- RELEASE( &glIP_Adapters );
- return NULL;
- }
-
- // Fill Structure
- strcpy( dev->Device, Path );
- dev->NRef = 1;
- // Open Device
- dev->DeviceFD = VFS_Open( dev->Device, VFS_OPENFLAG_READ|VFS_OPENFLAG_WRITE );
- if( dev->DeviceFD == -1 ) {
- free( dev );
- RELEASE( &glIP_Adapters );
- return NULL;
- }
+ case 6: { // TODO: address compression
+ static char ret[8*4+7+1];
+ const Uint16 *addr = Address;
+ sprintf(ret, "%x:%x:%x:%x:%x:%x:%x:%x",
+ ntohs(addr[0]), ntohs(addr[1]), ntohs(addr[2]), ntohs(addr[3]),
+ ntohs(addr[4]), ntohs(addr[5]), ntohs(addr[6]), ntohs(addr[7])
+ );
+ return ret;
+ }
- // Check that it is a network interface
- if( VFS_IOCtl(dev->DeviceFD, 0, NULL) != DRV_TYPE_NETWORK ) {
- Warning("IPStack_GetAdapter: '%s' is not a network interface", dev->Device);
- VFS_Close( dev->DeviceFD );
- free( dev );
- RELEASE( &glIP_Adapters );
- return NULL;
+ default:
+ return "";
}
-
- // Get MAC Address
- VFS_IOCtl(dev->DeviceFD, NET_IOCTL_GETMAC, &dev->MacAddr);
-
- // Add to list
- dev->Next = gIP_Adapters;
- gIP_Adapters = dev;
-
- RELEASE( &glIP_Adapters );
- return dev;
}