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