blob: c48fd7519163f55be5dcf1730e5aaa8c0607f11f [file] [log] [blame]
Simon Kelleyc49778d2016-01-06 18:52:33 +00001/* dnsmasq is Copyright (c) 2000-2016 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);
Simon Kelley9e4abcb2004-01-22 19:47:41 +000025
Simon Kelley824af852008-02-12 20:43:05 +000026/* Send a UDP packet with its source address set as "source"
Simon Kelley44a2a312004-03-10 20:04:35 +000027 unless nowild is true, when we just send it with the kernel default */
Simon Kelley29689cf2012-03-22 14:01:00 +000028int send_from(int fd, int nowild, char *packet, size_t len,
29 union mysockaddr *to, struct all_addr *source,
Simon Kelley50303b12012-04-04 22:13:17 +010030 unsigned int iface)
Simon Kelley9e4abcb2004-01-22 19:47:41 +000031{
Simon Kelley44a2a312004-03-10 20:04:35 +000032 struct msghdr msg;
33 struct iovec iov[1];
Simon Kelley44a2a312004-03-10 20:04:35 +000034 union {
35 struct cmsghdr align; /* this ensures alignment */
Simon Kelley5e9e0ef2006-04-17 14:24:29 +010036#if defined(HAVE_LINUX_NETWORK)
Simon Kelley44a2a312004-03-10 20:04:35 +000037 char control[CMSG_SPACE(sizeof(struct in_pktinfo))];
38#elif defined(IP_SENDSRCADDR)
39 char control[CMSG_SPACE(sizeof(struct in_addr))];
40#endif
41#ifdef HAVE_IPV6
42 char control6[CMSG_SPACE(sizeof(struct in6_pktinfo))];
43#endif
44 } control_u;
Simon Kelleyfeba5c12004-07-27 20:28:58 +010045
Simon Kelley44a2a312004-03-10 20:04:35 +000046 iov[0].iov_base = packet;
47 iov[0].iov_len = len;
48
Simon Kelleyfeba5c12004-07-27 20:28:58 +010049 msg.msg_control = NULL;
50 msg.msg_controllen = 0;
Simon Kelley44a2a312004-03-10 20:04:35 +000051 msg.msg_flags = 0;
52 msg.msg_name = to;
53 msg.msg_namelen = sa_len(to);
54 msg.msg_iov = iov;
55 msg.msg_iovlen = 1;
Simon Kelleyfeba5c12004-07-27 20:28:58 +010056
Simon Kelley26128d22004-11-14 16:43:54 +000057 if (!nowild)
Simon Kelleyfeba5c12004-07-27 20:28:58 +010058 {
Simon Kelley26128d22004-11-14 16:43:54 +000059 struct cmsghdr *cmptr;
Simon Kelleyfeba5c12004-07-27 20:28:58 +010060 msg.msg_control = &control_u;
61 msg.msg_controllen = sizeof(control_u);
Simon Kelley26128d22004-11-14 16:43:54 +000062 cmptr = CMSG_FIRSTHDR(&msg);
Simon Kelley44a2a312004-03-10 20:04:35 +000063
Simon Kelley26128d22004-11-14 16:43:54 +000064 if (to->sa.sa_family == AF_INET)
65 {
Simon Kelley5e9e0ef2006-04-17 14:24:29 +010066#if defined(HAVE_LINUX_NETWORK)
Simon Kelley8ef5ada2010-06-03 19:42:45 +010067 struct in_pktinfo p;
68 p.ipi_ifindex = 0;
69 p.ipi_spec_dst = source->addr.addr4;
70 memcpy(CMSG_DATA(cmptr), &p, sizeof(p));
Simon Kelley26128d22004-11-14 16:43:54 +000071 msg.msg_controllen = cmptr->cmsg_len = CMSG_LEN(sizeof(struct in_pktinfo));
Simon Kelleyc72daea2012-01-05 21:33:27 +000072 cmptr->cmsg_level = IPPROTO_IP;
Simon Kelley26128d22004-11-14 16:43:54 +000073 cmptr->cmsg_type = IP_PKTINFO;
74#elif defined(IP_SENDSRCADDR)
Simon Kelley8ef5ada2010-06-03 19:42:45 +010075 memcpy(CMSG_DATA(cmptr), &(source->addr.addr4), sizeof(source->addr.addr4));
Simon Kelley26128d22004-11-14 16:43:54 +000076 msg.msg_controllen = cmptr->cmsg_len = CMSG_LEN(sizeof(struct in_addr));
77 cmptr->cmsg_level = IPPROTO_IP;
78 cmptr->cmsg_type = IP_SENDSRCADDR;
Simon Kelley44a2a312004-03-10 20:04:35 +000079#endif
Simon Kelley26128d22004-11-14 16:43:54 +000080 }
Simon Kelley26128d22004-11-14 16:43:54 +000081 else
Simon Kelleyb8187c82005-11-26 21:46:27 +000082#ifdef HAVE_IPV6
Simon Kelley26128d22004-11-14 16:43:54 +000083 {
Simon Kelley8ef5ada2010-06-03 19:42:45 +010084 struct in6_pktinfo p;
85 p.ipi6_ifindex = iface; /* Need iface for IPv6 to handle link-local addrs */
86 p.ipi6_addr = source->addr.addr6;
87 memcpy(CMSG_DATA(cmptr), &p, sizeof(p));
Simon Kelley26128d22004-11-14 16:43:54 +000088 msg.msg_controllen = cmptr->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
Simon Kelley316e2732010-01-22 20:16:09 +000089 cmptr->cmsg_type = daemon->v6pktinfo;
Simon Kelleyc72daea2012-01-05 21:33:27 +000090 cmptr->cmsg_level = IPPROTO_IPV6;
Simon Kelley26128d22004-11-14 16:43:54 +000091 }
Simon Kelley3d8df262005-08-29 12:19:27 +010092#else
Simon Kelleyc72daea2012-01-05 21:33:27 +000093 (void)iface; /* eliminate warning */
Simon Kelley26128d22004-11-14 16:43:54 +000094#endif
95 }
Simon Kelleyfeba5c12004-07-27 20:28:58 +010096
Simon Kelleyff841eb2015-03-11 21:36:30 +000097 while (retry_send(sendmsg(fd, &msg, 0)));
98
99 /* If interface is still in DAD, EINVAL results - ignore that. */
100 if (errno != 0 && errno != EINVAL)
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100101 {
Simon Kelley50303b12012-04-04 22:13:17 +0100102 my_syslog(LOG_ERR, _("failed to send packet: %s"), strerror(errno));
Simon Kelley29689cf2012-03-22 14:01:00 +0000103 return 0;
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100104 }
Simon Kelley29d28dd2012-12-03 14:05:59 +0000105
Simon Kelley29689cf2012-03-22 14:01:00 +0000106 return 1;
Simon Kelley44a2a312004-03-10 20:04:35 +0000107}
108
Simon Kelley367341f2016-01-12 15:58:23 +0000109static unsigned int search_servers(time_t now, struct all_addr **addrpp, unsigned int qtype,
110 char *qdomain, int *type, char **domain, int *norebind)
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100111
112{
113 /* If the query ends in the domain in one of our servers, set
114 domain to point to that name. We find the largest match to allow both
115 domain.org and sub.domain.org to exist. */
116
117 unsigned int namelen = strlen(qdomain);
118 unsigned int matchlen = 0;
119 struct server *serv;
Simon Kelley28866e92011-02-14 20:19:14 +0000120 unsigned int flags = 0;
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100121
Simon Kelley3be34542004-09-11 19:12:13 +0100122 for (serv = daemon->servers; serv; serv=serv->next)
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100123 /* domain matches take priority over NODOTS matches */
Simon Kelley3d8df262005-08-29 12:19:27 +0100124 if ((serv->flags & SERV_FOR_NODOTS) && *type != SERV_HAS_DOMAIN && !strchr(qdomain, '.') && namelen != 0)
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100125 {
Simon Kelley28866e92011-02-14 20:19:14 +0000126 unsigned int sflag = serv->addr.sa.sa_family == AF_INET ? F_IPV4 : F_IPV6;
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100127 *type = SERV_FOR_NODOTS;
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100128 if (serv->flags & SERV_NO_ADDR)
Simon Kelley36717ee2004-09-20 19:20:58 +0100129 flags = F_NXDOMAIN;
130 else if (serv->flags & SERV_LITERAL_ADDRESS)
131 {
132 if (sflag & qtype)
133 {
134 flags = sflag;
135 if (serv->addr.sa.sa_family == AF_INET)
136 *addrpp = (struct all_addr *)&serv->addr.in.sin_addr;
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100137#ifdef HAVE_IPV6
Simon Kelley36717ee2004-09-20 19:20:58 +0100138 else
139 *addrpp = (struct all_addr *)&serv->addr.in6.sin6_addr;
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100140#endif
Simon Kelley36717ee2004-09-20 19:20:58 +0100141 }
Simon Kelley824af852008-02-12 20:43:05 +0000142 else if (!flags || (flags & F_NXDOMAIN))
Simon Kelley36717ee2004-09-20 19:20:58 +0100143 flags = F_NOERR;
144 }
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100145 }
146 else if (serv->flags & SERV_HAS_DOMAIN)
147 {
148 unsigned int domainlen = strlen(serv->domain);
Simon Kelleyb8187c82005-11-26 21:46:27 +0000149 char *matchstart = qdomain + namelen - domainlen;
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100150 if (namelen >= domainlen &&
Simon Kelleyb8187c82005-11-26 21:46:27 +0000151 hostname_isequal(matchstart, serv->domain) &&
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100152 (domainlen == 0 || namelen == domainlen || *(matchstart-1) == '.' ))
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100153 {
Simon Kelley92be34a2016-01-16 18:39:54 +0000154 if ((serv->flags & SERV_NO_REBIND) && norebind)
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100155 *norebind = 1;
Simon Kelley28866e92011-02-14 20:19:14 +0000156 else
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100157 {
Simon Kelley28866e92011-02-14 20:19:14 +0000158 unsigned int sflag = serv->addr.sa.sa_family == AF_INET ? F_IPV4 : F_IPV6;
159 /* implement priority rules for --address and --server for same domain.
160 --address wins if the address is for the correct AF
161 --server wins otherwise. */
162 if (domainlen != 0 && domainlen == matchlen)
Simon Kelley36717ee2004-09-20 19:20:58 +0100163 {
Simon Kelley28866e92011-02-14 20:19:14 +0000164 if ((serv->flags & SERV_LITERAL_ADDRESS))
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100165 {
Simon Kelley28866e92011-02-14 20:19:14 +0000166 if (!(sflag & qtype) && flags == 0)
167 continue;
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100168 }
Simon Kelley28866e92011-02-14 20:19:14 +0000169 else
170 {
171 if (flags & (F_IPV4 | F_IPV6))
172 continue;
173 }
Simon Kelley36717ee2004-09-20 19:20:58 +0100174 }
Simon Kelley28866e92011-02-14 20:19:14 +0000175
176 if (domainlen >= matchlen)
177 {
Simon Kelley367341f2016-01-12 15:58:23 +0000178 *type = serv->flags & (SERV_HAS_DOMAIN | SERV_USE_RESOLV | SERV_NO_REBIND | SERV_DO_DNSSEC);
Simon Kelley28866e92011-02-14 20:19:14 +0000179 *domain = serv->domain;
180 matchlen = domainlen;
181 if (serv->flags & SERV_NO_ADDR)
182 flags = F_NXDOMAIN;
183 else if (serv->flags & SERV_LITERAL_ADDRESS)
184 {
185 if (sflag & qtype)
186 {
187 flags = sflag;
188 if (serv->addr.sa.sa_family == AF_INET)
189 *addrpp = (struct all_addr *)&serv->addr.in.sin_addr;
190#ifdef HAVE_IPV6
191 else
192 *addrpp = (struct all_addr *)&serv->addr.in6.sin6_addr;
193#endif
194 }
195 else if (!flags || (flags & F_NXDOMAIN))
196 flags = F_NOERR;
197 }
198 else
199 flags = 0;
200 }
201 }
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100202 }
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100203 }
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100204
Simon Kelley7de060b2011-08-26 17:24:52 +0100205 if (flags == 0 && !(qtype & F_QUERY) &&
Simon Kelley28866e92011-02-14 20:19:14 +0000206 option_bool(OPT_NODOTS_LOCAL) && !strchr(qdomain, '.') && namelen != 0)
Simon Kelley7de060b2011-08-26 17:24:52 +0100207 /* don't forward A or AAAA queries for simple names, except the empty name */
208 flags = F_NOERR;
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100209
Simon Kelley5aabfc72007-08-29 11:24:47 +0100210 if (flags == F_NXDOMAIN && check_for_local_domain(qdomain, now))
Simon Kelleyc1bb8502004-08-11 18:40:17 +0100211 flags = F_NOERR;
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100212
Simon Kelley824af852008-02-12 20:43:05 +0000213 if (flags)
214 {
215 int logflags = 0;
216
217 if (flags == F_NXDOMAIN || flags == F_NOERR)
218 logflags = F_NEG | qtype;
219
Simon Kelley1a6bca82008-07-11 11:11:42 +0100220 log_query(logflags | flags | F_CONFIG | F_FORWARD, qdomain, *addrpp, NULL);
Simon Kelley824af852008-02-12 20:43:05 +0000221 }
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100222 else if ((*type) & SERV_USE_RESOLV)
223 {
224 *type = 0; /* use normal servers for this domain */
225 *domain = NULL;
226 }
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100227 return flags;
228}
Simon Kelley44a2a312004-03-10 20:04:35 +0000229
Simon Kelley824af852008-02-12 20:43:05 +0000230static int forward_query(int udpfd, union mysockaddr *udpaddr,
231 struct all_addr *dst_addr, unsigned int dst_iface,
Simon Kelley83349b82014-02-10 21:02:01 +0000232 struct dns_header *header, size_t plen, time_t now,
Simon Kelley613ad152014-02-25 23:02:28 +0000233 struct frec *forward, int ad_reqd, int do_bit)
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000234{
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000235 char *domain = NULL;
Simon Kelley367341f2016-01-12 15:58:23 +0000236 int type = SERV_DO_DNSSEC, norebind = 0;
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000237 struct all_addr *addrp = NULL;
Simon Kelley28866e92011-02-14 20:19:14 +0000238 unsigned int flags = 0;
Simon Kelleyde379512004-06-22 20:23:33 +0100239 struct server *start = NULL;
Simon Kelley8a9be9e2014-01-25 23:17:21 +0000240#ifdef HAVE_DNSSEC
241 void *hash = hash_questions(header, plen, daemon->namebuff);
Simon Kelley367341f2016-01-12 15:58:23 +0000242 int do_dnssec = 0;
Simon Kelley8a9be9e2014-01-25 23:17:21 +0000243#else
244 unsigned int crc = questions_crc(header, plen, daemon->namebuff);
245 void *hash = &crc;
246#endif
247 unsigned int gotname = extract_request(header, plen, daemon->namebuff, NULL);
248
Simon Kelley00a5b5d2014-02-28 18:10:55 +0000249 (void)do_bit;
250
Simon Kelley3d8df262005-08-29 12:19:27 +0100251 /* may be no servers available. */
252 if (!daemon->servers)
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000253 forward = NULL;
Simon Kelley8a9be9e2014-01-25 23:17:21 +0000254 else if (forward || (hash && (forward = lookup_frec_by_sender(ntohs(header->id), udpaddr, hash))))
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000255 {
Simon Kelleya77cec82015-05-08 16:25:38 +0100256 /* If we didn't get an answer advertising a maximal packet in EDNS,
257 fall back to 1280, which should work everywhere on IPv6.
258 If that generates an answer, it will become the new default
259 for this server */
260 forward->flags |= FREC_TEST_PKTSZ;
261
Simon Kelleye0c0ad32014-01-16 22:42:07 +0000262#ifdef HAVE_DNSSEC
Simon Kelleydac74312014-02-13 16:43:49 +0000263 /* If we've already got an answer to this query, but we're awaiting keys for validation,
Simon Kelleye0c0ad32014-01-16 22:42:07 +0000264 there's no point retrying the query, retry the key query instead...... */
265 if (forward->blocking_query)
266 {
Simon Kelleyfa14bec2015-12-20 17:12:16 +0000267 int fd, is_sign;
268 unsigned char *pheader;
Simon Kelleya77cec82015-05-08 16:25:38 +0100269
270 forward->flags &= ~FREC_TEST_PKTSZ;
271
Simon Kelleye0c0ad32014-01-16 22:42:07 +0000272 while (forward->blocking_query)
273 forward = forward->blocking_query;
Simon Kelleya77cec82015-05-08 16:25:38 +0100274
275 forward->flags |= FREC_TEST_PKTSZ;
Simon Kelleye0c0ad32014-01-16 22:42:07 +0000276
277 blockdata_retrieve(forward->stash, forward->stash_len, (void *)header);
278 plen = forward->stash_len;
279
Simon Kelley5bb88f02015-12-21 16:23:47 +0000280 if (find_pseudoheader(header, plen, NULL, &pheader, &is_sign, NULL) && !is_sign)
Simon Kelleyfa14bec2015-12-20 17:12:16 +0000281 PUTSHORT(SAFE_PKTSZ, pheader);
Simon Kelleya77cec82015-05-08 16:25:38 +0100282
Simon Kelley2b291912014-03-21 11:13:55 +0000283 if (forward->sentto->addr.sa.sa_family == AF_INET)
Simon Kelley25cf5e32015-01-09 15:53:03 +0000284 log_query(F_NOEXTRA | F_DNSSEC | F_IPV4, "retry", (struct all_addr *)&forward->sentto->addr.in.sin_addr, "dnssec");
Simon Kelleye0c0ad32014-01-16 22:42:07 +0000285#ifdef HAVE_IPV6
286 else
Simon Kelley25cf5e32015-01-09 15:53:03 +0000287 log_query(F_NOEXTRA | F_DNSSEC | F_IPV6, "retry", (struct all_addr *)&forward->sentto->addr.in6.sin6_addr, "dnssec");
Simon Kelleye0c0ad32014-01-16 22:42:07 +0000288#endif
289
290 if (forward->sentto->sfd)
291 fd = forward->sentto->sfd->fd;
292 else
293 {
294#ifdef HAVE_IPV6
295 if (forward->sentto->addr.sa.sa_family == AF_INET6)
296 fd = forward->rfd6->fd;
297 else
298#endif
299 fd = forward->rfd4->fd;
300 }
301
Simon Kelleyff841eb2015-03-11 21:36:30 +0000302 while (retry_send( sendto(fd, (char *)header, plen, 0,
303 &forward->sentto->addr.sa,
304 sa_len(&forward->sentto->addr))));
Simon Kelleye0c0ad32014-01-16 22:42:07 +0000305
306 return 1;
307 }
308#endif
309
Simon Kelleyde379512004-06-22 20:23:33 +0100310 /* retry on existing query, send to all available servers */
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000311 domain = forward->sentto->domain;
Simon Kelley824af852008-02-12 20:43:05 +0000312 forward->sentto->failed_queries++;
Simon Kelley28866e92011-02-14 20:19:14 +0000313 if (!option_bool(OPT_ORDER))
Simon Kelleyde379512004-06-22 20:23:33 +0100314 {
Simon Kelley0a852542005-03-23 20:28:59 +0000315 forward->forwardall = 1;
Simon Kelley3be34542004-09-11 19:12:13 +0100316 daemon->last_server = NULL;
Simon Kelleyde379512004-06-22 20:23:33 +0100317 }
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000318 type = forward->sentto->flags & SERV_TYPE;
Simon Kelley367341f2016-01-12 15:58:23 +0000319#ifdef HAVE_DNSSEC
320 do_dnssec = forward->sentto->flags & SERV_DO_DNSSEC;
321#endif
322
Simon Kelleyde379512004-06-22 20:23:33 +0100323 if (!(start = forward->sentto->next))
Simon Kelley3be34542004-09-11 19:12:13 +0100324 start = daemon->servers; /* at end of list, recycle */
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000325 header->id = htons(forward->new_id);
326 }
327 else
328 {
329 if (gotname)
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100330 flags = search_servers(now, &addrp, gotname, daemon->namebuff, &type, &domain, &norebind);
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000331
Simon Kelley367341f2016-01-12 15:58:23 +0000332#ifdef HAVE_DNSSEC
333 do_dnssec = type & SERV_DO_DNSSEC;
334 type &= ~SERV_DO_DNSSEC;
335#endif
336
Simon Kelley3a237152013-12-12 12:15:50 +0000337 if (!flags && !(forward = get_new_frec(now, NULL, 0)))
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100338 /* table full - server failure. */
339 flags = F_NEG;
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000340
341 if (forward)
342 {
Simon Kelley0a852542005-03-23 20:28:59 +0000343 forward->source = *udpaddr;
344 forward->dest = *dst_addr;
345 forward->iface = dst_iface;
Simon Kelley0a852542005-03-23 20:28:59 +0000346 forward->orig_id = ntohs(header->id);
Simon Kelley8a9be9e2014-01-25 23:17:21 +0000347 forward->new_id = get_id();
Simon Kelley832af0b2007-01-21 20:01:28 +0000348 forward->fd = udpfd;
Simon Kelley8a9be9e2014-01-25 23:17:21 +0000349 memcpy(forward->hash, hash, HASH_SIZE);
Simon Kelley0a852542005-03-23 20:28:59 +0000350 forward->forwardall = 0;
Simon Kelleyed4c0762013-10-08 20:46:34 +0100351 forward->flags = 0;
Simon Kelley28866e92011-02-14 20:19:14 +0000352 if (norebind)
353 forward->flags |= FREC_NOREBIND;
Simon Kelley572b41e2011-02-18 18:11:18 +0000354 if (header->hb4 & HB4_CD)
Simon Kelley28866e92011-02-14 20:19:14 +0000355 forward->flags |= FREC_CHECKING_DISABLED;
Simon Kelley83349b82014-02-10 21:02:01 +0000356 if (ad_reqd)
357 forward->flags |= FREC_AD_QUESTION;
Simon Kelley7fa836e2014-02-10 20:11:24 +0000358#ifdef HAVE_DNSSEC
359 forward->work_counter = DNSSEC_WORK;
Simon Kelley613ad152014-02-25 23:02:28 +0000360 if (do_bit)
361 forward->flags |= FREC_DO_QUESTION;
Simon Kelley7fa836e2014-02-10 20:11:24 +0000362#endif
Simon Kelley613ad152014-02-25 23:02:28 +0000363
Simon Kelley28866e92011-02-14 20:19:14 +0000364 header->id = htons(forward->new_id);
365
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100366 /* In strict_order mode, always try servers in the order
367 specified in resolv.conf, if a domain is given
368 always try all the available servers,
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000369 otherwise, use the one last known to work. */
370
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100371 if (type == 0)
372 {
Simon Kelley28866e92011-02-14 20:19:14 +0000373 if (option_bool(OPT_ORDER))
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100374 start = daemon->servers;
375 else if (!(start = daemon->last_server) ||
376 daemon->forwardcount++ > FORWARD_TEST ||
377 difftime(now, daemon->forwardtime) > FORWARD_TIME)
378 {
379 start = daemon->servers;
380 forward->forwardall = 1;
381 daemon->forwardcount = 0;
382 daemon->forwardtime = now;
383 }
384 }
385 else
Simon Kelleyde379512004-06-22 20:23:33 +0100386 {
Simon Kelley3be34542004-09-11 19:12:13 +0100387 start = daemon->servers;
Simon Kelley28866e92011-02-14 20:19:14 +0000388 if (!option_bool(OPT_ORDER))
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100389 forward->forwardall = 1;
Simon Kelleyde379512004-06-22 20:23:33 +0100390 }
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000391 }
392 }
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100393
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000394 /* check for send errors here (no route to host)
395 if we fail to send to all nameservers, send back an error
396 packet straight away (helps modem users when offline) */
397
398 if (!flags && forward)
399 {
Simon Kelleyde379512004-06-22 20:23:33 +0100400 struct server *firstsentto = start;
Simon Kelley33702ab2015-12-28 23:17:15 +0000401 int subnet, forwarded = 0;
Simon Kelleyd3a8b392015-12-23 12:27:37 +0000402 size_t edns0_len;
403
Simon Kelley25cf5e32015-01-09 15:53:03 +0000404 /* If a query is retried, use the log_id for the retry when logging the answer. */
405 forward->log_id = daemon->log_id;
406
Simon Kelley33702ab2015-12-28 23:17:15 +0000407 edns0_len = add_edns0_config(header, plen, ((unsigned char *)header) + PACKETSZ, &forward->source, now, &subnet);
408
409 if (edns0_len != plen)
Simon Kelleyfa14bec2015-12-20 17:12:16 +0000410 {
Simon Kelley33702ab2015-12-28 23:17:15 +0000411 plen = edns0_len;
412 forward->flags |= FREC_ADDED_PHEADER;
413
414 if (subnet)
415 forward->flags |= FREC_HAS_SUBNET;
Simon Kelleyfa14bec2015-12-20 17:12:16 +0000416 }
Simon Kelley33702ab2015-12-28 23:17:15 +0000417
Simon Kelley3a237152013-12-12 12:15:50 +0000418#ifdef HAVE_DNSSEC
Simon Kelley367341f2016-01-12 15:58:23 +0000419 if (option_bool(OPT_DNSSEC_VALID) && do_dnssec)
Simon Kelley0fc2f312014-01-08 10:26:58 +0000420 {
Simon Kelley33702ab2015-12-28 23:17:15 +0000421 size_t new = add_do_bit(header, plen, ((unsigned char *) header) + PACKETSZ);
Simon Kelley613ad152014-02-25 23:02:28 +0000422
Simon Kelleyfa14bec2015-12-20 17:12:16 +0000423 if (new != plen)
424 forward->flags |= FREC_ADDED_PHEADER;
425
426 plen = new;
427
Simon Kelley5b3bf922014-01-25 17:03:07 +0000428 /* For debugging, set Checking Disabled, otherwise, have the upstream check too,
429 this allows it to select auth servers when one is returning bad data. */
430 if (option_bool(OPT_DNSSEC_DEBUG))
431 header->hb4 |= HB4_CD;
Simon Kelley613ad152014-02-25 23:02:28 +0000432
Simon Kelley0fc2f312014-01-08 10:26:58 +0000433 }
Simon Kelley3a237152013-12-12 12:15:50 +0000434#endif
Simon Kelleyd3a8b392015-12-23 12:27:37 +0000435
436 /* If we're sending an EDNS0 with any options, we can't recreate the query from a reply. */
437 if (find_pseudoheader(header, plen, &edns0_len, NULL, NULL, NULL) && edns0_len > 11)
438 forward->flags |= FREC_HAS_EXTRADATA;
Simon Kelleya77cec82015-05-08 16:25:38 +0100439
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000440 while (1)
441 {
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000442 /* only send to servers dealing with our domain.
443 domain may be NULL, in which case server->domain
444 must be NULL also. */
445
Simon Kelleyde379512004-06-22 20:23:33 +0100446 if (type == (start->flags & SERV_TYPE) &&
Simon Kelleyfd9fa482004-10-21 20:24:00 +0100447 (type != SERV_HAS_DOMAIN || hostname_isequal(domain, start->domain)) &&
Simon Kelleyb5ea1cc2014-07-29 16:34:14 +0100448 !(start->flags & (SERV_LITERAL_ADDRESS | SERV_LOOP)))
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000449 {
Simon Kelley1a6bca82008-07-11 11:11:42 +0100450 int fd;
451
452 /* find server socket to use, may need to get random one. */
453 if (start->sfd)
454 fd = start->sfd->fd;
455 else
456 {
457#ifdef HAVE_IPV6
458 if (start->addr.sa.sa_family == AF_INET6)
459 {
460 if (!forward->rfd6 &&
461 !(forward->rfd6 = allocate_rfd(AF_INET6)))
462 break;
Simon Kelley3927da42008-07-20 15:10:39 +0100463 daemon->rfd_save = forward->rfd6;
Simon Kelley1a6bca82008-07-11 11:11:42 +0100464 fd = forward->rfd6->fd;
465 }
466 else
467#endif
468 {
469 if (!forward->rfd4 &&
470 !(forward->rfd4 = allocate_rfd(AF_INET)))
471 break;
Simon Kelley3927da42008-07-20 15:10:39 +0100472 daemon->rfd_save = forward->rfd4;
Simon Kelley1a6bca82008-07-11 11:11:42 +0100473 fd = forward->rfd4->fd;
474 }
Simon Kelley7de060b2011-08-26 17:24:52 +0100475
476#ifdef HAVE_CONNTRACK
477 /* Copy connection mark of incoming query to outgoing connection. */
478 if (option_bool(OPT_CONNTRACK))
479 {
480 unsigned int mark;
Giacomo Tazzari797a7af2013-04-22 13:16:37 +0100481 if (get_incoming_mark(&forward->source, &forward->dest, 0, &mark))
Simon Kelley7de060b2011-08-26 17:24:52 +0100482 setsockopt(fd, SOL_SOCKET, SO_MARK, &mark, sizeof(unsigned int));
483 }
484#endif
Simon Kelley1a6bca82008-07-11 11:11:42 +0100485 }
486
Simon Kelleyfa14bec2015-12-20 17:12:16 +0000487#ifdef HAVE_DNSSEC
Simon Kelley5bb88f02015-12-21 16:23:47 +0000488 if (option_bool(OPT_DNSSEC_VALID) && (forward->flags & FREC_ADDED_PHEADER))
Simon Kelleyfa14bec2015-12-20 17:12:16 +0000489 {
490 /* Difficult one here. If our client didn't send EDNS0, we will have set the UDP
491 packet size to 512. But that won't provide space for the RRSIGS in many cases.
492 The RRSIGS will be stripped out before the answer goes back, so the packet should
493 shrink again. So, if we added a do-bit, bump the udp packet size to the value
Simon Kelley5aa5f0f2015-12-21 17:20:35 +0000494 known to be OK for this server. We check returned size after stripping and set
495 the truncated bit if it's still too big. */
Simon Kelleyfa14bec2015-12-20 17:12:16 +0000496 unsigned char *pheader;
497 int is_sign;
Simon Kelley5bb88f02015-12-21 16:23:47 +0000498 if (find_pseudoheader(header, plen, NULL, &pheader, &is_sign, NULL) && !is_sign)
Simon Kelleyfa14bec2015-12-20 17:12:16 +0000499 PUTSHORT(start->edns_pktsz, pheader);
500 }
501#endif
502
Simon Kelleyff841eb2015-03-11 21:36:30 +0000503 if (retry_send(sendto(fd, (char *)header, plen, 0,
504 &start->addr.sa,
505 sa_len(&start->addr))))
506 continue;
507
508 if (errno == 0)
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000509 {
Simon Kelleycdeda282006-03-16 20:16:06 +0000510 /* Keep info in case we want to re-send this packet */
511 daemon->srv_save = start;
512 daemon->packet_len = plen;
513
Simon Kelleyde379512004-06-22 20:23:33 +0100514 if (!gotname)
Simon Kelley3be34542004-09-11 19:12:13 +0100515 strcpy(daemon->namebuff, "query");
Simon Kelleyde379512004-06-22 20:23:33 +0100516 if (start->addr.sa.sa_family == AF_INET)
Simon Kelley3be34542004-09-11 19:12:13 +0100517 log_query(F_SERVER | F_IPV4 | F_FORWARD, daemon->namebuff,
Simon Kelley1a6bca82008-07-11 11:11:42 +0100518 (struct all_addr *)&start->addr.in.sin_addr, NULL);
Simon Kelleyde379512004-06-22 20:23:33 +0100519#ifdef HAVE_IPV6
520 else
Simon Kelley3be34542004-09-11 19:12:13 +0100521 log_query(F_SERVER | F_IPV6 | F_FORWARD, daemon->namebuff,
Simon Kelley1a6bca82008-07-11 11:11:42 +0100522 (struct all_addr *)&start->addr.in6.sin6_addr, NULL);
Simon Kelleyde379512004-06-22 20:23:33 +0100523#endif
Simon Kelley824af852008-02-12 20:43:05 +0000524 start->queries++;
Simon Kelleyde379512004-06-22 20:23:33 +0100525 forwarded = 1;
526 forward->sentto = start;
Simon Kelley0a852542005-03-23 20:28:59 +0000527 if (!forward->forwardall)
Simon Kelleyde379512004-06-22 20:23:33 +0100528 break;
Simon Kelley0a852542005-03-23 20:28:59 +0000529 forward->forwardall++;
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000530 }
531 }
532
Simon Kelleyde379512004-06-22 20:23:33 +0100533 if (!(start = start->next))
Simon Kelley3be34542004-09-11 19:12:13 +0100534 start = daemon->servers;
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000535
Simon Kelleyde379512004-06-22 20:23:33 +0100536 if (start == firstsentto)
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000537 break;
538 }
539
Simon Kelleyde379512004-06-22 20:23:33 +0100540 if (forwarded)
Simon Kelley824af852008-02-12 20:43:05 +0000541 return 1;
Simon Kelleyde379512004-06-22 20:23:33 +0100542
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000543 /* could not send on, prepare to return */
544 header->id = htons(forward->orig_id);
Simon Kelley1a6bca82008-07-11 11:11:42 +0100545 free_frec(forward); /* cancel */
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000546 }
547
548 /* could not send on, return empty answer or address if known for whole domain */
Simon Kelleyb8187c82005-11-26 21:46:27 +0000549 if (udpfd != -1)
550 {
Simon Kelleycdeda282006-03-16 20:16:06 +0000551 plen = setup_reply(header, plen, addrp, flags, daemon->local_ttl);
Simon Kelley54dd3932012-06-20 11:23:38 +0100552 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 +0000553 }
554
Simon Kelley824af852008-02-12 20:43:05 +0000555 return 0;
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000556}
557
Simon Kelleyed4c0762013-10-08 20:46:34 +0100558static size_t process_reply(struct dns_header *header, time_t now, struct server *server, size_t n, int check_rebind,
Simon Kelleyfe3992f2015-04-03 21:25:05 +0100559 int no_cache, int cache_secure, int bogusanswer, int ad_reqd, int do_bit, int added_pheader,
560 int check_subnet, union mysockaddr *query_source)
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100561{
Simon Kelley36717ee2004-09-20 19:20:58 +0100562 unsigned char *pheader, *sizep;
Jason A. Donenfeld13d86c72013-02-22 18:20:53 +0000563 char **sets = 0;
Simon Kelley832af0b2007-01-21 20:01:28 +0000564 int munged = 0, is_sign;
Simon Kelleycdeda282006-03-16 20:16:06 +0000565 size_t plen;
566
Simon Kelley83349b82014-02-10 21:02:01 +0000567 (void)ad_reqd;
Simon Kelley982faf42015-04-03 21:42:30 +0100568 (void)do_bit;
569 (void)bogusanswer;
Simon Kelley83349b82014-02-10 21:02:01 +0000570
Jason A. Donenfeld13d86c72013-02-22 18:20:53 +0000571#ifdef HAVE_IPSET
Simon Kelley82a14af2014-04-13 20:48:57 +0100572 if (daemon->ipsets && extract_request(header, n, daemon->namebuff, NULL))
Jason A. Donenfeld13d86c72013-02-22 18:20:53 +0000573 {
Simon Kelley82a14af2014-04-13 20:48:57 +0100574 /* Similar algorithm to search_servers. */
575 struct ipsets *ipset_pos;
576 unsigned int namelen = strlen(daemon->namebuff);
577 unsigned int matchlen = 0;
578 for (ipset_pos = daemon->ipsets; ipset_pos; ipset_pos = ipset_pos->next)
Simon Kelley6c0cb852014-01-17 14:40:46 +0000579 {
Simon Kelley82a14af2014-04-13 20:48:57 +0100580 unsigned int domainlen = strlen(ipset_pos->domain);
581 char *matchstart = daemon->namebuff + namelen - domainlen;
582 if (namelen >= domainlen && hostname_isequal(matchstart, ipset_pos->domain) &&
583 (domainlen == 0 || namelen == domainlen || *(matchstart - 1) == '.' ) &&
584 domainlen >= matchlen)
585 {
586 matchlen = domainlen;
587 sets = ipset_pos->sets;
588 }
Simon Kelley6c0cb852014-01-17 14:40:46 +0000589 }
Jason A. Donenfeld13d86c72013-02-22 18:20:53 +0000590 }
591#endif
592
Simon Kelley5bb88f02015-12-21 16:23:47 +0000593 if ((pheader = find_pseudoheader(header, n, &plen, &sizep, &is_sign, NULL)))
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100594 {
Simon Kelleyed4c0762013-10-08 20:46:34 +0100595 if (check_subnet && !check_source(header, plen, pheader, query_source))
596 {
597 my_syslog(LOG_WARNING, _("discarding DNS reply: subnet option mismatch"));
598 return 0;
599 }
Simon Kelley613ad152014-02-25 23:02:28 +0000600
Simon Kelleyfa14bec2015-12-20 17:12:16 +0000601 if (!is_sign)
Simon Kelley613ad152014-02-25 23:02:28 +0000602 {
Simon Kelleyfa14bec2015-12-20 17:12:16 +0000603 if (added_pheader)
604 {
605 /* client didn't send EDNS0, we added one, strip it off before returning answer. */
606 n = rrfilter(header, n, 0);
607 pheader = NULL;
608 }
609 else
610 {
Simon Kelley33702ab2015-12-28 23:17:15 +0000611 unsigned short udpsz;
612
Simon Kelleyfa14bec2015-12-20 17:12:16 +0000613 /* If upstream is advertising a larger UDP packet size
614 than we allow, trim it so that we don't get overlarge
615 requests for the client. We can't do this for signed packets. */
Simon Kelleyfa14bec2015-12-20 17:12:16 +0000616 GETSHORT(udpsz, sizep);
617 if (udpsz > daemon->edns_pktsz)
Simon Kelley33702ab2015-12-28 23:17:15 +0000618 {
619 sizep -= 2;
620 PUTSHORT(daemon->edns_pktsz, sizep);
621 }
622
623#ifdef HAVE_DNSSEC
624 /* If the client didn't set the do bit, but we did, reset it. */
625 if (option_bool(OPT_DNSSEC_VALID) && !do_bit)
626 {
627 unsigned short flags;
628 sizep += 2; /* skip RCODE */
629 GETSHORT(flags, sizep);
630 flags &= ~0x8000;
631 sizep -= 2;
632 PUTSHORT(flags, sizep);
633 }
634#endif
Simon Kelleyfa14bec2015-12-20 17:12:16 +0000635 }
Simon Kelley613ad152014-02-25 23:02:28 +0000636 }
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100637 }
Simon Kelley83349b82014-02-10 21:02:01 +0000638
Simon Kelley28866e92011-02-14 20:19:14 +0000639 /* RFC 4035 sect 4.6 para 3 */
Giovanni Bajo237724c2012-04-05 02:46:52 +0200640 if (!is_sign && !option_bool(OPT_DNSSEC_PROXY))
Simon Kelley795501b2014-01-08 18:11:55 +0000641 header->hb4 &= ~HB4_AD;
Simon Kelley3a237152013-12-12 12:15:50 +0000642
Simon Kelley572b41e2011-02-18 18:11:18 +0000643 if (OPCODE(header) != QUERY || (RCODE(header) != NOERROR && RCODE(header) != NXDOMAIN))
Simon Kelley8938ae02014-05-01 17:46:25 +0100644 return resize_packet(header, n, pheader, plen);
Simon Kelley36717ee2004-09-20 19:20:58 +0100645
Simon Kelley0a852542005-03-23 20:28:59 +0000646 /* Complain loudly if the upstream server is non-recursive. */
Simon Kelley92be34a2016-01-16 18:39:54 +0000647 if (!(header->hb4 & HB4_RA) && RCODE(header) == NOERROR &&
Simon Kelley0a852542005-03-23 20:28:59 +0000648 server && !(server->flags & SERV_WARNED_RECURSIVE))
649 {
Simon Kelley3d8df262005-08-29 12:19:27 +0100650 prettyprint_addr(&server->addr, daemon->namebuff);
Simon Kelleyf2621c72007-04-29 19:47:21 +0100651 my_syslog(LOG_WARNING, _("nameserver %s refused to do a recursive query"), daemon->namebuff);
Simon Kelley28866e92011-02-14 20:19:14 +0000652 if (!option_bool(OPT_LOG))
Simon Kelley0a852542005-03-23 20:28:59 +0000653 server->flags |= SERV_WARNED_RECURSIVE;
654 }
Giovanni Bajoe292e932012-04-22 14:32:02 +0200655
Simon Kelley572b41e2011-02-18 18:11:18 +0000656 if (daemon->bogus_addr && RCODE(header) != NXDOMAIN &&
Simon Kelleyfd9fa482004-10-21 20:24:00 +0100657 check_for_bogus_wildcard(header, n, daemon->namebuff, daemon->bogus_addr, now))
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100658 {
Simon Kelleyfd9fa482004-10-21 20:24:00 +0100659 munged = 1;
Simon Kelley572b41e2011-02-18 18:11:18 +0000660 SET_RCODE(header, NXDOMAIN);
661 header->hb3 &= ~HB3_AA;
Simon Kelley6938f342014-01-26 22:47:39 +0000662 cache_secure = 0;
Simon Kelley36717ee2004-09-20 19:20:58 +0100663 }
Simon Kelleyfd9fa482004-10-21 20:24:00 +0100664 else
Simon Kelley36717ee2004-09-20 19:20:58 +0100665 {
Simon Kelley6938f342014-01-26 22:47:39 +0000666 int doctored = 0;
667
Simon Kelley572b41e2011-02-18 18:11:18 +0000668 if (RCODE(header) == NXDOMAIN &&
Simon Kelleyfd9fa482004-10-21 20:24:00 +0100669 extract_request(header, n, daemon->namebuff, NULL) &&
Simon Kelley5aabfc72007-08-29 11:24:47 +0100670 check_for_local_domain(daemon->namebuff, now))
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100671 {
Simon Kelley36717ee2004-09-20 19:20:58 +0100672 /* if we forwarded a query for a locally known name (because it was for
673 an unknown type) and the answer is NXDOMAIN, convert that to NODATA,
674 since we know that the domain exists, even if upstream doesn't */
Simon Kelleyfd9fa482004-10-21 20:24:00 +0100675 munged = 1;
Simon Kelley572b41e2011-02-18 18:11:18 +0000676 header->hb3 |= HB3_AA;
677 SET_RCODE(header, NOERROR);
Simon Kelley6938f342014-01-26 22:47:39 +0000678 cache_secure = 0;
Simon Kelley36717ee2004-09-20 19:20:58 +0100679 }
Simon Kelley832af0b2007-01-21 20:01:28 +0000680
Simon Kelley6938f342014-01-26 22:47:39 +0000681 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 +0000682 {
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100683 my_syslog(LOG_WARNING, _("possible DNS-rebind attack detected: %s"), daemon->namebuff);
Simon Kelley824af852008-02-12 20:43:05 +0000684 munged = 1;
Simon Kelley6938f342014-01-26 22:47:39 +0000685 cache_secure = 0;
Simon Kelley824af852008-02-12 20:43:05 +0000686 }
Simon Kelley6938f342014-01-26 22:47:39 +0000687
688 if (doctored)
689 cache_secure = 0;
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100690 }
Simon Kelleyfd9fa482004-10-21 20:24:00 +0100691
Simon Kelleya25720a2014-01-14 23:13:55 +0000692#ifdef HAVE_DNSSEC
Simon Kelley33702ab2015-12-28 23:17:15 +0000693 if (bogusanswer && !(header->hb4 & HB4_CD) && !option_bool(OPT_DNSSEC_DEBUG))
Simon Kelleya25720a2014-01-14 23:13:55 +0000694 {
Simon Kelley33702ab2015-12-28 23:17:15 +0000695 /* Bogus reply, turn into SERVFAIL */
696 SET_RCODE(header, SERVFAIL);
697 munged = 1;
Simon Kelleya25720a2014-01-14 23:13:55 +0000698 }
Simon Kelley6938f342014-01-26 22:47:39 +0000699
700 if (option_bool(OPT_DNSSEC_VALID))
Simon Kelleyfa14bec2015-12-20 17:12:16 +0000701 {
702 header->hb4 &= ~HB4_AD;
703
704 if (!(header->hb4 & HB4_CD) && ad_reqd && cache_secure)
705 header->hb4 |= HB4_AD;
706
707 /* If the requestor didn't set the DO bit, don't return DNSSEC info. */
708 if (!do_bit)
709 n = rrfilter(header, n, 1);
710 }
Simon Kelleya25720a2014-01-14 23:13:55 +0000711#endif
712
Simon Kelleyfd9fa482004-10-21 20:24:00 +0100713 /* do this after extract_addresses. Ensure NODATA reply and remove
714 nameserver info. */
715
716 if (munged)
717 {
718 header->ancount = htons(0);
719 header->nscount = htons(0);
720 header->arcount = htons(0);
Simon Kelley150162b2015-03-27 09:58:26 +0000721 header->hb3 &= ~HB3_TC;
Simon Kelleyfd9fa482004-10-21 20:24:00 +0100722 }
723
Simon Kelley36717ee2004-09-20 19:20:58 +0100724 /* the bogus-nxdomain stuff, doctor and NXDOMAIN->NODATA munging can all elide
725 sections of the packet. Find the new length here and put back pseudoheader
726 if it was removed. */
727 return resize_packet(header, n, pheader, plen);
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100728}
729
Simon Kelley3be34542004-09-11 19:12:13 +0100730/* sets new last_server */
Simon Kelley1a6bca82008-07-11 11:11:42 +0100731void reply_query(int fd, int family, time_t now)
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000732{
733 /* packet from peer server, extract data for cache, and send to
734 original requester */
Simon Kelley572b41e2011-02-18 18:11:18 +0000735 struct dns_header *header;
Simon Kelleyde379512004-06-22 20:23:33 +0100736 union mysockaddr serveraddr;
Simon Kelley832af0b2007-01-21 20:01:28 +0000737 struct frec *forward;
Simon Kelleyde379512004-06-22 20:23:33 +0100738 socklen_t addrlen = sizeof(serveraddr);
Simon Kelley60b68062014-01-08 12:10:28 +0000739 ssize_t n = recvfrom(fd, daemon->packet, daemon->packet_buff_sz, 0, &serveraddr.sa, &addrlen);
Simon Kelleycdeda282006-03-16 20:16:06 +0000740 size_t nn;
Simon Kelley1a6bca82008-07-11 11:11:42 +0100741 struct server *server;
Simon Kelley8a9be9e2014-01-25 23:17:21 +0000742 void *hash;
743#ifndef HAVE_DNSSEC
744 unsigned int crc;
745#endif
746
Simon Kelleycdeda282006-03-16 20:16:06 +0000747 /* packet buffer overwritten */
748 daemon->srv_save = NULL;
Simon Kelley832af0b2007-01-21 20:01:28 +0000749
Simon Kelleyde379512004-06-22 20:23:33 +0100750 /* Determine the address of the server replying so that we can mark that as good */
Simon Kelley1a6bca82008-07-11 11:11:42 +0100751 serveraddr.sa.sa_family = family;
Simon Kelleyde379512004-06-22 20:23:33 +0100752#ifdef HAVE_IPV6
753 if (serveraddr.sa.sa_family == AF_INET6)
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100754 serveraddr.in6.sin6_flowinfo = 0;
Simon Kelleyde379512004-06-22 20:23:33 +0100755#endif
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000756
Simon Kelley490f9072014-03-24 22:04:42 +0000757 header = (struct dns_header *)daemon->packet;
758
759 if (n < (int)sizeof(struct dns_header) || !(header->hb3 & HB3_QR))
760 return;
761
Simon Kelley1a6bca82008-07-11 11:11:42 +0100762 /* spoof check: answer must come from known server, */
763 for (server = daemon->servers; server; server = server->next)
764 if (!(server->flags & (SERV_LITERAL_ADDRESS | SERV_NO_ADDR)) &&
765 sockaddr_isequal(&server->addr, &serveraddr))
766 break;
Simon Kelley490f9072014-03-24 22:04:42 +0000767
768 if (!server)
769 return;
770
Simon Kelley8a9be9e2014-01-25 23:17:21 +0000771#ifdef HAVE_DNSSEC
772 hash = hash_questions(header, n, daemon->namebuff);
773#else
774 hash = &crc;
775 crc = questions_crc(header, n, daemon->namebuff);
776#endif
Simon Kelleyfd9fa482004-10-21 20:24:00 +0100777
Simon Kelley490f9072014-03-24 22:04:42 +0000778 if (!(forward = lookup_frec(ntohs(header->id), hash)))
Simon Kelley1a6bca82008-07-11 11:11:42 +0100779 return;
Simon Kelley490f9072014-03-24 22:04:42 +0000780
Simon Kelley25cf5e32015-01-09 15:53:03 +0000781 /* log_query gets called indirectly all over the place, so
782 pass these in global variables - sorry. */
783 daemon->log_display_id = forward->log_id;
784 daemon->log_source_addr = &forward->source;
785
Glen Huang32fc6db2014-12-27 15:28:12 +0000786 if (daemon->ignore_addr && RCODE(header) == NOERROR &&
787 check_for_ignored_address(header, n, daemon->ignore_addr))
788 return;
789
Simon Kelleyd3a8b392015-12-23 12:27:37 +0000790 /* Note: if we send extra options in the EDNS0 header, we can't recreate
791 the query from the reply. */
Simon Kelley2ae195f2015-01-18 22:20:48 +0000792 if (RCODE(header) == REFUSED &&
Simon Kelley28866e92011-02-14 20:19:14 +0000793 !option_bool(OPT_ORDER) &&
Simon Kelleyd3a8b392015-12-23 12:27:37 +0000794 forward->forwardall == 0 &&
795 !(forward->flags & FREC_HAS_EXTRADATA))
Simon Kelley1a6bca82008-07-11 11:11:42 +0100796 /* for broken servers, attempt to send to another one. */
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000797 {
Simon Kelley1a6bca82008-07-11 11:11:42 +0100798 unsigned char *pheader;
799 size_t plen;
800 int is_sign;
Simon Kelley832af0b2007-01-21 20:01:28 +0000801
Simon Kelley1a6bca82008-07-11 11:11:42 +0100802 /* recreate query from reply */
Simon Kelley5bb88f02015-12-21 16:23:47 +0000803 pheader = find_pseudoheader(header, (size_t)n, &plen, NULL, &is_sign, NULL);
Simon Kelley1a6bca82008-07-11 11:11:42 +0100804 if (!is_sign)
Simon Kelley832af0b2007-01-21 20:01:28 +0000805 {
Simon Kelley1a6bca82008-07-11 11:11:42 +0100806 header->ancount = htons(0);
807 header->nscount = htons(0);
808 header->arcount = htons(0);
809 if ((nn = resize_packet(header, (size_t)n, pheader, plen)))
810 {
swiggerbd7bfa22015-06-01 20:54:59 +0100811 header->hb3 &= ~(HB3_QR | HB3_AA | HB3_TC);
Simon Kelleyfa14bec2015-12-20 17:12:16 +0000812 header->hb4 &= ~(HB4_RA | HB4_RCODE | HB4_CD | HB4_AD);
813 if (forward->flags |= FREC_CHECKING_DISABLED)
814 header->hb4 |= HB4_CD;
815 if (forward->flags |= FREC_AD_QUESTION)
816 header->hb4 |= HB4_AD;
817 if (forward->flags & FREC_DO_QUESTION)
Simon Kelley33702ab2015-12-28 23:17:15 +0000818 add_do_bit(header, nn, (unsigned char *)pheader + plen);
Simon Kelleyfa14bec2015-12-20 17:12:16 +0000819 forward_query(-1, NULL, NULL, 0, header, nn, now, forward, forward->flags & FREC_AD_QUESTION, forward->flags & FREC_DO_QUESTION);
Simon Kelley1a6bca82008-07-11 11:11:42 +0100820 return;
821 }
822 }
823 }
Simon Kelley3a237152013-12-12 12:15:50 +0000824
825 server = forward->sentto;
Simon Kelley1a6bca82008-07-11 11:11:42 +0100826 if ((forward->sentto->flags & SERV_TYPE) == 0)
827 {
Simon Kelley51967f92014-03-25 21:07:00 +0000828 if (RCODE(header) == REFUSED)
Simon Kelley1a6bca82008-07-11 11:11:42 +0100829 server = NULL;
830 else
831 {
832 struct server *last_server;
Simon Kelley832af0b2007-01-21 20:01:28 +0000833
Simon Kelley1a6bca82008-07-11 11:11:42 +0100834 /* find good server by address if possible, otherwise assume the last one we sent to */
835 for (last_server = daemon->servers; last_server; last_server = last_server->next)
836 if (!(last_server->flags & (SERV_LITERAL_ADDRESS | SERV_HAS_DOMAIN | SERV_FOR_NODOTS | SERV_NO_ADDR)) &&
837 sockaddr_isequal(&last_server->addr, &serveraddr))
838 {
839 server = last_server;
840 break;
841 }
842 }
Simon Kelley28866e92011-02-14 20:19:14 +0000843 if (!option_bool(OPT_ALL_SERVERS))
Simon Kelley1a6bca82008-07-11 11:11:42 +0100844 daemon->last_server = server;
845 }
Simon Kelleya77cec82015-05-08 16:25:38 +0100846
847 /* We tried resending to this server with a smaller maximum size and got an answer.
Simon Kelley86fa1042015-05-10 13:50:59 +0100848 Make that permanent. To avoid reduxing the packet size for an single dropped packet,
849 only do this when we get a truncated answer, or one larger than the safe size. */
850 if (server && (forward->flags & FREC_TEST_PKTSZ) &&
851 ((header->hb3 & HB3_TC) || n >= SAFE_PKTSZ))
Simon Kelleya77cec82015-05-08 16:25:38 +0100852 server->edns_pktsz = SAFE_PKTSZ;
853
Simon Kelley1a6bca82008-07-11 11:11:42 +0100854 /* If the answer is an error, keep the forward record in place in case
855 we get a good reply from another server. Kill it when we've
856 had replies from all to avoid filling the forwarding table when
857 everything is broken */
Simon Kelley51967f92014-03-25 21:07:00 +0000858 if (forward->forwardall == 0 || --forward->forwardall == 1 || RCODE(header) != SERVFAIL)
Simon Kelley1a6bca82008-07-11 11:11:42 +0100859 {
Simon Kelleyfe3992f2015-04-03 21:25:05 +0100860 int check_rebind = 0, no_cache_dnssec = 0, cache_secure = 0, bogusanswer = 0;
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100861
Simon Kelley3a237152013-12-12 12:15:50 +0000862 if (option_bool(OPT_NO_REBIND))
863 check_rebind = !(forward->flags & FREC_NOREBIND);
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100864
Simon Kelley3a237152013-12-12 12:15:50 +0000865 /* Don't cache replies where DNSSEC validation was turned off, either
866 the upstream server told us so, or the original query specified it. */
867 if ((header->hb4 & HB4_CD) || (forward->flags & FREC_CHECKING_DISABLED))
868 no_cache_dnssec = 1;
869
870#ifdef HAVE_DNSSEC
Simon Kelley367341f2016-01-12 15:58:23 +0000871 if (server && (server->flags & SERV_DO_DNSSEC) &&
Simon Kelley57573712016-01-11 22:50:00 +0000872 option_bool(OPT_DNSSEC_VALID) && !(forward->flags & FREC_CHECKING_DISABLED))
Simon Kelley3a237152013-12-12 12:15:50 +0000873 {
Simon Kelley9a31b682015-12-15 10:20:39 +0000874 int status = 0;
Simon Kelley0fc2f312014-01-08 10:26:58 +0000875
876 /* We've had a reply already, which we're validating. Ignore this duplicate */
Simon Kelleye0c0ad32014-01-16 22:42:07 +0000877 if (forward->blocking_query)
Simon Kelley0fc2f312014-01-08 10:26:58 +0000878 return;
Simon Kelley9a31b682015-12-15 10:20:39 +0000879
880 /* Truncated answer can't be validated.
Simon Kelley5d3b87a2014-01-20 11:57:23 +0000881 If this is an answer to a DNSSEC-generated query, we still
882 need to get the client to retry over TCP, so return
883 an answer with the TC bit set, even if the actual answer fits.
884 */
Simon Kelley9a31b682015-12-15 10:20:39 +0000885 if (header->hb3 & HB3_TC)
886 status = STAT_TRUNCATED;
887
888 while (1)
Simon Kelley00a5b5d2014-02-28 18:10:55 +0000889 {
Simon Kelley9a31b682015-12-15 10:20:39 +0000890 /* As soon as anything returns BOGUS, we stop and unwind, to do otherwise
891 would invite infinite loops, since the answers to DNSKEY and DS queries
892 will not be cached, so they'll be repeated. */
893 if (status != STAT_BOGUS && status != STAT_TRUNCATED && status != STAT_ABANDONED)
Simon Kelley00a5b5d2014-02-28 18:10:55 +0000894 {
Simon Kelley9a31b682015-12-15 10:20:39 +0000895 if (forward->flags & FREC_DNSKEY_QUERY)
896 status = dnssec_validate_by_ds(now, header, n, daemon->namebuff, daemon->keyname, forward->class);
897 else if (forward->flags & FREC_DS_QUERY)
898 status = dnssec_validate_ds(now, header, n, daemon->namebuff, daemon->keyname, forward->class);
Simon Kelley00a5b5d2014-02-28 18:10:55 +0000899 else
Simon Kelley9a31b682015-12-15 10:20:39 +0000900 status = dnssec_validate_reply(now, header, n, daemon->namebuff, daemon->keyname, &forward->class,
901 option_bool(OPT_DNSSEC_NO_SIGN), NULL, NULL);
Simon Kelley00a5b5d2014-02-28 18:10:55 +0000902 }
Simon Kelley0fc2f312014-01-08 10:26:58 +0000903
Simon Kelley9a31b682015-12-15 10:20:39 +0000904 /* Can't validate, as we're missing key data. Put this
905 answer aside, whilst we get that. */
906 if (status == STAT_NEED_DS || status == STAT_NEED_KEY)
Simon Kelley3a237152013-12-12 12:15:50 +0000907 {
Simon Kelley9a31b682015-12-15 10:20:39 +0000908 struct frec *new, *orig;
Simon Kelley9d633042013-12-13 15:36:55 +0000909
Simon Kelley9a31b682015-12-15 10:20:39 +0000910 /* Free any saved query */
911 if (forward->stash)
912 blockdata_free(forward->stash);
913
914 /* Now save reply pending receipt of key data */
915 if (!(forward->stash = blockdata_alloc((char *)header, n)))
Simon Kelley97e618a2015-01-07 21:55:43 +0000916 return;
Simon Kelley9a31b682015-12-15 10:20:39 +0000917 forward->stash_len = n;
Simon Kelleye0c0ad32014-01-16 22:42:07 +0000918
Simon Kelley9a31b682015-12-15 10:20:39 +0000919 /* Find the original query that started it all.... */
920 for (orig = forward; orig->dependent; orig = orig->dependent);
Simon Kelley7fa836e2014-02-10 20:11:24 +0000921
Simon Kelley9a31b682015-12-15 10:20:39 +0000922 if (--orig->work_counter == 0 || !(new = get_new_frec(now, NULL, 1)))
923 status = STAT_ABANDONED;
Simon Kelleye0c0ad32014-01-16 22:42:07 +0000924 else
Simon Kelley3a237152013-12-12 12:15:50 +0000925 {
Simon Kelley92be34a2016-01-16 18:39:54 +0000926 int fd, type;
Simon Kelley9a31b682015-12-15 10:20:39 +0000927 struct frec *next = new->next;
Simon Kelley92be34a2016-01-16 18:39:54 +0000928 char *domain;
929
Simon Kelley9a31b682015-12-15 10:20:39 +0000930 *new = *forward; /* copy everything, then overwrite */
931 new->next = next;
932 new->blocking_query = NULL;
Simon Kelley92be34a2016-01-16 18:39:54 +0000933
934 /* Find server to forward to. This will normally be the
935 same as for the original query, but may be another if
936 servers for domains are involved. */
937 if (search_servers(now, NULL, F_QUERY, daemon->keyname, &type, &domain, NULL) == 0)
938 {
939 struct server *start = server;
940 type &= ~SERV_DO_DNSSEC;
941
942 while (1)
943 {
944 if (type == (start->flags & SERV_TYPE) &&
945 (type != SERV_HAS_DOMAIN || hostname_isequal(domain, start->domain)) &&
946 !(start->flags & (SERV_LITERAL_ADDRESS | SERV_LOOP)))
947 {
948 server = start;
949 break;
950 }
951
952 if (!(start = start->next))
953 start = daemon->servers;
954 if (start == server)
955 break;
956 }
957 }
Simon Kelley9a31b682015-12-15 10:20:39 +0000958 new->sentto = server;
Simon Kelley92be34a2016-01-16 18:39:54 +0000959
Simon Kelley9a31b682015-12-15 10:20:39 +0000960 new->rfd4 = NULL;
Simon Kelley3a237152013-12-12 12:15:50 +0000961#ifdef HAVE_IPV6
Simon Kelley9a31b682015-12-15 10:20:39 +0000962 new->rfd6 = NULL;
Simon Kelley7fa836e2014-02-10 20:11:24 +0000963#endif
Simon Kelley9a31b682015-12-15 10:20:39 +0000964 new->flags &= ~(FREC_DNSKEY_QUERY | FREC_DS_QUERY);
965
966 new->dependent = forward; /* to find query awaiting new one. */
967 forward->blocking_query = new; /* for garbage cleaning */
968 /* validate routines leave name of required record in daemon->keyname */
969 if (status == STAT_NEED_KEY)
Simon Kelley7fa836e2014-02-10 20:11:24 +0000970 {
Simon Kelley9a31b682015-12-15 10:20:39 +0000971 new->flags |= FREC_DNSKEY_QUERY;
Simon Kelley33702ab2015-12-28 23:17:15 +0000972 nn = dnssec_generate_query(header, ((unsigned char *) header) + server->edns_pktsz,
Simon Kelley9a31b682015-12-15 10:20:39 +0000973 daemon->keyname, forward->class, T_DNSKEY, &server->addr, server->edns_pktsz);
Simon Kelleyf1668d22014-01-08 16:53:27 +0000974 }
Simon Kelley9a31b682015-12-15 10:20:39 +0000975 else
976 {
977 new->flags |= FREC_DS_QUERY;
Simon Kelley33702ab2015-12-28 23:17:15 +0000978 nn = dnssec_generate_query(header,((unsigned char *) header) + server->edns_pktsz,
Simon Kelley9a31b682015-12-15 10:20:39 +0000979 daemon->keyname, forward->class, T_DS, &server->addr, server->edns_pktsz);
980 }
981 if ((hash = hash_questions(header, nn, daemon->namebuff)))
982 memcpy(new->hash, hash, HASH_SIZE);
983 new->new_id = get_id();
984 header->id = htons(new->new_id);
985 /* Save query for retransmission */
986 new->stash = blockdata_alloc((char *)header, nn);
987 new->stash_len = nn;
988
989 /* Don't resend this. */
990 daemon->srv_save = NULL;
991
992 if (server->sfd)
993 fd = server->sfd->fd;
994 else
995 {
996 fd = -1;
997#ifdef HAVE_IPV6
998 if (server->addr.sa.sa_family == AF_INET6)
999 {
1000 if (new->rfd6 || (new->rfd6 = allocate_rfd(AF_INET6)))
1001 fd = new->rfd6->fd;
1002 }
1003 else
1004#endif
1005 {
1006 if (new->rfd4 || (new->rfd4 = allocate_rfd(AF_INET)))
1007 fd = new->rfd4->fd;
1008 }
1009 }
1010
1011 if (fd != -1)
1012 {
1013 while (retry_send(sendto(fd, (char *)header, nn, 0,
1014 &server->addr.sa,
1015 sa_len(&server->addr))));
1016 server->queries++;
1017 }
1018 }
Simon Kelley7fa836e2014-02-10 20:11:24 +00001019 return;
Simon Kelley3a237152013-12-12 12:15:50 +00001020 }
Simon Kelley3a237152013-12-12 12:15:50 +00001021
Simon Kelley9a31b682015-12-15 10:20:39 +00001022 /* Validated original answer, all done. */
1023 if (!forward->dependent)
1024 break;
1025
1026 /* validated subsdiary query, (and cached result)
1027 pop that and return to the previous query we were working on. */
Simon Kelley0744ca62014-01-25 16:40:15 +00001028 struct frec *prev = forward->dependent;
1029 free_frec(forward);
1030 forward = prev;
1031 forward->blocking_query = NULL; /* already gone */
1032 blockdata_retrieve(forward->stash, forward->stash_len, (void *)header);
1033 n = forward->stash_len;
Simon Kelley3a237152013-12-12 12:15:50 +00001034 }
Simon Kelley9a31b682015-12-15 10:20:39 +00001035
Simon Kelley5d3b87a2014-01-20 11:57:23 +00001036
Simon Kelleyfe3992f2015-04-03 21:25:05 +01001037 no_cache_dnssec = 0;
Simon Kelleyfe3992f2015-04-03 21:25:05 +01001038
Simon Kelley5d3b87a2014-01-20 11:57:23 +00001039 if (status == STAT_TRUNCATED)
Simon Kelley0744ca62014-01-25 16:40:15 +00001040 header->hb3 |= HB3_TC;
Simon Kelley5d3b87a2014-01-20 11:57:23 +00001041 else
Simon Kelley7fa836e2014-02-10 20:11:24 +00001042 {
Simon Kelley554b5802015-04-17 22:50:20 +01001043 char *result, *domain = "result";
Simon Kelley7fa836e2014-02-10 20:11:24 +00001044
Simon Kelley9a31b682015-12-15 10:20:39 +00001045 if (status == STAT_ABANDONED)
Simon Kelley150162b2015-03-27 09:58:26 +00001046 {
1047 result = "ABANDONED";
1048 status = STAT_BOGUS;
1049 }
Simon Kelley7fa836e2014-02-10 20:11:24 +00001050 else
1051 result = (status == STAT_SECURE ? "SECURE" : (status == STAT_INSECURE ? "INSECURE" : "BOGUS"));
1052
Simon Kelley554b5802015-04-17 22:50:20 +01001053 if (status == STAT_BOGUS && extract_request(header, n, daemon->namebuff, NULL))
1054 domain = daemon->namebuff;
Simon Kelley9a31b682015-12-15 10:20:39 +00001055
Simon Kelley554b5802015-04-17 22:50:20 +01001056 log_query(F_KEYTAG | F_SECSTAT, domain, NULL, result);
Simon Kelley7fa836e2014-02-10 20:11:24 +00001057 }
Simon Kelley5d3b87a2014-01-20 11:57:23 +00001058
Simon Kelley3a237152013-12-12 12:15:50 +00001059 if (status == STAT_SECURE)
1060 cache_secure = 1;
Simon Kelley3a237152013-12-12 12:15:50 +00001061 else if (status == STAT_BOGUS)
Simon Kelleyfe3992f2015-04-03 21:25:05 +01001062 {
1063 no_cache_dnssec = 1;
1064 bogusanswer = 1;
1065 }
Simon Kelley3a237152013-12-12 12:15:50 +00001066 }
Simon Kelley83349b82014-02-10 21:02:01 +00001067#endif
1068
1069 /* restore CD bit to the value in the query */
1070 if (forward->flags & FREC_CHECKING_DISABLED)
1071 header->hb4 |= HB4_CD;
1072 else
1073 header->hb4 &= ~HB4_CD;
Simon Kelley3a237152013-12-12 12:15:50 +00001074
Simon Kelleyfe3992f2015-04-03 21:25:05 +01001075 if ((nn = process_reply(header, now, server, (size_t)n, check_rebind, no_cache_dnssec, cache_secure, bogusanswer,
Simon Kelley613ad152014-02-25 23:02:28 +00001076 forward->flags & FREC_AD_QUESTION, forward->flags & FREC_DO_QUESTION,
1077 forward->flags & FREC_ADDED_PHEADER, forward->flags & FREC_HAS_SUBNET, &forward->source)))
Simon Kelley832af0b2007-01-21 20:01:28 +00001078 {
Simon Kelley1a6bca82008-07-11 11:11:42 +01001079 header->id = htons(forward->orig_id);
Simon Kelley572b41e2011-02-18 18:11:18 +00001080 header->hb4 |= HB4_RA; /* recursion if available */
Simon Kelley5aa5f0f2015-12-21 17:20:35 +00001081#ifdef HAVE_DNSSEC
1082 /* We added an EDNSO header for the purpose of getting DNSSEC RRs, and set the value of the UDP payload size
1083 greater than the no-EDNS0-implied 512 to have if space for the RRSIGS. If, having stripped them and the EDNS0
1084 header, the answer is still bigger than 512, truncate it and mark it so. The client then retries with TCP. */
1085 if (option_bool(OPT_DNSSEC_VALID) && (forward->flags & FREC_ADDED_PHEADER) && (nn > PACKETSZ))
1086 {
1087 header->ancount = htons(0);
1088 header->nscount = htons(0);
1089 header->arcount = htons(0);
1090 header->hb3 |= HB3_TC;
1091 nn = resize_packet(header, nn, NULL, 0);
1092 }
1093#endif
Simon Kelley54dd3932012-06-20 11:23:38 +01001094 send_from(forward->fd, option_bool(OPT_NOWILD) || option_bool (OPT_CLEVERBIND), daemon->packet, nn,
Simon Kelley50303b12012-04-04 22:13:17 +01001095 &forward->source, &forward->dest, forward->iface);
Simon Kelley832af0b2007-01-21 20:01:28 +00001096 }
Simon Kelley1a6bca82008-07-11 11:11:42 +01001097 free_frec(forward); /* cancel */
Simon Kelley9e4abcb2004-01-22 19:47:41 +00001098 }
Simon Kelley9e4abcb2004-01-22 19:47:41 +00001099}
Simon Kelley44a2a312004-03-10 20:04:35 +00001100
Simon Kelley1a6bca82008-07-11 11:11:42 +01001101
Simon Kelley5aabfc72007-08-29 11:24:47 +01001102void receive_query(struct listener *listen, time_t now)
Simon Kelley44a2a312004-03-10 20:04:35 +00001103{
Simon Kelley572b41e2011-02-18 18:11:18 +00001104 struct dns_header *header = (struct dns_header *)daemon->packet;
Simon Kelley44a2a312004-03-10 20:04:35 +00001105 union mysockaddr source_addr;
Simon Kelleyfa14bec2015-12-20 17:12:16 +00001106 unsigned char *pheader;
1107 unsigned short type, udp_size = PACKETSZ; /* default if no EDNS0 */
Simon Kelley44a2a312004-03-10 20:04:35 +00001108 struct all_addr dst_addr;
Simon Kelleyf6b7dc42005-01-23 12:06:08 +00001109 struct in_addr netmask, dst_addr_4;
Simon Kelleycdeda282006-03-16 20:16:06 +00001110 size_t m;
1111 ssize_t n;
Simon Kelleyfa14bec2015-12-20 17:12:16 +00001112 int if_index = 0, auth_dns = 0, do_bit = 0, have_pseudoheader = 0;
Vladislav Grishenko3b195962013-11-26 11:08:21 +00001113#ifdef HAVE_AUTH
1114 int local_auth = 0;
1115#endif
Simon Kelley44a2a312004-03-10 20:04:35 +00001116 struct iovec iov[1];
1117 struct msghdr msg;
1118 struct cmsghdr *cmptr;
Simon Kelley44a2a312004-03-10 20:04:35 +00001119 union {
1120 struct cmsghdr align; /* this ensures alignment */
1121#ifdef HAVE_IPV6
1122 char control6[CMSG_SPACE(sizeof(struct in6_pktinfo))];
1123#endif
Simon Kelley5e9e0ef2006-04-17 14:24:29 +01001124#if defined(HAVE_LINUX_NETWORK)
Simon Kelley44a2a312004-03-10 20:04:35 +00001125 char control[CMSG_SPACE(sizeof(struct in_pktinfo))];
Simon Kelley824af852008-02-12 20:43:05 +00001126#elif defined(IP_RECVDSTADDR) && defined(HAVE_SOLARIS_NETWORK)
1127 char control[CMSG_SPACE(sizeof(struct in_addr)) +
1128 CMSG_SPACE(sizeof(unsigned int))];
Simon Kelley44a2a312004-03-10 20:04:35 +00001129#elif defined(IP_RECVDSTADDR)
1130 char control[CMSG_SPACE(sizeof(struct in_addr)) +
1131 CMSG_SPACE(sizeof(struct sockaddr_dl))];
1132#endif
1133 } control_u;
Simon Kelley2329bef2013-12-03 13:41:16 +00001134#ifdef HAVE_IPV6
1135 /* Can always get recvd interface for IPv6 */
1136 int check_dst = !option_bool(OPT_NOWILD) || listen->family == AF_INET6;
1137#else
1138 int check_dst = !option_bool(OPT_NOWILD);
1139#endif
1140
Simon Kelleycdeda282006-03-16 20:16:06 +00001141 /* packet buffer overwritten */
1142 daemon->srv_save = NULL;
1143
Hans Dedecker98906272014-12-09 22:22:53 +00001144 dst_addr_4.s_addr = dst_addr.addr.addr4.s_addr = 0;
Simon Kelley4f7b3042012-11-28 21:27:02 +00001145 netmask.s_addr = 0;
1146
Simon Kelley7e5664b2013-04-05 16:57:41 +01001147 if (option_bool(OPT_NOWILD) && listen->iface)
Simon Kelleyf6b7dc42005-01-23 12:06:08 +00001148 {
Simon Kelley4f7b3042012-11-28 21:27:02 +00001149 auth_dns = listen->iface->dns_auth;
1150
1151 if (listen->family == AF_INET)
1152 {
Hans Dedecker98906272014-12-09 22:22:53 +00001153 dst_addr_4 = dst_addr.addr.addr4 = listen->iface->addr.in.sin_addr;
Simon Kelley4f7b3042012-11-28 21:27:02 +00001154 netmask = listen->iface->netmask;
1155 }
Simon Kelleyf6b7dc42005-01-23 12:06:08 +00001156 }
Simon Kelley4f7b3042012-11-28 21:27:02 +00001157
Simon Kelley3be34542004-09-11 19:12:13 +01001158 iov[0].iov_base = daemon->packet;
1159 iov[0].iov_len = daemon->edns_pktsz;
Simon Kelley44a2a312004-03-10 20:04:35 +00001160
1161 msg.msg_control = control_u.control;
1162 msg.msg_controllen = sizeof(control_u);
1163 msg.msg_flags = 0;
1164 msg.msg_name = &source_addr;
1165 msg.msg_namelen = sizeof(source_addr);
1166 msg.msg_iov = iov;
1167 msg.msg_iovlen = 1;
1168
Simon Kelleyde379512004-06-22 20:23:33 +01001169 if ((n = recvmsg(listen->fd, &msg, 0)) == -1)
Simon Kelley3be34542004-09-11 19:12:13 +01001170 return;
Simon Kelley44a2a312004-03-10 20:04:35 +00001171
Simon Kelley572b41e2011-02-18 18:11:18 +00001172 if (n < (int)sizeof(struct dns_header) ||
Simon Kelley5e9e0ef2006-04-17 14:24:29 +01001173 (msg.msg_flags & MSG_TRUNC) ||
Simon Kelley572b41e2011-02-18 18:11:18 +00001174 (header->hb3 & HB3_QR))
Simon Kelley3be34542004-09-11 19:12:13 +01001175 return;
Simon Kelley44a2a312004-03-10 20:04:35 +00001176
Simon Kelley26128d22004-11-14 16:43:54 +00001177 source_addr.sa.sa_family = listen->family;
Simon Kelley2a7a2b82014-03-22 19:18:06 +00001178
1179 if (listen->family == AF_INET)
1180 {
1181 /* Source-port == 0 is an error, we can't send back to that.
1182 http://www.ietf.org/mail-archive/web/dnsop/current/msg11441.html */
1183 if (source_addr.in.sin_port == 0)
1184 return;
1185 }
Simon Kelley26128d22004-11-14 16:43:54 +00001186#ifdef HAVE_IPV6
Simon Kelley2a7a2b82014-03-22 19:18:06 +00001187 else
1188 {
1189 /* Source-port == 0 is an error, we can't send back to that. */
1190 if (source_addr.in6.sin6_port == 0)
1191 return;
1192 source_addr.in6.sin6_flowinfo = 0;
1193 }
Simon Kelley26128d22004-11-14 16:43:54 +00001194#endif
Simon Kelley2a7a2b82014-03-22 19:18:06 +00001195
Simon Kelleyc8a80482014-03-05 14:29:54 +00001196 /* We can be configured to only accept queries from at-most-one-hop-away addresses. */
1197 if (option_bool(OPT_LOCAL_SERVICE))
1198 {
1199 struct addrlist *addr;
1200#ifdef HAVE_IPV6
1201 if (listen->family == AF_INET6)
1202 {
1203 for (addr = daemon->interface_addrs; addr; addr = addr->next)
1204 if ((addr->flags & ADDRLIST_IPV6) &&
1205 is_same_net6(&addr->addr.addr.addr6, &source_addr.in6.sin6_addr, addr->prefixlen))
1206 break;
1207 }
1208 else
1209#endif
1210 {
1211 struct in_addr netmask;
1212 for (addr = daemon->interface_addrs; addr; addr = addr->next)
1213 {
Richard Genoud15b1b7e2014-09-17 21:12:00 +01001214 netmask.s_addr = htonl(~(in_addr_t)0 << (32 - addr->prefixlen));
Simon Kelleyc8a80482014-03-05 14:29:54 +00001215 if (!(addr->flags & ADDRLIST_IPV6) &&
1216 is_same_net(addr->addr.addr.addr4, source_addr.in.sin_addr, netmask))
1217 break;
1218 }
1219 }
1220 if (!addr)
1221 {
Simon Kelley0c8584e2014-03-12 20:12:56 +00001222 static int warned = 0;
1223 if (!warned)
1224 {
1225 my_syslog(LOG_WARNING, _("Ignoring query from non-local network"));
1226 warned = 1;
1227 }
Simon Kelleyc8a80482014-03-05 14:29:54 +00001228 return;
1229 }
1230 }
1231
Simon Kelley2329bef2013-12-03 13:41:16 +00001232 if (check_dst)
Simon Kelley44a2a312004-03-10 20:04:35 +00001233 {
Simon Kelley8a911cc2004-03-16 18:35:52 +00001234 struct ifreq ifr;
1235
Simon Kelley26128d22004-11-14 16:43:54 +00001236 if (msg.msg_controllen < sizeof(struct cmsghdr))
1237 return;
1238
Simon Kelley5e9e0ef2006-04-17 14:24:29 +01001239#if defined(HAVE_LINUX_NETWORK)
Simon Kelley26128d22004-11-14 16:43:54 +00001240 if (listen->family == AF_INET)
1241 for (cmptr = CMSG_FIRSTHDR(&msg); cmptr; cmptr = CMSG_NXTHDR(&msg, cmptr))
Simon Kelleyc72daea2012-01-05 21:33:27 +00001242 if (cmptr->cmsg_level == IPPROTO_IP && cmptr->cmsg_type == IP_PKTINFO)
Simon Kelley26128d22004-11-14 16:43:54 +00001243 {
Simon Kelley8ef5ada2010-06-03 19:42:45 +01001244 union {
1245 unsigned char *c;
1246 struct in_pktinfo *p;
1247 } p;
1248 p.c = CMSG_DATA(cmptr);
1249 dst_addr_4 = dst_addr.addr.addr4 = p.p->ipi_spec_dst;
1250 if_index = p.p->ipi_ifindex;
Simon Kelley26128d22004-11-14 16:43:54 +00001251 }
1252#elif defined(IP_RECVDSTADDR) && defined(IP_RECVIF)
1253 if (listen->family == AF_INET)
1254 {
1255 for (cmptr = CMSG_FIRSTHDR(&msg); cmptr; cmptr = CMSG_NXTHDR(&msg, cmptr))
Simon Kelley8ef5ada2010-06-03 19:42:45 +01001256 {
1257 union {
1258 unsigned char *c;
1259 unsigned int *i;
1260 struct in_addr *a;
1261#ifndef HAVE_SOLARIS_NETWORK
1262 struct sockaddr_dl *s;
Simon Kelley824af852008-02-12 20:43:05 +00001263#endif
Simon Kelley8ef5ada2010-06-03 19:42:45 +01001264 } p;
1265 p.c = CMSG_DATA(cmptr);
1266 if (cmptr->cmsg_level == IPPROTO_IP && cmptr->cmsg_type == IP_RECVDSTADDR)
1267 dst_addr_4 = dst_addr.addr.addr4 = *(p.a);
1268 else if (cmptr->cmsg_level == IPPROTO_IP && cmptr->cmsg_type == IP_RECVIF)
1269#ifdef HAVE_SOLARIS_NETWORK
1270 if_index = *(p.i);
1271#else
1272 if_index = p.s->sdl_index;
1273#endif
1274 }
Simon Kelley26128d22004-11-14 16:43:54 +00001275 }
1276#endif
1277
1278#ifdef HAVE_IPV6
1279 if (listen->family == AF_INET6)
1280 {
1281 for (cmptr = CMSG_FIRSTHDR(&msg); cmptr; cmptr = CMSG_NXTHDR(&msg, cmptr))
Simon Kelleyc72daea2012-01-05 21:33:27 +00001282 if (cmptr->cmsg_level == IPPROTO_IPV6 && cmptr->cmsg_type == daemon->v6pktinfo)
Simon Kelley26128d22004-11-14 16:43:54 +00001283 {
Simon Kelley8ef5ada2010-06-03 19:42:45 +01001284 union {
1285 unsigned char *c;
1286 struct in6_pktinfo *p;
1287 } p;
1288 p.c = CMSG_DATA(cmptr);
1289
1290 dst_addr.addr.addr6 = p.p->ipi6_addr;
1291 if_index = p.p->ipi6_ifindex;
Simon Kelley26128d22004-11-14 16:43:54 +00001292 }
1293 }
1294#endif
1295
1296 /* enforce available interface configuration */
1297
Simon Kelleye25db1f2013-01-29 22:10:26 +00001298 if (!indextoname(listen->fd, if_index, ifr.ifr_name))
Simon Kelley832af0b2007-01-21 20:01:28 +00001299 return;
1300
Simon Kelleye25db1f2013-01-29 22:10:26 +00001301 if (!iface_check(listen->family, &dst_addr, ifr.ifr_name, &auth_dns))
1302 {
1303 if (!option_bool(OPT_CLEVERBIND))
Simon Kelley115ac3e2013-05-20 11:28:32 +01001304 enumerate_interfaces(0);
Simon Kelley3f2873d2013-05-14 11:28:47 +01001305 if (!loopback_exception(listen->fd, listen->family, &dst_addr, ifr.ifr_name) &&
1306 !label_exception(if_index, listen->family, &dst_addr))
Simon Kelleye25db1f2013-01-29 22:10:26 +00001307 return;
1308 }
1309
Simon Kelley552af8b2012-02-29 20:10:31 +00001310 if (listen->family == AF_INET && option_bool(OPT_LOCALISE))
1311 {
1312 struct irec *iface;
1313
1314 /* get the netmask of the interface whch has the address we were sent to.
1315 This is no neccessarily the interface we arrived on. */
1316
1317 for (iface = daemon->interfaces; iface; iface = iface->next)
1318 if (iface->addr.sa.sa_family == AF_INET &&
1319 iface->addr.in.sin_addr.s_addr == dst_addr_4.s_addr)
1320 break;
1321
1322 /* interface may be new */
Simon Kelleye25db1f2013-01-29 22:10:26 +00001323 if (!iface && !option_bool(OPT_CLEVERBIND))
Simon Kelley115ac3e2013-05-20 11:28:32 +01001324 enumerate_interfaces(0);
Simon Kelley552af8b2012-02-29 20:10:31 +00001325
1326 for (iface = daemon->interfaces; iface; iface = iface->next)
1327 if (iface->addr.sa.sa_family == AF_INET &&
1328 iface->addr.in.sin_addr.s_addr == dst_addr_4.s_addr)
1329 break;
1330
1331 /* If we failed, abandon localisation */
1332 if (iface)
1333 netmask = iface->netmask;
1334 else
1335 dst_addr_4.s_addr = 0;
1336 }
Simon Kelley44a2a312004-03-10 20:04:35 +00001337 }
Simon Kelley25cf5e32015-01-09 15:53:03 +00001338
1339 /* log_query gets called indirectly all over the place, so
1340 pass these in global variables - sorry. */
1341 daemon->log_display_id = ++daemon->log_id;
1342 daemon->log_source_addr = &source_addr;
Simon Kelley44a2a312004-03-10 20:04:35 +00001343
Simon Kelleycdeda282006-03-16 20:16:06 +00001344 if (extract_request(header, (size_t)n, daemon->namebuff, &type))
Simon Kelley44a2a312004-03-10 20:04:35 +00001345 {
Simon Kelleyb485ed92013-10-18 22:00:39 +01001346#ifdef HAVE_AUTH
1347 struct auth_zone *zone;
1348#endif
Simon Kelley610e7822014-02-06 14:45:17 +00001349 char *types = querystr(auth_dns ? "auth" : "query", type);
1350
Simon Kelley44a2a312004-03-10 20:04:35 +00001351 if (listen->family == AF_INET)
Simon Kelley3be34542004-09-11 19:12:13 +01001352 log_query(F_QUERY | F_IPV4 | F_FORWARD, daemon->namebuff,
Simon Kelley1a6bca82008-07-11 11:11:42 +01001353 (struct all_addr *)&source_addr.in.sin_addr, types);
Simon Kelley44a2a312004-03-10 20:04:35 +00001354#ifdef HAVE_IPV6
1355 else
Simon Kelley3be34542004-09-11 19:12:13 +01001356 log_query(F_QUERY | F_IPV6 | F_FORWARD, daemon->namebuff,
Simon Kelley1a6bca82008-07-11 11:11:42 +01001357 (struct all_addr *)&source_addr.in6.sin6_addr, types);
Simon Kelley44a2a312004-03-10 20:04:35 +00001358#endif
Simon Kelley44a2a312004-03-10 20:04:35 +00001359
Simon Kelley4820dce2012-12-18 18:30:30 +00001360#ifdef HAVE_AUTH
Simon Kelleyb485ed92013-10-18 22:00:39 +01001361 /* find queries for zones we're authoritative for, and answer them directly */
Simon Kelley3a3965a2015-08-09 17:45:06 +01001362 if (!auth_dns && !option_bool(OPT_LOCALISE))
Simon Kelley6008bdb2013-10-21 21:47:03 +01001363 for (zone = daemon->auth_zones; zone; zone = zone->next)
1364 if (in_zone(zone, daemon->namebuff, NULL))
1365 {
1366 auth_dns = 1;
1367 local_auth = 1;
1368 break;
1369 }
Simon Kelleyb485ed92013-10-18 22:00:39 +01001370#endif
Simon Kelleyb5ea1cc2014-07-29 16:34:14 +01001371
1372#ifdef HAVE_LOOP
1373 /* Check for forwarding loop */
1374 if (detect_loop(daemon->namebuff, type))
1375 return;
1376#endif
Simon Kelleyb485ed92013-10-18 22:00:39 +01001377 }
1378
Simon Kelley5bb88f02015-12-21 16:23:47 +00001379 if (find_pseudoheader(header, (size_t)n, NULL, &pheader, NULL, NULL))
Simon Kelleyfa14bec2015-12-20 17:12:16 +00001380 {
1381 unsigned short flags;
1382
1383 have_pseudoheader = 1;
1384 GETSHORT(udp_size, pheader);
1385 pheader += 2; /* ext_rcode */
1386 GETSHORT(flags, pheader);
1387
1388 if (flags & 0x8000)
1389 do_bit = 1;/* do bit */
1390
1391 /* If the client provides an EDNS0 UDP size, use that to limit our reply.
1392 (bounded by the maximum configured). If no EDNS0, then it
1393 defaults to 512 */
1394 if (udp_size > daemon->edns_pktsz)
1395 udp_size = daemon->edns_pktsz;
1396 }
1397
Simon Kelleyb485ed92013-10-18 22:00:39 +01001398#ifdef HAVE_AUTH
Simon Kelley4f7b3042012-11-28 21:27:02 +00001399 if (auth_dns)
Simon Kelley824af852008-02-12 20:43:05 +00001400 {
Simon Kelleyfa14bec2015-12-20 17:12:16 +00001401 m = answer_auth(header, ((char *) header) + udp_size, (size_t)n, now, &source_addr,
1402 local_auth, do_bit, have_pseudoheader);
Simon Kelley4f7b3042012-11-28 21:27:02 +00001403 if (m >= 1)
Simon Kelleyb485ed92013-10-18 22:00:39 +01001404 {
1405 send_from(listen->fd, option_bool(OPT_NOWILD) || option_bool(OPT_CLEVERBIND),
1406 (char *)header, m, &source_addr, &dst_addr, if_index);
1407 daemon->auth_answer++;
1408 }
Simon Kelley824af852008-02-12 20:43:05 +00001409 }
Simon Kelley44a2a312004-03-10 20:04:35 +00001410 else
Simon Kelley4820dce2012-12-18 18:30:30 +00001411#endif
Simon Kelley4f7b3042012-11-28 21:27:02 +00001412 {
Simon Kelleyfa14bec2015-12-20 17:12:16 +00001413 int ad_reqd = do_bit;
1414 /* RFC 6840 5.7 */
1415 if (header->hb4 & HB4_AD)
1416 ad_reqd = 1;
1417
1418 m = answer_request(header, ((char *) header) + udp_size, (size_t)n,
1419 dst_addr_4, netmask, now, ad_reqd, do_bit, have_pseudoheader);
Simon Kelley4f7b3042012-11-28 21:27:02 +00001420
1421 if (m >= 1)
1422 {
1423 send_from(listen->fd, option_bool(OPT_NOWILD) || option_bool(OPT_CLEVERBIND),
1424 (char *)header, m, &source_addr, &dst_addr, if_index);
1425 daemon->local_answer++;
1426 }
1427 else if (forward_query(listen->fd, &source_addr, &dst_addr, if_index,
Simon Kelley613ad152014-02-25 23:02:28 +00001428 header, (size_t)n, now, NULL, ad_reqd, do_bit))
Simon Kelley4f7b3042012-11-28 21:27:02 +00001429 daemon->queries_forwarded++;
1430 else
1431 daemon->local_answer++;
1432 }
Simon Kelley44a2a312004-03-10 20:04:35 +00001433}
1434
Simon Kelley7d7b7b32014-01-08 15:53:35 +00001435#ifdef HAVE_DNSSEC
Simon Kelley7fa836e2014-02-10 20:11:24 +00001436static int tcp_key_recurse(time_t now, int status, struct dns_header *header, size_t n,
1437 int class, char *name, char *keyname, struct server *server, int *keycount)
Simon Kelley7d7b7b32014-01-08 15:53:35 +00001438{
1439 /* Recurse up the key heirarchy */
Simon Kelley7d7b7b32014-01-08 15:53:35 +00001440 int new_status;
Simon Kelley9a31b682015-12-15 10:20:39 +00001441 unsigned char *packet = NULL;
1442 size_t m;
1443 unsigned char *payload = NULL;
1444 struct dns_header *new_header = NULL;
1445 u16 *length = NULL;
1446 unsigned char c1, c2;
Simon Kelley7d7b7b32014-01-08 15:53:35 +00001447
Simon Kelley9a31b682015-12-15 10:20:39 +00001448 while (1)
Simon Kelley00a5b5d2014-02-28 18:10:55 +00001449 {
Simon Kelley9a31b682015-12-15 10:20:39 +00001450 /* limit the amount of work we do, to avoid cycling forever on loops in the DNS */
1451 if (--(*keycount) == 0)
1452 new_status = STAT_ABANDONED;
1453 else if (status == STAT_NEED_KEY)
1454 new_status = dnssec_validate_by_ds(now, header, n, name, keyname, class);
1455 else if (status == STAT_NEED_DS)
1456 new_status = dnssec_validate_ds(now, header, n, name, keyname, class);
1457 else
1458 new_status = dnssec_validate_reply(now, header, n, name, keyname, &class, option_bool(OPT_DNSSEC_NO_SIGN), NULL, NULL);
Simon Kelley00a5b5d2014-02-28 18:10:55 +00001459
Simon Kelley9a31b682015-12-15 10:20:39 +00001460 if (new_status != STAT_NEED_DS && new_status != STAT_NEED_KEY)
1461 break;
Simon Kelley00a5b5d2014-02-28 18:10:55 +00001462
Simon Kelley9a31b682015-12-15 10:20:39 +00001463 /* Can't validate because we need a key/DS whose name now in keyname.
1464 Make query for same, and recurse to validate */
Simon Kelley7fa836e2014-02-10 20:11:24 +00001465 if (!packet)
Simon Kelley9a31b682015-12-15 10:20:39 +00001466 {
1467 packet = whine_malloc(65536 + MAXDNAME + RRFIXEDSZ + sizeof(u16));
1468 payload = &packet[2];
1469 new_header = (struct dns_header *)payload;
1470 length = (u16 *)packet;
1471 }
1472
1473 if (!packet)
1474 {
1475 new_status = STAT_ABANDONED;
1476 break;
1477 }
1478
Simon Kelley33702ab2015-12-28 23:17:15 +00001479 m = dnssec_generate_query(new_header, ((unsigned char *) new_header) + 65536, keyname, class,
Simon Kelleya77cec82015-05-08 16:25:38 +01001480 new_status == STAT_NEED_KEY ? T_DNSKEY : T_DS, &server->addr, server->edns_pktsz);
Simon Kelley7d7b7b32014-01-08 15:53:35 +00001481
Simon Kelley7fa836e2014-02-10 20:11:24 +00001482 *length = htons(m);
1483
1484 if (!read_write(server->tcpfd, packet, m + sizeof(u16), 0) ||
1485 !read_write(server->tcpfd, &c1, 1, 1) ||
1486 !read_write(server->tcpfd, &c2, 1, 1) ||
1487 !read_write(server->tcpfd, payload, (c1 << 8) | c2, 1))
Simon Kelley7d7b7b32014-01-08 15:53:35 +00001488 {
Simon Kelley9a31b682015-12-15 10:20:39 +00001489 new_status = STAT_ABANDONED;
1490 break;
Simon Kelley7d7b7b32014-01-08 15:53:35 +00001491 }
Simon Kelley9a31b682015-12-15 10:20:39 +00001492
1493 m = (c1 << 8) | c2;
Simon Kelleyfe3992f2015-04-03 21:25:05 +01001494
Simon Kelley9a31b682015-12-15 10:20:39 +00001495 new_status = tcp_key_recurse(now, new_status, new_header, m, class, name, keyname, server, keycount);
1496
1497 if (new_status != STAT_OK)
1498 break;
Simon Kelley7d7b7b32014-01-08 15:53:35 +00001499 }
Simon Kelley9a31b682015-12-15 10:20:39 +00001500
1501 if (packet)
1502 free(packet);
1503
Simon Kelley7d7b7b32014-01-08 15:53:35 +00001504 return new_status;
1505}
1506#endif
1507
1508
Simon Kelleyfeba5c12004-07-27 20:28:58 +01001509/* The daemon forks before calling this: it should deal with one connection,
1510 blocking as neccessary, and then return. Note, need to be a bit careful
1511 about resources for debug mode, when the fork is suppressed: that's
1512 done by the caller. */
Simon Kelley5aabfc72007-08-29 11:24:47 +01001513unsigned char *tcp_request(int confd, time_t now,
Simon Kelley4f7b3042012-11-28 21:27:02 +00001514 union mysockaddr *local_addr, struct in_addr netmask, int auth_dns)
Simon Kelleyfeba5c12004-07-27 20:28:58 +01001515{
Simon Kelley28866e92011-02-14 20:19:14 +00001516 size_t size = 0;
1517 int norebind = 0;
Vladislav Grishenko3b195962013-11-26 11:08:21 +00001518#ifdef HAVE_AUTH
Simon Kelley19b16892013-10-20 10:19:39 +01001519 int local_auth = 0;
Vladislav Grishenko3b195962013-11-26 11:08:21 +00001520#endif
Simon Kelleyfa14bec2015-12-20 17:12:16 +00001521 int checking_disabled, do_bit, added_pheader = 0, have_pseudoheader = 0;
Simon Kelleyfe3992f2015-04-03 21:25:05 +01001522 int check_subnet, no_cache_dnssec = 0, cache_secure = 0, bogusanswer = 0;
Simon Kelleycdeda282006-03-16 20:16:06 +00001523 size_t m;
Simon Kelleyee86ce62012-12-07 11:54:46 +00001524 unsigned short qtype;
1525 unsigned int gotname;
Simon Kelleyfeba5c12004-07-27 20:28:58 +01001526 unsigned char c1, c2;
Simon Kelley4b5ea122013-04-22 10:18:26 +01001527 /* Max TCP packet + slop + size */
1528 unsigned char *packet = whine_malloc(65536 + MAXDNAME + RRFIXEDSZ + sizeof(u16));
1529 unsigned char *payload = &packet[2];
1530 /* largest field in header is 16-bits, so this is still sufficiently aligned */
1531 struct dns_header *header = (struct dns_header *)payload;
1532 u16 *length = (u16 *)packet;
Simon Kelley3be34542004-09-11 19:12:13 +01001533 struct server *last_server;
Simon Kelley7de060b2011-08-26 17:24:52 +01001534 struct in_addr dst_addr_4;
1535 union mysockaddr peer_addr;
1536 socklen_t peer_len = sizeof(union mysockaddr);
Simon Kelley25cf5e32015-01-09 15:53:03 +00001537 int query_count = 0;
Simon Kelleyfa14bec2015-12-20 17:12:16 +00001538 unsigned char *pheader;
Simon Kelley25cf5e32015-01-09 15:53:03 +00001539
Simon Kelley7de060b2011-08-26 17:24:52 +01001540 if (getpeername(confd, (struct sockaddr *)&peer_addr, &peer_len) == -1)
1541 return packet;
Simon Kelleyc8a80482014-03-05 14:29:54 +00001542
1543 /* We can be configured to only accept queries from at-most-one-hop-away addresses. */
1544 if (option_bool(OPT_LOCAL_SERVICE))
1545 {
1546 struct addrlist *addr;
1547#ifdef HAVE_IPV6
1548 if (peer_addr.sa.sa_family == AF_INET6)
1549 {
1550 for (addr = daemon->interface_addrs; addr; addr = addr->next)
1551 if ((addr->flags & ADDRLIST_IPV6) &&
1552 is_same_net6(&addr->addr.addr.addr6, &peer_addr.in6.sin6_addr, addr->prefixlen))
1553 break;
1554 }
1555 else
1556#endif
1557 {
1558 struct in_addr netmask;
1559 for (addr = daemon->interface_addrs; addr; addr = addr->next)
1560 {
Richard Genoud15b1b7e2014-09-17 21:12:00 +01001561 netmask.s_addr = htonl(~(in_addr_t)0 << (32 - addr->prefixlen));
Simon Kelleyc8a80482014-03-05 14:29:54 +00001562 if (!(addr->flags & ADDRLIST_IPV6) &&
1563 is_same_net(addr->addr.addr.addr4, peer_addr.in.sin_addr, netmask))
1564 break;
1565 }
1566 }
1567 if (!addr)
1568 {
1569 my_syslog(LOG_WARNING, _("Ignoring query from non-local network"));
1570 return packet;
1571 }
1572 }
Simon Kelley7de060b2011-08-26 17:24:52 +01001573
Simon Kelleyfeba5c12004-07-27 20:28:58 +01001574 while (1)
1575 {
Simon Kelley25cf5e32015-01-09 15:53:03 +00001576 if (query_count == TCP_MAX_QUERIES ||
1577 !packet ||
Simon Kelleyfeba5c12004-07-27 20:28:58 +01001578 !read_write(confd, &c1, 1, 1) || !read_write(confd, &c2, 1, 1) ||
1579 !(size = c1 << 8 | c2) ||
Simon Kelley4b5ea122013-04-22 10:18:26 +01001580 !read_write(confd, payload, size, 1))
Simon Kelleyfeba5c12004-07-27 20:28:58 +01001581 return packet;
1582
Simon Kelley572b41e2011-02-18 18:11:18 +00001583 if (size < (int)sizeof(struct dns_header))
Simon Kelleyfeba5c12004-07-27 20:28:58 +01001584 continue;
1585
Simon Kelley25cf5e32015-01-09 15:53:03 +00001586 query_count++;
1587
1588 /* log_query gets called indirectly all over the place, so
1589 pass these in global variables - sorry. */
1590 daemon->log_display_id = ++daemon->log_id;
1591 daemon->log_source_addr = &peer_addr;
1592
Simon Kelley28866e92011-02-14 20:19:14 +00001593 /* save state of "cd" flag in query */
Simon Kelley7d7b7b32014-01-08 15:53:35 +00001594 if ((checking_disabled = header->hb4 & HB4_CD))
1595 no_cache_dnssec = 1;
Simon Kelley28866e92011-02-14 20:19:14 +00001596
Simon Kelley3be34542004-09-11 19:12:13 +01001597 if ((gotname = extract_request(header, (unsigned int)size, daemon->namebuff, &qtype)))
Simon Kelleyfeba5c12004-07-27 20:28:58 +01001598 {
Simon Kelleyb485ed92013-10-18 22:00:39 +01001599#ifdef HAVE_AUTH
1600 struct auth_zone *zone;
1601#endif
Simon Kelley610e7822014-02-06 14:45:17 +00001602 char *types = querystr(auth_dns ? "auth" : "query", qtype);
Simon Kelley7de060b2011-08-26 17:24:52 +01001603
1604 if (peer_addr.sa.sa_family == AF_INET)
1605 log_query(F_QUERY | F_IPV4 | F_FORWARD, daemon->namebuff,
1606 (struct all_addr *)&peer_addr.in.sin_addr, types);
Simon Kelleyfeba5c12004-07-27 20:28:58 +01001607#ifdef HAVE_IPV6
Simon Kelley7de060b2011-08-26 17:24:52 +01001608 else
1609 log_query(F_QUERY | F_IPV6 | F_FORWARD, daemon->namebuff,
1610 (struct all_addr *)&peer_addr.in6.sin6_addr, types);
Simon Kelleyfeba5c12004-07-27 20:28:58 +01001611#endif
Simon Kelleyb485ed92013-10-18 22:00:39 +01001612
1613#ifdef HAVE_AUTH
1614 /* find queries for zones we're authoritative for, and answer them directly */
Simon Kelley3a3965a2015-08-09 17:45:06 +01001615 if (!auth_dns && !option_bool(OPT_LOCALISE))
Simon Kelley6008bdb2013-10-21 21:47:03 +01001616 for (zone = daemon->auth_zones; zone; zone = zone->next)
1617 if (in_zone(zone, daemon->namebuff, NULL))
1618 {
1619 auth_dns = 1;
1620 local_auth = 1;
1621 break;
1622 }
Simon Kelleyb485ed92013-10-18 22:00:39 +01001623#endif
Simon Kelleyfeba5c12004-07-27 20:28:58 +01001624 }
1625
Simon Kelley7de060b2011-08-26 17:24:52 +01001626 if (local_addr->sa.sa_family == AF_INET)
1627 dst_addr_4 = local_addr->in.sin_addr;
1628 else
1629 dst_addr_4.s_addr = 0;
1630
Simon Kelleyfa14bec2015-12-20 17:12:16 +00001631 do_bit = 0;
1632
Simon Kelley5bb88f02015-12-21 16:23:47 +00001633 if (find_pseudoheader(header, (size_t)size, NULL, &pheader, NULL, NULL))
Simon Kelleyfa14bec2015-12-20 17:12:16 +00001634 {
1635 unsigned short flags;
1636
1637 have_pseudoheader = 1;
1638 pheader += 4; /* udp_size, ext_rcode */
1639 GETSHORT(flags, pheader);
1640
1641 if (flags & 0x8000)
Simon Kelley5bb88f02015-12-21 16:23:47 +00001642 do_bit = 1; /* do bit */
Simon Kelleyfa14bec2015-12-20 17:12:16 +00001643 }
1644
Simon Kelley4820dce2012-12-18 18:30:30 +00001645#ifdef HAVE_AUTH
Simon Kelley4f7b3042012-11-28 21:27:02 +00001646 if (auth_dns)
Simon Kelleyfa14bec2015-12-20 17:12:16 +00001647 m = answer_auth(header, ((char *) header) + 65536, (size_t)size, now, &peer_addr,
1648 local_auth, do_bit, have_pseudoheader);
Simon Kelley4f7b3042012-11-28 21:27:02 +00001649 else
Simon Kelley4820dce2012-12-18 18:30:30 +00001650#endif
Simon Kelleyfeba5c12004-07-27 20:28:58 +01001651 {
Simon Kelleyfa14bec2015-12-20 17:12:16 +00001652 int ad_reqd = do_bit;
1653 /* RFC 6840 5.7 */
1654 if (header->hb4 & HB4_AD)
1655 ad_reqd = 1;
1656
1657 /* m > 0 if answered from cache */
1658 m = answer_request(header, ((char *) header) + 65536, (size_t)size,
1659 dst_addr_4, netmask, now, ad_reqd, do_bit, have_pseudoheader);
Simon Kelleyfeba5c12004-07-27 20:28:58 +01001660
Simon Kelley4f7b3042012-11-28 21:27:02 +00001661 /* Do this by steam now we're not in the select() loop */
Simon Kelleyb842bc92015-07-12 21:09:11 +01001662 check_log_writer(1);
Simon Kelley4f7b3042012-11-28 21:27:02 +00001663
1664 if (m == 0)
Simon Kelleyfeba5c12004-07-27 20:28:58 +01001665 {
Simon Kelley4f7b3042012-11-28 21:27:02 +00001666 unsigned int flags = 0;
1667 struct all_addr *addrp = NULL;
1668 int type = 0;
1669 char *domain = NULL;
Simon Kelley33702ab2015-12-28 23:17:15 +00001670 size_t new_size = add_edns0_config(header, size, ((unsigned char *) header) + 65536, &peer_addr, now, &check_subnet);
Simon Kelleyed4c0762013-10-08 20:46:34 +01001671
Simon Kelley33702ab2015-12-28 23:17:15 +00001672 if (size != new_size)
1673 {
1674 added_pheader = 1;
1675 size = new_size;
1676 }
1677
Simon Kelley4f7b3042012-11-28 21:27:02 +00001678 if (gotname)
1679 flags = search_servers(now, &addrp, gotname, daemon->namebuff, &type, &domain, &norebind);
1680
Simon Kelley367341f2016-01-12 15:58:23 +00001681#ifdef HAVE_DNSSEC
1682 type &= ~SERV_DO_DNSSEC;
1683#endif
1684
Simon Kelley4f7b3042012-11-28 21:27:02 +00001685 if (type != 0 || option_bool(OPT_ORDER) || !daemon->last_server)
1686 last_server = daemon->servers;
1687 else
1688 last_server = daemon->last_server;
1689
1690 if (!flags && last_server)
1691 {
1692 struct server *firstsendto = NULL;
Simon Kelley8a9be9e2014-01-25 23:17:21 +00001693#ifdef HAVE_DNSSEC
Simon Kelley703c7ff2014-01-25 23:46:23 +00001694 unsigned char *newhash, hash[HASH_SIZE];
Simon Kelley63758382014-04-16 22:20:55 +01001695 if ((newhash = hash_questions(header, (unsigned int)size, daemon->namebuff)))
Simon Kelley8a9be9e2014-01-25 23:17:21 +00001696 memcpy(hash, newhash, HASH_SIZE);
Tomas Hozzab37f8b92014-03-25 20:52:28 +00001697 else
1698 memset(hash, 0, HASH_SIZE);
Simon Kelley8a9be9e2014-01-25 23:17:21 +00001699#else
Simon Kelley4f7b3042012-11-28 21:27:02 +00001700 unsigned int crc = questions_crc(header, (unsigned int)size, daemon->namebuff);
Simon Kelley8a9be9e2014-01-25 23:17:21 +00001701#endif
Simon Kelley4f7b3042012-11-28 21:27:02 +00001702 /* Loop round available servers until we succeed in connecting to one.
1703 Note that this code subtley ensures that consecutive queries on this connection
1704 which can go to the same server, do so. */
1705 while (1)
Simon Kelleyfeba5c12004-07-27 20:28:58 +01001706 {
Simon Kelley4f7b3042012-11-28 21:27:02 +00001707 if (!firstsendto)
1708 firstsendto = last_server;
1709 else
1710 {
1711 if (!(last_server = last_server->next))
1712 last_server = daemon->servers;
1713
1714 if (last_server == firstsendto)
1715 break;
1716 }
1717
1718 /* server for wrong domain */
1719 if (type != (last_server->flags & SERV_TYPE) ||
Simon Kelleyb5ea1cc2014-07-29 16:34:14 +01001720 (type == SERV_HAS_DOMAIN && !hostname_isequal(domain, last_server->domain)) ||
1721 (last_server->flags & (SERV_LITERAL_ADDRESS | SERV_LOOP)))
Simon Kelley7de060b2011-08-26 17:24:52 +01001722 continue;
1723
Simon Kelley4f7b3042012-11-28 21:27:02 +00001724 if (last_server->tcpfd == -1)
1725 {
1726 if ((last_server->tcpfd = socket(last_server->addr.sa.sa_family, SOCK_STREAM, 0)) == -1)
1727 continue;
1728
Karl Vogele9828b62014-10-03 21:45:15 +01001729#ifdef HAVE_CONNTRACK
1730 /* Copy connection mark of incoming query to outgoing connection. */
1731 if (option_bool(OPT_CONNTRACK))
1732 {
1733 unsigned int mark;
1734 struct all_addr local;
1735#ifdef HAVE_IPV6
1736 if (local_addr->sa.sa_family == AF_INET6)
1737 local.addr.addr6 = local_addr->in6.sin6_addr;
1738 else
1739#endif
1740 local.addr.addr4 = local_addr->in.sin_addr;
1741
1742 if (get_incoming_mark(&peer_addr, &local, 1, &mark))
1743 setsockopt(last_server->tcpfd, SOL_SOCKET, SO_MARK, &mark, sizeof(unsigned int));
1744 }
1745#endif
1746
Simon Kelley4f7b3042012-11-28 21:27:02 +00001747 if ((!local_bind(last_server->tcpfd, &last_server->source_addr, last_server->interface, 1) ||
1748 connect(last_server->tcpfd, &last_server->addr.sa, sa_len(&last_server->addr)) == -1))
1749 {
1750 close(last_server->tcpfd);
1751 last_server->tcpfd = -1;
1752 continue;
1753 }
1754
Simon Kelley7d7b7b32014-01-08 15:53:35 +00001755#ifdef HAVE_DNSSEC
Simon Kelley367341f2016-01-12 15:58:23 +00001756 if (option_bool(OPT_DNSSEC_VALID) && (last_server->flags & SERV_DO_DNSSEC))
Simon Kelley7d7b7b32014-01-08 15:53:35 +00001757 {
Simon Kelley33702ab2015-12-28 23:17:15 +00001758 new_size = add_do_bit(header, size, ((unsigned char *) header) + 65536);
1759
1760 if (size != new_size)
1761 {
1762 added_pheader = 1;
1763 size = new_size;
1764 }
Simon Kelley613ad152014-02-25 23:02:28 +00001765
Simon Kelley2ecd9bd2014-02-13 16:42:02 +00001766 /* For debugging, set Checking Disabled, otherwise, have the upstream check too,
1767 this allows it to select auth servers when one is returning bad data. */
1768 if (option_bool(OPT_DNSSEC_DEBUG))
1769 header->hb4 |= HB4_CD;
Simon Kelley7d7b7b32014-01-08 15:53:35 +00001770 }
1771#endif
Simon Kelley4f7b3042012-11-28 21:27:02 +00001772 }
1773
Simon Kelley4b5ea122013-04-22 10:18:26 +01001774 *length = htons(size);
Simon Kelley1fc02682014-04-29 12:30:18 +01001775
1776 /* get query name again for logging - may have been overwritten */
1777 if (!(gotname = extract_request(header, (unsigned int)size, daemon->namebuff, &qtype)))
1778 strcpy(daemon->namebuff, "query");
Simon Kelley4f7b3042012-11-28 21:27:02 +00001779
Simon Kelley4b5ea122013-04-22 10:18:26 +01001780 if (!read_write(last_server->tcpfd, packet, size + sizeof(u16), 0) ||
Simon Kelley4f7b3042012-11-28 21:27:02 +00001781 !read_write(last_server->tcpfd, &c1, 1, 1) ||
Simon Kelley7d7b7b32014-01-08 15:53:35 +00001782 !read_write(last_server->tcpfd, &c2, 1, 1) ||
1783 !read_write(last_server->tcpfd, payload, (c1 << 8) | c2, 1))
Simon Kelley7de060b2011-08-26 17:24:52 +01001784 {
1785 close(last_server->tcpfd);
1786 last_server->tcpfd = -1;
1787 continue;
Simon Kelley4f7b3042012-11-28 21:27:02 +00001788 }
1789
1790 m = (c1 << 8) | c2;
Simon Kelley4f7b3042012-11-28 21:27:02 +00001791
Simon Kelley4f7b3042012-11-28 21:27:02 +00001792 if (last_server->addr.sa.sa_family == AF_INET)
1793 log_query(F_SERVER | F_IPV4 | F_FORWARD, daemon->namebuff,
1794 (struct all_addr *)&last_server->addr.in.sin_addr, NULL);
Simon Kelleyfeba5c12004-07-27 20:28:58 +01001795#ifdef HAVE_IPV6
Simon Kelley4f7b3042012-11-28 21:27:02 +00001796 else
1797 log_query(F_SERVER | F_IPV6 | F_FORWARD, daemon->namebuff,
1798 (struct all_addr *)&last_server->addr.in6.sin6_addr, NULL);
Simon Kelleyfeba5c12004-07-27 20:28:58 +01001799#endif
Simon Kelley7d7b7b32014-01-08 15:53:35 +00001800
1801#ifdef HAVE_DNSSEC
Simon Kelley367341f2016-01-12 15:58:23 +00001802 if (option_bool(OPT_DNSSEC_VALID) && !checking_disabled && (last_server->flags & SERV_DO_DNSSEC))
Simon Kelley7d7b7b32014-01-08 15:53:35 +00001803 {
Simon Kelley7fa836e2014-02-10 20:11:24 +00001804 int keycount = DNSSEC_WORK; /* Limit to number of DNSSEC questions, to catch loops and avoid filling cache. */
Simon Kelley9a31b682015-12-15 10:20:39 +00001805 int status = tcp_key_recurse(now, STAT_OK, header, m, 0, daemon->namebuff, daemon->keyname, last_server, &keycount);
Simon Kelley554b5802015-04-17 22:50:20 +01001806 char *result, *domain = "result";
Simon Kelleyfe3992f2015-04-03 21:25:05 +01001807
Simon Kelley9a31b682015-12-15 10:20:39 +00001808 if (status == STAT_ABANDONED)
Simon Kelley150162b2015-03-27 09:58:26 +00001809 {
1810 result = "ABANDONED";
1811 status = STAT_BOGUS;
1812 }
Simon Kelley7fa836e2014-02-10 20:11:24 +00001813 else
1814 result = (status == STAT_SECURE ? "SECURE" : (status == STAT_INSECURE ? "INSECURE" : "BOGUS"));
Simon Kelleye66b4df2015-04-28 20:45:57 +01001815
1816 if (status == STAT_BOGUS && extract_request(header, m, daemon->namebuff, NULL))
1817 domain = daemon->namebuff;
Simon Kelley554b5802015-04-17 22:50:20 +01001818
1819 log_query(F_KEYTAG | F_SECSTAT, domain, NULL, result);
Simon Kelley7fa836e2014-02-10 20:11:24 +00001820
Simon Kelley7d7b7b32014-01-08 15:53:35 +00001821 if (status == STAT_BOGUS)
Simon Kelleyfe3992f2015-04-03 21:25:05 +01001822 {
1823 no_cache_dnssec = 1;
1824 bogusanswer = 1;
1825 }
1826
Simon Kelley7d7b7b32014-01-08 15:53:35 +00001827 if (status == STAT_SECURE)
1828 cache_secure = 1;
1829 }
1830#endif
1831
1832 /* restore CD bit to the value in the query */
1833 if (checking_disabled)
1834 header->hb4 |= HB4_CD;
1835 else
1836 header->hb4 &= ~HB4_CD;
Simon Kelley4f7b3042012-11-28 21:27:02 +00001837
1838 /* There's no point in updating the cache, since this process will exit and
1839 lose the information after a few queries. We make this call for the alias and
1840 bogus-nxdomain side-effects. */
1841 /* If the crc of the question section doesn't match the crc we sent, then
1842 someone might be attempting to insert bogus values into the cache by
1843 sending replies containing questions and bogus answers. */
Simon Kelley8a9be9e2014-01-25 23:17:21 +00001844#ifdef HAVE_DNSSEC
1845 newhash = hash_questions(header, (unsigned int)m, daemon->namebuff);
1846 if (!newhash || memcmp(hash, newhash, HASH_SIZE) != 0)
Simon Kelley703c7ff2014-01-25 23:46:23 +00001847 {
1848 m = 0;
1849 break;
1850 }
Simon Kelley8a9be9e2014-01-25 23:17:21 +00001851#else
1852 if (crc != questions_crc(header, (unsigned int)m, daemon->namebuff))
Simon Kelley703c7ff2014-01-25 23:46:23 +00001853 {
1854 m = 0;
1855 break;
1856 }
Simon Kelley8a9be9e2014-01-25 23:17:21 +00001857#endif
1858
1859 m = process_reply(header, now, last_server, (unsigned int)m,
Simon Kelleye66b4df2015-04-28 20:45:57 +01001860 option_bool(OPT_NO_REBIND) && !norebind, no_cache_dnssec, cache_secure, bogusanswer,
Simon Kelleyfa14bec2015-12-20 17:12:16 +00001861 ad_reqd, do_bit, added_pheader, check_subnet, &peer_addr);
Simon Kelley4f7b3042012-11-28 21:27:02 +00001862
1863 break;
1864 }
Simon Kelleyfeba5c12004-07-27 20:28:58 +01001865 }
Simon Kelley4f7b3042012-11-28 21:27:02 +00001866
1867 /* In case of local answer or no connections made. */
1868 if (m == 0)
1869 m = setup_reply(header, (unsigned int)size, addrp, flags, daemon->local_ttl);
Simon Kelleyfeba5c12004-07-27 20:28:58 +01001870 }
Simon Kelleyfeba5c12004-07-27 20:28:58 +01001871 }
Simon Kelley4f7b3042012-11-28 21:27:02 +00001872
Simon Kelleyb842bc92015-07-12 21:09:11 +01001873 check_log_writer(1);
Simon Kelleyfeba5c12004-07-27 20:28:58 +01001874
Simon Kelley4b5ea122013-04-22 10:18:26 +01001875 *length = htons(m);
1876
1877 if (m == 0 || !read_write(confd, packet, m + sizeof(u16), 0))
Simon Kelleyfeba5c12004-07-27 20:28:58 +01001878 return packet;
1879 }
1880}
1881
Simon Kelley16972692006-10-16 20:04:18 +01001882static struct frec *allocate_frec(time_t now)
Simon Kelley9e4abcb2004-01-22 19:47:41 +00001883{
Simon Kelley16972692006-10-16 20:04:18 +01001884 struct frec *f;
Simon Kelley9e4abcb2004-01-22 19:47:41 +00001885
Simon Kelley5aabfc72007-08-29 11:24:47 +01001886 if ((f = (struct frec *)whine_malloc(sizeof(struct frec))))
Simon Kelley9e4abcb2004-01-22 19:47:41 +00001887 {
Simon Kelley1a6bca82008-07-11 11:11:42 +01001888 f->next = daemon->frec_list;
Simon Kelley9e4abcb2004-01-22 19:47:41 +00001889 f->time = now;
Simon Kelley832af0b2007-01-21 20:01:28 +00001890 f->sentto = NULL;
Simon Kelley1a6bca82008-07-11 11:11:42 +01001891 f->rfd4 = NULL;
Simon Kelley28866e92011-02-14 20:19:14 +00001892 f->flags = 0;
Simon Kelley1a6bca82008-07-11 11:11:42 +01001893#ifdef HAVE_IPV6
1894 f->rfd6 = NULL;
1895#endif
Simon Kelley3a237152013-12-12 12:15:50 +00001896#ifdef HAVE_DNSSEC
Simon Kelley97bc7982014-01-31 10:19:52 +00001897 f->dependent = NULL;
Simon Kelley3a237152013-12-12 12:15:50 +00001898 f->blocking_query = NULL;
Simon Kelley4619d942014-01-16 19:53:06 +00001899 f->stash = NULL;
Simon Kelley3a237152013-12-12 12:15:50 +00001900#endif
Simon Kelley1a6bca82008-07-11 11:11:42 +01001901 daemon->frec_list = f;
Simon Kelley9e4abcb2004-01-22 19:47:41 +00001902 }
Simon Kelley16972692006-10-16 20:04:18 +01001903
1904 return f;
1905}
1906
Simon Kelleyb5ea1cc2014-07-29 16:34:14 +01001907struct randfd *allocate_rfd(int family)
Simon Kelley1a6bca82008-07-11 11:11:42 +01001908{
1909 static int finger = 0;
1910 int i;
1911
1912 /* limit the number of sockets we have open to avoid starvation of
1913 (eg) TFTP. Once we have a reasonable number, randomness should be OK */
1914
1915 for (i = 0; i < RANDOM_SOCKS; i++)
Simon Kelley9009d742008-11-14 20:04:27 +00001916 if (daemon->randomsocks[i].refcount == 0)
Simon Kelley1a6bca82008-07-11 11:11:42 +01001917 {
Simon Kelley9009d742008-11-14 20:04:27 +00001918 if ((daemon->randomsocks[i].fd = random_sock(family)) == -1)
1919 break;
1920
Simon Kelley1a6bca82008-07-11 11:11:42 +01001921 daemon->randomsocks[i].refcount = 1;
1922 daemon->randomsocks[i].family = family;
1923 return &daemon->randomsocks[i];
1924 }
1925
Simon Kelley9009d742008-11-14 20:04:27 +00001926 /* No free ones or cannot get new socket, grab an existing one */
Simon Kelley1a6bca82008-07-11 11:11:42 +01001927 for (i = 0; i < RANDOM_SOCKS; i++)
1928 {
1929 int j = (i+finger) % RANDOM_SOCKS;
Simon Kelley9009d742008-11-14 20:04:27 +00001930 if (daemon->randomsocks[j].refcount != 0 &&
1931 daemon->randomsocks[j].family == family &&
1932 daemon->randomsocks[j].refcount != 0xffff)
Simon Kelley1a6bca82008-07-11 11:11:42 +01001933 {
1934 finger = j;
1935 daemon->randomsocks[j].refcount++;
1936 return &daemon->randomsocks[j];
1937 }
1938 }
1939
1940 return NULL; /* doom */
1941}
Simon Kelleyb5ea1cc2014-07-29 16:34:14 +01001942
1943void free_rfd(struct randfd *rfd)
1944{
1945 if (rfd && --(rfd->refcount) == 0)
1946 close(rfd->fd);
1947}
1948
Simon Kelley1a6bca82008-07-11 11:11:42 +01001949static void free_frec(struct frec *f)
1950{
Simon Kelleyb5ea1cc2014-07-29 16:34:14 +01001951 free_rfd(f->rfd4);
Simon Kelley1a6bca82008-07-11 11:11:42 +01001952 f->rfd4 = NULL;
1953 f->sentto = NULL;
Simon Kelley28866e92011-02-14 20:19:14 +00001954 f->flags = 0;
Simon Kelley1a6bca82008-07-11 11:11:42 +01001955
1956#ifdef HAVE_IPV6
Simon Kelleyb5ea1cc2014-07-29 16:34:14 +01001957 free_rfd(f->rfd6);
Simon Kelley1a6bca82008-07-11 11:11:42 +01001958 f->rfd6 = NULL;
1959#endif
Simon Kelley3a237152013-12-12 12:15:50 +00001960
1961#ifdef HAVE_DNSSEC
1962 if (f->stash)
Simon Kelley0fc2f312014-01-08 10:26:58 +00001963 {
1964 blockdata_free(f->stash);
1965 f->stash = NULL;
1966 }
Simon Kelley3a237152013-12-12 12:15:50 +00001967
1968 /* Anything we're waiting on is pointless now, too */
1969 if (f->blocking_query)
1970 free_frec(f->blocking_query);
1971 f->blocking_query = NULL;
Simon Kelley39048ad2014-01-21 17:33:58 +00001972 f->dependent = NULL;
Simon Kelley3a237152013-12-12 12:15:50 +00001973#endif
Simon Kelley1a6bca82008-07-11 11:11:42 +01001974}
1975
Simon Kelley16972692006-10-16 20:04:18 +01001976/* if wait==NULL return a free or older than TIMEOUT record.
1977 else return *wait zero if one available, or *wait is delay to
Simon Kelley1a6bca82008-07-11 11:11:42 +01001978 when the oldest in-use record will expire. Impose an absolute
Simon Kelley3a237152013-12-12 12:15:50 +00001979 limit of 4*TIMEOUT before we wipe things (for random sockets).
1980 If force is set, always return a result, even if we have
1981 to allocate above the limit. */
1982struct frec *get_new_frec(time_t now, int *wait, int force)
Simon Kelley16972692006-10-16 20:04:18 +01001983{
Simon Kelley1a6bca82008-07-11 11:11:42 +01001984 struct frec *f, *oldest, *target;
Simon Kelley16972692006-10-16 20:04:18 +01001985 int count;
1986
1987 if (wait)
1988 *wait = 0;
1989
Simon Kelley1a6bca82008-07-11 11:11:42 +01001990 for (f = daemon->frec_list, oldest = NULL, target = NULL, count = 0; f; f = f->next, count++)
Simon Kelley832af0b2007-01-21 20:01:28 +00001991 if (!f->sentto)
Simon Kelley1a6bca82008-07-11 11:11:42 +01001992 target = f;
1993 else
Simon Kelley16972692006-10-16 20:04:18 +01001994 {
Simon Kelley9a31b682015-12-15 10:20:39 +00001995#ifdef HAVE_DNSSEC
1996 /* Don't free DNSSEC sub-queries here, as we may end up with
1997 dangling references to them. They'll go when their "real" query
1998 is freed. */
1999 if (!f->dependent)
2000#endif
2001 {
2002 if (difftime(now, f->time) >= 4*TIMEOUT)
2003 {
2004 free_frec(f);
2005 target = f;
2006 }
2007
2008
2009 if (!oldest || difftime(f->time, oldest->time) <= 0)
2010 oldest = f;
2011 }
Simon Kelley16972692006-10-16 20:04:18 +01002012 }
Simon Kelley1a6bca82008-07-11 11:11:42 +01002013
2014 if (target)
2015 {
2016 target->time = now;
2017 return target;
2018 }
Simon Kelley16972692006-10-16 20:04:18 +01002019
2020 /* can't find empty one, use oldest if there is one
2021 and it's older than timeout */
2022 if (oldest && ((int)difftime(now, oldest->time)) >= TIMEOUT)
2023 {
2024 /* keep stuff for twice timeout if we can by allocating a new
2025 record instead */
2026 if (difftime(now, oldest->time) < 2*TIMEOUT &&
2027 count <= daemon->ftabsize &&
2028 (f = allocate_frec(now)))
2029 return f;
2030
2031 if (!wait)
2032 {
Simon Kelley1a6bca82008-07-11 11:11:42 +01002033 free_frec(oldest);
Simon Kelley16972692006-10-16 20:04:18 +01002034 oldest->time = now;
2035 }
2036 return oldest;
2037 }
2038
2039 /* none available, calculate time 'till oldest record expires */
Simon Kelley3a237152013-12-12 12:15:50 +00002040 if (!force && count > daemon->ftabsize)
Simon Kelley16972692006-10-16 20:04:18 +01002041 {
Marcelo Salhab Brogliato0da5e892013-05-31 11:49:06 +01002042 static time_t last_log = 0;
2043
Simon Kelley16972692006-10-16 20:04:18 +01002044 if (oldest && wait)
2045 *wait = oldest->time + (time_t)TIMEOUT - now;
Marcelo Salhab Brogliato0da5e892013-05-31 11:49:06 +01002046
2047 if ((int)difftime(now, last_log) > 5)
2048 {
2049 last_log = now;
2050 my_syslog(LOG_WARNING, _("Maximum number of concurrent DNS queries reached (max: %d)"), daemon->ftabsize);
2051 }
2052
Simon Kelley16972692006-10-16 20:04:18 +01002053 return NULL;
2054 }
2055
2056 if (!(f = allocate_frec(now)) && wait)
2057 /* wait one second on malloc failure */
2058 *wait = 1;
2059
Simon Kelley9e4abcb2004-01-22 19:47:41 +00002060 return f; /* OK if malloc fails and this is NULL */
2061}
2062
Simon Kelley832af0b2007-01-21 20:01:28 +00002063/* crc is all-ones if not known. */
Simon Kelley8a9be9e2014-01-25 23:17:21 +00002064static struct frec *lookup_frec(unsigned short id, void *hash)
Simon Kelley9e4abcb2004-01-22 19:47:41 +00002065{
2066 struct frec *f;
2067
Simon Kelley1a6bca82008-07-11 11:11:42 +01002068 for(f = daemon->frec_list; f; f = f->next)
Simon Kelley832af0b2007-01-21 20:01:28 +00002069 if (f->sentto && f->new_id == id &&
Simon Kelley8a9be9e2014-01-25 23:17:21 +00002070 (!hash || memcmp(hash, f->hash, HASH_SIZE) == 0))
Simon Kelley9e4abcb2004-01-22 19:47:41 +00002071 return f;
2072
2073 return NULL;
2074}
2075
2076static struct frec *lookup_frec_by_sender(unsigned short id,
Simon Kelleyfd9fa482004-10-21 20:24:00 +01002077 union mysockaddr *addr,
Simon Kelley8a9be9e2014-01-25 23:17:21 +00002078 void *hash)
Simon Kelley9e4abcb2004-01-22 19:47:41 +00002079{
Simon Kelleyfeba5c12004-07-27 20:28:58 +01002080 struct frec *f;
2081
Simon Kelley1a6bca82008-07-11 11:11:42 +01002082 for(f = daemon->frec_list; f; f = f->next)
Simon Kelley832af0b2007-01-21 20:01:28 +00002083 if (f->sentto &&
Simon Kelley9e4abcb2004-01-22 19:47:41 +00002084 f->orig_id == id &&
Simon Kelley8a9be9e2014-01-25 23:17:21 +00002085 memcmp(hash, f->hash, HASH_SIZE) == 0 &&
Simon Kelley9e4abcb2004-01-22 19:47:41 +00002086 sockaddr_isequal(&f->source, addr))
2087 return f;
2088
2089 return NULL;
2090}
Simon Kelley47a95162014-07-08 22:22:02 +01002091
2092/* Send query packet again, if we can. */
2093void resend_query()
2094{
2095 if (daemon->srv_save)
2096 {
2097 int fd;
2098
2099 if (daemon->srv_save->sfd)
2100 fd = daemon->srv_save->sfd->fd;
2101 else if (daemon->rfd_save && daemon->rfd_save->refcount != 0)
2102 fd = daemon->rfd_save->fd;
2103 else
2104 return;
2105
Simon Kelleyff841eb2015-03-11 21:36:30 +00002106 while(retry_send(sendto(fd, daemon->packet, daemon->packet_len, 0,
2107 &daemon->srv_save->addr.sa,
2108 sa_len(&daemon->srv_save->addr))));
Simon Kelley47a95162014-07-08 22:22:02 +01002109 }
2110}
Simon Kelley9e4abcb2004-01-22 19:47:41 +00002111
Simon Kelley849a8352006-06-09 21:02:31 +01002112/* A server record is going away, remove references to it */
Simon Kelley5aabfc72007-08-29 11:24:47 +01002113void server_gone(struct server *server)
Simon Kelley849a8352006-06-09 21:02:31 +01002114{
2115 struct frec *f;
2116
Simon Kelley1a6bca82008-07-11 11:11:42 +01002117 for (f = daemon->frec_list; f; f = f->next)
Simon Kelley832af0b2007-01-21 20:01:28 +00002118 if (f->sentto && f->sentto == server)
Simon Kelley1a6bca82008-07-11 11:11:42 +01002119 free_frec(f);
Simon Kelley849a8352006-06-09 21:02:31 +01002120
2121 if (daemon->last_server == server)
2122 daemon->last_server = NULL;
2123
2124 if (daemon->srv_save == server)
2125 daemon->srv_save = NULL;
2126}
Simon Kelley9e4abcb2004-01-22 19:47:41 +00002127
Simon Kelley316e2732010-01-22 20:16:09 +00002128/* return unique random ids. */
Simon Kelley8a9be9e2014-01-25 23:17:21 +00002129static unsigned short get_id(void)
Simon Kelley9e4abcb2004-01-22 19:47:41 +00002130{
2131 unsigned short ret = 0;
Simon Kelley832af0b2007-01-21 20:01:28 +00002132
Simon Kelley316e2732010-01-22 20:16:09 +00002133 do
Simon Kelley832af0b2007-01-21 20:01:28 +00002134 ret = rand16();
Simon Kelley8a9be9e2014-01-25 23:17:21 +00002135 while (lookup_frec(ret, NULL));
Simon Kelley832af0b2007-01-21 20:01:28 +00002136
Simon Kelley9e4abcb2004-01-22 19:47:41 +00002137 return ret;
2138}
2139
2140
2141
2142
2143