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

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