Kevin Cernekee | d05be17 | 2017-06-17 17:40:21 -0700 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 5 | #include "patchpanel/dns/dns_response.h" |
Kevin Cernekee | d05be17 | 2017-06-17 17:40:21 -0700 | [diff] [blame] | 6 | |
| 7 | #include <limits> |
| 8 | |
| 9 | #include "base/strings/string_util.h" |
| 10 | #include "base/sys_byteorder.h" |
| 11 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 12 | #include "patchpanel/dns/big_endian.h" |
| 13 | #include "patchpanel/dns/dns_protocol.h" |
| 14 | #include "patchpanel/dns/io_buffer.h" |
Kevin Cernekee | d05be17 | 2017-06-17 17:40:21 -0700 | [diff] [blame] | 15 | |
| 16 | namespace net { |
| 17 | |
| 18 | namespace { |
| 19 | |
| 20 | const size_t kHeaderSize = sizeof(dns_protocol::Header); |
| 21 | |
| 22 | const uint8_t kRcodeMask = 0xf; |
| 23 | |
| 24 | } // namespace |
| 25 | |
Ben Chan | 4f38650 | 2019-09-20 16:17:59 -0700 | [diff] [blame] | 26 | DnsResourceRecord::DnsResourceRecord() = default; |
Kevin Cernekee | d05be17 | 2017-06-17 17:40:21 -0700 | [diff] [blame] | 27 | |
Ben Chan | 4f38650 | 2019-09-20 16:17:59 -0700 | [diff] [blame] | 28 | DnsResourceRecord::~DnsResourceRecord() = default; |
Kevin Cernekee | d05be17 | 2017-06-17 17:40:21 -0700 | [diff] [blame] | 29 | |
Ben Chan | ac00904 | 2019-09-20 16:19:56 -0700 | [diff] [blame] | 30 | DnsRecordParser::DnsRecordParser() : packet_(nullptr), length_(0), cur_(0) {} |
Kevin Cernekee | d05be17 | 2017-06-17 17:40:21 -0700 | [diff] [blame] | 31 | |
| 32 | DnsRecordParser::DnsRecordParser(const void* packet, |
| 33 | size_t length, |
| 34 | size_t offset) |
| 35 | : packet_(reinterpret_cast<const char*>(packet)), |
| 36 | length_(length), |
| 37 | cur_(packet_ + offset) { |
| 38 | DCHECK_LE(offset, length); |
| 39 | } |
| 40 | |
| 41 | unsigned DnsRecordParser::ReadName(const void* const vpos, |
| 42 | std::string* out) const { |
| 43 | const char* const pos = reinterpret_cast<const char*>(vpos); |
| 44 | DCHECK(packet_); |
| 45 | DCHECK_LE(packet_, pos); |
| 46 | DCHECK_LE(pos, packet_ + length_); |
| 47 | |
| 48 | const char* p = pos; |
| 49 | const char* end = packet_ + length_; |
| 50 | // Count number of seen bytes to detect loops. |
| 51 | unsigned seen = 0; |
| 52 | // Remember how many bytes were consumed before first jump. |
| 53 | unsigned consumed = 0; |
| 54 | |
| 55 | if (pos >= end) |
| 56 | return 0; |
| 57 | |
| 58 | if (out) { |
| 59 | out->clear(); |
| 60 | out->reserve(dns_protocol::kMaxNameLength); |
| 61 | } |
| 62 | |
| 63 | for (;;) { |
| 64 | // The first two bits of the length give the type of the length. It's |
| 65 | // either a direct length or a pointer to the remainder of the name. |
| 66 | switch (*p & dns_protocol::kLabelMask) { |
| 67 | case dns_protocol::kLabelPointer: { |
| 68 | if (p + sizeof(uint16_t) > end) |
| 69 | return 0; |
| 70 | if (consumed == 0) { |
| 71 | consumed = p - pos + sizeof(uint16_t); |
| 72 | if (!out) |
| 73 | return consumed; // If name is not stored, that's all we need. |
| 74 | } |
| 75 | seen += sizeof(uint16_t); |
| 76 | // If seen the whole packet, then we must be in a loop. |
| 77 | if (seen > length_) |
| 78 | return 0; |
| 79 | uint16_t offset; |
| 80 | base::ReadBigEndian<uint16_t>(p, &offset); |
| 81 | offset &= dns_protocol::kOffsetMask; |
| 82 | p = packet_ + offset; |
| 83 | if (p >= end) |
| 84 | return 0; |
| 85 | break; |
| 86 | } |
| 87 | case dns_protocol::kLabelDirect: { |
| 88 | uint8_t label_len = *p; |
| 89 | ++p; |
| 90 | // Note: root domain (".") is NOT included. |
| 91 | if (label_len == 0) { |
| 92 | if (consumed == 0) { |
| 93 | consumed = p - pos; |
| 94 | } // else we set |consumed| before first jump |
| 95 | return consumed; |
| 96 | } |
| 97 | if (p + label_len >= end) |
| 98 | return 0; // Truncated or missing label. |
| 99 | if (out) { |
| 100 | if (!out->empty()) |
| 101 | out->append("."); |
| 102 | out->append(p, label_len); |
| 103 | } |
| 104 | p += label_len; |
| 105 | seen += 1 + label_len; |
| 106 | break; |
| 107 | } |
| 108 | default: |
| 109 | // unhandled label type |
| 110 | return 0; |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | bool DnsRecordParser::ReadRecord(DnsResourceRecord* out) { |
| 116 | DCHECK(packet_); |
| 117 | size_t consumed = ReadName(cur_, &out->name); |
| 118 | if (!consumed) |
| 119 | return false; |
| 120 | base::BigEndianReader reader(cur_ + consumed, |
| 121 | packet_ + length_ - (cur_ + consumed)); |
| 122 | uint16_t rdlen; |
Hidehiko Abe | 3a7e513 | 2018-02-15 13:07:50 +0900 | [diff] [blame] | 123 | if (reader.ReadU16(&out->type) && reader.ReadU16(&out->klass) && |
| 124 | reader.ReadU32(&out->ttl) && reader.ReadU16(&rdlen) && |
Kevin Cernekee | d05be17 | 2017-06-17 17:40:21 -0700 | [diff] [blame] | 125 | reader.ReadPiece(&out->rdata, rdlen)) { |
| 126 | cur_ = reader.ptr(); |
| 127 | return true; |
| 128 | } |
| 129 | return false; |
| 130 | } |
| 131 | |
| 132 | bool DnsRecordParser::SkipQuestion() { |
Ben Chan | ac00904 | 2019-09-20 16:19:56 -0700 | [diff] [blame] | 133 | size_t consumed = ReadName(cur_, nullptr); |
Kevin Cernekee | d05be17 | 2017-06-17 17:40:21 -0700 | [diff] [blame] | 134 | if (!consumed) |
| 135 | return false; |
| 136 | |
| 137 | const char* next = cur_ + consumed + 2 * sizeof(uint16_t); // QTYPE + QCLASS |
| 138 | if (next > packet_ + length_) |
| 139 | return false; |
| 140 | |
| 141 | cur_ = next; |
| 142 | |
| 143 | return true; |
| 144 | } |
| 145 | |
| 146 | DnsResponse::DnsResponse() |
Hidehiko Abe | 3a7e513 | 2018-02-15 13:07:50 +0900 | [diff] [blame] | 147 | : io_buffer_(new IOBufferWithSize(dns_protocol::kMaxUDPSize + 1)) {} |
Kevin Cernekee | d05be17 | 2017-06-17 17:40:21 -0700 | [diff] [blame] | 148 | |
| 149 | DnsResponse::DnsResponse(size_t length) |
Hidehiko Abe | 3a7e513 | 2018-02-15 13:07:50 +0900 | [diff] [blame] | 150 | : io_buffer_(new IOBufferWithSize(length)) {} |
Kevin Cernekee | d05be17 | 2017-06-17 17:40:21 -0700 | [diff] [blame] | 151 | |
Hidehiko Abe | 3a7e513 | 2018-02-15 13:07:50 +0900 | [diff] [blame] | 152 | DnsResponse::DnsResponse(const void* data, size_t length, size_t answer_offset) |
Kevin Cernekee | d05be17 | 2017-06-17 17:40:21 -0700 | [diff] [blame] | 153 | : io_buffer_(new IOBufferWithSize(length)), |
| 154 | parser_(io_buffer_->data(), length, answer_offset) { |
| 155 | DCHECK(data); |
| 156 | memcpy(io_buffer_->data(), data, length); |
| 157 | } |
| 158 | |
Ben Chan | 4f38650 | 2019-09-20 16:17:59 -0700 | [diff] [blame] | 159 | DnsResponse::~DnsResponse() = default; |
Kevin Cernekee | d05be17 | 2017-06-17 17:40:21 -0700 | [diff] [blame] | 160 | |
| 161 | bool DnsResponse::InitParseWithoutQuery(int nbytes) { |
| 162 | DCHECK_GE(nbytes, 0); |
| 163 | |
| 164 | if (nbytes < static_cast<int>(kHeaderSize) || nbytes >= io_buffer_->size()) |
| 165 | return false; |
| 166 | |
| 167 | parser_ = DnsRecordParser(io_buffer_->data(), nbytes, kHeaderSize); |
| 168 | |
| 169 | unsigned qdcount = base::NetToHost16(header()->qdcount); |
| 170 | for (unsigned i = 0; i < qdcount; ++i) { |
| 171 | if (!parser_.SkipQuestion()) { |
| 172 | parser_ = DnsRecordParser(); // Make parser invalid again. |
| 173 | return false; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | return true; |
| 178 | } |
| 179 | |
| 180 | bool DnsResponse::IsValid() const { |
| 181 | return parser_.IsValid(); |
| 182 | } |
| 183 | |
| 184 | uint16_t DnsResponse::flags() const { |
| 185 | DCHECK(parser_.IsValid()); |
| 186 | return base::NetToHost16(header()->flags) & ~(kRcodeMask); |
| 187 | } |
| 188 | |
| 189 | uint8_t DnsResponse::rcode() const { |
| 190 | DCHECK(parser_.IsValid()); |
| 191 | return base::NetToHost16(header()->flags) & kRcodeMask; |
| 192 | } |
| 193 | |
| 194 | unsigned DnsResponse::answer_count() const { |
| 195 | DCHECK(parser_.IsValid()); |
| 196 | return base::NetToHost16(header()->ancount); |
| 197 | } |
| 198 | |
| 199 | unsigned DnsResponse::additional_answer_count() const { |
| 200 | DCHECK(parser_.IsValid()); |
| 201 | return base::NetToHost16(header()->arcount); |
| 202 | } |
| 203 | |
| 204 | base::StringPiece DnsResponse::qname() const { |
| 205 | DCHECK(parser_.IsValid()); |
| 206 | // The response is HEADER QNAME QTYPE QCLASS ANSWER. |
| 207 | // |parser_| is positioned at the beginning of ANSWER, so the end of QNAME is |
| 208 | // two uint16_ts before it. |
| 209 | const size_t qname_size = |
| 210 | parser_.GetOffset() - 2 * sizeof(uint16_t) - kHeaderSize; |
| 211 | return base::StringPiece(io_buffer_->data() + kHeaderSize, qname_size); |
| 212 | } |
| 213 | |
| 214 | uint16_t DnsResponse::qtype() const { |
| 215 | DCHECK(parser_.IsValid()); |
| 216 | // QTYPE starts where QNAME ends. |
| 217 | const size_t type_offset = parser_.GetOffset() - 2 * sizeof(uint16_t); |
| 218 | uint16_t type; |
| 219 | base::ReadBigEndian<uint16_t>(io_buffer_->data() + type_offset, &type); |
| 220 | return type; |
| 221 | } |
| 222 | |
| 223 | DnsRecordParser DnsResponse::Parser() const { |
| 224 | DCHECK(parser_.IsValid()); |
| 225 | // Return a copy of the parser. |
| 226 | return parser_; |
| 227 | } |
| 228 | |
| 229 | const dns_protocol::Header* DnsResponse::header() const { |
| 230 | return reinterpret_cast<const dns_protocol::Header*>(io_buffer_->data()); |
| 231 | } |
| 232 | |
| 233 | } // namespace net |