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> |
| 24 | |
| 25 | #include <types/global.h> |
| 26 | #include <types/dns.h> |
| 27 | #include <types/proto_udp.h> |
| 28 | |
| 29 | #include <proto/checks.h> |
| 30 | #include <proto/dns.h> |
| 31 | #include <proto/fd.h> |
| 32 | #include <proto/log.h> |
| 33 | #include <proto/server.h> |
| 34 | #include <proto/task.h> |
| 35 | #include <proto/proto_udp.h> |
| 36 | |
| 37 | struct list dns_resolvers = LIST_HEAD_INIT(dns_resolvers); |
| 38 | struct dns_resolution *resolution = NULL; |
| 39 | |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 40 | /* |
| 41 | * pre-allocated memory for maximum record names in a DNS response |
| 42 | * Each name is DNS_MAX_NAME_SIZE, we add 1 for the NULL character |
| 43 | * |
| 44 | * WARNING: this is not thread safe... |
| 45 | */ |
| 46 | struct dns_response_packet dns_response; |
| 47 | struct chunk dns_trash = { }; |
| 48 | struct dns_query_item dns_query_records[DNS_MAX_QUERY_RECORDS]; |
| 49 | struct dns_answer_item dns_answer_records[DNS_MAX_ANSWER_RECORDS]; |
| 50 | |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 51 | static int64_t dns_query_id_seed; /* random seed */ |
| 52 | |
| 53 | /* proto_udp callback functions for a DNS resolution */ |
| 54 | struct dgram_data_cb resolve_dgram_cb = { |
| 55 | .recv = dns_resolve_recv, |
| 56 | .send = dns_resolve_send, |
| 57 | }; |
| 58 | |
| 59 | #if DEBUG |
| 60 | /* |
| 61 | * go through the resolutions associated to a resolvers section and print the ID and hostname in |
| 62 | * domain name format |
| 63 | * should be used for debug purpose only |
| 64 | */ |
| 65 | void dns_print_current_resolutions(struct dns_resolvers *resolvers) |
| 66 | { |
| 67 | list_for_each_entry(resolution, &resolvers->curr_resolution, list) { |
| 68 | printf(" resolution %d for %s\n", resolution->query_id, resolution->hostname_dn); |
| 69 | } |
| 70 | } |
| 71 | #endif |
| 72 | |
| 73 | /* |
| 74 | * check if there is more than 1 resolution in the resolver's resolution list |
| 75 | * return value: |
| 76 | * 0: empty list |
| 77 | * 1: exactly one entry in the list |
| 78 | * 2: more than one entry in the list |
| 79 | */ |
| 80 | int dns_check_resolution_queue(struct dns_resolvers *resolvers) |
| 81 | { |
| 82 | |
| 83 | if (LIST_ISEMPTY(&resolvers->curr_resolution)) |
| 84 | return 0; |
| 85 | |
| 86 | if ((resolvers->curr_resolution.n) && (resolvers->curr_resolution.n == resolvers->curr_resolution.p)) |
| 87 | return 1; |
| 88 | |
| 89 | if (! ((resolvers->curr_resolution.n == resolvers->curr_resolution.p) |
| 90 | && (&resolvers->curr_resolution != resolvers->curr_resolution.n))) |
| 91 | return 2; |
| 92 | |
| 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | /* |
| 97 | * reset all parameters of a DNS resolution to 0 (or equivalent) |
| 98 | * and clean it up from all associated lists (resolution->qid and resolution->list) |
| 99 | */ |
| 100 | void dns_reset_resolution(struct dns_resolution *resolution) |
| 101 | { |
| 102 | /* update resolution status */ |
| 103 | resolution->step = RSLV_STEP_NONE; |
| 104 | |
| 105 | resolution->try = 0; |
| 106 | resolution->try_cname = 0; |
| 107 | resolution->last_resolution = now_ms; |
| 108 | resolution->nb_responses = 0; |
| 109 | |
| 110 | /* clean up query id */ |
| 111 | eb32_delete(&resolution->qid); |
| 112 | resolution->query_id = 0; |
| 113 | resolution->qid.key = 0; |
| 114 | |
| 115 | /* default values */ |
Thierry Fournier | ada3484 | 2016-02-17 21:25:09 +0100 | [diff] [blame] | 116 | if (resolution->opts->family_prio == AF_INET) { |
Andrew Hayworth | e6a4a32 | 2015-10-19 22:29:51 +0000 | [diff] [blame] | 117 | resolution->query_type = DNS_RTYPE_A; |
| 118 | } else { |
| 119 | resolution->query_type = DNS_RTYPE_AAAA; |
| 120 | } |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 121 | |
| 122 | /* the second resolution in the queue becomes the first one */ |
| 123 | LIST_DEL(&resolution->list); |
| 124 | } |
| 125 | |
| 126 | /* |
| 127 | * function called when a network IO is generated on a name server socket for an incoming packet |
| 128 | * It performs the following actions: |
| 129 | * - check if the packet requires processing (not outdated resolution) |
| 130 | * - ensure the DNS packet received is valid and call requester's callback |
| 131 | * - call requester's error callback if invalid response |
Baptiste Assmann | 3cf7f98 | 2016-04-17 22:43:26 +0200 | [diff] [blame] | 132 | * - check the dn_name in the packet against the one sent |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 133 | */ |
| 134 | void dns_resolve_recv(struct dgram_conn *dgram) |
| 135 | { |
| 136 | struct dns_nameserver *nameserver; |
| 137 | struct dns_resolvers *resolvers; |
| 138 | struct dns_resolution *resolution; |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 139 | struct dns_query_item *query; |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 140 | unsigned char buf[DNS_MAX_UDP_MESSAGE + 1]; |
| 141 | unsigned char *bufend; |
| 142 | int fd, buflen, ret; |
| 143 | unsigned short query_id; |
| 144 | struct eb32_node *eb; |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 145 | struct dns_response_packet *dns_p = &dns_response; |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 146 | |
| 147 | fd = dgram->t.sock.fd; |
| 148 | |
| 149 | /* check if ready for reading */ |
| 150 | if (!fd_recv_ready(fd)) |
| 151 | return; |
| 152 | |
| 153 | /* no need to go further if we can't retrieve the nameserver */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 154 | if ((nameserver = dgram->owner) == NULL) |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 155 | return; |
| 156 | |
| 157 | resolvers = nameserver->resolvers; |
| 158 | |
| 159 | /* process all pending input messages */ |
| 160 | while (1) { |
| 161 | /* read message received */ |
| 162 | memset(buf, '\0', DNS_MAX_UDP_MESSAGE + 1); |
| 163 | if ((buflen = recv(fd, (char*)buf , DNS_MAX_UDP_MESSAGE, 0)) < 0) { |
| 164 | /* FIXME : for now we consider EAGAIN only */ |
| 165 | fd_cant_recv(fd); |
| 166 | break; |
| 167 | } |
| 168 | |
| 169 | /* message too big */ |
| 170 | if (buflen > DNS_MAX_UDP_MESSAGE) { |
| 171 | nameserver->counters.too_big += 1; |
| 172 | continue; |
| 173 | } |
| 174 | |
| 175 | /* initializing variables */ |
| 176 | bufend = buf + buflen; /* pointer to mark the end of the buffer */ |
| 177 | |
| 178 | /* read the query id from the packet (16 bits) */ |
| 179 | if (buf + 2 > bufend) { |
| 180 | nameserver->counters.invalid += 1; |
| 181 | continue; |
| 182 | } |
| 183 | query_id = dns_response_get_query_id(buf); |
| 184 | |
| 185 | /* search the query_id in the pending resolution tree */ |
Baptiste Assmann | 01daef3 | 2015-09-02 22:05:24 +0200 | [diff] [blame] | 186 | eb = eb32_lookup(&resolvers->query_ids, query_id); |
| 187 | if (eb == NULL) { |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 188 | /* unknown query id means an outdated response and can be safely ignored */ |
| 189 | nameserver->counters.outdated += 1; |
| 190 | continue; |
| 191 | } |
| 192 | |
| 193 | /* known query id means a resolution in prgress */ |
| 194 | resolution = eb32_entry(eb, struct dns_resolution, qid); |
| 195 | |
| 196 | if (!resolution) { |
| 197 | nameserver->counters.outdated += 1; |
| 198 | continue; |
| 199 | } |
| 200 | |
| 201 | /* number of responses received */ |
| 202 | resolution->nb_responses += 1; |
| 203 | |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 204 | ret = dns_validate_dns_response(buf, bufend, dns_p); |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 205 | |
| 206 | /* treat only errors */ |
| 207 | switch (ret) { |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 208 | case DNS_RESP_QUERY_COUNT_ERROR: |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 209 | case DNS_RESP_INVALID: |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 210 | nameserver->counters.invalid += 1; |
| 211 | resolution->requester_error_cb(resolution, DNS_RESP_INVALID); |
| 212 | continue; |
| 213 | |
| 214 | case DNS_RESP_ERROR: |
| 215 | nameserver->counters.other += 1; |
| 216 | resolution->requester_error_cb(resolution, DNS_RESP_ERROR); |
| 217 | continue; |
| 218 | |
| 219 | case DNS_RESP_ANCOUNT_ZERO: |
| 220 | nameserver->counters.any_err += 1; |
| 221 | resolution->requester_error_cb(resolution, DNS_RESP_ANCOUNT_ZERO); |
| 222 | continue; |
| 223 | |
| 224 | case DNS_RESP_NX_DOMAIN: |
| 225 | nameserver->counters.nx += 1; |
| 226 | resolution->requester_error_cb(resolution, DNS_RESP_NX_DOMAIN); |
| 227 | continue; |
| 228 | |
| 229 | case DNS_RESP_REFUSED: |
| 230 | nameserver->counters.refused += 1; |
| 231 | resolution->requester_error_cb(resolution, DNS_RESP_REFUSED); |
| 232 | continue; |
| 233 | |
| 234 | case DNS_RESP_CNAME_ERROR: |
| 235 | nameserver->counters.cname_error += 1; |
| 236 | resolution->requester_error_cb(resolution, DNS_RESP_CNAME_ERROR); |
| 237 | continue; |
| 238 | |
Baptiste Assmann | 0df5d96 | 2015-09-02 21:58:32 +0200 | [diff] [blame] | 239 | case DNS_RESP_TRUNCATED: |
| 240 | nameserver->counters.truncated += 1; |
| 241 | resolution->requester_error_cb(resolution, DNS_RESP_TRUNCATED); |
| 242 | continue; |
Baptiste Assmann | 96972bc | 2015-09-09 00:46:58 +0200 | [diff] [blame] | 243 | |
| 244 | case DNS_RESP_NO_EXPECTED_RECORD: |
| 245 | nameserver->counters.other += 1; |
| 246 | resolution->requester_error_cb(resolution, DNS_RESP_NO_EXPECTED_RECORD); |
| 247 | continue; |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 248 | } |
| 249 | |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 250 | /* Now let's check the query's dname corresponds to the one we sent. |
| 251 | * We can check only the first query of the list. We send one query at a time |
| 252 | * so we get one query in the response */ |
| 253 | query = LIST_NEXT(&dns_p->query_list, struct dns_query_item *, list); |
| 254 | if (query && memcmp(query->name, resolution->hostname_dn, resolution->hostname_dn_len) != 0) { |
| 255 | nameserver->counters.other += 1; |
| 256 | resolution->requester_error_cb(resolution, DNS_RESP_WRONG_NAME); |
| 257 | continue; |
| 258 | } |
| 259 | |
Baptiste Assmann | 37bb372 | 2015-08-07 10:18:32 +0200 | [diff] [blame] | 260 | nameserver->counters.valid += 1; |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 261 | resolution->requester_cb(resolution, nameserver, dns_p); |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 262 | } |
| 263 | } |
| 264 | |
| 265 | /* |
| 266 | * function called when a resolvers network socket is ready to send data |
| 267 | * It performs the following actions: |
| 268 | */ |
| 269 | void dns_resolve_send(struct dgram_conn *dgram) |
| 270 | { |
| 271 | int fd; |
| 272 | struct dns_nameserver *nameserver; |
| 273 | struct dns_resolvers *resolvers; |
| 274 | struct dns_resolution *resolution; |
| 275 | |
| 276 | fd = dgram->t.sock.fd; |
| 277 | |
| 278 | /* check if ready for sending */ |
| 279 | if (!fd_send_ready(fd)) |
| 280 | return; |
| 281 | |
| 282 | /* we don't want/need to be waked up any more for sending */ |
| 283 | fd_stop_send(fd); |
| 284 | |
| 285 | /* no need to go further if we can't retrieve the nameserver */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 286 | if ((nameserver = dgram->owner) == NULL) |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 287 | return; |
| 288 | |
| 289 | resolvers = nameserver->resolvers; |
| 290 | resolution = LIST_NEXT(&resolvers->curr_resolution, struct dns_resolution *, list); |
| 291 | |
| 292 | dns_send_query(resolution); |
| 293 | dns_update_resolvers_timeout(resolvers); |
| 294 | } |
| 295 | |
| 296 | /* |
| 297 | * forge and send a DNS query to resolvers associated to a resolution |
| 298 | * It performs the following actions: |
| 299 | * returns: |
| 300 | * 0 in case of error or safe ignorance |
| 301 | * 1 if no error |
| 302 | */ |
| 303 | int dns_send_query(struct dns_resolution *resolution) |
| 304 | { |
| 305 | struct dns_resolvers *resolvers; |
| 306 | struct dns_nameserver *nameserver; |
Erwan Velu | 5457eb4 | 2015-10-15 15:07:26 +0200 | [diff] [blame] | 307 | int ret, bufsize, fd; |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 308 | |
| 309 | resolvers = resolution->resolvers; |
| 310 | |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 311 | bufsize = dns_build_query(resolution->query_id, resolution->query_type, resolution->hostname_dn, |
| 312 | resolution->hostname_dn_len, trash.str, trash.size); |
| 313 | |
| 314 | if (bufsize == -1) |
| 315 | return 0; |
| 316 | |
| 317 | list_for_each_entry(nameserver, &resolvers->nameserver_list, list) { |
| 318 | fd = nameserver->dgram->t.sock.fd; |
| 319 | errno = 0; |
| 320 | |
| 321 | ret = send(fd, trash.str, bufsize, 0); |
| 322 | |
| 323 | if (ret > 0) |
| 324 | nameserver->counters.sent += 1; |
| 325 | |
| 326 | if (ret == 0 || errno == EAGAIN) { |
| 327 | /* nothing written, let's update the poller that we wanted to send |
| 328 | * but we were not able to */ |
| 329 | fd_want_send(fd); |
| 330 | fd_cant_send(fd); |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | /* update resolution */ |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 335 | resolution->nb_responses = 0; |
| 336 | resolution->last_sent_packet = now_ms; |
| 337 | |
| 338 | return 1; |
| 339 | } |
| 340 | |
| 341 | /* |
| 342 | * update a resolvers' task timeout for next wake up |
| 343 | */ |
| 344 | void dns_update_resolvers_timeout(struct dns_resolvers *resolvers) |
| 345 | { |
| 346 | struct dns_resolution *resolution; |
| 347 | |
| 348 | if (LIST_ISEMPTY(&resolvers->curr_resolution)) { |
| 349 | /* no more resolution pending, so no wakeup anymore */ |
| 350 | resolvers->t->expire = TICK_ETERNITY; |
| 351 | } |
| 352 | else { |
| 353 | resolution = LIST_NEXT(&resolvers->curr_resolution, struct dns_resolution *, list); |
| 354 | resolvers->t->expire = tick_add(resolution->last_sent_packet, resolvers->timeout.retry); |
| 355 | } |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | /* |
| 359 | * Analyse, re-build and copy the name <name> from the DNS response packet <buffer>. |
| 360 | * <name> must point to the 'data_len' information or pointer 'c0' for compressed data. |
| 361 | * The result is copied into <dest>, ensuring we don't overflow using <dest_len> |
| 362 | * Returns the number of bytes the caller can move forward. If 0 it means an error occured |
| 363 | * while parsing the name. |
| 364 | * <offset> is the number of bytes the caller could move forward. |
| 365 | */ |
| 366 | int dns_read_name(unsigned char *buffer, unsigned char *bufend, unsigned char *name, char *destination, int dest_len, int *offset) |
| 367 | { |
| 368 | int nb_bytes = 0, n = 0; |
| 369 | int label_len; |
| 370 | unsigned char *reader = name; |
| 371 | char *dest = destination; |
| 372 | |
| 373 | while (1) { |
| 374 | /* name compression is in use */ |
| 375 | if ((*reader & 0xc0) == 0xc0) { |
| 376 | /* a pointer must point BEFORE current position */ |
| 377 | if ((buffer + reader[1]) > reader) { |
| 378 | goto out_error; |
| 379 | } |
| 380 | |
| 381 | n = dns_read_name(buffer, bufend, buffer + reader[1], dest, dest_len - nb_bytes, offset); |
| 382 | if (n == 0) |
| 383 | goto out_error; |
| 384 | |
| 385 | dest += n; |
| 386 | nb_bytes += n; |
| 387 | goto out; |
| 388 | } |
| 389 | |
| 390 | label_len = *reader; |
| 391 | if (label_len == 0) |
| 392 | goto out; |
| 393 | /* Check if: |
| 394 | * - we won't read outside the buffer |
| 395 | * - there is enough place in the destination |
| 396 | */ |
| 397 | if ((reader + label_len >= bufend) || (nb_bytes + label_len >= dest_len)) |
| 398 | goto out_error; |
| 399 | |
| 400 | /* +1 to take label len + label string */ |
| 401 | label_len += 1; |
| 402 | |
| 403 | memcpy(dest, reader, label_len); |
| 404 | |
| 405 | dest += label_len; |
| 406 | nb_bytes += label_len; |
| 407 | reader += label_len; |
| 408 | } |
| 409 | |
| 410 | out: |
| 411 | /* offset computation: |
| 412 | * parse from <name> until finding either NULL or a pointer "c0xx" |
| 413 | */ |
| 414 | reader = name; |
| 415 | *offset = 0; |
| 416 | while (reader < bufend) { |
| 417 | if ((reader[0] & 0xc0) == 0xc0) { |
| 418 | *offset += 2; |
| 419 | break; |
| 420 | } |
| 421 | else if (*reader == 0) { |
| 422 | *offset += 1; |
| 423 | break; |
| 424 | } |
| 425 | *offset += 1; |
| 426 | ++reader; |
| 427 | } |
| 428 | |
| 429 | return nb_bytes; |
| 430 | |
| 431 | out_error: |
| 432 | return 0; |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | /* |
| 436 | * Function to validate that the buffer DNS response provided in <resp> and |
| 437 | * finishing before <bufend> is valid from a DNS protocol point of view. |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 438 | * |
| 439 | * The result is stored in the structured pointed by <dns_p>. |
| 440 | * It's up to the caller to allocate memory for <dns_p>. |
| 441 | * |
| 442 | * This function returns one of the DNS_RESP_* code to indicate the type of |
| 443 | * error found. |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 444 | */ |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 445 | int dns_validate_dns_response(unsigned char *resp, unsigned char *bufend, struct dns_response_packet *dns_p) |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 446 | { |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 447 | unsigned char *reader; |
| 448 | char *previous_dname, tmpname[DNS_MAX_NAME_SIZE]; |
| 449 | int len, flags, offset, ret; |
| 450 | int dns_query_record_id, dns_answer_record_id; |
| 451 | struct dns_query_item *dns_query; |
| 452 | struct dns_answer_item *dns_answer_record; |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 453 | |
| 454 | reader = resp; |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 455 | len = 0; |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 456 | previous_dname = NULL; |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 457 | |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 458 | /* initialization of local buffer */ |
| 459 | memset(dns_p, '\0', sizeof(struct dns_response_packet)); |
| 460 | chunk_reset(&dns_trash); |
| 461 | |
| 462 | /* query id */ |
| 463 | if (reader + 2 >= bufend) |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 464 | return DNS_RESP_INVALID; |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 465 | dns_p->header.id = reader[0] * 256 + reader[1]; |
| 466 | reader += 2; |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 467 | |
| 468 | /* |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 469 | * flags and rcode are stored over 2 bytes |
Baptiste Assmann | 3440f0d | 2015-09-02 22:08:38 +0200 | [diff] [blame] | 470 | * First byte contains: |
| 471 | * - response flag (1 bit) |
| 472 | * - opcode (4 bits) |
| 473 | * - authoritative (1 bit) |
| 474 | * - truncated (1 bit) |
| 475 | * - recursion desired (1 bit) |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 476 | */ |
Baptiste Assmann | 3440f0d | 2015-09-02 22:08:38 +0200 | [diff] [blame] | 477 | if (reader + 2 >= bufend) |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 478 | return DNS_RESP_INVALID; |
| 479 | |
Baptiste Assmann | 3440f0d | 2015-09-02 22:08:38 +0200 | [diff] [blame] | 480 | flags = reader[0] * 256 + reader[1]; |
| 481 | |
| 482 | if (flags & DNS_FLAG_TRUNCATED) |
| 483 | return DNS_RESP_TRUNCATED; |
| 484 | |
| 485 | if ((flags & DNS_FLAG_REPLYCODE) != DNS_RCODE_NO_ERROR) { |
| 486 | if ((flags & DNS_FLAG_REPLYCODE) == DNS_RCODE_NX_DOMAIN) |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 487 | return DNS_RESP_NX_DOMAIN; |
Baptiste Assmann | 3440f0d | 2015-09-02 22:08:38 +0200 | [diff] [blame] | 488 | else if ((flags & DNS_FLAG_REPLYCODE) == DNS_RCODE_REFUSED) |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 489 | return DNS_RESP_REFUSED; |
| 490 | |
| 491 | return DNS_RESP_ERROR; |
| 492 | } |
| 493 | |
Baptiste Assmann | 3440f0d | 2015-09-02 22:08:38 +0200 | [diff] [blame] | 494 | /* move forward 2 bytes for flags */ |
| 495 | reader += 2; |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 496 | |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 497 | /* 2 bytes for question count */ |
| 498 | if (reader + 2 >= bufend) |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 499 | return DNS_RESP_INVALID; |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 500 | dns_p->header.qdcount = reader[0] * 256 + reader[1]; |
| 501 | /* (for now) we send one query only, so we expect only one in the response too */ |
| 502 | if (dns_p->header.qdcount != 1) |
| 503 | return DNS_RESP_QUERY_COUNT_ERROR; |
| 504 | if (dns_p->header.qdcount > DNS_MAX_QUERY_RECORDS) |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 505 | return DNS_RESP_INVALID; |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 506 | reader += 2; |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 507 | |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 508 | /* 2 bytes for answer count */ |
| 509 | if (reader + 2 >= bufend) |
| 510 | return DNS_RESP_INVALID; |
| 511 | dns_p->header.ancount = reader[0] * 256 + reader[1]; |
| 512 | if (dns_p->header.ancount == 0) |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 513 | return DNS_RESP_ANCOUNT_ZERO; |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 514 | /* check if too many records are announced */ |
| 515 | if (dns_p->header.ancount > DNS_MAX_ANSWER_RECORDS) |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 516 | return DNS_RESP_INVALID; |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 517 | reader += 2; |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 518 | |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 519 | /* 2 bytes authority count */ |
| 520 | if (reader + 2 >= bufend) |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 521 | return DNS_RESP_INVALID; |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 522 | dns_p->header.nscount = reader[0] * 256 + reader[1]; |
| 523 | reader += 2; |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 524 | |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 525 | /* 2 bytes additional count */ |
| 526 | if (reader + 2 >= bufend) |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 527 | return DNS_RESP_INVALID; |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 528 | dns_p->header.arcount = reader[0] * 256 + reader[1]; |
| 529 | reader += 2; |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 530 | |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 531 | /* parsing dns queries */ |
| 532 | LIST_INIT(&dns_p->query_list); |
| 533 | for (dns_query_record_id = 0; dns_query_record_id < dns_p->header.qdcount; dns_query_record_id++) { |
| 534 | /* use next pre-allocated dns_query_item after ensuring there is |
| 535 | * still one available. |
| 536 | * It's then added to our packet query list. |
| 537 | */ |
| 538 | if (dns_query_record_id > DNS_MAX_QUERY_RECORDS) |
| 539 | return DNS_RESP_INVALID; |
| 540 | dns_query = &dns_query_records[dns_query_record_id]; |
| 541 | LIST_ADDQ(&dns_p->query_list, &dns_query->list); |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 542 | |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 543 | /* name is a NULL terminated string in our case, since we have |
| 544 | * one query per response and the first one can't be compressed |
| 545 | * (using the 0x0c format) |
| 546 | */ |
| 547 | offset = 0; |
| 548 | 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] | 549 | |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 550 | if (len == 0) |
| 551 | return DNS_RESP_INVALID; |
| 552 | |
| 553 | reader += offset; |
| 554 | previous_dname = dns_query->name; |
| 555 | |
| 556 | /* move forward 2 bytes for question type */ |
| 557 | if (reader + 2 >= bufend) |
| 558 | return DNS_RESP_INVALID; |
| 559 | dns_query->type = reader[0] * 256 + reader[1]; |
| 560 | reader += 2; |
| 561 | |
| 562 | /* move forward 2 bytes for question class */ |
| 563 | if (reader + 2 >= bufend) |
| 564 | return DNS_RESP_INVALID; |
| 565 | dns_query->class = reader[0] * 256 + reader[1]; |
| 566 | reader += 2; |
| 567 | } |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 568 | |
| 569 | /* now parsing response records */ |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 570 | LIST_INIT(&dns_p->answer_list); |
| 571 | for (dns_answer_record_id = 0; dns_answer_record_id < dns_p->header.ancount; dns_answer_record_id++) { |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 572 | if (reader >= bufend) |
| 573 | return DNS_RESP_INVALID; |
| 574 | |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 575 | /* pull next response record from the list, if still one available, then add it |
| 576 | * to the record list */ |
| 577 | if (dns_answer_record_id > DNS_MAX_ANSWER_RECORDS) |
| 578 | return DNS_RESP_INVALID; |
| 579 | dns_answer_record = &dns_answer_records[dns_answer_record_id]; |
| 580 | LIST_ADDQ(&dns_p->answer_list, &dns_answer_record->list); |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 581 | |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 582 | offset = 0; |
| 583 | 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] | 584 | |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 585 | if (len == 0) |
| 586 | return DNS_RESP_INVALID; |
| 587 | |
| 588 | /* check if the current record dname is valid. |
| 589 | * previous_dname points either to queried dname or last CNAME target |
| 590 | */ |
| 591 | if (memcmp(previous_dname, tmpname, len) != 0) { |
| 592 | if (dns_answer_record_id == 0) { |
| 593 | /* first record, means a mismatch issue between queried dname |
| 594 | * and dname found in the first record */ |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 595 | return DNS_RESP_INVALID; |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 596 | } else { |
| 597 | /* if not the first record, this means we have a CNAME resolution |
| 598 | * error */ |
| 599 | return DNS_RESP_CNAME_ERROR; |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 600 | } |
| 601 | |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 602 | } |
| 603 | |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 604 | dns_answer_record->name = chunk_newstr(&dns_trash); |
| 605 | if (dns_answer_record->name == NULL) |
| 606 | return DNS_RESP_INVALID; |
Baptiste Assmann | 5d681ba | 2015-10-15 15:23:28 +0200 | [diff] [blame] | 607 | |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 608 | ret = chunk_strncat(&dns_trash, tmpname, len); |
| 609 | if (ret == 0) |
| 610 | return DNS_RESP_INVALID; |
Baptiste Assmann | 2359ff1 | 2015-08-07 11:24:05 +0200 | [diff] [blame] | 611 | |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 612 | reader += offset; |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 613 | if (reader >= bufend) |
| 614 | return DNS_RESP_INVALID; |
| 615 | |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 616 | if (reader >= bufend) |
| 617 | return DNS_RESP_INVALID; |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 618 | |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 619 | /* 2 bytes for record type (A, AAAA, CNAME, etc...) */ |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 620 | if (reader + 2 > bufend) |
| 621 | return DNS_RESP_INVALID; |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 622 | dns_answer_record->type = reader[0] * 256 + reader[1]; |
| 623 | reader += 2; |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 624 | |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 625 | /* 2 bytes for class (2) */ |
| 626 | if (reader + 2 > bufend) |
| 627 | return DNS_RESP_INVALID; |
| 628 | dns_answer_record->class = reader[0] * 256 + reader[1]; |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 629 | reader += 2; |
| 630 | |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 631 | /* 4 bytes for ttl (4) */ |
| 632 | if (reader + 4 > bufend) |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 633 | return DNS_RESP_INVALID; |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 634 | dns_answer_record->ttl = reader[0] * 16777216 + reader[1] * 65536 |
| 635 | + reader[2] * 256 + reader[3]; |
| 636 | reader += 4; |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 637 | |
| 638 | /* now reading data len */ |
| 639 | if (reader + 2 > bufend) |
| 640 | return DNS_RESP_INVALID; |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 641 | dns_answer_record->data_len = reader[0] * 256 + reader[1]; |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 642 | |
| 643 | /* move forward 2 bytes for data len */ |
| 644 | reader += 2; |
| 645 | |
| 646 | /* analyzing record content */ |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 647 | switch (dns_answer_record->type) { |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 648 | case DNS_RTYPE_A: |
| 649 | /* ipv4 is stored on 4 bytes */ |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 650 | if (dns_answer_record->data_len != 4) |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 651 | return DNS_RESP_INVALID; |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 652 | dns_answer_record->address.sa_family = AF_INET; |
| 653 | memcpy(&(((struct sockaddr_in *)&dns_answer_record->address)->sin_addr), |
| 654 | reader, dns_answer_record->data_len); |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 655 | break; |
| 656 | |
| 657 | case DNS_RTYPE_CNAME: |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 658 | /* check if this is the last record and update the caller about the status: |
| 659 | * no IP could be found and last record was a CNAME. Could be triggered |
| 660 | * by a wrong query type |
| 661 | * |
| 662 | * + 1 because dns_answer_record_id starts at 0 while number of answers |
| 663 | * is an integer and starts at 1. |
| 664 | */ |
| 665 | if (dns_answer_record_id + 1 == dns_p->header.ancount) |
| 666 | return DNS_RESP_CNAME_ERROR; |
| 667 | |
| 668 | offset = 0; |
| 669 | len = dns_read_name(resp, bufend, reader, tmpname, DNS_MAX_NAME_SIZE, &offset); |
| 670 | |
| 671 | if (len == 0) |
| 672 | return DNS_RESP_INVALID; |
| 673 | |
| 674 | dns_answer_record->target = chunk_newstr(&dns_trash); |
| 675 | if (dns_answer_record->target == NULL) |
| 676 | return DNS_RESP_INVALID; |
| 677 | |
| 678 | ret = chunk_strncat(&dns_trash, tmpname, len); |
| 679 | if (ret == 0) |
| 680 | return DNS_RESP_INVALID; |
| 681 | |
| 682 | previous_dname = dns_answer_record->target; |
| 683 | |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 684 | break; |
| 685 | |
| 686 | case DNS_RTYPE_AAAA: |
| 687 | /* ipv6 is stored on 16 bytes */ |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 688 | if (dns_answer_record->data_len != 16) |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 689 | return DNS_RESP_INVALID; |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 690 | dns_answer_record->address.sa_family = AF_INET6; |
| 691 | memcpy(&(((struct sockaddr_in6 *)&dns_answer_record->address)->sin6_addr), |
| 692 | reader, dns_answer_record->data_len); |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 693 | break; |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 694 | |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 695 | } /* switch (record type) */ |
| 696 | |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 697 | /* move forward dns_answer_record->data_len for analyzing next record in the response */ |
| 698 | reader += dns_answer_record->data_len; |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 699 | } /* for i 0 to ancount */ |
| 700 | |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 701 | /* let's add a last \0 to close our last string */ |
| 702 | ret = chunk_strncat(&dns_trash, "\0", 1); |
| 703 | if (ret == 0) |
| 704 | return DNS_RESP_INVALID; |
Baptiste Assmann | 96972bc | 2015-09-09 00:46:58 +0200 | [diff] [blame] | 705 | |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 706 | return DNS_RESP_VALID; |
| 707 | } |
| 708 | |
| 709 | /* |
| 710 | * search dn_name resolution in resp. |
| 711 | * If existing IP not found, return the first IP matching family_priority, |
| 712 | * otherwise, first ip found |
| 713 | * The following tasks are the responsibility of the caller: |
Baptiste Assmann | 3cf7f98 | 2016-04-17 22:43:26 +0200 | [diff] [blame] | 714 | * - <dns_p> contains an error free DNS response |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 715 | * For both cases above, dns_validate_dns_response is required |
| 716 | * returns one of the DNS_UPD_* code |
| 717 | */ |
Thierry Fournier | ac88cfe | 2016-02-17 22:05:30 +0100 | [diff] [blame] | 718 | #define DNS_MAX_IP_REC 20 |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 719 | int dns_get_ip_from_response(struct dns_response_packet *dns_p, |
Thierry Fournier | ada3484 | 2016-02-17 21:25:09 +0100 | [diff] [blame] | 720 | struct dns_resolution *resol, void *currentip, |
| 721 | short currentip_sin_family, |
| 722 | void **newip, short *newip_sin_family) |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 723 | { |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 724 | struct dns_answer_item *record; |
Thierry Fournier | ada3484 | 2016-02-17 21:25:09 +0100 | [diff] [blame] | 725 | int family_priority; |
Baptiste Assmann | 3cf7f98 | 2016-04-17 22:43:26 +0200 | [diff] [blame] | 726 | int i, currentip_found; |
| 727 | unsigned char *newip4, *newip6; |
Thierry Fournier | ac88cfe | 2016-02-17 22:05:30 +0100 | [diff] [blame] | 728 | struct { |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 729 | void *ip; |
Thierry Fournier | ac88cfe | 2016-02-17 22:05:30 +0100 | [diff] [blame] | 730 | unsigned char type; |
| 731 | } rec[DNS_MAX_IP_REC]; |
| 732 | int currentip_sel; |
| 733 | int j; |
| 734 | int rec_nb = 0; |
| 735 | int score, max_score; |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 736 | |
Thierry Fournier | ada3484 | 2016-02-17 21:25:09 +0100 | [diff] [blame] | 737 | family_priority = resol->opts->family_prio; |
Baptiste Assmann | 3cf7f98 | 2016-04-17 22:43:26 +0200 | [diff] [blame] | 738 | *newip = newip4 = newip6 = NULL; |
| 739 | currentip_found = 0; |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 740 | *newip_sin_family = AF_UNSPEC; |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 741 | |
| 742 | /* now parsing response records */ |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 743 | list_for_each_entry(record, &dns_response.answer_list, list) { |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 744 | /* analyzing record content */ |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 745 | switch (record->type) { |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 746 | case DNS_RTYPE_A: |
Thierry Fournier | ac88cfe | 2016-02-17 22:05:30 +0100 | [diff] [blame] | 747 | /* Store IPv4, only if some room is avalaible. */ |
| 748 | if (rec_nb < DNS_MAX_IP_REC) { |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 749 | rec[rec_nb].ip = &(((struct sockaddr_in *)&record->address)->sin_addr); |
Thierry Fournier | ac88cfe | 2016-02-17 22:05:30 +0100 | [diff] [blame] | 750 | rec[rec_nb].type = AF_INET; |
| 751 | rec_nb++; |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 752 | } |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 753 | break; |
| 754 | |
Baptiste Assmann | 3cf7f98 | 2016-04-17 22:43:26 +0200 | [diff] [blame] | 755 | /* we're looking for IPs only. CNAME validation is done when |
| 756 | * parsing the response buffer for the first time */ |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 757 | case DNS_RTYPE_CNAME: |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 758 | break; |
| 759 | |
| 760 | case DNS_RTYPE_AAAA: |
Thierry Fournier | ac88cfe | 2016-02-17 22:05:30 +0100 | [diff] [blame] | 761 | /* Store IPv6, only if some room is avalaible. */ |
| 762 | if (rec_nb < DNS_MAX_IP_REC) { |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 763 | rec[rec_nb].ip = &(((struct sockaddr_in6 *)&record->address)->sin6_addr); |
Thierry Fournier | ac88cfe | 2016-02-17 22:05:30 +0100 | [diff] [blame] | 764 | rec[rec_nb].type = AF_INET6; |
| 765 | rec_nb++; |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 766 | } |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 767 | break; |
| 768 | |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 769 | } /* switch (record type) */ |
Baptiste Assmann | bcbd491 | 2016-04-18 19:42:57 +0200 | [diff] [blame] | 770 | } /* list for each record entries */ |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 771 | |
Thierry Fournier | ac88cfe | 2016-02-17 22:05:30 +0100 | [diff] [blame] | 772 | /* Select an IP regarding configuration preference. |
| 773 | * Top priority is the prefered network ip version, |
| 774 | * second priority is the prefered network. |
| 775 | * the last priority is the currently used IP, |
| 776 | * |
| 777 | * For these three priorities, a score is calculated. The |
| 778 | * weight are: |
| 779 | * 4 - prefered netwok ip version. |
| 780 | * 2 - prefered network. |
| 781 | * 1 - current ip. |
| 782 | * The result with the biggest score is returned. |
| 783 | */ |
| 784 | max_score = -1; |
| 785 | for (i = 0; i < rec_nb; i++) { |
| 786 | |
| 787 | score = 0; |
| 788 | |
| 789 | /* Check for prefered ip protocol. */ |
| 790 | if (rec[i].type == family_priority) |
| 791 | score += 4; |
| 792 | |
| 793 | /* Check for prefered network. */ |
| 794 | for (j = 0; j < resol->opts->pref_net_nb; j++) { |
| 795 | |
| 796 | /* Compare only the same adresses class. */ |
| 797 | if (resol->opts->pref_net[j].family != rec[i].type) |
| 798 | continue; |
| 799 | |
| 800 | if ((rec[i].type == AF_INET && |
Willy Tarreau | eec1d38 | 2016-07-13 11:59:39 +0200 | [diff] [blame] | 801 | in_net_ipv4(rec[i].ip, |
Thierry Fournier | ac88cfe | 2016-02-17 22:05:30 +0100 | [diff] [blame] | 802 | &resol->opts->pref_net[j].mask.in4, |
| 803 | &resol->opts->pref_net[j].addr.in4)) || |
| 804 | (rec[i].type == AF_INET6 && |
Willy Tarreau | eec1d38 | 2016-07-13 11:59:39 +0200 | [diff] [blame] | 805 | in_net_ipv6(rec[i].ip, |
Thierry Fournier | ac88cfe | 2016-02-17 22:05:30 +0100 | [diff] [blame] | 806 | &resol->opts->pref_net[j].mask.in6, |
| 807 | &resol->opts->pref_net[j].addr.in6))) { |
| 808 | score += 2; |
| 809 | break; |
| 810 | } |
| 811 | } |
| 812 | |
| 813 | /* Check for current ip matching. */ |
| 814 | if (rec[i].type == currentip_sin_family && |
| 815 | ((currentip_sin_family == AF_INET && |
Willy Tarreau | eec1d38 | 2016-07-13 11:59:39 +0200 | [diff] [blame] | 816 | memcmp(rec[i].ip, currentip, 4) == 0) || |
Thierry Fournier | ac88cfe | 2016-02-17 22:05:30 +0100 | [diff] [blame] | 817 | (currentip_sin_family == AF_INET6 && |
| 818 | memcmp(rec[i].ip, currentip, 16) == 0))) { |
| 819 | score += 1; |
| 820 | currentip_sel = 1; |
| 821 | } else |
| 822 | currentip_sel = 0; |
| 823 | |
| 824 | /* Keep the address if the score is better than the previous |
| 825 | * score. The maximum score is 7, if this value is reached, |
| 826 | * we break the parsing. Implicitly, this score is reached |
| 827 | * the ip selected is the current ip. |
| 828 | */ |
| 829 | if (score > max_score) { |
| 830 | if (rec[i].type == AF_INET) |
| 831 | newip4 = rec[i].ip; |
| 832 | else |
| 833 | newip6 = rec[i].ip; |
| 834 | currentip_found = currentip_sel; |
| 835 | if (score == 7) |
| 836 | return DNS_UPD_NO; |
| 837 | max_score = score; |
| 838 | } |
| 839 | } |
| 840 | |
Baptiste Assmann | 0453a1d | 2015-09-09 00:51:08 +0200 | [diff] [blame] | 841 | /* no IP found in the response */ |
| 842 | if (!newip4 && !newip6) { |
| 843 | return DNS_UPD_NO_IP_FOUND; |
| 844 | } |
| 845 | |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 846 | /* case when the caller looks first for an IPv4 address */ |
| 847 | if (family_priority == AF_INET) { |
| 848 | if (newip4) { |
| 849 | *newip = newip4; |
| 850 | *newip_sin_family = AF_INET; |
| 851 | if (currentip_found == 1) |
| 852 | return DNS_UPD_NO; |
| 853 | return DNS_UPD_SRVIP_NOT_FOUND; |
| 854 | } |
| 855 | else if (newip6) { |
| 856 | *newip = newip6; |
| 857 | *newip_sin_family = AF_INET6; |
| 858 | if (currentip_found == 1) |
| 859 | return DNS_UPD_NO; |
| 860 | return DNS_UPD_SRVIP_NOT_FOUND; |
| 861 | } |
| 862 | } |
| 863 | /* case when the caller looks first for an IPv6 address */ |
| 864 | else if (family_priority == AF_INET6) { |
| 865 | if (newip6) { |
| 866 | *newip = newip6; |
| 867 | *newip_sin_family = AF_INET6; |
| 868 | if (currentip_found == 1) |
| 869 | return DNS_UPD_NO; |
| 870 | return DNS_UPD_SRVIP_NOT_FOUND; |
| 871 | } |
| 872 | else if (newip4) { |
| 873 | *newip = newip4; |
| 874 | *newip_sin_family = AF_INET; |
| 875 | if (currentip_found == 1) |
| 876 | return DNS_UPD_NO; |
| 877 | return DNS_UPD_SRVIP_NOT_FOUND; |
| 878 | } |
| 879 | } |
| 880 | /* case when the caller have no preference (we prefer IPv6) */ |
| 881 | else if (family_priority == AF_UNSPEC) { |
| 882 | if (newip6) { |
| 883 | *newip = newip6; |
| 884 | *newip_sin_family = AF_INET6; |
| 885 | if (currentip_found == 1) |
| 886 | return DNS_UPD_NO; |
| 887 | return DNS_UPD_SRVIP_NOT_FOUND; |
| 888 | } |
| 889 | else if (newip4) { |
| 890 | *newip = newip4; |
| 891 | *newip_sin_family = AF_INET; |
| 892 | if (currentip_found == 1) |
| 893 | return DNS_UPD_NO; |
| 894 | return DNS_UPD_SRVIP_NOT_FOUND; |
| 895 | } |
| 896 | } |
| 897 | |
| 898 | /* no reason why we should change the server's IP address */ |
| 899 | return DNS_UPD_NO; |
| 900 | } |
| 901 | |
| 902 | /* |
| 903 | * returns the query id contained in a DNS response |
| 904 | */ |
Thiago Farina | b1af23e | 2016-01-20 23:46:34 +0100 | [diff] [blame] | 905 | unsigned short dns_response_get_query_id(unsigned char *resp) |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 906 | { |
| 907 | /* read the query id from the response */ |
| 908 | return resp[0] * 256 + resp[1]; |
| 909 | } |
| 910 | |
| 911 | /* |
| 912 | * used during haproxy's init phase |
| 913 | * parses resolvers sections and initializes: |
| 914 | * - task (time events) for each resolvers section |
| 915 | * - the datagram layer (network IO events) for each nameserver |
| 916 | * returns: |
| 917 | * 0 in case of error |
| 918 | * 1 when no error |
| 919 | */ |
| 920 | int dns_init_resolvers(void) |
| 921 | { |
| 922 | struct dns_resolvers *curr_resolvers; |
| 923 | struct dns_nameserver *curnameserver; |
| 924 | struct dgram_conn *dgram; |
| 925 | struct task *t; |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 926 | char *dns_trash_str; |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 927 | int fd; |
| 928 | |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 929 | dns_trash_str = malloc(global.tune.bufsize); |
| 930 | if (dns_trash_str == NULL) { |
Willy Tarreau | c3d8cd4 | 2016-10-01 09:20:32 +0200 | [diff] [blame^] | 931 | Alert("Starting resolvers: out of memory.\n"); |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 932 | return 0; |
| 933 | } |
| 934 | |
| 935 | /* allocate memory for the dns_trash buffer used to temporarily store |
| 936 | * the records of the received response */ |
| 937 | chunk_init(&dns_trash, dns_trash_str, global.tune.bufsize); |
| 938 | |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 939 | /* give a first random value to our dns query_id seed */ |
| 940 | dns_query_id_seed = random(); |
| 941 | |
| 942 | /* run through the resolvers section list */ |
| 943 | list_for_each_entry(curr_resolvers, &dns_resolvers, list) { |
| 944 | /* create the task associated to the resolvers section */ |
| 945 | if ((t = task_new()) == NULL) { |
| 946 | Alert("Starting [%s] resolvers: out of memory.\n", curr_resolvers->id); |
| 947 | return 0; |
| 948 | } |
| 949 | |
| 950 | /* update task's parameters */ |
| 951 | t->process = dns_process_resolve; |
| 952 | t->context = curr_resolvers; |
| 953 | t->expire = TICK_ETERNITY; |
| 954 | |
| 955 | curr_resolvers->t = t; |
| 956 | |
| 957 | list_for_each_entry(curnameserver, &curr_resolvers->nameserver_list, list) { |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 958 | if ((dgram = calloc(1, sizeof(*dgram))) == NULL) { |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 959 | Alert("Starting [%s/%s] nameserver: out of memory.\n", curr_resolvers->id, |
| 960 | curnameserver->id); |
| 961 | return 0; |
| 962 | } |
| 963 | /* update datagram's parameters */ |
| 964 | dgram->owner = (void *)curnameserver; |
| 965 | dgram->data = &resolve_dgram_cb; |
| 966 | |
| 967 | /* create network UDP socket for this nameserver */ |
| 968 | if ((fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) { |
| 969 | Alert("Starting [%s/%s] nameserver: can't create socket.\n", curr_resolvers->id, |
| 970 | curnameserver->id); |
| 971 | free(dgram); |
| 972 | dgram = NULL; |
| 973 | return 0; |
| 974 | } |
| 975 | |
| 976 | /* "connect" the UDP socket to the name server IP */ |
Baptiste Assmann | 8c62c47 | 2015-09-21 20:55:08 +0200 | [diff] [blame] | 977 | 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] | 978 | Alert("Starting [%s/%s] nameserver: can't connect socket.\n", curr_resolvers->id, |
| 979 | curnameserver->id); |
| 980 | close(fd); |
| 981 | free(dgram); |
| 982 | dgram = NULL; |
| 983 | return 0; |
| 984 | } |
| 985 | |
| 986 | /* make the socket non blocking */ |
| 987 | fcntl(fd, F_SETFL, O_NONBLOCK); |
| 988 | |
| 989 | /* add the fd in the fd list and update its parameters */ |
| 990 | fd_insert(fd); |
| 991 | fdtab[fd].owner = dgram; |
| 992 | fdtab[fd].iocb = dgram_fd_handler; |
| 993 | fd_want_recv(fd); |
| 994 | dgram->t.sock.fd = fd; |
| 995 | |
| 996 | /* update nameserver's datagram property */ |
| 997 | curnameserver->dgram = dgram; |
| 998 | |
| 999 | continue; |
| 1000 | } |
| 1001 | |
| 1002 | /* task can be queued */ |
| 1003 | task_queue(t); |
| 1004 | } |
| 1005 | |
| 1006 | return 1; |
| 1007 | } |
| 1008 | |
| 1009 | /* |
| 1010 | * Forge a DNS query. It needs the following information from the caller: |
| 1011 | * - <query_id>: the DNS query id corresponding to this query |
| 1012 | * - <query_type>: DNS_RTYPE_* request DNS record type (A, AAAA, ANY, etc...) |
| 1013 | * - <hostname_dn>: hostname in domain name format |
| 1014 | * - <hostname_dn_len>: length of <hostname_dn> |
| 1015 | * To store the query, the caller must pass a buffer <buf> and its size <bufsize> |
| 1016 | * |
| 1017 | * the DNS query is stored in <buf> |
| 1018 | * returns: |
| 1019 | * -1 if <buf> is too short |
| 1020 | */ |
| 1021 | int dns_build_query(int query_id, int query_type, char *hostname_dn, int hostname_dn_len, char *buf, int bufsize) |
| 1022 | { |
| 1023 | struct dns_header *dns; |
Vincent Bernat | 9b7125c | 2016-04-08 22:17:45 +0200 | [diff] [blame] | 1024 | struct dns_question qinfo; |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 1025 | char *ptr, *bufend; |
| 1026 | |
| 1027 | memset(buf, '\0', bufsize); |
| 1028 | ptr = buf; |
| 1029 | bufend = buf + bufsize; |
| 1030 | |
| 1031 | /* check if there is enough room for DNS headers */ |
| 1032 | if (ptr + sizeof(struct dns_header) >= bufend) |
| 1033 | return -1; |
| 1034 | |
| 1035 | /* set dns query headers */ |
| 1036 | dns = (struct dns_header *)ptr; |
| 1037 | dns->id = (unsigned short) htons(query_id); |
Nenad Merdanovic | 8ab7942 | 2016-07-13 14:03:43 +0200 | [diff] [blame] | 1038 | 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] | 1039 | dns->qdcount = htons(1); /* 1 question */ |
| 1040 | dns->ancount = 0; |
| 1041 | dns->nscount = 0; |
| 1042 | dns->arcount = 0; |
| 1043 | |
| 1044 | /* move forward ptr */ |
| 1045 | ptr += sizeof(struct dns_header); |
| 1046 | |
| 1047 | /* check if there is enough room for query hostname */ |
| 1048 | if ((ptr + hostname_dn_len) >= bufend) |
| 1049 | return -1; |
| 1050 | |
| 1051 | /* set up query hostname */ |
| 1052 | memcpy(ptr, hostname_dn, hostname_dn_len); |
| 1053 | ptr[hostname_dn_len + 1] = '\0'; |
| 1054 | |
| 1055 | /* move forward ptr */ |
| 1056 | ptr += (hostname_dn_len + 1); |
| 1057 | |
| 1058 | /* check if there is enough room for query hostname*/ |
| 1059 | if (ptr + sizeof(struct dns_question) >= bufend) |
| 1060 | return -1; |
| 1061 | |
| 1062 | /* set up query info (type and class) */ |
Vincent Bernat | 9b7125c | 2016-04-08 22:17:45 +0200 | [diff] [blame] | 1063 | qinfo.qtype = htons(query_type); |
| 1064 | qinfo.qclass = htons(DNS_RCLASS_IN); |
| 1065 | memcpy(ptr, &qinfo, sizeof(qinfo)); |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 1066 | |
| 1067 | ptr += sizeof(struct dns_question); |
| 1068 | |
| 1069 | return ptr - buf; |
| 1070 | } |
| 1071 | |
| 1072 | /* |
| 1073 | * turn a string into domain name label: |
| 1074 | * www.haproxy.org into 3www7haproxy3org |
| 1075 | * if dn memory is pre-allocated, you must provide its size in dn_len |
| 1076 | * if dn memory isn't allocated, dn_len must be set to 0. |
| 1077 | * In the second case, memory will be allocated. |
| 1078 | * in case of error, -1 is returned, otherwise, number of bytes copied in dn |
| 1079 | */ |
Willy Tarreau | 2100b49 | 2015-07-22 16:42:43 +0200 | [diff] [blame] | 1080 | 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] | 1081 | { |
| 1082 | char *c, *d; |
| 1083 | int i, offset; |
| 1084 | |
| 1085 | /* offset between string size and theorical dn size */ |
| 1086 | offset = 1; |
| 1087 | |
| 1088 | /* |
| 1089 | * first, get the size of the string turned into its domain name version |
| 1090 | * This function also validates the string respect the RFC |
| 1091 | */ |
| 1092 | if ((i = dns_str_to_dn_label_len(string)) == -1) |
| 1093 | return NULL; |
| 1094 | |
| 1095 | /* yes, so let's check there is enough memory */ |
| 1096 | if (dn_len < i + offset) |
| 1097 | return NULL; |
| 1098 | |
Willy Tarreau | d69d6f3 | 2015-07-22 16:45:36 +0200 | [diff] [blame] | 1099 | i = strlen(string); |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 1100 | memcpy(dn + offset, string, i); |
| 1101 | dn[i + offset] = '\0'; |
| 1102 | /* avoid a '\0' at the beginning of dn string which may prevent the for loop |
| 1103 | * below from working. |
| 1104 | * Actually, this is the reason of the offset. */ |
| 1105 | dn[0] = '0'; |
| 1106 | |
| 1107 | for (c = dn; *c ; ++c) { |
| 1108 | /* c points to the first '0' char or a dot, which we don't want to read */ |
| 1109 | d = c + offset; |
| 1110 | i = 0; |
| 1111 | while (*d != '.' && *d) { |
| 1112 | i++; |
| 1113 | d++; |
| 1114 | } |
| 1115 | *c = i; |
| 1116 | |
| 1117 | c = d - 1; /* because of c++ of the for loop */ |
| 1118 | } |
| 1119 | |
| 1120 | return dn; |
| 1121 | } |
| 1122 | |
| 1123 | /* |
| 1124 | * compute and return the length of <string> it it were translated into domain name |
| 1125 | * label: |
| 1126 | * www.haproxy.org into 3www7haproxy3org would return 16 |
| 1127 | * NOTE: add +1 for '\0' when allocating memory ;) |
| 1128 | */ |
| 1129 | int dns_str_to_dn_label_len(const char *string) |
| 1130 | { |
| 1131 | return strlen(string) + 1; |
| 1132 | } |
| 1133 | |
| 1134 | /* |
| 1135 | * validates host name: |
| 1136 | * - total size |
| 1137 | * - each label size individually |
| 1138 | * returns: |
| 1139 | * 0 in case of error. If <err> is not NULL, an error message is stored there. |
| 1140 | * 1 when no error. <err> is left unaffected. |
| 1141 | */ |
| 1142 | int dns_hostname_validation(const char *string, char **err) |
| 1143 | { |
| 1144 | const char *c, *d; |
| 1145 | int i; |
| 1146 | |
| 1147 | if (strlen(string) > DNS_MAX_NAME_SIZE) { |
| 1148 | if (err) |
| 1149 | *err = DNS_TOO_LONG_FQDN; |
| 1150 | return 0; |
| 1151 | } |
| 1152 | |
| 1153 | c = string; |
| 1154 | while (*c) { |
| 1155 | d = c; |
| 1156 | |
| 1157 | i = 0; |
| 1158 | while (*d != '.' && *d && i <= DNS_MAX_LABEL_SIZE) { |
| 1159 | i++; |
| 1160 | if (!((*d == '-') || (*d == '_') || |
| 1161 | ((*d >= 'a') && (*d <= 'z')) || |
| 1162 | ((*d >= 'A') && (*d <= 'Z')) || |
| 1163 | ((*d >= '0') && (*d <= '9')))) { |
| 1164 | if (err) |
| 1165 | *err = DNS_INVALID_CHARACTER; |
| 1166 | return 0; |
| 1167 | } |
| 1168 | d++; |
| 1169 | } |
| 1170 | |
| 1171 | if ((i >= DNS_MAX_LABEL_SIZE) && (d[i] != '.')) { |
| 1172 | if (err) |
| 1173 | *err = DNS_LABEL_TOO_LONG; |
| 1174 | return 0; |
| 1175 | } |
| 1176 | |
| 1177 | if (*d == '\0') |
| 1178 | goto out; |
| 1179 | |
| 1180 | c = ++d; |
| 1181 | } |
| 1182 | out: |
| 1183 | return 1; |
| 1184 | } |
| 1185 | |
| 1186 | /* |
| 1187 | * 2 bytes random generator to generate DNS query ID |
| 1188 | */ |
| 1189 | uint16_t dns_rnd16(void) |
| 1190 | { |
| 1191 | dns_query_id_seed ^= dns_query_id_seed << 13; |
| 1192 | dns_query_id_seed ^= dns_query_id_seed >> 7; |
| 1193 | dns_query_id_seed ^= dns_query_id_seed << 17; |
| 1194 | return dns_query_id_seed; |
| 1195 | } |
| 1196 | |
| 1197 | |
| 1198 | /* |
| 1199 | * function called when a timeout occurs during name resolution process |
| 1200 | * if max number of tries is reached, then stop, otherwise, retry. |
| 1201 | */ |
| 1202 | struct task *dns_process_resolve(struct task *t) |
| 1203 | { |
| 1204 | struct dns_resolvers *resolvers = t->context; |
| 1205 | struct dns_resolution *resolution, *res_back; |
Baptiste Assmann | 060e573 | 2016-01-06 02:01:59 +0100 | [diff] [blame] | 1206 | int res_preferred_afinet, res_preferred_afinet6; |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 1207 | |
| 1208 | /* timeout occurs inevitably for the first element of the FIFO queue */ |
| 1209 | if (LIST_ISEMPTY(&resolvers->curr_resolution)) { |
| 1210 | /* no first entry, so wake up was useless */ |
| 1211 | t->expire = TICK_ETERNITY; |
| 1212 | return t; |
| 1213 | } |
| 1214 | |
| 1215 | /* look for the first resolution which is not expired */ |
| 1216 | list_for_each_entry_safe(resolution, res_back, &resolvers->curr_resolution, list) { |
| 1217 | /* when we find the first resolution in the future, then we can stop here */ |
| 1218 | if (tick_is_le(now_ms, resolution->last_sent_packet)) |
| 1219 | goto out; |
| 1220 | |
| 1221 | /* |
| 1222 | * if current resolution has been tried too many times and finishes in timeout |
| 1223 | * we update its status and remove it from the list |
| 1224 | */ |
Baptiste Assmann | f778bb4 | 2015-09-09 00:54:38 +0200 | [diff] [blame] | 1225 | if (resolution->try <= 0) { |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 1226 | /* clean up resolution information and remove from the list */ |
| 1227 | dns_reset_resolution(resolution); |
| 1228 | |
| 1229 | /* notify the result to the requester */ |
| 1230 | resolution->requester_error_cb(resolution, DNS_RESP_TIMEOUT); |
Baptiste Assmann | 382824c | 2016-01-06 01:53:46 +0100 | [diff] [blame] | 1231 | goto out; |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 1232 | } |
| 1233 | |
Baptiste Assmann | f778bb4 | 2015-09-09 00:54:38 +0200 | [diff] [blame] | 1234 | resolution->try -= 1; |
| 1235 | |
Baptiste Assmann | 6f79aca | 2016-04-05 21:19:51 +0200 | [diff] [blame] | 1236 | res_preferred_afinet = resolution->opts->family_prio == AF_INET && resolution->query_type == DNS_RTYPE_A; |
| 1237 | res_preferred_afinet6 = resolution->opts->family_prio == AF_INET6 && resolution->query_type == DNS_RTYPE_AAAA; |
Baptiste Assmann | 060e573 | 2016-01-06 02:01:59 +0100 | [diff] [blame] | 1238 | |
| 1239 | /* let's change the query type if needed */ |
| 1240 | if (res_preferred_afinet6) { |
| 1241 | /* fallback from AAAA to A */ |
| 1242 | resolution->query_type = DNS_RTYPE_A; |
| 1243 | } |
| 1244 | else if (res_preferred_afinet) { |
| 1245 | /* fallback from A to AAAA */ |
| 1246 | resolution->query_type = DNS_RTYPE_AAAA; |
| 1247 | } |
| 1248 | |
Baptiste Assmann | 382824c | 2016-01-06 01:53:46 +0100 | [diff] [blame] | 1249 | /* resend the DNS query */ |
| 1250 | dns_send_query(resolution); |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 1251 | |
Baptiste Assmann | 382824c | 2016-01-06 01:53:46 +0100 | [diff] [blame] | 1252 | /* check if we have more than one resolution in the list */ |
| 1253 | if (dns_check_resolution_queue(resolvers) > 1) { |
| 1254 | /* move the rsolution to the end of the list */ |
| 1255 | LIST_DEL(&resolution->list); |
| 1256 | LIST_ADDQ(&resolvers->curr_resolution, &resolution->list); |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 1257 | } |
| 1258 | } |
| 1259 | |
| 1260 | out: |
| 1261 | dns_update_resolvers_timeout(resolvers); |
| 1262 | return t; |
| 1263 | } |