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