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