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 char buf[sizeof(type)+10]; \
147 sprintf(buf, type"%i", a->Index); \
148 return strdup(buf); \
153 CHECK_LIST(gpIP_AdapterList, "eth");
154 // TODO: Support other types of adapters (wifi, tap, ...)
159 tVFS_Node *Adapter_FindDir(tVFS_Node *Node, const char *Name)
161 tAdapter *a = Adapter_GetByName(Name);
168 static const char *casIOCtls_Root[] = { DRV_IOCTLNAMES, "add_vfs_adapter", NULL };
169 int Adapter_DirIOCtl(tVFS_Node *Node, int Num, void *Data)
173 BASE_IOCTLS(DRV_TYPE_MISC, "IPStack-AdapterList", VERSION, casIOCtls_Root)
176 if( !CheckString(Data) )
179 // Return non-zero if add fails
180 return IPStack_Adapter_AddVFS(Data) == NULL;
185 static const char *casIOCtls_Dev[] = { DRV_IOCTLNAMES, "get_hwaddr", NULL };
186 int Adapter_IOCtl(tVFS_Node *Node, int Num, void *Data)
188 tAdapter *adapter = Node->ImplPtr;
191 BASE_IOCTLS(DRV_TYPE_MISC, "IPStack-Adapter", VERSION, casIOCtls_Dev)
194 if( !CheckMem(Data, 6) )
197 memcpy(Data, adapter->HWAddr, 6);
203 // --- "Internal" (IPStack) API ---
204 tAdapter *Adapter_GetByName(const char *Name)
206 if( strcmp(Name, "lo") == 0 ) {
207 return &gIP_LoopAdapter;
210 if( strncmp(Name, "eth", 3) == 0 )
212 // Possibly an ethX interface
215 // Get the index at the end
216 ofs = ParseInt(Name + 3, &index);
217 if( ofs == 0 || Name[3+ofs] != '\0' )
220 // Find the adapter with that index
221 for( tAdapter *a = gpIP_AdapterList; a && a->Index <= index; a = a->Next ) {
222 if( a->Index == index )
232 void Adapter_SendPacket(tAdapter *Handle, tIPStackBuffer *Buffer)
234 if( Handle->Type == NULL )
237 void *data = IPStack_Buffer_CompactBuffer(Buffer, &outlen);
238 VFS_Write((tVAddr)Handle->CardHandle, outlen, data);
243 Handle->Type->SendPacket( Handle->CardHandle, Buffer );
248 void Adapter_int_WatchThread(void *Ptr)
250 tAdapter *Adapter = Ptr;
251 const int MTU = 1520;
252 tIPStackBuffer *buf = NULL;
255 if( Adapter->Type == NULL )
257 data = malloc( MTU );
258 buf = IPStack_Buffer_CreateBuffer(1);
262 Threads_SetName("Adapter Watcher");
263 Log_Log("IPStack", "Thread %i watching eth%i '%s'", Threads_GetTID(), Adapter->Index,
264 Adapter->Type?Adapter->Type->Name:"VFS");
268 if( Adapter->Type == NULL )
270 int len = VFS_Read((tVAddr)Adapter->CardHandle, MTU, data);
271 IPStack_Buffer_AppendSubBuffer(buf, len, 0, buf, NULL, NULL);
275 buf = Adapter->Type->WaitForPacket( Adapter->CardHandle );
278 Link_HandlePacket(Adapter, buf);
280 if( Adapter->Type == NULL )
282 IPStack_Buffer_ClearBuffer(buf);
286 IPStack_Buffer_DestroyBuffer(buf);
291 tIPStackBuffer *Adapter_int_LoopbackWaitPacket(void *Unused)
297 int Adapter_int_LoopbackSendPacket(void *Unused, tIPStackBuffer *Buffer)
299 // This is a little hacky :)
300 Link_HandlePacket(&gIP_LoopAdapter, Buffer);