blob: 8feadf26e03bb537e26acad5d0c99bec5a93849c [file] [log] [blame]
Kevin Cernekeed05be172017-06-17 17:40:21 -07001// 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 Evans3388a032020-03-24 11:25:55 +09005#ifndef PATCHPANEL_DNS_DNS_PROTOCOL_H_
6#define PATCHPANEL_DNS_DNS_PROTOCOL_H_
Kevin Cernekeed05be172017-06-17 17:40:21 -07007
8#include <stdint.h>
9
Jason Jeremy Iman9df645a2020-11-09 04:37:09 +090010#include "brillo/brillo_export.h"
Kevin Cernekeed05be172017-06-17 17:40:21 -070011
Jason Jeremy Imana21be272020-10-21 17:53:45 +090012namespace patchpanel {
Kevin Cernekeed05be172017-06-17 17:40:21 -070013
Jason Jeremy Imana21be272020-10-21 17:53:45 +090014// 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 Cernekeed05be172017-06-17 17:40:21 -070021namespace dns_protocol {
22
23static const uint16_t kDefaultPort = 53;
Jason Jeremy Imana21be272020-10-21 17:53:45 +090024// RFC 5353.
Kevin Cernekeed05be172017-06-17 17:40:21 -070025static const uint16_t kDefaultPortMulticast = 5353;
26
Jason Jeremy Imana21be272020-10-21 17:53:45 +090027// https://www.iana.org/assignments/multicast-addresses/multicast-addresses.xhtml#multicast-addresses-1
28static const char kMdnsMulticastGroupIPv4[] = "224.0.0.251";
29// https://www.iana.org/assignments/ipv6-multicast-addresses/ipv6-multicast-addresses.xhtml#link-local
30static const char kMdnsMulticastGroupIPv6[] = "FF02::FB";
31
Kevin Cernekeed05be172017-06-17 17:40:21 -070032// 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 Iman9df645a2020-11-09 04:37:09 +090091struct BRILLO_EXPORT Header {
Jason Jeremy Imana21be272020-10-21 17:53:45 +090092 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 Cernekeed05be172017-06-17 17:40:21 -070098};
99
100#pragma pack(pop)
101
102static const uint8_t kLabelMask = 0xc0;
103static const uint8_t kLabelPointer = 0xc0;
104static const uint8_t kLabelDirect = 0x0;
105static 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.
110static 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.
115static 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).
119static 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
123static const int kMaxMulticastSize = 9000;
124
Jason Jeremy Imana21be272020-10-21 17:53:45 +0900125// RFC 1035, Section 4.1.3.
126// TYPE (2 bytes) + CLASS (2 bytes) + TTL (4 bytes) + RDLENGTH (2 bytes)
127static const int kResourceRecordSizeInBytesWithoutNameAndRData = 10;
128
Kevin Cernekeed05be172017-06-17 17:40:21 -0700129// DNS class types.
130//
131// https://www.iana.org/assignments/dns-parameters/dns-parameters.xhtml#dns-parameters-2
132static const uint16_t kClassIN = 1;
Jason Jeremy Imana21be272020-10-21 17:53:45 +0900133// 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.
138static const uint16_t kFlagCacheFlush = 0x8000;
Kevin Cernekeed05be172017-06-17 17:40:21 -0700139
140// DNS resource record types.
141//
142// https://www.iana.org/assignments/dns-parameters/dns-parameters.xhtml#dns-parameters-4
143static const uint16_t kTypeA = 1;
144static const uint16_t kTypeCNAME = 5;
Jason Jeremy Imana21be272020-10-21 17:53:45 +0900145static const uint16_t kTypeSOA = 6;
Kevin Cernekeed05be172017-06-17 17:40:21 -0700146static const uint16_t kTypePTR = 12;
147static const uint16_t kTypeTXT = 16;
148static const uint16_t kTypeAAAA = 28;
149static const uint16_t kTypeSRV = 33;
Jason Jeremy Imana21be272020-10-21 17:53:45 +0900150static const uint16_t kTypeOPT = 41;
Kevin Cernekeed05be172017-06-17 17:40:21 -0700151static const uint16_t kTypeNSEC = 47;
Jason Jeremy Imana21be272020-10-21 17:53:45 +0900152static const uint16_t kTypeHttps = 65;
153static 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
160static const uint16_t kExperimentalTypeIntegrity = 65521;
Kevin Cernekeed05be172017-06-17 17:40:21 -0700161
162// DNS reply codes (RCODEs).
163//
164// https://www.iana.org/assignments/dns-parameters/dns-parameters.xhtml#dns-parameters-6
165static const uint8_t kRcodeNOERROR = 0;
166static const uint8_t kRcodeFORMERR = 1;
167static const uint8_t kRcodeSERVFAIL = 2;
168static const uint8_t kRcodeNXDOMAIN = 3;
169static const uint8_t kRcodeNOTIMP = 4;
170static const uint8_t kRcodeREFUSED = 5;
171
Jason Jeremy Imana21be272020-10-21 17:53:45 +0900172// DNS EDNS(0) option codes (OPT)
173//
174// https://www.iana.org/assignments/dns-parameters/dns-parameters.xhtml#dns-parameters-11
175static const uint16_t kEdnsPadding = 12;
176
Kevin Cernekeed05be172017-06-17 17:40:21 -0700177// DNS header flags.
178//
179// https://www.iana.org/assignments/dns-parameters/dns-parameters.xhtml#dns-parameters-12
180static const uint16_t kFlagResponse = 0x8000;
Jason Jeremy Imana21be272020-10-21 17:53:45 +0900181static const uint16_t kFlagAA = 0x400; // Authoritative Answer - response flag.
Kevin Cernekeed05be172017-06-17 17:40:21 -0700182static const uint16_t kFlagRD = 0x100; // Recursion Desired - query flag.
183static const uint16_t kFlagTC = 0x200; // Truncated - server flag.
184
185} // namespace dns_protocol
186
Jason Jeremy Imana21be272020-10-21 17:53:45 +0900187} // namespace patchpanel
Kevin Cernekeed05be172017-06-17 17:40:21 -0700188
Garrick Evans3388a032020-03-24 11:25:55 +0900189#endif // PATCHPANEL_DNS_DNS_PROTOCOL_H_