2 * Acess2 Networking Stack
3 * - By John Hodge (thePowersGang)
6 * - Network Adapter Management
9 #define VERSION VER2(0,1)
11 #include "include/buffer.h"
12 #include "include/adapters.h"
13 #include "include/adapters_int.h"
15 #include <api_drv_common.h> // For the VFS hack
16 #include <api_drv_network.h>
19 // --- "External" (NIC) API ---
20 void *IPStack_Adapter_Add(const tIPStack_AdapterType *Type, void *Ptr, const void *HWAddr);
21 void IPStack_Adapter_Del(void *Handle);
23 char *Adapter_ReadDir(tVFS_Node *Node, int Pos);
24 tVFS_Node *Adapter_FindDir(tVFS_Node *Node, const char *Name);
25 int Adapter_DirIOCtl(tVFS_Node *Node, int Num, void *Data);
26 int Adapter_IOCtl(tVFS_Node *Node, int Num, void *Data);
27 // --- "Internal" (IPStack) API ---
28 tAdapter *Adapter_GetByName(const char *Name);
29 void Adapter_SendPacket(tAdapter *Handle, tIPStackBuffer *Buffer);
31 void Adapter_int_WatchThread(void *Ptr);
32 tIPStackBuffer *Adapter_int_LoopbackWaitPacket(void *Unused);
33 int Adapter_int_LoopbackSendPacket(void *Unused, tIPStackBuffer *Buffer);
37 tVFS_NodeType gIP_AdaptersDirType = {
38 .ReadDir = Adapter_ReadDir,
39 .FindDir = Adapter_FindDir,
40 .IOCtl = Adapter_DirIOCtl
42 tVFS_NodeType gIP_AdapterType = {
43 // TODO: Should direct IO be allowed?
44 .IOCtl = Adapter_IOCtl
46 tVFS_Node gIP_AdaptersNode = {
47 .Type = &gIP_AdaptersDirType,
48 .Flags = VFS_FFLAG_DIRECTORY,
51 // --- Loopback Adapter ---
52 tIPStack_AdapterType gIP_LoopAdapterType = {
54 .WaitForPacket = Adapter_int_LoopbackWaitPacket,
55 .SendPacket = Adapter_int_LoopbackSendPacket
57 tAdapter gIP_LoopAdapter;
58 // --- Main adapter list ---
60 tAdapter *gpIP_AdapterList;
61 tAdapter *gpIP_AdapterList_Last = (void*)&gpIP_AdapterList; // HACK!
62 int giIP_NextAdapterIndex;
65 // --- "External" (NIC) API ---
66 void *IPStack_Adapter_Add(const tIPStack_AdapterType *Type, void *Ptr, const void *HWAddr)
70 ret = malloc(sizeof(tAdapter) + 6); // TODO: Don't assume 6-byte MAC addresses
73 ret->CardHandle = Ptr;
75 ret->Index = giIP_NextAdapterIndex++;
76 memcpy(ret->HWAddr, HWAddr, 6);
78 memset(&ret->Node, 0, sizeof(ret->Node));
79 ret->Node.Type = &gIP_AdapterType;
80 ret->Node.ImplPtr = ret;
83 gpIP_AdapterList_Last->Next = ret;
84 gpIP_AdapterList_Last = ret;
86 // Watch the adapter for incoming packets
87 tTID tid = Proc_SpawnWorker(Adapter_int_WatchThread, ret);
89 Log_Warning("IPStack", "Unable to create watcher thread for %p", ret);
95 void *IPStack_Adapter_AddVFS(const char *Path)
100 ENTER("sPath", Path);
103 fd = VFS_Open( Path, VFS_OPENFLAG_READ|VFS_OPENFLAG_WRITE );
109 // Check that it is a network interface
110 tmp = VFS_IOCtl(fd, 0, NULL);
111 LOG("Device type = %i", tmp);
112 if( tmp != DRV_TYPE_NETWORK ) {
113 Log_Warning("IPStack", "IPStack_Adapter_AddVFS: '%s' is not a network interface", Path);
120 VFS_IOCtl(fd, NET_IOCTL_GETMAC, mac);
122 return IPStack_Adapter_Add(NULL, (void*)fd, mac);
125 void IPStack_Adapter_Del(void *Handle)
127 Log_Error("IPStack", "TODO: Implement IPStack_Adapter_Del");
128 // TODO: Allow removing adapters during system operation
132 char *Adapter_ReadDir(tVFS_Node *Node, int Pos)
134 if( Pos < 0 ) return NULL;
142 #define CHECK_LIST(list, type) do{ \
144 for(i=0,a=list; i < Pos && a; i ++, a = a->Next ); \
146 return Adapter_GetName(a);\
151 CHECK_LIST(gpIP_AdapterList, "eth");
152 // TODO: Support other types of adapters (wifi, tap, ...)
157 tVFS_Node *Adapter_FindDir(tVFS_Node *Node, const char *Name)
159 tAdapter *a = Adapter_GetByName(Name);
166 static const char *casIOCtls_Root[] = { DRV_IOCTLNAMES, "add_vfs_adapter", NULL };
167 int Adapter_DirIOCtl(tVFS_Node *Node, int Num, void *Data)
171 BASE_IOCTLS(DRV_TYPE_MISC, "IPStack-AdapterList", VERSION, casIOCtls_Root)
174 if( !CheckString(Data) )
177 // Return non-zero if add fails
178 return IPStack_Adapter_AddVFS(Data) == NULL;
183 static const char *casIOCtls_Dev[] = { DRV_IOCTLNAMES, "get_hwaddr", NULL };
184 int Adapter_IOCtl(tVFS_Node *Node, int Num, void *Data)
186 tAdapter *adapter = Node->ImplPtr;
189 BASE_IOCTLS(DRV_TYPE_MISC, "IPStack-Adapter", VERSION, casIOCtls_Dev)
192 if( !CheckMem(Data, 6) )
195 memcpy(Data, adapter->HWAddr, 6);
201 // --- "Internal" (IPStack) API ---
202 tAdapter *Adapter_GetByName(const char *Name)
204 if( strcmp(Name, "lo") == 0 ) {
205 return &gIP_LoopAdapter;
208 if( strncmp(Name, "eth", 3) == 0 )
210 // Possibly an ethX interface
213 // Get the index at the end
214 ofs = ParseInt(Name + 3, &index);
215 if( ofs == 0 || Name[3+ofs] != '\0' )
218 // Find the adapter with that index
219 for( tAdapter *a = gpIP_AdapterList; a && a->Index <= index; a = a->Next ) {
220 if( a->Index == index )
230 char *Adapter_GetName(tAdapter *Adapter)
232 if( Adapter == &gIP_LoopAdapter )
238 // TODO: Support multiple adapter types
239 char buf[sizeof("eth")+10];
240 sprintf(buf, "eth%i", Adapter->Index);
245 void Adapter_SendPacket(tAdapter *Handle, tIPStackBuffer *Buffer)
247 if( Handle->Type == NULL )
250 void *data = IPStack_Buffer_CompactBuffer(Buffer, &outlen);
251 VFS_Write((tVAddr)Handle->CardHandle, outlen, data);
256 Handle->Type->SendPacket( Handle->CardHandle, Buffer );
261 void Adapter_int_WatchThread(void *Ptr)
263 tAdapter *Adapter = Ptr;
264 const int MTU = 1520;
265 tIPStackBuffer *buf = NULL;
268 if( Adapter->Type == NULL )
270 data = malloc( MTU );
271 buf = IPStack_Buffer_CreateBuffer(1);
275 Threads_SetName("Adapter Watcher");
276 Log_Log("IPStack", "Thread %i watching eth%i '%s'", Threads_GetTID(), Adapter->Index,
277 Adapter->Type?Adapter->Type->Name:"VFS");
281 if( Adapter->Type == NULL )
283 int len = VFS_Read((tVAddr)Adapter->CardHandle, MTU, data);
284 IPStack_Buffer_AppendSubBuffer(buf, len, 0, buf, NULL, NULL);
288 buf = Adapter->Type->WaitForPacket( Adapter->CardHandle );
291 Link_HandlePacket(Adapter, buf);
293 if( Adapter->Type == NULL )
295 IPStack_Buffer_ClearBuffer(buf);
299 IPStack_Buffer_DestroyBuffer(buf);
304 tIPStackBuffer *Adapter_int_LoopbackWaitPacket(void *Unused)
310 int Adapter_int_LoopbackSendPacket(void *Unused, tIPStackBuffer *Buffer)
312 // This is a little hacky :)
313 Link_HandlePacket(&gIP_LoopAdapter, Buffer);