]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/resolve/resolved-dns-packet.h
Merge pull request #2135 from zonque/resolved-mdns-3
[thirdparty/systemd.git] / src / resolve / resolved-dns-packet.h
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 #pragma once
4
5 /***
6 This file is part of systemd.
7
8 Copyright 2014 Lennart Poettering
9
10 systemd is free software; you can redistribute it and/or modify it
11 under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14
15 systemd is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Lesser General Public License for more details.
19
20 You should have received a copy of the GNU Lesser General Public License
21 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 ***/
23
24 #include <netinet/ip.h>
25 #include <netinet/udp.h>
26
27 #include "hashmap.h"
28 #include "in-addr-util.h"
29 #include "macro.h"
30 #include "sparse-endian.h"
31
32 typedef struct DnsPacketHeader DnsPacketHeader;
33 typedef struct DnsPacket DnsPacket;
34
35 #include "resolved-def.h"
36 #include "resolved-dns-answer.h"
37 #include "resolved-dns-question.h"
38 #include "resolved-dns-rr.h"
39
40 typedef enum DnsProtocol {
41 DNS_PROTOCOL_DNS,
42 DNS_PROTOCOL_MDNS,
43 DNS_PROTOCOL_LLMNR,
44 _DNS_PROTOCOL_MAX,
45 _DNS_PROTOCOL_INVALID = -1
46 } DnsProtocol;
47
48 struct DnsPacketHeader {
49 uint16_t id;
50 be16_t flags;
51 be16_t qdcount;
52 be16_t ancount;
53 be16_t nscount;
54 be16_t arcount;
55 };
56
57 #define DNS_PACKET_HEADER_SIZE sizeof(DnsPacketHeader)
58 #define UDP_PACKET_HEADER_SIZE (sizeof(struct iphdr) + sizeof(struct udphdr))
59
60 /* The various DNS protocols deviate in how large a packet can grow,
61 but the TCP transport has a 16bit size field, hence that appears to
62 be the absolute maximum. */
63 #define DNS_PACKET_SIZE_MAX 0xFFFF
64
65 /* RFC 1035 say 512 is the maximum, for classic unicast DNS */
66 #define DNS_PACKET_UNICAST_SIZE_MAX 512
67
68 /* With EDNS0 we can use larger packets, default to 4096, which is what is commonly used */
69 #define DNS_PACKET_UNICAST_SIZE_LARGE_MAX 4096
70
71 #define DNS_PACKET_SIZE_START 512
72
73 struct DnsPacket {
74 int n_ref;
75 DnsProtocol protocol;
76 size_t size, allocated, rindex;
77 void *_data; /* don't access directly, use DNS_PACKET_DATA()! */
78 Hashmap *names; /* For name compression */
79
80 /* Parsed data */
81 DnsQuestion *question;
82 DnsAnswer *answer;
83 DnsResourceRecord *opt;
84
85 /* Packet reception metadata */
86 int ifindex;
87 int family, ipproto;
88 union in_addr_union sender, destination;
89 uint16_t sender_port, destination_port;
90 uint32_t ttl;
91
92 /* For support of truncated packets */
93 DnsPacket *more;
94
95 bool on_stack:1;
96 bool extracted:1;
97 bool refuse_compression:1;
98 bool canonical_form:1;
99 };
100
101 static inline uint8_t* DNS_PACKET_DATA(DnsPacket *p) {
102 if (_unlikely_(!p))
103 return NULL;
104
105 if (p->_data)
106 return p->_data;
107
108 return ((uint8_t*) p) + ALIGN(sizeof(DnsPacket));
109 }
110
111 #define DNS_PACKET_HEADER(p) ((DnsPacketHeader*) DNS_PACKET_DATA(p))
112 #define DNS_PACKET_ID(p) DNS_PACKET_HEADER(p)->id
113 #define DNS_PACKET_QR(p) ((be16toh(DNS_PACKET_HEADER(p)->flags) >> 15) & 1)
114 #define DNS_PACKET_OPCODE(p) ((be16toh(DNS_PACKET_HEADER(p)->flags) >> 11) & 15)
115 #define DNS_PACKET_AA(p) ((be16toh(DNS_PACKET_HEADER(p)->flags) >> 10) & 1)
116 #define DNS_PACKET_TC(p) ((be16toh(DNS_PACKET_HEADER(p)->flags) >> 9) & 1)
117 #define DNS_PACKET_RD(p) ((be16toh(DNS_PACKET_HEADER(p)->flags) >> 8) & 1)
118 #define DNS_PACKET_RA(p) ((be16toh(DNS_PACKET_HEADER(p)->flags) >> 7) & 1)
119 #define DNS_PACKET_AD(p) ((be16toh(DNS_PACKET_HEADER(p)->flags) >> 5) & 1)
120 #define DNS_PACKET_CD(p) ((be16toh(DNS_PACKET_HEADER(p)->flags) >> 4) & 1)
121 #define DNS_PACKET_RCODE(p) (be16toh(DNS_PACKET_HEADER(p)->flags) & 15)
122
123 /* LLMNR defines some bits differently */
124 #define DNS_PACKET_LLMNR_C(p) DNS_PACKET_AA(p)
125 #define DNS_PACKET_LLMNR_T(p) DNS_PACKET_RD(p)
126
127 #define DNS_PACKET_QDCOUNT(p) be16toh(DNS_PACKET_HEADER(p)->qdcount)
128 #define DNS_PACKET_ANCOUNT(p) be16toh(DNS_PACKET_HEADER(p)->ancount)
129 #define DNS_PACKET_NSCOUNT(p) be16toh(DNS_PACKET_HEADER(p)->nscount)
130 #define DNS_PACKET_ARCOUNT(p) be16toh(DNS_PACKET_HEADER(p)->arcount)
131
132 #define DNS_PACKET_MAKE_FLAGS(qr, opcode, aa, tc, rd, ra, ad, cd, rcode) \
133 (((uint16_t) !!(qr) << 15) | \
134 ((uint16_t) ((opcode) & 15) << 11) | \
135 ((uint16_t) !!(aa) << 10) | /* on LLMNR: c */ \
136 ((uint16_t) !!(tc) << 9) | \
137 ((uint16_t) !!(rd) << 8) | /* on LLMNR: t */ \
138 ((uint16_t) !!(ra) << 7) | \
139 ((uint16_t) !!(ad) << 5) | \
140 ((uint16_t) !!(cd) << 4) | \
141 ((uint16_t) ((rcode) & 15)))
142
143 static inline unsigned DNS_PACKET_RRCOUNT(DnsPacket *p) {
144 return
145 (unsigned) DNS_PACKET_ANCOUNT(p) +
146 (unsigned) DNS_PACKET_NSCOUNT(p) +
147 (unsigned) DNS_PACKET_ARCOUNT(p);
148 }
149
150 int dns_packet_new(DnsPacket **p, DnsProtocol protocol, size_t mtu);
151 int dns_packet_new_query(DnsPacket **p, DnsProtocol protocol, size_t mtu, bool dnssec_checking_disabled);
152
153 void dns_packet_set_flags(DnsPacket *p, bool dnssec_checking_disabled, bool truncated);
154
155 DnsPacket *dns_packet_ref(DnsPacket *p);
156 DnsPacket *dns_packet_unref(DnsPacket *p);
157
158 DEFINE_TRIVIAL_CLEANUP_FUNC(DnsPacket*, dns_packet_unref);
159
160 int dns_packet_validate(DnsPacket *p);
161 int dns_packet_validate_reply(DnsPacket *p);
162 int dns_packet_validate_query(DnsPacket *p);
163
164 int dns_packet_is_reply_for(DnsPacket *p, const DnsResourceKey *key);
165
166 int dns_packet_append_blob(DnsPacket *p, const void *d, size_t sz, size_t *start);
167 int dns_packet_append_uint8(DnsPacket *p, uint8_t v, size_t *start);
168 int dns_packet_append_uint16(DnsPacket *p, uint16_t v, size_t *start);
169 int dns_packet_append_uint32(DnsPacket *p, uint32_t v, size_t *start);
170 int dns_packet_append_string(DnsPacket *p, const char *s, size_t *start);
171 int dns_packet_append_raw_string(DnsPacket *p, const void *s, size_t size, size_t *start);
172 int dns_packet_append_label(DnsPacket *p, const char *s, size_t l, size_t *start);
173 int dns_packet_append_name(DnsPacket *p, const char *name, bool allow_compression, size_t *start);
174 int dns_packet_append_key(DnsPacket *p, const DnsResourceKey *key, size_t *start);
175 int dns_packet_append_rr(DnsPacket *p, const DnsResourceRecord *rr, size_t *start, size_t *rdata_start);
176 int dns_packet_append_opt_rr(DnsPacket *p, uint16_t max_udp_size, bool edns0_do, size_t *start);
177
178 void dns_packet_truncate(DnsPacket *p, size_t sz);
179
180 int dns_packet_read(DnsPacket *p, size_t sz, const void **ret, size_t *start);
181 int dns_packet_read_blob(DnsPacket *p, void *d, size_t sz, size_t *start);
182 int dns_packet_read_uint8(DnsPacket *p, uint8_t *ret, size_t *start);
183 int dns_packet_read_uint16(DnsPacket *p, uint16_t *ret, size_t *start);
184 int dns_packet_read_uint32(DnsPacket *p, uint32_t *ret, size_t *start);
185 int dns_packet_read_string(DnsPacket *p, char **ret, size_t *start);
186 int dns_packet_read_raw_string(DnsPacket *p, const void **ret, size_t *size, size_t *start);
187 int dns_packet_read_name(DnsPacket *p, char **ret, bool allow_compression, size_t *start);
188 int dns_packet_read_key(DnsPacket *p, DnsResourceKey **ret, size_t *start);
189 int dns_packet_read_rr(DnsPacket *p, DnsResourceRecord **ret, size_t *start);
190
191 void dns_packet_rewind(DnsPacket *p, size_t idx);
192
193 int dns_packet_skip_question(DnsPacket *p);
194 int dns_packet_extract(DnsPacket *p);
195
196 static inline bool DNS_PACKET_SHALL_CACHE(DnsPacket *p) {
197 /* Never cache data originating from localhost, under the
198 * assumption, that it's coming from a locally DNS forwarder
199 * or server, that is caching on its own. */
200
201 return in_addr_is_localhost(p->family, &p->sender) == 0;
202 }
203
204 enum {
205 DNS_RCODE_SUCCESS = 0,
206 DNS_RCODE_FORMERR = 1,
207 DNS_RCODE_SERVFAIL = 2,
208 DNS_RCODE_NXDOMAIN = 3,
209 DNS_RCODE_NOTIMP = 4,
210 DNS_RCODE_REFUSED = 5,
211 DNS_RCODE_YXDOMAIN = 6,
212 DNS_RCODE_YXRRSET = 7,
213 DNS_RCODE_NXRRSET = 8,
214 DNS_RCODE_NOTAUTH = 9,
215 DNS_RCODE_NOTZONE = 10,
216 DNS_RCODE_BADVERS = 16,
217 DNS_RCODE_BADSIG = 16, /* duplicate value! */
218 DNS_RCODE_BADKEY = 17,
219 DNS_RCODE_BADTIME = 18,
220 DNS_RCODE_BADMODE = 19,
221 DNS_RCODE_BADNAME = 20,
222 DNS_RCODE_BADALG = 21,
223 DNS_RCODE_BADTRUNC = 22,
224 _DNS_RCODE_MAX_DEFINED
225 };
226
227 const char* dns_rcode_to_string(int i) _const_;
228 int dns_rcode_from_string(const char *s) _pure_;
229
230 const char* dns_protocol_to_string(DnsProtocol p) _const_;
231 DnsProtocol dns_protocol_from_string(const char *s) _pure_;
232
233 #define LLMNR_MULTICAST_IPV4_ADDRESS ((struct in_addr) { .s_addr = htobe32(224U << 24 | 252U) })
234 #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 } })
235
236 #define MDNS_MULTICAST_IPV4_ADDRESS ((struct in_addr) { .s_addr = htobe32(224U << 24 | 251U) })
237 #define MDNS_MULTICAST_IPV6_ADDRESS ((struct in6_addr) { .s6_addr = { 0xFF, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0xfb } })
238
239 static inline uint64_t SD_RESOLVED_FLAGS_MAKE(DnsProtocol protocol, int family, bool authenticated) {
240 uint64_t f;
241
242 /* Converts a protocol + family into a flags field as used in queries and responses */
243
244 f = authenticated ? SD_RESOLVED_AUTHENTICATED : 0;
245
246 switch (protocol) {
247 case DNS_PROTOCOL_DNS:
248 return f|SD_RESOLVED_DNS;
249
250 case DNS_PROTOCOL_LLMNR:
251 return f|(family == AF_INET6 ? SD_RESOLVED_LLMNR_IPV6 : SD_RESOLVED_LLMNR_IPV4);
252
253 case DNS_PROTOCOL_MDNS:
254 return family == AF_INET6 ? SD_RESOLVED_MDNS_IPV6 : SD_RESOLVED_MDNS_IPV4;
255
256 default:
257 break;
258 }
259
260 return 0;
261 }