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