blob: 27f619b2f47780e9c1d6ac3a257f403cffc495ef [file] [log] [blame]
Simon Kelleyc47e3ba2014-01-08 17:07:54 +00001/* dnsmasq is Copyright (c) 2000-2014 Simon Kelley
Simon Kelley9e4abcb2004-01-22 19:47:41 +00002
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
Simon Kelley824af852008-02-12 20:43:05 +00005 the Free Software Foundation; version 2 dated June, 1991, or
6 (at your option) version 3 dated 29 June, 2007.
7
Simon Kelley9e4abcb2004-01-22 19:47:41 +00008 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.
Simon Kelley824af852008-02-12 20:43:05 +000012
Simon Kelley73a08a22009-02-05 20:28:08 +000013 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/>.
Simon Kelley9e4abcb2004-01-22 19:47:41 +000015*/
16
Simon Kelley9e4abcb2004-01-22 19:47:41 +000017#include "dnsmasq.h"
18
Simon Kelley8a9be9e2014-01-25 23:17:21 +000019static struct frec *lookup_frec(unsigned short id, void *hash);
Simon Kelley9e4abcb2004-01-22 19:47:41 +000020static struct frec *lookup_frec_by_sender(unsigned short id,
Simon Kelleyfd9fa482004-10-21 20:24:00 +010021 union mysockaddr *addr,
Simon Kelley8a9be9e2014-01-25 23:17:21 +000022 void *hash);
23static unsigned short get_id(void);
Simon Kelley1a6bca82008-07-11 11:11:42 +010024static void free_frec(struct frec *f);
25static struct randfd *allocate_rfd(int family);
Simon Kelley9e4abcb2004-01-22 19:47:41 +000026
Simon Kelley824af852008-02-12 20:43:05 +000027/* Send a UDP packet with its source address set as "source"
Simon Kelley44a2a312004-03-10 20:04:35 +000028 unless nowild is true, when we just send it with the kernel default */
Simon Kelley29689cf2012-03-22 14:01:00 +000029int send_from(int fd, int nowild, char *packet, size_t len,
30 union mysockaddr *to, struct all_addr *source,
Simon Kelley50303b12012-04-04 22:13:17 +010031 unsigned int iface)
Simon Kelley9e4abcb2004-01-22 19:47:41 +000032{
Simon Kelley44a2a312004-03-10 20:04:35 +000033 struct msghdr msg;
34 struct iovec iov[1];
Simon Kelley44a2a312004-03-10 20:04:35 +000035 union {
36 struct cmsghdr align; /* this ensures alignment */
Simon Kelley5e9e0ef2006-04-17 14:24:29 +010037#if defined(HAVE_LINUX_NETWORK)
Simon Kelley44a2a312004-03-10 20:04:35 +000038 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;
Simon Kelleyfeba5c12004-07-27 20:28:58 +010046
Simon Kelley44a2a312004-03-10 20:04:35 +000047 iov[0].iov_base = packet;
48 iov[0].iov_len = len;
49
Simon Kelleyfeba5c12004-07-27 20:28:58 +010050 msg.msg_control = NULL;
51 msg.msg_controllen = 0;
Simon Kelley44a2a312004-03-10 20:04:35 +000052 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;
Simon Kelleyfeba5c12004-07-27 20:28:58 +010057
Simon Kelley26128d22004-11-14 16:43:54 +000058 if (!nowild)
Simon Kelleyfeba5c12004-07-27 20:28:58 +010059 {
Simon Kelley26128d22004-11-14 16:43:54 +000060 struct cmsghdr *cmptr;
Simon Kelleyfeba5c12004-07-27 20:28:58 +010061 msg.msg_control = &control_u;
62 msg.msg_controllen = sizeof(control_u);
Simon Kelley26128d22004-11-14 16:43:54 +000063 cmptr = CMSG_FIRSTHDR(&msg);
Simon Kelley44a2a312004-03-10 20:04:35 +000064
Simon Kelley26128d22004-11-14 16:43:54 +000065 if (to->sa.sa_family == AF_INET)
66 {
Simon Kelley5e9e0ef2006-04-17 14:24:29 +010067#if defined(HAVE_LINUX_NETWORK)
Simon Kelley8ef5ada2010-06-03 19:42:45 +010068 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));
Simon Kelley26128d22004-11-14 16:43:54 +000072 msg.msg_controllen = cmptr->cmsg_len = CMSG_LEN(sizeof(struct in_pktinfo));
Simon Kelleyc72daea2012-01-05 21:33:27 +000073 cmptr->cmsg_level = IPPROTO_IP;
Simon Kelley26128d22004-11-14 16:43:54 +000074 cmptr->cmsg_type = IP_PKTINFO;
75#elif defined(IP_SENDSRCADDR)
Simon Kelley8ef5ada2010-06-03 19:42:45 +010076 memcpy(CMSG_DATA(cmptr), &(source->addr.addr4), sizeof(source->addr.addr4));
Simon Kelley26128d22004-11-14 16:43:54 +000077 msg.msg_controllen = cmptr->cmsg_len = CMSG_LEN(sizeof(struct in_addr));
78 cmptr->cmsg_level = IPPROTO_IP;
79 cmptr->cmsg_type = IP_SENDSRCADDR;
Simon Kelley44a2a312004-03-10 20:04:35 +000080#endif
Simon Kelley26128d22004-11-14 16:43:54 +000081 }
Simon Kelley26128d22004-11-14 16:43:54 +000082 else
Simon Kelleyb8187c82005-11-26 21:46:27 +000083#ifdef HAVE_IPV6
Simon Kelley26128d22004-11-14 16:43:54 +000084 {
Simon Kelley8ef5ada2010-06-03 19:42:45 +010085 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));
Simon Kelley26128d22004-11-14 16:43:54 +000089 msg.msg_controllen = cmptr->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
Simon Kelley316e2732010-01-22 20:16:09 +000090 cmptr->cmsg_type = daemon->v6pktinfo;
Simon Kelleyc72daea2012-01-05 21:33:27 +000091 cmptr->cmsg_level = IPPROTO_IPV6;
Simon Kelley26128d22004-11-14 16:43:54 +000092 }
Simon Kelley3d8df262005-08-29 12:19:27 +010093#else
Simon Kelleyc72daea2012-01-05 21:33:27 +000094 (void)iface; /* eliminate warning */
Simon Kelley26128d22004-11-14 16:43:54 +000095#endif
96 }
Simon Kelleyfeba5c12004-07-27 20:28:58 +010097
Simon Kelley29d28dd2012-12-03 14:05:59 +000098 while (sendmsg(fd, &msg, 0) == -1)
Simon Kelleyfeba5c12004-07-27 20:28:58 +010099 {
Simon Kelleyfd9fa482004-10-21 20:24:00 +0100100 if (retry_send())
Simon Kelley29d28dd2012-12-03 14:05:59 +0000101 continue;
102
103 /* If interface is still in DAD, EINVAL results - ignore that. */
104 if (errno == EINVAL)
105 break;
Simon Kelley22d904d2012-02-26 20:13:45 +0000106
Simon Kelley50303b12012-04-04 22:13:17 +0100107 my_syslog(LOG_ERR, _("failed to send packet: %s"), strerror(errno));
Simon Kelley29689cf2012-03-22 14:01:00 +0000108 return 0;
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100109 }
Simon Kelley29d28dd2012-12-03 14:05:59 +0000110
Simon Kelley29689cf2012-03-22 14:01:00 +0000111 return 1;
Simon Kelley44a2a312004-03-10 20:04:35 +0000112}
113
Simon Kelley28866e92011-02-14 20:19:14 +0000114static unsigned int search_servers(time_t now, struct all_addr **addrpp,
115 unsigned int qtype, char *qdomain, int *type, char **domain, int *norebind)
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100116
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;
Simon Kelley28866e92011-02-14 20:19:14 +0000125 unsigned int flags = 0;
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100126
Simon Kelley3be34542004-09-11 19:12:13 +0100127 for (serv = daemon->servers; serv; serv=serv->next)
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100128 /* domain matches take priority over NODOTS matches */
Simon Kelley3d8df262005-08-29 12:19:27 +0100129 if ((serv->flags & SERV_FOR_NODOTS) && *type != SERV_HAS_DOMAIN && !strchr(qdomain, '.') && namelen != 0)
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100130 {
Simon Kelley28866e92011-02-14 20:19:14 +0000131 unsigned int sflag = serv->addr.sa.sa_family == AF_INET ? F_IPV4 : F_IPV6;
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100132 *type = SERV_FOR_NODOTS;
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100133 if (serv->flags & SERV_NO_ADDR)
Simon Kelley36717ee2004-09-20 19:20:58 +0100134 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;
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100142#ifdef HAVE_IPV6
Simon Kelley36717ee2004-09-20 19:20:58 +0100143 else
144 *addrpp = (struct all_addr *)&serv->addr.in6.sin6_addr;
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100145#endif
Simon Kelley36717ee2004-09-20 19:20:58 +0100146 }
Simon Kelley824af852008-02-12 20:43:05 +0000147 else if (!flags || (flags & F_NXDOMAIN))
Simon Kelley36717ee2004-09-20 19:20:58 +0100148 flags = F_NOERR;
149 }
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100150 }
151 else if (serv->flags & SERV_HAS_DOMAIN)
152 {
153 unsigned int domainlen = strlen(serv->domain);
Simon Kelleyb8187c82005-11-26 21:46:27 +0000154 char *matchstart = qdomain + namelen - domainlen;
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100155 if (namelen >= domainlen &&
Simon Kelleyb8187c82005-11-26 21:46:27 +0000156 hostname_isequal(matchstart, serv->domain) &&
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100157 (domainlen == 0 || namelen == domainlen || *(matchstart-1) == '.' ))
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100158 {
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100159 if (serv->flags & SERV_NO_REBIND)
160 *norebind = 1;
Simon Kelley28866e92011-02-14 20:19:14 +0000161 else
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100162 {
Simon Kelley28866e92011-02-14 20:19:14 +0000163 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)
Simon Kelley36717ee2004-09-20 19:20:58 +0100168 {
Simon Kelley28866e92011-02-14 20:19:14 +0000169 if ((serv->flags & SERV_LITERAL_ADDRESS))
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100170 {
Simon Kelley28866e92011-02-14 20:19:14 +0000171 if (!(sflag & qtype) && flags == 0)
172 continue;
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100173 }
Simon Kelley28866e92011-02-14 20:19:14 +0000174 else
175 {
176 if (flags & (F_IPV4 | F_IPV6))
177 continue;
178 }
Simon Kelley36717ee2004-09-20 19:20:58 +0100179 }
Simon Kelley28866e92011-02-14 20:19:14 +0000180
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 }
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100207 }
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100208 }
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100209
Simon Kelley7de060b2011-08-26 17:24:52 +0100210 if (flags == 0 && !(qtype & F_QUERY) &&
Simon Kelley28866e92011-02-14 20:19:14 +0000211 option_bool(OPT_NODOTS_LOCAL) && !strchr(qdomain, '.') && namelen != 0)
Simon Kelley7de060b2011-08-26 17:24:52 +0100212 /* don't forward A or AAAA queries for simple names, except the empty name */
213 flags = F_NOERR;
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100214
Simon Kelley5aabfc72007-08-29 11:24:47 +0100215 if (flags == F_NXDOMAIN && check_for_local_domain(qdomain, now))
Simon Kelleyc1bb8502004-08-11 18:40:17 +0100216 flags = F_NOERR;
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100217
Simon Kelley824af852008-02-12 20:43:05 +0000218 if (flags)
219 {
220 int logflags = 0;
221
222 if (flags == F_NXDOMAIN || flags == F_NOERR)
223 logflags = F_NEG | qtype;
224
Simon Kelley1a6bca82008-07-11 11:11:42 +0100225 log_query(logflags | flags | F_CONFIG | F_FORWARD, qdomain, *addrpp, NULL);
Simon Kelley824af852008-02-12 20:43:05 +0000226 }
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100227 else if ((*type) & SERV_USE_RESOLV)
228 {
229 *type = 0; /* use normal servers for this domain */
230 *domain = NULL;
231 }
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100232 return flags;
233}
Simon Kelley44a2a312004-03-10 20:04:35 +0000234
Simon Kelley824af852008-02-12 20:43:05 +0000235static int forward_query(int udpfd, union mysockaddr *udpaddr,
236 struct all_addr *dst_addr, unsigned int dst_iface,
Simon Kelley83349b82014-02-10 21:02:01 +0000237 struct dns_header *header, size_t plen, time_t now,
238 struct frec *forward, int ad_reqd)
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000239{
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000240 char *domain = NULL;
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100241 int type = 0, norebind = 0;
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000242 struct all_addr *addrp = NULL;
Simon Kelley28866e92011-02-14 20:19:14 +0000243 unsigned int flags = 0;
Simon Kelleyde379512004-06-22 20:23:33 +0100244 struct server *start = NULL;
Simon Kelley8a9be9e2014-01-25 23:17:21 +0000245#ifdef HAVE_DNSSEC
246 void *hash = hash_questions(header, plen, daemon->namebuff);
247#else
248 unsigned int crc = questions_crc(header, plen, daemon->namebuff);
249 void *hash = &crc;
250#endif
251 unsigned int gotname = extract_request(header, plen, daemon->namebuff, NULL);
252
Simon Kelley3d8df262005-08-29 12:19:27 +0100253 /* may be no servers available. */
254 if (!daemon->servers)
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000255 forward = NULL;
Simon Kelley8a9be9e2014-01-25 23:17:21 +0000256 else if (forward || (hash && (forward = lookup_frec_by_sender(ntohs(header->id), udpaddr, hash))))
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000257 {
Simon Kelleye0c0ad32014-01-16 22:42:07 +0000258#ifdef HAVE_DNSSEC
259 /* If we've already got an answer to this query, but we're awaiting keys for vaildation,
260 there's no point retrying the query, retry the key query instead...... */
261 if (forward->blocking_query)
262 {
263 int fd;
264
265 while (forward->blocking_query)
266 forward = forward->blocking_query;
267
268 blockdata_retrieve(forward->stash, forward->stash_len, (void *)header);
269 plen = forward->stash_len;
270
271 if (forward->sentto->addr.sa.sa_family)
272 log_query(F_DNSSEC | F_IPV4, "retry", (struct all_addr *)&forward->sentto->addr.in.sin_addr, "dnssec");
273#ifdef HAVE_IPV6
274 else
275 log_query(F_DNSSEC | F_IPV6, "retry", (struct all_addr *)&forward->sentto->addr.in6.sin6_addr, "dnssec");
276#endif
277
278 if (forward->sentto->sfd)
279 fd = forward->sentto->sfd->fd;
280 else
281 {
282#ifdef HAVE_IPV6
283 if (forward->sentto->addr.sa.sa_family == AF_INET6)
284 fd = forward->rfd6->fd;
285 else
286#endif
287 fd = forward->rfd4->fd;
288 }
289
290 while (sendto(fd, (char *)header, plen, 0,
291 &forward->sentto->addr.sa,
292 sa_len(&forward->sentto->addr)) == -1 && retry_send());
293
294 return 1;
295 }
296#endif
297
Simon Kelleyde379512004-06-22 20:23:33 +0100298 /* retry on existing query, send to all available servers */
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000299 domain = forward->sentto->domain;
Simon Kelley824af852008-02-12 20:43:05 +0000300 forward->sentto->failed_queries++;
Simon Kelley28866e92011-02-14 20:19:14 +0000301 if (!option_bool(OPT_ORDER))
Simon Kelleyde379512004-06-22 20:23:33 +0100302 {
Simon Kelley0a852542005-03-23 20:28:59 +0000303 forward->forwardall = 1;
Simon Kelley3be34542004-09-11 19:12:13 +0100304 daemon->last_server = NULL;
Simon Kelleyde379512004-06-22 20:23:33 +0100305 }
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000306 type = forward->sentto->flags & SERV_TYPE;
Simon Kelleyde379512004-06-22 20:23:33 +0100307 if (!(start = forward->sentto->next))
Simon Kelley3be34542004-09-11 19:12:13 +0100308 start = daemon->servers; /* at end of list, recycle */
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000309 header->id = htons(forward->new_id);
310 }
311 else
312 {
313 if (gotname)
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100314 flags = search_servers(now, &addrp, gotname, daemon->namebuff, &type, &domain, &norebind);
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000315
Simon Kelley3a237152013-12-12 12:15:50 +0000316 if (!flags && !(forward = get_new_frec(now, NULL, 0)))
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100317 /* table full - server failure. */
318 flags = F_NEG;
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000319
320 if (forward)
321 {
Simon Kelley0a852542005-03-23 20:28:59 +0000322 forward->source = *udpaddr;
323 forward->dest = *dst_addr;
324 forward->iface = dst_iface;
Simon Kelley0a852542005-03-23 20:28:59 +0000325 forward->orig_id = ntohs(header->id);
Simon Kelley8a9be9e2014-01-25 23:17:21 +0000326 forward->new_id = get_id();
Simon Kelley832af0b2007-01-21 20:01:28 +0000327 forward->fd = udpfd;
Simon Kelley8a9be9e2014-01-25 23:17:21 +0000328 memcpy(forward->hash, hash, HASH_SIZE);
Simon Kelley0a852542005-03-23 20:28:59 +0000329 forward->forwardall = 0;
Simon Kelleyed4c0762013-10-08 20:46:34 +0100330 forward->flags = 0;
Simon Kelley28866e92011-02-14 20:19:14 +0000331 if (norebind)
332 forward->flags |= FREC_NOREBIND;
Simon Kelley572b41e2011-02-18 18:11:18 +0000333 if (header->hb4 & HB4_CD)
Simon Kelley28866e92011-02-14 20:19:14 +0000334 forward->flags |= FREC_CHECKING_DISABLED;
Simon Kelley83349b82014-02-10 21:02:01 +0000335 if (ad_reqd)
336 forward->flags |= FREC_AD_QUESTION;
Simon Kelley7fa836e2014-02-10 20:11:24 +0000337#ifdef HAVE_DNSSEC
338 forward->work_counter = DNSSEC_WORK;
339#endif
Simon Kelley0a852542005-03-23 20:28:59 +0000340
Simon Kelley28866e92011-02-14 20:19:14 +0000341 header->id = htons(forward->new_id);
342
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100343 /* In strict_order mode, always try servers in the order
344 specified in resolv.conf, if a domain is given
345 always try all the available servers,
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000346 otherwise, use the one last known to work. */
347
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100348 if (type == 0)
349 {
Simon Kelley28866e92011-02-14 20:19:14 +0000350 if (option_bool(OPT_ORDER))
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100351 start = daemon->servers;
352 else if (!(start = daemon->last_server) ||
353 daemon->forwardcount++ > FORWARD_TEST ||
354 difftime(now, daemon->forwardtime) > FORWARD_TIME)
355 {
356 start = daemon->servers;
357 forward->forwardall = 1;
358 daemon->forwardcount = 0;
359 daemon->forwardtime = now;
360 }
361 }
362 else
Simon Kelleyde379512004-06-22 20:23:33 +0100363 {
Simon Kelley3be34542004-09-11 19:12:13 +0100364 start = daemon->servers;
Simon Kelley28866e92011-02-14 20:19:14 +0000365 if (!option_bool(OPT_ORDER))
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100366 forward->forwardall = 1;
Simon Kelleyde379512004-06-22 20:23:33 +0100367 }
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000368 }
369 }
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100370
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000371 /* check for send errors here (no route to host)
372 if we fail to send to all nameservers, send back an error
373 packet straight away (helps modem users when offline) */
374
375 if (!flags && forward)
376 {
Simon Kelleyde379512004-06-22 20:23:33 +0100377 struct server *firstsentto = start;
378 int forwarded = 0;
Simon Kelley28866e92011-02-14 20:19:14 +0000379
Giacomo Tazzari797a7af2013-04-22 13:16:37 +0100380 if (option_bool(OPT_ADD_MAC))
Simon Kelley60b68062014-01-08 12:10:28 +0000381 plen = add_mac(header, plen, ((char *) header) + daemon->packet_buff_sz, &forward->source);
Simon Kelley28866e92011-02-14 20:19:14 +0000382
Simon Kelleyed4c0762013-10-08 20:46:34 +0100383 if (option_bool(OPT_CLIENT_SUBNET))
384 {
Simon Kelley60b68062014-01-08 12:10:28 +0000385 size_t new = add_source_addr(header, plen, ((char *) header) + daemon->packet_buff_sz, &forward->source);
Simon Kelleyed4c0762013-10-08 20:46:34 +0100386 if (new != plen)
387 {
388 plen = new;
389 forward->flags |= FREC_HAS_SUBNET;
390 }
391 }
392
Simon Kelley3a237152013-12-12 12:15:50 +0000393#ifdef HAVE_DNSSEC
394 if (option_bool(OPT_DNSSEC_VALID))
Simon Kelley0fc2f312014-01-08 10:26:58 +0000395 {
Simon Kelley60b68062014-01-08 12:10:28 +0000396 plen = add_do_bit(header, plen, ((char *) header) + daemon->packet_buff_sz);
Simon Kelley5b3bf922014-01-25 17:03:07 +0000397 /* For debugging, set Checking Disabled, otherwise, have the upstream check too,
398 this allows it to select auth servers when one is returning bad data. */
399 if (option_bool(OPT_DNSSEC_DEBUG))
400 header->hb4 |= HB4_CD;
Simon Kelley0fc2f312014-01-08 10:26:58 +0000401 }
Simon Kelley3a237152013-12-12 12:15:50 +0000402#endif
403
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000404 while (1)
405 {
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000406 /* only send to servers dealing with our domain.
407 domain may be NULL, in which case server->domain
408 must be NULL also. */
409
Simon Kelleyde379512004-06-22 20:23:33 +0100410 if (type == (start->flags & SERV_TYPE) &&
Simon Kelleyfd9fa482004-10-21 20:24:00 +0100411 (type != SERV_HAS_DOMAIN || hostname_isequal(domain, start->domain)) &&
412 !(start->flags & SERV_LITERAL_ADDRESS))
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000413 {
Simon Kelley1a6bca82008-07-11 11:11:42 +0100414 int fd;
415
416 /* find server socket to use, may need to get random one. */
417 if (start->sfd)
418 fd = start->sfd->fd;
419 else
420 {
421#ifdef HAVE_IPV6
422 if (start->addr.sa.sa_family == AF_INET6)
423 {
424 if (!forward->rfd6 &&
425 !(forward->rfd6 = allocate_rfd(AF_INET6)))
426 break;
Simon Kelley3927da42008-07-20 15:10:39 +0100427 daemon->rfd_save = forward->rfd6;
Simon Kelley1a6bca82008-07-11 11:11:42 +0100428 fd = forward->rfd6->fd;
429 }
430 else
431#endif
432 {
433 if (!forward->rfd4 &&
434 !(forward->rfd4 = allocate_rfd(AF_INET)))
435 break;
Simon Kelley3927da42008-07-20 15:10:39 +0100436 daemon->rfd_save = forward->rfd4;
Simon Kelley1a6bca82008-07-11 11:11:42 +0100437 fd = forward->rfd4->fd;
438 }
Simon Kelley7de060b2011-08-26 17:24:52 +0100439
440#ifdef HAVE_CONNTRACK
441 /* Copy connection mark of incoming query to outgoing connection. */
442 if (option_bool(OPT_CONNTRACK))
443 {
444 unsigned int mark;
Giacomo Tazzari797a7af2013-04-22 13:16:37 +0100445 if (get_incoming_mark(&forward->source, &forward->dest, 0, &mark))
Simon Kelley7de060b2011-08-26 17:24:52 +0100446 setsockopt(fd, SOL_SOCKET, SO_MARK, &mark, sizeof(unsigned int));
447 }
448#endif
Simon Kelley1a6bca82008-07-11 11:11:42 +0100449 }
450
451 if (sendto(fd, (char *)header, plen, 0,
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100452 &start->addr.sa,
Simon Kelleyfd9fa482004-10-21 20:24:00 +0100453 sa_len(&start->addr)) == -1)
454 {
455 if (retry_send())
456 continue;
457 }
458 else
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000459 {
Simon Kelleycdeda282006-03-16 20:16:06 +0000460 /* Keep info in case we want to re-send this packet */
461 daemon->srv_save = start;
462 daemon->packet_len = plen;
463
Simon Kelleyde379512004-06-22 20:23:33 +0100464 if (!gotname)
Simon Kelley3be34542004-09-11 19:12:13 +0100465 strcpy(daemon->namebuff, "query");
Simon Kelleyde379512004-06-22 20:23:33 +0100466 if (start->addr.sa.sa_family == AF_INET)
Simon Kelley3be34542004-09-11 19:12:13 +0100467 log_query(F_SERVER | F_IPV4 | F_FORWARD, daemon->namebuff,
Simon Kelley1a6bca82008-07-11 11:11:42 +0100468 (struct all_addr *)&start->addr.in.sin_addr, NULL);
Simon Kelleyde379512004-06-22 20:23:33 +0100469#ifdef HAVE_IPV6
470 else
Simon Kelley3be34542004-09-11 19:12:13 +0100471 log_query(F_SERVER | F_IPV6 | F_FORWARD, daemon->namebuff,
Simon Kelley1a6bca82008-07-11 11:11:42 +0100472 (struct all_addr *)&start->addr.in6.sin6_addr, NULL);
Simon Kelleyde379512004-06-22 20:23:33 +0100473#endif
Simon Kelley824af852008-02-12 20:43:05 +0000474 start->queries++;
Simon Kelleyde379512004-06-22 20:23:33 +0100475 forwarded = 1;
476 forward->sentto = start;
Simon Kelley0a852542005-03-23 20:28:59 +0000477 if (!forward->forwardall)
Simon Kelleyde379512004-06-22 20:23:33 +0100478 break;
Simon Kelley0a852542005-03-23 20:28:59 +0000479 forward->forwardall++;
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000480 }
481 }
482
Simon Kelleyde379512004-06-22 20:23:33 +0100483 if (!(start = start->next))
Simon Kelley3be34542004-09-11 19:12:13 +0100484 start = daemon->servers;
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000485
Simon Kelleyde379512004-06-22 20:23:33 +0100486 if (start == firstsentto)
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000487 break;
488 }
489
Simon Kelleyde379512004-06-22 20:23:33 +0100490 if (forwarded)
Simon Kelley824af852008-02-12 20:43:05 +0000491 return 1;
Simon Kelleyde379512004-06-22 20:23:33 +0100492
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000493 /* could not send on, prepare to return */
494 header->id = htons(forward->orig_id);
Simon Kelley1a6bca82008-07-11 11:11:42 +0100495 free_frec(forward); /* cancel */
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000496 }
497
498 /* could not send on, return empty answer or address if known for whole domain */
Simon Kelleyb8187c82005-11-26 21:46:27 +0000499 if (udpfd != -1)
500 {
Simon Kelleycdeda282006-03-16 20:16:06 +0000501 plen = setup_reply(header, plen, addrp, flags, daemon->local_ttl);
Simon Kelley54dd3932012-06-20 11:23:38 +0100502 send_from(udpfd, option_bool(OPT_NOWILD) || option_bool(OPT_CLEVERBIND), (char *)header, plen, udpaddr, dst_addr, dst_iface);
Simon Kelleyb8187c82005-11-26 21:46:27 +0000503 }
504
Simon Kelley824af852008-02-12 20:43:05 +0000505 return 0;
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000506}
507
Simon Kelleyed4c0762013-10-08 20:46:34 +0100508static size_t process_reply(struct dns_header *header, time_t now, struct server *server, size_t n, int check_rebind,
Simon Kelley83349b82014-02-10 21:02:01 +0000509 int no_cache, int cache_secure, int ad_reqd, int check_subnet, union mysockaddr *query_source)
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100510{
Simon Kelley36717ee2004-09-20 19:20:58 +0100511 unsigned char *pheader, *sizep;
Jason A. Donenfeld13d86c72013-02-22 18:20:53 +0000512 char **sets = 0;
Simon Kelley832af0b2007-01-21 20:01:28 +0000513 int munged = 0, is_sign;
Simon Kelleycdeda282006-03-16 20:16:06 +0000514 size_t plen;
515
Simon Kelley83349b82014-02-10 21:02:01 +0000516 (void)ad_reqd;
517
Jason A. Donenfeld13d86c72013-02-22 18:20:53 +0000518#ifdef HAVE_IPSET
519 /* Similar algorithm to search_servers. */
520 struct ipsets *ipset_pos;
521 unsigned int namelen = strlen(daemon->namebuff);
522 unsigned int matchlen = 0;
523 for (ipset_pos = daemon->ipsets; ipset_pos; ipset_pos = ipset_pos->next)
524 {
525 unsigned int domainlen = strlen(ipset_pos->domain);
526 char *matchstart = daemon->namebuff + namelen - domainlen;
527 if (namelen >= domainlen && hostname_isequal(matchstart, ipset_pos->domain) &&
528 (domainlen == 0 || namelen == domainlen || *(matchstart - 1) == '.' ) &&
Simon Kelley6c0cb852014-01-17 14:40:46 +0000529 domainlen >= matchlen)
530 {
531 matchlen = domainlen;
532 sets = ipset_pos->sets;
533 }
Jason A. Donenfeld13d86c72013-02-22 18:20:53 +0000534 }
535#endif
536
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100537 /* If upstream is advertising a larger UDP packet size
Simon Kelley9009d742008-11-14 20:04:27 +0000538 than we allow, trim it so that we don't get overlarge
539 requests for the client. We can't do this for signed packets. */
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100540
Simon Kelleyed4c0762013-10-08 20:46:34 +0100541 if ((pheader = find_pseudoheader(header, n, &plen, &sizep, &is_sign)))
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100542 {
Simon Kelley83349b82014-02-10 21:02:01 +0000543 unsigned short udpsz;
544 unsigned char *psave = sizep;
545
546 GETSHORT(udpsz, sizep);
547
548 if (!is_sign && udpsz > daemon->edns_pktsz)
549 PUTSHORT(daemon->edns_pktsz, psave);
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100550
Simon Kelleyed4c0762013-10-08 20:46:34 +0100551 if (check_subnet && !check_source(header, plen, pheader, query_source))
552 {
553 my_syslog(LOG_WARNING, _("discarding DNS reply: subnet option mismatch"));
554 return 0;
555 }
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100556 }
Simon Kelley83349b82014-02-10 21:02:01 +0000557
Simon Kelley28866e92011-02-14 20:19:14 +0000558 /* RFC 4035 sect 4.6 para 3 */
Giovanni Bajo237724c2012-04-05 02:46:52 +0200559 if (!is_sign && !option_bool(OPT_DNSSEC_PROXY))
Simon Kelley795501b2014-01-08 18:11:55 +0000560 header->hb4 &= ~HB4_AD;
Simon Kelley3a237152013-12-12 12:15:50 +0000561
Simon Kelley572b41e2011-02-18 18:11:18 +0000562 if (OPCODE(header) != QUERY || (RCODE(header) != NOERROR && RCODE(header) != NXDOMAIN))
Simon Kelley36717ee2004-09-20 19:20:58 +0100563 return n;
564
Simon Kelley0a852542005-03-23 20:28:59 +0000565 /* Complain loudly if the upstream server is non-recursive. */
Simon Kelley572b41e2011-02-18 18:11:18 +0000566 if (!(header->hb4 & HB4_RA) && RCODE(header) == NOERROR && ntohs(header->ancount) == 0 &&
Simon Kelley0a852542005-03-23 20:28:59 +0000567 server && !(server->flags & SERV_WARNED_RECURSIVE))
568 {
Simon Kelley3d8df262005-08-29 12:19:27 +0100569 prettyprint_addr(&server->addr, daemon->namebuff);
Simon Kelleyf2621c72007-04-29 19:47:21 +0100570 my_syslog(LOG_WARNING, _("nameserver %s refused to do a recursive query"), daemon->namebuff);
Simon Kelley28866e92011-02-14 20:19:14 +0000571 if (!option_bool(OPT_LOG))
Simon Kelley0a852542005-03-23 20:28:59 +0000572 server->flags |= SERV_WARNED_RECURSIVE;
573 }
Giovanni Bajoe292e932012-04-22 14:32:02 +0200574
Simon Kelley572b41e2011-02-18 18:11:18 +0000575 if (daemon->bogus_addr && RCODE(header) != NXDOMAIN &&
Simon Kelleyfd9fa482004-10-21 20:24:00 +0100576 check_for_bogus_wildcard(header, n, daemon->namebuff, daemon->bogus_addr, now))
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100577 {
Simon Kelleyfd9fa482004-10-21 20:24:00 +0100578 munged = 1;
Simon Kelley572b41e2011-02-18 18:11:18 +0000579 SET_RCODE(header, NXDOMAIN);
580 header->hb3 &= ~HB3_AA;
Simon Kelley6938f342014-01-26 22:47:39 +0000581 cache_secure = 0;
Simon Kelley36717ee2004-09-20 19:20:58 +0100582 }
Simon Kelleyfd9fa482004-10-21 20:24:00 +0100583 else
Simon Kelley36717ee2004-09-20 19:20:58 +0100584 {
Simon Kelley6938f342014-01-26 22:47:39 +0000585 int doctored = 0;
586
Simon Kelley572b41e2011-02-18 18:11:18 +0000587 if (RCODE(header) == NXDOMAIN &&
Simon Kelleyfd9fa482004-10-21 20:24:00 +0100588 extract_request(header, n, daemon->namebuff, NULL) &&
Simon Kelley5aabfc72007-08-29 11:24:47 +0100589 check_for_local_domain(daemon->namebuff, now))
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100590 {
Simon Kelley36717ee2004-09-20 19:20:58 +0100591 /* if we forwarded a query for a locally known name (because it was for
592 an unknown type) and the answer is NXDOMAIN, convert that to NODATA,
593 since we know that the domain exists, even if upstream doesn't */
Simon Kelleyfd9fa482004-10-21 20:24:00 +0100594 munged = 1;
Simon Kelley572b41e2011-02-18 18:11:18 +0000595 header->hb3 |= HB3_AA;
596 SET_RCODE(header, NOERROR);
Simon Kelley6938f342014-01-26 22:47:39 +0000597 cache_secure = 0;
Simon Kelley36717ee2004-09-20 19:20:58 +0100598 }
Simon Kelley832af0b2007-01-21 20:01:28 +0000599
Simon Kelley6938f342014-01-26 22:47:39 +0000600 if (extract_addresses(header, n, daemon->namebuff, now, sets, is_sign, check_rebind, no_cache, cache_secure, &doctored))
Simon Kelley824af852008-02-12 20:43:05 +0000601 {
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100602 my_syslog(LOG_WARNING, _("possible DNS-rebind attack detected: %s"), daemon->namebuff);
Simon Kelley824af852008-02-12 20:43:05 +0000603 munged = 1;
Simon Kelley6938f342014-01-26 22:47:39 +0000604 cache_secure = 0;
Simon Kelley824af852008-02-12 20:43:05 +0000605 }
Simon Kelley6938f342014-01-26 22:47:39 +0000606
607 if (doctored)
608 cache_secure = 0;
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100609 }
Simon Kelleyfd9fa482004-10-21 20:24:00 +0100610
Simon Kelleya25720a2014-01-14 23:13:55 +0000611#ifdef HAVE_DNSSEC
612 if (no_cache && !(header->hb4 & HB4_CD))
613 {
Simon Kelley7d23a662014-01-26 09:33:21 +0000614 if (!option_bool(OPT_DNSSEC_DEBUG))
Simon Kelleya25720a2014-01-14 23:13:55 +0000615 {
616 /* Bogus reply, turn into SERVFAIL */
617 SET_RCODE(header, SERVFAIL);
618 munged = 1;
619 }
620 }
Simon Kelley6938f342014-01-26 22:47:39 +0000621
622 if (option_bool(OPT_DNSSEC_VALID))
623 header->hb4 &= ~HB4_AD;
624
Simon Kelley83349b82014-02-10 21:02:01 +0000625 if (!(header->hb4 & HB4_CD) && ad_reqd && cache_secure)
Simon Kelley6938f342014-01-26 22:47:39 +0000626 header->hb4 |= HB4_AD;
Simon Kelleya25720a2014-01-14 23:13:55 +0000627#endif
628
Simon Kelleyfd9fa482004-10-21 20:24:00 +0100629 /* do this after extract_addresses. Ensure NODATA reply and remove
630 nameserver info. */
631
632 if (munged)
633 {
634 header->ancount = htons(0);
635 header->nscount = htons(0);
636 header->arcount = htons(0);
637 }
638
Simon Kelley36717ee2004-09-20 19:20:58 +0100639 /* the bogus-nxdomain stuff, doctor and NXDOMAIN->NODATA munging can all elide
640 sections of the packet. Find the new length here and put back pseudoheader
641 if it was removed. */
642 return resize_packet(header, n, pheader, plen);
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100643}
644
Simon Kelley3be34542004-09-11 19:12:13 +0100645/* sets new last_server */
Simon Kelley1a6bca82008-07-11 11:11:42 +0100646void reply_query(int fd, int family, time_t now)
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000647{
648 /* packet from peer server, extract data for cache, and send to
649 original requester */
Simon Kelley572b41e2011-02-18 18:11:18 +0000650 struct dns_header *header;
Simon Kelleyde379512004-06-22 20:23:33 +0100651 union mysockaddr serveraddr;
Simon Kelley832af0b2007-01-21 20:01:28 +0000652 struct frec *forward;
Simon Kelleyde379512004-06-22 20:23:33 +0100653 socklen_t addrlen = sizeof(serveraddr);
Simon Kelley60b68062014-01-08 12:10:28 +0000654 ssize_t n = recvfrom(fd, daemon->packet, daemon->packet_buff_sz, 0, &serveraddr.sa, &addrlen);
Simon Kelleycdeda282006-03-16 20:16:06 +0000655 size_t nn;
Simon Kelley1a6bca82008-07-11 11:11:42 +0100656 struct server *server;
Simon Kelley8a9be9e2014-01-25 23:17:21 +0000657 void *hash;
658#ifndef HAVE_DNSSEC
659 unsigned int crc;
660#endif
661
Simon Kelleycdeda282006-03-16 20:16:06 +0000662 /* packet buffer overwritten */
663 daemon->srv_save = NULL;
Simon Kelley832af0b2007-01-21 20:01:28 +0000664
Simon Kelleyde379512004-06-22 20:23:33 +0100665 /* Determine the address of the server replying so that we can mark that as good */
Simon Kelley1a6bca82008-07-11 11:11:42 +0100666 serveraddr.sa.sa_family = family;
Simon Kelleyde379512004-06-22 20:23:33 +0100667#ifdef HAVE_IPV6
668 if (serveraddr.sa.sa_family == AF_INET6)
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100669 serveraddr.in6.sin6_flowinfo = 0;
Simon Kelleyde379512004-06-22 20:23:33 +0100670#endif
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000671
Simon Kelley1a6bca82008-07-11 11:11:42 +0100672 /* spoof check: answer must come from known server, */
673 for (server = daemon->servers; server; server = server->next)
674 if (!(server->flags & (SERV_LITERAL_ADDRESS | SERV_NO_ADDR)) &&
675 sockaddr_isequal(&server->addr, &serveraddr))
676 break;
677
Simon Kelley572b41e2011-02-18 18:11:18 +0000678 header = (struct dns_header *)daemon->packet;
Simon Kelley8a9be9e2014-01-25 23:17:21 +0000679
680#ifdef HAVE_DNSSEC
681 hash = hash_questions(header, n, daemon->namebuff);
682#else
683 hash = &crc;
684 crc = questions_crc(header, n, daemon->namebuff);
685#endif
Simon Kelleyfd9fa482004-10-21 20:24:00 +0100686
Simon Kelley1a6bca82008-07-11 11:11:42 +0100687 if (!server ||
Simon Kelley572b41e2011-02-18 18:11:18 +0000688 n < (int)sizeof(struct dns_header) || !(header->hb3 & HB3_QR) ||
Simon Kelley8a9be9e2014-01-25 23:17:21 +0000689 !(forward = lookup_frec(ntohs(header->id), hash)))
Simon Kelley1a6bca82008-07-11 11:11:42 +0100690 return;
Simon Kelley3a237152013-12-12 12:15:50 +0000691
Simon Kelley572b41e2011-02-18 18:11:18 +0000692 if ((RCODE(header) == SERVFAIL || RCODE(header) == REFUSED) &&
Simon Kelley28866e92011-02-14 20:19:14 +0000693 !option_bool(OPT_ORDER) &&
Simon Kelley1a6bca82008-07-11 11:11:42 +0100694 forward->forwardall == 0)
695 /* for broken servers, attempt to send to another one. */
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000696 {
Simon Kelley1a6bca82008-07-11 11:11:42 +0100697 unsigned char *pheader;
698 size_t plen;
699 int is_sign;
Simon Kelley832af0b2007-01-21 20:01:28 +0000700
Simon Kelley1a6bca82008-07-11 11:11:42 +0100701 /* recreate query from reply */
702 pheader = find_pseudoheader(header, (size_t)n, &plen, NULL, &is_sign);
703 if (!is_sign)
Simon Kelley832af0b2007-01-21 20:01:28 +0000704 {
Simon Kelley1a6bca82008-07-11 11:11:42 +0100705 header->ancount = htons(0);
706 header->nscount = htons(0);
707 header->arcount = htons(0);
708 if ((nn = resize_packet(header, (size_t)n, pheader, plen)))
709 {
Simon Kelley572b41e2011-02-18 18:11:18 +0000710 header->hb3 &= ~(HB3_QR | HB3_TC);
Simon Kelley83349b82014-02-10 21:02:01 +0000711 forward_query(-1, NULL, NULL, 0, header, nn, now, forward, 0);
Simon Kelley1a6bca82008-07-11 11:11:42 +0100712 return;
713 }
714 }
715 }
Simon Kelley3a237152013-12-12 12:15:50 +0000716
717 server = forward->sentto;
Simon Kelley1a6bca82008-07-11 11:11:42 +0100718
719 if ((forward->sentto->flags & SERV_TYPE) == 0)
720 {
Simon Kelley572b41e2011-02-18 18:11:18 +0000721 if (RCODE(header) == SERVFAIL || RCODE(header) == REFUSED)
Simon Kelley1a6bca82008-07-11 11:11:42 +0100722 server = NULL;
723 else
724 {
725 struct server *last_server;
Simon Kelley832af0b2007-01-21 20:01:28 +0000726
Simon Kelley1a6bca82008-07-11 11:11:42 +0100727 /* find good server by address if possible, otherwise assume the last one we sent to */
728 for (last_server = daemon->servers; last_server; last_server = last_server->next)
729 if (!(last_server->flags & (SERV_LITERAL_ADDRESS | SERV_HAS_DOMAIN | SERV_FOR_NODOTS | SERV_NO_ADDR)) &&
730 sockaddr_isequal(&last_server->addr, &serveraddr))
731 {
732 server = last_server;
733 break;
734 }
735 }
Simon Kelley28866e92011-02-14 20:19:14 +0000736 if (!option_bool(OPT_ALL_SERVERS))
Simon Kelley1a6bca82008-07-11 11:11:42 +0100737 daemon->last_server = server;
738 }
Simon Kelley3a237152013-12-12 12:15:50 +0000739
Simon Kelley1a6bca82008-07-11 11:11:42 +0100740 /* If the answer is an error, keep the forward record in place in case
741 we get a good reply from another server. Kill it when we've
742 had replies from all to avoid filling the forwarding table when
743 everything is broken */
744 if (forward->forwardall == 0 || --forward->forwardall == 1 ||
Simon Kelley572b41e2011-02-18 18:11:18 +0000745 (RCODE(header) != REFUSED && RCODE(header) != SERVFAIL))
Simon Kelley1a6bca82008-07-11 11:11:42 +0100746 {
Simon Kelley3a237152013-12-12 12:15:50 +0000747 int check_rebind = 0, no_cache_dnssec = 0, cache_secure = 0;
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100748
Simon Kelley3a237152013-12-12 12:15:50 +0000749 if (option_bool(OPT_NO_REBIND))
750 check_rebind = !(forward->flags & FREC_NOREBIND);
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100751
Simon Kelley3a237152013-12-12 12:15:50 +0000752 /* Don't cache replies where DNSSEC validation was turned off, either
753 the upstream server told us so, or the original query specified it. */
754 if ((header->hb4 & HB4_CD) || (forward->flags & FREC_CHECKING_DISABLED))
755 no_cache_dnssec = 1;
756
757#ifdef HAVE_DNSSEC
758 if (option_bool(OPT_DNSSEC_VALID) && !(forward->flags & FREC_CHECKING_DISABLED))
759 {
Simon Kelley9d633042013-12-13 15:36:55 +0000760 int status;
Simon Kelley0fc2f312014-01-08 10:26:58 +0000761
762 /* We've had a reply already, which we're validating. Ignore this duplicate */
Simon Kelleye0c0ad32014-01-16 22:42:07 +0000763 if (forward->blocking_query)
Simon Kelley0fc2f312014-01-08 10:26:58 +0000764 return;
Simon Kelley9d633042013-12-13 15:36:55 +0000765
Simon Kelley871417d2014-01-08 11:22:32 +0000766 if (header->hb3 & HB3_TC)
767 {
768 /* Truncated answer can't be validated.
Simon Kelley5d3b87a2014-01-20 11:57:23 +0000769 If this is an answer to a DNSSEC-generated query, we still
770 need to get the client to retry over TCP, so return
771 an answer with the TC bit set, even if the actual answer fits.
772 */
773 status = STAT_TRUNCATED;
Simon Kelley871417d2014-01-08 11:22:32 +0000774 }
775 else if (forward->flags & FREC_DNSKEY_QUERY)
Simon Kelley8d718cb2014-02-03 16:27:37 +0000776 status = dnssec_validate_by_ds(now, header, n, daemon->namebuff, daemon->keyname, forward->class);
Simon Kelleyc3e0b9b2013-12-31 13:50:39 +0000777 else if (forward->flags & FREC_DS_QUERY)
778 status = dnssec_validate_ds(now, header, n, daemon->namebuff, daemon->keyname, forward->class);
Simon Kelley9d633042013-12-13 15:36:55 +0000779 else
Simon Kelley0fc2f312014-01-08 10:26:58 +0000780 status = dnssec_validate_reply(now, header, n, daemon->namebuff, daemon->keyname, &forward->class);
Simon Kelley7fa836e2014-02-10 20:11:24 +0000781
Simon Kelley3a237152013-12-12 12:15:50 +0000782 /* Can't validate, as we're missing key data. Put this
783 answer aside, whilst we get that. */
784 if (status == STAT_NEED_DS || status == STAT_NEED_KEY)
785 {
Simon Kelley7fa836e2014-02-10 20:11:24 +0000786 struct frec *new, *orig;
Simon Kelley0fc2f312014-01-08 10:26:58 +0000787
Simon Kelley7fa836e2014-02-10 20:11:24 +0000788 /* Free any saved query */
789 if (forward->stash)
790 blockdata_free(forward->stash);
791
792 /* Now save reply pending receipt of key data */
793 if (!(forward->stash = blockdata_alloc((char *)header, n)))
794 return;
795 forward->stash_len = n;
796
797 anotherkey:
798 /* Find the original query that started it all.... */
799 for (orig = forward; orig->dependent; orig = orig->dependent);
800
801 if (--orig->work_counter == 0 || !(new = get_new_frec(now, NULL, 1)))
802 status = STAT_INSECURE;
803 else
Simon Kelley3a237152013-12-12 12:15:50 +0000804 {
Simon Kelley7fa836e2014-02-10 20:11:24 +0000805 int fd;
Simon Kelley0fc2f312014-01-08 10:26:58 +0000806 struct frec *next = new->next;
807 *new = *forward; /* copy everything, then overwrite */
808 new->next = next;
Simon Kelley0fc2f312014-01-08 10:26:58 +0000809 new->blocking_query = NULL;
Simon Kelleyf1668d22014-01-08 16:53:27 +0000810 new->rfd4 = NULL;
811#ifdef HAVE_IPV6
812 new->rfd6 = NULL;
813#endif
Simon Kelley0fc2f312014-01-08 10:26:58 +0000814 new->flags &= ~(FREC_DNSKEY_QUERY | FREC_DS_QUERY);
Simon Kelley9d633042013-12-13 15:36:55 +0000815
Simon Kelley7fa836e2014-02-10 20:11:24 +0000816 new->dependent = forward; /* to find query awaiting new one. */
817 forward->blocking_query = new; /* for garbage cleaning */
818 /* validate routines leave name of required record in daemon->keyname */
819 if (status == STAT_NEED_KEY)
820 {
821 new->flags |= FREC_DNSKEY_QUERY;
822 nn = dnssec_generate_query(header, ((char *) header) + daemon->packet_buff_sz,
823 daemon->keyname, forward->class, T_DNSKEY, &server->addr);
824 }
825 else
826 {
827 new->flags |= FREC_DS_QUERY;
828 nn = dnssec_generate_query(header,((char *) header) + daemon->packet_buff_sz,
829 daemon->keyname, forward->class, T_DS, &server->addr);
830 }
831 if ((hash = hash_questions(header, nn, daemon->namebuff)))
832 memcpy(new->hash, hash, HASH_SIZE);
833 new->new_id = get_id();
834 header->id = htons(new->new_id);
835 /* Save query for retransmission */
836 new->stash = blockdata_alloc((char *)header, nn);
837 new->stash_len = nn;
Simon Kelleye0c0ad32014-01-16 22:42:07 +0000838
Simon Kelley7fa836e2014-02-10 20:11:24 +0000839 /* Don't resend this. */
840 daemon->srv_save = NULL;
841
842 if (server->sfd)
843 fd = server->sfd->fd;
Simon Kelleye0c0ad32014-01-16 22:42:07 +0000844 else
Simon Kelley3a237152013-12-12 12:15:50 +0000845 {
Simon Kelley7fa836e2014-02-10 20:11:24 +0000846 fd = -1;
Simon Kelley3a237152013-12-12 12:15:50 +0000847#ifdef HAVE_IPV6
Simon Kelley7fa836e2014-02-10 20:11:24 +0000848 if (server->addr.sa.sa_family == AF_INET6)
Simon Kelleyf1668d22014-01-08 16:53:27 +0000849 {
Simon Kelley7fa836e2014-02-10 20:11:24 +0000850 if (new->rfd6 || (new->rfd6 = allocate_rfd(AF_INET6)))
851 fd = new->rfd6->fd;
852 }
853 else
854#endif
855 {
856 if (new->rfd4 || (new->rfd4 = allocate_rfd(AF_INET)))
857 fd = new->rfd4->fd;
Simon Kelleyf1668d22014-01-08 16:53:27 +0000858 }
Simon Kelley3a237152013-12-12 12:15:50 +0000859 }
Simon Kelley7fa836e2014-02-10 20:11:24 +0000860
861 if (fd != -1)
862 {
863 while (sendto(fd, (char *)header, nn, 0, &server->addr.sa, sa_len(&server->addr)) == -1 && retry_send());
864 server->queries++;
865 }
866
867 return;
Simon Kelley3a237152013-12-12 12:15:50 +0000868 }
Simon Kelley3a237152013-12-12 12:15:50 +0000869 }
870
871 /* Ok, we reached far enough up the chain-of-trust that we can validate something.
872 Now wind back down, pulling back answers which wouldn't previously validate
Simon Kelley7fa836e2014-02-10 20:11:24 +0000873 and validate them with the new data. Note that if an answer needs multiple
874 keys to validate, we may find another key is needed, in which case we set off
875 down another branch of the tree. Once we get to the original answer
876 (FREC_DNSSEC_QUERY not set) and it validates, return it to the original requestor. */
Simon Kelley0744ca62014-01-25 16:40:15 +0000877 while (forward->dependent)
Simon Kelley3a237152013-12-12 12:15:50 +0000878 {
Simon Kelley0744ca62014-01-25 16:40:15 +0000879 struct frec *prev = forward->dependent;
880 free_frec(forward);
881 forward = prev;
882 forward->blocking_query = NULL; /* already gone */
883 blockdata_retrieve(forward->stash, forward->stash_len, (void *)header);
884 n = forward->stash_len;
Simon Kelley0fc2f312014-01-08 10:26:58 +0000885
Simon Kelley0fc2f312014-01-08 10:26:58 +0000886 if (status == STAT_SECURE)
Simon Kelley0fc2f312014-01-08 10:26:58 +0000887 {
Simon Kelley0744ca62014-01-25 16:40:15 +0000888 if (forward->flags & FREC_DNSKEY_QUERY)
889 status = dnssec_validate_by_ds(now, header, n, daemon->namebuff, daemon->keyname, forward->class);
890 else if (forward->flags & FREC_DS_QUERY)
891 status = dnssec_validate_ds(now, header, n, daemon->namebuff, daemon->keyname, forward->class);
892 else
893 status = dnssec_validate_reply(now, header, n, daemon->namebuff, daemon->keyname, &forward->class);
894
895 if (status == STAT_NEED_DS || status == STAT_NEED_KEY)
Simon Kelley7fa836e2014-02-10 20:11:24 +0000896 goto anotherkey;
Simon Kelley3a237152013-12-12 12:15:50 +0000897 }
898 }
Simon Kelley5d3b87a2014-01-20 11:57:23 +0000899
900 if (status == STAT_TRUNCATED)
Simon Kelley0744ca62014-01-25 16:40:15 +0000901 header->hb3 |= HB3_TC;
Simon Kelley5d3b87a2014-01-20 11:57:23 +0000902 else
Simon Kelley7fa836e2014-02-10 20:11:24 +0000903 {
904 char *result;
905
906 if (forward->work_counter == 0)
907 result = "ABANDONED";
908 else
909 result = (status == STAT_SECURE ? "SECURE" : (status == STAT_INSECURE ? "INSECURE" : "BOGUS"));
910
911 log_query(F_KEYTAG | F_SECSTAT, "result", NULL, result);
912 }
Simon Kelley5d3b87a2014-01-20 11:57:23 +0000913
Simon Kelley0fc2f312014-01-08 10:26:58 +0000914 no_cache_dnssec = 0;
Simon Kelley5d3b87a2014-01-20 11:57:23 +0000915
Simon Kelley3a237152013-12-12 12:15:50 +0000916 if (status == STAT_SECURE)
917 cache_secure = 1;
Simon Kelley3a237152013-12-12 12:15:50 +0000918 else if (status == STAT_BOGUS)
919 no_cache_dnssec = 1;
920 }
Simon Kelley83349b82014-02-10 21:02:01 +0000921#endif
922
923 /* restore CD bit to the value in the query */
924 if (forward->flags & FREC_CHECKING_DISABLED)
925 header->hb4 |= HB4_CD;
926 else
927 header->hb4 &= ~HB4_CD;
Simon Kelley3a237152013-12-12 12:15:50 +0000928
929 if ((nn = process_reply(header, now, server, (size_t)n, check_rebind, no_cache_dnssec, cache_secure,
Simon Kelley83349b82014-02-10 21:02:01 +0000930 forward->flags & FREC_AD_QUESTION, forward->flags & FREC_HAS_SUBNET, &forward->source)))
Simon Kelley832af0b2007-01-21 20:01:28 +0000931 {
Simon Kelley1a6bca82008-07-11 11:11:42 +0100932 header->id = htons(forward->orig_id);
Simon Kelley572b41e2011-02-18 18:11:18 +0000933 header->hb4 |= HB4_RA; /* recursion if available */
Simon Kelley54dd3932012-06-20 11:23:38 +0100934 send_from(forward->fd, option_bool(OPT_NOWILD) || option_bool (OPT_CLEVERBIND), daemon->packet, nn,
Simon Kelley50303b12012-04-04 22:13:17 +0100935 &forward->source, &forward->dest, forward->iface);
Simon Kelley832af0b2007-01-21 20:01:28 +0000936 }
Simon Kelley1a6bca82008-07-11 11:11:42 +0100937 free_frec(forward); /* cancel */
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000938 }
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000939}
Simon Kelley44a2a312004-03-10 20:04:35 +0000940
Simon Kelley1a6bca82008-07-11 11:11:42 +0100941
Simon Kelley5aabfc72007-08-29 11:24:47 +0100942void receive_query(struct listener *listen, time_t now)
Simon Kelley44a2a312004-03-10 20:04:35 +0000943{
Simon Kelley572b41e2011-02-18 18:11:18 +0000944 struct dns_header *header = (struct dns_header *)daemon->packet;
Simon Kelley44a2a312004-03-10 20:04:35 +0000945 union mysockaddr source_addr;
Simon Kelleyc1bb8502004-08-11 18:40:17 +0100946 unsigned short type;
Simon Kelley44a2a312004-03-10 20:04:35 +0000947 struct all_addr dst_addr;
Simon Kelleyf6b7dc42005-01-23 12:06:08 +0000948 struct in_addr netmask, dst_addr_4;
Simon Kelleycdeda282006-03-16 20:16:06 +0000949 size_t m;
950 ssize_t n;
Vladislav Grishenko3b195962013-11-26 11:08:21 +0000951 int if_index = 0, auth_dns = 0;
952#ifdef HAVE_AUTH
953 int local_auth = 0;
954#endif
Simon Kelley44a2a312004-03-10 20:04:35 +0000955 struct iovec iov[1];
956 struct msghdr msg;
957 struct cmsghdr *cmptr;
Simon Kelley44a2a312004-03-10 20:04:35 +0000958 union {
959 struct cmsghdr align; /* this ensures alignment */
960#ifdef HAVE_IPV6
961 char control6[CMSG_SPACE(sizeof(struct in6_pktinfo))];
962#endif
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100963#if defined(HAVE_LINUX_NETWORK)
Simon Kelley44a2a312004-03-10 20:04:35 +0000964 char control[CMSG_SPACE(sizeof(struct in_pktinfo))];
Simon Kelley824af852008-02-12 20:43:05 +0000965#elif defined(IP_RECVDSTADDR) && defined(HAVE_SOLARIS_NETWORK)
966 char control[CMSG_SPACE(sizeof(struct in_addr)) +
967 CMSG_SPACE(sizeof(unsigned int))];
Simon Kelley44a2a312004-03-10 20:04:35 +0000968#elif defined(IP_RECVDSTADDR)
969 char control[CMSG_SPACE(sizeof(struct in_addr)) +
970 CMSG_SPACE(sizeof(struct sockaddr_dl))];
971#endif
972 } control_u;
Simon Kelley2329bef2013-12-03 13:41:16 +0000973#ifdef HAVE_IPV6
974 /* Can always get recvd interface for IPv6 */
975 int check_dst = !option_bool(OPT_NOWILD) || listen->family == AF_INET6;
976#else
977 int check_dst = !option_bool(OPT_NOWILD);
978#endif
979
Simon Kelleycdeda282006-03-16 20:16:06 +0000980 /* packet buffer overwritten */
981 daemon->srv_save = NULL;
982
Simon Kelley4f7b3042012-11-28 21:27:02 +0000983 dst_addr_4.s_addr = 0;
984 netmask.s_addr = 0;
985
Simon Kelley7e5664b2013-04-05 16:57:41 +0100986 if (option_bool(OPT_NOWILD) && listen->iface)
Simon Kelleyf6b7dc42005-01-23 12:06:08 +0000987 {
Simon Kelley4f7b3042012-11-28 21:27:02 +0000988 auth_dns = listen->iface->dns_auth;
989
990 if (listen->family == AF_INET)
991 {
992 dst_addr_4 = listen->iface->addr.in.sin_addr;
993 netmask = listen->iface->netmask;
994 }
Simon Kelleyf6b7dc42005-01-23 12:06:08 +0000995 }
Simon Kelley4f7b3042012-11-28 21:27:02 +0000996
Simon Kelley3be34542004-09-11 19:12:13 +0100997 iov[0].iov_base = daemon->packet;
998 iov[0].iov_len = daemon->edns_pktsz;
Simon Kelley44a2a312004-03-10 20:04:35 +0000999
1000 msg.msg_control = control_u.control;
1001 msg.msg_controllen = sizeof(control_u);
1002 msg.msg_flags = 0;
1003 msg.msg_name = &source_addr;
1004 msg.msg_namelen = sizeof(source_addr);
1005 msg.msg_iov = iov;
1006 msg.msg_iovlen = 1;
1007
Simon Kelleyde379512004-06-22 20:23:33 +01001008 if ((n = recvmsg(listen->fd, &msg, 0)) == -1)
Simon Kelley3be34542004-09-11 19:12:13 +01001009 return;
Simon Kelley44a2a312004-03-10 20:04:35 +00001010
Simon Kelley572b41e2011-02-18 18:11:18 +00001011 if (n < (int)sizeof(struct dns_header) ||
Simon Kelley5e9e0ef2006-04-17 14:24:29 +01001012 (msg.msg_flags & MSG_TRUNC) ||
Simon Kelley572b41e2011-02-18 18:11:18 +00001013 (header->hb3 & HB3_QR))
Simon Kelley3be34542004-09-11 19:12:13 +01001014 return;
Simon Kelley44a2a312004-03-10 20:04:35 +00001015
Simon Kelley26128d22004-11-14 16:43:54 +00001016 source_addr.sa.sa_family = listen->family;
1017#ifdef HAVE_IPV6
1018 if (listen->family == AF_INET6)
Simon Kelley5e9e0ef2006-04-17 14:24:29 +01001019 source_addr.in6.sin6_flowinfo = 0;
Simon Kelley26128d22004-11-14 16:43:54 +00001020#endif
Simon Kelley28866e92011-02-14 20:19:14 +00001021
Simon Kelley2329bef2013-12-03 13:41:16 +00001022 if (check_dst)
Simon Kelley44a2a312004-03-10 20:04:35 +00001023 {
Simon Kelley8a911cc2004-03-16 18:35:52 +00001024 struct ifreq ifr;
1025
Simon Kelley26128d22004-11-14 16:43:54 +00001026 if (msg.msg_controllen < sizeof(struct cmsghdr))
1027 return;
1028
Simon Kelley5e9e0ef2006-04-17 14:24:29 +01001029#if defined(HAVE_LINUX_NETWORK)
Simon Kelley26128d22004-11-14 16:43:54 +00001030 if (listen->family == AF_INET)
1031 for (cmptr = CMSG_FIRSTHDR(&msg); cmptr; cmptr = CMSG_NXTHDR(&msg, cmptr))
Simon Kelleyc72daea2012-01-05 21:33:27 +00001032 if (cmptr->cmsg_level == IPPROTO_IP && cmptr->cmsg_type == IP_PKTINFO)
Simon Kelley26128d22004-11-14 16:43:54 +00001033 {
Simon Kelley8ef5ada2010-06-03 19:42:45 +01001034 union {
1035 unsigned char *c;
1036 struct in_pktinfo *p;
1037 } p;
1038 p.c = CMSG_DATA(cmptr);
1039 dst_addr_4 = dst_addr.addr.addr4 = p.p->ipi_spec_dst;
1040 if_index = p.p->ipi_ifindex;
Simon Kelley26128d22004-11-14 16:43:54 +00001041 }
1042#elif defined(IP_RECVDSTADDR) && defined(IP_RECVIF)
1043 if (listen->family == AF_INET)
1044 {
1045 for (cmptr = CMSG_FIRSTHDR(&msg); cmptr; cmptr = CMSG_NXTHDR(&msg, cmptr))
Simon Kelley8ef5ada2010-06-03 19:42:45 +01001046 {
1047 union {
1048 unsigned char *c;
1049 unsigned int *i;
1050 struct in_addr *a;
1051#ifndef HAVE_SOLARIS_NETWORK
1052 struct sockaddr_dl *s;
Simon Kelley824af852008-02-12 20:43:05 +00001053#endif
Simon Kelley8ef5ada2010-06-03 19:42:45 +01001054 } p;
1055 p.c = CMSG_DATA(cmptr);
1056 if (cmptr->cmsg_level == IPPROTO_IP && cmptr->cmsg_type == IP_RECVDSTADDR)
1057 dst_addr_4 = dst_addr.addr.addr4 = *(p.a);
1058 else if (cmptr->cmsg_level == IPPROTO_IP && cmptr->cmsg_type == IP_RECVIF)
1059#ifdef HAVE_SOLARIS_NETWORK
1060 if_index = *(p.i);
1061#else
1062 if_index = p.s->sdl_index;
1063#endif
1064 }
Simon Kelley26128d22004-11-14 16:43:54 +00001065 }
1066#endif
1067
1068#ifdef HAVE_IPV6
1069 if (listen->family == AF_INET6)
1070 {
1071 for (cmptr = CMSG_FIRSTHDR(&msg); cmptr; cmptr = CMSG_NXTHDR(&msg, cmptr))
Simon Kelleyc72daea2012-01-05 21:33:27 +00001072 if (cmptr->cmsg_level == IPPROTO_IPV6 && cmptr->cmsg_type == daemon->v6pktinfo)
Simon Kelley26128d22004-11-14 16:43:54 +00001073 {
Simon Kelley8ef5ada2010-06-03 19:42:45 +01001074 union {
1075 unsigned char *c;
1076 struct in6_pktinfo *p;
1077 } p;
1078 p.c = CMSG_DATA(cmptr);
1079
1080 dst_addr.addr.addr6 = p.p->ipi6_addr;
1081 if_index = p.p->ipi6_ifindex;
Simon Kelley26128d22004-11-14 16:43:54 +00001082 }
1083 }
1084#endif
1085
1086 /* enforce available interface configuration */
1087
Simon Kelleye25db1f2013-01-29 22:10:26 +00001088 if (!indextoname(listen->fd, if_index, ifr.ifr_name))
Simon Kelley832af0b2007-01-21 20:01:28 +00001089 return;
1090
Simon Kelleye25db1f2013-01-29 22:10:26 +00001091 if (!iface_check(listen->family, &dst_addr, ifr.ifr_name, &auth_dns))
1092 {
1093 if (!option_bool(OPT_CLEVERBIND))
Simon Kelley115ac3e2013-05-20 11:28:32 +01001094 enumerate_interfaces(0);
Simon Kelley3f2873d2013-05-14 11:28:47 +01001095 if (!loopback_exception(listen->fd, listen->family, &dst_addr, ifr.ifr_name) &&
1096 !label_exception(if_index, listen->family, &dst_addr))
Simon Kelleye25db1f2013-01-29 22:10:26 +00001097 return;
1098 }
1099
Simon Kelley552af8b2012-02-29 20:10:31 +00001100 if (listen->family == AF_INET && option_bool(OPT_LOCALISE))
1101 {
1102 struct irec *iface;
1103
1104 /* get the netmask of the interface whch has the address we were sent to.
1105 This is no neccessarily the interface we arrived on. */
1106
1107 for (iface = daemon->interfaces; iface; iface = iface->next)
1108 if (iface->addr.sa.sa_family == AF_INET &&
1109 iface->addr.in.sin_addr.s_addr == dst_addr_4.s_addr)
1110 break;
1111
1112 /* interface may be new */
Simon Kelleye25db1f2013-01-29 22:10:26 +00001113 if (!iface && !option_bool(OPT_CLEVERBIND))
Simon Kelley115ac3e2013-05-20 11:28:32 +01001114 enumerate_interfaces(0);
Simon Kelley552af8b2012-02-29 20:10:31 +00001115
1116 for (iface = daemon->interfaces; iface; iface = iface->next)
1117 if (iface->addr.sa.sa_family == AF_INET &&
1118 iface->addr.in.sin_addr.s_addr == dst_addr_4.s_addr)
1119 break;
1120
1121 /* If we failed, abandon localisation */
1122 if (iface)
1123 netmask = iface->netmask;
1124 else
1125 dst_addr_4.s_addr = 0;
1126 }
Simon Kelley44a2a312004-03-10 20:04:35 +00001127 }
1128
Simon Kelleycdeda282006-03-16 20:16:06 +00001129 if (extract_request(header, (size_t)n, daemon->namebuff, &type))
Simon Kelley44a2a312004-03-10 20:04:35 +00001130 {
Simon Kelleyb485ed92013-10-18 22:00:39 +01001131#ifdef HAVE_AUTH
1132 struct auth_zone *zone;
1133#endif
Simon Kelley610e7822014-02-06 14:45:17 +00001134 char *types = querystr(auth_dns ? "auth" : "query", type);
1135
Simon Kelley44a2a312004-03-10 20:04:35 +00001136 if (listen->family == AF_INET)
Simon Kelley3be34542004-09-11 19:12:13 +01001137 log_query(F_QUERY | F_IPV4 | F_FORWARD, daemon->namebuff,
Simon Kelley1a6bca82008-07-11 11:11:42 +01001138 (struct all_addr *)&source_addr.in.sin_addr, types);
Simon Kelley44a2a312004-03-10 20:04:35 +00001139#ifdef HAVE_IPV6
1140 else
Simon Kelley3be34542004-09-11 19:12:13 +01001141 log_query(F_QUERY | F_IPV6 | F_FORWARD, daemon->namebuff,
Simon Kelley1a6bca82008-07-11 11:11:42 +01001142 (struct all_addr *)&source_addr.in6.sin6_addr, types);
Simon Kelley44a2a312004-03-10 20:04:35 +00001143#endif
Simon Kelley44a2a312004-03-10 20:04:35 +00001144
Simon Kelley4820dce2012-12-18 18:30:30 +00001145#ifdef HAVE_AUTH
Simon Kelleyb485ed92013-10-18 22:00:39 +01001146 /* find queries for zones we're authoritative for, and answer them directly */
Simon Kelley6008bdb2013-10-21 21:47:03 +01001147 if (!auth_dns)
1148 for (zone = daemon->auth_zones; zone; zone = zone->next)
1149 if (in_zone(zone, daemon->namebuff, NULL))
1150 {
1151 auth_dns = 1;
1152 local_auth = 1;
1153 break;
1154 }
Simon Kelleyb485ed92013-10-18 22:00:39 +01001155#endif
1156 }
1157
1158#ifdef HAVE_AUTH
Simon Kelley4f7b3042012-11-28 21:27:02 +00001159 if (auth_dns)
Simon Kelley824af852008-02-12 20:43:05 +00001160 {
Simon Kelley60b68062014-01-08 12:10:28 +00001161 m = answer_auth(header, ((char *) header) + daemon->packet_buff_sz, (size_t)n, now, &source_addr, local_auth);
Simon Kelley4f7b3042012-11-28 21:27:02 +00001162 if (m >= 1)
Simon Kelleyb485ed92013-10-18 22:00:39 +01001163 {
1164 send_from(listen->fd, option_bool(OPT_NOWILD) || option_bool(OPT_CLEVERBIND),
1165 (char *)header, m, &source_addr, &dst_addr, if_index);
1166 daemon->auth_answer++;
1167 }
Simon Kelley824af852008-02-12 20:43:05 +00001168 }
Simon Kelley44a2a312004-03-10 20:04:35 +00001169 else
Simon Kelley4820dce2012-12-18 18:30:30 +00001170#endif
Simon Kelley4f7b3042012-11-28 21:27:02 +00001171 {
Simon Kelley83349b82014-02-10 21:02:01 +00001172 int ad_reqd;
Simon Kelley60b68062014-01-08 12:10:28 +00001173 m = answer_request(header, ((char *) header) + daemon->packet_buff_sz, (size_t)n,
Simon Kelley83349b82014-02-10 21:02:01 +00001174 dst_addr_4, netmask, now, &ad_reqd);
Simon Kelley4f7b3042012-11-28 21:27:02 +00001175
1176 if (m >= 1)
1177 {
1178 send_from(listen->fd, option_bool(OPT_NOWILD) || option_bool(OPT_CLEVERBIND),
1179 (char *)header, m, &source_addr, &dst_addr, if_index);
1180 daemon->local_answer++;
1181 }
1182 else if (forward_query(listen->fd, &source_addr, &dst_addr, if_index,
Simon Kelley83349b82014-02-10 21:02:01 +00001183 header, (size_t)n, now, NULL, ad_reqd))
Simon Kelley4f7b3042012-11-28 21:27:02 +00001184 daemon->queries_forwarded++;
1185 else
1186 daemon->local_answer++;
1187 }
Simon Kelley44a2a312004-03-10 20:04:35 +00001188}
1189
Simon Kelley7d7b7b32014-01-08 15:53:35 +00001190#ifdef HAVE_DNSSEC
Simon Kelley7fa836e2014-02-10 20:11:24 +00001191static int tcp_key_recurse(time_t now, int status, struct dns_header *header, size_t n,
1192 int class, char *name, char *keyname, struct server *server, int *keycount)
Simon Kelley7d7b7b32014-01-08 15:53:35 +00001193{
1194 /* Recurse up the key heirarchy */
Simon Kelley7d7b7b32014-01-08 15:53:35 +00001195 int new_status;
Simon Kelley7d7b7b32014-01-08 15:53:35 +00001196
Simon Kelley7fa836e2014-02-10 20:11:24 +00001197 /* limit the amount of work we do, to avoid cycling forever on loops in the DNS */
1198 if (--(*keycount) == 0)
1199 return STAT_INSECURE;
Simon Kelley7d7b7b32014-01-08 15:53:35 +00001200
Simon Kelley7fa836e2014-02-10 20:11:24 +00001201 if (status == STAT_NEED_KEY)
1202 new_status = dnssec_validate_by_ds(now, header, n, name, keyname, class);
1203 else if (status == STAT_NEED_DS)
1204 new_status = dnssec_validate_ds(now, header, n, name, keyname, class);
Simon Kelley7d7b7b32014-01-08 15:53:35 +00001205 else
Simon Kelley7fa836e2014-02-10 20:11:24 +00001206 new_status = dnssec_validate_reply(now, header, n, name, keyname, &class);
1207
1208 /* Can't validate because we need a key/DS whose name now in keyname.
1209 Make query for same, and recurse to validate */
1210 if (new_status == STAT_NEED_DS || new_status == STAT_NEED_KEY)
Simon Kelley7d7b7b32014-01-08 15:53:35 +00001211 {
Simon Kelley7fa836e2014-02-10 20:11:24 +00001212 size_t m;
1213 unsigned char *packet = whine_malloc(65536 + MAXDNAME + RRFIXEDSZ + sizeof(u16));
1214 unsigned char *payload = &packet[2];
1215 struct dns_header *new_header = (struct dns_header *)payload;
1216 u16 *length = (u16 *)packet;
1217 unsigned char c1, c2;
1218
1219 if (!packet)
1220 return STAT_INSECURE;
1221
1222 another_tcp_key:
1223 m = dnssec_generate_query(new_header, ((char *) new_header) + 65536, keyname, class,
1224 new_status == STAT_NEED_KEY ? T_DNSKEY : T_DS, &server->addr);
Simon Kelley7d7b7b32014-01-08 15:53:35 +00001225
Simon Kelley7fa836e2014-02-10 20:11:24 +00001226 *length = htons(m);
1227
1228 if (!read_write(server->tcpfd, packet, m + sizeof(u16), 0) ||
1229 !read_write(server->tcpfd, &c1, 1, 1) ||
1230 !read_write(server->tcpfd, &c2, 1, 1) ||
1231 !read_write(server->tcpfd, payload, (c1 << 8) | c2, 1))
1232 new_status = STAT_INSECURE;
Simon Kelley7d7b7b32014-01-08 15:53:35 +00001233 else
Simon Kelley7d7b7b32014-01-08 15:53:35 +00001234 {
Simon Kelley7fa836e2014-02-10 20:11:24 +00001235 m = (c1 << 8) | c2;
1236
1237 if (tcp_key_recurse(now, new_status, new_header, m, class, name, keyname, server, keycount) == STAT_SECURE)
Simon Kelley7d7b7b32014-01-08 15:53:35 +00001238 {
Simon Kelley7fa836e2014-02-10 20:11:24 +00001239 /* Reached a validated record, now try again at this level.
1240 Note that we may get ANOTHER NEED_* if an answer needs more than one key.
1241 If so, go round again. */
Simon Kelley7d7b7b32014-01-08 15:53:35 +00001242
Simon Kelley7fa836e2014-02-10 20:11:24 +00001243 if (status == STAT_NEED_KEY)
1244 new_status = dnssec_validate_by_ds(now, header, n, name, keyname, class);
1245 else if (status == STAT_NEED_DS)
1246 new_status = dnssec_validate_ds(now, header, n, name, keyname, class);
1247 else
1248 new_status = dnssec_validate_reply(now, header, n, name, keyname, &class);
1249
Simon Kelley7d7b7b32014-01-08 15:53:35 +00001250 if (new_status == STAT_NEED_DS || new_status == STAT_NEED_KEY)
Simon Kelley7fa836e2014-02-10 20:11:24 +00001251 goto another_tcp_key;
Simon Kelley7d7b7b32014-01-08 15:53:35 +00001252 }
1253 }
Simon Kelley7fa836e2014-02-10 20:11:24 +00001254
1255 free(packet);
Simon Kelley7d7b7b32014-01-08 15:53:35 +00001256 }
Simon Kelley7fa836e2014-02-10 20:11:24 +00001257
Simon Kelley7d7b7b32014-01-08 15:53:35 +00001258 return new_status;
1259}
1260#endif
1261
1262
Simon Kelleyfeba5c12004-07-27 20:28:58 +01001263/* The daemon forks before calling this: it should deal with one connection,
1264 blocking as neccessary, and then return. Note, need to be a bit careful
1265 about resources for debug mode, when the fork is suppressed: that's
1266 done by the caller. */
Simon Kelley5aabfc72007-08-29 11:24:47 +01001267unsigned char *tcp_request(int confd, time_t now,
Simon Kelley4f7b3042012-11-28 21:27:02 +00001268 union mysockaddr *local_addr, struct in_addr netmask, int auth_dns)
Simon Kelleyfeba5c12004-07-27 20:28:58 +01001269{
Simon Kelley28866e92011-02-14 20:19:14 +00001270 size_t size = 0;
1271 int norebind = 0;
Vladislav Grishenko3b195962013-11-26 11:08:21 +00001272#ifdef HAVE_AUTH
Simon Kelley19b16892013-10-20 10:19:39 +01001273 int local_auth = 0;
Vladislav Grishenko3b195962013-11-26 11:08:21 +00001274#endif
Simon Kelley83349b82014-02-10 21:02:01 +00001275 int checking_disabled, ad_question, check_subnet, no_cache_dnssec = 0, cache_secure = 0;
Simon Kelleycdeda282006-03-16 20:16:06 +00001276 size_t m;
Simon Kelleyee86ce62012-12-07 11:54:46 +00001277 unsigned short qtype;
1278 unsigned int gotname;
Simon Kelleyfeba5c12004-07-27 20:28:58 +01001279 unsigned char c1, c2;
Simon Kelley4b5ea122013-04-22 10:18:26 +01001280 /* Max TCP packet + slop + size */
1281 unsigned char *packet = whine_malloc(65536 + MAXDNAME + RRFIXEDSZ + sizeof(u16));
1282 unsigned char *payload = &packet[2];
1283 /* largest field in header is 16-bits, so this is still sufficiently aligned */
1284 struct dns_header *header = (struct dns_header *)payload;
1285 u16 *length = (u16 *)packet;
Simon Kelley3be34542004-09-11 19:12:13 +01001286 struct server *last_server;
Simon Kelley7de060b2011-08-26 17:24:52 +01001287 struct in_addr dst_addr_4;
1288 union mysockaddr peer_addr;
1289 socklen_t peer_len = sizeof(union mysockaddr);
Simon Kelley3be34542004-09-11 19:12:13 +01001290
Simon Kelley7de060b2011-08-26 17:24:52 +01001291 if (getpeername(confd, (struct sockaddr *)&peer_addr, &peer_len) == -1)
1292 return packet;
1293
Simon Kelleyfeba5c12004-07-27 20:28:58 +01001294 while (1)
1295 {
1296 if (!packet ||
1297 !read_write(confd, &c1, 1, 1) || !read_write(confd, &c2, 1, 1) ||
1298 !(size = c1 << 8 | c2) ||
Simon Kelley4b5ea122013-04-22 10:18:26 +01001299 !read_write(confd, payload, size, 1))
Simon Kelleyfeba5c12004-07-27 20:28:58 +01001300 return packet;
1301
Simon Kelley572b41e2011-02-18 18:11:18 +00001302 if (size < (int)sizeof(struct dns_header))
Simon Kelleyfeba5c12004-07-27 20:28:58 +01001303 continue;
1304
Simon Kelleyed4c0762013-10-08 20:46:34 +01001305 check_subnet = 0;
1306
Simon Kelley28866e92011-02-14 20:19:14 +00001307 /* save state of "cd" flag in query */
Simon Kelley7d7b7b32014-01-08 15:53:35 +00001308 if ((checking_disabled = header->hb4 & HB4_CD))
1309 no_cache_dnssec = 1;
Simon Kelley28866e92011-02-14 20:19:14 +00001310
Simon Kelley3be34542004-09-11 19:12:13 +01001311 if ((gotname = extract_request(header, (unsigned int)size, daemon->namebuff, &qtype)))
Simon Kelleyfeba5c12004-07-27 20:28:58 +01001312 {
Simon Kelleyb485ed92013-10-18 22:00:39 +01001313#ifdef HAVE_AUTH
1314 struct auth_zone *zone;
1315#endif
Simon Kelley610e7822014-02-06 14:45:17 +00001316 char *types = querystr(auth_dns ? "auth" : "query", qtype);
Simon Kelley7de060b2011-08-26 17:24:52 +01001317
1318 if (peer_addr.sa.sa_family == AF_INET)
1319 log_query(F_QUERY | F_IPV4 | F_FORWARD, daemon->namebuff,
1320 (struct all_addr *)&peer_addr.in.sin_addr, types);
Simon Kelleyfeba5c12004-07-27 20:28:58 +01001321#ifdef HAVE_IPV6
Simon Kelley7de060b2011-08-26 17:24:52 +01001322 else
1323 log_query(F_QUERY | F_IPV6 | F_FORWARD, daemon->namebuff,
1324 (struct all_addr *)&peer_addr.in6.sin6_addr, types);
Simon Kelleyfeba5c12004-07-27 20:28:58 +01001325#endif
Simon Kelleyb485ed92013-10-18 22:00:39 +01001326
1327#ifdef HAVE_AUTH
1328 /* find queries for zones we're authoritative for, and answer them directly */
Simon Kelley6008bdb2013-10-21 21:47:03 +01001329 if (!auth_dns)
1330 for (zone = daemon->auth_zones; zone; zone = zone->next)
1331 if (in_zone(zone, daemon->namebuff, NULL))
1332 {
1333 auth_dns = 1;
1334 local_auth = 1;
1335 break;
1336 }
Simon Kelleyb485ed92013-10-18 22:00:39 +01001337#endif
Simon Kelleyfeba5c12004-07-27 20:28:58 +01001338 }
1339
Simon Kelley7de060b2011-08-26 17:24:52 +01001340 if (local_addr->sa.sa_family == AF_INET)
1341 dst_addr_4 = local_addr->in.sin_addr;
1342 else
1343 dst_addr_4.s_addr = 0;
1344
Simon Kelley4820dce2012-12-18 18:30:30 +00001345#ifdef HAVE_AUTH
Simon Kelley4f7b3042012-11-28 21:27:02 +00001346 if (auth_dns)
Simon Kelley19b16892013-10-20 10:19:39 +01001347 m = answer_auth(header, ((char *) header) + 65536, (size_t)size, now, &peer_addr, local_auth);
Simon Kelley4f7b3042012-11-28 21:27:02 +00001348 else
Simon Kelley4820dce2012-12-18 18:30:30 +00001349#endif
Simon Kelleyfeba5c12004-07-27 20:28:58 +01001350 {
Simon Kelley4f7b3042012-11-28 21:27:02 +00001351 /* m > 0 if answered from cache */
1352 m = answer_request(header, ((char *) header) + 65536, (size_t)size,
Simon Kelley83349b82014-02-10 21:02:01 +00001353 dst_addr_4, netmask, now, &ad_question);
Simon Kelleyfeba5c12004-07-27 20:28:58 +01001354
Simon Kelley4f7b3042012-11-28 21:27:02 +00001355 /* Do this by steam now we're not in the select() loop */
1356 check_log_writer(NULL);
1357
1358 if (m == 0)
Simon Kelleyfeba5c12004-07-27 20:28:58 +01001359 {
Simon Kelley4f7b3042012-11-28 21:27:02 +00001360 unsigned int flags = 0;
1361 struct all_addr *addrp = NULL;
1362 int type = 0;
1363 char *domain = NULL;
Simon Kelleyfeba5c12004-07-27 20:28:58 +01001364
Simon Kelley4f7b3042012-11-28 21:27:02 +00001365 if (option_bool(OPT_ADD_MAC))
1366 size = add_mac(header, size, ((char *) header) + 65536, &peer_addr);
Simon Kelleyed4c0762013-10-08 20:46:34 +01001367
1368 if (option_bool(OPT_CLIENT_SUBNET))
1369 {
1370 size_t new = add_source_addr(header, size, ((char *) header) + 65536, &peer_addr);
1371 if (size != new)
1372 {
1373 size = new;
1374 check_subnet = 1;
1375 }
1376 }
1377
Simon Kelley4f7b3042012-11-28 21:27:02 +00001378 if (gotname)
1379 flags = search_servers(now, &addrp, gotname, daemon->namebuff, &type, &domain, &norebind);
1380
1381 if (type != 0 || option_bool(OPT_ORDER) || !daemon->last_server)
1382 last_server = daemon->servers;
1383 else
1384 last_server = daemon->last_server;
1385
1386 if (!flags && last_server)
1387 {
1388 struct server *firstsendto = NULL;
Simon Kelley8a9be9e2014-01-25 23:17:21 +00001389#ifdef HAVE_DNSSEC
Simon Kelley703c7ff2014-01-25 23:46:23 +00001390 unsigned char *newhash, hash[HASH_SIZE];
Simon Kelley8a9be9e2014-01-25 23:17:21 +00001391 if ((newhash = hash_questions(header, (unsigned int)size, daemon->keyname)))
1392 memcpy(hash, newhash, HASH_SIZE);
1393#else
Simon Kelley4f7b3042012-11-28 21:27:02 +00001394 unsigned int crc = questions_crc(header, (unsigned int)size, daemon->namebuff);
Simon Kelley8a9be9e2014-01-25 23:17:21 +00001395#endif
Simon Kelley4f7b3042012-11-28 21:27:02 +00001396 /* Loop round available servers until we succeed in connecting to one.
1397 Note that this code subtley ensures that consecutive queries on this connection
1398 which can go to the same server, do so. */
1399 while (1)
Simon Kelleyfeba5c12004-07-27 20:28:58 +01001400 {
Simon Kelley4f7b3042012-11-28 21:27:02 +00001401 if (!firstsendto)
1402 firstsendto = last_server;
1403 else
1404 {
1405 if (!(last_server = last_server->next))
1406 last_server = daemon->servers;
1407
1408 if (last_server == firstsendto)
1409 break;
1410 }
1411
1412 /* server for wrong domain */
1413 if (type != (last_server->flags & SERV_TYPE) ||
1414 (type == SERV_HAS_DOMAIN && !hostname_isequal(domain, last_server->domain)))
Simon Kelley7de060b2011-08-26 17:24:52 +01001415 continue;
1416
Simon Kelley4f7b3042012-11-28 21:27:02 +00001417 if (last_server->tcpfd == -1)
1418 {
1419 if ((last_server->tcpfd = socket(last_server->addr.sa.sa_family, SOCK_STREAM, 0)) == -1)
1420 continue;
1421
1422 if ((!local_bind(last_server->tcpfd, &last_server->source_addr, last_server->interface, 1) ||
1423 connect(last_server->tcpfd, &last_server->addr.sa, sa_len(&last_server->addr)) == -1))
1424 {
1425 close(last_server->tcpfd);
1426 last_server->tcpfd = -1;
1427 continue;
1428 }
1429
Simon Kelley7d7b7b32014-01-08 15:53:35 +00001430#ifdef HAVE_DNSSEC
1431 if (option_bool(OPT_DNSSEC_VALID))
1432 {
1433 size = add_do_bit(header, size, ((char *) header) + 65536);
1434 header->hb4 |= HB4_CD;
1435 }
1436#endif
1437
Simon Kelley4f7b3042012-11-28 21:27:02 +00001438#ifdef HAVE_CONNTRACK
1439 /* Copy connection mark of incoming query to outgoing connection. */
1440 if (option_bool(OPT_CONNTRACK))
1441 {
1442 unsigned int mark;
1443 struct all_addr local;
1444#ifdef HAVE_IPV6
1445 if (local_addr->sa.sa_family == AF_INET6)
1446 local.addr.addr6 = local_addr->in6.sin6_addr;
1447 else
1448#endif
1449 local.addr.addr4 = local_addr->in.sin_addr;
1450
1451 if (get_incoming_mark(&peer_addr, &local, 1, &mark))
1452 setsockopt(last_server->tcpfd, SOL_SOCKET, SO_MARK, &mark, sizeof(unsigned int));
1453 }
1454#endif
1455 }
1456
Simon Kelley4b5ea122013-04-22 10:18:26 +01001457 *length = htons(size);
Simon Kelley4f7b3042012-11-28 21:27:02 +00001458
Simon Kelley4b5ea122013-04-22 10:18:26 +01001459 if (!read_write(last_server->tcpfd, packet, size + sizeof(u16), 0) ||
Simon Kelley4f7b3042012-11-28 21:27:02 +00001460 !read_write(last_server->tcpfd, &c1, 1, 1) ||
Simon Kelley7d7b7b32014-01-08 15:53:35 +00001461 !read_write(last_server->tcpfd, &c2, 1, 1) ||
1462 !read_write(last_server->tcpfd, payload, (c1 << 8) | c2, 1))
Simon Kelley7de060b2011-08-26 17:24:52 +01001463 {
1464 close(last_server->tcpfd);
1465 last_server->tcpfd = -1;
1466 continue;
Simon Kelley4f7b3042012-11-28 21:27:02 +00001467 }
1468
1469 m = (c1 << 8) | c2;
Simon Kelley4f7b3042012-11-28 21:27:02 +00001470
1471 if (!gotname)
1472 strcpy(daemon->namebuff, "query");
1473 if (last_server->addr.sa.sa_family == AF_INET)
1474 log_query(F_SERVER | F_IPV4 | F_FORWARD, daemon->namebuff,
1475 (struct all_addr *)&last_server->addr.in.sin_addr, NULL);
Simon Kelleyfeba5c12004-07-27 20:28:58 +01001476#ifdef HAVE_IPV6
Simon Kelley4f7b3042012-11-28 21:27:02 +00001477 else
1478 log_query(F_SERVER | F_IPV6 | F_FORWARD, daemon->namebuff,
1479 (struct all_addr *)&last_server->addr.in6.sin6_addr, NULL);
Simon Kelleyfeba5c12004-07-27 20:28:58 +01001480#endif
Simon Kelley7d7b7b32014-01-08 15:53:35 +00001481
1482#ifdef HAVE_DNSSEC
1483 if (option_bool(OPT_DNSSEC_VALID) && !checking_disabled)
1484 {
Simon Kelley7fa836e2014-02-10 20:11:24 +00001485 int keycount = DNSSEC_WORK; /* Limit to number of DNSSEC questions, to catch loops and avoid filling cache. */
1486 int status = tcp_key_recurse(now, STAT_TRUNCATED, header, m, 0, daemon->namebuff, daemon->keyname, last_server, &keycount);
1487 char *result;
Simon Kelley7d7b7b32014-01-08 15:53:35 +00001488
Simon Kelley7fa836e2014-02-10 20:11:24 +00001489 if (keycount == 0)
1490 result = "ABANDONED";
1491 else
1492 result = (status == STAT_SECURE ? "SECURE" : (status == STAT_INSECURE ? "INSECURE" : "BOGUS"));
1493
1494 log_query(F_KEYTAG | F_SECSTAT, "result", NULL, result);
1495
Simon Kelley7d7b7b32014-01-08 15:53:35 +00001496 if (status == STAT_BOGUS)
1497 no_cache_dnssec = 1;
Simon Kelley7fa836e2014-02-10 20:11:24 +00001498
Simon Kelley7d7b7b32014-01-08 15:53:35 +00001499 if (status == STAT_SECURE)
1500 cache_secure = 1;
1501 }
1502#endif
1503
1504 /* restore CD bit to the value in the query */
1505 if (checking_disabled)
1506 header->hb4 |= HB4_CD;
1507 else
1508 header->hb4 &= ~HB4_CD;
Simon Kelley4f7b3042012-11-28 21:27:02 +00001509
1510 /* There's no point in updating the cache, since this process will exit and
1511 lose the information after a few queries. We make this call for the alias and
1512 bogus-nxdomain side-effects. */
1513 /* If the crc of the question section doesn't match the crc we sent, then
1514 someone might be attempting to insert bogus values into the cache by
1515 sending replies containing questions and bogus answers. */
Simon Kelley8a9be9e2014-01-25 23:17:21 +00001516#ifdef HAVE_DNSSEC
1517 newhash = hash_questions(header, (unsigned int)m, daemon->namebuff);
1518 if (!newhash || memcmp(hash, newhash, HASH_SIZE) != 0)
Simon Kelley703c7ff2014-01-25 23:46:23 +00001519 {
1520 m = 0;
1521 break;
1522 }
Simon Kelley8a9be9e2014-01-25 23:17:21 +00001523#else
1524 if (crc != questions_crc(header, (unsigned int)m, daemon->namebuff))
Simon Kelley703c7ff2014-01-25 23:46:23 +00001525 {
1526 m = 0;
1527 break;
1528 }
Simon Kelley8a9be9e2014-01-25 23:17:21 +00001529#endif
1530
1531 m = process_reply(header, now, last_server, (unsigned int)m,
1532 option_bool(OPT_NO_REBIND) && !norebind, no_cache_dnssec,
Simon Kelley83349b82014-02-10 21:02:01 +00001533 cache_secure, ad_question, check_subnet, &peer_addr);
Simon Kelley4f7b3042012-11-28 21:27:02 +00001534
1535 break;
1536 }
Simon Kelleyfeba5c12004-07-27 20:28:58 +01001537 }
Simon Kelley4f7b3042012-11-28 21:27:02 +00001538
1539 /* In case of local answer or no connections made. */
1540 if (m == 0)
1541 m = setup_reply(header, (unsigned int)size, addrp, flags, daemon->local_ttl);
Simon Kelleyfeba5c12004-07-27 20:28:58 +01001542 }
Simon Kelleyfeba5c12004-07-27 20:28:58 +01001543 }
Simon Kelley4f7b3042012-11-28 21:27:02 +00001544
Simon Kelley5aabfc72007-08-29 11:24:47 +01001545 check_log_writer(NULL);
Simon Kelleyfeba5c12004-07-27 20:28:58 +01001546
Simon Kelley4b5ea122013-04-22 10:18:26 +01001547 *length = htons(m);
1548
1549 if (m == 0 || !read_write(confd, packet, m + sizeof(u16), 0))
Simon Kelleyfeba5c12004-07-27 20:28:58 +01001550 return packet;
1551 }
1552}
1553
Simon Kelley16972692006-10-16 20:04:18 +01001554static struct frec *allocate_frec(time_t now)
Simon Kelley9e4abcb2004-01-22 19:47:41 +00001555{
Simon Kelley16972692006-10-16 20:04:18 +01001556 struct frec *f;
Simon Kelley9e4abcb2004-01-22 19:47:41 +00001557
Simon Kelley5aabfc72007-08-29 11:24:47 +01001558 if ((f = (struct frec *)whine_malloc(sizeof(struct frec))))
Simon Kelley9e4abcb2004-01-22 19:47:41 +00001559 {
Simon Kelley1a6bca82008-07-11 11:11:42 +01001560 f->next = daemon->frec_list;
Simon Kelley9e4abcb2004-01-22 19:47:41 +00001561 f->time = now;
Simon Kelley832af0b2007-01-21 20:01:28 +00001562 f->sentto = NULL;
Simon Kelley1a6bca82008-07-11 11:11:42 +01001563 f->rfd4 = NULL;
Simon Kelley28866e92011-02-14 20:19:14 +00001564 f->flags = 0;
Simon Kelley1a6bca82008-07-11 11:11:42 +01001565#ifdef HAVE_IPV6
1566 f->rfd6 = NULL;
1567#endif
Simon Kelley3a237152013-12-12 12:15:50 +00001568#ifdef HAVE_DNSSEC
Simon Kelley97bc7982014-01-31 10:19:52 +00001569 f->dependent = NULL;
Simon Kelley3a237152013-12-12 12:15:50 +00001570 f->blocking_query = NULL;
Simon Kelley4619d942014-01-16 19:53:06 +00001571 f->stash = NULL;
Simon Kelley3a237152013-12-12 12:15:50 +00001572#endif
Simon Kelley1a6bca82008-07-11 11:11:42 +01001573 daemon->frec_list = f;
Simon Kelley9e4abcb2004-01-22 19:47:41 +00001574 }
Simon Kelley16972692006-10-16 20:04:18 +01001575
1576 return f;
1577}
1578
Simon Kelley1a6bca82008-07-11 11:11:42 +01001579static struct randfd *allocate_rfd(int family)
1580{
1581 static int finger = 0;
1582 int i;
1583
1584 /* limit the number of sockets we have open to avoid starvation of
1585 (eg) TFTP. Once we have a reasonable number, randomness should be OK */
1586
1587 for (i = 0; i < RANDOM_SOCKS; i++)
Simon Kelley9009d742008-11-14 20:04:27 +00001588 if (daemon->randomsocks[i].refcount == 0)
Simon Kelley1a6bca82008-07-11 11:11:42 +01001589 {
Simon Kelley9009d742008-11-14 20:04:27 +00001590 if ((daemon->randomsocks[i].fd = random_sock(family)) == -1)
1591 break;
1592
Simon Kelley1a6bca82008-07-11 11:11:42 +01001593 daemon->randomsocks[i].refcount = 1;
1594 daemon->randomsocks[i].family = family;
1595 return &daemon->randomsocks[i];
1596 }
1597
Simon Kelley9009d742008-11-14 20:04:27 +00001598 /* No free ones or cannot get new socket, grab an existing one */
Simon Kelley1a6bca82008-07-11 11:11:42 +01001599 for (i = 0; i < RANDOM_SOCKS; i++)
1600 {
1601 int j = (i+finger) % RANDOM_SOCKS;
Simon Kelley9009d742008-11-14 20:04:27 +00001602 if (daemon->randomsocks[j].refcount != 0 &&
1603 daemon->randomsocks[j].family == family &&
1604 daemon->randomsocks[j].refcount != 0xffff)
Simon Kelley1a6bca82008-07-11 11:11:42 +01001605 {
1606 finger = j;
1607 daemon->randomsocks[j].refcount++;
1608 return &daemon->randomsocks[j];
1609 }
1610 }
1611
1612 return NULL; /* doom */
1613}
Simon Kelley1a6bca82008-07-11 11:11:42 +01001614static void free_frec(struct frec *f)
1615{
1616 if (f->rfd4 && --(f->rfd4->refcount) == 0)
1617 close(f->rfd4->fd);
1618
1619 f->rfd4 = NULL;
1620 f->sentto = NULL;
Simon Kelley28866e92011-02-14 20:19:14 +00001621 f->flags = 0;
Simon Kelley1a6bca82008-07-11 11:11:42 +01001622
1623#ifdef HAVE_IPV6
1624 if (f->rfd6 && --(f->rfd6->refcount) == 0)
1625 close(f->rfd6->fd);
1626
1627 f->rfd6 = NULL;
1628#endif
Simon Kelley3a237152013-12-12 12:15:50 +00001629
1630#ifdef HAVE_DNSSEC
1631 if (f->stash)
Simon Kelley0fc2f312014-01-08 10:26:58 +00001632 {
1633 blockdata_free(f->stash);
1634 f->stash = NULL;
1635 }
Simon Kelley3a237152013-12-12 12:15:50 +00001636
1637 /* Anything we're waiting on is pointless now, too */
1638 if (f->blocking_query)
1639 free_frec(f->blocking_query);
1640 f->blocking_query = NULL;
Simon Kelley39048ad2014-01-21 17:33:58 +00001641 f->dependent = NULL;
Simon Kelley3a237152013-12-12 12:15:50 +00001642#endif
Simon Kelley1a6bca82008-07-11 11:11:42 +01001643}
1644
Simon Kelley16972692006-10-16 20:04:18 +01001645/* if wait==NULL return a free or older than TIMEOUT record.
1646 else return *wait zero if one available, or *wait is delay to
Simon Kelley1a6bca82008-07-11 11:11:42 +01001647 when the oldest in-use record will expire. Impose an absolute
Simon Kelley3a237152013-12-12 12:15:50 +00001648 limit of 4*TIMEOUT before we wipe things (for random sockets).
1649 If force is set, always return a result, even if we have
1650 to allocate above the limit. */
1651struct frec *get_new_frec(time_t now, int *wait, int force)
Simon Kelley16972692006-10-16 20:04:18 +01001652{
Simon Kelley1a6bca82008-07-11 11:11:42 +01001653 struct frec *f, *oldest, *target;
Simon Kelley16972692006-10-16 20:04:18 +01001654 int count;
1655
1656 if (wait)
1657 *wait = 0;
1658
Simon Kelley1a6bca82008-07-11 11:11:42 +01001659 for (f = daemon->frec_list, oldest = NULL, target = NULL, count = 0; f; f = f->next, count++)
Simon Kelley832af0b2007-01-21 20:01:28 +00001660 if (!f->sentto)
Simon Kelley1a6bca82008-07-11 11:11:42 +01001661 target = f;
1662 else
Simon Kelley16972692006-10-16 20:04:18 +01001663 {
Simon Kelley1a6bca82008-07-11 11:11:42 +01001664 if (difftime(now, f->time) >= 4*TIMEOUT)
1665 {
1666 free_frec(f);
1667 target = f;
1668 }
1669
1670 if (!oldest || difftime(f->time, oldest->time) <= 0)
1671 oldest = f;
Simon Kelley16972692006-10-16 20:04:18 +01001672 }
Simon Kelley1a6bca82008-07-11 11:11:42 +01001673
1674 if (target)
1675 {
1676 target->time = now;
1677 return target;
1678 }
Simon Kelley16972692006-10-16 20:04:18 +01001679
1680 /* can't find empty one, use oldest if there is one
1681 and it's older than timeout */
1682 if (oldest && ((int)difftime(now, oldest->time)) >= TIMEOUT)
1683 {
1684 /* keep stuff for twice timeout if we can by allocating a new
1685 record instead */
1686 if (difftime(now, oldest->time) < 2*TIMEOUT &&
1687 count <= daemon->ftabsize &&
1688 (f = allocate_frec(now)))
1689 return f;
1690
1691 if (!wait)
1692 {
Simon Kelley1a6bca82008-07-11 11:11:42 +01001693 free_frec(oldest);
Simon Kelley16972692006-10-16 20:04:18 +01001694 oldest->time = now;
1695 }
1696 return oldest;
1697 }
1698
1699 /* none available, calculate time 'till oldest record expires */
Simon Kelley3a237152013-12-12 12:15:50 +00001700 if (!force && count > daemon->ftabsize)
Simon Kelley16972692006-10-16 20:04:18 +01001701 {
Marcelo Salhab Brogliato0da5e892013-05-31 11:49:06 +01001702 static time_t last_log = 0;
1703
Simon Kelley16972692006-10-16 20:04:18 +01001704 if (oldest && wait)
1705 *wait = oldest->time + (time_t)TIMEOUT - now;
Marcelo Salhab Brogliato0da5e892013-05-31 11:49:06 +01001706
1707 if ((int)difftime(now, last_log) > 5)
1708 {
1709 last_log = now;
1710 my_syslog(LOG_WARNING, _("Maximum number of concurrent DNS queries reached (max: %d)"), daemon->ftabsize);
1711 }
1712
Simon Kelley16972692006-10-16 20:04:18 +01001713 return NULL;
1714 }
1715
1716 if (!(f = allocate_frec(now)) && wait)
1717 /* wait one second on malloc failure */
1718 *wait = 1;
1719
Simon Kelley9e4abcb2004-01-22 19:47:41 +00001720 return f; /* OK if malloc fails and this is NULL */
1721}
1722
Simon Kelley832af0b2007-01-21 20:01:28 +00001723/* crc is all-ones if not known. */
Simon Kelley8a9be9e2014-01-25 23:17:21 +00001724static struct frec *lookup_frec(unsigned short id, void *hash)
Simon Kelley9e4abcb2004-01-22 19:47:41 +00001725{
1726 struct frec *f;
1727
Simon Kelley1a6bca82008-07-11 11:11:42 +01001728 for(f = daemon->frec_list; f; f = f->next)
Simon Kelley832af0b2007-01-21 20:01:28 +00001729 if (f->sentto && f->new_id == id &&
Simon Kelley8a9be9e2014-01-25 23:17:21 +00001730 (!hash || memcmp(hash, f->hash, HASH_SIZE) == 0))
Simon Kelley9e4abcb2004-01-22 19:47:41 +00001731 return f;
1732
1733 return NULL;
1734}
1735
1736static struct frec *lookup_frec_by_sender(unsigned short id,
Simon Kelleyfd9fa482004-10-21 20:24:00 +01001737 union mysockaddr *addr,
Simon Kelley8a9be9e2014-01-25 23:17:21 +00001738 void *hash)
Simon Kelley9e4abcb2004-01-22 19:47:41 +00001739{
Simon Kelleyfeba5c12004-07-27 20:28:58 +01001740 struct frec *f;
1741
Simon Kelley1a6bca82008-07-11 11:11:42 +01001742 for(f = daemon->frec_list; f; f = f->next)
Simon Kelley832af0b2007-01-21 20:01:28 +00001743 if (f->sentto &&
Simon Kelley9e4abcb2004-01-22 19:47:41 +00001744 f->orig_id == id &&
Simon Kelley8a9be9e2014-01-25 23:17:21 +00001745 memcmp(hash, f->hash, HASH_SIZE) == 0 &&
Simon Kelley9e4abcb2004-01-22 19:47:41 +00001746 sockaddr_isequal(&f->source, addr))
1747 return f;
1748
1749 return NULL;
1750}
1751
Simon Kelley849a8352006-06-09 21:02:31 +01001752/* A server record is going away, remove references to it */
Simon Kelley5aabfc72007-08-29 11:24:47 +01001753void server_gone(struct server *server)
Simon Kelley849a8352006-06-09 21:02:31 +01001754{
1755 struct frec *f;
1756
Simon Kelley1a6bca82008-07-11 11:11:42 +01001757 for (f = daemon->frec_list; f; f = f->next)
Simon Kelley832af0b2007-01-21 20:01:28 +00001758 if (f->sentto && f->sentto == server)
Simon Kelley1a6bca82008-07-11 11:11:42 +01001759 free_frec(f);
Simon Kelley849a8352006-06-09 21:02:31 +01001760
1761 if (daemon->last_server == server)
1762 daemon->last_server = NULL;
1763
1764 if (daemon->srv_save == server)
1765 daemon->srv_save = NULL;
1766}
Simon Kelley9e4abcb2004-01-22 19:47:41 +00001767
Simon Kelley316e2732010-01-22 20:16:09 +00001768/* return unique random ids. */
Simon Kelley8a9be9e2014-01-25 23:17:21 +00001769static unsigned short get_id(void)
Simon Kelley9e4abcb2004-01-22 19:47:41 +00001770{
1771 unsigned short ret = 0;
Simon Kelley832af0b2007-01-21 20:01:28 +00001772
Simon Kelley316e2732010-01-22 20:16:09 +00001773 do
Simon Kelley832af0b2007-01-21 20:01:28 +00001774 ret = rand16();
Simon Kelley8a9be9e2014-01-25 23:17:21 +00001775 while (lookup_frec(ret, NULL));
Simon Kelley832af0b2007-01-21 20:01:28 +00001776
Simon Kelley9e4abcb2004-01-22 19:47:41 +00001777 return ret;
1778}
1779
1780
1781
1782
1783