]> git.proxmox.com Git - systemd.git/blame - src/resolve/resolved-dns-packet.h
Merge tag 'upstream/229'
[systemd.git] / src / resolve / resolved-dns-packet.h
CommitLineData
5eef597e
MP
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
7035cd9e 22#include <netinet/ip.h>
4c89c718 23#include <netinet/udp.h>
5eef597e 24
5eef597e
MP
25#include "hashmap.h"
26#include "in-addr-util.h"
4c89c718
MP
27#include "macro.h"
28#include "sparse-endian.h"
5eef597e
MP
29
30typedef struct DnsPacketHeader DnsPacketHeader;
31typedef struct DnsPacket DnsPacket;
32
5eef597e 33#include "resolved-def.h"
4c89c718
MP
34#include "resolved-dns-answer.h"
35#include "resolved-dns-question.h"
36#include "resolved-dns-rr.h"
5eef597e
MP
37
38typedef 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
46struct 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)
7035cd9e 56#define UDP_PACKET_HEADER_SIZE (sizeof(struct iphdr) + sizeof(struct udphdr))
5eef597e
MP
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
60 be the absolute maximum. */
61#define DNS_PACKET_SIZE_MAX 0xFFFF
62
63/* RFC 1035 say 512 is the maximum, for classic unicast DNS */
64#define DNS_PACKET_UNICAST_SIZE_MAX 512
65
4c89c718
MP
66/* With EDNS0 we can use larger packets, default to 4096, which is what is commonly used */
67#define DNS_PACKET_UNICAST_SIZE_LARGE_MAX 4096
68
5eef597e
MP
69#define DNS_PACKET_SIZE_START 512
70
71struct DnsPacket {
72 int n_ref;
73 DnsProtocol protocol;
74 size_t size, allocated, rindex;
75 void *_data; /* don't access directly, use DNS_PACKET_DATA()! */
76 Hashmap *names; /* For name compression */
4c89c718 77 size_t opt_start, opt_size;
5eef597e
MP
78
79 /* Parsed data */
80 DnsQuestion *question;
81 DnsAnswer *answer;
4c89c718 82 DnsResourceRecord *opt;
5eef597e 83
d9dfd233 84 /* Packet reception metadata */
5eef597e
MP
85 int ifindex;
86 int family, ipproto;
87 union in_addr_union sender, destination;
88 uint16_t sender_port, destination_port;
89 uint32_t ttl;
90
4c89c718
MP
91 /* For support of truncated packets */
92 DnsPacket *more;
93
94 bool on_stack:1;
95 bool extracted:1;
96 bool refuse_compression:1;
97 bool canonical_form:1;
5eef597e
MP
98};
99
100static inline uint8_t* DNS_PACKET_DATA(DnsPacket *p) {
101 if (_unlikely_(!p))
102 return NULL;
103
104 if (p->_data)
105 return p->_data;
106
107 return ((uint8_t*) p) + ALIGN(sizeof(DnsPacket));
108}
109
110#define DNS_PACKET_HEADER(p) ((DnsPacketHeader*) DNS_PACKET_DATA(p))
111#define DNS_PACKET_ID(p) DNS_PACKET_HEADER(p)->id
112#define DNS_PACKET_QR(p) ((be16toh(DNS_PACKET_HEADER(p)->flags) >> 15) & 1)
113#define DNS_PACKET_OPCODE(p) ((be16toh(DNS_PACKET_HEADER(p)->flags) >> 11) & 15)
7035cd9e 114#define DNS_PACKET_AA(p) ((be16toh(DNS_PACKET_HEADER(p)->flags) >> 10) & 1)
5eef597e 115#define DNS_PACKET_TC(p) ((be16toh(DNS_PACKET_HEADER(p)->flags) >> 9) & 1)
7035cd9e
MP
116#define DNS_PACKET_RD(p) ((be16toh(DNS_PACKET_HEADER(p)->flags) >> 8) & 1)
117#define DNS_PACKET_RA(p) ((be16toh(DNS_PACKET_HEADER(p)->flags) >> 7) & 1)
118#define DNS_PACKET_AD(p) ((be16toh(DNS_PACKET_HEADER(p)->flags) >> 5) & 1)
119#define DNS_PACKET_CD(p) ((be16toh(DNS_PACKET_HEADER(p)->flags) >> 4) & 1)
4c89c718
MP
120
121static 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
129 return rcode | (be16toh(DNS_PACKET_HEADER(p)->flags) & 15);
130}
7035cd9e
MP
131
132/* LLMNR defines some bits differently */
133#define DNS_PACKET_LLMNR_C(p) DNS_PACKET_AA(p)
134#define DNS_PACKET_LLMNR_T(p) DNS_PACKET_RD(p)
135
5eef597e
MP
136#define DNS_PACKET_QDCOUNT(p) be16toh(DNS_PACKET_HEADER(p)->qdcount)
137#define DNS_PACKET_ANCOUNT(p) be16toh(DNS_PACKET_HEADER(p)->ancount)
138#define DNS_PACKET_NSCOUNT(p) be16toh(DNS_PACKET_HEADER(p)->nscount)
139#define DNS_PACKET_ARCOUNT(p) be16toh(DNS_PACKET_HEADER(p)->arcount)
140
141#define DNS_PACKET_MAKE_FLAGS(qr, opcode, aa, tc, rd, ra, ad, cd, rcode) \
13d276d0
MP
142 (((uint16_t) !!(qr) << 15) | \
143 ((uint16_t) ((opcode) & 15) << 11) | \
144 ((uint16_t) !!(aa) << 10) | /* on LLMNR: c */ \
145 ((uint16_t) !!(tc) << 9) | \
146 ((uint16_t) !!(rd) << 8) | /* on LLMNR: t */ \
147 ((uint16_t) !!(ra) << 7) | \
148 ((uint16_t) !!(ad) << 5) | \
149 ((uint16_t) !!(cd) << 4) | \
150 ((uint16_t) ((rcode) & 15)))
5eef597e
MP
151
152static inline unsigned DNS_PACKET_RRCOUNT(DnsPacket *p) {
153 return
154 (unsigned) DNS_PACKET_ANCOUNT(p) +
155 (unsigned) DNS_PACKET_NSCOUNT(p) +
156 (unsigned) DNS_PACKET_ARCOUNT(p);
157}
158
159int dns_packet_new(DnsPacket **p, DnsProtocol protocol, size_t mtu);
4c89c718
MP
160int dns_packet_new_query(DnsPacket **p, DnsProtocol protocol, size_t mtu, bool dnssec_checking_disabled);
161
162void dns_packet_set_flags(DnsPacket *p, bool dnssec_checking_disabled, bool truncated);
5eef597e
MP
163
164DnsPacket *dns_packet_ref(DnsPacket *p);
165DnsPacket *dns_packet_unref(DnsPacket *p);
166
167DEFINE_TRIVIAL_CLEANUP_FUNC(DnsPacket*, dns_packet_unref);
168
169int dns_packet_validate(DnsPacket *p);
170int dns_packet_validate_reply(DnsPacket *p);
171int dns_packet_validate_query(DnsPacket *p);
172
4c89c718
MP
173int dns_packet_is_reply_for(DnsPacket *p, const DnsResourceKey *key);
174
5eef597e
MP
175int dns_packet_append_blob(DnsPacket *p, const void *d, size_t sz, size_t *start);
176int dns_packet_append_uint8(DnsPacket *p, uint8_t v, size_t *start);
177int dns_packet_append_uint16(DnsPacket *p, uint16_t v, size_t *start);
178int dns_packet_append_uint32(DnsPacket *p, uint32_t v, size_t *start);
179int dns_packet_append_string(DnsPacket *p, const char *s, size_t *start);
4c89c718
MP
180int dns_packet_append_raw_string(DnsPacket *p, const void *s, size_t size, size_t *start);
181int dns_packet_append_label(DnsPacket *p, const char *s, size_t l, bool canonical_candidate, size_t *start);
182int dns_packet_append_name(DnsPacket *p, const char *name, bool allow_compression, bool canonical_candidate, size_t *start);
5eef597e 183int dns_packet_append_key(DnsPacket *p, const DnsResourceKey *key, size_t *start);
4c89c718
MP
184int dns_packet_append_rr(DnsPacket *p, const DnsResourceRecord *rr, size_t *start, size_t *rdata_start);
185int dns_packet_append_opt(DnsPacket *p, uint16_t max_udp_size, bool edns0_do, size_t *start);
186
187void dns_packet_truncate(DnsPacket *p, size_t sz);
188int dns_packet_truncate_opt(DnsPacket *p);
5eef597e
MP
189
190int dns_packet_read(DnsPacket *p, size_t sz, const void **ret, size_t *start);
191int dns_packet_read_blob(DnsPacket *p, void *d, size_t sz, size_t *start);
192int dns_packet_read_uint8(DnsPacket *p, uint8_t *ret, size_t *start);
193int dns_packet_read_uint16(DnsPacket *p, uint16_t *ret, size_t *start);
194int dns_packet_read_uint32(DnsPacket *p, uint32_t *ret, size_t *start);
195int dns_packet_read_string(DnsPacket *p, char **ret, size_t *start);
4c89c718
MP
196int dns_packet_read_raw_string(DnsPacket *p, const void **ret, size_t *size, size_t *start);
197int dns_packet_read_name(DnsPacket *p, char **ret, bool allow_compression, size_t *start);
198int dns_packet_read_key(DnsPacket *p, DnsResourceKey **ret, bool *ret_cache_flush, size_t *start);
199int dns_packet_read_rr(DnsPacket *p, DnsResourceRecord **ret, bool *ret_cache_flush, size_t *start);
5eef597e
MP
200
201void dns_packet_rewind(DnsPacket *p, size_t idx);
202
203int dns_packet_skip_question(DnsPacket *p);
204int dns_packet_extract(DnsPacket *p);
205
4c89c718
MP
206static inline bool DNS_PACKET_SHALL_CACHE(DnsPacket *p) {
207 /* Never cache data originating from localhost, under the
208 * assumption, that it's coming from a locally DNS forwarder
209 * or server, that is caching on its own. */
210
211 return in_addr_is_localhost(p->family, &p->sender) == 0;
212}
213
214/* https://www.iana.org/assignments/dns-parameters/dns-parameters.xhtml#dns-parameters-6 */
5eef597e
MP
215enum {
216 DNS_RCODE_SUCCESS = 0,
217 DNS_RCODE_FORMERR = 1,
218 DNS_RCODE_SERVFAIL = 2,
219 DNS_RCODE_NXDOMAIN = 3,
220 DNS_RCODE_NOTIMP = 4,
221 DNS_RCODE_REFUSED = 5,
222 DNS_RCODE_YXDOMAIN = 6,
223 DNS_RCODE_YXRRSET = 7,
224 DNS_RCODE_NXRRSET = 8,
225 DNS_RCODE_NOTAUTH = 9,
226 DNS_RCODE_NOTZONE = 10,
227 DNS_RCODE_BADVERS = 16,
228 DNS_RCODE_BADSIG = 16, /* duplicate value! */
229 DNS_RCODE_BADKEY = 17,
230 DNS_RCODE_BADTIME = 18,
231 DNS_RCODE_BADMODE = 19,
232 DNS_RCODE_BADNAME = 20,
233 DNS_RCODE_BADALG = 21,
234 DNS_RCODE_BADTRUNC = 22,
235 _DNS_RCODE_MAX_DEFINED
236};
237
238const char* dns_rcode_to_string(int i) _const_;
239int dns_rcode_from_string(const char *s) _pure_;
240
241const char* dns_protocol_to_string(DnsProtocol p) _const_;
242DnsProtocol dns_protocol_from_string(const char *s) _pure_;
243
244#define LLMNR_MULTICAST_IPV4_ADDRESS ((struct in_addr) { .s_addr = htobe32(224U << 24 | 252U) })
245#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 } })
246
4c89c718
MP
247#define MDNS_MULTICAST_IPV4_ADDRESS ((struct in_addr) { .s_addr = htobe32(224U << 24 | 251U) })
248#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 } })
5eef597e 249
4c89c718
MP
250static inline uint64_t SD_RESOLVED_FLAGS_MAKE(DnsProtocol protocol, int family, bool authenticated) {
251 uint64_t f;
5eef597e 252
4c89c718 253 /* Converts a protocol + family into a flags field as used in queries and responses */
5eef597e 254
4c89c718 255 f = authenticated ? SD_RESOLVED_AUTHENTICATED : 0;
5eef597e 256
13d276d0
MP
257 switch (protocol) {
258 case DNS_PROTOCOL_DNS:
4c89c718 259 return f|SD_RESOLVED_DNS;
5eef597e 260
13d276d0 261 case DNS_PROTOCOL_LLMNR:
4c89c718
MP
262 return f|(family == AF_INET6 ? SD_RESOLVED_LLMNR_IPV6 : SD_RESOLVED_LLMNR_IPV4);
263
264 case DNS_PROTOCOL_MDNS:
265 return family == AF_INET6 ? SD_RESOLVED_MDNS_IPV6 : SD_RESOLVED_MDNS_IPV4;
5eef597e 266
13d276d0
MP
267 default:
268 break;
269 }
270
5eef597e
MP
271 return 0;
272}