blob: 2c602eaccffd4f9302e6d0822ac588a7345d24f1 [file] [log] [blame]
Emeric Brunc9437992021-02-12 19:42:55 +01001/*
2 * include/haproxy/dns-t.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 _HAPROXY_RESOLVERS_T_H
23#define _HAPROXY_RESOLVERS_T_H
24
25#include <import/eb32tree.h>
26
27#include <haproxy/connection-t.h>
28#include <haproxy/dns-t.h>
29#include <haproxy/obj_type-t.h>
30#include <haproxy/stats-t.h>
31#include <haproxy/task-t.h>
32#include <haproxy/thread.h>
33
34extern struct pool_head *resolv_requester_pool;
35
36/*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 */
42#define DNS_MAX_LABEL_SIZE 63
43#define DNS_MAX_NAME_SIZE 255
Emeric Brun4c751952021-03-08 16:41:29 +010044#define DNS_MAX_UDP_MESSAGE 65535
Emeric Brunc9437992021-02-12 19:42:55 +010045
46/* DNS minimum record size: 1 char + 1 NULL + type + class */
47#define DNS_MIN_RECORD_SIZE (1 + 1 + 2 + 2)
48
49/* DNS smallest fqdn 'a.gl' size */
50# define DNS_SMALLEST_FQDN_SIZE 4
51
52/* 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
59/* 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
65/* DNS error messages */
66#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"
69
70/* dns query class */
71#define DNS_RCLASS_IN 1 /* internet class */
72
73/* dns record types (non exhaustive list) */
74#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 */
80
81/* dns rcode values */
82#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 */
85
86/* dns flags masks */
87#define DNS_FLAG_TRUNCATED 0x0200 /* mask for truncated flag */
88#define DNS_FLAG_REPLYCODE 0x000F /* mask for reply code */
89
90/* max number of network preference entries are available from the
91 * configuration file.
92 */
93#define SRV_MAX_PREF_NET 5
94
95/* NOTE: big endian structure */
96struct resolv_query_item {
97 char name[DNS_MAX_NAME_SIZE+1]; /* query name */
98 unsigned short type; /* question type */
99 unsigned short class; /* query class */
100 struct list list;
101};
102
103/* NOTE: big endian structure */
104struct resolv_answer_item {
105 /*For SRV type, name also includes service and protocol value */
106 char name[DNS_MAX_NAME_SIZE+1]; /* answer name */
107 int16_t type; /* question type */
108 int16_t class; /* query class */
109 int32_t ttl; /* response TTL */
110 int16_t priority; /* SRV type priority */
111 uint16_t weight; /* SRV type weight */
112 uint16_t port; /* SRV type port */
Willy Tarreau20a02372021-10-14 22:52:04 +0200113 uint16_t data_len; /* number of bytes in the <data> field below */
Willy Tarreauc54aaba2021-10-14 22:30:38 +0200114 union {
115 struct sockaddr_in in4; /* IPv4 address for RTYPE_A */
116 struct sockaddr_in6 in6; /* IPv6 address for RTYPE_AAAA */
Willy Tarreau20a02372021-10-14 22:52:04 +0200117 char target[DNS_MAX_NAME_SIZE+1]; /* Response data: SRV or CNAME type target */
118 } data;
Christopher Faulet55c1c402021-03-11 09:36:05 +0100119 unsigned int last_seen; /* When was the answer was last seen */
Emeric Brunc9437992021-02-12 19:42:55 +0100120 struct resolv_answer_item *ar_item; /* pointer to a RRset from the additional section, if exists */
Emeric Brunf9ca5d82021-06-11 10:08:05 +0200121 struct list attached_servers; /* attached server head */
Emeric Brunc9437992021-02-12 19:42:55 +0100122 struct list list;
123};
124
125struct resolv_response {
126 struct dns_header header;
127 struct list query_list;
128 struct list answer_list;
129 /* authority ignored for now */
130};
131
132/* Resolvers section and parameters. It is linked to the name servers
133 * servers points to it.
134 * current resolution are stored in a FIFO list.
135 */
136struct resolvers {
137 __decl_thread(HA_SPINLOCK_T lock);
138 unsigned int accepted_payload_size; /* maximum payload size we accept for responses */
139 int nb_nameservers; /* total number of active nameservers in a resolvers section */
140 int resolve_retries; /* number of retries before giving up */
141 struct { /* time to: */
142 int resolve; /* wait between 2 queries for the same resolution */
143 int retry; /* wait for a response before retrying */
144 } timeout;
145 struct { /* time to hold current data when */
146 int valid; /* a response is valid */
147 int nx; /* a response doesn't exist */
148 int timeout; /* no answer was delivered */
149 int refused; /* dns server refused to answer */
150 int other; /* other dns response errors */
151 int obsolete; /* an answer hasn't been seen */
152 } hold;
153 struct task *t; /* timeout management */
154 struct {
155 struct list wait; /* resolutions managed to this resolvers section */
156 struct list curr; /* current running resolutions */
157 } resolutions;
158 struct eb_root query_ids; /* tree to quickly lookup/retrieve query ids currently in use
159 * used by each nameserver, but stored in resolvers since there must
160 * be a unique relation between an eb_root and an eb_node (resolution) */
161 struct list list; /* resolvers list */
162 struct list nameservers; /* dns server list */
163 struct proxy *px; /* px to handle connections to DNS servers */
164 char *id; /* resolvers unique identifier */
165 struct {
166 const char *file; /* file where the section appears */
167 int line; /* line where the section appears */
168 } conf; /* config information */
169};
170
171struct resolv_options {
172 int family_prio; /* which IP family should the resolver use when both are returned */
173 struct {
174 int family;
175 union {
176 struct in_addr in4;
177 struct in6_addr in6;
178 } addr;
179 union {
180 struct in_addr in4;
181 struct in6_addr in6;
182 } mask;
183 } pref_net[SRV_MAX_PREF_NET];
184 int pref_net_nb; /* The number of registered preferred networks. */
185 int accept_duplicate_ip; /* flag to indicate whether the associated object can use an IP address
186 already set to an other object of the same group */
187 int ignore_weight; /* flag to indicate whether to ignore the weight within the record */
188};
189
190/* Resolution structure associated to single server and used to manage name
191 * resolution for this server.
192 * The only link between the resolution and a nameserver is through the
193 * query_id.
194 */
195struct resolv_resolution {
196 struct resolvers *resolvers; /* pointer to the resolvers structure owning the resolution */
197 struct list requesters; /* list of requesters using this resolution */
198 int uuid; /* unique id (used for debugging purpose) */
199 char *hostname_dn; /* server hostname in domain name label format */
200 int hostname_dn_len; /* server domain name label len */
201 unsigned int last_resolution; /* time of the last resolution */
202 unsigned int last_query; /* time of the last query sent */
203 unsigned int last_valid; /* time of the last valid response */
204 int query_id; /* DNS query ID dedicated for this resolution */
205 struct eb32_node qid; /* ebtree query id */
206 int prefered_query_type; /* preferred query type */
207 int query_type; /* current query type */
208 int status; /* status of the resolution being processed RSLV_STATUS_* */
209 int step; /* RSLV_STEP_* */
210 int try; /* current resolution try */
211 int nb_queries; /* count number of queries sent */
212 int nb_responses; /* count number of responses received */
213
214 struct resolv_response response; /* structure hosting the DNS response */
215 struct resolv_query_item response_query_records[DNS_MAX_QUERY_RECORDS]; /* <response> query records */
216
217 struct list list; /* resolution list */
218};
219
220/* Structure used to describe the owner of a DNS resolution. */
221struct resolv_requester {
222 enum obj_type *owner; /* pointer to the owner (server or dns_srvrq) */
223 struct resolv_resolution *resolution; /* pointer to the owned DNS resolution */
224
225 int (*requester_cb)(struct resolv_requester *, struct dns_counters *); /* requester callback for valid response */
226 int (*requester_error_cb)(struct resolv_requester *, int); /* requester callback, for error management */
227
228 struct list list; /* requester list */
229};
230
231/* Last resolution status code */
232enum {
233 RSLV_STATUS_NONE = 0, /* no resolution occurred yet */
234 RSLV_STATUS_VALID, /* no error */
235 RSLV_STATUS_INVALID, /* invalid responses */
236 RSLV_STATUS_ERROR, /* error */
237 RSLV_STATUS_NX, /* NXDOMAIN */
238 RSLV_STATUS_REFUSED, /* server refused our query */
239 RSLV_STATUS_TIMEOUT, /* no response from DNS servers */
240 RSLV_STATUS_OTHER, /* other errors */
241};
242
243/* Current resolution step */
244enum {
245 RSLV_STEP_NONE = 0, /* nothing happening currently */
246 RSLV_STEP_RUNNING, /* resolution is running */
247};
248
249/* Return codes after analyzing a DNS response */
250enum {
251 RSLV_RESP_VALID = 0, /* valid response */
252 RSLV_RESP_INVALID, /* invalid response (various type of errors can trigger it) */
253 RSLV_RESP_ERROR, /* DNS error code */
254 RSLV_RESP_NX_DOMAIN, /* resolution unsuccessful */
255 RSLV_RESP_REFUSED, /* DNS server refused to answer */
256 RSLV_RESP_ANCOUNT_ZERO, /* no answers in the response */
257 RSLV_RESP_WRONG_NAME, /* response does not match query name */
258 RSLV_RESP_CNAME_ERROR, /* error when resolving a CNAME in an atomic response */
259 RSLV_RESP_TIMEOUT, /* DNS server has not answered in time */
260 RSLV_RESP_TRUNCATED, /* DNS response is truncated */
261 RSLV_RESP_NO_EXPECTED_RECORD, /* No expected records were found in the response */
262 RSLV_RESP_QUERY_COUNT_ERROR, /* we did not get the expected number of queries in the response */
263 RSLV_RESP_INTERNAL, /* internal resolver error */
264};
265
266/* Return codes after searching an IP in a DNS response buffer, using a family
267 * preference
268 */
269enum {
270 RSLV_UPD_NO = 1, /* provided IP was found and preference is matched
271 * OR provided IP found and preference is not matched, but no IP
272 * matching preference was found.
273 */
274 RSLV_UPD_SRVIP_NOT_FOUND, /* provided IP not found
275 * OR provided IP found and preference is not match and an IP
276 * matching preference was found.
277 */
278 RSLV_UPD_CNAME, /* CNAME without any IP provided in the response */
279 RSLV_UPD_NAME_ERROR, /* name in the response did not match the query */
280 RSLV_UPD_NO_IP_FOUND, /* no IP could be found in the response */
281 RSLV_UPD_OBSOLETE_IP, /* The server IP was obsolete, and no other IP was found */
282};
283
284struct proxy;
285struct resolv_srvrq {
286 enum obj_type obj_type; /* object type == OBJ_TYPE_SRVRQ */
287 struct resolvers *resolvers; /* pointer to the resolvers structure used for this server template */
288 struct proxy *proxy; /* associated proxy */
289 char *name;
290 char *hostname_dn; /* server hostname in Domain Name format */
291 int hostname_dn_len; /* string length of the server hostname in Domain Name format */
292 struct resolv_requester *requester; /* used to link to its DNS resolution */
Emeric Brun0c4a8a32021-06-11 10:48:45 +0200293 struct list attached_servers; /* List of the servers free to use */
294 struct eb_root named_servers; /* tree of servers indexed by hostnames found in server state file */
Emeric Brunc9437992021-02-12 19:42:55 +0100295 struct list list; /* Next SRV RQ for the same proxy */
296};
297
298#endif /* _HAPROXY_RESOLVERS_T_H */