A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
ipv4-icmp-no-route-test-suite.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2026 Centre Tecnologic de Telecomunicacions de Catalunya (CTTC)
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Gabriel Ferreira <gabrielcarvfer@gmail.com>
7 */
8
9/*
10 * Regression test for @issueid{825} (IP-layer "no route -> ICMP
11 * unreachable"): a router that receives a packet it must forward but for
12 * which it has no matching route must generate an ICMP Destination
13 * Unreachable (type 3) message back toward the source, so that a Ping (or any
14 * other) application on the source sees the error.
15 *
16 * What this test does:
17 * It builds a minimal, fully deterministic two-hop topology
18 *
19 * A (source) ----- R (router, two interfaces) D = 10.9.9.0/24
20 * (no route on R)
21 *
22 * A has a default route pointing at R. R is configured as an IP forwarder but
23 * deliberately has NO route to the destination subnet D (10.9.9.0/24). A
24 * raw ICMP socket (IP protocol number 1) is opened on A to capture any ICMP
25 * error returned by R. A then sends a single packet (via a raw socket) to an
26 * address inside D. R receives the packet, fails to route it, and -- on a
27 * correct IP implementation -- must answer with an ICMP Destination
28 * Unreachable (type 3, code 0 net-unreachable) sourced from R back to A.
29 *
30 * Expected behaviour:
31 * A receives exactly one ICMP Destination Unreachable message from R.
32 */
33
34#include "ns3/boolean.h"
35#include "ns3/iana-internet-protocol-numbers.h"
36#include "ns3/icmpv4.h"
37#include "ns3/inet-socket-address.h"
38#include "ns3/internet-stack-helper.h"
39#include "ns3/ipv4-address.h"
40#include "ns3/ipv4-l3-protocol.h"
41#include "ns3/ipv4-raw-socket-factory.h"
42#include "ns3/ipv4-static-routing-helper.h"
43#include "ns3/ipv4-static-routing.h"
44#include "ns3/ipv4.h"
45#include "ns3/log.h"
46#include "ns3/node.h"
47#include "ns3/packet.h"
48#include "ns3/rng-seed-manager.h"
49#include "ns3/simple-net-device-helper.h"
50#include "ns3/simple-net-device.h"
51#include "ns3/simulator.h"
52#include "ns3/socket-factory.h"
53#include "ns3/socket.h"
54#include "ns3/test.h"
55#include "ns3/uinteger.h"
56
57#include <cstdint>
58
59using namespace ns3;
60
61NS_LOG_COMPONENT_DEFINE("Ipv4IcmpNoRouteTestSuite");
62
63/**
64 * @ingroup internet-test
65 * @ingroup tests
66 *
67 * @brief Regression test for @issueid{825}: a router that cannot forward a
68 * packet (no matching route) must reply to the source with an ICMP
69 * Destination Unreachable (net-unreachable) message.
70 *
71 * Topology:
72 *
73 * A (10.0.0.1) ---- net0 ---- (10.0.0.2) R (10.0.1.1) ---- net1 ---- (dangling)
74 *
75 * - A: default route via R (10.0.0.2).
76 * - R: IP forwarder, has interface routes for 10.0.0.0/24 and 10.0.1.0/24
77 * but NO route for the destination subnet 10.9.9.0/24.
78 * - A opens a raw ICMP socket (protocol 1) to capture returned ICMP errors
79 * and sends a single raw packet to 10.9.9.9 (inside the un-routable D).
80 *
81 * Pre-fix observation: A receives 0 ICMP errors (R drops silently), so the
82 * final assertion fails. Post-fix, A receives one ICMP type-3 message and the
83 * test passes.
84 */
86{
87 public:
89 ~Ipv4IcmpNoRouteTestCase() override;
90
91 private:
92 void DoRun() override;
93
94 /**
95 * @brief Send a single raw IPv4 packet from A toward the un-routable
96 * destination.
97 * @param socket The sending raw socket on node A.
98 * @param dst The (un-routable) destination address inside subnet D.
99 */
100 void SendData(Ptr<Socket> socket, Ipv4Address dst);
101
102 /**
103 * @brief Receive callback for the raw ICMP socket on node A.
104 *
105 * Parses the received packet (full IPv4 datagram delivered by the raw
106 * socket: Ipv4Header followed by Icmpv4Header) and, if it is an ICMP
107 * Destination Unreachable, records it.
108 *
109 * @param socket The receiving raw ICMP socket on node A.
110 */
111 void ReceiveIcmp(Ptr<Socket> socket);
112
113 uint32_t m_icmpUnreachCount{0}; //!< Number of ICMP Dest Unreachable msgs received by A
114 Ipv4Address m_icmpSource; //!< Source address of the received ICMP error
115};
116
118 : TestCase("ICMP net-unreachable on no-route forwarding failure (issue #825)")
119{
120}
121
125
126void
128{
129 // A plain payload; the IP layer adds the Ipv4Header with protocol 1 (ICMP)
130 // because the raw socket was created with the ICMP protocol number.
132 Address realTo = InetSocketAddress(dst, 0);
133 socket->SendTo(p, 0, realTo);
134 NS_LOG_DEBUG("A sent " << p->GetSize() << " bytes to un-routable " << dst);
135}
136
137void
139{
140 Address from;
141 Ptr<Packet> p = socket->RecvFrom(0xffffffff, 0, from);
142 if (!p)
143 {
144 return;
145 }
146
147 // The raw socket delivers the full IPv4 datagram: Ipv4Header + ICMP.
148 Ipv4Header ipv4;
149 if (p->GetSize() < ipv4.GetSerializedSize())
150 {
151 return;
152 }
153 p->RemoveHeader(ipv4);
154
155 if (ipv4.GetProtocol() != iana::internetprotocolnumbers::ICMP)
156 {
157 return;
158 }
159
160 Icmpv4Header icmp;
161 p->RemoveHeader(icmp);
162
164 {
166 m_icmpSource = ipv4.GetSource();
167 NS_LOG_DEBUG("A received ICMP Destination Unreachable from " << ipv4.GetSource());
168 }
169}
170
171void
173{
174 // Make the run fully deterministic.
177
178 // Node A (source) and node R (router with two interfaces).
181
182 NodeContainer aAndR(nodeA, nodeR);
183
184 // net0: A <-> R (subnet 10.0.0.0/24).
185 SimpleNetDeviceHelper net0Helper;
186 net0Helper.SetNetDevicePointToPointMode(true);
187 NetDeviceContainer net0 = net0Helper.Install(aAndR);
188
189 // net1: a second interface on R only (subnet 10.0.1.0/24). R has a dangling
190 // device on this link so that it owns two interfaces, but has no route to
191 // the destination subnet D = 10.9.9.0/24.
192 SimpleNetDeviceHelper net1Helper;
193 net1Helper.SetNetDevicePointToPointMode(true);
194 NetDeviceContainer net1 = net1Helper.Install(NodeContainer(nodeR));
195
196 InternetStackHelper internet;
197 internet.Install(aAndR);
198
199 Ptr<Ipv4> ipv4;
200 uint32_t ifIndex;
201
202 // --- Node A addressing: single interface 10.0.0.1/24 ---
203 ipv4 = nodeA->GetObject<Ipv4>();
204 ifIndex = ipv4->AddInterface(net0.Get(0));
205 ipv4->AddAddress(ifIndex,
206 Ipv4InterfaceAddress(Ipv4Address("10.0.0.1"), Ipv4Mask("255.255.255.0")));
207 ipv4->SetUp(ifIndex);
208
209 // --- Node R addressing: 10.0.0.2/24 (toward A) and 10.0.1.1/24 (net1) ---
210 ipv4 = nodeR->GetObject<Ipv4>();
211 uint32_t rIfToA = ipv4->AddInterface(net0.Get(1));
212 ipv4->AddAddress(rIfToA,
213 Ipv4InterfaceAddress(Ipv4Address("10.0.0.2"), Ipv4Mask("255.255.255.0")));
214 ipv4->SetUp(rIfToA);
215
216 uint32_t rIfNet1 = ipv4->AddInterface(net1.Get(0));
217 ipv4->AddAddress(rIfNet1,
218 Ipv4InterfaceAddress(Ipv4Address("10.0.1.1"), Ipv4Mask("255.255.255.0")));
219 ipv4->SetUp(rIfNet1);
220
221 // R must act as an IP forwarder for RouteInput()/RouteInputError() to run.
222 ipv4->SetAttribute("IpForward", BooleanValue(true));
223
224 // --- Routing ---
225 Ipv4StaticRoutingHelper staticRoutingHelper;
226
227 // A: default route via R (10.0.0.2) out of A's only interface.
228 Ptr<Ipv4StaticRouting> aRouting =
229 staticRoutingHelper.GetStaticRouting(nodeA->GetObject<Ipv4>());
230 aRouting->SetDefaultRoute(Ipv4Address("10.0.0.2"), 1);
231
232 // R: intentionally NO route to the destination subnet D (10.9.9.0/24).
233 // R only has its directly-connected interface routes (added automatically
234 // when the interfaces are brought up). Forwarding a packet destined to D
235 // therefore fails to route on R, exercising RouteInputError().
236
237 // --- Raw ICMP socket on A to capture any returned ICMP error ---
238 Ptr<SocketFactory> rawFactory = nodeA->GetObject<Ipv4RawSocketFactory>();
239 Ptr<Socket> aIcmpSocket = rawFactory->CreateSocket();
240 aIcmpSocket->SetAttribute("Protocol", UintegerValue(iana::internetprotocolnumbers::ICMP));
242 0,
243 "Failed to bind the raw ICMP socket on A");
244 aIcmpSocket->SetRecvCallback(MakeCallback(&Ipv4IcmpNoRouteTestCase::ReceiveIcmp, this));
245
246 // --- Sending socket on A: a raw ICMP socket to inject a forwarded packet
247 // toward the un-routable destination 10.9.9.9 (inside subnet D). ---
248 Ptr<Socket> aSendSocket = rawFactory->CreateSocket();
249 aSendSocket->SetAttribute("Protocol", UintegerValue(iana::internetprotocolnumbers::ICMP));
250 NS_TEST_EXPECT_MSG_EQ(aSendSocket->Bind(InetSocketAddress(Ipv4Address("10.0.0.1"), 0)),
251 0,
252 "Failed to bind the raw sending socket on A");
253
254 Simulator::ScheduleWithContext(nodeA->GetId(),
255 Seconds(1.0),
257 this,
258 aSendSocket,
259 Ipv4Address("10.9.9.9"));
260
264
265 // A receives exactly one ICMP Destination Unreachable message, sourced
266 // from the router R (10.0.0.2).
268 1,
269 "Source A should receive exactly one ICMP Destination Unreachable "
270 "(net-unreachable) from the router on a no-route forwarding failure "
271 "(issue #825)");
272
274 Ipv4Address("10.0.0.2"),
275 "The ICMP Destination Unreachable should be sourced from the router R "
276 "(10.0.0.2)");
277}
278
279/**
280 * @ingroup internet-test
281 * @ingroup tests
282 *
283 * @brief Test suite for the ICMP no-route regression (@issueid{825}).
284 */
286{
287 public:
293};
294
295static Ipv4IcmpNoRouteTestSuite g_ipv4IcmpNoRouteTestSuite; //!< Static variable for test
296 //!< initialization
Regression test for Issue 825: a router that cannot forward a packet (no matching route) must reply t...
uint32_t m_icmpUnreachCount
Number of ICMP Dest Unreachable msgs received by A.
Ipv4Address m_icmpSource
Source address of the received ICMP error.
void SendData(Ptr< Socket > socket, Ipv4Address dst)
Send a single raw IPv4 packet from A toward the un-routable destination.
void DoRun() override
Implementation to actually run this TestCase.
void ReceiveIcmp(Ptr< Socket > socket)
Receive callback for the raw ICMP socket on node A.
Test suite for the ICMP no-route regression (Issue 825).
a polymophic address class
Definition address.h:114
Base class for all the ICMP packet headers.
Definition icmpv4.h:32
uint8_t GetType() const
Get ICMP type.
Definition icmpv4.cc:136
an Inet address class
aggregate IP/TCP/UDP functionality to existing Nodes.
Ipv4 addresses are stored in host order in this class.
static Ipv4Address GetAny()
Packet header for IPv4.
Definition ipv4-header.h:23
Access to the IPv4 forwarding table, interfaces, and configuration.
Definition ipv4.h:69
a class to store IPv4 address information on an interface
a class to represent an Ipv4 address mask
API to create RAW socket instances.
Helper class that adds ns3::Ipv4StaticRouting objects.
Ptr< Ipv4StaticRouting > GetStaticRouting(Ptr< Ipv4 > ipv4) const
Try and find the static routing protocol as either the main routing protocol or in the list of routin...
holds a vector of ns3::NetDevice pointers
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr<NetDevice> stored in this container at a given index.
keep track of a set of node pointers.
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:70
static void SetRun(uint64_t run)
Set the run number of simulation.
static void SetSeed(uint32_t seed)
Set the seed.
build a set of SimpleNetDevice objects
void SetNetDevicePointToPointMode(bool pointToPointMode)
SimpleNetDevice is Broadcast capable and ARP needing.
NetDeviceContainer Install(Ptr< Node > node) const
This method creates an ns3::SimpleChannel with the attributes configured by SimpleNetDeviceHelper::Se...
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:125
static void ScheduleWithContext(uint32_t context, const Time &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Definition simulator.h:597
static void Run()
Run the simulation.
Definition simulator.cc:161
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition simulator.cc:169
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition test.cc:296
@ QUICK
Fast test.
Definition test.h:1057
TestCase(const TestCase &)=delete
Caller graph was not generated because of its size.
Type
Type of test.
Definition test.h:1271
@ UNIT
This test suite implements a Unit Test.
Definition test.h:1273
TestSuite(std::string name, Type type=Type::UNIT)
Construct a new test suite.
Definition test.cc:494
Hold an unsigned integer type.
Definition uinteger.h:34
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition callback.h:690
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:201
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition log.h:267
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:627
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:492
#define NS_TEST_EXPECT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report if not.
Definition test.h:240
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1313
static Ipv4IcmpNoRouteTestSuite g_ipv4IcmpNoRouteTestSuite
Static variable for test initialization.
constexpr uint8_t ICMP
Internet Control Message for IPV4 [RFC792].
Every class exported by the ns3 library is enclosed in the ns3 namespace.