X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=Modules%2FIPStack%2Finterface.c;h=b0b63b9a9b1c7e8e443e1440a86d61102de5ebbb;hb=7c43aea724fea42bc297f47f9c180a5080c6fd17;hp=c6f55ba90977ddec0efc0e774e5acea5b01bec69;hpb=8051546ad5894e093211d2ec69dde6b99cdaa71d;p=tpg%2Facess2.git diff --git a/Modules/IPStack/interface.c b/Modules/IPStack/interface.c index c6f55ba9..b0b63b9a 100644 --- a/Modules/IPStack/interface.c +++ b/Modules/IPStack/interface.c @@ -6,8 +6,8 @@ #define VERSION VER2(0,10) #include "ipstack.h" #include "link.h" -#include -#include +#include +#include // === CONSTANTS === //! Default timeout value, 30 seconds @@ -23,8 +23,8 @@ char *IPStack_Root_ReadDir(tVFS_Node *Node, int Pos); tVFS_Node *IPStack_Root_FindDir(tVFS_Node *Node, const char *Name); int IPStack_Root_IOCtl(tVFS_Node *Node, int ID, void *Data); - int IPStack_AddInterface(const char *Device, const char *Name); int IPStack_AddFile(tSocketFile *File); +tInterface *IPStack_AddInterface(const char *Device, const char *Name); tAdapter *IPStack_GetAdapter(const char *Path); char *IPStack_Iface_ReadDir(tVFS_Node *Node, int Pos); @@ -32,20 +32,23 @@ tVFS_Node *IPStack_Iface_FindDir(tVFS_Node *Node, const char *Name); int IPStack_Iface_IOCtl(tVFS_Node *Node, int ID, void *Data); // === GLOBALS === +tVFS_NodeType gIP_InterfaceNodeType = { + .ReadDir = IPStack_Iface_ReadDir, + .FindDir = IPStack_Iface_FindDir, + .IOCtl = IPStack_Iface_IOCtl +}; //! Loopback (127.0.0.0/8, ::1) Pseudo-Interface tInterface gIP_LoopInterface = { - Node: { - ImplPtr: &gIP_LoopInterface, - Flags: VFS_FFLAG_DIRECTORY, - Size: -1, - NumACLs: 1, - ACLs: &gVFS_ACL_EveryoneRX, - ReadDir: IPStack_Iface_ReadDir, - FindDir: IPStack_Iface_FindDir, - IOCtl: IPStack_Iface_IOCtl + .Node = { + .ImplPtr = &gIP_LoopInterface, + .Flags = VFS_FFLAG_DIRECTORY, + .Size = -1, + .NumACLs = 1, + .ACLs = &gVFS_ACL_EveryoneRX, + .Type = &gIP_InterfaceNodeType }, - Adapter: NULL, - Type: 0 + .Adapter = NULL, + .Type = 0 }; tShortSpinlock glIP_Interfaces; tInterface *gIP_Interfaces = NULL; @@ -54,8 +57,8 @@ tInterface *gIP_Interfaces_Last = NULL; tSocketFile *gIP_FileTemplates; tAdapter gIP_LoopAdapter = { - DeviceLen: 8, - Device: "LOOPBACK" + .DeviceLen = 8, + .Device = "LOOPBACK" }; tMutex glIP_Adapters; tAdapter *gIP_Adapters = NULL; @@ -75,10 +78,12 @@ char *IPStack_Root_ReadDir(tVFS_Node *Node, int Pos) // Routing Subdir if( Pos == 0 ) { + LEAVE('s', "routes"); return strdup("routes"); } // Pseudo Interfaces if( Pos == 1 ) { + LEAVE('s', "lo"); return strdup("lo"); } Pos -= 2; @@ -136,36 +141,16 @@ tVFS_Node *IPStack_Root_FindDir(tVFS_Node *Node, const char *Name) // Routing subdir if( strcmp(Name, "routes") == 0 ) { + LEAVE('p', &gIP_RouteNode); return &gIP_RouteNode; } // Loopback if( strcmp(Name, "lo") == 0 ) { + LEAVE('p', &gIP_LoopInterface.Node); return &gIP_LoopInterface.Node; } - #if 0 - i = 0; num = 0; - while('0' <= Name[i] && Name[i] <= '9') - { - num *= 10; - num += Name[i] - '0'; - i ++; - } - if(Name[i] != '\0') { - LEAVE('n'); - return NULL; - } - - for( iface = gIP_Interfaces; iface; iface = iface->Next ) - { - if( (int)iface->Node.ImplInt == num ) - { - LEAVE('p', &iface->Node); - return &iface->Node; - } - } - #else for( iface = gIP_Interfaces; iface; iface = iface->Next ) { if( strcmp(iface->Name, Name) == 0 ) @@ -174,7 +159,6 @@ tVFS_Node *IPStack_Root_FindDir(tVFS_Node *Node, const char *Name) return &iface->Node; } } - #endif LEAVE('p', NULL); return NULL; @@ -192,34 +176,21 @@ int IPStack_Root_IOCtl(tVFS_Node *Node, int ID, void *Data) switch(ID) { // --- Standard IOCtls (0-3) --- - case DRV_IOCTL_TYPE: - LEAVE('i', DRV_TYPE_MISC); - return DRV_TYPE_MISC; - - case DRV_IOCTL_IDENT: - tmp = ModUtil_SetIdent(Data, "IPStack"); - LEAVE('i', 1); - return 1; - - case DRV_IOCTL_VERSION: - LEAVE('x', VERSION); - return VERSION; - - case DRV_IOCTL_LOOKUP: - tmp = ModUtil_LookupString( (char**)casIOCtls_Root, (char*)Data ); - LEAVE('i', tmp); - return tmp; + BASE_IOCTLS(DRV_TYPE_MISC, "IPStack", VERSION, casIOCtls_Root) - /* - * add_interface - * - Adds a new IP interface and binds it to a device - */ + /* + * add_interface + * - Adds a new IP interface and binds it to a device + */ case 4: if( Threads_GetUID() != 0 ) LEAVE_RET('i', -1); if( !CheckString( Data ) ) LEAVE_RET('i', -1); + LOG("New interface for '%s'", Data); { char name[4] = ""; - tmp = IPStack_AddInterface(Data, name); + tInterface *iface = IPStack_AddInterface(Data, name); + if(iface == NULL) LEAVE_RET('i', -1); + tmp = iface->Node.ImplInt; } LEAVE_RET('i', tmp); } @@ -228,10 +199,10 @@ int IPStack_Root_IOCtl(tVFS_Node *Node, int ID, void *Data) } /** - * \fn int IPStack_AddInterface(char *Device) + * \fn tInterface *IPStack_AddInterface(char *Device) * \brief Adds an interface to the list */ -int IPStack_AddInterface(const char *Device, const char *Name) +tInterface *IPStack_AddInterface(const char *Device, const char *Name) { tInterface *iface; tAdapter *card; @@ -241,8 +212,9 @@ int IPStack_AddInterface(const char *Device, const char *Name) card = IPStack_GetAdapter(Device); if( !card ) { - LEAVE('i', -1); - return -1; // ERR_YOURBAD + Log_Debug("IPStack", "Unable to open card '%s'", Device); + LEAVE('n'); + return NULL; // ERR_YOURBAD } nameLen = sprintf(NULL, "%i", giIP_NextIfaceId); @@ -250,16 +222,19 @@ int IPStack_AddInterface(const char *Device, const char *Name) iface = malloc( sizeof(tInterface) + nameLen + 1 - + IPStack_GetAddressSize(-1) + + IPStack_GetAddressSize(-1)*3 // Address, Route->Network, Route->NextHop ); if(!iface) { - LEAVE('i', -2); - return -2; // Return ERR_MYBAD + Log_Warning("IPStack", "AddInterface - malloc() failed"); + LEAVE('n'); + return NULL; // Return ERR_MYBAD } iface->Next = NULL; iface->Type = 0; // Unset type iface->Address = iface->Name + nameLen + 1; // Address + iface->Route.Network = iface->Address + IPStack_GetAddressSize(-1); + iface->Route.NextHop = iface->Route.Network + IPStack_GetAddressSize(-1); // Create Node iface->Node.ImplPtr = iface; @@ -267,29 +242,18 @@ int IPStack_AddInterface(const char *Device, const char *Name) iface->Node.Size = -1; iface->Node.NumACLs = 1; iface->Node.ACLs = &gVFS_ACL_EveryoneRX; - iface->Node.ReadDir = IPStack_Iface_ReadDir; - iface->Node.FindDir = IPStack_Iface_FindDir; - iface->Node.IOCtl = IPStack_Iface_IOCtl; - iface->Node.MkNod = NULL; - iface->Node.Link = NULL; - iface->Node.Relink = NULL; - iface->Node.Close = NULL; + iface->Node.Type = &gIP_InterfaceNodeType; // Set Defaults iface->TimeoutDelay = DEFAULT_TIMEOUT; // Get adapter handle - iface->Adapter = IPStack_GetAdapter(Device); - if( !iface->Adapter ) { - free( iface ); - LEAVE('i', -1); - return -1; // Return ERR_YOUFAIL - } + iface->Adapter = card; // Delay setting ImplInt until after the adapter is opened // Keeps things simple iface->Node.ImplInt = giIP_NextIfaceId++; - sprintf(iface->Name, "%i", iface->Node.ImplInt); + sprintf(iface->Name, "%i", (int)iface->Node.ImplInt); // Append to list SHORTLOCK( &glIP_Interfaces ); @@ -306,8 +270,8 @@ int IPStack_AddInterface(const char *Device, const char *Name) // gIP_DriverInfo.RootNode.Size ++; // Success! - LEAVE('i', iface->Node.ImplInt); - return iface->Node.ImplInt; + LEAVE('p', iface); + return iface; } /** @@ -381,23 +345,7 @@ int IPStack_Iface_IOCtl(tVFS_Node *Node, int ID, void *Data) switch(ID) { // --- Standard IOCtls (0-3) --- - case DRV_IOCTL_TYPE: - LEAVE('i', DRV_TYPE_MISC); - return DRV_TYPE_MISC; - - case DRV_IOCTL_IDENT: - tmp = ModUtil_SetIdent(Data, STR(IDENT)); - LEAVE('i', 1); - return 1; - - case DRV_IOCTL_VERSION: - LEAVE('x', VERSION); - return VERSION; - - case DRV_IOCTL_LOOKUP: - tmp = ModUtil_LookupString( (char**)casIOCtls_Iface, (char*)Data ); - LEAVE('i', tmp); - return tmp; + BASE_IOCTLS(DRV_TYPE_MISC, "IPStack", VERSION, casIOCtls_Iface) /* * getset_type @@ -421,6 +369,7 @@ int IPStack_Iface_IOCtl(tVFS_Node *Node, int ID, void *Data) // Set type iface->Type = *(int*)Data; + LOG("Interface type set to %i", iface->Type); size = IPStack_GetAddressSize(iface->Type); // Check it's actually valid if( iface->Type != 0 && size == 0 ) { @@ -456,9 +405,9 @@ int IPStack_Iface_IOCtl(tVFS_Node *Node, int ID, void *Data) size = IPStack_GetAddressSize(iface->Type); if( !CheckMem( Data, size ) ) LEAVE_RET('i', -1); // TODO: Protect against trashing + LOG("Interface address set to '%s'", IPStack_PrintAddress(iface->Type, Data)); memcpy( iface->Address, Data, size ); - LEAVE('i', 1); - return 1; + LEAVE_RET('i', 1); /* * getset_subnet @@ -476,12 +425,11 @@ int IPStack_Iface_IOCtl(tVFS_Node *Node, int ID, void *Data) // Is the mask sane? if( *(int*)Data < 0 || *(int*)Data > IPStack_GetAddressSize(iface->Type)*8-1 ) LEAVE_RET('i', -1); - + LOG("Set subnet bits to %i", *(int*)Data); // Ok, set it iface->SubnetBits = *(int*)Data; } - LEAVE('i', iface->SubnetBits); - return iface->SubnetBits; + LEAVE_RET('i', iface->SubnetBits); /* * get_device @@ -495,7 +443,7 @@ int IPStack_Iface_IOCtl(tVFS_Node *Node, int ID, void *Data) if( !CheckMem( Data, iface->Adapter->DeviceLen+1 ) ) LEAVE_RET('i', -1); strcpy( Data, iface->Adapter->Device ); - return iface->Adapter->DeviceLen; + LEAVE_RET('i', iface->Adapter->DeviceLen); /* * ping @@ -510,8 +458,7 @@ int IPStack_Iface_IOCtl(tVFS_Node *Node, int ID, void *Data) case 4: if( !CheckMem( Data, sizeof(tIPv4) ) ) LEAVE_RET('i', -1); tmp = IPv4_Ping(iface, *(tIPv4*)Data); - LEAVE('i', tmp); - return tmp; + LEAVE_RET('i', tmp); case 6: LEAVE_RET('i', 1); @@ -583,6 +530,7 @@ tAdapter *IPStack_GetAdapter(const char *Path) // Ok, so let's open it dev = malloc( sizeof(tAdapter) + strlen(Path) + 1 ); if(!dev) { + Log_Warning("IPStack", "GetAdapter - malloc() failed"); Mutex_Release( &glIP_Adapters ); LEAVE('n'); return NULL; @@ -606,7 +554,7 @@ tAdapter *IPStack_GetAdapter(const char *Path) tmp = VFS_IOCtl(dev->DeviceFD, 0, NULL); LOG("Device type = %i", tmp); if( tmp != DRV_TYPE_NETWORK ) { - Warning("IPStack_GetAdapter: '%s' is not a network interface", dev->Device); + Log_Warning("IPStack", "IPStack_GetAdapter: '%s' is not a network interface", dev->Device); VFS_Close( dev->DeviceFD ); free( dev ); Mutex_Release( &glIP_Adapters );