ENSC 427 : Communication Networks
Spring 2021
Project Team 10
Analysis of 5G network under DDoS attack using ns-3 simulator


Home Page


Source Code

/*

 *    (ue)
 *      \
 *       \
 *         ------- (eNodeB) ------(pgw) ------(remote host)/server
 *                 / |  \
 *                /  |   \
 *               /   |    \
 *             (B0),(B2)...(Bn)/attackers

Network topology and DDoS attacks are created by our group memebers with the reference to mmwave-simple-epc.cc file created by M. Miozzo and N. Baldo (Modified by: M. Mezzavilla, S. Dutta, R. Ford, M. Zhang) in [1] and the NS3 Cybersecurity Simulations created by Saket Upadhyay ([9] from reference list above).

Author: Namsakhi Kumar, Kwok Liang Lee, Edwin Tam

*/
#include "ns3/mmwave-helper.h"
#include "ns3/nstime.h"
#include "ns3/epc-helper.h"
#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/ipv4-global-routing-helper.h"
#include "ns3/internet-module.h"
#include "ns3/mobility-module.h"
#include "ns3/applications-module.h"
#include "ns3/point-to-point-helper.h"
#include "ns3/config-store.h"
#include "ns3/mmwave-point-to-point-epc-helper.h"

#include "ns3/netanim-module.h"
#include <sstream>
#include <string>

using namespace std;
using namespace ns3;
using namespace mmwave;

NS_LOG_COMPONENT_DEFINE ("MmWaveDDoS");


