diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-13 21:14:49 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-13 21:14:49 +0000 |
commit | 2f230033794fafdf10822568e763d4db68cf6c6b (patch) | |
tree | 39ca5c2325b7b43c9a28ca6d4ad4026a61e7eb97 /dnsdist-edns.cc | |
parent | Adding debian version 1.8.3-3. (diff) | |
download | dnsdist-2f230033794fafdf10822568e763d4db68cf6c6b.tar.xz dnsdist-2f230033794fafdf10822568e763d4db68cf6c6b.zip |
Merging upstream version 1.9.3.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'dnsdist-edns.cc')
-rw-r--r-- | dnsdist-edns.cc | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/dnsdist-edns.cc b/dnsdist-edns.cc new file mode 100644 index 0000000..0aa8539 --- /dev/null +++ b/dnsdist-edns.cc @@ -0,0 +1,94 @@ +/* + * This file is part of PowerDNS or dnsdist. + * Copyright -- PowerDNS.COM B.V. and its contributors + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of version 2 of the GNU General Public License as + * published by the Free Software Foundation. + * + * In addition, for the avoidance of any doubt, permission is granted to + * link this program with OpenSSL and to (re)distribute the binaries + * produced as the result of such linking. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ +#include "dnsdist-ecs.hh" +#include "dnsdist-edns.hh" +#include "ednsoptions.hh" +#include "ednsextendederror.hh" + +namespace dnsdist::edns +{ +std::pair<std::optional<uint16_t>, std::optional<std::string>> getExtendedDNSError(const PacketBuffer& packet) +{ + uint16_t optStart = 0; + size_t optLen = 0; + bool last = false; + + int res = locateEDNSOptRR(packet, &optStart, &optLen, &last); + + if (res != 0) { + return {std::nullopt, std::nullopt}; + } + + size_t optContentStart = 0; + uint16_t optContentLen = 0; + uint16_t infoCode{0}; + std::optional<std::string> extraText{std::nullopt}; + /* we need at least 2 bytes after the option length (info-code) */ + if (!isEDNSOptionInOpt(packet, optStart, optLen, EDNSOptionCode::EXTENDEDERROR, &optContentStart, &optContentLen) || optContentLen < sizeof(infoCode)) { + return {std::nullopt, std::nullopt}; + } + memcpy(&infoCode, &packet.at(optContentStart), sizeof(infoCode)); + infoCode = ntohs(infoCode); + + if (optContentLen > sizeof(infoCode)) { + extraText = std::string(); + extraText->resize(optContentLen - sizeof(infoCode)); + memcpy(extraText->data(), &packet.at(optContentStart + sizeof(infoCode)), optContentLen - sizeof(infoCode)); + } + return {infoCode, std::move(extraText)}; +} + +bool addExtendedDNSError(PacketBuffer& packet, size_t maximumPacketSize, uint16_t code, const std::string& extraStatus) +{ + uint16_t optStart = 0; + size_t optLen = 0; + bool last = false; + + int res = locateEDNSOptRR(packet, &optStart, &optLen, &last); + + if (res != 0) { + /* no EDNS OPT record in the response, something is not right */ + return false; + } + + EDNSExtendedError ede{.infoCode = code, .extraText = extraStatus}; + auto edeOptionPayload = makeEDNSExtendedErrorOptString(ede); + std::string edeOption; + generateEDNSOption(EDNSOptionCode::EXTENDEDERROR, edeOptionPayload, edeOption); + + /* we might have one record after the OPT one, we need to rewrite + the whole packet because of compression */ + PacketBuffer newContent; + bool ednsAdded = false; + bool edeAdded = false; + if (!slowRewriteEDNSOptionInQueryWithRecords(packet, newContent, ednsAdded, EDNSOptionCode::EXTENDEDERROR, edeAdded, true, edeOption)) { + return false; + } + + if (newContent.size() > maximumPacketSize) { + return false; + } + + packet = std::move(newContent); + return true; +} +} |