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