blob: 8c3e71cebe87f667a4b3952537459a10fa28f6df [file] [log] [blame]
Simon Kelleyaff33962015-01-31 20:13:40 +00001/* dnsmasq is Copyright (c) 2000-2015 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 Kelley00a5b5d2014-02-28 18:10:55 +000026#ifdef HAVE_DNSSEC
27static int tcp_key_recurse(time_t now, int status, struct dns_header *header, size_t n,
28 int class, char *name, char *keyname, struct server *server, int *keycount);
Simon Kelley97e618a2015-01-07 21:55:43 +000029static int do_check_sign(struct frec *forward, int status, time_t now, char *name, char *keyname);
30static int send_check_sign(struct frec *forward, time_t now, struct dns_header *header, size_t plen,
31 char *name, char *keyname);
Simon Kelley00a5b5d2014-02-28 18:10:55 +000032#endif
33
34
Simon Kelley824af852008-02-12 20:43:05 +000035/* Send a UDP packet with its source address set as "source"
Simon Kelley44a2a312004-03-10 20:04:35 +000036 unless nowild is true, when we just send it with the kernel default */
Simon Kelley29689cf2012-03-22 14:01:00 +000037int send_from(int fd, int nowild, char *packet, size_t len,
38 union mysockaddr *to, struct all_addr *source,
Simon Kelley50303b12012-04-04 22:13:17 +010039 unsigned int iface)
Simon Kelley9e4abcb2004-01-22 19:47:41 +000040{
Simon Kelley44a2a312004-03-10 20:04:35 +000041 struct msghdr msg;
42 struct iovec iov[1];
Simon Kelley44a2a312004-03-10 20:04:35 +000043 union {
44 struct cmsghdr align; /* this ensures alignment */
Simon Kelley5e9e0ef2006-04-17 14:24:29 +010045#if defined(HAVE_LINUX_NETWORK)
Simon Kelley44a2a312004-03-10 20:04:35 +000046 char control[CMSG_SPACE(sizeof(struct in_pktinfo))];
47#elif defined(IP_SENDSRCADDR)
48 char control[CMSG_SPACE(sizeof(struct in_addr))];
49#endif
50#ifdef HAVE_IPV6
51 char control6[CMSG_SPACE(sizeof(struct in6_pktinfo))];
52#endif
53 } control_u;
Simon Kelleyfeba5c12004-07-27 20:28:58 +010054
Simon Kelley44a2a312004-03-10 20:04:35 +000055 iov[0].iov_base = packet;
56 iov[0].iov_len = len;
57
Simon Kelleyfeba5c12004-07-27 20:28:58 +010058 msg.msg_control = NULL;
59 msg.msg_controllen = 0;
Simon Kelley44a2a312004-03-10 20:04:35 +000060 msg.msg_flags = 0;
61 msg.msg_name = to;
62 msg.msg_namelen = sa_len(to);
63 msg.msg_iov = iov;
64 msg.msg_iovlen = 1;
Simon Kelleyfeba5c12004-07-27 20:28:58 +010065
Simon Kelley26128d22004-11-14 16:43:54 +000066 if (!nowild)
Simon Kelleyfeba5c12004-07-27 20:28:58 +010067 {
Simon Kelley26128d22004-11-14 16:43:54 +000068 struct cmsghdr *cmptr;
Simon Kelleyfeba5c12004-07-27 20:28:58 +010069 msg.msg_control = &control_u;
70 msg.msg_controllen = sizeof(control_u);
Simon Kelley26128d22004-11-14 16:43:54 +000071 cmptr = CMSG_FIRSTHDR(&msg);
Simon Kelley44a2a312004-03-10 20:04:35 +000072
Simon Kelley26128d22004-11-14 16:43:54 +000073 if (to->sa.sa_family == AF_INET)
74 {
Simon Kelley5e9e0ef2006-04-17 14:24:29 +010075#if defined(HAVE_LINUX_NETWORK)
Simon Kelley8ef5ada2010-06-03 19:42:45 +010076 struct in_pktinfo p;
77 p.ipi_ifindex = 0;
78 p.ipi_spec_dst = source->addr.addr4;
79 memcpy(CMSG_DATA(cmptr), &p, sizeof(p));
Simon Kelley26128d22004-11-14 16:43:54 +000080 msg.msg_controllen = cmptr->cmsg_len = CMSG_LEN(sizeof(struct in_pktinfo));
Simon Kelleyc72daea2012-01-05 21:33:27 +000081 cmptr->cmsg_level = IPPROTO_IP;
Simon Kelley26128d22004-11-14 16:43:54 +000082 cmptr->cmsg_type = IP_PKTINFO;
83#elif defined(IP_SENDSRCADDR)
Simon Kelley8ef5ada2010-06-03 19:42:45 +010084 memcpy(CMSG_DATA(cmptr), &(source->addr.addr4), sizeof(source->addr.addr4));
Simon Kelley26128d22004-11-14 16:43:54 +000085 msg.msg_controllen = cmptr->cmsg_len = CMSG_LEN(sizeof(struct in_addr));
86 cmptr->cmsg_level = IPPROTO_IP;
87 cmptr->cmsg_type = IP_SENDSRCADDR;
Simon Kelley44a2a312004-03-10 20:04:35 +000088#endif
Simon Kelley26128d22004-11-14 16:43:54 +000089 }
Simon Kelley26128d22004-11-14 16:43:54 +000090 else
Simon Kelleyb8187c82005-11-26 21:46:27 +000091#ifdef HAVE_IPV6
Simon Kelley26128d22004-11-14 16:43:54 +000092 {
Simon Kelley8ef5ada2010-06-03 19:42:45 +010093 struct in6_pktinfo p;
94 p.ipi6_ifindex = iface; /* Need iface for IPv6 to handle link-local addrs */
95 p.ipi6_addr = source->addr.addr6;
96 memcpy(CMSG_DATA(cmptr), &p, sizeof(p));
Simon Kelley26128d22004-11-14 16:43:54 +000097 msg.msg_controllen = cmptr->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
Simon Kelley316e2732010-01-22 20:16:09 +000098 cmptr->cmsg_type = daemon->v6pktinfo;
Simon Kelleyc72daea2012-01-05 21:33:27 +000099 cmptr->cmsg_level = IPPROTO_IPV6;
Simon Kelley26128d22004-11-14 16:43:54 +0000100 }
Simon Kelley3d8df262005-08-29 12:19:27 +0100101#else
Simon Kelleyc72daea2012-01-05 21:33:27 +0000102 (void)iface; /* eliminate warning */
Simon Kelley26128d22004-11-14 16:43:54 +0000103#endif
104 }
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100105
Simon Kelleyff841eb2015-03-11 21:36:30 +0000106 while (retry_send(sendmsg(fd, &msg, 0)));
107
108 /* If interface is still in DAD, EINVAL results - ignore that. */
109 if (errno != 0 && errno != EINVAL)
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100110 {
Simon Kelley50303b12012-04-04 22:13:17 +0100111 my_syslog(LOG_ERR, _("failed to send packet: %s"), strerror(errno));
Simon Kelley29689cf2012-03-22 14:01:00 +0000112 return 0;
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100113 }
Simon Kelley29d28dd2012-12-03 14:05:59 +0000114
Simon Kelley29689cf2012-03-22 14:01:00 +0000115 return 1;
Simon Kelley44a2a312004-03-10 20:04:35 +0000116}
117
Simon Kelley28866e92011-02-14 20:19:14 +0000118static unsigned int search_servers(time_t now, struct all_addr **addrpp,
119 unsigned int qtype, char *qdomain, int *type, char **domain, int *norebind)
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100120
121{
122 /* If the query ends in the domain in one of our servers, set
123 domain to point to that name. We find the largest match to allow both
124 domain.org and sub.domain.org to exist. */
125
126 unsigned int namelen = strlen(qdomain);
127 unsigned int matchlen = 0;
128 struct server *serv;
Simon Kelley28866e92011-02-14 20:19:14 +0000129 unsigned int flags = 0;
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100130
Simon Kelley3be34542004-09-11 19:12:13 +0100131 for (serv = daemon->servers; serv; serv=serv->next)
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100132 /* domain matches take priority over NODOTS matches */
Simon Kelley3d8df262005-08-29 12:19:27 +0100133 if ((serv->flags & SERV_FOR_NODOTS) && *type != SERV_HAS_DOMAIN && !strchr(qdomain, '.') && namelen != 0)
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100134 {
Simon Kelley28866e92011-02-14 20:19:14 +0000135 unsigned int sflag = serv->addr.sa.sa_family == AF_INET ? F_IPV4 : F_IPV6;
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100136 *type = SERV_FOR_NODOTS;
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100137 if (serv->flags & SERV_NO_ADDR)
Simon Kelley36717ee2004-09-20 19:20:58 +0100138 flags = F_NXDOMAIN;
139 else if (serv->flags & SERV_LITERAL_ADDRESS)
140 {
141 if (sflag & qtype)
142 {
143 flags = sflag;
144 if (serv->addr.sa.sa_family == AF_INET)
145 *addrpp = (struct all_addr *)&serv->addr.in.sin_addr;
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100146#ifdef HAVE_IPV6
Simon Kelley36717ee2004-09-20 19:20:58 +0100147 else
148 *addrpp = (struct all_addr *)&serv->addr.in6.sin6_addr;
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100149#endif
Simon Kelley36717ee2004-09-20 19:20:58 +0100150 }
Simon Kelley824af852008-02-12 20:43:05 +0000151 else if (!flags || (flags & F_NXDOMAIN))
Simon Kelley36717ee2004-09-20 19:20:58 +0100152 flags = F_NOERR;
153 }
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100154 }
155 else if (serv->flags & SERV_HAS_DOMAIN)
156 {
157 unsigned int domainlen = strlen(serv->domain);
Simon Kelleyb8187c82005-11-26 21:46:27 +0000158 char *matchstart = qdomain + namelen - domainlen;
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100159 if (namelen >= domainlen &&
Simon Kelleyb8187c82005-11-26 21:46:27 +0000160 hostname_isequal(matchstart, serv->domain) &&
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100161 (domainlen == 0 || namelen == domainlen || *(matchstart-1) == '.' ))
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100162 {
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100163 if (serv->flags & SERV_NO_REBIND)
164 *norebind = 1;
Simon Kelley28866e92011-02-14 20:19:14 +0000165 else
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100166 {
Simon Kelley28866e92011-02-14 20:19:14 +0000167 unsigned int sflag = serv->addr.sa.sa_family == AF_INET ? F_IPV4 : F_IPV6;
168 /* implement priority rules for --address and --server for same domain.
169 --address wins if the address is for the correct AF
170 --server wins otherwise. */
171 if (domainlen != 0 && domainlen == matchlen)
Simon Kelley36717ee2004-09-20 19:20:58 +0100172 {
Simon Kelley28866e92011-02-14 20:19:14 +0000173 if ((serv->flags & SERV_LITERAL_ADDRESS))
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100174 {
Simon Kelley28866e92011-02-14 20:19:14 +0000175 if (!(sflag & qtype) && flags == 0)
176 continue;
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100177 }
Simon Kelley28866e92011-02-14 20:19:14 +0000178 else
179 {
180 if (flags & (F_IPV4 | F_IPV6))
181 continue;
182 }
Simon Kelley36717ee2004-09-20 19:20:58 +0100183 }
Simon Kelley28866e92011-02-14 20:19:14 +0000184
185 if (domainlen >= matchlen)
186 {
187 *type = serv->flags & (SERV_HAS_DOMAIN | SERV_USE_RESOLV | SERV_NO_REBIND);
188 *domain = serv->domain;
189 matchlen = domainlen;
190 if (serv->flags & SERV_NO_ADDR)
191 flags = F_NXDOMAIN;
192 else if (serv->flags & SERV_LITERAL_ADDRESS)
193 {
194 if (sflag & qtype)
195 {
196 flags = sflag;
197 if (serv->addr.sa.sa_family == AF_INET)
198 *addrpp = (struct all_addr *)&serv->addr.in.sin_addr;
199#ifdef HAVE_IPV6
200 else
201 *addrpp = (struct all_addr *)&serv->addr.in6.sin6_addr;
202#endif
203 }
204 else if (!flags || (flags & F_NXDOMAIN))
205 flags = F_NOERR;
206 }
207 else
208 flags = 0;
209 }
210 }
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100211 }
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100212 }
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100213
Simon Kelley7de060b2011-08-26 17:24:52 +0100214 if (flags == 0 && !(qtype & F_QUERY) &&
Simon Kelley28866e92011-02-14 20:19:14 +0000215 option_bool(OPT_NODOTS_LOCAL) && !strchr(qdomain, '.') && namelen != 0)
Simon Kelley7de060b2011-08-26 17:24:52 +0100216 /* don't forward A or AAAA queries for simple names, except the empty name */
217 flags = F_NOERR;
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100218
Simon Kelley5aabfc72007-08-29 11:24:47 +0100219 if (flags == F_NXDOMAIN && check_for_local_domain(qdomain, now))
Simon Kelleyc1bb8502004-08-11 18:40:17 +0100220 flags = F_NOERR;
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100221
Simon Kelley824af852008-02-12 20:43:05 +0000222 if (flags)
223 {
224 int logflags = 0;
225
226 if (flags == F_NXDOMAIN || flags == F_NOERR)
227 logflags = F_NEG | qtype;
228
Simon Kelley1a6bca82008-07-11 11:11:42 +0100229 log_query(logflags | flags | F_CONFIG | F_FORWARD, qdomain, *addrpp, NULL);
Simon Kelley824af852008-02-12 20:43:05 +0000230 }
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100231 else if ((*type) & SERV_USE_RESOLV)
232 {
233 *type = 0; /* use normal servers for this domain */
234 *domain = NULL;
235 }
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100236 return flags;
237}
Simon Kelley44a2a312004-03-10 20:04:35 +0000238
Simon Kelley824af852008-02-12 20:43:05 +0000239static int forward_query(int udpfd, union mysockaddr *udpaddr,
240 struct all_addr *dst_addr, unsigned int dst_iface,
Simon Kelley83349b82014-02-10 21:02:01 +0000241 struct dns_header *header, size_t plen, time_t now,
Simon Kelley613ad152014-02-25 23:02:28 +0000242 struct frec *forward, int ad_reqd, int do_bit)
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000243{
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000244 char *domain = NULL;
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100245 int type = 0, norebind = 0;
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000246 struct all_addr *addrp = NULL;
Simon Kelley28866e92011-02-14 20:19:14 +0000247 unsigned int flags = 0;
Simon Kelleyde379512004-06-22 20:23:33 +0100248 struct server *start = NULL;
Simon Kelley8a9be9e2014-01-25 23:17:21 +0000249#ifdef HAVE_DNSSEC
250 void *hash = hash_questions(header, plen, daemon->namebuff);
251#else
252 unsigned int crc = questions_crc(header, plen, daemon->namebuff);
253 void *hash = &crc;
254#endif
255 unsigned int gotname = extract_request(header, plen, daemon->namebuff, NULL);
Simon Kelleya77cec82015-05-08 16:25:38 +0100256 unsigned char *pheader;
Simon Kelley8a9be9e2014-01-25 23:17:21 +0000257
Simon Kelley00a5b5d2014-02-28 18:10:55 +0000258 (void)do_bit;
259
Simon Kelley3d8df262005-08-29 12:19:27 +0100260 /* may be no servers available. */
261 if (!daemon->servers)
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000262 forward = NULL;
Simon Kelley8a9be9e2014-01-25 23:17:21 +0000263 else if (forward || (hash && (forward = lookup_frec_by_sender(ntohs(header->id), udpaddr, hash))))
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000264 {
Simon Kelleya77cec82015-05-08 16:25:38 +0100265 /* If we didn't get an answer advertising a maximal packet in EDNS,
266 fall back to 1280, which should work everywhere on IPv6.
267 If that generates an answer, it will become the new default
268 for this server */
269 forward->flags |= FREC_TEST_PKTSZ;
270
Simon Kelleye0c0ad32014-01-16 22:42:07 +0000271#ifdef HAVE_DNSSEC
Simon Kelleydac74312014-02-13 16:43:49 +0000272 /* If we've already got an answer to this query, but we're awaiting keys for validation,
Simon Kelleye0c0ad32014-01-16 22:42:07 +0000273 there's no point retrying the query, retry the key query instead...... */
274 if (forward->blocking_query)
275 {
276 int fd;
Simon Kelleya77cec82015-05-08 16:25:38 +0100277
278 forward->flags &= ~FREC_TEST_PKTSZ;
279
Simon Kelleye0c0ad32014-01-16 22:42:07 +0000280 while (forward->blocking_query)
281 forward = forward->blocking_query;
Simon Kelleya77cec82015-05-08 16:25:38 +0100282
283 forward->flags |= FREC_TEST_PKTSZ;
Simon Kelleye0c0ad32014-01-16 22:42:07 +0000284
285 blockdata_retrieve(forward->stash, forward->stash_len, (void *)header);
286 plen = forward->stash_len;
287
Simon Kelleya77cec82015-05-08 16:25:38 +0100288 if (find_pseudoheader(header, plen, NULL, &pheader, NULL))
289 PUTSHORT((forward->flags & FREC_TEST_PKTSZ) ? SAFE_PKTSZ : forward->sentto->edns_pktsz, pheader);
290
Simon Kelley2b291912014-03-21 11:13:55 +0000291 if (forward->sentto->addr.sa.sa_family == AF_INET)
Simon Kelley25cf5e32015-01-09 15:53:03 +0000292 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 +0000293#ifdef HAVE_IPV6
294 else
Simon Kelley25cf5e32015-01-09 15:53:03 +0000295 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 +0000296#endif
297
298 if (forward->sentto->sfd)
299 fd = forward->sentto->sfd->fd;
300 else
301 {
302#ifdef HAVE_IPV6
303 if (forward->sentto->addr.sa.sa_family == AF_INET6)
304 fd = forward->rfd6->fd;
305 else
306#endif
307 fd = forward->rfd4->fd;
308 }
309
Simon Kelleyff841eb2015-03-11 21:36:30 +0000310 while (retry_send( sendto(fd, (char *)header, plen, 0,
311 &forward->sentto->addr.sa,
312 sa_len(&forward->sentto->addr))));
Simon Kelleye0c0ad32014-01-16 22:42:07 +0000313
314 return 1;
315 }
316#endif
317
Simon Kelleyde379512004-06-22 20:23:33 +0100318 /* retry on existing query, send to all available servers */
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000319 domain = forward->sentto->domain;
Simon Kelley824af852008-02-12 20:43:05 +0000320 forward->sentto->failed_queries++;
Simon Kelley28866e92011-02-14 20:19:14 +0000321 if (!option_bool(OPT_ORDER))
Simon Kelleyde379512004-06-22 20:23:33 +0100322 {
Simon Kelley0a852542005-03-23 20:28:59 +0000323 forward->forwardall = 1;
Simon Kelley3be34542004-09-11 19:12:13 +0100324 daemon->last_server = NULL;
Simon Kelleyde379512004-06-22 20:23:33 +0100325 }
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000326 type = forward->sentto->flags & SERV_TYPE;
Simon Kelleyde379512004-06-22 20:23:33 +0100327 if (!(start = forward->sentto->next))
Simon Kelley3be34542004-09-11 19:12:13 +0100328 start = daemon->servers; /* at end of list, recycle */
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000329 header->id = htons(forward->new_id);
330 }
331 else
332 {
333 if (gotname)
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100334 flags = search_servers(now, &addrp, gotname, daemon->namebuff, &type, &domain, &norebind);
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000335
Simon Kelley3a237152013-12-12 12:15:50 +0000336 if (!flags && !(forward = get_new_frec(now, NULL, 0)))
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100337 /* table full - server failure. */
338 flags = F_NEG;
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000339
340 if (forward)
341 {
Simon Kelley0a852542005-03-23 20:28:59 +0000342 forward->source = *udpaddr;
343 forward->dest = *dst_addr;
344 forward->iface = dst_iface;
Simon Kelley0a852542005-03-23 20:28:59 +0000345 forward->orig_id = ntohs(header->id);
Simon Kelley8a9be9e2014-01-25 23:17:21 +0000346 forward->new_id = get_id();
Simon Kelley832af0b2007-01-21 20:01:28 +0000347 forward->fd = udpfd;
Simon Kelley8a9be9e2014-01-25 23:17:21 +0000348 memcpy(forward->hash, hash, HASH_SIZE);
Simon Kelley0a852542005-03-23 20:28:59 +0000349 forward->forwardall = 0;
Simon Kelleyed4c0762013-10-08 20:46:34 +0100350 forward->flags = 0;
Simon Kelley28866e92011-02-14 20:19:14 +0000351 if (norebind)
352 forward->flags |= FREC_NOREBIND;
Simon Kelley572b41e2011-02-18 18:11:18 +0000353 if (header->hb4 & HB4_CD)
Simon Kelley28866e92011-02-14 20:19:14 +0000354 forward->flags |= FREC_CHECKING_DISABLED;
Simon Kelley83349b82014-02-10 21:02:01 +0000355 if (ad_reqd)
356 forward->flags |= FREC_AD_QUESTION;
Simon Kelley7fa836e2014-02-10 20:11:24 +0000357#ifdef HAVE_DNSSEC
358 forward->work_counter = DNSSEC_WORK;
Simon Kelley613ad152014-02-25 23:02:28 +0000359 if (do_bit)
360 forward->flags |= FREC_DO_QUESTION;
Simon Kelley7fa836e2014-02-10 20:11:24 +0000361#endif
Simon Kelley613ad152014-02-25 23:02:28 +0000362
Simon Kelley28866e92011-02-14 20:19:14 +0000363 header->id = htons(forward->new_id);
364
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100365 /* In strict_order mode, always try servers in the order
366 specified in resolv.conf, if a domain is given
367 always try all the available servers,
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000368 otherwise, use the one last known to work. */
369
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100370 if (type == 0)
371 {
Simon Kelley28866e92011-02-14 20:19:14 +0000372 if (option_bool(OPT_ORDER))
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100373 start = daemon->servers;
374 else if (!(start = daemon->last_server) ||
375 daemon->forwardcount++ > FORWARD_TEST ||
376 difftime(now, daemon->forwardtime) > FORWARD_TIME)
377 {
378 start = daemon->servers;
379 forward->forwardall = 1;
380 daemon->forwardcount = 0;
381 daemon->forwardtime = now;
382 }
383 }
384 else
Simon Kelleyde379512004-06-22 20:23:33 +0100385 {
Simon Kelley3be34542004-09-11 19:12:13 +0100386 start = daemon->servers;
Simon Kelley28866e92011-02-14 20:19:14 +0000387 if (!option_bool(OPT_ORDER))
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100388 forward->forwardall = 1;
Simon Kelleyde379512004-06-22 20:23:33 +0100389 }
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000390 }
391 }
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100392
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000393 /* check for send errors here (no route to host)
394 if we fail to send to all nameservers, send back an error
395 packet straight away (helps modem users when offline) */
396
397 if (!flags && forward)
398 {
Simon Kelleyde379512004-06-22 20:23:33 +0100399 struct server *firstsentto = start;
400 int forwarded = 0;
Simon Kelley28866e92011-02-14 20:19:14 +0000401
Simon Kelley25cf5e32015-01-09 15:53:03 +0000402 /* If a query is retried, use the log_id for the retry when logging the answer. */
403 forward->log_id = daemon->log_id;
404
Giacomo Tazzari797a7af2013-04-22 13:16:37 +0100405 if (option_bool(OPT_ADD_MAC))
Simon Kelley60b68062014-01-08 12:10:28 +0000406 plen = add_mac(header, plen, ((char *) header) + daemon->packet_buff_sz, &forward->source);
Simon Kelley28866e92011-02-14 20:19:14 +0000407
Simon Kelleyed4c0762013-10-08 20:46:34 +0100408 if (option_bool(OPT_CLIENT_SUBNET))
409 {
Simon Kelley60b68062014-01-08 12:10:28 +0000410 size_t new = add_source_addr(header, plen, ((char *) header) + daemon->packet_buff_sz, &forward->source);
Simon Kelleyed4c0762013-10-08 20:46:34 +0100411 if (new != plen)
412 {
413 plen = new;
414 forward->flags |= FREC_HAS_SUBNET;
415 }
416 }
417
Simon Kelley3a237152013-12-12 12:15:50 +0000418#ifdef HAVE_DNSSEC
419 if (option_bool(OPT_DNSSEC_VALID))
Simon Kelley0fc2f312014-01-08 10:26:58 +0000420 {
Simon Kelley613ad152014-02-25 23:02:28 +0000421 size_t new_plen = add_do_bit(header, plen, ((char *) header) + daemon->packet_buff_sz);
422
Simon Kelley5b3bf922014-01-25 17:03:07 +0000423 /* For debugging, set Checking Disabled, otherwise, have the upstream check too,
424 this allows it to select auth servers when one is returning bad data. */
425 if (option_bool(OPT_DNSSEC_DEBUG))
426 header->hb4 |= HB4_CD;
Simon Kelley613ad152014-02-25 23:02:28 +0000427
428 if (new_plen != plen)
429 forward->flags |= FREC_ADDED_PHEADER;
430
431 plen = new_plen;
Simon Kelley0fc2f312014-01-08 10:26:58 +0000432 }
Simon Kelley3a237152013-12-12 12:15:50 +0000433#endif
Simon Kelleya77cec82015-05-08 16:25:38 +0100434
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000435 while (1)
436 {
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000437 /* only send to servers dealing with our domain.
438 domain may be NULL, in which case server->domain
439 must be NULL also. */
440
Simon Kelleyde379512004-06-22 20:23:33 +0100441 if (type == (start->flags & SERV_TYPE) &&
Simon Kelleyfd9fa482004-10-21 20:24:00 +0100442 (type != SERV_HAS_DOMAIN || hostname_isequal(domain, start->domain)) &&
Simon Kelleyb5ea1cc2014-07-29 16:34:14 +0100443 !(start->flags & (SERV_LITERAL_ADDRESS | SERV_LOOP)))
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000444 {
Simon Kelley1a6bca82008-07-11 11:11:42 +0100445 int fd;
446
447 /* find server socket to use, may need to get random one. */
448 if (start->sfd)
449 fd = start->sfd->fd;
450 else
451 {
452#ifdef HAVE_IPV6
453 if (start->addr.sa.sa_family == AF_INET6)
454 {
455 if (!forward->rfd6 &&
456 !(forward->rfd6 = allocate_rfd(AF_INET6)))
457 break;
Simon Kelley3927da42008-07-20 15:10:39 +0100458 daemon->rfd_save = forward->rfd6;
Simon Kelley1a6bca82008-07-11 11:11:42 +0100459 fd = forward->rfd6->fd;
460 }
461 else
462#endif
463 {
464 if (!forward->rfd4 &&
465 !(forward->rfd4 = allocate_rfd(AF_INET)))
466 break;
Simon Kelley3927da42008-07-20 15:10:39 +0100467 daemon->rfd_save = forward->rfd4;
Simon Kelley1a6bca82008-07-11 11:11:42 +0100468 fd = forward->rfd4->fd;
469 }
Simon Kelley7de060b2011-08-26 17:24:52 +0100470
471#ifdef HAVE_CONNTRACK
472 /* Copy connection mark of incoming query to outgoing connection. */
473 if (option_bool(OPT_CONNTRACK))
474 {
475 unsigned int mark;
Giacomo Tazzari797a7af2013-04-22 13:16:37 +0100476 if (get_incoming_mark(&forward->source, &forward->dest, 0, &mark))
Simon Kelley7de060b2011-08-26 17:24:52 +0100477 setsockopt(fd, SOL_SOCKET, SO_MARK, &mark, sizeof(unsigned int));
478 }
479#endif
Simon Kelley1a6bca82008-07-11 11:11:42 +0100480 }
Simon Kelleya77cec82015-05-08 16:25:38 +0100481
482 if (find_pseudoheader(header, plen, NULL, &pheader, NULL))
483 PUTSHORT((forward->flags & FREC_TEST_PKTSZ) ? SAFE_PKTSZ : start->edns_pktsz, pheader);
Simon Kelley1a6bca82008-07-11 11:11:42 +0100484
Simon Kelleyff841eb2015-03-11 21:36:30 +0000485 if (retry_send(sendto(fd, (char *)header, plen, 0,
486 &start->addr.sa,
487 sa_len(&start->addr))))
488 continue;
489
490 if (errno == 0)
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000491 {
Simon Kelleycdeda282006-03-16 20:16:06 +0000492 /* Keep info in case we want to re-send this packet */
493 daemon->srv_save = start;
494 daemon->packet_len = plen;
495
Simon Kelleyde379512004-06-22 20:23:33 +0100496 if (!gotname)
Simon Kelley3be34542004-09-11 19:12:13 +0100497 strcpy(daemon->namebuff, "query");
Simon Kelleyde379512004-06-22 20:23:33 +0100498 if (start->addr.sa.sa_family == AF_INET)
Simon Kelley3be34542004-09-11 19:12:13 +0100499 log_query(F_SERVER | F_IPV4 | F_FORWARD, daemon->namebuff,
Simon Kelley1a6bca82008-07-11 11:11:42 +0100500 (struct all_addr *)&start->addr.in.sin_addr, NULL);
Simon Kelleyde379512004-06-22 20:23:33 +0100501#ifdef HAVE_IPV6
502 else
Simon Kelley3be34542004-09-11 19:12:13 +0100503 log_query(F_SERVER | F_IPV6 | F_FORWARD, daemon->namebuff,
Simon Kelley1a6bca82008-07-11 11:11:42 +0100504 (struct all_addr *)&start->addr.in6.sin6_addr, NULL);
Simon Kelleyde379512004-06-22 20:23:33 +0100505#endif
Simon Kelley824af852008-02-12 20:43:05 +0000506 start->queries++;
Simon Kelleyde379512004-06-22 20:23:33 +0100507 forwarded = 1;
508 forward->sentto = start;
Simon Kelley0a852542005-03-23 20:28:59 +0000509 if (!forward->forwardall)
Simon Kelleyde379512004-06-22 20:23:33 +0100510 break;
Simon Kelley0a852542005-03-23 20:28:59 +0000511 forward->forwardall++;
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000512 }
513 }
514
Simon Kelleyde379512004-06-22 20:23:33 +0100515 if (!(start = start->next))
Simon Kelley3be34542004-09-11 19:12:13 +0100516 start = daemon->servers;
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000517
Simon Kelleyde379512004-06-22 20:23:33 +0100518 if (start == firstsentto)
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000519 break;
520 }
521
Simon Kelleyde379512004-06-22 20:23:33 +0100522 if (forwarded)
Simon Kelley824af852008-02-12 20:43:05 +0000523 return 1;
Simon Kelleyde379512004-06-22 20:23:33 +0100524
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000525 /* could not send on, prepare to return */
526 header->id = htons(forward->orig_id);
Simon Kelley1a6bca82008-07-11 11:11:42 +0100527 free_frec(forward); /* cancel */
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000528 }
529
530 /* could not send on, return empty answer or address if known for whole domain */
Simon Kelleyb8187c82005-11-26 21:46:27 +0000531 if (udpfd != -1)
532 {
Simon Kelleycdeda282006-03-16 20:16:06 +0000533 plen = setup_reply(header, plen, addrp, flags, daemon->local_ttl);
Simon Kelley54dd3932012-06-20 11:23:38 +0100534 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 +0000535 }
536
Simon Kelley824af852008-02-12 20:43:05 +0000537 return 0;
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000538}
539
Simon Kelleyed4c0762013-10-08 20:46:34 +0100540static 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 +0100541 int no_cache, int cache_secure, int bogusanswer, int ad_reqd, int do_bit, int added_pheader,
542 int check_subnet, union mysockaddr *query_source)
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100543{
Simon Kelley36717ee2004-09-20 19:20:58 +0100544 unsigned char *pheader, *sizep;
Jason A. Donenfeld13d86c72013-02-22 18:20:53 +0000545 char **sets = 0;
Simon Kelley832af0b2007-01-21 20:01:28 +0000546 int munged = 0, is_sign;
Simon Kelleycdeda282006-03-16 20:16:06 +0000547 size_t plen;
548
Simon Kelley83349b82014-02-10 21:02:01 +0000549 (void)ad_reqd;
Simon Kelley982faf42015-04-03 21:42:30 +0100550 (void)do_bit;
551 (void)bogusanswer;
Simon Kelley83349b82014-02-10 21:02:01 +0000552
Jason A. Donenfeld13d86c72013-02-22 18:20:53 +0000553#ifdef HAVE_IPSET
Simon Kelley82a14af2014-04-13 20:48:57 +0100554 if (daemon->ipsets && extract_request(header, n, daemon->namebuff, NULL))
Jason A. Donenfeld13d86c72013-02-22 18:20:53 +0000555 {
Simon Kelley82a14af2014-04-13 20:48:57 +0100556 /* Similar algorithm to search_servers. */
557 struct ipsets *ipset_pos;
558 unsigned int namelen = strlen(daemon->namebuff);
559 unsigned int matchlen = 0;
560 for (ipset_pos = daemon->ipsets; ipset_pos; ipset_pos = ipset_pos->next)
Simon Kelley6c0cb852014-01-17 14:40:46 +0000561 {
Simon Kelley82a14af2014-04-13 20:48:57 +0100562 unsigned int domainlen = strlen(ipset_pos->domain);
563 char *matchstart = daemon->namebuff + namelen - domainlen;
564 if (namelen >= domainlen && hostname_isequal(matchstart, ipset_pos->domain) &&
565 (domainlen == 0 || namelen == domainlen || *(matchstart - 1) == '.' ) &&
566 domainlen >= matchlen)
567 {
568 matchlen = domainlen;
569 sets = ipset_pos->sets;
570 }
Simon Kelley6c0cb852014-01-17 14:40:46 +0000571 }
Jason A. Donenfeld13d86c72013-02-22 18:20:53 +0000572 }
573#endif
574
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100575 /* If upstream is advertising a larger UDP packet size
Simon Kelley9009d742008-11-14 20:04:27 +0000576 than we allow, trim it so that we don't get overlarge
577 requests for the client. We can't do this for signed packets. */
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100578
Simon Kelleyed4c0762013-10-08 20:46:34 +0100579 if ((pheader = find_pseudoheader(header, n, &plen, &sizep, &is_sign)))
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100580 {
Simon Kelley83349b82014-02-10 21:02:01 +0000581 unsigned short udpsz;
582 unsigned char *psave = sizep;
583
584 GETSHORT(udpsz, sizep);
585
586 if (!is_sign && udpsz > daemon->edns_pktsz)
587 PUTSHORT(daemon->edns_pktsz, psave);
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100588
Simon Kelleyed4c0762013-10-08 20:46:34 +0100589 if (check_subnet && !check_source(header, plen, pheader, query_source))
590 {
591 my_syslog(LOG_WARNING, _("discarding DNS reply: subnet option mismatch"));
592 return 0;
593 }
Simon Kelley613ad152014-02-25 23:02:28 +0000594
595 if (added_pheader)
596 {
597 pheader = 0;
598 header->arcount = htons(0);
599 }
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100600 }
Simon Kelley83349b82014-02-10 21:02:01 +0000601
Simon Kelley28866e92011-02-14 20:19:14 +0000602 /* RFC 4035 sect 4.6 para 3 */
Giovanni Bajo237724c2012-04-05 02:46:52 +0200603 if (!is_sign && !option_bool(OPT_DNSSEC_PROXY))
Simon Kelley795501b2014-01-08 18:11:55 +0000604 header->hb4 &= ~HB4_AD;
Simon Kelley3a237152013-12-12 12:15:50 +0000605
Simon Kelley572b41e2011-02-18 18:11:18 +0000606 if (OPCODE(header) != QUERY || (RCODE(header) != NOERROR && RCODE(header) != NXDOMAIN))
Simon Kelley8938ae02014-05-01 17:46:25 +0100607 return resize_packet(header, n, pheader, plen);
Simon Kelley36717ee2004-09-20 19:20:58 +0100608
Simon Kelley0a852542005-03-23 20:28:59 +0000609 /* Complain loudly if the upstream server is non-recursive. */
Simon Kelley572b41e2011-02-18 18:11:18 +0000610 if (!(header->hb4 & HB4_RA) && RCODE(header) == NOERROR && ntohs(header->ancount) == 0 &&
Simon Kelley0a852542005-03-23 20:28:59 +0000611 server && !(server->flags & SERV_WARNED_RECURSIVE))
612 {
Simon Kelley3d8df262005-08-29 12:19:27 +0100613 prettyprint_addr(&server->addr, daemon->namebuff);
Simon Kelleyf2621c72007-04-29 19:47:21 +0100614 my_syslog(LOG_WARNING, _("nameserver %s refused to do a recursive query"), daemon->namebuff);
Simon Kelley28866e92011-02-14 20:19:14 +0000615 if (!option_bool(OPT_LOG))
Simon Kelley0a852542005-03-23 20:28:59 +0000616 server->flags |= SERV_WARNED_RECURSIVE;
617 }
Giovanni Bajoe292e932012-04-22 14:32:02 +0200618
Simon Kelley572b41e2011-02-18 18:11:18 +0000619 if (daemon->bogus_addr && RCODE(header) != NXDOMAIN &&
Simon Kelleyfd9fa482004-10-21 20:24:00 +0100620 check_for_bogus_wildcard(header, n, daemon->namebuff, daemon->bogus_addr, now))
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100621 {
Simon Kelleyfd9fa482004-10-21 20:24:00 +0100622 munged = 1;
Simon Kelley572b41e2011-02-18 18:11:18 +0000623 SET_RCODE(header, NXDOMAIN);
624 header->hb3 &= ~HB3_AA;
Simon Kelley6938f342014-01-26 22:47:39 +0000625 cache_secure = 0;
Simon Kelley36717ee2004-09-20 19:20:58 +0100626 }
Simon Kelleyfd9fa482004-10-21 20:24:00 +0100627 else
Simon Kelley36717ee2004-09-20 19:20:58 +0100628 {
Simon Kelley6938f342014-01-26 22:47:39 +0000629 int doctored = 0;
630
Simon Kelley572b41e2011-02-18 18:11:18 +0000631 if (RCODE(header) == NXDOMAIN &&
Simon Kelleyfd9fa482004-10-21 20:24:00 +0100632 extract_request(header, n, daemon->namebuff, NULL) &&
Simon Kelley5aabfc72007-08-29 11:24:47 +0100633 check_for_local_domain(daemon->namebuff, now))
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100634 {
Simon Kelley36717ee2004-09-20 19:20:58 +0100635 /* if we forwarded a query for a locally known name (because it was for
636 an unknown type) and the answer is NXDOMAIN, convert that to NODATA,
637 since we know that the domain exists, even if upstream doesn't */
Simon Kelleyfd9fa482004-10-21 20:24:00 +0100638 munged = 1;
Simon Kelley572b41e2011-02-18 18:11:18 +0000639 header->hb3 |= HB3_AA;
640 SET_RCODE(header, NOERROR);
Simon Kelley6938f342014-01-26 22:47:39 +0000641 cache_secure = 0;
Simon Kelley36717ee2004-09-20 19:20:58 +0100642 }
Simon Kelley832af0b2007-01-21 20:01:28 +0000643
Simon Kelley6938f342014-01-26 22:47:39 +0000644 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 +0000645 {
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100646 my_syslog(LOG_WARNING, _("possible DNS-rebind attack detected: %s"), daemon->namebuff);
Simon Kelley824af852008-02-12 20:43:05 +0000647 munged = 1;
Simon Kelley6938f342014-01-26 22:47:39 +0000648 cache_secure = 0;
Simon Kelley824af852008-02-12 20:43:05 +0000649 }
Simon Kelley6938f342014-01-26 22:47:39 +0000650
651 if (doctored)
652 cache_secure = 0;
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100653 }
Simon Kelleyfd9fa482004-10-21 20:24:00 +0100654
Simon Kelleya25720a2014-01-14 23:13:55 +0000655#ifdef HAVE_DNSSEC
Simon Kelleyfe3992f2015-04-03 21:25:05 +0100656 if (bogusanswer && !(header->hb4 & HB4_CD))
Simon Kelleya25720a2014-01-14 23:13:55 +0000657 {
Simon Kelley7d23a662014-01-26 09:33:21 +0000658 if (!option_bool(OPT_DNSSEC_DEBUG))
Simon Kelleya25720a2014-01-14 23:13:55 +0000659 {
660 /* Bogus reply, turn into SERVFAIL */
661 SET_RCODE(header, SERVFAIL);
662 munged = 1;
663 }
664 }
Simon Kelley6938f342014-01-26 22:47:39 +0000665
666 if (option_bool(OPT_DNSSEC_VALID))
667 header->hb4 &= ~HB4_AD;
668
Simon Kelley83349b82014-02-10 21:02:01 +0000669 if (!(header->hb4 & HB4_CD) && ad_reqd && cache_secure)
Simon Kelley6938f342014-01-26 22:47:39 +0000670 header->hb4 |= HB4_AD;
Simon Kelley613ad152014-02-25 23:02:28 +0000671
672 /* If the requestor didn't set the DO bit, don't return DNSSEC info. */
673 if (!do_bit)
674 n = filter_rrsigs(header, n);
Simon Kelleya25720a2014-01-14 23:13:55 +0000675#endif
676
Simon Kelleyfd9fa482004-10-21 20:24:00 +0100677 /* do this after extract_addresses. Ensure NODATA reply and remove
678 nameserver info. */
679
680 if (munged)
681 {
682 header->ancount = htons(0);
683 header->nscount = htons(0);
684 header->arcount = htons(0);
Simon Kelley150162b2015-03-27 09:58:26 +0000685 header->hb3 &= ~HB3_TC;
Simon Kelleyfd9fa482004-10-21 20:24:00 +0100686 }
687
Simon Kelley36717ee2004-09-20 19:20:58 +0100688 /* the bogus-nxdomain stuff, doctor and NXDOMAIN->NODATA munging can all elide
689 sections of the packet. Find the new length here and put back pseudoheader
690 if it was removed. */
691 return resize_packet(header, n, pheader, plen);
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100692}
693
Simon Kelley3be34542004-09-11 19:12:13 +0100694/* sets new last_server */
Simon Kelley1a6bca82008-07-11 11:11:42 +0100695void reply_query(int fd, int family, time_t now)
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000696{
697 /* packet from peer server, extract data for cache, and send to
698 original requester */
Simon Kelley572b41e2011-02-18 18:11:18 +0000699 struct dns_header *header;
Simon Kelleyde379512004-06-22 20:23:33 +0100700 union mysockaddr serveraddr;
Simon Kelley832af0b2007-01-21 20:01:28 +0000701 struct frec *forward;
Simon Kelleyde379512004-06-22 20:23:33 +0100702 socklen_t addrlen = sizeof(serveraddr);
Simon Kelley60b68062014-01-08 12:10:28 +0000703 ssize_t n = recvfrom(fd, daemon->packet, daemon->packet_buff_sz, 0, &serveraddr.sa, &addrlen);
Simon Kelleycdeda282006-03-16 20:16:06 +0000704 size_t nn;
Simon Kelley1a6bca82008-07-11 11:11:42 +0100705 struct server *server;
Simon Kelley8a9be9e2014-01-25 23:17:21 +0000706 void *hash;
707#ifndef HAVE_DNSSEC
708 unsigned int crc;
709#endif
710
Simon Kelleycdeda282006-03-16 20:16:06 +0000711 /* packet buffer overwritten */
712 daemon->srv_save = NULL;
Simon Kelley832af0b2007-01-21 20:01:28 +0000713
Simon Kelleyde379512004-06-22 20:23:33 +0100714 /* Determine the address of the server replying so that we can mark that as good */
Simon Kelley1a6bca82008-07-11 11:11:42 +0100715 serveraddr.sa.sa_family = family;
Simon Kelleyde379512004-06-22 20:23:33 +0100716#ifdef HAVE_IPV6
717 if (serveraddr.sa.sa_family == AF_INET6)
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100718 serveraddr.in6.sin6_flowinfo = 0;
Simon Kelleyde379512004-06-22 20:23:33 +0100719#endif
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000720
Simon Kelley490f9072014-03-24 22:04:42 +0000721 header = (struct dns_header *)daemon->packet;
722
723 if (n < (int)sizeof(struct dns_header) || !(header->hb3 & HB3_QR))
724 return;
725
Simon Kelley1a6bca82008-07-11 11:11:42 +0100726 /* spoof check: answer must come from known server, */
727 for (server = daemon->servers; server; server = server->next)
728 if (!(server->flags & (SERV_LITERAL_ADDRESS | SERV_NO_ADDR)) &&
729 sockaddr_isequal(&server->addr, &serveraddr))
730 break;
Simon Kelley490f9072014-03-24 22:04:42 +0000731
732 if (!server)
733 return;
734
Simon Kelley8a9be9e2014-01-25 23:17:21 +0000735#ifdef HAVE_DNSSEC
736 hash = hash_questions(header, n, daemon->namebuff);
737#else
738 hash = &crc;
739 crc = questions_crc(header, n, daemon->namebuff);
740#endif
Simon Kelleyfd9fa482004-10-21 20:24:00 +0100741
Simon Kelley490f9072014-03-24 22:04:42 +0000742 if (!(forward = lookup_frec(ntohs(header->id), hash)))
Simon Kelley1a6bca82008-07-11 11:11:42 +0100743 return;
Simon Kelley490f9072014-03-24 22:04:42 +0000744
Simon Kelley25cf5e32015-01-09 15:53:03 +0000745 /* log_query gets called indirectly all over the place, so
746 pass these in global variables - sorry. */
747 daemon->log_display_id = forward->log_id;
748 daemon->log_source_addr = &forward->source;
749
Glen Huang32fc6db2014-12-27 15:28:12 +0000750 if (daemon->ignore_addr && RCODE(header) == NOERROR &&
751 check_for_ignored_address(header, n, daemon->ignore_addr))
752 return;
753
Simon Kelley2ae195f2015-01-18 22:20:48 +0000754 if (RCODE(header) == REFUSED &&
Simon Kelley28866e92011-02-14 20:19:14 +0000755 !option_bool(OPT_ORDER) &&
Simon Kelley1a6bca82008-07-11 11:11:42 +0100756 forward->forwardall == 0)
757 /* for broken servers, attempt to send to another one. */
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000758 {
Simon Kelley1a6bca82008-07-11 11:11:42 +0100759 unsigned char *pheader;
760 size_t plen;
761 int is_sign;
Simon Kelley832af0b2007-01-21 20:01:28 +0000762
Simon Kelley1a6bca82008-07-11 11:11:42 +0100763 /* recreate query from reply */
764 pheader = find_pseudoheader(header, (size_t)n, &plen, NULL, &is_sign);
765 if (!is_sign)
Simon Kelley832af0b2007-01-21 20:01:28 +0000766 {
Simon Kelley1a6bca82008-07-11 11:11:42 +0100767 header->ancount = htons(0);
768 header->nscount = htons(0);
769 header->arcount = htons(0);
770 if ((nn = resize_packet(header, (size_t)n, pheader, plen)))
771 {
swiggerbd7bfa22015-06-01 20:54:59 +0100772 header->hb3 &= ~(HB3_QR | HB3_AA | HB3_TC);
773 header->hb4 &= ~(HB4_RA | HB4_RCODE);
Simon Kelley613ad152014-02-25 23:02:28 +0000774 forward_query(-1, NULL, NULL, 0, header, nn, now, forward, 0, 0);
Simon Kelley1a6bca82008-07-11 11:11:42 +0100775 return;
776 }
777 }
778 }
Simon Kelley3a237152013-12-12 12:15:50 +0000779
780 server = forward->sentto;
Simon Kelley1a6bca82008-07-11 11:11:42 +0100781 if ((forward->sentto->flags & SERV_TYPE) == 0)
782 {
Simon Kelley51967f92014-03-25 21:07:00 +0000783 if (RCODE(header) == REFUSED)
Simon Kelley1a6bca82008-07-11 11:11:42 +0100784 server = NULL;
785 else
786 {
787 struct server *last_server;
Simon Kelley832af0b2007-01-21 20:01:28 +0000788
Simon Kelley1a6bca82008-07-11 11:11:42 +0100789 /* find good server by address if possible, otherwise assume the last one we sent to */
790 for (last_server = daemon->servers; last_server; last_server = last_server->next)
791 if (!(last_server->flags & (SERV_LITERAL_ADDRESS | SERV_HAS_DOMAIN | SERV_FOR_NODOTS | SERV_NO_ADDR)) &&
792 sockaddr_isequal(&last_server->addr, &serveraddr))
793 {
794 server = last_server;
795 break;
796 }
797 }
Simon Kelley28866e92011-02-14 20:19:14 +0000798 if (!option_bool(OPT_ALL_SERVERS))
Simon Kelley1a6bca82008-07-11 11:11:42 +0100799 daemon->last_server = server;
800 }
Simon Kelleya77cec82015-05-08 16:25:38 +0100801
802 /* We tried resending to this server with a smaller maximum size and got an answer.
Simon Kelley86fa1042015-05-10 13:50:59 +0100803 Make that permanent. To avoid reduxing the packet size for an single dropped packet,
804 only do this when we get a truncated answer, or one larger than the safe size. */
805 if (server && (forward->flags & FREC_TEST_PKTSZ) &&
806 ((header->hb3 & HB3_TC) || n >= SAFE_PKTSZ))
Simon Kelleya77cec82015-05-08 16:25:38 +0100807 server->edns_pktsz = SAFE_PKTSZ;
808
Simon Kelley1a6bca82008-07-11 11:11:42 +0100809 /* If the answer is an error, keep the forward record in place in case
810 we get a good reply from another server. Kill it when we've
811 had replies from all to avoid filling the forwarding table when
812 everything is broken */
Simon Kelley51967f92014-03-25 21:07:00 +0000813 if (forward->forwardall == 0 || --forward->forwardall == 1 || RCODE(header) != SERVFAIL)
Simon Kelley1a6bca82008-07-11 11:11:42 +0100814 {
Simon Kelleyfe3992f2015-04-03 21:25:05 +0100815 int check_rebind = 0, no_cache_dnssec = 0, cache_secure = 0, bogusanswer = 0;
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100816
Simon Kelley3a237152013-12-12 12:15:50 +0000817 if (option_bool(OPT_NO_REBIND))
818 check_rebind = !(forward->flags & FREC_NOREBIND);
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100819
Simon Kelley3a237152013-12-12 12:15:50 +0000820 /* Don't cache replies where DNSSEC validation was turned off, either
821 the upstream server told us so, or the original query specified it. */
822 if ((header->hb4 & HB4_CD) || (forward->flags & FREC_CHECKING_DISABLED))
823 no_cache_dnssec = 1;
824
825#ifdef HAVE_DNSSEC
Simon Kelley51967f92014-03-25 21:07:00 +0000826 if (server && option_bool(OPT_DNSSEC_VALID) && !(forward->flags & FREC_CHECKING_DISABLED))
Simon Kelley3a237152013-12-12 12:15:50 +0000827 {
Simon Kelley9d633042013-12-13 15:36:55 +0000828 int status;
Simon Kelley0fc2f312014-01-08 10:26:58 +0000829
830 /* We've had a reply already, which we're validating. Ignore this duplicate */
Simon Kelleye0c0ad32014-01-16 22:42:07 +0000831 if (forward->blocking_query)
Simon Kelley0fc2f312014-01-08 10:26:58 +0000832 return;
Simon Kelley9d633042013-12-13 15:36:55 +0000833
Simon Kelley871417d2014-01-08 11:22:32 +0000834 if (header->hb3 & HB3_TC)
835 {
836 /* Truncated answer can't be validated.
Simon Kelley5d3b87a2014-01-20 11:57:23 +0000837 If this is an answer to a DNSSEC-generated query, we still
838 need to get the client to retry over TCP, so return
839 an answer with the TC bit set, even if the actual answer fits.
840 */
841 status = STAT_TRUNCATED;
Simon Kelley871417d2014-01-08 11:22:32 +0000842 }
843 else if (forward->flags & FREC_DNSKEY_QUERY)
Simon Kelley8d718cb2014-02-03 16:27:37 +0000844 status = dnssec_validate_by_ds(now, header, n, daemon->namebuff, daemon->keyname, forward->class);
Simon Kelleyc3e0b9b2013-12-31 13:50:39 +0000845 else if (forward->flags & FREC_DS_QUERY)
Simon Kelley00a5b5d2014-02-28 18:10:55 +0000846 {
847 status = dnssec_validate_ds(now, header, n, daemon->namebuff, daemon->keyname, forward->class);
Simon Kelleyfe3992f2015-04-03 21:25:05 +0100848 /* Provably no DS, everything below is insecure, even if signatures are offered */
849 if (status == STAT_NO_DS)
850 /* We only cache sigs when we've validated a reply.
851 Avoid caching a reply with sigs if there's a vaildated break in the
852 DS chain, so we don't return replies from cache missing sigs. */
853 status = STAT_INSECURE_DS;
854 else if (status == STAT_NO_NS)
Simon Kelley97e618a2015-01-07 21:55:43 +0000855 status = STAT_BOGUS;
Simon Kelley00a5b5d2014-02-28 18:10:55 +0000856 }
857 else if (forward->flags & FREC_CHECK_NOSIGN)
Simon Kelley97e618a2015-01-07 21:55:43 +0000858 {
859 status = dnssec_validate_ds(now, header, n, daemon->namebuff, daemon->keyname, forward->class);
860 if (status != STAT_NEED_KEY)
861 status = do_check_sign(forward, status, now, daemon->namebuff, daemon->keyname);
862 }
Simon Kelley9d633042013-12-13 15:36:55 +0000863 else
Simon Kelley00a5b5d2014-02-28 18:10:55 +0000864 {
Simon Kelley97e618a2015-01-07 21:55:43 +0000865 status = dnssec_validate_reply(now, header, n, daemon->namebuff, daemon->keyname, &forward->class, NULL, NULL);
Simon Kelley00a5b5d2014-02-28 18:10:55 +0000866 if (status == STAT_NO_SIG)
867 {
868 if (option_bool(OPT_DNSSEC_NO_SIGN))
Simon Kelley97e618a2015-01-07 21:55:43 +0000869 status = send_check_sign(forward, now, header, n, daemon->namebuff, daemon->keyname);
Simon Kelley00a5b5d2014-02-28 18:10:55 +0000870 else
871 status = STAT_INSECURE;
872 }
873 }
Simon Kelley3a237152013-12-12 12:15:50 +0000874 /* Can't validate, as we're missing key data. Put this
875 answer aside, whilst we get that. */
Simon Kelley00a5b5d2014-02-28 18:10:55 +0000876 if (status == STAT_NEED_DS || status == STAT_NEED_DS_NEG || status == STAT_NEED_KEY)
Simon Kelley3a237152013-12-12 12:15:50 +0000877 {
Simon Kelley7fa836e2014-02-10 20:11:24 +0000878 struct frec *new, *orig;
Simon Kelley0fc2f312014-01-08 10:26:58 +0000879
Simon Kelley7fa836e2014-02-10 20:11:24 +0000880 /* Free any saved query */
881 if (forward->stash)
882 blockdata_free(forward->stash);
883
884 /* Now save reply pending receipt of key data */
885 if (!(forward->stash = blockdata_alloc((char *)header, n)))
886 return;
887 forward->stash_len = n;
888
889 anotherkey:
890 /* Find the original query that started it all.... */
891 for (orig = forward; orig->dependent; orig = orig->dependent);
892
893 if (--orig->work_counter == 0 || !(new = get_new_frec(now, NULL, 1)))
894 status = STAT_INSECURE;
895 else
Simon Kelley3a237152013-12-12 12:15:50 +0000896 {
Simon Kelley7fa836e2014-02-10 20:11:24 +0000897 int fd;
Simon Kelley0fc2f312014-01-08 10:26:58 +0000898 struct frec *next = new->next;
899 *new = *forward; /* copy everything, then overwrite */
900 new->next = next;
Simon Kelley0fc2f312014-01-08 10:26:58 +0000901 new->blocking_query = NULL;
Simon Kelley8a8bbad2014-03-27 22:02:17 +0000902 new->sentto = server;
Simon Kelleyf1668d22014-01-08 16:53:27 +0000903 new->rfd4 = NULL;
Simon Kelley97e618a2015-01-07 21:55:43 +0000904 new->orig_domain = NULL;
Simon Kelleyf1668d22014-01-08 16:53:27 +0000905#ifdef HAVE_IPV6
906 new->rfd6 = NULL;
907#endif
Simon Kelley00a5b5d2014-02-28 18:10:55 +0000908 new->flags &= ~(FREC_DNSKEY_QUERY | FREC_DS_QUERY | FREC_CHECK_NOSIGN);
Simon Kelley9d633042013-12-13 15:36:55 +0000909
Simon Kelley7fa836e2014-02-10 20:11:24 +0000910 new->dependent = forward; /* to find query awaiting new one. */
911 forward->blocking_query = new; /* for garbage cleaning */
912 /* validate routines leave name of required record in daemon->keyname */
913 if (status == STAT_NEED_KEY)
914 {
915 new->flags |= FREC_DNSKEY_QUERY;
916 nn = dnssec_generate_query(header, ((char *) header) + daemon->packet_buff_sz,
Simon Kelleya77cec82015-05-08 16:25:38 +0100917 daemon->keyname, forward->class, T_DNSKEY, &server->addr, server->edns_pktsz);
Simon Kelley7fa836e2014-02-10 20:11:24 +0000918 }
919 else
920 {
Simon Kelley00a5b5d2014-02-28 18:10:55 +0000921 if (status == STAT_NEED_DS_NEG)
922 new->flags |= FREC_CHECK_NOSIGN;
923 else
924 new->flags |= FREC_DS_QUERY;
Simon Kelley7fa836e2014-02-10 20:11:24 +0000925 nn = dnssec_generate_query(header,((char *) header) + daemon->packet_buff_sz,
Simon Kelleya77cec82015-05-08 16:25:38 +0100926 daemon->keyname, forward->class, T_DS, &server->addr, server->edns_pktsz);
Simon Kelley7fa836e2014-02-10 20:11:24 +0000927 }
928 if ((hash = hash_questions(header, nn, daemon->namebuff)))
929 memcpy(new->hash, hash, HASH_SIZE);
930 new->new_id = get_id();
931 header->id = htons(new->new_id);
932 /* Save query for retransmission */
Simon Kelley97e618a2015-01-07 21:55:43 +0000933 if (!(new->stash = blockdata_alloc((char *)header, nn)))
934 return;
935
Simon Kelley7fa836e2014-02-10 20:11:24 +0000936 new->stash_len = nn;
Simon Kelleye0c0ad32014-01-16 22:42:07 +0000937
Simon Kelley7fa836e2014-02-10 20:11:24 +0000938 /* Don't resend this. */
939 daemon->srv_save = NULL;
940
941 if (server->sfd)
942 fd = server->sfd->fd;
Simon Kelleye0c0ad32014-01-16 22:42:07 +0000943 else
Simon Kelley3a237152013-12-12 12:15:50 +0000944 {
Simon Kelley7fa836e2014-02-10 20:11:24 +0000945 fd = -1;
Simon Kelley3a237152013-12-12 12:15:50 +0000946#ifdef HAVE_IPV6
Simon Kelley7fa836e2014-02-10 20:11:24 +0000947 if (server->addr.sa.sa_family == AF_INET6)
Simon Kelleyf1668d22014-01-08 16:53:27 +0000948 {
Simon Kelley7fa836e2014-02-10 20:11:24 +0000949 if (new->rfd6 || (new->rfd6 = allocate_rfd(AF_INET6)))
950 fd = new->rfd6->fd;
951 }
952 else
953#endif
954 {
955 if (new->rfd4 || (new->rfd4 = allocate_rfd(AF_INET)))
956 fd = new->rfd4->fd;
Simon Kelleyf1668d22014-01-08 16:53:27 +0000957 }
Simon Kelley3a237152013-12-12 12:15:50 +0000958 }
Simon Kelley7fa836e2014-02-10 20:11:24 +0000959
960 if (fd != -1)
961 {
Simon Kelleyff841eb2015-03-11 21:36:30 +0000962 while (retry_send(sendto(fd, (char *)header, nn, 0,
963 &server->addr.sa,
964 sa_len(&server->addr))));
Simon Kelley7fa836e2014-02-10 20:11:24 +0000965 server->queries++;
966 }
967
968 return;
Simon Kelley3a237152013-12-12 12:15:50 +0000969 }
Simon Kelley3a237152013-12-12 12:15:50 +0000970 }
971
972 /* Ok, we reached far enough up the chain-of-trust that we can validate something.
973 Now wind back down, pulling back answers which wouldn't previously validate
Simon Kelley7fa836e2014-02-10 20:11:24 +0000974 and validate them with the new data. Note that if an answer needs multiple
975 keys to validate, we may find another key is needed, in which case we set off
976 down another branch of the tree. Once we get to the original answer
977 (FREC_DNSSEC_QUERY not set) and it validates, return it to the original requestor. */
Simon Kelley0744ca62014-01-25 16:40:15 +0000978 while (forward->dependent)
Simon Kelley3a237152013-12-12 12:15:50 +0000979 {
Simon Kelley0744ca62014-01-25 16:40:15 +0000980 struct frec *prev = forward->dependent;
981 free_frec(forward);
982 forward = prev;
983 forward->blocking_query = NULL; /* already gone */
984 blockdata_retrieve(forward->stash, forward->stash_len, (void *)header);
985 n = forward->stash_len;
Simon Kelley0fc2f312014-01-08 10:26:58 +0000986
Simon Kelley0fc2f312014-01-08 10:26:58 +0000987 if (status == STAT_SECURE)
Simon Kelley0fc2f312014-01-08 10:26:58 +0000988 {
Simon Kelley0744ca62014-01-25 16:40:15 +0000989 if (forward->flags & FREC_DNSKEY_QUERY)
990 status = dnssec_validate_by_ds(now, header, n, daemon->namebuff, daemon->keyname, forward->class);
991 else if (forward->flags & FREC_DS_QUERY)
Simon Kelley00a5b5d2014-02-28 18:10:55 +0000992 {
993 status = dnssec_validate_ds(now, header, n, daemon->namebuff, daemon->keyname, forward->class);
Simon Kelleyfe3992f2015-04-03 21:25:05 +0100994 /* Provably no DS, everything below is insecure, even if signatures are offered */
995 if (status == STAT_NO_DS)
996 /* We only cache sigs when we've validated a reply.
997 Avoid caching a reply with sigs if there's a vaildated break in the
998 DS chain, so we don't return replies from cache missing sigs. */
999 status = STAT_INSECURE_DS;
1000 else if (status == STAT_NO_NS)
1001 status = STAT_BOGUS;
Simon Kelley00a5b5d2014-02-28 18:10:55 +00001002 }
1003 else if (forward->flags & FREC_CHECK_NOSIGN)
Simon Kelley97e618a2015-01-07 21:55:43 +00001004 {
1005 status = dnssec_validate_ds(now, header, n, daemon->namebuff, daemon->keyname, forward->class);
1006 if (status != STAT_NEED_KEY)
1007 status = do_check_sign(forward, status, now, daemon->namebuff, daemon->keyname);
1008 }
Simon Kelley0744ca62014-01-25 16:40:15 +00001009 else
Simon Kelley00a5b5d2014-02-28 18:10:55 +00001010 {
Simon Kelley97e618a2015-01-07 21:55:43 +00001011 status = dnssec_validate_reply(now, header, n, daemon->namebuff, daemon->keyname, &forward->class, NULL, NULL);
Simon Kelley00a5b5d2014-02-28 18:10:55 +00001012 if (status == STAT_NO_SIG)
1013 {
1014 if (option_bool(OPT_DNSSEC_NO_SIGN))
Simon Kelley97e618a2015-01-07 21:55:43 +00001015 status = send_check_sign(forward, now, header, n, daemon->namebuff, daemon->keyname);
Simon Kelley00a5b5d2014-02-28 18:10:55 +00001016 else
1017 status = STAT_INSECURE;
1018 }
1019 }
1020
1021 if (status == STAT_NEED_DS || status == STAT_NEED_DS_NEG || status == STAT_NEED_KEY)
Simon Kelley7fa836e2014-02-10 20:11:24 +00001022 goto anotherkey;
Simon Kelley3a237152013-12-12 12:15:50 +00001023 }
1024 }
Simon Kelley5d3b87a2014-01-20 11:57:23 +00001025
Simon Kelleyfe3992f2015-04-03 21:25:05 +01001026 no_cache_dnssec = 0;
1027
1028 if (status == STAT_INSECURE_DS)
1029 {
1030 /* We only cache sigs when we've validated a reply.
1031 Avoid caching a reply with sigs if there's a vaildated break in the
1032 DS chain, so we don't return replies from cache missing sigs. */
1033 status = STAT_INSECURE;
1034 no_cache_dnssec = 1;
1035 }
1036
Simon Kelley5d3b87a2014-01-20 11:57:23 +00001037 if (status == STAT_TRUNCATED)
Simon Kelley0744ca62014-01-25 16:40:15 +00001038 header->hb3 |= HB3_TC;
Simon Kelley5d3b87a2014-01-20 11:57:23 +00001039 else
Simon Kelley7fa836e2014-02-10 20:11:24 +00001040 {
Simon Kelley554b5802015-04-17 22:50:20 +01001041 char *result, *domain = "result";
Simon Kelley7fa836e2014-02-10 20:11:24 +00001042
1043 if (forward->work_counter == 0)
Simon Kelley150162b2015-03-27 09:58:26 +00001044 {
1045 result = "ABANDONED";
1046 status = STAT_BOGUS;
1047 }
Simon Kelley7fa836e2014-02-10 20:11:24 +00001048 else
1049 result = (status == STAT_SECURE ? "SECURE" : (status == STAT_INSECURE ? "INSECURE" : "BOGUS"));
1050
Simon Kelley554b5802015-04-17 22:50:20 +01001051 if (status == STAT_BOGUS && extract_request(header, n, daemon->namebuff, NULL))
1052 domain = daemon->namebuff;
1053
1054 log_query(F_KEYTAG | F_SECSTAT, domain, NULL, result);
Simon Kelley7fa836e2014-02-10 20:11:24 +00001055 }
Simon Kelley5d3b87a2014-01-20 11:57:23 +00001056
Simon Kelley3a237152013-12-12 12:15:50 +00001057 if (status == STAT_SECURE)
1058 cache_secure = 1;
Simon Kelley3a237152013-12-12 12:15:50 +00001059 else if (status == STAT_BOGUS)
Simon Kelleyfe3992f2015-04-03 21:25:05 +01001060 {
1061 no_cache_dnssec = 1;
1062 bogusanswer = 1;
1063 }
Simon Kelley3a237152013-12-12 12:15:50 +00001064 }
Simon Kelley83349b82014-02-10 21:02:01 +00001065#endif
1066
1067 /* restore CD bit to the value in the query */
1068 if (forward->flags & FREC_CHECKING_DISABLED)
1069 header->hb4 |= HB4_CD;
1070 else
1071 header->hb4 &= ~HB4_CD;
Simon Kelley3a237152013-12-12 12:15:50 +00001072
Simon Kelleyfe3992f2015-04-03 21:25:05 +01001073 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 +00001074 forward->flags & FREC_AD_QUESTION, forward->flags & FREC_DO_QUESTION,
1075 forward->flags & FREC_ADDED_PHEADER, forward->flags & FREC_HAS_SUBNET, &forward->source)))
Simon Kelley832af0b2007-01-21 20:01:28 +00001076 {
Simon Kelley1a6bca82008-07-11 11:11:42 +01001077 header->id = htons(forward->orig_id);
Simon Kelley572b41e2011-02-18 18:11:18 +00001078 header->hb4 |= HB4_RA; /* recursion if available */
Simon Kelley54dd3932012-06-20 11:23:38 +01001079 send_from(forward->fd, option_bool(OPT_NOWILD) || option_bool (OPT_CLEVERBIND), daemon->packet, nn,
Simon Kelley50303b12012-04-04 22:13:17 +01001080 &forward->source, &forward->dest, forward->iface);
Simon Kelley832af0b2007-01-21 20:01:28 +00001081 }
Simon Kelley1a6bca82008-07-11 11:11:42 +01001082 free_frec(forward); /* cancel */
Simon Kelley9e4abcb2004-01-22 19:47:41 +00001083 }
Simon Kelley9e4abcb2004-01-22 19:47:41 +00001084}
Simon Kelley44a2a312004-03-10 20:04:35 +00001085
Simon Kelley1a6bca82008-07-11 11:11:42 +01001086
Simon Kelley5aabfc72007-08-29 11:24:47 +01001087void receive_query(struct listener *listen, time_t now)
Simon Kelley44a2a312004-03-10 20:04:35 +00001088{
Simon Kelley572b41e2011-02-18 18:11:18 +00001089 struct dns_header *header = (struct dns_header *)daemon->packet;
Simon Kelley44a2a312004-03-10 20:04:35 +00001090 union mysockaddr source_addr;
Simon Kelleyc1bb8502004-08-11 18:40:17 +01001091 unsigned short type;
Simon Kelley44a2a312004-03-10 20:04:35 +00001092 struct all_addr dst_addr;
Simon Kelleyf6b7dc42005-01-23 12:06:08 +00001093 struct in_addr netmask, dst_addr_4;
Simon Kelleycdeda282006-03-16 20:16:06 +00001094 size_t m;
1095 ssize_t n;
Vladislav Grishenko3b195962013-11-26 11:08:21 +00001096 int if_index = 0, auth_dns = 0;
1097#ifdef HAVE_AUTH
1098 int local_auth = 0;
1099#endif
Simon Kelley44a2a312004-03-10 20:04:35 +00001100 struct iovec iov[1];
1101 struct msghdr msg;
1102 struct cmsghdr *cmptr;
Simon Kelley44a2a312004-03-10 20:04:35 +00001103 union {
1104 struct cmsghdr align; /* this ensures alignment */
1105#ifdef HAVE_IPV6
1106 char control6[CMSG_SPACE(sizeof(struct in6_pktinfo))];
1107#endif
Simon Kelley5e9e0ef2006-04-17 14:24:29 +01001108#if defined(HAVE_LINUX_NETWORK)
Simon Kelley44a2a312004-03-10 20:04:35 +00001109 char control[CMSG_SPACE(sizeof(struct in_pktinfo))];
Simon Kelley824af852008-02-12 20:43:05 +00001110#elif defined(IP_RECVDSTADDR) && defined(HAVE_SOLARIS_NETWORK)
1111 char control[CMSG_SPACE(sizeof(struct in_addr)) +
1112 CMSG_SPACE(sizeof(unsigned int))];
Simon Kelley44a2a312004-03-10 20:04:35 +00001113#elif defined(IP_RECVDSTADDR)
1114 char control[CMSG_SPACE(sizeof(struct in_addr)) +
1115 CMSG_SPACE(sizeof(struct sockaddr_dl))];
1116#endif
1117 } control_u;
Simon Kelley2329bef2013-12-03 13:41:16 +00001118#ifdef HAVE_IPV6
1119 /* Can always get recvd interface for IPv6 */
1120 int check_dst = !option_bool(OPT_NOWILD) || listen->family == AF_INET6;
1121#else
1122 int check_dst = !option_bool(OPT_NOWILD);
1123#endif
1124
Simon Kelleycdeda282006-03-16 20:16:06 +00001125 /* packet buffer overwritten */
1126 daemon->srv_save = NULL;
1127
Hans Dedecker98906272014-12-09 22:22:53 +00001128 dst_addr_4.s_addr = dst_addr.addr.addr4.s_addr = 0;
Simon Kelley4f7b3042012-11-28 21:27:02 +00001129 netmask.s_addr = 0;
1130
Simon Kelley7e5664b2013-04-05 16:57:41 +01001131 if (option_bool(OPT_NOWILD) && listen->iface)
Simon Kelleyf6b7dc42005-01-23 12:06:08 +00001132 {
Simon Kelley4f7b3042012-11-28 21:27:02 +00001133 auth_dns = listen->iface->dns_auth;
1134
1135 if (listen->family == AF_INET)
1136 {
Hans Dedecker98906272014-12-09 22:22:53 +00001137 dst_addr_4 = dst_addr.addr.addr4 = listen->iface->addr.in.sin_addr;
Simon Kelley4f7b3042012-11-28 21:27:02 +00001138 netmask = listen->iface->netmask;
1139 }
Simon Kelleyf6b7dc42005-01-23 12:06:08 +00001140 }
Simon Kelley4f7b3042012-11-28 21:27:02 +00001141
Simon Kelley3be34542004-09-11 19:12:13 +01001142 iov[0].iov_base = daemon->packet;
1143 iov[0].iov_len = daemon->edns_pktsz;
Simon Kelley44a2a312004-03-10 20:04:35 +00001144
1145 msg.msg_control = control_u.control;
1146 msg.msg_controllen = sizeof(control_u);
1147 msg.msg_flags = 0;
1148 msg.msg_name = &source_addr;
1149 msg.msg_namelen = sizeof(source_addr);
1150 msg.msg_iov = iov;
1151 msg.msg_iovlen = 1;
1152
Simon Kelleyde379512004-06-22 20:23:33 +01001153 if ((n = recvmsg(listen->fd, &msg, 0)) == -1)
Simon Kelley3be34542004-09-11 19:12:13 +01001154 return;
Simon Kelley44a2a312004-03-10 20:04:35 +00001155
Simon Kelley572b41e2011-02-18 18:11:18 +00001156 if (n < (int)sizeof(struct dns_header) ||
Simon Kelley5e9e0ef2006-04-17 14:24:29 +01001157 (msg.msg_flags & MSG_TRUNC) ||
Simon Kelley572b41e2011-02-18 18:11:18 +00001158 (header->hb3 & HB3_QR))
Simon Kelley3be34542004-09-11 19:12:13 +01001159 return;
Simon Kelley44a2a312004-03-10 20:04:35 +00001160
Simon Kelley26128d22004-11-14 16:43:54 +00001161 source_addr.sa.sa_family = listen->family;
Simon Kelley2a7a2b82014-03-22 19:18:06 +00001162
1163 if (listen->family == AF_INET)
1164 {
1165 /* Source-port == 0 is an error, we can't send back to that.
1166 http://www.ietf.org/mail-archive/web/dnsop/current/msg11441.html */
1167 if (source_addr.in.sin_port == 0)
1168 return;
1169 }
Simon Kelley26128d22004-11-14 16:43:54 +00001170#ifdef HAVE_IPV6
Simon Kelley2a7a2b82014-03-22 19:18:06 +00001171 else
1172 {
1173 /* Source-port == 0 is an error, we can't send back to that. */
1174 if (source_addr.in6.sin6_port == 0)
1175 return;
1176 source_addr.in6.sin6_flowinfo = 0;
1177 }
Simon Kelley26128d22004-11-14 16:43:54 +00001178#endif
Simon Kelley2a7a2b82014-03-22 19:18:06 +00001179
Simon Kelleyc8a80482014-03-05 14:29:54 +00001180 /* We can be configured to only accept queries from at-most-one-hop-away addresses. */
1181 if (option_bool(OPT_LOCAL_SERVICE))
1182 {
1183 struct addrlist *addr;
1184#ifdef HAVE_IPV6
1185 if (listen->family == AF_INET6)
1186 {
1187 for (addr = daemon->interface_addrs; addr; addr = addr->next)
1188 if ((addr->flags & ADDRLIST_IPV6) &&
1189 is_same_net6(&addr->addr.addr.addr6, &source_addr.in6.sin6_addr, addr->prefixlen))
1190 break;
1191 }
1192 else
1193#endif
1194 {
1195 struct in_addr netmask;
1196 for (addr = daemon->interface_addrs; addr; addr = addr->next)
1197 {
Richard Genoud15b1b7e2014-09-17 21:12:00 +01001198 netmask.s_addr = htonl(~(in_addr_t)0 << (32 - addr->prefixlen));
Simon Kelleyc8a80482014-03-05 14:29:54 +00001199 if (!(addr->flags & ADDRLIST_IPV6) &&
1200 is_same_net(addr->addr.addr.addr4, source_addr.in.sin_addr, netmask))
1201 break;
1202 }
1203 }
1204 if (!addr)
1205 {
Simon Kelley0c8584e2014-03-12 20:12:56 +00001206 static int warned = 0;
1207 if (!warned)
1208 {
1209 my_syslog(LOG_WARNING, _("Ignoring query from non-local network"));
1210 warned = 1;
1211 }
Simon Kelleyc8a80482014-03-05 14:29:54 +00001212 return;
1213 }
1214 }
1215
Simon Kelley2329bef2013-12-03 13:41:16 +00001216 if (check_dst)
Simon Kelley44a2a312004-03-10 20:04:35 +00001217 {
Simon Kelley8a911cc2004-03-16 18:35:52 +00001218 struct ifreq ifr;
1219
Simon Kelley26128d22004-11-14 16:43:54 +00001220 if (msg.msg_controllen < sizeof(struct cmsghdr))
1221 return;
1222
Simon Kelley5e9e0ef2006-04-17 14:24:29 +01001223#if defined(HAVE_LINUX_NETWORK)
Simon Kelley26128d22004-11-14 16:43:54 +00001224 if (listen->family == AF_INET)
1225 for (cmptr = CMSG_FIRSTHDR(&msg); cmptr; cmptr = CMSG_NXTHDR(&msg, cmptr))
Simon Kelleyc72daea2012-01-05 21:33:27 +00001226 if (cmptr->cmsg_level == IPPROTO_IP && cmptr->cmsg_type == IP_PKTINFO)
Simon Kelley26128d22004-11-14 16:43:54 +00001227 {
Simon Kelley8ef5ada2010-06-03 19:42:45 +01001228 union {
1229 unsigned char *c;
1230 struct in_pktinfo *p;
1231 } p;
1232 p.c = CMSG_DATA(cmptr);
1233 dst_addr_4 = dst_addr.addr.addr4 = p.p->ipi_spec_dst;
1234 if_index = p.p->ipi_ifindex;
Simon Kelley26128d22004-11-14 16:43:54 +00001235 }
1236#elif defined(IP_RECVDSTADDR) && defined(IP_RECVIF)
1237 if (listen->family == AF_INET)
1238 {
1239 for (cmptr = CMSG_FIRSTHDR(&msg); cmptr; cmptr = CMSG_NXTHDR(&msg, cmptr))
Simon Kelley8ef5ada2010-06-03 19:42:45 +01001240 {
1241 union {
1242 unsigned char *c;
1243 unsigned int *i;
1244 struct in_addr *a;
1245#ifndef HAVE_SOLARIS_NETWORK
1246 struct sockaddr_dl *s;
Simon Kelley824af852008-02-12 20:43:05 +00001247#endif
Simon Kelley8ef5ada2010-06-03 19:42:45 +01001248 } p;
1249 p.c = CMSG_DATA(cmptr);
1250 if (cmptr->cmsg_level == IPPROTO_IP && cmptr->cmsg_type == IP_RECVDSTADDR)
1251 dst_addr_4 = dst_addr.addr.addr4 = *(p.a);
1252 else if (cmptr->cmsg_level == IPPROTO_IP && cmptr->cmsg_type == IP_RECVIF)
1253#ifdef HAVE_SOLARIS_NETWORK
1254 if_index = *(p.i);
1255#else
1256 if_index = p.s->sdl_index;
1257#endif
1258 }
Simon Kelley26128d22004-11-14 16:43:54 +00001259 }
1260#endif
1261
1262#ifdef HAVE_IPV6
1263 if (listen->family == AF_INET6)
1264 {
1265 for (cmptr = CMSG_FIRSTHDR(&msg); cmptr; cmptr = CMSG_NXTHDR(&msg, cmptr))
Simon Kelleyc72daea2012-01-05 21:33:27 +00001266 if (cmptr->cmsg_level == IPPROTO_IPV6 && cmptr->cmsg_type == daemon->v6pktinfo)
Simon Kelley26128d22004-11-14 16:43:54 +00001267 {
Simon Kelley8ef5ada2010-06-03 19:42:45 +01001268 union {
1269 unsigned char *c;
1270 struct in6_pktinfo *p;
1271 } p;
1272 p.c = CMSG_DATA(cmptr);
1273
1274 dst_addr.addr.addr6 = p.p->ipi6_addr;
1275 if_index = p.p->ipi6_ifindex;
Simon Kelley26128d22004-11-14 16:43:54 +00001276 }
1277 }
1278#endif
1279
1280 /* enforce available interface configuration */
1281
Simon Kelleye25db1f2013-01-29 22:10:26 +00001282 if (!indextoname(listen->fd, if_index, ifr.ifr_name))
Simon Kelley832af0b2007-01-21 20:01:28 +00001283 return;
1284
Simon Kelleye25db1f2013-01-29 22:10:26 +00001285 if (!iface_check(listen->family, &dst_addr, ifr.ifr_name, &auth_dns))
1286 {
1287 if (!option_bool(OPT_CLEVERBIND))
Simon Kelley115ac3e2013-05-20 11:28:32 +01001288 enumerate_interfaces(0);
Simon Kelley3f2873d2013-05-14 11:28:47 +01001289 if (!loopback_exception(listen->fd, listen->family, &dst_addr, ifr.ifr_name) &&
1290 !label_exception(if_index, listen->family, &dst_addr))
Simon Kelleye25db1f2013-01-29 22:10:26 +00001291 return;
1292 }
1293
Simon Kelley552af8b2012-02-29 20:10:31 +00001294 if (listen->family == AF_INET && option_bool(OPT_LOCALISE))
1295 {
1296 struct irec *iface;
1297
1298 /* get the netmask of the interface whch has the address we were sent to.
1299 This is no neccessarily the interface we arrived on. */
1300
1301 for (iface = daemon->interfaces; iface; iface = iface->next)
1302 if (iface->addr.sa.sa_family == AF_INET &&
1303 iface->addr.in.sin_addr.s_addr == dst_addr_4.s_addr)
1304 break;
1305
1306 /* interface may be new */
Simon Kelleye25db1f2013-01-29 22:10:26 +00001307 if (!iface && !option_bool(OPT_CLEVERBIND))
Simon Kelley115ac3e2013-05-20 11:28:32 +01001308 enumerate_interfaces(0);
Simon Kelley552af8b2012-02-29 20:10:31 +00001309
1310 for (iface = daemon->interfaces; iface; iface = iface->next)
1311 if (iface->addr.sa.sa_family == AF_INET &&
1312 iface->addr.in.sin_addr.s_addr == dst_addr_4.s_addr)
1313 break;
1314
1315 /* If we failed, abandon localisation */
1316 if (iface)
1317 netmask = iface->netmask;
1318 else
1319 dst_addr_4.s_addr = 0;
1320 }
Simon Kelley44a2a312004-03-10 20:04:35 +00001321 }
Simon Kelley25cf5e32015-01-09 15:53:03 +00001322
1323 /* log_query gets called indirectly all over the place, so
1324 pass these in global variables - sorry. */
1325 daemon->log_display_id = ++daemon->log_id;
1326 daemon->log_source_addr = &source_addr;
Simon Kelley44a2a312004-03-10 20:04:35 +00001327
Simon Kelleycdeda282006-03-16 20:16:06 +00001328 if (extract_request(header, (size_t)n, daemon->namebuff, &type))
Simon Kelley44a2a312004-03-10 20:04:35 +00001329 {
Simon Kelleyb485ed92013-10-18 22:00:39 +01001330#ifdef HAVE_AUTH
1331 struct auth_zone *zone;
1332#endif
Simon Kelley610e7822014-02-06 14:45:17 +00001333 char *types = querystr(auth_dns ? "auth" : "query", type);
1334
Simon Kelley44a2a312004-03-10 20:04:35 +00001335 if (listen->family == AF_INET)
Simon Kelley3be34542004-09-11 19:12:13 +01001336 log_query(F_QUERY | F_IPV4 | F_FORWARD, daemon->namebuff,
Simon Kelley1a6bca82008-07-11 11:11:42 +01001337 (struct all_addr *)&source_addr.in.sin_addr, types);
Simon Kelley44a2a312004-03-10 20:04:35 +00001338#ifdef HAVE_IPV6
1339 else
Simon Kelley3be34542004-09-11 19:12:13 +01001340 log_query(F_QUERY | F_IPV6 | F_FORWARD, daemon->namebuff,
Simon Kelley1a6bca82008-07-11 11:11:42 +01001341 (struct all_addr *)&source_addr.in6.sin6_addr, types);
Simon Kelley44a2a312004-03-10 20:04:35 +00001342#endif
Simon Kelley44a2a312004-03-10 20:04:35 +00001343
Simon Kelley4820dce2012-12-18 18:30:30 +00001344#ifdef HAVE_AUTH
Simon Kelleyb485ed92013-10-18 22:00:39 +01001345 /* find queries for zones we're authoritative for, and answer them directly */
Simon Kelley6008bdb2013-10-21 21:47:03 +01001346 if (!auth_dns)
1347 for (zone = daemon->auth_zones; zone; zone = zone->next)
1348 if (in_zone(zone, daemon->namebuff, NULL))
1349 {
1350 auth_dns = 1;
1351 local_auth = 1;
1352 break;
1353 }
Simon Kelleyb485ed92013-10-18 22:00:39 +01001354#endif
Simon Kelleyb5ea1cc2014-07-29 16:34:14 +01001355
1356#ifdef HAVE_LOOP
1357 /* Check for forwarding loop */
1358 if (detect_loop(daemon->namebuff, type))
1359 return;
1360#endif
Simon Kelleyb485ed92013-10-18 22:00:39 +01001361 }
1362
1363#ifdef HAVE_AUTH
Simon Kelley4f7b3042012-11-28 21:27:02 +00001364 if (auth_dns)
Simon Kelley824af852008-02-12 20:43:05 +00001365 {
Simon Kelley60b68062014-01-08 12:10:28 +00001366 m = answer_auth(header, ((char *) header) + daemon->packet_buff_sz, (size_t)n, now, &source_addr, local_auth);
Simon Kelley4f7b3042012-11-28 21:27:02 +00001367 if (m >= 1)
Simon Kelleyb485ed92013-10-18 22:00:39 +01001368 {
1369 send_from(listen->fd, option_bool(OPT_NOWILD) || option_bool(OPT_CLEVERBIND),
1370 (char *)header, m, &source_addr, &dst_addr, if_index);
1371 daemon->auth_answer++;
1372 }
Simon Kelley824af852008-02-12 20:43:05 +00001373 }
Simon Kelley44a2a312004-03-10 20:04:35 +00001374 else
Simon Kelley4820dce2012-12-18 18:30:30 +00001375#endif
Simon Kelley4f7b3042012-11-28 21:27:02 +00001376 {
Simon Kelley613ad152014-02-25 23:02:28 +00001377 int ad_reqd, do_bit;
Simon Kelley60b68062014-01-08 12:10:28 +00001378 m = answer_request(header, ((char *) header) + daemon->packet_buff_sz, (size_t)n,
Simon Kelley613ad152014-02-25 23:02:28 +00001379 dst_addr_4, netmask, now, &ad_reqd, &do_bit);
Simon Kelley4f7b3042012-11-28 21:27:02 +00001380
1381 if (m >= 1)
1382 {
1383 send_from(listen->fd, option_bool(OPT_NOWILD) || option_bool(OPT_CLEVERBIND),
1384 (char *)header, m, &source_addr, &dst_addr, if_index);
1385 daemon->local_answer++;
1386 }
1387 else if (forward_query(listen->fd, &source_addr, &dst_addr, if_index,
Simon Kelley613ad152014-02-25 23:02:28 +00001388 header, (size_t)n, now, NULL, ad_reqd, do_bit))
Simon Kelley4f7b3042012-11-28 21:27:02 +00001389 daemon->queries_forwarded++;
1390 else
1391 daemon->local_answer++;
1392 }
Simon Kelley44a2a312004-03-10 20:04:35 +00001393}
1394
Simon Kelley7d7b7b32014-01-08 15:53:35 +00001395#ifdef HAVE_DNSSEC
Simon Kelley00a5b5d2014-02-28 18:10:55 +00001396
1397/* UDP: we've got an unsigned answer, return STAT_INSECURE if we can prove there's no DS
1398 and therefore the answer shouldn't be signed, or STAT_BOGUS if it should be, or
1399 STAT_NEED_DS_NEG and keyname if we need to do the query. */
Simon Kelley97e618a2015-01-07 21:55:43 +00001400static int send_check_sign(struct frec *forward, time_t now, struct dns_header *header, size_t plen,
1401 char *name, char *keyname)
Simon Kelley00a5b5d2014-02-28 18:10:55 +00001402{
Simon Kelley00a5b5d2014-02-28 18:10:55 +00001403 int status = dnssec_chase_cname(now, header, plen, name, keyname);
1404
1405 if (status != STAT_INSECURE)
1406 return status;
1407
Simon Kelley97e618a2015-01-07 21:55:43 +00001408 /* Store the domain we're trying to check. */
1409 forward->name_start = strlen(name);
1410 forward->name_len = forward->name_start + 1;
1411 if (!(forward->orig_domain = blockdata_alloc(name, forward->name_len)))
1412 return STAT_BOGUS;
1413
1414 return do_check_sign(forward, 0, now, name, keyname);
1415}
1416
1417/* We either have a a reply (header non-NULL, or we need to start by looking in the cache */
1418static int do_check_sign(struct frec *forward, int status, time_t now, char *name, char *keyname)
1419{
1420 /* get domain we're checking back from blockdata store, it's stored on the original query. */
1421 while (forward->dependent)
1422 forward = forward->dependent;
1423
1424 blockdata_retrieve(forward->orig_domain, forward->name_len, name);
1425
Simon Kelley00a5b5d2014-02-28 18:10:55 +00001426 while (1)
1427 {
Simon Kelley97e618a2015-01-07 21:55:43 +00001428 char *p;
Simon Kelley00a5b5d2014-02-28 18:10:55 +00001429
Simon Kelley97e618a2015-01-07 21:55:43 +00001430 if (status == 0)
1431 {
1432 struct crec *crecp;
1433
1434 /* Haven't received answer, see if in cache */
1435 if (!(crecp = cache_find_by_name(NULL, &name[forward->name_start], now, F_DS)))
1436 {
1437 /* put name of DS record we're missing into keyname */
1438 strcpy(keyname, &name[forward->name_start]);
1439 /* and wait for reply to arrive */
1440 return STAT_NEED_DS_NEG;
1441 }
1442
1443 /* F_DNSSECOK misused in DS cache records to non-existance of NS record */
1444 if (!(crecp->flags & F_NEG))
1445 status = STAT_SECURE;
1446 else if (crecp->flags & F_DNSSECOK)
1447 status = STAT_NO_DS;
1448 else
1449 status = STAT_NO_NS;
1450 }
1451
1452 /* Have entered non-signed part of DNS tree. */
1453 if (status == STAT_NO_DS)
1454 return STAT_INSECURE;
1455
1456 if (status == STAT_BOGUS)
Simon Kelley4e1fe442014-03-26 12:24:19 +00001457 return STAT_BOGUS;
1458
Simon Kelley97e618a2015-01-07 21:55:43 +00001459 /* There's a proven DS record, or we're within a zone, where there doesn't need
1460 to be a DS record. Add a name and try again.
1461 If we've already tried the whole name, then fail */
Simon Kelley00a5b5d2014-02-28 18:10:55 +00001462
Simon Kelley97e618a2015-01-07 21:55:43 +00001463 if (forward->name_start == 0)
1464 return STAT_BOGUS;
Simon Kelley4872aa72014-04-26 22:13:31 +01001465
Simon Kelley97e618a2015-01-07 21:55:43 +00001466 for (p = &name[forward->name_start-2]; (*p != '.') && (p != name); p--);
1467
1468 if (p != name)
1469 p++;
1470
1471 forward->name_start = p - name;
1472 status = 0; /* force to cache when we iterate. */
Simon Kelley00a5b5d2014-02-28 18:10:55 +00001473 }
Simon Kelley00a5b5d2014-02-28 18:10:55 +00001474}
1475
Simon Kelleyfe3992f2015-04-03 21:25:05 +01001476/* Move down from the root, until we find a signed non-existance of a DS, in which case
Simon Kelley00a5b5d2014-02-28 18:10:55 +00001477 an unsigned answer is OK, or we find a signed DS, in which case there should be
1478 a signature, and the answer is BOGUS */
1479static int tcp_check_for_unsigned_zone(time_t now, struct dns_header *header, size_t plen, int class, char *name,
1480 char *keyname, struct server *server, int *keycount)
1481{
1482 size_t m;
1483 unsigned char *packet, *payload;
1484 u16 *length;
Simon Kelley97e618a2015-01-07 21:55:43 +00001485 int status, name_len;
1486 struct blockdata *block;
1487
1488 char *name_start;
Simon Kelley00a5b5d2014-02-28 18:10:55 +00001489
1490 /* Get first insecure entry in CNAME chain */
1491 status = tcp_key_recurse(now, STAT_CHASE_CNAME, header, plen, class, name, keyname, server, keycount);
1492 if (status == STAT_BOGUS)
1493 return STAT_BOGUS;
1494
1495 if (!(packet = whine_malloc(65536 + MAXDNAME + RRFIXEDSZ + sizeof(u16))))
1496 return STAT_BOGUS;
1497
1498 payload = &packet[2];
1499 header = (struct dns_header *)payload;
1500 length = (u16 *)packet;
Simon Kelley97e618a2015-01-07 21:55:43 +00001501
1502 /* Stash the name away, since the buffer will be trashed when we recurse */
1503 name_len = strlen(name) + 1;
1504 name_start = name + name_len - 1;
Simon Kelley00a5b5d2014-02-28 18:10:55 +00001505
Simon Kelley97e618a2015-01-07 21:55:43 +00001506 if (!(block = blockdata_alloc(name, name_len)))
1507 {
1508 free(packet);
1509 return STAT_BOGUS;
1510 }
1511
Simon Kelley00a5b5d2014-02-28 18:10:55 +00001512 while (1)
1513 {
Simon Kelley00a5b5d2014-02-28 18:10:55 +00001514 unsigned char c1, c2;
Simon Kelley97e618a2015-01-07 21:55:43 +00001515 struct crec *crecp;
1516
Simon Kelley00a5b5d2014-02-28 18:10:55 +00001517 if (--(*keycount) == 0)
Tomas Hozzafc2833f2014-03-25 20:43:21 +00001518 {
1519 free(packet);
Simon Kelley97e618a2015-01-07 21:55:43 +00001520 blockdata_free(block);
Tomas Hozzafc2833f2014-03-25 20:43:21 +00001521 return STAT_BOGUS;
1522 }
Simon Kelley97e618a2015-01-07 21:55:43 +00001523
Simon Kelley424c4a82015-01-07 21:58:05 +00001524 while ((crecp = cache_find_by_name(NULL, name_start, now, F_DS)))
Simon Kelley97e618a2015-01-07 21:55:43 +00001525 {
1526 if ((crecp->flags & F_NEG) && (crecp->flags & F_DNSSECOK))
1527 {
1528 /* Found a secure denial of DS - delegation is indeed insecure */
1529 free(packet);
1530 blockdata_free(block);
1531 return STAT_INSECURE;
1532 }
1533
1534 /* Here, either there's a secure DS, or no NS and no DS, and therefore no delegation.
1535 Add another label and continue. */
1536
1537 if (name_start == name)
1538 {
1539 free(packet);
1540 blockdata_free(block);
1541 return STAT_BOGUS; /* run out of labels */
1542 }
1543
1544 name_start -= 2;
1545 while (*name_start != '.' && name_start != name)
1546 name_start--;
1547 if (name_start != name)
1548 name_start++;
Simon Kelley00a5b5d2014-02-28 18:10:55 +00001549 }
1550
Simon Kelley97e618a2015-01-07 21:55:43 +00001551 /* Can't find it in the cache, have to send a query */
Simon Kelley4e1fe442014-03-26 12:24:19 +00001552
Simon Kelleya77cec82015-05-08 16:25:38 +01001553 m = dnssec_generate_query(header, ((char *) header) + 65536, name_start, class, T_DS, &server->addr, server->edns_pktsz);
Simon Kelley00a5b5d2014-02-28 18:10:55 +00001554
Simon Kelley97e618a2015-01-07 21:55:43 +00001555 *length = htons(m);
1556
1557 if (read_write(server->tcpfd, packet, m + sizeof(u16), 0) &&
1558 read_write(server->tcpfd, &c1, 1, 1) &&
1559 read_write(server->tcpfd, &c2, 1, 1) &&
1560 read_write(server->tcpfd, payload, (c1 << 8) | c2, 1))
Simon Kelley00a5b5d2014-02-28 18:10:55 +00001561 {
Simon Kelley97e618a2015-01-07 21:55:43 +00001562 m = (c1 << 8) | c2;
Simon Kelley00a5b5d2014-02-28 18:10:55 +00001563
Simon Kelley97e618a2015-01-07 21:55:43 +00001564 /* Note this trashes all three name workspaces */
1565 status = tcp_key_recurse(now, STAT_NEED_DS_NEG, header, m, class, name, keyname, server, keycount);
1566
1567 if (status == STAT_NO_DS)
Simon Kelley00a5b5d2014-02-28 18:10:55 +00001568 {
Simon Kelley97e618a2015-01-07 21:55:43 +00001569 /* Found a secure denial of DS - delegation is indeed insecure */
1570 free(packet);
1571 blockdata_free(block);
1572 return STAT_INSECURE;
Simon Kelley00a5b5d2014-02-28 18:10:55 +00001573 }
Simon Kelley97e618a2015-01-07 21:55:43 +00001574
1575 if (status == STAT_BOGUS)
1576 {
1577 free(packet);
1578 blockdata_free(block);
1579 return STAT_BOGUS;
1580 }
1581
1582 /* Here, either there's a secure DS, or no NS and no DS, and therefore no delegation.
1583 Add another label and continue. */
1584
1585 /* Get name we're checking back. */
1586 blockdata_retrieve(block, name_len, name);
1587
1588 if (name_start == name)
1589 {
1590 free(packet);
1591 blockdata_free(block);
1592 return STAT_BOGUS; /* run out of labels */
1593 }
1594
1595 name_start -= 2;
1596 while (*name_start != '.' && name_start != name)
1597 name_start--;
1598 if (name_start != name)
1599 name_start++;
Simon Kelley00a5b5d2014-02-28 18:10:55 +00001600 }
Simon Kelley97e618a2015-01-07 21:55:43 +00001601 else
1602 {
1603 /* IO failure */
1604 free(packet);
1605 blockdata_free(block);
1606 return STAT_BOGUS; /* run out of labels */
1607 }
Simon Kelley00a5b5d2014-02-28 18:10:55 +00001608 }
1609}
1610
Simon Kelley7fa836e2014-02-10 20:11:24 +00001611static int tcp_key_recurse(time_t now, int status, struct dns_header *header, size_t n,
1612 int class, char *name, char *keyname, struct server *server, int *keycount)
Simon Kelley7d7b7b32014-01-08 15:53:35 +00001613{
1614 /* Recurse up the key heirarchy */
Simon Kelley7d7b7b32014-01-08 15:53:35 +00001615 int new_status;
Simon Kelley7d7b7b32014-01-08 15:53:35 +00001616
Simon Kelley7fa836e2014-02-10 20:11:24 +00001617 /* limit the amount of work we do, to avoid cycling forever on loops in the DNS */
1618 if (--(*keycount) == 0)
1619 return STAT_INSECURE;
Simon Kelley7d7b7b32014-01-08 15:53:35 +00001620
Simon Kelley7fa836e2014-02-10 20:11:24 +00001621 if (status == STAT_NEED_KEY)
1622 new_status = dnssec_validate_by_ds(now, header, n, name, keyname, class);
Simon Kelley00a5b5d2014-02-28 18:10:55 +00001623 else if (status == STAT_NEED_DS || status == STAT_NEED_DS_NEG)
1624 {
1625 new_status = dnssec_validate_ds(now, header, n, name, keyname, class);
Simon Kelleyfe3992f2015-04-03 21:25:05 +01001626 if (status == STAT_NEED_DS)
1627 {
1628 if (new_status == STAT_NO_DS)
1629 new_status = STAT_INSECURE_DS;
1630 else if (new_status == STAT_NO_NS)
1631 new_status = STAT_BOGUS;
1632 }
Simon Kelley00a5b5d2014-02-28 18:10:55 +00001633 }
1634 else if (status == STAT_CHASE_CNAME)
1635 new_status = dnssec_chase_cname(now, header, n, name, keyname);
1636 else
1637 {
Simon Kelley97e618a2015-01-07 21:55:43 +00001638 new_status = dnssec_validate_reply(now, header, n, name, keyname, &class, NULL, NULL);
Simon Kelley00a5b5d2014-02-28 18:10:55 +00001639
1640 if (new_status == STAT_NO_SIG)
1641 {
1642 if (option_bool(OPT_DNSSEC_NO_SIGN))
1643 new_status = tcp_check_for_unsigned_zone(now, header, n, class, name, keyname, server, keycount);
1644 else
1645 new_status = STAT_INSECURE;
1646 }
1647 }
1648
Simon Kelley7fa836e2014-02-10 20:11:24 +00001649 /* Can't validate because we need a key/DS whose name now in keyname.
1650 Make query for same, and recurse to validate */
1651 if (new_status == STAT_NEED_DS || new_status == STAT_NEED_KEY)
Simon Kelley7d7b7b32014-01-08 15:53:35 +00001652 {
Simon Kelley7fa836e2014-02-10 20:11:24 +00001653 size_t m;
1654 unsigned char *packet = whine_malloc(65536 + MAXDNAME + RRFIXEDSZ + sizeof(u16));
1655 unsigned char *payload = &packet[2];
1656 struct dns_header *new_header = (struct dns_header *)payload;
1657 u16 *length = (u16 *)packet;
1658 unsigned char c1, c2;
1659
1660 if (!packet)
1661 return STAT_INSECURE;
1662
1663 another_tcp_key:
1664 m = dnssec_generate_query(new_header, ((char *) new_header) + 65536, keyname, class,
Simon Kelleya77cec82015-05-08 16:25:38 +01001665 new_status == STAT_NEED_KEY ? T_DNSKEY : T_DS, &server->addr, server->edns_pktsz);
Simon Kelley7d7b7b32014-01-08 15:53:35 +00001666
Simon Kelley7fa836e2014-02-10 20:11:24 +00001667 *length = htons(m);
1668
1669 if (!read_write(server->tcpfd, packet, m + sizeof(u16), 0) ||
1670 !read_write(server->tcpfd, &c1, 1, 1) ||
1671 !read_write(server->tcpfd, &c2, 1, 1) ||
1672 !read_write(server->tcpfd, payload, (c1 << 8) | c2, 1))
1673 new_status = STAT_INSECURE;
Simon Kelley7d7b7b32014-01-08 15:53:35 +00001674 else
Simon Kelley7d7b7b32014-01-08 15:53:35 +00001675 {
Simon Kelley7fa836e2014-02-10 20:11:24 +00001676 m = (c1 << 8) | c2;
1677
Simon Kelley00a5b5d2014-02-28 18:10:55 +00001678 new_status = tcp_key_recurse(now, new_status, new_header, m, class, name, keyname, server, keycount);
1679
1680 if (new_status == STAT_SECURE)
Simon Kelley7d7b7b32014-01-08 15:53:35 +00001681 {
Simon Kelley7fa836e2014-02-10 20:11:24 +00001682 /* Reached a validated record, now try again at this level.
1683 Note that we may get ANOTHER NEED_* if an answer needs more than one key.
1684 If so, go round again. */
Simon Kelley7d7b7b32014-01-08 15:53:35 +00001685
Simon Kelley7fa836e2014-02-10 20:11:24 +00001686 if (status == STAT_NEED_KEY)
1687 new_status = dnssec_validate_by_ds(now, header, n, name, keyname, class);
Simon Kelley00a5b5d2014-02-28 18:10:55 +00001688 else if (status == STAT_NEED_DS || status == STAT_NEED_DS_NEG)
1689 {
1690 new_status = dnssec_validate_ds(now, header, n, name, keyname, class);
Simon Kelleyfe3992f2015-04-03 21:25:05 +01001691 if (status == STAT_NEED_DS)
1692 {
1693 if (new_status == STAT_NO_DS)
1694 new_status = STAT_INSECURE_DS;
1695 else if (new_status == STAT_NO_NS)
1696 new_status = STAT_BOGUS; /* Validated no DS */
1697 }
Simon Kelley00a5b5d2014-02-28 18:10:55 +00001698 }
1699 else if (status == STAT_CHASE_CNAME)
1700 new_status = dnssec_chase_cname(now, header, n, name, keyname);
1701 else
1702 {
Simon Kelley97e618a2015-01-07 21:55:43 +00001703 new_status = dnssec_validate_reply(now, header, n, name, keyname, &class, NULL, NULL);
Simon Kelley00a5b5d2014-02-28 18:10:55 +00001704
1705 if (new_status == STAT_NO_SIG)
1706 {
1707 if (option_bool(OPT_DNSSEC_NO_SIGN))
1708 new_status = tcp_check_for_unsigned_zone(now, header, n, class, name, keyname, server, keycount);
1709 else
1710 new_status = STAT_INSECURE;
1711 }
1712 }
1713
Simon Kelley7d7b7b32014-01-08 15:53:35 +00001714 if (new_status == STAT_NEED_DS || new_status == STAT_NEED_KEY)
Simon Kelley7fa836e2014-02-10 20:11:24 +00001715 goto another_tcp_key;
Simon Kelley7d7b7b32014-01-08 15:53:35 +00001716 }
1717 }
Simon Kelleyfe3992f2015-04-03 21:25:05 +01001718
Simon Kelley7fa836e2014-02-10 20:11:24 +00001719 free(packet);
Simon Kelley7d7b7b32014-01-08 15:53:35 +00001720 }
Simon Kelley7d7b7b32014-01-08 15:53:35 +00001721 return new_status;
1722}
1723#endif
1724
1725
Simon Kelleyfeba5c12004-07-27 20:28:58 +01001726/* The daemon forks before calling this: it should deal with one connection,
1727 blocking as neccessary, and then return. Note, need to be a bit careful
1728 about resources for debug mode, when the fork is suppressed: that's
1729 done by the caller. */
Simon Kelley5aabfc72007-08-29 11:24:47 +01001730unsigned char *tcp_request(int confd, time_t now,
Simon Kelley4f7b3042012-11-28 21:27:02 +00001731 union mysockaddr *local_addr, struct in_addr netmask, int auth_dns)
Simon Kelleyfeba5c12004-07-27 20:28:58 +01001732{
Simon Kelley28866e92011-02-14 20:19:14 +00001733 size_t size = 0;
1734 int norebind = 0;
Vladislav Grishenko3b195962013-11-26 11:08:21 +00001735#ifdef HAVE_AUTH
Simon Kelley19b16892013-10-20 10:19:39 +01001736 int local_auth = 0;
Vladislav Grishenko3b195962013-11-26 11:08:21 +00001737#endif
Simon Kelley613ad152014-02-25 23:02:28 +00001738 int checking_disabled, ad_question, do_bit, added_pheader = 0;
Simon Kelleyfe3992f2015-04-03 21:25:05 +01001739 int check_subnet, no_cache_dnssec = 0, cache_secure = 0, bogusanswer = 0;
Simon Kelleycdeda282006-03-16 20:16:06 +00001740 size_t m;
Simon Kelleyee86ce62012-12-07 11:54:46 +00001741 unsigned short qtype;
1742 unsigned int gotname;
Simon Kelleyfeba5c12004-07-27 20:28:58 +01001743 unsigned char c1, c2;
Simon Kelley4b5ea122013-04-22 10:18:26 +01001744 /* Max TCP packet + slop + size */
1745 unsigned char *packet = whine_malloc(65536 + MAXDNAME + RRFIXEDSZ + sizeof(u16));
1746 unsigned char *payload = &packet[2];
1747 /* largest field in header is 16-bits, so this is still sufficiently aligned */
1748 struct dns_header *header = (struct dns_header *)payload;
1749 u16 *length = (u16 *)packet;
Simon Kelley3be34542004-09-11 19:12:13 +01001750 struct server *last_server;
Simon Kelley7de060b2011-08-26 17:24:52 +01001751 struct in_addr dst_addr_4;
1752 union mysockaddr peer_addr;
1753 socklen_t peer_len = sizeof(union mysockaddr);
Simon Kelley25cf5e32015-01-09 15:53:03 +00001754 int query_count = 0;
1755
Simon Kelley7de060b2011-08-26 17:24:52 +01001756 if (getpeername(confd, (struct sockaddr *)&peer_addr, &peer_len) == -1)
1757 return packet;
Simon Kelleyc8a80482014-03-05 14:29:54 +00001758
1759 /* We can be configured to only accept queries from at-most-one-hop-away addresses. */
1760 if (option_bool(OPT_LOCAL_SERVICE))
1761 {
1762 struct addrlist *addr;
1763#ifdef HAVE_IPV6
1764 if (peer_addr.sa.sa_family == AF_INET6)
1765 {
1766 for (addr = daemon->interface_addrs; addr; addr = addr->next)
1767 if ((addr->flags & ADDRLIST_IPV6) &&
1768 is_same_net6(&addr->addr.addr.addr6, &peer_addr.in6.sin6_addr, addr->prefixlen))
1769 break;
1770 }
1771 else
1772#endif
1773 {
1774 struct in_addr netmask;
1775 for (addr = daemon->interface_addrs; addr; addr = addr->next)
1776 {
Richard Genoud15b1b7e2014-09-17 21:12:00 +01001777 netmask.s_addr = htonl(~(in_addr_t)0 << (32 - addr->prefixlen));
Simon Kelleyc8a80482014-03-05 14:29:54 +00001778 if (!(addr->flags & ADDRLIST_IPV6) &&
1779 is_same_net(addr->addr.addr.addr4, peer_addr.in.sin_addr, netmask))
1780 break;
1781 }
1782 }
1783 if (!addr)
1784 {
1785 my_syslog(LOG_WARNING, _("Ignoring query from non-local network"));
1786 return packet;
1787 }
1788 }
Simon Kelley7de060b2011-08-26 17:24:52 +01001789
Simon Kelleyfeba5c12004-07-27 20:28:58 +01001790 while (1)
1791 {
Simon Kelley25cf5e32015-01-09 15:53:03 +00001792 if (query_count == TCP_MAX_QUERIES ||
1793 !packet ||
Simon Kelleyfeba5c12004-07-27 20:28:58 +01001794 !read_write(confd, &c1, 1, 1) || !read_write(confd, &c2, 1, 1) ||
1795 !(size = c1 << 8 | c2) ||
Simon Kelley4b5ea122013-04-22 10:18:26 +01001796 !read_write(confd, payload, size, 1))
Simon Kelleyfeba5c12004-07-27 20:28:58 +01001797 return packet;
1798
Simon Kelley572b41e2011-02-18 18:11:18 +00001799 if (size < (int)sizeof(struct dns_header))
Simon Kelleyfeba5c12004-07-27 20:28:58 +01001800 continue;
1801
Simon Kelley25cf5e32015-01-09 15:53:03 +00001802 query_count++;
1803
1804 /* log_query gets called indirectly all over the place, so
1805 pass these in global variables - sorry. */
1806 daemon->log_display_id = ++daemon->log_id;
1807 daemon->log_source_addr = &peer_addr;
1808
Simon Kelleyed4c0762013-10-08 20:46:34 +01001809 check_subnet = 0;
1810
Simon Kelley28866e92011-02-14 20:19:14 +00001811 /* save state of "cd" flag in query */
Simon Kelley7d7b7b32014-01-08 15:53:35 +00001812 if ((checking_disabled = header->hb4 & HB4_CD))
1813 no_cache_dnssec = 1;
Simon Kelley28866e92011-02-14 20:19:14 +00001814
Simon Kelley3be34542004-09-11 19:12:13 +01001815 if ((gotname = extract_request(header, (unsigned int)size, daemon->namebuff, &qtype)))
Simon Kelleyfeba5c12004-07-27 20:28:58 +01001816 {
Simon Kelleyb485ed92013-10-18 22:00:39 +01001817#ifdef HAVE_AUTH
1818 struct auth_zone *zone;
1819#endif
Simon Kelley610e7822014-02-06 14:45:17 +00001820 char *types = querystr(auth_dns ? "auth" : "query", qtype);
Simon Kelley7de060b2011-08-26 17:24:52 +01001821
1822 if (peer_addr.sa.sa_family == AF_INET)
1823 log_query(F_QUERY | F_IPV4 | F_FORWARD, daemon->namebuff,
1824 (struct all_addr *)&peer_addr.in.sin_addr, types);
Simon Kelleyfeba5c12004-07-27 20:28:58 +01001825#ifdef HAVE_IPV6
Simon Kelley7de060b2011-08-26 17:24:52 +01001826 else
1827 log_query(F_QUERY | F_IPV6 | F_FORWARD, daemon->namebuff,
1828 (struct all_addr *)&peer_addr.in6.sin6_addr, types);
Simon Kelleyfeba5c12004-07-27 20:28:58 +01001829#endif
Simon Kelleyb485ed92013-10-18 22:00:39 +01001830
1831#ifdef HAVE_AUTH
1832 /* find queries for zones we're authoritative for, and answer them directly */
Simon Kelley6008bdb2013-10-21 21:47:03 +01001833 if (!auth_dns)
1834 for (zone = daemon->auth_zones; zone; zone = zone->next)
1835 if (in_zone(zone, daemon->namebuff, NULL))
1836 {
1837 auth_dns = 1;
1838 local_auth = 1;
1839 break;
1840 }
Simon Kelleyb485ed92013-10-18 22:00:39 +01001841#endif
Simon Kelleyfeba5c12004-07-27 20:28:58 +01001842 }
1843
Simon Kelley7de060b2011-08-26 17:24:52 +01001844 if (local_addr->sa.sa_family == AF_INET)
1845 dst_addr_4 = local_addr->in.sin_addr;
1846 else
1847 dst_addr_4.s_addr = 0;
1848
Simon Kelley4820dce2012-12-18 18:30:30 +00001849#ifdef HAVE_AUTH
Simon Kelley4f7b3042012-11-28 21:27:02 +00001850 if (auth_dns)
Simon Kelley19b16892013-10-20 10:19:39 +01001851 m = answer_auth(header, ((char *) header) + 65536, (size_t)size, now, &peer_addr, local_auth);
Simon Kelley4f7b3042012-11-28 21:27:02 +00001852 else
Simon Kelley4820dce2012-12-18 18:30:30 +00001853#endif
Simon Kelleyfeba5c12004-07-27 20:28:58 +01001854 {
Simon Kelley4f7b3042012-11-28 21:27:02 +00001855 /* m > 0 if answered from cache */
1856 m = answer_request(header, ((char *) header) + 65536, (size_t)size,
Simon Kelley613ad152014-02-25 23:02:28 +00001857 dst_addr_4, netmask, now, &ad_question, &do_bit);
Simon Kelleyfeba5c12004-07-27 20:28:58 +01001858
Simon Kelley4f7b3042012-11-28 21:27:02 +00001859 /* Do this by steam now we're not in the select() loop */
1860 check_log_writer(NULL);
1861
1862 if (m == 0)
Simon Kelleyfeba5c12004-07-27 20:28:58 +01001863 {
Simon Kelley4f7b3042012-11-28 21:27:02 +00001864 unsigned int flags = 0;
1865 struct all_addr *addrp = NULL;
1866 int type = 0;
1867 char *domain = NULL;
Simon Kelleyfeba5c12004-07-27 20:28:58 +01001868
Simon Kelley4f7b3042012-11-28 21:27:02 +00001869 if (option_bool(OPT_ADD_MAC))
1870 size = add_mac(header, size, ((char *) header) + 65536, &peer_addr);
Simon Kelleyed4c0762013-10-08 20:46:34 +01001871
1872 if (option_bool(OPT_CLIENT_SUBNET))
1873 {
1874 size_t new = add_source_addr(header, size, ((char *) header) + 65536, &peer_addr);
1875 if (size != new)
1876 {
1877 size = new;
1878 check_subnet = 1;
1879 }
1880 }
1881
Simon Kelley4f7b3042012-11-28 21:27:02 +00001882 if (gotname)
1883 flags = search_servers(now, &addrp, gotname, daemon->namebuff, &type, &domain, &norebind);
1884
1885 if (type != 0 || option_bool(OPT_ORDER) || !daemon->last_server)
1886 last_server = daemon->servers;
1887 else
1888 last_server = daemon->last_server;
1889
1890 if (!flags && last_server)
1891 {
1892 struct server *firstsendto = NULL;
Simon Kelley8a9be9e2014-01-25 23:17:21 +00001893#ifdef HAVE_DNSSEC
Simon Kelley703c7ff2014-01-25 23:46:23 +00001894 unsigned char *newhash, hash[HASH_SIZE];
Simon Kelley63758382014-04-16 22:20:55 +01001895 if ((newhash = hash_questions(header, (unsigned int)size, daemon->namebuff)))
Simon Kelley8a9be9e2014-01-25 23:17:21 +00001896 memcpy(hash, newhash, HASH_SIZE);
Tomas Hozzab37f8b92014-03-25 20:52:28 +00001897 else
1898 memset(hash, 0, HASH_SIZE);
Simon Kelley8a9be9e2014-01-25 23:17:21 +00001899#else
Simon Kelley4f7b3042012-11-28 21:27:02 +00001900 unsigned int crc = questions_crc(header, (unsigned int)size, daemon->namebuff);
Simon Kelley8a9be9e2014-01-25 23:17:21 +00001901#endif
Simon Kelley4f7b3042012-11-28 21:27:02 +00001902 /* Loop round available servers until we succeed in connecting to one.
1903 Note that this code subtley ensures that consecutive queries on this connection
1904 which can go to the same server, do so. */
1905 while (1)
Simon Kelleyfeba5c12004-07-27 20:28:58 +01001906 {
Simon Kelley4f7b3042012-11-28 21:27:02 +00001907 if (!firstsendto)
1908 firstsendto = last_server;
1909 else
1910 {
1911 if (!(last_server = last_server->next))
1912 last_server = daemon->servers;
1913
1914 if (last_server == firstsendto)
1915 break;
1916 }
1917
1918 /* server for wrong domain */
1919 if (type != (last_server->flags & SERV_TYPE) ||
Simon Kelleyb5ea1cc2014-07-29 16:34:14 +01001920 (type == SERV_HAS_DOMAIN && !hostname_isequal(domain, last_server->domain)) ||
1921 (last_server->flags & (SERV_LITERAL_ADDRESS | SERV_LOOP)))
Simon Kelley7de060b2011-08-26 17:24:52 +01001922 continue;
1923
Simon Kelley4f7b3042012-11-28 21:27:02 +00001924 if (last_server->tcpfd == -1)
1925 {
1926 if ((last_server->tcpfd = socket(last_server->addr.sa.sa_family, SOCK_STREAM, 0)) == -1)
1927 continue;
1928
Karl Vogele9828b62014-10-03 21:45:15 +01001929#ifdef HAVE_CONNTRACK
1930 /* Copy connection mark of incoming query to outgoing connection. */
1931 if (option_bool(OPT_CONNTRACK))
1932 {
1933 unsigned int mark;
1934 struct all_addr local;
1935#ifdef HAVE_IPV6
1936 if (local_addr->sa.sa_family == AF_INET6)
1937 local.addr.addr6 = local_addr->in6.sin6_addr;
1938 else
1939#endif
1940 local.addr.addr4 = local_addr->in.sin_addr;
1941
1942 if (get_incoming_mark(&peer_addr, &local, 1, &mark))
1943 setsockopt(last_server->tcpfd, SOL_SOCKET, SO_MARK, &mark, sizeof(unsigned int));
1944 }
1945#endif
1946
Simon Kelley4f7b3042012-11-28 21:27:02 +00001947 if ((!local_bind(last_server->tcpfd, &last_server->source_addr, last_server->interface, 1) ||
1948 connect(last_server->tcpfd, &last_server->addr.sa, sa_len(&last_server->addr)) == -1))
1949 {
1950 close(last_server->tcpfd);
1951 last_server->tcpfd = -1;
1952 continue;
1953 }
1954
Simon Kelley7d7b7b32014-01-08 15:53:35 +00001955#ifdef HAVE_DNSSEC
1956 if (option_bool(OPT_DNSSEC_VALID))
1957 {
Simon Kelley613ad152014-02-25 23:02:28 +00001958 size_t new_size = add_do_bit(header, size, ((char *) header) + 65536);
1959
Simon Kelley2ecd9bd2014-02-13 16:42:02 +00001960 /* For debugging, set Checking Disabled, otherwise, have the upstream check too,
1961 this allows it to select auth servers when one is returning bad data. */
1962 if (option_bool(OPT_DNSSEC_DEBUG))
1963 header->hb4 |= HB4_CD;
Simon Kelley613ad152014-02-25 23:02:28 +00001964
1965 if (size != new_size)
1966 added_pheader = 1;
1967
1968 size = new_size;
Simon Kelley7d7b7b32014-01-08 15:53:35 +00001969 }
1970#endif
Simon Kelley4f7b3042012-11-28 21:27:02 +00001971 }
1972
Simon Kelley4b5ea122013-04-22 10:18:26 +01001973 *length = htons(size);
Simon Kelley1fc02682014-04-29 12:30:18 +01001974
1975 /* get query name again for logging - may have been overwritten */
1976 if (!(gotname = extract_request(header, (unsigned int)size, daemon->namebuff, &qtype)))
1977 strcpy(daemon->namebuff, "query");
Simon Kelley4f7b3042012-11-28 21:27:02 +00001978
Simon Kelley4b5ea122013-04-22 10:18:26 +01001979 if (!read_write(last_server->tcpfd, packet, size + sizeof(u16), 0) ||
Simon Kelley4f7b3042012-11-28 21:27:02 +00001980 !read_write(last_server->tcpfd, &c1, 1, 1) ||
Simon Kelley7d7b7b32014-01-08 15:53:35 +00001981 !read_write(last_server->tcpfd, &c2, 1, 1) ||
1982 !read_write(last_server->tcpfd, payload, (c1 << 8) | c2, 1))
Simon Kelley7de060b2011-08-26 17:24:52 +01001983 {
1984 close(last_server->tcpfd);
1985 last_server->tcpfd = -1;
1986 continue;
Simon Kelley4f7b3042012-11-28 21:27:02 +00001987 }
1988
1989 m = (c1 << 8) | c2;
Simon Kelley4f7b3042012-11-28 21:27:02 +00001990
Simon Kelley4f7b3042012-11-28 21:27:02 +00001991 if (last_server->addr.sa.sa_family == AF_INET)
1992 log_query(F_SERVER | F_IPV4 | F_FORWARD, daemon->namebuff,
1993 (struct all_addr *)&last_server->addr.in.sin_addr, NULL);
Simon Kelleyfeba5c12004-07-27 20:28:58 +01001994#ifdef HAVE_IPV6
Simon Kelley4f7b3042012-11-28 21:27:02 +00001995 else
1996 log_query(F_SERVER | F_IPV6 | F_FORWARD, daemon->namebuff,
1997 (struct all_addr *)&last_server->addr.in6.sin6_addr, NULL);
Simon Kelleyfeba5c12004-07-27 20:28:58 +01001998#endif
Simon Kelley7d7b7b32014-01-08 15:53:35 +00001999
2000#ifdef HAVE_DNSSEC
2001 if (option_bool(OPT_DNSSEC_VALID) && !checking_disabled)
2002 {
Simon Kelley7fa836e2014-02-10 20:11:24 +00002003 int keycount = DNSSEC_WORK; /* Limit to number of DNSSEC questions, to catch loops and avoid filling cache. */
2004 int status = tcp_key_recurse(now, STAT_TRUNCATED, header, m, 0, daemon->namebuff, daemon->keyname, last_server, &keycount);
Simon Kelley554b5802015-04-17 22:50:20 +01002005 char *result, *domain = "result";
Simon Kelley7d7b7b32014-01-08 15:53:35 +00002006
Simon Kelleyfe3992f2015-04-03 21:25:05 +01002007 if (status == STAT_INSECURE_DS)
2008 {
2009 /* We only cache sigs when we've validated a reply.
2010 Avoid caching a reply with sigs if there's a vaildated break in the
2011 DS chain, so we don't return replies from cache missing sigs. */
2012 status = STAT_INSECURE;
2013 no_cache_dnssec = 1;
2014 }
2015
Simon Kelley7fa836e2014-02-10 20:11:24 +00002016 if (keycount == 0)
Simon Kelley150162b2015-03-27 09:58:26 +00002017 {
2018 result = "ABANDONED";
2019 status = STAT_BOGUS;
2020 }
Simon Kelley7fa836e2014-02-10 20:11:24 +00002021 else
2022 result = (status == STAT_SECURE ? "SECURE" : (status == STAT_INSECURE ? "INSECURE" : "BOGUS"));
Simon Kelleye66b4df2015-04-28 20:45:57 +01002023
2024 if (status == STAT_BOGUS && extract_request(header, m, daemon->namebuff, NULL))
2025 domain = daemon->namebuff;
Simon Kelley554b5802015-04-17 22:50:20 +01002026
2027 log_query(F_KEYTAG | F_SECSTAT, domain, NULL, result);
Simon Kelley7fa836e2014-02-10 20:11:24 +00002028
Simon Kelley7d7b7b32014-01-08 15:53:35 +00002029 if (status == STAT_BOGUS)
Simon Kelleyfe3992f2015-04-03 21:25:05 +01002030 {
2031 no_cache_dnssec = 1;
2032 bogusanswer = 1;
2033 }
2034
Simon Kelley7d7b7b32014-01-08 15:53:35 +00002035 if (status == STAT_SECURE)
2036 cache_secure = 1;
2037 }
2038#endif
2039
2040 /* restore CD bit to the value in the query */
2041 if (checking_disabled)
2042 header->hb4 |= HB4_CD;
2043 else
2044 header->hb4 &= ~HB4_CD;
Simon Kelley4f7b3042012-11-28 21:27:02 +00002045
2046 /* There's no point in updating the cache, since this process will exit and
2047 lose the information after a few queries. We make this call for the alias and
2048 bogus-nxdomain side-effects. */
2049 /* If the crc of the question section doesn't match the crc we sent, then
2050 someone might be attempting to insert bogus values into the cache by
2051 sending replies containing questions and bogus answers. */
Simon Kelley8a9be9e2014-01-25 23:17:21 +00002052#ifdef HAVE_DNSSEC
2053 newhash = hash_questions(header, (unsigned int)m, daemon->namebuff);
2054 if (!newhash || memcmp(hash, newhash, HASH_SIZE) != 0)
Simon Kelley703c7ff2014-01-25 23:46:23 +00002055 {
2056 m = 0;
2057 break;
2058 }
Simon Kelley8a9be9e2014-01-25 23:17:21 +00002059#else
2060 if (crc != questions_crc(header, (unsigned int)m, daemon->namebuff))
Simon Kelley703c7ff2014-01-25 23:46:23 +00002061 {
2062 m = 0;
2063 break;
2064 }
Simon Kelley8a9be9e2014-01-25 23:17:21 +00002065#endif
2066
2067 m = process_reply(header, now, last_server, (unsigned int)m,
Simon Kelleye66b4df2015-04-28 20:45:57 +01002068 option_bool(OPT_NO_REBIND) && !norebind, no_cache_dnssec, cache_secure, bogusanswer,
2069 ad_question, do_bit, added_pheader, check_subnet, &peer_addr);
Simon Kelley4f7b3042012-11-28 21:27:02 +00002070
2071 break;
2072 }
Simon Kelleyfeba5c12004-07-27 20:28:58 +01002073 }
Simon Kelley4f7b3042012-11-28 21:27:02 +00002074
2075 /* In case of local answer or no connections made. */
2076 if (m == 0)
2077 m = setup_reply(header, (unsigned int)size, addrp, flags, daemon->local_ttl);
Simon Kelleyfeba5c12004-07-27 20:28:58 +01002078 }
Simon Kelleyfeba5c12004-07-27 20:28:58 +01002079 }
Simon Kelley4f7b3042012-11-28 21:27:02 +00002080
Simon Kelley5aabfc72007-08-29 11:24:47 +01002081 check_log_writer(NULL);
Simon Kelleyfeba5c12004-07-27 20:28:58 +01002082
Simon Kelley4b5ea122013-04-22 10:18:26 +01002083 *length = htons(m);
2084
2085 if (m == 0 || !read_write(confd, packet, m + sizeof(u16), 0))
Simon Kelleyfeba5c12004-07-27 20:28:58 +01002086 return packet;
2087 }
2088}
2089
Simon Kelley16972692006-10-16 20:04:18 +01002090static struct frec *allocate_frec(time_t now)
Simon Kelley9e4abcb2004-01-22 19:47:41 +00002091{
Simon Kelley16972692006-10-16 20:04:18 +01002092 struct frec *f;
Simon Kelley9e4abcb2004-01-22 19:47:41 +00002093
Simon Kelley5aabfc72007-08-29 11:24:47 +01002094 if ((f = (struct frec *)whine_malloc(sizeof(struct frec))))
Simon Kelley9e4abcb2004-01-22 19:47:41 +00002095 {
Simon Kelley1a6bca82008-07-11 11:11:42 +01002096 f->next = daemon->frec_list;
Simon Kelley9e4abcb2004-01-22 19:47:41 +00002097 f->time = now;
Simon Kelley832af0b2007-01-21 20:01:28 +00002098 f->sentto = NULL;
Simon Kelley1a6bca82008-07-11 11:11:42 +01002099 f->rfd4 = NULL;
Simon Kelley28866e92011-02-14 20:19:14 +00002100 f->flags = 0;
Simon Kelley1a6bca82008-07-11 11:11:42 +01002101#ifdef HAVE_IPV6
2102 f->rfd6 = NULL;
2103#endif
Simon Kelley3a237152013-12-12 12:15:50 +00002104#ifdef HAVE_DNSSEC
Simon Kelley97bc7982014-01-31 10:19:52 +00002105 f->dependent = NULL;
Simon Kelley3a237152013-12-12 12:15:50 +00002106 f->blocking_query = NULL;
Simon Kelley4619d942014-01-16 19:53:06 +00002107 f->stash = NULL;
Simon Kelley97e618a2015-01-07 21:55:43 +00002108 f->orig_domain = NULL;
Simon Kelley3a237152013-12-12 12:15:50 +00002109#endif
Simon Kelley1a6bca82008-07-11 11:11:42 +01002110 daemon->frec_list = f;
Simon Kelley9e4abcb2004-01-22 19:47:41 +00002111 }
Simon Kelley16972692006-10-16 20:04:18 +01002112
2113 return f;
2114}
2115
Simon Kelleyb5ea1cc2014-07-29 16:34:14 +01002116struct randfd *allocate_rfd(int family)
Simon Kelley1a6bca82008-07-11 11:11:42 +01002117{
2118 static int finger = 0;
2119 int i;
2120
2121 /* limit the number of sockets we have open to avoid starvation of
2122 (eg) TFTP. Once we have a reasonable number, randomness should be OK */
2123
2124 for (i = 0; i < RANDOM_SOCKS; i++)
Simon Kelley9009d742008-11-14 20:04:27 +00002125 if (daemon->randomsocks[i].refcount == 0)
Simon Kelley1a6bca82008-07-11 11:11:42 +01002126 {
Simon Kelley9009d742008-11-14 20:04:27 +00002127 if ((daemon->randomsocks[i].fd = random_sock(family)) == -1)
2128 break;
2129
Simon Kelley1a6bca82008-07-11 11:11:42 +01002130 daemon->randomsocks[i].refcount = 1;
2131 daemon->randomsocks[i].family = family;
2132 return &daemon->randomsocks[i];
2133 }
2134
Simon Kelley9009d742008-11-14 20:04:27 +00002135 /* No free ones or cannot get new socket, grab an existing one */
Simon Kelley1a6bca82008-07-11 11:11:42 +01002136 for (i = 0; i < RANDOM_SOCKS; i++)
2137 {
2138 int j = (i+finger) % RANDOM_SOCKS;
Simon Kelley9009d742008-11-14 20:04:27 +00002139 if (daemon->randomsocks[j].refcount != 0 &&
2140 daemon->randomsocks[j].family == family &&
2141 daemon->randomsocks[j].refcount != 0xffff)
Simon Kelley1a6bca82008-07-11 11:11:42 +01002142 {
2143 finger = j;
2144 daemon->randomsocks[j].refcount++;
2145 return &daemon->randomsocks[j];
2146 }
2147 }
2148
2149 return NULL; /* doom */
2150}
Simon Kelleyb5ea1cc2014-07-29 16:34:14 +01002151
2152void free_rfd(struct randfd *rfd)
2153{
2154 if (rfd && --(rfd->refcount) == 0)
2155 close(rfd->fd);
2156}
2157
Simon Kelley1a6bca82008-07-11 11:11:42 +01002158static void free_frec(struct frec *f)
2159{
Simon Kelleyb5ea1cc2014-07-29 16:34:14 +01002160 free_rfd(f->rfd4);
Simon Kelley1a6bca82008-07-11 11:11:42 +01002161 f->rfd4 = NULL;
2162 f->sentto = NULL;
Simon Kelley28866e92011-02-14 20:19:14 +00002163 f->flags = 0;
Simon Kelley1a6bca82008-07-11 11:11:42 +01002164
2165#ifdef HAVE_IPV6
Simon Kelleyb5ea1cc2014-07-29 16:34:14 +01002166 free_rfd(f->rfd6);
Simon Kelley1a6bca82008-07-11 11:11:42 +01002167 f->rfd6 = NULL;
2168#endif
Simon Kelley3a237152013-12-12 12:15:50 +00002169
2170#ifdef HAVE_DNSSEC
2171 if (f->stash)
Simon Kelley0fc2f312014-01-08 10:26:58 +00002172 {
2173 blockdata_free(f->stash);
2174 f->stash = NULL;
2175 }
Simon Kelley3a237152013-12-12 12:15:50 +00002176
Simon Kelley97e618a2015-01-07 21:55:43 +00002177 if (f->orig_domain)
2178 {
2179 blockdata_free(f->orig_domain);
2180 f->orig_domain = NULL;
2181 }
2182
Simon Kelley3a237152013-12-12 12:15:50 +00002183 /* Anything we're waiting on is pointless now, too */
2184 if (f->blocking_query)
2185 free_frec(f->blocking_query);
2186 f->blocking_query = NULL;
Simon Kelley39048ad2014-01-21 17:33:58 +00002187 f->dependent = NULL;
Simon Kelley3a237152013-12-12 12:15:50 +00002188#endif
Simon Kelley1a6bca82008-07-11 11:11:42 +01002189}
2190
Simon Kelley16972692006-10-16 20:04:18 +01002191/* if wait==NULL return a free or older than TIMEOUT record.
2192 else return *wait zero if one available, or *wait is delay to
Simon Kelley1a6bca82008-07-11 11:11:42 +01002193 when the oldest in-use record will expire. Impose an absolute
Simon Kelley3a237152013-12-12 12:15:50 +00002194 limit of 4*TIMEOUT before we wipe things (for random sockets).
2195 If force is set, always return a result, even if we have
2196 to allocate above the limit. */
2197struct frec *get_new_frec(time_t now, int *wait, int force)
Simon Kelley16972692006-10-16 20:04:18 +01002198{
Simon Kelley1a6bca82008-07-11 11:11:42 +01002199 struct frec *f, *oldest, *target;
Simon Kelley16972692006-10-16 20:04:18 +01002200 int count;
2201
2202 if (wait)
2203 *wait = 0;
2204
Simon Kelley1a6bca82008-07-11 11:11:42 +01002205 for (f = daemon->frec_list, oldest = NULL, target = NULL, count = 0; f; f = f->next, count++)
Simon Kelley832af0b2007-01-21 20:01:28 +00002206 if (!f->sentto)
Simon Kelley1a6bca82008-07-11 11:11:42 +01002207 target = f;
2208 else
Simon Kelley16972692006-10-16 20:04:18 +01002209 {
Simon Kelley1a6bca82008-07-11 11:11:42 +01002210 if (difftime(now, f->time) >= 4*TIMEOUT)
2211 {
2212 free_frec(f);
2213 target = f;
2214 }
2215
2216 if (!oldest || difftime(f->time, oldest->time) <= 0)
2217 oldest = f;
Simon Kelley16972692006-10-16 20:04:18 +01002218 }
Simon Kelley1a6bca82008-07-11 11:11:42 +01002219
2220 if (target)
2221 {
2222 target->time = now;
2223 return target;
2224 }
Simon Kelley16972692006-10-16 20:04:18 +01002225
2226 /* can't find empty one, use oldest if there is one
2227 and it's older than timeout */
2228 if (oldest && ((int)difftime(now, oldest->time)) >= TIMEOUT)
2229 {
2230 /* keep stuff for twice timeout if we can by allocating a new
2231 record instead */
2232 if (difftime(now, oldest->time) < 2*TIMEOUT &&
2233 count <= daemon->ftabsize &&
2234 (f = allocate_frec(now)))
2235 return f;
2236
2237 if (!wait)
2238 {
Simon Kelley1a6bca82008-07-11 11:11:42 +01002239 free_frec(oldest);
Simon Kelley16972692006-10-16 20:04:18 +01002240 oldest->time = now;
2241 }
2242 return oldest;
2243 }
2244
2245 /* none available, calculate time 'till oldest record expires */
Simon Kelley3a237152013-12-12 12:15:50 +00002246 if (!force && count > daemon->ftabsize)
Simon Kelley16972692006-10-16 20:04:18 +01002247 {
Marcelo Salhab Brogliato0da5e892013-05-31 11:49:06 +01002248 static time_t last_log = 0;
2249
Simon Kelley16972692006-10-16 20:04:18 +01002250 if (oldest && wait)
2251 *wait = oldest->time + (time_t)TIMEOUT - now;
Marcelo Salhab Brogliato0da5e892013-05-31 11:49:06 +01002252
2253 if ((int)difftime(now, last_log) > 5)
2254 {
2255 last_log = now;
2256 my_syslog(LOG_WARNING, _("Maximum number of concurrent DNS queries reached (max: %d)"), daemon->ftabsize);
2257 }
2258
Simon Kelley16972692006-10-16 20:04:18 +01002259 return NULL;
2260 }
2261
2262 if (!(f = allocate_frec(now)) && wait)
2263 /* wait one second on malloc failure */
2264 *wait = 1;
2265
Simon Kelley9e4abcb2004-01-22 19:47:41 +00002266 return f; /* OK if malloc fails and this is NULL */
2267}
2268
Simon Kelley832af0b2007-01-21 20:01:28 +00002269/* crc is all-ones if not known. */
Simon Kelley8a9be9e2014-01-25 23:17:21 +00002270static struct frec *lookup_frec(unsigned short id, void *hash)
Simon Kelley9e4abcb2004-01-22 19:47:41 +00002271{
2272 struct frec *f;
2273
Simon Kelley1a6bca82008-07-11 11:11:42 +01002274 for(f = daemon->frec_list; f; f = f->next)
Simon Kelley832af0b2007-01-21 20:01:28 +00002275 if (f->sentto && f->new_id == id &&
Simon Kelley8a9be9e2014-01-25 23:17:21 +00002276 (!hash || memcmp(hash, f->hash, HASH_SIZE) == 0))
Simon Kelley9e4abcb2004-01-22 19:47:41 +00002277 return f;
2278
2279 return NULL;
2280}
2281
2282static struct frec *lookup_frec_by_sender(unsigned short id,
Simon Kelleyfd9fa482004-10-21 20:24:00 +01002283 union mysockaddr *addr,
Simon Kelley8a9be9e2014-01-25 23:17:21 +00002284 void *hash)
Simon Kelley9e4abcb2004-01-22 19:47:41 +00002285{
Simon Kelleyfeba5c12004-07-27 20:28:58 +01002286 struct frec *f;
2287
Simon Kelley1a6bca82008-07-11 11:11:42 +01002288 for(f = daemon->frec_list; f; f = f->next)
Simon Kelley832af0b2007-01-21 20:01:28 +00002289 if (f->sentto &&
Simon Kelley9e4abcb2004-01-22 19:47:41 +00002290 f->orig_id == id &&
Simon Kelley8a9be9e2014-01-25 23:17:21 +00002291 memcmp(hash, f->hash, HASH_SIZE) == 0 &&
Simon Kelley9e4abcb2004-01-22 19:47:41 +00002292 sockaddr_isequal(&f->source, addr))
2293 return f;
2294
2295 return NULL;
2296}
Simon Kelley47a95162014-07-08 22:22:02 +01002297
2298/* Send query packet again, if we can. */
2299void resend_query()
2300{
2301 if (daemon->srv_save)
2302 {
2303 int fd;
2304
2305 if (daemon->srv_save->sfd)
2306 fd = daemon->srv_save->sfd->fd;
2307 else if (daemon->rfd_save && daemon->rfd_save->refcount != 0)
2308 fd = daemon->rfd_save->fd;
2309 else
2310 return;
2311
Simon Kelleyff841eb2015-03-11 21:36:30 +00002312 while(retry_send(sendto(fd, daemon->packet, daemon->packet_len, 0,
2313 &daemon->srv_save->addr.sa,
2314 sa_len(&daemon->srv_save->addr))));
Simon Kelley47a95162014-07-08 22:22:02 +01002315 }
2316}
Simon Kelley9e4abcb2004-01-22 19:47:41 +00002317
Simon Kelley849a8352006-06-09 21:02:31 +01002318/* A server record is going away, remove references to it */
Simon Kelley5aabfc72007-08-29 11:24:47 +01002319void server_gone(struct server *server)
Simon Kelley849a8352006-06-09 21:02:31 +01002320{
2321 struct frec *f;
2322
Simon Kelley1a6bca82008-07-11 11:11:42 +01002323 for (f = daemon->frec_list; f; f = f->next)
Simon Kelley832af0b2007-01-21 20:01:28 +00002324 if (f->sentto && f->sentto == server)
Simon Kelley1a6bca82008-07-11 11:11:42 +01002325 free_frec(f);
Simon Kelley849a8352006-06-09 21:02:31 +01002326
2327 if (daemon->last_server == server)
2328 daemon->last_server = NULL;
2329
2330 if (daemon->srv_save == server)
2331 daemon->srv_save = NULL;
2332}
Simon Kelley9e4abcb2004-01-22 19:47:41 +00002333
Simon Kelley316e2732010-01-22 20:16:09 +00002334/* return unique random ids. */
Simon Kelley8a9be9e2014-01-25 23:17:21 +00002335static unsigned short get_id(void)
Simon Kelley9e4abcb2004-01-22 19:47:41 +00002336{
2337 unsigned short ret = 0;
Simon Kelley832af0b2007-01-21 20:01:28 +00002338
Simon Kelley316e2732010-01-22 20:16:09 +00002339 do
Simon Kelley832af0b2007-01-21 20:01:28 +00002340 ret = rand16();
Simon Kelley8a9be9e2014-01-25 23:17:21 +00002341 while (lookup_frec(ret, NULL));
Simon Kelley832af0b2007-01-21 20:01:28 +00002342
Simon Kelley9e4abcb2004-01-22 19:47:41 +00002343 return ret;
2344}
2345
2346
2347
2348
2349