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