Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Name server resolution |
| 3 | * |
| 4 | * Copyright 2014 Baptiste Assmann <bedis9@gmail.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #include <errno.h> |
| 14 | #include <fcntl.h> |
| 15 | #include <stdio.h> |
| 16 | #include <stdlib.h> |
| 17 | #include <string.h> |
| 18 | #include <unistd.h> |
| 19 | |
| 20 | #include <sys/types.h> |
| 21 | |
Baptiste Assmann | 044fd5b | 2018-08-10 10:56:38 +0200 | [diff] [blame] | 22 | #include <common/cfgparse.h> |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 23 | #include <common/errors.h> |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 24 | #include <common/initcall.h> |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 25 | #include <common/time.h> |
| 26 | #include <common/ticks.h> |
Olivier Houchard | 8da5f98 | 2017-08-04 18:35:36 +0200 | [diff] [blame] | 27 | #include <common/net_helper.h> |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 28 | |
Baptiste Assmann | 333939c | 2019-01-21 08:34:50 +0100 | [diff] [blame] | 29 | #include <types/action.h> |
William Lallemand | 69e9644 | 2016-11-19 00:58:54 +0100 | [diff] [blame] | 30 | #include <types/applet.h> |
| 31 | #include <types/cli.h> |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 32 | #include <types/global.h> |
| 33 | #include <types/dns.h> |
William Lallemand | 69e9644 | 2016-11-19 00:58:54 +0100 | [diff] [blame] | 34 | #include <types/stats.h> |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 35 | |
Baptiste Assmann | 333939c | 2019-01-21 08:34:50 +0100 | [diff] [blame] | 36 | #include <proto/action.h> |
William Lallemand | 69e9644 | 2016-11-19 00:58:54 +0100 | [diff] [blame] | 37 | #include <proto/channel.h> |
| 38 | #include <proto/cli.h> |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 39 | #include <proto/checks.h> |
| 40 | #include <proto/dns.h> |
| 41 | #include <proto/fd.h> |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 42 | #include <proto/http_ana.h> |
Baptiste Assmann | 333939c | 2019-01-21 08:34:50 +0100 | [diff] [blame] | 43 | #include <proto/http_rules.h> |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 44 | #include <proto/log.h> |
Baptiste Assmann | 333939c | 2019-01-21 08:34:50 +0100 | [diff] [blame] | 45 | #include <proto/sample.h> |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 46 | #include <proto/server.h> |
| 47 | #include <proto/task.h> |
| 48 | #include <proto/proto_udp.h> |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 49 | #include <proto/proxy.h> |
William Lallemand | 69e9644 | 2016-11-19 00:58:54 +0100 | [diff] [blame] | 50 | #include <proto/stream_interface.h> |
Baptiste Assmann | 333939c | 2019-01-21 08:34:50 +0100 | [diff] [blame] | 51 | #include <proto/tcp_rules.h> |
| 52 | #include <proto/vars.h> |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 53 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 54 | struct list dns_resolvers = LIST_HEAD_INIT(dns_resolvers); |
| 55 | struct list dns_srvrq_list = LIST_HEAD_INIT(dns_srvrq_list); |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 56 | |
Tim Duesterhus | fcac33d | 2020-01-18 02:04:12 +0100 | [diff] [blame] | 57 | static THREAD_LOCAL uint64_t dns_query_id_seed = 0; /* random seed */ |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 58 | |
| 59 | DECLARE_STATIC_POOL(dns_answer_item_pool, "dns_answer_item", sizeof(struct dns_answer_item)); |
| 60 | DECLARE_STATIC_POOL(dns_resolution_pool, "dns_resolution", sizeof(struct dns_resolution)); |
Baptiste Assmann | dfd35fd | 2019-01-21 08:18:09 +0100 | [diff] [blame] | 61 | DECLARE_POOL(dns_requester_pool, "dns_requester", sizeof(struct dns_requester)); |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 62 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 63 | static unsigned int resolution_uuid = 1; |
Baptiste Assmann | 333939c | 2019-01-21 08:34:50 +0100 | [diff] [blame] | 64 | unsigned int dns_failed_resolutions = 0; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 65 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 66 | /* Returns a pointer to the resolvers matching the id <id>. NULL is returned if |
| 67 | * no match is found. |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 68 | */ |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 69 | struct dns_resolvers *find_resolvers_by_id(const char *id) |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 70 | { |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 71 | struct dns_resolvers *res; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 72 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 73 | list_for_each_entry(res, &dns_resolvers, list) { |
| 74 | if (!strcmp(res->id, id)) |
| 75 | return res; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 76 | } |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 77 | return NULL; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 78 | } |
| 79 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 80 | /* Returns a pointer on the SRV request matching the name <name> for the proxy |
| 81 | * <px>. NULL is returned if no match is found. |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 82 | */ |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 83 | 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] | 84 | { |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 85 | struct dns_srvrq *srvrq; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 86 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 87 | list_for_each_entry(srvrq, &dns_srvrq_list, list) { |
| 88 | if (srvrq->proxy == px && !strcmp(srvrq->name, name)) |
| 89 | return srvrq; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 90 | } |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 91 | return NULL; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 92 | } |
| 93 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 94 | /* Allocates a new SRVRQ for the given server with the name <fqdn>. It returns |
| 95 | * NULL if an error occurred. */ |
| 96 | struct dns_srvrq *new_dns_srvrq(struct server *srv, char *fqdn) |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 97 | { |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 98 | struct proxy *px = srv->proxy; |
| 99 | struct dns_srvrq *srvrq = NULL; |
| 100 | int fqdn_len, hostname_dn_len; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 101 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 102 | fqdn_len = strlen(fqdn); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 103 | hostname_dn_len = dns_str_to_dn_label(fqdn, fqdn_len + 1, trash.area, |
| 104 | trash.size); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 105 | if (hostname_dn_len == -1) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 106 | ha_alert("config : %s '%s', server '%s': failed to parse FQDN '%s'\n", |
| 107 | proxy_type_str(px), px->id, srv->id, fqdn); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 108 | goto err; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 109 | } |
| 110 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 111 | if ((srvrq = calloc(1, sizeof(*srvrq))) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 112 | ha_alert("config : %s '%s', server '%s': out of memory\n", |
| 113 | proxy_type_str(px), px->id, srv->id); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 114 | goto err; |
| 115 | } |
| 116 | srvrq->obj_type = OBJ_TYPE_SRVRQ; |
| 117 | srvrq->proxy = px; |
| 118 | srvrq->name = strdup(fqdn); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 119 | srvrq->hostname_dn = strdup(trash.area); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 120 | srvrq->hostname_dn_len = hostname_dn_len; |
| 121 | if (!srvrq->name || !srvrq->hostname_dn) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 122 | ha_alert("config : %s '%s', server '%s': out of memory\n", |
| 123 | proxy_type_str(px), px->id, srv->id); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 124 | goto err; |
| 125 | } |
| 126 | LIST_ADDQ(&dns_srvrq_list, &srvrq->list); |
| 127 | return srvrq; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 128 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 129 | err: |
| 130 | if (srvrq) { |
| 131 | free(srvrq->name); |
| 132 | free(srvrq->hostname_dn); |
| 133 | free(srvrq); |
| 134 | } |
| 135 | return NULL; |
| 136 | } |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 137 | |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 138 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 139 | /* 2 bytes random generator to generate DNS query ID */ |
| 140 | static inline uint16_t dns_rnd16(void) |
| 141 | { |
Christopher Faulet | b2812a6 | 2017-10-04 16:17:58 +0200 | [diff] [blame] | 142 | if (!dns_query_id_seed) |
| 143 | dns_query_id_seed = now_ms; |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 144 | dns_query_id_seed ^= dns_query_id_seed << 13; |
| 145 | dns_query_id_seed ^= dns_query_id_seed >> 7; |
| 146 | dns_query_id_seed ^= dns_query_id_seed << 17; |
| 147 | return dns_query_id_seed; |
| 148 | } |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 149 | |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 150 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 151 | static inline int dns_resolution_timeout(struct dns_resolution *res) |
| 152 | { |
Baptiste Assmann | f50e1ac | 2019-11-07 11:02:18 +0100 | [diff] [blame] | 153 | return res->resolvers->timeout.resolve; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 154 | } |
| 155 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 156 | /* Updates a resolvers' task timeout for next wake up and queue it */ |
| 157 | static void dns_update_resolvers_timeout(struct dns_resolvers *resolvers) |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 158 | { |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 159 | struct dns_resolution *res; |
| 160 | int next; |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 161 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 162 | next = tick_add(now_ms, resolvers->timeout.resolve); |
| 163 | if (!LIST_ISEMPTY(&resolvers->resolutions.curr)) { |
| 164 | res = LIST_NEXT(&resolvers->resolutions.curr, struct dns_resolution *, list); |
| 165 | next = MIN(next, tick_add(res->last_query, resolvers->timeout.retry)); |
| 166 | } |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 167 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 168 | list_for_each_entry(res, &resolvers->resolutions.wait, list) |
| 169 | next = MIN(next, tick_add(res->last_resolution, dns_resolution_timeout(res))); |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 170 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 171 | resolvers->t->expire = next; |
| 172 | task_queue(resolvers->t); |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 173 | } |
| 174 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 175 | /* Opens an UDP socket on the namesaver's IP/Port, if required. Returns 0 on |
| 176 | * success, -1 otherwise. |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 177 | */ |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 178 | static int dns_connect_namesaver(struct dns_nameserver *ns) |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 179 | { |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 180 | struct dgram_conn *dgram = ns->dgram; |
| 181 | int fd; |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 182 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 183 | /* Already connected */ |
| 184 | if (dgram->t.sock.fd != -1) |
| 185 | return 0; |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 186 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 187 | /* Create an UDP socket and connect it on the nameserver's IP/Port */ |
| 188 | if ((fd = socket(ns->addr.ss_family, SOCK_DGRAM, IPPROTO_UDP)) == -1) { |
| 189 | send_log(NULL, LOG_WARNING, |
| 190 | "DNS : resolvers '%s': can't create socket for nameserver '%s'.\n", |
| 191 | ns->resolvers->id, ns->id); |
| 192 | return -1; |
| 193 | } |
| 194 | if (connect(fd, (struct sockaddr*)&ns->addr, get_addr_len(&ns->addr)) == -1) { |
| 195 | send_log(NULL, LOG_WARNING, |
| 196 | "DNS : resolvers '%s': can't connect socket for nameserver '%s'.\n", |
| 197 | ns->resolvers->id, ns->id); |
| 198 | close(fd); |
| 199 | return -1; |
| 200 | } |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 201 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 202 | /* Make the socket non blocking */ |
| 203 | fcntl(fd, F_SETFL, O_NONBLOCK); |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 204 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 205 | /* Add the fd in the fd list and update its parameters */ |
| 206 | dgram->t.sock.fd = fd; |
Willy Tarreau | a9786b6 | 2018-01-25 07:22:13 +0100 | [diff] [blame] | 207 | fd_insert(fd, dgram, dgram_fd_handler, MAX_THREADS_MASK); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 208 | fd_want_recv(fd); |
| 209 | return 0; |
| 210 | } |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 211 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 212 | /* Forges a DNS query. It needs the following information from the caller: |
| 213 | * - <query_id> : the DNS query id corresponding to this query |
| 214 | * - <query_type> : DNS_RTYPE_* request DNS record type (A, AAAA, ANY...) |
| 215 | * - <hostname_dn> : hostname in domain name format |
| 216 | * - <hostname_dn_len> : length of <hostname_dn> |
| 217 | * |
| 218 | * To store the query, the caller must pass a buffer <buf> and its size |
| 219 | * <bufsize>. It returns the number of written bytes in success, -1 if <buf> is |
| 220 | * too short. |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 221 | */ |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 222 | static int dns_build_query(int query_id, int query_type, unsigned int accepted_payload_size, |
| 223 | char *hostname_dn, int hostname_dn_len, char *buf, int bufsize) |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 224 | { |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 225 | struct dns_header dns_hdr; |
| 226 | struct dns_question qinfo; |
| 227 | struct dns_additional_record edns; |
| 228 | char *p = buf; |
Baptiste Assmann | fa4a663 | 2017-05-04 09:05:00 +0200 | [diff] [blame] | 229 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 230 | if (sizeof(dns_hdr) + sizeof(qinfo) + sizeof(edns) + hostname_dn_len >= bufsize) |
| 231 | return -1; |
Baptiste Assmann | fa4a663 | 2017-05-04 09:05:00 +0200 | [diff] [blame] | 232 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 233 | memset(buf, 0, bufsize); |
Baptiste Assmann | fa4a663 | 2017-05-04 09:05:00 +0200 | [diff] [blame] | 234 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 235 | /* Set dns query headers */ |
| 236 | dns_hdr.id = (unsigned short) htons(query_id); |
| 237 | dns_hdr.flags = htons(0x0100); /* qr=0, opcode=0, aa=0, tc=0, rd=1, ra=0, z=0, rcode=0 */ |
| 238 | dns_hdr.qdcount = htons(1); /* 1 question */ |
| 239 | dns_hdr.ancount = 0; |
| 240 | dns_hdr.nscount = 0; |
| 241 | dns_hdr.arcount = htons(1); |
| 242 | memcpy(p, &dns_hdr, sizeof(dns_hdr)); |
| 243 | p += sizeof(dns_hdr); |
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 up query hostname */ |
| 246 | memcpy(p, hostname_dn, hostname_dn_len); |
| 247 | p += hostname_dn_len; |
| 248 | *p++ = 0; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 249 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 250 | /* Set up query info (type and class) */ |
| 251 | qinfo.qtype = htons(query_type); |
| 252 | qinfo.qclass = htons(DNS_RCLASS_IN); |
| 253 | memcpy(p, &qinfo, sizeof(qinfo)); |
| 254 | p += sizeof(qinfo); |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 255 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 256 | /* Set the DNS extension */ |
| 257 | edns.name = 0; |
| 258 | edns.type = htons(DNS_RTYPE_OPT); |
| 259 | edns.udp_payload_size = htons(accepted_payload_size); |
| 260 | edns.extension = 0; |
| 261 | edns.data_length = 0; |
| 262 | memcpy(p, &edns, sizeof(edns)); |
| 263 | p += sizeof(edns); |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 264 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 265 | return (p - buf); |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 266 | } |
| 267 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 268 | /* Sends a DNS query to resolvers associated to a resolution. It returns 0 on |
| 269 | * success, -1 otherwise. |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 270 | */ |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 271 | static int dns_send_query(struct dns_resolution *resolution) |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 272 | { |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 273 | struct dns_resolvers *resolvers = resolution->resolvers; |
| 274 | struct dns_nameserver *ns; |
Willy Tarreau | 0eae632 | 2019-12-20 11:18:54 +0100 | [diff] [blame] | 275 | int len; |
| 276 | |
| 277 | /* Update resolution */ |
| 278 | resolution->nb_queries = 0; |
| 279 | resolution->nb_responses = 0; |
| 280 | resolution->last_query = now_ms; |
| 281 | |
| 282 | len = dns_build_query(resolution->query_id, resolution->query_type, |
| 283 | resolvers->accepted_payload_size, |
| 284 | resolution->hostname_dn, resolution->hostname_dn_len, |
| 285 | trash.area, trash.size); |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 286 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 287 | list_for_each_entry(ns, &resolvers->nameservers, list) { |
| 288 | int fd = ns->dgram->t.sock.fd; |
Willy Tarreau | 0eae632 | 2019-12-20 11:18:54 +0100 | [diff] [blame] | 289 | int ret; |
| 290 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 291 | if (fd == -1) { |
| 292 | if (dns_connect_namesaver(ns) == -1) |
| 293 | continue; |
| 294 | fd = ns->dgram->t.sock.fd; |
| 295 | resolvers->nb_nameservers++; |
| 296 | } |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 297 | |
Willy Tarreau | 0eae632 | 2019-12-20 11:18:54 +0100 | [diff] [blame] | 298 | if (len < 0) |
| 299 | goto snd_error; |
| 300 | |
| 301 | ret = send(fd, trash.area, len, 0); |
| 302 | if (ret == len) { |
| 303 | ns->counters.sent++; |
| 304 | resolution->nb_queries++; |
| 305 | continue; |
| 306 | } |
| 307 | |
| 308 | if (ret == -1 && errno == EAGAIN) { |
| 309 | /* retry once the socket is ready */ |
| 310 | fd_cant_send(fd); |
| 311 | continue; |
| 312 | } |
| 313 | |
| 314 | snd_error: |
| 315 | ns->counters.snd_error++; |
| 316 | resolution->nb_queries++; |
| 317 | } |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 318 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 319 | /* Push the resolution at the end of the active list */ |
| 320 | LIST_DEL(&resolution->list); |
| 321 | LIST_ADDQ(&resolvers->resolutions.curr, &resolution->list); |
| 322 | return 0; |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 323 | } |
| 324 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 325 | /* Prepares and sends a DNS resolution. It returns 1 if the query was sent, 0 if |
| 326 | * skipped and -1 if an error occurred. |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 327 | */ |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 328 | static int |
| 329 | dns_run_resolution(struct dns_resolution *resolution) |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 330 | { |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 331 | struct dns_resolvers *resolvers = resolution->resolvers; |
| 332 | int query_id, i; |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 333 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 334 | /* Avoid sending requests for resolutions that don't yet have an |
| 335 | * hostname, ie resolutions linked to servers that do not yet have an |
| 336 | * fqdn */ |
| 337 | if (!resolution->hostname_dn) |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 338 | return 0; |
| 339 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 340 | /* Check if a resolution has already been started for this server return |
| 341 | * directly to avoid resolution pill up. */ |
| 342 | if (resolution->step != RSLV_STEP_NONE) |
| 343 | return 0; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 344 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 345 | /* Generates a new query id. We try at most 100 times to find a free |
| 346 | * query id */ |
| 347 | for (i = 0; i < 100; ++i) { |
| 348 | query_id = dns_rnd16(); |
| 349 | if (!eb32_lookup(&resolvers->query_ids, query_id)) |
Olivier Houchard | 8da5f98 | 2017-08-04 18:35:36 +0200 | [diff] [blame] | 350 | break; |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 351 | query_id = -1; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 352 | } |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 353 | if (query_id == -1) { |
| 354 | send_log(NULL, LOG_NOTICE, |
| 355 | "could not generate a query id for %s, in resolvers %s.\n", |
| 356 | resolution->hostname_dn, resolvers->id); |
| 357 | return -1; |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 358 | } |
| 359 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 360 | /* Update resolution parameters */ |
| 361 | resolution->query_id = query_id; |
| 362 | resolution->qid.key = query_id; |
| 363 | resolution->step = RSLV_STEP_RUNNING; |
| 364 | resolution->query_type = resolution->prefered_query_type; |
| 365 | resolution->try = resolvers->resolve_retries; |
| 366 | eb32_insert(&resolvers->query_ids, &resolution->qid); |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 367 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 368 | /* Send the DNS query */ |
| 369 | resolution->try -= 1; |
| 370 | dns_send_query(resolution); |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 371 | return 1; |
| 372 | } |
| 373 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 374 | /* Performs a name resolution for the requester <req> */ |
| 375 | void dns_trigger_resolution(struct dns_requester *req) |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 376 | { |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 377 | struct dns_resolvers *resolvers; |
| 378 | struct dns_resolution *res; |
| 379 | int exp; |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 380 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 381 | if (!req || !req->resolution) |
| 382 | return; |
| 383 | res = req->resolution; |
| 384 | resolvers = res->resolvers; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 385 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 386 | /* The resolution must not be triggered yet. Use the cached response, if |
| 387 | * valid */ |
| 388 | exp = tick_add(res->last_resolution, resolvers->hold.valid); |
Olivier Houchard | f3d9e60 | 2018-05-22 18:40:07 +0200 | [diff] [blame] | 389 | if (resolvers->t && (res->status != RSLV_STATUS_VALID || |
| 390 | !tick_isset(res->last_resolution) || tick_is_expired(exp, now_ms))) |
| 391 | task_wakeup(resolvers->t, TASK_WOKEN_OTHER); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 392 | } |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 393 | |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 394 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 395 | /* Resets some resolution parameters to initial values and also delete the query |
| 396 | * ID from the resolver's tree. |
| 397 | */ |
| 398 | static void dns_reset_resolution(struct dns_resolution *resolution) |
| 399 | { |
| 400 | /* update resolution status */ |
| 401 | resolution->step = RSLV_STEP_NONE; |
| 402 | resolution->try = 0; |
| 403 | resolution->last_resolution = now_ms; |
| 404 | resolution->nb_queries = 0; |
| 405 | resolution->nb_responses = 0; |
| 406 | resolution->query_type = resolution->prefered_query_type; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 407 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 408 | /* clean up query id */ |
| 409 | eb32_delete(&resolution->qid); |
| 410 | resolution->query_id = 0; |
| 411 | resolution->qid.key = 0; |
| 412 | } |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 413 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 414 | /* Returns the query id contained in a DNS response */ |
| 415 | static inline unsigned short dns_response_get_query_id(unsigned char *resp) |
| 416 | { |
| 417 | return resp[0] * 256 + resp[1]; |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 418 | } |
| 419 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 420 | |
| 421 | /* Analyses, re-builds and copies the name <name> from the DNS response packet |
| 422 | * <buffer>. <name> must point to the 'data_len' information or pointer 'c0' |
| 423 | * for compressed data. The result is copied into <dest>, ensuring we don't |
| 424 | * overflow using <dest_len> Returns the number of bytes the caller can move |
| 425 | * forward. If 0 it means an error occurred while parsing the name. <offset> is |
| 426 | * the number of bytes the caller could move forward. |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 427 | */ |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 428 | int dns_read_name(unsigned char *buffer, unsigned char *bufend, |
| 429 | unsigned char *name, char *destination, int dest_len, |
Remi Gacogne | 58df5ae | 2018-12-05 17:52:54 +0100 | [diff] [blame] | 430 | int *offset, unsigned int depth) |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 431 | { |
| 432 | int nb_bytes = 0, n = 0; |
| 433 | int label_len; |
| 434 | unsigned char *reader = name; |
| 435 | char *dest = destination; |
| 436 | |
| 437 | while (1) { |
Remi Gacogne | 2d19fbc | 2018-12-05 17:55:10 +0100 | [diff] [blame] | 438 | if (reader >= bufend) |
| 439 | goto err; |
| 440 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 441 | /* Name compression is in use */ |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 442 | if ((*reader & 0xc0) == 0xc0) { |
Remi Gacogne | 2d19fbc | 2018-12-05 17:55:10 +0100 | [diff] [blame] | 443 | if (reader + 1 >= bufend) |
| 444 | goto err; |
| 445 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 446 | /* Must point BEFORE current position */ |
| 447 | if ((buffer + reader[1]) > reader) |
| 448 | goto err; |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 449 | |
Remi Gacogne | 58df5ae | 2018-12-05 17:52:54 +0100 | [diff] [blame] | 450 | if (depth++ > 100) |
| 451 | goto err; |
| 452 | |
Nikhil Agrawal | 2fa66c3 | 2018-12-20 10:50:59 +0530 | [diff] [blame] | 453 | n = dns_read_name(buffer, bufend, buffer + (*reader & 0x3f)*256 + reader[1], |
Remi Gacogne | 58df5ae | 2018-12-05 17:52:54 +0100 | [diff] [blame] | 454 | dest, dest_len - nb_bytes, offset, depth); |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 455 | if (n == 0) |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 456 | goto err; |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 457 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 458 | dest += n; |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 459 | nb_bytes += n; |
| 460 | goto out; |
| 461 | } |
| 462 | |
| 463 | label_len = *reader; |
| 464 | if (label_len == 0) |
| 465 | goto out; |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 466 | |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 467 | /* Check if: |
| 468 | * - we won't read outside the buffer |
| 469 | * - there is enough place in the destination |
| 470 | */ |
| 471 | if ((reader + label_len >= bufend) || (nb_bytes + label_len >= dest_len)) |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 472 | goto err; |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 473 | |
| 474 | /* +1 to take label len + label string */ |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 475 | label_len++; |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 476 | |
| 477 | memcpy(dest, reader, label_len); |
| 478 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 479 | dest += label_len; |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 480 | nb_bytes += label_len; |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 481 | reader += label_len; |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 482 | } |
| 483 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 484 | out: |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 485 | /* offset computation: |
| 486 | * parse from <name> until finding either NULL or a pointer "c0xx" |
| 487 | */ |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 488 | reader = name; |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 489 | *offset = 0; |
| 490 | while (reader < bufend) { |
| 491 | if ((reader[0] & 0xc0) == 0xc0) { |
| 492 | *offset += 2; |
| 493 | break; |
| 494 | } |
| 495 | else if (*reader == 0) { |
| 496 | *offset += 1; |
| 497 | break; |
| 498 | } |
| 499 | *offset += 1; |
| 500 | ++reader; |
| 501 | } |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 502 | return nb_bytes; |
| 503 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 504 | err: |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 505 | return 0; |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 506 | } |
| 507 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 508 | /* Checks for any obsolete record, also identify any SRV request, and try to |
| 509 | * find a corresponding server. |
| 510 | */ |
| 511 | static void dns_check_dns_response(struct dns_resolution *res) |
| 512 | { |
| 513 | struct dns_resolvers *resolvers = res->resolvers; |
| 514 | struct dns_requester *req, *reqback; |
| 515 | struct dns_answer_item *item, *itemback; |
| 516 | struct server *srv; |
| 517 | struct dns_srvrq *srvrq; |
| 518 | |
Baptiste Assmann | 13a9232 | 2019-06-07 09:40:55 +0200 | [diff] [blame] | 519 | /* clean up obsolete Additional records */ |
| 520 | list_for_each_entry_safe(item, itemback, &res->response.ar_list, list) { |
| 521 | if ((item->last_seen + resolvers->hold.obsolete / 1000) < now.tv_sec) { |
| 522 | LIST_DEL(&item->list); |
| 523 | pool_free(dns_answer_item_pool, item); |
| 524 | } |
| 525 | } |
| 526 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 527 | list_for_each_entry_safe(item, itemback, &res->response.answer_list, list) { |
| 528 | |
| 529 | /* Remove obsolete items */ |
| 530 | if ((item->last_seen + resolvers->hold.obsolete / 1000) < now.tv_sec) { |
| 531 | if (item->type != DNS_RTYPE_SRV) |
| 532 | goto rm_obselete_item; |
| 533 | |
| 534 | list_for_each_entry_safe(req, reqback, &res->requesters, list) { |
| 535 | if ((srvrq = objt_dns_srvrq(req->owner)) == NULL) |
| 536 | continue; |
| 537 | |
| 538 | /* Remove any associated server */ |
| 539 | for (srv = srvrq->proxy->srv; srv != NULL; srv = srv->next) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 540 | HA_SPIN_LOCK(SERVER_LOCK, &srv->lock); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 541 | if (srv->srvrq == srvrq && srv->svc_port == item->port && |
| 542 | item->data_len == srv->hostname_dn_len && |
| 543 | !memcmp(srv->hostname_dn, item->target, item->data_len)) { |
| 544 | snr_update_srv_status(srv, 1); |
| 545 | free(srv->hostname); |
| 546 | free(srv->hostname_dn); |
| 547 | srv->hostname = NULL; |
| 548 | srv->hostname_dn = NULL; |
| 549 | srv->hostname_dn_len = 0; |
| 550 | dns_unlink_resolution(srv->dns_requester); |
| 551 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 552 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 553 | } |
| 554 | } |
| 555 | |
| 556 | rm_obselete_item: |
| 557 | LIST_DEL(&item->list); |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 558 | pool_free(dns_answer_item_pool, item); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 559 | continue; |
| 560 | } |
| 561 | |
| 562 | if (item->type != DNS_RTYPE_SRV) |
| 563 | continue; |
| 564 | |
| 565 | /* Now process SRV records */ |
| 566 | list_for_each_entry_safe(req, reqback, &res->requesters, list) { |
| 567 | if ((srvrq = objt_dns_srvrq(req->owner)) == NULL) |
| 568 | continue; |
| 569 | |
| 570 | /* Check if a server already uses that hostname */ |
| 571 | for (srv = srvrq->proxy->srv; srv != NULL; srv = srv->next) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 572 | HA_SPIN_LOCK(SERVER_LOCK, &srv->lock); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 573 | if (srv->srvrq == srvrq && srv->svc_port == item->port && |
| 574 | item->data_len == srv->hostname_dn_len && |
Daniel Corbett | f871691 | 2019-11-17 09:48:56 -0500 | [diff] [blame] | 575 | !memcmp(srv->hostname_dn, item->target, item->data_len) && |
| 576 | !srv->dns_opts.ignore_weight) { |
Olivier Houchard | 2ec2db9 | 2018-01-08 16:28:57 +0100 | [diff] [blame] | 577 | int ha_weight; |
| 578 | |
Baptiste Assmann | 25e6fc2 | 2019-10-21 15:13:48 +0200 | [diff] [blame] | 579 | /* DNS weight range if from 0 to 65535 |
| 580 | * HAProxy weight is from 0 to 256 |
| 581 | * The rule below ensures that weight 0 is well respected |
| 582 | * while allowing a "mapping" from DNS weight into HAProxy's one. |
Olivier Houchard | 2ec2db9 | 2018-01-08 16:28:57 +0100 | [diff] [blame] | 583 | */ |
Baptiste Assmann | 25e6fc2 | 2019-10-21 15:13:48 +0200 | [diff] [blame] | 584 | ha_weight = (item->weight + 255) / 256; |
Olivier Houchard | 2ec2db9 | 2018-01-08 16:28:57 +0100 | [diff] [blame] | 585 | if (srv->uweight != ha_weight) { |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 586 | char weight[9]; |
| 587 | |
Olivier Houchard | 2ec2db9 | 2018-01-08 16:28:57 +0100 | [diff] [blame] | 588 | snprintf(weight, sizeof(weight), "%d", ha_weight); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 589 | server_parse_weight_change_request(srv, weight); |
| 590 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 591 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 592 | break; |
| 593 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 594 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 595 | } |
| 596 | if (srv) |
| 597 | continue; |
| 598 | |
| 599 | /* If not, try to find a server with undefined hostname */ |
| 600 | for (srv = srvrq->proxy->srv; srv != NULL; srv = srv->next) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 601 | HA_SPIN_LOCK(SERVER_LOCK, &srv->lock); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 602 | if (srv->srvrq == srvrq && !srv->hostname_dn) |
| 603 | break; |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 604 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 605 | } |
| 606 | /* And update this server, if found */ |
| 607 | if (srv) { |
| 608 | const char *msg = NULL; |
| 609 | char weight[9]; |
Olivier Houchard | 2ec2db9 | 2018-01-08 16:28:57 +0100 | [diff] [blame] | 610 | int ha_weight; |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 611 | char hostname[DNS_MAX_NAME_SIZE]; |
| 612 | |
| 613 | if (dns_dn_label_to_str(item->target, item->data_len+1, |
Olivier Houchard | 55dcdf4 | 2017-11-06 15:15:04 +0100 | [diff] [blame] | 614 | hostname, DNS_MAX_NAME_SIZE) == -1) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 615 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 616 | continue; |
Olivier Houchard | 55dcdf4 | 2017-11-06 15:15:04 +0100 | [diff] [blame] | 617 | } |
Baptiste Assmann | 13a9232 | 2019-06-07 09:40:55 +0200 | [diff] [blame] | 618 | |
| 619 | /* Check if an Additional Record is associated to this SRV record. |
| 620 | * Perform some sanity checks too to ensure the record can be used. |
| 621 | * If all fine, we simply pick up the IP address found and associate |
| 622 | * it to the server. |
| 623 | */ |
| 624 | if ((item->ar_item != NULL) && |
| 625 | (item->ar_item->type == DNS_RTYPE_A || item->ar_item->type == DNS_RTYPE_AAAA)) |
| 626 | { |
| 627 | |
| 628 | switch (item->ar_item->type) { |
| 629 | case DNS_RTYPE_A: |
| 630 | update_server_addr(srv, &(((struct sockaddr_in*)&item->ar_item->address)->sin_addr), AF_INET, "DNS additional recrd"); |
| 631 | break; |
| 632 | case DNS_RTYPE_AAAA: |
| 633 | update_server_addr(srv, &(((struct sockaddr_in6*)&item->ar_item->address)->sin6_addr), AF_INET6, "DNS additional recrd"); |
| 634 | break; |
| 635 | } |
| 636 | |
| 637 | srv->flags |= SRV_F_NO_RESOLUTION; |
| 638 | } |
| 639 | |
Olivier Houchard | d16bfe6 | 2017-10-31 15:21:19 +0100 | [diff] [blame] | 640 | msg = update_server_fqdn(srv, hostname, "SRV record", 1); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 641 | if (msg) |
| 642 | send_log(srv->proxy, LOG_NOTICE, "%s", msg); |
| 643 | |
| 644 | srv->svc_port = item->port; |
| 645 | srv->flags &= ~SRV_F_MAPPORTS; |
| 646 | if ((srv->check.state & CHK_ST_CONFIGURED) && |
| 647 | !(srv->flags & SRV_F_CHECKPORT)) |
| 648 | srv->check.port = item->port; |
Olivier Houchard | 2ec2db9 | 2018-01-08 16:28:57 +0100 | [diff] [blame] | 649 | |
Daniel Corbett | f871691 | 2019-11-17 09:48:56 -0500 | [diff] [blame] | 650 | if (!srv->dns_opts.ignore_weight) { |
| 651 | /* DNS weight range if from 0 to 65535 |
| 652 | * HAProxy weight is from 0 to 256 |
| 653 | * The rule below ensures that weight 0 is well respected |
| 654 | * while allowing a "mapping" from DNS weight into HAProxy's one. |
| 655 | */ |
| 656 | ha_weight = (item->weight + 255) / 256; |
Olivier Houchard | 2ec2db9 | 2018-01-08 16:28:57 +0100 | [diff] [blame] | 657 | |
Daniel Corbett | f871691 | 2019-11-17 09:48:56 -0500 | [diff] [blame] | 658 | snprintf(weight, sizeof(weight), "%d", ha_weight); |
| 659 | server_parse_weight_change_request(srv, weight); |
| 660 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 661 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 662 | } |
| 663 | } |
| 664 | } |
| 665 | } |
| 666 | |
| 667 | /* Validates that the buffer DNS response provided in <resp> and finishing |
| 668 | * before <bufend> is valid from a DNS protocol point of view. |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 669 | * |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 670 | * The result is stored in <resolution>' response, buf_response, |
| 671 | * response_query_records and response_answer_records members. |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 672 | * |
| 673 | * This function returns one of the DNS_RESP_* code to indicate the type of |
| 674 | * error found. |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 675 | */ |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 676 | static int dns_validate_dns_response(unsigned char *resp, unsigned char *bufend, |
| 677 | struct dns_resolution *resolution, int max_answer_records) |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 678 | { |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 679 | unsigned char *reader; |
| 680 | char *previous_dname, tmpname[DNS_MAX_NAME_SIZE]; |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 681 | int len, flags, offset; |
| 682 | int dns_query_record_id; |
Baptiste Assmann | 69fce67 | 2017-05-04 08:37:45 +0200 | [diff] [blame] | 683 | int nb_saved_records; |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 684 | struct dns_query_item *dns_query; |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 685 | struct dns_answer_item *dns_answer_record, *tmp_record; |
Baptiste Assmann | 729c901 | 2017-05-22 15:13:10 +0200 | [diff] [blame] | 686 | struct dns_response_packet *dns_p; |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 687 | int i, found = 0; |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 688 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 689 | reader = resp; |
| 690 | len = 0; |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 691 | previous_dname = NULL; |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 692 | dns_query = NULL; |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 693 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 694 | /* Initialization of response buffer and structure */ |
Baptiste Assmann | 729c901 | 2017-05-22 15:13:10 +0200 | [diff] [blame] | 695 | dns_p = &resolution->response; |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 696 | |
| 697 | /* query id */ |
| 698 | if (reader + 2 >= bufend) |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 699 | return DNS_RESP_INVALID; |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 700 | dns_p->header.id = reader[0] * 256 + reader[1]; |
| 701 | reader += 2; |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 702 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 703 | /* Flags and rcode are stored over 2 bytes |
Baptiste Assmann | 3440f0d | 2015-09-02 22:08:38 +0200 | [diff] [blame] | 704 | * First byte contains: |
| 705 | * - response flag (1 bit) |
| 706 | * - opcode (4 bits) |
| 707 | * - authoritative (1 bit) |
| 708 | * - truncated (1 bit) |
| 709 | * - recursion desired (1 bit) |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 710 | */ |
Baptiste Assmann | 3440f0d | 2015-09-02 22:08:38 +0200 | [diff] [blame] | 711 | if (reader + 2 >= bufend) |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 712 | return DNS_RESP_INVALID; |
| 713 | |
Baptiste Assmann | 3440f0d | 2015-09-02 22:08:38 +0200 | [diff] [blame] | 714 | flags = reader[0] * 256 + reader[1]; |
| 715 | |
Baptiste Assmann | 3440f0d | 2015-09-02 22:08:38 +0200 | [diff] [blame] | 716 | if ((flags & DNS_FLAG_REPLYCODE) != DNS_RCODE_NO_ERROR) { |
| 717 | if ((flags & DNS_FLAG_REPLYCODE) == DNS_RCODE_NX_DOMAIN) |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 718 | return DNS_RESP_NX_DOMAIN; |
Baptiste Assmann | 3440f0d | 2015-09-02 22:08:38 +0200 | [diff] [blame] | 719 | else if ((flags & DNS_FLAG_REPLYCODE) == DNS_RCODE_REFUSED) |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 720 | return DNS_RESP_REFUSED; |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 721 | return DNS_RESP_ERROR; |
| 722 | } |
| 723 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 724 | /* Move forward 2 bytes for flags */ |
Baptiste Assmann | 3440f0d | 2015-09-02 22:08:38 +0200 | [diff] [blame] | 725 | reader += 2; |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 726 | |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 727 | /* 2 bytes for question count */ |
| 728 | if (reader + 2 >= bufend) |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 729 | return DNS_RESP_INVALID; |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 730 | dns_p->header.qdcount = reader[0] * 256 + reader[1]; |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 731 | /* (for now) we send one query only, so we expect only one in the |
| 732 | * response too */ |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 733 | if (dns_p->header.qdcount != 1) |
| 734 | return DNS_RESP_QUERY_COUNT_ERROR; |
| 735 | if (dns_p->header.qdcount > DNS_MAX_QUERY_RECORDS) |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 736 | return DNS_RESP_INVALID; |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 737 | reader += 2; |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 738 | |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 739 | /* 2 bytes for answer count */ |
| 740 | if (reader + 2 >= bufend) |
| 741 | return DNS_RESP_INVALID; |
| 742 | dns_p->header.ancount = reader[0] * 256 + reader[1]; |
| 743 | if (dns_p->header.ancount == 0) |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 744 | return DNS_RESP_ANCOUNT_ZERO; |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 745 | /* Check if too many records are announced */ |
Baptiste Assmann | 9d8dbbc | 2017-08-18 23:35:08 +0200 | [diff] [blame] | 746 | if (dns_p->header.ancount > max_answer_records) |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 747 | return DNS_RESP_INVALID; |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 748 | reader += 2; |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 749 | |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 750 | /* 2 bytes authority count */ |
| 751 | if (reader + 2 >= bufend) |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 752 | return DNS_RESP_INVALID; |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 753 | dns_p->header.nscount = reader[0] * 256 + reader[1]; |
| 754 | reader += 2; |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 755 | |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 756 | /* 2 bytes additional count */ |
| 757 | if (reader + 2 >= bufend) |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 758 | return DNS_RESP_INVALID; |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 759 | dns_p->header.arcount = reader[0] * 256 + reader[1]; |
| 760 | reader += 2; |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 761 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 762 | /* Parsing dns queries */ |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 763 | LIST_INIT(&dns_p->query_list); |
| 764 | 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] | 765 | /* Use next pre-allocated dns_query_item after ensuring there is |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 766 | * still one available. |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 767 | * It's then added to our packet query list. */ |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 768 | if (dns_query_record_id > DNS_MAX_QUERY_RECORDS) |
| 769 | return DNS_RESP_INVALID; |
Baptiste Assmann | 729c901 | 2017-05-22 15:13:10 +0200 | [diff] [blame] | 770 | dns_query = &resolution->response_query_records[dns_query_record_id]; |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 771 | LIST_ADDQ(&dns_p->query_list, &dns_query->list); |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 772 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 773 | /* Name is a NULL terminated string in our case, since we have |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 774 | * one query per response and the first one can't be compressed |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 775 | * (using the 0x0c format) */ |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 776 | offset = 0; |
Remi Gacogne | 58df5ae | 2018-12-05 17:52:54 +0100 | [diff] [blame] | 777 | len = dns_read_name(resp, bufend, reader, dns_query->name, DNS_MAX_NAME_SIZE, &offset, 0); |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 778 | |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 779 | if (len == 0) |
| 780 | return DNS_RESP_INVALID; |
| 781 | |
| 782 | reader += offset; |
| 783 | previous_dname = dns_query->name; |
| 784 | |
| 785 | /* move forward 2 bytes for question type */ |
| 786 | if (reader + 2 >= bufend) |
| 787 | return DNS_RESP_INVALID; |
| 788 | dns_query->type = reader[0] * 256 + reader[1]; |
| 789 | reader += 2; |
| 790 | |
| 791 | /* move forward 2 bytes for question class */ |
| 792 | if (reader + 2 >= bufend) |
| 793 | return DNS_RESP_INVALID; |
| 794 | dns_query->class = reader[0] * 256 + reader[1]; |
| 795 | reader += 2; |
| 796 | } |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 797 | |
Baptiste Assmann | 251abb9 | 2017-08-11 09:58:27 +0200 | [diff] [blame] | 798 | /* TRUNCATED flag must be checked after we could read the query type |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 799 | * because a TRUNCATED SRV query type response can still be exploited */ |
Baptiste Assmann | 251abb9 | 2017-08-11 09:58:27 +0200 | [diff] [blame] | 800 | if (dns_query->type != DNS_RTYPE_SRV && flags & DNS_FLAG_TRUNCATED) |
| 801 | return DNS_RESP_TRUNCATED; |
| 802 | |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 803 | /* now parsing response records */ |
Baptiste Assmann | 69fce67 | 2017-05-04 08:37:45 +0200 | [diff] [blame] | 804 | nb_saved_records = 0; |
Olivier Houchard | 8da5f98 | 2017-08-04 18:35:36 +0200 | [diff] [blame] | 805 | for (i = 0; i < dns_p->header.ancount; i++) { |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 806 | if (reader >= bufend) |
| 807 | return DNS_RESP_INVALID; |
| 808 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 809 | dns_answer_record = pool_alloc(dns_answer_item_pool); |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 810 | if (dns_answer_record == NULL) |
| 811 | return (DNS_RESP_INVALID); |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 812 | |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 813 | offset = 0; |
Remi Gacogne | 58df5ae | 2018-12-05 17:52:54 +0100 | [diff] [blame] | 814 | len = dns_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset, 0); |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 815 | |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 816 | if (len == 0) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 817 | pool_free(dns_answer_item_pool, dns_answer_record); |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 818 | return DNS_RESP_INVALID; |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 819 | } |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 820 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 821 | /* Check if the current record dname is valid. previous_dname |
| 822 | * points either to queried dname or last CNAME target */ |
Baptiste Assmann | ddc8ce6 | 2017-08-11 10:31:22 +0200 | [diff] [blame] | 823 | if (dns_query->type != DNS_RTYPE_SRV && memcmp(previous_dname, tmpname, len) != 0) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 824 | pool_free(dns_answer_item_pool, dns_answer_record); |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 825 | if (i == 0) { |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 826 | /* First record, means a mismatch issue between |
| 827 | * queried dname and dname found in the first |
| 828 | * record */ |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 829 | return DNS_RESP_INVALID; |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 830 | } |
| 831 | else { |
| 832 | /* If not the first record, this means we have a |
| 833 | * CNAME resolution error */ |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 834 | return DNS_RESP_CNAME_ERROR; |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 835 | } |
| 836 | |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 837 | } |
| 838 | |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 839 | memcpy(dns_answer_record->name, tmpname, len); |
| 840 | dns_answer_record->name[len] = 0; |
Baptiste Assmann | 2359ff1 | 2015-08-07 11:24:05 +0200 | [diff] [blame] | 841 | |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 842 | reader += offset; |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 843 | if (reader >= bufend) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 844 | pool_free(dns_answer_item_pool, dns_answer_record); |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 845 | return DNS_RESP_INVALID; |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 846 | } |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 847 | |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 848 | /* 2 bytes for record type (A, AAAA, CNAME, etc...) */ |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 849 | if (reader + 2 > bufend) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 850 | pool_free(dns_answer_item_pool, dns_answer_record); |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 851 | return DNS_RESP_INVALID; |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 852 | } |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 853 | dns_answer_record->type = reader[0] * 256 + reader[1]; |
| 854 | reader += 2; |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 855 | |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 856 | /* 2 bytes for class (2) */ |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 857 | if (reader + 2 > bufend) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 858 | pool_free(dns_answer_item_pool, dns_answer_record); |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 859 | return DNS_RESP_INVALID; |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 860 | } |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 861 | dns_answer_record->class = reader[0] * 256 + reader[1]; |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 862 | reader += 2; |
| 863 | |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 864 | /* 4 bytes for ttl (4) */ |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 865 | if (reader + 4 > bufend) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 866 | pool_free(dns_answer_item_pool, dns_answer_record); |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 867 | return DNS_RESP_INVALID; |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 868 | } |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 869 | dns_answer_record->ttl = reader[0] * 16777216 + reader[1] * 65536 |
| 870 | + reader[2] * 256 + reader[3]; |
| 871 | reader += 4; |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 872 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 873 | /* Now reading data len */ |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 874 | if (reader + 2 > bufend) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 875 | pool_free(dns_answer_item_pool, dns_answer_record); |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 876 | return DNS_RESP_INVALID; |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 877 | } |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 878 | dns_answer_record->data_len = reader[0] * 256 + reader[1]; |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 879 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 880 | /* Move forward 2 bytes for data len */ |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 881 | reader += 2; |
| 882 | |
Jérôme Magnin | 8d4e7dc | 2018-12-20 16:47:31 +0100 | [diff] [blame] | 883 | if (reader + dns_answer_record->data_len > bufend) { |
Remi Gacogne | efbbdf7 | 2018-12-05 17:56:29 +0100 | [diff] [blame] | 884 | pool_free(dns_answer_item_pool, dns_answer_record); |
| 885 | return DNS_RESP_INVALID; |
| 886 | } |
| 887 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 888 | /* Analyzing record content */ |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 889 | switch (dns_answer_record->type) { |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 890 | case DNS_RTYPE_A: |
| 891 | /* ipv4 is stored on 4 bytes */ |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 892 | if (dns_answer_record->data_len != 4) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 893 | pool_free(dns_answer_item_pool, dns_answer_record); |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 894 | return DNS_RESP_INVALID; |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 895 | } |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 896 | dns_answer_record->address.sa_family = AF_INET; |
| 897 | memcpy(&(((struct sockaddr_in *)&dns_answer_record->address)->sin_addr), |
| 898 | reader, dns_answer_record->data_len); |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 899 | break; |
| 900 | |
| 901 | case DNS_RTYPE_CNAME: |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 902 | /* 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] | 903 | * no IP could be found and last record was a CNAME. Could be triggered |
| 904 | * by a wrong query type |
| 905 | * |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 906 | * + 1 because dns_answer_record_id starts at 0 |
| 907 | * while number of answers is an integer and |
| 908 | * starts at 1. |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 909 | */ |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 910 | if (i + 1 == dns_p->header.ancount) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 911 | pool_free(dns_answer_item_pool, dns_answer_record); |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 912 | return DNS_RESP_CNAME_ERROR; |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 913 | } |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 914 | |
| 915 | offset = 0; |
Remi Gacogne | 58df5ae | 2018-12-05 17:52:54 +0100 | [diff] [blame] | 916 | len = dns_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset, 0); |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 917 | if (len == 0) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 918 | pool_free(dns_answer_item_pool, dns_answer_record); |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 919 | return DNS_RESP_INVALID; |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 920 | } |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 921 | |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 922 | memcpy(dns_answer_record->target, tmpname, len); |
| 923 | dns_answer_record->target[len] = 0; |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 924 | previous_dname = dns_answer_record->target; |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 925 | break; |
| 926 | |
Olivier Houchard | 8da5f98 | 2017-08-04 18:35:36 +0200 | [diff] [blame] | 927 | |
| 928 | case DNS_RTYPE_SRV: |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 929 | /* Answer must contain : |
Olivier Houchard | 8da5f98 | 2017-08-04 18:35:36 +0200 | [diff] [blame] | 930 | * - 2 bytes for the priority |
| 931 | * - 2 bytes for the weight |
| 932 | * - 2 bytes for the port |
| 933 | * - the target hostname |
| 934 | */ |
| 935 | if (dns_answer_record->data_len <= 6) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 936 | pool_free(dns_answer_item_pool, dns_answer_record); |
Olivier Houchard | 8da5f98 | 2017-08-04 18:35:36 +0200 | [diff] [blame] | 937 | return DNS_RESP_INVALID; |
| 938 | } |
Willy Tarreau | d5370e1 | 2017-09-19 14:59:52 +0200 | [diff] [blame] | 939 | dns_answer_record->priority = read_n16(reader); |
Olivier Houchard | 8da5f98 | 2017-08-04 18:35:36 +0200 | [diff] [blame] | 940 | reader += sizeof(uint16_t); |
Willy Tarreau | d5370e1 | 2017-09-19 14:59:52 +0200 | [diff] [blame] | 941 | dns_answer_record->weight = read_n16(reader); |
Olivier Houchard | 8da5f98 | 2017-08-04 18:35:36 +0200 | [diff] [blame] | 942 | reader += sizeof(uint16_t); |
Willy Tarreau | d5370e1 | 2017-09-19 14:59:52 +0200 | [diff] [blame] | 943 | dns_answer_record->port = read_n16(reader); |
Olivier Houchard | 8da5f98 | 2017-08-04 18:35:36 +0200 | [diff] [blame] | 944 | reader += sizeof(uint16_t); |
| 945 | offset = 0; |
Remi Gacogne | 58df5ae | 2018-12-05 17:52:54 +0100 | [diff] [blame] | 946 | len = dns_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset, 0); |
Olivier Houchard | 8da5f98 | 2017-08-04 18:35:36 +0200 | [diff] [blame] | 947 | if (len == 0) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 948 | pool_free(dns_answer_item_pool, dns_answer_record); |
Olivier Houchard | 8da5f98 | 2017-08-04 18:35:36 +0200 | [diff] [blame] | 949 | return DNS_RESP_INVALID; |
| 950 | } |
Olivier Houchard | 8da5f98 | 2017-08-04 18:35:36 +0200 | [diff] [blame] | 951 | dns_answer_record->data_len = len; |
| 952 | memcpy(dns_answer_record->target, tmpname, len); |
| 953 | dns_answer_record->target[len] = 0; |
| 954 | break; |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 955 | |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 956 | case DNS_RTYPE_AAAA: |
| 957 | /* ipv6 is stored on 16 bytes */ |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 958 | if (dns_answer_record->data_len != 16) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 959 | pool_free(dns_answer_item_pool, dns_answer_record); |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 960 | return DNS_RESP_INVALID; |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 961 | } |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 962 | dns_answer_record->address.sa_family = AF_INET6; |
| 963 | memcpy(&(((struct sockaddr_in6 *)&dns_answer_record->address)->sin6_addr), |
| 964 | reader, dns_answer_record->data_len); |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 965 | break; |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 966 | |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 967 | } /* switch (record type) */ |
| 968 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 969 | /* Increment the counter for number of records saved into our |
| 970 | * local response */ |
| 971 | nb_saved_records++; |
Baptiste Assmann | 69fce67 | 2017-05-04 08:37:45 +0200 | [diff] [blame] | 972 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 973 | /* Move forward dns_answer_record->data_len for analyzing next |
| 974 | * record in the response */ |
| 975 | reader += ((dns_answer_record->type == DNS_RTYPE_SRV) |
| 976 | ? offset |
| 977 | : dns_answer_record->data_len); |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 978 | |
| 979 | /* Lookup to see if we already had this entry */ |
Olivier Houchard | 8da5f98 | 2017-08-04 18:35:36 +0200 | [diff] [blame] | 980 | found = 0; |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 981 | list_for_each_entry(tmp_record, &dns_p->answer_list, list) { |
| 982 | if (tmp_record->type != dns_answer_record->type) |
| 983 | continue; |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 984 | |
| 985 | switch(tmp_record->type) { |
| 986 | case DNS_RTYPE_A: |
| 987 | if (!memcmp(&((struct sockaddr_in *)&dns_answer_record->address)->sin_addr, |
| 988 | &((struct sockaddr_in *)&tmp_record->address)->sin_addr, |
| 989 | sizeof(in_addr_t))) |
| 990 | found = 1; |
| 991 | break; |
| 992 | |
| 993 | case DNS_RTYPE_AAAA: |
| 994 | if (!memcmp(&((struct sockaddr_in6 *)&dns_answer_record->address)->sin6_addr, |
| 995 | &((struct sockaddr_in6 *)&tmp_record->address)->sin6_addr, |
| 996 | sizeof(struct in6_addr))) |
| 997 | found = 1; |
| 998 | break; |
| 999 | |
Olivier Houchard | 8da5f98 | 2017-08-04 18:35:36 +0200 | [diff] [blame] | 1000 | case DNS_RTYPE_SRV: |
| 1001 | if (dns_answer_record->data_len == tmp_record->data_len && |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1002 | !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] | 1003 | dns_answer_record->port == tmp_record->port) { |
| 1004 | tmp_record->weight = dns_answer_record->weight; |
| 1005 | found = 1; |
| 1006 | } |
| 1007 | break; |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1008 | |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 1009 | default: |
| 1010 | break; |
| 1011 | } |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1012 | |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 1013 | if (found == 1) |
| 1014 | break; |
| 1015 | } |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1016 | |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 1017 | if (found == 1) { |
| 1018 | tmp_record->last_seen = now.tv_sec; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 1019 | pool_free(dns_answer_item_pool, dns_answer_record); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1020 | } |
| 1021 | else { |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 1022 | dns_answer_record->last_seen = now.tv_sec; |
Baptiste Assmann | 13a9232 | 2019-06-07 09:40:55 +0200 | [diff] [blame] | 1023 | dns_answer_record->ar_item = NULL; |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 1024 | LIST_ADDQ(&dns_p->answer_list, &dns_answer_record->list); |
| 1025 | } |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 1026 | } /* for i 0 to ancount */ |
| 1027 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1028 | /* Save the number of records we really own */ |
Baptiste Assmann | 69fce67 | 2017-05-04 08:37:45 +0200 | [diff] [blame] | 1029 | dns_p->header.ancount = nb_saved_records; |
Baptiste Assmann | 13a9232 | 2019-06-07 09:40:55 +0200 | [diff] [blame] | 1030 | |
| 1031 | /* now parsing additional records */ |
| 1032 | nb_saved_records = 0; |
Baptiste Assmann | 13a9232 | 2019-06-07 09:40:55 +0200 | [diff] [blame] | 1033 | for (i = 0; i < dns_p->header.arcount; i++) { |
| 1034 | if (reader >= bufend) |
| 1035 | return DNS_RESP_INVALID; |
| 1036 | |
| 1037 | dns_answer_record = pool_alloc(dns_answer_item_pool); |
| 1038 | if (dns_answer_record == NULL) |
| 1039 | return (DNS_RESP_INVALID); |
| 1040 | |
| 1041 | offset = 0; |
| 1042 | len = dns_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset, 0); |
| 1043 | |
| 1044 | if (len == 0) { |
| 1045 | pool_free(dns_answer_item_pool, dns_answer_record); |
| 1046 | return DNS_RESP_INVALID; |
| 1047 | } |
| 1048 | |
| 1049 | /* Check if the current record dname is valid. previous_dname |
| 1050 | * points either to queried dname or last CNAME target */ |
| 1051 | if (dns_query->type != DNS_RTYPE_SRV && memcmp(previous_dname, tmpname, len) != 0) { |
| 1052 | pool_free(dns_answer_item_pool, dns_answer_record); |
| 1053 | if (i == 0) { |
| 1054 | /* First record, means a mismatch issue between |
| 1055 | * queried dname and dname found in the first |
| 1056 | * record */ |
| 1057 | return DNS_RESP_INVALID; |
| 1058 | } |
| 1059 | else { |
| 1060 | /* If not the first record, this means we have a |
| 1061 | * CNAME resolution error */ |
| 1062 | return DNS_RESP_CNAME_ERROR; |
| 1063 | } |
| 1064 | |
| 1065 | } |
| 1066 | |
| 1067 | memcpy(dns_answer_record->name, tmpname, len); |
| 1068 | dns_answer_record->name[len] = 0; |
| 1069 | |
| 1070 | reader += offset; |
| 1071 | if (reader >= bufend) { |
| 1072 | pool_free(dns_answer_item_pool, dns_answer_record); |
| 1073 | return DNS_RESP_INVALID; |
| 1074 | } |
| 1075 | |
| 1076 | /* 2 bytes for record type (A, AAAA, CNAME, etc...) */ |
| 1077 | if (reader + 2 > bufend) { |
| 1078 | pool_free(dns_answer_item_pool, dns_answer_record); |
| 1079 | return DNS_RESP_INVALID; |
| 1080 | } |
| 1081 | dns_answer_record->type = reader[0] * 256 + reader[1]; |
| 1082 | reader += 2; |
| 1083 | |
| 1084 | /* 2 bytes for class (2) */ |
| 1085 | if (reader + 2 > bufend) { |
| 1086 | pool_free(dns_answer_item_pool, dns_answer_record); |
| 1087 | return DNS_RESP_INVALID; |
| 1088 | } |
| 1089 | dns_answer_record->class = reader[0] * 256 + reader[1]; |
| 1090 | reader += 2; |
| 1091 | |
| 1092 | /* 4 bytes for ttl (4) */ |
| 1093 | if (reader + 4 > bufend) { |
| 1094 | pool_free(dns_answer_item_pool, dns_answer_record); |
| 1095 | return DNS_RESP_INVALID; |
| 1096 | } |
| 1097 | dns_answer_record->ttl = reader[0] * 16777216 + reader[1] * 65536 |
| 1098 | + reader[2] * 256 + reader[3]; |
| 1099 | reader += 4; |
| 1100 | |
| 1101 | /* Now reading data len */ |
| 1102 | if (reader + 2 > bufend) { |
| 1103 | pool_free(dns_answer_item_pool, dns_answer_record); |
| 1104 | return DNS_RESP_INVALID; |
| 1105 | } |
| 1106 | dns_answer_record->data_len = reader[0] * 256 + reader[1]; |
| 1107 | |
| 1108 | /* Move forward 2 bytes for data len */ |
| 1109 | reader += 2; |
| 1110 | |
| 1111 | if (reader + dns_answer_record->data_len > bufend) { |
| 1112 | pool_free(dns_answer_item_pool, dns_answer_record); |
| 1113 | return DNS_RESP_INVALID; |
| 1114 | } |
| 1115 | |
| 1116 | /* Analyzing record content */ |
| 1117 | switch (dns_answer_record->type) { |
| 1118 | case DNS_RTYPE_A: |
| 1119 | /* ipv4 is stored on 4 bytes */ |
| 1120 | if (dns_answer_record->data_len != 4) { |
| 1121 | pool_free(dns_answer_item_pool, dns_answer_record); |
| 1122 | return DNS_RESP_INVALID; |
| 1123 | } |
| 1124 | dns_answer_record->address.sa_family = AF_INET; |
| 1125 | memcpy(&(((struct sockaddr_in *)&dns_answer_record->address)->sin_addr), |
| 1126 | reader, dns_answer_record->data_len); |
| 1127 | break; |
| 1128 | |
| 1129 | case DNS_RTYPE_AAAA: |
| 1130 | /* ipv6 is stored on 16 bytes */ |
| 1131 | if (dns_answer_record->data_len != 16) { |
| 1132 | pool_free(dns_answer_item_pool, dns_answer_record); |
| 1133 | return DNS_RESP_INVALID; |
| 1134 | } |
| 1135 | dns_answer_record->address.sa_family = AF_INET6; |
| 1136 | memcpy(&(((struct sockaddr_in6 *)&dns_answer_record->address)->sin6_addr), |
| 1137 | reader, dns_answer_record->data_len); |
| 1138 | break; |
| 1139 | |
| 1140 | default: |
| 1141 | pool_free(dns_answer_item_pool, dns_answer_record); |
| 1142 | continue; |
| 1143 | |
| 1144 | } /* switch (record type) */ |
| 1145 | |
| 1146 | /* Increment the counter for number of records saved into our |
| 1147 | * local response */ |
| 1148 | nb_saved_records++; |
| 1149 | |
| 1150 | /* Move forward dns_answer_record->data_len for analyzing next |
| 1151 | * record in the response */ |
| 1152 | reader += ((dns_answer_record->type == DNS_RTYPE_SRV) |
| 1153 | ? offset |
| 1154 | : dns_answer_record->data_len); |
| 1155 | |
| 1156 | /* Lookup to see if we already had this entry */ |
| 1157 | found = 0; |
| 1158 | list_for_each_entry(tmp_record, &dns_p->answer_list, list) { |
| 1159 | if (tmp_record->type != dns_answer_record->type) |
| 1160 | continue; |
| 1161 | |
| 1162 | switch(tmp_record->type) { |
| 1163 | case DNS_RTYPE_A: |
| 1164 | if (!memcmp(&((struct sockaddr_in *)&dns_answer_record->address)->sin_addr, |
| 1165 | &((struct sockaddr_in *)&tmp_record->address)->sin_addr, |
| 1166 | sizeof(in_addr_t))) |
| 1167 | found = 1; |
| 1168 | break; |
| 1169 | |
| 1170 | case DNS_RTYPE_AAAA: |
| 1171 | if (!memcmp(&((struct sockaddr_in6 *)&dns_answer_record->address)->sin6_addr, |
| 1172 | &((struct sockaddr_in6 *)&tmp_record->address)->sin6_addr, |
| 1173 | sizeof(struct in6_addr))) |
| 1174 | found = 1; |
| 1175 | break; |
| 1176 | |
| 1177 | default: |
| 1178 | break; |
| 1179 | } |
| 1180 | |
| 1181 | if (found == 1) |
| 1182 | break; |
| 1183 | } |
| 1184 | |
| 1185 | if (found == 1) { |
| 1186 | tmp_record->last_seen = now.tv_sec; |
| 1187 | pool_free(dns_answer_item_pool, dns_answer_record); |
| 1188 | } |
| 1189 | else { |
| 1190 | dns_answer_record->last_seen = now.tv_sec; |
| 1191 | dns_answer_record->ar_item = NULL; |
| 1192 | |
| 1193 | // looking for the SRV record in the response list linked to this additional record |
| 1194 | list_for_each_entry(tmp_record, &dns_p->answer_list, list) { |
| 1195 | if ( !( |
| 1196 | (tmp_record->type == DNS_RTYPE_SRV) && |
| 1197 | (tmp_record->ar_item == NULL) && |
| 1198 | (memcmp(tmp_record->target, dns_answer_record->name, tmp_record->data_len) == 0) |
| 1199 | ) |
| 1200 | ) |
| 1201 | continue; |
| 1202 | tmp_record->ar_item = dns_answer_record; |
| 1203 | } |
Baptiste Assmann | 13a9232 | 2019-06-07 09:40:55 +0200 | [diff] [blame] | 1204 | |
| 1205 | LIST_ADDQ(&dns_p->ar_list, &dns_answer_record->list); |
| 1206 | } |
| 1207 | } /* for i 0 to arcount */ |
| 1208 | |
| 1209 | /* Save the number of records we really own */ |
| 1210 | dns_p->header.arcount = nb_saved_records; |
| 1211 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1212 | dns_check_dns_response(resolution); |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 1213 | return DNS_RESP_VALID; |
| 1214 | } |
| 1215 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1216 | /* Searches dn_name resolution in resp. |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 1217 | * If existing IP not found, return the first IP matching family_priority, |
| 1218 | * otherwise, first ip found |
| 1219 | * The following tasks are the responsibility of the caller: |
Baptiste Assmann | 3cf7f98 | 2016-04-17 22:43:26 +0200 | [diff] [blame] | 1220 | * - <dns_p> contains an error free DNS response |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 1221 | * For both cases above, dns_validate_dns_response is required |
| 1222 | * returns one of the DNS_UPD_* code |
| 1223 | */ |
Baptiste Assmann | fb7091e | 2017-05-03 15:43:12 +0200 | [diff] [blame] | 1224 | int dns_get_ip_from_response(struct dns_response_packet *dns_p, |
Baptiste Assmann | 4274637 | 2017-05-03 12:12:02 +0200 | [diff] [blame] | 1225 | struct dns_options *dns_opts, void *currentip, |
Thierry Fournier | ada3484 | 2016-02-17 21:25:09 +0100 | [diff] [blame] | 1226 | short currentip_sin_family, |
Baptiste Assmann | fb7091e | 2017-05-03 15:43:12 +0200 | [diff] [blame] | 1227 | void **newip, short *newip_sin_family, |
| 1228 | void *owner) |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 1229 | { |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 1230 | struct dns_answer_item *record; |
Thierry Fournier | ada3484 | 2016-02-17 21:25:09 +0100 | [diff] [blame] | 1231 | int family_priority; |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 1232 | int currentip_found; |
Baptiste Assmann | 3cf7f98 | 2016-04-17 22:43:26 +0200 | [diff] [blame] | 1233 | unsigned char *newip4, *newip6; |
Thierry Fournier | ac88cfe | 2016-02-17 22:05:30 +0100 | [diff] [blame] | 1234 | int currentip_sel; |
| 1235 | int j; |
Thierry Fournier | ac88cfe | 2016-02-17 22:05:30 +0100 | [diff] [blame] | 1236 | int score, max_score; |
Baptiste Assmann | 8e2d943 | 2018-06-22 15:04:43 +0200 | [diff] [blame] | 1237 | int allowed_duplicated_ip; |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 1238 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1239 | family_priority = dns_opts->family_prio; |
Baptiste Assmann | 8e2d943 | 2018-06-22 15:04:43 +0200 | [diff] [blame] | 1240 | allowed_duplicated_ip = dns_opts->accept_duplicate_ip; |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1241 | *newip = newip4 = newip6 = NULL; |
| 1242 | currentip_found = 0; |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 1243 | *newip_sin_family = AF_UNSPEC; |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1244 | max_score = -1; |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 1245 | |
Thierry Fournier | ac88cfe | 2016-02-17 22:05:30 +0100 | [diff] [blame] | 1246 | /* Select an IP regarding configuration preference. |
Joseph Herlant | 42cf639 | 2018-11-15 10:33:28 -0800 | [diff] [blame] | 1247 | * Top priority is the preferred network ip version, |
| 1248 | * second priority is the preferred network. |
Thierry Fournier | ac88cfe | 2016-02-17 22:05:30 +0100 | [diff] [blame] | 1249 | * the last priority is the currently used IP, |
| 1250 | * |
| 1251 | * For these three priorities, a score is calculated. The |
| 1252 | * weight are: |
Joseph Herlant | 42cf639 | 2018-11-15 10:33:28 -0800 | [diff] [blame] | 1253 | * 8 - preferred ip version. |
| 1254 | * 4 - preferred network. |
Baptiste | fc72590 | 2016-12-26 23:21:08 +0100 | [diff] [blame] | 1255 | * 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] | 1256 | * 1 - current ip. |
| 1257 | * The result with the biggest score is returned. |
| 1258 | */ |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 1259 | |
| 1260 | list_for_each_entry(record, &dns_p->answer_list, list) { |
| 1261 | void *ip; |
| 1262 | unsigned char ip_type; |
Thierry Fournier | ac88cfe | 2016-02-17 22:05:30 +0100 | [diff] [blame] | 1263 | |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 1264 | if (record->type == DNS_RTYPE_A) { |
| 1265 | ip = &(((struct sockaddr_in *)&record->address)->sin_addr); |
| 1266 | ip_type = AF_INET; |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1267 | } |
| 1268 | else if (record->type == DNS_RTYPE_AAAA) { |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 1269 | ip_type = AF_INET6; |
| 1270 | ip = &(((struct sockaddr_in6 *)&record->address)->sin6_addr); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1271 | } |
| 1272 | else |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 1273 | continue; |
Thierry Fournier | ac88cfe | 2016-02-17 22:05:30 +0100 | [diff] [blame] | 1274 | score = 0; |
| 1275 | |
Joseph Herlant | 42cf639 | 2018-11-15 10:33:28 -0800 | [diff] [blame] | 1276 | /* Check for preferred ip protocol. */ |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 1277 | if (ip_type == family_priority) |
Baptiste | fc72590 | 2016-12-26 23:21:08 +0100 | [diff] [blame] | 1278 | score += 8; |
Thierry Fournier | ac88cfe | 2016-02-17 22:05:30 +0100 | [diff] [blame] | 1279 | |
Joseph Herlant | 42cf639 | 2018-11-15 10:33:28 -0800 | [diff] [blame] | 1280 | /* Check for preferred network. */ |
Baptiste Assmann | 4274637 | 2017-05-03 12:12:02 +0200 | [diff] [blame] | 1281 | for (j = 0; j < dns_opts->pref_net_nb; j++) { |
Thierry Fournier | ac88cfe | 2016-02-17 22:05:30 +0100 | [diff] [blame] | 1282 | |
| 1283 | /* Compare only the same adresses class. */ |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 1284 | if (dns_opts->pref_net[j].family != ip_type) |
Thierry Fournier | ac88cfe | 2016-02-17 22:05:30 +0100 | [diff] [blame] | 1285 | continue; |
| 1286 | |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 1287 | if ((ip_type == AF_INET && |
| 1288 | in_net_ipv4(ip, |
Baptiste Assmann | 4274637 | 2017-05-03 12:12:02 +0200 | [diff] [blame] | 1289 | &dns_opts->pref_net[j].mask.in4, |
| 1290 | &dns_opts->pref_net[j].addr.in4)) || |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 1291 | (ip_type == AF_INET6 && |
| 1292 | in_net_ipv6(ip, |
Baptiste Assmann | 4274637 | 2017-05-03 12:12:02 +0200 | [diff] [blame] | 1293 | &dns_opts->pref_net[j].mask.in6, |
| 1294 | &dns_opts->pref_net[j].addr.in6))) { |
Baptiste | fc72590 | 2016-12-26 23:21:08 +0100 | [diff] [blame] | 1295 | score += 4; |
Thierry Fournier | ac88cfe | 2016-02-17 22:05:30 +0100 | [diff] [blame] | 1296 | break; |
| 1297 | } |
| 1298 | } |
| 1299 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1300 | /* Check if the IP found in the record is already affected to a |
Baptiste Assmann | 84221b4 | 2018-06-22 13:03:50 +0200 | [diff] [blame] | 1301 | * member of a group. If not, the score should be incremented |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1302 | * by 2. */ |
Baptiste Assmann | 84221b4 | 2018-06-22 13:03:50 +0200 | [diff] [blame] | 1303 | if (owner && snr_check_ip_callback(owner, ip, &ip_type)) { |
Baptiste Assmann | 8e2d943 | 2018-06-22 15:04:43 +0200 | [diff] [blame] | 1304 | if (!allowed_duplicated_ip) { |
| 1305 | continue; |
| 1306 | } |
Baptiste Assmann | 84221b4 | 2018-06-22 13:03:50 +0200 | [diff] [blame] | 1307 | } else { |
| 1308 | score += 2; |
| 1309 | } |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1310 | |
Thierry Fournier | ac88cfe | 2016-02-17 22:05:30 +0100 | [diff] [blame] | 1311 | /* Check for current ip matching. */ |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 1312 | if (ip_type == currentip_sin_family && |
Thierry Fournier | ac88cfe | 2016-02-17 22:05:30 +0100 | [diff] [blame] | 1313 | ((currentip_sin_family == AF_INET && |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1314 | !memcmp(ip, currentip, 4)) || |
Thierry Fournier | ac88cfe | 2016-02-17 22:05:30 +0100 | [diff] [blame] | 1315 | (currentip_sin_family == AF_INET6 && |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1316 | !memcmp(ip, currentip, 16)))) { |
| 1317 | score++; |
Thierry Fournier | ac88cfe | 2016-02-17 22:05:30 +0100 | [diff] [blame] | 1318 | currentip_sel = 1; |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1319 | } |
| 1320 | else |
Thierry Fournier | ac88cfe | 2016-02-17 22:05:30 +0100 | [diff] [blame] | 1321 | currentip_sel = 0; |
| 1322 | |
| 1323 | /* Keep the address if the score is better than the previous |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1324 | * score. The maximum score is 15, if this value is reached, we |
| 1325 | * break the parsing. Implicitly, this score is reached the ip |
| 1326 | * selected is the current ip. */ |
Thierry Fournier | ac88cfe | 2016-02-17 22:05:30 +0100 | [diff] [blame] | 1327 | if (score > max_score) { |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 1328 | if (ip_type == AF_INET) |
| 1329 | newip4 = ip; |
Thierry Fournier | ac88cfe | 2016-02-17 22:05:30 +0100 | [diff] [blame] | 1330 | else |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 1331 | newip6 = ip; |
Thierry Fournier | ac88cfe | 2016-02-17 22:05:30 +0100 | [diff] [blame] | 1332 | currentip_found = currentip_sel; |
Baptiste | fc72590 | 2016-12-26 23:21:08 +0100 | [diff] [blame] | 1333 | if (score == 15) |
Thierry Fournier | ac88cfe | 2016-02-17 22:05:30 +0100 | [diff] [blame] | 1334 | return DNS_UPD_NO; |
| 1335 | max_score = score; |
| 1336 | } |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 1337 | } /* list for each record entries */ |
Thierry Fournier | ac88cfe | 2016-02-17 22:05:30 +0100 | [diff] [blame] | 1338 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1339 | /* No IP found in the response */ |
Olivier Houchard | a8c6db8 | 2017-07-06 18:46:47 +0200 | [diff] [blame] | 1340 | if (!newip4 && !newip6) |
Baptiste Assmann | 0453a1d | 2015-09-09 00:51:08 +0200 | [diff] [blame] | 1341 | return DNS_UPD_NO_IP_FOUND; |
Baptiste Assmann | 0453a1d | 2015-09-09 00:51:08 +0200 | [diff] [blame] | 1342 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1343 | /* Case when the caller looks first for an IPv4 address */ |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 1344 | if (family_priority == AF_INET) { |
| 1345 | if (newip4) { |
| 1346 | *newip = newip4; |
| 1347 | *newip_sin_family = AF_INET; |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 1348 | } |
| 1349 | else if (newip6) { |
| 1350 | *newip = newip6; |
| 1351 | *newip_sin_family = AF_INET6; |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 1352 | } |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1353 | if (!currentip_found) |
| 1354 | goto not_found; |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 1355 | } |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1356 | /* Case when the caller looks first for an IPv6 address */ |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 1357 | else if (family_priority == AF_INET6) { |
| 1358 | if (newip6) { |
| 1359 | *newip = newip6; |
| 1360 | *newip_sin_family = AF_INET6; |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 1361 | } |
| 1362 | else if (newip4) { |
| 1363 | *newip = newip4; |
| 1364 | *newip_sin_family = AF_INET; |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 1365 | } |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1366 | if (!currentip_found) |
| 1367 | goto not_found; |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 1368 | } |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1369 | /* Case when the caller have no preference (we prefer IPv6) */ |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 1370 | else if (family_priority == AF_UNSPEC) { |
| 1371 | if (newip6) { |
| 1372 | *newip = newip6; |
| 1373 | *newip_sin_family = AF_INET6; |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 1374 | } |
| 1375 | else if (newip4) { |
| 1376 | *newip = newip4; |
| 1377 | *newip_sin_family = AF_INET; |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 1378 | } |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1379 | if (!currentip_found) |
| 1380 | goto not_found; |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 1381 | } |
| 1382 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1383 | /* No reason why we should change the server's IP address */ |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 1384 | return DNS_UPD_NO; |
Baptiste Assmann | 8ea0bcc | 2017-05-04 08:24:11 +0200 | [diff] [blame] | 1385 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1386 | not_found: |
Baptiste Assmann | 8ea0bcc | 2017-05-04 08:24:11 +0200 | [diff] [blame] | 1387 | list_for_each_entry(record, &dns_p->answer_list, list) { |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1388 | /* Move the first record to the end of the list, for internal |
| 1389 | * round robin */ |
| 1390 | LIST_DEL(&record->list); |
| 1391 | LIST_ADDQ(&dns_p->answer_list, &record->list); |
| 1392 | break; |
Baptiste Assmann | 8ea0bcc | 2017-05-04 08:24:11 +0200 | [diff] [blame] | 1393 | } |
| 1394 | return DNS_UPD_SRVIP_NOT_FOUND; |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 1395 | } |
| 1396 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1397 | /* Turns a domain name label into a string. |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 1398 | * |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1399 | * <dn> must be a null-terminated string. <dn_len> must include the terminating |
| 1400 | * 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] | 1401 | * |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1402 | * In case of error, -1 is returned, otherwise, the number of bytes copied in |
| 1403 | * <str> (including the terminating null byte). |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 1404 | */ |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1405 | 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] | 1406 | { |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1407 | char *ptr; |
| 1408 | int i, sz; |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 1409 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1410 | if (str_len < dn_len - 1) |
Baptiste Assmann | 2af08fe | 2017-08-14 00:13:01 +0200 | [diff] [blame] | 1411 | return -1; |
| 1412 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1413 | ptr = str; |
| 1414 | for (i = 0; i < dn_len-1; ++i) { |
| 1415 | sz = dn[i]; |
| 1416 | if (i) |
| 1417 | *ptr++ = '.'; |
| 1418 | memcpy(ptr, dn+i+1, sz); |
| 1419 | ptr += sz; |
| 1420 | i += sz; |
Olivier Houchard | 8da5f98 | 2017-08-04 18:35:36 +0200 | [diff] [blame] | 1421 | } |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1422 | *ptr++ = '\0'; |
| 1423 | return (ptr - str); |
Olivier Houchard | 8da5f98 | 2017-08-04 18:35:36 +0200 | [diff] [blame] | 1424 | } |
| 1425 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1426 | /* Turns a string into domain name label: www.haproxy.org into 3www7haproxy3org |
| 1427 | * |
| 1428 | * <str> must be a null-terminated string. <str_len> must include the |
| 1429 | * terminating null byte. <dn> buffer must be allocated and its size must be |
| 1430 | * passed in <dn_len>. |
| 1431 | * |
| 1432 | * In case of error, -1 is returned, otherwise, the number of bytes copied in |
| 1433 | * <dn> (excluding the terminating null byte). |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 1434 | */ |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1435 | 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] | 1436 | { |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 1437 | int i, offset; |
| 1438 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1439 | if (dn_len < str_len + 1) |
| 1440 | return -1; |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 1441 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1442 | /* First byte of dn will be used to store the length of the first |
| 1443 | * label */ |
| 1444 | offset = 0; |
| 1445 | for (i = 0; i < str_len; ++i) { |
| 1446 | if (str[i] == '.') { |
| 1447 | /* 2 or more consecutive dots is invalid */ |
| 1448 | if (i == offset) |
| 1449 | return -1; |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 1450 | |
Lukas Tribus | 81725b8 | 2020-02-27 15:47:24 +0100 | [diff] [blame] | 1451 | /* ignore trailing dot */ |
| 1452 | if (i + 2 == str_len) { |
| 1453 | i++; |
| 1454 | break; |
| 1455 | } |
| 1456 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1457 | dn[offset] = (i - offset); |
| 1458 | offset = i+1; |
| 1459 | continue; |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 1460 | } |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1461 | dn[i+1] = str[i]; |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 1462 | } |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1463 | dn[offset] = (i - offset - 1); |
| 1464 | dn[i] = '\0'; |
| 1465 | return i; |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 1466 | } |
| 1467 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1468 | /* Validates host name: |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 1469 | * - total size |
| 1470 | * - each label size individually |
| 1471 | * returns: |
| 1472 | * 0 in case of error. If <err> is not NULL, an error message is stored there. |
| 1473 | * 1 when no error. <err> is left unaffected. |
| 1474 | */ |
| 1475 | int dns_hostname_validation(const char *string, char **err) |
| 1476 | { |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 1477 | int i; |
| 1478 | |
| 1479 | if (strlen(string) > DNS_MAX_NAME_SIZE) { |
| 1480 | if (err) |
| 1481 | *err = DNS_TOO_LONG_FQDN; |
| 1482 | return 0; |
| 1483 | } |
| 1484 | |
William Dauchy | aecd5dc | 2020-01-26 19:52:34 +0100 | [diff] [blame] | 1485 | while (*string) { |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 1486 | i = 0; |
William Dauchy | aecd5dc | 2020-01-26 19:52:34 +0100 | [diff] [blame] | 1487 | while (*string && *string != '.' && i < DNS_MAX_LABEL_SIZE) { |
| 1488 | if (!(*string == '-' || *string == '_' || |
| 1489 | (*string >= 'a' && *string <= 'z') || |
| 1490 | (*string >= 'A' && *string <= 'Z') || |
| 1491 | (*string >= '0' && *string <= '9'))) { |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 1492 | if (err) |
| 1493 | *err = DNS_INVALID_CHARACTER; |
| 1494 | return 0; |
| 1495 | } |
William Dauchy | aecd5dc | 2020-01-26 19:52:34 +0100 | [diff] [blame] | 1496 | i++; |
| 1497 | string++; |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 1498 | } |
| 1499 | |
William Dauchy | aecd5dc | 2020-01-26 19:52:34 +0100 | [diff] [blame] | 1500 | if (!(*string)) |
| 1501 | break; |
| 1502 | |
| 1503 | if (*string != '.' && i >= DNS_MAX_LABEL_SIZE) { |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 1504 | if (err) |
| 1505 | *err = DNS_LABEL_TOO_LONG; |
| 1506 | return 0; |
| 1507 | } |
| 1508 | |
William Dauchy | aecd5dc | 2020-01-26 19:52:34 +0100 | [diff] [blame] | 1509 | string++; |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 1510 | } |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 1511 | return 1; |
| 1512 | } |
| 1513 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1514 | /* Picks up an available resolution from the different resolution list |
| 1515 | * associated to a resolvers section, in this order: |
| 1516 | * 1. check in resolutions.curr for the same hostname and query_type |
| 1517 | * 2. check in resolutions.wait for the same hostname and query_type |
| 1518 | * 3. Get a new resolution from resolution pool |
| 1519 | * |
| 1520 | * Returns an available resolution, NULL if none found. |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 1521 | */ |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1522 | static struct dns_resolution *dns_pick_resolution(struct dns_resolvers *resolvers, |
| 1523 | char **hostname_dn, int hostname_dn_len, |
| 1524 | int query_type) |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 1525 | { |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1526 | struct dns_resolution *res; |
| 1527 | |
| 1528 | if (!*hostname_dn) |
| 1529 | goto from_pool; |
| 1530 | |
| 1531 | /* Search for same hostname and query type in resolutions.curr */ |
| 1532 | list_for_each_entry(res, &resolvers->resolutions.curr, list) { |
| 1533 | if (!res->hostname_dn) |
| 1534 | continue; |
| 1535 | if ((query_type == res->prefered_query_type) && |
| 1536 | hostname_dn_len == res->hostname_dn_len && |
| 1537 | !memcmp(*hostname_dn, res->hostname_dn, hostname_dn_len)) |
| 1538 | return res; |
| 1539 | } |
| 1540 | |
| 1541 | /* Search for same hostname and query type in resolutions.wait */ |
| 1542 | list_for_each_entry(res, &resolvers->resolutions.wait, list) { |
| 1543 | if (!res->hostname_dn) |
| 1544 | continue; |
| 1545 | if ((query_type == res->prefered_query_type) && |
| 1546 | hostname_dn_len == res->hostname_dn_len && |
| 1547 | !memcmp(*hostname_dn, res->hostname_dn, hostname_dn_len)) |
| 1548 | return res; |
| 1549 | } |
| 1550 | |
| 1551 | from_pool: |
| 1552 | /* No resolution could be found, so let's allocate a new one */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 1553 | res = pool_alloc(dns_resolution_pool); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1554 | if (res) { |
| 1555 | memset(res, 0, sizeof(*res)); |
| 1556 | res->resolvers = resolvers; |
| 1557 | res->uuid = resolution_uuid; |
| 1558 | res->status = RSLV_STATUS_NONE; |
| 1559 | res->step = RSLV_STEP_NONE; |
| 1560 | res->last_valid = now_ms; |
| 1561 | |
| 1562 | LIST_INIT(&res->requesters); |
| 1563 | LIST_INIT(&res->response.answer_list); |
Baptiste Assmann | 13a9232 | 2019-06-07 09:40:55 +0200 | [diff] [blame] | 1564 | LIST_INIT(&res->response.ar_list); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1565 | |
| 1566 | res->prefered_query_type = query_type; |
| 1567 | res->query_type = query_type; |
| 1568 | res->hostname_dn = *hostname_dn; |
| 1569 | res->hostname_dn_len = hostname_dn_len; |
| 1570 | |
| 1571 | ++resolution_uuid; |
| 1572 | |
| 1573 | /* Move the resolution to the resolvers wait queue */ |
| 1574 | LIST_ADDQ(&resolvers->resolutions.wait, &res->list); |
| 1575 | } |
| 1576 | return res; |
| 1577 | } |
| 1578 | |
| 1579 | /* Releases a resolution from its requester(s) and move it back to the pool */ |
| 1580 | static void dns_free_resolution(struct dns_resolution *resolution) |
| 1581 | { |
| 1582 | struct dns_requester *req, *reqback; |
| 1583 | |
| 1584 | /* clean up configuration */ |
| 1585 | dns_reset_resolution(resolution); |
| 1586 | resolution->hostname_dn = NULL; |
| 1587 | resolution->hostname_dn_len = 0; |
| 1588 | |
| 1589 | list_for_each_entry_safe(req, reqback, &resolution->requesters, list) { |
| 1590 | LIST_DEL(&req->list); |
| 1591 | req->resolution = NULL; |
| 1592 | } |
| 1593 | |
| 1594 | LIST_DEL(&resolution->list); |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 1595 | pool_free(dns_resolution_pool, resolution); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1596 | } |
| 1597 | |
| 1598 | /* Links a requester (a server or a dns_srvrq) with a resolution. It returns 0 |
| 1599 | * on success, -1 otherwise. |
| 1600 | */ |
Olivier Houchard | 55dcdf4 | 2017-11-06 15:15:04 +0100 | [diff] [blame] | 1601 | int dns_link_resolution(void *requester, int requester_type, int requester_locked) |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1602 | { |
| 1603 | struct dns_resolution *res = NULL; |
| 1604 | struct dns_requester *req; |
| 1605 | struct dns_resolvers *resolvers; |
| 1606 | struct server *srv = NULL; |
| 1607 | struct dns_srvrq *srvrq = NULL; |
Baptiste Assmann | 333939c | 2019-01-21 08:34:50 +0100 | [diff] [blame] | 1608 | struct stream *stream = NULL; |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1609 | char **hostname_dn; |
| 1610 | int hostname_dn_len, query_type; |
| 1611 | |
| 1612 | switch (requester_type) { |
| 1613 | case OBJ_TYPE_SERVER: |
| 1614 | srv = (struct server *)requester; |
| 1615 | hostname_dn = &srv->hostname_dn; |
| 1616 | hostname_dn_len = srv->hostname_dn_len; |
| 1617 | resolvers = srv->resolvers; |
| 1618 | query_type = ((srv->dns_opts.family_prio == AF_INET) |
| 1619 | ? DNS_RTYPE_A |
| 1620 | : DNS_RTYPE_AAAA); |
| 1621 | break; |
| 1622 | |
| 1623 | case OBJ_TYPE_SRVRQ: |
| 1624 | srvrq = (struct dns_srvrq *)requester; |
| 1625 | hostname_dn = &srvrq->hostname_dn; |
| 1626 | hostname_dn_len = srvrq->hostname_dn_len; |
| 1627 | resolvers = srvrq->resolvers; |
| 1628 | query_type = DNS_RTYPE_SRV; |
| 1629 | break; |
| 1630 | |
Baptiste Assmann | 333939c | 2019-01-21 08:34:50 +0100 | [diff] [blame] | 1631 | case OBJ_TYPE_STREAM: |
| 1632 | stream = (struct stream *)requester; |
| 1633 | hostname_dn = &stream->dns_ctx.hostname_dn; |
| 1634 | hostname_dn_len = stream->dns_ctx.hostname_dn_len; |
| 1635 | resolvers = stream->dns_ctx.parent->arg.dns.resolvers; |
Christopher Faulet | a416843 | 2020-01-24 18:08:42 +0100 | [diff] [blame] | 1636 | query_type = ((stream->dns_ctx.parent->arg.dns.dns_opts->family_prio == AF_INET) |
Baptiste Assmann | 333939c | 2019-01-21 08:34:50 +0100 | [diff] [blame] | 1637 | ? DNS_RTYPE_A |
| 1638 | : DNS_RTYPE_AAAA); |
| 1639 | break; |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1640 | default: |
| 1641 | goto err; |
| 1642 | } |
| 1643 | |
| 1644 | /* Get a resolution from the resolvers' wait queue or pool */ |
| 1645 | if ((res = dns_pick_resolution(resolvers, hostname_dn, hostname_dn_len, query_type)) == NULL) |
| 1646 | goto err; |
| 1647 | |
| 1648 | if (srv) { |
Olivier Houchard | 55dcdf4 | 2017-11-06 15:15:04 +0100 | [diff] [blame] | 1649 | if (!requester_locked) |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1650 | HA_SPIN_LOCK(SERVER_LOCK, &srv->lock); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1651 | if (srv->dns_requester == NULL) { |
Baptiste Assmann | dfd35fd | 2019-01-21 08:18:09 +0100 | [diff] [blame] | 1652 | if ((req = pool_alloc(dns_requester_pool)) == NULL) { |
Olivier Houchard | 55dcdf4 | 2017-11-06 15:15:04 +0100 | [diff] [blame] | 1653 | if (!requester_locked) |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1654 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1655 | goto err; |
Willy Tarreau | 5ec8457 | 2017-11-05 10:35:57 +0100 | [diff] [blame] | 1656 | } |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1657 | req->owner = &srv->obj_type; |
| 1658 | srv->dns_requester = req; |
| 1659 | } |
| 1660 | else |
| 1661 | req = srv->dns_requester; |
Olivier Houchard | 55dcdf4 | 2017-11-06 15:15:04 +0100 | [diff] [blame] | 1662 | if (!requester_locked) |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1663 | HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock); |
Baptiste Assmann | db4c852 | 2018-01-30 08:08:04 +0100 | [diff] [blame] | 1664 | |
| 1665 | req->requester_cb = snr_resolution_cb; |
| 1666 | req->requester_error_cb = snr_resolution_error_cb; |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1667 | } |
| 1668 | else if (srvrq) { |
| 1669 | if (srvrq->dns_requester == NULL) { |
Baptiste Assmann | dfd35fd | 2019-01-21 08:18:09 +0100 | [diff] [blame] | 1670 | if ((req = pool_alloc(dns_requester_pool)) == NULL) |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1671 | goto err; |
| 1672 | req->owner = &srvrq->obj_type; |
| 1673 | srvrq->dns_requester = req; |
| 1674 | } |
| 1675 | else |
| 1676 | req = srvrq->dns_requester; |
Baptiste Assmann | db4c852 | 2018-01-30 08:08:04 +0100 | [diff] [blame] | 1677 | |
| 1678 | req->requester_cb = snr_resolution_cb; |
| 1679 | req->requester_error_cb = snr_resolution_error_cb; |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1680 | } |
Baptiste Assmann | 333939c | 2019-01-21 08:34:50 +0100 | [diff] [blame] | 1681 | else if (stream) { |
| 1682 | if (stream->dns_ctx.dns_requester == NULL) { |
| 1683 | if ((req = pool_alloc(dns_requester_pool)) == NULL) |
| 1684 | goto err; |
| 1685 | req->owner = &stream->obj_type; |
| 1686 | stream->dns_ctx.dns_requester = req; |
| 1687 | } |
| 1688 | else |
| 1689 | req = stream->dns_ctx.dns_requester; |
| 1690 | |
| 1691 | req->requester_cb = act_resolution_cb; |
| 1692 | req->requester_error_cb = act_resolution_error_cb; |
| 1693 | } |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1694 | else |
| 1695 | goto err; |
| 1696 | |
| 1697 | req->resolution = res; |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1698 | |
| 1699 | LIST_ADDQ(&res->requesters, &req->list); |
| 1700 | return 0; |
| 1701 | |
| 1702 | err: |
| 1703 | if (res && LIST_ISEMPTY(&res->requesters)) |
| 1704 | dns_free_resolution(res); |
| 1705 | return -1; |
| 1706 | } |
| 1707 | |
| 1708 | /* Removes a requester from a DNS resoltion. It takes takes care of all the |
| 1709 | * consequences. It also cleans up some parameters from the requester. |
| 1710 | */ |
| 1711 | void dns_unlink_resolution(struct dns_requester *requester) |
| 1712 | { |
| 1713 | struct dns_resolution *res; |
| 1714 | struct dns_requester *req; |
| 1715 | |
| 1716 | /* Nothing to do */ |
| 1717 | if (!requester || !requester->resolution) |
| 1718 | return; |
| 1719 | res = requester->resolution; |
| 1720 | |
| 1721 | /* Clean up the requester */ |
| 1722 | LIST_DEL(&requester->list); |
| 1723 | requester->resolution = NULL; |
| 1724 | |
| 1725 | /* We need to find another requester linked on this resolution */ |
| 1726 | if (!LIST_ISEMPTY(&res->requesters)) |
| 1727 | req = LIST_NEXT(&res->requesters, struct dns_requester *, list); |
| 1728 | else { |
| 1729 | dns_free_resolution(res); |
| 1730 | return; |
| 1731 | } |
| 1732 | |
| 1733 | /* Move hostname_dn related pointers to the next requester */ |
| 1734 | switch (obj_type(req->owner)) { |
| 1735 | case OBJ_TYPE_SERVER: |
Willy Tarreau | 433c16f | 2018-09-20 11:15:27 +0200 | [diff] [blame] | 1736 | res->hostname_dn = __objt_server(req->owner)->hostname_dn; |
| 1737 | res->hostname_dn_len = __objt_server(req->owner)->hostname_dn_len; |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1738 | break; |
| 1739 | case OBJ_TYPE_SRVRQ: |
Willy Tarreau | 433c16f | 2018-09-20 11:15:27 +0200 | [diff] [blame] | 1740 | res->hostname_dn = __objt_dns_srvrq(req->owner)->hostname_dn; |
| 1741 | res->hostname_dn_len = __objt_dns_srvrq(req->owner)->hostname_dn_len; |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1742 | break; |
Baptiste Assmann | 333939c | 2019-01-21 08:34:50 +0100 | [diff] [blame] | 1743 | case OBJ_TYPE_STREAM: |
| 1744 | res->hostname_dn = __objt_stream(req->owner)->dns_ctx.hostname_dn; |
| 1745 | res->hostname_dn_len = __objt_stream(req->owner)->dns_ctx.hostname_dn_len; |
| 1746 | break; |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1747 | default: |
| 1748 | res->hostname_dn = NULL; |
| 1749 | res->hostname_dn_len = 0; |
| 1750 | break; |
| 1751 | } |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 1752 | } |
| 1753 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1754 | /* Called when a network IO is generated on a name server socket for an incoming |
| 1755 | * packet. It performs the following actions: |
| 1756 | * - check if the packet requires processing (not outdated resolution) |
| 1757 | * - ensure the DNS packet received is valid and call requester's callback |
| 1758 | * - call requester's error callback if invalid response |
| 1759 | * - check the dn_name in the packet against the one sent |
| 1760 | */ |
| 1761 | static void dns_resolve_recv(struct dgram_conn *dgram) |
| 1762 | { |
| 1763 | struct dns_nameserver *ns, *tmpns; |
| 1764 | struct dns_resolvers *resolvers; |
| 1765 | struct dns_resolution *res; |
| 1766 | struct dns_query_item *query; |
| 1767 | unsigned char buf[DNS_MAX_UDP_MESSAGE + 1]; |
| 1768 | unsigned char *bufend; |
| 1769 | int fd, buflen, dns_resp; |
| 1770 | int max_answer_records; |
| 1771 | unsigned short query_id; |
| 1772 | struct eb32_node *eb; |
| 1773 | struct dns_requester *req; |
| 1774 | |
| 1775 | fd = dgram->t.sock.fd; |
| 1776 | |
| 1777 | /* check if ready for reading */ |
| 1778 | if (!fd_recv_ready(fd)) |
| 1779 | return; |
| 1780 | |
| 1781 | /* no need to go further if we can't retrieve the nameserver */ |
Willy Tarreau | 1c75995 | 2019-12-10 18:38:09 +0100 | [diff] [blame] | 1782 | if ((ns = dgram->owner) == NULL) { |
| 1783 | _HA_ATOMIC_AND(&fdtab[fd].ev, ~(FD_POLL_HUP|FD_POLL_ERR)); |
| 1784 | fd_stop_recv(fd); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1785 | return; |
Willy Tarreau | 1c75995 | 2019-12-10 18:38:09 +0100 | [diff] [blame] | 1786 | } |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1787 | |
| 1788 | resolvers = ns->resolvers; |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1789 | HA_SPIN_LOCK(DNS_LOCK, &resolvers->lock); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1790 | |
| 1791 | /* process all pending input messages */ |
Willy Tarreau | 1c75995 | 2019-12-10 18:38:09 +0100 | [diff] [blame] | 1792 | while (fd_recv_ready(fd)) { |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1793 | /* read message received */ |
| 1794 | memset(buf, '\0', resolvers->accepted_payload_size + 1); |
| 1795 | if ((buflen = recv(fd, (char*)buf , resolvers->accepted_payload_size + 1, 0)) < 0) { |
Willy Tarreau | 1c75995 | 2019-12-10 18:38:09 +0100 | [diff] [blame] | 1796 | /* FIXME : for now we consider EAGAIN only, but at |
| 1797 | * least we purge sticky errors that would cause us to |
| 1798 | * be called in loops. |
| 1799 | */ |
| 1800 | _HA_ATOMIC_AND(&fdtab[fd].ev, ~(FD_POLL_HUP|FD_POLL_ERR)); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1801 | fd_cant_recv(fd); |
| 1802 | break; |
| 1803 | } |
| 1804 | |
| 1805 | /* message too big */ |
| 1806 | if (buflen > resolvers->accepted_payload_size) { |
| 1807 | ns->counters.too_big++; |
| 1808 | continue; |
| 1809 | } |
| 1810 | |
| 1811 | /* initializing variables */ |
| 1812 | bufend = buf + buflen; /* pointer to mark the end of the buffer */ |
| 1813 | |
| 1814 | /* read the query id from the packet (16 bits) */ |
| 1815 | if (buf + 2 > bufend) { |
| 1816 | ns->counters.invalid++; |
| 1817 | continue; |
| 1818 | } |
| 1819 | query_id = dns_response_get_query_id(buf); |
| 1820 | |
| 1821 | /* search the query_id in the pending resolution tree */ |
| 1822 | eb = eb32_lookup(&resolvers->query_ids, query_id); |
| 1823 | if (eb == NULL) { |
| 1824 | /* unknown query id means an outdated response and can be safely ignored */ |
| 1825 | ns->counters.outdated++; |
| 1826 | continue; |
| 1827 | } |
| 1828 | |
William Dauchy | be8a387 | 2019-11-27 23:32:41 +0100 | [diff] [blame] | 1829 | /* known query id means a resolution in progress */ |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1830 | res = eb32_entry(eb, struct dns_resolution, qid); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1831 | /* number of responses received */ |
| 1832 | res->nb_responses++; |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 1833 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1834 | max_answer_records = (resolvers->accepted_payload_size - DNS_HEADER_SIZE) / DNS_MIN_RECORD_SIZE; |
| 1835 | dns_resp = dns_validate_dns_response(buf, bufend, res, max_answer_records); |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 1836 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1837 | switch (dns_resp) { |
| 1838 | case DNS_RESP_VALID: |
| 1839 | break; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 1840 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1841 | case DNS_RESP_INVALID: |
| 1842 | case DNS_RESP_QUERY_COUNT_ERROR: |
| 1843 | case DNS_RESP_WRONG_NAME: |
| 1844 | res->status = RSLV_STATUS_INVALID; |
| 1845 | ns->counters.invalid++; |
| 1846 | break; |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 1847 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1848 | case DNS_RESP_NX_DOMAIN: |
| 1849 | res->status = RSLV_STATUS_NX; |
| 1850 | ns->counters.nx++; |
| 1851 | break; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 1852 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1853 | case DNS_RESP_REFUSED: |
| 1854 | res->status = RSLV_STATUS_REFUSED; |
| 1855 | ns->counters.refused++; |
| 1856 | break; |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 1857 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1858 | case DNS_RESP_ANCOUNT_ZERO: |
| 1859 | res->status = RSLV_STATUS_OTHER; |
| 1860 | ns->counters.any_err++; |
| 1861 | break; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 1862 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1863 | case DNS_RESP_CNAME_ERROR: |
| 1864 | res->status = RSLV_STATUS_OTHER; |
| 1865 | ns->counters.cname_error++; |
| 1866 | break; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 1867 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1868 | case DNS_RESP_TRUNCATED: |
| 1869 | res->status = RSLV_STATUS_OTHER; |
| 1870 | ns->counters.truncated++; |
| 1871 | break; |
Baptiste Assmann | e70bc05 | 2017-08-21 16:51:09 +0200 | [diff] [blame] | 1872 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1873 | case DNS_RESP_NO_EXPECTED_RECORD: |
| 1874 | case DNS_RESP_ERROR: |
| 1875 | case DNS_RESP_INTERNAL: |
| 1876 | res->status = RSLV_STATUS_OTHER; |
| 1877 | ns->counters.other++; |
| 1878 | break; |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 1879 | } |
| 1880 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1881 | /* Wait all nameservers response to handle errors */ |
| 1882 | if (dns_resp != DNS_RESP_VALID && res->nb_responses < resolvers->nb_nameservers) |
| 1883 | continue; |
Olivier Houchard | 8da5f98 | 2017-08-04 18:35:36 +0200 | [diff] [blame] | 1884 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1885 | /* Process error codes */ |
| 1886 | if (dns_resp != DNS_RESP_VALID) { |
| 1887 | if (res->prefered_query_type != res->query_type) { |
| 1888 | /* The fallback on the query type was already performed, |
| 1889 | * so check the try counter. If it falls to 0, we can |
| 1890 | * report an error. Else, wait the next attempt. */ |
| 1891 | if (!res->try) |
| 1892 | goto report_res_error; |
| 1893 | } |
| 1894 | else { |
| 1895 | /* Fallback from A to AAAA or the opposite and re-send |
| 1896 | * the resolution immediately. try counter is not |
| 1897 | * decremented. */ |
| 1898 | if (res->prefered_query_type == DNS_RTYPE_A) { |
| 1899 | res->query_type = DNS_RTYPE_AAAA; |
| 1900 | dns_send_query(res); |
Olivier Houchard | 8da5f98 | 2017-08-04 18:35:36 +0200 | [diff] [blame] | 1901 | } |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1902 | else if (res->prefered_query_type == DNS_RTYPE_AAAA) { |
| 1903 | res->query_type = DNS_RTYPE_A; |
| 1904 | dns_send_query(res); |
Olivier Houchard | 8da5f98 | 2017-08-04 18:35:36 +0200 | [diff] [blame] | 1905 | } |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1906 | } |
| 1907 | continue; |
| 1908 | } |
Olivier Houchard | 8da5f98 | 2017-08-04 18:35:36 +0200 | [diff] [blame] | 1909 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1910 | /* Now let's check the query's dname corresponds to the one we |
| 1911 | * sent. We can check only the first query of the list. We send |
| 1912 | * one query at a time so we get one query in the response */ |
| 1913 | query = LIST_NEXT(&res->response.query_list, struct dns_query_item *, list); |
| 1914 | if (query && memcmp(query->name, res->hostname_dn, res->hostname_dn_len) != 0) { |
| 1915 | dns_resp = DNS_RESP_WRONG_NAME; |
| 1916 | ns->counters.other++; |
| 1917 | goto report_res_error; |
| 1918 | } |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 1919 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1920 | /* So the resolution succeeded */ |
| 1921 | res->status = RSLV_STATUS_VALID; |
| 1922 | res->last_valid = now_ms; |
| 1923 | ns->counters.valid++; |
| 1924 | goto report_res_success; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 1925 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1926 | report_res_error: |
| 1927 | list_for_each_entry(req, &res->requesters, list) |
| 1928 | req->requester_error_cb(req, dns_resp); |
| 1929 | dns_reset_resolution(res); |
| 1930 | LIST_DEL(&res->list); |
| 1931 | LIST_ADDQ(&resolvers->resolutions.wait, &res->list); |
| 1932 | continue; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 1933 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1934 | report_res_success: |
| 1935 | /* Only the 1rst requester s managed by the server, others are |
| 1936 | * from the cache */ |
| 1937 | tmpns = ns; |
| 1938 | list_for_each_entry(req, &res->requesters, list) { |
Olivier Houchard | 2838107 | 2017-11-06 17:30:28 +0100 | [diff] [blame] | 1939 | struct server *s = objt_server(req->owner); |
| 1940 | |
| 1941 | if (s) |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1942 | HA_SPIN_LOCK(SERVER_LOCK, &s->lock); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1943 | req->requester_cb(req, tmpns); |
Olivier Houchard | 2838107 | 2017-11-06 17:30:28 +0100 | [diff] [blame] | 1944 | if (s) |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1945 | HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1946 | tmpns = NULL; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 1947 | } |
Baptiste Assmann | 4274637 | 2017-05-03 12:12:02 +0200 | [diff] [blame] | 1948 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1949 | dns_reset_resolution(res); |
| 1950 | LIST_DEL(&res->list); |
| 1951 | LIST_ADDQ(&resolvers->resolutions.wait, &res->list); |
| 1952 | continue; |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 1953 | } |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 1954 | dns_update_resolvers_timeout(resolvers); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1955 | HA_SPIN_UNLOCK(DNS_LOCK, &resolvers->lock); |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 1956 | } |
William Lallemand | 69e9644 | 2016-11-19 00:58:54 +0100 | [diff] [blame] | 1957 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1958 | /* Called when a resolvers network socket is ready to send data */ |
| 1959 | static void dns_resolve_send(struct dgram_conn *dgram) |
Baptiste Assmann | fa4a663 | 2017-05-04 09:05:00 +0200 | [diff] [blame] | 1960 | { |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1961 | struct dns_resolvers *resolvers; |
| 1962 | struct dns_nameserver *ns; |
| 1963 | struct dns_resolution *res; |
| 1964 | int fd; |
Baptiste Assmann | fa4a663 | 2017-05-04 09:05:00 +0200 | [diff] [blame] | 1965 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1966 | fd = dgram->t.sock.fd; |
Baptiste Assmann | fa4a663 | 2017-05-04 09:05:00 +0200 | [diff] [blame] | 1967 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1968 | /* check if ready for sending */ |
| 1969 | if (!fd_send_ready(fd)) |
| 1970 | return; |
Baptiste Assmann | fa4a663 | 2017-05-04 09:05:00 +0200 | [diff] [blame] | 1971 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1972 | /* we don't want/need to be waked up any more for sending */ |
| 1973 | fd_stop_send(fd); |
Baptiste Assmann | fa4a663 | 2017-05-04 09:05:00 +0200 | [diff] [blame] | 1974 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1975 | /* no need to go further if we can't retrieve the nameserver */ |
| 1976 | if ((ns = dgram->owner) == NULL) |
| 1977 | return; |
Baptiste Assmann | fa4a663 | 2017-05-04 09:05:00 +0200 | [diff] [blame] | 1978 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1979 | resolvers = ns->resolvers; |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1980 | HA_SPIN_LOCK(DNS_LOCK, &resolvers->lock); |
Christopher Faulet | b2812a6 | 2017-10-04 16:17:58 +0200 | [diff] [blame] | 1981 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1982 | list_for_each_entry(res, &resolvers->resolutions.curr, list) { |
Willy Tarreau | f6ee9dc | 2018-08-22 04:52:02 +0200 | [diff] [blame] | 1983 | int ret, len; |
Baptiste Assmann | fa4a663 | 2017-05-04 09:05:00 +0200 | [diff] [blame] | 1984 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1985 | if (res->nb_queries == resolvers->nb_nameservers) |
| 1986 | continue; |
Baptiste Assmann | fa4a663 | 2017-05-04 09:05:00 +0200 | [diff] [blame] | 1987 | |
Willy Tarreau | f6ee9dc | 2018-08-22 04:52:02 +0200 | [diff] [blame] | 1988 | len = dns_build_query(res->query_id, res->query_type, |
| 1989 | resolvers->accepted_payload_size, |
| 1990 | res->hostname_dn, res->hostname_dn_len, |
| 1991 | trash.area, trash.size); |
| 1992 | if (len == -1) |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1993 | goto snd_error; |
Baptiste Assmann | fa4a663 | 2017-05-04 09:05:00 +0200 | [diff] [blame] | 1994 | |
Willy Tarreau | f6ee9dc | 2018-08-22 04:52:02 +0200 | [diff] [blame] | 1995 | ret = send(fd, trash.area, len, 0); |
Willy Tarreau | 0eae632 | 2019-12-20 11:18:54 +0100 | [diff] [blame] | 1996 | if (ret != len) { |
| 1997 | if (ret == -1 && errno == EAGAIN) { |
| 1998 | /* retry once the socket is ready */ |
| 1999 | fd_cant_send(fd); |
| 2000 | continue; |
| 2001 | } |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2002 | goto snd_error; |
Willy Tarreau | 0eae632 | 2019-12-20 11:18:54 +0100 | [diff] [blame] | 2003 | } |
Baptiste Assmann | fa4a663 | 2017-05-04 09:05:00 +0200 | [diff] [blame] | 2004 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2005 | ns->counters.sent++; |
| 2006 | res->nb_queries++; |
| 2007 | continue; |
Baptiste Assmann | fa4a663 | 2017-05-04 09:05:00 +0200 | [diff] [blame] | 2008 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2009 | snd_error: |
| 2010 | ns->counters.snd_error++; |
| 2011 | res->nb_queries++; |
Baptiste Assmann | fa4a663 | 2017-05-04 09:05:00 +0200 | [diff] [blame] | 2012 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2013 | HA_SPIN_UNLOCK(DNS_LOCK, &resolvers->lock); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2014 | } |
Baptiste Assmann | fa4a663 | 2017-05-04 09:05:00 +0200 | [diff] [blame] | 2015 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2016 | /* Processes DNS resolution. First, it checks the active list to detect expired |
| 2017 | * resolutions and retry them if possible. Else a timeout is reported. Then, it |
| 2018 | * checks the wait list to trigger new resolutions. |
| 2019 | */ |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 2020 | static struct task *dns_process_resolvers(struct task *t, void *context, unsigned short state) |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2021 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 2022 | struct dns_resolvers *resolvers = context; |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2023 | struct dns_resolution *res, *resback; |
| 2024 | int exp; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 2025 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2026 | HA_SPIN_LOCK(DNS_LOCK, &resolvers->lock); |
Christopher Faulet | b2812a6 | 2017-10-04 16:17:58 +0200 | [diff] [blame] | 2027 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2028 | /* Handle all expired resolutions from the active list */ |
| 2029 | list_for_each_entry_safe(res, resback, &resolvers->resolutions.curr, list) { |
| 2030 | /* When we find the first resolution in the future, then we can |
| 2031 | * stop here */ |
| 2032 | exp = tick_add(res->last_query, resolvers->timeout.retry); |
| 2033 | if (!tick_is_expired(exp, now_ms)) |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 2034 | break; |
Baptiste Assmann | fa4a663 | 2017-05-04 09:05:00 +0200 | [diff] [blame] | 2035 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2036 | /* If current resolution has been tried too many times and |
| 2037 | * finishes in timeout we update its status and remove it from |
| 2038 | * the list */ |
| 2039 | if (!res->try) { |
| 2040 | struct dns_requester *req; |
Baptiste Assmann | fa4a663 | 2017-05-04 09:05:00 +0200 | [diff] [blame] | 2041 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2042 | /* Notify the result to the requesters */ |
| 2043 | if (!res->nb_responses) |
| 2044 | res->status = RSLV_STATUS_TIMEOUT; |
| 2045 | list_for_each_entry(req, &res->requesters, list) |
| 2046 | req->requester_error_cb(req, res->status); |
Baptiste Assmann | fa4a663 | 2017-05-04 09:05:00 +0200 | [diff] [blame] | 2047 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2048 | /* Clean up resolution info and remove it from the |
| 2049 | * current list */ |
| 2050 | dns_reset_resolution(res); |
| 2051 | LIST_DEL(&res->list); |
| 2052 | LIST_ADDQ(&resolvers->resolutions.wait, &res->list); |
William Lallemand | 69e9644 | 2016-11-19 00:58:54 +0100 | [diff] [blame] | 2053 | } |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2054 | else { |
| 2055 | /* Otherwise resend the DNS query and requeue the resolution */ |
| 2056 | if (!res->nb_responses || res->prefered_query_type != res->query_type) { |
| 2057 | /* No response received (a real timeout) or fallback already done */ |
| 2058 | res->query_type = res->prefered_query_type; |
| 2059 | res->try--; |
| 2060 | } |
| 2061 | else { |
| 2062 | /* Fallback from A to AAAA or the opposite and re-send |
| 2063 | * the resolution immediately. try counter is not |
| 2064 | * decremented. */ |
| 2065 | if (res->prefered_query_type == DNS_RTYPE_A) |
| 2066 | res->query_type = DNS_RTYPE_AAAA; |
| 2067 | else if (res->prefered_query_type == DNS_RTYPE_AAAA) |
| 2068 | res->query_type = DNS_RTYPE_A; |
| 2069 | else |
| 2070 | res->try--; |
| 2071 | } |
| 2072 | dns_send_query(res); |
William Lallemand | 69e9644 | 2016-11-19 00:58:54 +0100 | [diff] [blame] | 2073 | } |
| 2074 | } |
William Lallemand | 69e9644 | 2016-11-19 00:58:54 +0100 | [diff] [blame] | 2075 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2076 | /* Handle all resolutions in the wait list */ |
| 2077 | list_for_each_entry_safe(res, resback, &resolvers->resolutions.wait, list) { |
| 2078 | exp = tick_add(res->last_resolution, dns_resolution_timeout(res)); |
| 2079 | if (tick_isset(res->last_resolution) && !tick_is_expired(exp, now_ms)) |
| 2080 | continue; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 2081 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2082 | if (dns_run_resolution(res) != 1) { |
| 2083 | res->last_resolution = now_ms; |
| 2084 | LIST_DEL(&res->list); |
| 2085 | LIST_ADDQ(&resolvers->resolutions.wait, &res->list); |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 2086 | } |
| 2087 | } |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 2088 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2089 | dns_update_resolvers_timeout(resolvers); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2090 | HA_SPIN_UNLOCK(DNS_LOCK, &resolvers->lock); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2091 | return t; |
| 2092 | } |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 2093 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2094 | /* proto_udp callback functions for a DNS resolution */ |
| 2095 | struct dgram_data_cb resolve_dgram_cb = { |
| 2096 | .recv = dns_resolve_recv, |
| 2097 | .send = dns_resolve_send, |
| 2098 | }; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 2099 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2100 | /* Release memory allocated by DNS */ |
| 2101 | static void dns_deinit(void) |
| 2102 | { |
| 2103 | struct dns_resolvers *resolvers, *resolversback; |
| 2104 | struct dns_nameserver *ns, *nsback; |
| 2105 | struct dns_resolution *res, *resback; |
| 2106 | struct dns_requester *req, *reqback; |
| 2107 | struct dns_srvrq *srvrq, *srvrqback; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 2108 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2109 | list_for_each_entry_safe(resolvers, resolversback, &dns_resolvers, list) { |
| 2110 | list_for_each_entry_safe(ns, nsback, &resolvers->nameservers, list) { |
| 2111 | free(ns->id); |
| 2112 | free((char *)ns->conf.file); |
| 2113 | if (ns->dgram && ns->dgram->t.sock.fd != -1) |
| 2114 | fd_delete(ns->dgram->t.sock.fd); |
| 2115 | free(ns->dgram); |
| 2116 | LIST_DEL(&ns->list); |
| 2117 | free(ns); |
| 2118 | } |
| 2119 | |
| 2120 | list_for_each_entry_safe(res, resback, &resolvers->resolutions.curr, list) { |
| 2121 | list_for_each_entry_safe(req, reqback, &res->requesters, list) { |
| 2122 | LIST_DEL(&req->list); |
Baptiste Assmann | dfd35fd | 2019-01-21 08:18:09 +0100 | [diff] [blame] | 2123 | pool_free(dns_requester_pool, req); |
Olivier Houchard | 8da5f98 | 2017-08-04 18:35:36 +0200 | [diff] [blame] | 2124 | } |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2125 | dns_free_resolution(res); |
| 2126 | } |
Olivier Houchard | 8da5f98 | 2017-08-04 18:35:36 +0200 | [diff] [blame] | 2127 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2128 | list_for_each_entry_safe(res, resback, &resolvers->resolutions.wait, list) { |
| 2129 | list_for_each_entry_safe(req, reqback, &res->requesters, list) { |
| 2130 | LIST_DEL(&req->list); |
Baptiste Assmann | dfd35fd | 2019-01-21 08:18:09 +0100 | [diff] [blame] | 2131 | pool_free(dns_requester_pool, req); |
Olivier Houchard | 8da5f98 | 2017-08-04 18:35:36 +0200 | [diff] [blame] | 2132 | } |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2133 | dns_free_resolution(res); |
| 2134 | } |
Olivier Houchard | 8da5f98 | 2017-08-04 18:35:36 +0200 | [diff] [blame] | 2135 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2136 | free(resolvers->id); |
| 2137 | free((char *)resolvers->conf.file); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 2138 | task_destroy(resolvers->t); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2139 | LIST_DEL(&resolvers->list); |
| 2140 | free(resolvers); |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 2141 | } |
| 2142 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2143 | list_for_each_entry_safe(srvrq, srvrqback, &dns_srvrq_list, list) { |
| 2144 | free(srvrq->name); |
| 2145 | free(srvrq->hostname_dn); |
| 2146 | LIST_DEL(&srvrq->list); |
| 2147 | free(srvrq); |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 2148 | } |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 2149 | } |
| 2150 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2151 | /* Finalizes the DNS configuration by allocating required resources and checking |
| 2152 | * live parameters. |
| 2153 | * Returns 0 on success, ERR_* flags otherwise. |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 2154 | */ |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2155 | static int dns_finalize_config(void) |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 2156 | { |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2157 | struct dns_resolvers *resolvers; |
| 2158 | struct proxy *px; |
| 2159 | int err_code = 0; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 2160 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2161 | /* allocate pool of resolution per resolvers */ |
| 2162 | list_for_each_entry(resolvers, &dns_resolvers, list) { |
| 2163 | struct dns_nameserver *ns; |
| 2164 | struct task *t; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 2165 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2166 | /* Check if we can create the socket with nameservers info */ |
| 2167 | list_for_each_entry(ns, &resolvers->nameservers, list) { |
| 2168 | struct dgram_conn *dgram = NULL; |
| 2169 | int fd; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 2170 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2171 | /* Check nameserver info */ |
| 2172 | if ((fd = socket(ns->addr.ss_family, SOCK_DGRAM, IPPROTO_UDP)) == -1) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2173 | ha_alert("config : resolvers '%s': can't create socket for nameserver '%s'.\n", |
| 2174 | resolvers->id, ns->id); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2175 | err_code |= (ERR_ALERT|ERR_ABORT); |
Olivier Houchard | 8da5f98 | 2017-08-04 18:35:36 +0200 | [diff] [blame] | 2176 | continue; |
Olivier Houchard | 8da5f98 | 2017-08-04 18:35:36 +0200 | [diff] [blame] | 2177 | } |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2178 | if (connect(fd, (struct sockaddr*)&ns->addr, get_addr_len(&ns->addr)) == -1) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2179 | ha_alert("config : resolvers '%s': can't connect socket for nameserver '%s'.\n", |
| 2180 | resolvers->id, ns->id); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2181 | close(fd); |
| 2182 | err_code |= (ERR_ALERT|ERR_ABORT); |
Olivier Houchard | 8da5f98 | 2017-08-04 18:35:36 +0200 | [diff] [blame] | 2183 | continue; |
Olivier Houchard | 8da5f98 | 2017-08-04 18:35:36 +0200 | [diff] [blame] | 2184 | } |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2185 | close(fd); |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 2186 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2187 | /* Create dgram structure that will hold the UPD socket |
| 2188 | * and attach it on the current nameserver */ |
| 2189 | if ((dgram = calloc(1, sizeof(*dgram))) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2190 | ha_alert("config: resolvers '%s' : out of memory.\n", |
| 2191 | resolvers->id); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2192 | err_code |= (ERR_ALERT|ERR_ABORT); |
| 2193 | goto err; |
| 2194 | } |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 2195 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2196 | /* Leave dgram partially initialized, no FD attached for |
| 2197 | * now. */ |
| 2198 | dgram->owner = ns; |
| 2199 | dgram->data = &resolve_dgram_cb; |
| 2200 | dgram->t.sock.fd = -1; |
| 2201 | ns->dgram = dgram; |
| 2202 | } |
Baptiste Assmann | 81ed1a0 | 2017-05-03 10:11:44 +0200 | [diff] [blame] | 2203 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2204 | /* Create the task associated to the resolvers section */ |
Emeric Brun | c60def8 | 2017-09-27 14:59:38 +0200 | [diff] [blame] | 2205 | if ((t = task_new(MAX_THREADS_MASK)) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2206 | ha_alert("config : resolvers '%s' : out of memory.\n", resolvers->id); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2207 | err_code |= (ERR_ALERT|ERR_ABORT); |
| 2208 | goto err; |
| 2209 | } |
Baptiste Assmann | 81ed1a0 | 2017-05-03 10:11:44 +0200 | [diff] [blame] | 2210 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2211 | /* Update task's parameters */ |
| 2212 | t->process = dns_process_resolvers; |
| 2213 | t->context = resolvers; |
| 2214 | resolvers->t = t; |
| 2215 | task_wakeup(t, TASK_WOKEN_INIT); |
Baptiste Assmann | 81ed1a0 | 2017-05-03 10:11:44 +0200 | [diff] [blame] | 2216 | } |
| 2217 | |
Olivier Houchard | fbc74e8 | 2017-11-24 16:54:05 +0100 | [diff] [blame] | 2218 | for (px = proxies_list; px; px = px->next) { |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2219 | struct server *srv; |
Baptiste Assmann | 81ed1a0 | 2017-05-03 10:11:44 +0200 | [diff] [blame] | 2220 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2221 | for (srv = px->srv; srv; srv = srv->next) { |
| 2222 | struct dns_resolvers *resolvers; |
Baptiste Assmann | 81ed1a0 | 2017-05-03 10:11:44 +0200 | [diff] [blame] | 2223 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2224 | if (!srv->resolvers_id) |
| 2225 | continue; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 2226 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2227 | if ((resolvers = find_resolvers_by_id(srv->resolvers_id)) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2228 | ha_alert("config : %s '%s', server '%s': unable to find required resolvers '%s'\n", |
| 2229 | proxy_type_str(px), px->id, srv->id, srv->resolvers_id); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2230 | err_code |= (ERR_ALERT|ERR_ABORT); |
| 2231 | continue; |
| 2232 | } |
| 2233 | srv->resolvers = resolvers; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 2234 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2235 | if (srv->srvrq && !srv->srvrq->resolvers) { |
| 2236 | srv->srvrq->resolvers = srv->resolvers; |
Olivier Houchard | 55dcdf4 | 2017-11-06 15:15:04 +0100 | [diff] [blame] | 2237 | if (dns_link_resolution(srv->srvrq, OBJ_TYPE_SRVRQ, 0) == -1) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2238 | ha_alert("config : %s '%s' : unable to set DNS resolution for server '%s'.\n", |
| 2239 | proxy_type_str(px), px->id, srv->id); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2240 | err_code |= (ERR_ALERT|ERR_ABORT); |
| 2241 | continue; |
| 2242 | } |
| 2243 | } |
Olivier Houchard | 55dcdf4 | 2017-11-06 15:15:04 +0100 | [diff] [blame] | 2244 | if (dns_link_resolution(srv, OBJ_TYPE_SERVER, 0) == -1) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 2245 | ha_alert("config : %s '%s', unable to set DNS resolution for server '%s'.\n", |
| 2246 | proxy_type_str(px), px->id, srv->id); |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2247 | err_code |= (ERR_ALERT|ERR_ABORT); |
| 2248 | continue; |
| 2249 | } |
| 2250 | } |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 2251 | } |
| 2252 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2253 | if (err_code & (ERR_ALERT|ERR_ABORT)) |
| 2254 | goto err; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 2255 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2256 | return err_code; |
| 2257 | err: |
| 2258 | dns_deinit(); |
| 2259 | return err_code; |
| 2260 | |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 2261 | } |
| 2262 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2263 | /* if an arg is found, it sets the resolvers section pointer into cli.p0 */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 2264 | static int cli_parse_stat_resolvers(char **args, char *payload, struct appctx *appctx, void *private) |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 2265 | { |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2266 | struct dns_resolvers *presolvers; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 2267 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2268 | if (*args[2]) { |
| 2269 | list_for_each_entry(presolvers, &dns_resolvers, list) { |
| 2270 | if (strcmp(presolvers->id, args[2]) == 0) { |
| 2271 | appctx->ctx.cli.p0 = presolvers; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 2272 | break; |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2273 | } |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 2274 | } |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 2275 | if (appctx->ctx.cli.p0 == NULL) |
| 2276 | return cli_err(appctx, "Can't find that resolvers section\n"); |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 2277 | } |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2278 | return 0; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 2279 | } |
| 2280 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2281 | /* Dumps counters from all resolvers section and associated name servers. It |
| 2282 | * returns 0 if the output buffer is full and it needs to be called again, |
| 2283 | * 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] | 2284 | * <cli.p0> if it's not null. |
William Lallemand | 69e9644 | 2016-11-19 00:58:54 +0100 | [diff] [blame] | 2285 | */ |
| 2286 | static int cli_io_handler_dump_resolvers_to_buffer(struct appctx *appctx) |
| 2287 | { |
| 2288 | struct stream_interface *si = appctx->owner; |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2289 | struct dns_resolvers *resolvers; |
| 2290 | struct dns_nameserver *ns; |
William Lallemand | 69e9644 | 2016-11-19 00:58:54 +0100 | [diff] [blame] | 2291 | |
| 2292 | chunk_reset(&trash); |
| 2293 | |
| 2294 | switch (appctx->st2) { |
| 2295 | case STAT_ST_INIT: |
| 2296 | appctx->st2 = STAT_ST_LIST; /* let's start producing data */ |
| 2297 | /* fall through */ |
| 2298 | |
| 2299 | case STAT_ST_LIST: |
| 2300 | if (LIST_ISEMPTY(&dns_resolvers)) { |
| 2301 | chunk_appendf(&trash, "No resolvers found\n"); |
| 2302 | } |
| 2303 | else { |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2304 | list_for_each_entry(resolvers, &dns_resolvers, list) { |
| 2305 | if (appctx->ctx.cli.p0 != NULL && appctx->ctx.cli.p0 != resolvers) |
William Lallemand | 69e9644 | 2016-11-19 00:58:54 +0100 | [diff] [blame] | 2306 | continue; |
| 2307 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2308 | chunk_appendf(&trash, "Resolvers section %s\n", resolvers->id); |
| 2309 | list_for_each_entry(ns, &resolvers->nameservers, list) { |
| 2310 | chunk_appendf(&trash, " nameserver %s:\n", ns->id); |
| 2311 | chunk_appendf(&trash, " sent: %lld\n", ns->counters.sent); |
| 2312 | chunk_appendf(&trash, " snd_error: %lld\n", ns->counters.snd_error); |
| 2313 | chunk_appendf(&trash, " valid: %lld\n", ns->counters.valid); |
| 2314 | chunk_appendf(&trash, " update: %lld\n", ns->counters.update); |
| 2315 | chunk_appendf(&trash, " cname: %lld\n", ns->counters.cname); |
| 2316 | chunk_appendf(&trash, " cname_error: %lld\n", ns->counters.cname_error); |
| 2317 | chunk_appendf(&trash, " any_err: %lld\n", ns->counters.any_err); |
| 2318 | chunk_appendf(&trash, " nx: %lld\n", ns->counters.nx); |
| 2319 | chunk_appendf(&trash, " timeout: %lld\n", ns->counters.timeout); |
| 2320 | chunk_appendf(&trash, " refused: %lld\n", ns->counters.refused); |
| 2321 | chunk_appendf(&trash, " other: %lld\n", ns->counters.other); |
| 2322 | chunk_appendf(&trash, " invalid: %lld\n", ns->counters.invalid); |
| 2323 | chunk_appendf(&trash, " too_big: %lld\n", ns->counters.too_big); |
| 2324 | chunk_appendf(&trash, " truncated: %lld\n", ns->counters.truncated); |
| 2325 | chunk_appendf(&trash, " outdated: %lld\n", ns->counters.outdated); |
William Lallemand | 69e9644 | 2016-11-19 00:58:54 +0100 | [diff] [blame] | 2326 | } |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 2327 | chunk_appendf(&trash, "\n"); |
William Lallemand | 69e9644 | 2016-11-19 00:58:54 +0100 | [diff] [blame] | 2328 | } |
| 2329 | } |
| 2330 | |
| 2331 | /* display response */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2332 | if (ci_putchk(si_ic(si), &trash) == -1) { |
William Lallemand | 69e9644 | 2016-11-19 00:58:54 +0100 | [diff] [blame] | 2333 | /* let's try again later from this session. We add ourselves into |
| 2334 | * this session's users so that it can remove us upon termination. |
| 2335 | */ |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 2336 | si_rx_room_blk(si); |
William Lallemand | 69e9644 | 2016-11-19 00:58:54 +0100 | [diff] [blame] | 2337 | return 0; |
| 2338 | } |
William Lallemand | 69e9644 | 2016-11-19 00:58:54 +0100 | [diff] [blame] | 2339 | /* fall through */ |
| 2340 | |
| 2341 | default: |
| 2342 | appctx->st2 = STAT_ST_FIN; |
| 2343 | return 1; |
| 2344 | } |
| 2345 | } |
| 2346 | |
| 2347 | /* register cli keywords */ |
Christopher Faulet | ff88efb | 2017-10-03 16:00:57 +0200 | [diff] [blame] | 2348 | static struct cli_kw_list cli_kws = {{ }, { |
| 2349 | { { "show", "resolvers", NULL }, "show resolvers [id]: dumps counters from all resolvers section and\n" |
| 2350 | " associated name servers", |
| 2351 | cli_parse_stat_resolvers, cli_io_handler_dump_resolvers_to_buffer }, |
| 2352 | {{},} |
| 2353 | } |
| 2354 | }; |
William Lallemand | 69e9644 | 2016-11-19 00:58:54 +0100 | [diff] [blame] | 2355 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 2356 | INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws); |
William Lallemand | 69e9644 | 2016-11-19 00:58:54 +0100 | [diff] [blame] | 2357 | |
Baptiste Assmann | 333939c | 2019-01-21 08:34:50 +0100 | [diff] [blame] | 2358 | /* |
| 2359 | * Prepare <rule> for hostname resolution. |
| 2360 | * Returns -1 in case of any allocation failure, 0 if not. |
| 2361 | * On error, a global failure counter is also incremented. |
| 2362 | */ |
| 2363 | static int action_prepare_for_resolution(struct stream *stream, const char *hostname) |
| 2364 | { |
| 2365 | char *hostname_dn; |
| 2366 | int hostname_len, hostname_dn_len; |
| 2367 | struct buffer *tmp = get_trash_chunk(); |
| 2368 | |
| 2369 | if (!hostname) |
| 2370 | return 0; |
| 2371 | |
| 2372 | hostname_len = strlen(hostname); |
| 2373 | hostname_dn = tmp->area; |
| 2374 | hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1, |
| 2375 | hostname_dn, tmp->size); |
| 2376 | if (hostname_dn_len == -1) |
| 2377 | goto err; |
| 2378 | |
| 2379 | |
| 2380 | stream->dns_ctx.hostname_dn = strdup(hostname_dn); |
| 2381 | stream->dns_ctx.hostname_dn_len = hostname_dn_len; |
| 2382 | if (!stream->dns_ctx.hostname_dn) |
| 2383 | goto err; |
| 2384 | |
| 2385 | return 0; |
| 2386 | |
| 2387 | err: |
| 2388 | free(stream->dns_ctx.hostname_dn); stream->dns_ctx.hostname_dn = NULL; |
| 2389 | dns_failed_resolutions += 1; |
| 2390 | return -1; |
| 2391 | } |
| 2392 | |
| 2393 | |
| 2394 | /* |
| 2395 | * Execute the "do-resolution" action. May be called from {tcp,http}request. |
| 2396 | */ |
| 2397 | enum act_return dns_action_do_resolve(struct act_rule *rule, struct proxy *px, |
| 2398 | struct session *sess, struct stream *s, int flags) |
| 2399 | { |
Baptiste Assmann | 333939c | 2019-01-21 08:34:50 +0100 | [diff] [blame] | 2400 | struct dns_resolution *resolution; |
Willy Tarreau | 45726fd | 2019-07-17 10:38:45 +0200 | [diff] [blame] | 2401 | struct sample *smp; |
| 2402 | char *fqdn; |
Baptiste Assmann | 7264dfe | 2019-10-30 16:06:53 +0100 | [diff] [blame] | 2403 | struct dns_requester *req; |
| 2404 | struct dns_resolvers *resolvers; |
| 2405 | struct dns_resolution *res; |
| 2406 | int exp; |
Baptiste Assmann | 333939c | 2019-01-21 08:34:50 +0100 | [diff] [blame] | 2407 | |
| 2408 | /* we have a response to our DNS resolution */ |
Baptiste Assmann | 7264dfe | 2019-10-30 16:06:53 +0100 | [diff] [blame] | 2409 | use_cache: |
Baptiste Assmann | 333939c | 2019-01-21 08:34:50 +0100 | [diff] [blame] | 2410 | if (s->dns_ctx.dns_requester && s->dns_ctx.dns_requester->resolution != NULL) { |
| 2411 | resolution = s->dns_ctx.dns_requester->resolution; |
Baptiste Assmann | 4c52e4b | 2019-10-01 15:32:40 +0200 | [diff] [blame] | 2412 | if (resolution->step == RSLV_STEP_RUNNING) { |
| 2413 | return ACT_RET_YIELD; |
| 2414 | } |
Baptiste Assmann | 333939c | 2019-01-21 08:34:50 +0100 | [diff] [blame] | 2415 | if (resolution->step == RSLV_STEP_NONE) { |
| 2416 | /* We update the variable only if we have a valid response. */ |
| 2417 | if (resolution->status == RSLV_STATUS_VALID) { |
| 2418 | struct sample smp; |
| 2419 | short ip_sin_family = 0; |
| 2420 | void *ip = NULL; |
| 2421 | |
Christopher Faulet | a416843 | 2020-01-24 18:08:42 +0100 | [diff] [blame] | 2422 | dns_get_ip_from_response(&resolution->response, rule->arg.dns.dns_opts, NULL, |
Baptiste Assmann | 333939c | 2019-01-21 08:34:50 +0100 | [diff] [blame] | 2423 | 0, &ip, &ip_sin_family, NULL); |
| 2424 | |
| 2425 | switch (ip_sin_family) { |
| 2426 | case AF_INET: |
| 2427 | smp.data.type = SMP_T_IPV4; |
| 2428 | memcpy(&smp.data.u.ipv4, ip, 4); |
| 2429 | break; |
| 2430 | case AF_INET6: |
| 2431 | smp.data.type = SMP_T_IPV6; |
| 2432 | memcpy(&smp.data.u.ipv6, ip, 16); |
| 2433 | break; |
| 2434 | default: |
| 2435 | ip = NULL; |
| 2436 | } |
| 2437 | |
| 2438 | if (ip) { |
| 2439 | smp.px = px; |
| 2440 | smp.sess = sess; |
| 2441 | smp.strm = s; |
| 2442 | |
| 2443 | vars_set_by_name(rule->arg.dns.varname, strlen(rule->arg.dns.varname), &smp); |
| 2444 | } |
| 2445 | } |
| 2446 | } |
| 2447 | |
| 2448 | free(s->dns_ctx.hostname_dn); s->dns_ctx.hostname_dn = NULL; |
| 2449 | s->dns_ctx.hostname_dn_len = 0; |
| 2450 | dns_unlink_resolution(s->dns_ctx.dns_requester); |
| 2451 | |
| 2452 | pool_free(dns_requester_pool, s->dns_ctx.dns_requester); |
| 2453 | s->dns_ctx.dns_requester = NULL; |
| 2454 | |
| 2455 | return ACT_RET_CONT; |
| 2456 | } |
| 2457 | |
| 2458 | /* need to configure and start a new DNS resolution */ |
Willy Tarreau | 45726fd | 2019-07-17 10:38:45 +0200 | [diff] [blame] | 2459 | smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.dns.expr, SMP_T_STR); |
| 2460 | if (smp == NULL) |
| 2461 | return ACT_RET_CONT; |
Baptiste Assmann | 333939c | 2019-01-21 08:34:50 +0100 | [diff] [blame] | 2462 | |
Willy Tarreau | 45726fd | 2019-07-17 10:38:45 +0200 | [diff] [blame] | 2463 | fqdn = smp->data.u.str.area; |
| 2464 | if (action_prepare_for_resolution(s, fqdn) == -1) |
Christopher Faulet | 1340376 | 2019-12-13 09:01:57 +0100 | [diff] [blame] | 2465 | return ACT_RET_CONT; /* on error, ignore the action */ |
Baptiste Assmann | 333939c | 2019-01-21 08:34:50 +0100 | [diff] [blame] | 2466 | |
Willy Tarreau | 45726fd | 2019-07-17 10:38:45 +0200 | [diff] [blame] | 2467 | s->dns_ctx.parent = rule; |
| 2468 | dns_link_resolution(s, OBJ_TYPE_STREAM, 0); |
Baptiste Assmann | 7264dfe | 2019-10-30 16:06:53 +0100 | [diff] [blame] | 2469 | |
| 2470 | /* Check if there is a fresh enough response in the cache of our associated resolution */ |
| 2471 | req = s->dns_ctx.dns_requester; |
| 2472 | if (!req || !req->resolution) { |
| 2473 | dns_trigger_resolution(s->dns_ctx.dns_requester); |
| 2474 | return ACT_RET_YIELD; |
| 2475 | } |
| 2476 | res = req->resolution; |
| 2477 | resolvers = res->resolvers; |
| 2478 | |
| 2479 | exp = tick_add(res->last_resolution, resolvers->hold.valid); |
| 2480 | if (resolvers->t && res->status == RSLV_STATUS_VALID && tick_isset(res->last_resolution) |
| 2481 | && !tick_is_expired(exp, now_ms)) { |
| 2482 | goto use_cache; |
| 2483 | } |
| 2484 | |
Willy Tarreau | 45726fd | 2019-07-17 10:38:45 +0200 | [diff] [blame] | 2485 | dns_trigger_resolution(s->dns_ctx.dns_requester); |
Baptiste Assmann | 333939c | 2019-01-21 08:34:50 +0100 | [diff] [blame] | 2486 | return ACT_RET_YIELD; |
| 2487 | } |
| 2488 | |
Christopher Faulet | 3b2bb63 | 2020-01-24 18:12:58 +0100 | [diff] [blame] | 2489 | static void release_dns_action(struct act_rule *rule) |
| 2490 | { |
| 2491 | release_sample_expr(rule->arg.dns.expr); |
| 2492 | free(rule->arg.dns.varname); |
| 2493 | free(rule->arg.dns.resolvers_id); |
| 2494 | free(rule->arg.dns.dns_opts); |
| 2495 | } |
| 2496 | |
Baptiste Assmann | 333939c | 2019-01-21 08:34:50 +0100 | [diff] [blame] | 2497 | |
| 2498 | /* parse "do-resolve" action |
| 2499 | * This action takes the following arguments: |
| 2500 | * do-resolve(<varName>,<resolversSectionName>,<resolvePrefer>) <expr> |
| 2501 | * |
| 2502 | * - <varName> is the variable name where the result of the DNS resolution will be stored |
| 2503 | * (mandatory) |
| 2504 | * - <resolversSectionName> is the name of the resolvers section to use to perform the resolution |
| 2505 | * (mandatory) |
| 2506 | * - <resolvePrefer> can be either 'ipv4' or 'ipv6' and is the IP family we would like to resolve first |
| 2507 | * (optional), defaults to ipv6 |
| 2508 | * - <expr> is an HAProxy expression used to fetch the name to be resolved |
| 2509 | */ |
| 2510 | enum act_parse_ret dns_parse_do_resolve(const char **args, int *orig_arg, struct proxy *px, struct act_rule *rule, char **err) |
| 2511 | { |
| 2512 | int cur_arg; |
| 2513 | struct sample_expr *expr; |
| 2514 | unsigned int where; |
| 2515 | const char *beg, *end; |
| 2516 | |
| 2517 | /* orig_arg points to the first argument, but we need to analyse the command itself first */ |
| 2518 | cur_arg = *orig_arg - 1; |
| 2519 | |
| 2520 | /* locate varName, which is mandatory */ |
| 2521 | beg = strchr(args[cur_arg], '('); |
| 2522 | if (beg == NULL) |
| 2523 | goto do_resolve_parse_error; |
| 2524 | beg = beg + 1; /* beg should points to the first character after opening parenthesis '(' */ |
| 2525 | end = strchr(beg, ','); |
| 2526 | if (end == NULL) |
| 2527 | goto do_resolve_parse_error; |
| 2528 | rule->arg.dns.varname = my_strndup(beg, end - beg); |
| 2529 | if (rule->arg.dns.varname == NULL) |
| 2530 | goto do_resolve_parse_error; |
| 2531 | |
| 2532 | |
| 2533 | /* locate resolversSectionName, which is mandatory. |
| 2534 | * Since next parameters are optional, the delimiter may be comma ',' |
| 2535 | * or closing parenthesis ')' |
| 2536 | */ |
| 2537 | beg = end + 1; |
| 2538 | end = strchr(beg, ','); |
| 2539 | if (end == NULL) |
| 2540 | end = strchr(beg, ')'); |
| 2541 | if (end == NULL) |
| 2542 | goto do_resolve_parse_error; |
| 2543 | rule->arg.dns.resolvers_id = my_strndup(beg, end - beg); |
| 2544 | if (rule->arg.dns.resolvers_id == NULL) |
| 2545 | goto do_resolve_parse_error; |
| 2546 | |
| 2547 | |
Christopher Faulet | a416843 | 2020-01-24 18:08:42 +0100 | [diff] [blame] | 2548 | rule->arg.dns.dns_opts = calloc(1, sizeof(*rule->arg.dns.dns_opts)); |
| 2549 | if (rule->arg.dns.dns_opts == NULL) |
| 2550 | goto do_resolve_parse_error; |
| 2551 | |
Baptiste Assmann | 333939c | 2019-01-21 08:34:50 +0100 | [diff] [blame] | 2552 | /* Default priority is ipv6 */ |
Christopher Faulet | a416843 | 2020-01-24 18:08:42 +0100 | [diff] [blame] | 2553 | rule->arg.dns.dns_opts->family_prio = AF_INET6; |
Baptiste Assmann | 333939c | 2019-01-21 08:34:50 +0100 | [diff] [blame] | 2554 | |
| 2555 | /* optional arguments accepted for now: |
| 2556 | * ipv4 or ipv6 |
| 2557 | */ |
| 2558 | while (*end != ')') { |
| 2559 | beg = end + 1; |
| 2560 | end = strchr(beg, ','); |
| 2561 | if (end == NULL) |
| 2562 | end = strchr(beg, ')'); |
| 2563 | if (end == NULL) |
| 2564 | goto do_resolve_parse_error; |
| 2565 | |
| 2566 | if (strncmp(beg, "ipv4", end - beg) == 0) { |
Christopher Faulet | a416843 | 2020-01-24 18:08:42 +0100 | [diff] [blame] | 2567 | rule->arg.dns.dns_opts->family_prio = AF_INET; |
Baptiste Assmann | 333939c | 2019-01-21 08:34:50 +0100 | [diff] [blame] | 2568 | } |
| 2569 | else if (strncmp(beg, "ipv6", end - beg) == 0) { |
Christopher Faulet | a416843 | 2020-01-24 18:08:42 +0100 | [diff] [blame] | 2570 | rule->arg.dns.dns_opts->family_prio = AF_INET6; |
Baptiste Assmann | 333939c | 2019-01-21 08:34:50 +0100 | [diff] [blame] | 2571 | } |
| 2572 | else { |
| 2573 | goto do_resolve_parse_error; |
| 2574 | } |
| 2575 | } |
| 2576 | |
| 2577 | cur_arg = cur_arg + 1; |
| 2578 | |
Willy Tarreau | e3b57bf | 2020-02-14 16:50:14 +0100 | [diff] [blame] | 2579 | expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args, NULL); |
Baptiste Assmann | 333939c | 2019-01-21 08:34:50 +0100 | [diff] [blame] | 2580 | if (!expr) |
| 2581 | goto do_resolve_parse_error; |
| 2582 | |
| 2583 | |
| 2584 | where = 0; |
| 2585 | if (px->cap & PR_CAP_FE) |
| 2586 | where |= SMP_VAL_FE_HRQ_HDR; |
| 2587 | if (px->cap & PR_CAP_BE) |
| 2588 | where |= SMP_VAL_BE_HRQ_HDR; |
| 2589 | |
| 2590 | if (!(expr->fetch->val & where)) { |
| 2591 | memprintf(err, |
| 2592 | "fetch method '%s' extracts information from '%s', none of which is available here", |
| 2593 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
| 2594 | free(expr); |
| 2595 | return ACT_RET_PRS_ERR; |
| 2596 | } |
| 2597 | rule->arg.dns.expr = expr; |
| 2598 | rule->action = ACT_CUSTOM; |
| 2599 | rule->action_ptr = dns_action_do_resolve; |
| 2600 | *orig_arg = cur_arg; |
| 2601 | |
| 2602 | rule->check_ptr = check_action_do_resolve; |
Christopher Faulet | 3b2bb63 | 2020-01-24 18:12:58 +0100 | [diff] [blame] | 2603 | rule->release_ptr = release_dns_action; |
Baptiste Assmann | 333939c | 2019-01-21 08:34:50 +0100 | [diff] [blame] | 2604 | |
| 2605 | return ACT_RET_PRS_OK; |
| 2606 | |
| 2607 | do_resolve_parse_error: |
| 2608 | free(rule->arg.dns.varname); rule->arg.dns.varname = NULL; |
| 2609 | free(rule->arg.dns.resolvers_id); rule->arg.dns.resolvers_id = NULL; |
| 2610 | memprintf(err, "Can't parse '%s'. Expects 'do-resolve(<varname>,<resolvers>[,<options>]) <expr>'. Available options are 'ipv4' and 'ipv6'", |
| 2611 | args[cur_arg]); |
| 2612 | return ACT_RET_PRS_ERR; |
| 2613 | } |
| 2614 | |
| 2615 | static struct action_kw_list http_req_kws = { { }, { |
| 2616 | { "do-resolve", dns_parse_do_resolve, 1 }, |
| 2617 | { /* END */ } |
| 2618 | }}; |
| 2619 | |
| 2620 | INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_kws); |
| 2621 | |
| 2622 | static struct action_kw_list tcp_req_cont_actions = {ILH, { |
| 2623 | { "do-resolve", dns_parse_do_resolve, 1 }, |
| 2624 | { /* END */ } |
| 2625 | }}; |
| 2626 | |
| 2627 | INITCALL1(STG_REGISTER, tcp_req_cont_keywords_register, &tcp_req_cont_actions); |
| 2628 | |
| 2629 | /* Check an "http-request do-resolve" action. |
| 2630 | * |
| 2631 | * The function returns 1 in success case, otherwise, it returns 0 and err is |
| 2632 | * filled. |
| 2633 | */ |
| 2634 | int check_action_do_resolve(struct act_rule *rule, struct proxy *px, char **err) |
| 2635 | { |
| 2636 | struct dns_resolvers *resolvers = NULL; |
| 2637 | |
| 2638 | if (rule->arg.dns.resolvers_id == NULL) { |
| 2639 | memprintf(err,"Proxy '%s': %s", px->id, "do-resolve action without resolvers"); |
| 2640 | return 0; |
| 2641 | } |
| 2642 | |
| 2643 | resolvers = find_resolvers_by_id(rule->arg.dns.resolvers_id); |
| 2644 | if (resolvers == NULL) { |
| 2645 | memprintf(err,"Can't find resolvers section '%s' for do-resolve action", rule->arg.dns.resolvers_id); |
| 2646 | return 0; |
| 2647 | } |
| 2648 | rule->arg.dns.resolvers = resolvers; |
| 2649 | |
| 2650 | return 1; |
| 2651 | } |
| 2652 | |
Willy Tarreau | 172f5ce | 2018-11-26 11:21:50 +0100 | [diff] [blame] | 2653 | REGISTER_POST_DEINIT(dns_deinit); |
Willy Tarreau | e655251 | 2018-11-26 11:33:13 +0100 | [diff] [blame] | 2654 | REGISTER_CONFIG_POSTPARSER("dns runtime resolver", dns_finalize_config); |