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