]>
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 | |
32fc6dbe GH |
727 | if (daemon->ignore_addr && RCODE(header) == NOERROR && |
728 | check_for_ignored_address(header, n, daemon->ignore_addr)) | |
729 | return; | |
730 | ||
572b41eb | 731 | if ((RCODE(header) == SERVFAIL || RCODE(header) == REFUSED) && |
28866e95 | 732 | !option_bool(OPT_ORDER) && |
1a6bca81 SK |
733 | forward->forwardall == 0) |
734 | /* for broken servers, attempt to send to another one. */ | |
9e4abcb5 | 735 | { |
1a6bca81 SK |
736 | unsigned char *pheader; |
737 | size_t plen; | |
738 | int is_sign; | |
832af0ba | 739 | |
1a6bca81 SK |
740 | /* recreate query from reply */ |
741 | pheader = find_pseudoheader(header, (size_t)n, &plen, NULL, &is_sign); | |
742 | if (!is_sign) | |
832af0ba | 743 | { |
1a6bca81 SK |
744 | header->ancount = htons(0); |
745 | header->nscount = htons(0); | |
746 | header->arcount = htons(0); | |
747 | if ((nn = resize_packet(header, (size_t)n, pheader, plen))) | |
832af0ba | 748 | { |
572b41eb | 749 | header->hb3 &= ~(HB3_QR | HB3_TC); |
613ad15d | 750 | forward_query(-1, NULL, NULL, 0, header, nn, now, forward, 0, 0); |
1a6bca81 | 751 | return; |
832af0ba | 752 | } |
832af0ba | 753 | } |
1a6bca81 | 754 | } |
3a237152 SK |
755 | |
756 | server = forward->sentto; | |
1a6bca81 SK |
757 | |
758 | if ((forward->sentto->flags & SERV_TYPE) == 0) | |
759 | { | |
51967f98 | 760 | if (RCODE(header) == REFUSED) |
1a6bca81 SK |
761 | server = NULL; |
762 | else | |
b8187c80 | 763 | { |
1a6bca81 SK |
764 | struct server *last_server; |
765 | ||
766 | /* find good server by address if possible, otherwise assume the last one we sent to */ | |
767 | for (last_server = daemon->servers; last_server; last_server = last_server->next) | |
768 | if (!(last_server->flags & (SERV_LITERAL_ADDRESS | SERV_HAS_DOMAIN | SERV_FOR_NODOTS | SERV_NO_ADDR)) && | |
769 | sockaddr_isequal(&last_server->addr, &serveraddr)) | |
770 | { | |
771 | server = last_server; | |
772 | break; | |
773 | } | |
774 | } | |
28866e95 | 775 | if (!option_bool(OPT_ALL_SERVERS)) |
1a6bca81 SK |
776 | daemon->last_server = server; |
777 | } | |
3a237152 | 778 | |
1a6bca81 SK |
779 | /* If the answer is an error, keep the forward record in place in case |
780 | we get a good reply from another server. Kill it when we've | |
781 | had replies from all to avoid filling the forwarding table when | |
782 | everything is broken */ | |
51967f98 | 783 | if (forward->forwardall == 0 || --forward->forwardall == 1 || RCODE(header) != SERVFAIL) |
1a6bca81 | 784 | { |
3a237152 SK |
785 | int check_rebind = 0, no_cache_dnssec = 0, cache_secure = 0; |
786 | ||
787 | if (option_bool(OPT_NO_REBIND)) | |
788 | check_rebind = !(forward->flags & FREC_NOREBIND); | |
789 | ||
790 | /* Don't cache replies where DNSSEC validation was turned off, either | |
791 | the upstream server told us so, or the original query specified it. */ | |
792 | if ((header->hb4 & HB4_CD) || (forward->flags & FREC_CHECKING_DISABLED)) | |
793 | no_cache_dnssec = 1; | |
794 | ||
795 | #ifdef HAVE_DNSSEC | |
51967f98 | 796 | if (server && option_bool(OPT_DNSSEC_VALID) && !(forward->flags & FREC_CHECKING_DISABLED)) |
3a237152 | 797 | { |
9d633048 | 798 | int status; |
0fc2f313 SK |
799 | |
800 | /* We've had a reply already, which we're validating. Ignore this duplicate */ | |
e0c0ad3b | 801 | if (forward->blocking_query) |
0fc2f313 | 802 | return; |
9d633048 | 803 | |
871417d4 SK |
804 | if (header->hb3 & HB3_TC) |
805 | { | |
806 | /* Truncated answer can't be validated. | |
5d3b87a4 SK |
807 | If this is an answer to a DNSSEC-generated query, we still |
808 | need to get the client to retry over TCP, so return | |
809 | an answer with the TC bit set, even if the actual answer fits. | |
810 | */ | |
811 | status = STAT_TRUNCATED; | |
871417d4 SK |
812 | } |
813 | else if (forward->flags & FREC_DNSKEY_QUERY) | |
8d718cbb | 814 | status = dnssec_validate_by_ds(now, header, n, daemon->namebuff, daemon->keyname, forward->class); |
c3e0b9b6 | 815 | else if (forward->flags & FREC_DS_QUERY) |
00a5b5d4 SK |
816 | { |
817 | status = dnssec_validate_ds(now, header, n, daemon->namebuff, daemon->keyname, forward->class); | |
818 | if (status == STAT_NO_DS) | |
819 | status = STAT_INSECURE; | |
820 | } | |
821 | else if (forward->flags & FREC_CHECK_NOSIGN) | |
822 | status = do_check_sign(now, header, n, daemon->namebuff, daemon->keyname, forward->class); | |
9d633048 | 823 | else |
00a5b5d4 SK |
824 | { |
825 | status = dnssec_validate_reply(now, header, n, daemon->namebuff, daemon->keyname, &forward->class, NULL); | |
826 | if (status == STAT_NO_SIG) | |
827 | { | |
828 | if (option_bool(OPT_DNSSEC_NO_SIGN)) | |
829 | status = send_check_sign(now, header, n, daemon->namebuff, daemon->keyname); | |
830 | else | |
831 | status = STAT_INSECURE; | |
832 | } | |
833 | } | |
3a237152 SK |
834 | /* Can't validate, as we're missing key data. Put this |
835 | answer aside, whilst we get that. */ | |
00a5b5d4 | 836 | if (status == STAT_NEED_DS || status == STAT_NEED_DS_NEG || status == STAT_NEED_KEY) |
3a237152 | 837 | { |
7fa836e1 SK |
838 | struct frec *new, *orig; |
839 | ||
840 | /* Free any saved query */ | |
841 | if (forward->stash) | |
842 | blockdata_free(forward->stash); | |
843 | ||
844 | /* Now save reply pending receipt of key data */ | |
845 | if (!(forward->stash = blockdata_alloc((char *)header, n))) | |
846 | return; | |
847 | forward->stash_len = n; | |
0fc2f313 | 848 | |
7fa836e1 SK |
849 | anotherkey: |
850 | /* Find the original query that started it all.... */ | |
851 | for (orig = forward; orig->dependent; orig = orig->dependent); | |
852 | ||
853 | if (--orig->work_counter == 0 || !(new = get_new_frec(now, NULL, 1))) | |
854 | status = STAT_INSECURE; | |
855 | else | |
3a237152 | 856 | { |
7fa836e1 | 857 | int fd; |
0fc2f313 SK |
858 | struct frec *next = new->next; |
859 | *new = *forward; /* copy everything, then overwrite */ | |
860 | new->next = next; | |
0fc2f313 | 861 | new->blocking_query = NULL; |
8a8bbad0 | 862 | new->sentto = server; |
f1668d27 SK |
863 | new->rfd4 = NULL; |
864 | #ifdef HAVE_IPV6 | |
865 | new->rfd6 = NULL; | |
866 | #endif | |
00a5b5d4 | 867 | new->flags &= ~(FREC_DNSKEY_QUERY | FREC_DS_QUERY | FREC_CHECK_NOSIGN); |
9d633048 | 868 | |
7fa836e1 SK |
869 | new->dependent = forward; /* to find query awaiting new one. */ |
870 | forward->blocking_query = new; /* for garbage cleaning */ | |
871 | /* validate routines leave name of required record in daemon->keyname */ | |
872 | if (status == STAT_NEED_KEY) | |
873 | { | |
874 | new->flags |= FREC_DNSKEY_QUERY; | |
875 | nn = dnssec_generate_query(header, ((char *) header) + daemon->packet_buff_sz, | |
876 | daemon->keyname, forward->class, T_DNSKEY, &server->addr); | |
877 | } | |
878 | else | |
879 | { | |
00a5b5d4 SK |
880 | if (status == STAT_NEED_DS_NEG) |
881 | new->flags |= FREC_CHECK_NOSIGN; | |
882 | else | |
883 | new->flags |= FREC_DS_QUERY; | |
7fa836e1 SK |
884 | nn = dnssec_generate_query(header,((char *) header) + daemon->packet_buff_sz, |
885 | daemon->keyname, forward->class, T_DS, &server->addr); | |
886 | } | |
887 | if ((hash = hash_questions(header, nn, daemon->namebuff))) | |
888 | memcpy(new->hash, hash, HASH_SIZE); | |
889 | new->new_id = get_id(); | |
890 | header->id = htons(new->new_id); | |
891 | /* Save query for retransmission */ | |
892 | new->stash = blockdata_alloc((char *)header, nn); | |
893 | new->stash_len = nn; | |
894 | ||
895 | /* Don't resend this. */ | |
896 | daemon->srv_save = NULL; | |
e0c0ad3b | 897 | |
7fa836e1 SK |
898 | if (server->sfd) |
899 | fd = server->sfd->fd; | |
e0c0ad3b | 900 | else |
3a237152 | 901 | { |
7fa836e1 SK |
902 | fd = -1; |
903 | #ifdef HAVE_IPV6 | |
904 | if (server->addr.sa.sa_family == AF_INET6) | |
9d633048 | 905 | { |
7fa836e1 SK |
906 | if (new->rfd6 || (new->rfd6 = allocate_rfd(AF_INET6))) |
907 | fd = new->rfd6->fd; | |
9d633048 | 908 | } |
3a237152 | 909 | else |
3a237152 | 910 | #endif |
f1668d27 | 911 | { |
7fa836e1 SK |
912 | if (new->rfd4 || (new->rfd4 = allocate_rfd(AF_INET))) |
913 | fd = new->rfd4->fd; | |
f1668d27 | 914 | } |
3a237152 | 915 | } |
7fa836e1 SK |
916 | |
917 | if (fd != -1) | |
918 | { | |
919 | while (sendto(fd, (char *)header, nn, 0, &server->addr.sa, sa_len(&server->addr)) == -1 && retry_send()); | |
920 | server->queries++; | |
921 | } | |
922 | ||
923 | return; | |
3a237152 | 924 | } |
3a237152 SK |
925 | } |
926 | ||
927 | /* Ok, we reached far enough up the chain-of-trust that we can validate something. | |
928 | Now wind back down, pulling back answers which wouldn't previously validate | |
7fa836e1 SK |
929 | and validate them with the new data. Note that if an answer needs multiple |
930 | keys to validate, we may find another key is needed, in which case we set off | |
931 | down another branch of the tree. Once we get to the original answer | |
932 | (FREC_DNSSEC_QUERY not set) and it validates, return it to the original requestor. */ | |
0744ca66 | 933 | while (forward->dependent) |
3a237152 | 934 | { |
0744ca66 SK |
935 | struct frec *prev = forward->dependent; |
936 | free_frec(forward); | |
937 | forward = prev; | |
938 | forward->blocking_query = NULL; /* already gone */ | |
939 | blockdata_retrieve(forward->stash, forward->stash_len, (void *)header); | |
940 | n = forward->stash_len; | |
941 | ||
942 | if (status == STAT_SECURE) | |
3a237152 | 943 | { |
0744ca66 SK |
944 | if (forward->flags & FREC_DNSKEY_QUERY) |
945 | status = dnssec_validate_by_ds(now, header, n, daemon->namebuff, daemon->keyname, forward->class); | |
946 | else if (forward->flags & FREC_DS_QUERY) | |
00a5b5d4 SK |
947 | { |
948 | status = dnssec_validate_ds(now, header, n, daemon->namebuff, daemon->keyname, forward->class); | |
949 | if (status == STAT_NO_DS) | |
950 | status = STAT_INSECURE; | |
951 | } | |
952 | else if (forward->flags & FREC_CHECK_NOSIGN) | |
953 | status = do_check_sign(now, header, n, daemon->namebuff, daemon->keyname, forward->class); | |
0744ca66 | 954 | else |
00a5b5d4 SK |
955 | { |
956 | status = dnssec_validate_reply(now, header, n, daemon->namebuff, daemon->keyname, &forward->class, NULL); | |
957 | if (status == STAT_NO_SIG) | |
958 | { | |
959 | if (option_bool(OPT_DNSSEC_NO_SIGN)) | |
960 | status = send_check_sign(now, header, n, daemon->namebuff, daemon->keyname); | |
961 | else | |
962 | status = STAT_INSECURE; | |
963 | } | |
964 | } | |
965 | ||
966 | if (status == STAT_NEED_DS || status == STAT_NEED_DS_NEG || status == STAT_NEED_KEY) | |
7fa836e1 | 967 | goto anotherkey; |
3a237152 SK |
968 | } |
969 | } | |
5d3b87a4 SK |
970 | |
971 | if (status == STAT_TRUNCATED) | |
0744ca66 | 972 | header->hb3 |= HB3_TC; |
5d3b87a4 | 973 | else |
7fa836e1 SK |
974 | { |
975 | char *result; | |
976 | ||
977 | if (forward->work_counter == 0) | |
978 | result = "ABANDONED"; | |
979 | else | |
980 | result = (status == STAT_SECURE ? "SECURE" : (status == STAT_INSECURE ? "INSECURE" : "BOGUS")); | |
981 | ||
982 | log_query(F_KEYTAG | F_SECSTAT, "result", NULL, result); | |
983 | } | |
5d3b87a4 | 984 | |
0fc2f313 | 985 | no_cache_dnssec = 0; |
5d3b87a4 | 986 | |
3a237152 SK |
987 | if (status == STAT_SECURE) |
988 | cache_secure = 1; | |
3a237152 SK |
989 | else if (status == STAT_BOGUS) |
990 | no_cache_dnssec = 1; | |
991 | } | |
83349b8a SK |
992 | #endif |
993 | ||
994 | /* restore CD bit to the value in the query */ | |
995 | if (forward->flags & FREC_CHECKING_DISABLED) | |
996 | header->hb4 |= HB4_CD; | |
997 | else | |
998 | header->hb4 &= ~HB4_CD; | |
8ef5ada2 | 999 | |
3a237152 | 1000 | if ((nn = process_reply(header, now, server, (size_t)n, check_rebind, no_cache_dnssec, cache_secure, |
613ad15d SK |
1001 | forward->flags & FREC_AD_QUESTION, forward->flags & FREC_DO_QUESTION, |
1002 | forward->flags & FREC_ADDED_PHEADER, forward->flags & FREC_HAS_SUBNET, &forward->source))) | |
1a6bca81 SK |
1003 | { |
1004 | header->id = htons(forward->orig_id); | |
572b41eb | 1005 | header->hb4 |= HB4_RA; /* recursion if available */ |
54dd393f | 1006 | send_from(forward->fd, option_bool(OPT_NOWILD) || option_bool (OPT_CLEVERBIND), daemon->packet, nn, |
50303b19 | 1007 | &forward->source, &forward->dest, forward->iface); |
b8187c80 | 1008 | } |
1a6bca81 | 1009 | free_frec(forward); /* cancel */ |
9e4abcb5 | 1010 | } |
9e4abcb5 | 1011 | } |
44a2a316 | 1012 | |
1a6bca81 | 1013 | |
5aabfc78 | 1014 | void receive_query(struct listener *listen, time_t now) |
44a2a316 | 1015 | { |
572b41eb | 1016 | struct dns_header *header = (struct dns_header *)daemon->packet; |
44a2a316 | 1017 | union mysockaddr source_addr; |
c1bb8504 | 1018 | unsigned short type; |
44a2a316 | 1019 | struct all_addr dst_addr; |
f6b7dc47 | 1020 | struct in_addr netmask, dst_addr_4; |
cdeda28f SK |
1021 | size_t m; |
1022 | ssize_t n; | |
3b195961 VG |
1023 | int if_index = 0, auth_dns = 0; |
1024 | #ifdef HAVE_AUTH | |
1025 | int local_auth = 0; | |
1026 | #endif | |
44a2a316 SK |
1027 | struct iovec iov[1]; |
1028 | struct msghdr msg; | |
1029 | struct cmsghdr *cmptr; | |
44a2a316 SK |
1030 | union { |
1031 | struct cmsghdr align; /* this ensures alignment */ | |
1032 | #ifdef HAVE_IPV6 | |
1033 | char control6[CMSG_SPACE(sizeof(struct in6_pktinfo))]; | |
1034 | #endif | |
5e9e0efb | 1035 | #if defined(HAVE_LINUX_NETWORK) |
44a2a316 | 1036 | char control[CMSG_SPACE(sizeof(struct in_pktinfo))]; |
824af85b SK |
1037 | #elif defined(IP_RECVDSTADDR) && defined(HAVE_SOLARIS_NETWORK) |
1038 | char control[CMSG_SPACE(sizeof(struct in_addr)) + | |
1039 | CMSG_SPACE(sizeof(unsigned int))]; | |
44a2a316 SK |
1040 | #elif defined(IP_RECVDSTADDR) |
1041 | char control[CMSG_SPACE(sizeof(struct in_addr)) + | |
1042 | CMSG_SPACE(sizeof(struct sockaddr_dl))]; | |
1043 | #endif | |
1044 | } control_u; | |
2329bef5 SK |
1045 | #ifdef HAVE_IPV6 |
1046 | /* Can always get recvd interface for IPv6 */ | |
1047 | int check_dst = !option_bool(OPT_NOWILD) || listen->family == AF_INET6; | |
1048 | #else | |
1049 | int check_dst = !option_bool(OPT_NOWILD); | |
1050 | #endif | |
1051 | ||
cdeda28f SK |
1052 | /* packet buffer overwritten */ |
1053 | daemon->srv_save = NULL; | |
1054 | ||
98906275 | 1055 | dst_addr_4.s_addr = dst_addr.addr.addr4.s_addr = 0; |
4f7b304f SK |
1056 | netmask.s_addr = 0; |
1057 | ||
7e5664bd | 1058 | if (option_bool(OPT_NOWILD) && listen->iface) |
3d8df260 | 1059 | { |
4f7b304f SK |
1060 | auth_dns = listen->iface->dns_auth; |
1061 | ||
1062 | if (listen->family == AF_INET) | |
1063 | { | |
98906275 | 1064 | dst_addr_4 = dst_addr.addr.addr4 = listen->iface->addr.in.sin_addr; |
4f7b304f SK |
1065 | netmask = listen->iface->netmask; |
1066 | } | |
3d8df260 | 1067 | } |
4f7b304f | 1068 | |
3be34541 SK |
1069 | iov[0].iov_base = daemon->packet; |
1070 | iov[0].iov_len = daemon->edns_pktsz; | |
44a2a316 SK |
1071 | |
1072 | msg.msg_control = control_u.control; | |
1073 | msg.msg_controllen = sizeof(control_u); | |
1074 | msg.msg_flags = 0; | |
1075 | msg.msg_name = &source_addr; | |
1076 | msg.msg_namelen = sizeof(source_addr); | |
1077 | msg.msg_iov = iov; | |
1078 | msg.msg_iovlen = 1; | |
1079 | ||
de37951c | 1080 | if ((n = recvmsg(listen->fd, &msg, 0)) == -1) |
3be34541 | 1081 | return; |
44a2a316 | 1082 | |
572b41eb | 1083 | if (n < (int)sizeof(struct dns_header) || |
5e9e0efb | 1084 | (msg.msg_flags & MSG_TRUNC) || |
572b41eb | 1085 | (header->hb3 & HB3_QR)) |
26128d27 SK |
1086 | return; |
1087 | ||
44a2a316 | 1088 | source_addr.sa.sa_family = listen->family; |
2a7a2b84 SK |
1089 | |
1090 | if (listen->family == AF_INET) | |
1091 | { | |
1092 | /* Source-port == 0 is an error, we can't send back to that. | |
1093 | http://www.ietf.org/mail-archive/web/dnsop/current/msg11441.html */ | |
1094 | if (source_addr.in.sin_port == 0) | |
1095 | return; | |
1096 | } | |
44a2a316 | 1097 | #ifdef HAVE_IPV6 |
2a7a2b84 SK |
1098 | else |
1099 | { | |
1100 | /* Source-port == 0 is an error, we can't send back to that. */ | |
1101 | if (source_addr.in6.sin6_port == 0) | |
1102 | return; | |
1103 | source_addr.in6.sin6_flowinfo = 0; | |
1104 | } | |
44a2a316 | 1105 | #endif |
2a7a2b84 | 1106 | |
c8a80487 SK |
1107 | /* We can be configured to only accept queries from at-most-one-hop-away addresses. */ |
1108 | if (option_bool(OPT_LOCAL_SERVICE)) | |
1109 | { | |
1110 | struct addrlist *addr; | |
1111 | #ifdef HAVE_IPV6 | |
1112 | if (listen->family == AF_INET6) | |
1113 | { | |
1114 | for (addr = daemon->interface_addrs; addr; addr = addr->next) | |
1115 | if ((addr->flags & ADDRLIST_IPV6) && | |
1116 | is_same_net6(&addr->addr.addr.addr6, &source_addr.in6.sin6_addr, addr->prefixlen)) | |
1117 | break; | |
1118 | } | |
1119 | else | |
1120 | #endif | |
1121 | { | |
1122 | struct in_addr netmask; | |
1123 | for (addr = daemon->interface_addrs; addr; addr = addr->next) | |
1124 | { | |
15b1b7e9 | 1125 | netmask.s_addr = htonl(~(in_addr_t)0 << (32 - addr->prefixlen)); |
c8a80487 SK |
1126 | if (!(addr->flags & ADDRLIST_IPV6) && |
1127 | is_same_net(addr->addr.addr.addr4, source_addr.in.sin_addr, netmask)) | |
1128 | break; | |
1129 | } | |
1130 | } | |
1131 | if (!addr) | |
1132 | { | |
0c8584ea SK |
1133 | static int warned = 0; |
1134 | if (!warned) | |
1135 | { | |
1136 | my_syslog(LOG_WARNING, _("Ignoring query from non-local network")); | |
1137 | warned = 1; | |
1138 | } | |
c8a80487 SK |
1139 | return; |
1140 | } | |
1141 | } | |
1142 | ||
2329bef5 | 1143 | if (check_dst) |
26128d27 SK |
1144 | { |
1145 | struct ifreq ifr; | |
1146 | ||
1147 | if (msg.msg_controllen < sizeof(struct cmsghdr)) | |
1148 | return; | |
44a2a316 | 1149 | |
5e9e0efb | 1150 | #if defined(HAVE_LINUX_NETWORK) |
26128d27 SK |
1151 | if (listen->family == AF_INET) |
1152 | for (cmptr = CMSG_FIRSTHDR(&msg); cmptr; cmptr = CMSG_NXTHDR(&msg, cmptr)) | |
c72daea8 | 1153 | if (cmptr->cmsg_level == IPPROTO_IP && cmptr->cmsg_type == IP_PKTINFO) |
26128d27 | 1154 | { |
8ef5ada2 SK |
1155 | union { |
1156 | unsigned char *c; | |
1157 | struct in_pktinfo *p; | |
1158 | } p; | |
1159 | p.c = CMSG_DATA(cmptr); | |
1160 | dst_addr_4 = dst_addr.addr.addr4 = p.p->ipi_spec_dst; | |
1161 | if_index = p.p->ipi_ifindex; | |
26128d27 SK |
1162 | } |
1163 | #elif defined(IP_RECVDSTADDR) && defined(IP_RECVIF) | |
1164 | if (listen->family == AF_INET) | |
44a2a316 | 1165 | { |
26128d27 | 1166 | for (cmptr = CMSG_FIRSTHDR(&msg); cmptr; cmptr = CMSG_NXTHDR(&msg, cmptr)) |
8ef5ada2 SK |
1167 | { |
1168 | union { | |
1169 | unsigned char *c; | |
1170 | unsigned int *i; | |
1171 | struct in_addr *a; | |
1172 | #ifndef HAVE_SOLARIS_NETWORK | |
1173 | struct sockaddr_dl *s; | |
1174 | #endif | |
1175 | } p; | |
1176 | p.c = CMSG_DATA(cmptr); | |
1177 | if (cmptr->cmsg_level == IPPROTO_IP && cmptr->cmsg_type == IP_RECVDSTADDR) | |
1178 | dst_addr_4 = dst_addr.addr.addr4 = *(p.a); | |
1179 | else if (cmptr->cmsg_level == IPPROTO_IP && cmptr->cmsg_type == IP_RECVIF) | |
824af85b | 1180 | #ifdef HAVE_SOLARIS_NETWORK |
8ef5ada2 | 1181 | if_index = *(p.i); |
824af85b | 1182 | #else |
8ef5ada2 | 1183 | if_index = p.s->sdl_index; |
824af85b | 1184 | #endif |
8ef5ada2 | 1185 | } |
44a2a316 | 1186 | } |
44a2a316 | 1187 | #endif |
26128d27 | 1188 | |
44a2a316 | 1189 | #ifdef HAVE_IPV6 |
26128d27 SK |
1190 | if (listen->family == AF_INET6) |
1191 | { | |
1192 | for (cmptr = CMSG_FIRSTHDR(&msg); cmptr; cmptr = CMSG_NXTHDR(&msg, cmptr)) | |
c72daea8 | 1193 | if (cmptr->cmsg_level == IPPROTO_IPV6 && cmptr->cmsg_type == daemon->v6pktinfo) |
26128d27 | 1194 | { |
8ef5ada2 SK |
1195 | union { |
1196 | unsigned char *c; | |
1197 | struct in6_pktinfo *p; | |
1198 | } p; | |
1199 | p.c = CMSG_DATA(cmptr); | |
1200 | ||
1201 | dst_addr.addr.addr6 = p.p->ipi6_addr; | |
1202 | if_index = p.p->ipi6_ifindex; | |
26128d27 SK |
1203 | } |
1204 | } | |
44a2a316 | 1205 | #endif |
26128d27 SK |
1206 | |
1207 | /* enforce available interface configuration */ | |
1208 | ||
e25db1f2 | 1209 | if (!indextoname(listen->fd, if_index, ifr.ifr_name)) |
5e9e0efb | 1210 | return; |
832af0ba | 1211 | |
e25db1f2 SK |
1212 | if (!iface_check(listen->family, &dst_addr, ifr.ifr_name, &auth_dns)) |
1213 | { | |
1214 | if (!option_bool(OPT_CLEVERBIND)) | |
115ac3e4 | 1215 | enumerate_interfaces(0); |
3f2873d4 SK |
1216 | if (!loopback_exception(listen->fd, listen->family, &dst_addr, ifr.ifr_name) && |
1217 | !label_exception(if_index, listen->family, &dst_addr)) | |
e25db1f2 SK |
1218 | return; |
1219 | } | |
1220 | ||
552af8b9 SK |
1221 | if (listen->family == AF_INET && option_bool(OPT_LOCALISE)) |
1222 | { | |
1223 | struct irec *iface; | |
1224 | ||
1225 | /* get the netmask of the interface whch has the address we were sent to. | |
1226 | This is no neccessarily the interface we arrived on. */ | |
1227 | ||
1228 | for (iface = daemon->interfaces; iface; iface = iface->next) | |
1229 | if (iface->addr.sa.sa_family == AF_INET && | |
1230 | iface->addr.in.sin_addr.s_addr == dst_addr_4.s_addr) | |
1231 | break; | |
1232 | ||
1233 | /* interface may be new */ | |
e25db1f2 | 1234 | if (!iface && !option_bool(OPT_CLEVERBIND)) |
115ac3e4 | 1235 | enumerate_interfaces(0); |
552af8b9 SK |
1236 | |
1237 | for (iface = daemon->interfaces; iface; iface = iface->next) | |
1238 | if (iface->addr.sa.sa_family == AF_INET && | |
1239 | iface->addr.in.sin_addr.s_addr == dst_addr_4.s_addr) | |
1240 | break; | |
1241 | ||
1242 | /* If we failed, abandon localisation */ | |
1243 | if (iface) | |
1244 | netmask = iface->netmask; | |
1245 | else | |
1246 | dst_addr_4.s_addr = 0; | |
1247 | } | |
44a2a316 SK |
1248 | } |
1249 | ||
cdeda28f | 1250 | if (extract_request(header, (size_t)n, daemon->namebuff, &type)) |
44a2a316 | 1251 | { |
b485ed97 SK |
1252 | #ifdef HAVE_AUTH |
1253 | struct auth_zone *zone; | |
1254 | #endif | |
610e782a SK |
1255 | char *types = querystr(auth_dns ? "auth" : "query", type); |
1256 | ||
44a2a316 | 1257 | if (listen->family == AF_INET) |
3be34541 | 1258 | log_query(F_QUERY | F_IPV4 | F_FORWARD, daemon->namebuff, |
1a6bca81 | 1259 | (struct all_addr *)&source_addr.in.sin_addr, types); |
44a2a316 SK |
1260 | #ifdef HAVE_IPV6 |
1261 | else | |
3be34541 | 1262 | log_query(F_QUERY | F_IPV6 | F_FORWARD, daemon->namebuff, |
1a6bca81 | 1263 | (struct all_addr *)&source_addr.in6.sin6_addr, types); |
44a2a316 | 1264 | #endif |
44a2a316 | 1265 | |
b485ed97 SK |
1266 | #ifdef HAVE_AUTH |
1267 | /* find queries for zones we're authoritative for, and answer them directly */ | |
6008bdbb SK |
1268 | if (!auth_dns) |
1269 | for (zone = daemon->auth_zones; zone; zone = zone->next) | |
1270 | if (in_zone(zone, daemon->namebuff, NULL)) | |
1271 | { | |
1272 | auth_dns = 1; | |
1273 | local_auth = 1; | |
1274 | break; | |
1275 | } | |
b485ed97 | 1276 | #endif |
b5ea1cc2 SK |
1277 | |
1278 | #ifdef HAVE_LOOP | |
1279 | /* Check for forwarding loop */ | |
1280 | if (detect_loop(daemon->namebuff, type)) | |
1281 | return; | |
1282 | #endif | |
b485ed97 SK |
1283 | } |
1284 | ||
4820dce9 | 1285 | #ifdef HAVE_AUTH |
4f7b304f | 1286 | if (auth_dns) |
824af85b | 1287 | { |
60b68069 | 1288 | m = answer_auth(header, ((char *) header) + daemon->packet_buff_sz, (size_t)n, now, &source_addr, local_auth); |
4f7b304f | 1289 | if (m >= 1) |
b485ed97 SK |
1290 | { |
1291 | send_from(listen->fd, option_bool(OPT_NOWILD) || option_bool(OPT_CLEVERBIND), | |
1292 | (char *)header, m, &source_addr, &dst_addr, if_index); | |
1293 | daemon->auth_answer++; | |
1294 | } | |
824af85b | 1295 | } |
44a2a316 | 1296 | else |
4820dce9 | 1297 | #endif |
4f7b304f | 1298 | { |
613ad15d | 1299 | int ad_reqd, do_bit; |
60b68069 | 1300 | m = answer_request(header, ((char *) header) + daemon->packet_buff_sz, (size_t)n, |
613ad15d | 1301 | dst_addr_4, netmask, now, &ad_reqd, &do_bit); |
4f7b304f SK |
1302 | |
1303 | if (m >= 1) | |
1304 | { | |
1305 | send_from(listen->fd, option_bool(OPT_NOWILD) || option_bool(OPT_CLEVERBIND), | |
1306 | (char *)header, m, &source_addr, &dst_addr, if_index); | |
1307 | daemon->local_answer++; | |
1308 | } | |
1309 | else if (forward_query(listen->fd, &source_addr, &dst_addr, if_index, | |
613ad15d | 1310 | header, (size_t)n, now, NULL, ad_reqd, do_bit)) |
4f7b304f SK |
1311 | daemon->queries_forwarded++; |
1312 | else | |
1313 | daemon->local_answer++; | |
1314 | } | |
44a2a316 SK |
1315 | } |
1316 | ||
7d7b7b31 | 1317 | #ifdef HAVE_DNSSEC |
00a5b5d4 SK |
1318 | |
1319 | /* UDP: we've got an unsigned answer, return STAT_INSECURE if we can prove there's no DS | |
1320 | and therefore the answer shouldn't be signed, or STAT_BOGUS if it should be, or | |
1321 | STAT_NEED_DS_NEG and keyname if we need to do the query. */ | |
1322 | static int send_check_sign(time_t now, struct dns_header *header, size_t plen, char *name, char *keyname) | |
1323 | { | |
1324 | struct crec *crecp; | |
1325 | char *name_start = name; | |
1326 | int status = dnssec_chase_cname(now, header, plen, name, keyname); | |
1327 | ||
1328 | if (status != STAT_INSECURE) | |
1329 | return status; | |
1330 | ||
1331 | while (1) | |
1332 | { | |
1333 | crecp = cache_find_by_name(NULL, name_start, now, F_DS); | |
1334 | ||
1335 | if (crecp && (crecp->flags & F_DNSSECOK)) | |
1336 | return (crecp->flags & F_NEG) ? STAT_INSECURE : STAT_BOGUS; | |
1337 | ||
1338 | if (crecp && (crecp->flags & F_NEG) && (name_start = strchr(name_start, '.'))) | |
1339 | { | |
1340 | name_start++; /* chop a label off and try again */ | |
1341 | continue; | |
1342 | } | |
1343 | ||
4e1fe444 SK |
1344 | /* Reached the root */ |
1345 | if (!name_start) | |
1346 | return STAT_BOGUS; | |
1347 | ||
00a5b5d4 SK |
1348 | strcpy(keyname, name_start); |
1349 | return STAT_NEED_DS_NEG; | |
1350 | } | |
1351 | } | |
1352 | ||
1353 | /* Got answer to DS query from send_check_sign, check for proven non-existence, or make the next DS query to try. */ | |
1354 | static int do_check_sign(time_t now, struct dns_header *header, size_t plen, char *name, char *keyname, int class) | |
1355 | ||
1356 | { | |
1357 | char *name_start; | |
1358 | unsigned char *p; | |
4872aa74 SK |
1359 | int status; |
1360 | ||
1361 | /* In this case only, a SERVFAIL reply allows us to continue up the tree, looking for a | |
1362 | suitable NSEC reply to DS queries. */ | |
1363 | if (RCODE(header) != SERVFAIL) | |
1364 | { | |
1365 | status = dnssec_validate_ds(now, header, plen, name, keyname, class); | |
1366 | ||
1367 | if (status != STAT_INSECURE) | |
1368 | { | |
1369 | if (status == STAT_NO_DS) | |
1370 | status = STAT_INSECURE; | |
1371 | return status; | |
1372 | } | |
00a5b5d4 SK |
1373 | } |
1374 | ||
1375 | p = (unsigned char *)(header+1); | |
1376 | ||
1377 | if (extract_name(header, plen, &p, name, 1, 4) && | |
1378 | (name_start = strchr(name, '.'))) | |
1379 | { | |
1380 | name_start++; /* chop a label off and try again */ | |
1381 | strcpy(keyname, name_start); | |
1382 | return STAT_NEED_DS_NEG; | |
1383 | } | |
1384 | ||
1385 | return STAT_BOGUS; | |
1386 | } | |
1387 | ||
1388 | /* Move toward the root, until we find a signed non-existance of a DS, in which case | |
1389 | an unsigned answer is OK, or we find a signed DS, in which case there should be | |
1390 | a signature, and the answer is BOGUS */ | |
1391 | static int tcp_check_for_unsigned_zone(time_t now, struct dns_header *header, size_t plen, int class, char *name, | |
1392 | char *keyname, struct server *server, int *keycount) | |
1393 | { | |
1394 | size_t m; | |
1395 | unsigned char *packet, *payload; | |
1396 | u16 *length; | |
1397 | unsigned char *p = (unsigned char *)(header+1); | |
1398 | int status; | |
1399 | char *name_start = name; | |
1400 | ||
1401 | /* Get first insecure entry in CNAME chain */ | |
1402 | status = tcp_key_recurse(now, STAT_CHASE_CNAME, header, plen, class, name, keyname, server, keycount); | |
1403 | if (status == STAT_BOGUS) | |
1404 | return STAT_BOGUS; | |
1405 | ||
1406 | if (!(packet = whine_malloc(65536 + MAXDNAME + RRFIXEDSZ + sizeof(u16)))) | |
1407 | return STAT_BOGUS; | |
1408 | ||
1409 | payload = &packet[2]; | |
1410 | header = (struct dns_header *)payload; | |
1411 | length = (u16 *)packet; | |
1412 | ||
1413 | while (1) | |
1414 | { | |
1415 | unsigned char *newhash, hash[HASH_SIZE]; | |
1416 | unsigned char c1, c2; | |
1417 | struct crec *crecp = cache_find_by_name(NULL, name_start, now, F_DS); | |
1418 | ||
1419 | if (--(*keycount) == 0) | |
fc2833f1 TH |
1420 | { |
1421 | free(packet); | |
1422 | return STAT_BOGUS; | |
1423 | } | |
1424 | ||
00a5b5d4 SK |
1425 | if (crecp && (crecp->flags & F_DNSSECOK)) |
1426 | { | |
1427 | free(packet); | |
1428 | return (crecp->flags & F_NEG) ? STAT_INSECURE : STAT_BOGUS; | |
1429 | } | |
1430 | ||
1431 | /* If we have cached insecurely that a DS doesn't exist, | |
1432 | ise that is a hit for where to start looking for the secure one */ | |
1433 | if (crecp && (crecp->flags & F_NEG) && (name_start = strchr(name_start, '.'))) | |
1434 | { | |
1435 | name_start++; /* chop a label off and try again */ | |
1436 | continue; | |
1437 | } | |
1438 | ||
4e1fe444 SK |
1439 | /* reached the root */ |
1440 | if (!name_start) | |
1441 | { | |
1442 | free(packet); | |
1443 | return STAT_BOGUS; | |
1444 | } | |
1445 | ||
00a5b5d4 SK |
1446 | m = dnssec_generate_query(header, ((char *) header) + 65536, name_start, class, T_DS, &server->addr); |
1447 | ||
1448 | /* We rely on the question section coming back unchanged, ensure it is with the hash. */ | |
1449 | if ((newhash = hash_questions(header, (unsigned int)m, name))) | |
00a5b5d4 | 1450 | { |
b37f8b99 TH |
1451 | memcpy(hash, newhash, HASH_SIZE); |
1452 | ||
1453 | *length = htons(m); | |
00a5b5d4 | 1454 | |
b37f8b99 TH |
1455 | if (read_write(server->tcpfd, packet, m + sizeof(u16), 0) && |
1456 | read_write(server->tcpfd, &c1, 1, 1) && | |
1457 | read_write(server->tcpfd, &c2, 1, 1) && | |
1458 | read_write(server->tcpfd, payload, (c1 << 8) | c2, 1)) | |
00a5b5d4 | 1459 | { |
b37f8b99 | 1460 | m = (c1 << 8) | c2; |
00a5b5d4 | 1461 | |
b37f8b99 TH |
1462 | newhash = hash_questions(header, (unsigned int)m, name); |
1463 | if (newhash && memcmp(hash, newhash, HASH_SIZE) == 0) | |
00a5b5d4 | 1464 | { |
1fc02680 SK |
1465 | /* In this case only, a SERVFAIL reply allows us to continue up the tree, looking for a |
1466 | suitable NSEC reply to DS queries. */ | |
1467 | if (RCODE(header) == SERVFAIL) | |
1468 | status = STAT_INSECURE; | |
1469 | else | |
1470 | /* Note this trashes all three name workspaces */ | |
1471 | status = tcp_key_recurse(now, STAT_NEED_DS_NEG, header, m, class, name, keyname, server, keycount); | |
00a5b5d4 | 1472 | |
b37f8b99 TH |
1473 | /* We've found a DS which proves the bit of the DNS where the |
1474 | original query is, is unsigned, so the answer is OK, | |
1475 | if unvalidated. */ | |
1476 | if (status == STAT_NO_DS) | |
00a5b5d4 | 1477 | { |
b37f8b99 TH |
1478 | free(packet); |
1479 | return STAT_INSECURE; | |
1480 | } | |
1481 | ||
1482 | /* No DS, not got to DNSSEC-land yet, go up. */ | |
1483 | if (status == STAT_INSECURE) | |
1484 | { | |
1485 | p = (unsigned char *)(header+1); | |
1486 | ||
1487 | if (extract_name(header, plen, &p, name, 1, 4) && | |
1488 | (name_start = strchr(name, '.'))) | |
1489 | { | |
1490 | name_start++; /* chop a label off and try again */ | |
1491 | continue; | |
1492 | } | |
00a5b5d4 SK |
1493 | } |
1494 | } | |
1495 | } | |
1496 | } | |
1497 | ||
1498 | free(packet); | |
1499 | ||
1500 | return STAT_BOGUS; | |
1501 | } | |
1502 | } | |
1503 | ||
7fa836e1 SK |
1504 | static int tcp_key_recurse(time_t now, int status, struct dns_header *header, size_t n, |
1505 | int class, char *name, char *keyname, struct server *server, int *keycount) | |
7d7b7b31 SK |
1506 | { |
1507 | /* Recurse up the key heirarchy */ | |
7d7b7b31 | 1508 | int new_status; |
7d7b7b31 | 1509 | |
7fa836e1 SK |
1510 | /* limit the amount of work we do, to avoid cycling forever on loops in the DNS */ |
1511 | if (--(*keycount) == 0) | |
1512 | return STAT_INSECURE; | |
7d7b7b31 | 1513 | |
7fa836e1 SK |
1514 | if (status == STAT_NEED_KEY) |
1515 | new_status = dnssec_validate_by_ds(now, header, n, name, keyname, class); | |
00a5b5d4 SK |
1516 | else if (status == STAT_NEED_DS || status == STAT_NEED_DS_NEG) |
1517 | { | |
1518 | new_status = dnssec_validate_ds(now, header, n, name, keyname, class); | |
1519 | if (status == STAT_NEED_DS && new_status == STAT_NO_DS) | |
1520 | new_status = STAT_INSECURE; | |
1521 | } | |
1522 | else if (status == STAT_CHASE_CNAME) | |
1523 | new_status = dnssec_chase_cname(now, header, n, name, keyname); | |
1524 | else | |
1525 | { | |
1526 | new_status = dnssec_validate_reply(now, header, n, name, keyname, &class, NULL); | |
1527 | ||
1528 | if (new_status == STAT_NO_SIG) | |
1529 | { | |
1530 | if (option_bool(OPT_DNSSEC_NO_SIGN)) | |
1531 | new_status = tcp_check_for_unsigned_zone(now, header, n, class, name, keyname, server, keycount); | |
1532 | else | |
1533 | new_status = STAT_INSECURE; | |
1534 | } | |
1535 | } | |
1536 | ||
7fa836e1 SK |
1537 | /* Can't validate because we need a key/DS whose name now in keyname. |
1538 | Make query for same, and recurse to validate */ | |
1539 | if (new_status == STAT_NEED_DS || new_status == STAT_NEED_KEY) | |
7d7b7b31 | 1540 | { |
7fa836e1 SK |
1541 | size_t m; |
1542 | unsigned char *packet = whine_malloc(65536 + MAXDNAME + RRFIXEDSZ + sizeof(u16)); | |
1543 | unsigned char *payload = &packet[2]; | |
1544 | struct dns_header *new_header = (struct dns_header *)payload; | |
1545 | u16 *length = (u16 *)packet; | |
1546 | unsigned char c1, c2; | |
1547 | ||
1548 | if (!packet) | |
1549 | return STAT_INSECURE; | |
1550 | ||
1551 | another_tcp_key: | |
1552 | m = dnssec_generate_query(new_header, ((char *) new_header) + 65536, keyname, class, | |
1553 | new_status == STAT_NEED_KEY ? T_DNSKEY : T_DS, &server->addr); | |
7d7b7b31 | 1554 | |
7fa836e1 | 1555 | *length = htons(m); |
7d7b7b31 | 1556 | |
7fa836e1 SK |
1557 | if (!read_write(server->tcpfd, packet, m + sizeof(u16), 0) || |
1558 | !read_write(server->tcpfd, &c1, 1, 1) || | |
1559 | !read_write(server->tcpfd, &c2, 1, 1) || | |
1560 | !read_write(server->tcpfd, payload, (c1 << 8) | c2, 1)) | |
1561 | new_status = STAT_INSECURE; | |
1562 | else | |
7d7b7b31 | 1563 | { |
7fa836e1 SK |
1564 | m = (c1 << 8) | c2; |
1565 | ||
00a5b5d4 SK |
1566 | new_status = tcp_key_recurse(now, new_status, new_header, m, class, name, keyname, server, keycount); |
1567 | ||
1568 | if (new_status == STAT_SECURE) | |
7d7b7b31 | 1569 | { |
7fa836e1 SK |
1570 | /* Reached a validated record, now try again at this level. |
1571 | Note that we may get ANOTHER NEED_* if an answer needs more than one key. | |
1572 | If so, go round again. */ | |
7d7b7b31 | 1573 | |
7fa836e1 SK |
1574 | if (status == STAT_NEED_KEY) |
1575 | new_status = dnssec_validate_by_ds(now, header, n, name, keyname, class); | |
00a5b5d4 SK |
1576 | else if (status == STAT_NEED_DS || status == STAT_NEED_DS_NEG) |
1577 | { | |
1578 | new_status = dnssec_validate_ds(now, header, n, name, keyname, class); | |
1579 | if (status == STAT_NEED_DS && new_status == STAT_NO_DS) | |
1580 | new_status = STAT_INSECURE; /* Validated no DS */ | |
1581 | } | |
1582 | else if (status == STAT_CHASE_CNAME) | |
1583 | new_status = dnssec_chase_cname(now, header, n, name, keyname); | |
1584 | else | |
1585 | { | |
1586 | new_status = dnssec_validate_reply(now, header, n, name, keyname, &class, NULL); | |
1587 | ||
1588 | if (new_status == STAT_NO_SIG) | |
1589 | { | |
1590 | if (option_bool(OPT_DNSSEC_NO_SIGN)) | |
1591 | new_status = tcp_check_for_unsigned_zone(now, header, n, class, name, keyname, server, keycount); | |
1592 | else | |
1593 | new_status = STAT_INSECURE; | |
1594 | } | |
1595 | } | |
1596 | ||
7d7b7b31 | 1597 | if (new_status == STAT_NEED_DS || new_status == STAT_NEED_KEY) |
7fa836e1 | 1598 | goto another_tcp_key; |
7d7b7b31 SK |
1599 | } |
1600 | } | |
7d7b7b31 | 1601 | |
7fa836e1 SK |
1602 | free(packet); |
1603 | } | |
7d7b7b31 SK |
1604 | return new_status; |
1605 | } | |
1606 | #endif | |
1607 | ||
1608 | ||
feba5c1d SK |
1609 | /* The daemon forks before calling this: it should deal with one connection, |
1610 | blocking as neccessary, and then return. Note, need to be a bit careful | |
1611 | about resources for debug mode, when the fork is suppressed: that's | |
1612 | done by the caller. */ | |
5aabfc78 | 1613 | unsigned char *tcp_request(int confd, time_t now, |
4f7b304f | 1614 | union mysockaddr *local_addr, struct in_addr netmask, int auth_dns) |
feba5c1d | 1615 | { |
28866e95 SK |
1616 | size_t size = 0; |
1617 | int norebind = 0; | |
3b195961 | 1618 | #ifdef HAVE_AUTH |
19b16891 | 1619 | int local_auth = 0; |
3b195961 | 1620 | #endif |
613ad15d SK |
1621 | int checking_disabled, ad_question, do_bit, added_pheader = 0; |
1622 | int check_subnet, no_cache_dnssec = 0, cache_secure = 0; | |
cdeda28f | 1623 | size_t m; |
ee86ce68 SK |
1624 | unsigned short qtype; |
1625 | unsigned int gotname; | |
feba5c1d | 1626 | unsigned char c1, c2; |
4b5ea12e SK |
1627 | /* Max TCP packet + slop + size */ |
1628 | unsigned char *packet = whine_malloc(65536 + MAXDNAME + RRFIXEDSZ + sizeof(u16)); | |
1629 | unsigned char *payload = &packet[2]; | |
1630 | /* largest field in header is 16-bits, so this is still sufficiently aligned */ | |
1631 | struct dns_header *header = (struct dns_header *)payload; | |
1632 | u16 *length = (u16 *)packet; | |
3be34541 | 1633 | struct server *last_server; |
7de060b0 SK |
1634 | struct in_addr dst_addr_4; |
1635 | union mysockaddr peer_addr; | |
1636 | socklen_t peer_len = sizeof(union mysockaddr); | |
3be34541 | 1637 | |
7de060b0 SK |
1638 | if (getpeername(confd, (struct sockaddr *)&peer_addr, &peer_len) == -1) |
1639 | return packet; | |
c8a80487 SK |
1640 | |
1641 | /* We can be configured to only accept queries from at-most-one-hop-away addresses. */ | |
1642 | if (option_bool(OPT_LOCAL_SERVICE)) | |
1643 | { | |
1644 | struct addrlist *addr; | |
1645 | #ifdef HAVE_IPV6 | |
1646 | if (peer_addr.sa.sa_family == AF_INET6) | |
1647 | { | |
1648 | for (addr = daemon->interface_addrs; addr; addr = addr->next) | |
1649 | if ((addr->flags & ADDRLIST_IPV6) && | |
1650 | is_same_net6(&addr->addr.addr.addr6, &peer_addr.in6.sin6_addr, addr->prefixlen)) | |
1651 | break; | |
1652 | } | |
1653 | else | |
1654 | #endif | |
1655 | { | |
1656 | struct in_addr netmask; | |
1657 | for (addr = daemon->interface_addrs; addr; addr = addr->next) | |
1658 | { | |
15b1b7e9 | 1659 | netmask.s_addr = htonl(~(in_addr_t)0 << (32 - addr->prefixlen)); |
c8a80487 SK |
1660 | if (!(addr->flags & ADDRLIST_IPV6) && |
1661 | is_same_net(addr->addr.addr.addr4, peer_addr.in.sin_addr, netmask)) | |
1662 | break; | |
1663 | } | |
1664 | } | |
1665 | if (!addr) | |
1666 | { | |
1667 | my_syslog(LOG_WARNING, _("Ignoring query from non-local network")); | |
1668 | return packet; | |
1669 | } | |
1670 | } | |
7de060b0 | 1671 | |
feba5c1d SK |
1672 | while (1) |
1673 | { | |
1674 | if (!packet || | |
1675 | !read_write(confd, &c1, 1, 1) || !read_write(confd, &c2, 1, 1) || | |
1676 | !(size = c1 << 8 | c2) || | |
4b5ea12e | 1677 | !read_write(confd, payload, size, 1)) |
feba5c1d SK |
1678 | return packet; |
1679 | ||
572b41eb | 1680 | if (size < (int)sizeof(struct dns_header)) |
feba5c1d SK |
1681 | continue; |
1682 | ||
ed4c0767 SK |
1683 | check_subnet = 0; |
1684 | ||
28866e95 | 1685 | /* save state of "cd" flag in query */ |
7d7b7b31 SK |
1686 | if ((checking_disabled = header->hb4 & HB4_CD)) |
1687 | no_cache_dnssec = 1; | |
28866e95 | 1688 | |
3be34541 | 1689 | if ((gotname = extract_request(header, (unsigned int)size, daemon->namebuff, &qtype))) |
feba5c1d | 1690 | { |
b485ed97 SK |
1691 | #ifdef HAVE_AUTH |
1692 | struct auth_zone *zone; | |
1693 | #endif | |
610e782a | 1694 | char *types = querystr(auth_dns ? "auth" : "query", qtype); |
7de060b0 SK |
1695 | |
1696 | if (peer_addr.sa.sa_family == AF_INET) | |
1697 | log_query(F_QUERY | F_IPV4 | F_FORWARD, daemon->namebuff, | |
1698 | (struct all_addr *)&peer_addr.in.sin_addr, types); | |
feba5c1d | 1699 | #ifdef HAVE_IPV6 |
7de060b0 SK |
1700 | else |
1701 | log_query(F_QUERY | F_IPV6 | F_FORWARD, daemon->namebuff, | |
1702 | (struct all_addr *)&peer_addr.in6.sin6_addr, types); | |
feba5c1d | 1703 | #endif |
b485ed97 SK |
1704 | |
1705 | #ifdef HAVE_AUTH | |
1706 | /* find queries for zones we're authoritative for, and answer them directly */ | |
6008bdbb SK |
1707 | if (!auth_dns) |
1708 | for (zone = daemon->auth_zones; zone; zone = zone->next) | |
1709 | if (in_zone(zone, daemon->namebuff, NULL)) | |
1710 | { | |
1711 | auth_dns = 1; | |
1712 | local_auth = 1; | |
1713 | break; | |
1714 | } | |
b485ed97 | 1715 | #endif |
feba5c1d SK |
1716 | } |
1717 | ||
7de060b0 SK |
1718 | if (local_addr->sa.sa_family == AF_INET) |
1719 | dst_addr_4 = local_addr->in.sin_addr; | |
1720 | else | |
1721 | dst_addr_4.s_addr = 0; | |
1722 | ||
4820dce9 | 1723 | #ifdef HAVE_AUTH |
4f7b304f | 1724 | if (auth_dns) |
19b16891 | 1725 | m = answer_auth(header, ((char *) header) + 65536, (size_t)size, now, &peer_addr, local_auth); |
4f7b304f | 1726 | else |
4820dce9 | 1727 | #endif |
feba5c1d | 1728 | { |
4f7b304f SK |
1729 | /* m > 0 if answered from cache */ |
1730 | m = answer_request(header, ((char *) header) + 65536, (size_t)size, | |
613ad15d | 1731 | dst_addr_4, netmask, now, &ad_question, &do_bit); |
feba5c1d | 1732 | |
4f7b304f SK |
1733 | /* Do this by steam now we're not in the select() loop */ |
1734 | check_log_writer(NULL); | |
1735 | ||
1736 | if (m == 0) | |
feba5c1d | 1737 | { |
4f7b304f SK |
1738 | unsigned int flags = 0; |
1739 | struct all_addr *addrp = NULL; | |
1740 | int type = 0; | |
1741 | char *domain = NULL; | |
feba5c1d | 1742 | |
4f7b304f SK |
1743 | if (option_bool(OPT_ADD_MAC)) |
1744 | size = add_mac(header, size, ((char *) header) + 65536, &peer_addr); | |
ed4c0767 SK |
1745 | |
1746 | if (option_bool(OPT_CLIENT_SUBNET)) | |
1747 | { | |
1748 | size_t new = add_source_addr(header, size, ((char *) header) + 65536, &peer_addr); | |
1749 | if (size != new) | |
1750 | { | |
1751 | size = new; | |
1752 | check_subnet = 1; | |
1753 | } | |
1754 | } | |
1755 | ||
4f7b304f SK |
1756 | if (gotname) |
1757 | flags = search_servers(now, &addrp, gotname, daemon->namebuff, &type, &domain, &norebind); | |
1758 | ||
1759 | if (type != 0 || option_bool(OPT_ORDER) || !daemon->last_server) | |
1760 | last_server = daemon->servers; | |
1761 | else | |
1762 | last_server = daemon->last_server; | |
1763 | ||
1764 | if (!flags && last_server) | |
1765 | { | |
1766 | struct server *firstsendto = NULL; | |
8a9be9e4 | 1767 | #ifdef HAVE_DNSSEC |
703c7ff4 | 1768 | unsigned char *newhash, hash[HASH_SIZE]; |
63758384 | 1769 | if ((newhash = hash_questions(header, (unsigned int)size, daemon->namebuff))) |
8a9be9e4 | 1770 | memcpy(hash, newhash, HASH_SIZE); |
b37f8b99 TH |
1771 | else |
1772 | memset(hash, 0, HASH_SIZE); | |
8a9be9e4 | 1773 | #else |
4f7b304f | 1774 | unsigned int crc = questions_crc(header, (unsigned int)size, daemon->namebuff); |
8a9be9e4 | 1775 | #endif |
4f7b304f SK |
1776 | /* Loop round available servers until we succeed in connecting to one. |
1777 | Note that this code subtley ensures that consecutive queries on this connection | |
1778 | which can go to the same server, do so. */ | |
1779 | while (1) | |
feba5c1d | 1780 | { |
4f7b304f SK |
1781 | if (!firstsendto) |
1782 | firstsendto = last_server; | |
1783 | else | |
1784 | { | |
1785 | if (!(last_server = last_server->next)) | |
1786 | last_server = daemon->servers; | |
1787 | ||
1788 | if (last_server == firstsendto) | |
1789 | break; | |
1790 | } | |
1791 | ||
1792 | /* server for wrong domain */ | |
1793 | if (type != (last_server->flags & SERV_TYPE) || | |
b5ea1cc2 SK |
1794 | (type == SERV_HAS_DOMAIN && !hostname_isequal(domain, last_server->domain)) || |
1795 | (last_server->flags & (SERV_LITERAL_ADDRESS | SERV_LOOP))) | |
7de060b0 SK |
1796 | continue; |
1797 | ||
4f7b304f | 1798 | if (last_server->tcpfd == -1) |
7de060b0 | 1799 | { |
4f7b304f SK |
1800 | if ((last_server->tcpfd = socket(last_server->addr.sa.sa_family, SOCK_STREAM, 0)) == -1) |
1801 | continue; | |
1802 | ||
e9828b6f KV |
1803 | #ifdef HAVE_CONNTRACK |
1804 | /* Copy connection mark of incoming query to outgoing connection. */ | |
1805 | if (option_bool(OPT_CONNTRACK)) | |
1806 | { | |
1807 | unsigned int mark; | |
1808 | struct all_addr local; | |
1809 | #ifdef HAVE_IPV6 | |
1810 | if (local_addr->sa.sa_family == AF_INET6) | |
1811 | local.addr.addr6 = local_addr->in6.sin6_addr; | |
1812 | else | |
1813 | #endif | |
1814 | local.addr.addr4 = local_addr->in.sin_addr; | |
1815 | ||
1816 | if (get_incoming_mark(&peer_addr, &local, 1, &mark)) | |
1817 | setsockopt(last_server->tcpfd, SOL_SOCKET, SO_MARK, &mark, sizeof(unsigned int)); | |
1818 | } | |
1819 | #endif | |
1820 | ||
4f7b304f SK |
1821 | if ((!local_bind(last_server->tcpfd, &last_server->source_addr, last_server->interface, 1) || |
1822 | connect(last_server->tcpfd, &last_server->addr.sa, sa_len(&last_server->addr)) == -1)) | |
1823 | { | |
1824 | close(last_server->tcpfd); | |
1825 | last_server->tcpfd = -1; | |
1826 | continue; | |
1827 | } | |
1828 | ||
7d7b7b31 SK |
1829 | #ifdef HAVE_DNSSEC |
1830 | if (option_bool(OPT_DNSSEC_VALID)) | |
1831 | { | |
613ad15d SK |
1832 | size_t new_size = add_do_bit(header, size, ((char *) header) + 65536); |
1833 | ||
2ecd9bd5 SK |
1834 | /* For debugging, set Checking Disabled, otherwise, have the upstream check too, |
1835 | this allows it to select auth servers when one is returning bad data. */ | |
1836 | if (option_bool(OPT_DNSSEC_DEBUG)) | |
1837 | header->hb4 |= HB4_CD; | |
613ad15d SK |
1838 | |
1839 | if (size != new_size) | |
1840 | added_pheader = 1; | |
1841 | ||
1842 | size = new_size; | |
7d7b7b31 SK |
1843 | } |
1844 | #endif | |
4f7b304f SK |
1845 | } |
1846 | ||
4b5ea12e | 1847 | *length = htons(size); |
1fc02680 SK |
1848 | |
1849 | /* get query name again for logging - may have been overwritten */ | |
1850 | if (!(gotname = extract_request(header, (unsigned int)size, daemon->namebuff, &qtype))) | |
1851 | strcpy(daemon->namebuff, "query"); | |
4f7b304f | 1852 | |
4b5ea12e | 1853 | if (!read_write(last_server->tcpfd, packet, size + sizeof(u16), 0) || |
4f7b304f | 1854 | !read_write(last_server->tcpfd, &c1, 1, 1) || |
7d7b7b31 SK |
1855 | !read_write(last_server->tcpfd, &c2, 1, 1) || |
1856 | !read_write(last_server->tcpfd, payload, (c1 << 8) | c2, 1)) | |
4f7b304f SK |
1857 | { |
1858 | close(last_server->tcpfd); | |
1859 | last_server->tcpfd = -1; | |
1860 | continue; | |
1861 | } | |
1862 | ||
1863 | m = (c1 << 8) | c2; | |
4f7b304f | 1864 | |
4f7b304f SK |
1865 | if (last_server->addr.sa.sa_family == AF_INET) |
1866 | log_query(F_SERVER | F_IPV4 | F_FORWARD, daemon->namebuff, | |
1867 | (struct all_addr *)&last_server->addr.in.sin_addr, NULL); | |
feba5c1d | 1868 | #ifdef HAVE_IPV6 |
4f7b304f SK |
1869 | else |
1870 | log_query(F_SERVER | F_IPV6 | F_FORWARD, daemon->namebuff, | |
1871 | (struct all_addr *)&last_server->addr.in6.sin6_addr, NULL); | |
feba5c1d | 1872 | #endif |
7d7b7b31 SK |
1873 | |
1874 | #ifdef HAVE_DNSSEC | |
1875 | if (option_bool(OPT_DNSSEC_VALID) && !checking_disabled) | |
1876 | { | |
7fa836e1 SK |
1877 | int keycount = DNSSEC_WORK; /* Limit to number of DNSSEC questions, to catch loops and avoid filling cache. */ |
1878 | int status = tcp_key_recurse(now, STAT_TRUNCATED, header, m, 0, daemon->namebuff, daemon->keyname, last_server, &keycount); | |
1879 | char *result; | |
1880 | ||
1881 | if (keycount == 0) | |
1882 | result = "ABANDONED"; | |
1883 | else | |
1884 | result = (status == STAT_SECURE ? "SECURE" : (status == STAT_INSECURE ? "INSECURE" : "BOGUS")); | |
7d7b7b31 | 1885 | |
7fa836e1 | 1886 | log_query(F_KEYTAG | F_SECSTAT, "result", NULL, result); |
7d7b7b31 | 1887 | |
7d7b7b31 SK |
1888 | if (status == STAT_BOGUS) |
1889 | no_cache_dnssec = 1; | |
7fa836e1 | 1890 | |
7d7b7b31 SK |
1891 | if (status == STAT_SECURE) |
1892 | cache_secure = 1; | |
1893 | } | |
1894 | #endif | |
1895 | ||
1896 | /* restore CD bit to the value in the query */ | |
1897 | if (checking_disabled) | |
1898 | header->hb4 |= HB4_CD; | |
1899 | else | |
1900 | header->hb4 &= ~HB4_CD; | |
4f7b304f SK |
1901 | |
1902 | /* There's no point in updating the cache, since this process will exit and | |
1903 | lose the information after a few queries. We make this call for the alias and | |
1904 | bogus-nxdomain side-effects. */ | |
1905 | /* If the crc of the question section doesn't match the crc we sent, then | |
1906 | someone might be attempting to insert bogus values into the cache by | |
1907 | sending replies containing questions and bogus answers. */ | |
8a9be9e4 SK |
1908 | #ifdef HAVE_DNSSEC |
1909 | newhash = hash_questions(header, (unsigned int)m, daemon->namebuff); | |
1910 | if (!newhash || memcmp(hash, newhash, HASH_SIZE) != 0) | |
703c7ff4 SK |
1911 | { |
1912 | m = 0; | |
1913 | break; | |
1914 | } | |
8a9be9e4 SK |
1915 | #else |
1916 | if (crc != questions_crc(header, (unsigned int)m, daemon->namebuff)) | |
703c7ff4 SK |
1917 | { |
1918 | m = 0; | |
1919 | break; | |
1920 | } | |
8a9be9e4 SK |
1921 | #endif |
1922 | ||
1923 | m = process_reply(header, now, last_server, (unsigned int)m, | |
1924 | option_bool(OPT_NO_REBIND) && !norebind, no_cache_dnssec, | |
613ad15d | 1925 | cache_secure, ad_question, do_bit, added_pheader, check_subnet, &peer_addr); |
4f7b304f SK |
1926 | |
1927 | break; | |
1928 | } | |
feba5c1d | 1929 | } |
4f7b304f SK |
1930 | |
1931 | /* In case of local answer or no connections made. */ | |
1932 | if (m == 0) | |
1933 | m = setup_reply(header, (unsigned int)size, addrp, flags, daemon->local_ttl); | |
feba5c1d | 1934 | } |
feba5c1d | 1935 | } |
4f7b304f | 1936 | |
5aabfc78 | 1937 | check_log_writer(NULL); |
feba5c1d | 1938 | |
4b5ea12e SK |
1939 | *length = htons(m); |
1940 | ||
1941 | if (m == 0 || !read_write(confd, packet, m + sizeof(u16), 0)) | |
feba5c1d SK |
1942 | return packet; |
1943 | } | |
1944 | } | |
1945 | ||
1697269c | 1946 | static struct frec *allocate_frec(time_t now) |
9e4abcb5 | 1947 | { |
1697269c SK |
1948 | struct frec *f; |
1949 | ||
5aabfc78 | 1950 | if ((f = (struct frec *)whine_malloc(sizeof(struct frec)))) |
9e4abcb5 | 1951 | { |
1a6bca81 | 1952 | f->next = daemon->frec_list; |
1697269c | 1953 | f->time = now; |
832af0ba | 1954 | f->sentto = NULL; |
1a6bca81 | 1955 | f->rfd4 = NULL; |
28866e95 | 1956 | f->flags = 0; |
1a6bca81 SK |
1957 | #ifdef HAVE_IPV6 |
1958 | f->rfd6 = NULL; | |
3a237152 SK |
1959 | #endif |
1960 | #ifdef HAVE_DNSSEC | |
97bc798b | 1961 | f->dependent = NULL; |
3a237152 | 1962 | f->blocking_query = NULL; |
4619d946 | 1963 | f->stash = NULL; |
1a6bca81 SK |
1964 | #endif |
1965 | daemon->frec_list = f; | |
1697269c | 1966 | } |
9e4abcb5 | 1967 | |
1697269c SK |
1968 | return f; |
1969 | } | |
9e4abcb5 | 1970 | |
b5ea1cc2 | 1971 | struct randfd *allocate_rfd(int family) |
1a6bca81 SK |
1972 | { |
1973 | static int finger = 0; | |
1974 | int i; | |
1975 | ||
1976 | /* limit the number of sockets we have open to avoid starvation of | |
1977 | (eg) TFTP. Once we have a reasonable number, randomness should be OK */ | |
1978 | ||
1979 | for (i = 0; i < RANDOM_SOCKS; i++) | |
9009d746 | 1980 | if (daemon->randomsocks[i].refcount == 0) |
1a6bca81 | 1981 | { |
9009d746 SK |
1982 | if ((daemon->randomsocks[i].fd = random_sock(family)) == -1) |
1983 | break; | |
1984 | ||
1a6bca81 SK |
1985 | daemon->randomsocks[i].refcount = 1; |
1986 | daemon->randomsocks[i].family = family; | |
1987 | return &daemon->randomsocks[i]; | |
1988 | } | |
1989 | ||
9009d746 | 1990 | /* No free ones or cannot get new socket, grab an existing one */ |
1a6bca81 SK |
1991 | for (i = 0; i < RANDOM_SOCKS; i++) |
1992 | { | |
1993 | int j = (i+finger) % RANDOM_SOCKS; | |
9009d746 SK |
1994 | if (daemon->randomsocks[j].refcount != 0 && |
1995 | daemon->randomsocks[j].family == family && | |
1996 | daemon->randomsocks[j].refcount != 0xffff) | |
1a6bca81 SK |
1997 | { |
1998 | finger = j; | |
1999 | daemon->randomsocks[j].refcount++; | |
2000 | return &daemon->randomsocks[j]; | |
2001 | } | |
2002 | } | |
2003 | ||
2004 | return NULL; /* doom */ | |
2005 | } | |
b5ea1cc2 SK |
2006 | |
2007 | void free_rfd(struct randfd *rfd) | |
2008 | { | |
2009 | if (rfd && --(rfd->refcount) == 0) | |
2010 | close(rfd->fd); | |
2011 | } | |
2012 | ||
1a6bca81 SK |
2013 | static void free_frec(struct frec *f) |
2014 | { | |
b5ea1cc2 | 2015 | free_rfd(f->rfd4); |
1a6bca81 SK |
2016 | f->rfd4 = NULL; |
2017 | f->sentto = NULL; | |
28866e95 | 2018 | f->flags = 0; |
1a6bca81 SK |
2019 | |
2020 | #ifdef HAVE_IPV6 | |
b5ea1cc2 | 2021 | free_rfd(f->rfd6); |
1a6bca81 SK |
2022 | f->rfd6 = NULL; |
2023 | #endif | |
3a237152 SK |
2024 | |
2025 | #ifdef HAVE_DNSSEC | |
2026 | if (f->stash) | |
0fc2f313 SK |
2027 | { |
2028 | blockdata_free(f->stash); | |
2029 | f->stash = NULL; | |
2030 | } | |
3a237152 SK |
2031 | |
2032 | /* Anything we're waiting on is pointless now, too */ | |
2033 | if (f->blocking_query) | |
2034 | free_frec(f->blocking_query); | |
2035 | f->blocking_query = NULL; | |
39048ad1 | 2036 | f->dependent = NULL; |
3a237152 | 2037 | #endif |
1a6bca81 SK |
2038 | } |
2039 | ||
1697269c SK |
2040 | /* if wait==NULL return a free or older than TIMEOUT record. |
2041 | else return *wait zero if one available, or *wait is delay to | |
1a6bca81 | 2042 | when the oldest in-use record will expire. Impose an absolute |
3a237152 SK |
2043 | limit of 4*TIMEOUT before we wipe things (for random sockets). |
2044 | If force is set, always return a result, even if we have | |
2045 | to allocate above the limit. */ | |
2046 | struct frec *get_new_frec(time_t now, int *wait, int force) | |
1697269c | 2047 | { |
1a6bca81 | 2048 | struct frec *f, *oldest, *target; |
1697269c SK |
2049 | int count; |
2050 | ||
2051 | if (wait) | |
2052 | *wait = 0; | |
2053 | ||
1a6bca81 | 2054 | for (f = daemon->frec_list, oldest = NULL, target = NULL, count = 0; f; f = f->next, count++) |
832af0ba | 2055 | if (!f->sentto) |
1a6bca81 SK |
2056 | target = f; |
2057 | else | |
1697269c | 2058 | { |
1a6bca81 SK |
2059 | if (difftime(now, f->time) >= 4*TIMEOUT) |
2060 | { | |
2061 | free_frec(f); | |
2062 | target = f; | |
2063 | } | |
2064 | ||
2065 | if (!oldest || difftime(f->time, oldest->time) <= 0) | |
2066 | oldest = f; | |
1697269c | 2067 | } |
1a6bca81 SK |
2068 | |
2069 | if (target) | |
2070 | { | |
2071 | target->time = now; | |
2072 | return target; | |
2073 | } | |
9e4abcb5 SK |
2074 | |
2075 | /* can't find empty one, use oldest if there is one | |
2076 | and it's older than timeout */ | |
1697269c | 2077 | if (oldest && ((int)difftime(now, oldest->time)) >= TIMEOUT) |
9e4abcb5 | 2078 | { |
1697269c SK |
2079 | /* keep stuff for twice timeout if we can by allocating a new |
2080 | record instead */ | |
2081 | if (difftime(now, oldest->time) < 2*TIMEOUT && | |
2082 | count <= daemon->ftabsize && | |
2083 | (f = allocate_frec(now))) | |
2084 | return f; | |
2085 | ||
2086 | if (!wait) | |
2087 | { | |
1a6bca81 | 2088 | free_frec(oldest); |
1697269c SK |
2089 | oldest->time = now; |
2090 | } | |
9e4abcb5 SK |
2091 | return oldest; |
2092 | } | |
2093 | ||
1697269c | 2094 | /* none available, calculate time 'till oldest record expires */ |
3a237152 | 2095 | if (!force && count > daemon->ftabsize) |
1697269c | 2096 | { |
0da5e897 MSB |
2097 | static time_t last_log = 0; |
2098 | ||
1697269c SK |
2099 | if (oldest && wait) |
2100 | *wait = oldest->time + (time_t)TIMEOUT - now; | |
0da5e897 MSB |
2101 | |
2102 | if ((int)difftime(now, last_log) > 5) | |
2103 | { | |
2104 | last_log = now; | |
2105 | my_syslog(LOG_WARNING, _("Maximum number of concurrent DNS queries reached (max: %d)"), daemon->ftabsize); | |
2106 | } | |
2107 | ||
9e4abcb5 SK |
2108 | return NULL; |
2109 | } | |
1697269c SK |
2110 | |
2111 | if (!(f = allocate_frec(now)) && wait) | |
2112 | /* wait one second on malloc failure */ | |
2113 | *wait = 1; | |
9e4abcb5 | 2114 | |
9e4abcb5 SK |
2115 | return f; /* OK if malloc fails and this is NULL */ |
2116 | } | |
2117 | ||
832af0ba | 2118 | /* crc is all-ones if not known. */ |
8a9be9e4 | 2119 | static struct frec *lookup_frec(unsigned short id, void *hash) |
9e4abcb5 SK |
2120 | { |
2121 | struct frec *f; | |
2122 | ||
1a6bca81 | 2123 | for(f = daemon->frec_list; f; f = f->next) |
832af0ba | 2124 | if (f->sentto && f->new_id == id && |
8a9be9e4 | 2125 | (!hash || memcmp(hash, f->hash, HASH_SIZE) == 0)) |
9e4abcb5 SK |
2126 | return f; |
2127 | ||
2128 | return NULL; | |
2129 | } | |
2130 | ||
2131 | static struct frec *lookup_frec_by_sender(unsigned short id, | |
fd9fa481 | 2132 | union mysockaddr *addr, |
8a9be9e4 | 2133 | void *hash) |
9e4abcb5 | 2134 | { |
feba5c1d SK |
2135 | struct frec *f; |
2136 | ||
1a6bca81 | 2137 | for(f = daemon->frec_list; f; f = f->next) |
832af0ba | 2138 | if (f->sentto && |
9e4abcb5 | 2139 | f->orig_id == id && |
8a9be9e4 | 2140 | memcmp(hash, f->hash, HASH_SIZE) == 0 && |
9e4abcb5 SK |
2141 | sockaddr_isequal(&f->source, addr)) |
2142 | return f; | |
2143 | ||
2144 | return NULL; | |
2145 | } | |
47a95169 SK |
2146 | |
2147 | /* Send query packet again, if we can. */ | |
2148 | void resend_query() | |
2149 | { | |
2150 | if (daemon->srv_save) | |
2151 | { | |
2152 | int fd; | |
2153 | ||
2154 | if (daemon->srv_save->sfd) | |
2155 | fd = daemon->srv_save->sfd->fd; | |
2156 | else if (daemon->rfd_save && daemon->rfd_save->refcount != 0) | |
2157 | fd = daemon->rfd_save->fd; | |
2158 | else | |
2159 | return; | |
2160 | ||
2161 | while(sendto(fd, daemon->packet, daemon->packet_len, 0, | |
2162 | &daemon->srv_save->addr.sa, sa_len(&daemon->srv_save->addr)) == -1 && retry_send()); | |
2163 | } | |
2164 | } | |
9e4abcb5 | 2165 | |
849a8357 | 2166 | /* A server record is going away, remove references to it */ |
5aabfc78 | 2167 | void server_gone(struct server *server) |
849a8357 SK |
2168 | { |
2169 | struct frec *f; | |
2170 | ||
1a6bca81 | 2171 | for (f = daemon->frec_list; f; f = f->next) |
832af0ba | 2172 | if (f->sentto && f->sentto == server) |
1a6bca81 | 2173 | free_frec(f); |
849a8357 SK |
2174 | |
2175 | if (daemon->last_server == server) | |
2176 | daemon->last_server = NULL; | |
2177 | ||
2178 | if (daemon->srv_save == server) | |
2179 | daemon->srv_save = NULL; | |
2180 | } | |
9e4abcb5 | 2181 | |
316e2730 | 2182 | /* return unique random ids. */ |
8a9be9e4 | 2183 | static unsigned short get_id(void) |
9e4abcb5 SK |
2184 | { |
2185 | unsigned short ret = 0; | |
832af0ba | 2186 | |
316e2730 | 2187 | do |
832af0ba | 2188 | ret = rand16(); |
8a9be9e4 | 2189 | while (lookup_frec(ret, NULL)); |
832af0ba | 2190 | |
9e4abcb5 SK |
2191 | return ret; |
2192 | } | |
2193 | ||
2194 | ||
2195 | ||
2196 | ||
2197 |