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