blob: 488d3996cb5c45932f76cd69c9150d8c16b03c99 [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 Assmann325137d2015-04-13 23:40:55 +020037/*DNS maximum values */
38/*
39 * Maximum issued from RFC:
40 * RFC 1035: https://www.ietf.org/rfc/rfc1035.txt chapter 2.3.4
41 * RFC 2671: http://tools.ietf.org/html/rfc2671
42 */
Christopher Faulet67957bd2017-09-27 11:00:59 +020043#define DNS_MAX_LABEL_SIZE 63
44#define DNS_MAX_NAME_SIZE 255
45#define DNS_MAX_UDP_MESSAGE 8192
Baptiste Assmann325137d2015-04-13 23:40:55 +020046
Baptiste Assmann4ec076f2015-12-09 14:02:01 +010047/* DNS minimun record size: 1 char + 1 NULL + type + class */
Christopher Faulet67957bd2017-09-27 11:00:59 +020048#define DNS_MIN_RECORD_SIZE (1 + 1 + 2 + 2)
Baptiste Assmann4ec076f2015-12-09 14:02:01 +010049
Baptiste Assmannd0aa6d22017-04-03 14:40:20 +020050/* DNS smallest fqdn 'a.gl' size */
Christopher Faulet67957bd2017-09-27 11:00:59 +020051# define DNS_SMALLEST_FQDN_SIZE 4
Baptiste Assmannd0aa6d22017-04-03 14:40:20 +020052
Baptiste Assmann4ec076f2015-12-09 14:02:01 +010053/* maximum number of query records in a DNS response
54 * For now, we allow only one */
55#define DNS_MAX_QUERY_RECORDS 1
56
57/* maximum number of answer record in a DNS response */
58#define DNS_MAX_ANSWER_RECORDS ((DNS_MAX_UDP_MESSAGE - DNS_HEADER_SIZE) / DNS_MIN_RECORD_SIZE)
59
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +020060/* size of dns_buffer used to store responses from the buffer
61 * dns_buffer is used to store data collected from records found in a response.
62 * Before using it, caller will always check that there is at least DNS_MAX_NAME_SIZE bytes
63 * available */
64#define DNS_ANALYZE_BUFFER_SIZE DNS_MAX_UDP_MESSAGE + DNS_MAX_NAME_SIZE
65
Baptiste Assmann325137d2015-04-13 23:40:55 +020066/* DNS error messages */
Christopher Faulet67957bd2017-09-27 11:00:59 +020067#define DNS_TOO_LONG_FQDN "hostname too long"
68#define DNS_LABEL_TOO_LONG "one label too long"
69#define DNS_INVALID_CHARACTER "found an invalid character"
Baptiste Assmann325137d2015-04-13 23:40:55 +020070
71/* dns query class */
Christopher Faulet67957bd2017-09-27 11:00:59 +020072#define DNS_RCLASS_IN 1 /* internet class */
Baptiste Assmann325137d2015-04-13 23:40:55 +020073
74/* dns record types (non exhaustive list) */
Christopher Faulet67957bd2017-09-27 11:00:59 +020075#define DNS_RTYPE_A 1 /* IPv4 address */
76#define DNS_RTYPE_CNAME 5 /* canonical name */
77#define DNS_RTYPE_AAAA 28 /* IPv6 address */
78#define DNS_RTYPE_SRV 33 /* SRV record */
79#define DNS_RTYPE_OPT 41 /* OPT */
80#define DNS_RTYPE_ANY 255 /* all records */
Baptiste Assmann325137d2015-04-13 23:40:55 +020081
82/* dns rcode values */
Christopher Faulet67957bd2017-09-27 11:00:59 +020083#define DNS_RCODE_NO_ERROR 0 /* no error */
84#define DNS_RCODE_NX_DOMAIN 3 /* non existent domain */
85#define DNS_RCODE_REFUSED 5 /* query refused */
Baptiste Assmann325137d2015-04-13 23:40:55 +020086
Baptiste Assmann042d0a12015-09-02 21:52:37 +020087/* dns flags masks */
Christopher Faulet67957bd2017-09-27 11:00:59 +020088#define DNS_FLAG_TRUNCATED 0x0200 /* mask for truncated flag */
89#define DNS_FLAG_REPLYCODE 0x000F /* mask for reply code */
Baptiste Assmann042d0a12015-09-02 21:52:37 +020090
Thierry Fournierac88cfe2016-02-17 22:05:30 +010091/* max number of network preference entries are avalaible from the
92 * configuration file.
93 */
94#define SRV_MAX_PREF_NET 5
95
Baptiste Assmanned97c952015-07-21 15:34:51 +020096/* DNS header size */
Christopher Faulet67957bd2017-09-27 11:00:59 +020097#define DNS_HEADER_SIZE ((int)sizeof(struct dns_header))
Baptiste Assmann201c07f2017-05-22 15:17:15 +020098
Baptiste Assmann325137d2015-04-13 23:40:55 +020099/* DNS request or response header structure */
100struct dns_header {
Nenad Merdanovic8ab79422016-07-13 14:03:43 +0200101 uint16_t id;
102 uint16_t flags;
103 uint16_t qdcount;
104 uint16_t ancount;
105 uint16_t nscount;
106 uint16_t arcount;
107} __attribute__ ((packed));
Baptiste Assmann325137d2015-04-13 23:40:55 +0200108
109/* short structure to describe a DNS question */
Baptiste Assmann83b0a172016-09-05 19:09:49 +0200110/* NOTE: big endian structure */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200111struct dns_question {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200112 unsigned short qtype; /* question type */
113 unsigned short qclass; /* query class */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200114};
115
Baptiste Assmann83b0a172016-09-05 19:09:49 +0200116/* NOTE: big endian structure */
Baptiste Assmann5748f732015-07-21 15:36:24 +0200117struct dns_query_item {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200118 char name[DNS_MAX_NAME_SIZE]; /* query name */
119 unsigned short type; /* question type */
120 unsigned short class; /* query class */
121 struct list list;
Baptiste Assmann5748f732015-07-21 15:36:24 +0200122};
123
Baptiste Assmann83b0a172016-09-05 19:09:49 +0200124/* NOTE: big endian structure */
Baptiste Assmann2af08fe2017-08-14 00:13:01 +0200125struct dns_additional_record {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200126 uint8_t name; /* domain name, must be 0 (RFC 6891) */
127 uint16_t type; /* record type DNS_RTYPE_OPT (41) */
128 uint16_t udp_payload_size; /* maximum size accepted for the response */
129 uint32_t extension; /* extended rcode and flags, not used for now */
130 uint16_t data_length; /* data length */
131/* as of today, we don't support yet edns options, that said I already put a
132 * placeholder here for this purpose. We may need to define a dns_option_record
133 * structure which itself should point to different type of data, based on the
134 * extension set (client subnet, tcp keepalive, etc...)*/
135// struct list options; /* list of option records */
Baptiste Assmann2af08fe2017-08-14 00:13:01 +0200136} __attribute__ ((packed));
137
138/* NOTE: big endian structure */
Baptiste Assmann5748f732015-07-21 15:36:24 +0200139struct dns_answer_item {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200140 /*For SRV type, name also includes service and protocol value */
141 char name[DNS_MAX_NAME_SIZE]; /* answer name */
142 int16_t type; /* question type */
143 int16_t class; /* query class */
144 int32_t ttl; /* response TTL */
145 int16_t priority; /* SRV type priority */
Olivier Houchard2ec2db92018-01-08 16:28:57 +0100146 uint16_t weight; /* SRV type weight */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200147 int16_t port; /* SRV type port */
148 int16_t data_len; /* number of bytes in target below */
149 struct sockaddr address; /* IPv4 or IPv6, network format */
150 char target[DNS_MAX_NAME_SIZE]; /* Response data: SRV or CNAME type target */
151 time_t last_seen; /* When was the answer was last seen */
152 struct list list;
Baptiste Assmann5748f732015-07-21 15:36:24 +0200153};
154
155struct dns_response_packet {
156 struct dns_header header;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200157 struct list query_list;
158 struct list answer_list;
Baptiste Assmann5748f732015-07-21 15:36:24 +0200159 /* authority and additional_information ignored for now */
160};
161
Christopher Faulet67957bd2017-09-27 11:00:59 +0200162/* Resolvers section and parameters. It is linked to the name servers
Baptiste Assmann325137d2015-04-13 23:40:55 +0200163 * servers points to it.
164 * current resolution are stored in a FIFO list.
165 */
166struct dns_resolvers {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200167 char *id; /* resolvers unique identifier */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200168 struct {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200169 const char *file; /* file where the section appears */
170 int line; /* line where the section appears */
171 } conf; /* config information */
172 struct list nameservers; /* dns server list */
173 unsigned int accepted_payload_size; /* maximum payload size we accept for responses */
174 int nb_nameservers; /* total number of active nameservers in a resolvers section */
175 int resolve_retries; /* number of retries before giving up */
176 struct { /* time to: */
177 int resolve; /* wait between 2 queries for the same resolution */
178 int retry; /* wait for a response before retrying */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200179 } timeout;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200180 struct { /* time to hold current data when */
181 int valid; /* a response is valid */
182 int nx; /* a response doesn't exist */
183 int timeout; /* no answer was delivered */
184 int refused; /* dns server refused to answer */
185 int other; /* other dns response errors */
186 int obsolete; /* an answer hasn't been seen */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200187 } hold;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200188 struct task *t; /* timeout management */
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200189 struct {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200190 struct list wait; /* resolutions managed to this resolvers section */
191 struct list curr; /* current running resolutions */
192 } resolutions;
193 struct eb_root query_ids; /* tree to quickly lookup/retrieve query ids currently in use
194 * used by each nameserver, but stored in resolvers since there must
195 * be a unique relation between an eb_root and an eb_node (resolution) */
Christopher Faulet9dcf9b62017-11-13 10:34:01 +0100196 __decl_hathreads(HA_SPINLOCK_T lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200197 struct list list; /* resolvers list */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200198};
199
Christopher Faulet67957bd2017-09-27 11:00:59 +0200200/* Structure describing a name server used during name resolution.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200201 * A name server belongs to a resolvers section.
202 */
203struct dns_nameserver {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200204 char *id; /* nameserver unique identifier */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200205 struct {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200206 const char *file; /* file where the section appears */
207 int line; /* line where the section appears */
208 } conf; /* config information */
209
210 struct dns_resolvers *resolvers;
211 struct dgram_conn *dgram; /* transport layer */
212 struct sockaddr_storage addr; /* IP address */
213
214 struct { /* numbers relted to this name server: */
215 long long sent; /* - queries sent */
216 long long snd_error; /* - sending errors */
217 long long valid; /* - valid response */
218 long long update; /* - valid response used to update server's IP */
219 long long cname; /* - CNAME response requiring new resolution */
220 long long cname_error; /* - error when resolving CNAMEs */
221 long long any_err; /* - void response (usually because ANY qtype) */
222 long long nx; /* - NX response */
223 long long timeout; /* - queries which reached timeout */
224 long long refused; /* - queries refused */
225 long long other; /* - other type of response */
226 long long invalid; /* - malformed DNS response */
227 long long too_big; /* - too big response */
228 long long outdated; /* - outdated response (server slower than the other ones) */
229 long long truncated; /* - truncated response */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200230 } counters;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200231 struct list list; /* nameserver chained list */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200232};
233
Thierry Fournierada34842016-02-17 21:25:09 +0100234struct dns_options {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200235 int family_prio; /* which IP family should the resolver use when both are returned */
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100236 struct {
237 int family;
238 union {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200239 struct in_addr in4;
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100240 struct in6_addr in6;
241 } addr;
242 union {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200243 struct in_addr in4;
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100244 struct in6_addr in6;
245 } mask;
246 } pref_net[SRV_MAX_PREF_NET];
Christopher Faulet67957bd2017-09-27 11:00:59 +0200247 int pref_net_nb; /* The number of registered prefered networks. */
Baptiste Assmann8e2d9432018-06-22 15:04:43 +0200248 int accept_duplicate_ip; /* flag to indicate whether the associated object can use an IP address
249 already set to an other object of the same group */
Thierry Fournierada34842016-02-17 21:25:09 +0100250};
251
Christopher Faulet67957bd2017-09-27 11:00:59 +0200252/* Resolution structure associated to single server and used to manage name
253 * resolution for this server.
254 * The only link between the resolution and a nameserver is through the
255 * query_id.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200256 */
257struct dns_resolution {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200258 struct dns_resolvers *resolvers; /* pointer to the resolvers structure owning the resolution */
259 struct list requesters; /* list of requesters using this resolution */
260 int uuid; /* unique id (used for debugging purpose) */
261 char *hostname_dn; /* server hostname in domain name label format */
262 int hostname_dn_len; /* server domain name label len */
263 unsigned int last_resolution; /* time of the last resolution */
264 unsigned int last_query; /* time of the last query sent */
265 unsigned int last_valid; /* time of the last valid response */
266 int query_id; /* DNS query ID dedicated for this resolution */
267 struct eb32_node qid; /* ebtree query id */
268 int prefered_query_type; /* prefered query type */
269 int query_type; /* current query type */
270 int status; /* status of the resolution being processed RSLV_STATUS_* */
271 int step; /* RSLV_STEP_* */
272 int try; /* current resolution try */
273 int nb_queries; /* count number of queries sent */
274 int nb_responses; /* count number of responses received */
275
276 struct dns_response_packet response; /* structure hosting the DNS response */
277 struct dns_query_item response_query_records[DNS_MAX_QUERY_RECORDS]; /* <response> query records */
278
279 struct list list; /* resolution list */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200280};
281
Christopher Faulet67957bd2017-09-27 11:00:59 +0200282/* Structure used to describe the owner of a DNS resolution. */
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200283struct dns_requester {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200284 enum obj_type *owner; /* pointer to the owner (server or dns_srvrq) */
285 struct dns_resolution *resolution; /* pointer to the owned DNS resolution */
286
287 int (*requester_cb)(struct dns_requester *, struct dns_nameserver *); /* requester callback for valid response */
288 int (*requester_error_cb)(struct dns_requester *, int); /* requester callback, for error management */
289
290 struct list list; /* requester list */
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200291};
292
Christopher Faulet67957bd2017-09-27 11:00:59 +0200293/* Last resolution status code */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200294enum {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200295 RSLV_STATUS_NONE = 0, /* no resolution occured yet */
296 RSLV_STATUS_VALID, /* no error */
297 RSLV_STATUS_INVALID, /* invalid responses */
298 RSLV_STATUS_ERROR, /* error */
299 RSLV_STATUS_NX, /* NXDOMAIN */
300 RSLV_STATUS_REFUSED, /* server refused our query */
301 RSLV_STATUS_TIMEOUT, /* no response from DNS servers */
302 RSLV_STATUS_OTHER, /* other errors */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200303};
304
Christopher Faulet67957bd2017-09-27 11:00:59 +0200305/* Current resolution step */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200306enum {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200307 RSLV_STEP_NONE = 0, /* nothing happening currently */
308 RSLV_STEP_RUNNING, /* resolution is running */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200309};
310
Christopher Faulet67957bd2017-09-27 11:00:59 +0200311/* Return codes after analyzing a DNS response */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200312enum {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200313 DNS_RESP_VALID = 0, /* valid response */
314 DNS_RESP_INVALID, /* invalid response (various type of errors can trigger it) */
315 DNS_RESP_ERROR, /* DNS error code */
316 DNS_RESP_NX_DOMAIN, /* resolution unsuccessful */
317 DNS_RESP_REFUSED, /* DNS server refused to answer */
318 DNS_RESP_ANCOUNT_ZERO, /* no answers in the response */
319 DNS_RESP_WRONG_NAME, /* response does not match query name */
320 DNS_RESP_CNAME_ERROR, /* error when resolving a CNAME in an atomic response */
321 DNS_RESP_TIMEOUT, /* DNS server has not answered in time */
322 DNS_RESP_TRUNCATED, /* DNS response is truncated */
323 DNS_RESP_NO_EXPECTED_RECORD, /* No expected records were found in the response */
324 DNS_RESP_QUERY_COUNT_ERROR, /* we did not get the expected number of queries in the response */
325 DNS_RESP_INTERNAL, /* internal resolver error */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200326};
327
Christopher Faulet67957bd2017-09-27 11:00:59 +0200328/* Return codes after searching an IP in a DNS response buffer, using a family
329 * preference
330 */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200331enum {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200332 DNS_UPD_NO = 1, /* provided IP was found and preference is matched
333 * OR provided IP found and preference is not matched, but no IP
334 * matching preference was found */
335 DNS_UPD_SRVIP_NOT_FOUND, /* provided IP not found
336 * OR provided IP found and preference is not match and an IP
337 * matching preference was found */
338 DNS_UPD_CNAME, /* CNAME without any IP provided in the response */
339 DNS_UPD_NAME_ERROR, /* name in the response did not match the query */
340 DNS_UPD_NO_IP_FOUND, /* no IP could be found in the response */
341 DNS_UPD_OBSOLETE_IP, /* The server IP was obsolete, and no other IP was found */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200342};
343
Olivier Houchard8da5f982017-08-04 18:35:36 +0200344struct dns_srvrq {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200345 enum obj_type obj_type; /* object type == OBJ_TYPE_SRVRQ */
346 struct dns_resolvers *resolvers; /* pointer to the resolvers structure used for this server template */
347 struct proxy *proxy; /* associated proxy */
348 char *name;
349 char *hostname_dn; /* server hostname in Domain Name format */
350 int hostname_dn_len; /* string length of the server hostname in Domain Name format */
351 struct dns_requester *dns_requester; /* used to link to its DNS resolution */
352 struct list list; /* Next SRV RQ for the same proxy */
Olivier Houchard8da5f982017-08-04 18:35:36 +0200353};
354
Baptiste Assmann325137d2015-04-13 23:40:55 +0200355#endif /* _TYPES_DNS_H */