3 * - Link/Media Layer Interface
7 #include "include/buffer.h"
10 #define MAX_PACKET_SIZE 2048
13 void Link_RegisterType(Uint16 Type, tPacketCallback Callback);
14 void Link_SendPacket(tAdapter *Adapter, Uint16 Type, tMacAddr To, tIPStackBuffer *Buffer);
15 void Link_WatchDevice(tAdapter *Adapter);
17 void Link_InitCRC(void);
18 Uint32 Link_CalculateCRC(tIPStackBuffer *Buffer);
19 Uint32 Link_CalculatePartialCRC(Uint32 CRC, const void *Data, int Length);
22 int giRegisteredTypes = 0;
23 int giRegisteredTypeSpace = 0;
26 tPacketCallback Callback;
28 int gbLink_CRCTableGenerated = 0;
29 Uint32 gaiLink_CRCTable[256];
33 * \fn void Link_RegisterType(Uint16 Type, tPacketCallback Callback)
34 * \brief Registers a callback for a specific packet type
36 * \todo Make thread safe (place a mutex on the list)
38 void Link_RegisterType(Uint16 Type, tPacketCallback Callback)
43 for( i = giRegisteredTypes; i -- ; )
45 if(gaRegisteredTypes[i].Type == Type) {
46 Log_Warning("Net Link", "Attempt to register 0x%x twice", Type);
50 if(gaRegisteredTypes[i].Callback == NULL) break;
55 giRegisteredTypeSpace += 5;
56 tmp = realloc(gaRegisteredTypes, giRegisteredTypeSpace*sizeof(*gaRegisteredTypes));
58 Log_Warning("Net Link",
59 "Out of heap space! (Attempted to allocate %i)",
60 giRegisteredTypeSpace*sizeof(*gaRegisteredTypes)
64 gaRegisteredTypes = tmp;
65 i = giRegisteredTypes;
69 gaRegisteredTypes[i].Callback = Callback;
70 gaRegisteredTypes[i].Type = Type;
74 * \fn void Link_SendPacket(tAdapter *Adapter, Uint16 Type, tMacAddr To, int Length, void *Buffer)
75 * \brief Formats and sends a packet on the specified interface
77 void Link_SendPacket(tAdapter *Adapter, Uint16 Type, tMacAddr To, tIPStackBuffer *Buffer)
79 int length = IPStack_Buffer_GetLength(Buffer);
80 int ofs = 4 - (length & 3);
81 Uint8 buf[sizeof(tEthernetHeader) + ofs + 4];
82 tEthernetHeader *hdr = (void*)buf;
84 if( ofs == 4 ) ofs = 0;
86 Log_Log("Net Link", "Sending %i bytes to %02x:%02x:%02x:%02x:%02x:%02x (Type 0x%x)",
87 length, To.B[0], To.B[1], To.B[2], To.B[3], To.B[4], To.B[5], Type);
90 hdr->Src = Adapter->MacAddr;
91 hdr->Type = htons(Type);
92 *(Uint32*)(buf + sizeof(tEthernetHeader) + ofs) = 0;
94 IPStack_Buffer_AppendSubBuffer(Buffer, sizeof(tEthernetHeader), ofs + 4, hdr, NULL, NULL);
96 *(Uint32*)(buf + sizeof(tEthernetHeader) + ofs) = htonl( Link_CalculateCRC(Buffer) );
99 void *data = IPStack_Buffer_CompactBuffer(Buffer, &outlen);
100 VFS_Write(Adapter->DeviceFD, outlen, data);
104 void Link_WorkerThread(void *Ptr)
106 tAdapter *Adapter = Ptr;
108 Threads_SetName(Adapter->Device);
109 Log_Log("Net Link", "Thread %i watching '%s'", Threads_GetTID(), Adapter->Device);
112 while(Adapter->DeviceFD != -1)
114 Uint8 buf[MAX_PACKET_SIZE];
115 tEthernetHeader *hdr = (void*)buf;
119 // Wait for a packet (Read on a network device is blocking)
120 //Log_Debug("NET", "Waiting on adapter FD#0x%x", Adapter->DeviceFD);
121 ret = VFS_Read(Adapter->DeviceFD, MAX_PACKET_SIZE, buf);
124 if(ret < sizeof(tEthernetHeader)) {
125 Log_Log("Net Link", "Recieved an undersized packet (%i < %i)",
126 ret, sizeof(tEthernetHeader));
131 "Packet from %02x:%02x:%02x:%02x:%02x:%02x"
132 " to %02x:%02x:%02x:%02x:%02x:%02x (Type=%04x)",
133 hdr->Src.B[0], hdr->Src.B[1], hdr->Src.B[2],
134 hdr->Src.B[3], hdr->Src.B[4], hdr->Src.B[5],
135 hdr->Dest.B[0], hdr->Dest.B[1], hdr->Dest.B[2],
136 hdr->Dest.B[3], hdr->Dest.B[4], hdr->Dest.B[5],
139 checksum = *(Uint32*)&hdr->Data[ret-sizeof(tEthernetHeader)-4];
140 //Log_Log("NET", "Checksum 0x%08x", checksum);
141 // TODO: Check checksum
143 // Check if there is a registered callback for this packet type
144 for( i = giRegisteredTypes; i--; )
146 if(gaRegisteredTypes[i].Type == ntohs(hdr->Type)) break;
150 Log_Log("Net Link", "Unregistered type 0x%x", ntohs(hdr->Type));
155 gaRegisteredTypes[i].Callback(
158 ret - sizeof(tEthernetHeader),
163 Log_Log("Net Link", "Watcher terminated (file closed)");
169 * \fn void Link_WatchDevice(tAdapter *Adapter)
170 * \brief Spawns a worker thread to watch the specified adapter
172 void Link_WatchDevice(tAdapter *Adapter)
176 if( !gbLink_CRCTableGenerated )
179 tid = Proc_SpawnWorker(Link_WorkerThread, Adapter); // Create a new worker thread
182 Log_Warning("Net Link", "Unable to create watcher thread for '%s'", Adapter->Device);
186 Log_Log("Net Link", "Watching '%s' using tid %i", Adapter->Device, tid);
189 // From http://www.cl.cam.ac.uk/research/srg/bluebook/21/crc/node6.html
190 #define QUOTIENT 0x04c11db7
191 void Link_InitCRC(void)
196 for (i = 0; i < 256; i++)
199 for (j = 0; j < 8; j++)
201 if (crc & 0x80000000)
202 crc = (crc << 1) ^ QUOTIENT;
206 gaiLink_CRCTable[i] = crc;
209 gbLink_CRCTableGenerated = 1;
212 Uint32 Link_CalculateCRC(tIPStackBuffer *Buffer)
214 Uint32 ret = 0xFFFFFFFF;
219 while( (id = IPStack_Buffer_GetBuffer(Buffer, id, &length, &data)) != -1 )
221 ret = Link_CalculatePartialCRC(ret, data, length);
227 Uint32 Link_CalculatePartialCRC(Uint32 CRC, const void *Data, int Length)
229 // x32 + x26 + x23 + x22 + x16 + x12 + x11 + x10 + x8 + x7 + x5 + x4 + x2 + x + 1
230 const Uint32 *data = Data;
232 for( int i = 0; i < Length/4; i++ )
234 CRC = (CRC << 8 | *data++) ^ gaiLink_CRCTable[CRC >> 24];