From 107a87368c3786c1be3c2bfaa0ba6ede956971c5 Mon Sep 17 00:00:00 2001 From: John Hodge Date: Mon, 28 Jan 2013 14:20:51 +0800 Subject: [PATCH] IPStack - Breaking things (constant iftype) --- KernelLand/Kernel/include/acess.h | 2 +- KernelLand/Modules/IPStack/interface.c | 98 +++++++++++++++------ KernelLand/Modules/IPStack/interface.h | 10 ++- Usermode/Applications/dhcpclient_src/main.c | 13 ++- Usermode/Applications/ip_src/addr.c | 60 +++++++------ 5 files changed, 121 insertions(+), 62 deletions(-) diff --git a/KernelLand/Kernel/include/acess.h b/KernelLand/Kernel/include/acess.h index b846a51e..bcd5f5ed 100644 --- a/KernelLand/Kernel/include/acess.h +++ b/KernelLand/Kernel/include/acess.h @@ -219,7 +219,7 @@ extern Uint MM_GetFlags(tVAddr VAddr); * \param VAddr Base address to check * \return 1 if the memory is all user-accessable, 0 otherwise */ -#define MM_IsUser(VAddr) (!(MM_GetFlags((VAddr))&MM_PFLAG_KERNEL)) +#define MM_IsUser(VAddr) (!(MM_GetFlags((tVAddr)(VAddr))&MM_PFLAG_KERNEL)) /** * \brief Temporarily map a page into the address space * \param PAddr Physical addres to map diff --git a/KernelLand/Modules/IPStack/interface.c b/KernelLand/Modules/IPStack/interface.c index 9ab4b9ae..6f357134 100644 --- a/KernelLand/Modules/IPStack/interface.c +++ b/KernelLand/Modules/IPStack/interface.c @@ -26,7 +26,7 @@ tVFS_Node *IPStack_Root_FindDir(tVFS_Node *Node, const char *Name); int IPStack_Root_IOCtl(tVFS_Node *Node, int ID, void *Data); int IPStack_AddFile(tSocketFile *File); -tInterface *IPStack_AddInterface(const char *Device, const char *Name); +tInterface *IPStack_AddInterface(const char *Device, int Type, const char *Name); int IPStack_Iface_ReadDir(tVFS_Node *Node, int Pos, char Dest[FILENAME_MAX]); tVFS_Node *IPStack_Iface_FindDir(tVFS_Node *Node, const char *Name); @@ -101,8 +101,7 @@ int IPStack_Root_ReadDir(tVFS_Node *Node, int Pos, char Dest[FILENAME_MAX]) } // Create the name - Pos = iface->Node.ImplInt; - snprintf(Dest, FILENAME_MAX, "%i", Pos); + strncpy(Dest, iface->Name, FILENAME_MAX); LEAVE('i', 0); return 0; @@ -167,19 +166,32 @@ int IPStack_Root_IOCtl(tVFS_Node *Node, int ID, void *Data) /* * add_interface - * - Adds a new IP interface and binds it to a device + * - Adds a new 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] = ""; - tInterface *iface = IPStack_AddInterface(Data, name); - if(iface == NULL) LEAVE_RET('i', -1); - tmp = iface->Node.ImplInt; - } + case 4: { + const struct { + const char *Device; + const char *Name; + int Type; + } *ifinfo = Data; + + if( Threads_GetUID() != 0 ) + LEAVE_RET('i', -1); + if( !CheckMem(ifinfo, sizeof(*ifinfo)) ) + LEAVE_RET('i', -1); + if( !MM_IsUser(ifinfo->Device) || !CheckString( ifinfo->Device ) ) + LEAVE_RET('i', -1); + if( !MM_IsUser(ifinfo->Name) || !CheckString( ifinfo->Name ) ) + LEAVE_RET('i', -1); + + LOG("New interface of type %i for '%s' named '%s'", + ifinfo->Type, ifinfo->Device, ifinfo->Name); + tInterface *iface = IPStack_AddInterface(ifinfo->Device, ifinfo->Type, ifinfo->Name); + if(iface == NULL) LEAVE_RET('i', -1); + + tmp = iface->Node.ImplInt; LEAVE_RET('i', tmp); + } } LEAVE('i', 0); return 0; @@ -189,40 +201,60 @@ int IPStack_Root_IOCtl(tVFS_Node *Node, int ID, void *Data) * \fn tInterface *IPStack_AddInterface(char *Device) * \brief Adds an interface to the list */ -tInterface *IPStack_AddInterface(const char *Device, const char *Name) +tInterface *IPStack_AddInterface(const char *Device, int Type, const char *Name) { tInterface *iface; tAdapter *card; - int nameLen; + int nameLen, addrsize; ENTER("sDevice", Device); + // Check address type + addrsize = IPStack_GetAddressSize(Type); + if( addrsize == -1 ) { + Log_Debug("IPStack", "Bad address type %i", Type); + return NULL; + } + + // Open card card = Adapter_GetByName(Device); if( !card ) { Log_Debug("IPStack", "Unable to open card '%s'", Device); LEAVE('n'); return NULL; // ERR_YOURBAD } - - nameLen = sprintf(NULL, "%i", giIP_NextIfaceId); - + + // Get name length + if( Name[0] ) + { + nameLen = strlen(Name); + } + else + { + nameLen = sprintf(NULL, "%i", giIP_NextIfaceId); + } + iface = malloc( sizeof(tInterface) + nameLen + 1 - + IPStack_GetAddressSize(-1)*3 // Address, Route->Network, Route->NextHop + + addrsize*3 // Address, Route->Network, Route->NextHop ); if(!iface) { Log_Warning("IPStack", "AddInterface - malloc() failed"); + // TODO: Close card LEAVE('n'); return NULL; // Return ERR_MYBAD } iface->Next = NULL; - iface->Type = 0; // Unset type + iface->Type = Type; // Unset type iface->Address = iface->Name + nameLen + 1; // Address + memset(iface->Address, 0, addrsize); memset(&iface->Route, 0, sizeof(iface->Route)); - iface->Route.Network = iface->Address + IPStack_GetAddressSize(-1); - iface->Route.NextHop = iface->Route.Network + IPStack_GetAddressSize(-1); + iface->Route.Network = iface->Address + addrsize; + memset(iface->Route.Network, 0, addrsize); + iface->Route.NextHop = iface->Route.Network + addrsize; + memset(iface->Route.NextHop, 0, addrsize); // Create Node iface->Node.ImplPtr = iface; @@ -238,10 +270,17 @@ tInterface *IPStack_AddInterface(const char *Device, const char *Name) // Get adapter handle iface->Adapter = card; - // Delay setting ImplInt until after the adapter is opened - // Keeps things simple - iface->Node.ImplInt = giIP_NextIfaceId++; - sprintf(iface->Name, "%i", (int)iface->Node.ImplInt); + // Set name + if( Name[0] ) + { + iface->Node.ImplInt = 0; + strcpy(iface->Name, Name); + } + else + { + iface->Node.ImplInt = giIP_NextIfaceId++; + sprintf(iface->Name, "%i", (int)iface->Node.ImplInt); + } // Append to list SHORTLOCK( &glIP_Interfaces ); @@ -255,8 +294,6 @@ tInterface *IPStack_AddInterface(const char *Device, const char *Name) } SHORTREL( &glIP_Interfaces ); -// gIP_DriverInfo.RootNode.Size ++; - // Success! LEAVE('p', iface); return iface; @@ -345,6 +382,7 @@ int IPStack_Iface_IOCtl(tVFS_Node *Node, int ID, void *Data) // Set Type? if( Data ) { +#if 0 // Ok, it's set type if( Threads_GetUID() != 0 ) { LOG("Attempt by non-root to alter an interface (%i)", Threads_GetUID()); @@ -370,6 +408,8 @@ int IPStack_Iface_IOCtl(tVFS_Node *Node, int ID, void *Data) // Clear address memset(iface->Address, 0, size); +#endif + Log_Notice("IPStack", "Interface ioctl(settype) no longer usable"); } LEAVE('i', iface->Type); return iface->Type; diff --git a/KernelLand/Modules/IPStack/interface.h b/KernelLand/Modules/IPStack/interface.h index 60459a0a..b6f7e584 100644 --- a/KernelLand/Modules/IPStack/interface.h +++ b/KernelLand/Modules/IPStack/interface.h @@ -1,10 +1,16 @@ - +/* + * Acess2 IP Stack + * - By John Hodge + * + * interface.h + * - Interface manipulation/access definitions + */ #ifndef _IPSTACK__INTERFACE_H_ #define _IPSTACK__INTERFACE_H_ extern tInterface gIP_LoopInterface; extern tVFS_NodeType gIP_RootNodeType; -extern tInterface *IPStack_AddInterface(const char *Device, const char *Name); +extern tInterface *IPStack_AddInterface(const char *Device, int Type, const char *Name); #endif diff --git a/Usermode/Applications/dhcpclient_src/main.c b/Usermode/Applications/dhcpclient_src/main.c index 73e85d8e..5b1d3802 100644 --- a/Usermode/Applications/dhcpclient_src/main.c +++ b/Usermode/Applications/dhcpclient_src/main.c @@ -221,7 +221,15 @@ int Start_Interface(tInterface *Iface) fprintf(stderr, "ERROR: Unable to open '/Devices/ip'\n"); return -1; } - Iface->Num = _SysIOCtl(fd, 4, (void*)Iface->Adapter); // Create interface + struct { + const char *Device; + const char *Name; + int Type; + } ifinfo; + ifinfo.Device = Iface->Adapter; + ifinfo.Name = ""; + ifinfo.Type = 4; + Iface->Num = _SysIOCtl(fd, 4, &ifinfo); // Create interface if( Iface->Num == -1 ) { fprintf(stderr, "ERROR: Unable to create new interface\n"); return -1; @@ -235,7 +243,6 @@ int Start_Interface(tInterface *Iface) fprintf(stderr, "ERROR: Unable to open '%s'\n", path); return -1; } - tmp = 4; _SysIOCtl(fd, 4, &tmp); // Set to IPv4 _SysIOCtl(fd, 6, addr); // Set address to 0.0.0.0 tmp = 0; _SysIOCtl(fd, 7, &tmp); // Set subnet mask to 0 @@ -248,7 +255,7 @@ int Start_Interface(tInterface *Iface) } tmp = 68; _SysIOCtl(fd, 4, &tmp); // Local port tmp = 67; _SysIOCtl(fd, 5, &tmp); // Remote port - tmp = 0; _SysIOCtl(fd, 7, &tmp); // Remote addr mask bits - we don't care where the reply comes from + tmp = 0; _SysIOCtl(fd, 7, &tmp); // Remote addr mask bits - we don't care where the reply comes from addr[0] = addr[1] = addr[2] = addr[3] = 255; // 255.255.255.255 _SysIOCtl(fd, 8, addr); // Remote address diff --git a/Usermode/Applications/ip_src/addr.c b/Usermode/Applications/ip_src/addr.c index 2046bb09..2b048c5e 100644 --- a/Usermode/Applications/ip_src/addr.c +++ b/Usermode/Applications/ip_src/addr.c @@ -9,8 +9,8 @@ // === PROTOTYPES === int Addr_main(int argc, char *argv[]); - int AddInterface(const char *Device); - int SetAddress(int IFNum, const char *Address); + int AddInterface(const char *Device, int Type); + int SetAddress(int IFNum, const void *Address, int SubnetSize); void DumpInterfaces(void); void DumpInterface(const char *Name); void DumpRoute(const char *Name); @@ -26,14 +26,28 @@ int Addr_main(int argc, char *argv[]) } else if( strcmp(argv[1], "add") == 0 ) { + uint8_t addr[16]; + int type, subnet_size; + + // Check argument counts if( argc - 2 < 2 ) { - fprintf(stderr, "ERROR: '%s add' requires two arguments, %i passed\n", argv[0], argc-2); + fprintf(stderr, "ERROR: '%s add' requires two arguments, %i passed\n", + argv[0], argc-2); PrintUsage(argv[0]); return -1; } - ret = AddInterface( argv[2] ); + + // Parse IP Address + type = ParseIPAddress(argv[3], addr, &subnet_size); + if(type == 0) { + fprintf(stderr, "'%s' cannot be parsed as an IP address\n", argv[3]); + return -1; + } + + // Fun + ret = AddInterface( argv[2], type ); if(ret < 0) return ret; - ret = SetAddress( ret, argv[3] ); + ret = SetAddress( ret, addr, subnet_size ); return ret; } // Delete an interface @@ -57,12 +71,20 @@ int Addr_main(int argc, char *argv[]) * \brief Create a new interface using the passed device * \param Device Network device to bind to */ -int AddInterface(const char *Device) +int AddInterface(const char *Device, int Type) { int dp, ret; dp = _SysOpen(IPSTACK_ROOT, OPENFLAG_READ); - ret = _SysIOCtl(dp, 4, (void*)Device); + struct { + const char *Dev; + const char *Name; + int Type; + } ifinfo; + ifinfo.Dev = Device; + ifinfo.Name = ""; + ifinfo.Type = Type; + ret = _SysIOCtl(dp, 4, &ifinfo); _SysClose(dp); if( ret < 0 ) { @@ -78,19 +100,10 @@ int AddInterface(const char *Device) /** * \brief Set the address on an interface from a textual IP address */ -int SetAddress(int IFNum, const char *Address) +int SetAddress(int IFNum, const void *Address, int SubnetSize) { - uint8_t addr[16]; - int type; char path[sizeof(IPSTACK_ROOT)+1+5+1]; // ip000 - int tmp, fd, subnet; - - // Parse IP Address - type = ParseIPAddress(Address, addr, &subnet); - if(type == 0) { - fprintf(stderr, "'%s' cannot be parsed as an IP address\n", Address); - return -1; - } + int fd; // Open file sprintf(path, IPSTACK_ROOT"/%i", IFNum); @@ -100,18 +113,11 @@ int SetAddress(int IFNum, const char *Address) return -1; } - tmp = type; - tmp = _SysIOCtl(fd, _SysIOCtl(fd, 3, "getset_type"), &tmp); - if( tmp != type ) { - fprintf(stderr, "Error in setting address type (got %i, expected %i)\n", tmp, type); - _SysClose(fd); - return -1; - } // Set Address - _SysIOCtl(fd, _SysIOCtl(fd, 3, "set_address"), addr); + _SysIOCtl(fd, _SysIOCtl(fd, 3, "set_address"), (void*)Address); // Set Subnet - _SysIOCtl(fd, _SysIOCtl(fd, 3, "getset_subnet"), &subnet); + _SysIOCtl(fd, _SysIOCtl(fd, 3, "getset_subnet"), &SubnetSize); _SysClose(fd); -- 2.20.1