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