Work on IP/TCP Stack
[tpg/acess2.git] / Modules / IPStack / tcp.c
1 /*
2  * Acess2 IP Stack
3  * - TCP Handling
4  */
5 #include "ipstack.h"
6 #include "ipv4.h"
7 #include "tcp.h"
8
9 #define TCP_MIN_DYNPORT 0x1000
10 #define TCP_MAX_HALFOPEN        1024    // Should be enough
11
12 // === PROTOTYPES ===
13 void    TCP_Initialise();
14 void    TCP_StartConnection(tTCPConnection *Conn);
15 void    TCP_GetPacket(tInterface *Interface, void *Address, int Length, void *Buffer);
16 void    TCP_INT_HandleConnectionPacket(tTCPConnection *Connection, tTCPHeader *Header);
17 Uint16  TCP_GetUnusedPort();
18  int    TCP_AllocatePort(Uint16 Port);
19  int    TCP_DeallocatePort(Uint16 Port);
20 // --- Server
21 tVFS_Node       *TCP_Server_Init(tInterface *Interface);
22 char    *TCP_Server_ReadDir(tVFS_Node *Node, int Pos);
23 tVFS_Node       *TCP_Server_FindDir(tVFS_Node *Node, char *Name);
24  int    TCP_Server_IOCtl(tVFS_Node *Node, int ID, void *Data);
25 void    TCP_Server_Close(tVFS_Node *Node);
26 // --- Client
27 tVFS_Node       *TCP_Client_Init(tInterface *Interface);
28 Uint64  TCP_Client_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer);
29 Uint64  TCP_Client_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer);
30  int    TCP_Client_IOCtl(tVFS_Node *Node, int ID, void *Data);
31 void    TCP_Client_Close(tVFS_Node *Node);
32
33 // === TEMPLATES ===
34 tSocketFile     gTCP_ServerFile = {NULL, "tcps", TCP_Server_Init};
35 tSocketFile     gTCP_ClientFile = {NULL, "tcpc", TCP_Client_Init};
36
37 // === GLOBALS ===
38  int    giTCP_NumHalfopen = 0;
39 tSpinlock       glTCP_Listeners;
40 tTCPListener    *gTCP_Listeners;
41 tSpinlock       glTCP_OutbountCons;
42 tTCPConnection  *gTCP_OutbountCons;
43 Uint32  gaTCP_PortBitmap[0x800];
44  int    giTCP_NextOutPort = TCP_MIN_DYNPORT;
45
46 // === CODE ===
47 /**
48  * \fn void TCP_Initialise()
49  * \brief Initialise the TCP Layer
50  */
51 void TCP_Initialise()
52 {
53         IPStack_AddFile(&gTCP_ServerFile);
54         IPStack_AddFile(&gTCP_ClientFile);
55         IPv4_RegisterCallback(IP4PROT_TCP, TCP_GetPacket);
56 }
57
58 /**
59  * \brief Open a connection to another host using TCP
60  */
61 void TCP_StartConnection(tTCPConnection *Conn)
62 {       
63         // SEND PACKET
64         // Send a TCP SYN to the target to open the connection
65         return ;
66 }
67
68 /**
69  * \fn void TCP_GetPacket(tInterface *Interface, void *Address, int Length, void *Buffer)
70  * \brief Handles a packet from the IP Layer
71  */
72 void TCP_GetPacket(tInterface *Interface, void *Address, int Length, void *Buffer)
73 {
74         tTCPHeader      *hdr = Buffer;
75         tTCPListener    *srv;
76         tTCPConnection  *conn;
77         
78         Log("[TCP  ] sizeof(tTCPHeader) = %i", sizeof(tTCPHeader));
79         Log("[TCP  ] DestPort = %i", ntohs(hdr->DestPort));
80         Log("[TCP  ] DestPort = %i", ntohs(hdr->DestPort));
81         Log("[TCP  ] SequenceNumber = %i", ntohl(hdr->SequenceNumber));
82         Log("[TCP  ] AcknowlegementNumber = %i", ntohl(hdr->AcknowlegementNumber));
83         Log("[TCP  ] DataOffset = %i", hdr->DataOffset >> 4);
84         Log("[TCP  ] Flags = {");
85         Log("[TCP  ]   CWR = %B", !!(hdr->Flags & TCP_FLAG_CWR));
86         Log("[TCP  ]   ECE = %B", !!(hdr->Flags & TCP_FLAG_ECE));
87         Log("[TCP  ]   URG = %B", !!(hdr->Flags & TCP_FLAG_URG));
88         Log("[TCP  ]   ACK = %B", !!(hdr->Flags & TCP_FLAG_ACK));
89         Log("[TCP  ]   PSH = %B", !!(hdr->Flags & TCP_FLAG_PSH));
90         Log("[TCP  ]   RST = %B", !!(hdr->Flags & TCP_FLAG_RST));
91         Log("[TCP  ]   SYN = %B", !!(hdr->Flags & TCP_FLAG_SYN));
92         Log("[TCP  ]   FIN = %B", !!(hdr->Flags & TCP_FLAG_FIN));
93         Log("[TCP  ] }");
94         Log("[TCP  ] WindowSize = %i", htons(hdr->WindowSize));
95         Log("[TCP  ] Checksum = 0x%x", htons(hdr->Checksum));
96         Log("[TCP  ] UrgentPointer = 0x%x", htons(hdr->UrgentPointer));
97         
98         // Check Servers
99         {
100                 for( srv = gTCP_Listeners; srv; srv = srv->Next )
101                 {
102                         // Check the interface
103                         if(srv->Interface && srv->Interface != Interface)       continue;
104                         // Check the destination port
105                         if(srv->Port != hdr->DestPort)  continue;
106                         
107                         // Is this in an established connection?
108                         for( conn = srv->Connections; conn; conn = conn->Next )
109                         {
110                                 // Check that it is coming in on the same interface
111                                 if(conn->Interface != Interface)        continue;
112                                 
113                                 // Check Source Port
114                                 if(conn->RemotePort != hdr->SourcePort) continue;
115                                 
116                                 // Check Source IP
117                                 if(conn->Interface->Type == 6 && !IP6_EQU(conn->RemoteIP.v6, *(tIPv6*)Address))
118                                         continue;
119                                 if(conn->Interface->Type == 4 && !IP4_EQU(conn->RemoteIP.v4, *(tIPv4*)Address))
120                                         continue;
121                                 
122                                 // We have a response!
123                                 TCP_INT_HandleConnectionPacket(conn, hdr)
124                                 
125                                 return;
126                         }
127                         
128                         // Open a new connection (well, check that it's a SYN)
129                         //TODO
130                         
131                         break;
132                 }
133         }
134         
135         
136         // Check Open Connections
137         {
138                 for( conn = gTCP_OutbountCons; conn; conn = conn->Next )
139                 {
140                         // Check that it is coming in on the same interface
141                         if(conn->Interface != Interface)        continue;
142                         
143                         // Check Source Port
144                         if(conn->RemotePort != hdr->SourcePort) continue;
145                         
146                         // Check Source IP
147                         if(conn->Interface->Type == 6 && !IP6_EQU(conn->RemoteIP.v6, *(tIPv6*)Address))
148                                 continue;
149                         if(conn->Interface->Type == 4 && !IP4_EQU(conn->RemoteIP.v4, *(tIPv4*)Address))
150                                 continue;
151                         
152                         TCP_INT_HandleConnectionPacket(conn, hdr)
153                 }
154         }
155 }
156
157 /**
158  * \brief Handles a packet sent to a specific connection
159  */
160 void TCP_INT_HandleConnectionPacket(tTCPConnection *Connection, tTCPHeader *Header)
161 {
162         
163 }
164
165 /**
166  * \fn Uint16 TCP_GetUnusedPort()
167  * \brief Gets an unused port and allocates it
168  */
169 Uint16 TCP_GetUnusedPort()
170 {
171         Uint16  ret;
172         
173         // Get Next outbound port
174         ret = giTCP_NextOutPort++;
175         while( gaTCP_PortBitmap[ret/32] & (1 << (ret%32)) )
176         {
177                 ret ++;
178                 giTCP_NextOutPort++;
179                 if(giTCP_NextOutPort == 0x10000) {
180                         ret = giTCP_NextOutPort = TCP_MIN_DYNPORT;
181                 }
182         }
183         
184         // Mark the new port as used
185         gaTCP_PortBitmap[ret/32] |= 1 << (ret%32);
186         
187         return ret;
188 }
189
190 /**
191  * \fn int TCP_AllocatePort(Uint16 Port)
192  * \brief Marks a port as used
193  */
194 int TCP_AllocatePort(Uint16 Port)
195 {
196         // Check if the port has already been allocated
197         if( gaTCP_PortBitmap[Port/32] & (1 << (Port%32)) )
198                 return 0;
199         
200         // Allocate
201         gaTCP_PortBitmap[Port/32] |= 1 << (Port%32);
202         
203         return 1;
204 }
205
206 /**
207  * \fn int TCP_DeallocatePort(Uint16 Port)
208  * \brief Marks a port as unused
209  */
210 int TCP_DeallocatePort(Uint16 Port)
211 {
212         // Check if the port has already been allocated
213         if( !(gaTCP_PortBitmap[Port/32] & (1 << (Port%32))) )
214                 return 0;
215         
216         // Allocate
217         gaTCP_PortBitmap[Port/32] &= ~(1 << (Port%32));
218         
219         return 1;
220 }
221
222 // --- Server
223 tVFS_Node *TCP_Server_Init(tInterface *Interface)
224 {
225         tTCPListener    *srv = malloc( sizeof(tTCPListener) );
226         
227         srv->Interface = Interface;
228         srv->Port = 0;
229         srv->Connections = NULL;
230         srv->Next = NULL;
231         srv->Node.ImplPtr = srv;
232         srv->Node.NumACLs = 1;
233         srv->Node.ACLs = &gVFS_ACL_EveryoneRW;
234         srv->Node.ReadDir = TCP_Server_ReadDir;
235         srv->Node.FindDir = TCP_Server_FindDir;
236         srv->Node.IOCtl = TCP_Server_IOCtl;
237         srv->Node.Close = TCP_Server_Close;
238         
239         LOCK(&glTCP_Listeners);
240         srv->Next = gTCP_Listeners;
241         gTCP_Listeners = srv;
242         RELEASE(&glTCP_Listeners);
243         
244         return &srv->Node;
245 }
246
247 char *TCP_Server_ReadDir(tVFS_Node *Node, int Pos)
248 {
249         return NULL;
250 }
251
252 tVFS_Node *TCP_Server_FindDir(tVFS_Node *Node, char *Name)
253 {
254         return NULL;
255 }
256
257 int TCP_Server_IOCtl(tVFS_Node *Node, int ID, void *Data)
258 {
259         tTCPListener    *srv = Node->ImplPtr;
260         
261         switch(ID)
262         {
263         case 4: // Get/Set Port
264                 if(!Data)
265                         return srv->Port;
266                 
267                 if(srv->Port)
268                         return -1;
269                 
270                 if(!CheckMem(Data, sizeof(Uint16)))
271                         return -1;
272                 
273                 if(Threads_GetUID() != 0 && *(Uint16*)Data < 1024)
274                         return -1;
275                 
276                 srv->Port = *(Uint16*)Data;
277                 if(srv->Port == 0)
278                         srv->Port = TCP_GetUnusedPort();
279                 else
280                         TCP_AllocatePort(srv->Port);
281                 return srv->Port;
282         }
283         return 0;
284 }
285
286 void TCP_Server_Close(tVFS_Node *Node)
287 {
288         free(Node->ImplPtr);
289 }
290
291 // --- Client
292 tVFS_Node *TCP_Client_Init(tInterface *Interface)
293 {
294         tTCPConnection  *conn = malloc( sizeof(tTCPConnection) );
295         
296         conn->State = TCP_ST_CLOSED;
297         conn->Interface = Interface;
298         conn->LocalPort = 0;
299         conn->RemotePort = 0;
300         memset( &conn->RemoteIP, 0, sizeof(conn->RemoteIP) );
301         
302         conn->Node.ImplPtr = conn;
303         conn->Node.NumACLs = 1;
304         conn->Node.ACLs = &gVFS_ACL_EveryoneRW;
305         conn->Node.Read = TCP_Client_Read;
306         conn->Node.Write = TCP_Client_Write;
307         conn->Node.IOCtl = TCP_Client_IOCtl;
308         conn->Node.Close = TCP_Client_Close;
309         
310         LOCK(&glTCP_OutbountCons);
311         conn->Next = gTCP_OutbountCons;
312         gTCP_OutbountCons = conn;
313         RELEASE(&glTCP_OutbountCons);
314         
315         return &conn->Node;
316 }
317
318 Uint64 TCP_Client_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
319 {
320         return 0;
321 }
322
323 Uint64 TCP_Client_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
324 {
325         return 0;
326 }
327
328 int TCP_Client_IOCtl(tVFS_Node *Node, int ID, void *Data)
329 {
330         tTCPConnection  *conn = Node->ImplPtr;
331         
332         switch(ID)
333         {
334         case 4: // Get/Set local port
335                 if(!Data)
336                         return conn->LocalPort;
337                 if(conn->State != TCP_ST_CLOSED)
338                         return -1;
339                 if(!CheckMem(Data, sizeof(Uint16)))
340                         return -1;
341                 
342                 if(Threads_GetUID() != 0 && *(Uint16*)Data < 1024)
343                         return -1;
344                 
345                 conn->LocalPort = *(Uint16*)Data;
346                 return 0;
347         
348         case 5: // Get/Set remote port
349                 if(!Data)       return conn->RemotePort;
350                 if(conn->State != TCP_ST_CLOSED)        return -1;
351                 if(!CheckMem(Data, sizeof(Uint16)))     return -1;
352                 conn->RemotePort = *(Uint16*)Data;
353                 return 0;
354         
355         case 6: // Set Remote IP
356                 if( conn->State != TCP_ST_CLOSED )
357                         return -1;
358                 if( conn->Interface->Type == 4 )
359                 {
360                         if(!CheckMem(Data, sizeof(tIPv4)))      return -1;
361                         conn->RemoteIP.v4 = *(tIPv4*)Data;
362                 }
363                 else if( conn->Interface->Type == 6 )
364                 {
365                         if(!CheckMem(Data, sizeof(tIPv6)))      return -1;
366                         conn->RemoteIP.v6 = *(tIPv6*)Data;
367                 }
368                 return 0;
369         
370         case 7: // Connect
371                 if(conn->LocalPort == -1)
372                         conn->LocalPort = TCP_GetUnusedPort();
373                 if(conn->RemotePort == -1)
374                         return 0;
375                 
376                 TCP_StartConnection(conn);
377                 return 1;
378         }
379         
380         return 0;
381 }
382
383 void TCP_Client_Close(tVFS_Node *Node)
384 {
385         free(Node->ImplPtr);
386 }

UCC git Repository :: git.ucc.asn.au