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