Multiple IPStack Related changes (and other bugfixes)
[tpg/acess2.git] / Modules / IPStack / arp.c
index d9f837c..9e92414 100644 (file)
@@ -1,18 +1,22 @@
 /*
  * Acess2 IP Stack
  * - Address Resolution Protocol
+ * - Part of the IPv4 protocol
  */
 #define DEBUG  0
 #include "ipstack.h"
 #include "arp.h"
 #include "link.h"
 
+#define ARPv6  0
 #define        ARP_CACHE_SIZE  64
 #define        ARP_MAX_AGE             (60*60*1000)    // 1Hr
 
 // === IMPORTS ===
 extern tInterface      *IPv4_GetInterface(tAdapter *Adapter, tIPv4 Address, int Broadcast);
+#if ARPv6
 extern tInterface      *IPv6_GetInterface(tAdapter *Adapter, tIPv6 Address, int Broadcast);
+#endif
 
 // === PROTOTYPES ===
  int   ARP_Initialise();
@@ -27,7 +31,8 @@ struct sARP_Cache4 {
        Sint64  LastUsed;
 }      *gaARP_Cache4;
  int   giARP_Cache4Space;
-tSpinlock      glARP_Cache4;
+tMutex glARP_Cache4;
+#if ARPv6
 struct sARP_Cache6 {
        tIPv6   IP;
        tMacAddr        MAC;
@@ -35,7 +40,8 @@ struct sARP_Cache6 {
        Sint64  LastUsed;
 }      *gaARP_Cache6;
  int   giARP_Cache6Space;
-tSpinlock      glARP_Cache6;
+tMutex glARP_Cache6;
+#endif
  int   giARP_LastUpdateID = 0;
 
 // === CODE ===
@@ -49,9 +55,11 @@ int ARP_Initialise()
        memset( gaARP_Cache4, 0, ARP_CACHE_SIZE * sizeof(struct sARP_Cache4) );
        giARP_Cache4Space = ARP_CACHE_SIZE;
        
+       #if ARPv6
        gaARP_Cache6 = malloc( ARP_CACHE_SIZE * sizeof(struct sARP_Cache6) );
        memset( gaARP_Cache6, 0, ARP_CACHE_SIZE * sizeof(struct sARP_Cache6) );
        giARP_Cache6Space = ARP_CACHE_SIZE;
+       #endif
        
        Link_RegisterType(0x0806, ARP_int_GetPacket);
        return 1;
@@ -68,7 +76,21 @@ tMacAddr ARP_Resolve4(tInterface *Interface, tIPv4 Address)
        
        ENTER("pInterface xAddress", Interface, Address);
        
-       LOCK( &glARP_Cache4 );
+       // Check routing tables
+       {
+               tRoute  *route = IPStack_FindRoute(4, Interface, &Address);
+               if( route ) {
+                       // If the next hop is defined, use it
+                       // - 0.0.0.0 as the next hop means "no next hop / direct"
+                       if( ((tIPv4*)route->NextHop)->L != 0 ) {
+                               // Recursion: see /Recursion/
+                               return ARP_Resolve4(Interface, *(tIPv4*)route->NextHop);
+                       }
+               }
+       }
+       
+       // Check ARP Cache
+       Mutex_Acquire( &glARP_Cache4 );
        for( i = 0; i < giARP_Cache4Space; i++ )
        {
                if(gaARP_Cache4[i].IP.L != Address.L)   continue;
@@ -76,7 +98,7 @@ tMacAddr ARP_Resolve4(tInterface *Interface, tIPv4 Address)
                // Check if the entry needs to be refreshed
                if( now() - gaARP_Cache4[i].LastUpdate > ARP_MAX_AGE )  break;
                
-               RELEASE( &glARP_Cache4 );
+               Mutex_Release( &glARP_Cache4 );
                LOG("Return %x:%x:%x:%x:%x:%x",
                        gaARP_Cache4[i].MAC.B[0], gaARP_Cache4[i].MAC.B[1],
                        gaARP_Cache4[i].MAC.B[2], gaARP_Cache4[i].MAC.B[3],
@@ -85,7 +107,7 @@ tMacAddr ARP_Resolve4(tInterface *Interface, tIPv4 Address)
                LEAVE('-');
                return gaARP_Cache4[i].MAC;
        }
-       RELEASE( &glARP_Cache4 );
+       Mutex_Release( &glARP_Cache4 );
        
        lastID = giARP_LastUpdateID;
        
@@ -99,7 +121,7 @@ tMacAddr ARP_Resolve4(tInterface *Interface, tIPv4 Address)
        req.SWSize = 4;
        req.Request = htons(1);
        req.SourceMac = Interface->Adapter->MacAddr;
-       req.SourceIP = Interface->IP4.Address;
+       req.SourceIP = *(tIPv4*)Interface->Address;
        req.DestMac = cMAC_BROADCAST;
        req.DestIP = Address;
        
@@ -112,15 +134,15 @@ tMacAddr ARP_Resolve4(tInterface *Interface, tIPv4 Address)
                while(lastID == giARP_LastUpdateID)     Threads_Yield();
                lastID = giARP_LastUpdateID;
                
-               LOCK( &glARP_Cache4 );
+               Mutex_Acquire( &glARP_Cache4 );
                for( i = 0; i < giARP_Cache4Space; i++ )
                {
                        if(gaARP_Cache4[i].IP.L != Address.L)   continue;
                        
-                       RELEASE( &glARP_Cache4 );
+                       Mutex_Release( &glARP_Cache4 );
                        return gaARP_Cache4[i].MAC;
                }
-               RELEASE( &glARP_Cache4 );
+               Mutex_Release( &glARP_Cache4 );
        }
 }
 
