From: John Hodge Date: Sun, 23 Jan 2011 15:08:11 +0000 (+0800) Subject: IPStack - Heaps of network fixes X-Git-Tag: rel0.07~21 X-Git-Url: https://git.ucc.asn.au/?a=commitdiff_plain;h=23096bbffa94688e90db5045d124dcdfe83a132e;hp=9143c184873d0b55444dc1c1084f3e9f3f2614bf;p=tpg%2Facess2.git IPStack - Heaps of network fixes - Also reduced verbosity --- diff --git a/Modules/IPStack/arp.c b/Modules/IPStack/arp.c index 35532f6f..82382635 100644 --- a/Modules/IPStack/arp.c +++ b/Modules/IPStack/arp.c @@ -272,33 +272,25 @@ void ARP_int_GetPacket(tAdapter *Adapter, tMacAddr From, int Length, void *Buffe } #endif - Log_Debug("ARP", "Request ID %i", ntohs(req4->Request)); - switch( ntohs(req4->Request) ) { case 1: // You want my IP? - Log_Debug("ARP", "ARP Request Address class %i", req4->SWSize); // Check what type of IP it is switch( req4->SWSize ) { case 4: - Log_Debug("ARP", "From MAC %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]); - //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], req4->SourceIP.B[0], req4->SourceIP.B[1], req4->SourceIP.B[2], req4->SourceIP.B[3]); + Log_Debug("ARP", " from MAC %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]); iface = IPv4_GetInterface(Adapter, req4->DestIP, 0); if( iface ) { - Log_Debug("ARP", "Caching sender's IP Address"); ARP_UpdateCache4(req4->SourceIP, req4->SourceMac); req4->DestIP = req4->SourceIP; diff --git a/Modules/IPStack/icmp.c b/Modules/IPStack/icmp.c index 9f671752..de3a9db5 100644 --- a/Modules/IPStack/icmp.c +++ b/Modules/IPStack/icmp.c @@ -37,23 +37,22 @@ void ICMP_GetPacket(tInterface *Interface, void *Address, int Length, void *Buff { tICMPHeader *hdr = Buffer; - Log("[ICMP ] Length = %i", Length); - Log("[ICMP ] hdr->Type = %i", hdr->Type); - Log("[ICMP ] hdr->Code = %i", hdr->Code); - Log("[ICMP ] hdr->Checksum = 0x%x", ntohs(hdr->Checksum)); - Log("[ICMP ] hdr->ID = 0x%x", ntohs(hdr->ID)); - Log("[ICMP ] hdr->Sequence = 0x%x", ntohs(hdr->Sequence)); + //Log_Debug("ICMPv4", "Length = %i", Length); + Log_Debug("ICMPv4", "hdr->Type, hdr->Code = %i, %i", hdr->Type, hdr->Code); + //Log_Debug("ICMPv4", "hdr->Checksum = 0x%x", ntohs(hdr->Checksum)); + Log_Debug("ICMPv4", "hdr->ID = 0x%x", ntohs(hdr->ID)); + Log_Debug("ICMPv4", "hdr->Sequence = 0x%x", ntohs(hdr->Sequence)); switch(hdr->Type) { // -- 0: Echo Reply case ICMP_ECHOREPLY: if(hdr->Code != 0) { - Warning("[ICMP ] Code == %i for ICMP Echo Reply, should be 0", hdr->Code); + Log_Warning("ICMPv4", "Code == %i for ICMP Echo Reply, should be 0", hdr->Code); return ; } if(hdr->ID != (Uint16)~hdr->Sequence) { - Warning("[ICMP ] ID and Sequence values do not match"); + Log_Warning("ICMPv4", "ID and Sequence values do not match"); //return ; } gICMP_PingSlots[hdr->ID].bArrived = 1; @@ -64,10 +63,10 @@ void ICMP_GetPacket(tInterface *Interface, void *Address, int Length, void *Buff switch(hdr->Code) { case 3: // Port Unreachable - Log("[ICMP ] Destination Unreachable (Port Unreachable)"); + Log_Debug("ICMPv4", "Destination Unreachable (Port Unreachable)"); break; default: - Log("[ICMP ] Destination Unreachable (Code %i)", hdr->Code); + Log_Debug("ICMPv4", "Destination Unreachable (Code %i)", hdr->Code); break; } // IPv4_Unreachable( Interface, hdr->Code, htons(hdr->Length)-sizeof(tICMPHeader), hdr->Data ); @@ -76,14 +75,14 @@ void ICMP_GetPacket(tInterface *Interface, void *Address, int Length, void *Buff // -- 8: Echo Request case ICMP_ECHOREQ: if(hdr->Code != 0) { - Warning("[ICMP ] Code == %i for ICMP Echo Request, should be 0", hdr->Code); + Log_Warning("ICMPv4", "Code == %i for ICMP Echo Request, should be 0", hdr->Code); return ; } - Log("[ICMP ] Replying"); + //Log_Debug("ICMPv4", "Replying"); hdr->Type = ICMP_ECHOREPLY; hdr->Checksum = 0; hdr->Checksum = htons( IPv4_Checksum(hdr, Length) ); - Log("[ICMP ] Checksum = 0x%04x", hdr->Checksum); + //Log_Debug("ICMPv4", "Checksum = 0x%04x", hdr->Checksum); IPv4_SendPacket(Interface, *(tIPv4*)Address, 1, ntohs(hdr->Sequence), Length, hdr); break; default: diff --git a/Modules/IPStack/ipv4.c b/Modules/IPStack/ipv4.c index ad6c891a..3c67c03a 100644 --- a/Modules/IPStack/ipv4.c +++ b/Modules/IPStack/ipv4.c @@ -102,7 +102,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 = 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]); @@ -149,9 +149,9 @@ void IPv4_int_GetPacket(tAdapter *Adapter, tMacAddr From, int Length, void *Buff // Check Header checksum { Uint16 hdrVal, compVal; - hdrVal = hdr->HeaderChecksum; + hdrVal = ntohs(hdr->HeaderChecksum); hdr->HeaderChecksum = 0; - compVal = IPv4_Checksum(hdr, hdr->HeaderLength); + compVal = IPv4_Checksum(hdr, hdr->HeaderLength * 4); if(hdrVal != compVal) { Log_Log("IPv4", "Header checksum fails (%04x != %04x)", hdrVal, compVal); return ; @@ -208,7 +208,7 @@ void IPv4_int_GetPacket(tAdapter *Adapter, tMacAddr From, int Length, void *Buff // Routing if(!iface) { - Log_Debug("IPv4", "Route the packet"); + //Log_Debug("IPv4", "Route the packet"); // TODO: Parse Routing tables and determine where to send it @@ -241,6 +241,7 @@ tInterface *IPv4_GetInterface(tAdapter *Adapter, tIPv4 Address, int Broadcast) { if( iface->Adapter != Adapter ) continue; if( iface->Type != 4 ) continue; + //Log_Debug("IPv4", "%x == %x?\n", addr, ntohl(((tIPv4*)iface->Address)->L)); if( IP4_EQU(Address, *(tIPv4*)iface->Address) ) return iface; @@ -282,7 +283,7 @@ Uint32 IPv4_Netmask(int FixedBits) */ Uint16 IPv4_Checksum(const void *Buf, int Size) { - Uint16 sum = 0; + Uint32 sum = 0; const Uint16 *arr = Buf; int i; @@ -290,11 +291,14 @@ Uint16 IPv4_Checksum(const void *Buf, int Size) for(i = 0; i < Size; i++ ) { Uint16 val = ntohs(arr[i]); - if((int)sum + val > 0xFFFF) - sum ++; // Simulate 1's complement sum += val; } - return ~sum ; + + // Apply one's complement + while (sum >> 16) + sum = (sum & 0xFFFF) + (sum >> 16); + + return ~sum; } /** diff --git a/Modules/IPStack/link.c b/Modules/IPStack/link.c index 88d92db5..4be9d80c 100644 --- a/Modules/IPStack/link.c +++ b/Modules/IPStack/link.c @@ -37,12 +37,10 @@ void Link_RegisterType(Uint16 Type, tPacketCallback Callback) int i; void *tmp; - Type = htons(Type); // Set to network order - for( i = giRegisteredTypes; i -- ; ) { if(gaRegisteredTypes[i].Type == Type) { - Log_Warning("NET", "Attempt to register 0x%x twice", Type); + Log_Warning("Net Link", "Attempt to register 0x%x twice", Type); return ; } // Ooh! Free slot! @@ -54,7 +52,7 @@ void Link_RegisterType(Uint16 Type, tPacketCallback Callback) giRegisteredTypeSpace += 5; tmp = realloc(gaRegisteredTypes, giRegisteredTypeSpace*sizeof(*gaRegisteredTypes)); if(!tmp) { - Log_Warning("NET", + Log_Warning("Net Link", "Out of heap space! (Attempted to allocate %i)", giRegisteredTypeSpace*sizeof(*gaRegisteredTypes) ); @@ -79,7 +77,7 @@ void Link_SendPacket(tAdapter *Adapter, Uint16 Type, tMacAddr To, int Length, vo Uint8 buf[bufSize]; // dynamic stack arrays ftw! tEthernetHeader *hdr = (void*)buf; - Log_Log("NET", "Sending %i bytes to %02x:%02x:%02x:%02x:%02x:%02x (Type 0x%x)", + Log_Log("Net Link", "Sending %i bytes to %02x:%02x:%02x:%02x:%02x:%02x (Type 0x%x)", Length, To.B[0], To.B[1], To.B[2], To.B[3], To.B[4], To.B[5], Type); hdr->Dest = To; @@ -108,7 +106,7 @@ void Link_WatchDevice(tAdapter *Adapter) } if(tid > 0) { - Log_Log("NET", "Watching '%s' using tid %i", Adapter->Device, tid); + Log_Log("Net Link", "Watching '%s' using tid %i", Adapter->Device, tid); return ; } @@ -116,7 +114,7 @@ void Link_WatchDevice(tAdapter *Adapter) Link_InitCRC(); Threads_SetName(Adapter->Device); - Log_Log("NET", "Thread %i watching '%s'", Threads_GetTID(), Adapter->Device); + Log_Log("Net Link", "Thread %i watching '%s'", Threads_GetTID(), Adapter->Device); // Child Thread while(Adapter->DeviceFD != -1) @@ -127,7 +125,7 @@ void Link_WatchDevice(tAdapter *Adapter) Uint32 checksum; // Wait for a packet (Read on a network device is blocking) - Log_Debug("NET", "Waiting on adapter FD#0x%x", Adapter->DeviceFD); + //Log_Debug("NET", "Waiting on adapter FD#0x%x", Adapter->DeviceFD); ret = VFS_Read(Adapter->DeviceFD, MAX_PACKET_SIZE, buf); if(ret == -1) break; @@ -136,25 +134,26 @@ void Link_WatchDevice(tAdapter *Adapter) continue; } - Log_Log("NET", "Packet from %02x:%02x:%02x:%02x:%02x:%02x", + Log_Log("Net Link", + "Packet from %02x:%02x:%02x:%02x:%02x:%02x" + " to %02x:%02x:%02x:%02x:%02x:%02x", hdr->Src.B[0], hdr->Src.B[1], hdr->Src.B[2], - hdr->Src.B[3], hdr->Src.B[4], hdr->Src.B[5] - ); - Log_Log("NET", "to %02x:%02x:%02x:%02x:%02x:%02x", + hdr->Src.B[3], hdr->Src.B[4], hdr->Src.B[5], hdr->Dest.B[0], hdr->Dest.B[1], hdr->Dest.B[2], hdr->Dest.B[3], hdr->Dest.B[4], hdr->Dest.B[5] ); checksum = *(Uint32*)&hdr->Data[ret-sizeof(tEthernetHeader)-4]; - Log_Log("NET", "Checksum 0x%08x", checksum); + //Log_Log("NET", "Checksum 0x%08x", checksum); + // TODO: Check checksum // Check if there is a registered callback for this packet type for( i = giRegisteredTypes; i--; ) { - if(gaRegisteredTypes[i].Type == hdr->Type) break; + if(gaRegisteredTypes[i].Type == ntohs(hdr->Type)) break; } // No? Ignore it if( i == -1 ) { - Log_Log("NET", "Unregistered type 0x%x", ntohs(hdr->Type)); + Log_Log("Net Link", "Unregistered type 0x%x", ntohs(hdr->Type)); continue; } diff --git a/Modules/IPStack/tcp.c b/Modules/IPStack/tcp.c index e42e940c..18150a36 100644 --- a/Modules/IPStack/tcp.c +++ b/Modules/IPStack/tcp.c @@ -84,7 +84,7 @@ void TCP_SendPacket( tTCPConnection *Conn, size_t Length, tTCPHeader *Data ) buf[2] = (htons(Length)<<16) | (6<<8) | 0; Data->Checksum = 0; memcpy( &buf[3], Data, Length ); - Data->Checksum = IPv4_Checksum( buf, buflen ); + Data->Checksum = htons( IPv4_Checksum( buf, buflen ) ); free(buf); IPv4_SendPacket(Conn->Interface, Conn->RemoteIP.v4, IP4PROT_TCP, 0, Length, Data); break;