From 52c7ab63e25ed11901a05b03ad69e75fa94312dc Mon Sep 17 00:00:00 2001 From: Tim Young Date: Sat, 10 Dec 2016 16:47:14 -0800 Subject: [PATCH] Fixed bug with network loop puzzle. That caused issues with tSourceIP. Needed to set tSourceIP when forwarding / routing, and coming out of a VPN --- EduNetworkBuilder/DeviceConfig.Designer.cs | 2 +- EduNetworkBuilder/DeviceConfig.cs | 4 +-- EduNetworkBuilder/EduNetworkBuilder.csproj | 5 ++-- EduNetworkBuilder/NB.cs | 1 + EduNetworkBuilder/Network.cs | 25 +++++++++++++++-- EduNetworkBuilder/NetworkCard.cs | 13 +++++++-- EduNetworkBuilder/NetworkDevice.cs | 32 ++++++++++++---------- EduNetworkBuilder/NetworkInterface.cs | 12 ++++++-- EduNetworkBuilder/Packet.cs | 2 +- 9 files changed, 69 insertions(+), 27 deletions(-) diff --git a/EduNetworkBuilder/DeviceConfig.Designer.cs b/EduNetworkBuilder/DeviceConfig.Designer.cs index 8a764f6..3ab4ba3 100644 --- a/EduNetworkBuilder/DeviceConfig.Designer.cs +++ b/EduNetworkBuilder/DeviceConfig.Designer.cs @@ -185,7 +185,7 @@ this.lbArpTable.ItemHeight = 17; this.lbArpTable.Location = new System.Drawing.Point(15, 269); this.lbArpTable.Name = "lbArpTable"; - this.lbArpTable.Size = new System.Drawing.Size(326, 89); + this.lbArpTable.Size = new System.Drawing.Size(349, 89); this.lbArpTable.TabIndex = 13; // // btnGateway diff --git a/EduNetworkBuilder/DeviceConfig.cs b/EduNetworkBuilder/DeviceConfig.cs index e2423d8..65baecd 100644 --- a/EduNetworkBuilder/DeviceConfig.cs +++ b/EduNetworkBuilder/DeviceConfig.cs @@ -211,10 +211,10 @@ namespace EduNetworkBuilder } lbArpTable.Items.Clear(); - lbArpTable.Items.Add("Arp Table MAC\tIPAddress"); + lbArpTable.Items.Add("Arp Table MAC\tIPAddress\tIF"); foreach(ArpEntry ae in ndCLonedItem.GetArps()) { - lbArpTable.Items.Add(ae.MACAddress + "\t" + ae.IPAddr); + lbArpTable.Items.Add(ae.MACAddress + "\t" + ae.IPAddr + "\t" + ae.NicOnWhichItIsFound.NicName); } //The IP Addresses for the given nic diff --git a/EduNetworkBuilder/EduNetworkBuilder.csproj b/EduNetworkBuilder/EduNetworkBuilder.csproj index a1956ee..fe73bfd 100644 --- a/EduNetworkBuilder/EduNetworkBuilder.csproj +++ b/EduNetworkBuilder/EduNetworkBuilder.csproj @@ -57,10 +57,10 @@ Resources\NBIco.ico - FC016CC0B0D70B0A6ABDABD9CDB267055A8FCCA3 + B9F30C1A5500E918477D84AE79FD91D601D3860E - EduNetworkBuilder_1_TemporaryKey.pfx + EduNetworkBuilder_2_TemporaryKey.pfx true @@ -214,6 +214,7 @@ VLANConfig.cs + SettingsSingleFileGenerator diff --git a/EduNetworkBuilder/NB.cs b/EduNetworkBuilder/NB.cs index aa1cf58..c7de61d 100644 --- a/EduNetworkBuilder/NB.cs +++ b/EduNetworkBuilder/NB.cs @@ -294,6 +294,7 @@ namespace EduNetworkBuilder public static int WirelessMaxSuccessfulLink = 100; //Packets will drop after this distance public static int WirelessReconnectDistance = 70; //Try to find a closer AP if we are this far out. public static int UntaggedVLAN = -1; //If the packet is not tagged. + public static int MaxPacketsBeforeOptimizing = 50; /// /// Find the global random number generator. diff --git a/EduNetworkBuilder/Network.cs b/EduNetworkBuilder/Network.cs index f75c99c..9ee4fa1 100644 --- a/EduNetworkBuilder/Network.cs +++ b/EduNetworkBuilder/Network.cs @@ -941,8 +941,29 @@ namespace EduNetworkBuilder public void addPacket(Packet toadd) { - if(toadd != null && !myPackets.Contains(toadd)) - myPackets.Add(toadd); + if (toadd != null && !myPackets.Contains(toadd)) + { + if (myPackets.Count > NB.MaxPacketsBeforeOptimizing) + { + bool foundit = false; + foreach(Packet pkt in myPackets) + { + if(pkt.MyType == toadd.MyType && pkt.WhereAmI == toadd.WhereAmI && + (pkt.sourceIP != null && pkt.sourceIP.Equals(toadd.sourceIP)) && + (pkt.destIP != null && pkt.destIP.Equals(toadd.destIP))) + { + foundit = true; + break; + } + } + if (!foundit) + myPackets.Add(toadd); + } + else + { + myPackets.Add(toadd); + } + } } public int CountPackets(PacketType WhatType) diff --git a/EduNetworkBuilder/NetworkCard.cs b/EduNetworkBuilder/NetworkCard.cs index 5a38f09..e7e8e04 100644 --- a/EduNetworkBuilder/NetworkCard.cs +++ b/EduNetworkBuilder/NetworkCard.cs @@ -464,8 +464,8 @@ namespace EduNetworkBuilder case NicType.eth: case NicType.wlan: //see if it the packet dest is local to this nic - if (tPacket.MyType == PacketType.dhcp_answer) - Console.WriteLine("DHCP Answer"); + //if (tPacket.MyType == PacketType.dhcp_answer) + // Console.WriteLine("DHCP Answer"); foreach (NetworkInterface nf in interfaces.ToList()) { if (tPacket.InboundNic != null && tPacket.InboundNic == this && @@ -477,6 +477,8 @@ namespace EduNetworkBuilder continue; //only send out arp requests on local networks nPacket = new Packet(tPacket);//Creates a new packet but sets isfresh=false nPacket.OutboundIF = nf; + if(nf != null) + nPacket.TsourceIP = nf.myIP; nPacket.InboundInterface = tPacket.InboundInterface; nf.ProcessOutboundPacket(nPacket); if (nPacket.MyStatus == PacketStatus.finished || nPacket.MyStatus == PacketStatus.finished_failed || nPacket.MyStatus == PacketStatus.finished_ok) @@ -532,6 +534,8 @@ namespace EduNetworkBuilder nPacket = new Packet(tPacket);//Creates a new packet but sets isfresh=false nPacket.OutboundIF = nf; nPacket.InboundInterface = tPacket.InboundInterface; + if (nf != null) + nPacket.TsourceIP = nf.myIP; nf.ProcessOutboundPacket(nPacket); if (nPacket.MyStatus == PacketStatus.finished || nPacket.MyStatus == PacketStatus.finished_failed || nPacket.MyStatus == PacketStatus.finished_ok) @@ -595,6 +599,8 @@ namespace EduNetworkBuilder tPacket.MyStatus = PacketStatus.encapsulated; tPacket.TsourceIP = nf.myIP; tPacket.destMAC = WhereFrom.LookupArpFromIP(tPacket.OutboundIP.GetIPString); + if (nf != null) + tPacket.TsourceIP = nf.myIP; //We need to make a new, tunnel packet if (myNicType == NicType.tun) @@ -672,7 +678,8 @@ namespace EduNetworkBuilder if (nPacket.TsourceIP == null) nPacket.TsourceIP = WhereFrom.HubManagementIP(); nl = myNet.GetLinkFromID(ConnectedLink); - if (nl == null) break; + if (nl == null) + break; nPacket.StartOnLink(nl, WhereFrom); //This sends the packet down the link. myNet.addPacket(nPacket); if (tPacket.isFresh) diff --git a/EduNetworkBuilder/NetworkDevice.cs b/EduNetworkBuilder/NetworkDevice.cs index 2199afe..96c37a8 100644 --- a/EduNetworkBuilder/NetworkDevice.cs +++ b/EduNetworkBuilder/NetworkDevice.cs @@ -1348,13 +1348,6 @@ namespace EduNetworkBuilder if (tPacket.TsourceIP != null && !tPacket.TsourceIP.IsLocal(tPacket.destIP) && !HasBroadcastAddress(tPacket.destIP)) return ResponseToPacket.none; foreach(IPConnectionEntry ipc in IPConnections) { - if(ipc.What == PacketType.dhcp_request) - { - //Console.WriteLine(NB.LeftPad(hostname) + ": packet=" + tPacket.MyType.ToString()); - //Console.WriteLine(" " + NB.LeftPad(hostname) + ": d:" + ipc.destIP.GetIPString + " s:" + tPacket.sourceIP.GetIPString); - //if (HasBroadcastAddress(ipc.destIP)) - // Console.WriteLine(" Broadcast"); - } if (ipc.destIP.GetIP == tPacket.sourceIP.GetIP || HasBroadcastAddress(ipc.destIP)) { if (ipc.What == PacketType.arp_request && tPacket.MyType == PacketType.arp_answer) @@ -1444,15 +1437,16 @@ namespace EduNetworkBuilder bool MacAddressMatch = HasMac(tPacket.destMAC); //It is an exact match if it is coming from a local source. - bool ExactMatch = HasIPAddress(tPacket.destIP) && (tPacket.TsourceIP == null || tPacket.TsourceIP.IsLocal(tPacket.destIP)); + bool HasIp = HasIPAddress(tPacket.destIP); + bool ExactMatch = HasIp && (tPacket.TsourceIP == null || tPacket.TsourceIP.IsLocal(tPacket.destIP)); bool LocalMatch = LocalMatches(tPacket); bool BroadcastMatch = HasBroadcastAddress(tPacket.destIP); bool NeedsRouting = MacAddressMatch && (!ExactMatch && !BroadcastMatch); if(ExactMatch || BroadcastMatch || LocalMatch) { - //Change this. Need a new ProcessArrival - if(tPacket.MyType != PacketType.dhcp_answer) //If it is a match, it should be handled elsewhere + //Change this. Need a new ProcessArrival + if (tPacket.MyType != PacketType.dhcp_answer) //If it is a match, it should be handled elsewhere ProcessArrival(tPacket); if (ExactMatch && tPacket.isFinshed()) return; @@ -1483,15 +1477,18 @@ namespace EduNetworkBuilder { //It is a local packet. Let it go out the interface it needs to go out tPacket.OutboundIP = tPacket.destIP; + tPacket.TsourceIP = null; } else if(tPacket.destIP.GetIPString != NB.BroadcastIPString) { //it needs to go to a gateway. Set the next destination is the GW tPacket.OutboundIP = dest; + tPacket.TsourceIP = null; } else { tPacket.OutboundIP = tPacket.destIP; + tPacket.TsourceIP = null; } } @@ -1508,6 +1505,7 @@ namespace EduNetworkBuilder if (arp.MACAddress == tPacket.destMAC) { dNic = NicFromID(arp.NicOnWhichItIsFound); + break; } //if (arp.MACAddress == tPacket.sourceMAC) //{ @@ -1529,7 +1527,7 @@ namespace EduNetworkBuilder // Resume processing at the nic level, not the device level when the arp comes back int count = 0; bool PacketSentOutWAN = false; - foreach(NetworkCard nic in NICs) + foreach (NetworkCard nic in NICs) { NicType NT = nic.GetNicType; //bridges and waps are basically switches @@ -1579,7 +1577,9 @@ namespace EduNetworkBuilder case NicType.port: case NicType.wport: if (PacketSentOutWAN) + { continue; //do not send packets out of ports if they have been sent out a WAN nic + } NetworkCard tdNic = dNic; if (myType == NetworkComponentType.wrouter) { @@ -1656,10 +1656,11 @@ namespace EduNetworkBuilder tPacket.Tracking.AddMessage(DebugLevel.info,this,NB.Translate("ND_ProcessArrival_Arrived")); tPacket.Tracking.Status = NB.Translate("ND_ProcessArrival_Arrived"); //If the encryption matches, we succeed. Otherwise, we fail - + tPacket.MyStatus = PacketStatus.finished_ok; //It stops here nPacket = tPacket.payloadPacket; NetworkCard nc = LocalNic(nPacket.TsourceIP, true); + nPacket.TsourceIP = null; if (nc != null) { if (nPacket != null) @@ -2471,6 +2472,7 @@ namespace EduNetworkBuilder return; } } + if (!ForwardsPackets()) { if (tPacket.MyType == PacketType.dhcp_request && !isDHCPServer) @@ -2872,8 +2874,10 @@ namespace EduNetworkBuilder { if(fwr.Source == inIF && fwr.Destination == outIF) { - if (fwr.Action == FirewallRuleType.Allow) return true; - if (fwr.Action == FirewallRuleType.Drop) return false; + if (fwr.Action == FirewallRuleType.Allow) + return true; + if (fwr.Action == FirewallRuleType.Drop) + return false; } } return true; diff --git a/EduNetworkBuilder/NetworkInterface.cs b/EduNetworkBuilder/NetworkInterface.cs index ec192d6..11efc1e 100644 --- a/EduNetworkBuilder/NetworkInterface.cs +++ b/EduNetworkBuilder/NetworkInterface.cs @@ -203,6 +203,13 @@ namespace EduNetworkBuilder NetworkDevice HD = theNet.GetDeviceFromID(AttachedToHostNic); if (HD == null) return; //Oops! Something went wrong. string hostname = HD.hostname; + //added to fix the issue with vpns and pings - 11-09-2016 + //Only do this on non-ports? + NetworkCard nic = HD.NicFromID(AttachedToHostNic); + if(nic != null && nic.GetNicType != NicType.port && nic.GetNicType != NicType.wport) + tPacket.TsourceIP = myIP; + if(tPacket.TsourceIP == null || tPacket.TsourceIP.GetIPString == NB.ZeroIPString) + tPacket.TsourceIP = myIP; if (What == VLANTagType.Forbidden) { @@ -224,8 +231,6 @@ namespace EduNetworkBuilder //We actually do not do anything. The tag remains intact. tPacket.VLANID = VI.ID; } - //added to fix the issue with vpns and pings - 11-09-2016 - tPacket.TsourceIP = myIP; } private VLANInfo IncomingVLAN(int ID) @@ -324,6 +329,9 @@ namespace EduNetworkBuilder if (tPacket.destIP != null && myIP.IsLocal(tPacket.destIP)) isgood = true; if (myIP.NetworkAddress == myIP.GetIP) isgood = true; + Network MyNet = NB.GetNetwork(); + NetworkDevice ND = MyNet.GetDeviceFromID(AttachedToHostNic); + tPacket.InboundInterface = this; if(isgood) diff --git a/EduNetworkBuilder/Packet.cs b/EduNetworkBuilder/Packet.cs index 4aa2fee..6fdea84 100644 --- a/EduNetworkBuilder/Packet.cs +++ b/EduNetworkBuilder/Packet.cs @@ -42,7 +42,7 @@ namespace EduNetworkBuilder public int health = 100; public IPAddress sourceIP; public IPAddress OriginalDestIP; - public IPAddress TsourceIP; + public IPAddress TsourceIP; //This is the local link source ip. It should go along with the local MAC address public IPAddress destIP; public string sourceMAC; public string destMAC;