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