blob: 9bf3c7e917b9f5fd87d6559466b719088cf4e5f0 [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 Assmannd20bbaf2016-03-26 15:09:48 +010033#define DNS_MAX_UDP_MESSAGE 512
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 */
85#define DNS_HEADER_SIZE sizeof(struct dns_header)
86
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 Assmann5748f732015-07-21 15:36:24 +0200116struct dns_answer_item {
117 struct list list;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200118 char name[DNS_MAX_NAME_SIZE]; /* answer name
Baptiste Assmann5748f732015-07-21 15:36:24 +0200119 * For SRV type, name also includes service
120 * and protocol value */
121 int16_t type; /* question type */
122 int16_t class; /* query class */
123 int32_t ttl; /* response TTL */
124 int16_t priority; /* SRV type priority */
125 int16_t weight; /* SRV type weight */
126 int16_t port; /* SRV type port */
127 int16_t data_len; /* number of bytes in target below */
128 struct sockaddr address; /* IPv4 or IPv6, network format */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200129 char target[DNS_MAX_NAME_SIZE]; /* Response data: SRV or CNAME type target */
130 time_t last_seen; /* When was the answer was last seen */
Baptiste Assmann5748f732015-07-21 15:36:24 +0200131};
132
133struct dns_response_packet {
134 struct dns_header header;
135 struct list query_list;
136 struct list answer_list;
137 /* authority and additional_information ignored for now */
138};
139
Baptiste Assmann325137d2015-04-13 23:40:55 +0200140/*
141 * resolvers section and parameters. It is linked to the name servers
142 * servers points to it.
143 * current resolution are stored in a FIFO list.
144 */
145struct dns_resolvers {
146 struct list list; /* resolvers list */
147 char *id; /* resolvers unique identifier */
148 struct {
149 const char *file; /* file where the section appears */
150 int line; /* line where the section appears */
151 } conf; /* config information */
152 struct list nameserver_list; /* dns server list */
153 int count_nameservers; /* total number of nameservers in a resolvers section */
154 int resolve_retries; /* number of retries before giving up */
155 struct { /* time to: */
156 int retry; /* wait for a response before retrying */
157 } timeout;
158 struct { /* time to hold current data when */
159 int valid; /* a response is valid */
Baptiste Assmann987e16d2016-11-02 22:23:31 +0100160 int nx; /* a response doesn't exist */
161 int timeout; /* no answer was delivered */
162 int refused; /* dns server refused to answer */
163 int other; /* other dns response errors */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200164 int obsolete; /* an answer hasn't been seen */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200165 } hold;
166 struct task *t; /* timeout management */
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200167 int resolution_pool_size; /* size of the resolution pool associated to this resolvers section */
168 struct {
169 struct list pool; /* resolution pool dedicated to this resolvers section */
170 struct list wait; /* resolutions managed to this resolvers section */
171 struct list curr; /* current running resolutions */
172 } resolution;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200173 struct eb_root query_ids; /* tree to quickly lookup/retrieve query ids currently in use */
174 /* used by each nameserver, but stored in resolvers since there must */
175 /* be a unique relation between an eb_root and an eb_node (resolution) */
176};
177
178/*
179 * structure describing a name server used during name resolution.
180 * A name server belongs to a resolvers section.
181 */
182struct dns_nameserver {
183 struct list list; /* nameserver chained list */
184 char *id; /* nameserver unique identifier */
185 struct {
186 const char *file; /* file where the section appears */
187 int line; /* line where the section appears */
188 } conf; /* config information */
189 struct dns_resolvers *resolvers;
190 struct dgram_conn *dgram; /* transport layer */
191 struct sockaddr_storage addr; /* IP address */
192 struct { /* numbers relted to this name server: */
193 long int sent; /* - queries sent */
194 long int valid; /* - valid response */
195 long int update; /* - valid response used to update server's IP */
196 long int cname; /* - CNAME response requiring new resolution */
197 long int cname_error; /* - error when resolving CNAMEs */
198 long int any_err; /* - void response (usually because ANY qtype) */
199 long int nx; /* - NX response */
200 long int timeout; /* - queries which reached timeout */
201 long int refused; /* - queries refused */
202 long int other; /* - other type of response */
203 long int invalid; /* - malformed DNS response */
204 long int too_big; /* - too big response */
205 long int outdated; /* - outdated response (server slower than the other ones) */
Baptiste Assmann6cdea932015-09-02 21:56:05 +0200206 long int truncated; /* - truncated response */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200207 } counters;
208};
209
Thierry Fournierada34842016-02-17 21:25:09 +0100210struct dns_options {
211 int family_prio; /* which IP family should the resolver use when both are returned */
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100212 struct {
213 int family;
214 union {
215 struct in_addr in4;
216 struct in6_addr in6;
217 } addr;
218 union {
219 struct in_addr in4;
220 struct in6_addr in6;
221 } mask;
222 } pref_net[SRV_MAX_PREF_NET];
223 int pref_net_nb; /* The number of registered prefered networks. */
Thierry Fournierada34842016-02-17 21:25:09 +0100224};
225
Baptiste Assmann325137d2015-04-13 23:40:55 +0200226/*
227 * resolution structure associated to single server and used to manage name resolution for
228 * this server.
229 * The only link between the resolution and a nameserver is through the query_id.
230 */
231struct dns_resolution {
232 struct list list; /* resolution list */
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200233 struct {
234 struct list wait; /* list of standby requesters for this resolution */
235 struct list curr; /* list of requesters currently active on this resolution */
236 } requester;
Baptiste Assmann729c9012017-05-22 15:13:10 +0200237 int (*requester_cb)(struct dns_resolution *, struct dns_nameserver *);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200238 /* requester callback for valid response */
239 int (*requester_error_cb)(struct dns_resolution *, int);
240 /* requester callback, for error management */
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200241 int uuid; /* unique id (used for debugging purpose) */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200242 char *hostname_dn; /* server hostname in domain name label format */
243 int hostname_dn_len; /* server domain name label len */
Baptiste Assmann189363e2015-09-03 10:55:20 +0200244 unsigned int last_resolution; /* time of the lastest valid resolution */
245 unsigned int last_sent_packet; /* time of the latest DNS packet sent */
246 unsigned int last_status_change; /* time of the latest DNS resolution status change */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200247 int query_id; /* DNS query ID dedicated for this resolution */
248 struct eb32_node qid; /* ebtree query id */
Andrew Hayworthe6a4a322015-10-19 22:29:51 +0000249 int query_type;
250 /* 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 +0200251 int status; /* status of the resolution being processed RSLV_STATUS_* */
252 int step; /* */
253 int try; /* current resolution try */
254 int try_cname; /* number of CNAME requests sent */
255 int nb_responses; /* count number of responses received */
Baptiste Assmannfa4a6632017-05-04 09:05:00 +0200256 unsigned long long revision; /* updated for each update */
Baptiste Assmann729c9012017-05-22 15:13:10 +0200257 struct dns_response_packet response; /* structure hosting the DNS response */
258 struct dns_query_item response_query_records[DNS_MAX_QUERY_RECORDS]; /* <response> query records */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200259};
260
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200261/*
262 * structure used to describe the owner of a DNS resolution.
263 */
264struct dns_requester {
265 struct list list; /* requester list */
266 enum obj_type *requester; /* pointer to the requester */
267 int prefered_query_type; /* prefered query type */
268 int (*requester_cb)(struct dns_requester *, struct dns_nameserver *);
269 /* requester callback for valid response */
270 int (*requester_error_cb)(struct dns_requester *, int);
271 /* requester callback, for error management */
272};
273
Baptiste Assmann325137d2015-04-13 23:40:55 +0200274/* last resolution status code */
275enum {
276 RSLV_STATUS_NONE = 0, /* no resolution occured yet */
277 RSLV_STATUS_VALID, /* no error */
278 RSLV_STATUS_INVALID, /* invalid responses */
279 RSLV_STATUS_ERROR, /* error */
280 RSLV_STATUS_NX, /* NXDOMAIN */
281 RSLV_STATUS_REFUSED, /* server refused our query */
282 RSLV_STATUS_TIMEOUT, /* no response from DNS servers */
283 RSLV_STATUS_OTHER, /* other errors */
284};
285
286/* current resolution step */
287enum {
288 RSLV_STEP_NONE = 0, /* nothing happening currently */
289 RSLV_STEP_RUNNING, /* resolution is running */
290};
291
292/* return codes after analyzing a DNS response */
293enum {
294 DNS_RESP_VALID = 0, /* valid response */
295 DNS_RESP_INVALID, /* invalid response (various type of errors can trigger it) */
296 DNS_RESP_ERROR, /* DNS error code */
297 DNS_RESP_NX_DOMAIN, /* resolution unsuccessful */
298 DNS_RESP_REFUSED, /* DNS server refused to answer */
299 DNS_RESP_ANCOUNT_ZERO, /* no answers in the response */
300 DNS_RESP_WRONG_NAME, /* response does not match query name */
301 DNS_RESP_CNAME_ERROR, /* error when resolving a CNAME in an atomic response */
302 DNS_RESP_TIMEOUT, /* DNS server has not answered in time */
Baptiste Assmann0df5d962015-09-02 21:58:32 +0200303 DNS_RESP_TRUNCATED, /* DNS response is truncated */
Baptiste Assmann96972bc2015-09-09 00:46:58 +0200304 DNS_RESP_NO_EXPECTED_RECORD, /* No expected records were found in the response */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200305 DNS_RESP_QUERY_COUNT_ERROR, /* we did not get the expected number of queries in the response */
Baptiste Assmann42746372017-05-03 12:12:02 +0200306 DNS_RESP_INTERNAL, /* internal resolver error */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200307};
308
309/* return codes after searching an IP in a DNS response buffer, using a family preference */
310enum {
311 DNS_UPD_NO = 1, /* provided IP was found and preference is matched
312 * OR provided IP found and preference is not matched, but no IP
313 * matching preference was found */
314 DNS_UPD_SRVIP_NOT_FOUND, /* provided IP not found
315 * OR provided IP found and preference is not match and an IP
316 * matching preference was found */
317 DNS_UPD_CNAME, /* CNAME without any IP provided in the response */
318 DNS_UPD_NAME_ERROR, /* name in the response did not match the query */
Baptiste Assmann0453a1d2015-09-09 00:51:08 +0200319 DNS_UPD_NO_IP_FOUND, /* no IP could be found in the response */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200320 DNS_UPD_OBSOLETE_IP, /* The server IP was obsolete, and no other IP was found */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200321};
322
Olivier Houchard8da5f982017-08-04 18:35:36 +0200323struct dns_srvrq {
324 enum obj_type obj_type; /* object type == OBJ_TYPE_SRVRQ */
325 struct dns_resolvers *resolvers; /* pointer to the resolvers structure used for this server template */
326
327 struct dns_resolution *resolution; /* server name resolution */
328
329 struct proxy *proxy; /* associated proxy */
330 char *name;
331 char *hostname_dn; /* server hostname in Domain Name format */
332 int hostname_dn_len; /* string length of the server hostname in Domain Name format */
333 struct dns_requester *dns_requester; /* used to link to its DNS resolution */
334 int inter; /* time in ms */
335 struct list list; /* Next SRV RQ for the same proxy */
336};
337
Baptiste Assmann325137d2015-04-13 23:40:55 +0200338#endif /* _TYPES_DNS_H */