]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/resolve/resolved-dns-packet.h
Merge pull request #530 from dvdhrm/resolve-host-dbus
[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
25 #include "macro.h"
26 #include "sparse-endian.h"
27 #include "hashmap.h"
28 #include "in-addr-util.h"
29
30 typedef struct DnsPacketHeader DnsPacketHeader;
31 typedef struct DnsPacket DnsPacket;
32
33 #include "resolved-dns-rr.h"
34 #include "resolved-dns-question.h"
35 #include "resolved-dns-answer.h"
36 #include "resolved-def.h"
37
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
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)
56
57 /* The various DNS protocols deviate in how large a packet can grow,
58 but the TCP transport has a 16bit size field, hence that appears to
59 be the absolute maximum. */
60 #define DNS_PACKET_SIZE_MAX 0xFFFF
61
62 /* RFC 1035 say 512 is the maximum, for classic unicast DNS */
63 #define DNS_PACKET_UNICAST_SIZE_MAX 512
64
65 #define DNS_PACKET_SIZE_START 512
66
67 struct DnsPacket {
68 int n_ref;
69 DnsProtocol protocol;
70 size_t size, allocated, rindex;
71 void *_data; /* don't access directly, use DNS_PACKET_DATA()! */
72 Hashmap *names; /* For name compression */
73
74 /* Parsed data */
75 DnsQuestion *question;
76 DnsAnswer *answer;
77
78 /* Packet reception meta data */
79 int ifindex;
80 int family, ipproto;
81 union in_addr_union sender, destination;
82 uint16_t sender_port, destination_port;
83 uint32_t ttl;
84
85 bool extracted;
86 };
87
88 static inline uint8_t* DNS_PACKET_DATA(DnsPacket *p) {
89 if (_unlikely_(!p))
90 return NULL;
91
92 if (p->_data)
93 return p->_data;
94
95 return ((uint8_t*) p) + ALIGN(sizeof(DnsPacket));
96 }
97
98 #define DNS_PACKET_HEADER(p) ((DnsPacketHeader*) DNS_PACKET_DATA(p))
99 #define DNS_PACKET_ID(p) DNS_PACKET_HEADER(p)->id
100 #define DNS_PACKET_QR(p) ((be16toh(DNS_PACKET_HEADER(p)->flags) >> 15) & 1)
101 #define DNS_PACKET_OPCODE(p) ((be16toh(DNS_PACKET_HEADER(p)->flags) >> 11) & 15)
102 #define DNS_PACKET_AA(p) ((be16toh(DNS_PACKET_HEADER(p)->flags) >> 10) & 1)
103 #define DNS_PACKET_TC(p) ((be16toh(DNS_PACKET_HEADER(p)->flags) >> 9) & 1)
104 #define DNS_PACKET_RD(p) ((be16toh(DNS_PACKET_HEADER(p)->flags) >> 8) & 1)
105 #define DNS_PACKET_RA(p) ((be16toh(DNS_PACKET_HEADER(p)->flags) >> 7) & 1)
106 #define DNS_PACKET_AD(p) ((be16toh(DNS_PACKET_HEADER(p)->flags) >> 5) & 1)
107 #define DNS_PACKET_CD(p) ((be16toh(DNS_PACKET_HEADER(p)->flags) >> 4) & 1)
108 #define DNS_PACKET_RCODE(p) (be16toh(DNS_PACKET_HEADER(p)->flags) & 15)
109
110 /* LLMNR defines some bits differently */
111 #define DNS_PACKET_LLMNR_C(p) DNS_PACKET_AA(p)
112 #define DNS_PACKET_LLMNR_T(p) DNS_PACKET_RD(p)
113
114 #define DNS_PACKET_QDCOUNT(p) be16toh(DNS_PACKET_HEADER(p)->qdcount)
115 #define DNS_PACKET_ANCOUNT(p) be16toh(DNS_PACKET_HEADER(p)->ancount)
116 #define DNS_PACKET_NSCOUNT(p) be16toh(DNS_PACKET_HEADER(p)->nscount)
117 #define DNS_PACKET_ARCOUNT(p) be16toh(DNS_PACKET_HEADER(p)->arcount)
118
119 #define DNS_PACKET_MAKE_FLAGS(qr, opcode, aa, tc, rd, ra, ad, cd, rcode) \
120 (((uint16_t) !!qr << 15) | \
121 ((uint16_t) (opcode & 15) << 11) | \
122 ((uint16_t) !!aa << 10) | \
123 ((uint16_t) !!tc << 9) | \
124 ((uint16_t) !!rd << 8) | \
125 ((uint16_t) !!ra << 7) | \
126 ((uint16_t) !!ad << 5) | \
127 ((uint16_t) !!cd << 4) | \
128 ((uint16_t) (rcode & 15)))
129
130 static inline unsigned DNS_PACKET_RRCOUNT(DnsPacket *p) {
131 return
132 (unsigned) DNS_PACKET_ANCOUNT(p) +
133 (unsigned) DNS_PACKET_NSCOUNT(p) +
134 (unsigned) DNS_PACKET_ARCOUNT(p);
135 }
136
137 int dns_packet_new(DnsPacket **p, DnsProtocol protocol, size_t mtu);
138 int dns_packet_new_query(DnsPacket **p, DnsProtocol protocol, size_t mtu);
139
140 DnsPacket *dns_packet_ref(DnsPacket *p);
141 DnsPacket *dns_packet_unref(DnsPacket *p);
142
143 DEFINE_TRIVIAL_CLEANUP_FUNC(DnsPacket*, dns_packet_unref);
144
145 int dns_packet_validate(DnsPacket *p);
146 int dns_packet_validate_reply(DnsPacket *p);
147 int dns_packet_validate_query(DnsPacket *p);
148
149 int dns_packet_append_blob(DnsPacket *p, const void *d, size_t sz, size_t *start);
150 int dns_packet_append_uint8(DnsPacket *p, uint8_t v, size_t *start);
151 int dns_packet_append_uint16(DnsPacket *p, uint16_t v, size_t *start);
152 int dns_packet_append_uint32(DnsPacket *p, uint32_t v, size_t *start);
153 int dns_packet_append_string(DnsPacket *p, const char *s, size_t *start);
154 int dns_packet_append_label(DnsPacket *p, const char *s, size_t l, size_t *start);
155 int dns_packet_append_name(DnsPacket *p, const char *name,
156 bool allow_compression, size_t *start);
157 int dns_packet_append_key(DnsPacket *p, const DnsResourceKey *key, size_t *start);
158 int dns_packet_append_rr(DnsPacket *p, const DnsResourceRecord *rr, size_t *start);
159
160 int dns_packet_read(DnsPacket *p, size_t sz, const void **ret, size_t *start);
161 int dns_packet_read_blob(DnsPacket *p, void *d, size_t sz, size_t *start);
162 int dns_packet_read_uint8(DnsPacket *p, uint8_t *ret, size_t *start);
163 int dns_packet_read_uint16(DnsPacket *p, uint16_t *ret, size_t *start);
164 int dns_packet_read_uint32(DnsPacket *p, uint32_t *ret, size_t *start);
165 int dns_packet_read_string(DnsPacket *p, char **ret, size_t *start);
166 int dns_packet_read_name(DnsPacket *p, char **ret,
167 bool allow_compression, size_t *start);
168 int dns_packet_read_key(DnsPacket *p, DnsResourceKey **ret, size_t *start);
169 int dns_packet_read_rr(DnsPacket *p, DnsResourceRecord **ret, size_t *start);
170
171 void dns_packet_rewind(DnsPacket *p, size_t idx);
172
173 int dns_packet_skip_question(DnsPacket *p);
174 int dns_packet_extract(DnsPacket *p);
175
176 enum {
177 DNS_RCODE_SUCCESS = 0,
178 DNS_RCODE_FORMERR = 1,
179 DNS_RCODE_SERVFAIL = 2,
180 DNS_RCODE_NXDOMAIN = 3,
181 DNS_RCODE_NOTIMP = 4,
182 DNS_RCODE_REFUSED = 5,
183 DNS_RCODE_YXDOMAIN = 6,
184 DNS_RCODE_YXRRSET = 7,
185 DNS_RCODE_NXRRSET = 8,
186 DNS_RCODE_NOTAUTH = 9,
187 DNS_RCODE_NOTZONE = 10,
188 DNS_RCODE_BADVERS = 16,
189 DNS_RCODE_BADSIG = 16, /* duplicate value! */
190 DNS_RCODE_BADKEY = 17,
191 DNS_RCODE_BADTIME = 18,
192 DNS_RCODE_BADMODE = 19,
193 DNS_RCODE_BADNAME = 20,
194 DNS_RCODE_BADALG = 21,
195 DNS_RCODE_BADTRUNC = 22,
196 _DNS_RCODE_MAX_DEFINED
197 };
198
199 const char* dns_rcode_to_string(int i) _const_;
200 int dns_rcode_from_string(const char *s) _pure_;
201
202 const char* dns_protocol_to_string(DnsProtocol p) _const_;
203 DnsProtocol dns_protocol_from_string(const char *s) _pure_;
204
205 #define LLMNR_MULTICAST_IPV4_ADDRESS ((struct in_addr) { .s_addr = htobe32(224U << 24 | 252U) })
206 #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 } })
207
208 #define DNSKEY_FLAG_ZONE_KEY (1u << 8)
209 #define DNSKEY_FLAG_SEP (1u << 0)
210
211 static inline uint16_t dnskey_to_flags(const DnsResourceRecord *rr) {
212 return (rr->dnskey.zone_key_flag * DNSKEY_FLAG_ZONE_KEY |
213 rr->dnskey.sep_flag * DNSKEY_FLAG_SEP);
214 }
215
216 /* http://tools.ietf.org/html/rfc4034#appendix-A.1 */
217 enum {
218 DNSSEC_ALGORITHM_RSAMD5 = 1,
219 DNSSEC_ALGORITHM_DH,
220 DNSSEC_ALGORITHM_DSA,
221 DNSSEC_ALGORITHM_ECC,
222 DNSSEC_ALGORITHM_RSASHA1,
223 DNSSEC_ALGORITHM_INDIRECT = 252,
224 DNSSEC_ALGORITHM_PRIVATEDNS,
225 DNSSEC_ALGORITHM_PRIVATEOID,
226 _DNSSEC_ALGORITHM_MAX_DEFINED
227 };
228
229 const char* dnssec_algorithm_to_string(int i) _const_;
230 int dnssec_algorithm_from_string(const char *s) _pure_;
231
232 static inline uint64_t SD_RESOLVED_FLAGS_MAKE(DnsProtocol protocol, int family) {
233
234 /* Converts a protocol + family into a flags field as used in queries */
235
236 if (protocol == DNS_PROTOCOL_DNS)
237 return SD_RESOLVED_DNS;
238
239 if (protocol == DNS_PROTOCOL_LLMNR)
240 return family == AF_INET6 ? SD_RESOLVED_LLMNR_IPV6 : SD_RESOLVED_LLMNR_IPV4;
241
242 return 0;
243 }