Modules/USB - Working on USB support again
[tpg/acess2.git] / Modules / IPStack / ipv4.c
index 69f029c..d16cc97 100644 (file)
@@ -21,7 +21,7 @@ extern tMacAddr       ARP_Resolve4(tInterface *Interface, tIPv4 Address);
 void   IPv4_int_GetPacket(tAdapter *Interface, tMacAddr From, int Length, void *Buffer);
 tInterface     *IPv4_GetInterface(tAdapter *Adapter, tIPv4 Address, int Broadcast);
 Uint32 IPv4_Netmask(int FixedBits);
-Uint16 IPv4_Checksum(const void *Buf, int Size);
+Uint16 IPv4_Checksum(const void *Buf, size_t Length);
  int   IPv4_Ping(tInterface *Iface, tIPv4 Addr);
 
 // === GLOBALS ===
@@ -64,13 +64,15 @@ int IPv4_RegisterCallback(int ID, tIPCallback Callback)
 int IPv4_SendPacket(tInterface *Iface, tIPv4 Address, int Protocol, int ID, int Length, const void *Data)
 {
        tMacAddr        to = ARP_Resolve4(Iface, Address);
-       const tMacAddr  zero = {{0,0,0,0,0,0}};
         int    bufSize = sizeof(tIPv4Header) + Length;
        char    buf[bufSize];
        tIPv4Header     *hdr = (void*)buf;
         int    ret;
        
-       if( MAC_EQU(to, zero) ) {
+       if( MAC_EQU(to, cMAC_ZERO) ) {
+               // No route to host
+               Log_Notice("IPv4", "No route to host %i.%i.%i.%i",
+                       Address.B[0], Address.B[1], Address.B[2], Address.B[3]);
                return 0;
        }
        
@@ -79,8 +81,9 @@ int IPv4_SendPacket(tInterface *Iface, tIPv4 Address, int Protocol, int ID, int
                4, (tIPv4*)Iface->Address, &Address,
                Protocol, 0,
                Length, Data);
-       if(ret != 0) {
+       if(ret > 0) {
                // Just drop it (with an error)
+               Log_Notice("IPv4", "Firewall dropped packet");
                return 0;
        }
        
@@ -102,7 +105,7 @@ int IPv4_SendPacket(tInterface *Iface, tIPv4 Address, int Protocol, int ID, int
        hdr->HeaderChecksum = 0;        // Will be set later
        hdr->Source = *(tIPv4*)Iface->Address;
        hdr->Destination = Address;
-       hdr->HeaderChecksum = htons(IPv4_Checksum(hdr, sizeof(tIPv4Header)));
+       hdr->HeaderChecksum = htons( IPv4_Checksum(hdr, sizeof(tIPv4Header)) );
        
        Log_Log("IPv4", "Sending packet to %i.%i.%i.%i",
                Address.B[0], Address.B[1], Address.B[2], Address.B[3]);
@@ -121,7 +124,7 @@ void IPv4_int_GetPacket(tAdapter *Adapter, tMacAddr From, int Length, void *Buff
        Uint8   *data;
         int    dataLength;
         int    ret;
-        
+       
        if(Length < sizeof(tIPv4Header))        return;
        
        #if 0
@@ -200,8 +203,12 @@ void IPv4_int_GetPacket(tAdapter *Adapter, tMacAddr From, int Length, void *Buff
        case 1:
                Log_Debug("IPv4", "Silently dropping packet");
                return ;
+       case -1:
+               // Bad rule
+               break ;
        // Unknown, silent drop
        default:
+               Log_Warning("IPv4", "Unknown firewall rule");
                return ;
        }
        
@@ -222,6 +229,8 @@ void IPv4_int_GetPacket(tAdapter *Adapter, tMacAddr From, int Length, void *Buff
                
                rt = IPStack_FindRoute(4, NULL, &hdr->Destination);     // Get the route (gets the interface)
                to = ARP_Resolve4(rt->Interface, hdr->Destination);     // Resolve address
+               if( MAC_EQU(to, cMAC_ZERO) )
+                       return ;
                
                // Send packet
                Log_Log("IPv4", "Forwarding packet to %i.%i.%i.%i (via %i.%i.%i.%i)",
@@ -302,18 +311,19 @@ Uint32 IPv4_Netmask(int FixedBits)
  * 
  * One's complement sum of all 16-bit words (bitwise inverted)
  */
-Uint16 IPv4_Checksum(const void *Buf, int Size)
+Uint16 IPv4_Checksum(const void *Buf, size_t Length)
 {
+       const Uint16    *words = Buf;
        Uint32  sum = 0;
-       const Uint16    *arr = Buf;
         int    i;
        
-       Size = (Size + 1) >> 1; // 16-bit word count
-       for(i = 0; i < Size; i++ )
+       // Sum all whole words
+       for(i = 0; i < Length/2; i++ )
        {
-               Uint16  val = ntohs(arr[i]);
-               sum += val;
+               sum += ntohs(words[i]);
        }
+       if( Length & 1 )
+               sum += ntohs( words[i] & 0xFF );
        
        // Apply one's complement
        while (sum >> 16)

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