3 * - By John Hodge (thePowersGang)
6 * - UDP Protocol handling
10 #include <api_drv_common.h>
13 #define UDP_ALLOC_BASE 0xC000
16 void UDP_Initialise();
17 void UDP_GetPacket(tInterface *Interface, void *Address, int Length, void *Buffer);
18 void UDP_Unreachable(tInterface *Interface, int Code, void *Address, int Length, void *Buffer);
19 void UDP_SendPacketTo(tUDPChannel *Channel, int AddrType, const void *Address, Uint16 Port, const void *Data, size_t Length);
20 // --- Client Channels
21 tVFS_Node *UDP_Channel_Init(tInterface *Interface);
22 size_t UDP_Channel_Read(tVFS_Node *Node, off_t Offset, size_t Length, void *Buffer, Uint Flags);
23 size_t UDP_Channel_Write(tVFS_Node *Node, off_t Offset, size_t Length, const void *Buffer, Uint Flags);
24 int UDP_Channel_IOCtl(tVFS_Node *Node, int ID, void *Data);
25 void UDP_Channel_Close(tVFS_Node *Node);
27 Uint16 UDP_int_AllocatePort(tUDPChannel *Channel);
28 int UDP_int_ClaimPort(tUDPChannel *Channel, Uint16 Port);
29 void UDP_int_FreePort(Uint16 Port);
30 Uint16 UDP_int_MakeChecksum(tInterface *Iface, const void *Dest, tUDPHeader *Hdr, size_t Len, const void *Data);
31 Uint16 UDP_int_PartialChecksum(Uint16 Prev, size_t Len, const void *Data);
32 Uint16 UDP_int_FinaliseChecksum(Uint16 Value);
35 tVFS_NodeType gUDP_NodeType = {
37 .Flags = VFS_NODETYPEFLAG_STREAM,
38 .Read = UDP_Channel_Read,
39 .Write = UDP_Channel_Write,
40 .IOCtl = UDP_Channel_IOCtl,
41 .Close = UDP_Channel_Close
43 tMutex glUDP_Channels; // TODO: Replace with a RWLock
44 tUDPChannel *gpUDP_Channels;
47 Uint32 gUDP_Ports[0x10000/32];
49 tSocketFile gUDP_SocketFile = {NULL, "udp", UDP_Channel_Init};
53 * \fn void TCP_Initialise()
54 * \brief Initialise the TCP Layer
58 IPStack_AddFile(&gUDP_SocketFile);
59 //IPv4_RegisterCallback(IP4PROT_UDP, UDP_GetPacket, UDP_Unreachable);
60 IPv4_RegisterCallback(IP4PROT_UDP, UDP_GetPacket);
64 * \brief Scan a list of tUDPChannels and find process the first match
65 * \return 0 if no match was found, -1 on error and 1 if a match was found
67 int UDP_int_ScanList(tUDPChannel *List, tInterface *Interface, void *Address, int Length, void *Buffer)
69 tUDPHeader *hdr = Buffer;
71 for(tUDPChannel *chan = List; chan; chan = chan->Next)
73 // Match local endpoint
74 LOG("(%p):%i - %s/%i:%i",
75 chan->Interface, chan->LocalPort,
76 IPStack_PrintAddress(chan->Remote.AddrType, &chan->Remote.Addr), chan->RemoteMask,
79 if(chan->Interface && chan->Interface != Interface) continue;
80 if(chan->LocalPort != ntohs(hdr->DestPort)) continue;
82 // Check for remote port restriction
83 if(chan->Remote.Port && chan->Remote.Port != ntohs(hdr->SourcePort))
85 // Check for remote address restriction
88 if(chan->Remote.AddrType != Interface->Type)
90 if(!IPStack_CompareAddress(Interface->Type, Address,
91 &chan->Remote.Addr, chan->RemoteMask)
96 Log_Log("UDP", "Recieved packet for %p", chan);
97 // Create the cached packet
98 int len = ntohs(hdr->Length);
99 tUDPPacket *pack = malloc(sizeof(tUDPPacket) + len);
101 memcpy(&pack->Remote.Addr, Address, IPStack_GetAddressSize(Interface->Type));
102 pack->Remote.Port = ntohs(hdr->SourcePort);
103 pack->Remote.AddrType = Interface->Type;
105 memcpy(pack->Data, hdr->Data, len);
107 // Add the packet to the channel's queue
108 SHORTLOCK(&chan->lQueue);
110 chan->QueueEnd->Next = pack;
112 chan->QueueEnd = chan->Queue = pack;
113 SHORTREL(&chan->lQueue);
114 VFS_MarkAvaliable(&chan->Node, 1);
115 Mutex_Release(&glUDP_Channels);
122 * \fn void UDP_GetPacket(tInterface *Interface, void *Address, int Length, void *Buffer)
123 * \brief Handles a packet from the IP Layer
125 void UDP_GetPacket(tInterface *Interface, void *Address, int Length, void *Buffer)
127 tUDPHeader *hdr = Buffer;
130 size_t len = strlen( IPStack_PrintAddress(Interface->Type, Address) );
132 strcpy(tmp, IPStack_PrintAddress(Interface->Type, Address));
133 Log_Debug("UDP", "%i bytes %s:%i -> %s:%i (Cksum 0x%04x)",
135 tmp, ntohs(hdr->SourcePort),
136 IPStack_PrintAddress(Interface->Type, Interface->Address), ntohs(hdr->DestPort),
137 ntohs(hdr->Checksum));
140 // Check registered connections
141 Mutex_Acquire(&glUDP_Channels);
142 UDP_int_ScanList(gpUDP_Channels, Interface, Address, Length, Buffer);
143 Mutex_Release(&glUDP_Channels);
147 * \brief Handle an ICMP Unrechable Error
149 void UDP_Unreachable(tInterface *Interface, int Code, void *Address, int Length, void *Buffer)
155 * \brief Send a packet
156 * \param Channel Channel to send the packet from
157 * \param Data Packet data
158 * \param Length Length in bytes of packet data
160 void UDP_SendPacketTo(tUDPChannel *Channel, int AddrType, const void *Address, Uint16 Port, const void *Data, size_t Length)
164 if(Channel->Interface && Channel->Interface->Type != AddrType) {
165 LOG("Bad interface type for channel packet, IF is %i, but packet is %i",
166 Channel->Interface->Type, AddrType);
171 hdr.SourcePort = htons( Channel->LocalPort );
172 hdr.DestPort = htons( Port );
173 hdr.Length = htons( sizeof(tUDPHeader) + Length );
175 hdr.Checksum = htons( UDP_int_MakeChecksum(Channel->Interface, Address, &hdr, Length, Data) );
177 tIPStackBuffer *buffer;
181 // Pass on the the IPv4 Layer
182 buffer = IPStack_Buffer_CreateBuffer(2 + IPV4_BUFFERS);
183 IPStack_Buffer_AppendSubBuffer(buffer, Length, 0, Data, NULL, NULL);
184 IPStack_Buffer_AppendSubBuffer(buffer, sizeof(hdr), 0, &hdr, NULL, NULL);
185 // TODO: What if Channel->Interface is NULL here?
186 ASSERT(Channel->Interface);
187 IPv4_SendPacket(Channel->Interface, *(tIPv4*)Address, IP4PROT_UDP, 0, buffer);
190 Log_Warning("UDP", "TODO: Implement on proto %i", AddrType);
195 // --- Client Channels
196 tVFS_Node *UDP_Channel_Init(tInterface *Interface)
199 new = calloc( sizeof(tUDPChannel), 1 );
200 new->Interface = Interface;
202 new->Node.ImplPtr = new;
203 new->Node.NumACLs = 1;
204 new->Node.ACLs = &gVFS_ACL_EveryoneRW;
205 new->Node.Type = &gUDP_NodeType;
207 Mutex_Acquire(&glUDP_Channels);
208 new->Next = gpUDP_Channels;
209 gpUDP_Channels = new;
210 Mutex_Release(&glUDP_Channels);
215 tUDPPacket *UDP_Channel_WaitForPacket(tUDPChannel *chan, Uint VFSFlags)
217 // EVIL - Yield until queue is created (avoids races)
218 while(chan->Queue == NULL)
223 tTime timeout_z = 0, *timeout = (VFSFlags & VFS_IOFLAG_NOBLOCK) ? &timeout_z : NULL;
224 int rv = VFS_SelectNode(&chan->Node, VFS_SELECT_READ, timeout, "UDP_Channel_Read");
226 errno = (VFSFlags & VFS_IOFLAG_NOBLOCK) ? EWOULDBLOCK : EINTR;
229 SHORTLOCK(&chan->lQueue);
230 if(chan->Queue == NULL) {
231 SHORTREL(&chan->lQueue);
234 tUDPPacket *pack = chan->Queue;
235 chan->Queue = pack->Next;
237 chan->QueueEnd = NULL;
238 VFS_MarkAvaliable(&chan->Node, 0); // Nothing left
240 SHORTREL(&chan->lQueue);
247 * \brief Read from the channel file (wait for a packet)
249 size_t UDP_Channel_Read(tVFS_Node *Node, off_t Offset, size_t Length, void *Buffer, Uint Flags)
251 tUDPChannel *chan = Node->ImplPtr;
253 if(chan->LocalPort == 0) {
254 Log_Notice("UDP", "Channel %p sent with no local port", chan);
258 tUDPPacket *pack = UDP_Channel_WaitForPacket(chan, Flags);
263 size_t addrlen = IPStack_GetAddressSize(pack->Remote.AddrType);
264 tUDPEndpoint *ep = Buffer;
265 size_t ofs = 4 + addrlen;
267 // Check that the header fits
270 Log_Notice("UDP", "Insuficient space for header in buffer (%i < %i)", (int)Length, ofs);
275 ep->Port = pack->Remote.Port;
276 ep->AddrType = pack->Remote.AddrType;
277 memcpy(&ep->Addr, &pack->Remote.Addr, addrlen);
280 if(Length > ofs + pack->Length) Length = ofs + pack->Length;
281 memcpy((char*)Buffer + ofs, pack->Data, Length - ofs);
283 // Free cached packet
290 * \brief Write to the channel file (send a packet)
292 size_t UDP_Channel_Write(tVFS_Node *Node, off_t Offset, size_t Length, const void *Buffer, Uint Flags)
294 tUDPChannel *chan = Node->ImplPtr;
295 const tUDPEndpoint *ep;
299 if(chan->LocalPort == 0) {
300 Log_Notice("UDP", "Write to channel %p with zero local port", chan);
305 ofs = 2 + 2 + IPStack_GetAddressSize( ep->AddrType );
307 data = (const char *)Buffer + ofs;
309 UDP_SendPacketTo(chan, ep->AddrType, &ep->Addr, ep->Port, data, (size_t)Length - ofs);
315 * \brief Names for channel IOCtl Calls
317 static const char *casIOCtls_Channel[] = {
328 * \brief Channel IOCtls
330 int UDP_Channel_IOCtl(tVFS_Node *Node, int ID, void *Data)
332 tUDPChannel *chan = Node->ImplPtr;
333 ENTER("pNode iID pData", Node, ID, Data);
336 BASE_IOCTLS(DRV_TYPE_MISC, "UDP Channel", 0x100, casIOCtls_Channel);
338 case 4: { // getset_localport (returns bool success)
339 if(!Data) LEAVE_RET('i', chan->LocalPort);
340 if(!CheckMem( Data, sizeof(Uint16) ) ) {
341 LOG("Invalid pointer %p", Data);
345 int req_port = *(Uint16*)Data;
346 // Permissions check (Ports lower than 1024 are root-only)
347 if(req_port != 0 && req_port < 1024) {
348 if( Threads_GetUID() != 0 ) {
349 LOG("Attempt by non-superuser to listen on port %i", req_port);
353 // Allocate a random port if requested
355 UDP_int_AllocatePort(chan);
356 // Else, mark the requested port as used
357 else if( UDP_int_ClaimPort(chan, req_port) ) {
358 LOG("Port %i is currently in use", req_port);
361 LEAVE_RET('i', chan->LocalPort);
364 case 5: // getset_remoteport (returns bool success)
365 if(!Data) LEAVE_RET('i', chan->Remote.Port);
366 if(!CheckMem( Data, sizeof(Uint16) ) ) {
367 LOG("Invalid pointer %p", Data);
370 chan->Remote.Port = *(Uint16*)Data;
371 LEAVE('i', chan->Remote.Port);
372 return chan->Remote.Port;
374 case 6: // getset_remotemask (returns bool success)
375 if(!Data) LEAVE_RET('i', chan->RemoteMask);
376 if(!CheckMem(Data, sizeof(int))) {
377 LOG("Data pointer invalid");
380 if( !chan->Interface ) {
381 LOG("Can't set remote mask on NULL interface");
384 int mask = *(int*)Data;
385 int addr_bits = IPStack_GetAddressSize(chan->Interface->Type) * 8;
386 if( mask > addr_bits ) {
387 LOG("Mask too large (%i > max %i)", mask, addr_bits);
390 chan->RemoteMask = mask;
391 LEAVE('i', chan->RemoteMask);
392 return chan->RemoteMask;
394 case 7: // set_remoteaddr (returns bool success)
395 if( !chan->Interface ) {
396 LOG("Can't set remote address on NULL interface");
399 if(!CheckMem(Data, IPStack_GetAddressSize(chan->Interface->Type))) {
400 LOG("Invalid pointer");
403 LOG("Set remote addr %s", IPStack_PrintAddress(chan->Interface->Type, Data));
404 chan->Remote.AddrType = chan->Interface->Type;
405 memcpy(&chan->Remote.Addr, Data, IPStack_GetAddressSize(chan->Interface->Type));
409 if(!CheckMem(Data, 2*sizeof(void*)+2)) {
410 LOG("Data pointer invalid");
413 const struct sSendToArgs {
414 const tUDPEndpoint* ep;
417 } info = *(const struct sSendToArgs*)Data;
418 LOG("sendto(buf=%p + %u, ep=%p)", info.buf, info.buflen, info.ep);
419 if(!CheckMem(info.ep, 2+2) || !CheckMem(info.ep, 2+2+IPStack_GetAddressSize(info.ep->AddrType)) ) {
422 if(!CheckMem(info.buf, info.buflen)) {
426 UDP_SendPacketTo(chan, info.ep->AddrType, &info.ep->Addr, info.ep->Port,
427 info.buf, (size_t)info.buflen);
429 LEAVE_RET('i', info.buflen); }
430 case 9: { // recvfrom
431 if(!CheckMem(Data, 2*sizeof(void*)+2)) {
432 LOG("Data pointer invalid");
435 const struct sRecvFromArgs {
439 } info = *(const struct sRecvFromArgs*)Data;
440 LOG("recvfrom(buf=%p + %u, ep=%p)", info.buf, info.buflen, info.ep);
441 if(!CheckMem(info.ep, 2+2)) {
444 if(!CheckMem(info.buf, info.buflen)) {
448 tUDPPacket *pack = UDP_Channel_WaitForPacket(chan, 0);
454 size_t addrsize = IPStack_GetAddressSize(pack->Remote.AddrType);
455 if( !CheckMem(info.ep, 2+2+addrsize) ) {
456 LOG("Insufficient space for source address");
460 info.ep->Port = pack->Remote.Port;
461 info.ep->AddrType = pack->Remote.AddrType;
462 memcpy(&info.ep->Addr, &pack->Remote.Addr, addrsize);
464 size_t retlen = (info.buflen < pack->Length ? info.buflen : pack->Length);
465 memcpy(info.buf, pack->Data, retlen);
469 LEAVE_RET('i', retlen); }
475 * \brief Close and destroy an open channel
477 void UDP_Channel_Close(tVFS_Node *Node)
479 tUDPChannel *chan = Node->ImplPtr;
482 // Remove from the main list first
483 Mutex_Acquire(&glUDP_Channels);
484 if(gpUDP_Channels == chan)
485 gpUDP_Channels = gpUDP_Channels->Next;
488 for(prev = gpUDP_Channels;
489 prev->Next && prev->Next != chan;
492 Log_Warning("UDP", "Bookeeping Fail, channel %p is not in main list", chan);
494 prev->Next = prev->Next->Next;
496 Mutex_Release(&glUDP_Channels);
499 SHORTLOCK(&chan->lQueue);
504 chan->Queue = tmp->Next;
507 SHORTREL(&chan->lQueue);
509 // Free channel structure
514 * \return Port Number on success, or zero on failure
516 Uint16 UDP_int_AllocatePort(tUDPChannel *Channel)
518 Mutex_Acquire(&glUDP_Ports);
520 for( int base = UDP_ALLOC_BASE; base < 0x10000; base += 32 )
522 if( gUDP_Ports[base/32] == 0xFFFFFFFF )
524 for( int i = 0; i < 32; i++ )
526 if( gUDP_Ports[base/32] & (1 << i) )
528 gUDP_Ports[base/32] |= (1 << i);
529 Mutex_Release(&glUDP_Ports);
530 // If claim succeeds, good
531 if( UDP_int_ClaimPort(Channel, base + i) == 0 )
533 // otherwise keep looking
534 Mutex_Acquire(&glUDP_Ports);
538 Mutex_Release(&glUDP_Ports);
543 * \brief Allocate a specific port
544 * \return Boolean Success
546 int UDP_int_ClaimPort(tUDPChannel *Channel, Uint16 Port)
548 // Search channel list for a connection with same (or wildcard)
549 // interface, and same port
550 Mutex_Acquire(&glUDP_Channels);
551 for( tUDPChannel *ch = gpUDP_Channels; ch; ch = ch->Next)
555 if( ch->Interface && ch->Interface != Channel->Interface )
557 if( ch->LocalPort != Port )
559 Mutex_Release(&glUDP_Channels);
562 Channel->LocalPort = Port;
563 Mutex_Release(&glUDP_Channels);
568 * \brief Free an allocated port
570 void UDP_int_FreePort(Uint16 Port)
572 Mutex_Acquire(&glUDP_Ports);
573 gUDP_Ports[Port/32] &= ~(1 << (Port%32));
574 Mutex_Release(&glUDP_Ports);
580 Uint16 UDP_int_MakeChecksum(tInterface *Interface, const void *Dest, tUDPHeader *Hdr, size_t Len, const void *Data)
582 size_t addrsize = IPStack_GetAddressSize(Interface->Type);
590 switch(Interface->Type)
592 case 4: pheader.Protocol = IP4PROT_UDP; break;
593 //case 6: pheader.Protocol = IP6PROT_UDP; break;
595 Log_Warning("UDP", "Unimplemented _MakeChecksum proto %i", Interface->Type);
598 pheader.UDPLength = Hdr->Length;
601 csum = UDP_int_PartialChecksum(csum, addrsize, Interface->Address);
602 csum = UDP_int_PartialChecksum(csum, addrsize, Dest);
603 csum = UDP_int_PartialChecksum(csum, sizeof(pheader), &pheader);
604 csum = UDP_int_PartialChecksum(csum, sizeof(tUDPHeader), Hdr);
605 csum = UDP_int_PartialChecksum(csum, Len, Data);
607 return UDP_int_FinaliseChecksum(csum);
610 static inline Uint16 _add_ones_complement16(Uint16 a, Uint16 b)
612 // One's complement arithmatic, overflows increment bottom bit
613 return a + b + (b > 0xFFFF - a ? 1 : 0);
616 Uint16 UDP_int_PartialChecksum(Uint16 Prev, size_t Len, const void *Data)
619 const Uint16 *data = Data;
620 for( int i = 0; i < Len/2; i ++ )
621 ret = _add_ones_complement16(ret, htons(*data++));
623 ret = _add_ones_complement16(ret, htons(*(const Uint8*)data));
627 Uint16 UDP_int_FinaliseChecksum(Uint16 Value)
629 Value = ~Value; // One's complement it
630 return (Value == 0 ? 0xFFFF : Value);