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