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