lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | /* dnsmasq is Copyright (c) 2000-2021 Simon Kelley |
| 2 | |
| 3 | This program is free software; you can redistribute it and/or modify |
| 4 | it under the terms of the GNU General Public License as published by |
| 5 | the Free Software Foundation; version 2 dated June, 1991, or |
| 6 | (at your option) version 3 dated 29 June, 2007. |
| 7 | |
| 8 | This program is distributed in the hope that it will be useful, |
| 9 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | GNU General Public License for more details. |
| 12 | |
| 13 | You should have received a copy of the GNU General Public License |
| 14 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 | */ |
| 16 | |
| 17 | #include "dnsmasq.h" |
| 18 | |
| 19 | static struct frec *get_new_frec(time_t now, struct server *serv, int force); |
| 20 | static struct frec *lookup_frec(unsigned short id, int fd, void *hash, int *firstp, int *lastp); |
| 21 | static struct frec *lookup_frec_by_query(void *hash, unsigned int flags, unsigned int flagmask); |
| 22 | |
| 23 | static unsigned short get_id(void); |
| 24 | static void free_frec(struct frec *f); |
| 25 | static void query_full(time_t now, char *domain); |
| 26 | |
| 27 | static void return_reply(time_t now, struct frec *forward, struct dns_header *header, ssize_t n, int status); |
| 28 | |
| 29 | /* Send a UDP packet with its source address set as "source" |
| 30 | unless nowild is true, when we just send it with the kernel default */ |
| 31 | int send_from(int fd, int nowild, char *packet, size_t len, |
| 32 | union mysockaddr *to, union all_addr *source, |
| 33 | unsigned int iface) |
| 34 | { |
| 35 | struct msghdr msg; |
| 36 | struct iovec iov[1]; |
| 37 | union { |
| 38 | struct cmsghdr align; /* this ensures alignment */ |
| 39 | #if defined(HAVE_LINUX_NETWORK) |
| 40 | char control[CMSG_SPACE(sizeof(struct in_pktinfo))]; |
| 41 | #elif defined(IP_SENDSRCADDR) |
| 42 | char control[CMSG_SPACE(sizeof(struct in_addr))]; |
| 43 | #endif |
| 44 | char control6[CMSG_SPACE(sizeof(struct in6_pktinfo))]; |
| 45 | } control_u; |
| 46 | |
| 47 | iov[0].iov_base = packet; |
| 48 | iov[0].iov_len = len; |
| 49 | |
| 50 | msg.msg_control = NULL; |
| 51 | msg.msg_controllen = 0; |
| 52 | msg.msg_flags = 0; |
| 53 | msg.msg_name = to; |
| 54 | msg.msg_namelen = sa_len(to); |
| 55 | msg.msg_iov = iov; |
| 56 | msg.msg_iovlen = 1; |
| 57 | |
| 58 | if (!nowild) |
| 59 | { |
| 60 | struct cmsghdr *cmptr; |
| 61 | msg.msg_control = &control_u; |
| 62 | msg.msg_controllen = sizeof(control_u); |
| 63 | cmptr = CMSG_FIRSTHDR(&msg); |
| 64 | |
| 65 | if (to->sa.sa_family == AF_INET) |
| 66 | { |
| 67 | #if defined(HAVE_LINUX_NETWORK) |
| 68 | struct in_pktinfo p; |
| 69 | p.ipi_ifindex = 0; |
| 70 | p.ipi_spec_dst = source->addr4; |
| 71 | msg.msg_controllen = CMSG_SPACE(sizeof(struct in_pktinfo)); |
| 72 | memcpy(CMSG_DATA(cmptr), &p, sizeof(p)); |
| 73 | cmptr->cmsg_len = CMSG_LEN(sizeof(struct in_pktinfo)); |
| 74 | cmptr->cmsg_level = IPPROTO_IP; |
| 75 | cmptr->cmsg_type = IP_PKTINFO; |
| 76 | #elif defined(IP_SENDSRCADDR) |
| 77 | msg.msg_controllen = CMSG_SPACE(sizeof(struct in_addr)); |
| 78 | memcpy(CMSG_DATA(cmptr), &(source->addr4), sizeof(source->addr4)); |
| 79 | cmptr->cmsg_len = CMSG_LEN(sizeof(struct in_addr)); |
| 80 | cmptr->cmsg_level = IPPROTO_IP; |
| 81 | cmptr->cmsg_type = IP_SENDSRCADDR; |
| 82 | #endif |
| 83 | } |
| 84 | else |
| 85 | { |
| 86 | struct in6_pktinfo p; |
| 87 | p.ipi6_ifindex = iface; /* Need iface for IPv6 to handle link-local addrs */ |
| 88 | p.ipi6_addr = source->addr6; |
| 89 | msg.msg_controllen = CMSG_SPACE(sizeof(struct in6_pktinfo)); |
| 90 | memcpy(CMSG_DATA(cmptr), &p, sizeof(p)); |
| 91 | cmptr->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo)); |
| 92 | cmptr->cmsg_type = daemon->v6pktinfo; |
| 93 | cmptr->cmsg_level = IPPROTO_IPV6; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | while (retry_send(sendmsg(fd, &msg, 0))); |
| 98 | |
| 99 | if (errno != 0) |
| 100 | { |
| 101 | #ifdef HAVE_LINUX_NETWORK |
| 102 | /* If interface is still in DAD, EINVAL results - ignore that. */ |
| 103 | if (errno != EINVAL) |
| 104 | my_syslog(LOG_ERR, _("failed to send packet: %s"), strerror(errno)); |
| 105 | #endif |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | return 1; |
| 110 | } |
| 111 | |
| 112 | #ifdef HAVE_CONNTRACK |
| 113 | static void set_outgoing_mark(struct frec *forward, int fd) |
| 114 | { |
| 115 | /* Copy connection mark of incoming query to outgoing connection. */ |
| 116 | unsigned int mark; |
| 117 | if (get_incoming_mark(&forward->frec_src.source, &forward->frec_src.dest, 0, &mark)) |
| 118 | setsockopt(fd, SOL_SOCKET, SO_MARK, &mark, sizeof(unsigned int)); |
| 119 | } |
| 120 | #endif |
| 121 | |
| 122 | static void log_query_mysockaddr(unsigned int flags, char *name, union mysockaddr *addr, char *arg) |
| 123 | { |
| 124 | if (addr->sa.sa_family == AF_INET) |
| 125 | log_query(flags | F_IPV4, name, (union all_addr *)&addr->in.sin_addr, arg); |
| 126 | else |
| 127 | log_query(flags | F_IPV6, name, (union all_addr *)&addr->in6.sin6_addr, arg); |
| 128 | } |
| 129 | |
| 130 | static void server_send(struct server *server, int fd, |
| 131 | const void *header, size_t plen, int flags) |
| 132 | { |
| 133 | while (retry_send(sendto(fd, header, plen, flags, |
| 134 | &server->addr.sa, |
| 135 | sa_len(&server->addr)))); |
| 136 | } |
| 137 | |
| 138 | #ifdef HAVE_DNSSEC |
| 139 | static void server_send_log(struct server *server, int fd, |
| 140 | const void *header, size_t plen, int dumpflags, |
| 141 | unsigned int logflags, char *name, char *arg) |
| 142 | { |
| 143 | #ifdef HAVE_DUMPFILE |
| 144 | dump_packet(dumpflags, (void *)header, (size_t)plen, NULL, &server->addr); |
| 145 | #endif |
| 146 | log_query_mysockaddr(logflags, name, &server->addr, arg); |
| 147 | server_send(server, fd, header, plen, 0); |
| 148 | } |
| 149 | #endif |
| 150 | |
| 151 | static int domain_no_rebind(char *domain) |
| 152 | { |
| 153 | struct server *serv; |
| 154 | int dlen = (int)strlen(domain); |
| 155 | |
| 156 | for (serv = daemon->no_rebind; serv; serv = serv->next) |
| 157 | if (dlen >= serv->domain_len && strcmp(serv->domain, &domain[dlen - serv->domain_len]) == 0) |
| 158 | return 1; |
| 159 | |
| 160 | return 0; |
| 161 | } |
| 162 | |
| 163 | static int forward_query(int udpfd, union mysockaddr *udpaddr, |
| 164 | union all_addr *dst_addr, unsigned int dst_iface, |
| 165 | struct dns_header *header, size_t plen, char *limit, time_t now, |
| 166 | struct frec *forward, int ad_reqd, int do_bit) |
| 167 | { |
| 168 | unsigned int flags = 0; |
| 169 | unsigned int fwd_flags = 0; |
| 170 | int is_dnssec = forward && (forward->flags & (FREC_DNSKEY_QUERY | FREC_DS_QUERY)); |
| 171 | struct server *master; |
| 172 | unsigned int gotname = extract_request(header, plen, daemon->namebuff, NULL); |
| 173 | void *hash = hash_questions(header, plen, daemon->namebuff); |
| 174 | unsigned char *oph = find_pseudoheader(header, plen, NULL, NULL, NULL, NULL); |
| 175 | int old_src = 0; |
| 176 | int first, last, start = 0; |
| 177 | int subnet, cacheable, forwarded = 0; |
| 178 | size_t edns0_len; |
| 179 | unsigned char *pheader; |
| 180 | int ede = EDE_UNSET; |
| 181 | (void)do_bit; |
| 182 | |
| 183 | if (header->hb4 & HB4_CD) |
| 184 | fwd_flags |= FREC_CHECKING_DISABLED; |
| 185 | if (ad_reqd) |
| 186 | fwd_flags |= FREC_AD_QUESTION; |
| 187 | if (oph) |
| 188 | fwd_flags |= FREC_HAS_PHEADER; |
| 189 | #ifdef HAVE_DNSSEC |
| 190 | if (do_bit) |
| 191 | fwd_flags |= FREC_DO_QUESTION; |
| 192 | #endif |
| 193 | |
| 194 | /* Check for retry on existing query. |
| 195 | FREC_DNSKEY and FREC_DS_QUERY are never set in flags, so the test below |
| 196 | ensures that no frec created for internal DNSSEC query can be returned here. |
| 197 | |
| 198 | Similarly FREC_NO_CACHE is never set in flags, so a query which is |
| 199 | contigent on a particular source address EDNS0 option will never be matched. */ |
| 200 | if (forward) |
| 201 | old_src = 1; |
| 202 | else if ((forward = lookup_frec_by_query(hash, fwd_flags, |
| 203 | FREC_CHECKING_DISABLED | FREC_AD_QUESTION | FREC_DO_QUESTION | |
| 204 | FREC_HAS_PHEADER | FREC_DNSKEY_QUERY | FREC_DS_QUERY | FREC_NO_CACHE))) |
| 205 | { |
| 206 | struct frec_src *src; |
| 207 | |
| 208 | for (src = &forward->frec_src; src; src = src->next) |
| 209 | if (src->orig_id == ntohs(header->id) && |
| 210 | sockaddr_isequal(&src->source, udpaddr)) |
| 211 | break; |
| 212 | |
| 213 | if (src) |
| 214 | old_src = 1; |
| 215 | else |
| 216 | { |
| 217 | /* Existing query, but from new source, just add this |
| 218 | client to the list that will get the reply.*/ |
| 219 | |
| 220 | /* Note whine_malloc() zeros memory. */ |
| 221 | if (!daemon->free_frec_src && |
| 222 | daemon->frec_src_count < daemon->ftabsize && |
| 223 | (daemon->free_frec_src = whine_malloc(sizeof(struct frec_src)))) |
| 224 | { |
| 225 | daemon->frec_src_count++; |
| 226 | daemon->free_frec_src->next = NULL; |
| 227 | } |
| 228 | |
| 229 | /* If we've been spammed with many duplicates, return REFUSED. */ |
| 230 | if (!daemon->free_frec_src) |
| 231 | { |
| 232 | query_full(now, NULL); |
| 233 | goto reply; |
| 234 | } |
| 235 | |
| 236 | src = daemon->free_frec_src; |
| 237 | daemon->free_frec_src = src->next; |
| 238 | src->next = forward->frec_src.next; |
| 239 | forward->frec_src.next = src; |
| 240 | src->orig_id = ntohs(header->id); |
| 241 | src->source = *udpaddr; |
| 242 | src->dest = *dst_addr; |
| 243 | src->log_id = daemon->log_id; |
| 244 | src->iface = dst_iface; |
| 245 | src->fd = udpfd; |
| 246 | |
| 247 | /* closely spaced identical queries cannot be a try and a retry, so |
| 248 | it's safe to wait for the reply from the first without |
| 249 | forwarding the second. */ |
| 250 | if (difftime(now, forward->time) < 2) |
| 251 | return 0; |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | /* new query */ |
| 256 | if (!forward) |
| 257 | { |
| 258 | if (lookup_domain(daemon->namebuff, gotname, &first, &last)) |
| 259 | flags = is_local_answer(now, first, daemon->namebuff); |
| 260 | else |
| 261 | { |
| 262 | /* no available server. */ |
| 263 | ede = EDE_NOT_READY; |
| 264 | flags = 0; |
| 265 | } |
| 266 | |
| 267 | /* don't forward A or AAAA queries for simple names, except the empty name */ |
| 268 | if (!flags && |
| 269 | option_bool(OPT_NODOTS_LOCAL) && |
| 270 | (gotname & (F_IPV4 | F_IPV6)) && |
| 271 | !strchr(daemon->namebuff, '.') && |
| 272 | strlen(daemon->namebuff) != 0) |
| 273 | flags = check_for_local_domain(daemon->namebuff, now) ? F_NOERR : F_NXDOMAIN; |
| 274 | |
| 275 | /* Configured answer. */ |
| 276 | if (flags || ede == EDE_NOT_READY) |
| 277 | goto reply; |
| 278 | |
| 279 | master = daemon->serverarray[first]; |
| 280 | |
| 281 | if (!(forward = get_new_frec(now, master, 0))) |
| 282 | goto reply; |
| 283 | /* table full - flags == 0, return REFUSED */ |
| 284 | |
| 285 | forward->frec_src.source = *udpaddr; |
| 286 | forward->frec_src.orig_id = ntohs(header->id); |
| 287 | forward->frec_src.dest = *dst_addr; |
| 288 | forward->frec_src.iface = dst_iface; |
| 289 | forward->frec_src.next = NULL; |
| 290 | forward->frec_src.fd = udpfd; |
| 291 | forward->new_id = get_id(); |
| 292 | memcpy(forward->hash, hash, HASH_SIZE); |
| 293 | forward->forwardall = 0; |
| 294 | forward->flags = fwd_flags; |
| 295 | if (domain_no_rebind(daemon->namebuff)) |
| 296 | forward->flags |= FREC_NOREBIND; |
| 297 | if (header->hb4 & HB4_CD) |
| 298 | forward->flags |= FREC_CHECKING_DISABLED; |
| 299 | if (ad_reqd) |
| 300 | forward->flags |= FREC_AD_QUESTION; |
| 301 | #ifdef HAVE_DNSSEC |
| 302 | forward->work_counter = DNSSEC_WORK; |
| 303 | if (do_bit) |
| 304 | forward->flags |= FREC_DO_QUESTION; |
| 305 | #endif |
| 306 | |
| 307 | start = first; |
| 308 | |
| 309 | if (option_bool(OPT_ALL_SERVERS)) |
| 310 | forward->forwardall = 1; |
| 311 | |
| 312 | if (!option_bool(OPT_ORDER)) |
| 313 | { |
| 314 | if (master->forwardcount++ > FORWARD_TEST || |
| 315 | difftime(now, master->forwardtime) > FORWARD_TIME || |
| 316 | master->last_server == -1) |
| 317 | { |
| 318 | master->forwardtime = now; |
| 319 | master->forwardcount = 0; |
| 320 | forward->forwardall = 1; |
| 321 | } |
| 322 | else |
| 323 | start = master->last_server; |
| 324 | } |
| 325 | } |
| 326 | else |
| 327 | { |
| 328 | /* retry on existing query, from original source. Send to all available servers */ |
| 329 | #ifdef HAVE_DNSSEC |
| 330 | /* If we've already got an answer to this query, but we're awaiting keys for validation, |
| 331 | there's no point retrying the query, retry the key query instead...... */ |
| 332 | if (forward->blocking_query) |
| 333 | { |
| 334 | int is_sign; |
| 335 | unsigned char *pheader; |
| 336 | |
| 337 | while (forward->blocking_query) |
| 338 | forward = forward->blocking_query; |
| 339 | |
| 340 | blockdata_retrieve(forward->stash, forward->stash_len, (void *)header); |
| 341 | plen = forward->stash_len; |
| 342 | /* get query for logging. */ |
| 343 | extract_request(header, plen, daemon->namebuff, NULL); |
| 344 | |
| 345 | if (find_pseudoheader(header, plen, NULL, &pheader, &is_sign, NULL) && !is_sign) |
| 346 | PUTSHORT(SAFE_PKTSZ, pheader); |
| 347 | |
| 348 | /* Find suitable servers: should never fail. */ |
| 349 | if (!filter_servers(forward->sentto->arrayposn, F_DNSSECOK, &first, &last)) |
| 350 | return 0; |
| 351 | |
| 352 | is_dnssec = 1; |
| 353 | forward->forwardall = 1; |
| 354 | } |
| 355 | else |
| 356 | #endif |
| 357 | { |
| 358 | /* retry on existing query, from original source. Send to all available servers */ |
| 359 | forward->sentto->failed_queries++; |
| 360 | |
| 361 | if (!filter_servers(forward->sentto->arrayposn, F_SERVER, &first, &last)) |
| 362 | goto reply; |
| 363 | |
| 364 | master = daemon->serverarray[first]; |
| 365 | |
| 366 | /* Forward to all available servers on retry of query from same host. */ |
| 367 | if (!option_bool(OPT_ORDER) && old_src) |
| 368 | forward->forwardall = 1; |
| 369 | else |
| 370 | { |
| 371 | start = forward->sentto->arrayposn; |
| 372 | |
| 373 | if (option_bool(OPT_ORDER)) |
| 374 | { |
| 375 | /* In strict order mode, there must be a server later in the list |
| 376 | left to send to, otherwise without the forwardall mechanism, |
| 377 | code further on will cycle around the list forwever if they |
| 378 | all return REFUSED. If at the last, give up. */ |
| 379 | if (++start == last) |
| 380 | goto reply; |
| 381 | } |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | /* If we didn't get an answer advertising a maximal packet in EDNS, |
| 386 | fall back to 1280, which should work everywhere on IPv6. |
| 387 | If that generates an answer, it will become the new default |
| 388 | for this server */ |
| 389 | forward->flags |= FREC_TEST_PKTSZ; |
| 390 | } |
| 391 | |
| 392 | /* If a query is retried, use the log_id for the retry when logging the answer. */ |
| 393 | forward->frec_src.log_id = daemon->log_id; |
| 394 | |
| 395 | /* We may be resending a DNSSEC query here, for which the below processing is not necessary. */ |
| 396 | if (!is_dnssec) |
| 397 | { |
| 398 | header->id = htons(forward->new_id); |
| 399 | |
| 400 | plen = add_edns0_config(header, plen, ((unsigned char *)header) + PACKETSZ, &forward->frec_src.source, now, &subnet, &cacheable); |
| 401 | |
| 402 | if (subnet) |
| 403 | forward->flags |= FREC_HAS_SUBNET; |
| 404 | |
| 405 | if (!cacheable) |
| 406 | forward->flags |= FREC_NO_CACHE; |
| 407 | |
| 408 | #ifdef HAVE_DNSSEC |
| 409 | if (option_bool(OPT_DNSSEC_VALID) && (master->flags & SERV_DO_DNSSEC)) |
| 410 | { |
| 411 | plen = add_do_bit(header, plen, ((unsigned char *) header) + PACKETSZ); |
| 412 | |
| 413 | /* For debugging, set Checking Disabled, otherwise, have the upstream check too, |
| 414 | this allows it to select auth servers when one is returning bad data. */ |
| 415 | if (option_bool(OPT_DNSSEC_DEBUG)) |
| 416 | header->hb4 |= HB4_CD; |
| 417 | |
| 418 | } |
| 419 | #endif |
| 420 | |
| 421 | if (find_pseudoheader(header, plen, &edns0_len, &pheader, NULL, NULL)) |
| 422 | { |
| 423 | /* If there wasn't a PH before, and there is now, we added it. */ |
| 424 | if (!oph) |
| 425 | forward->flags |= FREC_ADDED_PHEADER; |
| 426 | |
| 427 | /* If we're sending an EDNS0 with any options, we can't recreate the query from a reply. */ |
| 428 | if (edns0_len > 11) |
| 429 | forward->flags |= FREC_HAS_EXTRADATA; |
| 430 | |
| 431 | /* Reduce udp size on retransmits. */ |
| 432 | if (forward->flags & FREC_TEST_PKTSZ) |
| 433 | PUTSHORT(SAFE_PKTSZ, pheader); |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | if (forward->forwardall) |
| 438 | start = first; |
| 439 | |
| 440 | forwarded = 0; |
| 441 | |
| 442 | /* check for send errors here (no route to host) |
| 443 | if we fail to send to all nameservers, send back an error |
| 444 | packet straight away (helps modem users when offline) */ |
| 445 | |
| 446 | while (1) |
| 447 | { |
| 448 | int fd; |
| 449 | struct server *srv = daemon->serverarray[start]; |
| 450 | |
| 451 | if ((fd = allocate_rfd(&forward->rfds, srv)) != -1) |
| 452 | { |
| 453 | |
| 454 | #ifdef HAVE_CONNTRACK |
| 455 | /* Copy connection mark of incoming query to outgoing connection. */ |
| 456 | if (option_bool(OPT_CONNTRACK)) |
| 457 | set_outgoing_mark(forward, fd); |
| 458 | #endif |
| 459 | |
| 460 | #ifdef HAVE_DNSSEC |
| 461 | if (option_bool(OPT_DNSSEC_VALID) && (forward->flags & FREC_ADDED_PHEADER)) |
| 462 | { |
| 463 | /* Difficult one here. If our client didn't send EDNS0, we will have set the UDP |
| 464 | packet size to 512. But that won't provide space for the RRSIGS in many cases. |
| 465 | The RRSIGS will be stripped out before the answer goes back, so the packet should |
| 466 | shrink again. So, if we added a do-bit, bump the udp packet size to the value |
| 467 | known to be OK for this server. We check returned size after stripping and set |
| 468 | the truncated bit if it's still too big. */ |
| 469 | unsigned char *pheader; |
| 470 | int is_sign; |
| 471 | if (find_pseudoheader(header, plen, NULL, &pheader, &is_sign, NULL) && !is_sign) |
| 472 | PUTSHORT(srv->edns_pktsz, pheader); |
| 473 | } |
| 474 | #endif |
| 475 | |
| 476 | if (retry_send(sendto(fd, (char *)header, plen, 0, |
| 477 | &srv->addr.sa, |
| 478 | sa_len(&srv->addr)))) |
| 479 | continue; |
| 480 | |
| 481 | if (errno == 0) |
| 482 | { |
| 483 | #ifdef HAVE_DUMPFILE |
| 484 | dump_packet(DUMP_UP_QUERY, (void *)header, plen, NULL, &srv->addr); |
| 485 | #endif |
| 486 | |
| 487 | /* Keep info in case we want to re-send this packet */ |
| 488 | daemon->srv_save = srv; |
| 489 | daemon->packet_len = plen; |
| 490 | daemon->fd_save = fd; |
| 491 | |
| 492 | if (!(forward->flags & (FREC_DNSKEY_QUERY | FREC_DS_QUERY))) |
| 493 | { |
| 494 | if (!gotname) |
| 495 | strcpy(daemon->namebuff, "query"); |
| 496 | log_query_mysockaddr(F_SERVER | F_FORWARD, daemon->namebuff, |
| 497 | &srv->addr, NULL); |
| 498 | } |
| 499 | #ifdef HAVE_DNSSEC |
| 500 | else |
| 501 | log_query_mysockaddr(F_NOEXTRA | F_DNSSEC, daemon->namebuff, &srv->addr, |
| 502 | querystr("dnssec-retry", (forward->flags & FREC_DNSKEY_QUERY) ? T_DNSKEY : T_DS)); |
| 503 | #endif |
| 504 | |
| 505 | srv->queries++; |
| 506 | forwarded = 1; |
| 507 | forward->sentto = srv; |
| 508 | if (!forward->forwardall) |
| 509 | break; |
| 510 | forward->forwardall++; |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | if (++start == last) |
| 515 | break; |
| 516 | } |
| 517 | |
| 518 | if (forwarded || is_dnssec) |
| 519 | return 1; |
| 520 | |
| 521 | /* could not send on, prepare to return */ |
| 522 | header->id = htons(forward->frec_src.orig_id); |
| 523 | free_frec(forward); /* cancel */ |
| 524 | ede = EDE_NETERR; |
| 525 | |
| 526 | reply: |
| 527 | if (udpfd != -1) |
| 528 | { |
| 529 | if (!(plen = make_local_answer(flags, gotname, plen, header, daemon->namebuff, limit, first, last, ede))) |
| 530 | return 0; |
| 531 | |
| 532 | if (oph) |
| 533 | { |
| 534 | u16 swap = htons((u16)ede); |
| 535 | |
| 536 | if (ede != EDE_UNSET) |
| 537 | plen = add_pseudoheader(header, plen, (unsigned char *)limit, daemon->edns_pktsz, EDNS0_OPTION_EDE, (unsigned char *)&swap, 2, do_bit, 0); |
| 538 | else |
| 539 | plen = add_pseudoheader(header, plen, (unsigned char *)limit, daemon->edns_pktsz, 0, NULL, 0, do_bit, 0); |
| 540 | } |
| 541 | |
| 542 | #if defined(HAVE_CONNTRACK) && defined(HAVE_UBUS) |
| 543 | if (option_bool(OPT_CMARK_ALST_EN)) |
| 544 | { |
| 545 | unsigned int mark; |
| 546 | int have_mark = get_incoming_mark(udpaddr, dst_addr, /* istcp: */ 0, &mark); |
| 547 | if (have_mark && ((u32)mark & daemon->allowlist_mask)) |
| 548 | report_addresses(header, plen, mark); |
| 549 | } |
| 550 | #endif |
| 551 | |
| 552 | send_from(udpfd, option_bool(OPT_NOWILD) || option_bool(OPT_CLEVERBIND), (char *)header, plen, udpaddr, dst_addr, dst_iface); |
| 553 | } |
| 554 | |
| 555 | return 0; |
| 556 | } |
| 557 | |
| 558 | static size_t process_reply(struct dns_header *header, time_t now, struct server *server, size_t n, int check_rebind, |
| 559 | int no_cache, int cache_secure, int bogusanswer, int ad_reqd, int do_bit, int added_pheader, |
| 560 | int check_subnet, union mysockaddr *query_source, unsigned char *limit, int ede) |
| 561 | { |
| 562 | unsigned char *pheader, *sizep; |
| 563 | char **sets = 0; |
| 564 | int munged = 0, is_sign; |
| 565 | unsigned int rcode = RCODE(header); |
| 566 | size_t plen; |
| 567 | |
| 568 | (void)ad_reqd; |
| 569 | (void)do_bit; |
| 570 | (void)bogusanswer; |
| 571 | |
| 572 | #ifdef HAVE_IPSET |
| 573 | if (daemon->ipsets && extract_request(header, n, daemon->namebuff, NULL)) |
| 574 | { |
| 575 | /* Similar algorithm to search_servers. */ |
| 576 | struct ipsets *ipset_pos; |
| 577 | unsigned int namelen = strlen(daemon->namebuff); |
| 578 | unsigned int matchlen = 0; |
| 579 | for (ipset_pos = daemon->ipsets; ipset_pos; ipset_pos = ipset_pos->next) |
| 580 | { |
| 581 | unsigned int domainlen = strlen(ipset_pos->domain); |
| 582 | char *matchstart = daemon->namebuff + namelen - domainlen; |
| 583 | if (namelen >= domainlen && hostname_isequal(matchstart, ipset_pos->domain) && |
| 584 | (domainlen == 0 || namelen == domainlen || *(matchstart - 1) == '.' ) && |
| 585 | domainlen >= matchlen) |
| 586 | { |
| 587 | matchlen = domainlen; |
| 588 | sets = ipset_pos->sets; |
| 589 | } |
| 590 | } |
| 591 | } |
| 592 | #endif |
| 593 | |
| 594 | if ((pheader = find_pseudoheader(header, n, &plen, &sizep, &is_sign, NULL))) |
| 595 | { |
| 596 | /* Get extended RCODE. */ |
| 597 | rcode |= sizep[2] << 4; |
| 598 | |
| 599 | if (check_subnet && !check_source(header, plen, pheader, query_source)) |
| 600 | { |
| 601 | my_syslog(LOG_WARNING, _("discarding DNS reply: subnet option mismatch")); |
| 602 | return 0; |
| 603 | } |
| 604 | |
| 605 | if (!is_sign) |
| 606 | { |
| 607 | if (added_pheader) |
| 608 | { |
| 609 | /* client didn't send EDNS0, we added one, strip it off before returning answer. */ |
| 610 | n = rrfilter(header, n, 0); |
| 611 | pheader = NULL; |
| 612 | } |
| 613 | else |
| 614 | { |
| 615 | /* If upstream is advertising a larger UDP packet size |
| 616 | than we allow, trim it so that we don't get overlarge |
| 617 | requests for the client. We can't do this for signed packets. */ |
| 618 | unsigned short udpsz; |
| 619 | GETSHORT(udpsz, sizep); |
| 620 | if (udpsz > daemon->edns_pktsz) |
| 621 | { |
| 622 | sizep -= 2; |
| 623 | PUTSHORT(daemon->edns_pktsz, sizep); |
| 624 | } |
| 625 | |
| 626 | #ifdef HAVE_DNSSEC |
| 627 | /* If the client didn't set the do bit, but we did, reset it. */ |
| 628 | if (option_bool(OPT_DNSSEC_VALID) && !do_bit) |
| 629 | { |
| 630 | unsigned short flags; |
| 631 | sizep += 2; /* skip RCODE */ |
| 632 | GETSHORT(flags, sizep); |
| 633 | flags &= ~0x8000; |
| 634 | sizep -= 2; |
| 635 | PUTSHORT(flags, sizep); |
| 636 | } |
| 637 | #endif |
| 638 | } |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | /* RFC 4035 sect 4.6 para 3 */ |
| 643 | if (!is_sign && !option_bool(OPT_DNSSEC_PROXY)) |
| 644 | header->hb4 &= ~HB4_AD; |
| 645 | |
| 646 | header->hb4 |= HB4_RA; /* recursion if available */ |
| 647 | |
| 648 | if (OPCODE(header) != QUERY) |
| 649 | return resize_packet(header, n, pheader, plen); |
| 650 | |
| 651 | if (rcode != NOERROR && rcode != NXDOMAIN) |
| 652 | { |
| 653 | union all_addr a; |
| 654 | a.log.rcode = rcode; |
| 655 | a.log.ede = ede; |
| 656 | log_query(F_UPSTREAM | F_RCODE, "error", &a, NULL); |
| 657 | |
| 658 | return resize_packet(header, n, pheader, plen); |
| 659 | } |
| 660 | |
| 661 | /* Complain loudly if the upstream server is non-recursive. */ |
| 662 | if (!(header->hb4 & HB4_RA) && rcode == NOERROR && |
| 663 | server && !(server->flags & SERV_WARNED_RECURSIVE)) |
| 664 | { |
| 665 | (void)prettyprint_addr(&server->addr, daemon->namebuff); |
| 666 | my_syslog(LOG_WARNING, _("nameserver %s refused to do a recursive query"), daemon->namebuff); |
| 667 | if (!option_bool(OPT_LOG)) |
| 668 | server->flags |= SERV_WARNED_RECURSIVE; |
| 669 | } |
| 670 | |
| 671 | if (daemon->bogus_addr && rcode != NXDOMAIN && |
| 672 | check_for_bogus_wildcard(header, n, daemon->namebuff, now)) |
| 673 | { |
| 674 | munged = 1; |
| 675 | SET_RCODE(header, NXDOMAIN); |
| 676 | header->hb3 &= ~HB3_AA; |
| 677 | cache_secure = 0; |
| 678 | ede = EDE_BLOCKED; |
| 679 | } |
| 680 | else |
| 681 | { |
| 682 | int doctored = 0; |
| 683 | |
| 684 | if (rcode == NXDOMAIN && |
| 685 | extract_request(header, n, daemon->namebuff, NULL)) |
| 686 | { |
| 687 | if (check_for_local_domain(daemon->namebuff, now) || |
| 688 | lookup_domain(daemon->namebuff, F_CONFIG, NULL, NULL)) |
| 689 | { |
| 690 | /* if we forwarded a query for a locally known name (because it was for |
| 691 | an unknown type) and the answer is NXDOMAIN, convert that to NODATA, |
| 692 | since we know that the domain exists, even if upstream doesn't */ |
| 693 | munged = 1; |
| 694 | header->hb3 |= HB3_AA; |
| 695 | SET_RCODE(header, NOERROR); |
| 696 | cache_secure = 0; |
| 697 | } |
| 698 | } |
| 699 | |
| 700 | if (extract_addresses(header, n, daemon->namebuff, now, sets, is_sign, check_rebind, no_cache, cache_secure, &doctored)) |
| 701 | { |
| 702 | my_syslog(LOG_WARNING, _("possible DNS-rebind attack detected: %s"), daemon->namebuff); |
| 703 | munged = 1; |
| 704 | cache_secure = 0; |
| 705 | ede = EDE_BLOCKED; |
| 706 | } |
| 707 | |
| 708 | if (doctored) |
| 709 | cache_secure = 0; |
| 710 | } |
| 711 | |
| 712 | #ifdef HAVE_DNSSEC |
| 713 | if (bogusanswer && !(header->hb4 & HB4_CD) && !option_bool(OPT_DNSSEC_DEBUG)) |
| 714 | { |
| 715 | /* Bogus reply, turn into SERVFAIL */ |
| 716 | SET_RCODE(header, SERVFAIL); |
| 717 | munged = 1; |
| 718 | } |
| 719 | |
| 720 | if (option_bool(OPT_DNSSEC_VALID)) |
| 721 | { |
| 722 | header->hb4 &= ~HB4_AD; |
| 723 | |
| 724 | if (!(header->hb4 & HB4_CD) && ad_reqd && cache_secure) |
| 725 | header->hb4 |= HB4_AD; |
| 726 | |
| 727 | /* If the requestor didn't set the DO bit, don't return DNSSEC info. */ |
| 728 | if (!do_bit) |
| 729 | n = rrfilter(header, n, 1); |
| 730 | } |
| 731 | #endif |
| 732 | |
| 733 | /* do this after extract_addresses. Ensure NODATA reply and remove |
| 734 | nameserver info. */ |
| 735 | if (munged) |
| 736 | { |
| 737 | header->ancount = htons(0); |
| 738 | header->nscount = htons(0); |
| 739 | header->arcount = htons(0); |
| 740 | header->hb3 &= ~HB3_TC; |
| 741 | } |
| 742 | |
| 743 | /* the bogus-nxdomain stuff, doctor and NXDOMAIN->NODATA munging can all elide |
| 744 | sections of the packet. Find the new length here and put back pseudoheader |
| 745 | if it was removed. */ |
| 746 | n = resize_packet(header, n, pheader, plen); |
| 747 | |
| 748 | if (pheader && ede != EDE_UNSET) |
| 749 | { |
| 750 | u16 swap = htons((u16)ede); |
| 751 | n = add_pseudoheader(header, n, limit, daemon->edns_pktsz, EDNS0_OPTION_EDE, (unsigned char *)&swap, 2, do_bit, 1); |
| 752 | } |
| 753 | |
| 754 | return n; |
| 755 | } |
| 756 | |
| 757 | #ifdef HAVE_DNSSEC |
| 758 | static void dnssec_validate(struct frec *forward, struct dns_header *header, |
| 759 | ssize_t plen, int status, time_t now) |
| 760 | { |
| 761 | daemon->log_display_id = forward->frec_src.log_id; |
| 762 | |
| 763 | /* We've had a reply already, which we're validating. Ignore this duplicate */ |
| 764 | if (forward->blocking_query) |
| 765 | return; |
| 766 | |
| 767 | /* Truncated answer can't be validated. |
| 768 | If this is an answer to a DNSSEC-generated query, we still |
| 769 | need to get the client to retry over TCP, so return |
| 770 | an answer with the TC bit set, even if the actual answer fits. |
| 771 | */ |
| 772 | if (header->hb3 & HB3_TC) |
| 773 | status = STAT_TRUNCATED; |
| 774 | |
| 775 | /* If all replies to a query are REFUSED, give up. */ |
| 776 | if (RCODE(header) == REFUSED) |
| 777 | status = STAT_ABANDONED; |
| 778 | |
| 779 | /* As soon as anything returns BOGUS, we stop and unwind, to do otherwise |
| 780 | would invite infinite loops, since the answers to DNSKEY and DS queries |
| 781 | will not be cached, so they'll be repeated. */ |
| 782 | if (!STAT_ISEQUAL(status, STAT_BOGUS) && !STAT_ISEQUAL(status, STAT_TRUNCATED) && !STAT_ISEQUAL(status, STAT_ABANDONED)) |
| 783 | { |
| 784 | if (forward->flags & FREC_DNSKEY_QUERY) |
| 785 | status = dnssec_validate_by_ds(now, header, plen, daemon->namebuff, daemon->keyname, forward->class); |
| 786 | else if (forward->flags & FREC_DS_QUERY) |
| 787 | status = dnssec_validate_ds(now, header, plen, daemon->namebuff, daemon->keyname, forward->class); |
| 788 | else |
| 789 | status = dnssec_validate_reply(now, header, plen, daemon->namebuff, daemon->keyname, &forward->class, |
| 790 | !option_bool(OPT_DNSSEC_IGN_NS) && (forward->sentto->flags & SERV_DO_DNSSEC), |
| 791 | NULL, NULL, NULL); |
| 792 | #ifdef HAVE_DUMPFILE |
| 793 | if (STAT_ISEQUAL(status, STAT_BOGUS)) |
| 794 | dump_packet((forward->flags & (FREC_DNSKEY_QUERY | FREC_DS_QUERY)) ? DUMP_SEC_BOGUS : DUMP_BOGUS, |
| 795 | header, (size_t)plen, &forward->sentto->addr, NULL); |
| 796 | #endif |
| 797 | } |
| 798 | |
| 799 | /* Can't validate, as we're missing key data. Put this |
| 800 | answer aside, whilst we get that. */ |
| 801 | if (STAT_ISEQUAL(status, STAT_NEED_DS) || STAT_ISEQUAL(status, STAT_NEED_KEY)) |
| 802 | { |
| 803 | struct frec *new = NULL; |
| 804 | int serverind; |
| 805 | struct blockdata *stash; |
| 806 | |
| 807 | /* Now save reply pending receipt of key data */ |
| 808 | if ((serverind = dnssec_server(forward->sentto, daemon->keyname, NULL, NULL)) != -1 && |
| 809 | (stash = blockdata_alloc((char *)header, plen))) |
| 810 | { |
| 811 | struct server *server = daemon->serverarray[serverind]; |
| 812 | struct frec *orig; |
| 813 | unsigned int flags; |
| 814 | void *hash; |
| 815 | size_t nn; |
| 816 | |
| 817 | /* validate routines leave name of required record in daemon->keyname */ |
| 818 | nn = dnssec_generate_query(header, ((unsigned char *) header) + server->edns_pktsz, |
| 819 | daemon->keyname, forward->class, |
| 820 | STAT_ISEQUAL(status, STAT_NEED_KEY) ? T_DNSKEY : T_DS, server->edns_pktsz); |
| 821 | |
| 822 | flags = STAT_ISEQUAL(status, STAT_NEED_KEY) ? FREC_DNSKEY_QUERY : FREC_DS_QUERY; |
| 823 | hash = hash_questions(header, nn, daemon->namebuff); |
| 824 | |
| 825 | if ((new = lookup_frec_by_query(hash, flags, FREC_DNSKEY_QUERY | FREC_DS_QUERY))) |
| 826 | { |
| 827 | forward->next_dependent = new->dependent; |
| 828 | new->dependent = forward; |
| 829 | /* Make consistent, only replace query copy with unvalidated answer |
| 830 | when we set ->blocking_query. */ |
| 831 | if (forward->stash) |
| 832 | blockdata_free(forward->stash); |
| 833 | forward->blocking_query = new; |
| 834 | forward->stash_len = plen; |
| 835 | forward->stash = stash; |
| 836 | return; |
| 837 | } |
| 838 | |
| 839 | /* Find the original query that started it all.... */ |
| 840 | for (orig = forward; orig->dependent; orig = orig->dependent); |
| 841 | |
| 842 | /* Make sure we don't expire and free the orig frec during the |
| 843 | allocation of a new one: third arg of get_new_frec() does that. */ |
| 844 | if (--orig->work_counter == 0 || !(new = get_new_frec(now, server, 1))) |
| 845 | blockdata_free(stash); /* don't leak this on failure. */ |
| 846 | else |
| 847 | { |
| 848 | int fd; |
| 849 | struct frec *next = new->next; |
| 850 | |
| 851 | *new = *forward; /* copy everything, then overwrite */ |
| 852 | new->next = next; |
| 853 | new->blocking_query = NULL; |
| 854 | |
| 855 | new->frec_src.log_id = daemon->log_display_id = ++daemon->log_id; |
| 856 | new->sentto = server; |
| 857 | new->rfds = NULL; |
| 858 | new->frec_src.next = NULL; |
| 859 | new->flags &= ~(FREC_DNSKEY_QUERY | FREC_DS_QUERY | FREC_HAS_EXTRADATA); |
| 860 | new->flags |= flags; |
| 861 | new->forwardall = 0; |
| 862 | |
| 863 | forward->next_dependent = NULL; |
| 864 | new->dependent = forward; /* to find query awaiting new one. */ |
| 865 | |
| 866 | /* Make consistent, only replace query copy with unvalidated answer |
| 867 | when we set ->blocking_query. */ |
| 868 | forward->blocking_query = new; |
| 869 | if (forward->stash) |
| 870 | blockdata_free(forward->stash); |
| 871 | forward->stash_len = plen; |
| 872 | forward->stash = stash; |
| 873 | |
| 874 | memcpy(new->hash, hash, HASH_SIZE); |
| 875 | new->new_id = get_id(); |
| 876 | header->id = htons(new->new_id); |
| 877 | /* Save query for retransmission */ |
| 878 | new->stash = blockdata_alloc((char *)header, nn); |
| 879 | new->stash_len = nn; |
| 880 | |
| 881 | /* Don't resend this. */ |
| 882 | daemon->srv_save = NULL; |
| 883 | |
| 884 | if ((fd = allocate_rfd(&new->rfds, server)) != -1) |
| 885 | { |
| 886 | #ifdef HAVE_CONNTRACK |
| 887 | if (option_bool(OPT_CONNTRACK)) |
| 888 | set_outgoing_mark(orig, fd); |
| 889 | #endif |
| 890 | server_send_log(server, fd, header, nn, DUMP_SEC_QUERY, |
| 891 | F_NOEXTRA | F_DNSSEC, daemon->keyname, |
| 892 | querystr("dnssec-query", STAT_ISEQUAL(status, STAT_NEED_KEY) ? T_DNSKEY : T_DS)); |
| 893 | server->queries++; |
| 894 | } |
| 895 | |
| 896 | return; |
| 897 | } |
| 898 | } |
| 899 | |
| 900 | /* sending DNSSEC query failed. */ |
| 901 | status = STAT_ABANDONED; |
| 902 | } |
| 903 | |
| 904 | /* Validated original answer, all done. */ |
| 905 | if (!forward->dependent) |
| 906 | return_reply(now, forward, header, plen, status); |
| 907 | else |
| 908 | { |
| 909 | /* validated subsidiary query/queries, (and cached result) |
| 910 | pop that and return to the previous query/queries we were working on. */ |
| 911 | struct frec *prev, *nxt = forward->dependent; |
| 912 | |
| 913 | free_frec(forward); |
| 914 | |
| 915 | while ((prev = nxt)) |
| 916 | { |
| 917 | /* ->next_dependent will have changed after return from recursive call below. */ |
| 918 | nxt = prev->next_dependent; |
| 919 | prev->blocking_query = NULL; /* already gone */ |
| 920 | blockdata_retrieve(prev->stash, prev->stash_len, (void *)header); |
| 921 | dnssec_validate(prev, header, prev->stash_len, status, now); |
| 922 | } |
| 923 | } |
| 924 | } |
| 925 | #endif |
| 926 | |
| 927 | /* sets new last_server */ |
| 928 | void reply_query(int fd, time_t now) |
| 929 | { |
| 930 | /* packet from peer server, extract data for cache, and send to |
| 931 | original requester */ |
| 932 | struct dns_header *header; |
| 933 | union mysockaddr serveraddr; |
| 934 | struct frec *forward; |
| 935 | socklen_t addrlen = sizeof(serveraddr); |
| 936 | ssize_t n = recvfrom(fd, daemon->packet, daemon->packet_buff_sz, 0, &serveraddr.sa, &addrlen); |
| 937 | struct server *server; |
| 938 | void *hash; |
| 939 | int first, last, c; |
| 940 | |
| 941 | /* packet buffer overwritten */ |
| 942 | daemon->srv_save = NULL; |
| 943 | |
| 944 | /* Determine the address of the server replying so that we can mark that as good */ |
| 945 | if (serveraddr.sa.sa_family == AF_INET6) |
| 946 | serveraddr.in6.sin6_flowinfo = 0; |
| 947 | |
| 948 | header = (struct dns_header *)daemon->packet; |
| 949 | |
| 950 | if (n < (int)sizeof(struct dns_header) || !(header->hb3 & HB3_QR)) |
| 951 | return; |
| 952 | |
| 953 | hash = hash_questions(header, n, daemon->namebuff); |
| 954 | |
| 955 | if (!(forward = lookup_frec(ntohs(header->id), fd, hash, &first, &last))) |
| 956 | return; |
| 957 | |
| 958 | /* spoof check: answer must come from known server, also |
| 959 | we may have sent the same query to multiple servers from |
| 960 | the same local socket, and would like to know which one has answered. */ |
| 961 | for (c = first; c != last; c++) |
| 962 | if (sockaddr_isequal(&daemon->serverarray[c]->addr, &serveraddr)) |
| 963 | break; |
| 964 | |
| 965 | if (c == last) |
| 966 | return; |
| 967 | |
| 968 | server = daemon->serverarray[c]; |
| 969 | |
| 970 | if (RCODE(header) != REFUSED) |
| 971 | daemon->serverarray[first]->last_server = c; |
| 972 | else if (daemon->serverarray[first]->last_server == c) |
| 973 | daemon->serverarray[first]->last_server = -1; |
| 974 | |
| 975 | /* If sufficient time has elapsed, try and expand UDP buffer size again. */ |
| 976 | if (difftime(now, server->pktsz_reduced) > UDP_TEST_TIME) |
| 977 | server->edns_pktsz = daemon->edns_pktsz; |
| 978 | |
| 979 | #ifdef HAVE_DUMPFILE |
| 980 | dump_packet((forward->flags & (FREC_DNSKEY_QUERY | FREC_DS_QUERY)) ? DUMP_SEC_REPLY : DUMP_UP_REPLY, |
| 981 | (void *)header, n, &serveraddr, NULL); |
| 982 | #endif |
| 983 | |
| 984 | /* log_query gets called indirectly all over the place, so |
| 985 | pass these in global variables - sorry. */ |
| 986 | daemon->log_display_id = forward->frec_src.log_id; |
| 987 | daemon->log_source_addr = &forward->frec_src.source; |
| 988 | |
| 989 | if (daemon->ignore_addr && RCODE(header) == NOERROR && |
| 990 | check_for_ignored_address(header, n)) |
| 991 | return; |
| 992 | |
| 993 | /* Note: if we send extra options in the EDNS0 header, we can't recreate |
| 994 | the query from the reply. */ |
| 995 | if ((RCODE(header) == REFUSED || RCODE(header) == SERVFAIL) && |
| 996 | forward->forwardall == 0 && |
| 997 | !(forward->flags & FREC_HAS_EXTRADATA)) |
| 998 | /* for broken servers, attempt to send to another one. */ |
| 999 | { |
| 1000 | unsigned char *pheader, *udpsz; |
| 1001 | unsigned short udp_size = PACKETSZ; /* default if no EDNS0 */ |
| 1002 | size_t plen; |
| 1003 | int is_sign; |
| 1004 | size_t nn = 0; |
| 1005 | |
| 1006 | #ifdef HAVE_DNSSEC |
| 1007 | /* DNSSEC queries have a copy of the original query stashed. |
| 1008 | The query MAY have got a good answer, and be awaiting |
| 1009 | the results of further queries, in which case |
| 1010 | The Stash contains something else and we don't need to retry anyway. */ |
| 1011 | if ((forward->flags & (FREC_DNSKEY_QUERY | FREC_DS_QUERY)) && !forward->blocking_query) |
| 1012 | { |
| 1013 | blockdata_retrieve(forward->stash, forward->stash_len, (void *)header); |
| 1014 | nn = forward->stash_len; |
| 1015 | udp_size = daemon->edns_pktsz; |
| 1016 | } |
| 1017 | else |
| 1018 | #endif |
| 1019 | { |
| 1020 | /* recreate query from reply */ |
| 1021 | if ((pheader = find_pseudoheader(header, (size_t)n, &plen, &udpsz, &is_sign, NULL))) |
| 1022 | GETSHORT(udp_size, udpsz); |
| 1023 | |
| 1024 | /* If the client provides an EDNS0 UDP size, use that to limit our reply. |
| 1025 | (bounded by the maximum configured). If no EDNS0, then it |
| 1026 | defaults to 512 */ |
| 1027 | if (udp_size > daemon->edns_pktsz) |
| 1028 | udp_size = daemon->edns_pktsz; |
| 1029 | else if (udp_size < PACKETSZ) |
| 1030 | udp_size = PACKETSZ; /* Sanity check - can't reduce below default. RFC 6891 6.2.3 */ |
| 1031 | |
| 1032 | if (!is_sign && |
| 1033 | (nn = resize_packet(header, (size_t)n, pheader, plen)) && |
| 1034 | (forward->flags & FREC_DO_QUESTION)) |
| 1035 | add_do_bit(header, nn, (unsigned char *)pheader + plen); |
| 1036 | |
| 1037 | header->ancount = htons(0); |
| 1038 | header->nscount = htons(0); |
| 1039 | header->arcount = htons(0); |
| 1040 | header->hb3 &= ~(HB3_QR | HB3_AA | HB3_TC); |
| 1041 | header->hb4 &= ~(HB4_RA | HB4_RCODE | HB4_CD | HB4_AD); |
| 1042 | if (forward->flags & FREC_CHECKING_DISABLED) |
| 1043 | header->hb4 |= HB4_CD; |
| 1044 | if (forward->flags & FREC_AD_QUESTION) |
| 1045 | header->hb4 |= HB4_AD; |
| 1046 | } |
| 1047 | |
| 1048 | if (nn) |
| 1049 | { |
| 1050 | forward_query(-1, NULL, NULL, 0, header, nn, ((char *) header) + udp_size, now, forward, |
| 1051 | forward->flags & FREC_AD_QUESTION, forward->flags & FREC_DO_QUESTION); |
| 1052 | return; |
| 1053 | } |
| 1054 | } |
| 1055 | |
| 1056 | /* If the answer is an error, keep the forward record in place in case |
| 1057 | we get a good reply from another server. Kill it when we've |
| 1058 | had replies from all to avoid filling the forwarding table when |
| 1059 | everything is broken */ |
| 1060 | |
| 1061 | /* decrement count of replies recieved if we sent to more than one server. */ |
| 1062 | if (forward->forwardall && (--forward->forwardall > 1) && RCODE(header) == REFUSED) |
| 1063 | return; |
| 1064 | |
| 1065 | /* We tried resending to this server with a smaller maximum size and got an answer. |
| 1066 | Make that permanent. To avoid reduxing the packet size for a single dropped packet, |
| 1067 | only do this when we get a truncated answer, or one larger than the safe size. */ |
| 1068 | if (server->edns_pktsz > SAFE_PKTSZ && (forward->flags & FREC_TEST_PKTSZ) && |
| 1069 | ((header->hb3 & HB3_TC) || n >= SAFE_PKTSZ)) |
| 1070 | { |
| 1071 | server->edns_pktsz = SAFE_PKTSZ; |
| 1072 | server->pktsz_reduced = now; |
| 1073 | (void)prettyprint_addr(&server->addr, daemon->addrbuff); |
| 1074 | my_syslog(LOG_WARNING, _("reducing DNS packet size for nameserver %s to %d"), daemon->addrbuff, SAFE_PKTSZ); |
| 1075 | } |
| 1076 | |
| 1077 | forward->sentto = server; |
| 1078 | |
| 1079 | #ifdef HAVE_DNSSEC |
| 1080 | if ((forward->sentto->flags & SERV_DO_DNSSEC) && |
| 1081 | option_bool(OPT_DNSSEC_VALID) && |
| 1082 | !(forward->flags & FREC_CHECKING_DISABLED)) |
| 1083 | dnssec_validate(forward, header, n, STAT_OK, now); |
| 1084 | else |
| 1085 | #endif |
| 1086 | return_reply(now, forward, header, n, STAT_OK); |
| 1087 | } |
| 1088 | |
| 1089 | static void return_reply(time_t now, struct frec *forward, struct dns_header *header, ssize_t n, int status) |
| 1090 | { |
| 1091 | int check_rebind = 0, no_cache_dnssec = 0, cache_secure = 0, bogusanswer = 0; |
| 1092 | size_t nn; |
| 1093 | int ede = EDE_UNSET; |
| 1094 | |
| 1095 | (void)status; |
| 1096 | |
| 1097 | daemon->log_display_id = forward->frec_src.log_id; |
| 1098 | daemon->log_source_addr = &forward->frec_src.source; |
| 1099 | |
| 1100 | /* Don't cache replies where DNSSEC validation was turned off, either |
| 1101 | the upstream server told us so, or the original query specified it. */ |
| 1102 | if ((header->hb4 & HB4_CD) || (forward->flags & FREC_CHECKING_DISABLED)) |
| 1103 | no_cache_dnssec = 1; |
| 1104 | |
| 1105 | #ifdef HAVE_DNSSEC |
| 1106 | if (!STAT_ISEQUAL(status, STAT_OK)) |
| 1107 | { |
| 1108 | /* status is STAT_OK when validation not turned on. */ |
| 1109 | no_cache_dnssec = 0; |
| 1110 | |
| 1111 | if (STAT_ISEQUAL(status, STAT_TRUNCATED)) |
| 1112 | header->hb3 |= HB3_TC; |
| 1113 | else |
| 1114 | { |
| 1115 | char *result, *domain = "result"; |
| 1116 | union all_addr a; |
| 1117 | |
| 1118 | a.log.ede = ede = errflags_to_ede(status); |
| 1119 | |
| 1120 | if (STAT_ISEQUAL(status, STAT_ABANDONED)) |
| 1121 | { |
| 1122 | result = "ABANDONED"; |
| 1123 | status = STAT_BOGUS; |
| 1124 | } |
| 1125 | else |
| 1126 | result = (STAT_ISEQUAL(status, STAT_SECURE) ? "SECURE" : (STAT_ISEQUAL(status, STAT_INSECURE) ? "INSECURE" : "BOGUS")); |
| 1127 | |
| 1128 | if (STAT_ISEQUAL(status, STAT_SECURE)) |
| 1129 | cache_secure = 1; |
| 1130 | else if (STAT_ISEQUAL(status, STAT_BOGUS)) |
| 1131 | { |
| 1132 | no_cache_dnssec = 1; |
| 1133 | bogusanswer = 1; |
| 1134 | |
| 1135 | if (extract_request(header, n, daemon->namebuff, NULL)) |
| 1136 | domain = daemon->namebuff; |
| 1137 | } |
| 1138 | |
| 1139 | log_query(F_SECSTAT, domain, &a, result); |
| 1140 | } |
| 1141 | } |
| 1142 | #endif |
| 1143 | |
| 1144 | if (option_bool(OPT_NO_REBIND)) |
| 1145 | check_rebind = !(forward->flags & FREC_NOREBIND); |
| 1146 | |
| 1147 | /* restore CD bit to the value in the query */ |
| 1148 | if (forward->flags & FREC_CHECKING_DISABLED) |
| 1149 | header->hb4 |= HB4_CD; |
| 1150 | else |
| 1151 | header->hb4 &= ~HB4_CD; |
| 1152 | |
| 1153 | /* Never cache answers which are contingent on the source or MAC address EDSN0 option, |
| 1154 | since the cache is ignorant of such things. */ |
| 1155 | if (forward->flags & FREC_NO_CACHE) |
| 1156 | no_cache_dnssec = 1; |
| 1157 | |
| 1158 | if ((nn = process_reply(header, now, forward->sentto, (size_t)n, check_rebind, no_cache_dnssec, cache_secure, bogusanswer, |
| 1159 | forward->flags & FREC_AD_QUESTION, forward->flags & FREC_DO_QUESTION, |
| 1160 | forward->flags & FREC_ADDED_PHEADER, forward->flags & FREC_HAS_SUBNET, &forward->frec_src.source, |
| 1161 | ((unsigned char *)header) + daemon->edns_pktsz, ede))) |
| 1162 | { |
| 1163 | struct frec_src *src; |
| 1164 | |
| 1165 | header->id = htons(forward->frec_src.orig_id); |
| 1166 | #ifdef HAVE_DNSSEC |
| 1167 | /* We added an EDNSO header for the purpose of getting DNSSEC RRs, and set the value of the UDP payload size |
| 1168 | greater than the no-EDNS0-implied 512 to have space for the RRSIGS. If, having stripped them and the EDNS0 |
| 1169 | header, the answer is still bigger than 512, truncate it and mark it so. The client then retries with TCP. */ |
| 1170 | if (option_bool(OPT_DNSSEC_VALID) && (forward->flags & FREC_ADDED_PHEADER) && (nn > PACKETSZ)) |
| 1171 | { |
| 1172 | header->ancount = htons(0); |
| 1173 | header->nscount = htons(0); |
| 1174 | header->arcount = htons(0); |
| 1175 | header->hb3 |= HB3_TC; |
| 1176 | nn = resize_packet(header, nn, NULL, 0); |
| 1177 | } |
| 1178 | #endif |
| 1179 | |
| 1180 | for (src = &forward->frec_src; src; src = src->next) |
| 1181 | { |
| 1182 | header->id = htons(src->orig_id); |
| 1183 | |
| 1184 | #ifdef HAVE_DUMPFILE |
| 1185 | dump_packet(DUMP_REPLY, daemon->packet, (size_t)nn, NULL, &src->source); |
| 1186 | #endif |
| 1187 | |
| 1188 | #if defined(HAVE_CONNTRACK) && defined(HAVE_UBUS) |
| 1189 | if (option_bool(OPT_CMARK_ALST_EN)) |
| 1190 | { |
| 1191 | unsigned int mark; |
| 1192 | int have_mark = get_incoming_mark(&src->source, &src->dest, /* istcp: */ 0, &mark); |
| 1193 | if (have_mark && ((u32)mark & daemon->allowlist_mask)) |
| 1194 | report_addresses(header, nn, mark); |
| 1195 | } |
| 1196 | #endif |
| 1197 | |
| 1198 | send_from(src->fd, option_bool(OPT_NOWILD) || option_bool (OPT_CLEVERBIND), daemon->packet, nn, |
| 1199 | &src->source, &src->dest, src->iface); |
| 1200 | |
| 1201 | if (option_bool(OPT_EXTRALOG) && src != &forward->frec_src) |
| 1202 | { |
| 1203 | daemon->log_display_id = src->log_id; |
| 1204 | daemon->log_source_addr = &src->source; |
| 1205 | log_query(F_UPSTREAM, "query", NULL, "duplicate"); |
| 1206 | } |
| 1207 | } |
| 1208 | } |
| 1209 | |
| 1210 | free_frec(forward); /* cancel */ |
| 1211 | } |
| 1212 | |
| 1213 | |
| 1214 | #ifdef HAVE_CONNTRACK |
| 1215 | static int is_query_allowed_for_mark(u32 mark, const char *name) |
| 1216 | { |
| 1217 | int is_allowable_name, did_validate_name = 0; |
| 1218 | struct allowlist *allowlists; |
| 1219 | char **patterns_pos; |
| 1220 | |
| 1221 | for (allowlists = daemon->allowlists; allowlists; allowlists = allowlists->next) |
| 1222 | if (allowlists->mark == (mark & daemon->allowlist_mask & allowlists->mask)) |
| 1223 | for (patterns_pos = allowlists->patterns; *patterns_pos; patterns_pos++) |
| 1224 | { |
| 1225 | if (!strcmp(*patterns_pos, "*")) |
| 1226 | return 1; |
| 1227 | if (!did_validate_name) |
| 1228 | { |
| 1229 | is_allowable_name = name ? is_valid_dns_name(name) : 0; |
| 1230 | did_validate_name = 1; |
| 1231 | } |
| 1232 | if (is_allowable_name && is_dns_name_matching_pattern(name, *patterns_pos)) |
| 1233 | return 1; |
| 1234 | } |
| 1235 | return 0; |
| 1236 | } |
| 1237 | |
| 1238 | static size_t answer_disallowed(struct dns_header *header, size_t qlen, u32 mark, const char *name) |
| 1239 | { |
| 1240 | unsigned char *p; |
| 1241 | (void)name; |
| 1242 | (void)mark; |
| 1243 | |
| 1244 | #ifdef HAVE_UBUS |
| 1245 | if (name) |
| 1246 | ubus_event_bcast_connmark_allowlist_refused(mark, name); |
| 1247 | #endif |
| 1248 | |
| 1249 | setup_reply(header, /* flags: */ 0, EDE_BLOCKED); |
| 1250 | |
| 1251 | if (!(p = skip_questions(header, qlen))) |
| 1252 | return 0; |
| 1253 | return p - (unsigned char *)header; |
| 1254 | } |
| 1255 | #endif |
| 1256 | |
| 1257 | void receive_query(struct listener *listen, time_t now) |
| 1258 | { |
| 1259 | struct dns_header *header = (struct dns_header *)daemon->packet; |
| 1260 | union mysockaddr source_addr; |
| 1261 | unsigned char *pheader; |
| 1262 | unsigned short type, udp_size = PACKETSZ; /* default if no EDNS0 */ |
| 1263 | union all_addr dst_addr; |
| 1264 | struct in_addr netmask, dst_addr_4; |
| 1265 | size_t m,plen; |
| 1266 | ssize_t n,num; |
| 1267 | int if_index = 0, auth_dns = 0, do_bit = 0, have_pseudoheader = 0; |
| 1268 | #ifdef HAVE_CONNTRACK |
| 1269 | unsigned int mark = 0; |
| 1270 | int have_mark = 0; |
| 1271 | int is_single_query = 0, allowed = 1; |
| 1272 | #endif |
| 1273 | #ifdef HAVE_AUTH |
| 1274 | int local_auth = 0; |
| 1275 | #endif |
| 1276 | struct iovec iov[1]; |
| 1277 | struct msghdr msg; |
| 1278 | struct cmsghdr *cmptr; |
| 1279 | union { |
| 1280 | struct cmsghdr align; /* this ensures alignment */ |
| 1281 | char control6[CMSG_SPACE(sizeof(struct in6_pktinfo))]; |
| 1282 | #if defined(HAVE_LINUX_NETWORK) |
| 1283 | char control[CMSG_SPACE(sizeof(struct in_pktinfo))]; |
| 1284 | #elif defined(IP_RECVDSTADDR) && defined(HAVE_SOLARIS_NETWORK) |
| 1285 | char control[CMSG_SPACE(sizeof(struct in_addr)) + |
| 1286 | CMSG_SPACE(sizeof(unsigned int))]; |
| 1287 | #elif defined(IP_RECVDSTADDR) |
| 1288 | char control[CMSG_SPACE(sizeof(struct in_addr)) + |
| 1289 | CMSG_SPACE(sizeof(struct sockaddr_dl))]; |
| 1290 | #endif |
| 1291 | } control_u; |
| 1292 | int family = listen->addr.sa.sa_family; |
| 1293 | /* Can always get recvd interface for IPv6 */ |
| 1294 | int check_dst = !option_bool(OPT_NOWILD) || family == AF_INET6; |
| 1295 | |
| 1296 | /* packet buffer overwritten */ |
| 1297 | daemon->srv_save = NULL; |
| 1298 | |
| 1299 | dst_addr_4.s_addr = dst_addr.addr4.s_addr = 0; |
| 1300 | netmask.s_addr = 0; |
| 1301 | |
| 1302 | if (option_bool(OPT_NOWILD) && listen->iface) |
| 1303 | { |
| 1304 | auth_dns = listen->iface->dns_auth; |
| 1305 | |
| 1306 | if (family == AF_INET) |
| 1307 | { |
| 1308 | dst_addr_4 = dst_addr.addr4 = listen->iface->addr.in.sin_addr; |
| 1309 | netmask = listen->iface->netmask; |
| 1310 | } |
| 1311 | } |
| 1312 | |
| 1313 | iov[0].iov_base = daemon->packet; |
| 1314 | iov[0].iov_len = daemon->edns_pktsz; |
| 1315 | |
| 1316 | msg.msg_control = control_u.control; |
| 1317 | msg.msg_controllen = sizeof(control_u); |
| 1318 | msg.msg_flags = 0; |
| 1319 | msg.msg_name = &source_addr; |
| 1320 | msg.msg_namelen = sizeof(source_addr); |
| 1321 | msg.msg_iov = iov; |
| 1322 | msg.msg_iovlen = 1; |
| 1323 | |
| 1324 | if ((n = recvmsg(listen->fd, &msg, 0)) == -1) |
| 1325 | return; |
| 1326 | |
| 1327 | if (n < (int)sizeof(struct dns_header) || |
| 1328 | (msg.msg_flags & MSG_TRUNC) || |
| 1329 | (header->hb3 & HB3_QR)) |
| 1330 | return; |
| 1331 | num=n; |
| 1332 | /* Clear buffer beyond request to avoid risk of |
| 1333 | information disclosure. */ |
| 1334 | memset(daemon->packet + n, 0, daemon->edns_pktsz - n); |
| 1335 | |
| 1336 | source_addr.sa.sa_family = family; |
| 1337 | |
| 1338 | if (family == AF_INET) |
| 1339 | { |
| 1340 | /* Source-port == 0 is an error, we can't send back to that. |
| 1341 | http://www.ietf.org/mail-archive/web/dnsop/current/msg11441.html */ |
| 1342 | if (source_addr.in.sin_port == 0) |
| 1343 | return; |
| 1344 | } |
| 1345 | else |
| 1346 | { |
| 1347 | /* Source-port == 0 is an error, we can't send back to that. */ |
| 1348 | if (source_addr.in6.sin6_port == 0) |
| 1349 | return; |
| 1350 | source_addr.in6.sin6_flowinfo = 0; |
| 1351 | } |
| 1352 | |
| 1353 | /* We can be configured to only accept queries from at-most-one-hop-away addresses. */ |
| 1354 | if (option_bool(OPT_LOCAL_SERVICE)) |
| 1355 | { |
| 1356 | struct addrlist *addr; |
| 1357 | |
| 1358 | if (family == AF_INET6) |
| 1359 | { |
| 1360 | for (addr = daemon->interface_addrs; addr; addr = addr->next) |
| 1361 | if ((addr->flags & ADDRLIST_IPV6) && |
| 1362 | is_same_net6(&addr->addr.addr6, &source_addr.in6.sin6_addr, addr->prefixlen)) |
| 1363 | break; |
| 1364 | } |
| 1365 | else |
| 1366 | { |
| 1367 | struct in_addr netmask; |
| 1368 | for (addr = daemon->interface_addrs; addr; addr = addr->next) |
| 1369 | { |
| 1370 | netmask.s_addr = htonl(~(in_addr_t)0 << (32 - addr->prefixlen)); |
| 1371 | if (!(addr->flags & ADDRLIST_IPV6) && |
| 1372 | is_same_net(addr->addr.addr4, source_addr.in.sin_addr, netmask)) |
| 1373 | break; |
| 1374 | } |
| 1375 | } |
| 1376 | if (!addr) |
| 1377 | { |
| 1378 | static int warned = 0; |
| 1379 | if (!warned) |
| 1380 | { |
| 1381 | my_syslog(LOG_WARNING, _("Ignoring query from non-local network")); |
| 1382 | warned = 1; |
| 1383 | } |
| 1384 | return; |
| 1385 | } |
| 1386 | } |
| 1387 | |
| 1388 | if (check_dst) |
| 1389 | { |
| 1390 | struct ifreq ifr; |
| 1391 | |
| 1392 | if (msg.msg_controllen < sizeof(struct cmsghdr)) |
| 1393 | return; |
| 1394 | |
| 1395 | #if defined(HAVE_LINUX_NETWORK) |
| 1396 | if (family == AF_INET) |
| 1397 | for (cmptr = CMSG_FIRSTHDR(&msg); cmptr; cmptr = CMSG_NXTHDR(&msg, cmptr)) |
| 1398 | if (cmptr->cmsg_level == IPPROTO_IP && cmptr->cmsg_type == IP_PKTINFO) |
| 1399 | { |
| 1400 | union { |
| 1401 | unsigned char *c; |
| 1402 | struct in_pktinfo *p; |
| 1403 | } p; |
| 1404 | p.c = CMSG_DATA(cmptr); |
| 1405 | dst_addr_4 = dst_addr.addr4 = p.p->ipi_spec_dst; |
| 1406 | if_index = p.p->ipi_ifindex; |
| 1407 | } |
| 1408 | #elif defined(IP_RECVDSTADDR) && defined(IP_RECVIF) |
| 1409 | if (family == AF_INET) |
| 1410 | { |
| 1411 | for (cmptr = CMSG_FIRSTHDR(&msg); cmptr; cmptr = CMSG_NXTHDR(&msg, cmptr)) |
| 1412 | { |
| 1413 | union { |
| 1414 | unsigned char *c; |
| 1415 | unsigned int *i; |
| 1416 | struct in_addr *a; |
| 1417 | #ifndef HAVE_SOLARIS_NETWORK |
| 1418 | struct sockaddr_dl *s; |
| 1419 | #endif |
| 1420 | } p; |
| 1421 | p.c = CMSG_DATA(cmptr); |
| 1422 | if (cmptr->cmsg_level == IPPROTO_IP && cmptr->cmsg_type == IP_RECVDSTADDR) |
| 1423 | dst_addr_4 = dst_addr.addr4 = *(p.a); |
| 1424 | else if (cmptr->cmsg_level == IPPROTO_IP && cmptr->cmsg_type == IP_RECVIF) |
| 1425 | #ifdef HAVE_SOLARIS_NETWORK |
| 1426 | if_index = *(p.i); |
| 1427 | #else |
| 1428 | if_index = p.s->sdl_index; |
| 1429 | #endif |
| 1430 | } |
| 1431 | } |
| 1432 | #endif |
| 1433 | |
| 1434 | if (family == AF_INET6) |
| 1435 | { |
| 1436 | for (cmptr = CMSG_FIRSTHDR(&msg); cmptr; cmptr = CMSG_NXTHDR(&msg, cmptr)) |
| 1437 | if (cmptr->cmsg_level == IPPROTO_IPV6 && cmptr->cmsg_type == daemon->v6pktinfo) |
| 1438 | { |
| 1439 | union { |
| 1440 | unsigned char *c; |
| 1441 | struct in6_pktinfo *p; |
| 1442 | } p; |
| 1443 | p.c = CMSG_DATA(cmptr); |
| 1444 | |
| 1445 | dst_addr.addr6 = p.p->ipi6_addr; |
| 1446 | if_index = p.p->ipi6_ifindex; |
| 1447 | } |
| 1448 | } |
| 1449 | |
| 1450 | /* enforce available interface configuration */ |
| 1451 | |
| 1452 | if (!indextoname(listen->fd, if_index, ifr.ifr_name)) |
| 1453 | return; |
| 1454 | |
| 1455 | if (!iface_check(family, &dst_addr, ifr.ifr_name, &auth_dns)) |
| 1456 | { |
| 1457 | if (!option_bool(OPT_CLEVERBIND)) |
| 1458 | enumerate_interfaces(0); |
| 1459 | if (!loopback_exception(listen->fd, family, &dst_addr, ifr.ifr_name) && |
| 1460 | !label_exception(if_index, family, &dst_addr)) |
| 1461 | return; |
| 1462 | } |
| 1463 | |
| 1464 | if (family == AF_INET && option_bool(OPT_LOCALISE)) |
| 1465 | { |
| 1466 | struct irec *iface; |
| 1467 | |
| 1468 | /* get the netmask of the interface which has the address we were sent to. |
| 1469 | This is no necessarily the interface we arrived on. */ |
| 1470 | |
| 1471 | for (iface = daemon->interfaces; iface; iface = iface->next) |
| 1472 | if (iface->addr.sa.sa_family == AF_INET && |
| 1473 | iface->addr.in.sin_addr.s_addr == dst_addr_4.s_addr) |
| 1474 | break; |
| 1475 | |
| 1476 | /* interface may be new */ |
| 1477 | if (!iface && !option_bool(OPT_CLEVERBIND)) |
| 1478 | enumerate_interfaces(0); |
| 1479 | |
| 1480 | for (iface = daemon->interfaces; iface; iface = iface->next) |
| 1481 | if (iface->addr.sa.sa_family == AF_INET && |
| 1482 | iface->addr.in.sin_addr.s_addr == dst_addr_4.s_addr) |
| 1483 | break; |
| 1484 | |
| 1485 | /* If we failed, abandon localisation */ |
| 1486 | if (iface) |
| 1487 | netmask = iface->netmask; |
| 1488 | else |
| 1489 | dst_addr_4.s_addr = 0; |
| 1490 | } |
| 1491 | } |
| 1492 | |
| 1493 | /* log_query gets called indirectly all over the place, so |
| 1494 | pass these in global variables - sorry. */ |
| 1495 | daemon->log_display_id = ++daemon->log_id; |
| 1496 | daemon->log_source_addr = &source_addr; |
| 1497 | |
| 1498 | #ifdef HAVE_DUMPFILE |
| 1499 | dump_packet(DUMP_QUERY, daemon->packet, (size_t)n, &source_addr, NULL); |
| 1500 | #endif |
| 1501 | |
| 1502 | #ifdef HAVE_CONNTRACK |
| 1503 | if (option_bool(OPT_CMARK_ALST_EN)) |
| 1504 | have_mark = get_incoming_mark(&source_addr, &dst_addr, /* istcp: */ 0, &mark); |
| 1505 | #endif |
| 1506 | |
| 1507 | if (extract_request(header, (size_t)n, daemon->namebuff, &type)) |
| 1508 | { |
| 1509 | #ifdef HAVE_AUTH |
| 1510 | struct auth_zone *zone; |
| 1511 | #endif |
| 1512 | char *types = querystr(auth_dns ? "auth" : "query", type); |
| 1513 | |
| 1514 | log_query_mysockaddr(F_QUERY | F_FORWARD, daemon->namebuff, |
| 1515 | &source_addr, types); |
| 1516 | |
| 1517 | #ifdef HAVE_CONNTRACK |
| 1518 | is_single_query = 1; |
| 1519 | #endif |
| 1520 | |
| 1521 | #ifdef HAVE_AUTH |
| 1522 | /* find queries for zones we're authoritative for, and answer them directly */ |
| 1523 | if (!auth_dns && !option_bool(OPT_LOCALISE)) |
| 1524 | for (zone = daemon->auth_zones; zone; zone = zone->next) |
| 1525 | if (in_zone(zone, daemon->namebuff, NULL)) |
| 1526 | { |
| 1527 | auth_dns = 1; |
| 1528 | local_auth = 1; |
| 1529 | break; |
| 1530 | } |
| 1531 | #endif |
| 1532 | |
| 1533 | #ifdef HAVE_LOOP |
| 1534 | /* Check for forwarding loop */ |
| 1535 | if (detect_loop(daemon->namebuff, type)) |
| 1536 | return; |
| 1537 | #endif |
| 1538 | } |
| 1539 | |
| 1540 | if (find_pseudoheader(header, (size_t)n, NULL, &pheader, NULL, NULL)) |
| 1541 | { |
| 1542 | unsigned short flags; |
| 1543 | |
| 1544 | have_pseudoheader = 1; |
| 1545 | GETSHORT(udp_size, pheader); |
| 1546 | pheader += 2; /* ext_rcode */ |
| 1547 | GETSHORT(flags, pheader); |
| 1548 | |
| 1549 | if (flags & 0x8000) |
| 1550 | do_bit = 1;/* do bit */ |
| 1551 | |
| 1552 | /* If the client provides an EDNS0 UDP size, use that to limit our reply. |
| 1553 | (bounded by the maximum configured). If no EDNS0, then it |
| 1554 | defaults to 512 */ |
| 1555 | if (udp_size > daemon->edns_pktsz) |
| 1556 | udp_size = daemon->edns_pktsz; |
| 1557 | else if (udp_size < PACKETSZ) |
| 1558 | udp_size = PACKETSZ; /* Sanity check - can't reduce below default. RFC 6891 6.2.3 */ |
| 1559 | } |
| 1560 | #if 1 |
| 1561 | //printf("@!@dnsmasq %s len=%d\n",daemon->namebuff,strlen(daemon->namebuff)); |
| 1562 | if(strlen(daemon->namebuff) == 0)//nessus DNS Server Spoofed Request Amplification DDoS |
| 1563 | return; |
| 1564 | { |
| 1565 | char cfg_buf[32]; |
| 1566 | memset(cfg_buf, 0x00, sizeof(cfg_buf)); |
| 1567 | sc_cfg_get("lan_domain_Enabled", cfg_buf, sizeof(cfg_buf)); |
| 1568 | if(0 == strcmp(cfg_buf, "1")) |
| 1569 | { |
| 1570 | static char name_buf[512]; |
| 1571 | memset(name_buf,0x00, sizeof(name_buf)); |
| 1572 | sc_cfg_get("LocalDomain", name_buf, sizeof(name_buf)); |
| 1573 | if(!strcmp(daemon->namebuff,name_buf)) |
| 1574 | { |
| 1575 | |
| 1576 | if (type != T_A) |
| 1577 | { |
| 1578 | plen = (size_t)n; |
| 1579 | setup_reply(daemon->packet, 0, 0); |
| 1580 | unsigned char *p = skip_questions(header, plen); |
| 1581 | if(p) |
| 1582 | { |
| 1583 | plen = p - (unsigned char *)header; |
| 1584 | //send_from(listen->fd, daemon->options & OPT_NOWILD, daemon->packet, plen, &source_addr, &dst_addr, if_index); |
| 1585 | send_from(listen->fd, option_bool(OPT_NOWILD), daemon->packet, plen, &source_addr, &dst_addr, if_index); |
| 1586 | } |
| 1587 | return; |
| 1588 | } |
| 1589 | |
| 1590 | daemon->packet[2] = daemon->packet[2]|0x80; |
| 1591 | daemon->packet[7] = daemon->packet[7]|0x01; |
| 1592 | daemon->packet[num] = 0xc0; |
| 1593 | daemon->packet[num+1] = 0x0c; |
| 1594 | daemon->packet[num+2] = 0x00; |
| 1595 | daemon->packet[num+3] = 0x01; |
| 1596 | daemon->packet[num+4] = 0x00; |
| 1597 | daemon->packet[num+5] = 0x01; |
| 1598 | daemon->packet[num+6] = 0x00; |
| 1599 | daemon->packet[num+7] = 0x00; |
| 1600 | daemon->packet[num+8] = 0x00; |
| 1601 | daemon->packet[num+9] = 0x0a; |
| 1602 | daemon->packet[num+10] = 0x00; |
| 1603 | daemon->packet[num+11] = 0x04; |
| 1604 | daemon->packet[num+12] = *((char *)(&(dst_addr.addr4)) + 0)& 0xff; |
| 1605 | daemon->packet[num+13] = *((char *)(&(dst_addr.addr4)) + 1)& 0xff; |
| 1606 | daemon->packet[num+14] = *((char *)(&(dst_addr.addr4)) + 2)& 0xff; |
| 1607 | daemon->packet[num+15] = *((char *)(&(dst_addr.addr4)) + 3)& 0xff; |
| 1608 | //send_from(listen->fd, daemon->options & OPT_NOWILD, daemon->packet, num+16, &source_addr, &dst_addr, if_index); |
| 1609 | send_from(listen->fd, option_bool(OPT_NOWILD), daemon->packet, num+16, &source_addr, &dst_addr, if_index); |
| 1610 | return; |
| 1611 | } |
| 1612 | } |
| 1613 | { |
| 1614 | char web_buf[16] = {0}; |
| 1615 | sc_cfg_get("DNS_proxy", web_buf, sizeof(web_buf)); |
| 1616 | if(0 == strcmp(web_buf, "disable")) |
| 1617 | { |
| 1618 | return; |
| 1619 | } |
| 1620 | } |
| 1621 | } |
| 1622 | #endif |
| 1623 | #ifdef HAVE_CONNTRACK |
| 1624 | #ifdef HAVE_AUTH |
| 1625 | if (!auth_dns || local_auth) |
| 1626 | #endif |
| 1627 | if (option_bool(OPT_CMARK_ALST_EN) && have_mark && ((u32)mark & daemon->allowlist_mask)) |
| 1628 | allowed = is_query_allowed_for_mark((u32)mark, is_single_query ? daemon->namebuff : NULL); |
| 1629 | #endif |
| 1630 | |
| 1631 | if (0); |
| 1632 | #ifdef HAVE_CONNTRACK |
| 1633 | else if (!allowed) |
| 1634 | { |
| 1635 | u16 swap = htons(EDE_BLOCKED); |
| 1636 | |
| 1637 | m = answer_disallowed(header, (size_t)n, (u32)mark, is_single_query ? daemon->namebuff : NULL); |
| 1638 | |
| 1639 | if (have_pseudoheader && m != 0) |
| 1640 | m = add_pseudoheader(header, m, ((unsigned char *) header) + udp_size, daemon->edns_pktsz, |
| 1641 | EDNS0_OPTION_EDE, (unsigned char *)&swap, 2, do_bit, 0); |
| 1642 | |
| 1643 | if (m >= 1) |
| 1644 | { |
| 1645 | #ifdef HAVE_DUMPFILE |
| 1646 | dump_packet(DUMP_REPLY, daemon->packet, m, NULL, &source_addr); |
| 1647 | #endif |
| 1648 | send_from(listen->fd, option_bool(OPT_NOWILD) || option_bool(OPT_CLEVERBIND), |
| 1649 | (char *)header, m, &source_addr, &dst_addr, if_index); |
| 1650 | daemon->metrics[METRIC_DNS_LOCAL_ANSWERED]++; |
| 1651 | } |
| 1652 | } |
| 1653 | #endif |
| 1654 | #ifdef HAVE_AUTH |
| 1655 | else if (auth_dns) |
| 1656 | { |
| 1657 | m = answer_auth(header, ((char *) header) + udp_size, (size_t)n, now, &source_addr, |
| 1658 | local_auth, do_bit, have_pseudoheader); |
| 1659 | if (m >= 1) |
| 1660 | { |
| 1661 | #ifdef HAVE_DUMPFILE |
| 1662 | dump_packet(DUMP_REPLY, daemon->packet, m, NULL, &source_addr); |
| 1663 | #endif |
| 1664 | #if defined(HAVE_CONNTRACK) && defined(HAVE_UBUS) |
| 1665 | if (local_auth) |
| 1666 | if (option_bool(OPT_CMARK_ALST_EN) && have_mark && ((u32)mark & daemon->allowlist_mask)) |
| 1667 | report_addresses(header, m, mark); |
| 1668 | #endif |
| 1669 | send_from(listen->fd, option_bool(OPT_NOWILD) || option_bool(OPT_CLEVERBIND), |
| 1670 | (char *)header, m, &source_addr, &dst_addr, if_index); |
| 1671 | daemon->metrics[METRIC_DNS_AUTH_ANSWERED]++; |
| 1672 | } |
| 1673 | } |
| 1674 | #endif |
| 1675 | else |
| 1676 | { |
| 1677 | int ad_reqd = do_bit; |
| 1678 | /* RFC 6840 5.7 */ |
| 1679 | if (header->hb4 & HB4_AD) |
| 1680 | ad_reqd = 1; |
| 1681 | |
| 1682 | m = answer_request(header, ((char *) header) + udp_size, (size_t)n, |
| 1683 | dst_addr_4, netmask, now, ad_reqd, do_bit, have_pseudoheader); |
| 1684 | |
| 1685 | if (m >= 1) |
| 1686 | { |
| 1687 | #ifdef HAVE_DUMPFILE |
| 1688 | dump_packet(DUMP_REPLY, daemon->packet, m, NULL, &source_addr); |
| 1689 | #endif |
| 1690 | #if defined(HAVE_CONNTRACK) && defined(HAVE_UBUS) |
| 1691 | if (option_bool(OPT_CMARK_ALST_EN) && have_mark && ((u32)mark & daemon->allowlist_mask)) |
| 1692 | report_addresses(header, m, mark); |
| 1693 | #endif |
| 1694 | send_from(listen->fd, option_bool(OPT_NOWILD) || option_bool(OPT_CLEVERBIND), |
| 1695 | (char *)header, m, &source_addr, &dst_addr, if_index); |
| 1696 | daemon->metrics[METRIC_DNS_LOCAL_ANSWERED]++; |
| 1697 | } |
| 1698 | else if (forward_query(listen->fd, &source_addr, &dst_addr, if_index, |
| 1699 | header, (size_t)n, ((char *) header) + udp_size, now, NULL, ad_reqd, do_bit)) |
| 1700 | daemon->metrics[METRIC_DNS_QUERIES_FORWARDED]++; |
| 1701 | else |
| 1702 | daemon->metrics[METRIC_DNS_LOCAL_ANSWERED]++; |
| 1703 | } |
| 1704 | } |
| 1705 | |
| 1706 | /* Send query in packet, qsize to a server determined by first,last,start and |
| 1707 | get the reply. return reply size. */ |
| 1708 | static ssize_t tcp_talk(int first, int last, int start, unsigned char *packet, size_t qsize, |
| 1709 | int have_mark, unsigned int mark, struct server **servp) |
| 1710 | { |
| 1711 | int firstsendto = -1; |
| 1712 | u16 *length = (u16 *)packet; |
| 1713 | unsigned char *payload = &packet[2]; |
| 1714 | struct dns_header *header = (struct dns_header *)payload; |
| 1715 | unsigned char c1, c2; |
| 1716 | unsigned char hash[HASH_SIZE]; |
| 1717 | unsigned int rsize; |
| 1718 | |
| 1719 | (void)mark; |
| 1720 | (void)have_mark; |
| 1721 | |
| 1722 | memcpy(hash, hash_questions(header, (unsigned int)qsize, daemon->namebuff), HASH_SIZE); |
| 1723 | |
| 1724 | while (1) |
| 1725 | { |
| 1726 | int data_sent = 0; |
| 1727 | struct server *serv; |
| 1728 | |
| 1729 | if (firstsendto == -1) |
| 1730 | firstsendto = start; |
| 1731 | else |
| 1732 | { |
| 1733 | start++; |
| 1734 | |
| 1735 | if (start == last) |
| 1736 | start = first; |
| 1737 | |
| 1738 | if (start == firstsendto) |
| 1739 | break; |
| 1740 | } |
| 1741 | |
| 1742 | serv = daemon->serverarray[start]; |
| 1743 | |
| 1744 | retry: |
| 1745 | *length = htons(qsize); |
| 1746 | |
| 1747 | if (serv->tcpfd == -1) |
| 1748 | { |
| 1749 | if ((serv->tcpfd = socket(serv->addr.sa.sa_family, SOCK_STREAM, 0)) == -1) |
| 1750 | continue; |
| 1751 | |
| 1752 | #ifdef HAVE_CONNTRACK |
| 1753 | /* Copy connection mark of incoming query to outgoing connection. */ |
| 1754 | if (have_mark) |
| 1755 | setsockopt(serv->tcpfd, SOL_SOCKET, SO_MARK, &mark, sizeof(unsigned int)); |
| 1756 | #endif |
| 1757 | |
| 1758 | if ((!local_bind(serv->tcpfd, &serv->source_addr, serv->interface, 0, 1))) |
| 1759 | { |
| 1760 | close(serv->tcpfd); |
| 1761 | serv->tcpfd = -1; |
| 1762 | continue; |
| 1763 | } |
| 1764 | |
| 1765 | #ifdef MSG_FASTOPEN |
| 1766 | server_send(serv, serv->tcpfd, packet, qsize + sizeof(u16), MSG_FASTOPEN); |
| 1767 | |
| 1768 | if (errno == 0) |
| 1769 | data_sent = 1; |
| 1770 | #endif |
| 1771 | |
| 1772 | if (!data_sent && connect(serv->tcpfd, &serv->addr.sa, sa_len(&serv->addr)) == -1) |
| 1773 | { |
| 1774 | close(serv->tcpfd); |
| 1775 | serv->tcpfd = -1; |
| 1776 | continue; |
| 1777 | } |
| 1778 | |
| 1779 | daemon->serverarray[first]->last_server = start; |
| 1780 | serv->flags &= ~SERV_GOT_TCP; |
| 1781 | } |
| 1782 | |
| 1783 | if ((!data_sent && !read_write(serv->tcpfd, packet, qsize + sizeof(u16), 0)) || |
| 1784 | !read_write(serv->tcpfd, &c1, 1, 1) || |
| 1785 | !read_write(serv->tcpfd, &c2, 1, 1) || |
| 1786 | !read_write(serv->tcpfd, payload, (rsize = (c1 << 8) | c2), 1)) |
| 1787 | { |
| 1788 | close(serv->tcpfd); |
| 1789 | serv->tcpfd = -1; |
| 1790 | /* We get data then EOF, reopen connection to same server, |
| 1791 | else try next. This avoids DoS from a server which accepts |
| 1792 | connections and then closes them. */ |
| 1793 | if (serv->flags & SERV_GOT_TCP) |
| 1794 | goto retry; |
| 1795 | else |
| 1796 | continue; |
| 1797 | } |
| 1798 | |
| 1799 | /* If the hash of the question section doesn't match the crc we sent, then |
| 1800 | someone might be attempting to insert bogus values into the cache by |
| 1801 | sending replies containing questions and bogus answers. |
| 1802 | Try another server, or give up */ |
| 1803 | if (memcmp(hash, hash_questions(header, rsize, daemon->namebuff), HASH_SIZE) != 0) |
| 1804 | continue; |
| 1805 | |
| 1806 | serv->flags |= SERV_GOT_TCP; |
| 1807 | |
| 1808 | *servp = serv; |
| 1809 | return rsize; |
| 1810 | } |
| 1811 | |
| 1812 | return 0; |
| 1813 | } |
| 1814 | |
| 1815 | #ifdef HAVE_DNSSEC |
| 1816 | /* Recurse down the key hierarchy */ |
| 1817 | static int tcp_key_recurse(time_t now, int status, struct dns_header *header, size_t n, |
| 1818 | int class, char *name, char *keyname, struct server *server, |
| 1819 | int have_mark, unsigned int mark, int *keycount) |
| 1820 | { |
| 1821 | int first, last, start, new_status; |
| 1822 | unsigned char *packet = NULL; |
| 1823 | struct dns_header *new_header = NULL; |
| 1824 | |
| 1825 | while (1) |
| 1826 | { |
| 1827 | size_t m; |
| 1828 | int log_save; |
| 1829 | |
| 1830 | /* limit the amount of work we do, to avoid cycling forever on loops in the DNS */ |
| 1831 | if (--(*keycount) == 0) |
| 1832 | new_status = STAT_ABANDONED; |
| 1833 | else if (STAT_ISEQUAL(status, STAT_NEED_KEY)) |
| 1834 | new_status = dnssec_validate_by_ds(now, header, n, name, keyname, class); |
| 1835 | else if (STAT_ISEQUAL(status, STAT_NEED_DS)) |
| 1836 | new_status = dnssec_validate_ds(now, header, n, name, keyname, class); |
| 1837 | else |
| 1838 | new_status = dnssec_validate_reply(now, header, n, name, keyname, &class, |
| 1839 | !option_bool(OPT_DNSSEC_IGN_NS) && (server->flags & SERV_DO_DNSSEC), |
| 1840 | NULL, NULL, NULL); |
| 1841 | |
| 1842 | if (!STAT_ISEQUAL(new_status, STAT_NEED_DS) && !STAT_ISEQUAL(new_status, STAT_NEED_KEY)) |
| 1843 | break; |
| 1844 | |
| 1845 | /* Can't validate because we need a key/DS whose name now in keyname. |
| 1846 | Make query for same, and recurse to validate */ |
| 1847 | if (!packet) |
| 1848 | { |
| 1849 | packet = whine_malloc(65536 + MAXDNAME + RRFIXEDSZ + sizeof(u16)); |
| 1850 | new_header = (struct dns_header *)&packet[2]; |
| 1851 | } |
| 1852 | |
| 1853 | if (!packet) |
| 1854 | { |
| 1855 | new_status = STAT_ABANDONED; |
| 1856 | break; |
| 1857 | } |
| 1858 | |
| 1859 | m = dnssec_generate_query(new_header, ((unsigned char *) new_header) + 65536, keyname, class, |
| 1860 | STAT_ISEQUAL(new_status, STAT_NEED_KEY) ? T_DNSKEY : T_DS, server->edns_pktsz); |
| 1861 | |
| 1862 | if ((start = dnssec_server(server, daemon->keyname, &first, &last)) == -1 || |
| 1863 | (m = tcp_talk(first, last, start, packet, m, have_mark, mark, &server)) == 0) |
| 1864 | { |
| 1865 | new_status = STAT_ABANDONED; |
| 1866 | break; |
| 1867 | } |
| 1868 | |
| 1869 | log_save = daemon->log_display_id; |
| 1870 | daemon->log_display_id = ++daemon->log_id; |
| 1871 | |
| 1872 | log_query_mysockaddr(F_NOEXTRA | F_DNSSEC, keyname, &server->addr, |
| 1873 | querystr("dnssec-query", STAT_ISEQUAL(new_status, STAT_NEED_KEY) ? T_DNSKEY : T_DS)); |
| 1874 | |
| 1875 | new_status = tcp_key_recurse(now, new_status, new_header, m, class, name, keyname, server, have_mark, mark, keycount); |
| 1876 | |
| 1877 | daemon->log_display_id = log_save; |
| 1878 | |
| 1879 | if (!STAT_ISEQUAL(new_status, STAT_OK)) |
| 1880 | break; |
| 1881 | } |
| 1882 | |
| 1883 | if (packet) |
| 1884 | free(packet); |
| 1885 | |
| 1886 | return new_status; |
| 1887 | } |
| 1888 | #endif |
| 1889 | |
| 1890 | |
| 1891 | /* The daemon forks before calling this: it should deal with one connection, |
| 1892 | blocking as necessary, and then return. Note, need to be a bit careful |
| 1893 | about resources for debug mode, when the fork is suppressed: that's |
| 1894 | done by the caller. */ |
| 1895 | unsigned char *tcp_request(int confd, time_t now, |
| 1896 | union mysockaddr *local_addr, struct in_addr netmask, int auth_dns) |
| 1897 | { |
| 1898 | size_t size = 0; |
| 1899 | int norebind; |
| 1900 | #ifdef HAVE_CONNTRACK |
| 1901 | int is_single_query = 0, allowed = 1; |
| 1902 | #endif |
| 1903 | #ifdef HAVE_AUTH |
| 1904 | int local_auth = 0; |
| 1905 | #endif |
| 1906 | int checking_disabled, do_bit, added_pheader = 0, have_pseudoheader = 0; |
| 1907 | int check_subnet, cacheable, no_cache_dnssec = 0, cache_secure = 0, bogusanswer = 0; |
| 1908 | size_t m; |
| 1909 | unsigned short qtype; |
| 1910 | unsigned int gotname; |
| 1911 | /* Max TCP packet + slop + size */ |
| 1912 | unsigned char *packet = whine_malloc(65536 + MAXDNAME + RRFIXEDSZ + sizeof(u16)); |
| 1913 | unsigned char *payload = &packet[2]; |
| 1914 | unsigned char c1, c2; |
| 1915 | /* largest field in header is 16-bits, so this is still sufficiently aligned */ |
| 1916 | struct dns_header *header = (struct dns_header *)payload; |
| 1917 | u16 *length = (u16 *)packet; |
| 1918 | struct server *serv; |
| 1919 | struct in_addr dst_addr_4; |
| 1920 | union mysockaddr peer_addr; |
| 1921 | socklen_t peer_len = sizeof(union mysockaddr); |
| 1922 | int query_count = 0; |
| 1923 | unsigned char *pheader; |
| 1924 | unsigned int mark = 0; |
| 1925 | int have_mark = 0; |
| 1926 | int first, last; |
| 1927 | unsigned int flags = 0; |
| 1928 | |
| 1929 | if (getpeername(confd, (struct sockaddr *)&peer_addr, &peer_len) == -1) |
| 1930 | return packet; |
| 1931 | |
| 1932 | #ifdef HAVE_CONNTRACK |
| 1933 | /* Get connection mark of incoming query to set on outgoing connections. */ |
| 1934 | if (option_bool(OPT_CONNTRACK) || option_bool(OPT_CMARK_ALST_EN)) |
| 1935 | { |
| 1936 | union all_addr local; |
| 1937 | |
| 1938 | if (local_addr->sa.sa_family == AF_INET6) |
| 1939 | local.addr6 = local_addr->in6.sin6_addr; |
| 1940 | else |
| 1941 | local.addr4 = local_addr->in.sin_addr; |
| 1942 | |
| 1943 | have_mark = get_incoming_mark(&peer_addr, &local, 1, &mark); |
| 1944 | } |
| 1945 | #endif |
| 1946 | |
| 1947 | /* We can be configured to only accept queries from at-most-one-hop-away addresses. */ |
| 1948 | if (option_bool(OPT_LOCAL_SERVICE)) |
| 1949 | { |
| 1950 | struct addrlist *addr; |
| 1951 | |
| 1952 | if (peer_addr.sa.sa_family == AF_INET6) |
| 1953 | { |
| 1954 | for (addr = daemon->interface_addrs; addr; addr = addr->next) |
| 1955 | if ((addr->flags & ADDRLIST_IPV6) && |
| 1956 | is_same_net6(&addr->addr.addr6, &peer_addr.in6.sin6_addr, addr->prefixlen)) |
| 1957 | break; |
| 1958 | } |
| 1959 | else |
| 1960 | { |
| 1961 | struct in_addr netmask; |
| 1962 | for (addr = daemon->interface_addrs; addr; addr = addr->next) |
| 1963 | { |
| 1964 | netmask.s_addr = htonl(~(in_addr_t)0 << (32 - addr->prefixlen)); |
| 1965 | if (!(addr->flags & ADDRLIST_IPV6) && |
| 1966 | is_same_net(addr->addr.addr4, peer_addr.in.sin_addr, netmask)) |
| 1967 | break; |
| 1968 | } |
| 1969 | } |
| 1970 | if (!addr) |
| 1971 | { |
| 1972 | my_syslog(LOG_WARNING, _("Ignoring query from non-local network")); |
| 1973 | return packet; |
| 1974 | } |
| 1975 | } |
| 1976 | |
| 1977 | while (1) |
| 1978 | { |
| 1979 | int ede = EDE_UNSET; |
| 1980 | |
| 1981 | if (query_count == TCP_MAX_QUERIES || |
| 1982 | !packet || |
| 1983 | !read_write(confd, &c1, 1, 1) || !read_write(confd, &c2, 1, 1) || |
| 1984 | !(size = c1 << 8 | c2) || |
| 1985 | !read_write(confd, payload, size, 1)) |
| 1986 | return packet; |
| 1987 | |
| 1988 | if (size < (int)sizeof(struct dns_header)) |
| 1989 | continue; |
| 1990 | |
| 1991 | /* Clear buffer beyond request to avoid risk of |
| 1992 | information disclosure. */ |
| 1993 | memset(payload + size, 0, 65536 - size); |
| 1994 | |
| 1995 | query_count++; |
| 1996 | |
| 1997 | /* log_query gets called indirectly all over the place, so |
| 1998 | pass these in global variables - sorry. */ |
| 1999 | daemon->log_display_id = ++daemon->log_id; |
| 2000 | daemon->log_source_addr = &peer_addr; |
| 2001 | |
| 2002 | /* save state of "cd" flag in query */ |
| 2003 | if ((checking_disabled = header->hb4 & HB4_CD)) |
| 2004 | no_cache_dnssec = 1; |
| 2005 | |
| 2006 | if ((gotname = extract_request(header, (unsigned int)size, daemon->namebuff, &qtype))) |
| 2007 | { |
| 2008 | #ifdef HAVE_AUTH |
| 2009 | struct auth_zone *zone; |
| 2010 | #endif |
| 2011 | char *types = querystr(auth_dns ? "auth" : "query", qtype); |
| 2012 | |
| 2013 | log_query_mysockaddr(F_QUERY | F_FORWARD, daemon->namebuff, |
| 2014 | &peer_addr, types); |
| 2015 | |
| 2016 | #ifdef HAVE_CONNTRACK |
| 2017 | is_single_query = 1; |
| 2018 | #endif |
| 2019 | |
| 2020 | #ifdef HAVE_AUTH |
| 2021 | /* find queries for zones we're authoritative for, and answer them directly */ |
| 2022 | if (!auth_dns && !option_bool(OPT_LOCALISE)) |
| 2023 | for (zone = daemon->auth_zones; zone; zone = zone->next) |
| 2024 | if (in_zone(zone, daemon->namebuff, NULL)) |
| 2025 | { |
| 2026 | auth_dns = 1; |
| 2027 | local_auth = 1; |
| 2028 | break; |
| 2029 | } |
| 2030 | #endif |
| 2031 | } |
| 2032 | |
| 2033 | norebind = domain_no_rebind(daemon->namebuff); |
| 2034 | |
| 2035 | if (local_addr->sa.sa_family == AF_INET) |
| 2036 | dst_addr_4 = local_addr->in.sin_addr; |
| 2037 | else |
| 2038 | dst_addr_4.s_addr = 0; |
| 2039 | |
| 2040 | do_bit = 0; |
| 2041 | |
| 2042 | if (find_pseudoheader(header, (size_t)size, NULL, &pheader, NULL, NULL)) |
| 2043 | { |
| 2044 | unsigned short flags; |
| 2045 | |
| 2046 | have_pseudoheader = 1; |
| 2047 | pheader += 4; /* udp_size, ext_rcode */ |
| 2048 | GETSHORT(flags, pheader); |
| 2049 | |
| 2050 | if (flags & 0x8000) |
| 2051 | do_bit = 1; /* do bit */ |
| 2052 | } |
| 2053 | |
| 2054 | #ifdef HAVE_CONNTRACK |
| 2055 | #ifdef HAVE_AUTH |
| 2056 | if (!auth_dns || local_auth) |
| 2057 | #endif |
| 2058 | if (option_bool(OPT_CMARK_ALST_EN) && have_mark && ((u32)mark & daemon->allowlist_mask)) |
| 2059 | allowed = is_query_allowed_for_mark((u32)mark, is_single_query ? daemon->namebuff : NULL); |
| 2060 | #endif |
| 2061 | |
| 2062 | if (0); |
| 2063 | #ifdef HAVE_CONNTRACK |
| 2064 | else if (!allowed) |
| 2065 | { |
| 2066 | u16 swap = htons(EDE_BLOCKED); |
| 2067 | |
| 2068 | m = answer_disallowed(header, size, (u32)mark, is_single_query ? daemon->namebuff : NULL); |
| 2069 | |
| 2070 | if (have_pseudoheader && m != 0) |
| 2071 | m = add_pseudoheader(header, m, ((unsigned char *) header) + 65536, daemon->edns_pktsz, |
| 2072 | EDNS0_OPTION_EDE, (unsigned char *)&swap, 2, do_bit, 0); |
| 2073 | } |
| 2074 | #endif |
| 2075 | #ifdef HAVE_AUTH |
| 2076 | else if (auth_dns) |
| 2077 | m = answer_auth(header, ((char *) header) + 65536, (size_t)size, now, &peer_addr, |
| 2078 | local_auth, do_bit, have_pseudoheader); |
| 2079 | #endif |
| 2080 | else |
| 2081 | { |
| 2082 | int ad_reqd = do_bit; |
| 2083 | /* RFC 6840 5.7 */ |
| 2084 | if (header->hb4 & HB4_AD) |
| 2085 | ad_reqd = 1; |
| 2086 | |
| 2087 | /* m > 0 if answered from cache */ |
| 2088 | m = answer_request(header, ((char *) header) + 65536, (size_t)size, |
| 2089 | dst_addr_4, netmask, now, ad_reqd, do_bit, have_pseudoheader); |
| 2090 | |
| 2091 | /* Do this by steam now we're not in the select() loop */ |
| 2092 | check_log_writer(1); |
| 2093 | |
| 2094 | if (m == 0) |
| 2095 | { |
| 2096 | struct server *master; |
| 2097 | int start; |
| 2098 | |
| 2099 | if (lookup_domain(daemon->namebuff, gotname, &first, &last)) |
| 2100 | flags = is_local_answer(now, first, daemon->namebuff); |
| 2101 | else |
| 2102 | { |
| 2103 | /* No configured servers */ |
| 2104 | ede = EDE_NOT_READY; |
| 2105 | flags = 0; |
| 2106 | } |
| 2107 | |
| 2108 | /* don't forward A or AAAA queries for simple names, except the empty name */ |
| 2109 | if (!flags && |
| 2110 | option_bool(OPT_NODOTS_LOCAL) && |
| 2111 | (gotname & (F_IPV4 | F_IPV6)) && |
| 2112 | !strchr(daemon->namebuff, '.') && |
| 2113 | strlen(daemon->namebuff) != 0) |
| 2114 | flags = check_for_local_domain(daemon->namebuff, now) ? F_NOERR : F_NXDOMAIN; |
| 2115 | |
| 2116 | if (!flags && ede != EDE_NOT_READY) |
| 2117 | { |
| 2118 | master = daemon->serverarray[first]; |
| 2119 | |
| 2120 | if (option_bool(OPT_ORDER) || master->last_server == -1) |
| 2121 | start = first; |
| 2122 | else |
| 2123 | start = master->last_server; |
| 2124 | |
| 2125 | size = add_edns0_config(header, size, ((unsigned char *) header) + 65536, &peer_addr, now, &check_subnet, &cacheable); |
| 2126 | |
| 2127 | #ifdef HAVE_DNSSEC |
| 2128 | if (option_bool(OPT_DNSSEC_VALID) && (master->flags & SERV_DO_DNSSEC)) |
| 2129 | { |
| 2130 | size = add_do_bit(header, size, ((unsigned char *) header) + 65536); |
| 2131 | |
| 2132 | /* For debugging, set Checking Disabled, otherwise, have the upstream check too, |
| 2133 | this allows it to select auth servers when one is returning bad data. */ |
| 2134 | if (option_bool(OPT_DNSSEC_DEBUG)) |
| 2135 | header->hb4 |= HB4_CD; |
| 2136 | } |
| 2137 | #endif |
| 2138 | |
| 2139 | /* Check if we added a pheader on forwarding - may need to |
| 2140 | strip it from the reply. */ |
| 2141 | if (!have_pseudoheader && find_pseudoheader(header, size, NULL, NULL, NULL, NULL)) |
| 2142 | added_pheader = 1; |
| 2143 | |
| 2144 | /* Loop round available servers until we succeed in connecting to one. */ |
| 2145 | if ((m = tcp_talk(first, last, start, packet, size, have_mark, mark, &serv)) == 0) |
| 2146 | { |
| 2147 | ede = EDE_NETERR; |
| 2148 | break; |
| 2149 | } |
| 2150 | |
| 2151 | /* get query name again for logging - may have been overwritten */ |
| 2152 | if (!(gotname = extract_request(header, (unsigned int)size, daemon->namebuff, &qtype))) |
| 2153 | strcpy(daemon->namebuff, "query"); |
| 2154 | log_query_mysockaddr(F_SERVER | F_FORWARD, daemon->namebuff, &serv->addr, NULL); |
| 2155 | |
| 2156 | #ifdef HAVE_DNSSEC |
| 2157 | if (option_bool(OPT_DNSSEC_VALID) && !checking_disabled && (master->flags & SERV_DO_DNSSEC)) |
| 2158 | { |
| 2159 | int keycount = DNSSEC_WORK; /* Limit to number of DNSSEC questions, to catch loops and avoid filling cache. */ |
| 2160 | int status = tcp_key_recurse(now, STAT_OK, header, m, 0, daemon->namebuff, daemon->keyname, |
| 2161 | serv, have_mark, mark, &keycount); |
| 2162 | char *result, *domain = "result"; |
| 2163 | |
| 2164 | union all_addr a; |
| 2165 | a.log.ede = ede = errflags_to_ede(status); |
| 2166 | |
| 2167 | if (STAT_ISEQUAL(status, STAT_ABANDONED)) |
| 2168 | { |
| 2169 | result = "ABANDONED"; |
| 2170 | status = STAT_BOGUS; |
| 2171 | } |
| 2172 | else |
| 2173 | result = (STAT_ISEQUAL(status, STAT_SECURE) ? "SECURE" : (STAT_ISEQUAL(status, STAT_INSECURE) ? "INSECURE" : "BOGUS")); |
| 2174 | |
| 2175 | if (STAT_ISEQUAL(status, STAT_SECURE)) |
| 2176 | cache_secure = 1; |
| 2177 | else if (STAT_ISEQUAL(status, STAT_BOGUS)) |
| 2178 | { |
| 2179 | no_cache_dnssec = 1; |
| 2180 | bogusanswer = 1; |
| 2181 | |
| 2182 | if (extract_request(header, m, daemon->namebuff, NULL)) |
| 2183 | domain = daemon->namebuff; |
| 2184 | } |
| 2185 | |
| 2186 | log_query(F_SECSTAT, domain, &a, result); |
| 2187 | } |
| 2188 | #endif |
| 2189 | |
| 2190 | /* restore CD bit to the value in the query */ |
| 2191 | if (checking_disabled) |
| 2192 | header->hb4 |= HB4_CD; |
| 2193 | else |
| 2194 | header->hb4 &= ~HB4_CD; |
| 2195 | |
| 2196 | /* Never cache answers which are contingent on the source or MAC address EDSN0 option, |
| 2197 | since the cache is ignorant of such things. */ |
| 2198 | if (!cacheable) |
| 2199 | no_cache_dnssec = 1; |
| 2200 | |
| 2201 | m = process_reply(header, now, serv, (unsigned int)m, |
| 2202 | option_bool(OPT_NO_REBIND) && !norebind, no_cache_dnssec, cache_secure, bogusanswer, |
| 2203 | ad_reqd, do_bit, added_pheader, check_subnet, &peer_addr, ((unsigned char *)header) + 65536, ede); |
| 2204 | } |
| 2205 | } |
| 2206 | } |
| 2207 | |
| 2208 | /* In case of local answer or no connections made. */ |
| 2209 | if (m == 0) |
| 2210 | { |
| 2211 | if (!(m = make_local_answer(flags, gotname, size, header, daemon->namebuff, |
| 2212 | ((char *) header) + 65536, first, last, ede))) |
| 2213 | break; |
| 2214 | |
| 2215 | if (have_pseudoheader) |
| 2216 | { |
| 2217 | u16 swap = htons((u16)ede); |
| 2218 | |
| 2219 | if (ede != EDE_UNSET) |
| 2220 | m = add_pseudoheader(header, m, ((unsigned char *) header) + 65536, daemon->edns_pktsz, EDNS0_OPTION_EDE, (unsigned char *)&swap, 2, do_bit, 0); |
| 2221 | else |
| 2222 | m = add_pseudoheader(header, m, ((unsigned char *) header) + 65536, daemon->edns_pktsz, 0, NULL, 0, do_bit, 0); |
| 2223 | } |
| 2224 | } |
| 2225 | |
| 2226 | check_log_writer(1); |
| 2227 | |
| 2228 | *length = htons(m); |
| 2229 | |
| 2230 | #if defined(HAVE_CONNTRACK) && defined(HAVE_UBUS) |
| 2231 | #ifdef HAVE_AUTH |
| 2232 | if (!auth_dns || local_auth) |
| 2233 | #endif |
| 2234 | if (option_bool(OPT_CMARK_ALST_EN) && have_mark && ((u32)mark & daemon->allowlist_mask)) |
| 2235 | report_addresses(header, m, mark); |
| 2236 | #endif |
| 2237 | if (!read_write(confd, packet, m + sizeof(u16), 0)) |
| 2238 | break; |
| 2239 | } |
| 2240 | |
| 2241 | return packet; |
| 2242 | } |
| 2243 | |
| 2244 | /* return a UDP socket bound to a random port, have to cope with straying into |
| 2245 | occupied port nos and reserved ones. */ |
| 2246 | static int random_sock(struct server *s) |
| 2247 | { |
| 2248 | int fd; |
| 2249 | |
| 2250 | if ((fd = socket(s->source_addr.sa.sa_family, SOCK_DGRAM, 0)) != -1) |
| 2251 | { |
| 2252 | if (local_bind(fd, &s->source_addr, s->interface, s->ifindex, 0)) |
| 2253 | return fd; |
| 2254 | |
| 2255 | if (s->interface[0] == 0) |
| 2256 | (void)prettyprint_addr(&s->source_addr, daemon->namebuff); |
| 2257 | else |
| 2258 | strcpy(daemon->namebuff, s->interface); |
| 2259 | |
| 2260 | my_syslog(LOG_ERR, _("failed to bind server socket to %s: %s"), |
| 2261 | daemon->namebuff, strerror(errno)); |
| 2262 | close(fd); |
| 2263 | } |
| 2264 | |
| 2265 | return -1; |
| 2266 | } |
| 2267 | |
| 2268 | /* compare source addresses and interface, serv2 can be null. */ |
| 2269 | static int server_isequal(const struct server *serv1, |
| 2270 | const struct server *serv2) |
| 2271 | { |
| 2272 | return (serv2 && |
| 2273 | serv2->ifindex == serv1->ifindex && |
| 2274 | sockaddr_isequal(&serv2->source_addr, &serv1->source_addr) && |
| 2275 | strncmp(serv2->interface, serv1->interface, IF_NAMESIZE) == 0); |
| 2276 | } |
| 2277 | |
| 2278 | /* fdlp points to chain of randomfds already in use by transaction. |
| 2279 | If there's already a suitable one, return it, else allocate a |
| 2280 | new one and add it to the list. |
| 2281 | |
| 2282 | Not leaking any resources in the face of allocation failures |
| 2283 | is rather convoluted here. |
| 2284 | |
| 2285 | Note that rfd->serv may be NULL, when a server goes away. |
| 2286 | */ |
| 2287 | int allocate_rfd(struct randfd_list **fdlp, struct server *serv) |
| 2288 | { |
| 2289 | static int finger = 0; |
| 2290 | int i, j = 0; |
| 2291 | struct randfd_list *rfl; |
| 2292 | struct randfd *rfd = NULL; |
| 2293 | int fd = 0; |
| 2294 | |
| 2295 | /* If server has a pre-allocated fd, use that. */ |
| 2296 | if (serv->sfd) |
| 2297 | return serv->sfd->fd; |
| 2298 | |
| 2299 | /* existing suitable random port socket linked to this transaction? */ |
| 2300 | for (rfl = *fdlp; rfl; rfl = rfl->next) |
| 2301 | if (server_isequal(serv, rfl->rfd->serv)) |
| 2302 | return rfl->rfd->fd; |
| 2303 | |
| 2304 | /* No. need new link. */ |
| 2305 | if ((rfl = daemon->rfl_spare)) |
| 2306 | daemon->rfl_spare = rfl->next; |
| 2307 | else if (!(rfl = whine_malloc(sizeof(struct randfd_list)))) |
| 2308 | return -1; |
| 2309 | |
| 2310 | /* limit the number of sockets we have open to avoid starvation of |
| 2311 | (eg) TFTP. Once we have a reasonable number, randomness should be OK */ |
| 2312 | for (i = 0; i < daemon->numrrand; i++) |
| 2313 | if (daemon->randomsocks[i].refcount == 0) |
| 2314 | { |
| 2315 | if ((fd = random_sock(serv)) != -1) |
| 2316 | { |
| 2317 | rfd = &daemon->randomsocks[i]; |
| 2318 | rfd->serv = serv; |
| 2319 | rfd->fd = fd; |
| 2320 | rfd->refcount = 1; |
| 2321 | } |
| 2322 | break; |
| 2323 | } |
| 2324 | |
| 2325 | /* No free ones or cannot get new socket, grab an existing one */ |
| 2326 | if (!rfd) |
| 2327 | for (j = 0; j < daemon->numrrand; j++) |
| 2328 | { |
| 2329 | i = (j + finger) % daemon->numrrand; |
| 2330 | if (daemon->randomsocks[i].refcount != 0 && |
| 2331 | server_isequal(serv, daemon->randomsocks[i].serv) && |
| 2332 | daemon->randomsocks[i].refcount != 0xfffe) |
| 2333 | { |
| 2334 | finger = i + 1; |
| 2335 | rfd = &daemon->randomsocks[i]; |
| 2336 | rfd->refcount++; |
| 2337 | break; |
| 2338 | } |
| 2339 | } |
| 2340 | |
| 2341 | if (j == daemon->numrrand) |
| 2342 | { |
| 2343 | struct randfd_list *rfl_poll; |
| 2344 | |
| 2345 | /* there are no free slots, and non with the same parameters we can piggy-back on. |
| 2346 | We're going to have to allocate a new temporary record, distinguished by |
| 2347 | refcount == 0xffff. This will exist in the frec randfd list, never be shared, |
| 2348 | and be freed when no longer in use. It will also be held on |
| 2349 | the daemon->rfl_poll list so the poll system can find it. */ |
| 2350 | |
| 2351 | if ((rfl_poll = daemon->rfl_spare)) |
| 2352 | daemon->rfl_spare = rfl_poll->next; |
| 2353 | else |
| 2354 | rfl_poll = whine_malloc(sizeof(struct randfd_list)); |
| 2355 | |
| 2356 | if (!rfl_poll || |
| 2357 | !(rfd = whine_malloc(sizeof(struct randfd))) || |
| 2358 | (fd = random_sock(serv)) == -1) |
| 2359 | { |
| 2360 | |
| 2361 | /* Don't leak anything we may already have */ |
| 2362 | rfl->next = daemon->rfl_spare; |
| 2363 | daemon->rfl_spare = rfl; |
| 2364 | |
| 2365 | if (rfl_poll) |
| 2366 | { |
| 2367 | rfl_poll->next = daemon->rfl_spare; |
| 2368 | daemon->rfl_spare = rfl_poll; |
| 2369 | } |
| 2370 | |
| 2371 | if (rfd) |
| 2372 | free(rfd); |
| 2373 | |
| 2374 | return -1; /* doom */ |
| 2375 | } |
| 2376 | |
| 2377 | /* Note rfd->serv not set here, since it's not reused */ |
| 2378 | rfd->fd = fd; |
| 2379 | rfd->refcount = 0xffff; /* marker for temp record */ |
| 2380 | |
| 2381 | rfl_poll->rfd = rfd; |
| 2382 | rfl_poll->next = daemon->rfl_poll; |
| 2383 | daemon->rfl_poll = rfl_poll; |
| 2384 | } |
| 2385 | |
| 2386 | rfl->rfd = rfd; |
| 2387 | rfl->next = *fdlp; |
| 2388 | *fdlp = rfl; |
| 2389 | |
| 2390 | return rfl->rfd->fd; |
| 2391 | } |
| 2392 | |
| 2393 | void free_rfds(struct randfd_list **fdlp) |
| 2394 | { |
| 2395 | struct randfd_list *tmp, *rfl, *poll, *next, **up; |
| 2396 | |
| 2397 | for (rfl = *fdlp; rfl; rfl = tmp) |
| 2398 | { |
| 2399 | if (rfl->rfd->refcount == 0xffff || --(rfl->rfd->refcount) == 0) |
| 2400 | close(rfl->rfd->fd); |
| 2401 | |
| 2402 | /* temporary overflow record */ |
| 2403 | if (rfl->rfd->refcount == 0xffff) |
| 2404 | { |
| 2405 | free(rfl->rfd); |
| 2406 | |
| 2407 | /* go through the link of all these by steam to delete. |
| 2408 | This list is expected to be almost always empty. */ |
| 2409 | for (poll = daemon->rfl_poll, up = &daemon->rfl_poll; poll; poll = next) |
| 2410 | { |
| 2411 | next = poll->next; |
| 2412 | |
| 2413 | if (poll->rfd == rfl->rfd) |
| 2414 | { |
| 2415 | *up = poll->next; |
| 2416 | poll->next = daemon->rfl_spare; |
| 2417 | daemon->rfl_spare = poll; |
| 2418 | } |
| 2419 | else |
| 2420 | up = &poll->next; |
| 2421 | } |
| 2422 | } |
| 2423 | |
| 2424 | tmp = rfl->next; |
| 2425 | rfl->next = daemon->rfl_spare; |
| 2426 | daemon->rfl_spare = rfl; |
| 2427 | } |
| 2428 | |
| 2429 | *fdlp = NULL; |
| 2430 | } |
| 2431 | |
| 2432 | static void free_frec(struct frec *f) |
| 2433 | { |
| 2434 | struct frec_src *last; |
| 2435 | |
| 2436 | /* add back to freelist if not the record builtin to every frec. */ |
| 2437 | for (last = f->frec_src.next; last && last->next; last = last->next) ; |
| 2438 | if (last) |
| 2439 | { |
| 2440 | last->next = daemon->free_frec_src; |
| 2441 | daemon->free_frec_src = f->frec_src.next; |
| 2442 | } |
| 2443 | |
| 2444 | f->frec_src.next = NULL; |
| 2445 | free_rfds(&f->rfds); |
| 2446 | f->sentto = NULL; |
| 2447 | f->flags = 0; |
| 2448 | |
| 2449 | #ifdef HAVE_DNSSEC |
| 2450 | if (f->stash) |
| 2451 | { |
| 2452 | blockdata_free(f->stash); |
| 2453 | f->stash = NULL; |
| 2454 | } |
| 2455 | |
| 2456 | /* Anything we're waiting on is pointless now, too */ |
| 2457 | if (f->blocking_query) |
| 2458 | { |
| 2459 | struct frec *n, **up; |
| 2460 | |
| 2461 | /* unlink outselves from the blocking query's dependents list. */ |
| 2462 | for (n = f->blocking_query->dependent, up = &f->blocking_query->dependent; n; n = n->next_dependent) |
| 2463 | if (n == f) |
| 2464 | { |
| 2465 | *up = n->next_dependent; |
| 2466 | break; |
| 2467 | } |
| 2468 | else |
| 2469 | up = &n->next_dependent; |
| 2470 | |
| 2471 | /* If we were the only/last dependent, free the blocking query too. */ |
| 2472 | if (!f->blocking_query->dependent) |
| 2473 | free_frec(f->blocking_query); |
| 2474 | } |
| 2475 | |
| 2476 | f->blocking_query = NULL; |
| 2477 | f->dependent = NULL; |
| 2478 | f->next_dependent = NULL; |
| 2479 | #endif |
| 2480 | } |
| 2481 | |
| 2482 | |
| 2483 | |
| 2484 | /* Impose an absolute |
| 2485 | limit of 4*TIMEOUT before we wipe things (for random sockets). |
| 2486 | If force is set, always return a result, even if we have |
| 2487 | to allocate above the limit, and don'y free any records. |
| 2488 | This is set when allocating for DNSSEC to avoid cutting off |
| 2489 | the branch we are sitting on. */ |
| 2490 | static struct frec *get_new_frec(time_t now, struct server *master, int force) |
| 2491 | { |
| 2492 | struct frec *f, *oldest, *target; |
| 2493 | int count; |
| 2494 | |
| 2495 | /* look for free records, garbage collect old records and count number in use by our server-group. */ |
| 2496 | for (f = daemon->frec_list, oldest = NULL, target = NULL, count = 0; f; f = f->next) |
| 2497 | { |
| 2498 | if (!f->sentto) |
| 2499 | target = f; |
| 2500 | else |
| 2501 | { |
| 2502 | #ifdef HAVE_DNSSEC |
| 2503 | /* Don't free DNSSEC sub-queries here, as we may end up with |
| 2504 | dangling references to them. They'll go when their "real" query |
| 2505 | is freed. */ |
| 2506 | if (!f->dependent && !force) |
| 2507 | #endif |
| 2508 | { |
| 2509 | if (difftime(now, f->time) >= 4*TIMEOUT) |
| 2510 | { |
| 2511 | free_frec(f); |
| 2512 | target = f; |
| 2513 | } |
| 2514 | else if (!oldest || difftime(f->time, oldest->time) <= 0) |
| 2515 | oldest = f; |
| 2516 | } |
| 2517 | } |
| 2518 | |
| 2519 | if (f->sentto && ((int)difftime(now, f->time)) < TIMEOUT && server_samegroup(f->sentto, master)) |
| 2520 | count++; |
| 2521 | } |
| 2522 | |
| 2523 | if (!force && count >= daemon->ftabsize) |
| 2524 | { |
| 2525 | query_full(now, master->domain); |
| 2526 | return NULL; |
| 2527 | } |
| 2528 | |
| 2529 | if (!target && oldest && ((int)difftime(now, oldest->time)) >= TIMEOUT) |
| 2530 | { |
| 2531 | /* can't find empty one, use oldest if there is one and it's older than timeout */ |
| 2532 | free_frec(oldest); |
| 2533 | target = oldest; |
| 2534 | } |
| 2535 | |
| 2536 | if (!target && (target = (struct frec *)whine_malloc(sizeof(struct frec)))) |
| 2537 | { |
| 2538 | target->next = daemon->frec_list; |
| 2539 | daemon->frec_list = target; |
| 2540 | } |
| 2541 | |
| 2542 | if (target) |
| 2543 | target->time = now; |
| 2544 | |
| 2545 | return target; |
| 2546 | } |
| 2547 | |
| 2548 | static void query_full(time_t now, char *domain) |
| 2549 | { |
| 2550 | static time_t last_log = 0; |
| 2551 | |
| 2552 | if ((int)difftime(now, last_log) > 5) |
| 2553 | { |
| 2554 | last_log = now; |
| 2555 | if (!domain || strlen(domain) == 0) |
| 2556 | my_syslog(LOG_WARNING, _("Maximum number of concurrent DNS queries reached (max: %d)"), daemon->ftabsize); |
| 2557 | else |
| 2558 | my_syslog(LOG_WARNING, _("Maximum number of concurrent DNS queries to %s reached (max: %d)"), domain, daemon->ftabsize); |
| 2559 | } |
| 2560 | } |
| 2561 | |
| 2562 | |
| 2563 | static struct frec *lookup_frec(unsigned short id, int fd, void *hash, int *firstp, int *lastp) |
| 2564 | { |
| 2565 | struct frec *f; |
| 2566 | struct server *s; |
| 2567 | int first, last; |
| 2568 | struct randfd_list *fdl; |
| 2569 | |
| 2570 | for(f = daemon->frec_list; f; f = f->next) |
| 2571 | if (f->sentto && f->new_id == id && |
| 2572 | (memcmp(hash, f->hash, HASH_SIZE) == 0)) |
| 2573 | { |
| 2574 | filter_servers(f->sentto->arrayposn, F_SERVER, firstp, lastp); |
| 2575 | |
| 2576 | /* sent from random port */ |
| 2577 | for (fdl = f->rfds; fdl; fdl = fdl->next) |
| 2578 | if (fdl->rfd->fd == fd) |
| 2579 | return f; |
| 2580 | |
| 2581 | /* Sent to upstream from socket associated with a server. |
| 2582 | Note we have to iterate over all the possible servers, since they may |
| 2583 | have different bound sockets. */ |
| 2584 | for (first = *firstp, last = *lastp; first != last; first++) |
| 2585 | { |
| 2586 | s = daemon->serverarray[first]; |
| 2587 | if (s->sfd && s->sfd->fd == fd) |
| 2588 | return f; |
| 2589 | } |
| 2590 | } |
| 2591 | |
| 2592 | return NULL; |
| 2593 | } |
| 2594 | |
| 2595 | static struct frec *lookup_frec_by_query(void *hash, unsigned int flags, unsigned int flagmask) |
| 2596 | { |
| 2597 | struct frec *f; |
| 2598 | |
| 2599 | for(f = daemon->frec_list; f; f = f->next) |
| 2600 | if (f->sentto && |
| 2601 | (f->flags & flagmask) == flags && |
| 2602 | memcmp(hash, f->hash, HASH_SIZE) == 0) |
| 2603 | return f; |
| 2604 | |
| 2605 | return NULL; |
| 2606 | } |
| 2607 | |
| 2608 | /* Send query packet again, if we can. */ |
| 2609 | void resend_query() |
| 2610 | { |
| 2611 | if (daemon->srv_save) |
| 2612 | server_send(daemon->srv_save, daemon->fd_save, |
| 2613 | daemon->packet, daemon->packet_len, 0); |
| 2614 | } |
| 2615 | |
| 2616 | /* A server record is going away, remove references to it */ |
| 2617 | void server_gone(struct server *server) |
| 2618 | { |
| 2619 | struct frec *f; |
| 2620 | int i; |
| 2621 | |
| 2622 | for (f = daemon->frec_list; f; f = f->next) |
| 2623 | if (f->sentto && f->sentto == server) |
| 2624 | free_frec(f); |
| 2625 | |
| 2626 | /* If any random socket refers to this server, NULL the reference. |
| 2627 | No more references to the socket will be created in the future. */ |
| 2628 | for (i = 0; i < daemon->numrrand; i++) |
| 2629 | if (daemon->randomsocks[i].refcount != 0 && daemon->randomsocks[i].serv == server) |
| 2630 | daemon->randomsocks[i].serv = NULL; |
| 2631 | |
| 2632 | if (daemon->srv_save == server) |
| 2633 | daemon->srv_save = NULL; |
| 2634 | } |
| 2635 | |
| 2636 | /* return unique random ids. */ |
| 2637 | static unsigned short get_id(void) |
| 2638 | { |
| 2639 | unsigned short ret = 0; |
| 2640 | struct frec *f; |
| 2641 | |
| 2642 | while (1) |
| 2643 | { |
| 2644 | ret = rand16(); |
| 2645 | |
| 2646 | /* ensure id is unique. */ |
| 2647 | for (f = daemon->frec_list; f; f = f->next) |
| 2648 | if (f->sentto && f->new_id == ret) |
| 2649 | break; |
| 2650 | |
| 2651 | if (!f) |
| 2652 | return ret; |
| 2653 | } |
| 2654 | } |