Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
Emeric Brun | 3406766 | 2021-06-11 10:48:45 +0200 | [diff] [blame] | 22 | #include <import/ebistree.h> |
| 23 | |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 24 | #include <haproxy/action.h> |
| 25 | #include <haproxy/api.h> |
| 26 | #include <haproxy/cfgparse.h> |
| 27 | #include <haproxy/channel.h> |
| 28 | #include <haproxy/check.h> |
| 29 | #include <haproxy/cli.h> |
| 30 | #include <haproxy/dns.h> |
| 31 | #include <haproxy/errors.h> |
| 32 | #include <haproxy/fd.h> |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 33 | #include <haproxy/http_rules.h> |
| 34 | #include <haproxy/log.h> |
| 35 | #include <haproxy/net_helper.h> |
| 36 | #include <haproxy/protocol.h> |
| 37 | #include <haproxy/proxy.h> |
| 38 | #include <haproxy/resolvers.h> |
| 39 | #include <haproxy/ring.h> |
| 40 | #include <haproxy/sample.h> |
| 41 | #include <haproxy/server.h> |
| 42 | #include <haproxy/stats.h> |
| 43 | #include <haproxy/stream_interface.h> |
| 44 | #include <haproxy/task.h> |
| 45 | #include <haproxy/tcp_rules.h> |
| 46 | #include <haproxy/ticks.h> |
| 47 | #include <haproxy/time.h> |
Willy Tarreau | ca14dd5 | 2021-05-08 12:59:47 +0200 | [diff] [blame] | 48 | #include <haproxy/tools.h> |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 49 | #include <haproxy/vars.h> |
| 50 | |
| 51 | |
| 52 | struct list sec_resolvers = LIST_HEAD_INIT(sec_resolvers); |
| 53 | struct list resolv_srvrq_list = LIST_HEAD_INIT(resolv_srvrq_list); |
| 54 | |
| 55 | static THREAD_LOCAL uint64_t resolv_query_id_seed = 0; /* random seed */ |
| 56 | struct resolvers *curr_resolvers = NULL; |
| 57 | |
| 58 | DECLARE_STATIC_POOL(resolv_answer_item_pool, "resolv_answer_item", sizeof(struct resolv_answer_item)); |
| 59 | DECLARE_STATIC_POOL(resolv_resolution_pool, "resolv_resolution", sizeof(struct resolv_resolution)); |
| 60 | DECLARE_POOL(resolv_requester_pool, "resolv_requester", sizeof(struct resolv_requester)); |
| 61 | |
| 62 | static unsigned int resolution_uuid = 1; |
| 63 | unsigned int resolv_failed_resolutions = 0; |
| 64 | |
| 65 | enum { |
| 66 | DNS_STAT_ID, |
| 67 | DNS_STAT_PID, |
| 68 | DNS_STAT_SENT, |
| 69 | DNS_STAT_SND_ERROR, |
| 70 | DNS_STAT_VALID, |
| 71 | DNS_STAT_UPDATE, |
| 72 | DNS_STAT_CNAME, |
| 73 | DNS_STAT_CNAME_ERROR, |
| 74 | DNS_STAT_ANY_ERR, |
| 75 | DNS_STAT_NX, |
| 76 | DNS_STAT_TIMEOUT, |
| 77 | DNS_STAT_REFUSED, |
| 78 | DNS_STAT_OTHER, |
| 79 | DNS_STAT_INVALID, |
| 80 | DNS_STAT_TOO_BIG, |
| 81 | DNS_STAT_TRUNCATED, |
| 82 | DNS_STAT_OUTDATED, |
| 83 | DNS_STAT_END, |
| 84 | }; |
| 85 | |
| 86 | static struct name_desc dns_stats[] = { |
| 87 | [DNS_STAT_ID] = { .name = "id", .desc = "ID" }, |
| 88 | [DNS_STAT_PID] = { .name = "pid", .desc = "Parent ID" }, |
| 89 | [DNS_STAT_SENT] = { .name = "sent", .desc = "Sent" }, |
| 90 | [DNS_STAT_SND_ERROR] = { .name = "send_error", .desc = "Send error" }, |
| 91 | [DNS_STAT_VALID] = { .name = "valid", .desc = "Valid" }, |
| 92 | [DNS_STAT_UPDATE] = { .name = "update", .desc = "Update" }, |
| 93 | [DNS_STAT_CNAME] = { .name = "cname", .desc = "CNAME" }, |
| 94 | [DNS_STAT_CNAME_ERROR] = { .name = "cname_error", .desc = "CNAME error" }, |
| 95 | [DNS_STAT_ANY_ERR] = { .name = "any_err", .desc = "Any errors" }, |
| 96 | [DNS_STAT_NX] = { .name = "nx", .desc = "NX" }, |
| 97 | [DNS_STAT_TIMEOUT] = { .name = "timeout", .desc = "Timeout" }, |
| 98 | [DNS_STAT_REFUSED] = { .name = "refused", .desc = "Refused" }, |
| 99 | [DNS_STAT_OTHER] = { .name = "other", .desc = "Other" }, |
| 100 | [DNS_STAT_INVALID] = { .name = "invalid", .desc = "Invalid" }, |
| 101 | [DNS_STAT_TOO_BIG] = { .name = "too_big", .desc = "Too big" }, |
| 102 | [DNS_STAT_TRUNCATED] = { .name = "truncated", .desc = "Truncated" }, |
| 103 | [DNS_STAT_OUTDATED] = { .name = "outdated", .desc = "Outdated" }, |
| 104 | }; |
| 105 | |
| 106 | static struct dns_counters dns_counters; |
| 107 | |
| 108 | static void dns_fill_stats(void *d, struct field *stats) |
| 109 | { |
| 110 | struct dns_counters *counters = d; |
| 111 | stats[DNS_STAT_ID] = mkf_str(FO_CONFIG, counters->id); |
| 112 | stats[DNS_STAT_PID] = mkf_str(FO_CONFIG, counters->pid); |
| 113 | stats[DNS_STAT_SENT] = mkf_u64(FN_GAUGE, counters->sent); |
| 114 | stats[DNS_STAT_SND_ERROR] = mkf_u64(FN_GAUGE, counters->snd_error); |
| 115 | stats[DNS_STAT_VALID] = mkf_u64(FN_GAUGE, counters->valid); |
| 116 | stats[DNS_STAT_UPDATE] = mkf_u64(FN_GAUGE, counters->update); |
| 117 | stats[DNS_STAT_CNAME] = mkf_u64(FN_GAUGE, counters->cname); |
| 118 | stats[DNS_STAT_CNAME_ERROR] = mkf_u64(FN_GAUGE, counters->cname_error); |
| 119 | stats[DNS_STAT_ANY_ERR] = mkf_u64(FN_GAUGE, counters->any_err); |
| 120 | stats[DNS_STAT_NX] = mkf_u64(FN_GAUGE, counters->nx); |
| 121 | stats[DNS_STAT_TIMEOUT] = mkf_u64(FN_GAUGE, counters->timeout); |
| 122 | stats[DNS_STAT_REFUSED] = mkf_u64(FN_GAUGE, counters->refused); |
| 123 | stats[DNS_STAT_OTHER] = mkf_u64(FN_GAUGE, counters->other); |
| 124 | stats[DNS_STAT_INVALID] = mkf_u64(FN_GAUGE, counters->invalid); |
| 125 | stats[DNS_STAT_TOO_BIG] = mkf_u64(FN_GAUGE, counters->too_big); |
| 126 | stats[DNS_STAT_TRUNCATED] = mkf_u64(FN_GAUGE, counters->truncated); |
| 127 | stats[DNS_STAT_OUTDATED] = mkf_u64(FN_GAUGE, counters->outdated); |
| 128 | } |
| 129 | |
| 130 | static struct stats_module dns_stats_module = { |
| 131 | .name = "dns", |
| 132 | .domain_flags = STATS_DOMAIN_DNS << STATS_DOMAIN, |
| 133 | .fill_stats = dns_fill_stats, |
| 134 | .stats = dns_stats, |
| 135 | .stats_count = DNS_STAT_END, |
| 136 | .counters = &dns_counters, |
| 137 | .counters_size = sizeof(dns_counters), |
| 138 | .clearable = 0, |
| 139 | }; |
| 140 | |
| 141 | INITCALL1(STG_REGISTER, stats_register_module, &dns_stats_module); |
| 142 | |
| 143 | /* Returns a pointer to the resolvers matching the id <id>. NULL is returned if |
| 144 | * no match is found. |
| 145 | */ |
| 146 | struct resolvers *find_resolvers_by_id(const char *id) |
| 147 | { |
| 148 | struct resolvers *res; |
| 149 | |
| 150 | list_for_each_entry(res, &sec_resolvers, list) { |
| 151 | if (strcmp(res->id, id) == 0) |
| 152 | return res; |
| 153 | } |
| 154 | return NULL; |
| 155 | } |
| 156 | |
| 157 | /* Compare hostnames in a case-insensitive way . |
| 158 | * Returns 0 if they are the same, non-zero otherwise |
| 159 | */ |
| 160 | static __inline int resolv_hostname_cmp(const char *name1, const char *name2, int len) |
| 161 | { |
| 162 | int i; |
| 163 | |
| 164 | for (i = 0; i < len; i++) |
| 165 | if (tolower((unsigned char)name1[i]) != tolower((unsigned char)name2[i])) |
| 166 | return -1; |
| 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | /* Returns a pointer on the SRV request matching the name <name> for the proxy |
| 171 | * <px>. NULL is returned if no match is found. |
| 172 | */ |
| 173 | struct resolv_srvrq *find_srvrq_by_name(const char *name, struct proxy *px) |
| 174 | { |
| 175 | struct resolv_srvrq *srvrq; |
| 176 | |
| 177 | list_for_each_entry(srvrq, &resolv_srvrq_list, list) { |
| 178 | if (srvrq->proxy == px && strcmp(srvrq->name, name) == 0) |
| 179 | return srvrq; |
| 180 | } |
| 181 | return NULL; |
| 182 | } |
| 183 | |
| 184 | /* Allocates a new SRVRQ for the given server with the name <fqdn>. It returns |
| 185 | * NULL if an error occurred. */ |
| 186 | struct resolv_srvrq *new_resolv_srvrq(struct server *srv, char *fqdn) |
| 187 | { |
| 188 | struct proxy *px = srv->proxy; |
| 189 | struct resolv_srvrq *srvrq = NULL; |
| 190 | int fqdn_len, hostname_dn_len; |
| 191 | |
| 192 | fqdn_len = strlen(fqdn); |
| 193 | hostname_dn_len = resolv_str_to_dn_label(fqdn, fqdn_len + 1, trash.area, |
| 194 | trash.size); |
| 195 | if (hostname_dn_len == -1) { |
Amaury Denoyelle | 1112430 | 2021-06-04 18:22:08 +0200 | [diff] [blame] | 196 | ha_alert("%s '%s', server '%s': failed to parse FQDN '%s'\n", |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 197 | proxy_type_str(px), px->id, srv->id, fqdn); |
| 198 | goto err; |
| 199 | } |
| 200 | |
| 201 | if ((srvrq = calloc(1, sizeof(*srvrq))) == NULL) { |
Amaury Denoyelle | 1112430 | 2021-06-04 18:22:08 +0200 | [diff] [blame] | 202 | ha_alert("%s '%s', server '%s': out of memory\n", |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 203 | proxy_type_str(px), px->id, srv->id); |
| 204 | goto err; |
| 205 | } |
| 206 | srvrq->obj_type = OBJ_TYPE_SRVRQ; |
| 207 | srvrq->proxy = px; |
| 208 | srvrq->name = strdup(fqdn); |
| 209 | srvrq->hostname_dn = strdup(trash.area); |
| 210 | srvrq->hostname_dn_len = hostname_dn_len; |
| 211 | if (!srvrq->name || !srvrq->hostname_dn) { |
Amaury Denoyelle | 1112430 | 2021-06-04 18:22:08 +0200 | [diff] [blame] | 212 | ha_alert("%s '%s', server '%s': out of memory\n", |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 213 | proxy_type_str(px), px->id, srv->id); |
| 214 | goto err; |
| 215 | } |
Emeric Brun | 3406766 | 2021-06-11 10:48:45 +0200 | [diff] [blame] | 216 | LIST_INIT(&srvrq->attached_servers); |
| 217 | srvrq->named_servers = EB_ROOT; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 218 | LIST_APPEND(&resolv_srvrq_list, &srvrq->list); |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 219 | return srvrq; |
| 220 | |
| 221 | err: |
| 222 | if (srvrq) { |
| 223 | free(srvrq->name); |
| 224 | free(srvrq->hostname_dn); |
| 225 | free(srvrq); |
| 226 | } |
| 227 | return NULL; |
| 228 | } |
| 229 | |
| 230 | |
Baptiste Assmann | 6a8d11d | 2021-03-10 12:22:18 +0100 | [diff] [blame] | 231 | /* finds and return the SRV answer item associated to a requester (whose type is 'server'). |
| 232 | * |
| 233 | * returns NULL in case of error or not found. |
| 234 | */ |
| 235 | struct resolv_answer_item *find_srvrq_answer_record(const struct resolv_requester *requester) |
| 236 | { |
| 237 | struct resolv_resolution *res; |
| 238 | struct resolv_answer_item *item; |
| 239 | struct server *srv; |
| 240 | |
| 241 | if (!requester) |
| 242 | return NULL; |
| 243 | |
| 244 | if ((srv = objt_server(requester->owner)) == NULL) |
| 245 | return NULL; |
| 246 | /* check if the server is managed by a SRV record */ |
| 247 | if (srv->srvrq == NULL) |
| 248 | return NULL; |
| 249 | |
| 250 | res = srv->srvrq->requester->resolution; |
| 251 | /* search an ANSWER record whose target points to the server's hostname and whose port is |
| 252 | * the same as server's svc_port */ |
| 253 | list_for_each_entry(item, &res->response.answer_list, list) { |
| 254 | if (resolv_hostname_cmp(srv->hostname_dn, item->target, srv->hostname_dn_len) == 0 && |
| 255 | (srv->svc_port == item->port)) |
| 256 | return item; |
| 257 | } |
| 258 | |
| 259 | return NULL; |
| 260 | } |
| 261 | |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 262 | /* 2 bytes random generator to generate DNS query ID */ |
| 263 | static inline uint16_t resolv_rnd16(void) |
| 264 | { |
| 265 | if (!resolv_query_id_seed) |
| 266 | resolv_query_id_seed = now_ms; |
| 267 | resolv_query_id_seed ^= resolv_query_id_seed << 13; |
| 268 | resolv_query_id_seed ^= resolv_query_id_seed >> 7; |
| 269 | resolv_query_id_seed ^= resolv_query_id_seed << 17; |
| 270 | return resolv_query_id_seed; |
| 271 | } |
| 272 | |
| 273 | |
| 274 | static inline int resolv_resolution_timeout(struct resolv_resolution *res) |
| 275 | { |
| 276 | return res->resolvers->timeout.resolve; |
| 277 | } |
| 278 | |
| 279 | /* Updates a resolvers' task timeout for next wake up and queue it */ |
| 280 | static void resolv_update_resolvers_timeout(struct resolvers *resolvers) |
| 281 | { |
| 282 | struct resolv_resolution *res; |
| 283 | int next; |
| 284 | |
| 285 | next = tick_add(now_ms, resolvers->timeout.resolve); |
| 286 | if (!LIST_ISEMPTY(&resolvers->resolutions.curr)) { |
| 287 | res = LIST_NEXT(&resolvers->resolutions.curr, struct resolv_resolution *, list); |
| 288 | next = MIN(next, tick_add(res->last_query, resolvers->timeout.retry)); |
| 289 | } |
| 290 | |
| 291 | list_for_each_entry(res, &resolvers->resolutions.wait, list) |
| 292 | next = MIN(next, tick_add(res->last_resolution, resolv_resolution_timeout(res))); |
| 293 | |
| 294 | resolvers->t->expire = next; |
| 295 | task_queue(resolvers->t); |
| 296 | } |
| 297 | |
| 298 | /* Forges a DNS query. It needs the following information from the caller: |
| 299 | * - <query_id> : the DNS query id corresponding to this query |
| 300 | * - <query_type> : DNS_RTYPE_* request DNS record type (A, AAAA, ANY...) |
| 301 | * - <hostname_dn> : hostname in domain name format |
| 302 | * - <hostname_dn_len> : length of <hostname_dn> |
| 303 | * |
| 304 | * To store the query, the caller must pass a buffer <buf> and its size |
| 305 | * <bufsize>. It returns the number of written bytes in success, -1 if <buf> is |
| 306 | * too short. |
| 307 | */ |
| 308 | static int resolv_build_query(int query_id, int query_type, unsigned int accepted_payload_size, |
| 309 | char *hostname_dn, int hostname_dn_len, char *buf, int bufsize) |
| 310 | { |
| 311 | struct dns_header dns_hdr; |
| 312 | struct dns_question qinfo; |
| 313 | struct dns_additional_record edns; |
| 314 | char *p = buf; |
| 315 | |
| 316 | if (sizeof(dns_hdr) + sizeof(qinfo) + sizeof(edns) + hostname_dn_len >= bufsize) |
| 317 | return -1; |
| 318 | |
| 319 | memset(buf, 0, bufsize); |
| 320 | |
| 321 | /* Set dns query headers */ |
| 322 | dns_hdr.id = (unsigned short) htons(query_id); |
| 323 | dns_hdr.flags = htons(0x0100); /* qr=0, opcode=0, aa=0, tc=0, rd=1, ra=0, z=0, rcode=0 */ |
| 324 | dns_hdr.qdcount = htons(1); /* 1 question */ |
| 325 | dns_hdr.ancount = 0; |
| 326 | dns_hdr.nscount = 0; |
| 327 | dns_hdr.arcount = htons(1); |
| 328 | memcpy(p, &dns_hdr, sizeof(dns_hdr)); |
| 329 | p += sizeof(dns_hdr); |
| 330 | |
| 331 | /* Set up query hostname */ |
| 332 | memcpy(p, hostname_dn, hostname_dn_len); |
| 333 | p += hostname_dn_len; |
| 334 | *p++ = 0; |
| 335 | |
| 336 | /* Set up query info (type and class) */ |
| 337 | qinfo.qtype = htons(query_type); |
| 338 | qinfo.qclass = htons(DNS_RCLASS_IN); |
| 339 | memcpy(p, &qinfo, sizeof(qinfo)); |
| 340 | p += sizeof(qinfo); |
| 341 | |
| 342 | /* Set the DNS extension */ |
| 343 | edns.name = 0; |
| 344 | edns.type = htons(DNS_RTYPE_OPT); |
| 345 | edns.udp_payload_size = htons(accepted_payload_size); |
| 346 | edns.extension = 0; |
| 347 | edns.data_length = 0; |
| 348 | memcpy(p, &edns, sizeof(edns)); |
| 349 | p += sizeof(edns); |
| 350 | |
| 351 | return (p - buf); |
| 352 | } |
| 353 | |
| 354 | /* Sends a DNS query to resolvers associated to a resolution. It returns 0 on |
| 355 | * success, -1 otherwise. |
| 356 | */ |
| 357 | static int resolv_send_query(struct resolv_resolution *resolution) |
| 358 | { |
| 359 | struct resolvers *resolvers = resolution->resolvers; |
| 360 | struct dns_nameserver *ns; |
| 361 | int len; |
| 362 | |
| 363 | /* Update resolution */ |
| 364 | resolution->nb_queries = 0; |
| 365 | resolution->nb_responses = 0; |
| 366 | resolution->last_query = now_ms; |
| 367 | |
| 368 | len = resolv_build_query(resolution->query_id, resolution->query_type, |
| 369 | resolvers->accepted_payload_size, |
| 370 | resolution->hostname_dn, resolution->hostname_dn_len, |
| 371 | trash.area, trash.size); |
| 372 | |
| 373 | list_for_each_entry(ns, &resolvers->nameservers, list) { |
| 374 | if (len < 0) { |
| 375 | ns->counters->snd_error++; |
| 376 | continue; |
| 377 | } |
| 378 | |
| 379 | if (dns_send_nameserver(ns, trash.area, len) < 0) |
| 380 | ns->counters->snd_error++; |
| 381 | else |
| 382 | resolution->nb_queries++; |
| 383 | } |
| 384 | |
| 385 | /* Push the resolution at the end of the active list */ |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 386 | LIST_DELETE(&resolution->list); |
| 387 | LIST_APPEND(&resolvers->resolutions.curr, &resolution->list); |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 388 | return 0; |
| 389 | } |
| 390 | |
| 391 | /* Prepares and sends a DNS resolution. It returns 1 if the query was sent, 0 if |
| 392 | * skipped and -1 if an error occurred. |
| 393 | */ |
| 394 | static int |
| 395 | resolv_run_resolution(struct resolv_resolution *resolution) |
| 396 | { |
| 397 | struct resolvers *resolvers = resolution->resolvers; |
| 398 | int query_id, i; |
| 399 | |
| 400 | /* Avoid sending requests for resolutions that don't yet have an |
| 401 | * hostname, ie resolutions linked to servers that do not yet have an |
| 402 | * fqdn */ |
| 403 | if (!resolution->hostname_dn) |
| 404 | return 0; |
| 405 | |
| 406 | /* Check if a resolution has already been started for this server return |
| 407 | * directly to avoid resolution pill up. */ |
| 408 | if (resolution->step != RSLV_STEP_NONE) |
| 409 | return 0; |
| 410 | |
| 411 | /* Generates a new query id. We try at most 100 times to find a free |
| 412 | * query id */ |
| 413 | for (i = 0; i < 100; ++i) { |
| 414 | query_id = resolv_rnd16(); |
| 415 | if (!eb32_lookup(&resolvers->query_ids, query_id)) |
| 416 | break; |
| 417 | query_id = -1; |
| 418 | } |
| 419 | if (query_id == -1) { |
| 420 | send_log(NULL, LOG_NOTICE, |
| 421 | "could not generate a query id for %s, in resolvers %s.\n", |
| 422 | resolution->hostname_dn, resolvers->id); |
| 423 | return -1; |
| 424 | } |
| 425 | |
| 426 | /* Update resolution parameters */ |
| 427 | resolution->query_id = query_id; |
| 428 | resolution->qid.key = query_id; |
| 429 | resolution->step = RSLV_STEP_RUNNING; |
| 430 | resolution->query_type = resolution->prefered_query_type; |
| 431 | resolution->try = resolvers->resolve_retries; |
| 432 | eb32_insert(&resolvers->query_ids, &resolution->qid); |
| 433 | |
| 434 | /* Send the DNS query */ |
| 435 | resolution->try -= 1; |
| 436 | resolv_send_query(resolution); |
| 437 | return 1; |
| 438 | } |
| 439 | |
| 440 | /* Performs a name resolution for the requester <req> */ |
| 441 | void resolv_trigger_resolution(struct resolv_requester *req) |
| 442 | { |
| 443 | struct resolvers *resolvers; |
| 444 | struct resolv_resolution *res; |
| 445 | int exp; |
| 446 | |
| 447 | if (!req || !req->resolution) |
| 448 | return; |
| 449 | res = req->resolution; |
| 450 | resolvers = res->resolvers; |
| 451 | |
| 452 | /* The resolution must not be triggered yet. Use the cached response, if |
| 453 | * valid */ |
| 454 | exp = tick_add(res->last_resolution, resolvers->hold.valid); |
| 455 | if (resolvers->t && (res->status != RSLV_STATUS_VALID || |
| 456 | !tick_isset(res->last_resolution) || tick_is_expired(exp, now_ms))) |
| 457 | task_wakeup(resolvers->t, TASK_WOKEN_OTHER); |
| 458 | } |
| 459 | |
| 460 | |
| 461 | /* Resets some resolution parameters to initial values and also delete the query |
| 462 | * ID from the resolver's tree. |
| 463 | */ |
| 464 | static void resolv_reset_resolution(struct resolv_resolution *resolution) |
| 465 | { |
| 466 | /* update resolution status */ |
| 467 | resolution->step = RSLV_STEP_NONE; |
| 468 | resolution->try = 0; |
| 469 | resolution->last_resolution = now_ms; |
| 470 | resolution->nb_queries = 0; |
| 471 | resolution->nb_responses = 0; |
| 472 | resolution->query_type = resolution->prefered_query_type; |
| 473 | |
| 474 | /* clean up query id */ |
| 475 | eb32_delete(&resolution->qid); |
| 476 | resolution->query_id = 0; |
| 477 | resolution->qid.key = 0; |
| 478 | } |
| 479 | |
| 480 | /* Returns the query id contained in a DNS response */ |
| 481 | static inline unsigned short resolv_response_get_query_id(unsigned char *resp) |
| 482 | { |
| 483 | return resp[0] * 256 + resp[1]; |
| 484 | } |
| 485 | |
| 486 | |
| 487 | /* Analyses, re-builds and copies the name <name> from the DNS response packet |
| 488 | * <buffer>. <name> must point to the 'data_len' information or pointer 'c0' |
| 489 | * for compressed data. The result is copied into <dest>, ensuring we don't |
| 490 | * overflow using <dest_len> Returns the number of bytes the caller can move |
| 491 | * forward. If 0 it means an error occurred while parsing the name. <offset> is |
| 492 | * the number of bytes the caller could move forward. |
| 493 | */ |
| 494 | int resolv_read_name(unsigned char *buffer, unsigned char *bufend, |
| 495 | unsigned char *name, char *destination, int dest_len, |
| 496 | int *offset, unsigned int depth) |
| 497 | { |
| 498 | int nb_bytes = 0, n = 0; |
| 499 | int label_len; |
| 500 | unsigned char *reader = name; |
| 501 | char *dest = destination; |
| 502 | |
| 503 | while (1) { |
| 504 | if (reader >= bufend) |
| 505 | goto err; |
| 506 | |
| 507 | /* Name compression is in use */ |
| 508 | if ((*reader & 0xc0) == 0xc0) { |
| 509 | if (reader + 1 >= bufend) |
| 510 | goto err; |
| 511 | |
| 512 | /* Must point BEFORE current position */ |
| 513 | if ((buffer + reader[1]) > reader) |
| 514 | goto err; |
| 515 | |
| 516 | if (depth++ > 100) |
| 517 | goto err; |
| 518 | |
| 519 | n = resolv_read_name(buffer, bufend, buffer + (*reader & 0x3f)*256 + reader[1], |
| 520 | dest, dest_len - nb_bytes, offset, depth); |
| 521 | if (n == 0) |
| 522 | goto err; |
| 523 | |
| 524 | dest += n; |
| 525 | nb_bytes += n; |
| 526 | goto out; |
| 527 | } |
| 528 | |
| 529 | label_len = *reader; |
| 530 | if (label_len == 0) |
| 531 | goto out; |
| 532 | |
| 533 | /* Check if: |
| 534 | * - we won't read outside the buffer |
| 535 | * - there is enough place in the destination |
| 536 | */ |
| 537 | if ((reader + label_len >= bufend) || (nb_bytes + label_len >= dest_len)) |
| 538 | goto err; |
| 539 | |
| 540 | /* +1 to take label len + label string */ |
| 541 | label_len++; |
| 542 | |
| 543 | memcpy(dest, reader, label_len); |
| 544 | |
| 545 | dest += label_len; |
| 546 | nb_bytes += label_len; |
| 547 | reader += label_len; |
| 548 | } |
| 549 | |
| 550 | out: |
| 551 | /* offset computation: |
| 552 | * parse from <name> until finding either NULL or a pointer "c0xx" |
| 553 | */ |
| 554 | reader = name; |
| 555 | *offset = 0; |
| 556 | while (reader < bufend) { |
| 557 | if ((reader[0] & 0xc0) == 0xc0) { |
| 558 | *offset += 2; |
| 559 | break; |
| 560 | } |
| 561 | else if (*reader == 0) { |
| 562 | *offset += 1; |
| 563 | break; |
| 564 | } |
| 565 | *offset += 1; |
| 566 | ++reader; |
| 567 | } |
| 568 | return nb_bytes; |
| 569 | |
| 570 | err: |
| 571 | return 0; |
| 572 | } |
| 573 | |
| 574 | /* Checks for any obsolete record, also identify any SRV request, and try to |
| 575 | * find a corresponding server. |
| 576 | */ |
| 577 | static void resolv_check_response(struct resolv_resolution *res) |
| 578 | { |
| 579 | struct resolvers *resolvers = res->resolvers; |
Christopher Faulet | db31b44 | 2021-03-11 18:19:41 +0100 | [diff] [blame] | 580 | struct resolv_requester *req; |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 581 | struct resolv_answer_item *item, *itemback; |
Emeric Brun | bd78c91 | 2021-06-11 10:08:05 +0200 | [diff] [blame] | 582 | struct server *srv, *srvback; |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 583 | struct resolv_srvrq *srvrq; |
| 584 | |
| 585 | list_for_each_entry_safe(item, itemback, &res->response.answer_list, list) { |
| 586 | struct resolv_answer_item *ar_item = item->ar_item; |
| 587 | |
| 588 | /* clean up obsolete Additional record */ |
Christopher Faulet | 55c1c40 | 2021-03-11 09:36:05 +0100 | [diff] [blame] | 589 | if (ar_item && tick_is_lt(tick_add(ar_item->last_seen, resolvers->hold.obsolete), now_ms)) { |
Christopher Faulet | 3e0600f | 2021-03-10 18:38:37 +0100 | [diff] [blame] | 590 | /* Cleaning up the AR item will trigger an extra DNS resolution, except if the SRV |
| 591 | * item is also obsolete. |
| 592 | */ |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 593 | pool_free(resolv_answer_item_pool, ar_item); |
| 594 | item->ar_item = NULL; |
| 595 | } |
| 596 | |
| 597 | /* Remove obsolete items */ |
Christopher Faulet | 55c1c40 | 2021-03-11 09:36:05 +0100 | [diff] [blame] | 598 | if (tick_is_lt(tick_add(item->last_seen, resolvers->hold.obsolete), now_ms)) { |
Emeric Brun | bd78c91 | 2021-06-11 10:08:05 +0200 | [diff] [blame] | 599 | if (item->type == DNS_RTYPE_A || item->type == DNS_RTYPE_AAAA) { |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 600 | /* Remove any associated server */ |
Emeric Brun | bd78c91 | 2021-06-11 10:08:05 +0200 | [diff] [blame] | 601 | list_for_each_entry_safe(srv, srvback, &item->attached_servers, ip_rec_item) { |
| 602 | LIST_DEL_INIT(&srv->ip_rec_item); |
| 603 | } |
| 604 | } |
| 605 | else if (item->type == DNS_RTYPE_SRV) { |
Emeric Brun | 3406766 | 2021-06-11 10:48:45 +0200 | [diff] [blame] | 606 | /* Remove any associated server */ |
| 607 | list_for_each_entry_safe(srv, srvback, &item->attached_servers, srv_rec_item) { |
| 608 | resolv_unlink_resolution(srv->resolv_requester, 0); |
| 609 | HA_SPIN_LOCK(SERVER_LOCK, &srv->lock); |
| 610 | srvrq_update_srv_status(srv, 1); |
| 611 | ha_free(&srv->hostname); |
| 612 | ha_free(&srv->hostname_dn); |
| 613 | srv->hostname_dn_len = 0; |
| 614 | memset(&srv->addr, 0, sizeof(srv->addr)); |
| 615 | srv->svc_port = 0; |
| 616 | srv->flags |= SRV_F_NO_RESOLUTION; |
| 617 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
| 618 | LIST_DELETE(&srv->srv_rec_item); |
| 619 | LIST_APPEND(&srv->srvrq->attached_servers, &srv->srv_rec_item); |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 620 | } |
| 621 | } |
| 622 | |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 623 | LIST_DELETE(&item->list); |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 624 | if (item->ar_item) { |
| 625 | pool_free(resolv_answer_item_pool, item->ar_item); |
| 626 | item->ar_item = NULL; |
| 627 | } |
| 628 | pool_free(resolv_answer_item_pool, item); |
| 629 | continue; |
| 630 | } |
| 631 | |
| 632 | if (item->type != DNS_RTYPE_SRV) |
| 633 | continue; |
| 634 | |
| 635 | /* Now process SRV records */ |
Christopher Faulet | db31b44 | 2021-03-11 18:19:41 +0100 | [diff] [blame] | 636 | list_for_each_entry(req, &res->requesters, list) { |
Emeric Brun | 3406766 | 2021-06-11 10:48:45 +0200 | [diff] [blame] | 637 | struct ebpt_node *node; |
| 638 | char target[DNS_MAX_NAME_SIZE+1]; |
| 639 | |
| 640 | int i; |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 641 | if ((srvrq = objt_resolv_srvrq(req->owner)) == NULL) |
| 642 | continue; |
| 643 | |
Emeric Brun | 3406766 | 2021-06-11 10:48:45 +0200 | [diff] [blame] | 644 | /* Check if a server already uses that record */ |
| 645 | srv = NULL; |
| 646 | list_for_each_entry(srv, &item->attached_servers, srv_rec_item) { |
| 647 | if (srv->srvrq == srvrq) { |
| 648 | HA_SPIN_LOCK(SERVER_LOCK, &srv->lock); |
| 649 | goto srv_found; |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 650 | } |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 651 | } |
| 652 | |
Emeric Brun | 3406766 | 2021-06-11 10:48:45 +0200 | [diff] [blame] | 653 | |
| 654 | /* If not empty we try to match a server |
| 655 | * in server state file tree with the same hostname |
| 656 | */ |
| 657 | if (!eb_is_empty(&srvrq->named_servers)) { |
| 658 | srv = NULL; |
| 659 | |
| 660 | /* convert the key to lookup in lower case */ |
| 661 | for (i = 0 ; item->target[i] ; i++) |
| 662 | target[i] = tolower(item->target[i]); |
| 663 | |
| 664 | node = ebis_lookup(&srvrq->named_servers, target); |
| 665 | if (node) { |
| 666 | srv = ebpt_entry(node, struct server, host_dn); |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 667 | HA_SPIN_LOCK(SERVER_LOCK, &srv->lock); |
Emeric Brun | 3406766 | 2021-06-11 10:48:45 +0200 | [diff] [blame] | 668 | |
| 669 | /* an entry was found with the same hostname |
| 670 | * let check this node if the port matches |
| 671 | * and try next node if the hostname |
| 672 | * is still the same |
| 673 | */ |
| 674 | while (1) { |
| 675 | if (srv->svc_port == item->port) { |
| 676 | /* server found, we remove it from tree */ |
| 677 | ebpt_delete(node); |
| 678 | free(srv->host_dn.key); |
| 679 | goto srv_found; |
| 680 | } |
| 681 | |
| 682 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
| 683 | |
| 684 | node = ebpt_next(node); |
| 685 | if (!node) |
| 686 | break; |
| 687 | |
| 688 | srv = ebpt_entry(node, struct server, host_dn); |
| 689 | HA_SPIN_LOCK(SERVER_LOCK, &srv->lock); |
| 690 | |
| 691 | if ((item->data_len != srv->hostname_dn_len) |
| 692 | || resolv_hostname_cmp(srv->hostname_dn, item->target, item->data_len)) { |
| 693 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
| 694 | break; |
| 695 | } |
| 696 | } |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 697 | } |
| 698 | } |
Emeric Brun | 3406766 | 2021-06-11 10:48:45 +0200 | [diff] [blame] | 699 | |
| 700 | /* Pick the first server listed in srvrq (those ones don't |
| 701 | * have hostname and are free to use) |
| 702 | */ |
| 703 | srv = NULL; |
| 704 | list_for_each_entry(srv, &srvrq->attached_servers, srv_rec_item) { |
| 705 | LIST_DEL_INIT(&srv->srv_rec_item); |
| 706 | HA_SPIN_LOCK(SERVER_LOCK, &srv->lock); |
| 707 | goto srv_found; |
| 708 | } |
| 709 | srv = NULL; |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 710 | |
Emeric Brun | 3406766 | 2021-06-11 10:48:45 +0200 | [diff] [blame] | 711 | srv_found: |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 712 | /* And update this server, if found (srv is locked here) */ |
| 713 | if (srv) { |
Christopher Faulet | 3e0600f | 2021-03-10 18:38:37 +0100 | [diff] [blame] | 714 | /* re-enable DNS resolution for this server by default */ |
| 715 | srv->flags &= ~SRV_F_NO_RESOLUTION; |
| 716 | |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 717 | /* Check if an Additional Record is associated to this SRV record. |
| 718 | * Perform some sanity checks too to ensure the record can be used. |
| 719 | * If all fine, we simply pick up the IP address found and associate |
Christopher Faulet | 3e0600f | 2021-03-10 18:38:37 +0100 | [diff] [blame] | 720 | * it to the server. And DNS resolution is disabled for this server. |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 721 | */ |
| 722 | if ((item->ar_item != NULL) && |
| 723 | (item->ar_item->type == DNS_RTYPE_A || item->ar_item->type == DNS_RTYPE_AAAA)) |
Christopher Faulet | 3e0600f | 2021-03-10 18:38:37 +0100 | [diff] [blame] | 724 | { |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 725 | |
| 726 | switch (item->ar_item->type) { |
| 727 | case DNS_RTYPE_A: |
Christopher Faulet | 69beaa9 | 2021-02-16 12:07:47 +0100 | [diff] [blame] | 728 | srv_update_addr(srv, &(((struct sockaddr_in*)&item->ar_item->address)->sin_addr), AF_INET, "DNS additional record"); |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 729 | break; |
| 730 | case DNS_RTYPE_AAAA: |
Christopher Faulet | 69beaa9 | 2021-02-16 12:07:47 +0100 | [diff] [blame] | 731 | srv_update_addr(srv, &(((struct sockaddr_in6*)&item->ar_item->address)->sin6_addr), AF_INET6, "DNS additional record"); |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 732 | break; |
| 733 | } |
| 734 | |
| 735 | srv->flags |= SRV_F_NO_RESOLUTION; |
Christopher Faulet | 3e0600f | 2021-03-10 18:38:37 +0100 | [diff] [blame] | 736 | |
| 737 | /* Unlink A/AAAA resolution for this server if there is an AR item. |
| 738 | * It is usless to perform an extra resolution |
| 739 | */ |
Christopher Faulet | 0efc099 | 2021-03-11 18:09:53 +0100 | [diff] [blame] | 740 | resolv_unlink_resolution(srv->resolv_requester, 0); |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 741 | } |
| 742 | |
| 743 | if (!srv->hostname_dn) { |
| 744 | const char *msg = NULL; |
| 745 | char hostname[DNS_MAX_NAME_SIZE]; |
| 746 | |
| 747 | if (resolv_dn_label_to_str(item->target, item->data_len+1, |
| 748 | hostname, DNS_MAX_NAME_SIZE) == -1) { |
| 749 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
| 750 | continue; |
| 751 | } |
Christopher Faulet | 69beaa9 | 2021-02-16 12:07:47 +0100 | [diff] [blame] | 752 | msg = srv_update_fqdn(srv, hostname, "SRV record", 1); |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 753 | if (msg) |
| 754 | send_log(srv->proxy, LOG_NOTICE, "%s", msg); |
| 755 | } |
| 756 | |
Emeric Brun | 3406766 | 2021-06-11 10:48:45 +0200 | [diff] [blame] | 757 | if (!LIST_INLIST(&srv->srv_rec_item)) |
| 758 | LIST_APPEND(&item->attached_servers, &srv->srv_rec_item); |
| 759 | |
Christopher Faulet | 3e0600f | 2021-03-10 18:38:37 +0100 | [diff] [blame] | 760 | if (!(srv->flags & SRV_F_NO_RESOLUTION)) { |
| 761 | /* If there is no AR item responsible of the FQDN resolution, |
| 762 | * trigger a dedicated DNS resolution |
| 763 | */ |
| 764 | if (!srv->resolv_requester || !srv->resolv_requester->resolution) |
| 765 | resolv_link_resolution(srv, OBJ_TYPE_SERVER, 1); |
| 766 | } |
| 767 | |
Christopher Faulet | ab177ac | 2021-03-10 15:34:52 +0100 | [diff] [blame] | 768 | /* Update the server status */ |
Christopher Faulet | 6b117ae | 2021-03-11 18:06:23 +0100 | [diff] [blame] | 769 | srvrq_update_srv_status(srv, (srv->addr.ss_family != AF_INET && srv->addr.ss_family != AF_INET6)); |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 770 | |
| 771 | srv->svc_port = item->port; |
| 772 | srv->flags &= ~SRV_F_MAPPORTS; |
| 773 | |
| 774 | if (!srv->resolv_opts.ignore_weight) { |
| 775 | char weight[9]; |
| 776 | int ha_weight; |
| 777 | |
| 778 | /* DNS weight range if from 0 to 65535 |
| 779 | * HAProxy weight is from 0 to 256 |
| 780 | * The rule below ensures that weight 0 is well respected |
| 781 | * while allowing a "mapping" from DNS weight into HAProxy's one. |
| 782 | */ |
| 783 | ha_weight = (item->weight + 255) / 256; |
| 784 | |
| 785 | snprintf(weight, sizeof(weight), "%d", ha_weight); |
| 786 | server_parse_weight_change_request(srv, weight); |
| 787 | } |
| 788 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
| 789 | } |
| 790 | } |
| 791 | } |
| 792 | } |
| 793 | |
| 794 | /* Validates that the buffer DNS response provided in <resp> and finishing |
| 795 | * before <bufend> is valid from a DNS protocol point of view. |
| 796 | * |
| 797 | * The result is stored in <resolution>' response, buf_response, |
| 798 | * response_query_records and response_answer_records members. |
| 799 | * |
| 800 | * This function returns one of the RSLV_RESP_* code to indicate the type of |
| 801 | * error found. |
| 802 | */ |
| 803 | static int resolv_validate_dns_response(unsigned char *resp, unsigned char *bufend, |
| 804 | struct resolv_resolution *resolution, int max_answer_records) |
| 805 | { |
| 806 | unsigned char *reader; |
| 807 | char *previous_dname, tmpname[DNS_MAX_NAME_SIZE]; |
| 808 | int len, flags, offset; |
| 809 | int query_record_id; |
| 810 | int nb_saved_records; |
| 811 | struct resolv_query_item *query; |
| 812 | struct resolv_answer_item *answer_record, *tmp_record; |
| 813 | struct resolv_response *r_res; |
| 814 | int i, found = 0; |
| 815 | int cause = RSLV_RESP_ERROR; |
| 816 | |
| 817 | reader = resp; |
| 818 | len = 0; |
| 819 | previous_dname = NULL; |
| 820 | query = NULL; |
| 821 | answer_record = NULL; |
| 822 | |
| 823 | /* Initialization of response buffer and structure */ |
| 824 | r_res = &resolution->response; |
| 825 | |
| 826 | /* query id */ |
| 827 | if (reader + 2 >= bufend) |
| 828 | goto invalid_resp; |
| 829 | |
| 830 | r_res->header.id = reader[0] * 256 + reader[1]; |
| 831 | reader += 2; |
| 832 | |
| 833 | /* Flags and rcode are stored over 2 bytes |
| 834 | * First byte contains: |
| 835 | * - response flag (1 bit) |
| 836 | * - opcode (4 bits) |
| 837 | * - authoritative (1 bit) |
| 838 | * - truncated (1 bit) |
| 839 | * - recursion desired (1 bit) |
| 840 | */ |
| 841 | if (reader + 2 >= bufend) |
| 842 | goto invalid_resp; |
| 843 | |
| 844 | flags = reader[0] * 256 + reader[1]; |
| 845 | |
| 846 | if ((flags & DNS_FLAG_REPLYCODE) != DNS_RCODE_NO_ERROR) { |
| 847 | if ((flags & DNS_FLAG_REPLYCODE) == DNS_RCODE_NX_DOMAIN) { |
| 848 | cause = RSLV_RESP_NX_DOMAIN; |
| 849 | goto return_error; |
| 850 | } |
| 851 | else if ((flags & DNS_FLAG_REPLYCODE) == DNS_RCODE_REFUSED) { |
| 852 | cause = RSLV_RESP_REFUSED; |
| 853 | goto return_error; |
| 854 | } |
| 855 | else { |
| 856 | cause = RSLV_RESP_ERROR; |
| 857 | goto return_error; |
| 858 | } |
| 859 | } |
| 860 | |
| 861 | /* Move forward 2 bytes for flags */ |
| 862 | reader += 2; |
| 863 | |
| 864 | /* 2 bytes for question count */ |
| 865 | if (reader + 2 >= bufend) |
| 866 | goto invalid_resp; |
| 867 | r_res->header.qdcount = reader[0] * 256 + reader[1]; |
| 868 | /* (for now) we send one query only, so we expect only one in the |
| 869 | * response too */ |
| 870 | if (r_res->header.qdcount != 1) { |
| 871 | cause = RSLV_RESP_QUERY_COUNT_ERROR; |
| 872 | goto return_error; |
| 873 | } |
| 874 | |
| 875 | if (r_res->header.qdcount > DNS_MAX_QUERY_RECORDS) |
| 876 | goto invalid_resp; |
| 877 | reader += 2; |
| 878 | |
| 879 | /* 2 bytes for answer count */ |
| 880 | if (reader + 2 >= bufend) |
| 881 | goto invalid_resp; |
| 882 | r_res->header.ancount = reader[0] * 256 + reader[1]; |
| 883 | if (r_res->header.ancount == 0) { |
| 884 | cause = RSLV_RESP_ANCOUNT_ZERO; |
| 885 | goto return_error; |
| 886 | } |
| 887 | |
| 888 | /* Check if too many records are announced */ |
| 889 | if (r_res->header.ancount > max_answer_records) |
| 890 | goto invalid_resp; |
| 891 | reader += 2; |
| 892 | |
| 893 | /* 2 bytes authority count */ |
| 894 | if (reader + 2 >= bufend) |
| 895 | goto invalid_resp; |
| 896 | r_res->header.nscount = reader[0] * 256 + reader[1]; |
| 897 | reader += 2; |
| 898 | |
| 899 | /* 2 bytes additional count */ |
| 900 | if (reader + 2 >= bufend) |
| 901 | goto invalid_resp; |
| 902 | r_res->header.arcount = reader[0] * 256 + reader[1]; |
| 903 | reader += 2; |
| 904 | |
| 905 | /* Parsing dns queries */ |
| 906 | LIST_INIT(&r_res->query_list); |
| 907 | for (query_record_id = 0; query_record_id < r_res->header.qdcount; query_record_id++) { |
| 908 | /* Use next pre-allocated resolv_query_item after ensuring there is |
| 909 | * still one available. |
| 910 | * It's then added to our packet query list. */ |
| 911 | if (query_record_id > DNS_MAX_QUERY_RECORDS) |
| 912 | goto invalid_resp; |
| 913 | query = &resolution->response_query_records[query_record_id]; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 914 | LIST_APPEND(&r_res->query_list, &query->list); |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 915 | |
| 916 | /* Name is a NULL terminated string in our case, since we have |
| 917 | * one query per response and the first one can't be compressed |
| 918 | * (using the 0x0c format) */ |
| 919 | offset = 0; |
| 920 | len = resolv_read_name(resp, bufend, reader, query->name, DNS_MAX_NAME_SIZE, &offset, 0); |
| 921 | |
| 922 | if (len == 0) |
| 923 | goto invalid_resp; |
| 924 | |
| 925 | reader += offset; |
| 926 | previous_dname = query->name; |
| 927 | |
| 928 | /* move forward 2 bytes for question type */ |
| 929 | if (reader + 2 >= bufend) |
| 930 | goto invalid_resp; |
| 931 | query->type = reader[0] * 256 + reader[1]; |
| 932 | reader += 2; |
| 933 | |
| 934 | /* move forward 2 bytes for question class */ |
| 935 | if (reader + 2 >= bufend) |
| 936 | goto invalid_resp; |
| 937 | query->class = reader[0] * 256 + reader[1]; |
| 938 | reader += 2; |
| 939 | } |
| 940 | |
| 941 | /* TRUNCATED flag must be checked after we could read the query type |
| 942 | * because a TRUNCATED SRV query type response can still be exploited */ |
| 943 | if (query->type != DNS_RTYPE_SRV && flags & DNS_FLAG_TRUNCATED) { |
| 944 | cause = RSLV_RESP_TRUNCATED; |
| 945 | goto return_error; |
| 946 | } |
| 947 | |
| 948 | /* now parsing response records */ |
| 949 | nb_saved_records = 0; |
| 950 | for (i = 0; i < r_res->header.ancount; i++) { |
| 951 | if (reader >= bufend) |
| 952 | goto invalid_resp; |
| 953 | |
| 954 | answer_record = pool_alloc(resolv_answer_item_pool); |
| 955 | if (answer_record == NULL) |
| 956 | goto invalid_resp; |
| 957 | |
| 958 | /* initialization */ |
| 959 | answer_record->ar_item = NULL; |
Christopher Faulet | 55c1c40 | 2021-03-11 09:36:05 +0100 | [diff] [blame] | 960 | answer_record->last_seen = TICK_ETERNITY; |
Emeric Brun | bd78c91 | 2021-06-11 10:08:05 +0200 | [diff] [blame] | 961 | LIST_INIT(&answer_record->attached_servers); |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 962 | |
| 963 | offset = 0; |
| 964 | len = resolv_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset, 0); |
| 965 | |
| 966 | if (len == 0) |
| 967 | goto invalid_resp; |
| 968 | |
| 969 | /* Check if the current record dname is valid. previous_dname |
| 970 | * points either to queried dname or last CNAME target */ |
| 971 | if (query->type != DNS_RTYPE_SRV && resolv_hostname_cmp(previous_dname, tmpname, len) != 0) { |
| 972 | if (i == 0) { |
| 973 | /* First record, means a mismatch issue between |
| 974 | * queried dname and dname found in the first |
| 975 | * record */ |
| 976 | goto invalid_resp; |
| 977 | } |
| 978 | else { |
| 979 | /* If not the first record, this means we have a |
| 980 | * CNAME resolution error. |
| 981 | */ |
| 982 | cause = RSLV_RESP_CNAME_ERROR; |
| 983 | goto return_error; |
| 984 | } |
| 985 | |
| 986 | } |
| 987 | |
| 988 | memcpy(answer_record->name, tmpname, len); |
| 989 | answer_record->name[len] = 0; |
| 990 | |
| 991 | reader += offset; |
| 992 | if (reader >= bufend) |
| 993 | goto invalid_resp; |
| 994 | |
| 995 | /* 2 bytes for record type (A, AAAA, CNAME, etc...) */ |
| 996 | if (reader + 2 > bufend) |
| 997 | goto invalid_resp; |
| 998 | |
| 999 | answer_record->type = reader[0] * 256 + reader[1]; |
| 1000 | reader += 2; |
| 1001 | |
| 1002 | /* 2 bytes for class (2) */ |
| 1003 | if (reader + 2 > bufend) |
| 1004 | goto invalid_resp; |
| 1005 | |
| 1006 | answer_record->class = reader[0] * 256 + reader[1]; |
| 1007 | reader += 2; |
| 1008 | |
| 1009 | /* 4 bytes for ttl (4) */ |
| 1010 | if (reader + 4 > bufend) |
| 1011 | goto invalid_resp; |
| 1012 | |
| 1013 | answer_record->ttl = reader[0] * 16777216 + reader[1] * 65536 |
| 1014 | + reader[2] * 256 + reader[3]; |
| 1015 | reader += 4; |
| 1016 | |
| 1017 | /* Now reading data len */ |
| 1018 | if (reader + 2 > bufend) |
| 1019 | goto invalid_resp; |
| 1020 | |
| 1021 | answer_record->data_len = reader[0] * 256 + reader[1]; |
| 1022 | |
| 1023 | /* Move forward 2 bytes for data len */ |
| 1024 | reader += 2; |
| 1025 | |
| 1026 | if (reader + answer_record->data_len > bufend) |
| 1027 | goto invalid_resp; |
| 1028 | |
| 1029 | /* Analyzing record content */ |
| 1030 | switch (answer_record->type) { |
| 1031 | case DNS_RTYPE_A: |
| 1032 | /* ipv4 is stored on 4 bytes */ |
| 1033 | if (answer_record->data_len != 4) |
| 1034 | goto invalid_resp; |
| 1035 | |
| 1036 | answer_record->address.sa_family = AF_INET; |
| 1037 | memcpy(&(((struct sockaddr_in *)&answer_record->address)->sin_addr), |
| 1038 | reader, answer_record->data_len); |
| 1039 | break; |
| 1040 | |
| 1041 | case DNS_RTYPE_CNAME: |
| 1042 | /* Check if this is the last record and update the caller about the status: |
| 1043 | * no IP could be found and last record was a CNAME. Could be triggered |
| 1044 | * by a wrong query type |
| 1045 | * |
| 1046 | * + 1 because answer_record_id starts at 0 |
| 1047 | * while number of answers is an integer and |
| 1048 | * starts at 1. |
| 1049 | */ |
| 1050 | if (i + 1 == r_res->header.ancount) { |
| 1051 | cause = RSLV_RESP_CNAME_ERROR; |
| 1052 | goto return_error; |
| 1053 | } |
| 1054 | |
| 1055 | offset = 0; |
| 1056 | len = resolv_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset, 0); |
| 1057 | if (len == 0) |
| 1058 | goto invalid_resp; |
| 1059 | |
| 1060 | memcpy(answer_record->target, tmpname, len); |
| 1061 | answer_record->target[len] = 0; |
| 1062 | previous_dname = answer_record->target; |
| 1063 | break; |
| 1064 | |
| 1065 | |
| 1066 | case DNS_RTYPE_SRV: |
| 1067 | /* Answer must contain : |
| 1068 | * - 2 bytes for the priority |
| 1069 | * - 2 bytes for the weight |
| 1070 | * - 2 bytes for the port |
| 1071 | * - the target hostname |
| 1072 | */ |
| 1073 | if (answer_record->data_len <= 6) |
| 1074 | goto invalid_resp; |
| 1075 | |
| 1076 | answer_record->priority = read_n16(reader); |
| 1077 | reader += sizeof(uint16_t); |
| 1078 | answer_record->weight = read_n16(reader); |
| 1079 | reader += sizeof(uint16_t); |
| 1080 | answer_record->port = read_n16(reader); |
| 1081 | reader += sizeof(uint16_t); |
| 1082 | offset = 0; |
| 1083 | len = resolv_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset, 0); |
| 1084 | if (len == 0) |
| 1085 | goto invalid_resp; |
| 1086 | |
| 1087 | answer_record->data_len = len; |
| 1088 | memcpy(answer_record->target, tmpname, len); |
| 1089 | answer_record->target[len] = 0; |
| 1090 | if (answer_record->ar_item != NULL) { |
| 1091 | pool_free(resolv_answer_item_pool, answer_record->ar_item); |
| 1092 | answer_record->ar_item = NULL; |
| 1093 | } |
| 1094 | break; |
| 1095 | |
| 1096 | case DNS_RTYPE_AAAA: |
| 1097 | /* ipv6 is stored on 16 bytes */ |
| 1098 | if (answer_record->data_len != 16) |
| 1099 | goto invalid_resp; |
| 1100 | |
| 1101 | answer_record->address.sa_family = AF_INET6; |
| 1102 | memcpy(&(((struct sockaddr_in6 *)&answer_record->address)->sin6_addr), |
| 1103 | reader, answer_record->data_len); |
| 1104 | break; |
| 1105 | |
| 1106 | } /* switch (record type) */ |
| 1107 | |
| 1108 | /* Increment the counter for number of records saved into our |
| 1109 | * local response */ |
| 1110 | nb_saved_records++; |
| 1111 | |
| 1112 | /* Move forward answer_record->data_len for analyzing next |
| 1113 | * record in the response */ |
| 1114 | reader += ((answer_record->type == DNS_RTYPE_SRV) |
| 1115 | ? offset |
| 1116 | : answer_record->data_len); |
| 1117 | |
| 1118 | /* Lookup to see if we already had this entry */ |
| 1119 | found = 0; |
| 1120 | list_for_each_entry(tmp_record, &r_res->answer_list, list) { |
| 1121 | if (tmp_record->type != answer_record->type) |
| 1122 | continue; |
| 1123 | |
| 1124 | switch(tmp_record->type) { |
| 1125 | case DNS_RTYPE_A: |
| 1126 | if (!memcmp(&((struct sockaddr_in *)&answer_record->address)->sin_addr, |
| 1127 | &((struct sockaddr_in *)&tmp_record->address)->sin_addr, |
| 1128 | sizeof(in_addr_t))) |
| 1129 | found = 1; |
| 1130 | break; |
| 1131 | |
| 1132 | case DNS_RTYPE_AAAA: |
| 1133 | if (!memcmp(&((struct sockaddr_in6 *)&answer_record->address)->sin6_addr, |
| 1134 | &((struct sockaddr_in6 *)&tmp_record->address)->sin6_addr, |
| 1135 | sizeof(struct in6_addr))) |
| 1136 | found = 1; |
| 1137 | break; |
| 1138 | |
| 1139 | case DNS_RTYPE_SRV: |
| 1140 | if (answer_record->data_len == tmp_record->data_len && |
| 1141 | !resolv_hostname_cmp(answer_record->target, tmp_record->target, answer_record->data_len) && |
| 1142 | answer_record->port == tmp_record->port) { |
| 1143 | tmp_record->weight = answer_record->weight; |
| 1144 | found = 1; |
| 1145 | } |
| 1146 | break; |
| 1147 | |
| 1148 | default: |
| 1149 | break; |
| 1150 | } |
| 1151 | |
| 1152 | if (found == 1) |
| 1153 | break; |
| 1154 | } |
| 1155 | |
| 1156 | if (found == 1) { |
Christopher Faulet | 55c1c40 | 2021-03-11 09:36:05 +0100 | [diff] [blame] | 1157 | tmp_record->last_seen = now_ms; |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 1158 | pool_free(resolv_answer_item_pool, answer_record); |
| 1159 | answer_record = NULL; |
| 1160 | } |
| 1161 | else { |
Christopher Faulet | 55c1c40 | 2021-03-11 09:36:05 +0100 | [diff] [blame] | 1162 | answer_record->last_seen = now_ms; |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 1163 | answer_record->ar_item = NULL; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1164 | LIST_APPEND(&r_res->answer_list, &answer_record->list); |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 1165 | answer_record = NULL; |
| 1166 | } |
| 1167 | } /* for i 0 to ancount */ |
| 1168 | |
| 1169 | /* Save the number of records we really own */ |
| 1170 | r_res->header.ancount = nb_saved_records; |
| 1171 | |
| 1172 | /* now parsing additional records for SRV queries only */ |
| 1173 | if (query->type != DNS_RTYPE_SRV) |
| 1174 | goto skip_parsing_additional_records; |
| 1175 | |
| 1176 | /* if we find Authority records, just skip them */ |
| 1177 | for (i = 0; i < r_res->header.nscount; i++) { |
| 1178 | offset = 0; |
| 1179 | len = resolv_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, |
| 1180 | &offset, 0); |
| 1181 | if (len == 0) |
| 1182 | continue; |
| 1183 | |
| 1184 | if (reader + offset + 10 >= bufend) |
| 1185 | goto invalid_resp; |
| 1186 | |
| 1187 | reader += offset; |
| 1188 | /* skip 2 bytes for class */ |
| 1189 | reader += 2; |
| 1190 | /* skip 2 bytes for type */ |
| 1191 | reader += 2; |
| 1192 | /* skip 4 bytes for ttl */ |
| 1193 | reader += 4; |
| 1194 | /* read data len */ |
| 1195 | len = reader[0] * 256 + reader[1]; |
| 1196 | reader += 2; |
| 1197 | |
| 1198 | if (reader + len >= bufend) |
| 1199 | goto invalid_resp; |
| 1200 | |
| 1201 | reader += len; |
| 1202 | } |
| 1203 | |
| 1204 | nb_saved_records = 0; |
| 1205 | for (i = 0; i < r_res->header.arcount; i++) { |
| 1206 | if (reader >= bufend) |
| 1207 | goto invalid_resp; |
| 1208 | |
| 1209 | answer_record = pool_alloc(resolv_answer_item_pool); |
| 1210 | if (answer_record == NULL) |
| 1211 | goto invalid_resp; |
Christopher Faulet | 55c1c40 | 2021-03-11 09:36:05 +0100 | [diff] [blame] | 1212 | answer_record->last_seen = TICK_ETERNITY; |
Emeric Brun | bd78c91 | 2021-06-11 10:08:05 +0200 | [diff] [blame] | 1213 | LIST_INIT(&answer_record->attached_servers); |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 1214 | |
| 1215 | offset = 0; |
| 1216 | len = resolv_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset, 0); |
| 1217 | |
| 1218 | if (len == 0) { |
| 1219 | pool_free(resolv_answer_item_pool, answer_record); |
| 1220 | answer_record = NULL; |
| 1221 | continue; |
| 1222 | } |
| 1223 | |
| 1224 | memcpy(answer_record->name, tmpname, len); |
| 1225 | answer_record->name[len] = 0; |
| 1226 | |
| 1227 | reader += offset; |
| 1228 | if (reader >= bufend) |
| 1229 | goto invalid_resp; |
| 1230 | |
| 1231 | /* 2 bytes for record type (A, AAAA, CNAME, etc...) */ |
| 1232 | if (reader + 2 > bufend) |
| 1233 | goto invalid_resp; |
| 1234 | |
| 1235 | answer_record->type = reader[0] * 256 + reader[1]; |
| 1236 | reader += 2; |
| 1237 | |
| 1238 | /* 2 bytes for class (2) */ |
| 1239 | if (reader + 2 > bufend) |
| 1240 | goto invalid_resp; |
| 1241 | |
| 1242 | answer_record->class = reader[0] * 256 + reader[1]; |
| 1243 | reader += 2; |
| 1244 | |
| 1245 | /* 4 bytes for ttl (4) */ |
| 1246 | if (reader + 4 > bufend) |
| 1247 | goto invalid_resp; |
| 1248 | |
| 1249 | answer_record->ttl = reader[0] * 16777216 + reader[1] * 65536 |
| 1250 | + reader[2] * 256 + reader[3]; |
| 1251 | reader += 4; |
| 1252 | |
| 1253 | /* Now reading data len */ |
| 1254 | if (reader + 2 > bufend) |
| 1255 | goto invalid_resp; |
| 1256 | |
| 1257 | answer_record->data_len = reader[0] * 256 + reader[1]; |
| 1258 | |
| 1259 | /* Move forward 2 bytes for data len */ |
| 1260 | reader += 2; |
| 1261 | |
| 1262 | if (reader + answer_record->data_len > bufend) |
| 1263 | goto invalid_resp; |
| 1264 | |
| 1265 | /* Analyzing record content */ |
| 1266 | switch (answer_record->type) { |
| 1267 | case DNS_RTYPE_A: |
| 1268 | /* ipv4 is stored on 4 bytes */ |
| 1269 | if (answer_record->data_len != 4) |
| 1270 | goto invalid_resp; |
| 1271 | |
| 1272 | answer_record->address.sa_family = AF_INET; |
| 1273 | memcpy(&(((struct sockaddr_in *)&answer_record->address)->sin_addr), |
| 1274 | reader, answer_record->data_len); |
| 1275 | break; |
| 1276 | |
| 1277 | case DNS_RTYPE_AAAA: |
| 1278 | /* ipv6 is stored on 16 bytes */ |
| 1279 | if (answer_record->data_len != 16) |
| 1280 | goto invalid_resp; |
| 1281 | |
| 1282 | answer_record->address.sa_family = AF_INET6; |
| 1283 | memcpy(&(((struct sockaddr_in6 *)&answer_record->address)->sin6_addr), |
| 1284 | reader, answer_record->data_len); |
| 1285 | break; |
| 1286 | |
| 1287 | default: |
| 1288 | pool_free(resolv_answer_item_pool, answer_record); |
| 1289 | answer_record = NULL; |
| 1290 | continue; |
| 1291 | |
| 1292 | } /* switch (record type) */ |
| 1293 | |
| 1294 | /* Increment the counter for number of records saved into our |
| 1295 | * local response */ |
| 1296 | nb_saved_records++; |
| 1297 | |
| 1298 | /* Move forward answer_record->data_len for analyzing next |
| 1299 | * record in the response */ |
Christopher Faulet | 77f8606 | 2021-03-10 15:19:57 +0100 | [diff] [blame] | 1300 | reader += answer_record->data_len; |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 1301 | |
| 1302 | /* Lookup to see if we already had this entry */ |
| 1303 | found = 0; |
| 1304 | list_for_each_entry(tmp_record, &r_res->answer_list, list) { |
Christopher Faulet | 77f8606 | 2021-03-10 15:19:57 +0100 | [diff] [blame] | 1305 | struct resolv_answer_item *ar_item; |
| 1306 | |
| 1307 | if (tmp_record->type != DNS_RTYPE_SRV || !tmp_record->ar_item) |
| 1308 | continue; |
| 1309 | |
| 1310 | ar_item = tmp_record->ar_item; |
Christopher Faulet | e8674c7 | 2021-03-12 16:42:45 +0100 | [diff] [blame] | 1311 | if (ar_item->type != answer_record->type || ar_item->last_seen == now_ms || |
Christopher Faulet | 77f8606 | 2021-03-10 15:19:57 +0100 | [diff] [blame] | 1312 | len != tmp_record->data_len || |
| 1313 | resolv_hostname_cmp(answer_record->name, tmp_record->target, tmp_record->data_len)) |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 1314 | continue; |
| 1315 | |
Christopher Faulet | 77f8606 | 2021-03-10 15:19:57 +0100 | [diff] [blame] | 1316 | switch(ar_item->type) { |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 1317 | case DNS_RTYPE_A: |
| 1318 | if (!memcmp(&((struct sockaddr_in *)&answer_record->address)->sin_addr, |
Christopher Faulet | 77f8606 | 2021-03-10 15:19:57 +0100 | [diff] [blame] | 1319 | &((struct sockaddr_in *)&ar_item->address)->sin_addr, |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 1320 | sizeof(in_addr_t))) |
| 1321 | found = 1; |
| 1322 | break; |
| 1323 | |
| 1324 | case DNS_RTYPE_AAAA: |
| 1325 | if (!memcmp(&((struct sockaddr_in6 *)&answer_record->address)->sin6_addr, |
Christopher Faulet | 77f8606 | 2021-03-10 15:19:57 +0100 | [diff] [blame] | 1326 | &((struct sockaddr_in6 *)&ar_item->address)->sin6_addr, |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 1327 | sizeof(struct in6_addr))) |
| 1328 | found = 1; |
| 1329 | break; |
| 1330 | |
| 1331 | default: |
| 1332 | break; |
| 1333 | } |
| 1334 | |
| 1335 | if (found == 1) |
| 1336 | break; |
| 1337 | } |
| 1338 | |
| 1339 | if (found == 1) { |
Christopher Faulet | 55c1c40 | 2021-03-11 09:36:05 +0100 | [diff] [blame] | 1340 | tmp_record->ar_item->last_seen = now_ms; |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 1341 | pool_free(resolv_answer_item_pool, answer_record); |
| 1342 | answer_record = NULL; |
| 1343 | } |
| 1344 | else { |
Christopher Faulet | 55c1c40 | 2021-03-11 09:36:05 +0100 | [diff] [blame] | 1345 | answer_record->last_seen = now_ms; |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 1346 | answer_record->ar_item = NULL; |
| 1347 | |
| 1348 | // looking for the SRV record in the response list linked to this additional record |
| 1349 | list_for_each_entry(tmp_record, &r_res->answer_list, list) { |
| 1350 | if (tmp_record->type == DNS_RTYPE_SRV && |
| 1351 | tmp_record->ar_item == NULL && |
| 1352 | !resolv_hostname_cmp(tmp_record->target, answer_record->name, tmp_record->data_len)) { |
| 1353 | /* Always use the received additional record to refresh info */ |
| 1354 | if (tmp_record->ar_item) |
| 1355 | pool_free(resolv_answer_item_pool, tmp_record->ar_item); |
| 1356 | tmp_record->ar_item = answer_record; |
Christopher Faulet | 9c246a4 | 2021-02-23 11:59:19 +0100 | [diff] [blame] | 1357 | answer_record = NULL; |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 1358 | break; |
| 1359 | } |
| 1360 | } |
Christopher Faulet | 9c246a4 | 2021-02-23 11:59:19 +0100 | [diff] [blame] | 1361 | if (answer_record) { |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 1362 | pool_free(resolv_answer_item_pool, answer_record); |
Christopher Faulet | 9c246a4 | 2021-02-23 11:59:19 +0100 | [diff] [blame] | 1363 | answer_record = NULL; |
| 1364 | } |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 1365 | } |
| 1366 | } /* for i 0 to arcount */ |
| 1367 | |
| 1368 | skip_parsing_additional_records: |
| 1369 | |
| 1370 | /* Save the number of records we really own */ |
| 1371 | r_res->header.arcount = nb_saved_records; |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 1372 | resolv_check_response(resolution); |
| 1373 | return RSLV_RESP_VALID; |
| 1374 | |
| 1375 | invalid_resp: |
| 1376 | cause = RSLV_RESP_INVALID; |
| 1377 | |
| 1378 | return_error: |
| 1379 | pool_free(resolv_answer_item_pool, answer_record); |
| 1380 | return cause; |
| 1381 | } |
| 1382 | |
| 1383 | /* Searches dn_name resolution in resp. |
| 1384 | * If existing IP not found, return the first IP matching family_priority, |
| 1385 | * otherwise, first ip found |
| 1386 | * The following tasks are the responsibility of the caller: |
| 1387 | * - <r_res> contains an error free DNS response |
| 1388 | * For both cases above, resolv_validate_dns_response is required |
| 1389 | * returns one of the RSLV_UPD_* code |
| 1390 | */ |
| 1391 | int resolv_get_ip_from_response(struct resolv_response *r_res, |
| 1392 | struct resolv_options *resolv_opts, void *currentip, |
| 1393 | short currentip_sin_family, |
| 1394 | void **newip, short *newip_sin_family, |
Emeric Brun | bd78c91 | 2021-06-11 10:08:05 +0200 | [diff] [blame] | 1395 | struct server *owner) |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 1396 | { |
Emeric Brun | bd78c91 | 2021-06-11 10:08:05 +0200 | [diff] [blame] | 1397 | struct resolv_answer_item *record, *found_record = NULL; |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 1398 | int family_priority; |
| 1399 | int currentip_found; |
| 1400 | unsigned char *newip4, *newip6; |
| 1401 | int currentip_sel; |
| 1402 | int j; |
| 1403 | int score, max_score; |
| 1404 | int allowed_duplicated_ip; |
| 1405 | |
Emeric Brun | bd78c91 | 2021-06-11 10:08:05 +0200 | [diff] [blame] | 1406 | /* srv is linked to an alive ip record */ |
| 1407 | if (owner && LIST_INLIST(&owner->ip_rec_item)) |
| 1408 | return RSLV_UPD_NO; |
| 1409 | |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 1410 | family_priority = resolv_opts->family_prio; |
| 1411 | allowed_duplicated_ip = resolv_opts->accept_duplicate_ip; |
| 1412 | *newip = newip4 = newip6 = NULL; |
| 1413 | currentip_found = 0; |
| 1414 | *newip_sin_family = AF_UNSPEC; |
| 1415 | max_score = -1; |
| 1416 | |
| 1417 | /* Select an IP regarding configuration preference. |
| 1418 | * Top priority is the preferred network ip version, |
| 1419 | * second priority is the preferred network. |
| 1420 | * the last priority is the currently used IP, |
| 1421 | * |
| 1422 | * For these three priorities, a score is calculated. The |
| 1423 | * weight are: |
| 1424 | * 8 - preferred ip version. |
| 1425 | * 4 - preferred network. |
| 1426 | * 2 - if the ip in the record is not affected to any other server in the same backend (duplication) |
| 1427 | * 1 - current ip. |
| 1428 | * The result with the biggest score is returned. |
| 1429 | */ |
| 1430 | |
| 1431 | list_for_each_entry(record, &r_res->answer_list, list) { |
| 1432 | void *ip; |
| 1433 | unsigned char ip_type; |
| 1434 | |
| 1435 | if (record->type == DNS_RTYPE_A) { |
| 1436 | ip = &(((struct sockaddr_in *)&record->address)->sin_addr); |
| 1437 | ip_type = AF_INET; |
| 1438 | } |
| 1439 | else if (record->type == DNS_RTYPE_AAAA) { |
| 1440 | ip_type = AF_INET6; |
| 1441 | ip = &(((struct sockaddr_in6 *)&record->address)->sin6_addr); |
| 1442 | } |
| 1443 | else |
| 1444 | continue; |
| 1445 | score = 0; |
| 1446 | |
| 1447 | /* Check for preferred ip protocol. */ |
| 1448 | if (ip_type == family_priority) |
| 1449 | score += 8; |
| 1450 | |
| 1451 | /* Check for preferred network. */ |
| 1452 | for (j = 0; j < resolv_opts->pref_net_nb; j++) { |
| 1453 | |
| 1454 | /* Compare only the same addresses class. */ |
| 1455 | if (resolv_opts->pref_net[j].family != ip_type) |
| 1456 | continue; |
| 1457 | |
| 1458 | if ((ip_type == AF_INET && |
| 1459 | in_net_ipv4(ip, |
| 1460 | &resolv_opts->pref_net[j].mask.in4, |
| 1461 | &resolv_opts->pref_net[j].addr.in4)) || |
| 1462 | (ip_type == AF_INET6 && |
| 1463 | in_net_ipv6(ip, |
| 1464 | &resolv_opts->pref_net[j].mask.in6, |
| 1465 | &resolv_opts->pref_net[j].addr.in6))) { |
| 1466 | score += 4; |
| 1467 | break; |
| 1468 | } |
| 1469 | } |
| 1470 | |
| 1471 | /* Check if the IP found in the record is already affected to a |
| 1472 | * member of a group. If not, the score should be incremented |
| 1473 | * by 2. */ |
Emeric Brun | bd78c91 | 2021-06-11 10:08:05 +0200 | [diff] [blame] | 1474 | if (owner) { |
| 1475 | struct server *srv; |
| 1476 | int already_used = 0; |
| 1477 | |
| 1478 | list_for_each_entry(srv, &record->attached_servers, ip_rec_item) { |
| 1479 | if (srv == owner) |
| 1480 | continue; |
| 1481 | if (srv->proxy == owner->proxy) { |
| 1482 | already_used = 1; |
| 1483 | break; |
| 1484 | } |
| 1485 | } |
| 1486 | if (already_used) { |
| 1487 | if (!allowed_duplicated_ip) { |
| 1488 | continue; |
| 1489 | } |
| 1490 | } |
| 1491 | else { |
| 1492 | score += 2; |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 1493 | } |
| 1494 | } else { |
| 1495 | score += 2; |
| 1496 | } |
| 1497 | |
| 1498 | /* Check for current ip matching. */ |
| 1499 | if (ip_type == currentip_sin_family && |
| 1500 | ((currentip_sin_family == AF_INET && |
| 1501 | !memcmp(ip, currentip, 4)) || |
| 1502 | (currentip_sin_family == AF_INET6 && |
| 1503 | !memcmp(ip, currentip, 16)))) { |
| 1504 | score++; |
| 1505 | currentip_sel = 1; |
| 1506 | } |
| 1507 | else |
| 1508 | currentip_sel = 0; |
| 1509 | |
| 1510 | /* Keep the address if the score is better than the previous |
| 1511 | * score. The maximum score is 15, if this value is reached, we |
| 1512 | * break the parsing. Implicitly, this score is reached the ip |
| 1513 | * selected is the current ip. */ |
| 1514 | if (score > max_score) { |
| 1515 | if (ip_type == AF_INET) |
| 1516 | newip4 = ip; |
| 1517 | else |
| 1518 | newip6 = ip; |
Emeric Brun | bd78c91 | 2021-06-11 10:08:05 +0200 | [diff] [blame] | 1519 | found_record = record; |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 1520 | currentip_found = currentip_sel; |
Emeric Brun | bd78c91 | 2021-06-11 10:08:05 +0200 | [diff] [blame] | 1521 | if (score == 15) { |
| 1522 | /* this was not registered on the current record but it matches |
| 1523 | * let's fix it (it may comes from state file */ |
| 1524 | if (owner) |
| 1525 | LIST_APPEND(&found_record->attached_servers, &owner->ip_rec_item); |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 1526 | return RSLV_UPD_NO; |
Emeric Brun | bd78c91 | 2021-06-11 10:08:05 +0200 | [diff] [blame] | 1527 | } |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 1528 | max_score = score; |
| 1529 | } |
| 1530 | } /* list for each record entries */ |
| 1531 | |
| 1532 | /* No IP found in the response */ |
| 1533 | if (!newip4 && !newip6) |
| 1534 | return RSLV_UPD_NO_IP_FOUND; |
| 1535 | |
| 1536 | /* Case when the caller looks first for an IPv4 address */ |
| 1537 | if (family_priority == AF_INET) { |
| 1538 | if (newip4) { |
| 1539 | *newip = newip4; |
| 1540 | *newip_sin_family = AF_INET; |
| 1541 | } |
| 1542 | else if (newip6) { |
| 1543 | *newip = newip6; |
| 1544 | *newip_sin_family = AF_INET6; |
| 1545 | } |
| 1546 | if (!currentip_found) |
| 1547 | goto not_found; |
| 1548 | } |
| 1549 | /* Case when the caller looks first for an IPv6 address */ |
| 1550 | else if (family_priority == AF_INET6) { |
| 1551 | if (newip6) { |
| 1552 | *newip = newip6; |
| 1553 | *newip_sin_family = AF_INET6; |
| 1554 | } |
| 1555 | else if (newip4) { |
| 1556 | *newip = newip4; |
| 1557 | *newip_sin_family = AF_INET; |
| 1558 | } |
| 1559 | if (!currentip_found) |
| 1560 | goto not_found; |
| 1561 | } |
| 1562 | /* Case when the caller have no preference (we prefer IPv6) */ |
| 1563 | else if (family_priority == AF_UNSPEC) { |
| 1564 | if (newip6) { |
| 1565 | *newip = newip6; |
| 1566 | *newip_sin_family = AF_INET6; |
| 1567 | } |
| 1568 | else if (newip4) { |
| 1569 | *newip = newip4; |
| 1570 | *newip_sin_family = AF_INET; |
| 1571 | } |
| 1572 | if (!currentip_found) |
| 1573 | goto not_found; |
| 1574 | } |
| 1575 | |
| 1576 | /* No reason why we should change the server's IP address */ |
| 1577 | return RSLV_UPD_NO; |
| 1578 | |
| 1579 | not_found: |
Emeric Brun | bd78c91 | 2021-06-11 10:08:05 +0200 | [diff] [blame] | 1580 | |
| 1581 | /* the ip of this record was chosen for the server */ |
| 1582 | if (owner && found_record) { |
| 1583 | LIST_APPEND(&found_record->attached_servers, &owner->ip_rec_item); |
| 1584 | } |
| 1585 | |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 1586 | list_for_each_entry(record, &r_res->answer_list, list) { |
| 1587 | /* Move the first record to the end of the list, for internal |
| 1588 | * round robin */ |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1589 | LIST_DELETE(&record->list); |
| 1590 | LIST_APPEND(&r_res->answer_list, &record->list); |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 1591 | break; |
| 1592 | } |
| 1593 | return RSLV_UPD_SRVIP_NOT_FOUND; |
| 1594 | } |
| 1595 | |
| 1596 | /* Turns a domain name label into a string. |
| 1597 | * |
| 1598 | * <dn> must be a null-terminated string. <dn_len> must include the terminating |
| 1599 | * null byte. <str> must be allocated and its size must be passed in <str_len>. |
| 1600 | * |
| 1601 | * In case of error, -1 is returned, otherwise, the number of bytes copied in |
| 1602 | * <str> (including the terminating null byte). |
| 1603 | */ |
| 1604 | int resolv_dn_label_to_str(const char *dn, int dn_len, char *str, int str_len) |
| 1605 | { |
| 1606 | char *ptr; |
| 1607 | int i, sz; |
| 1608 | |
| 1609 | if (str_len < dn_len - 1) |
| 1610 | return -1; |
| 1611 | |
| 1612 | ptr = str; |
| 1613 | for (i = 0; i < dn_len-1; ++i) { |
| 1614 | sz = dn[i]; |
| 1615 | if (i) |
| 1616 | *ptr++ = '.'; |
| 1617 | memcpy(ptr, dn+i+1, sz); |
| 1618 | ptr += sz; |
| 1619 | i += sz; |
| 1620 | } |
| 1621 | *ptr++ = '\0'; |
| 1622 | return (ptr - str); |
| 1623 | } |
| 1624 | |
| 1625 | /* Turns a string into domain name label: www.haproxy.org into 3www7haproxy3org |
| 1626 | * |
| 1627 | * <str> must be a null-terminated string. <str_len> must include the |
| 1628 | * terminating null byte. <dn> buffer must be allocated and its size must be |
| 1629 | * passed in <dn_len>. |
| 1630 | * |
| 1631 | * In case of error, -1 is returned, otherwise, the number of bytes copied in |
| 1632 | * <dn> (excluding the terminating null byte). |
| 1633 | */ |
| 1634 | int resolv_str_to_dn_label(const char *str, int str_len, char *dn, int dn_len) |
| 1635 | { |
| 1636 | int i, offset; |
| 1637 | |
| 1638 | if (dn_len < str_len + 1) |
| 1639 | return -1; |
| 1640 | |
| 1641 | /* First byte of dn will be used to store the length of the first |
| 1642 | * label */ |
| 1643 | offset = 0; |
| 1644 | for (i = 0; i < str_len; ++i) { |
| 1645 | if (str[i] == '.') { |
| 1646 | /* 2 or more consecutive dots is invalid */ |
| 1647 | if (i == offset) |
| 1648 | return -1; |
| 1649 | |
| 1650 | /* ignore trailing dot */ |
| 1651 | if (i + 2 == str_len) { |
| 1652 | i++; |
| 1653 | break; |
| 1654 | } |
| 1655 | |
| 1656 | dn[offset] = (i - offset); |
| 1657 | offset = i+1; |
| 1658 | continue; |
| 1659 | } |
| 1660 | dn[i+1] = str[i]; |
| 1661 | } |
| 1662 | dn[offset] = (i - offset - 1); |
| 1663 | dn[i] = '\0'; |
| 1664 | return i; |
| 1665 | } |
| 1666 | |
| 1667 | /* Validates host name: |
| 1668 | * - total size |
| 1669 | * - each label size individually |
| 1670 | * returns: |
| 1671 | * 0 in case of error. If <err> is not NULL, an error message is stored there. |
| 1672 | * 1 when no error. <err> is left unaffected. |
| 1673 | */ |
| 1674 | int resolv_hostname_validation(const char *string, char **err) |
| 1675 | { |
| 1676 | int i; |
| 1677 | |
| 1678 | if (strlen(string) > DNS_MAX_NAME_SIZE) { |
| 1679 | if (err) |
| 1680 | *err = DNS_TOO_LONG_FQDN; |
| 1681 | return 0; |
| 1682 | } |
| 1683 | |
| 1684 | while (*string) { |
| 1685 | i = 0; |
| 1686 | while (*string && *string != '.' && i < DNS_MAX_LABEL_SIZE) { |
| 1687 | if (!(*string == '-' || *string == '_' || |
| 1688 | (*string >= 'a' && *string <= 'z') || |
| 1689 | (*string >= 'A' && *string <= 'Z') || |
| 1690 | (*string >= '0' && *string <= '9'))) { |
| 1691 | if (err) |
| 1692 | *err = DNS_INVALID_CHARACTER; |
| 1693 | return 0; |
| 1694 | } |
| 1695 | i++; |
| 1696 | string++; |
| 1697 | } |
| 1698 | |
| 1699 | if (!(*string)) |
| 1700 | break; |
| 1701 | |
| 1702 | if (*string != '.' && i >= DNS_MAX_LABEL_SIZE) { |
| 1703 | if (err) |
| 1704 | *err = DNS_LABEL_TOO_LONG; |
| 1705 | return 0; |
| 1706 | } |
| 1707 | |
| 1708 | string++; |
| 1709 | } |
| 1710 | return 1; |
| 1711 | } |
| 1712 | |
| 1713 | /* Picks up an available resolution from the different resolution list |
| 1714 | * associated to a resolvers section, in this order: |
| 1715 | * 1. check in resolutions.curr for the same hostname and query_type |
| 1716 | * 2. check in resolutions.wait for the same hostname and query_type |
| 1717 | * 3. Get a new resolution from resolution pool |
| 1718 | * |
| 1719 | * Returns an available resolution, NULL if none found. |
| 1720 | */ |
| 1721 | static struct resolv_resolution *resolv_pick_resolution(struct resolvers *resolvers, |
| 1722 | char **hostname_dn, int hostname_dn_len, |
| 1723 | int query_type) |
| 1724 | { |
| 1725 | struct resolv_resolution *res; |
| 1726 | |
| 1727 | if (!*hostname_dn) |
| 1728 | goto from_pool; |
| 1729 | |
| 1730 | /* Search for same hostname and query type in resolutions.curr */ |
| 1731 | list_for_each_entry(res, &resolvers->resolutions.curr, list) { |
| 1732 | if (!res->hostname_dn) |
| 1733 | continue; |
| 1734 | if ((query_type == res->prefered_query_type) && |
| 1735 | hostname_dn_len == res->hostname_dn_len && |
| 1736 | !resolv_hostname_cmp(*hostname_dn, res->hostname_dn, hostname_dn_len)) |
| 1737 | return res; |
| 1738 | } |
| 1739 | |
| 1740 | /* Search for same hostname and query type in resolutions.wait */ |
| 1741 | list_for_each_entry(res, &resolvers->resolutions.wait, list) { |
| 1742 | if (!res->hostname_dn) |
| 1743 | continue; |
| 1744 | if ((query_type == res->prefered_query_type) && |
| 1745 | hostname_dn_len == res->hostname_dn_len && |
| 1746 | !resolv_hostname_cmp(*hostname_dn, res->hostname_dn, hostname_dn_len)) |
| 1747 | return res; |
| 1748 | } |
| 1749 | |
| 1750 | from_pool: |
| 1751 | /* No resolution could be found, so let's allocate a new one */ |
Willy Tarreau | 70490eb | 2021-03-22 21:08:50 +0100 | [diff] [blame] | 1752 | res = pool_zalloc(resolv_resolution_pool); |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 1753 | if (res) { |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 1754 | res->resolvers = resolvers; |
| 1755 | res->uuid = resolution_uuid; |
| 1756 | res->status = RSLV_STATUS_NONE; |
| 1757 | res->step = RSLV_STEP_NONE; |
| 1758 | res->last_valid = now_ms; |
| 1759 | |
| 1760 | LIST_INIT(&res->requesters); |
| 1761 | LIST_INIT(&res->response.answer_list); |
| 1762 | |
| 1763 | res->prefered_query_type = query_type; |
| 1764 | res->query_type = query_type; |
| 1765 | res->hostname_dn = *hostname_dn; |
| 1766 | res->hostname_dn_len = hostname_dn_len; |
| 1767 | |
| 1768 | ++resolution_uuid; |
| 1769 | |
| 1770 | /* Move the resolution to the resolvers wait queue */ |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1771 | LIST_APPEND(&resolvers->resolutions.wait, &res->list); |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 1772 | } |
| 1773 | return res; |
| 1774 | } |
| 1775 | |
Christopher Faulet | 1dec5c7 | 2021-03-10 14:40:39 +0100 | [diff] [blame] | 1776 | void resolv_purge_resolution_answer_records(struct resolv_resolution *resolution) |
| 1777 | { |
| 1778 | struct resolv_answer_item *item, *itemback; |
| 1779 | |
| 1780 | list_for_each_entry_safe(item, itemback, &resolution->response.answer_list, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1781 | LIST_DELETE(&item->list); |
Christopher Faulet | 1dec5c7 | 2021-03-10 14:40:39 +0100 | [diff] [blame] | 1782 | pool_free(resolv_answer_item_pool, item->ar_item); |
| 1783 | pool_free(resolv_answer_item_pool, item); |
| 1784 | } |
| 1785 | } |
| 1786 | |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 1787 | /* Releases a resolution from its requester(s) and move it back to the pool */ |
| 1788 | static void resolv_free_resolution(struct resolv_resolution *resolution) |
| 1789 | { |
| 1790 | struct resolv_requester *req, *reqback; |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 1791 | |
| 1792 | /* clean up configuration */ |
| 1793 | resolv_reset_resolution(resolution); |
| 1794 | resolution->hostname_dn = NULL; |
| 1795 | resolution->hostname_dn_len = 0; |
| 1796 | |
| 1797 | list_for_each_entry_safe(req, reqback, &resolution->requesters, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1798 | LIST_DELETE(&req->list); |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 1799 | req->resolution = NULL; |
| 1800 | } |
Christopher Faulet | 1dec5c7 | 2021-03-10 14:40:39 +0100 | [diff] [blame] | 1801 | resolv_purge_resolution_answer_records(resolution); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1802 | LIST_DELETE(&resolution->list); |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 1803 | pool_free(resolv_resolution_pool, resolution); |
| 1804 | } |
| 1805 | |
| 1806 | /* Links a requester (a server or a resolv_srvrq) with a resolution. It returns 0 |
| 1807 | * on success, -1 otherwise. |
| 1808 | */ |
| 1809 | int resolv_link_resolution(void *requester, int requester_type, int requester_locked) |
| 1810 | { |
| 1811 | struct resolv_resolution *res = NULL; |
| 1812 | struct resolv_requester *req; |
| 1813 | struct resolvers *resolvers; |
| 1814 | struct server *srv = NULL; |
| 1815 | struct resolv_srvrq *srvrq = NULL; |
| 1816 | struct stream *stream = NULL; |
| 1817 | char **hostname_dn; |
| 1818 | int hostname_dn_len, query_type; |
| 1819 | |
| 1820 | switch (requester_type) { |
| 1821 | case OBJ_TYPE_SERVER: |
| 1822 | srv = (struct server *)requester; |
| 1823 | hostname_dn = &srv->hostname_dn; |
| 1824 | hostname_dn_len = srv->hostname_dn_len; |
| 1825 | resolvers = srv->resolvers; |
| 1826 | query_type = ((srv->resolv_opts.family_prio == AF_INET) |
| 1827 | ? DNS_RTYPE_A |
| 1828 | : DNS_RTYPE_AAAA); |
| 1829 | break; |
| 1830 | |
| 1831 | case OBJ_TYPE_SRVRQ: |
| 1832 | srvrq = (struct resolv_srvrq *)requester; |
| 1833 | hostname_dn = &srvrq->hostname_dn; |
| 1834 | hostname_dn_len = srvrq->hostname_dn_len; |
| 1835 | resolvers = srvrq->resolvers; |
| 1836 | query_type = DNS_RTYPE_SRV; |
| 1837 | break; |
| 1838 | |
| 1839 | case OBJ_TYPE_STREAM: |
| 1840 | stream = (struct stream *)requester; |
| 1841 | hostname_dn = &stream->resolv_ctx.hostname_dn; |
| 1842 | hostname_dn_len = stream->resolv_ctx.hostname_dn_len; |
| 1843 | resolvers = stream->resolv_ctx.parent->arg.resolv.resolvers; |
| 1844 | query_type = ((stream->resolv_ctx.parent->arg.resolv.opts->family_prio == AF_INET) |
| 1845 | ? DNS_RTYPE_A |
| 1846 | : DNS_RTYPE_AAAA); |
| 1847 | break; |
| 1848 | default: |
| 1849 | goto err; |
| 1850 | } |
| 1851 | |
| 1852 | /* Get a resolution from the resolvers' wait queue or pool */ |
| 1853 | if ((res = resolv_pick_resolution(resolvers, hostname_dn, hostname_dn_len, query_type)) == NULL) |
| 1854 | goto err; |
| 1855 | |
| 1856 | if (srv) { |
| 1857 | if (!requester_locked) |
| 1858 | HA_SPIN_LOCK(SERVER_LOCK, &srv->lock); |
| 1859 | if (srv->resolv_requester == NULL) { |
| 1860 | if ((req = pool_alloc(resolv_requester_pool)) == NULL) { |
| 1861 | if (!requester_locked) |
| 1862 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
| 1863 | goto err; |
| 1864 | } |
| 1865 | req->owner = &srv->obj_type; |
| 1866 | srv->resolv_requester = req; |
| 1867 | } |
| 1868 | else |
| 1869 | req = srv->resolv_requester; |
| 1870 | if (!requester_locked) |
| 1871 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
| 1872 | |
| 1873 | req->requester_cb = snr_resolution_cb; |
| 1874 | req->requester_error_cb = snr_resolution_error_cb; |
| 1875 | } |
| 1876 | else if (srvrq) { |
| 1877 | if (srvrq->requester == NULL) { |
| 1878 | if ((req = pool_alloc(resolv_requester_pool)) == NULL) |
| 1879 | goto err; |
| 1880 | req->owner = &srvrq->obj_type; |
| 1881 | srvrq->requester = req; |
| 1882 | } |
| 1883 | else |
| 1884 | req = srvrq->requester; |
| 1885 | |
| 1886 | req->requester_cb = snr_resolution_cb; |
Baptiste Assmann | b4badf7 | 2020-11-19 22:38:33 +0100 | [diff] [blame] | 1887 | req->requester_error_cb = srvrq_resolution_error_cb; |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 1888 | } |
| 1889 | else if (stream) { |
| 1890 | if (stream->resolv_ctx.requester == NULL) { |
| 1891 | if ((req = pool_alloc(resolv_requester_pool)) == NULL) |
| 1892 | goto err; |
| 1893 | req->owner = &stream->obj_type; |
| 1894 | stream->resolv_ctx.requester = req; |
| 1895 | } |
| 1896 | else |
| 1897 | req = stream->resolv_ctx.requester; |
| 1898 | |
| 1899 | req->requester_cb = act_resolution_cb; |
| 1900 | req->requester_error_cb = act_resolution_error_cb; |
| 1901 | } |
| 1902 | else |
| 1903 | goto err; |
| 1904 | |
| 1905 | req->resolution = res; |
| 1906 | |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1907 | LIST_APPEND(&res->requesters, &req->list); |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 1908 | return 0; |
| 1909 | |
| 1910 | err: |
| 1911 | if (res && LIST_ISEMPTY(&res->requesters)) |
| 1912 | resolv_free_resolution(res); |
| 1913 | return -1; |
| 1914 | } |
| 1915 | |
Emeric Brun | 3406766 | 2021-06-11 10:48:45 +0200 | [diff] [blame] | 1916 | /* This function removes all server/srvrq references on answer items |
| 1917 | * if <safe> is set to 1, in case of srvrq, sub server resquesters unlink |
| 1918 | * is called using safe == 1 to make it usable into callbacks |
| 1919 | */ |
| 1920 | void resolv_detach_from_resolution_answer_items(struct resolv_resolution *res, struct resolv_requester *req, int safe) |
| 1921 | { |
| 1922 | struct resolv_answer_item *item, *itemback; |
| 1923 | struct server *srv, *srvback; |
| 1924 | struct resolv_srvrq *srvrq; |
| 1925 | |
| 1926 | if ((srv = objt_server(req->owner)) != NULL) { |
| 1927 | LIST_DEL_INIT(&srv->ip_rec_item); |
| 1928 | } |
| 1929 | else if ((srvrq = objt_resolv_srvrq(req->owner)) != NULL) { |
| 1930 | list_for_each_entry_safe(item, itemback, &res->response.answer_list, list) { |
| 1931 | if (item->type == DNS_RTYPE_SRV) { |
| 1932 | list_for_each_entry_safe(srv, srvback, &item->attached_servers, srv_rec_item) { |
| 1933 | if (srv->srvrq == srvrq) { |
| 1934 | HA_SPIN_LOCK(SERVER_LOCK, &srv->lock); |
| 1935 | resolv_unlink_resolution(srv->resolv_requester, safe); |
| 1936 | srvrq_update_srv_status(srv, 1); |
| 1937 | ha_free(&srv->hostname); |
| 1938 | ha_free(&srv->hostname_dn); |
| 1939 | srv->hostname_dn_len = 0; |
| 1940 | memset(&srv->addr, 0, sizeof(srv->addr)); |
| 1941 | srv->svc_port = 0; |
| 1942 | srv->flags |= SRV_F_NO_RESOLUTION; |
| 1943 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
| 1944 | LIST_DELETE(&srv->srv_rec_item); |
| 1945 | LIST_APPEND(&srvrq->attached_servers, &srv->srv_rec_item); |
| 1946 | } |
| 1947 | } |
| 1948 | } |
| 1949 | } |
| 1950 | } |
| 1951 | } |
| 1952 | |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 1953 | /* Removes a requester from a DNS resolution. It takes takes care of all the |
| 1954 | * consequences. It also cleans up some parameters from the requester. |
Christopher Faulet | 0efc099 | 2021-03-11 18:09:53 +0100 | [diff] [blame] | 1955 | * if <safe> is set to 1, the corresponding resolution is not released. |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 1956 | */ |
Christopher Faulet | 0efc099 | 2021-03-11 18:09:53 +0100 | [diff] [blame] | 1957 | void resolv_unlink_resolution(struct resolv_requester *requester, int safe) |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 1958 | { |
| 1959 | struct resolv_resolution *res; |
| 1960 | struct resolv_requester *req; |
| 1961 | |
| 1962 | /* Nothing to do */ |
| 1963 | if (!requester || !requester->resolution) |
| 1964 | return; |
| 1965 | res = requester->resolution; |
| 1966 | |
Emeric Brun | bd78c91 | 2021-06-11 10:08:05 +0200 | [diff] [blame] | 1967 | /* remove ref from the resolution answer item list to the requester */ |
Emeric Brun | 3406766 | 2021-06-11 10:48:45 +0200 | [diff] [blame] | 1968 | resolv_detach_from_resolution_answer_items(res, requester, safe); |
Emeric Brun | bd78c91 | 2021-06-11 10:08:05 +0200 | [diff] [blame] | 1969 | |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 1970 | /* Clean up the requester */ |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1971 | LIST_DELETE(&requester->list); |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 1972 | requester->resolution = NULL; |
| 1973 | |
| 1974 | /* We need to find another requester linked on this resolution */ |
| 1975 | if (!LIST_ISEMPTY(&res->requesters)) |
| 1976 | req = LIST_NEXT(&res->requesters, struct resolv_requester *, list); |
| 1977 | else { |
Christopher Faulet | 0efc099 | 2021-03-11 18:09:53 +0100 | [diff] [blame] | 1978 | if (safe) { |
| 1979 | /* Don't release it yet. */ |
| 1980 | resolv_reset_resolution(res); |
| 1981 | res->hostname_dn = NULL; |
| 1982 | res->hostname_dn_len = 0; |
| 1983 | resolv_purge_resolution_answer_records(res); |
| 1984 | return; |
| 1985 | } |
| 1986 | |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 1987 | resolv_free_resolution(res); |
| 1988 | return; |
| 1989 | } |
| 1990 | |
| 1991 | /* Move hostname_dn related pointers to the next requester */ |
| 1992 | switch (obj_type(req->owner)) { |
| 1993 | case OBJ_TYPE_SERVER: |
| 1994 | res->hostname_dn = __objt_server(req->owner)->hostname_dn; |
| 1995 | res->hostname_dn_len = __objt_server(req->owner)->hostname_dn_len; |
| 1996 | break; |
| 1997 | case OBJ_TYPE_SRVRQ: |
| 1998 | res->hostname_dn = __objt_resolv_srvrq(req->owner)->hostname_dn; |
| 1999 | res->hostname_dn_len = __objt_resolv_srvrq(req->owner)->hostname_dn_len; |
| 2000 | break; |
| 2001 | case OBJ_TYPE_STREAM: |
| 2002 | res->hostname_dn = __objt_stream(req->owner)->resolv_ctx.hostname_dn; |
| 2003 | res->hostname_dn_len = __objt_stream(req->owner)->resolv_ctx.hostname_dn_len; |
| 2004 | break; |
| 2005 | default: |
| 2006 | res->hostname_dn = NULL; |
| 2007 | res->hostname_dn_len = 0; |
| 2008 | break; |
| 2009 | } |
| 2010 | } |
| 2011 | |
| 2012 | /* Called when a network IO is generated on a name server socket for an incoming |
| 2013 | * packet. It performs the following actions: |
| 2014 | * - check if the packet requires processing (not outdated resolution) |
| 2015 | * - ensure the DNS packet received is valid and call requester's callback |
| 2016 | * - call requester's error callback if invalid response |
| 2017 | * - check the dn_name in the packet against the one sent |
| 2018 | */ |
| 2019 | static int resolv_process_responses(struct dns_nameserver *ns) |
| 2020 | { |
| 2021 | struct dns_counters *tmpcounters; |
| 2022 | struct resolvers *resolvers; |
| 2023 | struct resolv_resolution *res; |
| 2024 | struct resolv_query_item *query; |
| 2025 | unsigned char buf[DNS_MAX_UDP_MESSAGE + 1]; |
| 2026 | unsigned char *bufend; |
| 2027 | int buflen, dns_resp; |
| 2028 | int max_answer_records; |
| 2029 | unsigned short query_id; |
| 2030 | struct eb32_node *eb; |
| 2031 | struct resolv_requester *req; |
Emeric Brun | 12ca658 | 2021-06-10 15:25:25 +0200 | [diff] [blame] | 2032 | int keep_answer_items; |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 2033 | |
| 2034 | resolvers = ns->parent; |
| 2035 | HA_SPIN_LOCK(DNS_LOCK, &resolvers->lock); |
| 2036 | |
| 2037 | /* process all pending input messages */ |
| 2038 | while (1) { |
| 2039 | /* read message received */ |
| 2040 | memset(buf, '\0', resolvers->accepted_payload_size + 1); |
| 2041 | if ((buflen = dns_recv_nameserver(ns, (void *)buf, sizeof(buf))) <= 0) { |
| 2042 | break; |
| 2043 | } |
| 2044 | |
| 2045 | /* message too big */ |
| 2046 | if (buflen > resolvers->accepted_payload_size) { |
| 2047 | ns->counters->too_big++; |
| 2048 | continue; |
| 2049 | } |
| 2050 | |
| 2051 | /* initializing variables */ |
| 2052 | bufend = buf + buflen; /* pointer to mark the end of the buffer */ |
| 2053 | |
| 2054 | /* read the query id from the packet (16 bits) */ |
| 2055 | if (buf + 2 > bufend) { |
| 2056 | ns->counters->invalid++; |
| 2057 | continue; |
| 2058 | } |
| 2059 | query_id = resolv_response_get_query_id(buf); |
| 2060 | |
| 2061 | /* search the query_id in the pending resolution tree */ |
| 2062 | eb = eb32_lookup(&resolvers->query_ids, query_id); |
| 2063 | if (eb == NULL) { |
| 2064 | /* unknown query id means an outdated response and can be safely ignored */ |
| 2065 | ns->counters->outdated++; |
| 2066 | continue; |
| 2067 | } |
| 2068 | |
| 2069 | /* known query id means a resolution in progress */ |
| 2070 | res = eb32_entry(eb, struct resolv_resolution, qid); |
| 2071 | /* number of responses received */ |
| 2072 | res->nb_responses++; |
| 2073 | |
| 2074 | max_answer_records = (resolvers->accepted_payload_size - DNS_HEADER_SIZE) / DNS_MIN_RECORD_SIZE; |
| 2075 | dns_resp = resolv_validate_dns_response(buf, bufend, res, max_answer_records); |
| 2076 | |
| 2077 | switch (dns_resp) { |
| 2078 | case RSLV_RESP_VALID: |
| 2079 | break; |
| 2080 | |
| 2081 | case RSLV_RESP_INVALID: |
| 2082 | case RSLV_RESP_QUERY_COUNT_ERROR: |
| 2083 | case RSLV_RESP_WRONG_NAME: |
| 2084 | res->status = RSLV_STATUS_INVALID; |
| 2085 | ns->counters->invalid++; |
| 2086 | break; |
| 2087 | |
| 2088 | case RSLV_RESP_NX_DOMAIN: |
| 2089 | res->status = RSLV_STATUS_NX; |
| 2090 | ns->counters->nx++; |
| 2091 | break; |
| 2092 | |
| 2093 | case RSLV_RESP_REFUSED: |
| 2094 | res->status = RSLV_STATUS_REFUSED; |
| 2095 | ns->counters->refused++; |
| 2096 | break; |
| 2097 | |
| 2098 | case RSLV_RESP_ANCOUNT_ZERO: |
| 2099 | res->status = RSLV_STATUS_OTHER; |
| 2100 | ns->counters->any_err++; |
| 2101 | break; |
| 2102 | |
| 2103 | case RSLV_RESP_CNAME_ERROR: |
| 2104 | res->status = RSLV_STATUS_OTHER; |
| 2105 | ns->counters->cname_error++; |
| 2106 | break; |
| 2107 | |
| 2108 | case RSLV_RESP_TRUNCATED: |
| 2109 | res->status = RSLV_STATUS_OTHER; |
| 2110 | ns->counters->truncated++; |
| 2111 | break; |
| 2112 | |
| 2113 | case RSLV_RESP_NO_EXPECTED_RECORD: |
| 2114 | case RSLV_RESP_ERROR: |
| 2115 | case RSLV_RESP_INTERNAL: |
| 2116 | res->status = RSLV_STATUS_OTHER; |
| 2117 | ns->counters->other++; |
| 2118 | break; |
| 2119 | } |
| 2120 | |
| 2121 | /* Wait all nameservers response to handle errors */ |
| 2122 | if (dns_resp != RSLV_RESP_VALID && res->nb_responses < res->nb_queries) |
| 2123 | continue; |
| 2124 | |
| 2125 | /* Process error codes */ |
| 2126 | if (dns_resp != RSLV_RESP_VALID) { |
| 2127 | if (res->prefered_query_type != res->query_type) { |
| 2128 | /* The fallback on the query type was already performed, |
| 2129 | * so check the try counter. If it falls to 0, we can |
| 2130 | * report an error. Else, wait the next attempt. */ |
| 2131 | if (!res->try) |
| 2132 | goto report_res_error; |
| 2133 | } |
| 2134 | else { |
| 2135 | /* Fallback from A to AAAA or the opposite and re-send |
| 2136 | * the resolution immediately. try counter is not |
| 2137 | * decremented. */ |
| 2138 | if (res->prefered_query_type == DNS_RTYPE_A) { |
| 2139 | res->query_type = DNS_RTYPE_AAAA; |
| 2140 | resolv_send_query(res); |
| 2141 | } |
| 2142 | else if (res->prefered_query_type == DNS_RTYPE_AAAA) { |
| 2143 | res->query_type = DNS_RTYPE_A; |
| 2144 | resolv_send_query(res); |
| 2145 | } |
| 2146 | } |
| 2147 | continue; |
| 2148 | } |
| 2149 | |
| 2150 | /* Now let's check the query's dname corresponds to the one we |
| 2151 | * sent. We can check only the first query of the list. We send |
| 2152 | * one query at a time so we get one query in the response */ |
| 2153 | query = LIST_NEXT(&res->response.query_list, struct resolv_query_item *, list); |
| 2154 | if (query && resolv_hostname_cmp(query->name, res->hostname_dn, res->hostname_dn_len) != 0) { |
| 2155 | dns_resp = RSLV_RESP_WRONG_NAME; |
| 2156 | ns->counters->other++; |
| 2157 | goto report_res_error; |
| 2158 | } |
| 2159 | |
| 2160 | /* So the resolution succeeded */ |
| 2161 | res->status = RSLV_STATUS_VALID; |
| 2162 | res->last_valid = now_ms; |
| 2163 | ns->counters->valid++; |
| 2164 | goto report_res_success; |
| 2165 | |
| 2166 | report_res_error: |
Emeric Brun | 12ca658 | 2021-06-10 15:25:25 +0200 | [diff] [blame] | 2167 | keep_answer_items = 0; |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 2168 | list_for_each_entry(req, &res->requesters, list) |
Emeric Brun | 12ca658 | 2021-06-10 15:25:25 +0200 | [diff] [blame] | 2169 | keep_answer_items |= req->requester_error_cb(req, dns_resp); |
| 2170 | if (!keep_answer_items) |
| 2171 | resolv_purge_resolution_answer_records(res); |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 2172 | resolv_reset_resolution(res); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 2173 | LIST_DELETE(&res->list); |
| 2174 | LIST_APPEND(&resolvers->resolutions.wait, &res->list); |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 2175 | continue; |
| 2176 | |
| 2177 | report_res_success: |
| 2178 | /* Only the 1rst requester s managed by the server, others are |
| 2179 | * from the cache */ |
| 2180 | tmpcounters = ns->counters; |
| 2181 | list_for_each_entry(req, &res->requesters, list) { |
| 2182 | struct server *s = objt_server(req->owner); |
| 2183 | |
| 2184 | if (s) |
| 2185 | HA_SPIN_LOCK(SERVER_LOCK, &s->lock); |
| 2186 | req->requester_cb(req, tmpcounters); |
| 2187 | if (s) |
| 2188 | HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock); |
| 2189 | tmpcounters = NULL; |
| 2190 | } |
| 2191 | |
| 2192 | resolv_reset_resolution(res); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 2193 | LIST_DELETE(&res->list); |
| 2194 | LIST_APPEND(&resolvers->resolutions.wait, &res->list); |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 2195 | continue; |
| 2196 | } |
| 2197 | resolv_update_resolvers_timeout(resolvers); |
| 2198 | HA_SPIN_UNLOCK(DNS_LOCK, &resolvers->lock); |
| 2199 | |
| 2200 | return buflen; |
| 2201 | } |
| 2202 | |
| 2203 | /* Processes DNS resolution. First, it checks the active list to detect expired |
| 2204 | * resolutions and retry them if possible. Else a timeout is reported. Then, it |
| 2205 | * checks the wait list to trigger new resolutions. |
| 2206 | */ |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 2207 | static struct task *process_resolvers(struct task *t, void *context, unsigned int state) |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 2208 | { |
| 2209 | struct resolvers *resolvers = context; |
| 2210 | struct resolv_resolution *res, *resback; |
| 2211 | int exp; |
| 2212 | |
| 2213 | HA_SPIN_LOCK(DNS_LOCK, &resolvers->lock); |
| 2214 | |
| 2215 | /* Handle all expired resolutions from the active list */ |
| 2216 | list_for_each_entry_safe(res, resback, &resolvers->resolutions.curr, list) { |
Christopher Faulet | 0efc099 | 2021-03-11 18:09:53 +0100 | [diff] [blame] | 2217 | if (LIST_ISEMPTY(&res->requesters)) { |
| 2218 | resolv_free_resolution(res); |
| 2219 | continue; |
| 2220 | } |
| 2221 | |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 2222 | /* When we find the first resolution in the future, then we can |
| 2223 | * stop here */ |
| 2224 | exp = tick_add(res->last_query, resolvers->timeout.retry); |
| 2225 | if (!tick_is_expired(exp, now_ms)) |
| 2226 | break; |
| 2227 | |
| 2228 | /* If current resolution has been tried too many times and |
| 2229 | * finishes in timeout we update its status and remove it from |
| 2230 | * the list */ |
| 2231 | if (!res->try) { |
| 2232 | struct resolv_requester *req; |
Emeric Brun | 12ca658 | 2021-06-10 15:25:25 +0200 | [diff] [blame] | 2233 | int keep_answer_items = 0; |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 2234 | |
| 2235 | /* Notify the result to the requesters */ |
| 2236 | if (!res->nb_responses) |
| 2237 | res->status = RSLV_STATUS_TIMEOUT; |
| 2238 | list_for_each_entry(req, &res->requesters, list) |
Emeric Brun | 12ca658 | 2021-06-10 15:25:25 +0200 | [diff] [blame] | 2239 | keep_answer_items |= req->requester_error_cb(req, res->status); |
| 2240 | if (!keep_answer_items) |
| 2241 | resolv_purge_resolution_answer_records(res); |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 2242 | |
| 2243 | /* Clean up resolution info and remove it from the |
| 2244 | * current list */ |
| 2245 | resolv_reset_resolution(res); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 2246 | LIST_DELETE(&res->list); |
| 2247 | LIST_APPEND(&resolvers->resolutions.wait, &res->list); |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 2248 | } |
| 2249 | else { |
| 2250 | /* Otherwise resend the DNS query and requeue the resolution */ |
| 2251 | if (!res->nb_responses || res->prefered_query_type != res->query_type) { |
| 2252 | /* No response received (a real timeout) or fallback already done */ |
| 2253 | res->query_type = res->prefered_query_type; |
| 2254 | res->try--; |
| 2255 | } |
| 2256 | else { |
| 2257 | /* Fallback from A to AAAA or the opposite and re-send |
| 2258 | * the resolution immediately. try counter is not |
| 2259 | * decremented. */ |
| 2260 | if (res->prefered_query_type == DNS_RTYPE_A) |
| 2261 | res->query_type = DNS_RTYPE_AAAA; |
| 2262 | else if (res->prefered_query_type == DNS_RTYPE_AAAA) |
| 2263 | res->query_type = DNS_RTYPE_A; |
| 2264 | else |
| 2265 | res->try--; |
| 2266 | } |
| 2267 | resolv_send_query(res); |
| 2268 | } |
| 2269 | } |
| 2270 | |
| 2271 | /* Handle all resolutions in the wait list */ |
| 2272 | list_for_each_entry_safe(res, resback, &resolvers->resolutions.wait, list) { |
Christopher Faulet | 0efc099 | 2021-03-11 18:09:53 +0100 | [diff] [blame] | 2273 | if (LIST_ISEMPTY(&res->requesters)) { |
| 2274 | resolv_free_resolution(res); |
| 2275 | continue; |
| 2276 | } |
| 2277 | |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 2278 | exp = tick_add(res->last_resolution, resolv_resolution_timeout(res)); |
| 2279 | if (tick_isset(res->last_resolution) && !tick_is_expired(exp, now_ms)) |
| 2280 | continue; |
| 2281 | |
| 2282 | if (resolv_run_resolution(res) != 1) { |
| 2283 | res->last_resolution = now_ms; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 2284 | LIST_DELETE(&res->list); |
| 2285 | LIST_APPEND(&resolvers->resolutions.wait, &res->list); |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 2286 | } |
| 2287 | } |
| 2288 | |
| 2289 | resolv_update_resolvers_timeout(resolvers); |
| 2290 | HA_SPIN_UNLOCK(DNS_LOCK, &resolvers->lock); |
| 2291 | return t; |
| 2292 | } |
| 2293 | |
| 2294 | /* Release memory allocated by DNS */ |
| 2295 | static void resolvers_deinit(void) |
| 2296 | { |
| 2297 | struct resolvers *resolvers, *resolversback; |
| 2298 | struct dns_nameserver *ns, *nsback; |
| 2299 | struct resolv_resolution *res, *resback; |
| 2300 | struct resolv_requester *req, *reqback; |
| 2301 | struct resolv_srvrq *srvrq, *srvrqback; |
| 2302 | |
| 2303 | list_for_each_entry_safe(resolvers, resolversback, &sec_resolvers, list) { |
| 2304 | list_for_each_entry_safe(ns, nsback, &resolvers->nameservers, list) { |
| 2305 | free(ns->id); |
| 2306 | free((char *)ns->conf.file); |
| 2307 | if (ns->dgram) { |
| 2308 | if (ns->dgram->conn.t.sock.fd != -1) { |
| 2309 | fd_delete(ns->dgram->conn.t.sock.fd); |
| 2310 | close(ns->dgram->conn.t.sock.fd); |
| 2311 | } |
| 2312 | if (ns->dgram->ring_req) |
| 2313 | ring_free(ns->dgram->ring_req); |
| 2314 | free(ns->dgram); |
| 2315 | } |
Emeric Brun | 56fc5d9 | 2021-02-12 20:05:45 +0100 | [diff] [blame] | 2316 | if (ns->stream) { |
| 2317 | if (ns->stream->ring_req) |
| 2318 | ring_free(ns->stream->ring_req); |
| 2319 | if (ns->stream->task_req) |
| 2320 | task_destroy(ns->stream->task_req); |
| 2321 | if (ns->stream->task_rsp) |
| 2322 | task_destroy(ns->stream->task_rsp); |
| 2323 | free(ns->stream); |
| 2324 | } |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 2325 | LIST_DELETE(&ns->list); |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 2326 | EXTRA_COUNTERS_FREE(ns->extra_counters); |
| 2327 | free(ns); |
| 2328 | } |
| 2329 | |
| 2330 | list_for_each_entry_safe(res, resback, &resolvers->resolutions.curr, list) { |
| 2331 | list_for_each_entry_safe(req, reqback, &res->requesters, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 2332 | LIST_DELETE(&req->list); |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 2333 | pool_free(resolv_requester_pool, req); |
| 2334 | } |
| 2335 | resolv_free_resolution(res); |
| 2336 | } |
| 2337 | |
| 2338 | list_for_each_entry_safe(res, resback, &resolvers->resolutions.wait, list) { |
| 2339 | list_for_each_entry_safe(req, reqback, &res->requesters, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 2340 | LIST_DELETE(&req->list); |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 2341 | pool_free(resolv_requester_pool, req); |
| 2342 | } |
| 2343 | resolv_free_resolution(res); |
| 2344 | } |
| 2345 | |
| 2346 | free(resolvers->id); |
| 2347 | free((char *)resolvers->conf.file); |
| 2348 | task_destroy(resolvers->t); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 2349 | LIST_DELETE(&resolvers->list); |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 2350 | free(resolvers); |
| 2351 | } |
| 2352 | |
| 2353 | list_for_each_entry_safe(srvrq, srvrqback, &resolv_srvrq_list, list) { |
| 2354 | free(srvrq->name); |
| 2355 | free(srvrq->hostname_dn); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 2356 | LIST_DELETE(&srvrq->list); |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 2357 | free(srvrq); |
| 2358 | } |
| 2359 | } |
| 2360 | |
| 2361 | /* Finalizes the DNS configuration by allocating required resources and checking |
| 2362 | * live parameters. |
| 2363 | * Returns 0 on success, ERR_* flags otherwise. |
| 2364 | */ |
| 2365 | static int resolvers_finalize_config(void) |
| 2366 | { |
| 2367 | struct resolvers *resolvers; |
| 2368 | struct proxy *px; |
| 2369 | int err_code = 0; |
| 2370 | |
| 2371 | /* allocate pool of resolution per resolvers */ |
| 2372 | list_for_each_entry(resolvers, &sec_resolvers, list) { |
| 2373 | struct dns_nameserver *ns; |
| 2374 | struct task *t; |
| 2375 | |
| 2376 | /* Check if we can create the socket with nameservers info */ |
| 2377 | list_for_each_entry(ns, &resolvers->nameservers, list) { |
| 2378 | int fd; |
| 2379 | |
| 2380 | if (ns->dgram) { |
| 2381 | /* Check nameserver info */ |
| 2382 | if ((fd = socket(ns->dgram->conn.addr.to.ss_family, SOCK_DGRAM, IPPROTO_UDP)) == -1) { |
Amaury Denoyelle | 1112430 | 2021-06-04 18:22:08 +0200 | [diff] [blame] | 2383 | ha_alert("resolvers '%s': can't create socket for nameserver '%s'.\n", |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 2384 | resolvers->id, ns->id); |
| 2385 | err_code |= (ERR_ALERT|ERR_ABORT); |
| 2386 | continue; |
| 2387 | } |
| 2388 | if (connect(fd, (struct sockaddr*)&ns->dgram->conn.addr.to, get_addr_len(&ns->dgram->conn.addr.to)) == -1) { |
Amaury Denoyelle | 1112430 | 2021-06-04 18:22:08 +0200 | [diff] [blame] | 2389 | ha_alert("resolvers '%s': can't connect socket for nameserver '%s'.\n", |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 2390 | resolvers->id, ns->id); |
| 2391 | close(fd); |
| 2392 | err_code |= (ERR_ALERT|ERR_ABORT); |
| 2393 | continue; |
| 2394 | } |
| 2395 | close(fd); |
| 2396 | } |
| 2397 | } |
| 2398 | |
| 2399 | /* Create the task associated to the resolvers section */ |
| 2400 | if ((t = task_new(MAX_THREADS_MASK)) == NULL) { |
Amaury Denoyelle | 1112430 | 2021-06-04 18:22:08 +0200 | [diff] [blame] | 2401 | ha_alert("resolvers '%s' : out of memory.\n", resolvers->id); |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 2402 | err_code |= (ERR_ALERT|ERR_ABORT); |
| 2403 | goto err; |
| 2404 | } |
| 2405 | |
| 2406 | /* Update task's parameters */ |
| 2407 | t->process = process_resolvers; |
| 2408 | t->context = resolvers; |
| 2409 | resolvers->t = t; |
| 2410 | task_wakeup(t, TASK_WOKEN_INIT); |
| 2411 | } |
| 2412 | |
| 2413 | for (px = proxies_list; px; px = px->next) { |
| 2414 | struct server *srv; |
| 2415 | |
| 2416 | for (srv = px->srv; srv; srv = srv->next) { |
| 2417 | struct resolvers *resolvers; |
| 2418 | |
| 2419 | if (!srv->resolvers_id) |
| 2420 | continue; |
| 2421 | |
| 2422 | if ((resolvers = find_resolvers_by_id(srv->resolvers_id)) == NULL) { |
Amaury Denoyelle | 1112430 | 2021-06-04 18:22:08 +0200 | [diff] [blame] | 2423 | ha_alert("%s '%s', server '%s': unable to find required resolvers '%s'\n", |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 2424 | proxy_type_str(px), px->id, srv->id, srv->resolvers_id); |
| 2425 | err_code |= (ERR_ALERT|ERR_ABORT); |
| 2426 | continue; |
| 2427 | } |
| 2428 | srv->resolvers = resolvers; |
| 2429 | |
| 2430 | if (srv->srvrq && !srv->srvrq->resolvers) { |
| 2431 | srv->srvrq->resolvers = srv->resolvers; |
| 2432 | if (resolv_link_resolution(srv->srvrq, OBJ_TYPE_SRVRQ, 0) == -1) { |
Amaury Denoyelle | 1112430 | 2021-06-04 18:22:08 +0200 | [diff] [blame] | 2433 | ha_alert("%s '%s' : unable to set DNS resolution for server '%s'.\n", |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 2434 | proxy_type_str(px), px->id, srv->id); |
| 2435 | err_code |= (ERR_ALERT|ERR_ABORT); |
| 2436 | continue; |
| 2437 | } |
| 2438 | } |
Christopher Faulet | d83a6df | 2021-03-12 10:23:05 +0100 | [diff] [blame] | 2439 | if (!srv->srvrq && resolv_link_resolution(srv, OBJ_TYPE_SERVER, 0) == -1) { |
Amaury Denoyelle | 1112430 | 2021-06-04 18:22:08 +0200 | [diff] [blame] | 2440 | ha_alert("%s '%s', unable to set DNS resolution for server '%s'.\n", |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 2441 | proxy_type_str(px), px->id, srv->id); |
| 2442 | err_code |= (ERR_ALERT|ERR_ABORT); |
| 2443 | continue; |
| 2444 | } |
| 2445 | } |
| 2446 | } |
| 2447 | |
| 2448 | if (err_code & (ERR_ALERT|ERR_ABORT)) |
| 2449 | goto err; |
| 2450 | |
| 2451 | return err_code; |
| 2452 | err: |
| 2453 | resolvers_deinit(); |
| 2454 | return err_code; |
| 2455 | |
| 2456 | } |
| 2457 | |
| 2458 | static int stats_dump_resolv_to_buffer(struct stream_interface *si, |
| 2459 | struct dns_nameserver *ns, |
| 2460 | struct field *stats, size_t stats_count, |
| 2461 | struct list *stat_modules) |
| 2462 | { |
| 2463 | struct appctx *appctx = __objt_appctx(si->end); |
| 2464 | struct channel *rep = si_ic(si); |
| 2465 | struct stats_module *mod; |
| 2466 | size_t idx = 0; |
| 2467 | |
| 2468 | memset(stats, 0, sizeof(struct field) * stats_count); |
| 2469 | |
| 2470 | list_for_each_entry(mod, stat_modules, list) { |
| 2471 | struct counters_node *counters = EXTRA_COUNTERS_GET(ns->extra_counters, mod); |
| 2472 | |
| 2473 | mod->fill_stats(counters, stats + idx); |
| 2474 | idx += mod->stats_count; |
| 2475 | } |
| 2476 | |
| 2477 | if (!stats_dump_one_line(stats, idx, appctx)) |
| 2478 | return 0; |
| 2479 | |
| 2480 | if (!stats_putchk(rep, NULL, &trash)) |
| 2481 | goto full; |
| 2482 | |
| 2483 | return 1; |
| 2484 | |
| 2485 | full: |
| 2486 | si_rx_room_rdy(si); |
| 2487 | return 0; |
| 2488 | } |
| 2489 | |
| 2490 | /* Uses <appctx.ctx.stats.obj1> as a pointer to the current resolver and <obj2> |
| 2491 | * as a pointer to the current nameserver. |
| 2492 | */ |
| 2493 | int stats_dump_resolvers(struct stream_interface *si, |
| 2494 | struct field *stats, size_t stats_count, |
| 2495 | struct list *stat_modules) |
| 2496 | { |
| 2497 | struct appctx *appctx = __objt_appctx(si->end); |
| 2498 | struct channel *rep = si_ic(si); |
| 2499 | struct resolvers *resolver = appctx->ctx.stats.obj1; |
| 2500 | struct dns_nameserver *ns = appctx->ctx.stats.obj2; |
| 2501 | |
| 2502 | if (!resolver) |
| 2503 | resolver = LIST_NEXT(&sec_resolvers, struct resolvers *, list); |
| 2504 | |
| 2505 | /* dump resolvers */ |
| 2506 | list_for_each_entry_from(resolver, &sec_resolvers, list) { |
| 2507 | appctx->ctx.stats.obj1 = resolver; |
| 2508 | |
| 2509 | ns = appctx->ctx.stats.obj2 ? |
| 2510 | appctx->ctx.stats.obj2 : |
| 2511 | LIST_NEXT(&resolver->nameservers, struct dns_nameserver *, list); |
| 2512 | |
| 2513 | list_for_each_entry_from(ns, &resolver->nameservers, list) { |
| 2514 | appctx->ctx.stats.obj2 = ns; |
| 2515 | |
| 2516 | if (buffer_almost_full(&rep->buf)) |
| 2517 | goto full; |
| 2518 | |
| 2519 | if (!stats_dump_resolv_to_buffer(si, ns, |
| 2520 | stats, stats_count, |
| 2521 | stat_modules)) { |
| 2522 | return 0; |
| 2523 | } |
| 2524 | } |
| 2525 | |
| 2526 | appctx->ctx.stats.obj2 = NULL; |
| 2527 | } |
| 2528 | |
| 2529 | return 1; |
| 2530 | |
| 2531 | full: |
| 2532 | si_rx_room_blk(si); |
| 2533 | return 0; |
| 2534 | } |
| 2535 | |
| 2536 | void resolv_stats_clear_counters(int clrall, struct list *stat_modules) |
| 2537 | { |
| 2538 | struct resolvers *resolvers; |
| 2539 | struct dns_nameserver *ns; |
| 2540 | struct stats_module *mod; |
| 2541 | void *counters; |
| 2542 | |
| 2543 | list_for_each_entry(mod, stat_modules, list) { |
| 2544 | if (!mod->clearable && !clrall) |
| 2545 | continue; |
| 2546 | |
| 2547 | list_for_each_entry(resolvers, &sec_resolvers, list) { |
| 2548 | list_for_each_entry(ns, &resolvers->nameservers, list) { |
| 2549 | counters = EXTRA_COUNTERS_GET(ns->extra_counters, mod); |
| 2550 | memcpy(counters, mod->counters, mod->counters_size); |
| 2551 | } |
| 2552 | } |
| 2553 | } |
| 2554 | |
| 2555 | } |
| 2556 | |
| 2557 | int resolv_allocate_counters(struct list *stat_modules) |
| 2558 | { |
| 2559 | struct stats_module *mod; |
| 2560 | struct resolvers *resolvers; |
| 2561 | struct dns_nameserver *ns; |
| 2562 | |
| 2563 | list_for_each_entry(resolvers, &sec_resolvers, list) { |
| 2564 | list_for_each_entry(ns, &resolvers->nameservers, list) { |
| 2565 | EXTRA_COUNTERS_REGISTER(&ns->extra_counters, COUNTERS_DNS, |
| 2566 | alloc_failed); |
| 2567 | |
| 2568 | list_for_each_entry(mod, stat_modules, list) { |
| 2569 | EXTRA_COUNTERS_ADD(mod, |
| 2570 | ns->extra_counters, |
| 2571 | mod->counters, |
| 2572 | mod->counters_size); |
| 2573 | } |
| 2574 | |
| 2575 | EXTRA_COUNTERS_ALLOC(ns->extra_counters, alloc_failed); |
| 2576 | |
| 2577 | list_for_each_entry(mod, stat_modules, list) { |
| 2578 | memcpy(ns->extra_counters->data + mod->counters_off[ns->extra_counters->type], |
| 2579 | mod->counters, mod->counters_size); |
| 2580 | |
| 2581 | /* Store the ns counters pointer */ |
| 2582 | if (strcmp(mod->name, "dns") == 0) { |
| 2583 | ns->counters = (struct dns_counters *)ns->extra_counters->data + mod->counters_off[COUNTERS_DNS]; |
| 2584 | ns->counters->id = ns->id; |
| 2585 | ns->counters->pid = resolvers->id; |
| 2586 | } |
| 2587 | } |
| 2588 | } |
| 2589 | } |
| 2590 | |
| 2591 | return 1; |
| 2592 | |
| 2593 | alloc_failed: |
| 2594 | return 0; |
| 2595 | } |
| 2596 | |
| 2597 | /* if an arg is found, it sets the resolvers section pointer into cli.p0 */ |
| 2598 | static int cli_parse_stat_resolvers(char **args, char *payload, struct appctx *appctx, void *private) |
| 2599 | { |
| 2600 | struct resolvers *presolvers; |
| 2601 | |
| 2602 | if (*args[2]) { |
| 2603 | list_for_each_entry(presolvers, &sec_resolvers, list) { |
| 2604 | if (strcmp(presolvers->id, args[2]) == 0) { |
| 2605 | appctx->ctx.cli.p0 = presolvers; |
| 2606 | break; |
| 2607 | } |
| 2608 | } |
| 2609 | if (appctx->ctx.cli.p0 == NULL) |
| 2610 | return cli_err(appctx, "Can't find that resolvers section\n"); |
| 2611 | } |
| 2612 | return 0; |
| 2613 | } |
| 2614 | |
| 2615 | /* Dumps counters from all resolvers section and associated name servers. It |
| 2616 | * returns 0 if the output buffer is full and it needs to be called again, |
| 2617 | * otherwise non-zero. It may limit itself to the resolver pointed to by |
| 2618 | * <cli.p0> if it's not null. |
| 2619 | */ |
| 2620 | static int cli_io_handler_dump_resolvers_to_buffer(struct appctx *appctx) |
| 2621 | { |
| 2622 | struct stream_interface *si = appctx->owner; |
| 2623 | struct resolvers *resolvers; |
| 2624 | struct dns_nameserver *ns; |
| 2625 | |
| 2626 | chunk_reset(&trash); |
| 2627 | |
| 2628 | switch (appctx->st2) { |
| 2629 | case STAT_ST_INIT: |
| 2630 | appctx->st2 = STAT_ST_LIST; /* let's start producing data */ |
| 2631 | /* fall through */ |
| 2632 | |
| 2633 | case STAT_ST_LIST: |
| 2634 | if (LIST_ISEMPTY(&sec_resolvers)) { |
| 2635 | chunk_appendf(&trash, "No resolvers found\n"); |
| 2636 | } |
| 2637 | else { |
| 2638 | list_for_each_entry(resolvers, &sec_resolvers, list) { |
| 2639 | if (appctx->ctx.cli.p0 != NULL && appctx->ctx.cli.p0 != resolvers) |
| 2640 | continue; |
| 2641 | |
| 2642 | chunk_appendf(&trash, "Resolvers section %s\n", resolvers->id); |
| 2643 | list_for_each_entry(ns, &resolvers->nameservers, list) { |
| 2644 | chunk_appendf(&trash, " nameserver %s:\n", ns->id); |
| 2645 | chunk_appendf(&trash, " sent: %lld\n", ns->counters->sent); |
| 2646 | chunk_appendf(&trash, " snd_error: %lld\n", ns->counters->snd_error); |
| 2647 | chunk_appendf(&trash, " valid: %lld\n", ns->counters->valid); |
| 2648 | chunk_appendf(&trash, " update: %lld\n", ns->counters->update); |
| 2649 | chunk_appendf(&trash, " cname: %lld\n", ns->counters->cname); |
| 2650 | chunk_appendf(&trash, " cname_error: %lld\n", ns->counters->cname_error); |
| 2651 | chunk_appendf(&trash, " any_err: %lld\n", ns->counters->any_err); |
| 2652 | chunk_appendf(&trash, " nx: %lld\n", ns->counters->nx); |
| 2653 | chunk_appendf(&trash, " timeout: %lld\n", ns->counters->timeout); |
| 2654 | chunk_appendf(&trash, " refused: %lld\n", ns->counters->refused); |
| 2655 | chunk_appendf(&trash, " other: %lld\n", ns->counters->other); |
| 2656 | chunk_appendf(&trash, " invalid: %lld\n", ns->counters->invalid); |
| 2657 | chunk_appendf(&trash, " too_big: %lld\n", ns->counters->too_big); |
| 2658 | chunk_appendf(&trash, " truncated: %lld\n", ns->counters->truncated); |
| 2659 | chunk_appendf(&trash, " outdated: %lld\n", ns->counters->outdated); |
| 2660 | } |
| 2661 | chunk_appendf(&trash, "\n"); |
| 2662 | } |
| 2663 | } |
| 2664 | |
| 2665 | /* display response */ |
| 2666 | if (ci_putchk(si_ic(si), &trash) == -1) { |
| 2667 | /* let's try again later from this session. We add ourselves into |
| 2668 | * this session's users so that it can remove us upon termination. |
| 2669 | */ |
| 2670 | si_rx_room_blk(si); |
| 2671 | return 0; |
| 2672 | } |
| 2673 | /* fall through */ |
| 2674 | |
| 2675 | default: |
| 2676 | appctx->st2 = STAT_ST_FIN; |
| 2677 | return 1; |
| 2678 | } |
| 2679 | } |
| 2680 | |
| 2681 | /* register cli keywords */ |
| 2682 | static struct cli_kw_list cli_kws = {{ }, { |
Willy Tarreau | b205bfd | 2021-05-07 11:38:37 +0200 | [diff] [blame] | 2683 | { { "show", "resolvers", NULL }, "show resolvers [id] : dumps counters from all resolvers section and associated name servers", |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 2684 | cli_parse_stat_resolvers, cli_io_handler_dump_resolvers_to_buffer }, |
| 2685 | {{},} |
| 2686 | } |
| 2687 | }; |
| 2688 | |
| 2689 | INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws); |
| 2690 | |
| 2691 | /* |
| 2692 | * Prepare <rule> for hostname resolution. |
| 2693 | * Returns -1 in case of any allocation failure, 0 if not. |
| 2694 | * On error, a global failure counter is also incremented. |
| 2695 | */ |
| 2696 | static int action_prepare_for_resolution(struct stream *stream, const char *hostname) |
| 2697 | { |
| 2698 | char *hostname_dn; |
| 2699 | int hostname_len, hostname_dn_len; |
| 2700 | struct buffer *tmp = get_trash_chunk(); |
| 2701 | |
| 2702 | if (!hostname) |
| 2703 | return 0; |
| 2704 | |
| 2705 | hostname_len = strlen(hostname); |
| 2706 | hostname_dn = tmp->area; |
| 2707 | hostname_dn_len = resolv_str_to_dn_label(hostname, hostname_len + 1, |
| 2708 | hostname_dn, tmp->size); |
| 2709 | if (hostname_dn_len == -1) |
| 2710 | goto err; |
| 2711 | |
| 2712 | |
| 2713 | stream->resolv_ctx.hostname_dn = strdup(hostname_dn); |
| 2714 | stream->resolv_ctx.hostname_dn_len = hostname_dn_len; |
| 2715 | if (!stream->resolv_ctx.hostname_dn) |
| 2716 | goto err; |
| 2717 | |
| 2718 | return 0; |
| 2719 | |
| 2720 | err: |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 2721 | ha_free(&stream->resolv_ctx.hostname_dn); |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 2722 | resolv_failed_resolutions += 1; |
| 2723 | return -1; |
| 2724 | } |
| 2725 | |
| 2726 | |
| 2727 | /* |
| 2728 | * Execute the "do-resolution" action. May be called from {tcp,http}request. |
| 2729 | */ |
| 2730 | enum act_return resolv_action_do_resolve(struct act_rule *rule, struct proxy *px, |
| 2731 | struct session *sess, struct stream *s, int flags) |
| 2732 | { |
| 2733 | struct resolv_resolution *resolution; |
| 2734 | struct sample *smp; |
| 2735 | char *fqdn; |
| 2736 | struct resolv_requester *req; |
| 2737 | struct resolvers *resolvers; |
| 2738 | struct resolv_resolution *res; |
| 2739 | int exp, locked = 0; |
| 2740 | enum act_return ret = ACT_RET_CONT; |
| 2741 | |
| 2742 | resolvers = rule->arg.resolv.resolvers; |
| 2743 | |
| 2744 | /* we have a response to our DNS resolution */ |
| 2745 | use_cache: |
| 2746 | if (s->resolv_ctx.requester && s->resolv_ctx.requester->resolution != NULL) { |
| 2747 | resolution = s->resolv_ctx.requester->resolution; |
| 2748 | if (!locked) { |
| 2749 | HA_SPIN_LOCK(DNS_LOCK, &resolvers->lock); |
| 2750 | locked = 1; |
| 2751 | } |
| 2752 | |
| 2753 | if (resolution->step == RSLV_STEP_RUNNING) |
| 2754 | goto yield; |
| 2755 | if (resolution->step == RSLV_STEP_NONE) { |
| 2756 | /* We update the variable only if we have a valid response. */ |
| 2757 | if (resolution->status == RSLV_STATUS_VALID) { |
| 2758 | struct sample smp; |
| 2759 | short ip_sin_family = 0; |
| 2760 | void *ip = NULL; |
| 2761 | |
| 2762 | resolv_get_ip_from_response(&resolution->response, rule->arg.resolv.opts, NULL, |
| 2763 | 0, &ip, &ip_sin_family, NULL); |
| 2764 | |
| 2765 | switch (ip_sin_family) { |
| 2766 | case AF_INET: |
| 2767 | smp.data.type = SMP_T_IPV4; |
| 2768 | memcpy(&smp.data.u.ipv4, ip, 4); |
| 2769 | break; |
| 2770 | case AF_INET6: |
| 2771 | smp.data.type = SMP_T_IPV6; |
| 2772 | memcpy(&smp.data.u.ipv6, ip, 16); |
| 2773 | break; |
| 2774 | default: |
| 2775 | ip = NULL; |
| 2776 | } |
| 2777 | |
| 2778 | if (ip) { |
| 2779 | smp.px = px; |
| 2780 | smp.sess = sess; |
| 2781 | smp.strm = s; |
| 2782 | |
| 2783 | vars_set_by_name(rule->arg.resolv.varname, strlen(rule->arg.resolv.varname), &smp); |
| 2784 | } |
| 2785 | } |
| 2786 | } |
| 2787 | |
| 2788 | goto release_requester; |
| 2789 | } |
| 2790 | |
| 2791 | /* need to configure and start a new DNS resolution */ |
| 2792 | smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.resolv.expr, SMP_T_STR); |
| 2793 | if (smp == NULL) |
| 2794 | goto end; |
| 2795 | |
| 2796 | fqdn = smp->data.u.str.area; |
| 2797 | if (action_prepare_for_resolution(s, fqdn) == -1) |
| 2798 | goto end; /* on error, ignore the action */ |
| 2799 | |
| 2800 | s->resolv_ctx.parent = rule; |
| 2801 | |
| 2802 | HA_SPIN_LOCK(DNS_LOCK, &resolvers->lock); |
| 2803 | locked = 1; |
| 2804 | |
| 2805 | resolv_link_resolution(s, OBJ_TYPE_STREAM, 0); |
| 2806 | |
| 2807 | /* Check if there is a fresh enough response in the cache of our associated resolution */ |
| 2808 | req = s->resolv_ctx.requester; |
| 2809 | if (!req || !req->resolution) |
| 2810 | goto release_requester; /* on error, ignore the action */ |
| 2811 | res = req->resolution; |
| 2812 | |
| 2813 | exp = tick_add(res->last_resolution, resolvers->hold.valid); |
| 2814 | if (resolvers->t && res->status == RSLV_STATUS_VALID && tick_isset(res->last_resolution) |
| 2815 | && !tick_is_expired(exp, now_ms)) { |
| 2816 | goto use_cache; |
| 2817 | } |
| 2818 | |
| 2819 | resolv_trigger_resolution(s->resolv_ctx.requester); |
| 2820 | |
| 2821 | yield: |
| 2822 | if (flags & ACT_OPT_FINAL) |
| 2823 | goto release_requester; |
| 2824 | ret = ACT_RET_YIELD; |
| 2825 | |
| 2826 | end: |
| 2827 | if (locked) |
| 2828 | HA_SPIN_UNLOCK(DNS_LOCK, &resolvers->lock); |
| 2829 | return ret; |
| 2830 | |
| 2831 | release_requester: |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 2832 | ha_free(&s->resolv_ctx.hostname_dn); |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 2833 | s->resolv_ctx.hostname_dn_len = 0; |
| 2834 | if (s->resolv_ctx.requester) { |
Christopher Faulet | 0efc099 | 2021-03-11 18:09:53 +0100 | [diff] [blame] | 2835 | resolv_unlink_resolution(s->resolv_ctx.requester, 0); |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 2836 | pool_free(resolv_requester_pool, s->resolv_ctx.requester); |
| 2837 | s->resolv_ctx.requester = NULL; |
| 2838 | } |
| 2839 | goto end; |
| 2840 | } |
| 2841 | |
| 2842 | static void release_resolv_action(struct act_rule *rule) |
| 2843 | { |
| 2844 | release_sample_expr(rule->arg.resolv.expr); |
| 2845 | free(rule->arg.resolv.varname); |
| 2846 | free(rule->arg.resolv.resolvers_id); |
| 2847 | free(rule->arg.resolv.opts); |
| 2848 | } |
| 2849 | |
| 2850 | |
| 2851 | /* parse "do-resolve" action |
| 2852 | * This action takes the following arguments: |
| 2853 | * do-resolve(<varName>,<resolversSectionName>,<resolvePrefer>) <expr> |
| 2854 | * |
| 2855 | * - <varName> is the variable name where the result of the DNS resolution will be stored |
| 2856 | * (mandatory) |
| 2857 | * - <resolversSectionName> is the name of the resolvers section to use to perform the resolution |
| 2858 | * (mandatory) |
| 2859 | * - <resolvePrefer> can be either 'ipv4' or 'ipv6' and is the IP family we would like to resolve first |
| 2860 | * (optional), defaults to ipv6 |
| 2861 | * - <expr> is an HAProxy expression used to fetch the name to be resolved |
| 2862 | */ |
| 2863 | enum act_parse_ret resolv_parse_do_resolve(const char **args, int *orig_arg, struct proxy *px, struct act_rule *rule, char **err) |
| 2864 | { |
| 2865 | int cur_arg; |
| 2866 | struct sample_expr *expr; |
| 2867 | unsigned int where; |
| 2868 | const char *beg, *end; |
| 2869 | |
| 2870 | /* orig_arg points to the first argument, but we need to analyse the command itself first */ |
| 2871 | cur_arg = *orig_arg - 1; |
| 2872 | |
| 2873 | /* locate varName, which is mandatory */ |
| 2874 | beg = strchr(args[cur_arg], '('); |
| 2875 | if (beg == NULL) |
| 2876 | goto do_resolve_parse_error; |
| 2877 | beg = beg + 1; /* beg should points to the first character after opening parenthesis '(' */ |
| 2878 | end = strchr(beg, ','); |
| 2879 | if (end == NULL) |
| 2880 | goto do_resolve_parse_error; |
| 2881 | rule->arg.resolv.varname = my_strndup(beg, end - beg); |
| 2882 | if (rule->arg.resolv.varname == NULL) |
| 2883 | goto do_resolve_parse_error; |
| 2884 | |
| 2885 | |
| 2886 | /* locate resolversSectionName, which is mandatory. |
| 2887 | * Since next parameters are optional, the delimiter may be comma ',' |
| 2888 | * or closing parenthesis ')' |
| 2889 | */ |
| 2890 | beg = end + 1; |
| 2891 | end = strchr(beg, ','); |
| 2892 | if (end == NULL) |
| 2893 | end = strchr(beg, ')'); |
| 2894 | if (end == NULL) |
| 2895 | goto do_resolve_parse_error; |
| 2896 | rule->arg.resolv.resolvers_id = my_strndup(beg, end - beg); |
| 2897 | if (rule->arg.resolv.resolvers_id == NULL) |
| 2898 | goto do_resolve_parse_error; |
| 2899 | |
| 2900 | |
| 2901 | rule->arg.resolv.opts = calloc(1, sizeof(*rule->arg.resolv.opts)); |
| 2902 | if (rule->arg.resolv.opts == NULL) |
| 2903 | goto do_resolve_parse_error; |
| 2904 | |
| 2905 | /* Default priority is ipv6 */ |
| 2906 | rule->arg.resolv.opts->family_prio = AF_INET6; |
| 2907 | |
| 2908 | /* optional arguments accepted for now: |
| 2909 | * ipv4 or ipv6 |
| 2910 | */ |
| 2911 | while (*end != ')') { |
| 2912 | beg = end + 1; |
| 2913 | end = strchr(beg, ','); |
| 2914 | if (end == NULL) |
| 2915 | end = strchr(beg, ')'); |
| 2916 | if (end == NULL) |
| 2917 | goto do_resolve_parse_error; |
| 2918 | |
| 2919 | if (strncmp(beg, "ipv4", end - beg) == 0) { |
| 2920 | rule->arg.resolv.opts->family_prio = AF_INET; |
| 2921 | } |
| 2922 | else if (strncmp(beg, "ipv6", end - beg) == 0) { |
| 2923 | rule->arg.resolv.opts->family_prio = AF_INET6; |
| 2924 | } |
| 2925 | else { |
| 2926 | goto do_resolve_parse_error; |
| 2927 | } |
| 2928 | } |
| 2929 | |
| 2930 | cur_arg = cur_arg + 1; |
| 2931 | |
| 2932 | expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args, NULL); |
| 2933 | if (!expr) |
| 2934 | goto do_resolve_parse_error; |
| 2935 | |
| 2936 | |
| 2937 | where = 0; |
| 2938 | if (px->cap & PR_CAP_FE) |
| 2939 | where |= SMP_VAL_FE_HRQ_HDR; |
| 2940 | if (px->cap & PR_CAP_BE) |
| 2941 | where |= SMP_VAL_BE_HRQ_HDR; |
| 2942 | |
| 2943 | if (!(expr->fetch->val & where)) { |
| 2944 | memprintf(err, |
| 2945 | "fetch method '%s' extracts information from '%s', none of which is available here", |
| 2946 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
| 2947 | free(expr); |
| 2948 | return ACT_RET_PRS_ERR; |
| 2949 | } |
| 2950 | rule->arg.resolv.expr = expr; |
| 2951 | rule->action = ACT_CUSTOM; |
| 2952 | rule->action_ptr = resolv_action_do_resolve; |
| 2953 | *orig_arg = cur_arg; |
| 2954 | |
| 2955 | rule->check_ptr = check_action_do_resolve; |
| 2956 | rule->release_ptr = release_resolv_action; |
| 2957 | |
| 2958 | return ACT_RET_PRS_OK; |
| 2959 | |
| 2960 | do_resolve_parse_error: |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 2961 | ha_free(&rule->arg.resolv.varname); |
| 2962 | ha_free(&rule->arg.resolv.resolvers_id); |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 2963 | memprintf(err, "Can't parse '%s'. Expects 'do-resolve(<varname>,<resolvers>[,<options>]) <expr>'. Available options are 'ipv4' and 'ipv6'", |
| 2964 | args[cur_arg]); |
| 2965 | return ACT_RET_PRS_ERR; |
| 2966 | } |
| 2967 | |
| 2968 | static struct action_kw_list http_req_kws = { { }, { |
Amaury Denoyelle | e4a617c | 2021-05-06 15:33:09 +0200 | [diff] [blame] | 2969 | { "do-resolve", resolv_parse_do_resolve, KWF_MATCH_PREFIX }, |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 2970 | { /* END */ } |
| 2971 | }}; |
| 2972 | |
| 2973 | INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_kws); |
| 2974 | |
| 2975 | static struct action_kw_list tcp_req_cont_actions = {ILH, { |
Amaury Denoyelle | e4a617c | 2021-05-06 15:33:09 +0200 | [diff] [blame] | 2976 | { "do-resolve", resolv_parse_do_resolve, KWF_MATCH_PREFIX }, |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 2977 | { /* END */ } |
| 2978 | }}; |
| 2979 | |
| 2980 | INITCALL1(STG_REGISTER, tcp_req_cont_keywords_register, &tcp_req_cont_actions); |
| 2981 | |
| 2982 | /* Check an "http-request do-resolve" action. |
| 2983 | * |
| 2984 | * The function returns 1 in success case, otherwise, it returns 0 and err is |
| 2985 | * filled. |
| 2986 | */ |
| 2987 | int check_action_do_resolve(struct act_rule *rule, struct proxy *px, char **err) |
| 2988 | { |
| 2989 | struct resolvers *resolvers = NULL; |
| 2990 | |
| 2991 | if (rule->arg.resolv.resolvers_id == NULL) { |
| 2992 | memprintf(err,"Proxy '%s': %s", px->id, "do-resolve action without resolvers"); |
| 2993 | return 0; |
| 2994 | } |
| 2995 | |
| 2996 | resolvers = find_resolvers_by_id(rule->arg.resolv.resolvers_id); |
| 2997 | if (resolvers == NULL) { |
| 2998 | memprintf(err,"Can't find resolvers section '%s' for do-resolve action", rule->arg.resolv.resolvers_id); |
| 2999 | return 0; |
| 3000 | } |
| 3001 | rule->arg.resolv.resolvers = resolvers; |
| 3002 | |
| 3003 | return 1; |
| 3004 | } |
| 3005 | |
| 3006 | void resolvers_setup_proxy(struct proxy *px) |
| 3007 | { |
| 3008 | px->last_change = now.tv_sec; |
| 3009 | px->cap = PR_CAP_FE | PR_CAP_BE; |
| 3010 | px->maxconn = 0; |
| 3011 | px->conn_retries = 1; |
| 3012 | px->timeout.server = TICK_ETERNITY; |
| 3013 | px->timeout.client = TICK_ETERNITY; |
| 3014 | px->timeout.connect = TICK_ETERNITY; |
| 3015 | px->accept = NULL; |
| 3016 | px->options2 |= PR_O2_INDEPSTR | PR_O2_SMARTCON; |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 3017 | } |
| 3018 | |
| 3019 | /* |
| 3020 | * Parse a <resolvers> section. |
| 3021 | * Returns the error code, 0 if OK, or any combination of : |
| 3022 | * - ERR_ABORT: must abort ASAP |
| 3023 | * - ERR_FATAL: we can continue parsing but not start the service |
| 3024 | * - ERR_WARN: a warning has been emitted |
| 3025 | * - ERR_ALERT: an alert has been emitted |
| 3026 | * Only the two first ones can stop processing, the two others are just |
| 3027 | * indicators. |
| 3028 | */ |
| 3029 | int cfg_parse_resolvers(const char *file, int linenum, char **args, int kwm) |
| 3030 | { |
| 3031 | const char *err; |
| 3032 | int err_code = 0; |
| 3033 | char *errmsg = NULL; |
| 3034 | struct proxy *p; |
| 3035 | |
| 3036 | if (strcmp(args[0], "resolvers") == 0) { /* new resolvers section */ |
| 3037 | if (!*args[1]) { |
| 3038 | ha_alert("parsing [%s:%d] : missing name for resolvers section.\n", file, linenum); |
| 3039 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3040 | goto out; |
| 3041 | } |
| 3042 | |
| 3043 | err = invalid_char(args[1]); |
| 3044 | if (err) { |
| 3045 | ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n", |
| 3046 | file, linenum, *err, args[0], args[1]); |
| 3047 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3048 | goto out; |
| 3049 | } |
| 3050 | |
| 3051 | list_for_each_entry(curr_resolvers, &sec_resolvers, list) { |
| 3052 | /* Error if two resolvers owns the same name */ |
| 3053 | if (strcmp(curr_resolvers->id, args[1]) == 0) { |
| 3054 | ha_alert("Parsing [%s:%d]: resolvers '%s' has same name as another resolvers (declared at %s:%d).\n", |
| 3055 | file, linenum, args[1], curr_resolvers->conf.file, curr_resolvers->conf.line); |
| 3056 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3057 | } |
| 3058 | } |
| 3059 | |
| 3060 | if ((curr_resolvers = calloc(1, sizeof(*curr_resolvers))) == NULL) { |
| 3061 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
| 3062 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3063 | goto out; |
| 3064 | } |
| 3065 | |
| 3066 | /* allocate new proxy to tcp servers */ |
| 3067 | p = calloc(1, sizeof *p); |
| 3068 | if (!p) { |
| 3069 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
| 3070 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3071 | goto out; |
| 3072 | } |
| 3073 | |
| 3074 | init_new_proxy(p); |
| 3075 | resolvers_setup_proxy(p); |
| 3076 | p->parent = curr_resolvers; |
| 3077 | p->id = strdup(args[1]); |
| 3078 | p->conf.args.file = p->conf.file = strdup(file); |
| 3079 | p->conf.args.line = p->conf.line = linenum; |
| 3080 | curr_resolvers->px = p; |
| 3081 | |
| 3082 | /* default values */ |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 3083 | LIST_APPEND(&sec_resolvers, &curr_resolvers->list); |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 3084 | curr_resolvers->conf.file = strdup(file); |
| 3085 | curr_resolvers->conf.line = linenum; |
| 3086 | curr_resolvers->id = strdup(args[1]); |
| 3087 | curr_resolvers->query_ids = EB_ROOT; |
| 3088 | /* default maximum response size */ |
| 3089 | curr_resolvers->accepted_payload_size = 512; |
| 3090 | /* default hold period for nx, other, refuse and timeout is 30s */ |
| 3091 | curr_resolvers->hold.nx = 30000; |
| 3092 | curr_resolvers->hold.other = 30000; |
| 3093 | curr_resolvers->hold.refused = 30000; |
| 3094 | curr_resolvers->hold.timeout = 30000; |
| 3095 | curr_resolvers->hold.obsolete = 0; |
| 3096 | /* default hold period for valid is 10s */ |
| 3097 | curr_resolvers->hold.valid = 10000; |
| 3098 | curr_resolvers->timeout.resolve = 1000; |
| 3099 | curr_resolvers->timeout.retry = 1000; |
| 3100 | curr_resolvers->resolve_retries = 3; |
| 3101 | LIST_INIT(&curr_resolvers->nameservers); |
| 3102 | LIST_INIT(&curr_resolvers->resolutions.curr); |
| 3103 | LIST_INIT(&curr_resolvers->resolutions.wait); |
| 3104 | HA_SPIN_INIT(&curr_resolvers->lock); |
| 3105 | } |
| 3106 | else if (strcmp(args[0], "nameserver") == 0) { /* nameserver definition */ |
| 3107 | struct dns_nameserver *newnameserver = NULL; |
| 3108 | struct sockaddr_storage *sk; |
| 3109 | int port1, port2; |
Emeric Brun | c8f3e45 | 2021-04-07 16:04:54 +0200 | [diff] [blame] | 3110 | struct protocol *proto; |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 3111 | |
| 3112 | if (!*args[2]) { |
| 3113 | ha_alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n", |
| 3114 | file, linenum, args[0]); |
| 3115 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3116 | goto out; |
| 3117 | } |
| 3118 | |
| 3119 | err = invalid_char(args[1]); |
| 3120 | if (err) { |
| 3121 | ha_alert("parsing [%s:%d] : character '%c' is not permitted in server name '%s'.\n", |
| 3122 | file, linenum, *err, args[1]); |
| 3123 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3124 | goto out; |
| 3125 | } |
| 3126 | |
| 3127 | list_for_each_entry(newnameserver, &curr_resolvers->nameservers, list) { |
| 3128 | /* Error if two resolvers owns the same name */ |
| 3129 | if (strcmp(newnameserver->id, args[1]) == 0) { |
| 3130 | ha_alert("Parsing [%s:%d]: nameserver '%s' has same name as another nameserver (declared at %s:%d).\n", |
| 3131 | file, linenum, args[1], newnameserver->conf.file, newnameserver->conf.line); |
| 3132 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3133 | } |
| 3134 | } |
| 3135 | |
Emeric Brun | c8f3e45 | 2021-04-07 16:04:54 +0200 | [diff] [blame] | 3136 | sk = str2sa_range(args[2], NULL, &port1, &port2, NULL, &proto, |
| 3137 | &errmsg, NULL, NULL, PA_O_RESOLVE | PA_O_PORT_OK | PA_O_PORT_MAND | PA_O_DGRAM | PA_O_STREAM | PA_O_DEFAULT_DGRAM); |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 3138 | if (!sk) { |
| 3139 | ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg); |
| 3140 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3141 | goto out; |
| 3142 | } |
| 3143 | |
| 3144 | if ((newnameserver = calloc(1, sizeof(*newnameserver))) == NULL) { |
| 3145 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
| 3146 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3147 | goto out; |
| 3148 | } |
| 3149 | |
Emeric Brun | c8f3e45 | 2021-04-07 16:04:54 +0200 | [diff] [blame] | 3150 | if (proto && proto->ctrl_type == SOCK_STREAM) { |
| 3151 | err_code |= parse_server(file, linenum, args, curr_resolvers->px, NULL, |
| 3152 | SRV_PARSE_PARSE_ADDR|SRV_PARSE_INITIAL_RESOLVE); |
| 3153 | if (err_code & (ERR_FATAL|ERR_ABORT)) { |
| 3154 | err_code |= ERR_ABORT; |
| 3155 | goto out; |
| 3156 | } |
| 3157 | |
| 3158 | if (dns_stream_init(newnameserver, curr_resolvers->px->srv) < 0) { |
| 3159 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
| 3160 | err_code |= ERR_ALERT|ERR_ABORT; |
| 3161 | goto out; |
| 3162 | } |
| 3163 | } |
| 3164 | else if (dns_dgram_init(newnameserver, sk) < 0) { |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 3165 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
| 3166 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3167 | goto out; |
| 3168 | } |
| 3169 | |
| 3170 | if ((newnameserver->conf.file = strdup(file)) == NULL) { |
| 3171 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
| 3172 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3173 | goto out; |
| 3174 | } |
| 3175 | |
| 3176 | if ((newnameserver->id = strdup(args[1])) == NULL) { |
| 3177 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
| 3178 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3179 | goto out; |
| 3180 | } |
| 3181 | |
| 3182 | newnameserver->parent = curr_resolvers; |
| 3183 | newnameserver->process_responses = resolv_process_responses; |
| 3184 | newnameserver->conf.line = linenum; |
| 3185 | /* the nameservers are linked backward first */ |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 3186 | LIST_APPEND(&curr_resolvers->nameservers, &newnameserver->list); |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 3187 | } |
| 3188 | else if (strcmp(args[0], "parse-resolv-conf") == 0) { |
| 3189 | struct dns_nameserver *newnameserver = NULL; |
| 3190 | const char *whitespace = "\r\n\t "; |
| 3191 | char *resolv_line = NULL; |
| 3192 | int resolv_linenum = 0; |
| 3193 | FILE *f = NULL; |
| 3194 | char *address = NULL; |
| 3195 | struct sockaddr_storage *sk = NULL; |
| 3196 | struct protocol *proto; |
| 3197 | int duplicate_name = 0; |
| 3198 | |
| 3199 | if ((resolv_line = malloc(sizeof(*resolv_line) * LINESIZE)) == NULL) { |
| 3200 | ha_alert("parsing [%s:%d] : out of memory.\n", |
| 3201 | file, linenum); |
| 3202 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3203 | goto resolv_out; |
| 3204 | } |
| 3205 | |
| 3206 | if ((f = fopen("/etc/resolv.conf", "r")) == NULL) { |
| 3207 | ha_alert("parsing [%s:%d] : failed to open /etc/resolv.conf.\n", |
| 3208 | file, linenum); |
| 3209 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3210 | goto resolv_out; |
| 3211 | } |
| 3212 | |
| 3213 | sk = calloc(1, sizeof(*sk)); |
| 3214 | if (sk == NULL) { |
| 3215 | ha_alert("parsing [/etc/resolv.conf:%d] : out of memory.\n", |
| 3216 | resolv_linenum); |
| 3217 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3218 | goto resolv_out; |
| 3219 | } |
| 3220 | |
| 3221 | while (fgets(resolv_line, LINESIZE, f) != NULL) { |
| 3222 | resolv_linenum++; |
| 3223 | if (strncmp(resolv_line, "nameserver", 10) != 0) |
| 3224 | continue; |
| 3225 | |
| 3226 | address = strtok(resolv_line + 10, whitespace); |
| 3227 | if (address == resolv_line + 10) |
| 3228 | continue; |
| 3229 | |
| 3230 | if (address == NULL) { |
| 3231 | ha_warning("parsing [/etc/resolv.conf:%d] : nameserver line is missing address.\n", |
| 3232 | resolv_linenum); |
| 3233 | err_code |= ERR_WARN; |
| 3234 | continue; |
| 3235 | } |
| 3236 | |
| 3237 | duplicate_name = 0; |
| 3238 | list_for_each_entry(newnameserver, &curr_resolvers->nameservers, list) { |
| 3239 | if (strcmp(newnameserver->id, address) == 0) { |
| 3240 | ha_warning("Parsing [/etc/resolv.conf:%d] : generated name for /etc/resolv.conf nameserver '%s' conflicts with another nameserver (declared at %s:%d), it appears to be a duplicate and will be excluded.\n", |
| 3241 | resolv_linenum, address, newnameserver->conf.file, newnameserver->conf.line); |
| 3242 | err_code |= ERR_WARN; |
| 3243 | duplicate_name = 1; |
| 3244 | } |
| 3245 | } |
| 3246 | |
| 3247 | if (duplicate_name) |
| 3248 | continue; |
| 3249 | |
| 3250 | memset(sk, 0, sizeof(*sk)); |
| 3251 | if (!str2ip2(address, sk, 1)) { |
| 3252 | ha_warning("parsing [/etc/resolv.conf:%d] : address '%s' could not be recognized, nameserver will be excluded.\n", |
| 3253 | resolv_linenum, address); |
| 3254 | err_code |= ERR_WARN; |
| 3255 | continue; |
| 3256 | } |
| 3257 | |
| 3258 | set_host_port(sk, 53); |
| 3259 | |
| 3260 | proto = protocol_by_family(sk->ss_family); |
| 3261 | if (!proto || !proto->connect) { |
| 3262 | ha_warning("parsing [/etc/resolv.conf:%d] : '%s' : connect() not supported for this address family.\n", |
| 3263 | resolv_linenum, address); |
| 3264 | err_code |= ERR_WARN; |
| 3265 | continue; |
| 3266 | } |
| 3267 | |
| 3268 | if ((newnameserver = calloc(1, sizeof(*newnameserver))) == NULL) { |
| 3269 | ha_alert("parsing [/etc/resolv.conf:%d] : out of memory.\n", resolv_linenum); |
| 3270 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3271 | goto resolv_out; |
| 3272 | } |
| 3273 | |
| 3274 | if (dns_dgram_init(newnameserver, sk) < 0) { |
| 3275 | ha_alert("parsing [/etc/resolv.conf:%d] : out of memory.\n", resolv_linenum); |
| 3276 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3277 | free(newnameserver); |
| 3278 | goto resolv_out; |
| 3279 | } |
| 3280 | |
| 3281 | newnameserver->conf.file = strdup("/etc/resolv.conf"); |
| 3282 | if (newnameserver->conf.file == NULL) { |
| 3283 | ha_alert("parsing [/etc/resolv.conf:%d] : out of memory.\n", resolv_linenum); |
| 3284 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3285 | free(newnameserver); |
| 3286 | goto resolv_out; |
| 3287 | } |
| 3288 | |
| 3289 | newnameserver->id = strdup(address); |
| 3290 | if (newnameserver->id == NULL) { |
| 3291 | ha_alert("parsing [/etc/resolv.conf:%d] : out of memory.\n", resolv_linenum); |
| 3292 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3293 | free((char *)newnameserver->conf.file); |
| 3294 | free(newnameserver); |
| 3295 | goto resolv_out; |
| 3296 | } |
| 3297 | |
| 3298 | newnameserver->parent = curr_resolvers; |
| 3299 | newnameserver->process_responses = resolv_process_responses; |
| 3300 | newnameserver->conf.line = resolv_linenum; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 3301 | LIST_APPEND(&curr_resolvers->nameservers, &newnameserver->list); |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 3302 | } |
| 3303 | |
| 3304 | resolv_out: |
| 3305 | free(sk); |
| 3306 | free(resolv_line); |
| 3307 | if (f != NULL) |
| 3308 | fclose(f); |
| 3309 | } |
| 3310 | else if (strcmp(args[0], "hold") == 0) { /* hold periods */ |
| 3311 | const char *res; |
| 3312 | unsigned int time; |
| 3313 | |
| 3314 | if (!*args[2]) { |
| 3315 | ha_alert("parsing [%s:%d] : '%s' expects an <event> and a <time> as arguments.\n", |
| 3316 | file, linenum, args[0]); |
| 3317 | ha_alert("<event> can be either 'valid', 'nx', 'refused', 'timeout', or 'other'\n"); |
| 3318 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3319 | goto out; |
| 3320 | } |
| 3321 | res = parse_time_err(args[2], &time, TIME_UNIT_MS); |
| 3322 | if (res == PARSE_TIME_OVER) { |
| 3323 | ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s>, maximum value is 2147483647 ms (~24.8 days).\n", |
| 3324 | file, linenum, args[1], args[0]); |
| 3325 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3326 | goto out; |
| 3327 | } |
| 3328 | else if (res == PARSE_TIME_UNDER) { |
| 3329 | ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s>, minimum non-null value is 1 ms.\n", |
| 3330 | file, linenum, args[1], args[0]); |
| 3331 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3332 | goto out; |
| 3333 | } |
| 3334 | else if (res) { |
| 3335 | ha_alert("parsing [%s:%d]: unexpected character '%c' in argument to <%s>.\n", |
| 3336 | file, linenum, *res, args[0]); |
| 3337 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3338 | goto out; |
| 3339 | } |
| 3340 | if (strcmp(args[1], "nx") == 0) |
| 3341 | curr_resolvers->hold.nx = time; |
| 3342 | else if (strcmp(args[1], "other") == 0) |
| 3343 | curr_resolvers->hold.other = time; |
| 3344 | else if (strcmp(args[1], "refused") == 0) |
| 3345 | curr_resolvers->hold.refused = time; |
| 3346 | else if (strcmp(args[1], "timeout") == 0) |
| 3347 | curr_resolvers->hold.timeout = time; |
| 3348 | else if (strcmp(args[1], "valid") == 0) |
| 3349 | curr_resolvers->hold.valid = time; |
| 3350 | else if (strcmp(args[1], "obsolete") == 0) |
| 3351 | curr_resolvers->hold.obsolete = time; |
| 3352 | else { |
| 3353 | ha_alert("parsing [%s:%d] : '%s' unknown <event>: '%s', expects either 'nx', 'timeout', 'valid', 'obsolete' or 'other'.\n", |
| 3354 | file, linenum, args[0], args[1]); |
| 3355 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3356 | goto out; |
| 3357 | } |
| 3358 | |
| 3359 | } |
| 3360 | else if (strcmp(args[0], "accepted_payload_size") == 0) { |
| 3361 | int i = 0; |
| 3362 | |
| 3363 | if (!*args[1]) { |
| 3364 | ha_alert("parsing [%s:%d] : '%s' expects <nb> as argument.\n", |
| 3365 | file, linenum, args[0]); |
| 3366 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3367 | goto out; |
| 3368 | } |
| 3369 | |
| 3370 | i = atoi(args[1]); |
| 3371 | if (i < DNS_HEADER_SIZE || i > DNS_MAX_UDP_MESSAGE) { |
| 3372 | ha_alert("parsing [%s:%d] : '%s' must be between %d and %d inclusive (was %s).\n", |
| 3373 | file, linenum, args[0], DNS_HEADER_SIZE, DNS_MAX_UDP_MESSAGE, args[1]); |
| 3374 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3375 | goto out; |
| 3376 | } |
| 3377 | |
| 3378 | curr_resolvers->accepted_payload_size = i; |
| 3379 | } |
| 3380 | else if (strcmp(args[0], "resolution_pool_size") == 0) { |
| 3381 | ha_alert("parsing [%s:%d] : '%s' directive is not supported anymore (it never appeared in a stable release).\n", |
| 3382 | file, linenum, args[0]); |
| 3383 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3384 | goto out; |
| 3385 | } |
| 3386 | else if (strcmp(args[0], "resolve_retries") == 0) { |
| 3387 | if (!*args[1]) { |
| 3388 | ha_alert("parsing [%s:%d] : '%s' expects <nb> as argument.\n", |
| 3389 | file, linenum, args[0]); |
| 3390 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3391 | goto out; |
| 3392 | } |
| 3393 | curr_resolvers->resolve_retries = atoi(args[1]); |
| 3394 | } |
| 3395 | else if (strcmp(args[0], "timeout") == 0) { |
| 3396 | if (!*args[1]) { |
| 3397 | ha_alert("parsing [%s:%d] : '%s' expects 'retry' or 'resolve' and <time> as arguments.\n", |
| 3398 | file, linenum, args[0]); |
| 3399 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3400 | goto out; |
| 3401 | } |
| 3402 | else if (strcmp(args[1], "retry") == 0 || |
| 3403 | strcmp(args[1], "resolve") == 0) { |
| 3404 | const char *res; |
| 3405 | unsigned int tout; |
| 3406 | |
| 3407 | if (!*args[2]) { |
| 3408 | ha_alert("parsing [%s:%d] : '%s %s' expects <time> as argument.\n", |
| 3409 | file, linenum, args[0], args[1]); |
| 3410 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3411 | goto out; |
| 3412 | } |
| 3413 | res = parse_time_err(args[2], &tout, TIME_UNIT_MS); |
| 3414 | if (res == PARSE_TIME_OVER) { |
| 3415 | ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s %s>, maximum value is 2147483647 ms (~24.8 days).\n", |
| 3416 | file, linenum, args[2], args[0], args[1]); |
| 3417 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3418 | goto out; |
| 3419 | } |
| 3420 | else if (res == PARSE_TIME_UNDER) { |
| 3421 | ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s %s>, minimum non-null value is 1 ms.\n", |
| 3422 | file, linenum, args[2], args[0], args[1]); |
| 3423 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3424 | goto out; |
| 3425 | } |
| 3426 | else if (res) { |
| 3427 | ha_alert("parsing [%s:%d]: unexpected character '%c' in argument to <%s %s>.\n", |
| 3428 | file, linenum, *res, args[0], args[1]); |
| 3429 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3430 | goto out; |
| 3431 | } |
| 3432 | if (args[1][2] == 't') |
| 3433 | curr_resolvers->timeout.retry = tout; |
| 3434 | else |
| 3435 | curr_resolvers->timeout.resolve = tout; |
| 3436 | } |
| 3437 | else { |
| 3438 | ha_alert("parsing [%s:%d] : '%s' expects 'retry' or 'resolve' and <time> as arguments got '%s'.\n", |
| 3439 | file, linenum, args[0], args[1]); |
| 3440 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3441 | goto out; |
| 3442 | } |
| 3443 | } |
| 3444 | else if (*args[0] != 0) { |
| 3445 | ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section\n", file, linenum, args[0], cursection); |
| 3446 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3447 | goto out; |
| 3448 | } |
| 3449 | |
| 3450 | out: |
| 3451 | free(errmsg); |
| 3452 | return err_code; |
| 3453 | } |
Emeric Brun | 56fc5d9 | 2021-02-12 20:05:45 +0100 | [diff] [blame] | 3454 | int cfg_post_parse_resolvers() |
| 3455 | { |
| 3456 | int err_code = 0; |
| 3457 | struct server *srv; |
| 3458 | |
| 3459 | if (curr_resolvers) { |
| 3460 | |
| 3461 | /* prepare forward server descriptors */ |
| 3462 | if (curr_resolvers->px) { |
| 3463 | srv = curr_resolvers->px->srv; |
| 3464 | while (srv) { |
Emeric Brun | 56fc5d9 | 2021-02-12 20:05:45 +0100 | [diff] [blame] | 3465 | /* init ssl if needed */ |
| 3466 | if (srv->use_ssl == 1 && xprt_get(XPRT_SSL) && xprt_get(XPRT_SSL)->prepare_srv) { |
| 3467 | if (xprt_get(XPRT_SSL)->prepare_srv(srv)) { |
| 3468 | ha_alert("unable to prepare SSL for server '%s' in resolvers section '%s'.\n", srv->id, curr_resolvers->id); |
| 3469 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3470 | break; |
| 3471 | } |
| 3472 | } |
Emeric Brun | 56fc5d9 | 2021-02-12 20:05:45 +0100 | [diff] [blame] | 3473 | srv = srv->next; |
| 3474 | } |
| 3475 | } |
| 3476 | } |
| 3477 | curr_resolvers = NULL; |
| 3478 | return err_code; |
| 3479 | } |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 3480 | |
Emeric Brun | 56fc5d9 | 2021-02-12 20:05:45 +0100 | [diff] [blame] | 3481 | REGISTER_CONFIG_SECTION("resolvers", cfg_parse_resolvers, cfg_post_parse_resolvers); |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 3482 | REGISTER_POST_DEINIT(resolvers_deinit); |
| 3483 | REGISTER_CONFIG_POSTPARSER("dns runtime resolver", resolvers_finalize_config); |