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