blob: 1c6855172909000cbc8a9e8fdafd5ae43f1d1ca0 [file] [log] [blame]
Baptiste Assmann325137d2015-04-13 23:40:55 +02001/*
2 * include/types/dns.h
3 * This file provides structures and types for DNS.
4 *
5 * Copyright (C) 2014 Baptiste Assmann <bedis9@gmail.com>
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation, version 2.1
10 * exclusively.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#ifndef _TYPES_DNS_H
23#define _TYPES_DNS_H
24
Christopher Faulet67957bd2017-09-27 11:00:59 +020025#include <eb32tree.h>
26
27#include <common/mini-clist.h>
28
29#include <types/connection.h>
30#include <types/obj_type.h>
31#include <types/proto_udp.h>
32#include <types/proxy.h>
33#include <types/server.h>
34#include <types/task.h>
35
Baptiste Assmann325137d2015-04-13 23:40:55 +020036/*DNS maximum values */
37/*
38 * Maximum issued from RFC:
39 * RFC 1035: https://www.ietf.org/rfc/rfc1035.txt chapter 2.3.4
40 * RFC 2671: http://tools.ietf.org/html/rfc2671
41 */
Christopher Faulet67957bd2017-09-27 11:00:59 +020042#define DNS_MAX_LABEL_SIZE 63
43#define DNS_MAX_NAME_SIZE 255
44#define DNS_MAX_UDP_MESSAGE 8192
Baptiste Assmann325137d2015-04-13 23:40:55 +020045
Baptiste Assmann4ec076f2015-12-09 14:02:01 +010046/* DNS minimun record size: 1 char + 1 NULL + type + class */
Christopher Faulet67957bd2017-09-27 11:00:59 +020047#define DNS_MIN_RECORD_SIZE (1 + 1 + 2 + 2)
Baptiste Assmann4ec076f2015-12-09 14:02:01 +010048
Baptiste Assmannd0aa6d22017-04-03 14:40:20 +020049/* DNS smallest fqdn 'a.gl' size */
Christopher Faulet67957bd2017-09-27 11:00:59 +020050# define DNS_SMALLEST_FQDN_SIZE 4
Baptiste Assmannd0aa6d22017-04-03 14:40:20 +020051
Baptiste Assmann4ec076f2015-12-09 14:02:01 +010052/* maximum number of query records in a DNS response
53 * For now, we allow only one */
54#define DNS_MAX_QUERY_RECORDS 1
55
56/* maximum number of answer record in a DNS response */
57#define DNS_MAX_ANSWER_RECORDS ((DNS_MAX_UDP_MESSAGE - DNS_HEADER_SIZE) / DNS_MIN_RECORD_SIZE)
58
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +020059/* size of dns_buffer used to store responses from the buffer
60 * dns_buffer is used to store data collected from records found in a response.
61 * Before using it, caller will always check that there is at least DNS_MAX_NAME_SIZE bytes
62 * available */
63#define DNS_ANALYZE_BUFFER_SIZE DNS_MAX_UDP_MESSAGE + DNS_MAX_NAME_SIZE
64
Baptiste Assmann325137d2015-04-13 23:40:55 +020065/* DNS error messages */
Christopher Faulet67957bd2017-09-27 11:00:59 +020066#define DNS_TOO_LONG_FQDN "hostname too long"
67#define DNS_LABEL_TOO_LONG "one label too long"
68#define DNS_INVALID_CHARACTER "found an invalid character"
Baptiste Assmann325137d2015-04-13 23:40:55 +020069
70/* dns query class */
Christopher Faulet67957bd2017-09-27 11:00:59 +020071#define DNS_RCLASS_IN 1 /* internet class */
Baptiste Assmann325137d2015-04-13 23:40:55 +020072
73/* dns record types (non exhaustive list) */
Christopher Faulet67957bd2017-09-27 11:00:59 +020074#define DNS_RTYPE_A 1 /* IPv4 address */
75#define DNS_RTYPE_CNAME 5 /* canonical name */
76#define DNS_RTYPE_AAAA 28 /* IPv6 address */
77#define DNS_RTYPE_SRV 33 /* SRV record */
78#define DNS_RTYPE_OPT 41 /* OPT */
79#define DNS_RTYPE_ANY 255 /* all records */
Baptiste Assmann325137d2015-04-13 23:40:55 +020080
81/* dns rcode values */
Christopher Faulet67957bd2017-09-27 11:00:59 +020082#define DNS_RCODE_NO_ERROR 0 /* no error */
83#define DNS_RCODE_NX_DOMAIN 3 /* non existent domain */
84#define DNS_RCODE_REFUSED 5 /* query refused */
Baptiste Assmann325137d2015-04-13 23:40:55 +020085
Baptiste Assmann042d0a12015-09-02 21:52:37 +020086/* dns flags masks */
Christopher Faulet67957bd2017-09-27 11:00:59 +020087#define DNS_FLAG_TRUNCATED 0x0200 /* mask for truncated flag */
88#define DNS_FLAG_REPLYCODE 0x000F /* mask for reply code */
Baptiste Assmann042d0a12015-09-02 21:52:37 +020089
Thierry Fournierac88cfe2016-02-17 22:05:30 +010090/* max number of network preference entries are avalaible from the
91 * configuration file.
92 */
93#define SRV_MAX_PREF_NET 5
94
Baptiste Assmanned97c952015-07-21 15:34:51 +020095/* DNS header size */
Christopher Faulet67957bd2017-09-27 11:00:59 +020096#define DNS_HEADER_SIZE ((int)sizeof(struct dns_header))
Baptiste Assmann201c07f2017-05-22 15:17:15 +020097
Baptiste Assmann325137d2015-04-13 23:40:55 +020098/* DNS request or response header structure */
99struct dns_header {
Nenad Merdanovic8ab79422016-07-13 14:03:43 +0200100 uint16_t id;
101 uint16_t flags;
102 uint16_t qdcount;
103 uint16_t ancount;
104 uint16_t nscount;
105 uint16_t arcount;
106} __attribute__ ((packed));
Baptiste Assmann325137d2015-04-13 23:40:55 +0200107
108/* short structure to describe a DNS question */
Baptiste Assmann83b0a172016-09-05 19:09:49 +0200109/* NOTE: big endian structure */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200110struct dns_question {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200111 unsigned short qtype; /* question type */
112 unsigned short qclass; /* query class */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200113};
114
Baptiste Assmann83b0a172016-09-05 19:09:49 +0200115/* NOTE: big endian structure */
Baptiste Assmann5748f732015-07-21 15:36:24 +0200116struct dns_query_item {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200117 char name[DNS_MAX_NAME_SIZE]; /* query name */
118 unsigned short type; /* question type */
119 unsigned short class; /* query class */
120 struct list list;
Baptiste Assmann5748f732015-07-21 15:36:24 +0200121};
122
Baptiste Assmann83b0a172016-09-05 19:09:49 +0200123/* NOTE: big endian structure */
Baptiste Assmann2af08fe2017-08-14 00:13:01 +0200124struct dns_additional_record {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200125 uint8_t name; /* domain name, must be 0 (RFC 6891) */
126 uint16_t type; /* record type DNS_RTYPE_OPT (41) */
127 uint16_t udp_payload_size; /* maximum size accepted for the response */
128 uint32_t extension; /* extended rcode and flags, not used for now */
129 uint16_t data_length; /* data length */
130/* as of today, we don't support yet edns options, that said I already put a
131 * placeholder here for this purpose. We may need to define a dns_option_record
132 * structure which itself should point to different type of data, based on the
133 * extension set (client subnet, tcp keepalive, etc...)*/
134// struct list options; /* list of option records */
Baptiste Assmann2af08fe2017-08-14 00:13:01 +0200135} __attribute__ ((packed));
136
137/* NOTE: big endian structure */
Baptiste Assmann5748f732015-07-21 15:36:24 +0200138struct dns_answer_item {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200139 /*For SRV type, name also includes service and protocol value */
140 char name[DNS_MAX_NAME_SIZE]; /* answer name */
141 int16_t type; /* question type */
142 int16_t class; /* query class */
143 int32_t ttl; /* response TTL */
144 int16_t priority; /* SRV type priority */
145 int16_t weight; /* SRV type weight */
146 int16_t port; /* SRV type port */
147 int16_t data_len; /* number of bytes in target below */
148 struct sockaddr address; /* IPv4 or IPv6, network format */
149 char target[DNS_MAX_NAME_SIZE]; /* Response data: SRV or CNAME type target */
150 time_t last_seen; /* When was the answer was last seen */
151 struct list list;
Baptiste Assmann5748f732015-07-21 15:36:24 +0200152};
153
154struct dns_response_packet {
155 struct dns_header header;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200156 struct list query_list;
157 struct list answer_list;
Baptiste Assmann5748f732015-07-21 15:36:24 +0200158 /* authority and additional_information ignored for now */
159};
160
Christopher Faulet67957bd2017-09-27 11:00:59 +0200161/* Resolvers section and parameters. It is linked to the name servers
Baptiste Assmann325137d2015-04-13 23:40:55 +0200162 * servers points to it.
163 * current resolution are stored in a FIFO list.
164 */
165struct dns_resolvers {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200166 char *id; /* resolvers unique identifier */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200167 struct {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200168 const char *file; /* file where the section appears */
169 int line; /* line where the section appears */
170 } conf; /* config information */
171 struct list nameservers; /* dns server list */
172 unsigned int accepted_payload_size; /* maximum payload size we accept for responses */
173 int nb_nameservers; /* total number of active nameservers in a resolvers section */
174 int resolve_retries; /* number of retries before giving up */
175 struct { /* time to: */
176 int resolve; /* wait between 2 queries for the same resolution */
177 int retry; /* wait for a response before retrying */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200178 } timeout;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200179 struct { /* time to hold current data when */
180 int valid; /* a response is valid */
181 int nx; /* a response doesn't exist */
182 int timeout; /* no answer was delivered */
183 int refused; /* dns server refused to answer */
184 int other; /* other dns response errors */
185 int obsolete; /* an answer hasn't been seen */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200186 } hold;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200187 struct task *t; /* timeout management */
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200188 struct {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200189 struct list wait; /* resolutions managed to this resolvers section */
190 struct list curr; /* current running resolutions */
191 } resolutions;
192 struct eb_root query_ids; /* tree to quickly lookup/retrieve query ids currently in use
193 * used by each nameserver, but stored in resolvers since there must
194 * be a unique relation between an eb_root and an eb_node (resolution) */
195 struct list list; /* resolvers list */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200196};
197
Christopher Faulet67957bd2017-09-27 11:00:59 +0200198/* Structure describing a name server used during name resolution.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200199 * A name server belongs to a resolvers section.
200 */
201struct dns_nameserver {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200202 char *id; /* nameserver unique identifier */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200203 struct {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200204 const char *file; /* file where the section appears */
205 int line; /* line where the section appears */
206 } conf; /* config information */
207
208 struct dns_resolvers *resolvers;
209 struct dgram_conn *dgram; /* transport layer */
210 struct sockaddr_storage addr; /* IP address */
211
212 struct { /* numbers relted to this name server: */
213 long long sent; /* - queries sent */
214 long long snd_error; /* - sending errors */
215 long long valid; /* - valid response */
216 long long update; /* - valid response used to update server's IP */
217 long long cname; /* - CNAME response requiring new resolution */
218 long long cname_error; /* - error when resolving CNAMEs */
219 long long any_err; /* - void response (usually because ANY qtype) */
220 long long nx; /* - NX response */
221 long long timeout; /* - queries which reached timeout */
222 long long refused; /* - queries refused */
223 long long other; /* - other type of response */
224 long long invalid; /* - malformed DNS response */
225 long long too_big; /* - too big response */
226 long long outdated; /* - outdated response (server slower than the other ones) */
227 long long truncated; /* - truncated response */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200228 } counters;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200229 struct list list; /* nameserver chained list */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200230};
231
Thierry Fournierada34842016-02-17 21:25:09 +0100232struct dns_options {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200233 int family_prio; /* which IP family should the resolver use when both are returned */
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100234 struct {
235 int family;
236 union {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200237 struct in_addr in4;
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100238 struct in6_addr in6;
239 } addr;
240 union {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200241 struct in_addr in4;
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100242 struct in6_addr in6;
243 } mask;
244 } pref_net[SRV_MAX_PREF_NET];
Christopher Faulet67957bd2017-09-27 11:00:59 +0200245 int pref_net_nb; /* The number of registered prefered networks. */
Thierry Fournierada34842016-02-17 21:25:09 +0100246};
247
Christopher Faulet67957bd2017-09-27 11:00:59 +0200248/* Resolution structure associated to single server and used to manage name
249 * resolution for this server.
250 * The only link between the resolution and a nameserver is through the
251 * query_id.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200252 */
253struct dns_resolution {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200254 struct dns_resolvers *resolvers; /* pointer to the resolvers structure owning the resolution */
255 struct list requesters; /* list of requesters using this resolution */
256 int uuid; /* unique id (used for debugging purpose) */
257 char *hostname_dn; /* server hostname in domain name label format */
258 int hostname_dn_len; /* server domain name label len */
259 unsigned int last_resolution; /* time of the last resolution */
260 unsigned int last_query; /* time of the last query sent */
261 unsigned int last_valid; /* time of the last valid response */
262 int query_id; /* DNS query ID dedicated for this resolution */
263 struct eb32_node qid; /* ebtree query id */
264 int prefered_query_type; /* prefered query type */
265 int query_type; /* current query type */
266 int status; /* status of the resolution being processed RSLV_STATUS_* */
267 int step; /* RSLV_STEP_* */
268 int try; /* current resolution try */
269 int nb_queries; /* count number of queries sent */
270 int nb_responses; /* count number of responses received */
271
272 struct dns_response_packet response; /* structure hosting the DNS response */
273 struct dns_query_item response_query_records[DNS_MAX_QUERY_RECORDS]; /* <response> query records */
274
275 struct list list; /* resolution list */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200276};
277
Christopher Faulet67957bd2017-09-27 11:00:59 +0200278/* Structure used to describe the owner of a DNS resolution. */
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200279struct dns_requester {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200280 enum obj_type *owner; /* pointer to the owner (server or dns_srvrq) */
281 struct dns_resolution *resolution; /* pointer to the owned DNS resolution */
282
283 int (*requester_cb)(struct dns_requester *, struct dns_nameserver *); /* requester callback for valid response */
284 int (*requester_error_cb)(struct dns_requester *, int); /* requester callback, for error management */
285
286 struct list list; /* requester list */
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200287};
288
Christopher Faulet67957bd2017-09-27 11:00:59 +0200289/* Last resolution status code */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200290enum {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200291 RSLV_STATUS_NONE = 0, /* no resolution occured yet */
292 RSLV_STATUS_VALID, /* no error */
293 RSLV_STATUS_INVALID, /* invalid responses */
294 RSLV_STATUS_ERROR, /* error */
295 RSLV_STATUS_NX, /* NXDOMAIN */
296 RSLV_STATUS_REFUSED, /* server refused our query */
297 RSLV_STATUS_TIMEOUT, /* no response from DNS servers */
298 RSLV_STATUS_OTHER, /* other errors */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200299};
300
Christopher Faulet67957bd2017-09-27 11:00:59 +0200301/* Current resolution step */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200302enum {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200303 RSLV_STEP_NONE = 0, /* nothing happening currently */
304 RSLV_STEP_RUNNING, /* resolution is running */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200305};
306
Christopher Faulet67957bd2017-09-27 11:00:59 +0200307/* Return codes after analyzing a DNS response */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200308enum {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200309 DNS_RESP_VALID = 0, /* valid response */
310 DNS_RESP_INVALID, /* invalid response (various type of errors can trigger it) */
311 DNS_RESP_ERROR, /* DNS error code */
312 DNS_RESP_NX_DOMAIN, /* resolution unsuccessful */
313 DNS_RESP_REFUSED, /* DNS server refused to answer */
314 DNS_RESP_ANCOUNT_ZERO, /* no answers in the response */
315 DNS_RESP_WRONG_NAME, /* response does not match query name */
316 DNS_RESP_CNAME_ERROR, /* error when resolving a CNAME in an atomic response */
317 DNS_RESP_TIMEOUT, /* DNS server has not answered in time */
318 DNS_RESP_TRUNCATED, /* DNS response is truncated */
319 DNS_RESP_NO_EXPECTED_RECORD, /* No expected records were found in the response */
320 DNS_RESP_QUERY_COUNT_ERROR, /* we did not get the expected number of queries in the response */
321 DNS_RESP_INTERNAL, /* internal resolver error */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200322};
323
Christopher Faulet67957bd2017-09-27 11:00:59 +0200324/* Return codes after searching an IP in a DNS response buffer, using a family
325 * preference
326 */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200327enum {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200328 DNS_UPD_NO = 1, /* provided IP was found and preference is matched
329 * OR provided IP found and preference is not matched, but no IP
330 * matching preference was found */
331 DNS_UPD_SRVIP_NOT_FOUND, /* provided IP not found
332 * OR provided IP found and preference is not match and an IP
333 * matching preference was found */
334 DNS_UPD_CNAME, /* CNAME without any IP provided in the response */
335 DNS_UPD_NAME_ERROR, /* name in the response did not match the query */
336 DNS_UPD_NO_IP_FOUND, /* no IP could be found in the response */
337 DNS_UPD_OBSOLETE_IP, /* The server IP was obsolete, and no other IP was found */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200338};
339
Olivier Houchard8da5f982017-08-04 18:35:36 +0200340struct dns_srvrq {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200341 enum obj_type obj_type; /* object type == OBJ_TYPE_SRVRQ */
342 struct dns_resolvers *resolvers; /* pointer to the resolvers structure used for this server template */
343 struct proxy *proxy; /* associated proxy */
344 char *name;
345 char *hostname_dn; /* server hostname in Domain Name format */
346 int hostname_dn_len; /* string length of the server hostname in Domain Name format */
347 struct dns_requester *dns_requester; /* used to link to its DNS resolution */
348 struct list list; /* Next SRV RQ for the same proxy */
Olivier Houchard8da5f982017-08-04 18:35:36 +0200349};
350
Baptiste Assmann325137d2015-04-13 23:40:55 +0200351#endif /* _TYPES_DNS_H */