blob: a8d468cf4dd3c1c94c5dcf602db00bf698e94c55 [file] [log] [blame]
Baptiste Assmann325137d2015-04-13 23:40:55 +02001/*
2 * Name server resolution
3 *
4 * Copyright 2014 Baptiste Assmann <bedis9@gmail.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <errno.h>
14#include <fcntl.h>
15#include <stdio.h>
16#include <stdlib.h>
17#include <string.h>
18#include <unistd.h>
19
20#include <sys/types.h>
21
Christopher Faulet67957bd2017-09-27 11:00:59 +020022#include <common/errors.h>
Baptiste Assmann325137d2015-04-13 23:40:55 +020023#include <common/time.h>
24#include <common/ticks.h>
Olivier Houchard8da5f982017-08-04 18:35:36 +020025#include <common/net_helper.h>
Baptiste Assmann325137d2015-04-13 23:40:55 +020026
William Lallemand69e96442016-11-19 00:58:54 +010027#include <types/applet.h>
28#include <types/cli.h>
Baptiste Assmann325137d2015-04-13 23:40:55 +020029#include <types/global.h>
30#include <types/dns.h>
William Lallemand69e96442016-11-19 00:58:54 +010031#include <types/stats.h>
Baptiste Assmann325137d2015-04-13 23:40:55 +020032
William Lallemand69e96442016-11-19 00:58:54 +010033#include <proto/channel.h>
34#include <proto/cli.h>
Baptiste Assmann325137d2015-04-13 23:40:55 +020035#include <proto/checks.h>
36#include <proto/dns.h>
37#include <proto/fd.h>
38#include <proto/log.h>
39#include <proto/server.h>
40#include <proto/task.h>
41#include <proto/proto_udp.h>
Christopher Faulet67957bd2017-09-27 11:00:59 +020042#include <proto/proxy.h>
William Lallemand69e96442016-11-19 00:58:54 +010043#include <proto/stream_interface.h>
Baptiste Assmann325137d2015-04-13 23:40:55 +020044
Christopher Faulet67957bd2017-09-27 11:00:59 +020045struct list dns_resolvers = LIST_HEAD_INIT(dns_resolvers);
46struct list dns_srvrq_list = LIST_HEAD_INIT(dns_srvrq_list);
Baptiste Assmann325137d2015-04-13 23:40:55 +020047
Christopher Fauletb2812a62017-10-04 16:17:58 +020048static THREAD_LOCAL int64_t dns_query_id_seed = 0; /* random seed */
Christopher Faulet67957bd2017-09-27 11:00:59 +020049static struct pool_head *dns_answer_item_pool = NULL;
50static struct pool_head *dns_resolution_pool = NULL;
51static unsigned int resolution_uuid = 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020052
Christopher Faulet67957bd2017-09-27 11:00:59 +020053/* Returns a pointer to the resolvers matching the id <id>. NULL is returned if
54 * no match is found.
Baptiste Assmann325137d2015-04-13 23:40:55 +020055 */
Christopher Faulet67957bd2017-09-27 11:00:59 +020056struct dns_resolvers *find_resolvers_by_id(const char *id)
Baptiste Assmann201c07f2017-05-22 15:17:15 +020057{
Christopher Faulet67957bd2017-09-27 11:00:59 +020058 struct dns_resolvers *res;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020059
Christopher Faulet67957bd2017-09-27 11:00:59 +020060 list_for_each_entry(res, &dns_resolvers, list) {
61 if (!strcmp(res->id, id))
62 return res;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020063 }
Christopher Faulet67957bd2017-09-27 11:00:59 +020064 return NULL;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020065}
66
Christopher Faulet67957bd2017-09-27 11:00:59 +020067/* Returns a pointer on the SRV request matching the name <name> for the proxy
68 * <px>. NULL is returned if no match is found.
Baptiste Assmann201c07f2017-05-22 15:17:15 +020069 */
Christopher Faulet67957bd2017-09-27 11:00:59 +020070struct dns_srvrq *find_srvrq_by_name(const char *name, struct proxy *px)
Baptiste Assmann201c07f2017-05-22 15:17:15 +020071{
Christopher Faulet67957bd2017-09-27 11:00:59 +020072 struct dns_srvrq *srvrq;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020073
Christopher Faulet67957bd2017-09-27 11:00:59 +020074 list_for_each_entry(srvrq, &dns_srvrq_list, list) {
75 if (srvrq->proxy == px && !strcmp(srvrq->name, name))
76 return srvrq;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020077 }
Christopher Faulet67957bd2017-09-27 11:00:59 +020078 return NULL;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020079}
80
Christopher Faulet67957bd2017-09-27 11:00:59 +020081/* Allocates a new SRVRQ for the given server with the name <fqdn>. It returns
82 * NULL if an error occurred. */
83struct dns_srvrq *new_dns_srvrq(struct server *srv, char *fqdn)
Baptiste Assmann201c07f2017-05-22 15:17:15 +020084{
Christopher Faulet67957bd2017-09-27 11:00:59 +020085 struct proxy *px = srv->proxy;
86 struct dns_srvrq *srvrq = NULL;
87 int fqdn_len, hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020088
Christopher Faulet67957bd2017-09-27 11:00:59 +020089 fqdn_len = strlen(fqdn);
90 hostname_dn_len = dns_str_to_dn_label(fqdn, fqdn_len + 1, trash.str, trash.size);
91 if (hostname_dn_len == -1) {
92 Alert("config : %s '%s', server '%s': failed to parse FQDN '%s'\n",
93 proxy_type_str(px), px->id, srv->id, fqdn);
94 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +020095 }
96
Christopher Faulet67957bd2017-09-27 11:00:59 +020097 if ((srvrq = calloc(1, sizeof(*srvrq))) == NULL) {
98 Alert("config : %s '%s', server '%s': out of memory\n",
99 proxy_type_str(px), px->id, srv->id);
100 goto err;
101 }
102 srvrq->obj_type = OBJ_TYPE_SRVRQ;
103 srvrq->proxy = px;
104 srvrq->name = strdup(fqdn);
105 srvrq->hostname_dn = strdup(trash.str);
106 srvrq->hostname_dn_len = hostname_dn_len;
107 if (!srvrq->name || !srvrq->hostname_dn) {
108 Alert("config : %s '%s', server '%s': out of memory\n",
109 proxy_type_str(px), px->id, srv->id);
110 goto err;
111 }
112 LIST_ADDQ(&dns_srvrq_list, &srvrq->list);
113 return srvrq;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200114
Christopher Faulet67957bd2017-09-27 11:00:59 +0200115 err:
116 if (srvrq) {
117 free(srvrq->name);
118 free(srvrq->hostname_dn);
119 free(srvrq);
120 }
121 return NULL;
122}
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200123
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200124
Christopher Faulet67957bd2017-09-27 11:00:59 +0200125/* 2 bytes random generator to generate DNS query ID */
126static inline uint16_t dns_rnd16(void)
127{
Christopher Fauletb2812a62017-10-04 16:17:58 +0200128 if (!dns_query_id_seed)
129 dns_query_id_seed = now_ms;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200130 dns_query_id_seed ^= dns_query_id_seed << 13;
131 dns_query_id_seed ^= dns_query_id_seed >> 7;
132 dns_query_id_seed ^= dns_query_id_seed << 17;
133 return dns_query_id_seed;
134}
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200135
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200136
Christopher Faulet67957bd2017-09-27 11:00:59 +0200137static inline int dns_resolution_timeout(struct dns_resolution *res)
138{
139 switch (res->status) {
140 case RSLV_STATUS_VALID: return res->resolvers->hold.valid;
141 default: return res->resolvers->timeout.resolve;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200142 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200143}
144
Christopher Faulet67957bd2017-09-27 11:00:59 +0200145/* Updates a resolvers' task timeout for next wake up and queue it */
146static void dns_update_resolvers_timeout(struct dns_resolvers *resolvers)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200147{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200148 struct dns_resolution *res;
149 int next;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200150
Christopher Faulet67957bd2017-09-27 11:00:59 +0200151 next = tick_add(now_ms, resolvers->timeout.resolve);
152 if (!LIST_ISEMPTY(&resolvers->resolutions.curr)) {
153 res = LIST_NEXT(&resolvers->resolutions.curr, struct dns_resolution *, list);
154 next = MIN(next, tick_add(res->last_query, resolvers->timeout.retry));
155 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200156
Christopher Faulet67957bd2017-09-27 11:00:59 +0200157 list_for_each_entry(res, &resolvers->resolutions.wait, list)
158 next = MIN(next, tick_add(res->last_resolution, dns_resolution_timeout(res)));
Baptiste Assmann325137d2015-04-13 23:40:55 +0200159
Christopher Faulet67957bd2017-09-27 11:00:59 +0200160 resolvers->t->expire = next;
161 task_queue(resolvers->t);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200162}
163
Christopher Faulet67957bd2017-09-27 11:00:59 +0200164/* Opens an UDP socket on the namesaver's IP/Port, if required. Returns 0 on
165 * success, -1 otherwise.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200166 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200167static int dns_connect_namesaver(struct dns_nameserver *ns)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200168{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200169 struct dgram_conn *dgram = ns->dgram;
170 int fd;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200171
Christopher Faulet67957bd2017-09-27 11:00:59 +0200172 /* Already connected */
173 if (dgram->t.sock.fd != -1)
174 return 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200175
Christopher Faulet67957bd2017-09-27 11:00:59 +0200176 /* Create an UDP socket and connect it on the nameserver's IP/Port */
177 if ((fd = socket(ns->addr.ss_family, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
178 send_log(NULL, LOG_WARNING,
179 "DNS : resolvers '%s': can't create socket for nameserver '%s'.\n",
180 ns->resolvers->id, ns->id);
181 return -1;
182 }
183 if (connect(fd, (struct sockaddr*)&ns->addr, get_addr_len(&ns->addr)) == -1) {
184 send_log(NULL, LOG_WARNING,
185 "DNS : resolvers '%s': can't connect socket for nameserver '%s'.\n",
186 ns->resolvers->id, ns->id);
187 close(fd);
188 return -1;
189 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200190
Christopher Faulet67957bd2017-09-27 11:00:59 +0200191 /* Make the socket non blocking */
192 fcntl(fd, F_SETFL, O_NONBLOCK);
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200193
Christopher Faulet67957bd2017-09-27 11:00:59 +0200194 /* Add the fd in the fd list and update its parameters */
195 dgram->t.sock.fd = fd;
196 fdtab[fd].owner = dgram;
197 fdtab[fd].iocb = dgram_fd_handler;
Christopher Faulet36716a72017-05-30 11:07:16 +0200198 fd_insert(fd, (unsigned long)-1);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200199 fd_want_recv(fd);
200 return 0;
201}
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200202
Christopher Faulet67957bd2017-09-27 11:00:59 +0200203/* Forges a DNS query. It needs the following information from the caller:
204 * - <query_id> : the DNS query id corresponding to this query
205 * - <query_type> : DNS_RTYPE_* request DNS record type (A, AAAA, ANY...)
206 * - <hostname_dn> : hostname in domain name format
207 * - <hostname_dn_len> : length of <hostname_dn>
208 *
209 * To store the query, the caller must pass a buffer <buf> and its size
210 * <bufsize>. It returns the number of written bytes in success, -1 if <buf> is
211 * too short.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200212 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200213static int dns_build_query(int query_id, int query_type, unsigned int accepted_payload_size,
214 char *hostname_dn, int hostname_dn_len, char *buf, int bufsize)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200215{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200216 struct dns_header dns_hdr;
217 struct dns_question qinfo;
218 struct dns_additional_record edns;
219 char *p = buf;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +0200220
Christopher Faulet67957bd2017-09-27 11:00:59 +0200221 if (sizeof(dns_hdr) + sizeof(qinfo) + sizeof(edns) + hostname_dn_len >= bufsize)
222 return -1;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +0200223
Christopher Faulet67957bd2017-09-27 11:00:59 +0200224 memset(buf, 0, bufsize);
Baptiste Assmannfa4a6632017-05-04 09:05:00 +0200225
Christopher Faulet67957bd2017-09-27 11:00:59 +0200226 /* Set dns query headers */
227 dns_hdr.id = (unsigned short) htons(query_id);
228 dns_hdr.flags = htons(0x0100); /* qr=0, opcode=0, aa=0, tc=0, rd=1, ra=0, z=0, rcode=0 */
229 dns_hdr.qdcount = htons(1); /* 1 question */
230 dns_hdr.ancount = 0;
231 dns_hdr.nscount = 0;
232 dns_hdr.arcount = htons(1);
233 memcpy(p, &dns_hdr, sizeof(dns_hdr));
234 p += sizeof(dns_hdr);
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200235
Christopher Faulet67957bd2017-09-27 11:00:59 +0200236 /* Set up query hostname */
237 memcpy(p, hostname_dn, hostname_dn_len);
238 p += hostname_dn_len;
239 *p++ = 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200240
Christopher Faulet67957bd2017-09-27 11:00:59 +0200241 /* Set up query info (type and class) */
242 qinfo.qtype = htons(query_type);
243 qinfo.qclass = htons(DNS_RCLASS_IN);
244 memcpy(p, &qinfo, sizeof(qinfo));
245 p += sizeof(qinfo);
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200246
Christopher Faulet67957bd2017-09-27 11:00:59 +0200247 /* Set the DNS extension */
248 edns.name = 0;
249 edns.type = htons(DNS_RTYPE_OPT);
250 edns.udp_payload_size = htons(accepted_payload_size);
251 edns.extension = 0;
252 edns.data_length = 0;
253 memcpy(p, &edns, sizeof(edns));
254 p += sizeof(edns);
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200255
Christopher Faulet67957bd2017-09-27 11:00:59 +0200256 return (p - buf);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200257}
258
Christopher Faulet67957bd2017-09-27 11:00:59 +0200259/* Sends a DNS query to resolvers associated to a resolution. It returns 0 on
260 * success, -1 otherwise.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200261 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200262static int dns_send_query(struct dns_resolution *resolution)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200263{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200264 struct dns_resolvers *resolvers = resolution->resolvers;
265 struct dns_nameserver *ns;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200266
Christopher Faulet67957bd2017-09-27 11:00:59 +0200267 list_for_each_entry(ns, &resolvers->nameservers, list) {
268 int fd = ns->dgram->t.sock.fd;
269 if (fd == -1) {
270 if (dns_connect_namesaver(ns) == -1)
271 continue;
272 fd = ns->dgram->t.sock.fd;
273 resolvers->nb_nameservers++;
274 }
275 fd_want_send(fd);
276 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200277
Christopher Faulet67957bd2017-09-27 11:00:59 +0200278 /* Update resolution */
279 resolution->nb_queries = 0;
280 resolution->nb_responses = 0;
281 resolution->last_query = now_ms;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200282
Christopher Faulet67957bd2017-09-27 11:00:59 +0200283 /* Push the resolution at the end of the active list */
284 LIST_DEL(&resolution->list);
285 LIST_ADDQ(&resolvers->resolutions.curr, &resolution->list);
286 return 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200287}
288
Christopher Faulet67957bd2017-09-27 11:00:59 +0200289/* Prepares and sends a DNS resolution. It returns 1 if the query was sent, 0 if
290 * skipped and -1 if an error occurred.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200291 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200292static int
293dns_run_resolution(struct dns_resolution *resolution)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200294{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200295 struct dns_resolvers *resolvers = resolution->resolvers;
296 int query_id, i;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200297
Christopher Faulet67957bd2017-09-27 11:00:59 +0200298 /* Avoid sending requests for resolutions that don't yet have an
299 * hostname, ie resolutions linked to servers that do not yet have an
300 * fqdn */
301 if (!resolution->hostname_dn)
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200302 return 0;
303
Christopher Faulet67957bd2017-09-27 11:00:59 +0200304 /* Check if a resolution has already been started for this server return
305 * directly to avoid resolution pill up. */
306 if (resolution->step != RSLV_STEP_NONE)
307 return 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200308
Christopher Faulet67957bd2017-09-27 11:00:59 +0200309 /* Generates a new query id. We try at most 100 times to find a free
310 * query id */
311 for (i = 0; i < 100; ++i) {
312 query_id = dns_rnd16();
313 if (!eb32_lookup(&resolvers->query_ids, query_id))
Olivier Houchard8da5f982017-08-04 18:35:36 +0200314 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200315 query_id = -1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200316 }
Christopher Faulet67957bd2017-09-27 11:00:59 +0200317 if (query_id == -1) {
318 send_log(NULL, LOG_NOTICE,
319 "could not generate a query id for %s, in resolvers %s.\n",
320 resolution->hostname_dn, resolvers->id);
321 return -1;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200322 }
323
Christopher Faulet67957bd2017-09-27 11:00:59 +0200324 /* Update resolution parameters */
325 resolution->query_id = query_id;
326 resolution->qid.key = query_id;
327 resolution->step = RSLV_STEP_RUNNING;
328 resolution->query_type = resolution->prefered_query_type;
329 resolution->try = resolvers->resolve_retries;
330 eb32_insert(&resolvers->query_ids, &resolution->qid);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200331
Christopher Faulet67957bd2017-09-27 11:00:59 +0200332 /* Send the DNS query */
333 resolution->try -= 1;
334 dns_send_query(resolution);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200335 return 1;
336}
337
Christopher Faulet67957bd2017-09-27 11:00:59 +0200338/* Performs a name resolution for the requester <req> */
339void dns_trigger_resolution(struct dns_requester *req)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200340{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200341 struct dns_resolvers *resolvers;
342 struct dns_resolution *res;
343 int exp;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200344
Christopher Faulet67957bd2017-09-27 11:00:59 +0200345 if (!req || !req->resolution)
346 return;
347 res = req->resolution;
348 resolvers = res->resolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200349
Christopher Faulet67957bd2017-09-27 11:00:59 +0200350 /* The resolution must not be triggered yet. Use the cached response, if
351 * valid */
352 exp = tick_add(res->last_resolution, resolvers->hold.valid);
353 if (res->status == RSLV_STATUS_VALID &&
354 tick_isset(res->last_resolution) && !tick_is_expired(exp, now_ms))
355 req->requester_cb(req, NULL);
356 else
357 dns_run_resolution(res);
358}
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200359
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200360
Christopher Faulet67957bd2017-09-27 11:00:59 +0200361/* Resets some resolution parameters to initial values and also delete the query
362 * ID from the resolver's tree.
363 */
364static void dns_reset_resolution(struct dns_resolution *resolution)
365{
366 /* update resolution status */
367 resolution->step = RSLV_STEP_NONE;
368 resolution->try = 0;
369 resolution->last_resolution = now_ms;
370 resolution->nb_queries = 0;
371 resolution->nb_responses = 0;
372 resolution->query_type = resolution->prefered_query_type;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200373
Christopher Faulet67957bd2017-09-27 11:00:59 +0200374 /* clean up query id */
375 eb32_delete(&resolution->qid);
376 resolution->query_id = 0;
377 resolution->qid.key = 0;
378}
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200379
Christopher Faulet67957bd2017-09-27 11:00:59 +0200380/* Returns the query id contained in a DNS response */
381static inline unsigned short dns_response_get_query_id(unsigned char *resp)
382{
383 return resp[0] * 256 + resp[1];
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200384}
385
Christopher Faulet67957bd2017-09-27 11:00:59 +0200386
387/* Analyses, re-builds and copies the name <name> from the DNS response packet
388 * <buffer>. <name> must point to the 'data_len' information or pointer 'c0'
389 * for compressed data. The result is copied into <dest>, ensuring we don't
390 * overflow using <dest_len> Returns the number of bytes the caller can move
391 * forward. If 0 it means an error occurred while parsing the name. <offset> is
392 * the number of bytes the caller could move forward.
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200393 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200394int dns_read_name(unsigned char *buffer, unsigned char *bufend,
395 unsigned char *name, char *destination, int dest_len,
396 int *offset)
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200397{
398 int nb_bytes = 0, n = 0;
399 int label_len;
400 unsigned char *reader = name;
401 char *dest = destination;
402
403 while (1) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200404 /* Name compression is in use */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200405 if ((*reader & 0xc0) == 0xc0) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200406 /* Must point BEFORE current position */
407 if ((buffer + reader[1]) > reader)
408 goto err;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200409
Christopher Faulet67957bd2017-09-27 11:00:59 +0200410 n = dns_read_name(buffer, bufend, buffer + reader[1],
411 dest, dest_len - nb_bytes, offset);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200412 if (n == 0)
Christopher Faulet67957bd2017-09-27 11:00:59 +0200413 goto err;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200414
Christopher Faulet67957bd2017-09-27 11:00:59 +0200415 dest += n;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200416 nb_bytes += n;
417 goto out;
418 }
419
420 label_len = *reader;
421 if (label_len == 0)
422 goto out;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200423
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200424 /* Check if:
425 * - we won't read outside the buffer
426 * - there is enough place in the destination
427 */
428 if ((reader + label_len >= bufend) || (nb_bytes + label_len >= dest_len))
Christopher Faulet67957bd2017-09-27 11:00:59 +0200429 goto err;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200430
431 /* +1 to take label len + label string */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200432 label_len++;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200433
434 memcpy(dest, reader, label_len);
435
Christopher Faulet67957bd2017-09-27 11:00:59 +0200436 dest += label_len;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200437 nb_bytes += label_len;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200438 reader += label_len;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200439 }
440
Christopher Faulet67957bd2017-09-27 11:00:59 +0200441 out:
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200442 /* offset computation:
443 * parse from <name> until finding either NULL or a pointer "c0xx"
444 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200445 reader = name;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200446 *offset = 0;
447 while (reader < bufend) {
448 if ((reader[0] & 0xc0) == 0xc0) {
449 *offset += 2;
450 break;
451 }
452 else if (*reader == 0) {
453 *offset += 1;
454 break;
455 }
456 *offset += 1;
457 ++reader;
458 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200459 return nb_bytes;
460
Christopher Faulet67957bd2017-09-27 11:00:59 +0200461 err:
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200462 return 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200463}
464
Christopher Faulet67957bd2017-09-27 11:00:59 +0200465/* Checks for any obsolete record, also identify any SRV request, and try to
466 * find a corresponding server.
467*/
468static void dns_check_dns_response(struct dns_resolution *res)
469{
470 struct dns_resolvers *resolvers = res->resolvers;
471 struct dns_requester *req, *reqback;
472 struct dns_answer_item *item, *itemback;
473 struct server *srv;
474 struct dns_srvrq *srvrq;
475
476 list_for_each_entry_safe(item, itemback, &res->response.answer_list, list) {
477
478 /* Remove obsolete items */
479 if ((item->last_seen + resolvers->hold.obsolete / 1000) < now.tv_sec) {
480 if (item->type != DNS_RTYPE_SRV)
481 goto rm_obselete_item;
482
483 list_for_each_entry_safe(req, reqback, &res->requesters, list) {
484 if ((srvrq = objt_dns_srvrq(req->owner)) == NULL)
485 continue;
486
487 /* Remove any associated server */
488 for (srv = srvrq->proxy->srv; srv != NULL; srv = srv->next) {
489 if (srv->srvrq == srvrq && srv->svc_port == item->port &&
490 item->data_len == srv->hostname_dn_len &&
491 !memcmp(srv->hostname_dn, item->target, item->data_len)) {
492 snr_update_srv_status(srv, 1);
493 free(srv->hostname);
494 free(srv->hostname_dn);
495 srv->hostname = NULL;
496 srv->hostname_dn = NULL;
497 srv->hostname_dn_len = 0;
498 dns_unlink_resolution(srv->dns_requester);
499 }
500 }
501 }
502
503 rm_obselete_item:
504 LIST_DEL(&item->list);
505 pool_free2(dns_answer_item_pool, item);
506 continue;
507 }
508
509 if (item->type != DNS_RTYPE_SRV)
510 continue;
511
512 /* Now process SRV records */
513 list_for_each_entry_safe(req, reqback, &res->requesters, list) {
514 if ((srvrq = objt_dns_srvrq(req->owner)) == NULL)
515 continue;
516
517 /* Check if a server already uses that hostname */
518 for (srv = srvrq->proxy->srv; srv != NULL; srv = srv->next) {
519 if (srv->srvrq == srvrq && srv->svc_port == item->port &&
520 item->data_len == srv->hostname_dn_len &&
521 !memcmp(srv->hostname_dn, item->target, item->data_len)) {
522 if (srv->uweight != item->weight) {
523 char weight[9];
524
525 snprintf(weight, sizeof(weight), "%d", item->weight);
526 server_parse_weight_change_request(srv, weight);
527 }
528 break;
529 }
530 }
531 if (srv)
532 continue;
533
534 /* If not, try to find a server with undefined hostname */
535 for (srv = srvrq->proxy->srv; srv != NULL; srv = srv->next) {
536 if (srv->srvrq == srvrq && !srv->hostname_dn)
537 break;
538 }
539 /* And update this server, if found */
540 if (srv) {
541 const char *msg = NULL;
542 char weight[9];
543 char hostname[DNS_MAX_NAME_SIZE];
544
545 if (dns_dn_label_to_str(item->target, item->data_len+1,
546 hostname, DNS_MAX_NAME_SIZE) == -1)
547 continue;
Olivier Houchardd16bfe62017-10-31 15:21:19 +0100548 msg = update_server_fqdn(srv, hostname, "SRV record", 1);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200549 if (msg)
550 send_log(srv->proxy, LOG_NOTICE, "%s", msg);
551
552 srv->svc_port = item->port;
553 srv->flags &= ~SRV_F_MAPPORTS;
554 if ((srv->check.state & CHK_ST_CONFIGURED) &&
555 !(srv->flags & SRV_F_CHECKPORT))
556 srv->check.port = item->port;
557 snprintf(weight, sizeof(weight), "%d", item->weight);
558 server_parse_weight_change_request(srv, weight);
559 }
560 }
561 }
562}
563
564/* Validates that the buffer DNS response provided in <resp> and finishing
565 * before <bufend> is valid from a DNS protocol point of view.
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200566 *
Christopher Faulet67957bd2017-09-27 11:00:59 +0200567 * The result is stored in <resolution>' response, buf_response,
568 * response_query_records and response_answer_records members.
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200569 *
570 * This function returns one of the DNS_RESP_* code to indicate the type of
571 * error found.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200572 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200573static int dns_validate_dns_response(unsigned char *resp, unsigned char *bufend,
574 struct dns_resolution *resolution, int max_answer_records)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200575{
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200576 unsigned char *reader;
577 char *previous_dname, tmpname[DNS_MAX_NAME_SIZE];
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200578 int len, flags, offset;
579 int dns_query_record_id;
Baptiste Assmann69fce672017-05-04 08:37:45 +0200580 int nb_saved_records;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200581 struct dns_query_item *dns_query;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200582 struct dns_answer_item *dns_answer_record, *tmp_record;
Baptiste Assmann729c9012017-05-22 15:13:10 +0200583 struct dns_response_packet *dns_p;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200584 int i, found = 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200585
Christopher Faulet67957bd2017-09-27 11:00:59 +0200586 reader = resp;
587 len = 0;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200588 previous_dname = NULL;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200589 dns_query = NULL;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200590
Christopher Faulet67957bd2017-09-27 11:00:59 +0200591 /* Initialization of response buffer and structure */
Baptiste Assmann729c9012017-05-22 15:13:10 +0200592 dns_p = &resolution->response;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200593
594 /* query id */
595 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200596 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200597 dns_p->header.id = reader[0] * 256 + reader[1];
598 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200599
Christopher Faulet67957bd2017-09-27 11:00:59 +0200600 /* Flags and rcode are stored over 2 bytes
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200601 * First byte contains:
602 * - response flag (1 bit)
603 * - opcode (4 bits)
604 * - authoritative (1 bit)
605 * - truncated (1 bit)
606 * - recursion desired (1 bit)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200607 */
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200608 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200609 return DNS_RESP_INVALID;
610
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200611 flags = reader[0] * 256 + reader[1];
612
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200613 if ((flags & DNS_FLAG_REPLYCODE) != DNS_RCODE_NO_ERROR) {
614 if ((flags & DNS_FLAG_REPLYCODE) == DNS_RCODE_NX_DOMAIN)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200615 return DNS_RESP_NX_DOMAIN;
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200616 else if ((flags & DNS_FLAG_REPLYCODE) == DNS_RCODE_REFUSED)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200617 return DNS_RESP_REFUSED;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200618 return DNS_RESP_ERROR;
619 }
620
Christopher Faulet67957bd2017-09-27 11:00:59 +0200621 /* Move forward 2 bytes for flags */
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200622 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200623
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200624 /* 2 bytes for question count */
625 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200626 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200627 dns_p->header.qdcount = reader[0] * 256 + reader[1];
Christopher Faulet67957bd2017-09-27 11:00:59 +0200628 /* (for now) we send one query only, so we expect only one in the
629 * response too */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200630 if (dns_p->header.qdcount != 1)
631 return DNS_RESP_QUERY_COUNT_ERROR;
632 if (dns_p->header.qdcount > DNS_MAX_QUERY_RECORDS)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200633 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200634 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200635
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200636 /* 2 bytes for answer count */
637 if (reader + 2 >= bufend)
638 return DNS_RESP_INVALID;
639 dns_p->header.ancount = reader[0] * 256 + reader[1];
640 if (dns_p->header.ancount == 0)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200641 return DNS_RESP_ANCOUNT_ZERO;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200642 /* Check if too many records are announced */
Baptiste Assmann9d8dbbc2017-08-18 23:35:08 +0200643 if (dns_p->header.ancount > max_answer_records)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200644 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200645 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200646
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200647 /* 2 bytes authority count */
648 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200649 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200650 dns_p->header.nscount = reader[0] * 256 + reader[1];
651 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200652
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200653 /* 2 bytes additional count */
654 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200655 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200656 dns_p->header.arcount = reader[0] * 256 + reader[1];
657 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200658
Christopher Faulet67957bd2017-09-27 11:00:59 +0200659 /* Parsing dns queries */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200660 LIST_INIT(&dns_p->query_list);
661 for (dns_query_record_id = 0; dns_query_record_id < dns_p->header.qdcount; dns_query_record_id++) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200662 /* Use next pre-allocated dns_query_item after ensuring there is
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200663 * still one available.
Christopher Faulet67957bd2017-09-27 11:00:59 +0200664 * It's then added to our packet query list. */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200665 if (dns_query_record_id > DNS_MAX_QUERY_RECORDS)
666 return DNS_RESP_INVALID;
Baptiste Assmann729c9012017-05-22 15:13:10 +0200667 dns_query = &resolution->response_query_records[dns_query_record_id];
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200668 LIST_ADDQ(&dns_p->query_list, &dns_query->list);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200669
Christopher Faulet67957bd2017-09-27 11:00:59 +0200670 /* Name is a NULL terminated string in our case, since we have
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200671 * one query per response and the first one can't be compressed
Christopher Faulet67957bd2017-09-27 11:00:59 +0200672 * (using the 0x0c format) */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200673 offset = 0;
674 len = dns_read_name(resp, bufend, reader, dns_query->name, DNS_MAX_NAME_SIZE, &offset);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200675
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200676 if (len == 0)
677 return DNS_RESP_INVALID;
678
679 reader += offset;
680 previous_dname = dns_query->name;
681
682 /* move forward 2 bytes for question type */
683 if (reader + 2 >= bufend)
684 return DNS_RESP_INVALID;
685 dns_query->type = reader[0] * 256 + reader[1];
686 reader += 2;
687
688 /* move forward 2 bytes for question class */
689 if (reader + 2 >= bufend)
690 return DNS_RESP_INVALID;
691 dns_query->class = reader[0] * 256 + reader[1];
692 reader += 2;
693 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200694
Baptiste Assmann251abb92017-08-11 09:58:27 +0200695 /* TRUNCATED flag must be checked after we could read the query type
Christopher Faulet67957bd2017-09-27 11:00:59 +0200696 * because a TRUNCATED SRV query type response can still be exploited */
Baptiste Assmann251abb92017-08-11 09:58:27 +0200697 if (dns_query->type != DNS_RTYPE_SRV && flags & DNS_FLAG_TRUNCATED)
698 return DNS_RESP_TRUNCATED;
699
Baptiste Assmann325137d2015-04-13 23:40:55 +0200700 /* now parsing response records */
Baptiste Assmann69fce672017-05-04 08:37:45 +0200701 nb_saved_records = 0;
Olivier Houchard8da5f982017-08-04 18:35:36 +0200702 for (i = 0; i < dns_p->header.ancount; i++) {
Baptiste Assmann325137d2015-04-13 23:40:55 +0200703 if (reader >= bufend)
704 return DNS_RESP_INVALID;
705
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200706 dns_answer_record = pool_alloc2(dns_answer_item_pool);
707 if (dns_answer_record == NULL)
708 return (DNS_RESP_INVALID);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200709
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200710 offset = 0;
711 len = dns_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200712
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200713 if (len == 0) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200714 pool_free2(dns_answer_item_pool, dns_answer_record);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200715 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200716 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200717
Christopher Faulet67957bd2017-09-27 11:00:59 +0200718 /* Check if the current record dname is valid. previous_dname
719 * points either to queried dname or last CNAME target */
Baptiste Assmannddc8ce62017-08-11 10:31:22 +0200720 if (dns_query->type != DNS_RTYPE_SRV && memcmp(previous_dname, tmpname, len) != 0) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200721 pool_free2(dns_answer_item_pool, dns_answer_record);
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200722 if (i == 0) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200723 /* First record, means a mismatch issue between
724 * queried dname and dname found in the first
725 * record */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200726 return DNS_RESP_INVALID;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200727 }
728 else {
729 /* If not the first record, this means we have a
730 * CNAME resolution error */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200731 return DNS_RESP_CNAME_ERROR;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200732 }
733
Baptiste Assmann325137d2015-04-13 23:40:55 +0200734 }
735
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200736 memcpy(dns_answer_record->name, tmpname, len);
737 dns_answer_record->name[len] = 0;
Baptiste Assmann2359ff12015-08-07 11:24:05 +0200738
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200739 reader += offset;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200740 if (reader >= bufend) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200741 pool_free2(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200742 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200743 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200744
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200745 /* 2 bytes for record type (A, AAAA, CNAME, etc...) */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200746 if (reader + 2 > bufend) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200747 pool_free2(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200748 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200749 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200750 dns_answer_record->type = reader[0] * 256 + reader[1];
751 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200752
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200753 /* 2 bytes for class (2) */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200754 if (reader + 2 > bufend) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200755 pool_free2(dns_answer_item_pool, dns_answer_record);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200756 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200757 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200758 dns_answer_record->class = reader[0] * 256 + reader[1];
Baptiste Assmann325137d2015-04-13 23:40:55 +0200759 reader += 2;
760
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200761 /* 4 bytes for ttl (4) */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200762 if (reader + 4 > bufend) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200763 pool_free2(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200764 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200765 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200766 dns_answer_record->ttl = reader[0] * 16777216 + reader[1] * 65536
767 + reader[2] * 256 + reader[3];
768 reader += 4;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200769
Christopher Faulet67957bd2017-09-27 11:00:59 +0200770 /* Now reading data len */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200771 if (reader + 2 > bufend) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200772 pool_free2(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200773 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200774 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200775 dns_answer_record->data_len = reader[0] * 256 + reader[1];
Baptiste Assmann325137d2015-04-13 23:40:55 +0200776
Christopher Faulet67957bd2017-09-27 11:00:59 +0200777 /* Move forward 2 bytes for data len */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200778 reader += 2;
779
Christopher Faulet67957bd2017-09-27 11:00:59 +0200780 /* Analyzing record content */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200781 switch (dns_answer_record->type) {
Baptiste Assmann325137d2015-04-13 23:40:55 +0200782 case DNS_RTYPE_A:
783 /* ipv4 is stored on 4 bytes */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200784 if (dns_answer_record->data_len != 4) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200785 pool_free2(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200786 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200787 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200788 dns_answer_record->address.sa_family = AF_INET;
789 memcpy(&(((struct sockaddr_in *)&dns_answer_record->address)->sin_addr),
790 reader, dns_answer_record->data_len);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200791 break;
792
793 case DNS_RTYPE_CNAME:
Christopher Faulet67957bd2017-09-27 11:00:59 +0200794 /* Check if this is the last record and update the caller about the status:
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200795 * no IP could be found and last record was a CNAME. Could be triggered
796 * by a wrong query type
797 *
Christopher Faulet67957bd2017-09-27 11:00:59 +0200798 * + 1 because dns_answer_record_id starts at 0
799 * while number of answers is an integer and
800 * starts at 1.
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200801 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200802 if (i + 1 == dns_p->header.ancount) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200803 pool_free2(dns_answer_item_pool, dns_answer_record);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200804 return DNS_RESP_CNAME_ERROR;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200805 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200806
807 offset = 0;
808 len = dns_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset);
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200809 if (len == 0) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200810 pool_free2(dns_answer_item_pool, dns_answer_record);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200811 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200812 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200813
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200814 memcpy(dns_answer_record->target, tmpname, len);
815 dns_answer_record->target[len] = 0;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200816 previous_dname = dns_answer_record->target;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200817 break;
818
Olivier Houchard8da5f982017-08-04 18:35:36 +0200819
820 case DNS_RTYPE_SRV:
Christopher Faulet67957bd2017-09-27 11:00:59 +0200821 /* Answer must contain :
Olivier Houchard8da5f982017-08-04 18:35:36 +0200822 * - 2 bytes for the priority
823 * - 2 bytes for the weight
824 * - 2 bytes for the port
825 * - the target hostname
826 */
827 if (dns_answer_record->data_len <= 6) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200828 pool_free2(dns_answer_item_pool, dns_answer_record);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200829 return DNS_RESP_INVALID;
830 }
Willy Tarreaud5370e12017-09-19 14:59:52 +0200831 dns_answer_record->priority = read_n16(reader);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200832 reader += sizeof(uint16_t);
Willy Tarreaud5370e12017-09-19 14:59:52 +0200833 dns_answer_record->weight = read_n16(reader);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200834 reader += sizeof(uint16_t);
Willy Tarreaud5370e12017-09-19 14:59:52 +0200835 dns_answer_record->port = read_n16(reader);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200836 reader += sizeof(uint16_t);
837 offset = 0;
838 len = dns_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset);
839 if (len == 0) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200840 pool_free2(dns_answer_item_pool, dns_answer_record);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200841 return DNS_RESP_INVALID;
842 }
Olivier Houchard8da5f982017-08-04 18:35:36 +0200843 dns_answer_record->data_len = len;
844 memcpy(dns_answer_record->target, tmpname, len);
845 dns_answer_record->target[len] = 0;
846 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200847
Baptiste Assmann325137d2015-04-13 23:40:55 +0200848 case DNS_RTYPE_AAAA:
849 /* ipv6 is stored on 16 bytes */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200850 if (dns_answer_record->data_len != 16) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200851 pool_free2(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200852 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200853 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200854 dns_answer_record->address.sa_family = AF_INET6;
855 memcpy(&(((struct sockaddr_in6 *)&dns_answer_record->address)->sin6_addr),
856 reader, dns_answer_record->data_len);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200857 break;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200858
Baptiste Assmann325137d2015-04-13 23:40:55 +0200859 } /* switch (record type) */
860
Christopher Faulet67957bd2017-09-27 11:00:59 +0200861 /* Increment the counter for number of records saved into our
862 * local response */
863 nb_saved_records++;
Baptiste Assmann69fce672017-05-04 08:37:45 +0200864
Christopher Faulet67957bd2017-09-27 11:00:59 +0200865 /* Move forward dns_answer_record->data_len for analyzing next
866 * record in the response */
867 reader += ((dns_answer_record->type == DNS_RTYPE_SRV)
868 ? offset
869 : dns_answer_record->data_len);
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200870
871 /* Lookup to see if we already had this entry */
Olivier Houchard8da5f982017-08-04 18:35:36 +0200872 found = 0;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200873 list_for_each_entry(tmp_record, &dns_p->answer_list, list) {
874 if (tmp_record->type != dns_answer_record->type)
875 continue;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200876
877 switch(tmp_record->type) {
878 case DNS_RTYPE_A:
879 if (!memcmp(&((struct sockaddr_in *)&dns_answer_record->address)->sin_addr,
880 &((struct sockaddr_in *)&tmp_record->address)->sin_addr,
881 sizeof(in_addr_t)))
882 found = 1;
883 break;
884
885 case DNS_RTYPE_AAAA:
886 if (!memcmp(&((struct sockaddr_in6 *)&dns_answer_record->address)->sin6_addr,
887 &((struct sockaddr_in6 *)&tmp_record->address)->sin6_addr,
888 sizeof(struct in6_addr)))
889 found = 1;
890 break;
891
Olivier Houchard8da5f982017-08-04 18:35:36 +0200892 case DNS_RTYPE_SRV:
893 if (dns_answer_record->data_len == tmp_record->data_len &&
Christopher Faulet67957bd2017-09-27 11:00:59 +0200894 !memcmp(dns_answer_record->target, tmp_record->target, dns_answer_record->data_len) &&
Olivier Houchard8da5f982017-08-04 18:35:36 +0200895 dns_answer_record->port == tmp_record->port) {
896 tmp_record->weight = dns_answer_record->weight;
897 found = 1;
898 }
899 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200900
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200901 default:
902 break;
903 }
Christopher Faulet67957bd2017-09-27 11:00:59 +0200904
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200905 if (found == 1)
906 break;
907 }
Christopher Faulet67957bd2017-09-27 11:00:59 +0200908
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200909 if (found == 1) {
910 tmp_record->last_seen = now.tv_sec;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200911 pool_free2(dns_answer_item_pool, dns_answer_record);
912 }
913 else {
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200914 dns_answer_record->last_seen = now.tv_sec;
915 LIST_ADDQ(&dns_p->answer_list, &dns_answer_record->list);
916 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200917 } /* for i 0 to ancount */
918
Christopher Faulet67957bd2017-09-27 11:00:59 +0200919 /* Save the number of records we really own */
Baptiste Assmann69fce672017-05-04 08:37:45 +0200920 dns_p->header.ancount = nb_saved_records;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200921 dns_check_dns_response(resolution);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200922 return DNS_RESP_VALID;
923}
924
Christopher Faulet67957bd2017-09-27 11:00:59 +0200925/* Searches dn_name resolution in resp.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200926 * If existing IP not found, return the first IP matching family_priority,
927 * otherwise, first ip found
928 * The following tasks are the responsibility of the caller:
Baptiste Assmann3cf7f982016-04-17 22:43:26 +0200929 * - <dns_p> contains an error free DNS response
Baptiste Assmann325137d2015-04-13 23:40:55 +0200930 * For both cases above, dns_validate_dns_response is required
931 * returns one of the DNS_UPD_* code
932 */
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100933#define DNS_MAX_IP_REC 20
Baptiste Assmannfb7091e2017-05-03 15:43:12 +0200934int dns_get_ip_from_response(struct dns_response_packet *dns_p,
Baptiste Assmann42746372017-05-03 12:12:02 +0200935 struct dns_options *dns_opts, void *currentip,
Thierry Fournierada34842016-02-17 21:25:09 +0100936 short currentip_sin_family,
Baptiste Assmannfb7091e2017-05-03 15:43:12 +0200937 void **newip, short *newip_sin_family,
938 void *owner)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200939{
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200940 struct dns_answer_item *record;
Thierry Fournierada34842016-02-17 21:25:09 +0100941 int family_priority;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200942 int currentip_found;
Baptiste Assmann3cf7f982016-04-17 22:43:26 +0200943 unsigned char *newip4, *newip6;
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100944 int currentip_sel;
945 int j;
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100946 int score, max_score;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200947
Christopher Faulet67957bd2017-09-27 11:00:59 +0200948 family_priority = dns_opts->family_prio;
949 *newip = newip4 = newip6 = NULL;
950 currentip_found = 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200951 *newip_sin_family = AF_UNSPEC;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200952 max_score = -1;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200953
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100954 /* Select an IP regarding configuration preference.
955 * Top priority is the prefered network ip version,
956 * second priority is the prefered network.
957 * the last priority is the currently used IP,
958 *
959 * For these three priorities, a score is calculated. The
960 * weight are:
Baptistefc725902016-12-26 23:21:08 +0100961 * 8 - prefered netwok ip version.
962 * 4 - prefered network.
963 * 2 - if the ip in the record is not affected to any other server in the same backend (duplication)
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100964 * 1 - current ip.
965 * The result with the biggest score is returned.
966 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200967
968 list_for_each_entry(record, &dns_p->answer_list, list) {
969 void *ip;
970 unsigned char ip_type;
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100971
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200972 if (record->type == DNS_RTYPE_A) {
973 ip = &(((struct sockaddr_in *)&record->address)->sin_addr);
974 ip_type = AF_INET;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200975 }
976 else if (record->type == DNS_RTYPE_AAAA) {
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200977 ip_type = AF_INET6;
978 ip = &(((struct sockaddr_in6 *)&record->address)->sin6_addr);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200979 }
980 else
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200981 continue;
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100982 score = 0;
983
984 /* Check for prefered ip protocol. */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200985 if (ip_type == family_priority)
Baptistefc725902016-12-26 23:21:08 +0100986 score += 8;
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100987
988 /* Check for prefered network. */
Baptiste Assmann42746372017-05-03 12:12:02 +0200989 for (j = 0; j < dns_opts->pref_net_nb; j++) {
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100990
991 /* Compare only the same adresses class. */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200992 if (dns_opts->pref_net[j].family != ip_type)
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100993 continue;
994
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200995 if ((ip_type == AF_INET &&
996 in_net_ipv4(ip,
Baptiste Assmann42746372017-05-03 12:12:02 +0200997 &dns_opts->pref_net[j].mask.in4,
998 &dns_opts->pref_net[j].addr.in4)) ||
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200999 (ip_type == AF_INET6 &&
1000 in_net_ipv6(ip,
Baptiste Assmann42746372017-05-03 12:12:02 +02001001 &dns_opts->pref_net[j].mask.in6,
1002 &dns_opts->pref_net[j].addr.in6))) {
Baptistefc725902016-12-26 23:21:08 +01001003 score += 4;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001004 break;
1005 }
1006 }
1007
Christopher Faulet67957bd2017-09-27 11:00:59 +02001008 /* Check if the IP found in the record is already affected to a
1009 * member of a group. If yes, the score should be incremented
1010 * by 2. */
1011 if (owner && snr_check_ip_callback(owner, ip, &ip_type))
1012 continue;
1013
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001014 /* Check for current ip matching. */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001015 if (ip_type == currentip_sin_family &&
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001016 ((currentip_sin_family == AF_INET &&
Christopher Faulet67957bd2017-09-27 11:00:59 +02001017 !memcmp(ip, currentip, 4)) ||
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001018 (currentip_sin_family == AF_INET6 &&
Christopher Faulet67957bd2017-09-27 11:00:59 +02001019 !memcmp(ip, currentip, 16)))) {
1020 score++;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001021 currentip_sel = 1;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001022 }
1023 else
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001024 currentip_sel = 0;
1025
1026 /* Keep the address if the score is better than the previous
Christopher Faulet67957bd2017-09-27 11:00:59 +02001027 * score. The maximum score is 15, if this value is reached, we
1028 * break the parsing. Implicitly, this score is reached the ip
1029 * selected is the current ip. */
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001030 if (score > max_score) {
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001031 if (ip_type == AF_INET)
1032 newip4 = ip;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001033 else
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001034 newip6 = ip;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001035 currentip_found = currentip_sel;
Baptistefc725902016-12-26 23:21:08 +01001036 if (score == 15)
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001037 return DNS_UPD_NO;
1038 max_score = score;
1039 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001040 } /* list for each record entries */
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001041
Christopher Faulet67957bd2017-09-27 11:00:59 +02001042 /* No IP found in the response */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001043 if (!newip4 && !newip6)
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02001044 return DNS_UPD_NO_IP_FOUND;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02001045
Christopher Faulet67957bd2017-09-27 11:00:59 +02001046 /* Case when the caller looks first for an IPv4 address */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001047 if (family_priority == AF_INET) {
1048 if (newip4) {
1049 *newip = newip4;
1050 *newip_sin_family = AF_INET;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001051 }
1052 else if (newip6) {
1053 *newip = newip6;
1054 *newip_sin_family = AF_INET6;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001055 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001056 if (!currentip_found)
1057 goto not_found;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001058 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001059 /* Case when the caller looks first for an IPv6 address */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001060 else if (family_priority == AF_INET6) {
1061 if (newip6) {
1062 *newip = newip6;
1063 *newip_sin_family = AF_INET6;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001064 }
1065 else if (newip4) {
1066 *newip = newip4;
1067 *newip_sin_family = AF_INET;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001068 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001069 if (!currentip_found)
1070 goto not_found;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001071 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001072 /* Case when the caller have no preference (we prefer IPv6) */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001073 else if (family_priority == AF_UNSPEC) {
1074 if (newip6) {
1075 *newip = newip6;
1076 *newip_sin_family = AF_INET6;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001077 }
1078 else if (newip4) {
1079 *newip = newip4;
1080 *newip_sin_family = AF_INET;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001081 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001082 if (!currentip_found)
1083 goto not_found;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001084 }
1085
Christopher Faulet67957bd2017-09-27 11:00:59 +02001086 /* No reason why we should change the server's IP address */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001087 return DNS_UPD_NO;
Baptiste Assmann8ea0bcc2017-05-04 08:24:11 +02001088
Christopher Faulet67957bd2017-09-27 11:00:59 +02001089 not_found:
Baptiste Assmann8ea0bcc2017-05-04 08:24:11 +02001090 list_for_each_entry(record, &dns_p->answer_list, list) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02001091 /* Move the first record to the end of the list, for internal
1092 * round robin */
1093 LIST_DEL(&record->list);
1094 LIST_ADDQ(&dns_p->answer_list, &record->list);
1095 break;
Baptiste Assmann8ea0bcc2017-05-04 08:24:11 +02001096 }
1097 return DNS_UPD_SRVIP_NOT_FOUND;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001098}
1099
Christopher Faulet67957bd2017-09-27 11:00:59 +02001100/* Turns a domain name label into a string.
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001101 *
Christopher Faulet67957bd2017-09-27 11:00:59 +02001102 * <dn> must be a null-terminated string. <dn_len> must include the terminating
1103 * null byte. <str> must be allocated and its size must be passed in <str_len>.
Baptiste Assmann325137d2015-04-13 23:40:55 +02001104 *
Christopher Faulet67957bd2017-09-27 11:00:59 +02001105 * In case of error, -1 is returned, otherwise, the number of bytes copied in
1106 * <str> (including the terminating null byte).
Baptiste Assmann325137d2015-04-13 23:40:55 +02001107 */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001108int dns_dn_label_to_str(const char *dn, int dn_len, char *str, int str_len)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001109{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001110 char *ptr;
1111 int i, sz;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001112
Christopher Faulet67957bd2017-09-27 11:00:59 +02001113 if (str_len < dn_len - 1)
Baptiste Assmann2af08fe2017-08-14 00:13:01 +02001114 return -1;
1115
Christopher Faulet67957bd2017-09-27 11:00:59 +02001116 ptr = str;
1117 for (i = 0; i < dn_len-1; ++i) {
1118 sz = dn[i];
1119 if (i)
1120 *ptr++ = '.';
1121 memcpy(ptr, dn+i+1, sz);
1122 ptr += sz;
1123 i += sz;
Olivier Houchard8da5f982017-08-04 18:35:36 +02001124 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001125 *ptr++ = '\0';
1126 return (ptr - str);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001127}
1128
Christopher Faulet67957bd2017-09-27 11:00:59 +02001129/* Turns a string into domain name label: www.haproxy.org into 3www7haproxy3org
1130 *
1131 * <str> must be a null-terminated string. <str_len> must include the
1132 * terminating null byte. <dn> buffer must be allocated and its size must be
1133 * passed in <dn_len>.
1134 *
1135 * In case of error, -1 is returned, otherwise, the number of bytes copied in
1136 * <dn> (excluding the terminating null byte).
Baptiste Assmann325137d2015-04-13 23:40:55 +02001137 */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001138int dns_str_to_dn_label(const char *str, int str_len, char *dn, int dn_len)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001139{
Baptiste Assmann325137d2015-04-13 23:40:55 +02001140 int i, offset;
1141
Christopher Faulet67957bd2017-09-27 11:00:59 +02001142 if (dn_len < str_len + 1)
1143 return -1;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001144
Christopher Faulet67957bd2017-09-27 11:00:59 +02001145 /* First byte of dn will be used to store the length of the first
1146 * label */
1147 offset = 0;
1148 for (i = 0; i < str_len; ++i) {
1149 if (str[i] == '.') {
1150 /* 2 or more consecutive dots is invalid */
1151 if (i == offset)
1152 return -1;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001153
Christopher Faulet67957bd2017-09-27 11:00:59 +02001154 dn[offset] = (i - offset);
1155 offset = i+1;
1156 continue;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001157 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001158 dn[i+1] = str[i];
Baptiste Assmann325137d2015-04-13 23:40:55 +02001159 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001160 dn[offset] = (i - offset - 1);
1161 dn[i] = '\0';
1162 return i;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001163}
1164
Christopher Faulet67957bd2017-09-27 11:00:59 +02001165/* Validates host name:
Baptiste Assmann325137d2015-04-13 23:40:55 +02001166 * - total size
1167 * - each label size individually
1168 * returns:
1169 * 0 in case of error. If <err> is not NULL, an error message is stored there.
1170 * 1 when no error. <err> is left unaffected.
1171 */
1172int dns_hostname_validation(const char *string, char **err)
1173{
1174 const char *c, *d;
1175 int i;
1176
1177 if (strlen(string) > DNS_MAX_NAME_SIZE) {
1178 if (err)
1179 *err = DNS_TOO_LONG_FQDN;
1180 return 0;
1181 }
1182
1183 c = string;
1184 while (*c) {
1185 d = c;
1186
1187 i = 0;
1188 while (*d != '.' && *d && i <= DNS_MAX_LABEL_SIZE) {
1189 i++;
1190 if (!((*d == '-') || (*d == '_') ||
1191 ((*d >= 'a') && (*d <= 'z')) ||
1192 ((*d >= 'A') && (*d <= 'Z')) ||
1193 ((*d >= '0') && (*d <= '9')))) {
1194 if (err)
1195 *err = DNS_INVALID_CHARACTER;
1196 return 0;
1197 }
1198 d++;
1199 }
1200
1201 if ((i >= DNS_MAX_LABEL_SIZE) && (d[i] != '.')) {
1202 if (err)
1203 *err = DNS_LABEL_TOO_LONG;
1204 return 0;
1205 }
1206
1207 if (*d == '\0')
1208 goto out;
1209
1210 c = ++d;
1211 }
1212 out:
1213 return 1;
1214}
1215
Christopher Faulet67957bd2017-09-27 11:00:59 +02001216/* Picks up an available resolution from the different resolution list
1217 * associated to a resolvers section, in this order:
1218 * 1. check in resolutions.curr for the same hostname and query_type
1219 * 2. check in resolutions.wait for the same hostname and query_type
1220 * 3. Get a new resolution from resolution pool
1221 *
1222 * Returns an available resolution, NULL if none found.
Baptiste Assmann325137d2015-04-13 23:40:55 +02001223 */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001224static struct dns_resolution *dns_pick_resolution(struct dns_resolvers *resolvers,
1225 char **hostname_dn, int hostname_dn_len,
1226 int query_type)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001227{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001228 struct dns_resolution *res;
1229
1230 if (!*hostname_dn)
1231 goto from_pool;
1232
1233 /* Search for same hostname and query type in resolutions.curr */
1234 list_for_each_entry(res, &resolvers->resolutions.curr, list) {
1235 if (!res->hostname_dn)
1236 continue;
1237 if ((query_type == res->prefered_query_type) &&
1238 hostname_dn_len == res->hostname_dn_len &&
1239 !memcmp(*hostname_dn, res->hostname_dn, hostname_dn_len))
1240 return res;
1241 }
1242
1243 /* Search for same hostname and query type in resolutions.wait */
1244 list_for_each_entry(res, &resolvers->resolutions.wait, list) {
1245 if (!res->hostname_dn)
1246 continue;
1247 if ((query_type == res->prefered_query_type) &&
1248 hostname_dn_len == res->hostname_dn_len &&
1249 !memcmp(*hostname_dn, res->hostname_dn, hostname_dn_len))
1250 return res;
1251 }
1252
1253 from_pool:
1254 /* No resolution could be found, so let's allocate a new one */
1255 res = pool_alloc2(dns_resolution_pool);
1256 if (res) {
1257 memset(res, 0, sizeof(*res));
1258 res->resolvers = resolvers;
1259 res->uuid = resolution_uuid;
1260 res->status = RSLV_STATUS_NONE;
1261 res->step = RSLV_STEP_NONE;
1262 res->last_valid = now_ms;
1263
1264 LIST_INIT(&res->requesters);
1265 LIST_INIT(&res->response.answer_list);
1266
1267 res->prefered_query_type = query_type;
1268 res->query_type = query_type;
1269 res->hostname_dn = *hostname_dn;
1270 res->hostname_dn_len = hostname_dn_len;
1271
1272 ++resolution_uuid;
1273
1274 /* Move the resolution to the resolvers wait queue */
1275 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
1276 }
1277 return res;
1278}
1279
1280/* Releases a resolution from its requester(s) and move it back to the pool */
1281static void dns_free_resolution(struct dns_resolution *resolution)
1282{
1283 struct dns_requester *req, *reqback;
1284
1285 /* clean up configuration */
1286 dns_reset_resolution(resolution);
1287 resolution->hostname_dn = NULL;
1288 resolution->hostname_dn_len = 0;
1289
1290 list_for_each_entry_safe(req, reqback, &resolution->requesters, list) {
1291 LIST_DEL(&req->list);
1292 req->resolution = NULL;
1293 }
1294
1295 LIST_DEL(&resolution->list);
1296 pool_free2(dns_resolution_pool, resolution);
1297}
1298
1299/* Links a requester (a server or a dns_srvrq) with a resolution. It returns 0
1300 * on success, -1 otherwise.
1301 */
1302int dns_link_resolution(void *requester, int requester_type)
1303{
1304 struct dns_resolution *res = NULL;
1305 struct dns_requester *req;
1306 struct dns_resolvers *resolvers;
1307 struct server *srv = NULL;
1308 struct dns_srvrq *srvrq = NULL;
1309 char **hostname_dn;
1310 int hostname_dn_len, query_type;
1311
1312 switch (requester_type) {
1313 case OBJ_TYPE_SERVER:
1314 srv = (struct server *)requester;
1315 hostname_dn = &srv->hostname_dn;
1316 hostname_dn_len = srv->hostname_dn_len;
1317 resolvers = srv->resolvers;
1318 query_type = ((srv->dns_opts.family_prio == AF_INET)
1319 ? DNS_RTYPE_A
1320 : DNS_RTYPE_AAAA);
1321 break;
1322
1323 case OBJ_TYPE_SRVRQ:
1324 srvrq = (struct dns_srvrq *)requester;
1325 hostname_dn = &srvrq->hostname_dn;
1326 hostname_dn_len = srvrq->hostname_dn_len;
1327 resolvers = srvrq->resolvers;
1328 query_type = DNS_RTYPE_SRV;
1329 break;
1330
1331 default:
1332 goto err;
1333 }
1334
1335 /* Get a resolution from the resolvers' wait queue or pool */
1336 if ((res = dns_pick_resolution(resolvers, hostname_dn, hostname_dn_len, query_type)) == NULL)
1337 goto err;
1338
1339 if (srv) {
1340 if (srv->dns_requester == NULL) {
1341 if ((req = calloc(1, sizeof(*req))) == NULL)
1342 goto err;
1343 req->owner = &srv->obj_type;
1344 srv->dns_requester = req;
1345 }
1346 else
1347 req = srv->dns_requester;
1348 }
1349 else if (srvrq) {
1350 if (srvrq->dns_requester == NULL) {
1351 if ((req = calloc(1, sizeof(*req))) == NULL)
1352 goto err;
1353 req->owner = &srvrq->obj_type;
1354 srvrq->dns_requester = req;
1355 }
1356 else
1357 req = srvrq->dns_requester;
1358 }
1359 else
1360 goto err;
1361
1362 req->resolution = res;
1363 req->requester_cb = snr_resolution_cb;
1364 req->requester_error_cb = snr_resolution_error_cb;
1365
1366 LIST_ADDQ(&res->requesters, &req->list);
1367 return 0;
1368
1369 err:
1370 if (res && LIST_ISEMPTY(&res->requesters))
1371 dns_free_resolution(res);
1372 return -1;
1373}
1374
1375/* Removes a requester from a DNS resoltion. It takes takes care of all the
1376 * consequences. It also cleans up some parameters from the requester.
1377 */
1378void dns_unlink_resolution(struct dns_requester *requester)
1379{
1380 struct dns_resolution *res;
1381 struct dns_requester *req;
1382
1383 /* Nothing to do */
1384 if (!requester || !requester->resolution)
1385 return;
1386 res = requester->resolution;
1387
1388 /* Clean up the requester */
1389 LIST_DEL(&requester->list);
1390 requester->resolution = NULL;
1391
1392 /* We need to find another requester linked on this resolution */
1393 if (!LIST_ISEMPTY(&res->requesters))
1394 req = LIST_NEXT(&res->requesters, struct dns_requester *, list);
1395 else {
1396 dns_free_resolution(res);
1397 return;
1398 }
1399
1400 /* Move hostname_dn related pointers to the next requester */
1401 switch (obj_type(req->owner)) {
1402 case OBJ_TYPE_SERVER:
1403 res->hostname_dn = objt_server(req->owner)->hostname_dn;
1404 res->hostname_dn_len = objt_server(req->owner)->hostname_dn_len;
1405 break;
1406 case OBJ_TYPE_SRVRQ:
1407 res->hostname_dn = objt_dns_srvrq(req->owner)->hostname_dn;
1408 res->hostname_dn_len = objt_dns_srvrq(req->owner)->hostname_dn_len;
1409 break;
1410 default:
1411 res->hostname_dn = NULL;
1412 res->hostname_dn_len = 0;
1413 break;
1414 }
Baptiste Assmann325137d2015-04-13 23:40:55 +02001415}
1416
Christopher Faulet67957bd2017-09-27 11:00:59 +02001417/* Called when a network IO is generated on a name server socket for an incoming
1418 * packet. It performs the following actions:
1419 * - check if the packet requires processing (not outdated resolution)
1420 * - ensure the DNS packet received is valid and call requester's callback
1421 * - call requester's error callback if invalid response
1422 * - check the dn_name in the packet against the one sent
1423 */
1424static void dns_resolve_recv(struct dgram_conn *dgram)
1425{
1426 struct dns_nameserver *ns, *tmpns;
1427 struct dns_resolvers *resolvers;
1428 struct dns_resolution *res;
1429 struct dns_query_item *query;
1430 unsigned char buf[DNS_MAX_UDP_MESSAGE + 1];
1431 unsigned char *bufend;
1432 int fd, buflen, dns_resp;
1433 int max_answer_records;
1434 unsigned short query_id;
1435 struct eb32_node *eb;
1436 struct dns_requester *req;
1437
1438 fd = dgram->t.sock.fd;
1439
1440 /* check if ready for reading */
1441 if (!fd_recv_ready(fd))
1442 return;
1443
1444 /* no need to go further if we can't retrieve the nameserver */
1445 if ((ns = dgram->owner) == NULL)
1446 return;
1447
1448 resolvers = ns->resolvers;
Christopher Fauletb2812a62017-10-04 16:17:58 +02001449 SPIN_LOCK(DNS_LOCK, &resolvers->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001450
1451 /* process all pending input messages */
1452 while (1) {
1453 /* read message received */
1454 memset(buf, '\0', resolvers->accepted_payload_size + 1);
1455 if ((buflen = recv(fd, (char*)buf , resolvers->accepted_payload_size + 1, 0)) < 0) {
1456 /* FIXME : for now we consider EAGAIN only */
1457 fd_cant_recv(fd);
1458 break;
1459 }
1460
1461 /* message too big */
1462 if (buflen > resolvers->accepted_payload_size) {
1463 ns->counters.too_big++;
1464 continue;
1465 }
1466
1467 /* initializing variables */
1468 bufend = buf + buflen; /* pointer to mark the end of the buffer */
1469
1470 /* read the query id from the packet (16 bits) */
1471 if (buf + 2 > bufend) {
1472 ns->counters.invalid++;
1473 continue;
1474 }
1475 query_id = dns_response_get_query_id(buf);
1476
1477 /* search the query_id in the pending resolution tree */
1478 eb = eb32_lookup(&resolvers->query_ids, query_id);
1479 if (eb == NULL) {
1480 /* unknown query id means an outdated response and can be safely ignored */
1481 ns->counters.outdated++;
1482 continue;
1483 }
1484
1485 /* known query id means a resolution in prgress */
1486 res = eb32_entry(eb, struct dns_resolution, qid);
1487 if (!res) {
1488 ns->counters.outdated++;
1489 continue;
1490 }
Baptiste Assmann325137d2015-04-13 23:40:55 +02001491
Christopher Faulet67957bd2017-09-27 11:00:59 +02001492 /* number of responses received */
1493 res->nb_responses++;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001494
Christopher Faulet67957bd2017-09-27 11:00:59 +02001495 max_answer_records = (resolvers->accepted_payload_size - DNS_HEADER_SIZE) / DNS_MIN_RECORD_SIZE;
1496 dns_resp = dns_validate_dns_response(buf, bufend, res, max_answer_records);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001497
Christopher Faulet67957bd2017-09-27 11:00:59 +02001498 switch (dns_resp) {
1499 case DNS_RESP_VALID:
1500 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001501
Christopher Faulet67957bd2017-09-27 11:00:59 +02001502 case DNS_RESP_INVALID:
1503 case DNS_RESP_QUERY_COUNT_ERROR:
1504 case DNS_RESP_WRONG_NAME:
1505 res->status = RSLV_STATUS_INVALID;
1506 ns->counters.invalid++;
1507 break;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001508
Christopher Faulet67957bd2017-09-27 11:00:59 +02001509 case DNS_RESP_NX_DOMAIN:
1510 res->status = RSLV_STATUS_NX;
1511 ns->counters.nx++;
1512 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001513
Christopher Faulet67957bd2017-09-27 11:00:59 +02001514 case DNS_RESP_REFUSED:
1515 res->status = RSLV_STATUS_REFUSED;
1516 ns->counters.refused++;
1517 break;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001518
Christopher Faulet67957bd2017-09-27 11:00:59 +02001519 case DNS_RESP_ANCOUNT_ZERO:
1520 res->status = RSLV_STATUS_OTHER;
1521 ns->counters.any_err++;
1522 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001523
Christopher Faulet67957bd2017-09-27 11:00:59 +02001524 case DNS_RESP_CNAME_ERROR:
1525 res->status = RSLV_STATUS_OTHER;
1526 ns->counters.cname_error++;
1527 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001528
Christopher Faulet67957bd2017-09-27 11:00:59 +02001529 case DNS_RESP_TRUNCATED:
1530 res->status = RSLV_STATUS_OTHER;
1531 ns->counters.truncated++;
1532 break;
Baptiste Assmanne70bc052017-08-21 16:51:09 +02001533
Christopher Faulet67957bd2017-09-27 11:00:59 +02001534 case DNS_RESP_NO_EXPECTED_RECORD:
1535 case DNS_RESP_ERROR:
1536 case DNS_RESP_INTERNAL:
1537 res->status = RSLV_STATUS_OTHER;
1538 ns->counters.other++;
1539 break;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001540 }
1541
Christopher Faulet67957bd2017-09-27 11:00:59 +02001542 /* Wait all nameservers response to handle errors */
1543 if (dns_resp != DNS_RESP_VALID && res->nb_responses < resolvers->nb_nameservers)
1544 continue;
Olivier Houchard8da5f982017-08-04 18:35:36 +02001545
Christopher Faulet67957bd2017-09-27 11:00:59 +02001546 /* Process error codes */
1547 if (dns_resp != DNS_RESP_VALID) {
1548 if (res->prefered_query_type != res->query_type) {
1549 /* The fallback on the query type was already performed,
1550 * so check the try counter. If it falls to 0, we can
1551 * report an error. Else, wait the next attempt. */
1552 if (!res->try)
1553 goto report_res_error;
1554 }
1555 else {
1556 /* Fallback from A to AAAA or the opposite and re-send
1557 * the resolution immediately. try counter is not
1558 * decremented. */
1559 if (res->prefered_query_type == DNS_RTYPE_A) {
1560 res->query_type = DNS_RTYPE_AAAA;
1561 dns_send_query(res);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001562 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001563 else if (res->prefered_query_type == DNS_RTYPE_AAAA) {
1564 res->query_type = DNS_RTYPE_A;
1565 dns_send_query(res);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001566 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001567 }
1568 continue;
1569 }
Olivier Houchard8da5f982017-08-04 18:35:36 +02001570
Christopher Faulet67957bd2017-09-27 11:00:59 +02001571 /* Now let's check the query's dname corresponds to the one we
1572 * sent. We can check only the first query of the list. We send
1573 * one query at a time so we get one query in the response */
1574 query = LIST_NEXT(&res->response.query_list, struct dns_query_item *, list);
1575 if (query && memcmp(query->name, res->hostname_dn, res->hostname_dn_len) != 0) {
1576 dns_resp = DNS_RESP_WRONG_NAME;
1577 ns->counters.other++;
1578 goto report_res_error;
1579 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001580
Christopher Faulet67957bd2017-09-27 11:00:59 +02001581 /* So the resolution succeeded */
1582 res->status = RSLV_STATUS_VALID;
1583 res->last_valid = now_ms;
1584 ns->counters.valid++;
1585 goto report_res_success;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001586
Christopher Faulet67957bd2017-09-27 11:00:59 +02001587 report_res_error:
1588 list_for_each_entry(req, &res->requesters, list)
1589 req->requester_error_cb(req, dns_resp);
1590 dns_reset_resolution(res);
1591 LIST_DEL(&res->list);
1592 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
1593 continue;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001594
Christopher Faulet67957bd2017-09-27 11:00:59 +02001595 report_res_success:
1596 /* Only the 1rst requester s managed by the server, others are
1597 * from the cache */
1598 tmpns = ns;
1599 list_for_each_entry(req, &res->requesters, list) {
1600 req->requester_cb(req, tmpns);
1601 tmpns = NULL;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001602 }
Baptiste Assmann42746372017-05-03 12:12:02 +02001603
Christopher Faulet67957bd2017-09-27 11:00:59 +02001604 dns_reset_resolution(res);
1605 LIST_DEL(&res->list);
1606 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
1607 continue;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001608 }
Baptiste Assmann325137d2015-04-13 23:40:55 +02001609 dns_update_resolvers_timeout(resolvers);
Christopher Fauletb2812a62017-10-04 16:17:58 +02001610 SPIN_UNLOCK(DNS_LOCK, &resolvers->lock);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001611}
William Lallemand69e96442016-11-19 00:58:54 +01001612
Christopher Faulet67957bd2017-09-27 11:00:59 +02001613/* Called when a resolvers network socket is ready to send data */
1614static void dns_resolve_send(struct dgram_conn *dgram)
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001615{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001616 struct dns_resolvers *resolvers;
1617 struct dns_nameserver *ns;
1618 struct dns_resolution *res;
1619 int fd;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001620
Christopher Faulet67957bd2017-09-27 11:00:59 +02001621 fd = dgram->t.sock.fd;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001622
Christopher Faulet67957bd2017-09-27 11:00:59 +02001623 /* check if ready for sending */
1624 if (!fd_send_ready(fd))
1625 return;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001626
Christopher Faulet67957bd2017-09-27 11:00:59 +02001627 /* we don't want/need to be waked up any more for sending */
1628 fd_stop_send(fd);
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001629
Christopher Faulet67957bd2017-09-27 11:00:59 +02001630 /* no need to go further if we can't retrieve the nameserver */
1631 if ((ns = dgram->owner) == NULL)
1632 return;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001633
Christopher Faulet67957bd2017-09-27 11:00:59 +02001634 resolvers = ns->resolvers;
Christopher Fauletb2812a62017-10-04 16:17:58 +02001635 SPIN_LOCK(DNS_LOCK, &resolvers->lock);
1636
Christopher Faulet67957bd2017-09-27 11:00:59 +02001637 list_for_each_entry(res, &resolvers->resolutions.curr, list) {
1638 int ret;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001639
Christopher Faulet67957bd2017-09-27 11:00:59 +02001640 if (res->nb_queries == resolvers->nb_nameservers)
1641 continue;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001642
Christopher Faulet67957bd2017-09-27 11:00:59 +02001643 trash.len = dns_build_query(res->query_id, res->query_type,
1644 resolvers->accepted_payload_size,
1645 res->hostname_dn, res->hostname_dn_len,
1646 trash.str, trash.size);
1647 if (trash.len == -1)
1648 goto snd_error;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001649
Christopher Faulet67957bd2017-09-27 11:00:59 +02001650 ret = send(fd, trash.str, trash.len, 0);
1651 if (ret != trash.len)
1652 goto snd_error;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001653
Christopher Faulet67957bd2017-09-27 11:00:59 +02001654 ns->counters.sent++;
1655 res->nb_queries++;
1656 continue;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001657
Christopher Faulet67957bd2017-09-27 11:00:59 +02001658 snd_error:
1659 ns->counters.snd_error++;
1660 res->nb_queries++;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001661 }
Christopher Fauletb2812a62017-10-04 16:17:58 +02001662 SPIN_UNLOCK(DNS_LOCK, &resolvers->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001663}
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001664
Christopher Faulet67957bd2017-09-27 11:00:59 +02001665/* Processes DNS resolution. First, it checks the active list to detect expired
1666 * resolutions and retry them if possible. Else a timeout is reported. Then, it
1667 * checks the wait list to trigger new resolutions.
1668 */
1669static struct task *dns_process_resolvers(struct task *t)
1670{
1671 struct dns_resolvers *resolvers = t->context;
1672 struct dns_resolution *res, *resback;
1673 int exp;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001674
Christopher Fauletb2812a62017-10-04 16:17:58 +02001675 SPIN_LOCK(DNS_LOCK, &resolvers->lock);
1676
Christopher Faulet67957bd2017-09-27 11:00:59 +02001677 /* Handle all expired resolutions from the active list */
1678 list_for_each_entry_safe(res, resback, &resolvers->resolutions.curr, list) {
1679 /* When we find the first resolution in the future, then we can
1680 * stop here */
1681 exp = tick_add(res->last_query, resolvers->timeout.retry);
1682 if (!tick_is_expired(exp, now_ms))
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001683 break;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001684
Christopher Faulet67957bd2017-09-27 11:00:59 +02001685 /* If current resolution has been tried too many times and
1686 * finishes in timeout we update its status and remove it from
1687 * the list */
1688 if (!res->try) {
1689 struct dns_requester *req;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001690
Christopher Faulet67957bd2017-09-27 11:00:59 +02001691 /* Notify the result to the requesters */
1692 if (!res->nb_responses)
1693 res->status = RSLV_STATUS_TIMEOUT;
1694 list_for_each_entry(req, &res->requesters, list)
1695 req->requester_error_cb(req, res->status);
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001696
Christopher Faulet67957bd2017-09-27 11:00:59 +02001697 /* Clean up resolution info and remove it from the
1698 * current list */
1699 dns_reset_resolution(res);
1700 LIST_DEL(&res->list);
1701 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
William Lallemand69e96442016-11-19 00:58:54 +01001702
Christopher Faulet67957bd2017-09-27 11:00:59 +02001703 /* This might be triggered by too big UDP packets
1704 * dropped somewhere on the network, so lowering the
1705 * accepted_payload_size announced */
1706 if (resolvers->accepted_payload_size > 1280)
1707 resolvers->accepted_payload_size = 1280;
William Lallemand69e96442016-11-19 00:58:54 +01001708 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001709 else {
1710 /* Otherwise resend the DNS query and requeue the resolution */
1711 if (!res->nb_responses || res->prefered_query_type != res->query_type) {
1712 /* No response received (a real timeout) or fallback already done */
1713 res->query_type = res->prefered_query_type;
1714 res->try--;
1715 }
1716 else {
1717 /* Fallback from A to AAAA or the opposite and re-send
1718 * the resolution immediately. try counter is not
1719 * decremented. */
1720 if (res->prefered_query_type == DNS_RTYPE_A)
1721 res->query_type = DNS_RTYPE_AAAA;
1722 else if (res->prefered_query_type == DNS_RTYPE_AAAA)
1723 res->query_type = DNS_RTYPE_A;
1724 else
1725 res->try--;
1726 }
1727 dns_send_query(res);
William Lallemand69e96442016-11-19 00:58:54 +01001728 }
1729 }
William Lallemand69e96442016-11-19 00:58:54 +01001730
Christopher Faulet67957bd2017-09-27 11:00:59 +02001731 /* Handle all resolutions in the wait list */
1732 list_for_each_entry_safe(res, resback, &resolvers->resolutions.wait, list) {
1733 exp = tick_add(res->last_resolution, dns_resolution_timeout(res));
1734 if (tick_isset(res->last_resolution) && !tick_is_expired(exp, now_ms))
1735 continue;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001736
Christopher Faulet67957bd2017-09-27 11:00:59 +02001737 if (dns_run_resolution(res) != 1) {
1738 res->last_resolution = now_ms;
1739 LIST_DEL(&res->list);
1740 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001741 }
1742 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001743
Christopher Faulet67957bd2017-09-27 11:00:59 +02001744 dns_update_resolvers_timeout(resolvers);
Christopher Fauletb2812a62017-10-04 16:17:58 +02001745 SPIN_UNLOCK(DNS_LOCK, &resolvers->lock);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001746 return t;
1747}
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001748
Christopher Faulet67957bd2017-09-27 11:00:59 +02001749/* proto_udp callback functions for a DNS resolution */
1750struct dgram_data_cb resolve_dgram_cb = {
1751 .recv = dns_resolve_recv,
1752 .send = dns_resolve_send,
1753};
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001754
Christopher Faulet67957bd2017-09-27 11:00:59 +02001755/* Release memory allocated by DNS */
1756static void dns_deinit(void)
1757{
1758 struct dns_resolvers *resolvers, *resolversback;
1759 struct dns_nameserver *ns, *nsback;
1760 struct dns_resolution *res, *resback;
1761 struct dns_requester *req, *reqback;
1762 struct dns_srvrq *srvrq, *srvrqback;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001763
Christopher Faulet67957bd2017-09-27 11:00:59 +02001764 list_for_each_entry_safe(resolvers, resolversback, &dns_resolvers, list) {
1765 list_for_each_entry_safe(ns, nsback, &resolvers->nameservers, list) {
1766 free(ns->id);
1767 free((char *)ns->conf.file);
1768 if (ns->dgram && ns->dgram->t.sock.fd != -1)
1769 fd_delete(ns->dgram->t.sock.fd);
1770 free(ns->dgram);
1771 LIST_DEL(&ns->list);
1772 free(ns);
1773 }
1774
1775 list_for_each_entry_safe(res, resback, &resolvers->resolutions.curr, list) {
1776 list_for_each_entry_safe(req, reqback, &res->requesters, list) {
1777 LIST_DEL(&req->list);
1778 free(req);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001779 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001780 dns_free_resolution(res);
1781 }
Olivier Houchard8da5f982017-08-04 18:35:36 +02001782
Christopher Faulet67957bd2017-09-27 11:00:59 +02001783 list_for_each_entry_safe(res, resback, &resolvers->resolutions.wait, list) {
1784 list_for_each_entry_safe(req, reqback, &res->requesters, list) {
1785 LIST_DEL(&req->list);
1786 free(req);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001787 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001788 dns_free_resolution(res);
1789 }
Olivier Houchard8da5f982017-08-04 18:35:36 +02001790
Christopher Faulet67957bd2017-09-27 11:00:59 +02001791 free(resolvers->id);
1792 free((char *)resolvers->conf.file);
1793 task_delete(resolvers->t);
1794 task_free(resolvers->t);
1795 LIST_DEL(&resolvers->list);
1796 free(resolvers);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001797 }
1798
Christopher Faulet67957bd2017-09-27 11:00:59 +02001799 list_for_each_entry_safe(srvrq, srvrqback, &dns_srvrq_list, list) {
1800 free(srvrq->name);
1801 free(srvrq->hostname_dn);
1802 LIST_DEL(&srvrq->list);
1803 free(srvrq);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001804 }
1805
Christopher Faulet67957bd2017-09-27 11:00:59 +02001806 pool_destroy2(dns_answer_item_pool);
1807 pool_destroy2(dns_resolution_pool);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001808}
1809
Christopher Faulet67957bd2017-09-27 11:00:59 +02001810/* Finalizes the DNS configuration by allocating required resources and checking
1811 * live parameters.
1812 * Returns 0 on success, ERR_* flags otherwise.
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001813 */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001814static int dns_finalize_config(void)
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001815{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001816 struct dns_resolvers *resolvers;
1817 struct proxy *px;
1818 int err_code = 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001819
Christopher Faulet67957bd2017-09-27 11:00:59 +02001820 /* allocate pool of resolution per resolvers */
1821 list_for_each_entry(resolvers, &dns_resolvers, list) {
1822 struct dns_nameserver *ns;
1823 struct task *t;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001824
Christopher Faulet67957bd2017-09-27 11:00:59 +02001825 /* Check if we can create the socket with nameservers info */
1826 list_for_each_entry(ns, &resolvers->nameservers, list) {
1827 struct dgram_conn *dgram = NULL;
1828 int fd;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001829
Christopher Faulet67957bd2017-09-27 11:00:59 +02001830 /* Check nameserver info */
1831 if ((fd = socket(ns->addr.ss_family, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
1832 Alert("config : resolvers '%s': can't create socket for nameserver '%s'.\n",
1833 resolvers->id, ns->id);
1834 err_code |= (ERR_ALERT|ERR_ABORT);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001835 continue;
Olivier Houchard8da5f982017-08-04 18:35:36 +02001836 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001837 if (connect(fd, (struct sockaddr*)&ns->addr, get_addr_len(&ns->addr)) == -1) {
1838 Alert("config : resolvers '%s': can't connect socket for nameserver '%s'.\n",
1839 resolvers->id, ns->id);
1840 close(fd);
1841 err_code |= (ERR_ALERT|ERR_ABORT);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001842 continue;
Olivier Houchard8da5f982017-08-04 18:35:36 +02001843 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001844 close(fd);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001845
Christopher Faulet67957bd2017-09-27 11:00:59 +02001846 /* Create dgram structure that will hold the UPD socket
1847 * and attach it on the current nameserver */
1848 if ((dgram = calloc(1, sizeof(*dgram))) == NULL) {
1849 Alert("config: resolvers '%s' : out of memory.\n",
1850 resolvers->id);
1851 err_code |= (ERR_ALERT|ERR_ABORT);
1852 goto err;
1853 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001854
Christopher Faulet67957bd2017-09-27 11:00:59 +02001855 /* Leave dgram partially initialized, no FD attached for
1856 * now. */
1857 dgram->owner = ns;
1858 dgram->data = &resolve_dgram_cb;
1859 dgram->t.sock.fd = -1;
1860 ns->dgram = dgram;
1861 }
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001862
Christopher Faulet67957bd2017-09-27 11:00:59 +02001863 /* Create the task associated to the resolvers section */
Emeric Brunc60def82017-09-27 14:59:38 +02001864 if ((t = task_new(MAX_THREADS_MASK)) == NULL) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02001865 Alert("config : resolvers '%s' : out of memory.\n", resolvers->id);
1866 err_code |= (ERR_ALERT|ERR_ABORT);
1867 goto err;
1868 }
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001869
Christopher Faulet67957bd2017-09-27 11:00:59 +02001870 /* Update task's parameters */
1871 t->process = dns_process_resolvers;
1872 t->context = resolvers;
1873 resolvers->t = t;
1874 task_wakeup(t, TASK_WOKEN_INIT);
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001875 }
1876
Christopher Faulet67957bd2017-09-27 11:00:59 +02001877 for (px = proxy; px; px = px->next) {
1878 struct server *srv;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001879
Christopher Faulet67957bd2017-09-27 11:00:59 +02001880 for (srv = px->srv; srv; srv = srv->next) {
1881 struct dns_resolvers *resolvers;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001882
Christopher Faulet67957bd2017-09-27 11:00:59 +02001883 if (!srv->resolvers_id)
1884 continue;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001885
Christopher Faulet67957bd2017-09-27 11:00:59 +02001886 if ((resolvers = find_resolvers_by_id(srv->resolvers_id)) == NULL) {
1887 Alert("config : %s '%s', server '%s': unable to find required resolvers '%s'\n",
1888 proxy_type_str(px), px->id, srv->id, srv->resolvers_id);
1889 err_code |= (ERR_ALERT|ERR_ABORT);
1890 continue;
1891 }
1892 srv->resolvers = resolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001893
Christopher Faulet67957bd2017-09-27 11:00:59 +02001894 if (srv->srvrq && !srv->srvrq->resolvers) {
1895 srv->srvrq->resolvers = srv->resolvers;
1896 if (dns_link_resolution(srv->srvrq, OBJ_TYPE_SRVRQ) == -1) {
1897 Alert("config : %s '%s' : unable to set DNS resolution for server '%s'.\n",
1898 proxy_type_str(px), px->id, srv->id);
1899 err_code |= (ERR_ALERT|ERR_ABORT);
1900 continue;
1901 }
1902 }
1903 if (dns_link_resolution(srv, OBJ_TYPE_SERVER) == -1) {
1904 Alert("config : %s '%s', unable to set DNS resolution for server '%s'.\n",
1905 proxy_type_str(px), px->id, srv->id);
1906 err_code |= (ERR_ALERT|ERR_ABORT);
1907 continue;
1908 }
1909 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001910 }
1911
Christopher Faulet67957bd2017-09-27 11:00:59 +02001912 if (err_code & (ERR_ALERT|ERR_ABORT))
1913 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001914
Christopher Faulet67957bd2017-09-27 11:00:59 +02001915 return err_code;
1916 err:
1917 dns_deinit();
1918 return err_code;
1919
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001920}
1921
Christopher Faulet67957bd2017-09-27 11:00:59 +02001922/* if an arg is found, it sets the resolvers section pointer into cli.p0 */
1923static int cli_parse_stat_resolvers(char **args, struct appctx *appctx, void *private)
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001924{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001925 struct dns_resolvers *presolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001926
Christopher Faulet67957bd2017-09-27 11:00:59 +02001927 if (*args[2]) {
1928 list_for_each_entry(presolvers, &dns_resolvers, list) {
1929 if (strcmp(presolvers->id, args[2]) == 0) {
1930 appctx->ctx.cli.p0 = presolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001931 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001932 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001933 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001934 if (appctx->ctx.cli.p0 == NULL) {
1935 appctx->ctx.cli.severity = LOG_ERR;
1936 appctx->ctx.cli.msg = "Can't find that resolvers section\n";
1937 appctx->st0 = CLI_ST_PRINT;
1938 return 1;
1939 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001940 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001941 return 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001942}
1943
Christopher Faulet67957bd2017-09-27 11:00:59 +02001944/* Dumps counters from all resolvers section and associated name servers. It
1945 * returns 0 if the output buffer is full and it needs to be called again,
1946 * otherwise non-zero. It may limit itself to the resolver pointed to by
Willy Tarreau777b5602016-12-16 18:06:26 +01001947 * <cli.p0> if it's not null.
William Lallemand69e96442016-11-19 00:58:54 +01001948 */
1949static int cli_io_handler_dump_resolvers_to_buffer(struct appctx *appctx)
1950{
1951 struct stream_interface *si = appctx->owner;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001952 struct dns_resolvers *resolvers;
1953 struct dns_nameserver *ns;
William Lallemand69e96442016-11-19 00:58:54 +01001954
1955 chunk_reset(&trash);
1956
1957 switch (appctx->st2) {
1958 case STAT_ST_INIT:
1959 appctx->st2 = STAT_ST_LIST; /* let's start producing data */
1960 /* fall through */
1961
1962 case STAT_ST_LIST:
1963 if (LIST_ISEMPTY(&dns_resolvers)) {
1964 chunk_appendf(&trash, "No resolvers found\n");
1965 }
1966 else {
Christopher Faulet67957bd2017-09-27 11:00:59 +02001967 list_for_each_entry(resolvers, &dns_resolvers, list) {
1968 if (appctx->ctx.cli.p0 != NULL && appctx->ctx.cli.p0 != resolvers)
William Lallemand69e96442016-11-19 00:58:54 +01001969 continue;
1970
Christopher Faulet67957bd2017-09-27 11:00:59 +02001971 chunk_appendf(&trash, "Resolvers section %s\n", resolvers->id);
1972 list_for_each_entry(ns, &resolvers->nameservers, list) {
1973 chunk_appendf(&trash, " nameserver %s:\n", ns->id);
1974 chunk_appendf(&trash, " sent: %lld\n", ns->counters.sent);
1975 chunk_appendf(&trash, " snd_error: %lld\n", ns->counters.snd_error);
1976 chunk_appendf(&trash, " valid: %lld\n", ns->counters.valid);
1977 chunk_appendf(&trash, " update: %lld\n", ns->counters.update);
1978 chunk_appendf(&trash, " cname: %lld\n", ns->counters.cname);
1979 chunk_appendf(&trash, " cname_error: %lld\n", ns->counters.cname_error);
1980 chunk_appendf(&trash, " any_err: %lld\n", ns->counters.any_err);
1981 chunk_appendf(&trash, " nx: %lld\n", ns->counters.nx);
1982 chunk_appendf(&trash, " timeout: %lld\n", ns->counters.timeout);
1983 chunk_appendf(&trash, " refused: %lld\n", ns->counters.refused);
1984 chunk_appendf(&trash, " other: %lld\n", ns->counters.other);
1985 chunk_appendf(&trash, " invalid: %lld\n", ns->counters.invalid);
1986 chunk_appendf(&trash, " too_big: %lld\n", ns->counters.too_big);
1987 chunk_appendf(&trash, " truncated: %lld\n", ns->counters.truncated);
1988 chunk_appendf(&trash, " outdated: %lld\n", ns->counters.outdated);
William Lallemand69e96442016-11-19 00:58:54 +01001989 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001990 chunk_appendf(&trash, "\n");
William Lallemand69e96442016-11-19 00:58:54 +01001991 }
1992 }
1993
1994 /* display response */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001995 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemand69e96442016-11-19 00:58:54 +01001996 /* let's try again later from this session. We add ourselves into
1997 * this session's users so that it can remove us upon termination.
1998 */
1999 si->flags |= SI_FL_WAIT_ROOM;
2000 return 0;
2001 }
2002
2003 appctx->st2 = STAT_ST_FIN;
2004 /* fall through */
2005
2006 default:
2007 appctx->st2 = STAT_ST_FIN;
2008 return 1;
2009 }
2010}
2011
2012/* register cli keywords */
Christopher Fauletff88efb2017-10-03 16:00:57 +02002013static struct cli_kw_list cli_kws = {{ }, {
2014 { { "show", "resolvers", NULL }, "show resolvers [id]: dumps counters from all resolvers section and\n"
2015 " associated name servers",
2016 cli_parse_stat_resolvers, cli_io_handler_dump_resolvers_to_buffer },
2017 {{},}
2018 }
2019};
William Lallemand69e96442016-11-19 00:58:54 +01002020
2021
2022__attribute__((constructor))
2023static void __dns_init(void)
2024{
Christopher Faulet67957bd2017-09-27 11:00:59 +02002025 dns_answer_item_pool = create_pool("dns_answer_item", sizeof(struct dns_answer_item), MEM_F_SHARED);
2026 dns_resolution_pool = create_pool("dns_resolution", sizeof(struct dns_resolution), MEM_F_SHARED);
2027
Christopher Faulet67957bd2017-09-27 11:00:59 +02002028 hap_register_post_check(dns_finalize_config);
2029 hap_register_post_deinit(dns_deinit);
2030
William Lallemand69e96442016-11-19 00:58:54 +01002031 cli_register_kw(&cli_kws);
2032}