blob: 06e014c0898dd33c724d0322295afd4fe4bcd9b9 [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
25/*DNS maximum values */
26/*
27 * Maximum issued from RFC:
28 * RFC 1035: https://www.ietf.org/rfc/rfc1035.txt chapter 2.3.4
29 * RFC 2671: http://tools.ietf.org/html/rfc2671
30 */
31#define DNS_MAX_LABEL_SIZE 63
32#define DNS_MAX_NAME_SIZE 255
Baptiste Assmann9d8dbbc2017-08-18 23:35:08 +020033#define DNS_MAX_UDP_MESSAGE 8192
Baptiste Assmann325137d2015-04-13 23:40:55 +020034
Baptiste Assmann4ec076f2015-12-09 14:02:01 +010035/* DNS minimun record size: 1 char + 1 NULL + type + class */
36#define DNS_MIN_RECORD_SIZE ( 1 + 1 + 2 + 2 )
37
Baptiste Assmannd0aa6d22017-04-03 14:40:20 +020038/* DNS smallest fqdn 'a.gl' size */
39# define DNS_SMALLEST_FQDN_SIZE 4
40
Baptiste Assmann4ec076f2015-12-09 14:02:01 +010041/* maximum number of query records in a DNS response
42 * For now, we allow only one */
43#define DNS_MAX_QUERY_RECORDS 1
44
45/* maximum number of answer record in a DNS response */
46#define DNS_MAX_ANSWER_RECORDS ((DNS_MAX_UDP_MESSAGE - DNS_HEADER_SIZE) / DNS_MIN_RECORD_SIZE)
47
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +020048/* size of dns_buffer used to store responses from the buffer
49 * dns_buffer is used to store data collected from records found in a response.
50 * Before using it, caller will always check that there is at least DNS_MAX_NAME_SIZE bytes
51 * available */
52#define DNS_ANALYZE_BUFFER_SIZE DNS_MAX_UDP_MESSAGE + DNS_MAX_NAME_SIZE
53
Baptiste Assmann325137d2015-04-13 23:40:55 +020054/* DNS error messages */
55#define DNS_TOO_LONG_FQDN "hostname too long"
56#define DNS_LABEL_TOO_LONG "one label too long"
57#define DNS_INVALID_CHARACTER "found an invalid character"
58
59/* dns query class */
60#define DNS_RCLASS_IN 1 /* internet class */
61
62/* dns record types (non exhaustive list) */
63#define DNS_RTYPE_A 1 /* IPv4 address */
64#define DNS_RTYPE_CNAME 5 /* canonical name */
65#define DNS_RTYPE_AAAA 28 /* IPv6 address */
Olivier Houchard8da5f982017-08-04 18:35:36 +020066#define DNS_RTYPE_SRV 33 /* SRV record */
Baptiste Assmann572ab8b2017-08-14 00:04:58 +020067#define DNS_RTYPE_OPT 41 /* OPT */
Baptiste Assmann325137d2015-04-13 23:40:55 +020068#define DNS_RTYPE_ANY 255 /* all records */
69
70/* dns rcode values */
71#define DNS_RCODE_NO_ERROR 0 /* no error */
72#define DNS_RCODE_NX_DOMAIN 3 /* non existent domain */
73#define DNS_RCODE_REFUSED 5 /* query refused */
74
Baptiste Assmann042d0a12015-09-02 21:52:37 +020075/* dns flags masks */
76#define DNS_FLAG_TRUNCATED 0x0200 /* mask for truncated flag */
77#define DNS_FLAG_REPLYCODE 0x000F /* mask for reply code */
78
Thierry Fournierac88cfe2016-02-17 22:05:30 +010079/* max number of network preference entries are avalaible from the
80 * configuration file.
81 */
82#define SRV_MAX_PREF_NET 5
83
Baptiste Assmanned97c952015-07-21 15:34:51 +020084/* DNS header size */
Willy Tarreau0c219be2017-08-22 12:01:26 +020085#define DNS_HEADER_SIZE ((int)sizeof(struct dns_header))
Baptiste Assmanned97c952015-07-21 15:34:51 +020086
Baptiste Assmann201c07f2017-05-22 15:17:15 +020087/* DNS resolution pool size, per resolvers section */
88#define DNS_DEFAULT_RESOLUTION_POOL_SIZE 64
89
Baptiste Assmann325137d2015-04-13 23:40:55 +020090/* DNS request or response header structure */
91struct dns_header {
Nenad Merdanovic8ab79422016-07-13 14:03:43 +020092 uint16_t id;
93 uint16_t flags;
94 uint16_t qdcount;
95 uint16_t ancount;
96 uint16_t nscount;
97 uint16_t arcount;
98} __attribute__ ((packed));
Baptiste Assmann325137d2015-04-13 23:40:55 +020099
100/* short structure to describe a DNS question */
Baptiste Assmann83b0a172016-09-05 19:09:49 +0200101/* NOTE: big endian structure */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200102struct dns_question {
103 unsigned short qtype; /* question type */
104 unsigned short qclass; /* query class */
105};
106
Baptiste Assmann83b0a172016-09-05 19:09:49 +0200107/* NOTE: big endian structure */
Baptiste Assmann5748f732015-07-21 15:36:24 +0200108struct dns_query_item {
109 struct list list;
110 char name[DNS_MAX_NAME_SIZE]; /* query name */
111 unsigned short type; /* question type */
112 unsigned short class; /* query class */
113};
114
Baptiste Assmann83b0a172016-09-05 19:09:49 +0200115/* NOTE: big endian structure */
Baptiste Assmann2af08fe2017-08-14 00:13:01 +0200116struct dns_additional_record {
117 uint8_t name; /* domain name, must be 0 (RFC 6891) */
118 uint16_t type; /* record type DNS_RTYPE_OPT (41) */
119 uint16_t udp_payload_size; /* maximum size accepted for the response */
120 uint32_t extension; /* extended rcode and flags, not used for now */
121 uint16_t data_length; /* data length */
122/* as of today, we don't support yet edns options, that said I already put a placeholder here
123 * for this purpose. We may need to define a dns_option_record structure which itself should
124 * point to different type of data, based on the extension set (client subnet, tcp keepalive,
125 * etc...)*/
126// struct list options; /* list of option records */
127} __attribute__ ((packed));
128
129/* NOTE: big endian structure */
Baptiste Assmann5748f732015-07-21 15:36:24 +0200130struct dns_answer_item {
131 struct list list;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200132 char name[DNS_MAX_NAME_SIZE]; /* answer name
Baptiste Assmann5748f732015-07-21 15:36:24 +0200133 * For SRV type, name also includes service
134 * and protocol value */
135 int16_t type; /* question type */
136 int16_t class; /* query class */
137 int32_t ttl; /* response TTL */
138 int16_t priority; /* SRV type priority */
139 int16_t weight; /* SRV type weight */
140 int16_t port; /* SRV type port */
141 int16_t data_len; /* number of bytes in target below */
142 struct sockaddr address; /* IPv4 or IPv6, network format */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200143 char target[DNS_MAX_NAME_SIZE]; /* Response data: SRV or CNAME type target */
144 time_t last_seen; /* When was the answer was last seen */
Baptiste Assmann5748f732015-07-21 15:36:24 +0200145};
146
147struct dns_response_packet {
148 struct dns_header header;
149 struct list query_list;
150 struct list answer_list;
151 /* authority and additional_information ignored for now */
152};
153
Baptiste Assmann325137d2015-04-13 23:40:55 +0200154/*
155 * resolvers section and parameters. It is linked to the name servers
156 * servers points to it.
157 * current resolution are stored in a FIFO list.
158 */
159struct dns_resolvers {
160 struct list list; /* resolvers list */
161 char *id; /* resolvers unique identifier */
162 struct {
163 const char *file; /* file where the section appears */
164 int line; /* line where the section appears */
165 } conf; /* config information */
166 struct list nameserver_list; /* dns server list */
Baptiste Assmann2af08fe2017-08-14 00:13:01 +0200167 unsigned int accepted_payload_size; /* maximum payload size we accept for responses */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200168 int count_nameservers; /* total number of nameservers in a resolvers section */
169 int resolve_retries; /* number of retries before giving up */
170 struct { /* time to: */
171 int retry; /* wait for a response before retrying */
172 } timeout;
173 struct { /* time to hold current data when */
174 int valid; /* a response is valid */
Baptiste Assmann987e16d2016-11-02 22:23:31 +0100175 int nx; /* a response doesn't exist */
176 int timeout; /* no answer was delivered */
177 int refused; /* dns server refused to answer */
178 int other; /* other dns response errors */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200179 int obsolete; /* an answer hasn't been seen */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200180 } hold;
181 struct task *t; /* timeout management */
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200182 int resolution_pool_size; /* size of the resolution pool associated to this resolvers section */
183 struct {
184 struct list pool; /* resolution pool dedicated to this resolvers section */
185 struct list wait; /* resolutions managed to this resolvers section */
186 struct list curr; /* current running resolutions */
187 } resolution;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200188 struct eb_root query_ids; /* tree to quickly lookup/retrieve query ids currently in use */
189 /* used by each nameserver, but stored in resolvers since there must */
190 /* be a unique relation between an eb_root and an eb_node (resolution) */
191};
192
193/*
194 * structure describing a name server used during name resolution.
195 * A name server belongs to a resolvers section.
196 */
197struct dns_nameserver {
198 struct list list; /* nameserver chained list */
199 char *id; /* nameserver unique identifier */
200 struct {
201 const char *file; /* file where the section appears */
202 int line; /* line where the section appears */
203 } conf; /* config information */
204 struct dns_resolvers *resolvers;
205 struct dgram_conn *dgram; /* transport layer */
206 struct sockaddr_storage addr; /* IP address */
207 struct { /* numbers relted to this name server: */
208 long int sent; /* - queries sent */
209 long int valid; /* - valid response */
210 long int update; /* - valid response used to update server's IP */
211 long int cname; /* - CNAME response requiring new resolution */
212 long int cname_error; /* - error when resolving CNAMEs */
213 long int any_err; /* - void response (usually because ANY qtype) */
214 long int nx; /* - NX response */
215 long int timeout; /* - queries which reached timeout */
216 long int refused; /* - queries refused */
217 long int other; /* - other type of response */
218 long int invalid; /* - malformed DNS response */
219 long int too_big; /* - too big response */
220 long int outdated; /* - outdated response (server slower than the other ones) */
Baptiste Assmann6cdea932015-09-02 21:56:05 +0200221 long int truncated; /* - truncated response */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200222 } counters;
223};
224
Thierry Fournierada34842016-02-17 21:25:09 +0100225struct dns_options {
226 int family_prio; /* which IP family should the resolver use when both are returned */
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100227 struct {
228 int family;
229 union {
230 struct in_addr in4;
231 struct in6_addr in6;
232 } addr;
233 union {
234 struct in_addr in4;
235 struct in6_addr in6;
236 } mask;
237 } pref_net[SRV_MAX_PREF_NET];
238 int pref_net_nb; /* The number of registered prefered networks. */
Thierry Fournierada34842016-02-17 21:25:09 +0100239};
240
Baptiste Assmann325137d2015-04-13 23:40:55 +0200241/*
242 * resolution structure associated to single server and used to manage name resolution for
243 * this server.
244 * The only link between the resolution and a nameserver is through the query_id.
245 */
246struct dns_resolution {
247 struct list list; /* resolution list */
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200248 struct {
249 struct list wait; /* list of standby requesters for this resolution */
250 struct list curr; /* list of requesters currently active on this resolution */
251 } requester;
Baptiste Assmann729c9012017-05-22 15:13:10 +0200252 int (*requester_cb)(struct dns_resolution *, struct dns_nameserver *);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200253 /* requester callback for valid response */
254 int (*requester_error_cb)(struct dns_resolution *, int);
255 /* requester callback, for error management */
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200256 int uuid; /* unique id (used for debugging purpose) */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200257 char *hostname_dn; /* server hostname in domain name label format */
258 int hostname_dn_len; /* server domain name label len */
Baptiste Assmann189363e2015-09-03 10:55:20 +0200259 unsigned int last_resolution; /* time of the lastest valid resolution */
260 unsigned int last_sent_packet; /* time of the latest DNS packet sent */
261 unsigned int last_status_change; /* time of the latest DNS resolution status change */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200262 int query_id; /* DNS query ID dedicated for this resolution */
263 struct eb32_node qid; /* ebtree query id */
Andrew Hayworthe6a4a322015-10-19 22:29:51 +0000264 int query_type;
265 /* query type to send. By default DNS_RTYPE_A or DNS_RTYPE_AAAA depending on resolver_family_priority */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200266 int status; /* status of the resolution being processed RSLV_STATUS_* */
267 int step; /* */
268 int try; /* current resolution try */
269 int try_cname; /* number of CNAME requests sent */
270 int nb_responses; /* count number of responses received */
Baptiste Assmannfa4a6632017-05-04 09:05:00 +0200271 unsigned long long revision; /* updated for each update */
Baptiste Assmann729c9012017-05-22 15:13:10 +0200272 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 */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200274};
275
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200276/*
277 * structure used to describe the owner of a DNS resolution.
278 */
279struct dns_requester {
280 struct list list; /* requester list */
281 enum obj_type *requester; /* pointer to the requester */
282 int prefered_query_type; /* prefered query type */
283 int (*requester_cb)(struct dns_requester *, struct dns_nameserver *);
284 /* requester callback for valid response */
285 int (*requester_error_cb)(struct dns_requester *, int);
286 /* requester callback, for error management */
287};
288
Baptiste Assmann325137d2015-04-13 23:40:55 +0200289/* last resolution status code */
290enum {
291 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 */
299};
300
301/* current resolution step */
302enum {
303 RSLV_STEP_NONE = 0, /* nothing happening currently */
304 RSLV_STEP_RUNNING, /* resolution is running */
305};
306
307/* return codes after analyzing a DNS response */
308enum {
309 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 */
Baptiste Assmann0df5d962015-09-02 21:58:32 +0200318 DNS_RESP_TRUNCATED, /* DNS response is truncated */
Baptiste Assmann96972bc2015-09-09 00:46:58 +0200319 DNS_RESP_NO_EXPECTED_RECORD, /* No expected records were found in the response */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200320 DNS_RESP_QUERY_COUNT_ERROR, /* we did not get the expected number of queries in the response */
Baptiste Assmann42746372017-05-03 12:12:02 +0200321 DNS_RESP_INTERNAL, /* internal resolver error */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200322};
323
324/* return codes after searching an IP in a DNS response buffer, using a family preference */
325enum {
326 DNS_UPD_NO = 1, /* provided IP was found and preference is matched
327 * OR provided IP found and preference is not matched, but no IP
328 * matching preference was found */
329 DNS_UPD_SRVIP_NOT_FOUND, /* provided IP not found
330 * OR provided IP found and preference is not match and an IP
331 * matching preference was found */
332 DNS_UPD_CNAME, /* CNAME without any IP provided in the response */
333 DNS_UPD_NAME_ERROR, /* name in the response did not match the query */
Baptiste Assmann0453a1d2015-09-09 00:51:08 +0200334 DNS_UPD_NO_IP_FOUND, /* no IP could be found in the response */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200335 DNS_UPD_OBSOLETE_IP, /* The server IP was obsolete, and no other IP was found */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200336};
337
Olivier Houchard8da5f982017-08-04 18:35:36 +0200338struct dns_srvrq {
339 enum obj_type obj_type; /* object type == OBJ_TYPE_SRVRQ */
340 struct dns_resolvers *resolvers; /* pointer to the resolvers structure used for this server template */
341
342 struct dns_resolution *resolution; /* server name resolution */
343
344 struct proxy *proxy; /* associated proxy */
345 char *name;
346 char *hostname_dn; /* server hostname in Domain Name format */
347 int hostname_dn_len; /* string length of the server hostname in Domain Name format */
348 struct dns_requester *dns_requester; /* used to link to its DNS resolution */
349 int inter; /* time in ms */
350 struct list list; /* Next SRV RQ for the same proxy */
351};
352
Baptiste Assmann325137d2015-04-13 23:40:55 +0200353#endif /* _TYPES_DNS_H */