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