]> git.ipfire.org Git - people/ms/dnsmasq.git/blob - src/forward.c
First functional DNSSEC - highly alpha.
[people/ms/dnsmasq.git] / src / forward.c
1 /* dnsmasq is Copyright (c) 2000-2013 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 *lookup_frec(unsigned short id, unsigned int crc);
20 static struct frec *lookup_frec_by_sender(unsigned short id,
21 union mysockaddr *addr,
22 unsigned int crc);
23 static unsigned short get_id(unsigned int crc);
24 static void free_frec(struct frec *f);
25 static struct randfd *allocate_rfd(int family);
26
27 /* Send a UDP packet with its source address set as "source"
28 unless nowild is true, when we just send it with the kernel default */
29 int send_from(int fd, int nowild, char *packet, size_t len,
30 union mysockaddr *to, struct all_addr *source,
31 unsigned int iface)
32 {
33 struct msghdr msg;
34 struct iovec iov[1];
35 union {
36 struct cmsghdr align; /* this ensures alignment */
37 #if defined(HAVE_LINUX_NETWORK)
38 char control[CMSG_SPACE(sizeof(struct in_pktinfo))];
39 #elif defined(IP_SENDSRCADDR)
40 char control[CMSG_SPACE(sizeof(struct in_addr))];
41 #endif
42 #ifdef HAVE_IPV6
43 char control6[CMSG_SPACE(sizeof(struct in6_pktinfo))];
44 #endif
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->addr.addr4;
71 memcpy(CMSG_DATA(cmptr), &p, sizeof(p));
72 msg.msg_controllen = cmptr->cmsg_len = CMSG_LEN(sizeof(struct in_pktinfo));
73 cmptr->cmsg_level = IPPROTO_IP;
74 cmptr->cmsg_type = IP_PKTINFO;
75 #elif defined(IP_SENDSRCADDR)
76 memcpy(CMSG_DATA(cmptr), &(source->addr.addr4), sizeof(source->addr.addr4));
77 msg.msg_controllen = cmptr->cmsg_len = CMSG_LEN(sizeof(struct in_addr));
78 cmptr->cmsg_level = IPPROTO_IP;
79 cmptr->cmsg_type = IP_SENDSRCADDR;
80 #endif
81 }
82 else
83 #ifdef HAVE_IPV6
84 {
85 struct in6_pktinfo p;
86 p.ipi6_ifindex = iface; /* Need iface for IPv6 to handle link-local addrs */
87 p.ipi6_addr = source->addr.addr6;
88 memcpy(CMSG_DATA(cmptr), &p, sizeof(p));
89 msg.msg_controllen = cmptr->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
90 cmptr->cmsg_type = daemon->v6pktinfo;
91 cmptr->cmsg_level = IPPROTO_IPV6;
92 }
93 #else
94 (void)iface; /* eliminate warning */
95 #endif
96 }
97
98 while (sendmsg(fd, &msg, 0) == -1)
99 {
100 if (retry_send())
101 continue;
102
103 /* If interface is still in DAD, EINVAL results - ignore that. */
104 if (errno == EINVAL)
105 break;
106
107 my_syslog(LOG_ERR, _("failed to send packet: %s"), strerror(errno));
108 return 0;
109 }
110
111 return 1;
112 }
113
114 static unsigned int search_servers(time_t now, struct all_addr **addrpp,
115 unsigned int qtype, char *qdomain, int *type, char **domain, int *norebind)
116
117 {
118 /* If the query ends in the domain in one of our servers, set
119 domain to point to that name. We find the largest match to allow both
120 domain.org and sub.domain.org to exist. */
121
122 unsigned int namelen = strlen(qdomain);
123 unsigned int matchlen = 0;
124 struct server *serv;
125 unsigned int flags = 0;
126
127 for (serv = daemon->servers; serv; serv=serv->next)
128 /* domain matches take priority over NODOTS matches */
129 if ((serv->flags & SERV_FOR_NODOTS) && *type != SERV_HAS_DOMAIN && !strchr(qdomain, '.') && namelen != 0)
130 {
131 unsigned int sflag = serv->addr.sa.sa_family == AF_INET ? F_IPV4 : F_IPV6;
132 *type = SERV_FOR_NODOTS;
133 if (serv->flags & SERV_NO_ADDR)
134 flags = F_NXDOMAIN;
135 else if (serv->flags & SERV_LITERAL_ADDRESS)
136 {
137 if (sflag & qtype)
138 {
139 flags = sflag;
140 if (serv->addr.sa.sa_family == AF_INET)
141 *addrpp = (struct all_addr *)&serv->addr.in.sin_addr;
142 #ifdef HAVE_IPV6
143 else
144 *addrpp = (struct all_addr *)&serv->addr.in6.sin6_addr;
145 #endif
146 }
147 else if (!flags || (flags & F_NXDOMAIN))
148 flags = F_NOERR;
149 }
150 }
151 else if (serv->flags & SERV_HAS_DOMAIN)
152 {
153 unsigned int domainlen = strlen(serv->domain);
154 char *matchstart = qdomain + namelen - domainlen;
155 if (namelen >= domainlen &&
156 hostname_isequal(matchstart, serv->domain) &&
157 (domainlen == 0 || namelen == domainlen || *(matchstart-1) == '.' ))
158 {
159 if (serv->flags & SERV_NO_REBIND)
160 *norebind = 1;
161 else
162 {
163 unsigned int sflag = serv->addr.sa.sa_family == AF_INET ? F_IPV4 : F_IPV6;
164 /* implement priority rules for --address and --server for same domain.
165 --address wins if the address is for the correct AF
166 --server wins otherwise. */
167 if (domainlen != 0 && domainlen == matchlen)
168 {
169 if ((serv->flags & SERV_LITERAL_ADDRESS))
170 {
171 if (!(sflag & qtype) && flags == 0)
172 continue;
173 }
174 else
175 {
176 if (flags & (F_IPV4 | F_IPV6))
177 continue;
178 }
179 }
180
181 if (domainlen >= matchlen)
182 {
183 *type = serv->flags & (SERV_HAS_DOMAIN | SERV_USE_RESOLV | SERV_NO_REBIND);
184 *domain = serv->domain;
185 matchlen = domainlen;
186 if (serv->flags & SERV_NO_ADDR)
187 flags = F_NXDOMAIN;
188 else if (serv->flags & SERV_LITERAL_ADDRESS)
189 {
190 if (sflag & qtype)
191 {
192 flags = sflag;
193 if (serv->addr.sa.sa_family == AF_INET)
194 *addrpp = (struct all_addr *)&serv->addr.in.sin_addr;
195 #ifdef HAVE_IPV6
196 else
197 *addrpp = (struct all_addr *)&serv->addr.in6.sin6_addr;
198 #endif
199 }
200 else if (!flags || (flags & F_NXDOMAIN))
201 flags = F_NOERR;
202 }
203 else
204 flags = 0;
205 }
206 }
207 }
208 }
209
210 if (flags == 0 && !(qtype & F_QUERY) &&
211 option_bool(OPT_NODOTS_LOCAL) && !strchr(qdomain, '.') && namelen != 0)
212 /* don't forward A or AAAA queries for simple names, except the empty name */
213 flags = F_NOERR;
214
215 if (flags == F_NXDOMAIN && check_for_local_domain(qdomain, now))
216 flags = F_NOERR;
217
218 if (flags)
219 {
220 int logflags = 0;
221
222 if (flags == F_NXDOMAIN || flags == F_NOERR)
223 logflags = F_NEG | qtype;
224
225 log_query(logflags | flags | F_CONFIG | F_FORWARD, qdomain, *addrpp, NULL);
226 }
227 else if ((*type) & SERV_USE_RESOLV)
228 {
229 *type = 0; /* use normal servers for this domain */
230 *domain = NULL;
231 }
232 return flags;
233 }
234
235 static int forward_query(int udpfd, union mysockaddr *udpaddr,
236 struct all_addr *dst_addr, unsigned int dst_iface,
237 struct dns_header *header, size_t plen, time_t now, struct frec *forward)
238 {
239 char *domain = NULL;
240 int type = 0, norebind = 0;
241 struct all_addr *addrp = NULL;
242 unsigned int crc = questions_crc(header, plen, daemon->namebuff);
243 unsigned int flags = 0;
244 unsigned int gotname = extract_request(header, plen, daemon->namebuff, NULL);
245 struct server *start = NULL;
246
247 /* RFC 4035: sect 4.6 para 2 */
248 header->hb4 &= ~HB4_AD;
249
250 /* may be no servers available. */
251 if (!daemon->servers)
252 forward = NULL;
253 else if (forward || (forward = lookup_frec_by_sender(ntohs(header->id), udpaddr, crc)))
254 {
255 /* retry on existing query, send to all available servers */
256 domain = forward->sentto->domain;
257 forward->sentto->failed_queries++;
258 if (!option_bool(OPT_ORDER))
259 {
260 forward->forwardall = 1;
261 daemon->last_server = NULL;
262 }
263 type = forward->sentto->flags & SERV_TYPE;
264 if (!(start = forward->sentto->next))
265 start = daemon->servers; /* at end of list, recycle */
266 header->id = htons(forward->new_id);
267 }
268 else
269 {
270 if (gotname)
271 flags = search_servers(now, &addrp, gotname, daemon->namebuff, &type, &domain, &norebind);
272
273 if (!flags && !(forward = get_new_frec(now, NULL, 0)))
274 /* table full - server failure. */
275 flags = F_NEG;
276
277 if (forward)
278 {
279 forward->source = *udpaddr;
280 forward->dest = *dst_addr;
281 forward->iface = dst_iface;
282 forward->orig_id = ntohs(header->id);
283 forward->new_id = get_id(crc);
284 forward->fd = udpfd;
285 forward->crc = crc;
286 forward->forwardall = 0;
287 forward->flags = 0;
288 if (norebind)
289 forward->flags |= FREC_NOREBIND;
290 if (header->hb4 & HB4_CD)
291 forward->flags |= FREC_CHECKING_DISABLED;
292
293 header->id = htons(forward->new_id);
294
295 /* In strict_order mode, always try servers in the order
296 specified in resolv.conf, if a domain is given
297 always try all the available servers,
298 otherwise, use the one last known to work. */
299
300 if (type == 0)
301 {
302 if (option_bool(OPT_ORDER))
303 start = daemon->servers;
304 else if (!(start = daemon->last_server) ||
305 daemon->forwardcount++ > FORWARD_TEST ||
306 difftime(now, daemon->forwardtime) > FORWARD_TIME)
307 {
308 start = daemon->servers;
309 forward->forwardall = 1;
310 daemon->forwardcount = 0;
311 daemon->forwardtime = now;
312 }
313 }
314 else
315 {
316 start = daemon->servers;
317 if (!option_bool(OPT_ORDER))
318 forward->forwardall = 1;
319 }
320 }
321 }
322
323 /* check for send errors here (no route to host)
324 if we fail to send to all nameservers, send back an error
325 packet straight away (helps modem users when offline) */
326
327 if (!flags && forward)
328 {
329 struct server *firstsentto = start;
330 int forwarded = 0;
331
332 if (option_bool(OPT_ADD_MAC))
333 plen = add_mac(header, plen, ((char *) header) + PACKETSZ, &forward->source);
334
335 if (option_bool(OPT_CLIENT_SUBNET))
336 {
337 size_t new = add_source_addr(header, plen, ((char *) header) + PACKETSZ, &forward->source);
338 if (new != plen)
339 {
340 plen = new;
341 forward->flags |= FREC_HAS_SUBNET;
342 }
343 }
344
345 #ifdef HAVE_DNSSEC
346 if (option_bool(OPT_DNSSEC_VALID))
347 {
348 plen = add_do_bit(header, plen, ((char *) header) + PACKETSZ);
349 header->hb4 |= HB4_CD;
350 }
351 #endif
352
353 while (1)
354 {
355 /* only send to servers dealing with our domain.
356 domain may be NULL, in which case server->domain
357 must be NULL also. */
358
359 if (type == (start->flags & SERV_TYPE) &&
360 (type != SERV_HAS_DOMAIN || hostname_isequal(domain, start->domain)) &&
361 !(start->flags & SERV_LITERAL_ADDRESS))
362 {
363 int fd;
364
365 /* find server socket to use, may need to get random one. */
366 if (start->sfd)
367 fd = start->sfd->fd;
368 else
369 {
370 #ifdef HAVE_IPV6
371 if (start->addr.sa.sa_family == AF_INET6)
372 {
373 if (!forward->rfd6 &&
374 !(forward->rfd6 = allocate_rfd(AF_INET6)))
375 break;
376 daemon->rfd_save = forward->rfd6;
377 fd = forward->rfd6->fd;
378 }
379 else
380 #endif
381 {
382 if (!forward->rfd4 &&
383 !(forward->rfd4 = allocate_rfd(AF_INET)))
384 break;
385 daemon->rfd_save = forward->rfd4;
386 fd = forward->rfd4->fd;
387 }
388
389 #ifdef HAVE_CONNTRACK
390 /* Copy connection mark of incoming query to outgoing connection. */
391 if (option_bool(OPT_CONNTRACK))
392 {
393 unsigned int mark;
394 if (get_incoming_mark(&forward->source, &forward->dest, 0, &mark))
395 setsockopt(fd, SOL_SOCKET, SO_MARK, &mark, sizeof(unsigned int));
396 }
397 #endif
398 }
399
400 if (sendto(fd, (char *)header, plen, 0,
401 &start->addr.sa,
402 sa_len(&start->addr)) == -1)
403 {
404 if (retry_send())
405 continue;
406 }
407 else
408 {
409 /* Keep info in case we want to re-send this packet */
410 daemon->srv_save = start;
411 daemon->packet_len = plen;
412
413 if (!gotname)
414 strcpy(daemon->namebuff, "query");
415 if (start->addr.sa.sa_family == AF_INET)
416 log_query(F_SERVER | F_IPV4 | F_FORWARD, daemon->namebuff,
417 (struct all_addr *)&start->addr.in.sin_addr, NULL);
418 #ifdef HAVE_IPV6
419 else
420 log_query(F_SERVER | F_IPV6 | F_FORWARD, daemon->namebuff,
421 (struct all_addr *)&start->addr.in6.sin6_addr, NULL);
422 #endif
423 start->queries++;
424 forwarded = 1;
425 forward->sentto = start;
426 if (!forward->forwardall)
427 break;
428 forward->forwardall++;
429 }
430 }
431
432 if (!(start = start->next))
433 start = daemon->servers;
434
435 if (start == firstsentto)
436 break;
437 }
438
439 if (forwarded)
440 return 1;
441
442 /* could not send on, prepare to return */
443 header->id = htons(forward->orig_id);
444 free_frec(forward); /* cancel */
445 }
446
447 /* could not send on, return empty answer or address if known for whole domain */
448 if (udpfd != -1)
449 {
450 plen = setup_reply(header, plen, addrp, flags, daemon->local_ttl);
451 send_from(udpfd, option_bool(OPT_NOWILD) || option_bool(OPT_CLEVERBIND), (char *)header, plen, udpaddr, dst_addr, dst_iface);
452 }
453
454 return 0;
455 }
456
457 static size_t process_reply(struct dns_header *header, time_t now, struct server *server, size_t n, int check_rebind,
458 int no_cache, int cache_secure, int check_subnet, union mysockaddr *query_source)
459 {
460 unsigned char *pheader, *sizep;
461 char **sets = 0;
462 int munged = 0, is_sign;
463 size_t plen;
464 int squash_ad = 0;
465
466 #ifdef HAVE_IPSET
467 /* Similar algorithm to search_servers. */
468 struct ipsets *ipset_pos;
469 unsigned int namelen = strlen(daemon->namebuff);
470 unsigned int matchlen = 0;
471 for (ipset_pos = daemon->ipsets; ipset_pos; ipset_pos = ipset_pos->next)
472 {
473 unsigned int domainlen = strlen(ipset_pos->domain);
474 char *matchstart = daemon->namebuff + namelen - domainlen;
475 if (namelen >= domainlen && hostname_isequal(matchstart, ipset_pos->domain) &&
476 (domainlen == 0 || namelen == domainlen || *(matchstart - 1) == '.' ) &&
477 domainlen >= matchlen) {
478 matchlen = domainlen;
479 sets = ipset_pos->sets;
480 }
481 }
482 #endif
483
484 /* If upstream is advertising a larger UDP packet size
485 than we allow, trim it so that we don't get overlarge
486 requests for the client. We can't do this for signed packets. */
487
488 if ((pheader = find_pseudoheader(header, n, &plen, &sizep, &is_sign)))
489 {
490 if (!is_sign)
491 {
492 unsigned short udpsz;
493 unsigned char *psave = sizep;
494
495 GETSHORT(udpsz, sizep);
496 if (udpsz > daemon->edns_pktsz)
497 PUTSHORT(daemon->edns_pktsz, psave);
498 }
499
500 if (check_subnet && !check_source(header, plen, pheader, query_source))
501 {
502 my_syslog(LOG_WARNING, _("discarding DNS reply: subnet option mismatch"));
503 return 0;
504 }
505 }
506
507 /* RFC 4035 sect 4.6 para 3 */
508 if (!is_sign && !option_bool(OPT_DNSSEC_PROXY))
509 squash_ad = 1;
510
511 #ifdef HAVE_DNSSEC
512 if (option_bool(OPT_DNSSEC_VALID))
513 squash_ad = no_cache;
514
515 if (cache_secure)
516 header->hb4 |= HB4_AD;
517 #endif
518
519 if (squash_ad)
520 header->hb4 &= ~HB4_AD;
521
522 if (OPCODE(header) != QUERY || (RCODE(header) != NOERROR && RCODE(header) != NXDOMAIN))
523 return n;
524
525 /* Complain loudly if the upstream server is non-recursive. */
526 if (!(header->hb4 & HB4_RA) && RCODE(header) == NOERROR && ntohs(header->ancount) == 0 &&
527 server && !(server->flags & SERV_WARNED_RECURSIVE))
528 {
529 prettyprint_addr(&server->addr, daemon->namebuff);
530 my_syslog(LOG_WARNING, _("nameserver %s refused to do a recursive query"), daemon->namebuff);
531 if (!option_bool(OPT_LOG))
532 server->flags |= SERV_WARNED_RECURSIVE;
533 }
534
535 if (daemon->bogus_addr && RCODE(header) != NXDOMAIN &&
536 check_for_bogus_wildcard(header, n, daemon->namebuff, daemon->bogus_addr, now))
537 {
538 munged = 1;
539 SET_RCODE(header, NXDOMAIN);
540 header->hb3 &= ~HB3_AA;
541 }
542 else
543 {
544 if (RCODE(header) == NXDOMAIN &&
545 extract_request(header, n, daemon->namebuff, NULL) &&
546 check_for_local_domain(daemon->namebuff, now))
547 {
548 /* if we forwarded a query for a locally known name (because it was for
549 an unknown type) and the answer is NXDOMAIN, convert that to NODATA,
550 since we know that the domain exists, even if upstream doesn't */
551 munged = 1;
552 header->hb3 |= HB3_AA;
553 SET_RCODE(header, NOERROR);
554 }
555
556 if (extract_addresses(header, n, daemon->namebuff, now, sets, is_sign, check_rebind, no_cache, cache_secure))
557 {
558 my_syslog(LOG_WARNING, _("possible DNS-rebind attack detected: %s"), daemon->namebuff);
559 munged = 1;
560 }
561 }
562
563 /* do this after extract_addresses. Ensure NODATA reply and remove
564 nameserver info. */
565
566 if (munged)
567 {
568 header->ancount = htons(0);
569 header->nscount = htons(0);
570 header->arcount = htons(0);
571 }
572
573 /* the bogus-nxdomain stuff, doctor and NXDOMAIN->NODATA munging can all elide
574 sections of the packet. Find the new length here and put back pseudoheader
575 if it was removed. */
576 return resize_packet(header, n, pheader, plen);
577 }
578
579 /* sets new last_server */
580 void reply_query(int fd, int family, time_t now)
581 {
582 /* packet from peer server, extract data for cache, and send to
583 original requester */
584 struct dns_header *header;
585 union mysockaddr serveraddr;
586 struct frec *forward;
587 socklen_t addrlen = sizeof(serveraddr);
588 ssize_t n = recvfrom(fd, daemon->packet, daemon->edns_pktsz, 0, &serveraddr.sa, &addrlen);
589 size_t nn;
590 struct server *server;
591
592 /* packet buffer overwritten */
593 daemon->srv_save = NULL;
594
595 /* Determine the address of the server replying so that we can mark that as good */
596 serveraddr.sa.sa_family = family;
597 #ifdef HAVE_IPV6
598 if (serveraddr.sa.sa_family == AF_INET6)
599 serveraddr.in6.sin6_flowinfo = 0;
600 #endif
601
602 /* spoof check: answer must come from known server, */
603 for (server = daemon->servers; server; server = server->next)
604 if (!(server->flags & (SERV_LITERAL_ADDRESS | SERV_NO_ADDR)) &&
605 sockaddr_isequal(&server->addr, &serveraddr))
606 break;
607
608 header = (struct dns_header *)daemon->packet;
609
610 if (!server ||
611 n < (int)sizeof(struct dns_header) || !(header->hb3 & HB3_QR) ||
612 !(forward = lookup_frec(ntohs(header->id), questions_crc(header, n, daemon->namebuff))))
613 return;
614
615 if ((RCODE(header) == SERVFAIL || RCODE(header) == REFUSED) &&
616 !option_bool(OPT_ORDER) &&
617 forward->forwardall == 0)
618 /* for broken servers, attempt to send to another one. */
619 {
620 unsigned char *pheader;
621 size_t plen;
622 int is_sign;
623
624 /* recreate query from reply */
625 pheader = find_pseudoheader(header, (size_t)n, &plen, NULL, &is_sign);
626 if (!is_sign)
627 {
628 header->ancount = htons(0);
629 header->nscount = htons(0);
630 header->arcount = htons(0);
631 if ((nn = resize_packet(header, (size_t)n, pheader, plen)))
632 {
633 header->hb3 &= ~(HB3_QR | HB3_TC);
634 forward_query(-1, NULL, NULL, 0, header, nn, now, forward);
635 return;
636 }
637 }
638 }
639
640 server = forward->sentto;
641
642 if ((forward->sentto->flags & SERV_TYPE) == 0)
643 {
644 if (RCODE(header) == SERVFAIL || RCODE(header) == REFUSED)
645 server = NULL;
646 else
647 {
648 struct server *last_server;
649
650 /* find good server by address if possible, otherwise assume the last one we sent to */
651 for (last_server = daemon->servers; last_server; last_server = last_server->next)
652 if (!(last_server->flags & (SERV_LITERAL_ADDRESS | SERV_HAS_DOMAIN | SERV_FOR_NODOTS | SERV_NO_ADDR)) &&
653 sockaddr_isequal(&last_server->addr, &serveraddr))
654 {
655 server = last_server;
656 break;
657 }
658 }
659 if (!option_bool(OPT_ALL_SERVERS))
660 daemon->last_server = server;
661 }
662
663 /* If the answer is an error, keep the forward record in place in case
664 we get a good reply from another server. Kill it when we've
665 had replies from all to avoid filling the forwarding table when
666 everything is broken */
667 if (forward->forwardall == 0 || --forward->forwardall == 1 ||
668 (RCODE(header) != REFUSED && RCODE(header) != SERVFAIL))
669 {
670 int check_rebind = 0, no_cache_dnssec = 0, cache_secure = 0;
671
672 if (option_bool(OPT_NO_REBIND))
673 check_rebind = !(forward->flags & FREC_NOREBIND);
674
675 /* Don't cache replies where DNSSEC validation was turned off, either
676 the upstream server told us so, or the original query specified it. */
677 if ((header->hb4 & HB4_CD) || (forward->flags & FREC_CHECKING_DISABLED))
678 no_cache_dnssec = 1;
679
680 #ifdef HAVE_DNSSEC
681 if (option_bool(OPT_DNSSEC_VALID) && !(forward->flags & FREC_CHECKING_DISABLED))
682 {
683 int status;
684
685 /* We've had a reply already, which we're validating. Ignore this duplicate */
686 if (forward->stash)
687 return;
688
689 if (forward->flags & FREC_DNSKEY_QUERY)
690 status = dnssec_validate_by_ds(now, header, n, daemon->namebuff, daemon->keyname, forward->class);
691 else if (forward->flags & FREC_DS_QUERY)
692 status = dnssec_validate_ds(now, header, n, daemon->namebuff, daemon->keyname, forward->class);
693 else
694 status = dnssec_validate_reply(now, header, n, daemon->namebuff, daemon->keyname, &forward->class);
695
696 /* Can't validate, as we're missing key data. Put this
697 answer aside, whilst we get that. */
698 if (status == STAT_NEED_DS || status == STAT_NEED_KEY)
699 {
700 struct frec *new;
701
702 if ((new = get_new_frec(now, NULL, 1)))
703 {
704 struct frec *next = new->next;
705 *new = *forward; /* copy everything, then overwrite */
706 new->next = next;
707 new->stash = NULL;
708 new->blocking_query = NULL;
709 new->flags &= ~(FREC_DNSKEY_QUERY | FREC_DS_QUERY);
710
711 if ((forward->stash = blockdata_alloc((char *)header, n)))
712 {
713 int fd;
714
715 forward->stash_len = n;
716
717 new->dependent = forward; /* to find query awaiting new one. */
718 forward->blocking_query = new; /* for garbage cleaning */
719 /* validate routines leave name of required record in daemon->keyname */
720 if (status == STAT_NEED_KEY)
721 {
722 new->flags |= FREC_DNSKEY_QUERY;
723 nn = dnssec_generate_query(header, daemon->keyname, forward->class, T_DNSKEY, &server->addr);
724 }
725 else if (status == STAT_NEED_DS)
726 {
727 new->flags |= FREC_DS_QUERY;
728 nn = dnssec_generate_query(header, daemon->keyname, forward->class, T_DS, &server->addr);
729 }
730 new->crc = questions_crc(header, nn, daemon->namebuff);
731 new->new_id = get_id(new->crc);
732 header->id = htons(new->new_id);
733
734 /* Don't resend this. */
735 daemon->srv_save = NULL;
736
737 if (server->sfd)
738 fd = server->sfd->fd;
739 else
740 #ifdef HAVE_IPV6
741 /* Note that we use the same random port for the DNSSEC stuff */
742 if (server->addr.sa.sa_family == AF_INET6)
743 {
744 fd = new->rfd6->fd;
745 new->rfd6->refcount++;
746 }
747 else
748 #endif
749 {
750 fd = new->rfd4->fd;
751 new->rfd4->refcount++;
752 }
753
754 /* Send DNSSEC query to same server as original query */
755 while (sendto(fd, (char *)header, nn, 0, &server->addr.sa, sa_len(&server->addr)) == -1 && retry_send());
756 server->queries++;
757 }
758 }
759
760 return;
761 }
762
763 /* Ok, we reached far enough up the chain-of-trust that we can validate something.
764 Now wind back down, pulling back answers which wouldn't previously validate
765 and validate them with the new data. Failure to find needed data here is an internal error.
766 Once we get to the original answer (FREC_DNSSEC_QUERY not set) and it validates,
767 return it to the original requestor. */
768 if (forward->flags & (FREC_DNSKEY_QUERY | FREC_DS_QUERY))
769 {
770 while (forward->dependent)
771 {
772 struct frec *prev;
773
774 if (status == STAT_SECURE)
775 {
776 if (forward->flags & FREC_DNSKEY_QUERY)
777 status = dnssec_validate_by_ds(now, header, n, daemon->namebuff, daemon->keyname, forward->class);
778 else if (forward->flags & FREC_DS_QUERY)
779 status = dnssec_validate_ds(now, header, n, daemon->namebuff, daemon->keyname, forward->class);
780 }
781
782 prev = forward->dependent;
783 free_frec(forward);
784 forward = prev;
785 forward->blocking_query = NULL; /* already gone */
786 blockdata_retrieve(forward->stash, forward->stash_len, (void *)header);
787 n = forward->stash_len;
788 }
789
790 /* All DNSKEY and DS records done and in cache, now finally validate original
791 answer, provided last DNSKEY is OK. */
792 if (status == STAT_SECURE)
793 status = dnssec_validate_reply(now, header, n, daemon->namebuff, daemon->keyname, &forward->class);
794
795 if (status == STAT_NEED_DS || status == STAT_NEED_KEY)
796 {
797 my_syslog(LOG_ERR, _("Unexpected missing data for DNSSEC validation"));
798 status = STAT_INSECURE;
799 }
800 }
801
802 log_query(F_KEYTAG | F_SECSTAT, "result", NULL,
803 status == STAT_SECURE ? "SECURE" : (status == STAT_INSECURE ? "INSECURE" : "BOGUS"));
804
805 no_cache_dnssec = 0;
806
807 if (status == STAT_SECURE)
808 cache_secure = 1;
809 /* TODO return SERVFAIL here */
810 else if (status == STAT_BOGUS)
811 no_cache_dnssec = 1;
812
813 /* restore CD bit to the value in the query */
814 if (forward->flags & FREC_CHECKING_DISABLED)
815 header->hb4 |= HB4_CD;
816 else
817 header->hb4 &= ~HB4_CD;
818 }
819 #endif
820
821 if ((nn = process_reply(header, now, server, (size_t)n, check_rebind, no_cache_dnssec, cache_secure,
822 forward->flags & FREC_HAS_SUBNET, &forward->source)))
823 {
824 header->id = htons(forward->orig_id);
825 header->hb4 |= HB4_RA; /* recursion if available */
826 send_from(forward->fd, option_bool(OPT_NOWILD) || option_bool (OPT_CLEVERBIND), daemon->packet, nn,
827 &forward->source, &forward->dest, forward->iface);
828 }
829 free_frec(forward); /* cancel */
830 }
831 }
832
833
834 void receive_query(struct listener *listen, time_t now)
835 {
836 struct dns_header *header = (struct dns_header *)daemon->packet;
837 union mysockaddr source_addr;
838 unsigned short type;
839 struct all_addr dst_addr;
840 struct in_addr netmask, dst_addr_4;
841 size_t m;
842 ssize_t n;
843 int if_index = 0, auth_dns = 0;
844 #ifdef HAVE_AUTH
845 int local_auth = 0;
846 #endif
847 struct iovec iov[1];
848 struct msghdr msg;
849 struct cmsghdr *cmptr;
850 union {
851 struct cmsghdr align; /* this ensures alignment */
852 #ifdef HAVE_IPV6
853 char control6[CMSG_SPACE(sizeof(struct in6_pktinfo))];
854 #endif
855 #if defined(HAVE_LINUX_NETWORK)
856 char control[CMSG_SPACE(sizeof(struct in_pktinfo))];
857 #elif defined(IP_RECVDSTADDR) && defined(HAVE_SOLARIS_NETWORK)
858 char control[CMSG_SPACE(sizeof(struct in_addr)) +
859 CMSG_SPACE(sizeof(unsigned int))];
860 #elif defined(IP_RECVDSTADDR)
861 char control[CMSG_SPACE(sizeof(struct in_addr)) +
862 CMSG_SPACE(sizeof(struct sockaddr_dl))];
863 #endif
864 } control_u;
865 #ifdef HAVE_IPV6
866 /* Can always get recvd interface for IPv6 */
867 int check_dst = !option_bool(OPT_NOWILD) || listen->family == AF_INET6;
868 #else
869 int check_dst = !option_bool(OPT_NOWILD);
870 #endif
871
872 /* packet buffer overwritten */
873 daemon->srv_save = NULL;
874
875 dst_addr_4.s_addr = 0;
876 netmask.s_addr = 0;
877
878 if (option_bool(OPT_NOWILD) && listen->iface)
879 {
880 auth_dns = listen->iface->dns_auth;
881
882 if (listen->family == AF_INET)
883 {
884 dst_addr_4 = listen->iface->addr.in.sin_addr;
885 netmask = listen->iface->netmask;
886 }
887 }
888
889 iov[0].iov_base = daemon->packet;
890 iov[0].iov_len = daemon->edns_pktsz;
891
892 msg.msg_control = control_u.control;
893 msg.msg_controllen = sizeof(control_u);
894 msg.msg_flags = 0;
895 msg.msg_name = &source_addr;
896 msg.msg_namelen = sizeof(source_addr);
897 msg.msg_iov = iov;
898 msg.msg_iovlen = 1;
899
900 if ((n = recvmsg(listen->fd, &msg, 0)) == -1)
901 return;
902
903 if (n < (int)sizeof(struct dns_header) ||
904 (msg.msg_flags & MSG_TRUNC) ||
905 (header->hb3 & HB3_QR))
906 return;
907
908 source_addr.sa.sa_family = listen->family;
909 #ifdef HAVE_IPV6
910 if (listen->family == AF_INET6)
911 source_addr.in6.sin6_flowinfo = 0;
912 #endif
913
914 if (check_dst)
915 {
916 struct ifreq ifr;
917
918 if (msg.msg_controllen < sizeof(struct cmsghdr))
919 return;
920
921 #if defined(HAVE_LINUX_NETWORK)
922 if (listen->family == AF_INET)
923 for (cmptr = CMSG_FIRSTHDR(&msg); cmptr; cmptr = CMSG_NXTHDR(&msg, cmptr))
924 if (cmptr->cmsg_level == IPPROTO_IP && cmptr->cmsg_type == IP_PKTINFO)
925 {
926 union {
927 unsigned char *c;
928 struct in_pktinfo *p;
929 } p;
930 p.c = CMSG_DATA(cmptr);
931 dst_addr_4 = dst_addr.addr.addr4 = p.p->ipi_spec_dst;
932 if_index = p.p->ipi_ifindex;
933 }
934 #elif defined(IP_RECVDSTADDR) && defined(IP_RECVIF)
935 if (listen->family == AF_INET)
936 {
937 for (cmptr = CMSG_FIRSTHDR(&msg); cmptr; cmptr = CMSG_NXTHDR(&msg, cmptr))
938 {
939 union {
940 unsigned char *c;
941 unsigned int *i;
942 struct in_addr *a;
943 #ifndef HAVE_SOLARIS_NETWORK
944 struct sockaddr_dl *s;
945 #endif
946 } p;
947 p.c = CMSG_DATA(cmptr);
948 if (cmptr->cmsg_level == IPPROTO_IP && cmptr->cmsg_type == IP_RECVDSTADDR)
949 dst_addr_4 = dst_addr.addr.addr4 = *(p.a);
950 else if (cmptr->cmsg_level == IPPROTO_IP && cmptr->cmsg_type == IP_RECVIF)
951 #ifdef HAVE_SOLARIS_NETWORK
952 if_index = *(p.i);
953 #else
954 if_index = p.s->sdl_index;
955 #endif
956 }
957 }
958 #endif
959
960 #ifdef HAVE_IPV6
961 if (listen->family == AF_INET6)
962 {
963 for (cmptr = CMSG_FIRSTHDR(&msg); cmptr; cmptr = CMSG_NXTHDR(&msg, cmptr))
964 if (cmptr->cmsg_level == IPPROTO_IPV6 && cmptr->cmsg_type == daemon->v6pktinfo)
965 {
966 union {
967 unsigned char *c;
968 struct in6_pktinfo *p;
969 } p;
970 p.c = CMSG_DATA(cmptr);
971
972 dst_addr.addr.addr6 = p.p->ipi6_addr;
973 if_index = p.p->ipi6_ifindex;
974 }
975 }
976 #endif
977
978 /* enforce available interface configuration */
979
980 if (!indextoname(listen->fd, if_index, ifr.ifr_name))
981 return;
982
983 if (!iface_check(listen->family, &dst_addr, ifr.ifr_name, &auth_dns))
984 {
985 if (!option_bool(OPT_CLEVERBIND))
986 enumerate_interfaces(0);
987 if (!loopback_exception(listen->fd, listen->family, &dst_addr, ifr.ifr_name) &&
988 !label_exception(if_index, listen->family, &dst_addr))
989 return;
990 }
991
992 if (listen->family == AF_INET && option_bool(OPT_LOCALISE))
993 {
994 struct irec *iface;
995
996 /* get the netmask of the interface whch has the address we were sent to.
997 This is no neccessarily the interface we arrived on. */
998
999 for (iface = daemon->interfaces; iface; iface = iface->next)
1000 if (iface->addr.sa.sa_family == AF_INET &&
1001 iface->addr.in.sin_addr.s_addr == dst_addr_4.s_addr)
1002 break;
1003
1004 /* interface may be new */
1005 if (!iface && !option_bool(OPT_CLEVERBIND))
1006 enumerate_interfaces(0);
1007
1008 for (iface = daemon->interfaces; iface; iface = iface->next)
1009 if (iface->addr.sa.sa_family == AF_INET &&
1010 iface->addr.in.sin_addr.s_addr == dst_addr_4.s_addr)
1011 break;
1012
1013 /* If we failed, abandon localisation */
1014 if (iface)
1015 netmask = iface->netmask;
1016 else
1017 dst_addr_4.s_addr = 0;
1018 }
1019 }
1020
1021 if (extract_request(header, (size_t)n, daemon->namebuff, &type))
1022 {
1023 char types[20];
1024 #ifdef HAVE_AUTH
1025 struct auth_zone *zone;
1026 #endif
1027
1028 querystr(auth_dns ? "auth" : "query", types, type);
1029
1030 if (listen->family == AF_INET)
1031 log_query(F_QUERY | F_IPV4 | F_FORWARD, daemon->namebuff,
1032 (struct all_addr *)&source_addr.in.sin_addr, types);
1033 #ifdef HAVE_IPV6
1034 else
1035 log_query(F_QUERY | F_IPV6 | F_FORWARD, daemon->namebuff,
1036 (struct all_addr *)&source_addr.in6.sin6_addr, types);
1037 #endif
1038
1039 #ifdef HAVE_AUTH
1040 /* find queries for zones we're authoritative for, and answer them directly */
1041 if (!auth_dns)
1042 for (zone = daemon->auth_zones; zone; zone = zone->next)
1043 if (in_zone(zone, daemon->namebuff, NULL))
1044 {
1045 auth_dns = 1;
1046 local_auth = 1;
1047 break;
1048 }
1049 #endif
1050 }
1051
1052 #ifdef HAVE_AUTH
1053 if (auth_dns)
1054 {
1055 m = answer_auth(header, ((char *) header) + PACKETSZ, (size_t)n, now, &source_addr, local_auth);
1056 if (m >= 1)
1057 {
1058 send_from(listen->fd, option_bool(OPT_NOWILD) || option_bool(OPT_CLEVERBIND),
1059 (char *)header, m, &source_addr, &dst_addr, if_index);
1060 daemon->auth_answer++;
1061 }
1062 }
1063 else
1064 #endif
1065 {
1066 m = answer_request(header, ((char *) header) + PACKETSZ, (size_t)n,
1067 dst_addr_4, netmask, now);
1068
1069 if (m >= 1)
1070 {
1071 send_from(listen->fd, option_bool(OPT_NOWILD) || option_bool(OPT_CLEVERBIND),
1072 (char *)header, m, &source_addr, &dst_addr, if_index);
1073 daemon->local_answer++;
1074 }
1075 else if (forward_query(listen->fd, &source_addr, &dst_addr, if_index,
1076 header, (size_t)n, now, NULL))
1077 daemon->queries_forwarded++;
1078 else
1079 daemon->local_answer++;
1080 }
1081 }
1082
1083 /* The daemon forks before calling this: it should deal with one connection,
1084 blocking as neccessary, and then return. Note, need to be a bit careful
1085 about resources for debug mode, when the fork is suppressed: that's
1086 done by the caller. */
1087 unsigned char *tcp_request(int confd, time_t now,
1088 union mysockaddr *local_addr, struct in_addr netmask, int auth_dns)
1089 {
1090 size_t size = 0;
1091 int norebind = 0;
1092 #ifdef HAVE_AUTH
1093 int local_auth = 0;
1094 #endif
1095 int checking_disabled, check_subnet;
1096 size_t m;
1097 unsigned short qtype;
1098 unsigned int gotname;
1099 unsigned char c1, c2;
1100 /* Max TCP packet + slop + size */
1101 unsigned char *packet = whine_malloc(65536 + MAXDNAME + RRFIXEDSZ + sizeof(u16));
1102 unsigned char *payload = &packet[2];
1103 /* largest field in header is 16-bits, so this is still sufficiently aligned */
1104 struct dns_header *header = (struct dns_header *)payload;
1105 u16 *length = (u16 *)packet;
1106 struct server *last_server;
1107 struct in_addr dst_addr_4;
1108 union mysockaddr peer_addr;
1109 socklen_t peer_len = sizeof(union mysockaddr);
1110
1111 if (getpeername(confd, (struct sockaddr *)&peer_addr, &peer_len) == -1)
1112 return packet;
1113
1114 while (1)
1115 {
1116 if (!packet ||
1117 !read_write(confd, &c1, 1, 1) || !read_write(confd, &c2, 1, 1) ||
1118 !(size = c1 << 8 | c2) ||
1119 !read_write(confd, payload, size, 1))
1120 return packet;
1121
1122 if (size < (int)sizeof(struct dns_header))
1123 continue;
1124
1125 check_subnet = 0;
1126
1127 /* save state of "cd" flag in query */
1128 checking_disabled = header->hb4 & HB4_CD;
1129
1130 /* RFC 4035: sect 4.6 para 2 */
1131 header->hb4 &= ~HB4_AD;
1132
1133 if ((gotname = extract_request(header, (unsigned int)size, daemon->namebuff, &qtype)))
1134 {
1135 char types[20];
1136 #ifdef HAVE_AUTH
1137 struct auth_zone *zone;
1138 #endif
1139 querystr(auth_dns ? "auth" : "query", types, qtype);
1140
1141 if (peer_addr.sa.sa_family == AF_INET)
1142 log_query(F_QUERY | F_IPV4 | F_FORWARD, daemon->namebuff,
1143 (struct all_addr *)&peer_addr.in.sin_addr, types);
1144 #ifdef HAVE_IPV6
1145 else
1146 log_query(F_QUERY | F_IPV6 | F_FORWARD, daemon->namebuff,
1147 (struct all_addr *)&peer_addr.in6.sin6_addr, types);
1148 #endif
1149
1150 #ifdef HAVE_AUTH
1151 /* find queries for zones we're authoritative for, and answer them directly */
1152 if (!auth_dns)
1153 for (zone = daemon->auth_zones; zone; zone = zone->next)
1154 if (in_zone(zone, daemon->namebuff, NULL))
1155 {
1156 auth_dns = 1;
1157 local_auth = 1;
1158 break;
1159 }
1160 #endif
1161 }
1162
1163 if (local_addr->sa.sa_family == AF_INET)
1164 dst_addr_4 = local_addr->in.sin_addr;
1165 else
1166 dst_addr_4.s_addr = 0;
1167
1168 #ifdef HAVE_AUTH
1169 if (auth_dns)
1170 m = answer_auth(header, ((char *) header) + 65536, (size_t)size, now, &peer_addr, local_auth);
1171 else
1172 #endif
1173 {
1174 /* m > 0 if answered from cache */
1175 m = answer_request(header, ((char *) header) + 65536, (size_t)size,
1176 dst_addr_4, netmask, now);
1177
1178 /* Do this by steam now we're not in the select() loop */
1179 check_log_writer(NULL);
1180
1181 if (m == 0)
1182 {
1183 unsigned int flags = 0;
1184 struct all_addr *addrp = NULL;
1185 int type = 0;
1186 char *domain = NULL;
1187
1188 if (option_bool(OPT_ADD_MAC))
1189 size = add_mac(header, size, ((char *) header) + 65536, &peer_addr);
1190
1191 if (option_bool(OPT_CLIENT_SUBNET))
1192 {
1193 size_t new = add_source_addr(header, size, ((char *) header) + 65536, &peer_addr);
1194 if (size != new)
1195 {
1196 size = new;
1197 check_subnet = 1;
1198 }
1199 }
1200
1201 if (gotname)
1202 flags = search_servers(now, &addrp, gotname, daemon->namebuff, &type, &domain, &norebind);
1203
1204 if (type != 0 || option_bool(OPT_ORDER) || !daemon->last_server)
1205 last_server = daemon->servers;
1206 else
1207 last_server = daemon->last_server;
1208
1209 if (!flags && last_server)
1210 {
1211 struct server *firstsendto = NULL;
1212 unsigned int crc = questions_crc(header, (unsigned int)size, daemon->namebuff);
1213
1214 /* Loop round available servers until we succeed in connecting to one.
1215 Note that this code subtley ensures that consecutive queries on this connection
1216 which can go to the same server, do so. */
1217 while (1)
1218 {
1219 if (!firstsendto)
1220 firstsendto = last_server;
1221 else
1222 {
1223 if (!(last_server = last_server->next))
1224 last_server = daemon->servers;
1225
1226 if (last_server == firstsendto)
1227 break;
1228 }
1229
1230 /* server for wrong domain */
1231 if (type != (last_server->flags & SERV_TYPE) ||
1232 (type == SERV_HAS_DOMAIN && !hostname_isequal(domain, last_server->domain)))
1233 continue;
1234
1235 if (last_server->tcpfd == -1)
1236 {
1237 if ((last_server->tcpfd = socket(last_server->addr.sa.sa_family, SOCK_STREAM, 0)) == -1)
1238 continue;
1239
1240 if ((!local_bind(last_server->tcpfd, &last_server->source_addr, last_server->interface, 1) ||
1241 connect(last_server->tcpfd, &last_server->addr.sa, sa_len(&last_server->addr)) == -1))
1242 {
1243 close(last_server->tcpfd);
1244 last_server->tcpfd = -1;
1245 continue;
1246 }
1247
1248 #ifdef HAVE_CONNTRACK
1249 /* Copy connection mark of incoming query to outgoing connection. */
1250 if (option_bool(OPT_CONNTRACK))
1251 {
1252 unsigned int mark;
1253 struct all_addr local;
1254 #ifdef HAVE_IPV6
1255 if (local_addr->sa.sa_family == AF_INET6)
1256 local.addr.addr6 = local_addr->in6.sin6_addr;
1257 else
1258 #endif
1259 local.addr.addr4 = local_addr->in.sin_addr;
1260
1261 if (get_incoming_mark(&peer_addr, &local, 1, &mark))
1262 setsockopt(last_server->tcpfd, SOL_SOCKET, SO_MARK, &mark, sizeof(unsigned int));
1263 }
1264 #endif
1265 }
1266
1267 *length = htons(size);
1268
1269 if (!read_write(last_server->tcpfd, packet, size + sizeof(u16), 0) ||
1270 !read_write(last_server->tcpfd, &c1, 1, 1) ||
1271 !read_write(last_server->tcpfd, &c2, 1, 1))
1272 {
1273 close(last_server->tcpfd);
1274 last_server->tcpfd = -1;
1275 continue;
1276 }
1277
1278 m = (c1 << 8) | c2;
1279 if (!read_write(last_server->tcpfd, payload, m, 1))
1280 return packet;
1281
1282 if (!gotname)
1283 strcpy(daemon->namebuff, "query");
1284 if (last_server->addr.sa.sa_family == AF_INET)
1285 log_query(F_SERVER | F_IPV4 | F_FORWARD, daemon->namebuff,
1286 (struct all_addr *)&last_server->addr.in.sin_addr, NULL);
1287 #ifdef HAVE_IPV6
1288 else
1289 log_query(F_SERVER | F_IPV6 | F_FORWARD, daemon->namebuff,
1290 (struct all_addr *)&last_server->addr.in6.sin6_addr, NULL);
1291 #endif
1292
1293 /* There's no point in updating the cache, since this process will exit and
1294 lose the information after a few queries. We make this call for the alias and
1295 bogus-nxdomain side-effects. */
1296 /* If the crc of the question section doesn't match the crc we sent, then
1297 someone might be attempting to insert bogus values into the cache by
1298 sending replies containing questions and bogus answers. */
1299 if (crc == questions_crc(header, (unsigned int)m, daemon->namebuff))
1300 m = process_reply(header, now, last_server, (unsigned int)m,
1301 option_bool(OPT_NO_REBIND) && !norebind, checking_disabled,
1302 0, check_subnet, &peer_addr); /* TODO - cache secure */
1303
1304 break;
1305 }
1306 }
1307
1308 /* In case of local answer or no connections made. */
1309 if (m == 0)
1310 m = setup_reply(header, (unsigned int)size, addrp, flags, daemon->local_ttl);
1311 }
1312 }
1313
1314 check_log_writer(NULL);
1315
1316 *length = htons(m);
1317
1318 if (m == 0 || !read_write(confd, packet, m + sizeof(u16), 0))
1319 return packet;
1320 }
1321 }
1322
1323 static struct frec *allocate_frec(time_t now)
1324 {
1325 struct frec *f;
1326
1327 if ((f = (struct frec *)whine_malloc(sizeof(struct frec))))
1328 {
1329 f->next = daemon->frec_list;
1330 f->time = now;
1331 f->sentto = NULL;
1332 f->rfd4 = NULL;
1333 f->flags = 0;
1334 #ifdef HAVE_IPV6
1335 f->rfd6 = NULL;
1336 #endif
1337 #ifdef HAVE_DNSSEC
1338 f->blocking_query = NULL;
1339 #endif
1340 daemon->frec_list = f;
1341 }
1342
1343 return f;
1344 }
1345
1346 static struct randfd *allocate_rfd(int family)
1347 {
1348 static int finger = 0;
1349 int i;
1350
1351 /* limit the number of sockets we have open to avoid starvation of
1352 (eg) TFTP. Once we have a reasonable number, randomness should be OK */
1353
1354 for (i = 0; i < RANDOM_SOCKS; i++)
1355 if (daemon->randomsocks[i].refcount == 0)
1356 {
1357 if ((daemon->randomsocks[i].fd = random_sock(family)) == -1)
1358 break;
1359
1360 daemon->randomsocks[i].refcount = 1;
1361 daemon->randomsocks[i].family = family;
1362 return &daemon->randomsocks[i];
1363 }
1364
1365 /* No free ones or cannot get new socket, grab an existing one */
1366 for (i = 0; i < RANDOM_SOCKS; i++)
1367 {
1368 int j = (i+finger) % RANDOM_SOCKS;
1369 if (daemon->randomsocks[j].refcount != 0 &&
1370 daemon->randomsocks[j].family == family &&
1371 daemon->randomsocks[j].refcount != 0xffff)
1372 {
1373 finger = j;
1374 daemon->randomsocks[j].refcount++;
1375 return &daemon->randomsocks[j];
1376 }
1377 }
1378
1379 return NULL; /* doom */
1380 }
1381 static void free_frec(struct frec *f)
1382 {
1383 if (f->rfd4 && --(f->rfd4->refcount) == 0)
1384 close(f->rfd4->fd);
1385
1386 f->rfd4 = NULL;
1387 f->sentto = NULL;
1388 f->flags = 0;
1389
1390 #ifdef HAVE_IPV6
1391 if (f->rfd6 && --(f->rfd6->refcount) == 0)
1392 close(f->rfd6->fd);
1393
1394 f->rfd6 = NULL;
1395 #endif
1396
1397 #ifdef HAVE_DNSSEC
1398 if (f->stash)
1399 {
1400 blockdata_free(f->stash);
1401 f->stash = NULL;
1402 }
1403
1404 /* Anything we're waiting on is pointless now, too */
1405 if (f->blocking_query)
1406 free_frec(f->blocking_query);
1407 f->blocking_query = NULL;
1408
1409 #endif
1410 }
1411
1412 /* if wait==NULL return a free or older than TIMEOUT record.
1413 else return *wait zero if one available, or *wait is delay to
1414 when the oldest in-use record will expire. Impose an absolute
1415 limit of 4*TIMEOUT before we wipe things (for random sockets).
1416 If force is set, always return a result, even if we have
1417 to allocate above the limit. */
1418 struct frec *get_new_frec(time_t now, int *wait, int force)
1419 {
1420 struct frec *f, *oldest, *target;
1421 int count;
1422
1423 if (wait)
1424 *wait = 0;
1425
1426 for (f = daemon->frec_list, oldest = NULL, target = NULL, count = 0; f; f = f->next, count++)
1427 if (!f->sentto)
1428 target = f;
1429 else
1430 {
1431 if (difftime(now, f->time) >= 4*TIMEOUT)
1432 {
1433 free_frec(f);
1434 target = f;
1435 }
1436
1437 if (!oldest || difftime(f->time, oldest->time) <= 0)
1438 oldest = f;
1439 }
1440
1441 if (target)
1442 {
1443 target->time = now;
1444 return target;
1445 }
1446
1447 /* can't find empty one, use oldest if there is one
1448 and it's older than timeout */
1449 if (oldest && ((int)difftime(now, oldest->time)) >= TIMEOUT)
1450 {
1451 /* keep stuff for twice timeout if we can by allocating a new
1452 record instead */
1453 if (difftime(now, oldest->time) < 2*TIMEOUT &&
1454 count <= daemon->ftabsize &&
1455 (f = allocate_frec(now)))
1456 return f;
1457
1458 if (!wait)
1459 {
1460 free_frec(oldest);
1461 oldest->time = now;
1462 }
1463 return oldest;
1464 }
1465
1466 /* none available, calculate time 'till oldest record expires */
1467 if (!force && count > daemon->ftabsize)
1468 {
1469 static time_t last_log = 0;
1470
1471 if (oldest && wait)
1472 *wait = oldest->time + (time_t)TIMEOUT - now;
1473
1474 if ((int)difftime(now, last_log) > 5)
1475 {
1476 last_log = now;
1477 my_syslog(LOG_WARNING, _("Maximum number of concurrent DNS queries reached (max: %d)"), daemon->ftabsize);
1478 }
1479
1480 return NULL;
1481 }
1482
1483 if (!(f = allocate_frec(now)) && wait)
1484 /* wait one second on malloc failure */
1485 *wait = 1;
1486
1487 return f; /* OK if malloc fails and this is NULL */
1488 }
1489
1490 /* crc is all-ones if not known. */
1491 static struct frec *lookup_frec(unsigned short id, unsigned int crc)
1492 {
1493 struct frec *f;
1494
1495 for(f = daemon->frec_list; f; f = f->next)
1496 if (f->sentto && f->new_id == id &&
1497 (f->crc == crc || crc == 0xffffffff))
1498 return f;
1499
1500 return NULL;
1501 }
1502
1503 static struct frec *lookup_frec_by_sender(unsigned short id,
1504 union mysockaddr *addr,
1505 unsigned int crc)
1506 {
1507 struct frec *f;
1508
1509 for(f = daemon->frec_list; f; f = f->next)
1510 if (f->sentto &&
1511 f->orig_id == id &&
1512 f->crc == crc &&
1513 sockaddr_isequal(&f->source, addr))
1514 return f;
1515
1516 return NULL;
1517 }
1518
1519 /* A server record is going away, remove references to it */
1520 void server_gone(struct server *server)
1521 {
1522 struct frec *f;
1523
1524 for (f = daemon->frec_list; f; f = f->next)
1525 if (f->sentto && f->sentto == server)
1526 free_frec(f);
1527
1528 if (daemon->last_server == server)
1529 daemon->last_server = NULL;
1530
1531 if (daemon->srv_save == server)
1532 daemon->srv_save = NULL;
1533 }
1534
1535 /* return unique random ids. */
1536 static unsigned short get_id(unsigned int crc)
1537 {
1538 unsigned short ret = 0;
1539
1540 do
1541 ret = rand16();
1542 while (lookup_frec(ret, crc));
1543
1544 return ret;
1545 }
1546
1547
1548
1549
1550