d65adb6efc6954f4a8787df33c96757142d6f878
[tpg/acess2.git] / Modules / IPStack / ipv4.c
1 /*
2  * Acess2 IP Stack
3  * - IPv4 Protcol Handling
4  */
5 #include "ipstack.h"
6 #include "link.h"
7 #include "ipv4.h"
8
9 #define DEFAULT_TTL     32
10
11 // === IMPORTS ===
12 extern tInterface       *gIP_Interfaces;
13 extern void     ICMP_Initialise();
14 extern  int     ICMP_Ping(tInterface *Interface, tIPv4 Addr);
15 extern tMacAddr ARP_Resolve4(tInterface *Interface, tIPv4 Address);
16
17 // === PROTOTYPES ===
18  int    IPv4_Initialise();
19  int    IPv4_RegisterCallback(int ID, tIPCallback Callback);
20 void    IPv4_int_GetPacket(tAdapter *Interface, tMacAddr From, int Length, void *Buffer);
21 tInterface      *IPv4_GetInterface(tAdapter *Adapter, tIPv4 Address, int Broadcast);
22 Uint32  IPv4_Netmask(int FixedBits);
23 Uint16  IPv4_Checksum(void *Buf, int Size);
24  int    IPv4_Ping(tInterface *Iface, tIPv4 Addr);
25
26 // === GLOBALS ===
27 tIPCallback     gaIPv4_Callbacks[256];
28
29 // === CODE ===
30 /**
31  * \fn int IPv4_Initialise()
32  */
33 int IPv4_Initialise()
34 {
35         ICMP_Initialise();
36         Link_RegisterType(IPV4_ETHERNET_ID, IPv4_int_GetPacket);
37         return 1;
38 }
39
40 /**
41  * \fn int IPv4_RegisterCallback( int ID, tIPCallback Callback )
42  * \brief Registers a callback
43  */
44 int IPv4_RegisterCallback(int ID, tIPCallback Callback)
45 {
46         if( ID < 0 || ID > 255 )        return 0;
47         if( gaIPv4_Callbacks[ID] )      return 0;
48         gaIPv4_Callbacks[ID] = Callback;
49         return 1;
50 }
51
52 /**
53  * \brief Creates and sends an IPv4 Packet
54  * \param Iface Interface
55  * \param Address       Destination IP
56  * \param Protocol      Protocol ID
57  * \param ID    Some random ID number
58  * \param Length        Data Length
59  * \param Data  Packet Data
60  */
61 int IPv4_SendPacket(tInterface *Iface, tIPv4 Address, int Protocol, int ID, int Length, void *Data)
62 {
63         tMacAddr        to = ARP_Resolve4(Iface, Address);
64          int    bufSize = sizeof(tIPv4Header) + Length;
65         char    buf[bufSize];
66         tIPv4Header     *hdr = (void*)buf;
67         
68         memcpy(&hdr->Options[0], Data, Length);
69         hdr->Version = 4;
70         hdr->HeaderLength = sizeof(tIPv4Header)/4;
71         hdr->DiffServices = 0;  // TODO: Check
72         
73         hdr->Reserved = 0;
74         hdr->DontFragment = 0;
75         hdr->MoreFragments = 0;
76         hdr->FragOffLow = 0;
77         hdr->FragOffHi = 0;
78         
79         hdr->TotalLength = htons( bufSize );
80         hdr->Identifcation = htons( ID );       // TODO: Check
81         hdr->TTL = DEFAULT_TTL;
82         hdr->Protocol = Protocol;
83         hdr->HeaderChecksum = 0;        // Will be set later
84         hdr->Source = Iface->IP4.Address;
85         hdr->Destination = Address;
86         hdr->HeaderChecksum = IPv4_Checksum(hdr, sizeof(tIPv4Header));
87         
88         Log_Log("IPv4", "Sending packet to %i.%i.%i.%i",
89                 Address.B[0], Address.B[1], Address.B[2], Address.B[3]);
90         Link_SendPacket(Iface->Adapter, IPV4_ETHERNET_ID, to, bufSize, buf);
91         return 1;
92 }
93
94 /**
95  * \fn void IPv4_int_GetPacket(tInterface *Adapter, tMacAddr From, int Length, void *Buffer)
96  * \brief Process an IPv4 Packet
97  */
98 void IPv4_int_GetPacket(tAdapter *Adapter, tMacAddr From, int Length, void *Buffer)
99 {
100         tIPv4Header     *hdr = Buffer;
101         tInterface      *iface;
102         Uint8   *data;
103          int    dataLength;
104         if(Length < sizeof(tIPv4Header))        return;
105         
106         //Log_Log("IPv4", "Version = %i", hdr->Version);
107         //Log_Log("IPv4", "HeaderLength = %i", hdr->HeaderLength);
108         //Log_Log("IPv4", "DiffServices = %i", hdr->DiffServices);
109         Log_Log("IPv4", "TotalLength = %i", ntohs(hdr->TotalLength) );
110         //Log_Log("IPv4", "Identifcation = %i", ntohs(hdr->Identifcation) );
111         //Log_Log("IPv4", "TTL = %i", hdr->TTL );
112         Log_Log("IPv4", "Protocol = %i", hdr->Protocol );
113         //Log_Log("IPv4", "HeaderChecksum = 0x%x", ntohs(hdr->HeaderChecksum) );
114         Log_Log("IPv4", "Source = %i.%i.%i.%i",
115                 hdr->Source.B[0], hdr->Source.B[1], hdr->Source.B[2], hdr->Source.B[3] );
116         Log_Log("IPv4", "Destination = %i.%i.%i.%i",
117                 hdr->Destination.B[0], hdr->Destination.B[1],
118                 hdr->Destination.B[2], hdr->Destination.B[3] );
119         
120         // Check that the version IS IPv4
121         if(hdr->Version != 4) {
122                 Log_Log("IPv4", "hdr->Version(%i) != 4", hdr->Version);
123                 return;
124         }
125         
126         // Check Header checksum
127         //TODO
128         
129         // Check Packet length
130         if( ntohs(hdr->TotalLength) > Length) {
131                 Log_Log("IPv4", "hdr->TotalLength(%i) > Length(%i)", ntohs(hdr->TotalLength), Length);
132                 return;
133         }
134         
135         // Get Interface (allowing broadcasts)
136         iface = IPv4_GetInterface(Adapter, hdr->Destination, 1);
137         if(!iface) {
138                 Log_Log("IPv4", "Ignoring Packet (Not for us)");
139                 return; // Not for us? Well, let's ignore it
140         }
141         
142         // Defragment
143         //TODO
144         
145         dataLength = ntohs(hdr->TotalLength) - sizeof(tIPv4Header);
146         data = &hdr->Options[0];
147         
148         // Send it on
149         if( gaIPv4_Callbacks[hdr->Protocol] )
150                 gaIPv4_Callbacks[hdr->Protocol] (iface, &hdr->Source, dataLength, data);
151         else
152                 Log_Log("IPv4", "Unknown Protocol %i", hdr->Protocol);
153 }
154
155 /**
156  * \fn tInterface *IPv4_GetInterface(tAdapter *Adapter, tIPv4 Address)
157  * \brief Searches an adapter for a matching address
158  */
159 tInterface *IPv4_GetInterface(tAdapter *Adapter, tIPv4 Address, int Broadcast)
160 {
161         tInterface      *iface = NULL;
162         Uint32  netmask;
163         Uint32  addr, this;
164         
165         addr = ntohl( Address.L );
166         
167         for( iface = gIP_Interfaces; iface; iface = iface->Next)
168         {
169                 if( iface->Adapter != Adapter ) continue;
170                 if( iface->Type != 4 )  continue;
171                 if( IP4_EQU(Address, iface->IP4.Address) )
172                         return iface;
173                 
174                 if( !Broadcast )        continue;
175                 
176                 this = ntohl( iface->IP4.Address.L );
177                 
178                 // Check for broadcast
179                 netmask = IPv4_Netmask(iface->IP4.SubnetBits);
180                 
181                 //Log("netmask = 0x%08x", netmask);
182                 //Log("addr = 0x%08x", addr);
183                 //Log("this = 0x%08x", this);
184                 //Log("%08x == %08x && %08x == %08x",
185                 //      (addr & netmask), (this & netmask),
186                 //      (addr & ~netmask), (0xFFFFFFFF & ~netmask)
187                 //      );
188                 if( (addr & netmask) == (this & netmask)
189                  && (addr & ~netmask) == (0xFFFFFFFF & ~netmask) )
190                         return iface;
191         }
192         return NULL;
193 }
194
195 /**
196  * \brief Convert a network prefix to a netmask
197  * 
198  * For example /24 will become 255.255.255.0
199  */
200 Uint32 IPv4_Netmask(int FixedBits)
201 {
202         Uint32  ret = 0xFFFFFFFF;
203         ret >>= (32-FixedBits);
204         ret <<= (32-FixedBits);
205         // Returs a little endian netmask
206         return ret;
207 }
208
209 /**
210  * \brief Calculate the IPv4 Checksum
211  */
212 Uint16 IPv4_Checksum(void *Buf, int Size)
213 {
214         Uint16  sum = 0;
215         Uint16  *arr = Buf;
216          int    i;
217         
218         Log("IPv4_Checksum: (%p, %i)", Buf, Size);
219         
220         Size = (Size + 1) >> 1;
221         for(i = 0; i < Size; i++ )
222         {
223                 if((int)sum + arr[i] > 0xFFFF)
224                         sum ++; // Simulate 1's complement
225                 sum += arr[i];
226         }
227         return ~sum ;
228 }
229
230 /**
231  * \brief Sends an ICMP Echo and waits for a reply
232  */
233 int IPv4_Ping(tInterface *Iface, tIPv4 Addr)
234 {
235         return ICMP_Ping(Iface, Addr);
236 }

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