]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#2976] Disabled stashed RAI copy into response
authorFrancis Dupont <fdupont@isc.org>
Sat, 30 Mar 2024 11:48:08 +0000 (12:48 +0100)
committerRazvan Becheriu <razvan@isc.org>
Fri, 26 Apr 2024 15:25:06 +0000 (18:25 +0300)
src/bin/dhcp4/dhcp4_srv.cc
src/bin/dhcp4/tests/dhcp4_srv_unittest.cc

index 2136dcb281adc6dd093b4dd5255331ec5acfcc25..6f7d74b1d377fa684e9f45f08dfc74b337a7f9f0 100644 (file)
@@ -377,13 +377,6 @@ Dhcpv4Exchange::copyDefaultOptions() {
         resp_->addOption(client_id);
     }
 
-    // If this packet is relayed, we want to copy Relay Agent Info option
-    // when it is not empty.
-    OptionPtr rai = query_->getOption(DHO_DHCP_AGENT_OPTIONS);
-    if (rai && (rai->len() > Option::OPTION4_HDR_LEN)) {
-        resp_->addOption(rai);
-    }
-
     // RFC 3011 states about the Subnet Selection Option
 
     // "Servers configured to support this option MUST return an
@@ -396,6 +389,21 @@ Dhcpv4Exchange::copyDefaultOptions() {
     if (subnet_sel) {
         resp_->addOption(subnet_sel);
     }
+
+    // If this packet is relayed, we want to copy Relay Agent Info option
+    // when it is not empty.
+    OptionPtr rai = query_->getOption(DHO_DHCP_AGENT_OPTIONS);
+    if (!rai || (rai->len() <= Option::OPTION4_HDR_LEN)) {
+        return;
+    }
+    // Do not copy recovered stashed RAI.
+    ConstElementPtr sao = CfgMgr::instance().getCurrentCfg()->
+        getConfiguredGlobal(CfgGlobals::STASH_AGENT_OPTIONS);
+    if (sao && (sao->getType() == data::Element::boolean) &&
+        sao->boolValue() && query_->inClass("STASH_AGENT_OPTIONS")) {
+        return;
+    }
+    resp_->addOption(rai);
 }
 
 void
index de9d16012e5f3b996aabc2a9b06e1a3ebd59007a..de5b17e441be7a1a4c1f44a969cf571b2fe32359 100644 (file)
@@ -363,7 +363,7 @@ TEST_F(Dhcpv4SrvTest, adjustIfaceDataUseRouting) {
     cfg_iface->use(AF_INET, "eth0");
     cfg_iface->use(AF_INET, "eth1");
     cfg_iface->setOutboundIface(CfgIface::USE_ROUTING);
-    CfgMgr::instance().commit();;
+    CfgMgr::instance().commit();
 
     // Create the instance of the incoming packet.
     boost::shared_ptr<Pkt4> req(new Pkt4(DHCPDISCOVER, 1234));
@@ -917,6 +917,52 @@ TEST_F(Dhcpv4SrvTest, initResponse) {
     EXPECT_EQ(IOAddress("192.0.2.3"), subnet_addr);
 }
 
+// This test verifies that recovered stashed agent options are not copied
+// into responses.
+TEST_F(Dhcpv4SrvTest, stashAgentOption) {
+    // Get a DISCOVER with a RAI.
+    Pkt4Ptr query = PktCaptures::captureRelayedDiscover();
+    ASSERT_NO_THROW(query->unpack());
+
+    // Get Relay Agent Info from query...
+    OptionPtr rai_query = query->getOption(DHO_DHCP_AGENT_OPTIONS);
+    ASSERT_TRUE(rai_query);
+
+    // Create exchange and get Response.
+    Dhcpv4Exchange ex = createExchange(query);
+    Pkt4Ptr response = ex.getResponse();
+    ASSERT_TRUE(response);
+
+    // Get Relay Agent Info from response...
+    OptionPtr rai_response = response->getOption(DHO_DHCP_AGENT_OPTIONS);
+    ASSERT_TRUE(rai_response);
+    EXPECT_TRUE(rai_response->equals(rai_query));
+
+    // Set the stash-agent-options.
+    CfgMgr::instance().clear();
+    CfgMgr::instance().getStagingCfg()->
+        addConfiguredGlobal("stash-agent-options", Element::create(true));
+    CfgMgr::instance().commit();
+
+    // Create exchange and get Response.
+    ex = createExchange(query);
+    response = ex.getResponse();
+    ASSERT_TRUE(response);
+    rai_response = response->getOption(DHO_DHCP_AGENT_OPTIONS);
+    ASSERT_TRUE(rai_response);
+    EXPECT_TRUE(rai_response->equals(rai_query));
+
+    // Put the query in the STASH_AGENT_OPTIONS class.
+    query->addClass("STASH_AGENT_OPTIONS");
+
+    // Retry: this time the RAI is not copied.
+    ex = createExchange(query);
+    response = ex.getResponse();
+    ASSERT_TRUE(response);
+    rai_response = response->getOption(DHO_DHCP_AGENT_OPTIONS);
+    EXPECT_FALSE(rai_response);
+}
+
 // This test verifies that the server identifier option is appended to
 // a specified DHCPv4 message and the server identifier is correct.
 TEST_F(Dhcpv4SrvTest, appendServerID) {