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