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