Skip to content

Commit f898646

Browse files
Scott O'Brienfacebook-github-bot
authored andcommitted
ICMPv4 Interface and Next-Hop Identification
Summary: ## Intent As we're designing the address allocations and routing for DCTypeF and future topologies, we would like to do away with the requirement of any form of IPv4 loopback address on the intermediate routers yet still give us the ability to route and troubleshoot V4 prefixes. Today, we can use RFC5549 to carry V4 prefixes over V6 next-hops, but we keep the V4 address on the loopback for the purpose of traceroute troubleshooting. This diff implements RFC4884 "Extended ICMP to support multi-part messages" and RFC5837 "Extending ICMP for Interface and Next-Hop Identification" behavior such that a TTL Exceeded message will contain the IP Address of the interface on which the datagram arrived. ## Additional ICMP complexity Today, we're following the simple ICMP header and 8 bytes of the datagram payload. This diff changes this to suite RFC5837, will put as much of the original payload as possible without going over the reassembly size This diff enforces extension headers with the IP Address Sub-Object. This should also be configurable and we should implement the other identification mechanisms. This diff makes part of rfc5837 section 4.3 for our use-case to identify the interface AND router: > The interface name MAY be some other human-meaningful name of the interface Reviewed By: phshaikh Differential Revision: D48922763 fbshipit-source-id: 6fd35edf857869c9169750f2d66ef45bc8033ba2
1 parent 19ae887 commit f898646

16 files changed

Lines changed: 931 additions & 32 deletions

