| 1 |
/* dnsmasq is Copyright (c) 2000-2026 Simon Kelley |
| 2 |
|
| 3 |
This program is free software; you can redistribute it and/or modify |
| 4 |
it under the terms of the GNU General Public License as published by |
| 5 |
the Free Software Foundation; version 2 dated June, 1991, or |
| 6 |
(at your option) version 3 dated 29 June, 2007. |
| 7 |
|
| 8 |
This program is distributed in the hope that it will be useful, |
| 9 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 |
GNU General Public License for more details. |
| 12 |
|
| 13 |
You should have received a copy of the GNU General Public License |
| 14 |
along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
*/ |
| 16 |
|
| 17 |
#define NAMESERVER_PORT 53 |
| 18 |
#define TFTP_PORT 69 |
| 19 |
#define MIN_PORT 1024 /* first non-reserved port */ |
| 20 |
#define MAX_PORT 65535u |
| 21 |
|
| 22 |
#define IN6ADDRSZ 16 |
| 23 |
#define INADDRSZ 4 |
| 24 |
|
| 25 |
/* Dnsmasq handles domains names internally as NULL-terminated C strings. |
| 26 |
The '.' characters in these strings are significant as label-seperators. |
| 27 |
'.' and /0 characters _within_ labels are escaped and take up two |
| 28 |
characters to allow labels to contain arbitrary characters. |
| 29 |
|
| 30 |
The maximum length of a domain name in wire format is 255 bytes. |
| 31 |
and the maximum length of this when coverted to a <label>.<label> string |
| 32 |
is 253 characters. Reasoning: the wire format representation of a |
| 33 |
label is one byte length followed by up to length characters so each of |
| 34 |
these labels except the last takes the same space since the length is |
| 35 |
replaced by the '.' seperator. |
| 36 |
The last label doesn't have the "." so it takes one less character |
| 37 |
and the terminating zero-length label takes no character either. |
| 38 |
|
| 39 |
When we introduce the dnsmasq escaped format, the maximum length |
| 40 |
of this representation is when there are the minimum number of labels, |
| 41 |
since that gives us the maximum number of characters that may need |
| 42 |
escaping. The minimum number of labels is four, so the overhead |
| 43 |
is five bytes (four non-zero lengths plus the zero length terminator) |
| 44 |
leaving a total of 250 characters in labels. If every one of these |
| 45 |
is escaped and becomes two characters, that gives 500 characters, |
| 46 |
plus the three '.' seperators between the four labels, for a total of |
| 47 |
503 characters. |
| 48 |
*/ |
| 49 |
#define MAXDNAME 255 /* Maximum size of a domain name in wire format */ |
| 50 |
#define MAXDNAMESTR 503 /* Maximum size of dnsmasq c-string representaion of a domain name. */ |
| 51 |
|
| 52 |
#define PACKETSZ 512 /* maximum packet size */ |
| 53 |
#define RRFIXEDSZ 10 /* #/bytes of fixed data in r record */ |
| 54 |
#define MAXLABEL 63 /* maximum length of domain label */ |
| 55 |
|
| 56 |
#define NOERROR 0 /* no error */ |
| 57 |
#define FORMERR 1 /* format error */ |
| 58 |
#define SERVFAIL 2 /* server failure */ |
| 59 |
#define NXDOMAIN 3 /* non existent domain */ |
| 60 |
#define NOTIMP 4 /* not implemented */ |
| 61 |
#define REFUSED 5 /* query refused */ |
| 62 |
|
| 63 |
#define QUERY 0 /* opcode */ |
| 64 |
|
| 65 |
#define C_IN 1 /* the arpa internet */ |
| 66 |
#define C_CHAOS 3 /* for chaos net (MIT) */ |
| 67 |
#define C_HESIOD 4 /* hesiod */ |
| 68 |
#define C_ANY 255 /* wildcard match */ |
| 69 |
|
| 70 |
#define T_A 1 |
| 71 |
#define T_NS 2 |
| 72 |
#define T_MD 3 |
| 73 |
#define T_MF 4 |
| 74 |
#define T_CNAME 5 |
| 75 |
#define T_SOA 6 |
| 76 |
#define T_MB 7 |
| 77 |
#define T_MG 8 |
| 78 |
#define T_MR 9 |
| 79 |
#define T_PTR 12 |
| 80 |
#define T_MINFO 14 |
| 81 |
#define T_MX 15 |
| 82 |
#define T_TXT 16 |
| 83 |
#define T_RP 17 |
| 84 |
#define T_AFSDB 18 |
| 85 |
#define T_RT 21 |
| 86 |
#define T_SIG 24 |
| 87 |
#define T_PX 26 |
| 88 |
#define T_AAAA 28 |
| 89 |
#define T_NXT 30 |
| 90 |
#define T_SRV 33 |
| 91 |
#define T_NAPTR 35 |
| 92 |
#define T_KX 36 |
| 93 |
#define T_DNAME 39 |
| 94 |
#define T_OPT 41 |
| 95 |
#define T_DS 43 |
| 96 |
#define T_RRSIG 46 |
| 97 |
#define T_NSEC 47 |
| 98 |
#define T_DNSKEY 48 |
| 99 |
#define T_NSEC3 50 |
| 100 |
#define T_TKEY 249 |
| 101 |
#define T_TSIG 250 |
| 102 |
#define T_AXFR 252 |
| 103 |
#define T_MAILB 253 |
| 104 |
#define T_ANY 255 |
| 105 |
#define T_CAA 257 |
| 106 |
|
| 107 |
#define EDNS0_OPTION_MAC 65001 /* dyndns.org temporary assignment */ |
| 108 |
#define EDNS0_OPTION_CLIENT_SUBNET 8 /* IANA */ |
| 109 |
#define EDNS0_OPTION_EDE 15 /* IANA - RFC 8914 */ |
| 110 |
#define EDNS0_OPTION_NOMDEVICEID 65073 /* Nominum temporary assignment */ |
| 111 |
#define EDNS0_OPTION_NOMCPEID 65074 /* Nominum temporary assignment */ |
| 112 |
#define EDNS0_OPTION_UMBRELLA 20292 /* Cisco Umbrella temporary assignment */ |
| 113 |
|
| 114 |
/* RFC-8914 extended errors, negative values are our definitions */ |
| 115 |
#define EDE_US_SERVFAIL -2 /* SERVFAIL from usptream */ |
| 116 |
#define EDE_UNSET -1 /* No extended DNS error available */ |
| 117 |
#define EDE_OTHER 0 /* Other */ |
| 118 |
#define EDE_USUPDNSKEY 1 /* Unsupported DNSKEY algo */ |
| 119 |
#define EDE_USUPDS 2 /* Unsupported DS Digest */ |
| 120 |
#define EDE_STALE 3 /* Stale answer */ |
| 121 |
#define EDE_FORGED 4 /* Forged answer */ |
| 122 |
#define EDE_DNSSEC_IND 5 /* DNSSEC Indeterminate */ |
| 123 |
#define EDE_DNSSEC_BOGUS 6 /* DNSSEC Bogus */ |
| 124 |
#define EDE_SIG_EXP 7 /* Signature Expired */ |
| 125 |
#define EDE_SIG_NYV 8 /* Signature Not Yet Valid */ |
| 126 |
#define EDE_NO_DNSKEY 9 /* DNSKEY missing */ |
| 127 |
#define EDE_NO_RRSIG 10 /* RRSIGs missing */ |
| 128 |
#define EDE_NO_ZONEKEY 11 /* No Zone Key Bit Set */ |
| 129 |
#define EDE_NO_NSEC 12 /* NSEC Missing */ |
| 130 |
#define EDE_CACHED_ERR 13 /* Cached Error */ |
| 131 |
#define EDE_NOT_READY 14 /* Not Ready */ |
| 132 |
#define EDE_BLOCKED 15 /* Blocked */ |
| 133 |
#define EDE_CENSORED 16 /* Censored */ |
| 134 |
#define EDE_FILTERED 17 /* Filtered */ |
| 135 |
#define EDE_PROHIBITED 18 /* Prohibited */ |
| 136 |
#define EDE_STALE_NXD 19 /* Stale NXDOMAIN */ |
| 137 |
#define EDE_NOT_AUTH 20 /* Not Authoritative */ |
| 138 |
#define EDE_NOT_SUP 21 /* Not Supported */ |
| 139 |
#define EDE_NO_AUTH 22 /* No Reachable Authority */ |
| 140 |
#define EDE_NETERR 23 /* Network error */ |
| 141 |
#define EDE_INVALID_DATA 24 /* Invalid Data */ |
| 142 |
#define EDE_SIG_E_B_V 25 /* Signature Expired before Valid */ |
| 143 |
#define EDE_TOO_EARLY 26 /* To Early */ |
| 144 |
#define EDE_UNS_NS3_ITER 27 /* Unsupported NSEC3 Iterations Value */ |
| 145 |
#define EDE_UNABLE_POLICY 28 /* Unable to conform to policy */ |
| 146 |
#define EDE_SYNTHESIZED 29 /* Synthesized */ |
| 147 |
|
| 148 |
|
| 149 |
struct dns_header { |
| 150 |
u16 id; |
| 151 |
u8 hb3,hb4; |
| 152 |
u16 qdcount,ancount,nscount,arcount; |
| 153 |
}; |
| 154 |
|
| 155 |
#define HB3_QR 0x80 /* Query */ |
| 156 |
#define HB3_OPCODE 0x78 |
| 157 |
#define HB3_AA 0x04 /* Authoritative Answer */ |
| 158 |
#define HB3_TC 0x02 /* TrunCated */ |
| 159 |
#define HB3_RD 0x01 /* Recursion Desired */ |
| 160 |
|
| 161 |
#define HB4_RA 0x80 /* Recursion Available */ |
| 162 |
#define HB4_AD 0x20 /* Authenticated Data */ |
| 163 |
#define HB4_CD 0x10 /* Checking Disabled */ |
| 164 |
#define HB4_RCODE 0x0f |
| 165 |
|
| 166 |
#define OPCODE(x) (((x)->hb3 & HB3_OPCODE) >> 3) |
| 167 |
#define SET_OPCODE(x, code) (x)->hb3 = ((x)->hb3 & ~HB3_OPCODE) | code |
| 168 |
|
| 169 |
#define RCODE(x) ((x)->hb4 & HB4_RCODE) |
| 170 |
#define SET_RCODE(x, code) (x)->hb4 = ((x)->hb4 & ~HB4_RCODE) | code |
| 171 |
|
| 172 |
#define GETSHORT(s, cp) do { \ |
| 173 |
unsigned char *t_cp = (unsigned char *)(cp); \ |
| 174 |
(s) = ((u16)t_cp[0] << 8) \ |
| 175 |
| ((u16)t_cp[1]) \ |
| 176 |
; \ |
| 177 |
(cp) += 2; \ |
| 178 |
} while(0) |
| 179 |
|
| 180 |
#define GETLONG(l, cp) do { \ |
| 181 |
unsigned char *t_cp = (unsigned char *)(cp); \ |
| 182 |
(l) = ((u32)t_cp[0] << 24) \ |
| 183 |
| ((u32)t_cp[1] << 16) \ |
| 184 |
| ((u32)t_cp[2] << 8) \ |
| 185 |
| ((u32)t_cp[3]) \ |
| 186 |
; \ |
| 187 |
(cp) += 4; \ |
| 188 |
} while (0) |
| 189 |
|
| 190 |
#define PUTSHORT(s, cp) do { \ |
| 191 |
u16 t_s = (u16)(s); \ |
| 192 |
unsigned char *t_cp = (unsigned char *)(cp); \ |
| 193 |
*t_cp++ = t_s >> 8; \ |
| 194 |
*t_cp = t_s; \ |
| 195 |
(cp) += 2; \ |
| 196 |
} while(0) |
| 197 |
|
| 198 |
#define PUTLONG(l, cp) do { \ |
| 199 |
u32 t_l = (u32)(l); \ |
| 200 |
unsigned char *t_cp = (unsigned char *)(cp); \ |
| 201 |
*t_cp++ = t_l >> 24; \ |
| 202 |
*t_cp++ = t_l >> 16; \ |
| 203 |
*t_cp++ = t_l >> 8; \ |
| 204 |
*t_cp = t_l; \ |
| 205 |
(cp) += 4; \ |
| 206 |
} while (0) |
| 207 |
|
| 208 |
#define CHECK_LEN(header, pp, plen, len) \ |
| 209 |
((size_t)((pp) - (unsigned char *)(header) + (len)) <= (plen)) |
| 210 |
|
| 211 |
#define ADD_RDLEN(header, pp, plen, len) \ |
| 212 |
(!CHECK_LEN(header, pp, plen, len) ? 0 : (((pp) += (len)), 1)) |
| 213 |
|
| 214 |
/* Escape character in our internal c-string format for names. |
| 215 |
Cannot be '.' or /000 and must be !isprint(). |
| 216 |
Note that escaped chars are stored as |
| 217 |
<NAME_ESCAPE> <orig-char+1> |
| 218 |
to ensure that the escaped form of /000 doesn't include /000 |
| 219 |
*/ |
| 220 |
#define NAME_ESCAPE 1 |
| 221 |
#define IS_NAME_ESCAPE(x) ((x) == 0 || (x) == '.' || (x) == NAME_ESCAPE) |
| 222 |
|