blob: c2b87c015b82ce4d46342b034b53e9c566c9c086 [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 Faulet67957bd2017-09-27 11:00:59 +020048static int64_t dns_query_id_seed = 0; /* random seed */
49static 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{
128 dns_query_id_seed ^= dns_query_id_seed << 13;
129 dns_query_id_seed ^= dns_query_id_seed >> 7;
130 dns_query_id_seed ^= dns_query_id_seed << 17;
131 return dns_query_id_seed;
132}
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200133
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200134
Christopher Faulet67957bd2017-09-27 11:00:59 +0200135static inline int dns_resolution_timeout(struct dns_resolution *res)
136{
137 switch (res->status) {
138 case RSLV_STATUS_VALID: return res->resolvers->hold.valid;
139 default: return res->resolvers->timeout.resolve;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200140 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200141}
142
Christopher Faulet67957bd2017-09-27 11:00:59 +0200143/* Updates a resolvers' task timeout for next wake up and queue it */
144static void dns_update_resolvers_timeout(struct dns_resolvers *resolvers)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200145{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200146 struct dns_resolution *res;
147 int next;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200148
Christopher Faulet67957bd2017-09-27 11:00:59 +0200149 next = tick_add(now_ms, resolvers->timeout.resolve);
150 if (!LIST_ISEMPTY(&resolvers->resolutions.curr)) {
151 res = LIST_NEXT(&resolvers->resolutions.curr, struct dns_resolution *, list);
152 next = MIN(next, tick_add(res->last_query, resolvers->timeout.retry));
153 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200154
Christopher Faulet67957bd2017-09-27 11:00:59 +0200155 list_for_each_entry(res, &resolvers->resolutions.wait, list)
156 next = MIN(next, tick_add(res->last_resolution, dns_resolution_timeout(res)));
Baptiste Assmann325137d2015-04-13 23:40:55 +0200157
Christopher Faulet67957bd2017-09-27 11:00:59 +0200158 resolvers->t->expire = next;
159 task_queue(resolvers->t);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200160}
161
Christopher Faulet67957bd2017-09-27 11:00:59 +0200162/* Opens an UDP socket on the namesaver's IP/Port, if required. Returns 0 on
163 * success, -1 otherwise.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200164 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200165static int dns_connect_namesaver(struct dns_nameserver *ns)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200166{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200167 struct dgram_conn *dgram = ns->dgram;
168 int fd;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200169
Christopher Faulet67957bd2017-09-27 11:00:59 +0200170 /* Already connected */
171 if (dgram->t.sock.fd != -1)
172 return 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200173
Christopher Faulet67957bd2017-09-27 11:00:59 +0200174 /* Create an UDP socket and connect it on the nameserver's IP/Port */
175 if ((fd = socket(ns->addr.ss_family, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
176 send_log(NULL, LOG_WARNING,
177 "DNS : resolvers '%s': can't create socket for nameserver '%s'.\n",
178 ns->resolvers->id, ns->id);
179 return -1;
180 }
181 if (connect(fd, (struct sockaddr*)&ns->addr, get_addr_len(&ns->addr)) == -1) {
182 send_log(NULL, LOG_WARNING,
183 "DNS : resolvers '%s': can't connect socket for nameserver '%s'.\n",
184 ns->resolvers->id, ns->id);
185 close(fd);
186 return -1;
187 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200188
Christopher Faulet67957bd2017-09-27 11:00:59 +0200189 /* Make the socket non blocking */
190 fcntl(fd, F_SETFL, O_NONBLOCK);
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200191
Christopher Faulet67957bd2017-09-27 11:00:59 +0200192 /* Add the fd in the fd list and update its parameters */
193 dgram->t.sock.fd = fd;
194 fdtab[fd].owner = dgram;
195 fdtab[fd].iocb = dgram_fd_handler;
Christopher Faulet36716a72017-05-30 11:07:16 +0200196 fd_insert(fd, (unsigned long)-1);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200197 fd_want_recv(fd);
198 return 0;
199}
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200200
Christopher Faulet67957bd2017-09-27 11:00:59 +0200201/* Forges a DNS query. It needs the following information from the caller:
202 * - <query_id> : the DNS query id corresponding to this query
203 * - <query_type> : DNS_RTYPE_* request DNS record type (A, AAAA, ANY...)
204 * - <hostname_dn> : hostname in domain name format
205 * - <hostname_dn_len> : length of <hostname_dn>
206 *
207 * To store the query, the caller must pass a buffer <buf> and its size
208 * <bufsize>. It returns the number of written bytes in success, -1 if <buf> is
209 * too short.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200210 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200211static int dns_build_query(int query_id, int query_type, unsigned int accepted_payload_size,
212 char *hostname_dn, int hostname_dn_len, char *buf, int bufsize)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200213{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200214 struct dns_header dns_hdr;
215 struct dns_question qinfo;
216 struct dns_additional_record edns;
217 char *p = buf;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +0200218
Christopher Faulet67957bd2017-09-27 11:00:59 +0200219 if (sizeof(dns_hdr) + sizeof(qinfo) + sizeof(edns) + hostname_dn_len >= bufsize)
220 return -1;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +0200221
Christopher Faulet67957bd2017-09-27 11:00:59 +0200222 memset(buf, 0, bufsize);
Baptiste Assmannfa4a6632017-05-04 09:05:00 +0200223
Christopher Faulet67957bd2017-09-27 11:00:59 +0200224 /* Set dns query headers */
225 dns_hdr.id = (unsigned short) htons(query_id);
226 dns_hdr.flags = htons(0x0100); /* qr=0, opcode=0, aa=0, tc=0, rd=1, ra=0, z=0, rcode=0 */
227 dns_hdr.qdcount = htons(1); /* 1 question */
228 dns_hdr.ancount = 0;
229 dns_hdr.nscount = 0;
230 dns_hdr.arcount = htons(1);
231 memcpy(p, &dns_hdr, sizeof(dns_hdr));
232 p += sizeof(dns_hdr);
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200233
Christopher Faulet67957bd2017-09-27 11:00:59 +0200234 /* Set up query hostname */
235 memcpy(p, hostname_dn, hostname_dn_len);
236 p += hostname_dn_len;
237 *p++ = 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200238
Christopher Faulet67957bd2017-09-27 11:00:59 +0200239 /* Set up query info (type and class) */
240 qinfo.qtype = htons(query_type);
241 qinfo.qclass = htons(DNS_RCLASS_IN);
242 memcpy(p, &qinfo, sizeof(qinfo));
243 p += sizeof(qinfo);
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200244
Christopher Faulet67957bd2017-09-27 11:00:59 +0200245 /* Set the DNS extension */
246 edns.name = 0;
247 edns.type = htons(DNS_RTYPE_OPT);
248 edns.udp_payload_size = htons(accepted_payload_size);
249 edns.extension = 0;
250 edns.data_length = 0;
251 memcpy(p, &edns, sizeof(edns));
252 p += sizeof(edns);
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200253
Christopher Faulet67957bd2017-09-27 11:00:59 +0200254 return (p - buf);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200255}
256
Christopher Faulet67957bd2017-09-27 11:00:59 +0200257/* Sends a DNS query to resolvers associated to a resolution. It returns 0 on
258 * success, -1 otherwise.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200259 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200260static int dns_send_query(struct dns_resolution *resolution)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200261{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200262 struct dns_resolvers *resolvers = resolution->resolvers;
263 struct dns_nameserver *ns;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200264
Christopher Faulet67957bd2017-09-27 11:00:59 +0200265 list_for_each_entry(ns, &resolvers->nameservers, list) {
266 int fd = ns->dgram->t.sock.fd;
267 if (fd == -1) {
268 if (dns_connect_namesaver(ns) == -1)
269 continue;
270 fd = ns->dgram->t.sock.fd;
271 resolvers->nb_nameservers++;
272 }
273 fd_want_send(fd);
274 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200275
Christopher Faulet67957bd2017-09-27 11:00:59 +0200276 /* Update resolution */
277 resolution->nb_queries = 0;
278 resolution->nb_responses = 0;
279 resolution->last_query = now_ms;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200280
Christopher Faulet67957bd2017-09-27 11:00:59 +0200281 /* Push the resolution at the end of the active list */
282 LIST_DEL(&resolution->list);
283 LIST_ADDQ(&resolvers->resolutions.curr, &resolution->list);
284 return 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200285}
286
Christopher Faulet67957bd2017-09-27 11:00:59 +0200287/* Prepares and sends a DNS resolution. It returns 1 if the query was sent, 0 if
288 * skipped and -1 if an error occurred.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200289 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200290static int
291dns_run_resolution(struct dns_resolution *resolution)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200292{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200293 struct dns_resolvers *resolvers = resolution->resolvers;
294 int query_id, i;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200295
Christopher Faulet67957bd2017-09-27 11:00:59 +0200296 /* Avoid sending requests for resolutions that don't yet have an
297 * hostname, ie resolutions linked to servers that do not yet have an
298 * fqdn */
299 if (!resolution->hostname_dn)
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200300 return 0;
301
Christopher Faulet67957bd2017-09-27 11:00:59 +0200302 /* Check if a resolution has already been started for this server return
303 * directly to avoid resolution pill up. */
304 if (resolution->step != RSLV_STEP_NONE)
305 return 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200306
Christopher Faulet67957bd2017-09-27 11:00:59 +0200307 /* Generates a new query id. We try at most 100 times to find a free
308 * query id */
309 for (i = 0; i < 100; ++i) {
310 query_id = dns_rnd16();
311 if (!eb32_lookup(&resolvers->query_ids, query_id))
Olivier Houchard8da5f982017-08-04 18:35:36 +0200312 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200313 query_id = -1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200314 }
Christopher Faulet67957bd2017-09-27 11:00:59 +0200315 if (query_id == -1) {
316 send_log(NULL, LOG_NOTICE,
317 "could not generate a query id for %s, in resolvers %s.\n",
318 resolution->hostname_dn, resolvers->id);
319 return -1;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200320 }
321
Christopher Faulet67957bd2017-09-27 11:00:59 +0200322 /* Update resolution parameters */
323 resolution->query_id = query_id;
324 resolution->qid.key = query_id;
325 resolution->step = RSLV_STEP_RUNNING;
326 resolution->query_type = resolution->prefered_query_type;
327 resolution->try = resolvers->resolve_retries;
328 eb32_insert(&resolvers->query_ids, &resolution->qid);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200329
Christopher Faulet67957bd2017-09-27 11:00:59 +0200330 /* Send the DNS query */
331 resolution->try -= 1;
332 dns_send_query(resolution);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200333 return 1;
334}
335
Christopher Faulet67957bd2017-09-27 11:00:59 +0200336/* Performs a name resolution for the requester <req> */
337void dns_trigger_resolution(struct dns_requester *req)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200338{
Christopher Faulet67957bd2017-09-27 11:00:59 +0200339 struct dns_resolvers *resolvers;
340 struct dns_resolution *res;
341 int exp;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200342
Christopher Faulet67957bd2017-09-27 11:00:59 +0200343 if (!req || !req->resolution)
344 return;
345 res = req->resolution;
346 resolvers = res->resolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200347
Christopher Faulet67957bd2017-09-27 11:00:59 +0200348 /* The resolution must not be triggered yet. Use the cached response, if
349 * valid */
350 exp = tick_add(res->last_resolution, resolvers->hold.valid);
351 if (res->status == RSLV_STATUS_VALID &&
352 tick_isset(res->last_resolution) && !tick_is_expired(exp, now_ms))
353 req->requester_cb(req, NULL);
354 else
355 dns_run_resolution(res);
356}
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200357
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200358
Christopher Faulet67957bd2017-09-27 11:00:59 +0200359/* Resets some resolution parameters to initial values and also delete the query
360 * ID from the resolver's tree.
361 */
362static void dns_reset_resolution(struct dns_resolution *resolution)
363{
364 /* update resolution status */
365 resolution->step = RSLV_STEP_NONE;
366 resolution->try = 0;
367 resolution->last_resolution = now_ms;
368 resolution->nb_queries = 0;
369 resolution->nb_responses = 0;
370 resolution->query_type = resolution->prefered_query_type;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200371
Christopher Faulet67957bd2017-09-27 11:00:59 +0200372 /* clean up query id */
373 eb32_delete(&resolution->qid);
374 resolution->query_id = 0;
375 resolution->qid.key = 0;
376}
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200377
Christopher Faulet67957bd2017-09-27 11:00:59 +0200378/* Returns the query id contained in a DNS response */
379static inline unsigned short dns_response_get_query_id(unsigned char *resp)
380{
381 return resp[0] * 256 + resp[1];
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200382}
383
Christopher Faulet67957bd2017-09-27 11:00:59 +0200384
385/* Analyses, re-builds and copies the name <name> from the DNS response packet
386 * <buffer>. <name> must point to the 'data_len' information or pointer 'c0'
387 * for compressed data. The result is copied into <dest>, ensuring we don't
388 * overflow using <dest_len> Returns the number of bytes the caller can move
389 * forward. If 0 it means an error occurred while parsing the name. <offset> is
390 * the number of bytes the caller could move forward.
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200391 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200392int dns_read_name(unsigned char *buffer, unsigned char *bufend,
393 unsigned char *name, char *destination, int dest_len,
394 int *offset)
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200395{
396 int nb_bytes = 0, n = 0;
397 int label_len;
398 unsigned char *reader = name;
399 char *dest = destination;
400
401 while (1) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200402 /* Name compression is in use */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200403 if ((*reader & 0xc0) == 0xc0) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200404 /* Must point BEFORE current position */
405 if ((buffer + reader[1]) > reader)
406 goto err;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200407
Christopher Faulet67957bd2017-09-27 11:00:59 +0200408 n = dns_read_name(buffer, bufend, buffer + reader[1],
409 dest, dest_len - nb_bytes, offset);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200410 if (n == 0)
Christopher Faulet67957bd2017-09-27 11:00:59 +0200411 goto err;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200412
Christopher Faulet67957bd2017-09-27 11:00:59 +0200413 dest += n;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200414 nb_bytes += n;
415 goto out;
416 }
417
418 label_len = *reader;
419 if (label_len == 0)
420 goto out;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200421
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200422 /* Check if:
423 * - we won't read outside the buffer
424 * - there is enough place in the destination
425 */
426 if ((reader + label_len >= bufend) || (nb_bytes + label_len >= dest_len))
Christopher Faulet67957bd2017-09-27 11:00:59 +0200427 goto err;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200428
429 /* +1 to take label len + label string */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200430 label_len++;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200431
432 memcpy(dest, reader, label_len);
433
Christopher Faulet67957bd2017-09-27 11:00:59 +0200434 dest += label_len;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200435 nb_bytes += label_len;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200436 reader += label_len;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200437 }
438
Christopher Faulet67957bd2017-09-27 11:00:59 +0200439 out:
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200440 /* offset computation:
441 * parse from <name> until finding either NULL or a pointer "c0xx"
442 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200443 reader = name;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200444 *offset = 0;
445 while (reader < bufend) {
446 if ((reader[0] & 0xc0) == 0xc0) {
447 *offset += 2;
448 break;
449 }
450 else if (*reader == 0) {
451 *offset += 1;
452 break;
453 }
454 *offset += 1;
455 ++reader;
456 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200457 return nb_bytes;
458
Christopher Faulet67957bd2017-09-27 11:00:59 +0200459 err:
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200460 return 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200461}
462
Christopher Faulet67957bd2017-09-27 11:00:59 +0200463/* Checks for any obsolete record, also identify any SRV request, and try to
464 * find a corresponding server.
465*/
466static void dns_check_dns_response(struct dns_resolution *res)
467{
468 struct dns_resolvers *resolvers = res->resolvers;
469 struct dns_requester *req, *reqback;
470 struct dns_answer_item *item, *itemback;
471 struct server *srv;
472 struct dns_srvrq *srvrq;
473
474 list_for_each_entry_safe(item, itemback, &res->response.answer_list, list) {
475
476 /* Remove obsolete items */
477 if ((item->last_seen + resolvers->hold.obsolete / 1000) < now.tv_sec) {
478 if (item->type != DNS_RTYPE_SRV)
479 goto rm_obselete_item;
480
481 list_for_each_entry_safe(req, reqback, &res->requesters, list) {
482 if ((srvrq = objt_dns_srvrq(req->owner)) == NULL)
483 continue;
484
485 /* Remove any associated server */
486 for (srv = srvrq->proxy->srv; srv != NULL; srv = srv->next) {
487 if (srv->srvrq == srvrq && srv->svc_port == item->port &&
488 item->data_len == srv->hostname_dn_len &&
489 !memcmp(srv->hostname_dn, item->target, item->data_len)) {
490 snr_update_srv_status(srv, 1);
491 free(srv->hostname);
492 free(srv->hostname_dn);
493 srv->hostname = NULL;
494 srv->hostname_dn = NULL;
495 srv->hostname_dn_len = 0;
496 dns_unlink_resolution(srv->dns_requester);
497 }
498 }
499 }
500
501 rm_obselete_item:
502 LIST_DEL(&item->list);
503 pool_free2(dns_answer_item_pool, item);
504 continue;
505 }
506
507 if (item->type != DNS_RTYPE_SRV)
508 continue;
509
510 /* Now process SRV records */
511 list_for_each_entry_safe(req, reqback, &res->requesters, list) {
512 if ((srvrq = objt_dns_srvrq(req->owner)) == NULL)
513 continue;
514
515 /* Check if a server already uses that hostname */
516 for (srv = srvrq->proxy->srv; srv != NULL; srv = srv->next) {
517 if (srv->srvrq == srvrq && srv->svc_port == item->port &&
518 item->data_len == srv->hostname_dn_len &&
519 !memcmp(srv->hostname_dn, item->target, item->data_len)) {
520 if (srv->uweight != item->weight) {
521 char weight[9];
522
523 snprintf(weight, sizeof(weight), "%d", item->weight);
524 server_parse_weight_change_request(srv, weight);
525 }
526 break;
527 }
528 }
529 if (srv)
530 continue;
531
532 /* If not, try to find a server with undefined hostname */
533 for (srv = srvrq->proxy->srv; srv != NULL; srv = srv->next) {
534 if (srv->srvrq == srvrq && !srv->hostname_dn)
535 break;
536 }
537 /* And update this server, if found */
538 if (srv) {
539 const char *msg = NULL;
540 char weight[9];
541 char hostname[DNS_MAX_NAME_SIZE];
542
543 if (dns_dn_label_to_str(item->target, item->data_len+1,
544 hostname, DNS_MAX_NAME_SIZE) == -1)
545 continue;
546 msg = update_server_fqdn(srv, hostname, "SRV record");
547 if (msg)
548 send_log(srv->proxy, LOG_NOTICE, "%s", msg);
549
550 srv->svc_port = item->port;
551 srv->flags &= ~SRV_F_MAPPORTS;
552 if ((srv->check.state & CHK_ST_CONFIGURED) &&
553 !(srv->flags & SRV_F_CHECKPORT))
554 srv->check.port = item->port;
555 snprintf(weight, sizeof(weight), "%d", item->weight);
556 server_parse_weight_change_request(srv, weight);
557 }
558 }
559 }
560}
561
562/* Validates that the buffer DNS response provided in <resp> and finishing
563 * before <bufend> is valid from a DNS protocol point of view.
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200564 *
Christopher Faulet67957bd2017-09-27 11:00:59 +0200565 * The result is stored in <resolution>' response, buf_response,
566 * response_query_records and response_answer_records members.
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200567 *
568 * This function returns one of the DNS_RESP_* code to indicate the type of
569 * error found.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200570 */
Christopher Faulet67957bd2017-09-27 11:00:59 +0200571static int dns_validate_dns_response(unsigned char *resp, unsigned char *bufend,
572 struct dns_resolution *resolution, int max_answer_records)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200573{
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200574 unsigned char *reader;
575 char *previous_dname, tmpname[DNS_MAX_NAME_SIZE];
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200576 int len, flags, offset;
577 int dns_query_record_id;
Baptiste Assmann69fce672017-05-04 08:37:45 +0200578 int nb_saved_records;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200579 struct dns_query_item *dns_query;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200580 struct dns_answer_item *dns_answer_record, *tmp_record;
Baptiste Assmann729c9012017-05-22 15:13:10 +0200581 struct dns_response_packet *dns_p;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200582 int i, found = 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200583
Christopher Faulet67957bd2017-09-27 11:00:59 +0200584 reader = resp;
585 len = 0;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200586 previous_dname = NULL;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200587 dns_query = NULL;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200588
Christopher Faulet67957bd2017-09-27 11:00:59 +0200589 /* Initialization of response buffer and structure */
Baptiste Assmann729c9012017-05-22 15:13:10 +0200590 dns_p = &resolution->response;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200591
592 /* query id */
593 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200594 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200595 dns_p->header.id = reader[0] * 256 + reader[1];
596 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200597
Christopher Faulet67957bd2017-09-27 11:00:59 +0200598 /* Flags and rcode are stored over 2 bytes
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200599 * First byte contains:
600 * - response flag (1 bit)
601 * - opcode (4 bits)
602 * - authoritative (1 bit)
603 * - truncated (1 bit)
604 * - recursion desired (1 bit)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200605 */
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200606 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200607 return DNS_RESP_INVALID;
608
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200609 flags = reader[0] * 256 + reader[1];
610
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200611 if ((flags & DNS_FLAG_REPLYCODE) != DNS_RCODE_NO_ERROR) {
612 if ((flags & DNS_FLAG_REPLYCODE) == DNS_RCODE_NX_DOMAIN)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200613 return DNS_RESP_NX_DOMAIN;
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200614 else if ((flags & DNS_FLAG_REPLYCODE) == DNS_RCODE_REFUSED)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200615 return DNS_RESP_REFUSED;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200616 return DNS_RESP_ERROR;
617 }
618
Christopher Faulet67957bd2017-09-27 11:00:59 +0200619 /* Move forward 2 bytes for flags */
Baptiste Assmann3440f0d2015-09-02 22:08:38 +0200620 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200621
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200622 /* 2 bytes for question count */
623 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200624 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200625 dns_p->header.qdcount = reader[0] * 256 + reader[1];
Christopher Faulet67957bd2017-09-27 11:00:59 +0200626 /* (for now) we send one query only, so we expect only one in the
627 * response too */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200628 if (dns_p->header.qdcount != 1)
629 return DNS_RESP_QUERY_COUNT_ERROR;
630 if (dns_p->header.qdcount > DNS_MAX_QUERY_RECORDS)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200631 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200632 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200633
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200634 /* 2 bytes for answer count */
635 if (reader + 2 >= bufend)
636 return DNS_RESP_INVALID;
637 dns_p->header.ancount = reader[0] * 256 + reader[1];
638 if (dns_p->header.ancount == 0)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200639 return DNS_RESP_ANCOUNT_ZERO;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200640 /* Check if too many records are announced */
Baptiste Assmann9d8dbbc2017-08-18 23:35:08 +0200641 if (dns_p->header.ancount > max_answer_records)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200642 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200643 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200644
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200645 /* 2 bytes authority count */
646 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200647 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200648 dns_p->header.nscount = reader[0] * 256 + reader[1];
649 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200650
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200651 /* 2 bytes additional count */
652 if (reader + 2 >= bufend)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200653 return DNS_RESP_INVALID;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200654 dns_p->header.arcount = reader[0] * 256 + reader[1];
655 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200656
Christopher Faulet67957bd2017-09-27 11:00:59 +0200657 /* Parsing dns queries */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200658 LIST_INIT(&dns_p->query_list);
659 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 +0200660 /* Use next pre-allocated dns_query_item after ensuring there is
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200661 * still one available.
Christopher Faulet67957bd2017-09-27 11:00:59 +0200662 * It's then added to our packet query list. */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200663 if (dns_query_record_id > DNS_MAX_QUERY_RECORDS)
664 return DNS_RESP_INVALID;
Baptiste Assmann729c9012017-05-22 15:13:10 +0200665 dns_query = &resolution->response_query_records[dns_query_record_id];
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200666 LIST_ADDQ(&dns_p->query_list, &dns_query->list);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200667
Christopher Faulet67957bd2017-09-27 11:00:59 +0200668 /* Name is a NULL terminated string in our case, since we have
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200669 * one query per response and the first one can't be compressed
Christopher Faulet67957bd2017-09-27 11:00:59 +0200670 * (using the 0x0c format) */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200671 offset = 0;
672 len = dns_read_name(resp, bufend, reader, dns_query->name, DNS_MAX_NAME_SIZE, &offset);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200673
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200674 if (len == 0)
675 return DNS_RESP_INVALID;
676
677 reader += offset;
678 previous_dname = dns_query->name;
679
680 /* move forward 2 bytes for question type */
681 if (reader + 2 >= bufend)
682 return DNS_RESP_INVALID;
683 dns_query->type = reader[0] * 256 + reader[1];
684 reader += 2;
685
686 /* move forward 2 bytes for question class */
687 if (reader + 2 >= bufend)
688 return DNS_RESP_INVALID;
689 dns_query->class = reader[0] * 256 + reader[1];
690 reader += 2;
691 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200692
Baptiste Assmann251abb92017-08-11 09:58:27 +0200693 /* TRUNCATED flag must be checked after we could read the query type
Christopher Faulet67957bd2017-09-27 11:00:59 +0200694 * because a TRUNCATED SRV query type response can still be exploited */
Baptiste Assmann251abb92017-08-11 09:58:27 +0200695 if (dns_query->type != DNS_RTYPE_SRV && flags & DNS_FLAG_TRUNCATED)
696 return DNS_RESP_TRUNCATED;
697
Baptiste Assmann325137d2015-04-13 23:40:55 +0200698 /* now parsing response records */
Baptiste Assmann69fce672017-05-04 08:37:45 +0200699 nb_saved_records = 0;
Olivier Houchard8da5f982017-08-04 18:35:36 +0200700 for (i = 0; i < dns_p->header.ancount; i++) {
Baptiste Assmann325137d2015-04-13 23:40:55 +0200701 if (reader >= bufend)
702 return DNS_RESP_INVALID;
703
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200704 dns_answer_record = pool_alloc2(dns_answer_item_pool);
705 if (dns_answer_record == NULL)
706 return (DNS_RESP_INVALID);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200707
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200708 offset = 0;
709 len = dns_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200710
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200711 if (len == 0) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200712 pool_free2(dns_answer_item_pool, dns_answer_record);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200713 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200714 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200715
Christopher Faulet67957bd2017-09-27 11:00:59 +0200716 /* Check if the current record dname is valid. previous_dname
717 * points either to queried dname or last CNAME target */
Baptiste Assmannddc8ce62017-08-11 10:31:22 +0200718 if (dns_query->type != DNS_RTYPE_SRV && memcmp(previous_dname, tmpname, len) != 0) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200719 pool_free2(dns_answer_item_pool, dns_answer_record);
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200720 if (i == 0) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200721 /* First record, means a mismatch issue between
722 * queried dname and dname found in the first
723 * record */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200724 return DNS_RESP_INVALID;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200725 }
726 else {
727 /* If not the first record, this means we have a
728 * CNAME resolution error */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200729 return DNS_RESP_CNAME_ERROR;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200730 }
731
Baptiste Assmann325137d2015-04-13 23:40:55 +0200732 }
733
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200734 memcpy(dns_answer_record->name, tmpname, len);
735 dns_answer_record->name[len] = 0;
Baptiste Assmann2359ff12015-08-07 11:24:05 +0200736
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200737 reader += offset;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200738 if (reader >= bufend) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200739 pool_free2(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200740 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200741 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200742
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200743 /* 2 bytes for record type (A, AAAA, CNAME, etc...) */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200744 if (reader + 2 > bufend) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200745 pool_free2(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200746 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200747 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200748 dns_answer_record->type = reader[0] * 256 + reader[1];
749 reader += 2;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200750
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200751 /* 2 bytes for class (2) */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200752 if (reader + 2 > bufend) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200753 pool_free2(dns_answer_item_pool, dns_answer_record);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200754 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200755 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200756 dns_answer_record->class = reader[0] * 256 + reader[1];
Baptiste Assmann325137d2015-04-13 23:40:55 +0200757 reader += 2;
758
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200759 /* 4 bytes for ttl (4) */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200760 if (reader + 4 > bufend) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200761 pool_free2(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200762 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200763 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200764 dns_answer_record->ttl = reader[0] * 16777216 + reader[1] * 65536
765 + reader[2] * 256 + reader[3];
766 reader += 4;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200767
Christopher Faulet67957bd2017-09-27 11:00:59 +0200768 /* Now reading data len */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200769 if (reader + 2 > bufend) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200770 pool_free2(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200771 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200772 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200773 dns_answer_record->data_len = reader[0] * 256 + reader[1];
Baptiste Assmann325137d2015-04-13 23:40:55 +0200774
Christopher Faulet67957bd2017-09-27 11:00:59 +0200775 /* Move forward 2 bytes for data len */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200776 reader += 2;
777
Christopher Faulet67957bd2017-09-27 11:00:59 +0200778 /* Analyzing record content */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200779 switch (dns_answer_record->type) {
Baptiste Assmann325137d2015-04-13 23:40:55 +0200780 case DNS_RTYPE_A:
781 /* ipv4 is stored on 4 bytes */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200782 if (dns_answer_record->data_len != 4) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200783 pool_free2(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200784 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200785 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200786 dns_answer_record->address.sa_family = AF_INET;
787 memcpy(&(((struct sockaddr_in *)&dns_answer_record->address)->sin_addr),
788 reader, dns_answer_record->data_len);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200789 break;
790
791 case DNS_RTYPE_CNAME:
Christopher Faulet67957bd2017-09-27 11:00:59 +0200792 /* Check if this is the last record and update the caller about the status:
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200793 * no IP could be found and last record was a CNAME. Could be triggered
794 * by a wrong query type
795 *
Christopher Faulet67957bd2017-09-27 11:00:59 +0200796 * + 1 because dns_answer_record_id starts at 0
797 * while number of answers is an integer and
798 * starts at 1.
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200799 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200800 if (i + 1 == dns_p->header.ancount) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200801 pool_free2(dns_answer_item_pool, dns_answer_record);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200802 return DNS_RESP_CNAME_ERROR;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200803 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200804
805 offset = 0;
806 len = dns_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset);
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200807 if (len == 0) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200808 pool_free2(dns_answer_item_pool, dns_answer_record);
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200809 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200810 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200811
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200812 memcpy(dns_answer_record->target, tmpname, len);
813 dns_answer_record->target[len] = 0;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200814 previous_dname = dns_answer_record->target;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200815 break;
816
Olivier Houchard8da5f982017-08-04 18:35:36 +0200817
818 case DNS_RTYPE_SRV:
Christopher Faulet67957bd2017-09-27 11:00:59 +0200819 /* Answer must contain :
Olivier Houchard8da5f982017-08-04 18:35:36 +0200820 * - 2 bytes for the priority
821 * - 2 bytes for the weight
822 * - 2 bytes for the port
823 * - the target hostname
824 */
825 if (dns_answer_record->data_len <= 6) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200826 pool_free2(dns_answer_item_pool, dns_answer_record);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200827 return DNS_RESP_INVALID;
828 }
Willy Tarreaud5370e12017-09-19 14:59:52 +0200829 dns_answer_record->priority = read_n16(reader);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200830 reader += sizeof(uint16_t);
Willy Tarreaud5370e12017-09-19 14:59:52 +0200831 dns_answer_record->weight = 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->port = read_n16(reader);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200834 reader += sizeof(uint16_t);
835 offset = 0;
836 len = dns_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset);
837 if (len == 0) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200838 pool_free2(dns_answer_item_pool, dns_answer_record);
Olivier Houchard8da5f982017-08-04 18:35:36 +0200839 return DNS_RESP_INVALID;
840 }
Olivier Houchard8da5f982017-08-04 18:35:36 +0200841 dns_answer_record->data_len = len;
842 memcpy(dns_answer_record->target, tmpname, len);
843 dns_answer_record->target[len] = 0;
844 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200845
Baptiste Assmann325137d2015-04-13 23:40:55 +0200846 case DNS_RTYPE_AAAA:
847 /* ipv6 is stored on 16 bytes */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200848 if (dns_answer_record->data_len != 16) {
Christopher Faulet67957bd2017-09-27 11:00:59 +0200849 pool_free2(dns_answer_item_pool, dns_answer_record);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200850 return DNS_RESP_INVALID;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200851 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200852 dns_answer_record->address.sa_family = AF_INET6;
853 memcpy(&(((struct sockaddr_in6 *)&dns_answer_record->address)->sin6_addr),
854 reader, dns_answer_record->data_len);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200855 break;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200856
Baptiste Assmann325137d2015-04-13 23:40:55 +0200857 } /* switch (record type) */
858
Christopher Faulet67957bd2017-09-27 11:00:59 +0200859 /* Increment the counter for number of records saved into our
860 * local response */
861 nb_saved_records++;
Baptiste Assmann69fce672017-05-04 08:37:45 +0200862
Christopher Faulet67957bd2017-09-27 11:00:59 +0200863 /* Move forward dns_answer_record->data_len for analyzing next
864 * record in the response */
865 reader += ((dns_answer_record->type == DNS_RTYPE_SRV)
866 ? offset
867 : dns_answer_record->data_len);
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200868
869 /* Lookup to see if we already had this entry */
Olivier Houchard8da5f982017-08-04 18:35:36 +0200870 found = 0;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200871 list_for_each_entry(tmp_record, &dns_p->answer_list, list) {
872 if (tmp_record->type != dns_answer_record->type)
873 continue;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200874
875 switch(tmp_record->type) {
876 case DNS_RTYPE_A:
877 if (!memcmp(&((struct sockaddr_in *)&dns_answer_record->address)->sin_addr,
878 &((struct sockaddr_in *)&tmp_record->address)->sin_addr,
879 sizeof(in_addr_t)))
880 found = 1;
881 break;
882
883 case DNS_RTYPE_AAAA:
884 if (!memcmp(&((struct sockaddr_in6 *)&dns_answer_record->address)->sin6_addr,
885 &((struct sockaddr_in6 *)&tmp_record->address)->sin6_addr,
886 sizeof(struct in6_addr)))
887 found = 1;
888 break;
889
Olivier Houchard8da5f982017-08-04 18:35:36 +0200890 case DNS_RTYPE_SRV:
891 if (dns_answer_record->data_len == tmp_record->data_len &&
Christopher Faulet67957bd2017-09-27 11:00:59 +0200892 !memcmp(dns_answer_record->target, tmp_record->target, dns_answer_record->data_len) &&
Olivier Houchard8da5f982017-08-04 18:35:36 +0200893 dns_answer_record->port == tmp_record->port) {
894 tmp_record->weight = dns_answer_record->weight;
895 found = 1;
896 }
897 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200898
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200899 default:
900 break;
901 }
Christopher Faulet67957bd2017-09-27 11:00:59 +0200902
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200903 if (found == 1)
904 break;
905 }
Christopher Faulet67957bd2017-09-27 11:00:59 +0200906
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200907 if (found == 1) {
908 tmp_record->last_seen = now.tv_sec;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200909 pool_free2(dns_answer_item_pool, dns_answer_record);
910 }
911 else {
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200912 dns_answer_record->last_seen = now.tv_sec;
913 LIST_ADDQ(&dns_p->answer_list, &dns_answer_record->list);
914 }
Baptiste Assmann325137d2015-04-13 23:40:55 +0200915 } /* for i 0 to ancount */
916
Christopher Faulet67957bd2017-09-27 11:00:59 +0200917 /* Save the number of records we really own */
Baptiste Assmann69fce672017-05-04 08:37:45 +0200918 dns_p->header.ancount = nb_saved_records;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200919 dns_check_dns_response(resolution);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200920 return DNS_RESP_VALID;
921}
922
Christopher Faulet67957bd2017-09-27 11:00:59 +0200923/* Searches dn_name resolution in resp.
Baptiste Assmann325137d2015-04-13 23:40:55 +0200924 * If existing IP not found, return the first IP matching family_priority,
925 * otherwise, first ip found
926 * The following tasks are the responsibility of the caller:
Baptiste Assmann3cf7f982016-04-17 22:43:26 +0200927 * - <dns_p> contains an error free DNS response
Baptiste Assmann325137d2015-04-13 23:40:55 +0200928 * For both cases above, dns_validate_dns_response is required
929 * returns one of the DNS_UPD_* code
930 */
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100931#define DNS_MAX_IP_REC 20
Baptiste Assmannfb7091e2017-05-03 15:43:12 +0200932int dns_get_ip_from_response(struct dns_response_packet *dns_p,
Baptiste Assmann42746372017-05-03 12:12:02 +0200933 struct dns_options *dns_opts, void *currentip,
Thierry Fournierada34842016-02-17 21:25:09 +0100934 short currentip_sin_family,
Baptiste Assmannfb7091e2017-05-03 15:43:12 +0200935 void **newip, short *newip_sin_family,
936 void *owner)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200937{
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200938 struct dns_answer_item *record;
Thierry Fournierada34842016-02-17 21:25:09 +0100939 int family_priority;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200940 int currentip_found;
Baptiste Assmann3cf7f982016-04-17 22:43:26 +0200941 unsigned char *newip4, *newip6;
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100942 int currentip_sel;
943 int j;
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100944 int score, max_score;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200945
Christopher Faulet67957bd2017-09-27 11:00:59 +0200946 family_priority = dns_opts->family_prio;
947 *newip = newip4 = newip6 = NULL;
948 currentip_found = 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200949 *newip_sin_family = AF_UNSPEC;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200950 max_score = -1;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200951
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100952 /* Select an IP regarding configuration preference.
953 * Top priority is the prefered network ip version,
954 * second priority is the prefered network.
955 * the last priority is the currently used IP,
956 *
957 * For these three priorities, a score is calculated. The
958 * weight are:
Baptistefc725902016-12-26 23:21:08 +0100959 * 8 - prefered netwok ip version.
960 * 4 - prefered network.
961 * 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 +0100962 * 1 - current ip.
963 * The result with the biggest score is returned.
964 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200965
966 list_for_each_entry(record, &dns_p->answer_list, list) {
967 void *ip;
968 unsigned char ip_type;
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100969
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200970 if (record->type == DNS_RTYPE_A) {
971 ip = &(((struct sockaddr_in *)&record->address)->sin_addr);
972 ip_type = AF_INET;
Christopher Faulet67957bd2017-09-27 11:00:59 +0200973 }
974 else if (record->type == DNS_RTYPE_AAAA) {
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200975 ip_type = AF_INET6;
976 ip = &(((struct sockaddr_in6 *)&record->address)->sin6_addr);
Christopher Faulet67957bd2017-09-27 11:00:59 +0200977 }
978 else
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200979 continue;
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100980 score = 0;
981
982 /* Check for prefered ip protocol. */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200983 if (ip_type == family_priority)
Baptistefc725902016-12-26 23:21:08 +0100984 score += 8;
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100985
986 /* Check for prefered network. */
Baptiste Assmann42746372017-05-03 12:12:02 +0200987 for (j = 0; j < dns_opts->pref_net_nb; j++) {
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100988
989 /* Compare only the same adresses class. */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200990 if (dns_opts->pref_net[j].family != ip_type)
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100991 continue;
992
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200993 if ((ip_type == AF_INET &&
994 in_net_ipv4(ip,
Baptiste Assmann42746372017-05-03 12:12:02 +0200995 &dns_opts->pref_net[j].mask.in4,
996 &dns_opts->pref_net[j].addr.in4)) ||
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200997 (ip_type == AF_INET6 &&
998 in_net_ipv6(ip,
Baptiste Assmann42746372017-05-03 12:12:02 +0200999 &dns_opts->pref_net[j].mask.in6,
1000 &dns_opts->pref_net[j].addr.in6))) {
Baptistefc725902016-12-26 23:21:08 +01001001 score += 4;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001002 break;
1003 }
1004 }
1005
Christopher Faulet67957bd2017-09-27 11:00:59 +02001006 /* Check if the IP found in the record is already affected to a
1007 * member of a group. If yes, the score should be incremented
1008 * by 2. */
1009 if (owner && snr_check_ip_callback(owner, ip, &ip_type))
1010 continue;
1011
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001012 /* Check for current ip matching. */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001013 if (ip_type == currentip_sin_family &&
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001014 ((currentip_sin_family == AF_INET &&
Christopher Faulet67957bd2017-09-27 11:00:59 +02001015 !memcmp(ip, currentip, 4)) ||
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001016 (currentip_sin_family == AF_INET6 &&
Christopher Faulet67957bd2017-09-27 11:00:59 +02001017 !memcmp(ip, currentip, 16)))) {
1018 score++;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001019 currentip_sel = 1;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001020 }
1021 else
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001022 currentip_sel = 0;
1023
1024 /* Keep the address if the score is better than the previous
Christopher Faulet67957bd2017-09-27 11:00:59 +02001025 * score. The maximum score is 15, if this value is reached, we
1026 * break the parsing. Implicitly, this score is reached the ip
1027 * selected is the current ip. */
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001028 if (score > max_score) {
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001029 if (ip_type == AF_INET)
1030 newip4 = ip;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001031 else
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001032 newip6 = ip;
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001033 currentip_found = currentip_sel;
Baptistefc725902016-12-26 23:21:08 +01001034 if (score == 15)
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001035 return DNS_UPD_NO;
1036 max_score = score;
1037 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001038 } /* list for each record entries */
Thierry Fournierac88cfe2016-02-17 22:05:30 +01001039
Christopher Faulet67957bd2017-09-27 11:00:59 +02001040 /* No IP found in the response */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02001041 if (!newip4 && !newip6)
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02001042 return DNS_UPD_NO_IP_FOUND;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02001043
Christopher Faulet67957bd2017-09-27 11:00:59 +02001044 /* Case when the caller looks first for an IPv4 address */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001045 if (family_priority == AF_INET) {
1046 if (newip4) {
1047 *newip = newip4;
1048 *newip_sin_family = AF_INET;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001049 }
1050 else if (newip6) {
1051 *newip = newip6;
1052 *newip_sin_family = AF_INET6;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001053 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001054 if (!currentip_found)
1055 goto not_found;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001056 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001057 /* Case when the caller looks first for an IPv6 address */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001058 else if (family_priority == AF_INET6) {
1059 if (newip6) {
1060 *newip = newip6;
1061 *newip_sin_family = AF_INET6;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001062 }
1063 else if (newip4) {
1064 *newip = newip4;
1065 *newip_sin_family = AF_INET;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001066 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001067 if (!currentip_found)
1068 goto not_found;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001069 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001070 /* Case when the caller have no preference (we prefer IPv6) */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001071 else if (family_priority == AF_UNSPEC) {
1072 if (newip6) {
1073 *newip = newip6;
1074 *newip_sin_family = AF_INET6;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001075 }
1076 else if (newip4) {
1077 *newip = newip4;
1078 *newip_sin_family = AF_INET;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001079 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001080 if (!currentip_found)
1081 goto not_found;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001082 }
1083
Christopher Faulet67957bd2017-09-27 11:00:59 +02001084 /* No reason why we should change the server's IP address */
Baptiste Assmann325137d2015-04-13 23:40:55 +02001085 return DNS_UPD_NO;
Baptiste Assmann8ea0bcc2017-05-04 08:24:11 +02001086
Christopher Faulet67957bd2017-09-27 11:00:59 +02001087 not_found:
Baptiste Assmann8ea0bcc2017-05-04 08:24:11 +02001088 list_for_each_entry(record, &dns_p->answer_list, list) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02001089 /* Move the first record to the end of the list, for internal
1090 * round robin */
1091 LIST_DEL(&record->list);
1092 LIST_ADDQ(&dns_p->answer_list, &record->list);
1093 break;
Baptiste Assmann8ea0bcc2017-05-04 08:24:11 +02001094 }
1095 return DNS_UPD_SRVIP_NOT_FOUND;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001096}
1097
Christopher Faulet67957bd2017-09-27 11:00:59 +02001098/* Turns a domain name label into a string.
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001099 *
Christopher Faulet67957bd2017-09-27 11:00:59 +02001100 * <dn> must be a null-terminated string. <dn_len> must include the terminating
1101 * null byte. <str> must be allocated and its size must be passed in <str_len>.
Baptiste Assmann325137d2015-04-13 23:40:55 +02001102 *
Christopher Faulet67957bd2017-09-27 11:00:59 +02001103 * In case of error, -1 is returned, otherwise, the number of bytes copied in
1104 * <str> (including the terminating null byte).
Baptiste Assmann325137d2015-04-13 23:40:55 +02001105 */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001106int dns_dn_label_to_str(const char *dn, int dn_len, char *str, int str_len)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001107{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001108 char *ptr;
1109 int i, sz;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001110
Christopher Faulet67957bd2017-09-27 11:00:59 +02001111 if (str_len < dn_len - 1)
Baptiste Assmann2af08fe2017-08-14 00:13:01 +02001112 return -1;
1113
Christopher Faulet67957bd2017-09-27 11:00:59 +02001114 ptr = str;
1115 for (i = 0; i < dn_len-1; ++i) {
1116 sz = dn[i];
1117 if (i)
1118 *ptr++ = '.';
1119 memcpy(ptr, dn+i+1, sz);
1120 ptr += sz;
1121 i += sz;
Olivier Houchard8da5f982017-08-04 18:35:36 +02001122 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001123 *ptr++ = '\0';
1124 return (ptr - str);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001125}
1126
Christopher Faulet67957bd2017-09-27 11:00:59 +02001127/* Turns a string into domain name label: www.haproxy.org into 3www7haproxy3org
1128 *
1129 * <str> must be a null-terminated string. <str_len> must include the
1130 * terminating null byte. <dn> buffer must be allocated and its size must be
1131 * passed in <dn_len>.
1132 *
1133 * In case of error, -1 is returned, otherwise, the number of bytes copied in
1134 * <dn> (excluding the terminating null byte).
Baptiste Assmann325137d2015-04-13 23:40:55 +02001135 */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001136int dns_str_to_dn_label(const char *str, int str_len, char *dn, int dn_len)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001137{
Baptiste Assmann325137d2015-04-13 23:40:55 +02001138 int i, offset;
1139
Christopher Faulet67957bd2017-09-27 11:00:59 +02001140 if (dn_len < str_len + 1)
1141 return -1;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001142
Christopher Faulet67957bd2017-09-27 11:00:59 +02001143 /* First byte of dn will be used to store the length of the first
1144 * label */
1145 offset = 0;
1146 for (i = 0; i < str_len; ++i) {
1147 if (str[i] == '.') {
1148 /* 2 or more consecutive dots is invalid */
1149 if (i == offset)
1150 return -1;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001151
Christopher Faulet67957bd2017-09-27 11:00:59 +02001152 dn[offset] = (i - offset);
1153 offset = i+1;
1154 continue;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001155 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001156 dn[i+1] = str[i];
Baptiste Assmann325137d2015-04-13 23:40:55 +02001157 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001158 dn[offset] = (i - offset - 1);
1159 dn[i] = '\0';
1160 return i;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001161}
1162
Christopher Faulet67957bd2017-09-27 11:00:59 +02001163/* Validates host name:
Baptiste Assmann325137d2015-04-13 23:40:55 +02001164 * - total size
1165 * - each label size individually
1166 * returns:
1167 * 0 in case of error. If <err> is not NULL, an error message is stored there.
1168 * 1 when no error. <err> is left unaffected.
1169 */
1170int dns_hostname_validation(const char *string, char **err)
1171{
1172 const char *c, *d;
1173 int i;
1174
1175 if (strlen(string) > DNS_MAX_NAME_SIZE) {
1176 if (err)
1177 *err = DNS_TOO_LONG_FQDN;
1178 return 0;
1179 }
1180
1181 c = string;
1182 while (*c) {
1183 d = c;
1184
1185 i = 0;
1186 while (*d != '.' && *d && i <= DNS_MAX_LABEL_SIZE) {
1187 i++;
1188 if (!((*d == '-') || (*d == '_') ||
1189 ((*d >= 'a') && (*d <= 'z')) ||
1190 ((*d >= 'A') && (*d <= 'Z')) ||
1191 ((*d >= '0') && (*d <= '9')))) {
1192 if (err)
1193 *err = DNS_INVALID_CHARACTER;
1194 return 0;
1195 }
1196 d++;
1197 }
1198
1199 if ((i >= DNS_MAX_LABEL_SIZE) && (d[i] != '.')) {
1200 if (err)
1201 *err = DNS_LABEL_TOO_LONG;
1202 return 0;
1203 }
1204
1205 if (*d == '\0')
1206 goto out;
1207
1208 c = ++d;
1209 }
1210 out:
1211 return 1;
1212}
1213
Christopher Faulet67957bd2017-09-27 11:00:59 +02001214/* Picks up an available resolution from the different resolution list
1215 * associated to a resolvers section, in this order:
1216 * 1. check in resolutions.curr for the same hostname and query_type
1217 * 2. check in resolutions.wait for the same hostname and query_type
1218 * 3. Get a new resolution from resolution pool
1219 *
1220 * Returns an available resolution, NULL if none found.
Baptiste Assmann325137d2015-04-13 23:40:55 +02001221 */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001222static struct dns_resolution *dns_pick_resolution(struct dns_resolvers *resolvers,
1223 char **hostname_dn, int hostname_dn_len,
1224 int query_type)
Baptiste Assmann325137d2015-04-13 23:40:55 +02001225{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001226 struct dns_resolution *res;
1227
1228 if (!*hostname_dn)
1229 goto from_pool;
1230
1231 /* Search for same hostname and query type in resolutions.curr */
1232 list_for_each_entry(res, &resolvers->resolutions.curr, list) {
1233 if (!res->hostname_dn)
1234 continue;
1235 if ((query_type == res->prefered_query_type) &&
1236 hostname_dn_len == res->hostname_dn_len &&
1237 !memcmp(*hostname_dn, res->hostname_dn, hostname_dn_len))
1238 return res;
1239 }
1240
1241 /* Search for same hostname and query type in resolutions.wait */
1242 list_for_each_entry(res, &resolvers->resolutions.wait, list) {
1243 if (!res->hostname_dn)
1244 continue;
1245 if ((query_type == res->prefered_query_type) &&
1246 hostname_dn_len == res->hostname_dn_len &&
1247 !memcmp(*hostname_dn, res->hostname_dn, hostname_dn_len))
1248 return res;
1249 }
1250
1251 from_pool:
1252 /* No resolution could be found, so let's allocate a new one */
1253 res = pool_alloc2(dns_resolution_pool);
1254 if (res) {
1255 memset(res, 0, sizeof(*res));
1256 res->resolvers = resolvers;
1257 res->uuid = resolution_uuid;
1258 res->status = RSLV_STATUS_NONE;
1259 res->step = RSLV_STEP_NONE;
1260 res->last_valid = now_ms;
1261
1262 LIST_INIT(&res->requesters);
1263 LIST_INIT(&res->response.answer_list);
1264
1265 res->prefered_query_type = query_type;
1266 res->query_type = query_type;
1267 res->hostname_dn = *hostname_dn;
1268 res->hostname_dn_len = hostname_dn_len;
1269
1270 ++resolution_uuid;
1271
1272 /* Move the resolution to the resolvers wait queue */
1273 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
1274 }
1275 return res;
1276}
1277
1278/* Releases a resolution from its requester(s) and move it back to the pool */
1279static void dns_free_resolution(struct dns_resolution *resolution)
1280{
1281 struct dns_requester *req, *reqback;
1282
1283 /* clean up configuration */
1284 dns_reset_resolution(resolution);
1285 resolution->hostname_dn = NULL;
1286 resolution->hostname_dn_len = 0;
1287
1288 list_for_each_entry_safe(req, reqback, &resolution->requesters, list) {
1289 LIST_DEL(&req->list);
1290 req->resolution = NULL;
1291 }
1292
1293 LIST_DEL(&resolution->list);
1294 pool_free2(dns_resolution_pool, resolution);
1295}
1296
1297/* Links a requester (a server or a dns_srvrq) with a resolution. It returns 0
1298 * on success, -1 otherwise.
1299 */
1300int dns_link_resolution(void *requester, int requester_type)
1301{
1302 struct dns_resolution *res = NULL;
1303 struct dns_requester *req;
1304 struct dns_resolvers *resolvers;
1305 struct server *srv = NULL;
1306 struct dns_srvrq *srvrq = NULL;
1307 char **hostname_dn;
1308 int hostname_dn_len, query_type;
1309
1310 switch (requester_type) {
1311 case OBJ_TYPE_SERVER:
1312 srv = (struct server *)requester;
1313 hostname_dn = &srv->hostname_dn;
1314 hostname_dn_len = srv->hostname_dn_len;
1315 resolvers = srv->resolvers;
1316 query_type = ((srv->dns_opts.family_prio == AF_INET)
1317 ? DNS_RTYPE_A
1318 : DNS_RTYPE_AAAA);
1319 break;
1320
1321 case OBJ_TYPE_SRVRQ:
1322 srvrq = (struct dns_srvrq *)requester;
1323 hostname_dn = &srvrq->hostname_dn;
1324 hostname_dn_len = srvrq->hostname_dn_len;
1325 resolvers = srvrq->resolvers;
1326 query_type = DNS_RTYPE_SRV;
1327 break;
1328
1329 default:
1330 goto err;
1331 }
1332
1333 /* Get a resolution from the resolvers' wait queue or pool */
1334 if ((res = dns_pick_resolution(resolvers, hostname_dn, hostname_dn_len, query_type)) == NULL)
1335 goto err;
1336
1337 if (srv) {
1338 if (srv->dns_requester == NULL) {
1339 if ((req = calloc(1, sizeof(*req))) == NULL)
1340 goto err;
1341 req->owner = &srv->obj_type;
1342 srv->dns_requester = req;
1343 }
1344 else
1345 req = srv->dns_requester;
1346 }
1347 else if (srvrq) {
1348 if (srvrq->dns_requester == NULL) {
1349 if ((req = calloc(1, sizeof(*req))) == NULL)
1350 goto err;
1351 req->owner = &srvrq->obj_type;
1352 srvrq->dns_requester = req;
1353 }
1354 else
1355 req = srvrq->dns_requester;
1356 }
1357 else
1358 goto err;
1359
1360 req->resolution = res;
1361 req->requester_cb = snr_resolution_cb;
1362 req->requester_error_cb = snr_resolution_error_cb;
1363
1364 LIST_ADDQ(&res->requesters, &req->list);
1365 return 0;
1366
1367 err:
1368 if (res && LIST_ISEMPTY(&res->requesters))
1369 dns_free_resolution(res);
1370 return -1;
1371}
1372
1373/* Removes a requester from a DNS resoltion. It takes takes care of all the
1374 * consequences. It also cleans up some parameters from the requester.
1375 */
1376void dns_unlink_resolution(struct dns_requester *requester)
1377{
1378 struct dns_resolution *res;
1379 struct dns_requester *req;
1380
1381 /* Nothing to do */
1382 if (!requester || !requester->resolution)
1383 return;
1384 res = requester->resolution;
1385
1386 /* Clean up the requester */
1387 LIST_DEL(&requester->list);
1388 requester->resolution = NULL;
1389
1390 /* We need to find another requester linked on this resolution */
1391 if (!LIST_ISEMPTY(&res->requesters))
1392 req = LIST_NEXT(&res->requesters, struct dns_requester *, list);
1393 else {
1394 dns_free_resolution(res);
1395 return;
1396 }
1397
1398 /* Move hostname_dn related pointers to the next requester */
1399 switch (obj_type(req->owner)) {
1400 case OBJ_TYPE_SERVER:
1401 res->hostname_dn = objt_server(req->owner)->hostname_dn;
1402 res->hostname_dn_len = objt_server(req->owner)->hostname_dn_len;
1403 break;
1404 case OBJ_TYPE_SRVRQ:
1405 res->hostname_dn = objt_dns_srvrq(req->owner)->hostname_dn;
1406 res->hostname_dn_len = objt_dns_srvrq(req->owner)->hostname_dn_len;
1407 break;
1408 default:
1409 res->hostname_dn = NULL;
1410 res->hostname_dn_len = 0;
1411 break;
1412 }
Baptiste Assmann325137d2015-04-13 23:40:55 +02001413}
1414
Christopher Faulet67957bd2017-09-27 11:00:59 +02001415/* Called when a network IO is generated on a name server socket for an incoming
1416 * packet. It performs the following actions:
1417 * - check if the packet requires processing (not outdated resolution)
1418 * - ensure the DNS packet received is valid and call requester's callback
1419 * - call requester's error callback if invalid response
1420 * - check the dn_name in the packet against the one sent
1421 */
1422static void dns_resolve_recv(struct dgram_conn *dgram)
1423{
1424 struct dns_nameserver *ns, *tmpns;
1425 struct dns_resolvers *resolvers;
1426 struct dns_resolution *res;
1427 struct dns_query_item *query;
1428 unsigned char buf[DNS_MAX_UDP_MESSAGE + 1];
1429 unsigned char *bufend;
1430 int fd, buflen, dns_resp;
1431 int max_answer_records;
1432 unsigned short query_id;
1433 struct eb32_node *eb;
1434 struct dns_requester *req;
1435
1436 fd = dgram->t.sock.fd;
1437
1438 /* check if ready for reading */
1439 if (!fd_recv_ready(fd))
1440 return;
1441
1442 /* no need to go further if we can't retrieve the nameserver */
1443 if ((ns = dgram->owner) == NULL)
1444 return;
1445
1446 resolvers = ns->resolvers;
1447
1448 /* process all pending input messages */
1449 while (1) {
1450 /* read message received */
1451 memset(buf, '\0', resolvers->accepted_payload_size + 1);
1452 if ((buflen = recv(fd, (char*)buf , resolvers->accepted_payload_size + 1, 0)) < 0) {
1453 /* FIXME : for now we consider EAGAIN only */
1454 fd_cant_recv(fd);
1455 break;
1456 }
1457
1458 /* message too big */
1459 if (buflen > resolvers->accepted_payload_size) {
1460 ns->counters.too_big++;
1461 continue;
1462 }
1463
1464 /* initializing variables */
1465 bufend = buf + buflen; /* pointer to mark the end of the buffer */
1466
1467 /* read the query id from the packet (16 bits) */
1468 if (buf + 2 > bufend) {
1469 ns->counters.invalid++;
1470 continue;
1471 }
1472 query_id = dns_response_get_query_id(buf);
1473
1474 /* search the query_id in the pending resolution tree */
1475 eb = eb32_lookup(&resolvers->query_ids, query_id);
1476 if (eb == NULL) {
1477 /* unknown query id means an outdated response and can be safely ignored */
1478 ns->counters.outdated++;
1479 continue;
1480 }
1481
1482 /* known query id means a resolution in prgress */
1483 res = eb32_entry(eb, struct dns_resolution, qid);
1484 if (!res) {
1485 ns->counters.outdated++;
1486 continue;
1487 }
Baptiste Assmann325137d2015-04-13 23:40:55 +02001488
Christopher Faulet67957bd2017-09-27 11:00:59 +02001489 /* number of responses received */
1490 res->nb_responses++;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001491
Christopher Faulet67957bd2017-09-27 11:00:59 +02001492 max_answer_records = (resolvers->accepted_payload_size - DNS_HEADER_SIZE) / DNS_MIN_RECORD_SIZE;
1493 dns_resp = dns_validate_dns_response(buf, bufend, res, max_answer_records);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001494
Christopher Faulet67957bd2017-09-27 11:00:59 +02001495 switch (dns_resp) {
1496 case DNS_RESP_VALID:
1497 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001498
Christopher Faulet67957bd2017-09-27 11:00:59 +02001499 case DNS_RESP_INVALID:
1500 case DNS_RESP_QUERY_COUNT_ERROR:
1501 case DNS_RESP_WRONG_NAME:
1502 res->status = RSLV_STATUS_INVALID;
1503 ns->counters.invalid++;
1504 break;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001505
Christopher Faulet67957bd2017-09-27 11:00:59 +02001506 case DNS_RESP_NX_DOMAIN:
1507 res->status = RSLV_STATUS_NX;
1508 ns->counters.nx++;
1509 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001510
Christopher Faulet67957bd2017-09-27 11:00:59 +02001511 case DNS_RESP_REFUSED:
1512 res->status = RSLV_STATUS_REFUSED;
1513 ns->counters.refused++;
1514 break;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001515
Christopher Faulet67957bd2017-09-27 11:00:59 +02001516 case DNS_RESP_ANCOUNT_ZERO:
1517 res->status = RSLV_STATUS_OTHER;
1518 ns->counters.any_err++;
1519 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001520
Christopher Faulet67957bd2017-09-27 11:00:59 +02001521 case DNS_RESP_CNAME_ERROR:
1522 res->status = RSLV_STATUS_OTHER;
1523 ns->counters.cname_error++;
1524 break;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001525
Christopher Faulet67957bd2017-09-27 11:00:59 +02001526 case DNS_RESP_TRUNCATED:
1527 res->status = RSLV_STATUS_OTHER;
1528 ns->counters.truncated++;
1529 break;
Baptiste Assmanne70bc052017-08-21 16:51:09 +02001530
Christopher Faulet67957bd2017-09-27 11:00:59 +02001531 case DNS_RESP_NO_EXPECTED_RECORD:
1532 case DNS_RESP_ERROR:
1533 case DNS_RESP_INTERNAL:
1534 res->status = RSLV_STATUS_OTHER;
1535 ns->counters.other++;
1536 break;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001537 }
1538
Christopher Faulet67957bd2017-09-27 11:00:59 +02001539 /* Wait all nameservers response to handle errors */
1540 if (dns_resp != DNS_RESP_VALID && res->nb_responses < resolvers->nb_nameservers)
1541 continue;
Olivier Houchard8da5f982017-08-04 18:35:36 +02001542
Christopher Faulet67957bd2017-09-27 11:00:59 +02001543 /* Process error codes */
1544 if (dns_resp != DNS_RESP_VALID) {
1545 if (res->prefered_query_type != res->query_type) {
1546 /* The fallback on the query type was already performed,
1547 * so check the try counter. If it falls to 0, we can
1548 * report an error. Else, wait the next attempt. */
1549 if (!res->try)
1550 goto report_res_error;
1551 }
1552 else {
1553 /* Fallback from A to AAAA or the opposite and re-send
1554 * the resolution immediately. try counter is not
1555 * decremented. */
1556 if (res->prefered_query_type == DNS_RTYPE_A) {
1557 res->query_type = DNS_RTYPE_AAAA;
1558 dns_send_query(res);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001559 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001560 else if (res->prefered_query_type == DNS_RTYPE_AAAA) {
1561 res->query_type = DNS_RTYPE_A;
1562 dns_send_query(res);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001563 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001564 }
1565 continue;
1566 }
Olivier Houchard8da5f982017-08-04 18:35:36 +02001567
Christopher Faulet67957bd2017-09-27 11:00:59 +02001568 /* Now let's check the query's dname corresponds to the one we
1569 * sent. We can check only the first query of the list. We send
1570 * one query at a time so we get one query in the response */
1571 query = LIST_NEXT(&res->response.query_list, struct dns_query_item *, list);
1572 if (query && memcmp(query->name, res->hostname_dn, res->hostname_dn_len) != 0) {
1573 dns_resp = DNS_RESP_WRONG_NAME;
1574 ns->counters.other++;
1575 goto report_res_error;
1576 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001577
Christopher Faulet67957bd2017-09-27 11:00:59 +02001578 /* So the resolution succeeded */
1579 res->status = RSLV_STATUS_VALID;
1580 res->last_valid = now_ms;
1581 ns->counters.valid++;
1582 goto report_res_success;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001583
Christopher Faulet67957bd2017-09-27 11:00:59 +02001584 report_res_error:
1585 list_for_each_entry(req, &res->requesters, list)
1586 req->requester_error_cb(req, dns_resp);
1587 dns_reset_resolution(res);
1588 LIST_DEL(&res->list);
1589 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
1590 continue;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001591
Christopher Faulet67957bd2017-09-27 11:00:59 +02001592 report_res_success:
1593 /* Only the 1rst requester s managed by the server, others are
1594 * from the cache */
1595 tmpns = ns;
1596 list_for_each_entry(req, &res->requesters, list) {
1597 req->requester_cb(req, tmpns);
1598 tmpns = NULL;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001599 }
Baptiste Assmann42746372017-05-03 12:12:02 +02001600
Christopher Faulet67957bd2017-09-27 11:00:59 +02001601 dns_reset_resolution(res);
1602 LIST_DEL(&res->list);
1603 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
1604 continue;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001605 }
Baptiste Assmann325137d2015-04-13 23:40:55 +02001606 dns_update_resolvers_timeout(resolvers);
Baptiste Assmann325137d2015-04-13 23:40:55 +02001607}
William Lallemand69e96442016-11-19 00:58:54 +01001608
Christopher Faulet67957bd2017-09-27 11:00:59 +02001609/* Called when a resolvers network socket is ready to send data */
1610static void dns_resolve_send(struct dgram_conn *dgram)
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001611{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001612 struct dns_resolvers *resolvers;
1613 struct dns_nameserver *ns;
1614 struct dns_resolution *res;
1615 int fd;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001616
Christopher Faulet67957bd2017-09-27 11:00:59 +02001617 fd = dgram->t.sock.fd;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001618
Christopher Faulet67957bd2017-09-27 11:00:59 +02001619 /* check if ready for sending */
1620 if (!fd_send_ready(fd))
1621 return;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001622
Christopher Faulet67957bd2017-09-27 11:00:59 +02001623 /* we don't want/need to be waked up any more for sending */
1624 fd_stop_send(fd);
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001625
Christopher Faulet67957bd2017-09-27 11:00:59 +02001626 /* no need to go further if we can't retrieve the nameserver */
1627 if ((ns = dgram->owner) == NULL)
1628 return;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001629
Christopher Faulet67957bd2017-09-27 11:00:59 +02001630 resolvers = ns->resolvers;
1631 list_for_each_entry(res, &resolvers->resolutions.curr, list) {
1632 int ret;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001633
Christopher Faulet67957bd2017-09-27 11:00:59 +02001634 if (res->nb_queries == resolvers->nb_nameservers)
1635 continue;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001636
Christopher Faulet67957bd2017-09-27 11:00:59 +02001637 trash.len = dns_build_query(res->query_id, res->query_type,
1638 resolvers->accepted_payload_size,
1639 res->hostname_dn, res->hostname_dn_len,
1640 trash.str, trash.size);
1641 if (trash.len == -1)
1642 goto snd_error;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001643
Christopher Faulet67957bd2017-09-27 11:00:59 +02001644 ret = send(fd, trash.str, trash.len, 0);
1645 if (ret != trash.len)
1646 goto snd_error;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001647
Christopher Faulet67957bd2017-09-27 11:00:59 +02001648 ns->counters.sent++;
1649 res->nb_queries++;
1650 continue;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001651
Christopher Faulet67957bd2017-09-27 11:00:59 +02001652 snd_error:
1653 ns->counters.snd_error++;
1654 res->nb_queries++;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001655 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001656}
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001657
Christopher Faulet67957bd2017-09-27 11:00:59 +02001658/* Processes DNS resolution. First, it checks the active list to detect expired
1659 * resolutions and retry them if possible. Else a timeout is reported. Then, it
1660 * checks the wait list to trigger new resolutions.
1661 */
1662static struct task *dns_process_resolvers(struct task *t)
1663{
1664 struct dns_resolvers *resolvers = t->context;
1665 struct dns_resolution *res, *resback;
1666 int exp;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001667
Christopher Faulet67957bd2017-09-27 11:00:59 +02001668 /* Handle all expired resolutions from the active list */
1669 list_for_each_entry_safe(res, resback, &resolvers->resolutions.curr, list) {
1670 /* When we find the first resolution in the future, then we can
1671 * stop here */
1672 exp = tick_add(res->last_query, resolvers->timeout.retry);
1673 if (!tick_is_expired(exp, now_ms))
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001674 break;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001675
Christopher Faulet67957bd2017-09-27 11:00:59 +02001676 /* If current resolution has been tried too many times and
1677 * finishes in timeout we update its status and remove it from
1678 * the list */
1679 if (!res->try) {
1680 struct dns_requester *req;
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001681
Christopher Faulet67957bd2017-09-27 11:00:59 +02001682 /* Notify the result to the requesters */
1683 if (!res->nb_responses)
1684 res->status = RSLV_STATUS_TIMEOUT;
1685 list_for_each_entry(req, &res->requesters, list)
1686 req->requester_error_cb(req, res->status);
Baptiste Assmannfa4a6632017-05-04 09:05:00 +02001687
Christopher Faulet67957bd2017-09-27 11:00:59 +02001688 /* Clean up resolution info and remove it from the
1689 * current list */
1690 dns_reset_resolution(res);
1691 LIST_DEL(&res->list);
1692 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
William Lallemand69e96442016-11-19 00:58:54 +01001693
Christopher Faulet67957bd2017-09-27 11:00:59 +02001694 /* This might be triggered by too big UDP packets
1695 * dropped somewhere on the network, so lowering the
1696 * accepted_payload_size announced */
1697 if (resolvers->accepted_payload_size > 1280)
1698 resolvers->accepted_payload_size = 1280;
William Lallemand69e96442016-11-19 00:58:54 +01001699 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001700 else {
1701 /* Otherwise resend the DNS query and requeue the resolution */
1702 if (!res->nb_responses || res->prefered_query_type != res->query_type) {
1703 /* No response received (a real timeout) or fallback already done */
1704 res->query_type = res->prefered_query_type;
1705 res->try--;
1706 }
1707 else {
1708 /* Fallback from A to AAAA or the opposite and re-send
1709 * the resolution immediately. try counter is not
1710 * decremented. */
1711 if (res->prefered_query_type == DNS_RTYPE_A)
1712 res->query_type = DNS_RTYPE_AAAA;
1713 else if (res->prefered_query_type == DNS_RTYPE_AAAA)
1714 res->query_type = DNS_RTYPE_A;
1715 else
1716 res->try--;
1717 }
1718 dns_send_query(res);
William Lallemand69e96442016-11-19 00:58:54 +01001719 }
1720 }
William Lallemand69e96442016-11-19 00:58:54 +01001721
Christopher Faulet67957bd2017-09-27 11:00:59 +02001722 /* Handle all resolutions in the wait list */
1723 list_for_each_entry_safe(res, resback, &resolvers->resolutions.wait, list) {
1724 exp = tick_add(res->last_resolution, dns_resolution_timeout(res));
1725 if (tick_isset(res->last_resolution) && !tick_is_expired(exp, now_ms))
1726 continue;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001727
Christopher Faulet67957bd2017-09-27 11:00:59 +02001728 if (dns_run_resolution(res) != 1) {
1729 res->last_resolution = now_ms;
1730 LIST_DEL(&res->list);
1731 LIST_ADDQ(&resolvers->resolutions.wait, &res->list);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001732 }
1733 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001734
Christopher Faulet67957bd2017-09-27 11:00:59 +02001735 dns_update_resolvers_timeout(resolvers);
1736 return t;
1737}
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001738
Christopher Faulet67957bd2017-09-27 11:00:59 +02001739/* proto_udp callback functions for a DNS resolution */
1740struct dgram_data_cb resolve_dgram_cb = {
1741 .recv = dns_resolve_recv,
1742 .send = dns_resolve_send,
1743};
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001744
Christopher Faulet67957bd2017-09-27 11:00:59 +02001745/* Release memory allocated by DNS */
1746static void dns_deinit(void)
1747{
1748 struct dns_resolvers *resolvers, *resolversback;
1749 struct dns_nameserver *ns, *nsback;
1750 struct dns_resolution *res, *resback;
1751 struct dns_requester *req, *reqback;
1752 struct dns_srvrq *srvrq, *srvrqback;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001753
Christopher Faulet67957bd2017-09-27 11:00:59 +02001754 list_for_each_entry_safe(resolvers, resolversback, &dns_resolvers, list) {
1755 list_for_each_entry_safe(ns, nsback, &resolvers->nameservers, list) {
1756 free(ns->id);
1757 free((char *)ns->conf.file);
1758 if (ns->dgram && ns->dgram->t.sock.fd != -1)
1759 fd_delete(ns->dgram->t.sock.fd);
1760 free(ns->dgram);
1761 LIST_DEL(&ns->list);
1762 free(ns);
1763 }
1764
1765 list_for_each_entry_safe(res, resback, &resolvers->resolutions.curr, list) {
1766 list_for_each_entry_safe(req, reqback, &res->requesters, list) {
1767 LIST_DEL(&req->list);
1768 free(req);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001769 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001770 dns_free_resolution(res);
1771 }
Olivier Houchard8da5f982017-08-04 18:35:36 +02001772
Christopher Faulet67957bd2017-09-27 11:00:59 +02001773 list_for_each_entry_safe(res, resback, &resolvers->resolutions.wait, list) {
1774 list_for_each_entry_safe(req, reqback, &res->requesters, list) {
1775 LIST_DEL(&req->list);
1776 free(req);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001777 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001778 dns_free_resolution(res);
1779 }
Olivier Houchard8da5f982017-08-04 18:35:36 +02001780
Christopher Faulet67957bd2017-09-27 11:00:59 +02001781 free(resolvers->id);
1782 free((char *)resolvers->conf.file);
1783 task_delete(resolvers->t);
1784 task_free(resolvers->t);
1785 LIST_DEL(&resolvers->list);
1786 free(resolvers);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001787 }
1788
Christopher Faulet67957bd2017-09-27 11:00:59 +02001789 list_for_each_entry_safe(srvrq, srvrqback, &dns_srvrq_list, list) {
1790 free(srvrq->name);
1791 free(srvrq->hostname_dn);
1792 LIST_DEL(&srvrq->list);
1793 free(srvrq);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001794 }
1795
Christopher Faulet67957bd2017-09-27 11:00:59 +02001796 pool_destroy2(dns_answer_item_pool);
1797 pool_destroy2(dns_resolution_pool);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001798}
1799
Christopher Faulet67957bd2017-09-27 11:00:59 +02001800/* Finalizes the DNS configuration by allocating required resources and checking
1801 * live parameters.
1802 * Returns 0 on success, ERR_* flags otherwise.
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001803 */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001804static int dns_finalize_config(void)
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001805{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001806 struct dns_resolvers *resolvers;
1807 struct proxy *px;
1808 int err_code = 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001809
Christopher Faulet67957bd2017-09-27 11:00:59 +02001810 /* allocate pool of resolution per resolvers */
1811 list_for_each_entry(resolvers, &dns_resolvers, list) {
1812 struct dns_nameserver *ns;
1813 struct task *t;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001814
Christopher Faulet67957bd2017-09-27 11:00:59 +02001815 /* Check if we can create the socket with nameservers info */
1816 list_for_each_entry(ns, &resolvers->nameservers, list) {
1817 struct dgram_conn *dgram = NULL;
1818 int fd;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001819
Christopher Faulet67957bd2017-09-27 11:00:59 +02001820 /* Check nameserver info */
1821 if ((fd = socket(ns->addr.ss_family, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
1822 Alert("config : resolvers '%s': can't create socket for nameserver '%s'.\n",
1823 resolvers->id, ns->id);
1824 err_code |= (ERR_ALERT|ERR_ABORT);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001825 continue;
Olivier Houchard8da5f982017-08-04 18:35:36 +02001826 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001827 if (connect(fd, (struct sockaddr*)&ns->addr, get_addr_len(&ns->addr)) == -1) {
1828 Alert("config : resolvers '%s': can't connect socket for nameserver '%s'.\n",
1829 resolvers->id, ns->id);
1830 close(fd);
1831 err_code |= (ERR_ALERT|ERR_ABORT);
Olivier Houchard8da5f982017-08-04 18:35:36 +02001832 continue;
Olivier Houchard8da5f982017-08-04 18:35:36 +02001833 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001834 close(fd);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001835
Christopher Faulet67957bd2017-09-27 11:00:59 +02001836 /* Create dgram structure that will hold the UPD socket
1837 * and attach it on the current nameserver */
1838 if ((dgram = calloc(1, sizeof(*dgram))) == NULL) {
1839 Alert("config: resolvers '%s' : out of memory.\n",
1840 resolvers->id);
1841 err_code |= (ERR_ALERT|ERR_ABORT);
1842 goto err;
1843 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001844
Christopher Faulet67957bd2017-09-27 11:00:59 +02001845 /* Leave dgram partially initialized, no FD attached for
1846 * now. */
1847 dgram->owner = ns;
1848 dgram->data = &resolve_dgram_cb;
1849 dgram->t.sock.fd = -1;
1850 ns->dgram = dgram;
1851 }
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001852
Christopher Faulet67957bd2017-09-27 11:00:59 +02001853 /* Create the task associated to the resolvers section */
1854 if ((t = task_new()) == NULL) {
1855 Alert("config : resolvers '%s' : out of memory.\n", resolvers->id);
1856 err_code |= (ERR_ALERT|ERR_ABORT);
1857 goto err;
1858 }
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001859
Christopher Faulet67957bd2017-09-27 11:00:59 +02001860 /* Update task's parameters */
1861 t->process = dns_process_resolvers;
1862 t->context = resolvers;
1863 resolvers->t = t;
1864 task_wakeup(t, TASK_WOKEN_INIT);
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001865 }
1866
Christopher Faulet67957bd2017-09-27 11:00:59 +02001867 for (px = proxy; px; px = px->next) {
1868 struct server *srv;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001869
Christopher Faulet67957bd2017-09-27 11:00:59 +02001870 for (srv = px->srv; srv; srv = srv->next) {
1871 struct dns_resolvers *resolvers;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001872
Christopher Faulet67957bd2017-09-27 11:00:59 +02001873 if (!srv->resolvers_id)
1874 continue;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001875
Christopher Faulet67957bd2017-09-27 11:00:59 +02001876 if ((resolvers = find_resolvers_by_id(srv->resolvers_id)) == NULL) {
1877 Alert("config : %s '%s', server '%s': unable to find required resolvers '%s'\n",
1878 proxy_type_str(px), px->id, srv->id, srv->resolvers_id);
1879 err_code |= (ERR_ALERT|ERR_ABORT);
1880 continue;
1881 }
1882 srv->resolvers = resolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001883
Christopher Faulet67957bd2017-09-27 11:00:59 +02001884 if (srv->srvrq && !srv->srvrq->resolvers) {
1885 srv->srvrq->resolvers = srv->resolvers;
1886 if (dns_link_resolution(srv->srvrq, OBJ_TYPE_SRVRQ) == -1) {
1887 Alert("config : %s '%s' : unable to set DNS resolution for server '%s'.\n",
1888 proxy_type_str(px), px->id, srv->id);
1889 err_code |= (ERR_ALERT|ERR_ABORT);
1890 continue;
1891 }
1892 }
1893 if (dns_link_resolution(srv, OBJ_TYPE_SERVER) == -1) {
1894 Alert("config : %s '%s', unable to set DNS resolution for server '%s'.\n",
1895 proxy_type_str(px), px->id, srv->id);
1896 err_code |= (ERR_ALERT|ERR_ABORT);
1897 continue;
1898 }
1899 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001900 }
1901
Christopher Faulet67957bd2017-09-27 11:00:59 +02001902 if (err_code & (ERR_ALERT|ERR_ABORT))
1903 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001904
Christopher Faulet67957bd2017-09-27 11:00:59 +02001905 return err_code;
1906 err:
1907 dns_deinit();
1908 return err_code;
1909
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001910}
1911
Christopher Faulet67957bd2017-09-27 11:00:59 +02001912/* if an arg is found, it sets the resolvers section pointer into cli.p0 */
1913static int cli_parse_stat_resolvers(char **args, struct appctx *appctx, void *private)
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001914{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001915 struct dns_resolvers *presolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001916
Christopher Faulet67957bd2017-09-27 11:00:59 +02001917 if (*args[2]) {
1918 list_for_each_entry(presolvers, &dns_resolvers, list) {
1919 if (strcmp(presolvers->id, args[2]) == 0) {
1920 appctx->ctx.cli.p0 = presolvers;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001921 break;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001922 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001923 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001924 if (appctx->ctx.cli.p0 == NULL) {
1925 appctx->ctx.cli.severity = LOG_ERR;
1926 appctx->ctx.cli.msg = "Can't find that resolvers section\n";
1927 appctx->st0 = CLI_ST_PRINT;
1928 return 1;
1929 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001930 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001931 return 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001932}
1933
Christopher Faulet67957bd2017-09-27 11:00:59 +02001934/* Dumps counters from all resolvers section and associated name servers. It
1935 * returns 0 if the output buffer is full and it needs to be called again,
1936 * otherwise non-zero. It may limit itself to the resolver pointed to by
Willy Tarreau777b5602016-12-16 18:06:26 +01001937 * <cli.p0> if it's not null.
William Lallemand69e96442016-11-19 00:58:54 +01001938 */
1939static int cli_io_handler_dump_resolvers_to_buffer(struct appctx *appctx)
1940{
1941 struct stream_interface *si = appctx->owner;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001942 struct dns_resolvers *resolvers;
1943 struct dns_nameserver *ns;
William Lallemand69e96442016-11-19 00:58:54 +01001944
1945 chunk_reset(&trash);
1946
1947 switch (appctx->st2) {
1948 case STAT_ST_INIT:
1949 appctx->st2 = STAT_ST_LIST; /* let's start producing data */
1950 /* fall through */
1951
1952 case STAT_ST_LIST:
1953 if (LIST_ISEMPTY(&dns_resolvers)) {
1954 chunk_appendf(&trash, "No resolvers found\n");
1955 }
1956 else {
Christopher Faulet67957bd2017-09-27 11:00:59 +02001957 list_for_each_entry(resolvers, &dns_resolvers, list) {
1958 if (appctx->ctx.cli.p0 != NULL && appctx->ctx.cli.p0 != resolvers)
William Lallemand69e96442016-11-19 00:58:54 +01001959 continue;
1960
Christopher Faulet67957bd2017-09-27 11:00:59 +02001961 chunk_appendf(&trash, "Resolvers section %s\n", resolvers->id);
1962 list_for_each_entry(ns, &resolvers->nameservers, list) {
1963 chunk_appendf(&trash, " nameserver %s:\n", ns->id);
1964 chunk_appendf(&trash, " sent: %lld\n", ns->counters.sent);
1965 chunk_appendf(&trash, " snd_error: %lld\n", ns->counters.snd_error);
1966 chunk_appendf(&trash, " valid: %lld\n", ns->counters.valid);
1967 chunk_appendf(&trash, " update: %lld\n", ns->counters.update);
1968 chunk_appendf(&trash, " cname: %lld\n", ns->counters.cname);
1969 chunk_appendf(&trash, " cname_error: %lld\n", ns->counters.cname_error);
1970 chunk_appendf(&trash, " any_err: %lld\n", ns->counters.any_err);
1971 chunk_appendf(&trash, " nx: %lld\n", ns->counters.nx);
1972 chunk_appendf(&trash, " timeout: %lld\n", ns->counters.timeout);
1973 chunk_appendf(&trash, " refused: %lld\n", ns->counters.refused);
1974 chunk_appendf(&trash, " other: %lld\n", ns->counters.other);
1975 chunk_appendf(&trash, " invalid: %lld\n", ns->counters.invalid);
1976 chunk_appendf(&trash, " too_big: %lld\n", ns->counters.too_big);
1977 chunk_appendf(&trash, " truncated: %lld\n", ns->counters.truncated);
1978 chunk_appendf(&trash, " outdated: %lld\n", ns->counters.outdated);
William Lallemand69e96442016-11-19 00:58:54 +01001979 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02001980 chunk_appendf(&trash, "\n");
William Lallemand69e96442016-11-19 00:58:54 +01001981 }
1982 }
1983
1984 /* display response */
Willy Tarreau06d80a92017-10-19 14:32:15 +02001985 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemand69e96442016-11-19 00:58:54 +01001986 /* let's try again later from this session. We add ourselves into
1987 * this session's users so that it can remove us upon termination.
1988 */
1989 si->flags |= SI_FL_WAIT_ROOM;
1990 return 0;
1991 }
1992
1993 appctx->st2 = STAT_ST_FIN;
1994 /* fall through */
1995
1996 default:
1997 appctx->st2 = STAT_ST_FIN;
1998 return 1;
1999 }
2000}
2001
2002/* register cli keywords */
Christopher Fauletff88efb2017-10-03 16:00:57 +02002003static struct cli_kw_list cli_kws = {{ }, {
2004 { { "show", "resolvers", NULL }, "show resolvers [id]: dumps counters from all resolvers section and\n"
2005 " associated name servers",
2006 cli_parse_stat_resolvers, cli_io_handler_dump_resolvers_to_buffer },
2007 {{},}
2008 }
2009};
William Lallemand69e96442016-11-19 00:58:54 +01002010
2011
2012__attribute__((constructor))
2013static void __dns_init(void)
2014{
Christopher Faulet67957bd2017-09-27 11:00:59 +02002015 dns_answer_item_pool = create_pool("dns_answer_item", sizeof(struct dns_answer_item), MEM_F_SHARED);
2016 dns_resolution_pool = create_pool("dns_resolution", sizeof(struct dns_resolution), MEM_F_SHARED);
2017
2018 /* give a first random value to our dns query_id seed */
2019 dns_query_id_seed = random();
2020
2021 hap_register_post_check(dns_finalize_config);
2022 hap_register_post_deinit(dns_deinit);
2023
William Lallemand69e96442016-11-19 00:58:54 +01002024 cli_register_kw(&cli_kws);
2025}