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