blob: f254fe1bbaef2a8495e2cfd990e49c4c397cf91a [file] [log] [blame]
Simon Kelley61744352013-01-31 14:34:40 +00001/* dnsmasq is Copyright (c) 2000-2013 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 Kelley832af0b2007-01-21 20:01:28 +000019static struct frec *lookup_frec(unsigned short id, unsigned int crc);
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,
22 unsigned int crc);
Simon Kelley316e2732010-01-22 20:16:09 +000023static unsigned short get_id(unsigned int crc);
Simon Kelley1a6bca82008-07-11 11:11:42 +010024static void free_frec(struct frec *f);
25static struct randfd *allocate_rfd(int family);
Simon Kelley9e4abcb2004-01-22 19:47:41 +000026
Simon Kelley824af852008-02-12 20:43:05 +000027/* Send a UDP packet with its source address set as "source"
Simon Kelley44a2a312004-03-10 20:04:35 +000028 unless nowild is true, when we just send it with the kernel default */
Simon Kelley29689cf2012-03-22 14:01:00 +000029int send_from(int fd, int nowild, char *packet, size_t len,
30 union mysockaddr *to, struct all_addr *source,
Simon Kelley50303b12012-04-04 22:13:17 +010031 unsigned int iface)
Simon Kelley9e4abcb2004-01-22 19:47:41 +000032{
Simon Kelley44a2a312004-03-10 20:04:35 +000033 struct msghdr msg;
34 struct iovec iov[1];
Simon Kelley44a2a312004-03-10 20:04:35 +000035 union {
36 struct cmsghdr align; /* this ensures alignment */
Simon Kelley5e9e0ef2006-04-17 14:24:29 +010037#if defined(HAVE_LINUX_NETWORK)
Simon Kelley44a2a312004-03-10 20:04:35 +000038 char control[CMSG_SPACE(sizeof(struct in_pktinfo))];
39#elif defined(IP_SENDSRCADDR)
40 char control[CMSG_SPACE(sizeof(struct in_addr))];
41#endif
42#ifdef HAVE_IPV6
43 char control6[CMSG_SPACE(sizeof(struct in6_pktinfo))];
44#endif
45 } control_u;
Simon Kelleyfeba5c12004-07-27 20:28:58 +010046
Simon Kelley44a2a312004-03-10 20:04:35 +000047 iov[0].iov_base = packet;
48 iov[0].iov_len = len;
49
Simon Kelleyfeba5c12004-07-27 20:28:58 +010050 msg.msg_control = NULL;
51 msg.msg_controllen = 0;
Simon Kelley44a2a312004-03-10 20:04:35 +000052 msg.msg_flags = 0;
53 msg.msg_name = to;
54 msg.msg_namelen = sa_len(to);
55 msg.msg_iov = iov;
56 msg.msg_iovlen = 1;
Simon Kelleyfeba5c12004-07-27 20:28:58 +010057
Simon Kelley26128d22004-11-14 16:43:54 +000058 if (!nowild)
Simon Kelleyfeba5c12004-07-27 20:28:58 +010059 {
Simon Kelley26128d22004-11-14 16:43:54 +000060 struct cmsghdr *cmptr;
Simon Kelleyfeba5c12004-07-27 20:28:58 +010061 msg.msg_control = &control_u;
62 msg.msg_controllen = sizeof(control_u);
Simon Kelley26128d22004-11-14 16:43:54 +000063 cmptr = CMSG_FIRSTHDR(&msg);
Simon Kelley44a2a312004-03-10 20:04:35 +000064
Simon Kelley26128d22004-11-14 16:43:54 +000065 if (to->sa.sa_family == AF_INET)
66 {
Simon Kelley5e9e0ef2006-04-17 14:24:29 +010067#if defined(HAVE_LINUX_NETWORK)
Simon Kelley8ef5ada2010-06-03 19:42:45 +010068 struct in_pktinfo p;
69 p.ipi_ifindex = 0;
70 p.ipi_spec_dst = source->addr.addr4;
71 memcpy(CMSG_DATA(cmptr), &p, sizeof(p));
Simon Kelley26128d22004-11-14 16:43:54 +000072 msg.msg_controllen = cmptr->cmsg_len = CMSG_LEN(sizeof(struct in_pktinfo));
Simon Kelleyc72daea2012-01-05 21:33:27 +000073 cmptr->cmsg_level = IPPROTO_IP;
Simon Kelley26128d22004-11-14 16:43:54 +000074 cmptr->cmsg_type = IP_PKTINFO;
75#elif defined(IP_SENDSRCADDR)
Simon Kelley8ef5ada2010-06-03 19:42:45 +010076 memcpy(CMSG_DATA(cmptr), &(source->addr.addr4), sizeof(source->addr.addr4));
Simon Kelley26128d22004-11-14 16:43:54 +000077 msg.msg_controllen = cmptr->cmsg_len = CMSG_LEN(sizeof(struct in_addr));
78 cmptr->cmsg_level = IPPROTO_IP;
79 cmptr->cmsg_type = IP_SENDSRCADDR;
Simon Kelley44a2a312004-03-10 20:04:35 +000080#endif
Simon Kelley26128d22004-11-14 16:43:54 +000081 }
Simon Kelley26128d22004-11-14 16:43:54 +000082 else
Simon Kelleyb8187c82005-11-26 21:46:27 +000083#ifdef HAVE_IPV6
Simon Kelley26128d22004-11-14 16:43:54 +000084 {
Simon Kelley8ef5ada2010-06-03 19:42:45 +010085 struct in6_pktinfo p;
86 p.ipi6_ifindex = iface; /* Need iface for IPv6 to handle link-local addrs */
87 p.ipi6_addr = source->addr.addr6;
88 memcpy(CMSG_DATA(cmptr), &p, sizeof(p));
Simon Kelley26128d22004-11-14 16:43:54 +000089 msg.msg_controllen = cmptr->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
Simon Kelley316e2732010-01-22 20:16:09 +000090 cmptr->cmsg_type = daemon->v6pktinfo;
Simon Kelleyc72daea2012-01-05 21:33:27 +000091 cmptr->cmsg_level = IPPROTO_IPV6;
Simon Kelley26128d22004-11-14 16:43:54 +000092 }
Simon Kelley3d8df262005-08-29 12:19:27 +010093#else
Simon Kelleyc72daea2012-01-05 21:33:27 +000094 (void)iface; /* eliminate warning */
Simon Kelley26128d22004-11-14 16:43:54 +000095#endif
96 }
Simon Kelleyfeba5c12004-07-27 20:28:58 +010097
Simon Kelley29d28dd2012-12-03 14:05:59 +000098 while (sendmsg(fd, &msg, 0) == -1)
Simon Kelleyfeba5c12004-07-27 20:28:58 +010099 {
Simon Kelleyfd9fa482004-10-21 20:24:00 +0100100 if (retry_send())
Simon Kelley29d28dd2012-12-03 14:05:59 +0000101 continue;
102
103 /* If interface is still in DAD, EINVAL results - ignore that. */
104 if (errno == EINVAL)
105 break;
Simon Kelley22d904d2012-02-26 20:13:45 +0000106
Simon Kelley50303b12012-04-04 22:13:17 +0100107 my_syslog(LOG_ERR, _("failed to send packet: %s"), strerror(errno));
Simon Kelley29689cf2012-03-22 14:01:00 +0000108 return 0;
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100109 }
Simon Kelley29d28dd2012-12-03 14:05:59 +0000110
Simon Kelley29689cf2012-03-22 14:01:00 +0000111 return 1;
Simon Kelley44a2a312004-03-10 20:04:35 +0000112}
113
Simon Kelley28866e92011-02-14 20:19:14 +0000114static unsigned int search_servers(time_t now, struct all_addr **addrpp,
115 unsigned int qtype, char *qdomain, int *type, char **domain, int *norebind)
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100116
117{
118 /* If the query ends in the domain in one of our servers, set
119 domain to point to that name. We find the largest match to allow both
120 domain.org and sub.domain.org to exist. */
121
122 unsigned int namelen = strlen(qdomain);
123 unsigned int matchlen = 0;
124 struct server *serv;
Simon Kelley28866e92011-02-14 20:19:14 +0000125 unsigned int flags = 0;
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100126
Simon Kelley3be34542004-09-11 19:12:13 +0100127 for (serv = daemon->servers; serv; serv=serv->next)
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100128 /* domain matches take priority over NODOTS matches */
Simon Kelley3d8df262005-08-29 12:19:27 +0100129 if ((serv->flags & SERV_FOR_NODOTS) && *type != SERV_HAS_DOMAIN && !strchr(qdomain, '.') && namelen != 0)
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100130 {
Simon Kelley28866e92011-02-14 20:19:14 +0000131 unsigned int sflag = serv->addr.sa.sa_family == AF_INET ? F_IPV4 : F_IPV6;
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100132 *type = SERV_FOR_NODOTS;
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100133 if (serv->flags & SERV_NO_ADDR)
Simon Kelley36717ee2004-09-20 19:20:58 +0100134 flags = F_NXDOMAIN;
135 else if (serv->flags & SERV_LITERAL_ADDRESS)
136 {
137 if (sflag & qtype)
138 {
139 flags = sflag;
140 if (serv->addr.sa.sa_family == AF_INET)
141 *addrpp = (struct all_addr *)&serv->addr.in.sin_addr;
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100142#ifdef HAVE_IPV6
Simon Kelley36717ee2004-09-20 19:20:58 +0100143 else
144 *addrpp = (struct all_addr *)&serv->addr.in6.sin6_addr;
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100145#endif
Simon Kelley36717ee2004-09-20 19:20:58 +0100146 }
Simon Kelley824af852008-02-12 20:43:05 +0000147 else if (!flags || (flags & F_NXDOMAIN))
Simon Kelley36717ee2004-09-20 19:20:58 +0100148 flags = F_NOERR;
149 }
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100150 }
151 else if (serv->flags & SERV_HAS_DOMAIN)
152 {
153 unsigned int domainlen = strlen(serv->domain);
Simon Kelleyb8187c82005-11-26 21:46:27 +0000154 char *matchstart = qdomain + namelen - domainlen;
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100155 if (namelen >= domainlen &&
Simon Kelleyb8187c82005-11-26 21:46:27 +0000156 hostname_isequal(matchstart, serv->domain) &&
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100157 (domainlen == 0 || namelen == domainlen || *(matchstart-1) == '.' ))
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100158 {
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100159 if (serv->flags & SERV_NO_REBIND)
160 *norebind = 1;
Simon Kelley28866e92011-02-14 20:19:14 +0000161 else
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100162 {
Simon Kelley28866e92011-02-14 20:19:14 +0000163 unsigned int sflag = serv->addr.sa.sa_family == AF_INET ? F_IPV4 : F_IPV6;
164 /* implement priority rules for --address and --server for same domain.
165 --address wins if the address is for the correct AF
166 --server wins otherwise. */
167 if (domainlen != 0 && domainlen == matchlen)
Simon Kelley36717ee2004-09-20 19:20:58 +0100168 {
Simon Kelley28866e92011-02-14 20:19:14 +0000169 if ((serv->flags & SERV_LITERAL_ADDRESS))
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100170 {
Simon Kelley28866e92011-02-14 20:19:14 +0000171 if (!(sflag & qtype) && flags == 0)
172 continue;
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100173 }
Simon Kelley28866e92011-02-14 20:19:14 +0000174 else
175 {
176 if (flags & (F_IPV4 | F_IPV6))
177 continue;
178 }
Simon Kelley36717ee2004-09-20 19:20:58 +0100179 }
Simon Kelley28866e92011-02-14 20:19:14 +0000180
181 if (domainlen >= matchlen)
182 {
183 *type = serv->flags & (SERV_HAS_DOMAIN | SERV_USE_RESOLV | SERV_NO_REBIND);
184 *domain = serv->domain;
185 matchlen = domainlen;
186 if (serv->flags & SERV_NO_ADDR)
187 flags = F_NXDOMAIN;
188 else if (serv->flags & SERV_LITERAL_ADDRESS)
189 {
190 if (sflag & qtype)
191 {
192 flags = sflag;
193 if (serv->addr.sa.sa_family == AF_INET)
194 *addrpp = (struct all_addr *)&serv->addr.in.sin_addr;
195#ifdef HAVE_IPV6
196 else
197 *addrpp = (struct all_addr *)&serv->addr.in6.sin6_addr;
198#endif
199 }
200 else if (!flags || (flags & F_NXDOMAIN))
201 flags = F_NOERR;
202 }
203 else
204 flags = 0;
205 }
206 }
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100207 }
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100208 }
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100209
Simon Kelley7de060b2011-08-26 17:24:52 +0100210 if (flags == 0 && !(qtype & F_QUERY) &&
Simon Kelley28866e92011-02-14 20:19:14 +0000211 option_bool(OPT_NODOTS_LOCAL) && !strchr(qdomain, '.') && namelen != 0)
Simon Kelley7de060b2011-08-26 17:24:52 +0100212 /* don't forward A or AAAA queries for simple names, except the empty name */
213 flags = F_NOERR;
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100214
Simon Kelley5aabfc72007-08-29 11:24:47 +0100215 if (flags == F_NXDOMAIN && check_for_local_domain(qdomain, now))
Simon Kelleyc1bb8502004-08-11 18:40:17 +0100216 flags = F_NOERR;
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100217
Simon Kelley824af852008-02-12 20:43:05 +0000218 if (flags)
219 {
220 int logflags = 0;
221
222 if (flags == F_NXDOMAIN || flags == F_NOERR)
223 logflags = F_NEG | qtype;
224
Simon Kelley1a6bca82008-07-11 11:11:42 +0100225 log_query(logflags | flags | F_CONFIG | F_FORWARD, qdomain, *addrpp, NULL);
Simon Kelley824af852008-02-12 20:43:05 +0000226 }
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100227 else if ((*type) & SERV_USE_RESOLV)
228 {
229 *type = 0; /* use normal servers for this domain */
230 *domain = NULL;
231 }
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100232 return flags;
233}
Simon Kelley44a2a312004-03-10 20:04:35 +0000234
Simon Kelley824af852008-02-12 20:43:05 +0000235static int forward_query(int udpfd, union mysockaddr *udpaddr,
236 struct all_addr *dst_addr, unsigned int dst_iface,
Simon Kelley572b41e2011-02-18 18:11:18 +0000237 struct dns_header *header, size_t plen, time_t now, struct frec *forward)
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000238{
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000239 char *domain = NULL;
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100240 int type = 0, norebind = 0;
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000241 struct all_addr *addrp = NULL;
Simon Kelleycdeda282006-03-16 20:16:06 +0000242 unsigned int crc = questions_crc(header, plen, daemon->namebuff);
Simon Kelley28866e92011-02-14 20:19:14 +0000243 unsigned int flags = 0;
244 unsigned int gotname = extract_request(header, plen, daemon->namebuff, NULL);
Simon Kelleyde379512004-06-22 20:23:33 +0100245 struct server *start = NULL;
Simon Kelley7de060b2011-08-26 17:24:52 +0100246
Simon Kelley28866e92011-02-14 20:19:14 +0000247 /* RFC 4035: sect 4.6 para 2 */
Simon Kelley572b41e2011-02-18 18:11:18 +0000248 header->hb4 &= ~HB4_AD;
249
Simon Kelley3d8df262005-08-29 12:19:27 +0100250 /* may be no servers available. */
251 if (!daemon->servers)
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000252 forward = NULL;
Simon Kelleyb8187c82005-11-26 21:46:27 +0000253 else if (forward || (forward = lookup_frec_by_sender(ntohs(header->id), udpaddr, crc)))
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000254 {
Simon Kelleyde379512004-06-22 20:23:33 +0100255 /* retry on existing query, send to all available servers */
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000256 domain = forward->sentto->domain;
Simon Kelley824af852008-02-12 20:43:05 +0000257 forward->sentto->failed_queries++;
Simon Kelley28866e92011-02-14 20:19:14 +0000258 if (!option_bool(OPT_ORDER))
Simon Kelleyde379512004-06-22 20:23:33 +0100259 {
Simon Kelley0a852542005-03-23 20:28:59 +0000260 forward->forwardall = 1;
Simon Kelley3be34542004-09-11 19:12:13 +0100261 daemon->last_server = NULL;
Simon Kelleyde379512004-06-22 20:23:33 +0100262 }
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000263 type = forward->sentto->flags & SERV_TYPE;
Simon Kelleyde379512004-06-22 20:23:33 +0100264 if (!(start = forward->sentto->next))
Simon Kelley3be34542004-09-11 19:12:13 +0100265 start = daemon->servers; /* at end of list, recycle */
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000266 header->id = htons(forward->new_id);
267 }
268 else
269 {
270 if (gotname)
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100271 flags = search_servers(now, &addrp, gotname, daemon->namebuff, &type, &domain, &norebind);
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000272
Simon Kelley3a237152013-12-12 12:15:50 +0000273 if (!flags && !(forward = get_new_frec(now, NULL, 0)))
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100274 /* table full - server failure. */
275 flags = F_NEG;
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000276
277 if (forward)
278 {
Simon Kelley0a852542005-03-23 20:28:59 +0000279 forward->source = *udpaddr;
280 forward->dest = *dst_addr;
281 forward->iface = dst_iface;
Simon Kelley0a852542005-03-23 20:28:59 +0000282 forward->orig_id = ntohs(header->id);
Simon Kelley316e2732010-01-22 20:16:09 +0000283 forward->new_id = get_id(crc);
Simon Kelley832af0b2007-01-21 20:01:28 +0000284 forward->fd = udpfd;
Simon Kelley0a852542005-03-23 20:28:59 +0000285 forward->crc = crc;
286 forward->forwardall = 0;
Simon Kelleyed4c0762013-10-08 20:46:34 +0100287 forward->flags = 0;
Simon Kelley28866e92011-02-14 20:19:14 +0000288 if (norebind)
289 forward->flags |= FREC_NOREBIND;
Simon Kelley572b41e2011-02-18 18:11:18 +0000290 if (header->hb4 & HB4_CD)
Simon Kelley28866e92011-02-14 20:19:14 +0000291 forward->flags |= FREC_CHECKING_DISABLED;
Simon Kelley0a852542005-03-23 20:28:59 +0000292
Simon Kelley28866e92011-02-14 20:19:14 +0000293 header->id = htons(forward->new_id);
294
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100295 /* In strict_order mode, always try servers in the order
296 specified in resolv.conf, if a domain is given
297 always try all the available servers,
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000298 otherwise, use the one last known to work. */
299
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100300 if (type == 0)
301 {
Simon Kelley28866e92011-02-14 20:19:14 +0000302 if (option_bool(OPT_ORDER))
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100303 start = daemon->servers;
304 else if (!(start = daemon->last_server) ||
305 daemon->forwardcount++ > FORWARD_TEST ||
306 difftime(now, daemon->forwardtime) > FORWARD_TIME)
307 {
308 start = daemon->servers;
309 forward->forwardall = 1;
310 daemon->forwardcount = 0;
311 daemon->forwardtime = now;
312 }
313 }
314 else
Simon Kelleyde379512004-06-22 20:23:33 +0100315 {
Simon Kelley3be34542004-09-11 19:12:13 +0100316 start = daemon->servers;
Simon Kelley28866e92011-02-14 20:19:14 +0000317 if (!option_bool(OPT_ORDER))
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100318 forward->forwardall = 1;
Simon Kelleyde379512004-06-22 20:23:33 +0100319 }
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000320 }
321 }
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100322
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000323 /* check for send errors here (no route to host)
324 if we fail to send to all nameservers, send back an error
325 packet straight away (helps modem users when offline) */
326
327 if (!flags && forward)
328 {
Simon Kelleyde379512004-06-22 20:23:33 +0100329 struct server *firstsentto = start;
330 int forwarded = 0;
Simon Kelley28866e92011-02-14 20:19:14 +0000331
Giacomo Tazzari797a7af2013-04-22 13:16:37 +0100332 if (option_bool(OPT_ADD_MAC))
333 plen = add_mac(header, plen, ((char *) header) + PACKETSZ, &forward->source);
Simon Kelley28866e92011-02-14 20:19:14 +0000334
Simon Kelleyed4c0762013-10-08 20:46:34 +0100335 if (option_bool(OPT_CLIENT_SUBNET))
336 {
337 size_t new = add_source_addr(header, plen, ((char *) header) + PACKETSZ, &forward->source);
338 if (new != plen)
339 {
340 plen = new;
341 forward->flags |= FREC_HAS_SUBNET;
342 }
343 }
344
Simon Kelley3a237152013-12-12 12:15:50 +0000345#ifdef HAVE_DNSSEC
346 if (option_bool(OPT_DNSSEC_VALID))
Simon Kelley0fc2f312014-01-08 10:26:58 +0000347 {
348 plen = add_do_bit(header, plen, ((char *) header) + PACKETSZ);
349 header->hb4 |= HB4_CD;
350 }
Simon Kelley3a237152013-12-12 12:15:50 +0000351#endif
352
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000353 while (1)
354 {
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000355 /* only send to servers dealing with our domain.
356 domain may be NULL, in which case server->domain
357 must be NULL also. */
358
Simon Kelleyde379512004-06-22 20:23:33 +0100359 if (type == (start->flags & SERV_TYPE) &&
Simon Kelleyfd9fa482004-10-21 20:24:00 +0100360 (type != SERV_HAS_DOMAIN || hostname_isequal(domain, start->domain)) &&
361 !(start->flags & SERV_LITERAL_ADDRESS))
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000362 {
Simon Kelley1a6bca82008-07-11 11:11:42 +0100363 int fd;
364
365 /* find server socket to use, may need to get random one. */
366 if (start->sfd)
367 fd = start->sfd->fd;
368 else
369 {
370#ifdef HAVE_IPV6
371 if (start->addr.sa.sa_family == AF_INET6)
372 {
373 if (!forward->rfd6 &&
374 !(forward->rfd6 = allocate_rfd(AF_INET6)))
375 break;
Simon Kelley3927da42008-07-20 15:10:39 +0100376 daemon->rfd_save = forward->rfd6;
Simon Kelley1a6bca82008-07-11 11:11:42 +0100377 fd = forward->rfd6->fd;
378 }
379 else
380#endif
381 {
382 if (!forward->rfd4 &&
383 !(forward->rfd4 = allocate_rfd(AF_INET)))
384 break;
Simon Kelley3927da42008-07-20 15:10:39 +0100385 daemon->rfd_save = forward->rfd4;
Simon Kelley1a6bca82008-07-11 11:11:42 +0100386 fd = forward->rfd4->fd;
387 }
Simon Kelley7de060b2011-08-26 17:24:52 +0100388
389#ifdef HAVE_CONNTRACK
390 /* Copy connection mark of incoming query to outgoing connection. */
391 if (option_bool(OPT_CONNTRACK))
392 {
393 unsigned int mark;
Giacomo Tazzari797a7af2013-04-22 13:16:37 +0100394 if (get_incoming_mark(&forward->source, &forward->dest, 0, &mark))
Simon Kelley7de060b2011-08-26 17:24:52 +0100395 setsockopt(fd, SOL_SOCKET, SO_MARK, &mark, sizeof(unsigned int));
396 }
397#endif
Simon Kelley1a6bca82008-07-11 11:11:42 +0100398 }
399
400 if (sendto(fd, (char *)header, plen, 0,
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100401 &start->addr.sa,
Simon Kelleyfd9fa482004-10-21 20:24:00 +0100402 sa_len(&start->addr)) == -1)
403 {
404 if (retry_send())
405 continue;
406 }
407 else
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000408 {
Simon Kelleycdeda282006-03-16 20:16:06 +0000409 /* Keep info in case we want to re-send this packet */
410 daemon->srv_save = start;
411 daemon->packet_len = plen;
412
Simon Kelleyde379512004-06-22 20:23:33 +0100413 if (!gotname)
Simon Kelley3be34542004-09-11 19:12:13 +0100414 strcpy(daemon->namebuff, "query");
Simon Kelleyde379512004-06-22 20:23:33 +0100415 if (start->addr.sa.sa_family == AF_INET)
Simon Kelley3be34542004-09-11 19:12:13 +0100416 log_query(F_SERVER | F_IPV4 | F_FORWARD, daemon->namebuff,
Simon Kelley1a6bca82008-07-11 11:11:42 +0100417 (struct all_addr *)&start->addr.in.sin_addr, NULL);
Simon Kelleyde379512004-06-22 20:23:33 +0100418#ifdef HAVE_IPV6
419 else
Simon Kelley3be34542004-09-11 19:12:13 +0100420 log_query(F_SERVER | F_IPV6 | F_FORWARD, daemon->namebuff,
Simon Kelley1a6bca82008-07-11 11:11:42 +0100421 (struct all_addr *)&start->addr.in6.sin6_addr, NULL);
Simon Kelleyde379512004-06-22 20:23:33 +0100422#endif
Simon Kelley824af852008-02-12 20:43:05 +0000423 start->queries++;
Simon Kelleyde379512004-06-22 20:23:33 +0100424 forwarded = 1;
425 forward->sentto = start;
Simon Kelley0a852542005-03-23 20:28:59 +0000426 if (!forward->forwardall)
Simon Kelleyde379512004-06-22 20:23:33 +0100427 break;
Simon Kelley0a852542005-03-23 20:28:59 +0000428 forward->forwardall++;
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000429 }
430 }
431
Simon Kelleyde379512004-06-22 20:23:33 +0100432 if (!(start = start->next))
Simon Kelley3be34542004-09-11 19:12:13 +0100433 start = daemon->servers;
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000434
Simon Kelleyde379512004-06-22 20:23:33 +0100435 if (start == firstsentto)
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000436 break;
437 }
438
Simon Kelleyde379512004-06-22 20:23:33 +0100439 if (forwarded)
Simon Kelley824af852008-02-12 20:43:05 +0000440 return 1;
Simon Kelleyde379512004-06-22 20:23:33 +0100441
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000442 /* could not send on, prepare to return */
443 header->id = htons(forward->orig_id);
Simon Kelley1a6bca82008-07-11 11:11:42 +0100444 free_frec(forward); /* cancel */
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000445 }
446
447 /* could not send on, return empty answer or address if known for whole domain */
Simon Kelleyb8187c82005-11-26 21:46:27 +0000448 if (udpfd != -1)
449 {
Simon Kelleycdeda282006-03-16 20:16:06 +0000450 plen = setup_reply(header, plen, addrp, flags, daemon->local_ttl);
Simon Kelley54dd3932012-06-20 11:23:38 +0100451 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 +0000452 }
453
Simon Kelley824af852008-02-12 20:43:05 +0000454 return 0;
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000455}
456
Simon Kelleyed4c0762013-10-08 20:46:34 +0100457static size_t process_reply(struct dns_header *header, time_t now, struct server *server, size_t n, int check_rebind,
Simon Kelley3a237152013-12-12 12:15:50 +0000458 int no_cache, int cache_secure, int check_subnet, union mysockaddr *query_source)
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100459{
Simon Kelley36717ee2004-09-20 19:20:58 +0100460 unsigned char *pheader, *sizep;
Jason A. Donenfeld13d86c72013-02-22 18:20:53 +0000461 char **sets = 0;
Simon Kelley832af0b2007-01-21 20:01:28 +0000462 int munged = 0, is_sign;
Simon Kelleycdeda282006-03-16 20:16:06 +0000463 size_t plen;
Simon Kelley3a237152013-12-12 12:15:50 +0000464 int squash_ad = 0;
Simon Kelleycdeda282006-03-16 20:16:06 +0000465
Jason A. Donenfeld13d86c72013-02-22 18:20:53 +0000466#ifdef HAVE_IPSET
467 /* Similar algorithm to search_servers. */
468 struct ipsets *ipset_pos;
469 unsigned int namelen = strlen(daemon->namebuff);
470 unsigned int matchlen = 0;
471 for (ipset_pos = daemon->ipsets; ipset_pos; ipset_pos = ipset_pos->next)
472 {
473 unsigned int domainlen = strlen(ipset_pos->domain);
474 char *matchstart = daemon->namebuff + namelen - domainlen;
475 if (namelen >= domainlen && hostname_isequal(matchstart, ipset_pos->domain) &&
476 (domainlen == 0 || namelen == domainlen || *(matchstart - 1) == '.' ) &&
477 domainlen >= matchlen) {
478 matchlen = domainlen;
479 sets = ipset_pos->sets;
480 }
481 }
482#endif
483
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100484 /* If upstream is advertising a larger UDP packet size
Simon Kelley9009d742008-11-14 20:04:27 +0000485 than we allow, trim it so that we don't get overlarge
486 requests for the client. We can't do this for signed packets. */
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100487
Simon Kelleyed4c0762013-10-08 20:46:34 +0100488 if ((pheader = find_pseudoheader(header, n, &plen, &sizep, &is_sign)))
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100489 {
Simon Kelleyed4c0762013-10-08 20:46:34 +0100490 if (!is_sign)
491 {
492 unsigned short udpsz;
493 unsigned char *psave = sizep;
494
495 GETSHORT(udpsz, sizep);
496 if (udpsz > daemon->edns_pktsz)
497 PUTSHORT(daemon->edns_pktsz, psave);
498 }
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100499
Simon Kelleyed4c0762013-10-08 20:46:34 +0100500 if (check_subnet && !check_source(header, plen, pheader, query_source))
501 {
502 my_syslog(LOG_WARNING, _("discarding DNS reply: subnet option mismatch"));
503 return 0;
504 }
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100505 }
Simon Kelleyed4c0762013-10-08 20:46:34 +0100506
Simon Kelley28866e92011-02-14 20:19:14 +0000507 /* RFC 4035 sect 4.6 para 3 */
Giovanni Bajo237724c2012-04-05 02:46:52 +0200508 if (!is_sign && !option_bool(OPT_DNSSEC_PROXY))
Simon Kelley3a237152013-12-12 12:15:50 +0000509 squash_ad = 1;
510
511#ifdef HAVE_DNSSEC
512 if (option_bool(OPT_DNSSEC_VALID))
513 squash_ad = no_cache;
Simon Kelley28866e92011-02-14 20:19:14 +0000514
Simon Kelley3a237152013-12-12 12:15:50 +0000515 if (cache_secure)
516 header->hb4 |= HB4_AD;
517#endif
518
519 if (squash_ad)
520 header->hb4 &= ~HB4_AD;
521
Simon Kelley572b41e2011-02-18 18:11:18 +0000522 if (OPCODE(header) != QUERY || (RCODE(header) != NOERROR && RCODE(header) != NXDOMAIN))
Simon Kelley36717ee2004-09-20 19:20:58 +0100523 return n;
524
Simon Kelley0a852542005-03-23 20:28:59 +0000525 /* Complain loudly if the upstream server is non-recursive. */
Simon Kelley572b41e2011-02-18 18:11:18 +0000526 if (!(header->hb4 & HB4_RA) && RCODE(header) == NOERROR && ntohs(header->ancount) == 0 &&
Simon Kelley0a852542005-03-23 20:28:59 +0000527 server && !(server->flags & SERV_WARNED_RECURSIVE))
528 {
Simon Kelley3d8df262005-08-29 12:19:27 +0100529 prettyprint_addr(&server->addr, daemon->namebuff);
Simon Kelleyf2621c72007-04-29 19:47:21 +0100530 my_syslog(LOG_WARNING, _("nameserver %s refused to do a recursive query"), daemon->namebuff);
Simon Kelley28866e92011-02-14 20:19:14 +0000531 if (!option_bool(OPT_LOG))
Simon Kelley0a852542005-03-23 20:28:59 +0000532 server->flags |= SERV_WARNED_RECURSIVE;
533 }
Giovanni Bajoe292e932012-04-22 14:32:02 +0200534
Simon Kelley572b41e2011-02-18 18:11:18 +0000535 if (daemon->bogus_addr && RCODE(header) != NXDOMAIN &&
Simon Kelleyfd9fa482004-10-21 20:24:00 +0100536 check_for_bogus_wildcard(header, n, daemon->namebuff, daemon->bogus_addr, now))
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100537 {
Simon Kelleyfd9fa482004-10-21 20:24:00 +0100538 munged = 1;
Simon Kelley572b41e2011-02-18 18:11:18 +0000539 SET_RCODE(header, NXDOMAIN);
540 header->hb3 &= ~HB3_AA;
Simon Kelley36717ee2004-09-20 19:20:58 +0100541 }
Simon Kelleyfd9fa482004-10-21 20:24:00 +0100542 else
Simon Kelley36717ee2004-09-20 19:20:58 +0100543 {
Simon Kelley572b41e2011-02-18 18:11:18 +0000544 if (RCODE(header) == NXDOMAIN &&
Simon Kelleyfd9fa482004-10-21 20:24:00 +0100545 extract_request(header, n, daemon->namebuff, NULL) &&
Simon Kelley5aabfc72007-08-29 11:24:47 +0100546 check_for_local_domain(daemon->namebuff, now))
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100547 {
Simon Kelley36717ee2004-09-20 19:20:58 +0100548 /* if we forwarded a query for a locally known name (because it was for
549 an unknown type) and the answer is NXDOMAIN, convert that to NODATA,
550 since we know that the domain exists, even if upstream doesn't */
Simon Kelleyfd9fa482004-10-21 20:24:00 +0100551 munged = 1;
Simon Kelley572b41e2011-02-18 18:11:18 +0000552 header->hb3 |= HB3_AA;
553 SET_RCODE(header, NOERROR);
Simon Kelley36717ee2004-09-20 19:20:58 +0100554 }
Simon Kelley832af0b2007-01-21 20:01:28 +0000555
Simon Kelley0fc2f312014-01-08 10:26:58 +0000556 if (extract_addresses(header, n, daemon->namebuff, now, sets, is_sign, check_rebind, no_cache, cache_secure))
Simon Kelley824af852008-02-12 20:43:05 +0000557 {
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100558 my_syslog(LOG_WARNING, _("possible DNS-rebind attack detected: %s"), daemon->namebuff);
Simon Kelley824af852008-02-12 20:43:05 +0000559 munged = 1;
560 }
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100561 }
Simon Kelleyfd9fa482004-10-21 20:24:00 +0100562
563 /* do this after extract_addresses. Ensure NODATA reply and remove
564 nameserver info. */
565
566 if (munged)
567 {
568 header->ancount = htons(0);
569 header->nscount = htons(0);
570 header->arcount = htons(0);
571 }
572
Simon Kelley36717ee2004-09-20 19:20:58 +0100573 /* the bogus-nxdomain stuff, doctor and NXDOMAIN->NODATA munging can all elide
574 sections of the packet. Find the new length here and put back pseudoheader
575 if it was removed. */
576 return resize_packet(header, n, pheader, plen);
Simon Kelleyfeba5c12004-07-27 20:28:58 +0100577}
578
Simon Kelley3be34542004-09-11 19:12:13 +0100579/* sets new last_server */
Simon Kelley1a6bca82008-07-11 11:11:42 +0100580void reply_query(int fd, int family, time_t now)
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000581{
582 /* packet from peer server, extract data for cache, and send to
583 original requester */
Simon Kelley572b41e2011-02-18 18:11:18 +0000584 struct dns_header *header;
Simon Kelleyde379512004-06-22 20:23:33 +0100585 union mysockaddr serveraddr;
Simon Kelley832af0b2007-01-21 20:01:28 +0000586 struct frec *forward;
Simon Kelleyde379512004-06-22 20:23:33 +0100587 socklen_t addrlen = sizeof(serveraddr);
Simon Kelley1a6bca82008-07-11 11:11:42 +0100588 ssize_t n = recvfrom(fd, daemon->packet, daemon->edns_pktsz, 0, &serveraddr.sa, &addrlen);
Simon Kelleycdeda282006-03-16 20:16:06 +0000589 size_t nn;
Simon Kelley1a6bca82008-07-11 11:11:42 +0100590 struct server *server;
591
Simon Kelleycdeda282006-03-16 20:16:06 +0000592 /* packet buffer overwritten */
593 daemon->srv_save = NULL;
Simon Kelley832af0b2007-01-21 20:01:28 +0000594
Simon Kelleyde379512004-06-22 20:23:33 +0100595 /* Determine the address of the server replying so that we can mark that as good */
Simon Kelley1a6bca82008-07-11 11:11:42 +0100596 serveraddr.sa.sa_family = family;
Simon Kelleyde379512004-06-22 20:23:33 +0100597#ifdef HAVE_IPV6
598 if (serveraddr.sa.sa_family == AF_INET6)
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100599 serveraddr.in6.sin6_flowinfo = 0;
Simon Kelleyde379512004-06-22 20:23:33 +0100600#endif
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000601
Simon Kelley1a6bca82008-07-11 11:11:42 +0100602 /* spoof check: answer must come from known server, */
603 for (server = daemon->servers; server; server = server->next)
604 if (!(server->flags & (SERV_LITERAL_ADDRESS | SERV_NO_ADDR)) &&
605 sockaddr_isequal(&server->addr, &serveraddr))
606 break;
607
Simon Kelley572b41e2011-02-18 18:11:18 +0000608 header = (struct dns_header *)daemon->packet;
Simon Kelleyfd9fa482004-10-21 20:24:00 +0100609
Simon Kelley1a6bca82008-07-11 11:11:42 +0100610 if (!server ||
Simon Kelley572b41e2011-02-18 18:11:18 +0000611 n < (int)sizeof(struct dns_header) || !(header->hb3 & HB3_QR) ||
Simon Kelley1a6bca82008-07-11 11:11:42 +0100612 !(forward = lookup_frec(ntohs(header->id), questions_crc(header, n, daemon->namebuff))))
613 return;
Simon Kelley3a237152013-12-12 12:15:50 +0000614
Simon Kelley572b41e2011-02-18 18:11:18 +0000615 if ((RCODE(header) == SERVFAIL || RCODE(header) == REFUSED) &&
Simon Kelley28866e92011-02-14 20:19:14 +0000616 !option_bool(OPT_ORDER) &&
Simon Kelley1a6bca82008-07-11 11:11:42 +0100617 forward->forwardall == 0)
618 /* for broken servers, attempt to send to another one. */
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000619 {
Simon Kelley1a6bca82008-07-11 11:11:42 +0100620 unsigned char *pheader;
621 size_t plen;
622 int is_sign;
Simon Kelley832af0b2007-01-21 20:01:28 +0000623
Simon Kelley1a6bca82008-07-11 11:11:42 +0100624 /* recreate query from reply */
625 pheader = find_pseudoheader(header, (size_t)n, &plen, NULL, &is_sign);
626 if (!is_sign)
Simon Kelley832af0b2007-01-21 20:01:28 +0000627 {
Simon Kelley1a6bca82008-07-11 11:11:42 +0100628 header->ancount = htons(0);
629 header->nscount = htons(0);
630 header->arcount = htons(0);
631 if ((nn = resize_packet(header, (size_t)n, pheader, plen)))
632 {
Simon Kelley572b41e2011-02-18 18:11:18 +0000633 header->hb3 &= ~(HB3_QR | HB3_TC);
Simon Kelley1a6bca82008-07-11 11:11:42 +0100634 forward_query(-1, NULL, NULL, 0, header, nn, now, forward);
635 return;
636 }
637 }
638 }
Simon Kelley3a237152013-12-12 12:15:50 +0000639
640 server = forward->sentto;
Simon Kelley1a6bca82008-07-11 11:11:42 +0100641
642 if ((forward->sentto->flags & SERV_TYPE) == 0)
643 {
Simon Kelley572b41e2011-02-18 18:11:18 +0000644 if (RCODE(header) == SERVFAIL || RCODE(header) == REFUSED)
Simon Kelley1a6bca82008-07-11 11:11:42 +0100645 server = NULL;
646 else
647 {
648 struct server *last_server;
Simon Kelley832af0b2007-01-21 20:01:28 +0000649
Simon Kelley1a6bca82008-07-11 11:11:42 +0100650 /* find good server by address if possible, otherwise assume the last one we sent to */
651 for (last_server = daemon->servers; last_server; last_server = last_server->next)
652 if (!(last_server->flags & (SERV_LITERAL_ADDRESS | SERV_HAS_DOMAIN | SERV_FOR_NODOTS | SERV_NO_ADDR)) &&
653 sockaddr_isequal(&last_server->addr, &serveraddr))
654 {
655 server = last_server;
656 break;
657 }
658 }
Simon Kelley28866e92011-02-14 20:19:14 +0000659 if (!option_bool(OPT_ALL_SERVERS))
Simon Kelley1a6bca82008-07-11 11:11:42 +0100660 daemon->last_server = server;
661 }
Simon Kelley3a237152013-12-12 12:15:50 +0000662
Simon Kelley1a6bca82008-07-11 11:11:42 +0100663 /* If the answer is an error, keep the forward record in place in case
664 we get a good reply from another server. Kill it when we've
665 had replies from all to avoid filling the forwarding table when
666 everything is broken */
667 if (forward->forwardall == 0 || --forward->forwardall == 1 ||
Simon Kelley572b41e2011-02-18 18:11:18 +0000668 (RCODE(header) != REFUSED && RCODE(header) != SERVFAIL))
Simon Kelley1a6bca82008-07-11 11:11:42 +0100669 {
Simon Kelley3a237152013-12-12 12:15:50 +0000670 int check_rebind = 0, no_cache_dnssec = 0, cache_secure = 0;
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100671
Simon Kelley3a237152013-12-12 12:15:50 +0000672 if (option_bool(OPT_NO_REBIND))
673 check_rebind = !(forward->flags & FREC_NOREBIND);
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100674
Simon Kelley3a237152013-12-12 12:15:50 +0000675 /* Don't cache replies where DNSSEC validation was turned off, either
676 the upstream server told us so, or the original query specified it. */
677 if ((header->hb4 & HB4_CD) || (forward->flags & FREC_CHECKING_DISABLED))
678 no_cache_dnssec = 1;
679
680#ifdef HAVE_DNSSEC
681 if (option_bool(OPT_DNSSEC_VALID) && !(forward->flags & FREC_CHECKING_DISABLED))
682 {
Simon Kelley9d633042013-12-13 15:36:55 +0000683 int status;
Simon Kelley0fc2f312014-01-08 10:26:58 +0000684
685 /* We've had a reply already, which we're validating. Ignore this duplicate */
686 if (forward->stash)
687 return;
Simon Kelley9d633042013-12-13 15:36:55 +0000688
Simon Kelley871417d2014-01-08 11:22:32 +0000689 if (header->hb3 & HB3_TC)
690 {
691 /* Truncated answer can't be validated.
692 The client will retry over TCP, but if this is an answer to a
693 DNSSEC-generated query, we have a problem. Should really re-send
694 over TCP. No-one with any sense will make a DNSKEY or DS RRset
695 exceed 4096, so this may not be a real problem. Just log
696 for now. */
697 if (forward->flags & (FREC_DNSKEY_QUERY | FREC_DS_QUERY))
698 my_syslog(LOG_ERR, _("Reply to DNSSEC query truncated - validation fails."));
699 status = STAT_INSECURE;
700 }
701 else if (forward->flags & FREC_DNSKEY_QUERY)
Simon Kelley0fc2f312014-01-08 10:26:58 +0000702 status = dnssec_validate_by_ds(now, header, n, daemon->namebuff, daemon->keyname, forward->class);
Simon Kelleyc3e0b9b2013-12-31 13:50:39 +0000703 else if (forward->flags & FREC_DS_QUERY)
704 status = dnssec_validate_ds(now, header, n, daemon->namebuff, daemon->keyname, forward->class);
Simon Kelley9d633042013-12-13 15:36:55 +0000705 else
Simon Kelley0fc2f312014-01-08 10:26:58 +0000706 status = dnssec_validate_reply(now, header, n, daemon->namebuff, daemon->keyname, &forward->class);
Simon Kelley3a237152013-12-12 12:15:50 +0000707
708 /* Can't validate, as we're missing key data. Put this
709 answer aside, whilst we get that. */
710 if (status == STAT_NEED_DS || status == STAT_NEED_KEY)
711 {
712 struct frec *new;
Simon Kelley0fc2f312014-01-08 10:26:58 +0000713
714 if ((new = get_new_frec(now, NULL, 1)))
Simon Kelley3a237152013-12-12 12:15:50 +0000715 {
Simon Kelley0fc2f312014-01-08 10:26:58 +0000716 struct frec *next = new->next;
717 *new = *forward; /* copy everything, then overwrite */
718 new->next = next;
719 new->stash = NULL;
720 new->blocking_query = NULL;
721 new->flags &= ~(FREC_DNSKEY_QUERY | FREC_DS_QUERY);
Simon Kelley9d633042013-12-13 15:36:55 +0000722
Simon Kelley0fc2f312014-01-08 10:26:58 +0000723 if ((forward->stash = blockdata_alloc((char *)header, n)))
Simon Kelley3a237152013-12-12 12:15:50 +0000724 {
725 int fd;
Simon Kelley9d633042013-12-13 15:36:55 +0000726
Simon Kelley0fc2f312014-01-08 10:26:58 +0000727 forward->stash_len = n;
728
Simon Kelley3a237152013-12-12 12:15:50 +0000729 new->dependent = forward; /* to find query awaiting new one. */
730 forward->blocking_query = new; /* for garbage cleaning */
Simon Kelley0fc2f312014-01-08 10:26:58 +0000731 /* validate routines leave name of required record in daemon->keyname */
Simon Kelley3a237152013-12-12 12:15:50 +0000732 if (status == STAT_NEED_KEY)
Simon Kelley9d633042013-12-13 15:36:55 +0000733 {
734 new->flags |= FREC_DNSKEY_QUERY;
Simon Kelley0fc2f312014-01-08 10:26:58 +0000735 nn = dnssec_generate_query(header, daemon->keyname, forward->class, T_DNSKEY, &server->addr);
Simon Kelley9d633042013-12-13 15:36:55 +0000736 }
Simon Kelley3a237152013-12-12 12:15:50 +0000737 else if (status == STAT_NEED_DS)
Simon Kelley9d633042013-12-13 15:36:55 +0000738 {
739 new->flags |= FREC_DS_QUERY;
Simon Kelley0fc2f312014-01-08 10:26:58 +0000740 nn = dnssec_generate_query(header, daemon->keyname, forward->class, T_DS, &server->addr);
Simon Kelley9d633042013-12-13 15:36:55 +0000741 }
Simon Kelley3a237152013-12-12 12:15:50 +0000742 new->crc = questions_crc(header, nn, daemon->namebuff);
743 new->new_id = get_id(new->crc);
Simon Kelleyc3e0b9b2013-12-31 13:50:39 +0000744 header->id = htons(new->new_id);
Simon Kelley9d633042013-12-13 15:36:55 +0000745
Simon Kelley3a237152013-12-12 12:15:50 +0000746 /* Don't resend this. */
747 daemon->srv_save = NULL;
748
749 if (server->sfd)
750 fd = server->sfd->fd;
751 else
752#ifdef HAVE_IPV6
Simon Kelley9d633042013-12-13 15:36:55 +0000753 /* Note that we use the same random port for the DNSSEC stuff */
754 if (server->addr.sa.sa_family == AF_INET6)
755 {
756 fd = new->rfd6->fd;
757 new->rfd6->refcount++;
758 }
759 else
Simon Kelley3a237152013-12-12 12:15:50 +0000760#endif
Simon Kelley9d633042013-12-13 15:36:55 +0000761 {
762 fd = new->rfd4->fd;
763 new->rfd4->refcount++;
764 }
765
Simon Kelley3a237152013-12-12 12:15:50 +0000766 /* Send DNSSEC query to same server as original query */
Simon Kelley0fc2f312014-01-08 10:26:58 +0000767 while (sendto(fd, (char *)header, nn, 0, &server->addr.sa, sa_len(&server->addr)) == -1 && retry_send());
768 server->queries++;
Simon Kelley3a237152013-12-12 12:15:50 +0000769 }
770 }
Simon Kelley0fc2f312014-01-08 10:26:58 +0000771
Simon Kelley3a237152013-12-12 12:15:50 +0000772 return;
773 }
774
775 /* Ok, we reached far enough up the chain-of-trust that we can validate something.
776 Now wind back down, pulling back answers which wouldn't previously validate
777 and validate them with the new data. Failure to find needed data here is an internal error.
778 Once we get to the original answer (FREC_DNSSEC_QUERY not set) and it validates,
779 return it to the original requestor. */
Simon Kelley0fc2f312014-01-08 10:26:58 +0000780 if (forward->flags & (FREC_DNSKEY_QUERY | FREC_DS_QUERY))
Simon Kelley3a237152013-12-12 12:15:50 +0000781 {
Simon Kelley0fc2f312014-01-08 10:26:58 +0000782 while (forward->dependent)
Simon Kelley3a237152013-12-12 12:15:50 +0000783 {
Simon Kelley0fc2f312014-01-08 10:26:58 +0000784 struct frec *prev;
Simon Kelleyc3e0b9b2013-12-31 13:50:39 +0000785
Simon Kelley0fc2f312014-01-08 10:26:58 +0000786 if (status == STAT_SECURE)
787 {
788 if (forward->flags & FREC_DNSKEY_QUERY)
789 status = dnssec_validate_by_ds(now, header, n, daemon->namebuff, daemon->keyname, forward->class);
790 else if (forward->flags & FREC_DS_QUERY)
791 status = dnssec_validate_ds(now, header, n, daemon->namebuff, daemon->keyname, forward->class);
792 }
793
794 prev = forward->dependent;
795 free_frec(forward);
796 forward = prev;
797 forward->blocking_query = NULL; /* already gone */
798 blockdata_retrieve(forward->stash, forward->stash_len, (void *)header);
799 n = forward->stash_len;
800 }
801
802 /* All DNSKEY and DS records done and in cache, now finally validate original
803 answer, provided last DNSKEY is OK. */
804 if (status == STAT_SECURE)
805 status = dnssec_validate_reply(now, header, n, daemon->namebuff, daemon->keyname, &forward->class);
806
807 if (status == STAT_NEED_DS || status == STAT_NEED_KEY)
808 {
809 my_syslog(LOG_ERR, _("Unexpected missing data for DNSSEC validation"));
810 status = STAT_INSECURE;
Simon Kelley3a237152013-12-12 12:15:50 +0000811 }
812 }
Simon Kelley0fc2f312014-01-08 10:26:58 +0000813
814 log_query(F_KEYTAG | F_SECSTAT, "result", NULL,
815 status == STAT_SECURE ? "SECURE" : (status == STAT_INSECURE ? "INSECURE" : "BOGUS"));
816
817 no_cache_dnssec = 0;
Simon Kelleyc3e0b9b2013-12-31 13:50:39 +0000818
Simon Kelley3a237152013-12-12 12:15:50 +0000819 if (status == STAT_SECURE)
820 cache_secure = 1;
821 /* TODO return SERVFAIL here */
822 else if (status == STAT_BOGUS)
823 no_cache_dnssec = 1;
Simon Kelley0fc2f312014-01-08 10:26:58 +0000824
825 /* restore CD bit to the value in the query */
826 if (forward->flags & FREC_CHECKING_DISABLED)
827 header->hb4 |= HB4_CD;
828 else
829 header->hb4 &= ~HB4_CD;
Simon Kelley3a237152013-12-12 12:15:50 +0000830 }
831#endif
832
833 if ((nn = process_reply(header, now, server, (size_t)n, check_rebind, no_cache_dnssec, cache_secure,
Simon Kelleyed4c0762013-10-08 20:46:34 +0100834 forward->flags & FREC_HAS_SUBNET, &forward->source)))
Simon Kelley832af0b2007-01-21 20:01:28 +0000835 {
Simon Kelley1a6bca82008-07-11 11:11:42 +0100836 header->id = htons(forward->orig_id);
Simon Kelley572b41e2011-02-18 18:11:18 +0000837 header->hb4 |= HB4_RA; /* recursion if available */
Simon Kelley54dd3932012-06-20 11:23:38 +0100838 send_from(forward->fd, option_bool(OPT_NOWILD) || option_bool (OPT_CLEVERBIND), daemon->packet, nn,
Simon Kelley50303b12012-04-04 22:13:17 +0100839 &forward->source, &forward->dest, forward->iface);
Simon Kelley832af0b2007-01-21 20:01:28 +0000840 }
Simon Kelley1a6bca82008-07-11 11:11:42 +0100841 free_frec(forward); /* cancel */
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000842 }
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000843}
Simon Kelley44a2a312004-03-10 20:04:35 +0000844
Simon Kelley1a6bca82008-07-11 11:11:42 +0100845
Simon Kelley5aabfc72007-08-29 11:24:47 +0100846void receive_query(struct listener *listen, time_t now)
Simon Kelley44a2a312004-03-10 20:04:35 +0000847{
Simon Kelley572b41e2011-02-18 18:11:18 +0000848 struct dns_header *header = (struct dns_header *)daemon->packet;
Simon Kelley44a2a312004-03-10 20:04:35 +0000849 union mysockaddr source_addr;
Simon Kelleyc1bb8502004-08-11 18:40:17 +0100850 unsigned short type;
Simon Kelley44a2a312004-03-10 20:04:35 +0000851 struct all_addr dst_addr;
Simon Kelleyf6b7dc42005-01-23 12:06:08 +0000852 struct in_addr netmask, dst_addr_4;
Simon Kelleycdeda282006-03-16 20:16:06 +0000853 size_t m;
854 ssize_t n;
Vladislav Grishenko3b195962013-11-26 11:08:21 +0000855 int if_index = 0, auth_dns = 0;
856#ifdef HAVE_AUTH
857 int local_auth = 0;
858#endif
Simon Kelley44a2a312004-03-10 20:04:35 +0000859 struct iovec iov[1];
860 struct msghdr msg;
861 struct cmsghdr *cmptr;
Simon Kelley44a2a312004-03-10 20:04:35 +0000862 union {
863 struct cmsghdr align; /* this ensures alignment */
864#ifdef HAVE_IPV6
865 char control6[CMSG_SPACE(sizeof(struct in6_pktinfo))];
866#endif
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100867#if defined(HAVE_LINUX_NETWORK)
Simon Kelley44a2a312004-03-10 20:04:35 +0000868 char control[CMSG_SPACE(sizeof(struct in_pktinfo))];
Simon Kelley824af852008-02-12 20:43:05 +0000869#elif defined(IP_RECVDSTADDR) && defined(HAVE_SOLARIS_NETWORK)
870 char control[CMSG_SPACE(sizeof(struct in_addr)) +
871 CMSG_SPACE(sizeof(unsigned int))];
Simon Kelley44a2a312004-03-10 20:04:35 +0000872#elif defined(IP_RECVDSTADDR)
873 char control[CMSG_SPACE(sizeof(struct in_addr)) +
874 CMSG_SPACE(sizeof(struct sockaddr_dl))];
875#endif
876 } control_u;
Simon Kelley2329bef2013-12-03 13:41:16 +0000877#ifdef HAVE_IPV6
878 /* Can always get recvd interface for IPv6 */
879 int check_dst = !option_bool(OPT_NOWILD) || listen->family == AF_INET6;
880#else
881 int check_dst = !option_bool(OPT_NOWILD);
882#endif
883
Simon Kelleycdeda282006-03-16 20:16:06 +0000884 /* packet buffer overwritten */
885 daemon->srv_save = NULL;
886
Simon Kelley4f7b3042012-11-28 21:27:02 +0000887 dst_addr_4.s_addr = 0;
888 netmask.s_addr = 0;
889
Simon Kelley7e5664b2013-04-05 16:57:41 +0100890 if (option_bool(OPT_NOWILD) && listen->iface)
Simon Kelleyf6b7dc42005-01-23 12:06:08 +0000891 {
Simon Kelley4f7b3042012-11-28 21:27:02 +0000892 auth_dns = listen->iface->dns_auth;
893
894 if (listen->family == AF_INET)
895 {
896 dst_addr_4 = listen->iface->addr.in.sin_addr;
897 netmask = listen->iface->netmask;
898 }
Simon Kelleyf6b7dc42005-01-23 12:06:08 +0000899 }
Simon Kelley4f7b3042012-11-28 21:27:02 +0000900
Simon Kelley3be34542004-09-11 19:12:13 +0100901 iov[0].iov_base = daemon->packet;
902 iov[0].iov_len = daemon->edns_pktsz;
Simon Kelley44a2a312004-03-10 20:04:35 +0000903
904 msg.msg_control = control_u.control;
905 msg.msg_controllen = sizeof(control_u);
906 msg.msg_flags = 0;
907 msg.msg_name = &source_addr;
908 msg.msg_namelen = sizeof(source_addr);
909 msg.msg_iov = iov;
910 msg.msg_iovlen = 1;
911
Simon Kelleyde379512004-06-22 20:23:33 +0100912 if ((n = recvmsg(listen->fd, &msg, 0)) == -1)
Simon Kelley3be34542004-09-11 19:12:13 +0100913 return;
Simon Kelley44a2a312004-03-10 20:04:35 +0000914
Simon Kelley572b41e2011-02-18 18:11:18 +0000915 if (n < (int)sizeof(struct dns_header) ||
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100916 (msg.msg_flags & MSG_TRUNC) ||
Simon Kelley572b41e2011-02-18 18:11:18 +0000917 (header->hb3 & HB3_QR))
Simon Kelley3be34542004-09-11 19:12:13 +0100918 return;
Simon Kelley44a2a312004-03-10 20:04:35 +0000919
Simon Kelley26128d22004-11-14 16:43:54 +0000920 source_addr.sa.sa_family = listen->family;
921#ifdef HAVE_IPV6
922 if (listen->family == AF_INET6)
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100923 source_addr.in6.sin6_flowinfo = 0;
Simon Kelley26128d22004-11-14 16:43:54 +0000924#endif
Simon Kelley28866e92011-02-14 20:19:14 +0000925
Simon Kelley2329bef2013-12-03 13:41:16 +0000926 if (check_dst)
Simon Kelley44a2a312004-03-10 20:04:35 +0000927 {
Simon Kelley8a911cc2004-03-16 18:35:52 +0000928 struct ifreq ifr;
929
Simon Kelley26128d22004-11-14 16:43:54 +0000930 if (msg.msg_controllen < sizeof(struct cmsghdr))
931 return;
932
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100933#if defined(HAVE_LINUX_NETWORK)
Simon Kelley26128d22004-11-14 16:43:54 +0000934 if (listen->family == AF_INET)
935 for (cmptr = CMSG_FIRSTHDR(&msg); cmptr; cmptr = CMSG_NXTHDR(&msg, cmptr))
Simon Kelleyc72daea2012-01-05 21:33:27 +0000936 if (cmptr->cmsg_level == IPPROTO_IP && cmptr->cmsg_type == IP_PKTINFO)
Simon Kelley26128d22004-11-14 16:43:54 +0000937 {
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100938 union {
939 unsigned char *c;
940 struct in_pktinfo *p;
941 } p;
942 p.c = CMSG_DATA(cmptr);
943 dst_addr_4 = dst_addr.addr.addr4 = p.p->ipi_spec_dst;
944 if_index = p.p->ipi_ifindex;
Simon Kelley26128d22004-11-14 16:43:54 +0000945 }
946#elif defined(IP_RECVDSTADDR) && defined(IP_RECVIF)
947 if (listen->family == AF_INET)
948 {
949 for (cmptr = CMSG_FIRSTHDR(&msg); cmptr; cmptr = CMSG_NXTHDR(&msg, cmptr))
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100950 {
951 union {
952 unsigned char *c;
953 unsigned int *i;
954 struct in_addr *a;
955#ifndef HAVE_SOLARIS_NETWORK
956 struct sockaddr_dl *s;
Simon Kelley824af852008-02-12 20:43:05 +0000957#endif
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100958 } p;
959 p.c = CMSG_DATA(cmptr);
960 if (cmptr->cmsg_level == IPPROTO_IP && cmptr->cmsg_type == IP_RECVDSTADDR)
961 dst_addr_4 = dst_addr.addr.addr4 = *(p.a);
962 else if (cmptr->cmsg_level == IPPROTO_IP && cmptr->cmsg_type == IP_RECVIF)
963#ifdef HAVE_SOLARIS_NETWORK
964 if_index = *(p.i);
965#else
966 if_index = p.s->sdl_index;
967#endif
968 }
Simon Kelley26128d22004-11-14 16:43:54 +0000969 }
970#endif
971
972#ifdef HAVE_IPV6
973 if (listen->family == AF_INET6)
974 {
975 for (cmptr = CMSG_FIRSTHDR(&msg); cmptr; cmptr = CMSG_NXTHDR(&msg, cmptr))
Simon Kelleyc72daea2012-01-05 21:33:27 +0000976 if (cmptr->cmsg_level == IPPROTO_IPV6 && cmptr->cmsg_type == daemon->v6pktinfo)
Simon Kelley26128d22004-11-14 16:43:54 +0000977 {
Simon Kelley8ef5ada2010-06-03 19:42:45 +0100978 union {
979 unsigned char *c;
980 struct in6_pktinfo *p;
981 } p;
982 p.c = CMSG_DATA(cmptr);
983
984 dst_addr.addr.addr6 = p.p->ipi6_addr;
985 if_index = p.p->ipi6_ifindex;
Simon Kelley26128d22004-11-14 16:43:54 +0000986 }
987 }
988#endif
989
990 /* enforce available interface configuration */
991
Simon Kelleye25db1f2013-01-29 22:10:26 +0000992 if (!indextoname(listen->fd, if_index, ifr.ifr_name))
Simon Kelley832af0b2007-01-21 20:01:28 +0000993 return;
994
Simon Kelleye25db1f2013-01-29 22:10:26 +0000995 if (!iface_check(listen->family, &dst_addr, ifr.ifr_name, &auth_dns))
996 {
997 if (!option_bool(OPT_CLEVERBIND))
Simon Kelley115ac3e2013-05-20 11:28:32 +0100998 enumerate_interfaces(0);
Simon Kelley3f2873d2013-05-14 11:28:47 +0100999 if (!loopback_exception(listen->fd, listen->family, &dst_addr, ifr.ifr_name) &&
1000 !label_exception(if_index, listen->family, &dst_addr))
Simon Kelleye25db1f2013-01-29 22:10:26 +00001001 return;
1002 }
1003
Simon Kelley552af8b2012-02-29 20:10:31 +00001004 if (listen->family == AF_INET && option_bool(OPT_LOCALISE))
1005 {
1006 struct irec *iface;
1007
1008 /* get the netmask of the interface whch has the address we were sent to.
1009 This is no neccessarily the interface we arrived on. */
1010
1011 for (iface = daemon->interfaces; iface; iface = iface->next)
1012 if (iface->addr.sa.sa_family == AF_INET &&
1013 iface->addr.in.sin_addr.s_addr == dst_addr_4.s_addr)
1014 break;
1015
1016 /* interface may be new */
Simon Kelleye25db1f2013-01-29 22:10:26 +00001017 if (!iface && !option_bool(OPT_CLEVERBIND))
Simon Kelley115ac3e2013-05-20 11:28:32 +01001018 enumerate_interfaces(0);
Simon Kelley552af8b2012-02-29 20:10:31 +00001019
1020 for (iface = daemon->interfaces; iface; iface = iface->next)
1021 if (iface->addr.sa.sa_family == AF_INET &&
1022 iface->addr.in.sin_addr.s_addr == dst_addr_4.s_addr)
1023 break;
1024
1025 /* If we failed, abandon localisation */
1026 if (iface)
1027 netmask = iface->netmask;
1028 else
1029 dst_addr_4.s_addr = 0;
1030 }
Simon Kelley44a2a312004-03-10 20:04:35 +00001031 }
1032
Simon Kelleycdeda282006-03-16 20:16:06 +00001033 if (extract_request(header, (size_t)n, daemon->namebuff, &type))
Simon Kelley44a2a312004-03-10 20:04:35 +00001034 {
Simon Kelley1a6bca82008-07-11 11:11:42 +01001035 char types[20];
Simon Kelleyb485ed92013-10-18 22:00:39 +01001036#ifdef HAVE_AUTH
1037 struct auth_zone *zone;
1038#endif
Simon Kelley1a6bca82008-07-11 11:11:42 +01001039
Simon Kelley4f7b3042012-11-28 21:27:02 +00001040 querystr(auth_dns ? "auth" : "query", types, type);
Simon Kelley1a6bca82008-07-11 11:11:42 +01001041
Simon Kelley44a2a312004-03-10 20:04:35 +00001042 if (listen->family == AF_INET)
Simon Kelley3be34542004-09-11 19:12:13 +01001043 log_query(F_QUERY | F_IPV4 | F_FORWARD, daemon->namebuff,
Simon Kelley1a6bca82008-07-11 11:11:42 +01001044 (struct all_addr *)&source_addr.in.sin_addr, types);
Simon Kelley44a2a312004-03-10 20:04:35 +00001045#ifdef HAVE_IPV6
1046 else
Simon Kelley3be34542004-09-11 19:12:13 +01001047 log_query(F_QUERY | F_IPV6 | F_FORWARD, daemon->namebuff,
Simon Kelley1a6bca82008-07-11 11:11:42 +01001048 (struct all_addr *)&source_addr.in6.sin6_addr, types);
Simon Kelley44a2a312004-03-10 20:04:35 +00001049#endif
Simon Kelley44a2a312004-03-10 20:04:35 +00001050
Simon Kelley4820dce2012-12-18 18:30:30 +00001051#ifdef HAVE_AUTH
Simon Kelleyb485ed92013-10-18 22:00:39 +01001052 /* find queries for zones we're authoritative for, and answer them directly */
Simon Kelley6008bdb2013-10-21 21:47:03 +01001053 if (!auth_dns)
1054 for (zone = daemon->auth_zones; zone; zone = zone->next)
1055 if (in_zone(zone, daemon->namebuff, NULL))
1056 {
1057 auth_dns = 1;
1058 local_auth = 1;
1059 break;
1060 }
Simon Kelleyb485ed92013-10-18 22:00:39 +01001061#endif
1062 }
1063
1064#ifdef HAVE_AUTH
Simon Kelley4f7b3042012-11-28 21:27:02 +00001065 if (auth_dns)
Simon Kelley824af852008-02-12 20:43:05 +00001066 {
Simon Kelley19b16892013-10-20 10:19:39 +01001067 m = answer_auth(header, ((char *) header) + PACKETSZ, (size_t)n, now, &source_addr, local_auth);
Simon Kelley4f7b3042012-11-28 21:27:02 +00001068 if (m >= 1)
Simon Kelleyb485ed92013-10-18 22:00:39 +01001069 {
1070 send_from(listen->fd, option_bool(OPT_NOWILD) || option_bool(OPT_CLEVERBIND),
1071 (char *)header, m, &source_addr, &dst_addr, if_index);
1072 daemon->auth_answer++;
1073 }
Simon Kelley824af852008-02-12 20:43:05 +00001074 }
Simon Kelley44a2a312004-03-10 20:04:35 +00001075 else
Simon Kelley4820dce2012-12-18 18:30:30 +00001076#endif
Simon Kelley4f7b3042012-11-28 21:27:02 +00001077 {
1078 m = answer_request(header, ((char *) header) + PACKETSZ, (size_t)n,
1079 dst_addr_4, netmask, now);
1080
1081 if (m >= 1)
1082 {
1083 send_from(listen->fd, option_bool(OPT_NOWILD) || option_bool(OPT_CLEVERBIND),
1084 (char *)header, m, &source_addr, &dst_addr, if_index);
1085 daemon->local_answer++;
1086 }
1087 else if (forward_query(listen->fd, &source_addr, &dst_addr, if_index,
1088 header, (size_t)n, now, NULL))
1089 daemon->queries_forwarded++;
1090 else
1091 daemon->local_answer++;
1092 }
Simon Kelley44a2a312004-03-10 20:04:35 +00001093}
1094
Simon Kelleyfeba5c12004-07-27 20:28:58 +01001095/* The daemon forks before calling this: it should deal with one connection,
1096 blocking as neccessary, and then return. Note, need to be a bit careful
1097 about resources for debug mode, when the fork is suppressed: that's
1098 done by the caller. */
Simon Kelley5aabfc72007-08-29 11:24:47 +01001099unsigned char *tcp_request(int confd, time_t now,
Simon Kelley4f7b3042012-11-28 21:27:02 +00001100 union mysockaddr *local_addr, struct in_addr netmask, int auth_dns)
Simon Kelleyfeba5c12004-07-27 20:28:58 +01001101{
Simon Kelley28866e92011-02-14 20:19:14 +00001102 size_t size = 0;
1103 int norebind = 0;
Vladislav Grishenko3b195962013-11-26 11:08:21 +00001104#ifdef HAVE_AUTH
Simon Kelley19b16892013-10-20 10:19:39 +01001105 int local_auth = 0;
Vladislav Grishenko3b195962013-11-26 11:08:21 +00001106#endif
Simon Kelleyed4c0762013-10-08 20:46:34 +01001107 int checking_disabled, check_subnet;
Simon Kelleycdeda282006-03-16 20:16:06 +00001108 size_t m;
Simon Kelleyee86ce62012-12-07 11:54:46 +00001109 unsigned short qtype;
1110 unsigned int gotname;
Simon Kelleyfeba5c12004-07-27 20:28:58 +01001111 unsigned char c1, c2;
Simon Kelley4b5ea122013-04-22 10:18:26 +01001112 /* Max TCP packet + slop + size */
1113 unsigned char *packet = whine_malloc(65536 + MAXDNAME + RRFIXEDSZ + sizeof(u16));
1114 unsigned char *payload = &packet[2];
1115 /* largest field in header is 16-bits, so this is still sufficiently aligned */
1116 struct dns_header *header = (struct dns_header *)payload;
1117 u16 *length = (u16 *)packet;
Simon Kelley3be34542004-09-11 19:12:13 +01001118 struct server *last_server;
Simon Kelley7de060b2011-08-26 17:24:52 +01001119 struct in_addr dst_addr_4;
1120 union mysockaddr peer_addr;
1121 socklen_t peer_len = sizeof(union mysockaddr);
Simon Kelley3be34542004-09-11 19:12:13 +01001122
Simon Kelley7de060b2011-08-26 17:24:52 +01001123 if (getpeername(confd, (struct sockaddr *)&peer_addr, &peer_len) == -1)
1124 return packet;
1125
Simon Kelleyfeba5c12004-07-27 20:28:58 +01001126 while (1)
1127 {
1128 if (!packet ||
1129 !read_write(confd, &c1, 1, 1) || !read_write(confd, &c2, 1, 1) ||
1130 !(size = c1 << 8 | c2) ||
Simon Kelley4b5ea122013-04-22 10:18:26 +01001131 !read_write(confd, payload, size, 1))
Simon Kelleyfeba5c12004-07-27 20:28:58 +01001132 return packet;
1133
Simon Kelley572b41e2011-02-18 18:11:18 +00001134 if (size < (int)sizeof(struct dns_header))
Simon Kelleyfeba5c12004-07-27 20:28:58 +01001135 continue;
1136
Simon Kelleyed4c0762013-10-08 20:46:34 +01001137 check_subnet = 0;
1138
Simon Kelley28866e92011-02-14 20:19:14 +00001139 /* save state of "cd" flag in query */
Simon Kelley572b41e2011-02-18 18:11:18 +00001140 checking_disabled = header->hb4 & HB4_CD;
Simon Kelley28866e92011-02-14 20:19:14 +00001141
1142 /* RFC 4035: sect 4.6 para 2 */
Simon Kelley572b41e2011-02-18 18:11:18 +00001143 header->hb4 &= ~HB4_AD;
Simon Kelleyfeba5c12004-07-27 20:28:58 +01001144
Simon Kelley3be34542004-09-11 19:12:13 +01001145 if ((gotname = extract_request(header, (unsigned int)size, daemon->namebuff, &qtype)))
Simon Kelleyfeba5c12004-07-27 20:28:58 +01001146 {
Simon Kelley7de060b2011-08-26 17:24:52 +01001147 char types[20];
Simon Kelleyb485ed92013-10-18 22:00:39 +01001148#ifdef HAVE_AUTH
1149 struct auth_zone *zone;
1150#endif
Simon Kelley4f7b3042012-11-28 21:27:02 +00001151 querystr(auth_dns ? "auth" : "query", types, qtype);
Simon Kelley7de060b2011-08-26 17:24:52 +01001152
1153 if (peer_addr.sa.sa_family == AF_INET)
1154 log_query(F_QUERY | F_IPV4 | F_FORWARD, daemon->namebuff,
1155 (struct all_addr *)&peer_addr.in.sin_addr, types);
Simon Kelleyfeba5c12004-07-27 20:28:58 +01001156#ifdef HAVE_IPV6
Simon Kelley7de060b2011-08-26 17:24:52 +01001157 else
1158 log_query(F_QUERY | F_IPV6 | F_FORWARD, daemon->namebuff,
1159 (struct all_addr *)&peer_addr.in6.sin6_addr, types);
Simon Kelleyfeba5c12004-07-27 20:28:58 +01001160#endif
Simon Kelleyb485ed92013-10-18 22:00:39 +01001161
1162#ifdef HAVE_AUTH
1163 /* find queries for zones we're authoritative for, and answer them directly */
Simon Kelley6008bdb2013-10-21 21:47:03 +01001164 if (!auth_dns)
1165 for (zone = daemon->auth_zones; zone; zone = zone->next)
1166 if (in_zone(zone, daemon->namebuff, NULL))
1167 {
1168 auth_dns = 1;
1169 local_auth = 1;
1170 break;
1171 }
Simon Kelleyb485ed92013-10-18 22:00:39 +01001172#endif
Simon Kelleyfeba5c12004-07-27 20:28:58 +01001173 }
1174
Simon Kelley7de060b2011-08-26 17:24:52 +01001175 if (local_addr->sa.sa_family == AF_INET)
1176 dst_addr_4 = local_addr->in.sin_addr;
1177 else
1178 dst_addr_4.s_addr = 0;
1179
Simon Kelley4820dce2012-12-18 18:30:30 +00001180#ifdef HAVE_AUTH
Simon Kelley4f7b3042012-11-28 21:27:02 +00001181 if (auth_dns)
Simon Kelley19b16892013-10-20 10:19:39 +01001182 m = answer_auth(header, ((char *) header) + 65536, (size_t)size, now, &peer_addr, local_auth);
Simon Kelley4f7b3042012-11-28 21:27:02 +00001183 else
Simon Kelley4820dce2012-12-18 18:30:30 +00001184#endif
Simon Kelleyfeba5c12004-07-27 20:28:58 +01001185 {
Simon Kelley4f7b3042012-11-28 21:27:02 +00001186 /* m > 0 if answered from cache */
1187 m = answer_request(header, ((char *) header) + 65536, (size_t)size,
1188 dst_addr_4, netmask, now);
Simon Kelleyfeba5c12004-07-27 20:28:58 +01001189
Simon Kelley4f7b3042012-11-28 21:27:02 +00001190 /* Do this by steam now we're not in the select() loop */
1191 check_log_writer(NULL);
1192
1193 if (m == 0)
Simon Kelleyfeba5c12004-07-27 20:28:58 +01001194 {
Simon Kelley4f7b3042012-11-28 21:27:02 +00001195 unsigned int flags = 0;
1196 struct all_addr *addrp = NULL;
1197 int type = 0;
1198 char *domain = NULL;
Simon Kelleyfeba5c12004-07-27 20:28:58 +01001199
Simon Kelley4f7b3042012-11-28 21:27:02 +00001200 if (option_bool(OPT_ADD_MAC))
1201 size = add_mac(header, size, ((char *) header) + 65536, &peer_addr);
Simon Kelleyed4c0762013-10-08 20:46:34 +01001202
1203 if (option_bool(OPT_CLIENT_SUBNET))
1204 {
1205 size_t new = add_source_addr(header, size, ((char *) header) + 65536, &peer_addr);
1206 if (size != new)
1207 {
1208 size = new;
1209 check_subnet = 1;
1210 }
1211 }
1212
Simon Kelley4f7b3042012-11-28 21:27:02 +00001213 if (gotname)
1214 flags = search_servers(now, &addrp, gotname, daemon->namebuff, &type, &domain, &norebind);
1215
1216 if (type != 0 || option_bool(OPT_ORDER) || !daemon->last_server)
1217 last_server = daemon->servers;
1218 else
1219 last_server = daemon->last_server;
1220
1221 if (!flags && last_server)
1222 {
1223 struct server *firstsendto = NULL;
1224 unsigned int crc = questions_crc(header, (unsigned int)size, daemon->namebuff);
1225
1226 /* Loop round available servers until we succeed in connecting to one.
1227 Note that this code subtley ensures that consecutive queries on this connection
1228 which can go to the same server, do so. */
1229 while (1)
Simon Kelleyfeba5c12004-07-27 20:28:58 +01001230 {
Simon Kelley4f7b3042012-11-28 21:27:02 +00001231 if (!firstsendto)
1232 firstsendto = last_server;
1233 else
1234 {
1235 if (!(last_server = last_server->next))
1236 last_server = daemon->servers;
1237
1238 if (last_server == firstsendto)
1239 break;
1240 }
1241
1242 /* server for wrong domain */
1243 if (type != (last_server->flags & SERV_TYPE) ||
1244 (type == SERV_HAS_DOMAIN && !hostname_isequal(domain, last_server->domain)))
Simon Kelley7de060b2011-08-26 17:24:52 +01001245 continue;
1246
Simon Kelley4f7b3042012-11-28 21:27:02 +00001247 if (last_server->tcpfd == -1)
1248 {
1249 if ((last_server->tcpfd = socket(last_server->addr.sa.sa_family, SOCK_STREAM, 0)) == -1)
1250 continue;
1251
1252 if ((!local_bind(last_server->tcpfd, &last_server->source_addr, last_server->interface, 1) ||
1253 connect(last_server->tcpfd, &last_server->addr.sa, sa_len(&last_server->addr)) == -1))
1254 {
1255 close(last_server->tcpfd);
1256 last_server->tcpfd = -1;
1257 continue;
1258 }
1259
1260#ifdef HAVE_CONNTRACK
1261 /* Copy connection mark of incoming query to outgoing connection. */
1262 if (option_bool(OPT_CONNTRACK))
1263 {
1264 unsigned int mark;
1265 struct all_addr local;
1266#ifdef HAVE_IPV6
1267 if (local_addr->sa.sa_family == AF_INET6)
1268 local.addr.addr6 = local_addr->in6.sin6_addr;
1269 else
1270#endif
1271 local.addr.addr4 = local_addr->in.sin_addr;
1272
1273 if (get_incoming_mark(&peer_addr, &local, 1, &mark))
1274 setsockopt(last_server->tcpfd, SOL_SOCKET, SO_MARK, &mark, sizeof(unsigned int));
1275 }
1276#endif
1277 }
1278
Simon Kelley4b5ea122013-04-22 10:18:26 +01001279 *length = htons(size);
Simon Kelley4f7b3042012-11-28 21:27:02 +00001280
Simon Kelley4b5ea122013-04-22 10:18:26 +01001281 if (!read_write(last_server->tcpfd, packet, size + sizeof(u16), 0) ||
Simon Kelley4f7b3042012-11-28 21:27:02 +00001282 !read_write(last_server->tcpfd, &c1, 1, 1) ||
1283 !read_write(last_server->tcpfd, &c2, 1, 1))
Simon Kelley7de060b2011-08-26 17:24:52 +01001284 {
1285 close(last_server->tcpfd);
1286 last_server->tcpfd = -1;
1287 continue;
Simon Kelley4f7b3042012-11-28 21:27:02 +00001288 }
1289
1290 m = (c1 << 8) | c2;
Simon Kelley4b5ea122013-04-22 10:18:26 +01001291 if (!read_write(last_server->tcpfd, payload, m, 1))
Simon Kelley4f7b3042012-11-28 21:27:02 +00001292 return packet;
1293
1294 if (!gotname)
1295 strcpy(daemon->namebuff, "query");
1296 if (last_server->addr.sa.sa_family == AF_INET)
1297 log_query(F_SERVER | F_IPV4 | F_FORWARD, daemon->namebuff,
1298 (struct all_addr *)&last_server->addr.in.sin_addr, NULL);
Simon Kelleyfeba5c12004-07-27 20:28:58 +01001299#ifdef HAVE_IPV6
Simon Kelley4f7b3042012-11-28 21:27:02 +00001300 else
1301 log_query(F_SERVER | F_IPV6 | F_FORWARD, daemon->namebuff,
1302 (struct all_addr *)&last_server->addr.in6.sin6_addr, NULL);
Simon Kelleyfeba5c12004-07-27 20:28:58 +01001303#endif
Simon Kelley4f7b3042012-11-28 21:27:02 +00001304
1305 /* There's no point in updating the cache, since this process will exit and
1306 lose the information after a few queries. We make this call for the alias and
1307 bogus-nxdomain side-effects. */
1308 /* If the crc of the question section doesn't match the crc we sent, then
1309 someone might be attempting to insert bogus values into the cache by
1310 sending replies containing questions and bogus answers. */
1311 if (crc == questions_crc(header, (unsigned int)m, daemon->namebuff))
1312 m = process_reply(header, now, last_server, (unsigned int)m,
Simon Kelleyed4c0762013-10-08 20:46:34 +01001313 option_bool(OPT_NO_REBIND) && !norebind, checking_disabled,
Simon Kelley3a237152013-12-12 12:15:50 +00001314 0, check_subnet, &peer_addr); /* TODO - cache secure */
Simon Kelley4f7b3042012-11-28 21:27:02 +00001315
1316 break;
1317 }
Simon Kelleyfeba5c12004-07-27 20:28:58 +01001318 }
Simon Kelley4f7b3042012-11-28 21:27:02 +00001319
1320 /* In case of local answer or no connections made. */
1321 if (m == 0)
1322 m = setup_reply(header, (unsigned int)size, addrp, flags, daemon->local_ttl);
Simon Kelleyfeba5c12004-07-27 20:28:58 +01001323 }
Simon Kelleyfeba5c12004-07-27 20:28:58 +01001324 }
Simon Kelley4f7b3042012-11-28 21:27:02 +00001325
Simon Kelley5aabfc72007-08-29 11:24:47 +01001326 check_log_writer(NULL);
Simon Kelleyfeba5c12004-07-27 20:28:58 +01001327
Simon Kelley4b5ea122013-04-22 10:18:26 +01001328 *length = htons(m);
1329
1330 if (m == 0 || !read_write(confd, packet, m + sizeof(u16), 0))
Simon Kelleyfeba5c12004-07-27 20:28:58 +01001331 return packet;
1332 }
1333}
1334
Simon Kelley16972692006-10-16 20:04:18 +01001335static struct frec *allocate_frec(time_t now)
Simon Kelley9e4abcb2004-01-22 19:47:41 +00001336{
Simon Kelley16972692006-10-16 20:04:18 +01001337 struct frec *f;
Simon Kelley9e4abcb2004-01-22 19:47:41 +00001338
Simon Kelley5aabfc72007-08-29 11:24:47 +01001339 if ((f = (struct frec *)whine_malloc(sizeof(struct frec))))
Simon Kelley9e4abcb2004-01-22 19:47:41 +00001340 {
Simon Kelley1a6bca82008-07-11 11:11:42 +01001341 f->next = daemon->frec_list;
Simon Kelley9e4abcb2004-01-22 19:47:41 +00001342 f->time = now;
Simon Kelley832af0b2007-01-21 20:01:28 +00001343 f->sentto = NULL;
Simon Kelley1a6bca82008-07-11 11:11:42 +01001344 f->rfd4 = NULL;
Simon Kelley28866e92011-02-14 20:19:14 +00001345 f->flags = 0;
Simon Kelley1a6bca82008-07-11 11:11:42 +01001346#ifdef HAVE_IPV6
1347 f->rfd6 = NULL;
1348#endif
Simon Kelley3a237152013-12-12 12:15:50 +00001349#ifdef HAVE_DNSSEC
1350 f->blocking_query = NULL;
1351#endif
Simon Kelley1a6bca82008-07-11 11:11:42 +01001352 daemon->frec_list = f;
Simon Kelley9e4abcb2004-01-22 19:47:41 +00001353 }
Simon Kelley16972692006-10-16 20:04:18 +01001354
1355 return f;
1356}
1357
Simon Kelley1a6bca82008-07-11 11:11:42 +01001358static struct randfd *allocate_rfd(int family)
1359{
1360 static int finger = 0;
1361 int i;
1362
1363 /* limit the number of sockets we have open to avoid starvation of
1364 (eg) TFTP. Once we have a reasonable number, randomness should be OK */
1365
1366 for (i = 0; i < RANDOM_SOCKS; i++)
Simon Kelley9009d742008-11-14 20:04:27 +00001367 if (daemon->randomsocks[i].refcount == 0)
Simon Kelley1a6bca82008-07-11 11:11:42 +01001368 {
Simon Kelley9009d742008-11-14 20:04:27 +00001369 if ((daemon->randomsocks[i].fd = random_sock(family)) == -1)
1370 break;
1371
Simon Kelley1a6bca82008-07-11 11:11:42 +01001372 daemon->randomsocks[i].refcount = 1;
1373 daemon->randomsocks[i].family = family;
1374 return &daemon->randomsocks[i];
1375 }
1376
Simon Kelley9009d742008-11-14 20:04:27 +00001377 /* No free ones or cannot get new socket, grab an existing one */
Simon Kelley1a6bca82008-07-11 11:11:42 +01001378 for (i = 0; i < RANDOM_SOCKS; i++)
1379 {
1380 int j = (i+finger) % RANDOM_SOCKS;
Simon Kelley9009d742008-11-14 20:04:27 +00001381 if (daemon->randomsocks[j].refcount != 0 &&
1382 daemon->randomsocks[j].family == family &&
1383 daemon->randomsocks[j].refcount != 0xffff)
Simon Kelley1a6bca82008-07-11 11:11:42 +01001384 {
1385 finger = j;
1386 daemon->randomsocks[j].refcount++;
1387 return &daemon->randomsocks[j];
1388 }
1389 }
1390
1391 return NULL; /* doom */
1392}
Simon Kelley1a6bca82008-07-11 11:11:42 +01001393static void free_frec(struct frec *f)
1394{
1395 if (f->rfd4 && --(f->rfd4->refcount) == 0)
1396 close(f->rfd4->fd);
1397
1398 f->rfd4 = NULL;
1399 f->sentto = NULL;
Simon Kelley28866e92011-02-14 20:19:14 +00001400 f->flags = 0;
Simon Kelley1a6bca82008-07-11 11:11:42 +01001401
1402#ifdef HAVE_IPV6
1403 if (f->rfd6 && --(f->rfd6->refcount) == 0)
1404 close(f->rfd6->fd);
1405
1406 f->rfd6 = NULL;
1407#endif
Simon Kelley3a237152013-12-12 12:15:50 +00001408
1409#ifdef HAVE_DNSSEC
1410 if (f->stash)
Simon Kelley0fc2f312014-01-08 10:26:58 +00001411 {
1412 blockdata_free(f->stash);
1413 f->stash = NULL;
1414 }
Simon Kelley3a237152013-12-12 12:15:50 +00001415
1416 /* Anything we're waiting on is pointless now, too */
1417 if (f->blocking_query)
1418 free_frec(f->blocking_query);
1419 f->blocking_query = NULL;
1420
1421#endif
Simon Kelley1a6bca82008-07-11 11:11:42 +01001422}
1423
Simon Kelley16972692006-10-16 20:04:18 +01001424/* if wait==NULL return a free or older than TIMEOUT record.
1425 else return *wait zero if one available, or *wait is delay to
Simon Kelley1a6bca82008-07-11 11:11:42 +01001426 when the oldest in-use record will expire. Impose an absolute
Simon Kelley3a237152013-12-12 12:15:50 +00001427 limit of 4*TIMEOUT before we wipe things (for random sockets).
1428 If force is set, always return a result, even if we have
1429 to allocate above the limit. */
1430struct frec *get_new_frec(time_t now, int *wait, int force)
Simon Kelley16972692006-10-16 20:04:18 +01001431{
Simon Kelley1a6bca82008-07-11 11:11:42 +01001432 struct frec *f, *oldest, *target;
Simon Kelley16972692006-10-16 20:04:18 +01001433 int count;
1434
1435 if (wait)
1436 *wait = 0;
1437
Simon Kelley1a6bca82008-07-11 11:11:42 +01001438 for (f = daemon->frec_list, oldest = NULL, target = NULL, count = 0; f; f = f->next, count++)
Simon Kelley832af0b2007-01-21 20:01:28 +00001439 if (!f->sentto)
Simon Kelley1a6bca82008-07-11 11:11:42 +01001440 target = f;
1441 else
Simon Kelley16972692006-10-16 20:04:18 +01001442 {
Simon Kelley1a6bca82008-07-11 11:11:42 +01001443 if (difftime(now, f->time) >= 4*TIMEOUT)
1444 {
1445 free_frec(f);
1446 target = f;
1447 }
1448
1449 if (!oldest || difftime(f->time, oldest->time) <= 0)
1450 oldest = f;
Simon Kelley16972692006-10-16 20:04:18 +01001451 }
Simon Kelley1a6bca82008-07-11 11:11:42 +01001452
1453 if (target)
1454 {
1455 target->time = now;
1456 return target;
1457 }
Simon Kelley16972692006-10-16 20:04:18 +01001458
1459 /* can't find empty one, use oldest if there is one
1460 and it's older than timeout */
1461 if (oldest && ((int)difftime(now, oldest->time)) >= TIMEOUT)
1462 {
1463 /* keep stuff for twice timeout if we can by allocating a new
1464 record instead */
1465 if (difftime(now, oldest->time) < 2*TIMEOUT &&
1466 count <= daemon->ftabsize &&
1467 (f = allocate_frec(now)))
1468 return f;
1469
1470 if (!wait)
1471 {
Simon Kelley1a6bca82008-07-11 11:11:42 +01001472 free_frec(oldest);
Simon Kelley16972692006-10-16 20:04:18 +01001473 oldest->time = now;
1474 }
1475 return oldest;
1476 }
1477
1478 /* none available, calculate time 'till oldest record expires */
Simon Kelley3a237152013-12-12 12:15:50 +00001479 if (!force && count > daemon->ftabsize)
Simon Kelley16972692006-10-16 20:04:18 +01001480 {
Marcelo Salhab Brogliato0da5e892013-05-31 11:49:06 +01001481 static time_t last_log = 0;
1482
Simon Kelley16972692006-10-16 20:04:18 +01001483 if (oldest && wait)
1484 *wait = oldest->time + (time_t)TIMEOUT - now;
Marcelo Salhab Brogliato0da5e892013-05-31 11:49:06 +01001485
1486 if ((int)difftime(now, last_log) > 5)
1487 {
1488 last_log = now;
1489 my_syslog(LOG_WARNING, _("Maximum number of concurrent DNS queries reached (max: %d)"), daemon->ftabsize);
1490 }
1491
Simon Kelley16972692006-10-16 20:04:18 +01001492 return NULL;
1493 }
1494
1495 if (!(f = allocate_frec(now)) && wait)
1496 /* wait one second on malloc failure */
1497 *wait = 1;
1498
Simon Kelley9e4abcb2004-01-22 19:47:41 +00001499 return f; /* OK if malloc fails and this is NULL */
1500}
1501
Simon Kelley832af0b2007-01-21 20:01:28 +00001502/* crc is all-ones if not known. */
1503static struct frec *lookup_frec(unsigned short id, unsigned int crc)
Simon Kelley9e4abcb2004-01-22 19:47:41 +00001504{
1505 struct frec *f;
1506
Simon Kelley1a6bca82008-07-11 11:11:42 +01001507 for(f = daemon->frec_list; f; f = f->next)
Simon Kelley832af0b2007-01-21 20:01:28 +00001508 if (f->sentto && f->new_id == id &&
1509 (f->crc == crc || crc == 0xffffffff))
Simon Kelley9e4abcb2004-01-22 19:47:41 +00001510 return f;
1511
1512 return NULL;
1513}
1514
1515static struct frec *lookup_frec_by_sender(unsigned short id,
Simon Kelleyfd9fa482004-10-21 20:24:00 +01001516 union mysockaddr *addr,
1517 unsigned int crc)
Simon Kelley9e4abcb2004-01-22 19:47:41 +00001518{
Simon Kelleyfeba5c12004-07-27 20:28:58 +01001519 struct frec *f;
1520
Simon Kelley1a6bca82008-07-11 11:11:42 +01001521 for(f = daemon->frec_list; f; f = f->next)
Simon Kelley832af0b2007-01-21 20:01:28 +00001522 if (f->sentto &&
Simon Kelley9e4abcb2004-01-22 19:47:41 +00001523 f->orig_id == id &&
Simon Kelleyfd9fa482004-10-21 20:24:00 +01001524 f->crc == crc &&
Simon Kelley9e4abcb2004-01-22 19:47:41 +00001525 sockaddr_isequal(&f->source, addr))
1526 return f;
1527
1528 return NULL;
1529}
1530
Simon Kelley849a8352006-06-09 21:02:31 +01001531/* A server record is going away, remove references to it */
Simon Kelley5aabfc72007-08-29 11:24:47 +01001532void server_gone(struct server *server)
Simon Kelley849a8352006-06-09 21:02:31 +01001533{
1534 struct frec *f;
1535
Simon Kelley1a6bca82008-07-11 11:11:42 +01001536 for (f = daemon->frec_list; f; f = f->next)
Simon Kelley832af0b2007-01-21 20:01:28 +00001537 if (f->sentto && f->sentto == server)
Simon Kelley1a6bca82008-07-11 11:11:42 +01001538 free_frec(f);
Simon Kelley849a8352006-06-09 21:02:31 +01001539
1540 if (daemon->last_server == server)
1541 daemon->last_server = NULL;
1542
1543 if (daemon->srv_save == server)
1544 daemon->srv_save = NULL;
1545}
Simon Kelley9e4abcb2004-01-22 19:47:41 +00001546
Simon Kelley316e2732010-01-22 20:16:09 +00001547/* return unique random ids. */
1548static unsigned short get_id(unsigned int crc)
Simon Kelley9e4abcb2004-01-22 19:47:41 +00001549{
1550 unsigned short ret = 0;
Simon Kelley832af0b2007-01-21 20:01:28 +00001551
Simon Kelley316e2732010-01-22 20:16:09 +00001552 do
Simon Kelley832af0b2007-01-21 20:01:28 +00001553 ret = rand16();
1554 while (lookup_frec(ret, crc));
1555
Simon Kelley9e4abcb2004-01-22 19:47:41 +00001556 return ret;
1557}
1558
1559
1560
1561
1562