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