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