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