]>
Commit | Line | Data |
---|---|---|
74b2466e LP |
1 | #pragma once |
2 | ||
3 | /*** | |
4 | This file is part of systemd. | |
5 | ||
6 | Copyright 2014 Lennart Poettering | |
7 | ||
8 | systemd is free software; you can redistribute it and/or modify it | |
9 | under the terms of the GNU Lesser General Public License as published by | |
10 | the Free Software Foundation; either version 2.1 of the License, or | |
11 | (at your option) any later version. | |
12 | ||
13 | systemd is distributed in the hope that it will be useful, but | |
14 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | Lesser General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU Lesser General Public License | |
19 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
20 | ***/ | |
21 | ||
a0166609 | 22 | #include <netinet/ip.h> |
71d35b6b | 23 | #include <netinet/udp.h> |
74b2466e | 24 | |
74b2466e | 25 | #include "hashmap.h" |
7e8e0422 | 26 | #include "in-addr-util.h" |
71d35b6b TA |
27 | #include "macro.h" |
28 | #include "sparse-endian.h" | |
7e8e0422 LP |
29 | |
30 | typedef struct DnsPacketHeader DnsPacketHeader; | |
31 | typedef struct DnsPacket DnsPacket; | |
32 | ||
51323288 | 33 | #include "resolved-def.h" |
71d35b6b TA |
34 | #include "resolved-dns-answer.h" |
35 | #include "resolved-dns-question.h" | |
36 | #include "resolved-dns-rr.h" | |
74b2466e | 37 | |
1716f6dc LP |
38 | typedef enum DnsProtocol { |
39 | DNS_PROTOCOL_DNS, | |
40 | DNS_PROTOCOL_MDNS, | |
41 | DNS_PROTOCOL_LLMNR, | |
42 | _DNS_PROTOCOL_MAX, | |
43 | _DNS_PROTOCOL_INVALID = -1 | |
44 | } DnsProtocol; | |
45 | ||
74b2466e LP |
46 | struct DnsPacketHeader { |
47 | uint16_t id; | |
48 | be16_t flags; | |
49 | be16_t qdcount; | |
50 | be16_t ancount; | |
51 | be16_t nscount; | |
52 | be16_t arcount; | |
53 | }; | |
54 | ||
55 | #define DNS_PACKET_HEADER_SIZE sizeof(DnsPacketHeader) | |
a0166609 | 56 | #define UDP_PACKET_HEADER_SIZE (sizeof(struct iphdr) + sizeof(struct udphdr)) |
c73ce96b LP |
57 | |
58 | /* The various DNS protocols deviate in how large a packet can grow, | |
59 | but the TCP transport has a 16bit size field, hence that appears to | |
e1c95994 | 60 | be the absolute maximum. */ |
64a21fda | 61 | #define DNS_PACKET_SIZE_MAX 0xFFFFu |
e1c95994 LP |
62 | |
63 | /* RFC 1035 say 512 is the maximum, for classic unicast DNS */ | |
64a21fda | 64 | #define DNS_PACKET_UNICAST_SIZE_MAX 512u |
e1c95994 | 65 | |
d74fb368 | 66 | /* With EDNS0 we can use larger packets, default to 4096, which is what is commonly used */ |
64a21fda | 67 | #define DNS_PACKET_UNICAST_SIZE_LARGE_MAX 4096u |
d74fb368 | 68 | |
74b2466e LP |
69 | struct DnsPacket { |
70 | int n_ref; | |
1716f6dc | 71 | DnsProtocol protocol; |
74b2466e | 72 | size_t size, allocated, rindex; |
faa133f3 | 73 | void *_data; /* don't access directly, use DNS_PACKET_DATA()! */ |
74b2466e | 74 | Hashmap *names; /* For name compression */ |
519ef046 | 75 | size_t opt_start, opt_size; |
faa133f3 LP |
76 | |
77 | /* Parsed data */ | |
78 | DnsQuestion *question; | |
79 | DnsAnswer *answer; | |
d75acfb0 | 80 | DnsResourceRecord *opt; |
1716f6dc | 81 | |
dc61b7e4 | 82 | /* Packet reception metadata */ |
1716f6dc | 83 | int ifindex; |
623a4c97 | 84 | int family, ipproto; |
1716f6dc | 85 | union in_addr_union sender, destination; |
623a4c97 | 86 | uint16_t sender_port, destination_port; |
0dd25fb9 | 87 | uint32_t ttl; |
a4076574 | 88 | |
9c491563 DM |
89 | /* For support of truncated packets */ |
90 | DnsPacket *more; | |
91 | ||
a8812dd7 LP |
92 | bool on_stack:1; |
93 | bool extracted:1; | |
94 | bool refuse_compression:1; | |
95 | bool canonical_form:1; | |
74b2466e LP |
96 | }; |
97 | ||
98 | static inline uint8_t* DNS_PACKET_DATA(DnsPacket *p) { | |
99 | if (_unlikely_(!p)) | |
100 | return NULL; | |
101 | ||
faa133f3 LP |
102 | if (p->_data) |
103 | return p->_data; | |
74b2466e LP |
104 | |
105 | return ((uint8_t*) p) + ALIGN(sizeof(DnsPacket)); | |
106 | } | |
107 | ||
108 | #define DNS_PACKET_HEADER(p) ((DnsPacketHeader*) DNS_PACKET_DATA(p)) | |
3cb10d3a LP |
109 | #define DNS_PACKET_ID(p) DNS_PACKET_HEADER(p)->id |
110 | #define DNS_PACKET_QR(p) ((be16toh(DNS_PACKET_HEADER(p)->flags) >> 15) & 1) | |
111 | #define DNS_PACKET_OPCODE(p) ((be16toh(DNS_PACKET_HEADER(p)->flags) >> 11) & 15) | |
8b757a38 | 112 | #define DNS_PACKET_AA(p) ((be16toh(DNS_PACKET_HEADER(p)->flags) >> 10) & 1) |
ad867662 | 113 | #define DNS_PACKET_TC(p) ((be16toh(DNS_PACKET_HEADER(p)->flags) >> 9) & 1) |
8b757a38 DM |
114 | #define DNS_PACKET_RD(p) ((be16toh(DNS_PACKET_HEADER(p)->flags) >> 8) & 1) |
115 | #define DNS_PACKET_RA(p) ((be16toh(DNS_PACKET_HEADER(p)->flags) >> 7) & 1) | |
116 | #define DNS_PACKET_AD(p) ((be16toh(DNS_PACKET_HEADER(p)->flags) >> 5) & 1) | |
117 | #define DNS_PACKET_CD(p) ((be16toh(DNS_PACKET_HEADER(p)->flags) >> 4) & 1) | |
f3abbe25 | 118 | |
b30bf55d LP |
119 | #define DNS_PACKET_FLAG_TC (UINT16_C(1) << 9) |
120 | ||
f3abbe25 LP |
121 | static inline uint16_t DNS_PACKET_RCODE(DnsPacket *p) { |
122 | uint16_t rcode; | |
123 | ||
124 | if (p->opt) | |
125 | rcode = (uint16_t) (p->opt->ttl >> 24); | |
126 | else | |
127 | rcode = 0; | |
128 | ||
b30bf55d LP |
129 | return rcode | (be16toh(DNS_PACKET_HEADER(p)->flags) & 0xF); |
130 | } | |
131 | ||
132 | static inline uint16_t DNS_PACKET_PAYLOAD_SIZE_MAX(DnsPacket *p) { | |
133 | ||
134 | /* Returns the advertised maximum datagram size for replies, or the DNS default if there's nothing defined. */ | |
135 | ||
136 | if (p->opt) | |
137 | return MAX(DNS_PACKET_UNICAST_SIZE_MAX, p->opt->key->class); | |
138 | ||
139 | return DNS_PACKET_UNICAST_SIZE_MAX; | |
140 | } | |
141 | ||
142 | static inline bool DNS_PACKET_DO(DnsPacket *p) { | |
143 | if (!p->opt) | |
144 | return false; | |
145 | ||
146 | return !!(p->opt->ttl & (1U << 15)); | |
147 | } | |
148 | ||
149 | static inline bool DNS_PACKET_VERSION_SUPPORTED(DnsPacket *p) { | |
150 | /* Returns true if this packet is in a version we support. Which means either non-EDNS or EDNS(0), but not EDNS | |
151 | * of any newer versions */ | |
152 | ||
153 | if (!p->opt) | |
154 | return true; | |
155 | ||
156 | return DNS_RESOURCE_RECORD_OPT_VERSION_SUPPORTED(p->opt); | |
f3abbe25 | 157 | } |
8b757a38 DM |
158 | |
159 | /* LLMNR defines some bits differently */ | |
160 | #define DNS_PACKET_LLMNR_C(p) DNS_PACKET_AA(p) | |
161 | #define DNS_PACKET_LLMNR_T(p) DNS_PACKET_RD(p) | |
162 | ||
3cb10d3a LP |
163 | #define DNS_PACKET_QDCOUNT(p) be16toh(DNS_PACKET_HEADER(p)->qdcount) |
164 | #define DNS_PACKET_ANCOUNT(p) be16toh(DNS_PACKET_HEADER(p)->ancount) | |
165 | #define DNS_PACKET_NSCOUNT(p) be16toh(DNS_PACKET_HEADER(p)->nscount) | |
166 | #define DNS_PACKET_ARCOUNT(p) be16toh(DNS_PACKET_HEADER(p)->arcount) | |
167 | ||
168 | #define DNS_PACKET_MAKE_FLAGS(qr, opcode, aa, tc, rd, ra, ad, cd, rcode) \ | |
f0258e47 LP |
169 | (((uint16_t) !!(qr) << 15) | \ |
170 | ((uint16_t) ((opcode) & 15) << 11) | \ | |
171 | ((uint16_t) !!(aa) << 10) | /* on LLMNR: c */ \ | |
172 | ((uint16_t) !!(tc) << 9) | \ | |
173 | ((uint16_t) !!(rd) << 8) | /* on LLMNR: t */ \ | |
174 | ((uint16_t) !!(ra) << 7) | \ | |
175 | ((uint16_t) !!(ad) << 5) | \ | |
176 | ((uint16_t) !!(cd) << 4) | \ | |
177 | ((uint16_t) ((rcode) & 15))) | |
3cb10d3a | 178 | |
322345fd LP |
179 | static inline unsigned DNS_PACKET_RRCOUNT(DnsPacket *p) { |
180 | return | |
181 | (unsigned) DNS_PACKET_ANCOUNT(p) + | |
182 | (unsigned) DNS_PACKET_NSCOUNT(p) + | |
183 | (unsigned) DNS_PACKET_ARCOUNT(p); | |
184 | } | |
74b2466e | 185 | |
1716f6dc | 186 | int dns_packet_new(DnsPacket **p, DnsProtocol protocol, size_t mtu); |
24710c48 | 187 | int dns_packet_new_query(DnsPacket **p, DnsProtocol protocol, size_t mtu, bool dnssec_checking_disabled); |
74b2466e | 188 | |
dbfbb6e7 DM |
189 | void dns_packet_set_flags(DnsPacket *p, bool dnssec_checking_disabled, bool truncated); |
190 | ||
74b2466e LP |
191 | DnsPacket *dns_packet_ref(DnsPacket *p); |
192 | DnsPacket *dns_packet_unref(DnsPacket *p); | |
193 | ||
194 | DEFINE_TRIVIAL_CLEANUP_FUNC(DnsPacket*, dns_packet_unref); | |
195 | ||
196 | int dns_packet_validate(DnsPacket *p); | |
197 | int dns_packet_validate_reply(DnsPacket *p); | |
623a4c97 | 198 | int dns_packet_validate_query(DnsPacket *p); |
74b2466e | 199 | |
8af5b883 LP |
200 | int dns_packet_is_reply_for(DnsPacket *p, const DnsResourceKey *key); |
201 | ||
623a4c97 | 202 | int dns_packet_append_blob(DnsPacket *p, const void *d, size_t sz, size_t *start); |
74b2466e LP |
203 | int dns_packet_append_uint8(DnsPacket *p, uint8_t v, size_t *start); |
204 | int dns_packet_append_uint16(DnsPacket *p, uint16_t v, size_t *start); | |
623a4c97 | 205 | int dns_packet_append_uint32(DnsPacket *p, uint32_t v, size_t *start); |
74b2466e | 206 | int dns_packet_append_string(DnsPacket *p, const char *s, size_t *start); |
2001c805 | 207 | int dns_packet_append_raw_string(DnsPacket *p, const void *s, size_t size, size_t *start); |
a3db237b LP |
208 | int dns_packet_append_label(DnsPacket *p, const char *s, size_t l, bool canonical_candidate, size_t *start); |
209 | int dns_packet_append_name(DnsPacket *p, const char *name, bool allow_compression, bool canonical_candidate, size_t *start); | |
58ab31d5 DR |
210 | int dns_packet_append_key(DnsPacket *p, const DnsResourceKey *key, const DnsAnswerFlags flags, size_t *start); |
211 | int dns_packet_append_rr(DnsPacket *p, const DnsResourceRecord *rr, const DnsAnswerFlags flags, size_t *start, size_t *rdata_start); | |
f2ed4c69 | 212 | int dns_packet_append_opt(DnsPacket *p, uint16_t max_udp_size, bool edns0_do, int rcode, size_t *start); |
f471bc11 LP |
213 | int dns_packet_append_question(DnsPacket *p, DnsQuestion *q); |
214 | int dns_packet_append_answer(DnsPacket *p, DnsAnswer *a); | |
74b2466e | 215 | |
9c5e12a4 | 216 | void dns_packet_truncate(DnsPacket *p, size_t sz); |
519ef046 | 217 | int dns_packet_truncate_opt(DnsPacket *p); |
9c5e12a4 | 218 | |
74b2466e | 219 | int dns_packet_read(DnsPacket *p, size_t sz, const void **ret, size_t *start); |
623a4c97 | 220 | int dns_packet_read_blob(DnsPacket *p, void *d, size_t sz, size_t *start); |
74b2466e LP |
221 | int dns_packet_read_uint8(DnsPacket *p, uint8_t *ret, size_t *start); |
222 | int dns_packet_read_uint16(DnsPacket *p, uint16_t *ret, size_t *start); | |
223 | int dns_packet_read_uint32(DnsPacket *p, uint32_t *ret, size_t *start); | |
224 | int dns_packet_read_string(DnsPacket *p, char **ret, size_t *start); | |
2001c805 LP |
225 | int dns_packet_read_raw_string(DnsPacket *p, const void **ret, size_t *size, size_t *start); |
226 | int dns_packet_read_name(DnsPacket *p, char **ret, bool allow_compression, size_t *start); | |
d2579eec LP |
227 | int dns_packet_read_key(DnsPacket *p, DnsResourceKey **ret, bool *ret_cache_flush, size_t *start); |
228 | int dns_packet_read_rr(DnsPacket *p, DnsResourceRecord **ret, bool *ret_cache_flush, size_t *start); | |
74b2466e | 229 | |
8ba9fd9c LP |
230 | void dns_packet_rewind(DnsPacket *p, size_t idx); |
231 | ||
74b2466e | 232 | int dns_packet_skip_question(DnsPacket *p); |
faa133f3 | 233 | int dns_packet_extract(DnsPacket *p); |
74b2466e | 234 | |
d830ebbd LP |
235 | static inline bool DNS_PACKET_SHALL_CACHE(DnsPacket *p) { |
236 | /* Never cache data originating from localhost, under the | |
237 | * assumption, that it's coming from a locally DNS forwarder | |
238 | * or server, that is caching on its own. */ | |
239 | ||
240 | return in_addr_is_localhost(p->family, &p->sender) == 0; | |
241 | } | |
242 | ||
f3abbe25 | 243 | /* https://www.iana.org/assignments/dns-parameters/dns-parameters.xhtml#dns-parameters-6 */ |
74b2466e LP |
244 | enum { |
245 | DNS_RCODE_SUCCESS = 0, | |
246 | DNS_RCODE_FORMERR = 1, | |
247 | DNS_RCODE_SERVFAIL = 2, | |
248 | DNS_RCODE_NXDOMAIN = 3, | |
249 | DNS_RCODE_NOTIMP = 4, | |
250 | DNS_RCODE_REFUSED = 5, | |
251 | DNS_RCODE_YXDOMAIN = 6, | |
252 | DNS_RCODE_YXRRSET = 7, | |
253 | DNS_RCODE_NXRRSET = 8, | |
254 | DNS_RCODE_NOTAUTH = 9, | |
255 | DNS_RCODE_NOTZONE = 10, | |
256 | DNS_RCODE_BADVERS = 16, | |
257 | DNS_RCODE_BADSIG = 16, /* duplicate value! */ | |
258 | DNS_RCODE_BADKEY = 17, | |
259 | DNS_RCODE_BADTIME = 18, | |
260 | DNS_RCODE_BADMODE = 19, | |
261 | DNS_RCODE_BADNAME = 20, | |
262 | DNS_RCODE_BADALG = 21, | |
263 | DNS_RCODE_BADTRUNC = 22, | |
6f21e066 | 264 | DNS_RCODE_BADCOOKIE = 23, |
f2ed4c69 LP |
265 | _DNS_RCODE_MAX_DEFINED, |
266 | _DNS_RCODE_MAX = 4095 /* 4 bit rcode in the header plus 8 bit rcode in OPT, makes 12 bit */ | |
74b2466e LP |
267 | }; |
268 | ||
269 | const char* dns_rcode_to_string(int i) _const_; | |
270 | int dns_rcode_from_string(const char *s) _pure_; | |
1716f6dc LP |
271 | |
272 | const char* dns_protocol_to_string(DnsProtocol p) _const_; | |
273 | DnsProtocol dns_protocol_from_string(const char *s) _pure_; | |
274 | ||
275 | #define LLMNR_MULTICAST_IPV4_ADDRESS ((struct in_addr) { .s_addr = htobe32(224U << 24 | 252U) }) | |
276 | #define LLMNR_MULTICAST_IPV6_ADDRESS ((struct in6_addr) { .s6_addr = { 0xFF, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x03 } }) | |
8db0d2f5 | 277 | |
0db4c90a | 278 | #define MDNS_MULTICAST_IPV4_ADDRESS ((struct in_addr) { .s_addr = htobe32(224U << 24 | 251U) }) |
b6c5d46b | 279 | #define MDNS_MULTICAST_IPV6_ADDRESS ((struct in6_addr) { .s6_addr = { 0xFF, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfb } }) |
0db4c90a | 280 | |
931851e8 LP |
281 | static inline uint64_t SD_RESOLVED_FLAGS_MAKE(DnsProtocol protocol, int family, bool authenticated) { |
282 | uint64_t f; | |
51323288 | 283 | |
931851e8 LP |
284 | /* Converts a protocol + family into a flags field as used in queries and responses */ |
285 | ||
286 | f = authenticated ? SD_RESOLVED_AUTHENTICATED : 0; | |
51323288 | 287 | |
106784eb DM |
288 | switch (protocol) { |
289 | case DNS_PROTOCOL_DNS: | |
931851e8 | 290 | return f|SD_RESOLVED_DNS; |
51323288 | 291 | |
106784eb | 292 | case DNS_PROTOCOL_LLMNR: |
931851e8 | 293 | return f|(family == AF_INET6 ? SD_RESOLVED_LLMNR_IPV6 : SD_RESOLVED_LLMNR_IPV4); |
51323288 | 294 | |
4e5bf5e1 | 295 | case DNS_PROTOCOL_MDNS: |
12e1893a | 296 | return f|(family == AF_INET6 ? SD_RESOLVED_MDNS_IPV6 : SD_RESOLVED_MDNS_IPV4); |
4e5bf5e1 | 297 | |
106784eb | 298 | default: |
12e1893a | 299 | return f; |
106784eb | 300 | } |
51323288 | 301 | } |