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