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