lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | /* dnsmasq is Copyright (c) 2000-2021 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 | #define PACKETSZ 512 /* maximum packet size */ |
| 26 | #define MAXDNAME 1025 /* maximum presentation domain name */ |
| 27 | #define RRFIXEDSZ 10 /* #/bytes of fixed data in r record */ |
| 28 | #define MAXLABEL 63 /* maximum length of domain label */ |
| 29 | |
| 30 | #define NOERROR 0 /* no error */ |
| 31 | #define FORMERR 1 /* format error */ |
| 32 | #define SERVFAIL 2 /* server failure */ |
| 33 | #define NXDOMAIN 3 /* non existent domain */ |
| 34 | #define NOTIMP 4 /* not implemented */ |
| 35 | #define REFUSED 5 /* query refused */ |
| 36 | |
| 37 | #define QUERY 0 /* opcode */ |
| 38 | |
| 39 | #define C_IN 1 /* the arpa internet */ |
| 40 | #define C_CHAOS 3 /* for chaos net (MIT) */ |
| 41 | #define C_HESIOD 4 /* hesiod */ |
| 42 | #define C_ANY 255 /* wildcard match */ |
| 43 | |
| 44 | #define T_A 1 |
| 45 | #define T_NS 2 |
| 46 | #define T_MD 3 |
| 47 | #define T_MF 4 |
| 48 | #define T_CNAME 5 |
| 49 | #define T_SOA 6 |
| 50 | #define T_MB 7 |
| 51 | #define T_MG 8 |
| 52 | #define T_MR 9 |
| 53 | #define T_PTR 12 |
| 54 | #define T_MINFO 14 |
| 55 | #define T_MX 15 |
| 56 | #define T_TXT 16 |
| 57 | #define T_RP 17 |
| 58 | #define T_AFSDB 18 |
| 59 | #define T_RT 21 |
| 60 | #define T_SIG 24 |
| 61 | #define T_PX 26 |
| 62 | #define T_AAAA 28 |
| 63 | #define T_NXT 30 |
| 64 | #define T_SRV 33 |
| 65 | #define T_NAPTR 35 |
| 66 | #define T_KX 36 |
| 67 | #define T_DNAME 39 |
| 68 | #define T_OPT 41 |
| 69 | #define T_DS 43 |
| 70 | #define T_RRSIG 46 |
| 71 | #define T_NSEC 47 |
| 72 | #define T_DNSKEY 48 |
| 73 | #define T_NSEC3 50 |
| 74 | #define T_TKEY 249 |
| 75 | #define T_TSIG 250 |
| 76 | #define T_AXFR 252 |
| 77 | #define T_MAILB 253 |
| 78 | #define T_ANY 255 |
| 79 | #define T_CAA 257 |
| 80 | |
| 81 | #define EDNS0_OPTION_MAC 65001 /* dyndns.org temporary assignment */ |
| 82 | #define EDNS0_OPTION_CLIENT_SUBNET 8 /* IANA */ |
| 83 | #define EDNS0_OPTION_EDE 15 /* IANA - RFC 8914 */ |
| 84 | #define EDNS0_OPTION_NOMDEVICEID 65073 /* Nominum temporary assignment */ |
| 85 | #define EDNS0_OPTION_NOMCPEID 65074 /* Nominum temporary assignment */ |
| 86 | #define EDNS0_OPTION_UMBRELLA 20292 /* Cisco Umbrella temporary assignment */ |
| 87 | |
| 88 | /* RFC-8914 extended errors, negative values are our definitions */ |
| 89 | #define EDE_UNSET -1 /* No extended DNS error available */ |
| 90 | #define EDE_OTHER 0 /* Other */ |
| 91 | #define EDE_USUPDNSKEY 1 /* Unsupported DNSKEY algo */ |
| 92 | #define EDE_USUPDS 2 /* Unsupported DS Digest */ |
| 93 | #define EDE_STALE 3 /* Stale answer */ |
| 94 | #define EDE_FORGED 4 /* Forged answer */ |
| 95 | #define EDE_DNSSEC_IND 5 /* DNSSEC Indeterminate */ |
| 96 | #define EDE_DNSSEC_BOGUS 6 /* DNSSEC Bogus */ |
| 97 | #define EDE_SIG_EXP 7 /* Signature Expired */ |
| 98 | #define EDE_SIG_NYV 8 /* Signature Not Yet Valid */ |
| 99 | #define EDE_NO_DNSKEY 9 /* DNSKEY missing */ |
| 100 | #define EDE_NO_RRSIG 10 /* RRSIGs missing */ |
| 101 | #define EDE_NO_ZONEKEY 11 /* No Zone Key Bit Set */ |
| 102 | #define EDE_NO_NSEC 12 /* NSEC Missing */ |
| 103 | #define EDE_CACHED_ERR 13 /* Cached Error */ |
| 104 | #define EDE_NOT_READY 14 /* Not Ready */ |
| 105 | #define EDE_BLOCKED 15 /* Blocked */ |
| 106 | #define EDE_CENSORED 16 /* Censored */ |
| 107 | #define EDE_FILTERED 17 /* Filtered */ |
| 108 | #define EDE_PROHIBITED 18 /* Prohibited */ |
| 109 | #define EDE_STALE_NXD 19 /* Stale NXDOMAIN */ |
| 110 | #define EDE_NOT_AUTH 20 /* Not Authoritative */ |
| 111 | #define EDE_NOT_SUP 21 /* Not Supported */ |
| 112 | #define EDE_NO_AUTH 22 /* No Reachable Authority */ |
| 113 | #define EDE_NETERR 23 /* Network error */ |
| 114 | #define EDE_INVALID_DATA 24 /* Invalid Data */ |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | struct dns_header { |
| 120 | u16 id; |
| 121 | u8 hb3,hb4; |
| 122 | u16 qdcount,ancount,nscount,arcount; |
| 123 | }; |
| 124 | |
| 125 | #define HB3_QR 0x80 /* Query */ |
| 126 | #define HB3_OPCODE 0x78 |
| 127 | #define HB3_AA 0x04 /* Authoritative Answer */ |
| 128 | #define HB3_TC 0x02 /* TrunCated */ |
| 129 | #define HB3_RD 0x01 /* Recursion Desired */ |
| 130 | |
| 131 | #define HB4_RA 0x80 /* Recursion Available */ |
| 132 | #define HB4_AD 0x20 /* Authenticated Data */ |
| 133 | #define HB4_CD 0x10 /* Checking Disabled */ |
| 134 | #define HB4_RCODE 0x0f |
| 135 | |
| 136 | #define OPCODE(x) (((x)->hb3 & HB3_OPCODE) >> 3) |
| 137 | #define SET_OPCODE(x, code) (x)->hb3 = ((x)->hb3 & ~HB3_OPCODE) | code |
| 138 | |
| 139 | #define RCODE(x) ((x)->hb4 & HB4_RCODE) |
| 140 | #define SET_RCODE(x, code) (x)->hb4 = ((x)->hb4 & ~HB4_RCODE) | code |
| 141 | |
| 142 | #define GETSHORT(s, cp) { \ |
| 143 | unsigned char *t_cp = (unsigned char *)(cp); \ |
| 144 | (s) = ((u16)t_cp[0] << 8) \ |
| 145 | | ((u16)t_cp[1]) \ |
| 146 | ; \ |
| 147 | (cp) += 2; \ |
| 148 | } |
| 149 | |
| 150 | #define GETLONG(l, cp) { \ |
| 151 | unsigned char *t_cp = (unsigned char *)(cp); \ |
| 152 | (l) = ((u32)t_cp[0] << 24) \ |
| 153 | | ((u32)t_cp[1] << 16) \ |
| 154 | | ((u32)t_cp[2] << 8) \ |
| 155 | | ((u32)t_cp[3]) \ |
| 156 | ; \ |
| 157 | (cp) += 4; \ |
| 158 | } |
| 159 | |
| 160 | #define PUTSHORT(s, cp) { \ |
| 161 | u16 t_s = (u16)(s); \ |
| 162 | unsigned char *t_cp = (unsigned char *)(cp); \ |
| 163 | *t_cp++ = t_s >> 8; \ |
| 164 | *t_cp = t_s; \ |
| 165 | (cp) += 2; \ |
| 166 | } |
| 167 | |
| 168 | #define PUTLONG(l, cp) { \ |
| 169 | u32 t_l = (u32)(l); \ |
| 170 | unsigned char *t_cp = (unsigned char *)(cp); \ |
| 171 | *t_cp++ = t_l >> 24; \ |
| 172 | *t_cp++ = t_l >> 16; \ |
| 173 | *t_cp++ = t_l >> 8; \ |
| 174 | *t_cp = t_l; \ |
| 175 | (cp) += 4; \ |
| 176 | } |
| 177 | |
| 178 | #define CHECK_LEN(header, pp, plen, len) \ |
| 179 | ((size_t)((pp) - (unsigned char *)(header) + (len)) <= (plen)) |
| 180 | |
| 181 | #define ADD_RDLEN(header, pp, plen, len) \ |
| 182 | (!CHECK_LEN(header, pp, plen, len) ? 0 : (((pp) += (len)), 1)) |
| 183 | |
| 184 | /* Escape character in our presentation format for names. |
| 185 | Cannot be '.' or /000 and must be !isprint(). |
| 186 | Note that escaped chars are stored as |
| 187 | <NAME_ESCAPE> <orig-char+1> |
| 188 | to ensure that the escaped form of /000 doesn't include /000 |
| 189 | */ |
| 190 | #define NAME_ESCAPE 1 |