@@ -134,7 +156,7 @@ void ARP_UpdateCache4(tIPv4 SWAddr, tMacAddr HWAddr)
         int    oldest = 0;
        
        // Find an entry for the IP address in the cache
-       LOCK(&glARP_Cache4);
+       Mutex_Acquire(&glARP_Cache4);
        for( i = giARP_Cache4Space; i--; )
        {
                if(gaARP_Cache4[oldest].LastUpdate > gaARP_Cache4[i].LastUpdate) {
@@ -161,9 +183,10 @@ void ARP_UpdateCache4(tIPv4 SWAddr, tMacAddr HWAddr)
        gaARP_Cache4[i].MAC = HWAddr;
        gaARP_Cache4[i].LastUpdate = now();
        giARP_LastUpdateID ++;
-       RELEASE(&glARP_Cache4);
+       Mutex_Release(&glARP_Cache4);
 }
 
+#if ARPv6
 /**
  * \brief Updates the ARP Cache entry for an IPv6 Address
  */
@@ -174,7 +197,7 @@ void ARP_UpdateCache6(tIPv6 SWAddr, tMacAddr HWAddr)
         int    oldest = 0;
        
        // Find an entry for the MAC address in the cache
-       LOCK(&glARP_Cache6);
+       Mutex_Acquire(&glARP_Cache6);
        for( i = giARP_Cache6Space; i--; )
        {
                if(gaARP_Cache6[oldest].LastUpdate > gaARP_Cache6[i].LastUpdate) {
@@ -195,8 +218,9 @@ void ARP_UpdateCache6(tIPv6 SWAddr, tMacAddr HWAddr)
        gaARP_Cache6[i].IP = SWAddr;
        gaARP_Cache6[i].LastUpdate = now();
        giARP_LastUpdateID ++;
-       RELEASE(&glARP_Cache6);
+       Mutex_Release(&glARP_Cache6);
 }
+#endif
 
 /**
  * \fn void ARP_int_GetPacket(tAdapter *Adapter, tMacAddr From, int Length, void *Buffer)
@@ -205,16 +229,18 @@ void ARP_UpdateCache6(tIPv6 SWAddr, tMacAddr HWAddr)
 void ARP_int_GetPacket(tAdapter *Adapter, tMacAddr From, int Length, void *Buffer)
 {
        tArpRequest4    *req4 = Buffer;
+       #if ARPv6
        tArpRequest6    *req6 = Buffer;
+       #endif
        tInterface      *iface;
        
        // Sanity Check Packet
-       if( Length < sizeof(tArpRequest4) ) {
+       if( Length < (int)sizeof(tArpRequest4) ) {
                Log_Log("ARP", "Recieved undersized packet");
                return ;
        }
        if( ntohs(req4->Type) != 0x0800 ) {
-               Log_Log("ARP", "Recieved a packet with a bad type 0x%x", ntohs(req4->Type));
+               Log_Log("ARP", "Recieved a packet with a bad type (0x%x)", ntohs(req4->Type));
                return ;
        }
        if( req4->HWSize != 6 ) {
@@ -248,14 +274,13 @@ void ARP_int_GetPacket(tAdapter *Adapter, tMacAddr From, int Length, void *Buffe
                                req4->SourceMac.B[0], req4->SourceMac.B[1],
                                req4->SourceMac.B[2], req4->SourceMac.B[3],
                                req4->SourceMac.B[4], req4->SourceMac.B[5]);
-                       Log_Debug("ARP", "to MAC %02x:%02x:%02x:%02x:%02x:%02x",
-                               req4->DestMac.B[0], req4->DestMac.B[1],
-                               req4->DestMac.B[2], req4->DestMac.B[3],
-                               req4->DestMac.B[4], req4->DestMac.B[5]);
-                       Log_Debug("ARP", "ARP Request IPv4 Address %i.%i.%i.%i",
+                       //Log_Debug("ARP", "to MAC %02x:%02x:%02x:%02x:%02x:%02x",
+                       //      req4->DestMac.B[0], req4->DestMac.B[1],
+                       //      req4->DestMac.B[2], req4->DestMac.B[3],
+                       //      req4->DestMac.B[4], req4->DestMac.B[5]);
+                       Log_Debug("ARP", "ARP Request IPv4 Address %i.%i.%i.%i from %i.%i.%i.%i",
                                req4->DestIP.B[0], req4->DestIP.B[1], req4->DestIP.B[2],
-                               req4->DestIP.B[3]);
-                       Log_Debug("ARP", "from %i.%i.%i.%i",
+                               req4->DestIP.B[3],
                                req4->SourceIP.B[0], req4->SourceIP.B[1],
                                req4->SourceIP.B[2], req4->SourceIP.B[3]);
                        iface = IPv4_GetInterface(Adapter, req4->DestIP, 0);
@@ -266,33 +291,42 @@ void ARP_int_GetPacket(tAdapter *Adapter, tMacAddr From, int Length, void *Buffe
                                
                                req4->DestIP = req4->SourceIP;
                                req4->DestMac = req4->SourceMac;
-                               req4->SourceIP = iface->IP4.Address;
+                               req4->SourceIP = *(tIPv4*)iface->Address;;
                                req4->SourceMac = Adapter->MacAddr;
                                req4->Request = htons(2);
-                               Log_Debug("ARP", "Hey, That's us!");
-                               Log_Debug("ARP", "Sending back %02x:%02x:%02x:%02x:%02x:%02x",
+                               Log_Debug("ARP", "Sending back us (%02x:%02x:%02x:%02x:%02x:%02x)",
                                        req4->SourceMac.B[0], req4->SourceMac.B[1],
                                        req4->SourceMac.B[2], req4->SourceMac.B[3],
                                        req4->SourceMac.B[4], req4->SourceMac.B[5]);
                                Link_SendPacket(Adapter, 0x0806, req4->DestMac, sizeof(tArpRequest4), req4);
                        }
                        break;
+               #if ARPv6
                case 6:
-                       if( Length < sizeof(tArpRequest6) ) {
-                               Log_Debug("ARP", "Recieved undersized packet (IPv6)");
+                       if( Length < (int)sizeof(tArpRequest6) ) {
+                               Log_Log("ARP", "Recieved undersized packet (IPv6)");
                                return ;
                        }
+                       Log_Debug("ARP", "ARP Request IPv6 Address %08x:%08x:%08x:%08x",
+                               ntohl(req6->DestIP.L[0]), ntohl(req6->DestIP.L[1]),
+                               ntohl(req6->DestIP.L[2]), ntohl(req6->DestIP.L[3])
+                               );
                        iface = IPv6_GetInterface(Adapter, req6->DestIP, 0);
                        if( iface )
                        {
                                req6->DestIP = req6->SourceIP;
                                req6->DestMac = req6->SourceMac;
-                               req6->SourceIP = iface->IP6.Address;
+                               req6->SourceIP = *(tIPv6*)iface->Address;
                                req6->SourceMac = Adapter->MacAddr;
                                req6->Request = htons(2);
+                               Log_Debug("ARP", "Sending back us (%02x:%02x:%02x:%02x:%02x:%02x)",
+                                       req4->SourceMac.B[0], req4->SourceMac.B[1],
+                                       req4->SourceMac.B[2], req4->SourceMac.B[3],
+                                       req4->SourceMac.B[4], req4->SourceMac.B[5]);
                                Link_SendPacket(Adapter, 0x0806, req6->DestMac, sizeof(tArpRequest6), req6);
                        }
                        break;
+               #endif
                default:
                        Log_Debug("ARP", "Unknown Protocol Address size (%i)", req4->SWSize);
                        return ;
@@ -307,13 +341,15 @@ void ARP_int_GetPacket(tAdapter *Adapter, tMacAddr From, int Length, void *Buffe
                case 4:
                        ARP_UpdateCache4( req4->SourceIP, From );
                        break;
+               #if ARPv6
                case 6:
-                       if( Length < sizeof(tArpRequest6) ) {
+                       if( Length < (int)sizeof(tArpRequest6) ) {
                                Log_Debug("ARP", "Recieved undersized packet (IPv6)");
                                return ;
                        }
                        ARP_UpdateCache6( req6->SourceIP, From );
                        break;
+               #endif
                default:
                        Log_Debug("ARP", "Unknown Protocol Address size (%i)", req4->SWSize);
                        return ;
@@ -322,7 +358,7 @@ void ARP_int_GetPacket(tAdapter *Adapter, tMacAddr From, int Length, void *Buffe
                break;
        
        default:
-               Warning("[ARP  ] Unknown Request ID %i", ntohs(req4->Request));
+               Log_Warning("ARP", "Unknown Request ID %i", ntohs(req4->Request));
                break;
        }
 }

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