Simon Kelley | 2a8710a | 2020-01-05 16:40:06 +0000 | [diff] [blame] | 1 | /* dnsmasq is Copyright (c) 2000-2020 Simon Kelley |
Simon Kelley | 572b41e | 2011-02-18 18:11:18 +0000 | [diff] [blame] | 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 | |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 17 | #define NAMESERVER_PORT 53 |
| 18 | #define TFTP_PORT 69 |
Simon Kelley | baf553d | 2018-01-29 22:49:27 +0000 | [diff] [blame] | 19 | #define MIN_PORT 1024 /* first non-reserved port */ |
Hans Dedecker | 926332a | 2016-01-23 10:48:12 +0000 | [diff] [blame] | 20 | #define MAX_PORT 65535u |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 21 | |
Simon Kelley | 572b41e | 2011-02-18 18:11:18 +0000 | [diff] [blame] | 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) */ |
Simon Kelley | cbf13a2 | 2014-01-25 17:59:14 +0000 | [diff] [blame] | 41 | #define C_HESIOD 4 /* hesiod */ |
Simon Kelley | 572b41e | 2011-02-18 18:11:18 +0000 | [diff] [blame] | 42 | #define C_ANY 255 /* wildcard match */ |
| 43 | |
| 44 | #define T_A 1 |
Simon Kelley | 5f8e58f | 2014-01-09 17:31:19 +0000 | [diff] [blame] | 45 | #define T_NS 2 |
| 46 | #define T_MD 3 |
| 47 | #define T_MF 4 |
Simon Kelley | 572b41e | 2011-02-18 18:11:18 +0000 | [diff] [blame] | 48 | #define T_CNAME 5 |
| 49 | #define T_SOA 6 |
Simon Kelley | 5f8e58f | 2014-01-09 17:31:19 +0000 | [diff] [blame] | 50 | #define T_MB 7 |
| 51 | #define T_MG 8 |
| 52 | #define T_MR 9 |
Simon Kelley | 572b41e | 2011-02-18 18:11:18 +0000 | [diff] [blame] | 53 | #define T_PTR 12 |
Simon Kelley | 5f8e58f | 2014-01-09 17:31:19 +0000 | [diff] [blame] | 54 | #define T_MINFO 14 |
Simon Kelley | 572b41e | 2011-02-18 18:11:18 +0000 | [diff] [blame] | 55 | #define T_MX 15 |
| 56 | #define T_TXT 16 |
Simon Kelley | 5f8e58f | 2014-01-09 17:31:19 +0000 | [diff] [blame] | 57 | #define T_RP 17 |
| 58 | #define T_AFSDB 18 |
| 59 | #define T_RT 21 |
Simon Kelley | 572b41e | 2011-02-18 18:11:18 +0000 | [diff] [blame] | 60 | #define T_SIG 24 |
Simon Kelley | 5f8e58f | 2014-01-09 17:31:19 +0000 | [diff] [blame] | 61 | #define T_PX 26 |
Simon Kelley | 572b41e | 2011-02-18 18:11:18 +0000 | [diff] [blame] | 62 | #define T_AAAA 28 |
Simon Kelley | 5f8e58f | 2014-01-09 17:31:19 +0000 | [diff] [blame] | 63 | #define T_NXT 30 |
Simon Kelley | 572b41e | 2011-02-18 18:11:18 +0000 | [diff] [blame] | 64 | #define T_SRV 33 |
| 65 | #define T_NAPTR 35 |
Simon Kelley | 5f8e58f | 2014-01-09 17:31:19 +0000 | [diff] [blame] | 66 | #define T_KX 36 |
| 67 | #define T_DNAME 39 |
Simon Kelley | 572b41e | 2011-02-18 18:11:18 +0000 | [diff] [blame] | 68 | #define T_OPT 41 |
Giovanni Bajo | a669f01 | 2012-04-05 02:47:28 +0200 | [diff] [blame] | 69 | #define T_DS 43 |
| 70 | #define T_RRSIG 46 |
| 71 | #define T_NSEC 47 |
| 72 | #define T_DNSKEY 48 |
Simon Kelley | 5107ace | 2014-02-23 10:48:32 +0000 | [diff] [blame] | 73 | #define T_NSEC3 50 |
Simon Kelley | 572b41e | 2011-02-18 18:11:18 +0000 | [diff] [blame] | 74 | #define T_TKEY 249 |
| 75 | #define T_TSIG 250 |
Simon Kelley | b75e936 | 2012-12-07 11:50:41 +0000 | [diff] [blame] | 76 | #define T_AXFR 252 |
Simon Kelley | 572b41e | 2011-02-18 18:11:18 +0000 | [diff] [blame] | 77 | #define T_MAILB 253 |
| 78 | #define T_ANY 255 |
Simon Kelley | 974a6d0 | 2018-08-23 23:01:16 +0100 | [diff] [blame] | 79 | #define T_CAA 257 |
Simon Kelley | 572b41e | 2011-02-18 18:11:18 +0000 | [diff] [blame] | 80 | |
Simon Kelley | ed4c076 | 2013-10-08 20:46:34 +0100 | [diff] [blame] | 81 | #define EDNS0_OPTION_MAC 65001 /* dyndns.org temporary assignment */ |
Simon Kelley | c3edf38 | 2013-10-10 21:09:15 +0100 | [diff] [blame] | 82 | #define EDNS0_OPTION_CLIENT_SUBNET 8 /* IANA */ |
Simon Kelley | 33702ab | 2015-12-28 23:17:15 +0000 | [diff] [blame] | 83 | #define EDNS0_OPTION_NOMDEVICEID 65073 /* Nominum temporary assignment */ |
| 84 | #define EDNS0_OPTION_NOMCPEID 65074 /* Nominum temporary assignment */ |
Simon Kelley | ed4c076 | 2013-10-08 20:46:34 +0100 | [diff] [blame] | 85 | |
Simon Kelley | 572b41e | 2011-02-18 18:11:18 +0000 | [diff] [blame] | 86 | struct dns_header { |
| 87 | u16 id; |
| 88 | u8 hb3,hb4; |
| 89 | u16 qdcount,ancount,nscount,arcount; |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 90 | }; |
Simon Kelley | 572b41e | 2011-02-18 18:11:18 +0000 | [diff] [blame] | 91 | |
swigger | bd7bfa2 | 2015-06-01 20:54:59 +0100 | [diff] [blame] | 92 | #define HB3_QR 0x80 /* Query */ |
Simon Kelley | 572b41e | 2011-02-18 18:11:18 +0000 | [diff] [blame] | 93 | #define HB3_OPCODE 0x78 |
swigger | bd7bfa2 | 2015-06-01 20:54:59 +0100 | [diff] [blame] | 94 | #define HB3_AA 0x04 /* Authoritative Answer */ |
| 95 | #define HB3_TC 0x02 /* TrunCated */ |
| 96 | #define HB3_RD 0x01 /* Recursion Desired */ |
Simon Kelley | 572b41e | 2011-02-18 18:11:18 +0000 | [diff] [blame] | 97 | |
swigger | bd7bfa2 | 2015-06-01 20:54:59 +0100 | [diff] [blame] | 98 | #define HB4_RA 0x80 /* Recursion Available */ |
| 99 | #define HB4_AD 0x20 /* Authenticated Data */ |
| 100 | #define HB4_CD 0x10 /* Checking Disabled */ |
Simon Kelley | 572b41e | 2011-02-18 18:11:18 +0000 | [diff] [blame] | 101 | #define HB4_RCODE 0x0f |
| 102 | |
| 103 | #define OPCODE(x) (((x)->hb3 & HB3_OPCODE) >> 3) |
Simon Kelley | 9d63304 | 2013-12-13 15:36:55 +0000 | [diff] [blame] | 104 | #define SET_OPCODE(x, code) (x)->hb3 = ((x)->hb3 & ~HB3_OPCODE) | code |
| 105 | |
Simon Kelley | 572b41e | 2011-02-18 18:11:18 +0000 | [diff] [blame] | 106 | #define RCODE(x) ((x)->hb4 & HB4_RCODE) |
| 107 | #define SET_RCODE(x, code) (x)->hb4 = ((x)->hb4 & ~HB4_RCODE) | code |
| 108 | |
| 109 | #define GETSHORT(s, cp) { \ |
| 110 | unsigned char *t_cp = (unsigned char *)(cp); \ |
| 111 | (s) = ((u16)t_cp[0] << 8) \ |
| 112 | | ((u16)t_cp[1]) \ |
| 113 | ; \ |
| 114 | (cp) += 2; \ |
| 115 | } |
| 116 | |
| 117 | #define GETLONG(l, cp) { \ |
| 118 | unsigned char *t_cp = (unsigned char *)(cp); \ |
| 119 | (l) = ((u32)t_cp[0] << 24) \ |
| 120 | | ((u32)t_cp[1] << 16) \ |
| 121 | | ((u32)t_cp[2] << 8) \ |
| 122 | | ((u32)t_cp[3]) \ |
| 123 | ; \ |
| 124 | (cp) += 4; \ |
| 125 | } |
| 126 | |
| 127 | #define PUTSHORT(s, cp) { \ |
| 128 | u16 t_s = (u16)(s); \ |
| 129 | unsigned char *t_cp = (unsigned char *)(cp); \ |
| 130 | *t_cp++ = t_s >> 8; \ |
| 131 | *t_cp = t_s; \ |
| 132 | (cp) += 2; \ |
| 133 | } |
| 134 | |
| 135 | #define PUTLONG(l, cp) { \ |
| 136 | u32 t_l = (u32)(l); \ |
| 137 | unsigned char *t_cp = (unsigned char *)(cp); \ |
| 138 | *t_cp++ = t_l >> 24; \ |
| 139 | *t_cp++ = t_l >> 16; \ |
| 140 | *t_cp++ = t_l >> 8; \ |
| 141 | *t_cp = t_l; \ |
| 142 | (cp) += 4; \ |
| 143 | } |
| 144 | |
Simon Kelley | c3e0b9b | 2013-12-31 13:50:39 +0000 | [diff] [blame] | 145 | #define CHECK_LEN(header, pp, plen, len) \ |
| 146 | ((size_t)((pp) - (unsigned char *)(header) + (len)) <= (plen)) |
| 147 | |
| 148 | #define ADD_RDLEN(header, pp, plen, len) \ |
| 149 | (!CHECK_LEN(header, pp, plen, len) ? 0 : (((pp) += (len)), 1)) |
Simon Kelley | cbe379a | 2015-04-21 22:57:06 +0100 | [diff] [blame] | 150 | |
| 151 | /* Escape character in our presentation format for names. |
Simon Kelley | b8f1655 | 2015-04-22 21:14:31 +0100 | [diff] [blame] | 152 | Cannot be '.' or /000 and must be !isprint(). |
| 153 | Note that escaped chars are stored as |
| 154 | <NAME_ESCAPE> <orig-char+1> |
| 155 | to ensure that the escaped form of /000 doesn't include /000 |
| 156 | */ |
Simon Kelley | cbe379a | 2015-04-21 22:57:06 +0100 | [diff] [blame] | 157 | #define NAME_ESCAPE 1 |