int main(int argc, char* argv[])
{
	uint16_t numEnb = 1; //Number of EnB (Base Station)
	uint16_t numUe = 1; //Number of Ue (User Equipment)
	uint16_t numAtt = 10; //Number of Attacker
	double simTime = 1.5; //Simulation Time
	bool EnableAttack = true; //Enable DDoS Attack
	bool harqEnabled = true; //Hybrid ARQ

	//Creating Nodes for Topology
	NodeContainer ueNodes; //for Ue nodes
	NodeContainer attNodes; //for attacker nodes
	NodeContainer enbNodes; //for EnB nodes

	ueNodes.Create(numUe); //Creating numUe amount of ue nodes
	attNodes.Create(numAtt); //Creating numAtt amount of attacker nodes
	enbNodes.Create(numEnb); //Creating numEnb amount of enb nodes

	Ptr mmwaveHelper = CreateObject(); //Creating mmwave object for using MmWave functions
	Ptr epcHelper = CreateObject(); //Creating Envolved Packet Core object(EPC) for using LTE functions
	mmwaveHelper->SetEpcHelper(epcHelper);	//Link LTE to MmWave
	mmwaveHelper->SetHarqEnabled(harqEnabled); //To enable IP address and MAC address conversion (Setting ARQ)

	Ptr pgw = epcHelper->GetPgwNode(); //Using EPC function to create a Packet Data Network Gateway(PGW) node

	NodeContainer remoteHostContainer; //Create container for remotehost/server
	remoteHostContainer.Create(1); //Creating 1 remote host node
	Ptr remoteHost = remoteHostContainer.Get(0); //Set the first Remote Host Container to be a remoteHost node

	//Create the internet
	InternetStackHelper internet;
	internet.Install(remoteHostContainer); //Install internet to the remote host such that the remote host has access to the internet
	internet.Install(ueNodes); //Install internet to User Equipments such that User Eqiupments have access to the internet
	internet.Install(attNodes); //Install internet to Attacker such that Attacker have access to the internet


	//Install mobility for each nodes
	//Install mobility model for Enb node 
	Ptr enbPositionAlloc = CreateObject();
	enbPositionAlloc->Add(Vector(40, 20, 0.0));//Set enb location
	MobilityHelper enbmobility;
	enbmobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
	enbmobility.SetPositionAllocator(enbPositionAlloc);
	enbmobility.Install(enbNodes);//Install enbnode to its location

	//Install mobility model for Ue nodes
	MobilityHelper uemobility;
	Ptr uePositionAlloc = CreateObject();
	for (unsigned i = 0; i < numUe; i++)
	{
		uePositionAlloc->Add(Vector(10, 10 * i, 0.0));//Location of the ue node
	}
	uemobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
	uemobility.SetPositionAllocator(uePositionAlloc);
	uemobility.Install(ueNodes);//Install the ue node to its location

	//Install mobility model for Attacker nodes
	MobilityHelper attackmobility;
	Ptr attackPositionAlloc = CreateObject();
	for (unsigned i = 0; i < numAtt; i++)
	{
		attackPositionAlloc->Add(Vector(20, 5 * i, 0.0));//Location of the attackers
	}
	attackmobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
	attackmobility.SetPositionAllocator(attackPositionAlloc);
	attackmobility.Install(attNodes);//Install attack nodes to its location

	//Install mobility model for pgw node
	MobilityHelper pgwmobility;
	Ptr pgwPositionAlloc = CreateObject();
	pgwPositionAlloc->Add(Vector(40, 50, 0.0));//Set location for pgw node
	pgwmobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
	pgwmobility.SetPositionAllocator(pgwPositionAlloc);
	pgwmobility.Install(pgw);//Install pgw node to its location

	//Install mobility model for remote host 
	MobilityHelper rmhmobility;
	Ptr rmhPositionAlloc = CreateObject();
	rmhPositionAlloc->Add(Vector(40, 70, 0.0));//Set remotehost location
	rmhmobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
	rmhmobility.SetPositionAllocator(rmhPositionAlloc);
	rmhmobility.Install(remoteHost);//Install remotehost node to its location

	//Install mMWave Device to the EnB nodes, Ue nodes, and Attacker nodes, so Ue and Attacker will be wireless
	NetDeviceContainer enbmmWaveDevs = mmwaveHelper->InstallEnbDevice(enbNodes);
	NetDeviceContainer uemmWaveDevs = mmwaveHelper->InstallUeDevice(ueNodes);
	NetDeviceContainer attmmWaveDevs = mmwaveHelper->InstallUeDevice(attNodes);

	//Create the Transmission line for remoteHost to pgw.*
	PointToPointHelper p2ph;
	p2ph.SetDeviceAttribute("DataRate", DataRateValue(DataRate("1Gb/s")));//Data rate of the transmission line
	p2ph.SetDeviceAttribute("Mtu", UintegerValue(1500));//Maximum transmission unit of each packet
	p2ph.SetChannelAttribute("Delay", TimeValue(MicroSeconds(100.0)));//Delay of the link

	NetDeviceContainer internetDevices = p2ph.Install(pgw, remoteHost); // Connecting PGW to RemoteHost 
	Ipv4AddressHelper ipv4h;
	ipv4h.SetBase("1.0.0.0", "255.0.0.0");
	Ipv4InterfaceContainer internetIpIfaces = ipv4h.Assign(internetDevices); //Assigning IP address to PGW and RemoteHost

	Ipv4Address remoteHostAddr = internetIpIfaces.GetAddress(1); //Get RemoteHost IP address
	Ipv4StaticRoutingHelper ipv4RoutingHelper; //Setting up ipv4
	Ptr remoteHostStaticRouting = ipv4RoutingHelper.GetStaticRouting(remoteHost->GetObject());
	remoteHostStaticRouting->AddNetworkRouteTo(Ipv4Address("7.0.0.0"), Ipv4Mask("255.0.0.0"), 1);

	//Install the IP stack on the UEs and attackers
	Ipv4InterfaceContainer ueIpIface, attIpIface;
	ueIpIface = epcHelper->AssignUeIpv4Address(NetDeviceContainer(uemmWaveDevs));
	attIpIface = epcHelper->AssignUeIpv4Address(NetDeviceContainer(attmmWaveDevs));

	// Assign IP address to UEs 
	for (uint32_t u = 0; u < ueNodes.GetN(); ++u)
	{
		Ptr ueNodePtr = ueNodes.Get(u);
		// Set the default gateway for the Ue
		Ptr ueStaticRouting = ipv4RoutingHelper.GetStaticRouting(ueNodePtr->GetObject());
		ueStaticRouting->SetDefaultRoute(epcHelper->GetUeDefaultGatewayAddress(), 1);
	}

	// Assign IP address to attackers
	for (uint32_t u = 0; u < attNodes.GetN(); ++u)
	{
		Ptr attNodePtr = attNodes.Get(u);
		// Set the default gateway for the Attacker
		Ptr attStaticRouting = ipv4RoutingHelper.GetStaticRouting(attNodePtr->GetObject());
		attStaticRouting->SetDefaultRoute(epcHelper->GetUeDefaultGatewayAddress(), 1);
	}

	mmwaveHelper->AttachToClosestEnb(uemmWaveDevs, enbmmWaveDevs); //Attach Ue to Enb node by using mmwave (wireless)
	mmwaveHelper->AttachToClosestEnb(attmmWaveDevs, enbmmWaveDevs); //Attach attackers to Enb node by using mmwave (wireless)

	//Ipv4GlobalRoutingHelper::PopulateRoutingTables();
	//Application 
	uint32_t UDPport = 3001;
	uint32_t TCPport = 3001; //Set Socket port to 3001 (tcp)
	#define DDOS_RATE "1Gb/s"
	uint32_t PacketSize = 1000;

	//Setting for DDoS
	if (EnableAttack)
	{
		// DDoS Application Behaviour
		OnOffHelper onoff("ns3::UdpSocketFactory", Address(InetSocketAddress(remoteHostAddr, UDPport)));//Setting up UDP socket for application
//On time = 1 & off time = 0 below will set traffic into constant bit rate
		onoff.SetAttribute("OnTime", StringValue("ns3::ConstantRandomVariable[Constant=1]"));
		onoff.SetAttribute("OffTime", StringValue("ns3::ConstantRandomVariable[Constant=0]"));
		onoff.SetConstantRate(DataRate(DDOS_RATE), PacketSize); //Setting up a sending rate

		ApplicationContainer onOffApp[numAtt]; //Setting up the number of containers for each attacker

		//Install application in all bots
		for (int k = 0; k < numAtt; ++k)
		{
			onOffApp[k] = onoff.Install(attNodes.Get(k)); //Socket link the K-th attacker to the remoteHost
			onOffApp[k].Start(Seconds(0.5)); //Attack Transmission Time Start
			onOffApp[k].Stop(Seconds(simTime)); //Attack Transmission Time End
		}
	}

      // Normal Application Behaviour
	OnOffHelper TCPonoff("ns3::TcpSocketFactory", Address(InetSocketAddress(remoteHostAddr, TCPport)));//Setting up UDP socket for application

	TCPonoff.SetConstantRate(DataRate("0.5Gb/s"),1000); //Setting up a sending rate

//On time = 1 & off time = 0 below will set traffic into constant bit rate
	TCPonoff.SetAttribute("OnTime", StringValue("ns3::ConstantRandomVariable[Constant=1]"));

	TCPonoff.SetAttribute("OffTime", StringValue("ns3::ConstantRandomVariable[Constant=0]"));

	ApplicationContainer TCPonOffApp[numUe]; //Setting up the number of containers for each user

	for (int k = 0; k < numUe; ++k)
	{
			TCPonOffApp[k] = TCPonoff.Install(ueNodes.Get(k)); //Socket link the K-th attacker to the remoteHost
			TCPonOffApp[k].Start(Seconds(0.1)); //Attack Transmission Time Start
			TCPonOffApp[k].Stop(Seconds(simTime)); //Attack Transmission Time End
	}

//TCP sink on receiver side
	PacketSinkHelper TCPsink("ns3::TcpSocketFactory", InetSocketAddress(Ipv4Address::GetAny(), TCPport)); //the first parameter is the protocol that is used to receive traffic, and used to create sockets for the applications

	ApplicationContainer TCPsinkApp = TCPsink.Install(remoteHost); //Socket link to remoteHost

	TCPsinkApp.Start(Seconds(0.1)); //UDP Transmission Time Start
	TCPsinkApp.Stop(Seconds(simTime)); //UDP Transmission Time End

	
//UDP sink on receiver side
PacketSinkHelper UDPsink("ns3::UdpSocketFactory", InetSocketAddress(Ipv4Address::GetAny(), UDPport)); //the first parameter is the protocol that is used to receive traffic, and used to create sockets for the applications

	ApplicationContainer UDPsinkApp = UDPsink.Install(remoteHost); //Socket link to remoteHost

	UDPsinkApp.Start(Seconds(0.1)); //UDP Transmission Time Start
	UDPsinkApp.Stop(Seconds(simTime)); //UDP Transmission Time End

	//Trace file for each node
mmwaveHelper->EnableTraces();
	p2ph.EnablePcapAll("Trace_file");


	//NetAnim setup and nodes labelling
	AnimationInterface anim("NetAnime_file.xml");

anim.UpdateNodeDescription (pgw->GetId(), "PGW");
	anim.UpdateNodeDescription(remoteHost->GetId(), "SERVER");

	for(uint32_t i = 0; i < numAtt ; i++)
	{
		anim.UpdateNodeDescription(attNodes.Get(i)->GetId(), "Attacker");

	}

	anim.UpdateNodeDescription(enbNodes.Get(0)->GetId(), "EnB");
	anim.UpdateNodeDescription(ueNodes.Get(0)->GetId(), "Ue");

	Simulator::Stop(Seconds(simTime));

	//Run the Simulation
	Simulator::Run();
	Simulator::Destroy();
	return 0;

}