]>
Commit | Line | Data |
---|---|---|
c47e3ba4 | 1 | /* dnsmasq is Copyright (c) 2000-2014 Simon Kelley |
9e4abcb5 SK |
2 | |
3 | This program is free software; you can redistribute it and/or modify | |
4 | it under the terms of the GNU General Public License as published by | |
824af85b SK |
5 | the Free Software Foundation; version 2 dated June, 1991, or |
6 | (at your option) version 3 dated 29 June, 2007. | |
7 | ||
9e4abcb5 SK |
8 | This program is distributed in the hope that it will be useful, |
9 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 | GNU General Public License for more details. | |
824af85b | 12 | |
73a08a24 SK |
13 | You should have received a copy of the GNU General Public License |
14 | along with this program. If not, see <http://www.gnu.org/licenses/>. | |
9e4abcb5 SK |
15 | */ |
16 | ||
9e4abcb5 SK |
17 | #include "dnsmasq.h" |
18 | ||
8a9be9e4 | 19 | static struct frec *lookup_frec(unsigned short id, void *hash); |
9e4abcb5 | 20 | static struct frec *lookup_frec_by_sender(unsigned short id, |
fd9fa481 | 21 | union mysockaddr *addr, |
8a9be9e4 SK |
22 | void *hash); |
23 | static unsigned short get_id(void); | |
1a6bca81 | 24 | static void free_frec(struct frec *f); |
9e4abcb5 | 25 | |
00a5b5d4 SK |
26 | #ifdef HAVE_DNSSEC |
27 | static 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); | |
29 | static int do_check_sign(time_t now, struct dns_header *header, size_t plen, char *name, char *keyname, int class); | |
30 | static int send_check_sign(time_t now, struct dns_header *header, size_t plen, char *name, char *keyname); | |
31 | #endif | |
32 | ||
33 | ||
824af85b | 34 | /* Send a UDP packet with its source address set as "source" |
44a2a316 | 35 | unless nowild is true, when we just send it with the kernel default */ |
29689cfa SK |
36 | int send_from(int fd, int nowild, char *packet, size_t len, |
37 | union mysockaddr *to, struct all_addr *source, | |
50303b19 | 38 | unsigned int iface) |
9e4abcb5 | 39 | { |
44a2a316 SK |
40 | struct msghdr msg; |
41 | struct iovec iov[1]; | |
44a2a316 SK |
42 | union { |
43 | struct cmsghdr align; /* this ensures alignment */ | |
5e9e0efb | 44 | #if defined(HAVE_LINUX_NETWORK) |
44a2a316 SK |
45 | char control[CMSG_SPACE(sizeof(struct in_pktinfo))]; |
46 | #elif defined(IP_SENDSRCADDR) | |
47 | char control[CMSG_SPACE(sizeof(struct in_addr))]; | |
48 | #endif | |
49 | #ifdef HAVE_IPV6 | |
50 | char control6[CMSG_SPACE(sizeof(struct in6_pktinfo))]; | |
51 | #endif | |
52 | } control_u; | |
feba5c1d | 53 | |
44a2a316 SK |
54 | iov[0].iov_base = packet; |
55 | iov[0].iov_len = len; | |
56 | ||
feba5c1d SK |
57 | msg.msg_control = NULL; |
58 | msg.msg_controllen = 0; | |
44a2a316 SK |
59 | msg.msg_flags = 0; |
60 | msg.msg_name = to; | |
61 | msg.msg_namelen = sa_len(to); | |
62 | msg.msg_iov = iov; | |
63 | msg.msg_iovlen = 1; | |
feba5c1d | 64 | |
26128d27 | 65 | if (!nowild) |
44a2a316 | 66 | { |
26128d27 | 67 | struct cmsghdr *cmptr; |
feba5c1d SK |
68 | msg.msg_control = &control_u; |
69 | msg.msg_controllen = sizeof(control_u); | |
26128d27 SK |
70 | cmptr = CMSG_FIRSTHDR(&msg); |
71 | ||
72 | if (to->sa.sa_family == AF_INET) | |
73 | { | |
5e9e0efb | 74 | #if defined(HAVE_LINUX_NETWORK) |
8ef5ada2 SK |
75 | struct in_pktinfo p; |
76 | p.ipi_ifindex = 0; | |
77 | p.ipi_spec_dst = source->addr.addr4; | |
78 | memcpy(CMSG_DATA(cmptr), &p, sizeof(p)); | |
26128d27 | 79 | msg.msg_controllen = cmptr->cmsg_len = CMSG_LEN(sizeof(struct in_pktinfo)); |
c72daea8 | 80 | cmptr->cmsg_level = IPPROTO_IP; |
26128d27 | 81 | cmptr->cmsg_type = IP_PKTINFO; |
44a2a316 | 82 | #elif defined(IP_SENDSRCADDR) |
8ef5ada2 | 83 | memcpy(CMSG_DATA(cmptr), &(source->addr.addr4), sizeof(source->addr.addr4)); |
26128d27 SK |
84 | msg.msg_controllen = cmptr->cmsg_len = CMSG_LEN(sizeof(struct in_addr)); |
85 | cmptr->cmsg_level = IPPROTO_IP; | |
86 | cmptr->cmsg_type = IP_SENDSRCADDR; | |
44a2a316 | 87 | #endif |
26128d27 | 88 | } |
26128d27 | 89 | else |
b8187c80 | 90 | #ifdef HAVE_IPV6 |
26128d27 | 91 | { |
8ef5ada2 SK |
92 | struct in6_pktinfo p; |
93 | p.ipi6_ifindex = iface; /* Need iface for IPv6 to handle link-local addrs */ | |
94 | p.ipi6_addr = source->addr.addr6; | |
95 | memcpy(CMSG_DATA(cmptr), &p, sizeof(p)); | |
26128d27 | 96 | msg.msg_controllen = cmptr->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo)); |
316e2730 | 97 | cmptr->cmsg_type = daemon->v6pktinfo; |
c72daea8 | 98 | cmptr->cmsg_level = IPPROTO_IPV6; |
26128d27 | 99 | } |
3d8df260 | 100 | #else |
c72daea8 | 101 | (void)iface; /* eliminate warning */ |
44a2a316 | 102 | #endif |
26128d27 | 103 | } |
feba5c1d | 104 | |
29d28dda | 105 | while (sendmsg(fd, &msg, 0) == -1) |
feba5c1d | 106 | { |
fd9fa481 | 107 | if (retry_send()) |
29d28dda | 108 | continue; |
22d904db | 109 | |
29d28dda SK |
110 | /* If interface is still in DAD, EINVAL results - ignore that. */ |
111 | if (errno == EINVAL) | |
112 | break; | |
29689cfa | 113 | |
29d28dda | 114 | my_syslog(LOG_ERR, _("failed to send packet: %s"), strerror(errno)); |
29689cfa | 115 | return 0; |
feba5c1d | 116 | } |
29d28dda | 117 | |
29689cfa | 118 | return 1; |
9e4abcb5 | 119 | } |
44a2a316 | 120 | |
28866e95 SK |
121 | static unsigned int search_servers(time_t now, struct all_addr **addrpp, |
122 | unsigned int qtype, char *qdomain, int *type, char **domain, int *norebind) | |
feba5c1d SK |
123 | |
124 | { | |
125 | /* If the query ends in the domain in one of our servers, set | |
126 | domain to point to that name. We find the largest match to allow both | |
127 | domain.org and sub.domain.org to exist. */ | |
128 | ||
129 | unsigned int namelen = strlen(qdomain); | |
130 | unsigned int matchlen = 0; | |
131 | struct server *serv; | |
28866e95 | 132 | unsigned int flags = 0; |
feba5c1d | 133 | |
3be34541 | 134 | for (serv = daemon->servers; serv; serv=serv->next) |
feba5c1d | 135 | /* domain matches take priority over NODOTS matches */ |
3d8df260 | 136 | if ((serv->flags & SERV_FOR_NODOTS) && *type != SERV_HAS_DOMAIN && !strchr(qdomain, '.') && namelen != 0) |
feba5c1d | 137 | { |
28866e95 | 138 | unsigned int sflag = serv->addr.sa.sa_family == AF_INET ? F_IPV4 : F_IPV6; |
feba5c1d | 139 | *type = SERV_FOR_NODOTS; |
feba5c1d | 140 | if (serv->flags & SERV_NO_ADDR) |
36717eee SK |
141 | flags = F_NXDOMAIN; |
142 | else if (serv->flags & SERV_LITERAL_ADDRESS) | |
143 | { | |
144 | if (sflag & qtype) | |
145 | { | |
146 | flags = sflag; | |
147 | if (serv->addr.sa.sa_family == AF_INET) | |
148 | *addrpp = (struct all_addr *)&serv->addr.in.sin_addr; | |
feba5c1d | 149 | #ifdef HAVE_IPV6 |
36717eee SK |
150 | else |
151 | *addrpp = (struct all_addr *)&serv->addr.in6.sin6_addr; | |
feba5c1d | 152 | #endif |
36717eee | 153 | } |
824af85b | 154 | else if (!flags || (flags & F_NXDOMAIN)) |
36717eee SK |
155 | flags = F_NOERR; |
156 | } | |
feba5c1d SK |
157 | } |
158 | else if (serv->flags & SERV_HAS_DOMAIN) | |
159 | { | |
160 | unsigned int domainlen = strlen(serv->domain); | |
b8187c80 | 161 | char *matchstart = qdomain + namelen - domainlen; |
feba5c1d | 162 | if (namelen >= domainlen && |
b8187c80 | 163 | hostname_isequal(matchstart, serv->domain) && |
8ef5ada2 | 164 | (domainlen == 0 || namelen == domainlen || *(matchstart-1) == '.' )) |
feba5c1d | 165 | { |
8ef5ada2 SK |
166 | if (serv->flags & SERV_NO_REBIND) |
167 | *norebind = 1; | |
28866e95 | 168 | else |
feba5c1d | 169 | { |
28866e95 SK |
170 | unsigned int sflag = serv->addr.sa.sa_family == AF_INET ? F_IPV4 : F_IPV6; |
171 | /* implement priority rules for --address and --server for same domain. | |
172 | --address wins if the address is for the correct AF | |
173 | --server wins otherwise. */ | |
174 | if (domainlen != 0 && domainlen == matchlen) | |
36717eee | 175 | { |
28866e95 | 176 | if ((serv->flags & SERV_LITERAL_ADDRESS)) |
8ef5ada2 | 177 | { |
28866e95 SK |
178 | if (!(sflag & qtype) && flags == 0) |
179 | continue; | |
180 | } | |
181 | else | |
182 | { | |
183 | if (flags & (F_IPV4 | F_IPV6)) | |
184 | continue; | |
185 | } | |
186 | } | |
187 | ||
188 | if (domainlen >= matchlen) | |
189 | { | |
190 | *type = serv->flags & (SERV_HAS_DOMAIN | SERV_USE_RESOLV | SERV_NO_REBIND); | |
191 | *domain = serv->domain; | |
192 | matchlen = domainlen; | |
193 | if (serv->flags & SERV_NO_ADDR) | |
194 | flags = F_NXDOMAIN; | |
195 | else if (serv->flags & SERV_LITERAL_ADDRESS) | |
196 | { | |
197 | if (sflag & qtype) | |
198 | { | |
199 | flags = sflag; | |
200 | if (serv->addr.sa.sa_family == AF_INET) | |
201 | *addrpp = (struct all_addr *)&serv->addr.in.sin_addr; | |
feba5c1d | 202 | #ifdef HAVE_IPV6 |
28866e95 SK |
203 | else |
204 | *addrpp = (struct all_addr *)&serv->addr.in6.sin6_addr; | |
feba5c1d | 205 | #endif |
28866e95 SK |
206 | } |
207 | else if (!flags || (flags & F_NXDOMAIN)) | |
208 | flags = F_NOERR; | |
8ef5ada2 | 209 | } |
28866e95 SK |
210 | else |
211 | flags = 0; | |
212 | } | |
213 | } | |
8ef5ada2 | 214 | } |
feba5c1d | 215 | } |
8ef5ada2 | 216 | |
7de060b0 | 217 | if (flags == 0 && !(qtype & F_QUERY) && |
28866e95 | 218 | option_bool(OPT_NODOTS_LOCAL) && !strchr(qdomain, '.') && namelen != 0) |
7de060b0 SK |
219 | /* don't forward A or AAAA queries for simple names, except the empty name */ |
220 | flags = F_NOERR; | |
8ef5ada2 | 221 | |
5aabfc78 | 222 | if (flags == F_NXDOMAIN && check_for_local_domain(qdomain, now)) |
c1bb8504 | 223 | flags = F_NOERR; |
feba5c1d | 224 | |
824af85b SK |
225 | if (flags) |
226 | { | |
227 | int logflags = 0; | |
228 | ||
229 | if (flags == F_NXDOMAIN || flags == F_NOERR) | |
230 | logflags = F_NEG | qtype; | |
231 | ||
1a6bca81 | 232 | log_query(logflags | flags | F_CONFIG | F_FORWARD, qdomain, *addrpp, NULL); |
824af85b | 233 | } |
8ef5ada2 SK |
234 | else if ((*type) & SERV_USE_RESOLV) |
235 | { | |
236 | *type = 0; /* use normal servers for this domain */ | |
237 | *domain = NULL; | |
238 | } | |
feba5c1d SK |
239 | return flags; |
240 | } | |
44a2a316 | 241 | |
824af85b SK |
242 | static int forward_query(int udpfd, union mysockaddr *udpaddr, |
243 | struct all_addr *dst_addr, unsigned int dst_iface, | |
83349b8a | 244 | struct dns_header *header, size_t plen, time_t now, |
613ad15d | 245 | struct frec *forward, int ad_reqd, int do_bit) |
9e4abcb5 | 246 | { |
9e4abcb5 | 247 | char *domain = NULL; |
8ef5ada2 | 248 | int type = 0, norebind = 0; |
9e4abcb5 | 249 | struct all_addr *addrp = NULL; |
28866e95 | 250 | unsigned int flags = 0; |
de37951c | 251 | struct server *start = NULL; |
8a9be9e4 SK |
252 | #ifdef HAVE_DNSSEC |
253 | void *hash = hash_questions(header, plen, daemon->namebuff); | |
254 | #else | |
255 | unsigned int crc = questions_crc(header, plen, daemon->namebuff); | |
256 | void *hash = &crc; | |
257 | #endif | |
258 | unsigned int gotname = extract_request(header, plen, daemon->namebuff, NULL); | |
259 | ||
00a5b5d4 SK |
260 | (void)do_bit; |
261 | ||
3d8df260 SK |
262 | /* may be no servers available. */ |
263 | if (!daemon->servers) | |
9e4abcb5 | 264 | forward = NULL; |
8a9be9e4 | 265 | else if (forward || (hash && (forward = lookup_frec_by_sender(ntohs(header->id), udpaddr, hash)))) |
9e4abcb5 | 266 | { |
e0c0ad3b | 267 | #ifdef HAVE_DNSSEC |
dac74312 | 268 | /* If we've already got an answer to this query, but we're awaiting keys for validation, |
e0c0ad3b SK |
269 | there's no point retrying the query, retry the key query instead...... */ |
270 | if (forward->blocking_query) | |
271 | { | |
272 | int fd; | |
273 | ||
274 | while (forward->blocking_query) | |
275 | forward = forward->blocking_query; | |
276 | ||
277 | blockdata_retrieve(forward->stash, forward->stash_len, (void *)header); | |
278 | plen = forward->stash_len; | |
279 | ||
2b29191e | 280 | if (forward->sentto->addr.sa.sa_family == AF_INET) |
e0c0ad3b SK |
281 | log_query(F_DNSSEC | F_IPV4, "retry", (struct all_addr *)&forward->sentto->addr.in.sin_addr, "dnssec"); |
282 | #ifdef HAVE_IPV6 | |
283 | else | |
284 | log_query(F_DNSSEC | F_IPV6, "retry", (struct all_addr *)&forward->sentto->addr.in6.sin6_addr, "dnssec"); | |
285 | #endif | |
286 | ||
287 | if (forward->sentto->sfd) | |
288 | fd = forward->sentto->sfd->fd; | |
289 | else | |
290 | { | |
291 | #ifdef HAVE_IPV6 | |
292 | if (forward->sentto->addr.sa.sa_family == AF_INET6) | |
293 | fd = forward->rfd6->fd; | |
294 | else | |
295 | #endif | |
296 | fd = forward->rfd4->fd; | |
297 | } | |
298 | ||
299 | while (sendto(fd, (char *)header, plen, 0, | |
300 | &forward->sentto->addr.sa, | |
2b29191e | 301 | sa_len(&forward->sentto->addr)) == -1 && retry_send()); |
e0c0ad3b SK |
302 | |
303 | return 1; | |
304 | } | |
305 | #endif | |
306 | ||
de37951c | 307 | /* retry on existing query, send to all available servers */ |
9e4abcb5 | 308 | domain = forward->sentto->domain; |
824af85b | 309 | forward->sentto->failed_queries++; |
28866e95 | 310 | if (!option_bool(OPT_ORDER)) |
de37951c | 311 | { |
0a852541 | 312 | forward->forwardall = 1; |
3be34541 | 313 | daemon->last_server = NULL; |
de37951c | 314 | } |
9e4abcb5 | 315 | type = forward->sentto->flags & SERV_TYPE; |
de37951c | 316 | if (!(start = forward->sentto->next)) |
3be34541 | 317 | start = daemon->servers; /* at end of list, recycle */ |
9e4abcb5 SK |
318 | header->id = htons(forward->new_id); |
319 | } | |
320 | else | |
321 | { | |
322 | if (gotname) | |
8ef5ada2 | 323 | flags = search_servers(now, &addrp, gotname, daemon->namebuff, &type, &domain, &norebind); |
9e4abcb5 | 324 | |
3a237152 | 325 | if (!flags && !(forward = get_new_frec(now, NULL, 0))) |
feba5c1d SK |
326 | /* table full - server failure. */ |
327 | flags = F_NEG; | |
9e4abcb5 SK |
328 | |
329 | if (forward) | |
330 | { | |
0a852541 SK |
331 | forward->source = *udpaddr; |
332 | forward->dest = *dst_addr; | |
333 | forward->iface = dst_iface; | |
0a852541 | 334 | forward->orig_id = ntohs(header->id); |
8a9be9e4 | 335 | forward->new_id = get_id(); |
832af0ba | 336 | forward->fd = udpfd; |
8a9be9e4 | 337 | memcpy(forward->hash, hash, HASH_SIZE); |
0a852541 | 338 | forward->forwardall = 0; |
ed4c0767 | 339 | forward->flags = 0; |
28866e95 SK |
340 | if (norebind) |
341 | forward->flags |= FREC_NOREBIND; | |
572b41eb | 342 | if (header->hb4 & HB4_CD) |
28866e95 | 343 | forward->flags |= FREC_CHECKING_DISABLED; |
83349b8a SK |
344 | if (ad_reqd) |
345 | forward->flags |= FREC_AD_QUESTION; | |
7fa836e1 SK |
346 | #ifdef HAVE_DNSSEC |
347 | forward->work_counter = DNSSEC_WORK; | |
613ad15d SK |
348 | if (do_bit) |
349 | forward->flags |= FREC_DO_QUESTION; | |
7fa836e1 | 350 | #endif |
613ad15d | 351 | |
28866e95 SK |
352 | header->id = htons(forward->new_id); |
353 | ||
8ef5ada2 SK |
354 | /* In strict_order mode, always try servers in the order |
355 | specified in resolv.conf, if a domain is given | |
356 | always try all the available servers, | |
9e4abcb5 SK |
357 | otherwise, use the one last known to work. */ |
358 | ||
8ef5ada2 SK |
359 | if (type == 0) |
360 | { | |
28866e95 | 361 | if (option_bool(OPT_ORDER)) |
8ef5ada2 SK |
362 | start = daemon->servers; |
363 | else if (!(start = daemon->last_server) || | |
364 | daemon->forwardcount++ > FORWARD_TEST || | |
365 | difftime(now, daemon->forwardtime) > FORWARD_TIME) | |
366 | { | |
367 | start = daemon->servers; | |
368 | forward->forwardall = 1; | |
369 | daemon->forwardcount = 0; | |
370 | daemon->forwardtime = now; | |
371 | } | |
372 | } | |
373 | else | |
de37951c | 374 | { |
3be34541 | 375 | start = daemon->servers; |
28866e95 | 376 | if (!option_bool(OPT_ORDER)) |
8ef5ada2 | 377 | forward->forwardall = 1; |
de37951c | 378 | } |
9e4abcb5 SK |
379 | } |
380 | } | |
feba5c1d | 381 | |
9e4abcb5 SK |
382 | /* check for send errors here (no route to host) |
383 | if we fail to send to all nameservers, send back an error | |
384 | packet straight away (helps modem users when offline) */ | |
385 | ||
386 | if (!flags && forward) | |
387 | { | |
de37951c SK |
388 | struct server *firstsentto = start; |
389 | int forwarded = 0; | |
28866e95 | 390 | |
797a7afb | 391 | if (option_bool(OPT_ADD_MAC)) |
60b68069 | 392 | plen = add_mac(header, plen, ((char *) header) + daemon->packet_buff_sz, &forward->source); |
28866e95 | 393 | |
ed4c0767 SK |
394 | if (option_bool(OPT_CLIENT_SUBNET)) |
395 | { | |
60b68069 | 396 | size_t new = add_source_addr(header, plen, ((char *) header) + daemon->packet_buff_sz, &forward->source); |
ed4c0767 SK |
397 | if (new != plen) |
398 | { | |
399 | plen = new; | |
400 | forward->flags |= FREC_HAS_SUBNET; | |
401 | } | |
402 | } | |
403 | ||
3a237152 SK |
404 | #ifdef HAVE_DNSSEC |
405 | if (option_bool(OPT_DNSSEC_VALID)) | |
0fc2f313 | 406 | { |
613ad15d SK |
407 | size_t new_plen = add_do_bit(header, plen, ((char *) header) + daemon->packet_buff_sz); |
408 | ||
5b3bf921 SK |
409 | /* For debugging, set Checking Disabled, otherwise, have the upstream check too, |
410 | this allows it to select auth servers when one is returning bad data. */ | |
411 | if (option_bool(OPT_DNSSEC_DEBUG)) | |
412 | header->hb4 |= HB4_CD; | |
613ad15d SK |
413 | |
414 | if (new_plen != plen) | |
415 | forward->flags |= FREC_ADDED_PHEADER; | |
416 | ||
417 | plen = new_plen; | |
0fc2f313 | 418 | } |
3a237152 SK |
419 | #endif |
420 | ||
9e4abcb5 SK |
421 | while (1) |
422 | { | |
9e4abcb5 SK |
423 | /* only send to servers dealing with our domain. |
424 | domain may be NULL, in which case server->domain | |
425 | must be NULL also. */ | |
426 | ||
de37951c | 427 | if (type == (start->flags & SERV_TYPE) && |
fd9fa481 | 428 | (type != SERV_HAS_DOMAIN || hostname_isequal(domain, start->domain)) && |
b5ea1cc2 | 429 | !(start->flags & (SERV_LITERAL_ADDRESS | SERV_LOOP))) |
9e4abcb5 | 430 | { |
1a6bca81 SK |
431 | int fd; |
432 | ||
433 | /* find server socket to use, may need to get random one. */ | |
434 | if (start->sfd) | |
435 | fd = start->sfd->fd; | |
436 | else | |
437 | { | |
438 | #ifdef HAVE_IPV6 | |
439 | if (start->addr.sa.sa_family == AF_INET6) | |
440 | { | |
441 | if (!forward->rfd6 && | |
442 | !(forward->rfd6 = allocate_rfd(AF_INET6))) | |
443 | break; | |
3927da46 | 444 | daemon->rfd_save = forward->rfd6; |
1a6bca81 SK |
445 | fd = forward->rfd6->fd; |
446 | } | |
447 | else | |
448 | #endif | |
449 | { | |
450 | if (!forward->rfd4 && | |
451 | !(forward->rfd4 = allocate_rfd(AF_INET))) | |
452 | break; | |
3927da46 | 453 | daemon->rfd_save = forward->rfd4; |
1a6bca81 SK |
454 | fd = forward->rfd4->fd; |
455 | } | |
7de060b0 SK |
456 | |
457 | #ifdef HAVE_CONNTRACK | |
458 | /* Copy connection mark of incoming query to outgoing connection. */ | |
459 | if (option_bool(OPT_CONNTRACK)) | |
460 | { | |
461 | unsigned int mark; | |
797a7afb | 462 | if (get_incoming_mark(&forward->source, &forward->dest, 0, &mark)) |
7de060b0 SK |
463 | setsockopt(fd, SOL_SOCKET, SO_MARK, &mark, sizeof(unsigned int)); |
464 | } | |
465 | #endif | |
1a6bca81 SK |
466 | } |
467 | ||
468 | if (sendto(fd, (char *)header, plen, 0, | |
feba5c1d | 469 | &start->addr.sa, |
fd9fa481 SK |
470 | sa_len(&start->addr)) == -1) |
471 | { | |
472 | if (retry_send()) | |
473 | continue; | |
474 | } | |
475 | else | |
9e4abcb5 | 476 | { |
cdeda28f SK |
477 | /* Keep info in case we want to re-send this packet */ |
478 | daemon->srv_save = start; | |
479 | daemon->packet_len = plen; | |
480 | ||
de37951c | 481 | if (!gotname) |
3be34541 | 482 | strcpy(daemon->namebuff, "query"); |
de37951c | 483 | if (start->addr.sa.sa_family == AF_INET) |
3be34541 | 484 | log_query(F_SERVER | F_IPV4 | F_FORWARD, daemon->namebuff, |
1a6bca81 | 485 | (struct all_addr *)&start->addr.in.sin_addr, NULL); |
de37951c SK |
486 | #ifdef HAVE_IPV6 |
487 | else | |
3be34541 | 488 | log_query(F_SERVER | F_IPV6 | F_FORWARD, daemon->namebuff, |
1a6bca81 | 489 | (struct all_addr *)&start->addr.in6.sin6_addr, NULL); |
de37951c | 490 | #endif |
824af85b | 491 | start->queries++; |
de37951c SK |
492 | forwarded = 1; |
493 | forward->sentto = start; | |
0a852541 | 494 | if (!forward->forwardall) |
de37951c | 495 | break; |
0a852541 | 496 | forward->forwardall++; |
9e4abcb5 SK |
497 | } |
498 | } | |
499 | ||
de37951c | 500 | if (!(start = start->next)) |
3be34541 | 501 | start = daemon->servers; |
9e4abcb5 | 502 | |
de37951c | 503 | if (start == firstsentto) |
9e4abcb5 SK |
504 | break; |
505 | } | |
506 | ||
de37951c | 507 | if (forwarded) |
824af85b | 508 | return 1; |
de37951c | 509 | |
9e4abcb5 SK |
510 | /* could not send on, prepare to return */ |
511 | header->id = htons(forward->orig_id); | |
1a6bca81 | 512 | free_frec(forward); /* cancel */ |
9e4abcb5 SK |
513 | } |
514 | ||
515 | /* could not send on, return empty answer or address if known for whole domain */ | |
b8187c80 SK |
516 | if (udpfd != -1) |
517 | { | |
cdeda28f | 518 | plen = setup_reply(header, plen, addrp, flags, daemon->local_ttl); |
54dd393f | 519 | send_from(udpfd, option_bool(OPT_NOWILD) || option_bool(OPT_CLEVERBIND), (char *)header, plen, udpaddr, dst_addr, dst_iface); |
b8187c80 SK |
520 | } |
521 | ||
824af85b | 522 | return 0; |
9e4abcb5 SK |
523 | } |
524 | ||
ed4c0767 | 525 | static size_t process_reply(struct dns_header *header, time_t now, struct server *server, size_t n, int check_rebind, |
613ad15d | 526 | int no_cache, int cache_secure, int ad_reqd, int do_bit, int added_pheader, int check_subnet, union mysockaddr *query_source) |
feba5c1d | 527 | { |
36717eee | 528 | unsigned char *pheader, *sizep; |
13d86c73 | 529 | char **sets = 0; |
832af0ba | 530 | int munged = 0, is_sign; |
cdeda28f SK |
531 | size_t plen; |
532 | ||
83349b8a | 533 | (void)ad_reqd; |
00a5b5d4 | 534 | (void) do_bit; |
83349b8a | 535 | |
13d86c73 | 536 | #ifdef HAVE_IPSET |
82a14af5 | 537 | if (daemon->ipsets && extract_request(header, n, daemon->namebuff, NULL)) |
13d86c73 | 538 | { |
82a14af5 SK |
539 | /* Similar algorithm to search_servers. */ |
540 | struct ipsets *ipset_pos; | |
541 | unsigned int namelen = strlen(daemon->namebuff); | |
542 | unsigned int matchlen = 0; | |
543 | for (ipset_pos = daemon->ipsets; ipset_pos; ipset_pos = ipset_pos->next) | |
6c0cb858 | 544 | { |
82a14af5 SK |
545 | unsigned int domainlen = strlen(ipset_pos->domain); |
546 | char *matchstart = daemon->namebuff + namelen - domainlen; | |
547 | if (namelen >= domainlen && hostname_isequal(matchstart, ipset_pos->domain) && | |
548 | (domainlen == 0 || namelen == domainlen || *(matchstart - 1) == '.' ) && | |
549 | domainlen >= matchlen) | |
550 | { | |
551 | matchlen = domainlen; | |
552 | sets = ipset_pos->sets; | |
553 | } | |
6c0cb858 | 554 | } |
13d86c73 JD |
555 | } |
556 | #endif | |
557 | ||
feba5c1d | 558 | /* If upstream is advertising a larger UDP packet size |
9009d746 SK |
559 | than we allow, trim it so that we don't get overlarge |
560 | requests for the client. We can't do this for signed packets. */ | |
feba5c1d | 561 | |
ed4c0767 | 562 | if ((pheader = find_pseudoheader(header, n, &plen, &sizep, &is_sign))) |
feba5c1d | 563 | { |
83349b8a SK |
564 | unsigned short udpsz; |
565 | unsigned char *psave = sizep; | |
566 | ||
567 | GETSHORT(udpsz, sizep); | |
568 | ||
569 | if (!is_sign && udpsz > daemon->edns_pktsz) | |
570 | PUTSHORT(daemon->edns_pktsz, psave); | |
feba5c1d | 571 | |
ed4c0767 SK |
572 | if (check_subnet && !check_source(header, plen, pheader, query_source)) |
573 | { | |
574 | my_syslog(LOG_WARNING, _("discarding DNS reply: subnet option mismatch")); | |
575 | return 0; | |
576 | } | |
613ad15d SK |
577 | |
578 | if (added_pheader) | |
579 | { | |
580 | pheader = 0; | |
581 | header->arcount = htons(0); | |
582 | } | |
feba5c1d | 583 | } |
83349b8a | 584 | |
28866e95 | 585 | /* RFC 4035 sect 4.6 para 3 */ |
237724c0 | 586 | if (!is_sign && !option_bool(OPT_DNSSEC_PROXY)) |
795501bc | 587 | header->hb4 &= ~HB4_AD; |
3a237152 | 588 | |
572b41eb | 589 | if (OPCODE(header) != QUERY || (RCODE(header) != NOERROR && RCODE(header) != NXDOMAIN)) |
8938ae05 | 590 | return resize_packet(header, n, pheader, plen); |
0a852541 | 591 | |
feba5c1d | 592 | /* Complain loudly if the upstream server is non-recursive. */ |
572b41eb | 593 | if (!(header->hb4 & HB4_RA) && RCODE(header) == NOERROR && ntohs(header->ancount) == 0 && |
0a852541 | 594 | server && !(server->flags & SERV_WARNED_RECURSIVE)) |
feba5c1d | 595 | { |
3d8df260 | 596 | prettyprint_addr(&server->addr, daemon->namebuff); |
f2621c7f | 597 | my_syslog(LOG_WARNING, _("nameserver %s refused to do a recursive query"), daemon->namebuff); |
28866e95 | 598 | if (!option_bool(OPT_LOG)) |
0a852541 SK |
599 | server->flags |= SERV_WARNED_RECURSIVE; |
600 | } | |
e292e93d | 601 | |
572b41eb | 602 | if (daemon->bogus_addr && RCODE(header) != NXDOMAIN && |
fd9fa481 | 603 | check_for_bogus_wildcard(header, n, daemon->namebuff, daemon->bogus_addr, now)) |
feba5c1d | 604 | { |
fd9fa481 | 605 | munged = 1; |
572b41eb SK |
606 | SET_RCODE(header, NXDOMAIN); |
607 | header->hb3 &= ~HB3_AA; | |
6938f347 | 608 | cache_secure = 0; |
36717eee | 609 | } |
fd9fa481 | 610 | else |
36717eee | 611 | { |
6938f347 SK |
612 | int doctored = 0; |
613 | ||
572b41eb | 614 | if (RCODE(header) == NXDOMAIN && |
fd9fa481 | 615 | extract_request(header, n, daemon->namebuff, NULL) && |
5aabfc78 | 616 | check_for_local_domain(daemon->namebuff, now)) |
36717eee SK |
617 | { |
618 | /* if we forwarded a query for a locally known name (because it was for | |
619 | an unknown type) and the answer is NXDOMAIN, convert that to NODATA, | |
620 | since we know that the domain exists, even if upstream doesn't */ | |
fd9fa481 | 621 | munged = 1; |
572b41eb SK |
622 | header->hb3 |= HB3_AA; |
623 | SET_RCODE(header, NOERROR); | |
6938f347 | 624 | cache_secure = 0; |
feba5c1d | 625 | } |
832af0ba | 626 | |
6938f347 | 627 | if (extract_addresses(header, n, daemon->namebuff, now, sets, is_sign, check_rebind, no_cache, cache_secure, &doctored)) |
824af85b | 628 | { |
8ef5ada2 | 629 | my_syslog(LOG_WARNING, _("possible DNS-rebind attack detected: %s"), daemon->namebuff); |
824af85b | 630 | munged = 1; |
6938f347 | 631 | cache_secure = 0; |
824af85b | 632 | } |
6938f347 SK |
633 | |
634 | if (doctored) | |
635 | cache_secure = 0; | |
feba5c1d | 636 | } |
fd9fa481 | 637 | |
a25720a3 SK |
638 | #ifdef HAVE_DNSSEC |
639 | if (no_cache && !(header->hb4 & HB4_CD)) | |
640 | { | |
7d23a66f | 641 | if (!option_bool(OPT_DNSSEC_DEBUG)) |
a25720a3 SK |
642 | { |
643 | /* Bogus reply, turn into SERVFAIL */ | |
644 | SET_RCODE(header, SERVFAIL); | |
645 | munged = 1; | |
646 | } | |
647 | } | |
6938f347 SK |
648 | |
649 | if (option_bool(OPT_DNSSEC_VALID)) | |
650 | header->hb4 &= ~HB4_AD; | |
651 | ||
83349b8a | 652 | if (!(header->hb4 & HB4_CD) && ad_reqd && cache_secure) |
6938f347 | 653 | header->hb4 |= HB4_AD; |
613ad15d SK |
654 | |
655 | /* If the requestor didn't set the DO bit, don't return DNSSEC info. */ | |
656 | if (!do_bit) | |
657 | n = filter_rrsigs(header, n); | |
a25720a3 SK |
658 | #endif |
659 | ||
fd9fa481 SK |
660 | /* do this after extract_addresses. Ensure NODATA reply and remove |
661 | nameserver info. */ | |
662 | ||
663 | if (munged) | |
664 | { | |
665 | header->ancount = htons(0); | |
666 | header->nscount = htons(0); | |
667 | header->arcount = htons(0); | |
668 | } | |
669 | ||
36717eee SK |
670 | /* the bogus-nxdomain stuff, doctor and NXDOMAIN->NODATA munging can all elide |
671 | sections of the packet. Find the new length here and put back pseudoheader | |
672 | if it was removed. */ | |
673 | return resize_packet(header, n, pheader, plen); | |
feba5c1d SK |
674 | } |
675 | ||
3be34541 | 676 | /* sets new last_server */ |
1a6bca81 | 677 | void reply_query(int fd, int family, time_t now) |
9e4abcb5 SK |
678 | { |
679 | /* packet from peer server, extract data for cache, and send to | |
680 | original requester */ | |
572b41eb | 681 | struct dns_header *header; |
de37951c | 682 | union mysockaddr serveraddr; |
832af0ba | 683 | struct frec *forward; |
de37951c | 684 | socklen_t addrlen = sizeof(serveraddr); |
60b68069 | 685 | ssize_t n = recvfrom(fd, daemon->packet, daemon->packet_buff_sz, 0, &serveraddr.sa, &addrlen); |
cdeda28f | 686 | size_t nn; |
1a6bca81 | 687 | struct server *server; |
8a9be9e4 SK |
688 | void *hash; |
689 | #ifndef HAVE_DNSSEC | |
690 | unsigned int crc; | |
691 | #endif | |
692 | ||
cdeda28f SK |
693 | /* packet buffer overwritten */ |
694 | daemon->srv_save = NULL; | |
832af0ba | 695 | |
de37951c | 696 | /* Determine the address of the server replying so that we can mark that as good */ |
1a6bca81 | 697 | serveraddr.sa.sa_family = family; |
de37951c SK |
698 | #ifdef HAVE_IPV6 |
699 | if (serveraddr.sa.sa_family == AF_INET6) | |
5e9e0efb | 700 | serveraddr.in6.sin6_flowinfo = 0; |
de37951c | 701 | #endif |
9e4abcb5 | 702 | |
490f9075 SK |
703 | header = (struct dns_header *)daemon->packet; |
704 | ||
705 | if (n < (int)sizeof(struct dns_header) || !(header->hb3 & HB3_QR)) | |
706 | return; | |
707 | ||
1a6bca81 SK |
708 | /* spoof check: answer must come from known server, */ |
709 | for (server = daemon->servers; server; server = server->next) | |
710 | if (!(server->flags & (SERV_LITERAL_ADDRESS | SERV_NO_ADDR)) && | |
711 | sockaddr_isequal(&server->addr, &serveraddr)) | |
712 | break; | |
490f9075 SK |
713 | |
714 | if (!server) | |
715 | return; | |
716 | ||
8a9be9e4 SK |
717 | #ifdef HAVE_DNSSEC |
718 | hash = hash_questions(header, n, daemon->namebuff); | |
719 | #else | |
720 | hash = &crc; | |
721 | crc = questions_crc(header, n, daemon->namebuff); | |
722 | #endif | |
fd9fa481 | 723 | |
490f9075 | 724 | if (!(forward = lookup_frec(ntohs(header->id), hash))) |
1a6bca81 | 725 | return; |
490f9075 | 726 | |
572b41eb | 727 | if ((RCODE(header) == SERVFAIL || RCODE(header) == REFUSED) && |
28866e95 | 728 | !option_bool(OPT_ORDER) && |
1a6bca81 SK |
729 | forward->forwardall == 0) |
730 | /* for broken servers, attempt to send to another one. */ | |
9e4abcb5 | 731 | { |
1a6bca81 SK |
732 | unsigned char *pheader; |
733 | size_t plen; | |
734 | int is_sign; | |
832af0ba | 735 | |
1a6bca81 SK |
736 | /* recreate query from reply */ |
737 | pheader = find_pseudoheader(header, (size_t)n, &plen, NULL, &is_sign); | |
738 | if (!is_sign) | |
832af0ba | 739 | { |
1a6bca81 SK |
740 | header->ancount = htons(0); |
741 | header->nscount = htons(0); | |
742 | header->arcount = htons(0); | |
743 | if ((nn = resize_packet(header, (size_t)n, pheader, plen))) | |
832af0ba | 744 | { |
572b41eb | 745 | header->hb3 &= ~(HB3_QR | HB3_TC); |
613ad15d | 746 | forward_query(-1, NULL, NULL, 0, header, nn, now, forward, 0, 0); |
1a6bca81 | 747 | return; |
832af0ba | 748 | } |
832af0ba | 749 | } |
1a6bca81 | 750 | } |
3a237152 SK |
751 | |
752 | server = forward->sentto; | |
1a6bca81 SK |
753 | |
754 | if ((forward->sentto->flags & SERV_TYPE) == 0) | |
755 | { | |
51967f98 | 756 | if (RCODE(header) == REFUSED) |
1a6bca81 SK |
757 | server = NULL; |
758 | else | |
b8187c80 | 759 | { |
1a6bca81 SK |
760 | struct server *last_server; |
761 | ||
762 | /* find good server by address if possible, otherwise assume the last one we sent to */ | |
763 | for (last_server = daemon->servers; last_server; last_server = last_server->next) | |
764 | if (!(last_server->flags & (SERV_LITERAL_ADDRESS | SERV_HAS_DOMAIN | SERV_FOR_NODOTS | SERV_NO_ADDR)) && | |
765 | sockaddr_isequal(&last_server->addr, &serveraddr)) | |
766 | { | |
767 | server = last_server; | |
768 | break; | |
769 | } | |
770 | } | |
28866e95 | 771 | if (!option_bool(OPT_ALL_SERVERS)) |
1a6bca81 SK |
772 | daemon->last_server = server; |
773 | } | |
3a237152 | 774 | |
1a6bca81 SK |
775 | /* If the answer is an error, keep the forward record in place in case |
776 | we get a good reply from another server. Kill it when we've | |
777 | had replies from all to avoid filling the forwarding table when | |
778 | everything is broken */ | |
51967f98 | 779 | if (forward->forwardall == 0 || --forward->forwardall == 1 || RCODE(header) != SERVFAIL) |
1a6bca81 | 780 | { |
3a237152 SK |
781 | int check_rebind = 0, no_cache_dnssec = 0, cache_secure = 0; |
782 | ||
783 | if (option_bool(OPT_NO_REBIND)) | |
784 | check_rebind = !(forward->flags & FREC_NOREBIND); | |
785 | ||
786 | /* Don't cache replies where DNSSEC validation was turned off, either | |
787 | the upstream server told us so, or the original query specified it. */ | |
788 | if ((header->hb4 & HB4_CD) || (forward->flags & FREC_CHECKING_DISABLED)) | |
789 | no_cache_dnssec = 1; | |
790 | ||
791 | #ifdef HAVE_DNSSEC | |
51967f98 | 792 | if (server && option_bool(OPT_DNSSEC_VALID) && !(forward->flags & FREC_CHECKING_DISABLED)) |
3a237152 | 793 | { |
9d633048 | 794 | int status; |
0fc2f313 SK |
795 | |
796 | /* We've had a reply already, which we're validating. Ignore this duplicate */ | |
e0c0ad3b | 797 | if (forward->blocking_query) |
0fc2f313 | 798 | return; |
9d633048 | 799 | |
871417d4 SK |
800 | if (header->hb3 & HB3_TC) |
801 | { | |
802 | /* Truncated answer can't be validated. | |
5d3b87a4 SK |
803 | If this is an answer to a DNSSEC-generated query, we still |
804 | need to get the client to retry over TCP, so return | |
805 | an answer with the TC bit set, even if the actual answer fits. | |
806 | */ | |
807 | status = STAT_TRUNCATED; | |
871417d4 SK |
808 | } |
809 | else if (forward->flags & FREC_DNSKEY_QUERY) | |
8d718cbb | 810 | status = dnssec_validate_by_ds(now, header, n, daemon->namebuff, daemon->keyname, forward->class); |
c3e0b9b6 | 811 | else if (forward->flags & FREC_DS_QUERY) |
00a5b5d4 SK |
812 | { |
813 | status = dnssec_validate_ds(now, header, n, daemon->namebuff, daemon->keyname, forward->class); | |
814 | if (status == STAT_NO_DS) | |
815 | status = STAT_INSECURE; | |
816 | } | |
817 | else if (forward->flags & FREC_CHECK_NOSIGN) | |
818 | status = do_check_sign(now, header, n, daemon->namebuff, daemon->keyname, forward->class); | |
9d633048 | 819 | else |
00a5b5d4 SK |
820 | { |
821 | status = dnssec_validate_reply(now, header, n, daemon->namebuff, daemon->keyname, &forward->class, NULL); | |
822 | if (status == STAT_NO_SIG) | |
823 | { | |
824 | if (option_bool(OPT_DNSSEC_NO_SIGN)) | |
825 | status = send_check_sign(now, header, n, daemon->namebuff, daemon->keyname); | |
826 | else | |
827 | status = STAT_INSECURE; | |
828 | } | |
829 | } | |
3a237152 SK |
830 | /* Can't validate, as we're missing key data. Put this |
831 | answer aside, whilst we get that. */ | |
00a5b5d4 | 832 | if (status == STAT_NEED_DS || status == STAT_NEED_DS_NEG || status == STAT_NEED_KEY) |
3a237152 | 833 | { |
7fa836e1 SK |
834 | struct frec *new, *orig; |
835 | ||
836 | /* Free any saved query */ | |
837 | if (forward->stash) | |
838 | blockdata_free(forward->stash); | |
839 | ||
840 | /* Now save reply pending receipt of key data */ | |
841 | if (!(forward->stash = blockdata_alloc((char *)header, n))) | |
842 | return; | |
843 | forward->stash_len = n; | |
0fc2f313 | 844 | |
7fa836e1 SK |
845 | anotherkey: |
846 | /* Find the original query that started it all.... */ | |
847 | for (orig = forward; orig->dependent; orig = orig->dependent); | |
848 | ||
849 | if (--orig->work_counter == 0 || !(new = get_new_frec(now, NULL, 1))) | |
850 | status = STAT_INSECURE; | |
851 | else | |
3a237152 | 852 | { |
7fa836e1 | 853 | int fd; |
0fc2f313 SK |
854 | struct frec *next = new->next; |
855 | *new = *forward; /* copy everything, then overwrite */ | |
856 | new->next = next; | |
0fc2f313 | 857 | new->blocking_query = NULL; |
8a8bbad0 | 858 | new->sentto = server; |
f1668d27 SK |
859 | new->rfd4 = NULL; |
860 | #ifdef HAVE_IPV6 | |
861 | new->rfd6 = NULL; | |
862 | #endif | |
00a5b5d4 | 863 | new->flags &= ~(FREC_DNSKEY_QUERY | FREC_DS_QUERY | FREC_CHECK_NOSIGN); |
9d633048 | 864 | |
7fa836e1 SK |
865 | new->dependent = forward; /* to find query awaiting new one. */ |
866 | forward->blocking_query = new; /* for garbage cleaning */ | |
867 | /* validate routines leave name of required record in daemon->keyname */ | |
868 | if (status == STAT_NEED_KEY) | |
869 | { | |
870 | new->flags |= FREC_DNSKEY_QUERY; | |
871 | nn = dnssec_generate_query(header, ((char *) header) + daemon->packet_buff_sz, | |
872 | daemon->keyname, forward->class, T_DNSKEY, &server->addr); | |
873 | } | |
874 | else | |
875 | { | |
00a5b5d4 SK |
876 | if (status == STAT_NEED_DS_NEG) |
877 | new->flags |= FREC_CHECK_NOSIGN; | |
878 | else | |
879 | new->flags |= FREC_DS_QUERY; | |
7fa836e1 SK |
880 | nn = dnssec_generate_query(header,((char *) header) + daemon->packet_buff_sz, |
881 | daemon->keyname, forward->class, T_DS, &server->addr); | |
882 | } | |
883 | if ((hash = hash_questions(header, nn, daemon->namebuff))) | |
884 | memcpy(new->hash, hash, HASH_SIZE); | |
885 | new->new_id = get_id(); | |
886 | header->id = htons(new->new_id); | |
887 | /* Save query for retransmission */ | |
888 | new->stash = blockdata_alloc((char *)header, nn); | |
889 | new->stash_len = nn; | |
890 | ||
891 | /* Don't resend this. */ | |
892 | daemon->srv_save = NULL; | |
e0c0ad3b | 893 | |
7fa836e1 SK |
894 | if (server->sfd) |
895 | fd = server->sfd->fd; | |
e0c0ad3b | 896 | else |
3a237152 | 897 | { |
7fa836e1 SK |
898 | fd = -1; |
899 | #ifdef HAVE_IPV6 | |
900 | if (server->addr.sa.sa_family == AF_INET6) | |
9d633048 | 901 | { |
7fa836e1 SK |
902 | if (new->rfd6 || (new->rfd6 = allocate_rfd(AF_INET6))) |
903 | fd = new->rfd6->fd; | |
9d633048 | 904 | } |
3a237152 | 905 | else |
3a237152 | 906 | #endif |
f1668d27 | 907 | { |
7fa836e1 SK |
908 | if (new->rfd4 || (new->rfd4 = allocate_rfd(AF_INET))) |
909 | fd = new->rfd4->fd; | |
f1668d27 | 910 | } |
3a237152 | 911 | } |
7fa836e1 SK |
912 | |
913 | if (fd != -1) | |
914 | { | |
915 | while (sendto(fd, (char *)header, nn, 0, &server->addr.sa, sa_len(&server->addr)) == -1 && retry_send()); | |
916 | server->queries++; | |
917 | } | |
918 | ||
919 | return; | |
3a237152 | 920 | } |
3a237152 SK |
921 | } |
922 | ||
923 | /* Ok, we reached far enough up the chain-of-trust that we can validate something. | |
924 | Now wind back down, pulling back answers which wouldn't previously validate | |
7fa836e1 SK |
925 | and validate them with the new data. Note that if an answer needs multiple |
926 | keys to validate, we may find another key is needed, in which case we set off | |
927 | down another branch of the tree. Once we get to the original answer | |
928 | (FREC_DNSSEC_QUERY not set) and it validates, return it to the original requestor. */ | |
0744ca66 | 929 | while (forward->dependent) |
3a237152 | 930 | { |
0744ca66 SK |
931 | struct frec *prev = forward->dependent; |
932 | free_frec(forward); | |
933 | forward = prev; | |
934 | forward->blocking_query = NULL; /* already gone */ | |
935 | blockdata_retrieve(forward->stash, forward->stash_len, (void *)header); | |
936 | n = forward->stash_len; | |
937 | ||
938 | if (status == STAT_SECURE) | |
3a237152 | 939 | { |
0744ca66 SK |
940 | if (forward->flags & FREC_DNSKEY_QUERY) |
941 | status = dnssec_validate_by_ds(now, header, n, daemon->namebuff, daemon->keyname, forward->class); | |
942 | else if (forward->flags & FREC_DS_QUERY) | |
00a5b5d4 SK |
943 | { |
944 | status = dnssec_validate_ds(now, header, n, daemon->namebuff, daemon->keyname, forward->class); | |
945 | if (status == STAT_NO_DS) | |
946 | status = STAT_INSECURE; | |
947 | } | |
948 | else if (forward->flags & FREC_CHECK_NOSIGN) | |
949 | status = do_check_sign(now, header, n, daemon->namebuff, daemon->keyname, forward->class); | |
0744ca66 | 950 | else |
00a5b5d4 SK |
951 | { |
952 | status = dnssec_validate_reply(now, header, n, daemon->namebuff, daemon->keyname, &forward->class, NULL); | |
953 | if (status == STAT_NO_SIG) | |
954 | { | |
955 | if (option_bool(OPT_DNSSEC_NO_SIGN)) | |
956 | status = send_check_sign(now, header, n, daemon->namebuff, daemon->keyname); | |
957 | else | |
958 | status = STAT_INSECURE; | |
959 | } | |
960 | } | |
961 | ||
962 | if (status == STAT_NEED_DS || status == STAT_NEED_DS_NEG || status == STAT_NEED_KEY) | |
7fa836e1 | 963 | goto anotherkey; |
3a237152 SK |
964 | } |
965 | } | |
5d3b87a4 SK |
966 | |
967 | if (status == STAT_TRUNCATED) | |
0744ca66 | 968 | header->hb3 |= HB3_TC; |
5d3b87a4 | 969 | else |
7fa836e1 SK |
970 | { |
971 | char *result; | |
972 | ||
973 | if (forward->work_counter == 0) | |
974 | result = "ABANDONED"; | |
975 | else | |
976 | result = (status == STAT_SECURE ? "SECURE" : (status == STAT_INSECURE ? "INSECURE" : "BOGUS")); | |
977 | ||
978 | log_query(F_KEYTAG | F_SECSTAT, "result", NULL, result); | |
979 | } | |
5d3b87a4 | 980 | |
0fc2f313 | 981 | no_cache_dnssec = 0; |
5d3b87a4 | 982 | |
3a237152 SK |
983 | if (status == STAT_SECURE) |
984 | cache_secure = 1; | |
3a237152 SK |
985 | else if (status == STAT_BOGUS) |
986 | no_cache_dnssec = 1; | |
987 | } | |
83349b8a SK |
988 | #endif |
989 | ||
990 | /* restore CD bit to the value in the query */ | |
991 | if (forward->flags & FREC_CHECKING_DISABLED) | |
992 | header->hb4 |= HB4_CD; | |
993 | else | |
994 | header->hb4 &= ~HB4_CD; | |
8ef5ada2 | 995 | |
3a237152 | 996 | if ((nn = process_reply(header, now, server, (size_t)n, check_rebind, no_cache_dnssec, cache_secure, |
613ad15d SK |
997 | forward->flags & FREC_AD_QUESTION, forward->flags & FREC_DO_QUESTION, |
998 | forward->flags & FREC_ADDED_PHEADER, forward->flags & FREC_HAS_SUBNET, &forward->source))) | |
1a6bca81 SK |
999 | { |
1000 | header->id = htons(forward->orig_id); | |
572b41eb | 1001 | header->hb4 |= HB4_RA; /* recursion if available */ |
54dd393f | 1002 | send_from(forward->fd, option_bool(OPT_NOWILD) || option_bool (OPT_CLEVERBIND), daemon->packet, nn, |
50303b19 | 1003 | &forward->source, &forward->dest, forward->iface); |
b8187c80 | 1004 | } |
1a6bca81 | 1005 | free_frec(forward); /* cancel */ |
9e4abcb5 | 1006 | } |
9e4abcb5 | 1007 | } |
44a2a316 | 1008 | |
1a6bca81 | 1009 | |
5aabfc78 | 1010 | void receive_query(struct listener *listen, time_t now) |
44a2a316 | 1011 | { |
572b41eb | 1012 | struct dns_header *header = (struct dns_header *)daemon->packet; |
44a2a316 | 1013 | union mysockaddr source_addr; |
c1bb8504 | 1014 | unsigned short type; |
44a2a316 | 1015 | struct all_addr dst_addr; |
f6b7dc47 | 1016 | struct in_addr netmask, dst_addr_4; |
cdeda28f SK |
1017 | size_t m; |
1018 | ssize_t n; | |
3b195961 VG |
1019 | int if_index = 0, auth_dns = 0; |
1020 | #ifdef HAVE_AUTH | |
1021 | int local_auth = 0; | |
1022 | #endif | |
44a2a316 SK |
1023 | struct iovec iov[1]; |
1024 | struct msghdr msg; | |
1025 | struct cmsghdr *cmptr; | |
44a2a316 SK |
1026 | union { |
1027 | struct cmsghdr align; /* this ensures alignment */ | |
1028 | #ifdef HAVE_IPV6 | |
1029 | char control6[CMSG_SPACE(sizeof(struct in6_pktinfo))]; | |
1030 | #endif | |
5e9e0efb | 1031 | #if defined(HAVE_LINUX_NETWORK) |
44a2a316 | 1032 | char control[CMSG_SPACE(sizeof(struct in_pktinfo))]; |
824af85b SK |
1033 | #elif defined(IP_RECVDSTADDR) && defined(HAVE_SOLARIS_NETWORK) |
1034 | char control[CMSG_SPACE(sizeof(struct in_addr)) + | |
1035 | CMSG_SPACE(sizeof(unsigned int))]; | |
44a2a316 SK |
1036 | #elif defined(IP_RECVDSTADDR) |
1037 | char control[CMSG_SPACE(sizeof(struct in_addr)) + | |
1038 | CMSG_SPACE(sizeof(struct sockaddr_dl))]; | |
1039 | #endif | |
1040 | } control_u; | |
2329bef5 SK |
1041 | #ifdef HAVE_IPV6 |
1042 | /* Can always get recvd interface for IPv6 */ | |
1043 | int check_dst = !option_bool(OPT_NOWILD) || listen->family == AF_INET6; | |
1044 | #else | |
1045 | int check_dst = !option_bool(OPT_NOWILD); | |
1046 | #endif | |
1047 | ||
cdeda28f SK |
1048 | /* packet buffer overwritten */ |
1049 | daemon->srv_save = NULL; | |
1050 | ||
4f7b304f SK |
1051 | dst_addr_4.s_addr = 0; |
1052 | netmask.s_addr = 0; | |
1053 | ||
7e5664bd | 1054 | if (option_bool(OPT_NOWILD) && listen->iface) |
3d8df260 | 1055 | { |
4f7b304f SK |
1056 | auth_dns = listen->iface->dns_auth; |
1057 | ||
1058 | if (listen->family == AF_INET) | |
1059 | { | |
1060 | dst_addr_4 = listen->iface->addr.in.sin_addr; | |
1061 | netmask = listen->iface->netmask; | |
1062 | } | |
3d8df260 | 1063 | } |
4f7b304f | 1064 | |
3be34541 SK |
1065 | iov[0].iov_base = daemon->packet; |
1066 | iov[0].iov_len = daemon->edns_pktsz; | |
44a2a316 SK |
1067 | |
1068 | msg.msg_control = control_u.control; | |
1069 | msg.msg_controllen = sizeof(control_u); | |
1070 | msg.msg_flags = 0; | |
1071 | msg.msg_name = &source_addr; | |
1072 | msg.msg_namelen = sizeof(source_addr); | |
1073 | msg.msg_iov = iov; | |
1074 | msg.msg_iovlen = 1; | |
1075 | ||
de37951c | 1076 | if ((n = recvmsg(listen->fd, &msg, 0)) == -1) |
3be34541 | 1077 | return; |
44a2a316 | 1078 | |
572b41eb | 1079 | if (n < (int)sizeof(struct dns_header) || |
5e9e0efb | 1080 | (msg.msg_flags & MSG_TRUNC) || |
572b41eb | 1081 | (header->hb3 & HB3_QR)) |
26128d27 SK |
1082 | return; |
1083 | ||
44a2a316 | 1084 | source_addr.sa.sa_family = listen->family; |
2a7a2b84 SK |
1085 | |
1086 | if (listen->family == AF_INET) | |
1087 | { | |
1088 | /* Source-port == 0 is an error, we can't send back to that. | |
1089 | http://www.ietf.org/mail-archive/web/dnsop/current/msg11441.html */ | |
1090 | if (source_addr.in.sin_port == 0) | |
1091 | return; | |
1092 | } | |
44a2a316 | 1093 | #ifdef HAVE_IPV6 |
2a7a2b84 SK |
1094 | else |
1095 | { | |
1096 | /* Source-port == 0 is an error, we can't send back to that. */ | |
1097 | if (source_addr.in6.sin6_port == 0) | |
1098 | return; | |
1099 | source_addr.in6.sin6_flowinfo = 0; | |
1100 | } | |
44a2a316 | 1101 | #endif |
2a7a2b84 | 1102 | |
c8a80487 SK |
1103 | /* We can be configured to only accept queries from at-most-one-hop-away addresses. */ |
1104 | if (option_bool(OPT_LOCAL_SERVICE)) | |
1105 | { | |
1106 | struct addrlist *addr; | |
1107 | #ifdef HAVE_IPV6 | |
1108 | if (listen->family == AF_INET6) | |
1109 | { | |
1110 | for (addr = daemon->interface_addrs; addr; addr = addr->next) | |
1111 | if ((addr->flags & ADDRLIST_IPV6) && | |
1112 | is_same_net6(&addr->addr.addr.addr6, &source_addr.in6.sin6_addr, addr->prefixlen)) | |
1113 | break; | |
1114 | } | |
1115 | else | |
1116 | #endif | |
1117 | { | |
1118 | struct in_addr netmask; | |
1119 | for (addr = daemon->interface_addrs; addr; addr = addr->next) | |
1120 | { | |
1121 | netmask.s_addr = 0xffffffff << (32 - addr->prefixlen); | |
1122 | if (!(addr->flags & ADDRLIST_IPV6) && | |
1123 | is_same_net(addr->addr.addr.addr4, source_addr.in.sin_addr, netmask)) | |
1124 | break; | |
1125 | } | |
1126 | } | |
1127 | if (!addr) | |
1128 | { | |
0c8584ea SK |
1129 | static int warned = 0; |
1130 | if (!warned) | |
1131 | { | |
1132 | my_syslog(LOG_WARNING, _("Ignoring query from non-local network")); | |
1133 | warned = 1; | |
1134 | } | |
c8a80487 SK |
1135 | return; |
1136 | } | |
1137 | } | |
1138 | ||
2329bef5 | 1139 | if (check_dst) |
26128d27 SK |
1140 | { |
1141 | struct ifreq ifr; | |
1142 | ||
1143 | if (msg.msg_controllen < sizeof(struct cmsghdr)) | |
1144 | return; | |
44a2a316 | 1145 | |
5e9e0efb | 1146 | #if defined(HAVE_LINUX_NETWORK) |
26128d27 SK |
1147 | if (listen->family == AF_INET) |
1148 | for (cmptr = CMSG_FIRSTHDR(&msg); cmptr; cmptr = CMSG_NXTHDR(&msg, cmptr)) | |
c72daea8 | 1149 | if (cmptr->cmsg_level == IPPROTO_IP && cmptr->cmsg_type == IP_PKTINFO) |
26128d27 | 1150 | { |
8ef5ada2 SK |
1151 | union { |
1152 | unsigned char *c; | |
1153 | struct in_pktinfo *p; | |
1154 | } p; | |
1155 | p.c = CMSG_DATA(cmptr); | |
1156 | dst_addr_4 = dst_addr.addr.addr4 = p.p->ipi_spec_dst; | |
1157 | if_index = p.p->ipi_ifindex; | |
26128d27 SK |
1158 | } |
1159 | #elif defined(IP_RECVDSTADDR) && defined(IP_RECVIF) | |
1160 | if (listen->family == AF_INET) | |
44a2a316 | 1161 | { |
26128d27 | 1162 | for (cmptr = CMSG_FIRSTHDR(&msg); cmptr; cmptr = CMSG_NXTHDR(&msg, cmptr)) |
8ef5ada2 SK |
1163 | { |
1164 | union { | |
1165 | unsigned char *c; | |
1166 | unsigned int *i; | |
1167 | struct in_addr *a; | |
1168 | #ifndef HAVE_SOLARIS_NETWORK | |
1169 | struct sockaddr_dl *s; | |
1170 | #endif | |
1171 | } p; | |
1172 | p.c = CMSG_DATA(cmptr); | |
1173 | if (cmptr->cmsg_level == IPPROTO_IP && cmptr->cmsg_type == IP_RECVDSTADDR) | |
1174 | dst_addr_4 = dst_addr.addr.addr4 = *(p.a); | |
1175 | else if (cmptr->cmsg_level == IPPROTO_IP && cmptr->cmsg_type == IP_RECVIF) | |
824af85b | 1176 | #ifdef HAVE_SOLARIS_NETWORK |
8ef5ada2 | 1177 | if_index = *(p.i); |
824af85b | 1178 | #else |
8ef5ada2 | 1179 | if_index = p.s->sdl_index; |
824af85b | 1180 | #endif |
8ef5ada2 | 1181 | } |
44a2a316 | 1182 | } |
44a2a316 | 1183 | #endif |
26128d27 | 1184 | |
44a2a316 | 1185 | #ifdef HAVE_IPV6 |
26128d27 SK |
1186 | if (listen->family == AF_INET6) |
1187 | { | |
1188 | for (cmptr = CMSG_FIRSTHDR(&msg); cmptr; cmptr = CMSG_NXTHDR(&msg, cmptr)) | |
c72daea8 | 1189 | if (cmptr->cmsg_level == IPPROTO_IPV6 && cmptr->cmsg_type == daemon->v6pktinfo) |
26128d27 | 1190 | { |
8ef5ada2 SK |
1191 | union { |
1192 | unsigned char *c; | |
1193 | struct in6_pktinfo *p; | |
1194 | } p; | |
1195 | p.c = CMSG_DATA(cmptr); | |
1196 | ||
1197 | dst_addr.addr.addr6 = p.p->ipi6_addr; | |
1198 | if_index = p.p->ipi6_ifindex; | |
26128d27 SK |
1199 | } |
1200 | } | |
44a2a316 | 1201 | #endif |
26128d27 SK |
1202 | |
1203 | /* enforce available interface configuration */ | |
1204 | ||
e25db1f2 | 1205 | if (!indextoname(listen->fd, if_index, ifr.ifr_name)) |
5e9e0efb | 1206 | return; |
832af0ba | 1207 | |
e25db1f2 SK |
1208 | if (!iface_check(listen->family, &dst_addr, ifr.ifr_name, &auth_dns)) |
1209 | { | |
1210 | if (!option_bool(OPT_CLEVERBIND)) | |
115ac3e4 | 1211 | enumerate_interfaces(0); |
3f2873d4 SK |
1212 | if (!loopback_exception(listen->fd, listen->family, &dst_addr, ifr.ifr_name) && |
1213 | !label_exception(if_index, listen->family, &dst_addr)) | |
e25db1f2 SK |
1214 | return; |
1215 | } | |
1216 | ||
552af8b9 SK |
1217 | if (listen->family == AF_INET && option_bool(OPT_LOCALISE)) |
1218 | { | |
1219 | struct irec *iface; | |
1220 | ||
1221 | /* get the netmask of the interface whch has the address we were sent to. | |
1222 | This is no neccessarily the interface we arrived on. */ | |
1223 | ||
1224 | for (iface = daemon->interfaces; iface; iface = iface->next) | |
1225 | if (iface->addr.sa.sa_family == AF_INET && | |
1226 | iface->addr.in.sin_addr.s_addr == dst_addr_4.s_addr) | |
1227 | break; | |
1228 | ||
1229 | /* interface may be new */ | |
e25db1f2 | 1230 | if (!iface && !option_bool(OPT_CLEVERBIND)) |
115ac3e4 | 1231 | enumerate_interfaces(0); |
552af8b9 SK |
1232 | |
1233 | for (iface = daemon->interfaces; iface; iface = iface->next) | |
1234 | if (iface->addr.sa.sa_family == AF_INET && | |
1235 | iface->addr.in.sin_addr.s_addr == dst_addr_4.s_addr) | |
1236 | break; | |
1237 | ||
1238 | /* If we failed, abandon localisation */ | |
1239 | if (iface) | |
1240 | netmask = iface->netmask; | |
1241 | else | |
1242 | dst_addr_4.s_addr = 0; | |
1243 | } | |
44a2a316 SK |
1244 | } |
1245 | ||
cdeda28f | 1246 | if (extract_request(header, (size_t)n, daemon->namebuff, &type)) |
44a2a316 | 1247 | { |
b485ed97 SK |
1248 | #ifdef HAVE_AUTH |
1249 | struct auth_zone *zone; | |
1250 | #endif | |
610e782a SK |
1251 | char *types = querystr(auth_dns ? "auth" : "query", type); |
1252 | ||
44a2a316 | 1253 | if (listen->family == AF_INET) |
3be34541 | 1254 | log_query(F_QUERY | F_IPV4 | F_FORWARD, daemon->namebuff, |
1a6bca81 | 1255 | (struct all_addr *)&source_addr.in.sin_addr, types); |
44a2a316 SK |
1256 | #ifdef HAVE_IPV6 |
1257 | else | |
3be34541 | 1258 | log_query(F_QUERY | F_IPV6 | F_FORWARD, daemon->namebuff, |
1a6bca81 | 1259 | (struct all_addr *)&source_addr.in6.sin6_addr, types); |
44a2a316 | 1260 | #endif |
44a2a316 | 1261 | |
b485ed97 SK |
1262 | #ifdef HAVE_AUTH |
1263 | /* find queries for zones we're authoritative for, and answer them directly */ | |
6008bdbb SK |
1264 | if (!auth_dns) |
1265 | for (zone = daemon->auth_zones; zone; zone = zone->next) | |
1266 | if (in_zone(zone, daemon->namebuff, NULL)) | |
1267 | { | |
1268 | auth_dns = 1; | |
1269 | local_auth = 1; | |
1270 | break; | |
1271 | } | |
b485ed97 | 1272 | #endif |
b5ea1cc2 SK |
1273 | |
1274 | #ifdef HAVE_LOOP | |
1275 | /* Check for forwarding loop */ | |
1276 | if (detect_loop(daemon->namebuff, type)) | |
1277 | return; | |
1278 | #endif | |
b485ed97 SK |
1279 | } |
1280 | ||
4820dce9 | 1281 | #ifdef HAVE_AUTH |
4f7b304f | 1282 | if (auth_dns) |
824af85b | 1283 | { |
60b68069 | 1284 | m = answer_auth(header, ((char *) header) + daemon->packet_buff_sz, (size_t)n, now, &source_addr, local_auth); |
4f7b304f | 1285 | if (m >= 1) |
b485ed97 SK |
1286 | { |
1287 | send_from(listen->fd, option_bool(OPT_NOWILD) || option_bool(OPT_CLEVERBIND), | |
1288 | (char *)header, m, &source_addr, &dst_addr, if_index); | |
1289 | daemon->auth_answer++; | |
1290 | } | |
824af85b | 1291 | } |
44a2a316 | 1292 | else |
4820dce9 | 1293 | #endif |
4f7b304f | 1294 | { |
613ad15d | 1295 | int ad_reqd, do_bit; |
60b68069 | 1296 | m = answer_request(header, ((char *) header) + daemon->packet_buff_sz, (size_t)n, |
613ad15d | 1297 | dst_addr_4, netmask, now, &ad_reqd, &do_bit); |
4f7b304f SK |
1298 | |
1299 | if (m >= 1) | |
1300 | { | |
1301 | send_from(listen->fd, option_bool(OPT_NOWILD) || option_bool(OPT_CLEVERBIND), | |
1302 | (char *)header, m, &source_addr, &dst_addr, if_index); | |
1303 | daemon->local_answer++; | |
1304 | } | |
1305 | else if (forward_query(listen->fd, &source_addr, &dst_addr, if_index, | |
613ad15d | 1306 | header, (size_t)n, now, NULL, ad_reqd, do_bit)) |
4f7b304f SK |
1307 | daemon->queries_forwarded++; |
1308 | else | |
1309 | daemon->local_answer++; | |
1310 | } | |
44a2a316 SK |
1311 | } |
1312 | ||
7d7b7b31 | 1313 | #ifdef HAVE_DNSSEC |
00a5b5d4 SK |
1314 | |
1315 | /* UDP: we've got an unsigned answer, return STAT_INSECURE if we can prove there's no DS | |
1316 | and therefore the answer shouldn't be signed, or STAT_BOGUS if it should be, or | |
1317 | STAT_NEED_DS_NEG and keyname if we need to do the query. */ | |
1318 | static int send_check_sign(time_t now, struct dns_header *header, size_t plen, char *name, char *keyname) | |
1319 | { | |
1320 | struct crec *crecp; | |
1321 | char *name_start = name; | |
1322 | int status = dnssec_chase_cname(now, header, plen, name, keyname); | |
1323 | ||
1324 | if (status != STAT_INSECURE) | |
1325 | return status; | |
1326 | ||
1327 | while (1) | |
1328 | { | |
1329 | crecp = cache_find_by_name(NULL, name_start, now, F_DS); | |
1330 | ||
1331 | if (crecp && (crecp->flags & F_DNSSECOK)) | |
1332 | return (crecp->flags & F_NEG) ? STAT_INSECURE : STAT_BOGUS; | |
1333 | ||
1334 | if (crecp && (crecp->flags & F_NEG) && (name_start = strchr(name_start, '.'))) | |
1335 | { | |
1336 | name_start++; /* chop a label off and try again */ | |
1337 | continue; | |
1338 | } | |
1339 | ||
4e1fe444 SK |
1340 | /* Reached the root */ |
1341 | if (!name_start) | |
1342 | return STAT_BOGUS; | |
1343 | ||
00a5b5d4 SK |
1344 | strcpy(keyname, name_start); |
1345 | return STAT_NEED_DS_NEG; | |
1346 | } | |
1347 | } | |
1348 | ||
1349 | /* Got answer to DS query from send_check_sign, check for proven non-existence, or make the next DS query to try. */ | |
1350 | static int do_check_sign(time_t now, struct dns_header *header, size_t plen, char *name, char *keyname, int class) | |
1351 | ||
1352 | { | |
1353 | char *name_start; | |
1354 | unsigned char *p; | |
4872aa74 SK |
1355 | int status; |
1356 | ||
1357 | /* In this case only, a SERVFAIL reply allows us to continue up the tree, looking for a | |
1358 | suitable NSEC reply to DS queries. */ | |
1359 | if (RCODE(header) != SERVFAIL) | |
1360 | { | |
1361 | status = dnssec_validate_ds(now, header, plen, name, keyname, class); | |
1362 | ||
1363 | if (status != STAT_INSECURE) | |
1364 | { | |
1365 | if (status == STAT_NO_DS) | |
1366 | status = STAT_INSECURE; | |
1367 | return status; | |
1368 | } | |
00a5b5d4 SK |
1369 | } |
1370 | ||
1371 | p = (unsigned char *)(header+1); | |
1372 | ||
1373 | if (extract_name(header, plen, &p, name, 1, 4) && | |
1374 | (name_start = strchr(name, '.'))) | |
1375 | { | |
1376 | name_start++; /* chop a label off and try again */ | |
1377 | strcpy(keyname, name_start); | |
1378 | return STAT_NEED_DS_NEG; | |
1379 | } | |
1380 | ||
1381 | return STAT_BOGUS; | |
1382 | } | |
1383 | ||
1384 | /* Move toward the root, until we find a signed non-existance of a DS, in which case | |
1385 | an unsigned answer is OK, or we find a signed DS, in which case there should be | |
1386 | a signature, and the answer is BOGUS */ | |
1387 | static int tcp_check_for_unsigned_zone(time_t now, struct dns_header *header, size_t plen, int class, char *name, | |
1388 | char *keyname, struct server *server, int *keycount) | |
1389 | { | |
1390 | size_t m; | |
1391 | unsigned char *packet, *payload; | |
1392 | u16 *length; | |
1393 | unsigned char *p = (unsigned char *)(header+1); | |
1394 | int status; | |
1395 | char *name_start = name; | |
1396 | ||
1397 | /* Get first insecure entry in CNAME chain */ | |
1398 | status = tcp_key_recurse(now, STAT_CHASE_CNAME, header, plen, class, name, keyname, server, keycount); | |
1399 | if (status == STAT_BOGUS) | |
1400 | return STAT_BOGUS; | |
1401 | ||
1402 | if (!(packet = whine_malloc(65536 + MAXDNAME + RRFIXEDSZ + sizeof(u16)))) | |
1403 | return STAT_BOGUS; | |
1404 | ||
1405 | payload = &packet[2]; | |
1406 | header = (struct dns_header *)payload; | |
1407 | length = (u16 *)packet; | |
1408 | ||
1409 | while (1) | |
1410 | { | |
1411 | unsigned char *newhash, hash[HASH_SIZE]; | |
1412 | unsigned char c1, c2; | |
1413 | struct crec *crecp = cache_find_by_name(NULL, name_start, now, F_DS); | |
1414 | ||
1415 | if (--(*keycount) == 0) | |
fc2833f1 TH |
1416 | { |
1417 | free(packet); | |
1418 | return STAT_BOGUS; | |
1419 | } | |
1420 | ||
00a5b5d4 SK |
1421 | if (crecp && (crecp->flags & F_DNSSECOK)) |
1422 | { | |
1423 | free(packet); | |
1424 | return (crecp->flags & F_NEG) ? STAT_INSECURE : STAT_BOGUS; | |
1425 | } | |
1426 | ||
1427 | /* If we have cached insecurely that a DS doesn't exist, | |
1428 | ise that is a hit for where to start looking for the secure one */ | |
1429 | if (crecp && (crecp->flags & F_NEG) && (name_start = strchr(name_start, '.'))) | |
1430 | { | |
1431 | name_start++; /* chop a label off and try again */ | |
1432 | continue; | |
1433 | } | |
1434 | ||
4e1fe444 SK |
1435 | /* reached the root */ |
1436 | if (!name_start) | |
1437 | { | |
1438 | free(packet); | |
1439 | return STAT_BOGUS; | |
1440 | } | |
1441 | ||
00a5b5d4 SK |
1442 | m = dnssec_generate_query(header, ((char *) header) + 65536, name_start, class, T_DS, &server->addr); |
1443 | ||
1444 | /* We rely on the question section coming back unchanged, ensure it is with the hash. */ | |
1445 | if ((newhash = hash_questions(header, (unsigned int)m, name))) | |
00a5b5d4 | 1446 | { |
b37f8b99 TH |
1447 | memcpy(hash, newhash, HASH_SIZE); |
1448 | ||
1449 | *length = htons(m); | |
00a5b5d4 | 1450 | |
b37f8b99 TH |
1451 | if (read_write(server->tcpfd, packet, m + sizeof(u16), 0) && |
1452 | read_write(server->tcpfd, &c1, 1, 1) && | |
1453 | read_write(server->tcpfd, &c2, 1, 1) && | |
1454 | read_write(server->tcpfd, payload, (c1 << 8) | c2, 1)) | |
00a5b5d4 | 1455 | { |
b37f8b99 | 1456 | m = (c1 << 8) | c2; |
00a5b5d4 | 1457 | |
b37f8b99 TH |
1458 | newhash = hash_questions(header, (unsigned int)m, name); |
1459 | if (newhash && memcmp(hash, newhash, HASH_SIZE) == 0) | |
00a5b5d4 | 1460 | { |
1fc02680 SK |
1461 | /* In this case only, a SERVFAIL reply allows us to continue up the tree, looking for a |
1462 | suitable NSEC reply to DS queries. */ | |
1463 | if (RCODE(header) == SERVFAIL) | |
1464 | status = STAT_INSECURE; | |
1465 | else | |
1466 | /* Note this trashes all three name workspaces */ | |
1467 | status = tcp_key_recurse(now, STAT_NEED_DS_NEG, header, m, class, name, keyname, server, keycount); | |
00a5b5d4 | 1468 | |
b37f8b99 TH |
1469 | /* We've found a DS which proves the bit of the DNS where the |
1470 | original query is, is unsigned, so the answer is OK, | |
1471 | if unvalidated. */ | |
1472 | if (status == STAT_NO_DS) | |
00a5b5d4 | 1473 | { |
b37f8b99 TH |
1474 | free(packet); |
1475 | return STAT_INSECURE; | |
1476 | } | |
1477 | ||
1478 | /* No DS, not got to DNSSEC-land yet, go up. */ | |
1479 | if (status == STAT_INSECURE) | |
1480 | { | |
1481 | p = (unsigned char *)(header+1); | |
1482 | ||
1483 | if (extract_name(header, plen, &p, name, 1, 4) && | |
1484 | (name_start = strchr(name, '.'))) | |
1485 | { | |
1486 | name_start++; /* chop a label off and try again */ | |
1487 | continue; | |
1488 | } | |
00a5b5d4 SK |
1489 | } |
1490 | } | |
1491 | } | |
1492 | } | |
1493 | ||
1494 | free(packet); | |
1495 | ||
1496 | return STAT_BOGUS; | |
1497 | } | |
1498 | } | |
1499 | ||
7fa836e1 SK |
1500 | static int tcp_key_recurse(time_t now, int status, struct dns_header *header, size_t n, |
1501 | int class, char *name, char *keyname, struct server *server, int *keycount) | |
7d7b7b31 SK |
1502 | { |
1503 | /* Recurse up the key heirarchy */ | |
7d7b7b31 | 1504 | int new_status; |
7d7b7b31 | 1505 | |
7fa836e1 SK |
1506 | /* limit the amount of work we do, to avoid cycling forever on loops in the DNS */ |
1507 | if (--(*keycount) == 0) | |
1508 | return STAT_INSECURE; | |
7d7b7b31 | 1509 | |
7fa836e1 SK |
1510 | if (status == STAT_NEED_KEY) |
1511 | new_status = dnssec_validate_by_ds(now, header, n, name, keyname, class); | |
00a5b5d4 SK |
1512 | else if (status == STAT_NEED_DS || status == STAT_NEED_DS_NEG) |
1513 | { | |
1514 | new_status = dnssec_validate_ds(now, header, n, name, keyname, class); | |
1515 | if (status == STAT_NEED_DS && new_status == STAT_NO_DS) | |
1516 | new_status = STAT_INSECURE; | |
1517 | } | |
1518 | else if (status == STAT_CHASE_CNAME) | |
1519 | new_status = dnssec_chase_cname(now, header, n, name, keyname); | |
1520 | else | |
1521 | { | |
1522 | new_status = dnssec_validate_reply(now, header, n, name, keyname, &class, NULL); | |
1523 | ||
1524 | if (new_status == STAT_NO_SIG) | |
1525 | { | |
1526 | if (option_bool(OPT_DNSSEC_NO_SIGN)) | |
1527 | new_status = tcp_check_for_unsigned_zone(now, header, n, class, name, keyname, server, keycount); | |
1528 | else | |
1529 | new_status = STAT_INSECURE; | |
1530 | } | |
1531 | } | |
1532 | ||
7fa836e1 SK |
1533 | /* Can't validate because we need a key/DS whose name now in keyname. |
1534 | Make query for same, and recurse to validate */ | |
1535 | if (new_status == STAT_NEED_DS || new_status == STAT_NEED_KEY) | |
7d7b7b31 | 1536 | { |
7fa836e1 SK |
1537 | size_t m; |
1538 | unsigned char *packet = whine_malloc(65536 + MAXDNAME + RRFIXEDSZ + sizeof(u16)); | |
1539 | unsigned char *payload = &packet[2]; | |
1540 | struct dns_header *new_header = (struct dns_header *)payload; | |
1541 | u16 *length = (u16 *)packet; | |
1542 | unsigned char c1, c2; | |
1543 | ||
1544 | if (!packet) | |
1545 | return STAT_INSECURE; | |
1546 | ||
1547 | another_tcp_key: | |
1548 | m = dnssec_generate_query(new_header, ((char *) new_header) + 65536, keyname, class, | |
1549 | new_status == STAT_NEED_KEY ? T_DNSKEY : T_DS, &server->addr); | |
7d7b7b31 | 1550 | |
7fa836e1 | 1551 | *length = htons(m); |
7d7b7b31 | 1552 | |
7fa836e1 SK |
1553 | if (!read_write(server->tcpfd, packet, m + sizeof(u16), 0) || |
1554 | !read_write(server->tcpfd, &c1, 1, 1) || | |
1555 | !read_write(server->tcpfd, &c2, 1, 1) || | |
1556 | !read_write(server->tcpfd, payload, (c1 << 8) | c2, 1)) | |
1557 | new_status = STAT_INSECURE; | |
1558 | else | |
7d7b7b31 | 1559 | { |
7fa836e1 SK |
1560 | m = (c1 << 8) | c2; |
1561 | ||
00a5b5d4 SK |
1562 | new_status = tcp_key_recurse(now, new_status, new_header, m, class, name, keyname, server, keycount); |
1563 | ||
1564 | if (new_status == STAT_SECURE) | |
7d7b7b31 | 1565 | { |
7fa836e1 SK |
1566 | /* Reached a validated record, now try again at this level. |
1567 | Note that we may get ANOTHER NEED_* if an answer needs more than one key. | |
1568 | If so, go round again. */ | |
7d7b7b31 | 1569 | |
7fa836e1 SK |
1570 | if (status == STAT_NEED_KEY) |
1571 | new_status = dnssec_validate_by_ds(now, header, n, name, keyname, class); | |
00a5b5d4 SK |
1572 | else if (status == STAT_NEED_DS || status == STAT_NEED_DS_NEG) |
1573 | { | |
1574 | new_status = dnssec_validate_ds(now, header, n, name, keyname, class); | |
1575 | if (status == STAT_NEED_DS && new_status == STAT_NO_DS) | |
1576 | new_status = STAT_INSECURE; /* Validated no DS */ | |
1577 | } | |
1578 | else if (status == STAT_CHASE_CNAME) | |
1579 | new_status = dnssec_chase_cname(now, header, n, name, keyname); | |
1580 | else | |
1581 | { | |
1582 | new_status = dnssec_validate_reply(now, header, n, name, keyname, &class, NULL); | |
1583 | ||
1584 | if (new_status == STAT_NO_SIG) | |
1585 | { | |
1586 | if (option_bool(OPT_DNSSEC_NO_SIGN)) | |
1587 | new_status = tcp_check_for_unsigned_zone(now, header, n, class, name, keyname, server, keycount); | |
1588 | else | |
1589 | new_status = STAT_INSECURE; | |
1590 | } | |
1591 | } | |
1592 | ||
7d7b7b31 | 1593 | if (new_status == STAT_NEED_DS || new_status == STAT_NEED_KEY) |
7fa836e1 | 1594 | goto another_tcp_key; |
7d7b7b31 SK |
1595 | } |
1596 | } | |
7d7b7b31 | 1597 | |
7fa836e1 SK |
1598 | free(packet); |
1599 | } | |
7d7b7b31 SK |
1600 | return new_status; |
1601 | } | |
1602 | #endif | |
1603 | ||
1604 | ||
feba5c1d SK |
1605 | /* The daemon forks before calling this: it should deal with one connection, |
1606 | blocking as neccessary, and then return. Note, need to be a bit careful | |
1607 | about resources for debug mode, when the fork is suppressed: that's | |
1608 | done by the caller. */ | |
5aabfc78 | 1609 | unsigned char *tcp_request(int confd, time_t now, |
4f7b304f | 1610 | union mysockaddr *local_addr, struct in_addr netmask, int auth_dns) |
feba5c1d | 1611 | { |
28866e95 SK |
1612 | size_t size = 0; |
1613 | int norebind = 0; | |
3b195961 | 1614 | #ifdef HAVE_AUTH |
19b16891 | 1615 | int local_auth = 0; |
3b195961 | 1616 | #endif |
613ad15d SK |
1617 | int checking_disabled, ad_question, do_bit, added_pheader = 0; |
1618 | int check_subnet, no_cache_dnssec = 0, cache_secure = 0; | |
cdeda28f | 1619 | size_t m; |
ee86ce68 SK |
1620 | unsigned short qtype; |
1621 | unsigned int gotname; | |
feba5c1d | 1622 | unsigned char c1, c2; |
4b5ea12e SK |
1623 | /* Max TCP packet + slop + size */ |
1624 | unsigned char *packet = whine_malloc(65536 + MAXDNAME + RRFIXEDSZ + sizeof(u16)); | |
1625 | unsigned char *payload = &packet[2]; | |
1626 | /* largest field in header is 16-bits, so this is still sufficiently aligned */ | |
1627 | struct dns_header *header = (struct dns_header *)payload; | |
1628 | u16 *length = (u16 *)packet; | |
3be34541 | 1629 | struct server *last_server; |
7de060b0 SK |
1630 | struct in_addr dst_addr_4; |
1631 | union mysockaddr peer_addr; | |
1632 | socklen_t peer_len = sizeof(union mysockaddr); | |
3be34541 | 1633 | |
7de060b0 SK |
1634 | if (getpeername(confd, (struct sockaddr *)&peer_addr, &peer_len) == -1) |
1635 | return packet; | |
c8a80487 SK |
1636 | |
1637 | /* We can be configured to only accept queries from at-most-one-hop-away addresses. */ | |
1638 | if (option_bool(OPT_LOCAL_SERVICE)) | |
1639 | { | |
1640 | struct addrlist *addr; | |
1641 | #ifdef HAVE_IPV6 | |
1642 | if (peer_addr.sa.sa_family == AF_INET6) | |
1643 | { | |
1644 | for (addr = daemon->interface_addrs; addr; addr = addr->next) | |
1645 | if ((addr->flags & ADDRLIST_IPV6) && | |
1646 | is_same_net6(&addr->addr.addr.addr6, &peer_addr.in6.sin6_addr, addr->prefixlen)) | |
1647 | break; | |
1648 | } | |
1649 | else | |
1650 | #endif | |
1651 | { | |
1652 | struct in_addr netmask; | |
1653 | for (addr = daemon->interface_addrs; addr; addr = addr->next) | |
1654 | { | |
1655 | netmask.s_addr = 0xffffffff << (32 - addr->prefixlen); | |
1656 | if (!(addr->flags & ADDRLIST_IPV6) && | |
1657 | is_same_net(addr->addr.addr.addr4, peer_addr.in.sin_addr, netmask)) | |
1658 | break; | |
1659 | } | |
1660 | } | |
1661 | if (!addr) | |
1662 | { | |
1663 | my_syslog(LOG_WARNING, _("Ignoring query from non-local network")); | |
1664 | return packet; | |
1665 | } | |
1666 | } | |
7de060b0 | 1667 | |
feba5c1d SK |
1668 | while (1) |
1669 | { | |
1670 | if (!packet || | |
1671 | !read_write(confd, &c1, 1, 1) || !read_write(confd, &c2, 1, 1) || | |
1672 | !(size = c1 << 8 | c2) || | |
4b5ea12e | 1673 | !read_write(confd, payload, size, 1)) |
feba5c1d SK |
1674 | return packet; |
1675 | ||
572b41eb | 1676 | if (size < (int)sizeof(struct dns_header)) |
feba5c1d SK |
1677 | continue; |
1678 | ||
ed4c0767 SK |
1679 | check_subnet = 0; |
1680 | ||
28866e95 | 1681 | /* save state of "cd" flag in query */ |
7d7b7b31 SK |
1682 | if ((checking_disabled = header->hb4 & HB4_CD)) |
1683 | no_cache_dnssec = 1; | |
28866e95 | 1684 | |
3be34541 | 1685 | if ((gotname = extract_request(header, (unsigned int)size, daemon->namebuff, &qtype))) |
feba5c1d | 1686 | { |
b485ed97 SK |
1687 | #ifdef HAVE_AUTH |
1688 | struct auth_zone *zone; | |
1689 | #endif | |
610e782a | 1690 | char *types = querystr(auth_dns ? "auth" : "query", qtype); |
7de060b0 SK |
1691 | |
1692 | if (peer_addr.sa.sa_family == AF_INET) | |
1693 | log_query(F_QUERY | F_IPV4 | F_FORWARD, daemon->namebuff, | |
1694 | (struct all_addr *)&peer_addr.in.sin_addr, types); | |
feba5c1d | 1695 | #ifdef HAVE_IPV6 |
7de060b0 SK |
1696 | else |
1697 | log_query(F_QUERY | F_IPV6 | F_FORWARD, daemon->namebuff, | |
1698 | (struct all_addr *)&peer_addr.in6.sin6_addr, types); | |
feba5c1d | 1699 | #endif |
b485ed97 SK |
1700 | |
1701 | #ifdef HAVE_AUTH | |
1702 | /* find queries for zones we're authoritative for, and answer them directly */ | |
6008bdbb SK |
1703 | if (!auth_dns) |
1704 | for (zone = daemon->auth_zones; zone; zone = zone->next) | |
1705 | if (in_zone(zone, daemon->namebuff, NULL)) | |
1706 | { | |
1707 | auth_dns = 1; | |
1708 | local_auth = 1; | |
1709 | break; | |
1710 | } | |
b485ed97 | 1711 | #endif |
feba5c1d SK |
1712 | } |
1713 | ||
7de060b0 SK |
1714 | if (local_addr->sa.sa_family == AF_INET) |
1715 | dst_addr_4 = local_addr->in.sin_addr; | |
1716 | else | |
1717 | dst_addr_4.s_addr = 0; | |
1718 | ||
4820dce9 | 1719 | #ifdef HAVE_AUTH |
4f7b304f | 1720 | if (auth_dns) |
19b16891 | 1721 | m = answer_auth(header, ((char *) header) + 65536, (size_t)size, now, &peer_addr, local_auth); |
4f7b304f | 1722 | else |
4820dce9 | 1723 | #endif |
feba5c1d | 1724 | { |
4f7b304f SK |
1725 | /* m > 0 if answered from cache */ |
1726 | m = answer_request(header, ((char *) header) + 65536, (size_t)size, | |
613ad15d | 1727 | dst_addr_4, netmask, now, &ad_question, &do_bit); |
feba5c1d | 1728 | |
4f7b304f SK |
1729 | /* Do this by steam now we're not in the select() loop */ |
1730 | check_log_writer(NULL); | |
1731 | ||
1732 | if (m == 0) | |
feba5c1d | 1733 | { |
4f7b304f SK |
1734 | unsigned int flags = 0; |
1735 | struct all_addr *addrp = NULL; | |
1736 | int type = 0; | |
1737 | char *domain = NULL; | |
feba5c1d | 1738 | |
4f7b304f SK |
1739 | if (option_bool(OPT_ADD_MAC)) |
1740 | size = add_mac(header, size, ((char *) header) + 65536, &peer_addr); | |
ed4c0767 SK |
1741 | |
1742 | if (option_bool(OPT_CLIENT_SUBNET)) | |
1743 | { | |
1744 | size_t new = add_source_addr(header, size, ((char *) header) + 65536, &peer_addr); | |
1745 | if (size != new) | |
1746 | { | |
1747 | size = new; | |
1748 | check_subnet = 1; | |
1749 | } | |
1750 | } | |
1751 | ||
4f7b304f SK |
1752 | if (gotname) |
1753 | flags = search_servers(now, &addrp, gotname, daemon->namebuff, &type, &domain, &norebind); | |
1754 | ||
1755 | if (type != 0 || option_bool(OPT_ORDER) || !daemon->last_server) | |
1756 | last_server = daemon->servers; | |
1757 | else | |
1758 | last_server = daemon->last_server; | |
1759 | ||
1760 | if (!flags && last_server) | |
1761 | { | |
1762 | struct server *firstsendto = NULL; | |
8a9be9e4 | 1763 | #ifdef HAVE_DNSSEC |
703c7ff4 | 1764 | unsigned char *newhash, hash[HASH_SIZE]; |
63758384 | 1765 | if ((newhash = hash_questions(header, (unsigned int)size, daemon->namebuff))) |
8a9be9e4 | 1766 | memcpy(hash, newhash, HASH_SIZE); |
b37f8b99 TH |
1767 | else |
1768 | memset(hash, 0, HASH_SIZE); | |
8a9be9e4 | 1769 | #else |
4f7b304f | 1770 | unsigned int crc = questions_crc(header, (unsigned int)size, daemon->namebuff); |
8a9be9e4 | 1771 | #endif |
4f7b304f SK |
1772 | /* Loop round available servers until we succeed in connecting to one. |
1773 | Note that this code subtley ensures that consecutive queries on this connection | |
1774 | which can go to the same server, do so. */ | |
1775 | while (1) | |
feba5c1d | 1776 | { |
4f7b304f SK |
1777 | if (!firstsendto) |
1778 | firstsendto = last_server; | |
1779 | else | |
1780 | { | |
1781 | if (!(last_server = last_server->next)) | |
1782 | last_server = daemon->servers; | |
1783 | ||
1784 | if (last_server == firstsendto) | |
1785 | break; | |
1786 | } | |
1787 | ||
1788 | /* server for wrong domain */ | |
1789 | if (type != (last_server->flags & SERV_TYPE) || | |
b5ea1cc2 SK |
1790 | (type == SERV_HAS_DOMAIN && !hostname_isequal(domain, last_server->domain)) || |
1791 | (last_server->flags & (SERV_LITERAL_ADDRESS | SERV_LOOP))) | |
7de060b0 SK |
1792 | continue; |
1793 | ||
4f7b304f | 1794 | if (last_server->tcpfd == -1) |
7de060b0 | 1795 | { |
4f7b304f SK |
1796 | if ((last_server->tcpfd = socket(last_server->addr.sa.sa_family, SOCK_STREAM, 0)) == -1) |
1797 | continue; | |
1798 | ||
1799 | if ((!local_bind(last_server->tcpfd, &last_server->source_addr, last_server->interface, 1) || | |
1800 | connect(last_server->tcpfd, &last_server->addr.sa, sa_len(&last_server->addr)) == -1)) | |
1801 | { | |
1802 | close(last_server->tcpfd); | |
1803 | last_server->tcpfd = -1; | |
1804 | continue; | |
1805 | } | |
1806 | ||
7d7b7b31 SK |
1807 | #ifdef HAVE_DNSSEC |
1808 | if (option_bool(OPT_DNSSEC_VALID)) | |
1809 | { | |
613ad15d SK |
1810 | size_t new_size = add_do_bit(header, size, ((char *) header) + 65536); |
1811 | ||
2ecd9bd5 SK |
1812 | /* For debugging, set Checking Disabled, otherwise, have the upstream check too, |
1813 | this allows it to select auth servers when one is returning bad data. */ | |
1814 | if (option_bool(OPT_DNSSEC_DEBUG)) | |
1815 | header->hb4 |= HB4_CD; | |
613ad15d SK |
1816 | |
1817 | if (size != new_size) | |
1818 | added_pheader = 1; | |
1819 | ||
1820 | size = new_size; | |
7d7b7b31 SK |
1821 | } |
1822 | #endif | |
1823 | ||
7de060b0 | 1824 | #ifdef HAVE_CONNTRACK |
4f7b304f SK |
1825 | /* Copy connection mark of incoming query to outgoing connection. */ |
1826 | if (option_bool(OPT_CONNTRACK)) | |
1827 | { | |
1828 | unsigned int mark; | |
1829 | struct all_addr local; | |
7de060b0 | 1830 | #ifdef HAVE_IPV6 |
4f7b304f SK |
1831 | if (local_addr->sa.sa_family == AF_INET6) |
1832 | local.addr.addr6 = local_addr->in6.sin6_addr; | |
1833 | else | |
7de060b0 | 1834 | #endif |
4f7b304f SK |
1835 | local.addr.addr4 = local_addr->in.sin_addr; |
1836 | ||
1837 | if (get_incoming_mark(&peer_addr, &local, 1, &mark)) | |
1838 | setsockopt(last_server->tcpfd, SOL_SOCKET, SO_MARK, &mark, sizeof(unsigned int)); | |
1839 | } | |
7de060b0 | 1840 | #endif |
4f7b304f SK |
1841 | } |
1842 | ||
4b5ea12e | 1843 | *length = htons(size); |
1fc02680 SK |
1844 | |
1845 | /* get query name again for logging - may have been overwritten */ | |
1846 | if (!(gotname = extract_request(header, (unsigned int)size, daemon->namebuff, &qtype))) | |
1847 | strcpy(daemon->namebuff, "query"); | |
4f7b304f | 1848 | |
4b5ea12e | 1849 | if (!read_write(last_server->tcpfd, packet, size + sizeof(u16), 0) || |
4f7b304f | 1850 | !read_write(last_server->tcpfd, &c1, 1, 1) || |
7d7b7b31 SK |
1851 | !read_write(last_server->tcpfd, &c2, 1, 1) || |
1852 | !read_write(last_server->tcpfd, payload, (c1 << 8) | c2, 1)) | |
4f7b304f SK |
1853 | { |
1854 | close(last_server->tcpfd); | |
1855 | last_server->tcpfd = -1; | |
1856 | continue; | |
1857 | } | |
1858 | ||
1859 | m = (c1 << 8) | c2; | |
4f7b304f | 1860 | |
4f7b304f SK |
1861 | if (last_server->addr.sa.sa_family == AF_INET) |
1862 | log_query(F_SERVER | F_IPV4 | F_FORWARD, daemon->namebuff, | |
1863 | (struct all_addr *)&last_server->addr.in.sin_addr, NULL); | |
feba5c1d | 1864 | #ifdef HAVE_IPV6 |
4f7b304f SK |
1865 | else |
1866 | log_query(F_SERVER | F_IPV6 | F_FORWARD, daemon->namebuff, | |
1867 | (struct all_addr *)&last_server->addr.in6.sin6_addr, NULL); | |
feba5c1d | 1868 | #endif |
7d7b7b31 SK |
1869 | |
1870 | #ifdef HAVE_DNSSEC | |
1871 | if (option_bool(OPT_DNSSEC_VALID) && !checking_disabled) | |
1872 | { | |
7fa836e1 SK |
1873 | int keycount = DNSSEC_WORK; /* Limit to number of DNSSEC questions, to catch loops and avoid filling cache. */ |
1874 | int status = tcp_key_recurse(now, STAT_TRUNCATED, header, m, 0, daemon->namebuff, daemon->keyname, last_server, &keycount); | |
1875 | char *result; | |
1876 | ||
1877 | if (keycount == 0) | |
1878 | result = "ABANDONED"; | |
1879 | else | |
1880 | result = (status == STAT_SECURE ? "SECURE" : (status == STAT_INSECURE ? "INSECURE" : "BOGUS")); | |
7d7b7b31 | 1881 | |
7fa836e1 | 1882 | log_query(F_KEYTAG | F_SECSTAT, "result", NULL, result); |
7d7b7b31 | 1883 | |
7d7b7b31 SK |
1884 | if (status == STAT_BOGUS) |
1885 | no_cache_dnssec = 1; | |
7fa836e1 | 1886 | |
7d7b7b31 SK |
1887 | if (status == STAT_SECURE) |
1888 | cache_secure = 1; | |
1889 | } | |
1890 | #endif | |
1891 | ||
1892 | /* restore CD bit to the value in the query */ | |
1893 | if (checking_disabled) | |
1894 | header->hb4 |= HB4_CD; | |
1895 | else | |
1896 | header->hb4 &= ~HB4_CD; | |
4f7b304f SK |
1897 | |
1898 | /* There's no point in updating the cache, since this process will exit and | |
1899 | lose the information after a few queries. We make this call for the alias and | |
1900 | bogus-nxdomain side-effects. */ | |
1901 | /* If the crc of the question section doesn't match the crc we sent, then | |
1902 | someone might be attempting to insert bogus values into the cache by | |
1903 | sending replies containing questions and bogus answers. */ | |
8a9be9e4 SK |
1904 | #ifdef HAVE_DNSSEC |
1905 | newhash = hash_questions(header, (unsigned int)m, daemon->namebuff); | |
1906 | if (!newhash || memcmp(hash, newhash, HASH_SIZE) != 0) | |
703c7ff4 SK |
1907 | { |
1908 | m = 0; | |
1909 | break; | |
1910 | } | |
8a9be9e4 SK |
1911 | #else |
1912 | if (crc != questions_crc(header, (unsigned int)m, daemon->namebuff)) | |
703c7ff4 SK |
1913 | { |
1914 | m = 0; | |
1915 | break; | |
1916 | } | |
8a9be9e4 SK |
1917 | #endif |
1918 | ||
1919 | m = process_reply(header, now, last_server, (unsigned int)m, | |
1920 | option_bool(OPT_NO_REBIND) && !norebind, no_cache_dnssec, | |
613ad15d | 1921 | cache_secure, ad_question, do_bit, added_pheader, check_subnet, &peer_addr); |
4f7b304f SK |
1922 | |
1923 | break; | |
1924 | } | |
feba5c1d | 1925 | } |
4f7b304f SK |
1926 | |
1927 | /* In case of local answer or no connections made. */ | |
1928 | if (m == 0) | |
1929 | m = setup_reply(header, (unsigned int)size, addrp, flags, daemon->local_ttl); | |
feba5c1d | 1930 | } |
feba5c1d | 1931 | } |
4f7b304f | 1932 | |
5aabfc78 | 1933 | check_log_writer(NULL); |
feba5c1d | 1934 | |
4b5ea12e SK |
1935 | *length = htons(m); |
1936 | ||
1937 | if (m == 0 || !read_write(confd, packet, m + sizeof(u16), 0)) | |
feba5c1d SK |
1938 | return packet; |
1939 | } | |
1940 | } | |
1941 | ||
1697269c | 1942 | static struct frec *allocate_frec(time_t now) |
9e4abcb5 | 1943 | { |
1697269c SK |
1944 | struct frec *f; |
1945 | ||
5aabfc78 | 1946 | if ((f = (struct frec *)whine_malloc(sizeof(struct frec)))) |
9e4abcb5 | 1947 | { |
1a6bca81 | 1948 | f->next = daemon->frec_list; |
1697269c | 1949 | f->time = now; |
832af0ba | 1950 | f->sentto = NULL; |
1a6bca81 | 1951 | f->rfd4 = NULL; |
28866e95 | 1952 | f->flags = 0; |
1a6bca81 SK |
1953 | #ifdef HAVE_IPV6 |
1954 | f->rfd6 = NULL; | |
3a237152 SK |
1955 | #endif |
1956 | #ifdef HAVE_DNSSEC | |
97bc798b | 1957 | f->dependent = NULL; |
3a237152 | 1958 | f->blocking_query = NULL; |
4619d946 | 1959 | f->stash = NULL; |
1a6bca81 SK |
1960 | #endif |
1961 | daemon->frec_list = f; | |
1697269c | 1962 | } |
9e4abcb5 | 1963 | |
1697269c SK |
1964 | return f; |
1965 | } | |
9e4abcb5 | 1966 | |
b5ea1cc2 | 1967 | struct randfd *allocate_rfd(int family) |
1a6bca81 SK |
1968 | { |
1969 | static int finger = 0; | |
1970 | int i; | |
1971 | ||
1972 | /* limit the number of sockets we have open to avoid starvation of | |
1973 | (eg) TFTP. Once we have a reasonable number, randomness should be OK */ | |
1974 | ||
1975 | for (i = 0; i < RANDOM_SOCKS; i++) | |
9009d746 | 1976 | if (daemon->randomsocks[i].refcount == 0) |
1a6bca81 | 1977 | { |
9009d746 SK |
1978 | if ((daemon->randomsocks[i].fd = random_sock(family)) == -1) |
1979 | break; | |
1980 | ||
1a6bca81 SK |
1981 | daemon->randomsocks[i].refcount = 1; |
1982 | daemon->randomsocks[i].family = family; | |
1983 | return &daemon->randomsocks[i]; | |
1984 | } | |
1985 | ||
9009d746 | 1986 | /* No free ones or cannot get new socket, grab an existing one */ |
1a6bca81 SK |
1987 | for (i = 0; i < RANDOM_SOCKS; i++) |
1988 | { | |
1989 | int j = (i+finger) % RANDOM_SOCKS; | |
9009d746 SK |
1990 | if (daemon->randomsocks[j].refcount != 0 && |
1991 | daemon->randomsocks[j].family == family && | |
1992 | daemon->randomsocks[j].refcount != 0xffff) | |
1a6bca81 SK |
1993 | { |
1994 | finger = j; | |
1995 | daemon->randomsocks[j].refcount++; | |
1996 | return &daemon->randomsocks[j]; | |
1997 | } | |
1998 | } | |
1999 | ||
2000 | return NULL; /* doom */ | |
2001 | } | |
b5ea1cc2 SK |
2002 | |
2003 | void free_rfd(struct randfd *rfd) | |
2004 | { | |
2005 | if (rfd && --(rfd->refcount) == 0) | |
2006 | close(rfd->fd); | |
2007 | } | |
2008 | ||
1a6bca81 SK |
2009 | static void free_frec(struct frec *f) |
2010 | { | |
b5ea1cc2 | 2011 | free_rfd(f->rfd4); |
1a6bca81 SK |
2012 | f->rfd4 = NULL; |
2013 | f->sentto = NULL; | |
28866e95 | 2014 | f->flags = 0; |
1a6bca81 SK |
2015 | |
2016 | #ifdef HAVE_IPV6 | |
b5ea1cc2 | 2017 | free_rfd(f->rfd6); |
1a6bca81 SK |
2018 | f->rfd6 = NULL; |
2019 | #endif | |
3a237152 SK |
2020 | |
2021 | #ifdef HAVE_DNSSEC | |
2022 | if (f->stash) | |
0fc2f313 SK |
2023 | { |
2024 | blockdata_free(f->stash); | |
2025 | f->stash = NULL; | |
2026 | } | |
3a237152 SK |
2027 | |
2028 | /* Anything we're waiting on is pointless now, too */ | |
2029 | if (f->blocking_query) | |
2030 | free_frec(f->blocking_query); | |
2031 | f->blocking_query = NULL; | |
39048ad1 | 2032 | f->dependent = NULL; |
3a237152 | 2033 | #endif |
1a6bca81 SK |
2034 | } |
2035 | ||
1697269c SK |
2036 | /* if wait==NULL return a free or older than TIMEOUT record. |
2037 | else return *wait zero if one available, or *wait is delay to | |
1a6bca81 | 2038 | when the oldest in-use record will expire. Impose an absolute |
3a237152 SK |
2039 | limit of 4*TIMEOUT before we wipe things (for random sockets). |
2040 | If force is set, always return a result, even if we have | |
2041 | to allocate above the limit. */ | |
2042 | struct frec *get_new_frec(time_t now, int *wait, int force) | |
1697269c | 2043 | { |
1a6bca81 | 2044 | struct frec *f, *oldest, *target; |
1697269c SK |
2045 | int count; |
2046 | ||
2047 | if (wait) | |
2048 | *wait = 0; | |
2049 | ||
1a6bca81 | 2050 | for (f = daemon->frec_list, oldest = NULL, target = NULL, count = 0; f; f = f->next, count++) |
832af0ba | 2051 | if (!f->sentto) |
1a6bca81 SK |
2052 | target = f; |
2053 | else | |
1697269c | 2054 | { |
1a6bca81 SK |
2055 | if (difftime(now, f->time) >= 4*TIMEOUT) |
2056 | { | |
2057 | free_frec(f); | |
2058 | target = f; | |
2059 | } | |
2060 | ||
2061 | if (!oldest || difftime(f->time, oldest->time) <= 0) | |
2062 | oldest = f; | |
1697269c | 2063 | } |
1a6bca81 SK |
2064 | |
2065 | if (target) | |
2066 | { | |
2067 | target->time = now; | |
2068 | return target; | |
2069 | } | |
9e4abcb5 SK |
2070 | |
2071 | /* can't find empty one, use oldest if there is one | |
2072 | and it's older than timeout */ | |
1697269c | 2073 | if (oldest && ((int)difftime(now, oldest->time)) >= TIMEOUT) |
9e4abcb5 | 2074 | { |
1697269c SK |
2075 | /* keep stuff for twice timeout if we can by allocating a new |
2076 | record instead */ | |
2077 | if (difftime(now, oldest->time) < 2*TIMEOUT && | |
2078 | count <= daemon->ftabsize && | |
2079 | (f = allocate_frec(now))) | |
2080 | return f; | |
2081 | ||
2082 | if (!wait) | |
2083 | { | |
1a6bca81 | 2084 | free_frec(oldest); |
1697269c SK |
2085 | oldest->time = now; |
2086 | } | |
9e4abcb5 SK |
2087 | return oldest; |
2088 | } | |
2089 | ||
1697269c | 2090 | /* none available, calculate time 'till oldest record expires */ |
3a237152 | 2091 | if (!force && count > daemon->ftabsize) |
1697269c | 2092 | { |
0da5e897 MSB |
2093 | static time_t last_log = 0; |
2094 | ||
1697269c SK |
2095 | if (oldest && wait) |
2096 | *wait = oldest->time + (time_t)TIMEOUT - now; | |
0da5e897 MSB |
2097 | |
2098 | if ((int)difftime(now, last_log) > 5) | |
2099 | { | |
2100 | last_log = now; | |
2101 | my_syslog(LOG_WARNING, _("Maximum number of concurrent DNS queries reached (max: %d)"), daemon->ftabsize); | |
2102 | } | |
2103 | ||
9e4abcb5 SK |
2104 | return NULL; |
2105 | } | |
1697269c SK |
2106 | |
2107 | if (!(f = allocate_frec(now)) && wait) | |
2108 | /* wait one second on malloc failure */ | |
2109 | *wait = 1; | |
9e4abcb5 | 2110 | |
9e4abcb5 SK |
2111 | return f; /* OK if malloc fails and this is NULL */ |
2112 | } | |
2113 | ||
832af0ba | 2114 | /* crc is all-ones if not known. */ |
8a9be9e4 | 2115 | static struct frec *lookup_frec(unsigned short id, void *hash) |
9e4abcb5 SK |
2116 | { |
2117 | struct frec *f; | |
2118 | ||
1a6bca81 | 2119 | for(f = daemon->frec_list; f; f = f->next) |
832af0ba | 2120 | if (f->sentto && f->new_id == id && |
8a9be9e4 | 2121 | (!hash || memcmp(hash, f->hash, HASH_SIZE) == 0)) |
9e4abcb5 SK |
2122 | return f; |
2123 | ||
2124 | return NULL; | |
2125 | } | |
2126 | ||
2127 | static struct frec *lookup_frec_by_sender(unsigned short id, | |
fd9fa481 | 2128 | union mysockaddr *addr, |
8a9be9e4 | 2129 | void *hash) |
9e4abcb5 | 2130 | { |
feba5c1d SK |
2131 | struct frec *f; |
2132 | ||
1a6bca81 | 2133 | for(f = daemon->frec_list; f; f = f->next) |
832af0ba | 2134 | if (f->sentto && |
9e4abcb5 | 2135 | f->orig_id == id && |
8a9be9e4 | 2136 | memcmp(hash, f->hash, HASH_SIZE) == 0 && |
9e4abcb5 SK |
2137 | sockaddr_isequal(&f->source, addr)) |
2138 | return f; | |
2139 | ||
2140 | return NULL; | |
2141 | } | |
47a95169 SK |
2142 | |
2143 | /* Send query packet again, if we can. */ | |
2144 | void resend_query() | |
2145 | { | |
2146 | if (daemon->srv_save) | |
2147 | { | |
2148 | int fd; | |
2149 | ||
2150 | if (daemon->srv_save->sfd) | |
2151 | fd = daemon->srv_save->sfd->fd; | |
2152 | else if (daemon->rfd_save && daemon->rfd_save->refcount != 0) | |
2153 | fd = daemon->rfd_save->fd; | |
2154 | else | |
2155 | return; | |
2156 | ||
2157 | while(sendto(fd, daemon->packet, daemon->packet_len, 0, | |
2158 | &daemon->srv_save->addr.sa, sa_len(&daemon->srv_save->addr)) == -1 && retry_send()); | |
2159 | } | |
2160 | } | |
9e4abcb5 | 2161 | |
849a8357 | 2162 | /* A server record is going away, remove references to it */ |
5aabfc78 | 2163 | void server_gone(struct server *server) |
849a8357 SK |
2164 | { |
2165 | struct frec *f; | |
2166 | ||
1a6bca81 | 2167 | for (f = daemon->frec_list; f; f = f->next) |
832af0ba | 2168 | if (f->sentto && f->sentto == server) |
1a6bca81 | 2169 | free_frec(f); |
849a8357 SK |
2170 | |
2171 | if (daemon->last_server == server) | |
2172 | daemon->last_server = NULL; | |
2173 | ||
2174 | if (daemon->srv_save == server) | |
2175 | daemon->srv_save = NULL; | |
2176 | } | |
9e4abcb5 | 2177 | |
316e2730 | 2178 | /* return unique random ids. */ |
8a9be9e4 | 2179 | static unsigned short get_id(void) |
9e4abcb5 SK |
2180 | { |
2181 | unsigned short ret = 0; | |
832af0ba | 2182 | |
316e2730 | 2183 | do |
832af0ba | 2184 | ret = rand16(); |
8a9be9e4 | 2185 | while (lookup_frec(ret, NULL)); |
832af0ba | 2186 | |
9e4abcb5 SK |
2187 | return ret; |
2188 | } | |
2189 | ||
2190 | ||
2191 | ||
2192 | ||
2193 |