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