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