blob: 5a60c0708b239123444f080d00df589428378a57 [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>
Christopher Fauletb2812a62017-10-04 16:17:58 +020028#include <common/hathreads.h>
Christopher Faulet67957bd2017-09-27 11:00:59 +020029
30#include <types/connection.h>
31#include <types/obj_type.h>
32#include <types/proto_udp.h>
33#include <types/proxy.h>
34#include <types/server.h>
35#include <types/task.h>
36
Baptiste Assmanndfd35fd2019-01-21 08:18:09 +010037extern struct pool_head *dns_requester_pool;
38
Baptiste Assmann325137d2015-04-13 23:40:55 +020039/*DNS maximum values */
40/*
41 * Maximum issued from RFC:
42 * RFC 1035: https://www.ietf.org/rfc/rfc1035.txt chapter 2.3.4
43 * RFC 2671: http://tools.ietf.org/html/rfc2671
44 */
Christopher Faulet67957bd2017-09-27 11:00:59 +020045#define DNS_MAX_LABEL_SIZE 63
46#define DNS_MAX_NAME_SIZE 255
47#define DNS_MAX_UDP_MESSAGE 8192
Baptiste Assmann325137d2015-04-13 23:40:55 +020048
Baptiste Assmann4ec076f2015-12-09 14:02:01 +010049/* DNS minimun record size: 1 char + 1 NULL + type + class */
Christopher Faulet67957bd2017-09-27 11:00:59 +020050#define DNS_MIN_RECORD_SIZE (1 + 1 + 2 + 2)
Baptiste Assmann4ec076f2015-12-09 14:02:01 +010051
Baptiste Assmannd0aa6d22017-04-03 14:40:20 +020052/* DNS smallest fqdn 'a.gl' size */
Christopher Faulet67957bd2017-09-27 11:00:59 +020053# define DNS_SMALLEST_FQDN_SIZE 4
Baptiste Assmannd0aa6d22017-04-03 14:40:20 +020054
Baptiste Assmann4ec076f2015-12-09 14:02:01 +010055/* maximum number of query records in a DNS response
56 * For now, we allow only one */
57#define DNS_MAX_QUERY_RECORDS 1
58
59/* maximum number of answer record in a DNS response */
60#define DNS_MAX_ANSWER_RECORDS ((DNS_MAX_UDP_MESSAGE - DNS_HEADER_SIZE) / DNS_MIN_RECORD_SIZE)
61
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +020062/* size of dns_buffer used to store responses from the buffer
63 * dns_buffer is used to store data collected from records found in a response.
64 * Before using it, caller will always check that there is at least DNS_MAX_NAME_SIZE bytes
65 * available */
66#define DNS_ANALYZE_BUFFER_SIZE DNS_MAX_UDP_MESSAGE + DNS_MAX_NAME_SIZE
67
Baptiste Assmann325137d2015-04-13 23:40:55 +020068/* DNS error messages */
Christopher Faulet67957bd2017-09-27 11:00:59 +020069#define DNS_TOO_LONG_FQDN "hostname too long"
70#define DNS_LABEL_TOO_LONG "one label too long"
71#define DNS_INVALID_CHARACTER "found an invalid character"
Baptiste Assmann325137d2015-04-13 23:40:55 +020072
73/* dns query class */
Christopher Faulet67957bd2017-09-27 11:00:59 +020074#define DNS_RCLASS_IN 1 /* internet class */
Baptiste Assmann325137d2015-04-13 23:40:55 +020075
76/* dns record types (non exhaustive list) */
Christopher Faulet67957bd2017-09-27 11:00:59 +020077#define DNS_RTYPE_A 1 /* IPv4 address */
78#define DNS_RTYPE_CNAME 5 /* canonical name */
79#define DNS_RTYPE_AAAA 28 /* IPv6 address */
80#define DNS_RTYPE_SRV 33 /* SRV record */
81#define DNS_RTYPE_OPT 41 /* OPT */
82#define DNS_RTYPE_ANY 255 /* all records */
Baptiste Assmann325137d2015-04-13 23:40:55 +020083
84/* dns rcode values */
Christopher Faulet67957bd2017-09-27 11:00:59 +020085#define DNS_RCODE_NO_ERROR 0 /* no error */
86#define DNS_RCODE_NX_DOMAIN 3 /* non existent domain */
87#define DNS_RCODE_REFUSED 5 /* query refused */
Baptiste Assmann325137d2015-04-13 23:40:55 +020088
Baptiste Assmann042d0a12015-09-02 21:52:37 +020089/* dns flags masks */
Christopher Faulet67957bd2017-09-27 11:00:59 +020090#define DNS_FLAG_TRUNCATED 0x0200 /* mask for truncated flag */
91#define DNS_FLAG_REPLYCODE 0x000F /* mask for reply code */
Baptiste Assmann042d0a12015-09-02 21:52:37 +020092
Joseph Herlant42cf6392018-11-15 10:33:28 -080093/* max number of network preference entries are available from the
Thierry Fournierac88cfe2016-02-17 22:05:30 +010094 * configuration file.
95 */
96#define SRV_MAX_PREF_NET 5
97
Baptiste Assmanned97c952015-07-21 15:34:51 +020098/* DNS header size */
Christopher Faulet67957bd2017-09-27 11:00:59 +020099#define DNS_HEADER_SIZE ((int)sizeof(struct dns_header))
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200100
Baptiste Assmann325137d2015-04-13 23:40:55 +0200101/* DNS request or response header structure */
102struct dns_header {
Nenad Merdanovic8ab79422016-07-13 14:03:43 +0200103 uint16_t id;
104 uint16_t flags;
105 uint16_t qdcount;
106 uint16_t ancount;
107 uint16_t nscount;
108 uint16_t arcount;
109} __attribute__ ((packed));
Baptiste Assmann325137d2015-04-13 23:40:55 +0200110
111/* short structure to describe a DNS question */
Baptiste Assmann83b0a172016-09-05 19:09:49 +0200112/* NOTE: big endian structure */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200113struct dns_question {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200114 unsigned short qtype; /* question type */
115 unsigned short qclass; /* query class */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200116};
117
Baptiste Assmann83b0a172016-09-05 19:09:49 +0200118/* NOTE: big endian structure */
Baptiste Assmann5748f732015-07-21 15:36:24 +0200119struct dns_query_item {
Remi Gacogne00488dd2018-12-05 17:59:56 +0100120 char name[DNS_MAX_NAME_SIZE+1]; /* query name */
121 unsigned short type; /* question type */
122 unsigned short class; /* query class */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200123 struct list list;
Baptiste Assmann5748f732015-07-21 15:36:24 +0200124};
125
Baptiste Assmann83b0a172016-09-05 19:09:49 +0200126/* NOTE: big endian structure */
Baptiste Assmann2af08fe2017-08-14 00:13:01 +0200127struct dns_additional_record {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200128 uint8_t name; /* domain name, must be 0 (RFC 6891) */
129 uint16_t type; /* record type DNS_RTYPE_OPT (41) */
130 uint16_t udp_payload_size; /* maximum size accepted for the response */
131 uint32_t extension; /* extended rcode and flags, not used for now */
132 uint16_t data_length; /* data length */
133/* as of today, we don't support yet edns options, that said I already put a
134 * placeholder here for this purpose. We may need to define a dns_option_record
135 * structure which itself should point to different type of data, based on the
136 * extension set (client subnet, tcp keepalive, etc...)*/
137// struct list options; /* list of option records */
Baptiste Assmann2af08fe2017-08-14 00:13:01 +0200138} __attribute__ ((packed));
139
140/* NOTE: big endian structure */
Baptiste Assmann5748f732015-07-21 15:36:24 +0200141struct dns_answer_item {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200142 /*For SRV type, name also includes service and protocol value */
Remi Gacogne00488dd2018-12-05 17:59:56 +0100143 char name[DNS_MAX_NAME_SIZE+1]; /* answer name */
144 int16_t type; /* question type */
145 int16_t class; /* query class */
146 int32_t ttl; /* response TTL */
147 int16_t priority; /* SRV type priority */
148 uint16_t weight; /* SRV type weight */
Willy Tarreaud1f56c92019-05-22 20:07:45 +0200149 uint16_t port; /* SRV type port */
Remi Gacogne00488dd2018-12-05 17:59:56 +0100150 uint16_t data_len; /* number of bytes in target below */
151 struct sockaddr address; /* IPv4 or IPv6, network format */
152 char target[DNS_MAX_NAME_SIZE+1]; /* Response data: SRV or CNAME type target */
153 time_t last_seen; /* When was the answer was last seen */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200154 struct list list;
Baptiste Assmann5748f732015-07-21 15:36:24 +0200155};
156
157struct dns_response_packet {
158 struct dns_header header;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200159 struct list query_list;
160 struct list answer_list;
Baptiste Assmann5748f732015-07-21 15:36:24 +0200161 /* authority and additional_information ignored for now */
162};
163
Christopher Faulet67957bd2017-09-27 11:00:59 +0200164/* Resolvers section and parameters. It is linked to the name servers
Baptiste Assmann325137d2015-04-13 23:40:55 +0200165 * servers points to it.
166 * current resolution are stored in a FIFO list.
167 */
168struct dns_resolvers {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200169 char *id; /* resolvers unique identifier */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200170 struct {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200171 const char *file; /* file where the section appears */
172 int line; /* line where the section appears */
173 } conf; /* config information */
174 struct list nameservers; /* dns server list */
175 unsigned int accepted_payload_size; /* maximum payload size we accept for responses */
176 int nb_nameservers; /* total number of active nameservers in a resolvers section */
177 int resolve_retries; /* number of retries before giving up */
178 struct { /* time to: */
179 int resolve; /* wait between 2 queries for the same resolution */
180 int retry; /* wait for a response before retrying */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200181 } timeout;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200182 struct { /* time to hold current data when */
183 int valid; /* a response is valid */
184 int nx; /* a response doesn't exist */
185 int timeout; /* no answer was delivered */
186 int refused; /* dns server refused to answer */
187 int other; /* other dns response errors */
188 int obsolete; /* an answer hasn't been seen */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200189 } hold;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200190 struct task *t; /* timeout management */
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200191 struct {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200192 struct list wait; /* resolutions managed to this resolvers section */
193 struct list curr; /* current running resolutions */
194 } resolutions;
195 struct eb_root query_ids; /* tree to quickly lookup/retrieve query ids currently in use
196 * used by each nameserver, but stored in resolvers since there must
197 * be a unique relation between an eb_root and an eb_node (resolution) */
Christopher Faulet9dcf9b62017-11-13 10:34:01 +0100198 __decl_hathreads(HA_SPINLOCK_T lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200199 struct list list; /* resolvers list */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200200};
201
Christopher Faulet67957bd2017-09-27 11:00:59 +0200202/* Structure describing a name server used during name resolution.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200203 * A name server belongs to a resolvers section.
204 */
205struct dns_nameserver {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200206 char *id; /* nameserver unique identifier */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200207 struct {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200208 const char *file; /* file where the section appears */
209 int line; /* line where the section appears */
210 } conf; /* config information */
211
212 struct dns_resolvers *resolvers;
213 struct dgram_conn *dgram; /* transport layer */
214 struct sockaddr_storage addr; /* IP address */
215
216 struct { /* numbers relted to this name server: */
217 long long sent; /* - queries sent */
218 long long snd_error; /* - sending errors */
219 long long valid; /* - valid response */
220 long long update; /* - valid response used to update server's IP */
221 long long cname; /* - CNAME response requiring new resolution */
222 long long cname_error; /* - error when resolving CNAMEs */
223 long long any_err; /* - void response (usually because ANY qtype) */
224 long long nx; /* - NX response */
225 long long timeout; /* - queries which reached timeout */
226 long long refused; /* - queries refused */
227 long long other; /* - other type of response */
228 long long invalid; /* - malformed DNS response */
229 long long too_big; /* - too big response */
230 long long outdated; /* - outdated response (server slower than the other ones) */
231 long long truncated; /* - truncated response */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200232 } counters;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200233 struct list list; /* nameserver chained list */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200234};
235
Thierry Fournierada34842016-02-17 21:25:09 +0100236struct dns_options {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200237 int family_prio; /* which IP family should the resolver use when both are returned */
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100238 struct {
239 int family;
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 } addr;
244 union {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200245 struct in_addr in4;
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100246 struct in6_addr in6;
247 } mask;
248 } pref_net[SRV_MAX_PREF_NET];
Joseph Herlant42cf6392018-11-15 10:33:28 -0800249 int pref_net_nb; /* The number of registered preferred networks. */
Baptiste Assmann8e2d9432018-06-22 15:04:43 +0200250 int accept_duplicate_ip; /* flag to indicate whether the associated object can use an IP address
251 already set to an other object of the same group */
Thierry Fournierada34842016-02-17 21:25:09 +0100252};
253
Christopher Faulet67957bd2017-09-27 11:00:59 +0200254/* Resolution structure associated to single server and used to manage name
255 * resolution for this server.
256 * The only link between the resolution and a nameserver is through the
257 * query_id.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200258 */
259struct dns_resolution {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200260 struct dns_resolvers *resolvers; /* pointer to the resolvers structure owning the resolution */
261 struct list requesters; /* list of requesters using this resolution */
262 int uuid; /* unique id (used for debugging purpose) */
263 char *hostname_dn; /* server hostname in domain name label format */
264 int hostname_dn_len; /* server domain name label len */
265 unsigned int last_resolution; /* time of the last resolution */
266 unsigned int last_query; /* time of the last query sent */
267 unsigned int last_valid; /* time of the last valid response */
268 int query_id; /* DNS query ID dedicated for this resolution */
269 struct eb32_node qid; /* ebtree query id */
Joseph Herlant42cf6392018-11-15 10:33:28 -0800270 int prefered_query_type; /* preferred query type */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200271 int query_type; /* current query type */
272 int status; /* status of the resolution being processed RSLV_STATUS_* */
273 int step; /* RSLV_STEP_* */
274 int try; /* current resolution try */
275 int nb_queries; /* count number of queries sent */
276 int nb_responses; /* count number of responses received */
277
278 struct dns_response_packet response; /* structure hosting the DNS response */
279 struct dns_query_item response_query_records[DNS_MAX_QUERY_RECORDS]; /* <response> query records */
280
281 struct list list; /* resolution list */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200282};
283
Christopher Faulet67957bd2017-09-27 11:00:59 +0200284/* Structure used to describe the owner of a DNS resolution. */
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200285struct dns_requester {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200286 enum obj_type *owner; /* pointer to the owner (server or dns_srvrq) */
287 struct dns_resolution *resolution; /* pointer to the owned DNS resolution */
288
289 int (*requester_cb)(struct dns_requester *, struct dns_nameserver *); /* requester callback for valid response */
290 int (*requester_error_cb)(struct dns_requester *, int); /* requester callback, for error management */
291
292 struct list list; /* requester list */
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200293};
294
Christopher Faulet67957bd2017-09-27 11:00:59 +0200295/* Last resolution status code */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200296enum {
Joseph Herlant42cf6392018-11-15 10:33:28 -0800297 RSLV_STATUS_NONE = 0, /* no resolution occurred yet */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200298 RSLV_STATUS_VALID, /* no error */
299 RSLV_STATUS_INVALID, /* invalid responses */
300 RSLV_STATUS_ERROR, /* error */
301 RSLV_STATUS_NX, /* NXDOMAIN */
302 RSLV_STATUS_REFUSED, /* server refused our query */
303 RSLV_STATUS_TIMEOUT, /* no response from DNS servers */
304 RSLV_STATUS_OTHER, /* other errors */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200305};
306
Christopher Faulet67957bd2017-09-27 11:00:59 +0200307/* Current resolution step */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200308enum {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200309 RSLV_STEP_NONE = 0, /* nothing happening currently */
310 RSLV_STEP_RUNNING, /* resolution is running */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200311};
312
Christopher Faulet67957bd2017-09-27 11:00:59 +0200313/* Return codes after analyzing a DNS response */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200314enum {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200315 DNS_RESP_VALID = 0, /* valid response */
316 DNS_RESP_INVALID, /* invalid response (various type of errors can trigger it) */
317 DNS_RESP_ERROR, /* DNS error code */
318 DNS_RESP_NX_DOMAIN, /* resolution unsuccessful */
319 DNS_RESP_REFUSED, /* DNS server refused to answer */
320 DNS_RESP_ANCOUNT_ZERO, /* no answers in the response */
321 DNS_RESP_WRONG_NAME, /* response does not match query name */
322 DNS_RESP_CNAME_ERROR, /* error when resolving a CNAME in an atomic response */
323 DNS_RESP_TIMEOUT, /* DNS server has not answered in time */
324 DNS_RESP_TRUNCATED, /* DNS response is truncated */
325 DNS_RESP_NO_EXPECTED_RECORD, /* No expected records were found in the response */
326 DNS_RESP_QUERY_COUNT_ERROR, /* we did not get the expected number of queries in the response */
327 DNS_RESP_INTERNAL, /* internal resolver error */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200328};
329
Christopher Faulet67957bd2017-09-27 11:00:59 +0200330/* Return codes after searching an IP in a DNS response buffer, using a family
331 * preference
332 */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200333enum {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200334 DNS_UPD_NO = 1, /* provided IP was found and preference is matched
335 * OR provided IP found and preference is not matched, but no IP
336 * matching preference was found */
337 DNS_UPD_SRVIP_NOT_FOUND, /* provided IP not found
338 * OR provided IP found and preference is not match and an IP
339 * matching preference was found */
340 DNS_UPD_CNAME, /* CNAME without any IP provided in the response */
341 DNS_UPD_NAME_ERROR, /* name in the response did not match the query */
342 DNS_UPD_NO_IP_FOUND, /* no IP could be found in the response */
343 DNS_UPD_OBSOLETE_IP, /* The server IP was obsolete, and no other IP was found */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200344};
345
Olivier Houchard8da5f982017-08-04 18:35:36 +0200346struct dns_srvrq {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200347 enum obj_type obj_type; /* object type == OBJ_TYPE_SRVRQ */
348 struct dns_resolvers *resolvers; /* pointer to the resolvers structure used for this server template */
349 struct proxy *proxy; /* associated proxy */
350 char *name;
351 char *hostname_dn; /* server hostname in Domain Name format */
352 int hostname_dn_len; /* string length of the server hostname in Domain Name format */
353 struct dns_requester *dns_requester; /* used to link to its DNS resolution */
354 struct list list; /* Next SRV RQ for the same proxy */
Olivier Houchard8da5f982017-08-04 18:35:36 +0200355};
356
Baptiste Assmann325137d2015-04-13 23:40:55 +0200357#endif /* _TYPES_DNS_H */