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