1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
|
/*
* 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 "dns.hh"
#include "ednsoptions.hh"
#include "iputils.hh"
#include "dnsparser.hh"
bool getNextEDNSOption(const char* data, size_t dataLen, uint16_t& optionCode, uint16_t& optionLen)
{
if (data == nullptr || dataLen < (sizeof(uint16_t) + sizeof(uint16_t))) {
return false;
}
size_t pos = 0;
const uint8_t* p = reinterpret_cast<const uint8_t*>(data);
optionCode = (static_cast<uint16_t>(p[pos]) * 256) + p[pos + 1];
pos += EDNS_OPTION_CODE_SIZE;
optionLen = (static_cast<uint16_t>(p[pos]) * 256) + p[pos + 1];
pos += EDNS_OPTION_LENGTH_SIZE;
(void) pos;
return true;
}
/* extract the position (relative to the optRR pointer!) and size of a specific EDNS0 option from a pointer on the beginning rdLen of the OPT RR */
int getEDNSOption(const char* optRR, const size_t len, uint16_t wantedOption, size_t* optionValuePosition, size_t * optionValueSize)
{
if (optRR == nullptr || optionValuePosition == nullptr || optionValueSize == nullptr) {
return EINVAL;
}
size_t pos = 0;
if (len < DNS_RDLENGTH_SIZE) {
return EINVAL;
}
const uint16_t rdLen = (((unsigned char) optRR[pos]) * 256) + ((unsigned char) optRR[pos+1]);
size_t rdPos = 0;
pos += DNS_RDLENGTH_SIZE;
if ((pos + rdLen) > len) {
return EINVAL;
}
while(len >= (pos + EDNS_OPTION_CODE_SIZE + EDNS_OPTION_LENGTH_SIZE) &&
rdLen >= (rdPos + EDNS_OPTION_CODE_SIZE + EDNS_OPTION_LENGTH_SIZE)) {
uint16_t optionCode;
uint16_t optionLen;
if (!getNextEDNSOption(optRR + pos, len-pos, optionCode, optionLen)) {
break;
}
pos += EDNS_OPTION_CODE_SIZE;
rdPos += EDNS_OPTION_CODE_SIZE;
pos += EDNS_OPTION_LENGTH_SIZE;
rdPos += EDNS_OPTION_LENGTH_SIZE;
if (optionLen > (rdLen - rdPos) || optionLen > (len - pos)) {
return EINVAL;
}
if (optionCode == wantedOption) {
*optionValuePosition = pos - (EDNS_OPTION_CODE_SIZE + EDNS_OPTION_LENGTH_SIZE);
*optionValueSize = optionLen + EDNS_OPTION_CODE_SIZE + EDNS_OPTION_LENGTH_SIZE;
return 0;
}
else {
/* skip this option */
pos += optionLen;
rdPos += optionLen;
}
}
return ENOENT;
}
bool slowParseEDNSOptions(const PacketBuffer& packet, EDNSOptionViewMap& options)
{
if (packet.size() < sizeof(dnsheader)) {
return false;
}
const dnsheader_aligned dnsHeader(packet.data());
if (ntohs(dnsHeader->qdcount) == 0) {
return false;
}
if (ntohs(dnsHeader->arcount) == 0) {
return false;
}
try {
uint64_t numrecords = ntohs(dnsHeader->ancount) + ntohs(dnsHeader->nscount) + ntohs(dnsHeader->arcount);
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast,cppcoreguidelines-pro-type-const-cast)
DNSPacketMangler dpm(const_cast<char*>(reinterpret_cast<const char*>(packet.data())), packet.size());
uint64_t index{};
for (index = 0; index < ntohs(dnsHeader->qdcount); ++index) {
dpm.skipDomainName();
/* type and class */
dpm.skipBytes(4);
}
for (index = 0; index < numrecords; ++index) {
dpm.skipDomainName();
uint8_t section = index < ntohs(dnsHeader->ancount) ? 1 : (index < (ntohs(dnsHeader->ancount) + ntohs(dnsHeader->nscount)) ? 2 : 3);
uint16_t dnstype = dpm.get16BitInt();
dpm.get16BitInt();
dpm.skipBytes(4); /* TTL */
if (section == 3 && dnstype == QType::OPT) {
uint32_t offset = dpm.getOffset();
if (offset >= packet.size()) {
return false;
}
/* if we survive this call, we can parse it safely */
dpm.skipRData();
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
return getEDNSOptions(reinterpret_cast<const char*>(&packet.at(offset)), packet.size() - offset, options) == 0;
}
dpm.skipRData();
}
}
catch (...) {
return false;
}
return true;
}
/* extract all EDNS0 options from a pointer on the beginning rdLen of the OPT RR */
int getEDNSOptions(const char* optRR, const size_t len, EDNSOptionViewMap& options)
{
size_t pos = 0;
if (optRR == nullptr || len < DNS_RDLENGTH_SIZE) {
return EINVAL;
}
const uint16_t rdLen = (((unsigned char) optRR[pos]) * 256) + ((unsigned char) optRR[pos+1]);
size_t rdPos = 0;
pos += DNS_RDLENGTH_SIZE;
if ((pos + rdLen) > len) {
return EINVAL;
}
while(len >= (pos + EDNS_OPTION_CODE_SIZE + EDNS_OPTION_LENGTH_SIZE) &&
rdLen >= (rdPos + EDNS_OPTION_CODE_SIZE + EDNS_OPTION_LENGTH_SIZE)) {
const uint16_t optionCode = (((unsigned char) optRR[pos]) * 256) + ((unsigned char) optRR[pos+1]);
pos += EDNS_OPTION_CODE_SIZE;
rdPos += EDNS_OPTION_CODE_SIZE;
const uint16_t optionLen = (((unsigned char) optRR[pos]) * 256) + ((unsigned char) optRR[pos+1]);
pos += EDNS_OPTION_LENGTH_SIZE;
rdPos += EDNS_OPTION_LENGTH_SIZE;
if (optionLen > (rdLen - rdPos) || optionLen > (len - pos))
return EINVAL;
EDNSOptionViewValue value;
value.content = optRR + pos;
value.size = optionLen;
options[optionCode].values.push_back(value);
/* skip this option */
pos += optionLen;
rdPos += optionLen;
}
return 0;
}
bool getEDNSOptionsFromContent(const std::string& content, std::vector<std::pair<uint16_t, std::string>>& options)
{
size_t pos = 0;
uint16_t code, len;
const size_t contentLength = content.size();
while (pos < contentLength && (contentLength - pos) >= (EDNS_OPTION_CODE_SIZE + EDNS_OPTION_LENGTH_SIZE)) {
code = (static_cast<unsigned char>(content.at(pos)) * 256) + static_cast<unsigned char>(content.at(pos+1));
pos += EDNS_OPTION_CODE_SIZE;
len = (static_cast<unsigned char>(content.at(pos)) * 256) + static_cast<unsigned char>(content.at(pos+1));
pos += EDNS_OPTION_LENGTH_SIZE;
if (pos > contentLength || len > (contentLength - pos)) {
return false;
}
options.emplace_back(code, std::string(&content.at(pos), len));
pos += len;
}
return true;
}
void generateEDNSOption(uint16_t optionCode, const std::string& payload, std::string& res)
{
const uint16_t ednsOptionCode = htons(optionCode);
const uint16_t payloadLen = htons(payload.length());
res.append((const char *) &ednsOptionCode, sizeof ednsOptionCode);
res.append((const char *) &payloadLen, sizeof payloadLen);
res.append(payload);
}
|