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