‎cmake/AgentPacket.cmake‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ add_library(packet
88
fboss/agent/packet/DHCPv4Packet.cpp
99
fboss/agent/packet/DHCPv6Packet.cpp
1010
fboss/agent/packet/EthHdr.cpp
11+
fboss/agent/packet/ICMPExtHdr.cpp
1112
fboss/agent/packet/ICMPHdr.cpp
1213
fboss/agent/packet/IPv4Hdr.cpp
1314
fboss/agent/packet/IPv6Hdr.cpp

‎fboss/agent/ApplyThriftConfig.cpp‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "fboss/agent/SwSwitch.h"
3030
#include "fboss/agent/SwitchIdScopeResolver.h"
3131
#include "fboss/agent/SwitchInfoUtils.h"
32+
#include "fboss/agent/Utils.h"
3233
#include "fboss/agent/gen-cpp2/switch_config_types.h"
3334
#include "fboss/agent/if/gen-cpp2/mpls_types.h"
3435
#include "fboss/agent/platforms/common/PlatformMapping.h"
@@ -4266,6 +4267,25 @@ shared_ptr<SwitchSettings> ThriftConfigApplier::updateSwitchSettings(
42664267
switchSettingsChange = true;
42674268
}
42684269

4270+
auto oldIcmpV4UnavailableSrcAddress = orig_->getIcmpV4UnavailableSrcAddress();
4271+
auto newIcmpV4UnavailableSrcAddress = cfg_->icmpV4UnavailableSrcAddress();
4272+
if (newIcmpV4UnavailableSrcAddress.has_value()) {
4273+
auto newIcmpV4Address = IPAddressV4(*newIcmpV4UnavailableSrcAddress);
4274+
if (newIcmpV4Address != oldIcmpV4UnavailableSrcAddress) {
4275+
newSwitchSettings->setIcmpV4UnavailableSrcAddress(newIcmpV4Address);
4276+
switchSettingsChange = true;
4277+
}
4278+
}
4279+
4280+
auto oldHostname = orig_->getHostname();
4281+
auto newHostname = cfg_->hostname() && !(*cfg_->hostname()).empty()
4282+
? *cfg_->hostname()
4283+
: getLocalHostname();
4284+
if (oldHostname != newHostname) {
4285+
newSwitchSettings->setHostname(newHostname);
4286+
switchSettingsChange = true;
4287+
}
4288+
42694289
{
42704290
bool qcmChanged = false;
42714291
auto newQcmConfig = updateQcmCfg(&qcmChanged);

‎fboss/agent/IPv4Handler.cpp‎

Lines changed: 117 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
#include <folly/MacAddress.h>
1515
#include <folly/io/Cursor.h>
1616
#include <folly/logging/xlog.h>
17+
#include <cstdint>
18+
#include <memory>
1719
#include "fboss/agent/ArpHandler.h"
1820
#include "fboss/agent/DHCPv4Handler.h"
1921
#include "fboss/agent/FbossError.h"
@@ -25,6 +27,7 @@
2527
#include "fboss/agent/SwitchStats.h"
2628
#include "fboss/agent/TxPacket.h"
2729
#include "fboss/agent/Utils.h"
30+
#include "fboss/agent/packet/ICMPExtHdr.h"
2831
#include "fboss/agent/packet/ICMPHdr.h"
2932
#include "fboss/agent/packet/IPv4Hdr.h"
3033
#include "fboss/agent/packet/UDPHeader.h"
@@ -45,6 +48,11 @@ using folly::io::Cursor;
4548
using folly::io::RWPrivateCursor;
4649
using std::unique_ptr;
4750

51+
DEFINE_bool(
52+
ipv4_ext_headers_enabled,
53+
false,
54+
"Flag to turn on IPv4 extension headers");
55+
4856
namespace facebook::fboss {
4957

5058
template <typename BodyFn>
@@ -87,25 +95,120 @@ void IPv4Handler::sendICMPTimeExceeded(
8795
IPv4Hdr& v4Hdr,
8896
Cursor cursor) {
8997
auto state = sw_->getState();
90-
91-
// payload serialization function
92-
// 4 bytes unused + ipv4 header + 8 bytes payload
93-
auto bodyLength =
94-
ICMPHdr::ICMPV4_UNUSED_LEN + v4Hdr.size() + ICMPHdr::ICMPV4_SENDER_BYTES;
95-
auto serializeBody = [&](RWPrivateCursor* sendCursor) {
96-
sendCursor->writeBE<uint32_t>(0); // unused bytes
97-
v4Hdr.write(sendCursor);
98-
sendCursor->push(cursor.data(), ICMPHdr::ICMPV4_SENDER_BYTES);
99-
};
100-
98+
// Send an ICMP Time Exceeded message with extension headers to show
99+
// the IP address used on the interface.
100+
101+
// Build the interface name sub object
102+
auto srcPort = sw_->getState()->getPort(port);
103+
std::string srcPortName =
104+
sw_->getState()->getHostname() + ":" + srcPort->getName();
105+
auto nameObj =
106+
std::make_unique<ICMPExtIfaceNameSubObject>(srcPortName.c_str());
107+
108+
// Build the IP sub object and find the Src IP
109+
std::unique_ptr<ICMPExtIPSubObject> ipObj = nullptr;
101110
IPAddressV4 srcIp;
102111
try {
103112
srcIp =
104113
getSwitchIntfIP(state, sw_->getState()->getInterfaceIDForPort(port));
114+
ipObj = std::make_unique<ICMPExtIpSubObjectV4>(ICMPExtIpSubObjectV4(srcIp));
115+
105116
} catch (const std::exception& ex) {
106-
srcIp = getAnyIntfIP(state);
117+
// Fall back to using a src IP of any interface on the switch, where if
118+
// none is available, the preconfigured src address
119+
try {
120+
srcIp = getAnyIntfIP(state);
121+
} catch (const FbossError& e) {
122+
srcIp = state->getIcmpV4UnavailableSrcAddress();
123+
}
124+
}
125+
126+
// Build the extension header objects
127+
ICMPExtHdr icmpExtHdr;
128+
129+
ICMPExtInterfaceInformationObject icmpIfaceInformation(
130+
std::move(ipObj), std::move(nameObj));
131+
132+
// Add the interface identification to the ICMP extension headers
133+
icmpExtHdr.objects.push_back(&icmpIfaceInformation);
134+
135+
uint32_t const extHdrLength =
136+
FLAGS_ipv4_ext_headers_enabled ? icmpExtHdr.length() : 0;
137+
138+
// RFC1812 extends the "original datagram" field to contain as many
139+
// octets as possible without causing the ICMP message to exceed the minimum
140+
// IPv4 reassembly buffer size (576 octets)
141+
// ...In order to achieve backwards compatibility, when the ICMP Extension
142+
// Structure is appended to an ICMP message and that ICMP message
143+
// contains an "original datagram" field, the "original datagram" field
144+
// MUST contain at least 128 octets. If the original datagram did not
145+
// contain 128 octets, the "original datagram" field MUST be zero padded
146+
// to 128 octets. (See Section 5.1 for rationale.)
147+
148+
const auto originalDatagramLengthBytes = cursor.length() + v4Hdr.size();
149+
150+
// Unless we're over 128 bytes, zero pad the payload
151+
uint8_t lengthIn32BitWords = 32;
152+
uint8_t paddingBytesRequired = 128 - originalDatagramLengthBytes;
153+
154+
if (originalDatagramLengthBytes >= 128) {
155+
// Find out the length of the payload we can add.
156+
uint16_t maxDatagramLength = 576 // Maximum size of IPv4 payload
157+
- v4Hdr.size() // Length of the IPv4 header
158+
- ICMPHdr::SIZE // UCMP header For fields {type, code, checksum}
159+
- ICMPHdr::ICMPV4_UNUSED_LEN // ICMP fields {unused, length, unused}
160+
- extHdrLength; // Length of any extension headers
161+
162+
// The "original datagram" field MUST be zero padded to the nearest
163+
// 32-bit boundary.
164+
165+
if (originalDatagramLengthBytes > maxDatagramLength) {
166+
lengthIn32BitWords = maxDatagramLength / 4;
167+
paddingBytesRequired = 0;
168+
} else {
169+
// Add 3, then divide by 4 to get the number of words in each datagram.
170+
// adding 3 to round up and account for partial words.
171+
lengthIn32BitWords = (originalDatagramLengthBytes + 3) / 4;
172+
173+
// Figure out how much padding is required here.
174+
paddingBytesRequired =
175+
lengthIn32BitWords * 4 - originalDatagramLengthBytes;
176+
}
107177
}
108178

179+
// Caluclate the size of the payload, excluding first 32 bits of of the header
180+
// which is landed by the createICMPv4Pkt.
181+
uint32_t bodyLength =
182+
ICMPHdr::ICMPV4_UNUSED_LEN // For {unused, length, unused}
183+
+ (lengthIn32BitWords *
184+
4) // Bytes of the payload (which includes original v4 hdr)
185+
+ extHdrLength; // Size of all the extension headers to include
186+
187+
// Lambda function to serialize the body with extension headers
188+
auto serializeBody = [&](RWPrivateCursor* sendCursor) {
189+
// RFC4884, section 4.2. Write the length surrounded by unused
190+
sendCursor->writeBE<uint8_t>(0); // unused bytes
191+
sendCursor->writeBE<uint8_t>(lengthIn32BitWords); // Length
192+
sendCursor->writeBE<uint16_t>(0); // unused bytes
193+
194+
// Write the IP header, and as much of the "original datagram"
195+
// as possible.
196+
v4Hdr.serialize(sendCursor);
197+
sendCursor->push(
198+
cursor.data(),
199+
lengthIn32BitWords * 4 - v4Hdr.size() - paddingBytesRequired);
200+
201+
// Pad the payload to the nearest 32-bit boundary
202+
for (auto i = 0; i < paddingBytesRequired; i++) {
203+
sendCursor->writeBE<uint8_t>(0);
204+
}
205+
206+
// Write the extension header
207+
if (FLAGS_ipv4_ext_headers_enabled) {
208+
icmpExtHdr.serialize(sendCursor);
209+
}
210+
};
211+
109212
auto icmpPkt = createICMPv4Pkt(
110213
sw_,
111214
dst,
@@ -212,8 +315,8 @@ void IPv4Handler::handlePacket(
212315
intf = state->getInterfaces()->getNodeIf(intfID);
213316
} else if (v4Hdr.dstAddr.isLinkLocal()) {
214317
// XXX: Ideally we should scope the limit to Link only. However we are
215-
// using v4 link locals in a special way on Galaxy/6pack which needs because
216-
// of which we do not limit the scope.
318+
// using v4 link locals in a special way on Galaxy/6pack which needs
319+
// because of which we do not limit the scope.
217320
//
218321
// Forward link-local packet directly to corresponding host interface
219322
// provided desAddr is assigned to that interface.

‎fboss/agent/IPv4Handler.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ class Cursor;
2323
}
2424
} // namespace folly
2525

26+
DECLARE_bool(ipv4_ext_headers_enabled);
27+
2628
namespace facebook::fboss {
2729

2830
class RxPacket;

‎fboss/agent/hw/mock/MockTestHandle.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ void MockTestHandle::rxPacket(
2222
std::optional<VlanID> srcVlan) {
2323
auto len = buf->computeChainDataLength();
2424
auto pkt = std::make_unique<MockRxPacket>(std::move(buf));
25+
// The minimum required frame length for ethernet is 64 bytes
2526
pkt->padToLength(std::max((int)len, 68)); // pad to min packet size if needed
2627
pkt->setSrcPort(srcPort);
2728
pkt->setSrcVlan(srcVlan);

‎fboss/agent/packet/ICMPExtHdr.cpp‎

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
// (c) Meta Platforms, Inc. and affiliates. Confidential and proprietary.
2+
3+
#include "fboss/agent/packet/ICMPExtHdr.h"
4+
#include <fboss/agent/packet/PTPHeader.h>
5+
#include <cstdint>
6+
#include <cstring>
7+
#include "fboss/agent/packet/PktUtil.h"
8+
9+
namespace facebook::fboss {
10+
11+
void ICMPExtObjectHdr::serialize(folly::io::RWPrivateCursor* cursor) {
12+
cursor->template writeBE<uint16_t>(length());
13+
cursor->template writeBE<uint8_t>(static_cast<uint8_t>(classNum));
14+
cursor->template writeBE<uint8_t>(cType());
15+
}
16+
17+
uint16_t ICMPExtInterfaceInformationObject::length() const {
18+
uint16_t ret = ICMPExtObjectHdr::SIZE;
19+
if (ifaceCType_.ipAddress) {
20+
ret += this->ipObject_->length();
21+
}
22+
if (ifaceCType_.name) {
23+
ret += this->nameObject_->length();
24+
}
25+
26+
return ret;
27+
}
28+
29+
void ICMPExtInterfaceInformationObject::serialize(
30+
folly::io::RWPrivateCursor* cursor) {
31+
// Write the ICMP Extension Object Header
32+
ICMPExtObjectHdr::serialize(cursor);
33+
34+
// Write all of the Sub Objects we're including in our cType.
35+
// Ordering matters here! See RFC 5837 4.1
36+
// "information order is thus: ifIndex, IP Address Sub-Object, Interface
37+
// Name Sub-Object, and MTU"
38+
if (ifaceCType_.ipAddress) {
39+
this->ipObject_->serialize(cursor);
40+
}
41+
if (ifaceCType_.name) {
42+
this->nameObject_->serialize(cursor);
43+
}
44+
}
45+
46+
void ICMPExtHdr::serialize(folly::io::RWPrivateCursor* cursor) {
47+
// Keep track of where we are to calculate the checksum
48+
folly::io::RWPrivateCursor csumCursor(*cursor);
49+
50+
// Write the version (should be value 2) into the first 4 bits
51+
// of the header
52+
uint8_t version = 2 << 4;
53+
cursor->template write<uint8_t>(version);
54+
55+
// Skip the reserved 12 bits (4 bits were borrowed from the version)
56+
cursor->template write<uint8_t>(0);
57+
58+
// Write a placeholder for the checksum
59+
cursor->write<uint16_t>(0);
60+
61+
for (auto obj : objects) {
62+
obj->serialize(cursor);
63+
}
64+
65+
uint64_t extHdrLength =
66+
cursor->getCurrentPosition() - csumCursor.getCurrentPosition();
67+
68+
// Calcualte the checksum,
69+
uint16_t checksum =
70+
PktUtil::internetChecksum(csumCursor.data(), extHdrLength);
71+
72+
// Skip ahead past the version and reserved bits, re-write the checksum
73+
csumCursor.skip(2);
74+
csumCursor.template writeBE<uint16_t>(checksum);
75+
}
76+
77+
uint32_t ICMPExtHdr::length() const {
78+
uint32_t ret = 4; // 4 bytes for ICMP Ext Header Size
79+
// Add the length of each Extended Object
80+
for (auto obj : objects) {
81+
ret += obj->length();
82+
}
83+
return ret;
84+
}
85+
86+
void ICMPExtIpSubObjectV4::serialize(folly::io::RWPrivateCursor* cursor) {
87+
cursor->template writeBE<uint16_t>(
88+
static_cast<uint16_t>(ICMPExtInterfaceIPSubObjectAFI::
89+
ICMP_EXT_INTERFACE_IP_SUB_OBJECT_AFI_IPV4));
90+
cursor->template writeBE<uint16_t>(0); // Reserved
91+
cursor->template write<uint32_t>(this->ipAddress_.toLong());
92+
}
93+
94+
void ICMPExtIpSubObjectV6::serialize(folly::io::RWPrivateCursor* cursor) {
95+
cursor->template writeBE<uint16_t>(
96+
static_cast<uint16_t>(ICMPExtInterfaceIPSubObjectAFI::
97+
ICMP_EXT_INTERFACE_IP_SUB_OBJECT_AFI_IPV6));
98+
cursor->template writeBE<uint16_t>(0); // Reserved
99+
// IPv6 address
100+
cursor->push(this->ipAddress_.bytes(), 16);
101+
}
102+
103+
ICMPExtIfaceNameSubObject::ICMPExtIfaceNameSubObject(const char* name) {
104+
strncpy(this->ifaceName_, name, sizeof(this->ifaceName_) - 1);
105+
}
106+
107+
uint32_t ICMPExtIfaceNameSubObject::length() const {
108+
uint8_t size = 2 +
109+
strlen(this->ifaceName_); // Allow 2 bytes for size and null-terminating
110+
uint8_t padding = (4 - (size % 4)) % 4;
111+
return size + padding;
112+
}
113+
114+
void ICMPExtIfaceNameSubObject::serialize(
115+
folly::io::RWPrivateCursor* cursor) const {
116+
// Work out the size to write, ensure that we're padded to a 4-byte boundry.
117+
// (including length octet).
118+
cursor->template write<uint8_t>(length());
119+
cursor->push(
120+
(const uint8_t*)this->ifaceName_, length() - 1); // -1 to exclude length
121+
}
122+
123+
} // namespace facebook::fboss

0 commit comments

Comments
 (0)