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