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