From d5f9dc201b9d74ba86ffce24694c5190d57f32f0 Mon Sep 17 00:00:00 2001 From: Stefano Avallone Date: Sun, 26 Nov 2023 18:59:37 +0100 Subject: [PATCH] internet: Set the TOS for ICMP Echo Requests/Replies --- src/internet-apps/model/ping.cc | 11 +++++++++-- src/internet-apps/model/ping.h | 2 ++ src/internet/model/icmpv4-l4-protocol.cc | 10 +++++++--- src/internet/model/icmpv4-l4-protocol.h | 4 +++- 4 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/internet-apps/model/ping.cc b/src/internet-apps/model/ping.cc index 96af7c094..05cb2a069 100644 --- a/src/internet-apps/model/ping.cc +++ b/src/internet-apps/model/ping.cc @@ -103,6 +103,12 @@ Ping::GetTypeId() TimeValue(Seconds(1)), MakeTimeAccessor(&Ping::m_timeout), MakeTimeChecker()) + .AddAttribute("Tos", + "The Type of Service used to send the ICMP Echo Requests. " + "All 8 bits of the TOS byte are set (including ECN bits).", + UintegerValue(0), + MakeUintegerAccessor(&Ping::m_tos), + MakeUintegerChecker()) .AddTraceSource("Tx", "The sequence number and ICMP echo response packet.", MakeTraceSourceAccessor(&Ping::m_txTrace), @@ -455,8 +461,9 @@ Ping::Send() header.EnableChecksum(); } p->AddHeader(header); - returnValue = - m_socket->SendTo(p, 0, InetSocketAddress(Ipv4Address::ConvertFrom(m_destination), 0)); + auto dest = InetSocketAddress(Ipv4Address::ConvertFrom(m_destination), 0); + dest.SetTos(m_tos); + returnValue = m_socket->SendTo(p, 0, dest); } else { diff --git a/src/internet-apps/model/ping.h b/src/internet-apps/model/ping.h index aaf80ea72..e6d23cf36 100644 --- a/src/internet-apps/model/ping.h +++ b/src/internet-apps/model/ping.h @@ -213,6 +213,8 @@ class Ping : public Application uint32_t m_size{56}; /// The socket we send packets from Ptr m_socket; + /// The Type of Service carried by ICMP ECHOs + uint8_t m_tos; /// ICMP ECHO sequence number uint16_t m_seq{0}; /// Callbacks for tracing the packet Tx events diff --git a/src/internet/model/icmpv4-l4-protocol.cc b/src/internet/model/icmpv4-l4-protocol.cc index db64443a1..8f823b795 100644 --- a/src/internet/model/icmpv4-l4-protocol.cc +++ b/src/internet/model/icmpv4-l4-protocol.cc @@ -222,14 +222,18 @@ void Icmpv4L4Protocol::HandleEcho(Ptr p, Icmpv4Header header, Ipv4Address source, - Ipv4Address destination) + Ipv4Address destination, + uint8_t tos) { - NS_LOG_FUNCTION(this << p << header << source << destination); + NS_LOG_FUNCTION(this << p << header << source << destination << tos); Ptr reply = Create(); Icmpv4Echo echo; p->RemoveHeader(echo); reply->AddHeader(echo); + SocketIpTosTag ipTosTag; + ipTosTag.SetTos(tos); + reply->ReplacePacketTag(ipTosTag); SendMessage(reply, destination, source, Icmpv4Header::ICMPV4_ECHO_REPLY, 0, nullptr); } @@ -327,7 +331,7 @@ Icmpv4L4Protocol::Receive(Ptr p, } } } - HandleEcho(p, icmp, header.GetSource(), dst); + HandleEcho(p, icmp, header.GetSource(), dst, header.GetTos()); break; } case Icmpv4Header::ICMPV4_DEST_UNREACH: diff --git a/src/internet/model/icmpv4-l4-protocol.h b/src/internet/model/icmpv4-l4-protocol.h index 18a3439f6..6f558bb0b 100644 --- a/src/internet/model/icmpv4-l4-protocol.h +++ b/src/internet/model/icmpv4-l4-protocol.h @@ -143,11 +143,13 @@ class Icmpv4L4Protocol : public IpL4Protocol * \param header the IP header * \param source the source address * \param destination the destination address + * \param tos the type of service */ void HandleEcho(Ptr p, Icmpv4Header header, Ipv4Address source, - Ipv4Address destination); + Ipv4Address destination, + uint8_t tos); /** * \brief Handles an incoming ICMP Destination Unreachable packet * \param p the packet