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 | #ifndef PATCHPANEL_DNS_DNS_PROTOCOL_H_ |
| 6 | #define PATCHPANEL_DNS_DNS_PROTOCOL_H_ |
Kevin Cernekee | d05be17 | 2017-06-17 17:40:21 -0700 | [diff] [blame] | 7 | |
| 8 | #include <stdint.h> |
| 9 | |
Jason Jeremy Iman | 9df645a | 2020-11-09 04:37:09 +0900 | [diff] [blame^] | 10 | #include "brillo/brillo_export.h" |
Kevin Cernekee | d05be17 | 2017-06-17 17:40:21 -0700 | [diff] [blame] | 11 | |
Jason Jeremy Iman | a21be27 | 2020-10-21 17:53:45 +0900 | [diff] [blame] | 12 | namespace patchpanel { |
Kevin Cernekee | d05be17 | 2017-06-17 17:40:21 -0700 | [diff] [blame] | 13 | |
Jason Jeremy Iman | a21be27 | 2020-10-21 17:53:45 +0900 | [diff] [blame] | 14 | // General constants and structs defined by the DNS and MDNS protocols. |
| 15 | // |
| 16 | // Direct interaction with DNS and MDNS, as well as parsing DNS and MDNS |
| 17 | // messages, should generally only be done within network stack code. |
| 18 | // Network-stack-external code should interact indirectly through network |
| 19 | // service APIs, e.g. NetworkContext::ResolveHost(). But these constants may |
| 20 | // still be useful for other minor purposes. |
Kevin Cernekee | d05be17 | 2017-06-17 17:40:21 -0700 | [diff] [blame] | 21 | namespace dns_protocol { |
| 22 | |
| 23 | static const uint16_t kDefaultPort = 53; |
Jason Jeremy Iman | a21be27 | 2020-10-21 17:53:45 +0900 | [diff] [blame] | 24 | // RFC 5353. |
Kevin Cernekee | d05be17 | 2017-06-17 17:40:21 -0700 | [diff] [blame] | 25 | static const uint16_t kDefaultPortMulticast = 5353; |
| 26 | |
Jason Jeremy Iman | a21be27 | 2020-10-21 17:53:45 +0900 | [diff] [blame] | 27 | // https://www.iana.org/assignments/multicast-addresses/multicast-addresses.xhtml#multicast-addresses-1 |
| 28 | static const char kMdnsMulticastGroupIPv4[] = "224.0.0.251"; |
| 29 | // https://www.iana.org/assignments/ipv6-multicast-addresses/ipv6-multicast-addresses.xhtml#link-local |
| 30 | static const char kMdnsMulticastGroupIPv6[] = "FF02::FB"; |
| 31 | |
Kevin Cernekee | d05be17 | 2017-06-17 17:40:21 -0700 | [diff] [blame] | 32 | // DNS packet consists of a header followed by questions and/or answers. |
| 33 | // For the meaning of specific fields, please see RFC 1035 and 2535 |
| 34 | |
| 35 | // Header format. |
| 36 | // 1 1 1 1 1 1 |
| 37 | // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 |
| 38 | // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 39 | // | ID | |
| 40 | // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 41 | // |QR| Opcode |AA|TC|RD|RA| Z|AD|CD| RCODE | |
| 42 | // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 43 | // | QDCOUNT | |
| 44 | // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 45 | // | ANCOUNT | |
| 46 | // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 47 | // | NSCOUNT | |
| 48 | // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 49 | // | ARCOUNT | |
| 50 | // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 51 | |
| 52 | // Question format. |
| 53 | // 1 1 1 1 1 1 |
| 54 | // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 |
| 55 | // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 56 | // | | |
| 57 | // / QNAME / |
| 58 | // / / |
| 59 | // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 60 | // | QTYPE | |
| 61 | // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 62 | // | QCLASS | |
| 63 | // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 64 | |
| 65 | // Answer format. |
| 66 | // 1 1 1 1 1 1 |
| 67 | // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 |
| 68 | // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 69 | // | | |
| 70 | // / / |
| 71 | // / NAME / |
| 72 | // | | |
| 73 | // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 74 | // | TYPE | |
| 75 | // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 76 | // | CLASS | |
| 77 | // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 78 | // | TTL | |
| 79 | // | | |
| 80 | // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 81 | // | RDLENGTH | |
| 82 | // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--| |
| 83 | // / RDATA / |
| 84 | // / / |
| 85 | // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 86 | |
| 87 | #pragma pack(push) |
| 88 | #pragma pack(1) |
| 89 | |
| 90 | // On-the-wire header. All uint16_t are in network order. |
Jason Jeremy Iman | 9df645a | 2020-11-09 04:37:09 +0900 | [diff] [blame^] | 91 | struct BRILLO_EXPORT Header { |
Jason Jeremy Iman | a21be27 | 2020-10-21 17:53:45 +0900 | [diff] [blame] | 92 | uint16_t id = 0; |
| 93 | uint16_t flags = 0; |
| 94 | uint16_t qdcount = 0; |
| 95 | uint16_t ancount = 0; |
| 96 | uint16_t nscount = 0; |
| 97 | uint16_t arcount = 0; |
Kevin Cernekee | d05be17 | 2017-06-17 17:40:21 -0700 | [diff] [blame] | 98 | }; |
| 99 | |
| 100 | #pragma pack(pop) |
| 101 | |
| 102 | static const uint8_t kLabelMask = 0xc0; |
| 103 | static const uint8_t kLabelPointer = 0xc0; |
| 104 | static const uint8_t kLabelDirect = 0x0; |
| 105 | static const uint16_t kOffsetMask = 0x3fff; |
| 106 | |
| 107 | // In MDns the most significant bit of the rrclass is designated as the |
| 108 | // "cache-flush bit", as described in http://www.rfc-editor.org/rfc/rfc6762.txt |
| 109 | // section 10.2. |
| 110 | static const uint16_t kMDnsClassMask = 0x7FFF; |
| 111 | |
| 112 | // RFC 1035, section 3.1: To simplify implementations, the total length of |
| 113 | // a domain name (i.e., label octets and label length octets) is restricted |
| 114 | // to 255 octets or less. |
| 115 | static const int kMaxNameLength = 255; |
| 116 | |
| 117 | // RFC 1035, section 4.2.1: Messages carried by UDP are restricted to 512 |
| 118 | // bytes (not counting the IP nor UDP headers). |
| 119 | static const int kMaxUDPSize = 512; |
| 120 | |
| 121 | // RFC 6762, section 17: Messages over the local link are restricted by the |
| 122 | // medium's MTU, and must be under 9000 bytes |
| 123 | static const int kMaxMulticastSize = 9000; |
| 124 | |
Jason Jeremy Iman | a21be27 | 2020-10-21 17:53:45 +0900 | [diff] [blame] | 125 | // RFC 1035, Section 4.1.3. |
| 126 | // TYPE (2 bytes) + CLASS (2 bytes) + TTL (4 bytes) + RDLENGTH (2 bytes) |
| 127 | static const int kResourceRecordSizeInBytesWithoutNameAndRData = 10; |
| 128 | |
Kevin Cernekee | d05be17 | 2017-06-17 17:40:21 -0700 | [diff] [blame] | 129 | // DNS class types. |
| 130 | // |
| 131 | // https://www.iana.org/assignments/dns-parameters/dns-parameters.xhtml#dns-parameters-2 |
| 132 | static const uint16_t kClassIN = 1; |
Jason Jeremy Iman | a21be27 | 2020-10-21 17:53:45 +0900 | [diff] [blame] | 133 | // RFC 6762, Section 10.2. |
| 134 | // |
| 135 | // For resource records sent through mDNS, the top bit of the class field in a |
| 136 | // resource record is repurposed to the cache-flush bit. This bit should only be |
| 137 | // used in mDNS transactions. |
| 138 | static const uint16_t kFlagCacheFlush = 0x8000; |
Kevin Cernekee | d05be17 | 2017-06-17 17:40:21 -0700 | [diff] [blame] | 139 | |
| 140 | // DNS resource record types. |
| 141 | // |
| 142 | // https://www.iana.org/assignments/dns-parameters/dns-parameters.xhtml#dns-parameters-4 |
| 143 | static const uint16_t kTypeA = 1; |
| 144 | static const uint16_t kTypeCNAME = 5; |
Jason Jeremy Iman | a21be27 | 2020-10-21 17:53:45 +0900 | [diff] [blame] | 145 | static const uint16_t kTypeSOA = 6; |
Kevin Cernekee | d05be17 | 2017-06-17 17:40:21 -0700 | [diff] [blame] | 146 | static const uint16_t kTypePTR = 12; |
| 147 | static const uint16_t kTypeTXT = 16; |
| 148 | static const uint16_t kTypeAAAA = 28; |
| 149 | static const uint16_t kTypeSRV = 33; |
Jason Jeremy Iman | a21be27 | 2020-10-21 17:53:45 +0900 | [diff] [blame] | 150 | static const uint16_t kTypeOPT = 41; |
Kevin Cernekee | d05be17 | 2017-06-17 17:40:21 -0700 | [diff] [blame] | 151 | static const uint16_t kTypeNSEC = 47; |
Jason Jeremy Iman | a21be27 | 2020-10-21 17:53:45 +0900 | [diff] [blame] | 152 | static const uint16_t kTypeHttps = 65; |
| 153 | static const uint16_t kTypeANY = 255; |
| 154 | |
| 155 | // Experimental DNS record types pending IANA assignment. |
| 156 | // |
| 157 | // The INTEGRITY RR type exists purely for measuring how the DNS ecosystem |
| 158 | // handles new RR types. |
| 159 | // https://docs.google.com/document/d/14eCqVyT_3MSj7ydqNFl1Yl0yg1fs6g24qmYUUdi5V-k/edit?usp=sharing |
| 160 | static const uint16_t kExperimentalTypeIntegrity = 65521; |
Kevin Cernekee | d05be17 | 2017-06-17 17:40:21 -0700 | [diff] [blame] | 161 | |
| 162 | // DNS reply codes (RCODEs). |
| 163 | // |
| 164 | // https://www.iana.org/assignments/dns-parameters/dns-parameters.xhtml#dns-parameters-6 |
| 165 | static const uint8_t kRcodeNOERROR = 0; |
| 166 | static const uint8_t kRcodeFORMERR = 1; |
| 167 | static const uint8_t kRcodeSERVFAIL = 2; |
| 168 | static const uint8_t kRcodeNXDOMAIN = 3; |
| 169 | static const uint8_t kRcodeNOTIMP = 4; |
| 170 | static const uint8_t kRcodeREFUSED = 5; |
| 171 | |
Jason Jeremy Iman | a21be27 | 2020-10-21 17:53:45 +0900 | [diff] [blame] | 172 | // DNS EDNS(0) option codes (OPT) |
| 173 | // |
| 174 | // https://www.iana.org/assignments/dns-parameters/dns-parameters.xhtml#dns-parameters-11 |
| 175 | static const uint16_t kEdnsPadding = 12; |
| 176 | |
Kevin Cernekee | d05be17 | 2017-06-17 17:40:21 -0700 | [diff] [blame] | 177 | // DNS header flags. |
| 178 | // |
| 179 | // https://www.iana.org/assignments/dns-parameters/dns-parameters.xhtml#dns-parameters-12 |
| 180 | static const uint16_t kFlagResponse = 0x8000; |
Jason Jeremy Iman | a21be27 | 2020-10-21 17:53:45 +0900 | [diff] [blame] | 181 | static const uint16_t kFlagAA = 0x400; // Authoritative Answer - response flag. |
Kevin Cernekee | d05be17 | 2017-06-17 17:40:21 -0700 | [diff] [blame] | 182 | static const uint16_t kFlagRD = 0x100; // Recursion Desired - query flag. |
| 183 | static const uint16_t kFlagTC = 0x200; // Truncated - server flag. |
| 184 | |
| 185 | } // namespace dns_protocol |
| 186 | |
Jason Jeremy Iman | a21be27 | 2020-10-21 17:53:45 +0900 | [diff] [blame] | 187 | } // namespace patchpanel |
Kevin Cernekee | d05be17 | 2017-06-17 17:40:21 -0700 | [diff] [blame] | 188 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 189 | #endif // PATCHPANEL_DNS_DNS_PROTOCOL_H_ |