Simon Kelley | 6174435 | 2013-01-31 14:34:40 +0000 | [diff] [blame] | 1 | /* dnsmasq is Copyright (c) 2000-2013 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 | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 19 | static struct frec *lookup_frec(unsigned short id, unsigned int crc); |
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, |
| 22 | unsigned int crc); |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 23 | static unsigned short get_id(unsigned int crc); |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 24 | static void free_frec(struct frec *f); |
| 25 | static struct randfd *allocate_rfd(int family); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 26 | |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 27 | /* Send a UDP packet with its source address set as "source" |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 28 | unless nowild is true, when we just send it with the kernel default */ |
Simon Kelley | 29689cf | 2012-03-22 14:01:00 +0000 | [diff] [blame] | 29 | int send_from(int fd, int nowild, char *packet, size_t len, |
| 30 | union mysockaddr *to, struct all_addr *source, |
Simon Kelley | 50303b1 | 2012-04-04 22:13:17 +0100 | [diff] [blame] | 31 | unsigned int iface) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 32 | { |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 33 | struct msghdr msg; |
| 34 | struct iovec iov[1]; |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 35 | union { |
| 36 | struct cmsghdr align; /* this ensures alignment */ |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 37 | #if defined(HAVE_LINUX_NETWORK) |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 38 | char control[CMSG_SPACE(sizeof(struct in_pktinfo))]; |
| 39 | #elif defined(IP_SENDSRCADDR) |
| 40 | char control[CMSG_SPACE(sizeof(struct in_addr))]; |
| 41 | #endif |
| 42 | #ifdef HAVE_IPV6 |
| 43 | char control6[CMSG_SPACE(sizeof(struct in6_pktinfo))]; |
| 44 | #endif |
| 45 | } control_u; |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 46 | |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 47 | iov[0].iov_base = packet; |
| 48 | iov[0].iov_len = len; |
| 49 | |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 50 | msg.msg_control = NULL; |
| 51 | msg.msg_controllen = 0; |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 52 | msg.msg_flags = 0; |
| 53 | msg.msg_name = to; |
| 54 | msg.msg_namelen = sa_len(to); |
| 55 | msg.msg_iov = iov; |
| 56 | msg.msg_iovlen = 1; |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 57 | |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 58 | if (!nowild) |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 59 | { |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 60 | struct cmsghdr *cmptr; |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 61 | msg.msg_control = &control_u; |
| 62 | msg.msg_controllen = sizeof(control_u); |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 63 | cmptr = CMSG_FIRSTHDR(&msg); |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 64 | |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 65 | if (to->sa.sa_family == AF_INET) |
| 66 | { |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 67 | #if defined(HAVE_LINUX_NETWORK) |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 68 | struct in_pktinfo p; |
| 69 | p.ipi_ifindex = 0; |
| 70 | p.ipi_spec_dst = source->addr.addr4; |
| 71 | memcpy(CMSG_DATA(cmptr), &p, sizeof(p)); |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 72 | msg.msg_controllen = cmptr->cmsg_len = CMSG_LEN(sizeof(struct in_pktinfo)); |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 73 | cmptr->cmsg_level = IPPROTO_IP; |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 74 | cmptr->cmsg_type = IP_PKTINFO; |
| 75 | #elif defined(IP_SENDSRCADDR) |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 76 | memcpy(CMSG_DATA(cmptr), &(source->addr.addr4), sizeof(source->addr.addr4)); |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 77 | msg.msg_controllen = cmptr->cmsg_len = CMSG_LEN(sizeof(struct in_addr)); |
| 78 | cmptr->cmsg_level = IPPROTO_IP; |
| 79 | cmptr->cmsg_type = IP_SENDSRCADDR; |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 80 | #endif |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 81 | } |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 82 | else |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 83 | #ifdef HAVE_IPV6 |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 84 | { |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 85 | struct in6_pktinfo p; |
| 86 | p.ipi6_ifindex = iface; /* Need iface for IPv6 to handle link-local addrs */ |
| 87 | p.ipi6_addr = source->addr.addr6; |
| 88 | memcpy(CMSG_DATA(cmptr), &p, sizeof(p)); |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 89 | msg.msg_controllen = cmptr->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo)); |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 90 | cmptr->cmsg_type = daemon->v6pktinfo; |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 91 | cmptr->cmsg_level = IPPROTO_IPV6; |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 92 | } |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 93 | #else |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 94 | (void)iface; /* eliminate warning */ |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 95 | #endif |
| 96 | } |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 97 | |
Simon Kelley | 29d28dd | 2012-12-03 14:05:59 +0000 | [diff] [blame] | 98 | while (sendmsg(fd, &msg, 0) == -1) |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 99 | { |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 100 | if (retry_send()) |
Simon Kelley | 29d28dd | 2012-12-03 14:05:59 +0000 | [diff] [blame] | 101 | continue; |
| 102 | |
| 103 | /* If interface is still in DAD, EINVAL results - ignore that. */ |
| 104 | if (errno == EINVAL) |
| 105 | break; |
Simon Kelley | 22d904d | 2012-02-26 20:13:45 +0000 | [diff] [blame] | 106 | |
Simon Kelley | 50303b1 | 2012-04-04 22:13:17 +0100 | [diff] [blame] | 107 | my_syslog(LOG_ERR, _("failed to send packet: %s"), strerror(errno)); |
Simon Kelley | 29689cf | 2012-03-22 14:01:00 +0000 | [diff] [blame] | 108 | return 0; |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 109 | } |
Simon Kelley | 29d28dd | 2012-12-03 14:05:59 +0000 | [diff] [blame] | 110 | |
Simon Kelley | 29689cf | 2012-03-22 14:01:00 +0000 | [diff] [blame] | 111 | return 1; |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 114 | static unsigned int search_servers(time_t now, struct all_addr **addrpp, |
| 115 | unsigned int qtype, char *qdomain, int *type, char **domain, int *norebind) |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 116 | |
| 117 | { |
| 118 | /* If the query ends in the domain in one of our servers, set |
| 119 | domain to point to that name. We find the largest match to allow both |
| 120 | domain.org and sub.domain.org to exist. */ |
| 121 | |
| 122 | unsigned int namelen = strlen(qdomain); |
| 123 | unsigned int matchlen = 0; |
| 124 | struct server *serv; |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 125 | unsigned int flags = 0; |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 126 | |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 127 | for (serv = daemon->servers; serv; serv=serv->next) |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 128 | /* domain matches take priority over NODOTS matches */ |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 129 | if ((serv->flags & SERV_FOR_NODOTS) && *type != SERV_HAS_DOMAIN && !strchr(qdomain, '.') && namelen != 0) |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 130 | { |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 131 | unsigned int sflag = serv->addr.sa.sa_family == AF_INET ? F_IPV4 : F_IPV6; |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 132 | *type = SERV_FOR_NODOTS; |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 133 | if (serv->flags & SERV_NO_ADDR) |
Simon Kelley | 36717ee | 2004-09-20 19:20:58 +0100 | [diff] [blame] | 134 | flags = F_NXDOMAIN; |
| 135 | else if (serv->flags & SERV_LITERAL_ADDRESS) |
| 136 | { |
| 137 | if (sflag & qtype) |
| 138 | { |
| 139 | flags = sflag; |
| 140 | if (serv->addr.sa.sa_family == AF_INET) |
| 141 | *addrpp = (struct all_addr *)&serv->addr.in.sin_addr; |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 142 | #ifdef HAVE_IPV6 |
Simon Kelley | 36717ee | 2004-09-20 19:20:58 +0100 | [diff] [blame] | 143 | else |
| 144 | *addrpp = (struct all_addr *)&serv->addr.in6.sin6_addr; |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 145 | #endif |
Simon Kelley | 36717ee | 2004-09-20 19:20:58 +0100 | [diff] [blame] | 146 | } |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 147 | else if (!flags || (flags & F_NXDOMAIN)) |
Simon Kelley | 36717ee | 2004-09-20 19:20:58 +0100 | [diff] [blame] | 148 | flags = F_NOERR; |
| 149 | } |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 150 | } |
| 151 | else if (serv->flags & SERV_HAS_DOMAIN) |
| 152 | { |
| 153 | unsigned int domainlen = strlen(serv->domain); |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 154 | char *matchstart = qdomain + namelen - domainlen; |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 155 | if (namelen >= domainlen && |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 156 | hostname_isequal(matchstart, serv->domain) && |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 157 | (domainlen == 0 || namelen == domainlen || *(matchstart-1) == '.' )) |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 158 | { |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 159 | if (serv->flags & SERV_NO_REBIND) |
| 160 | *norebind = 1; |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 161 | else |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 162 | { |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 163 | unsigned int sflag = serv->addr.sa.sa_family == AF_INET ? F_IPV4 : F_IPV6; |
| 164 | /* implement priority rules for --address and --server for same domain. |
| 165 | --address wins if the address is for the correct AF |
| 166 | --server wins otherwise. */ |
| 167 | if (domainlen != 0 && domainlen == matchlen) |
Simon Kelley | 36717ee | 2004-09-20 19:20:58 +0100 | [diff] [blame] | 168 | { |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 169 | if ((serv->flags & SERV_LITERAL_ADDRESS)) |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 170 | { |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 171 | if (!(sflag & qtype) && flags == 0) |
| 172 | continue; |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 173 | } |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 174 | else |
| 175 | { |
| 176 | if (flags & (F_IPV4 | F_IPV6)) |
| 177 | continue; |
| 178 | } |
Simon Kelley | 36717ee | 2004-09-20 19:20:58 +0100 | [diff] [blame] | 179 | } |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 180 | |
| 181 | if (domainlen >= matchlen) |
| 182 | { |
| 183 | *type = serv->flags & (SERV_HAS_DOMAIN | SERV_USE_RESOLV | SERV_NO_REBIND); |
| 184 | *domain = serv->domain; |
| 185 | matchlen = domainlen; |
| 186 | if (serv->flags & SERV_NO_ADDR) |
| 187 | flags = F_NXDOMAIN; |
| 188 | else if (serv->flags & SERV_LITERAL_ADDRESS) |
| 189 | { |
| 190 | if (sflag & qtype) |
| 191 | { |
| 192 | flags = sflag; |
| 193 | if (serv->addr.sa.sa_family == AF_INET) |
| 194 | *addrpp = (struct all_addr *)&serv->addr.in.sin_addr; |
| 195 | #ifdef HAVE_IPV6 |
| 196 | else |
| 197 | *addrpp = (struct all_addr *)&serv->addr.in6.sin6_addr; |
| 198 | #endif |
| 199 | } |
| 200 | else if (!flags || (flags & F_NXDOMAIN)) |
| 201 | flags = F_NOERR; |
| 202 | } |
| 203 | else |
| 204 | flags = 0; |
| 205 | } |
| 206 | } |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 207 | } |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 208 | } |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 209 | |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 210 | if (flags == 0 && !(qtype & F_QUERY) && |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 211 | option_bool(OPT_NODOTS_LOCAL) && !strchr(qdomain, '.') && namelen != 0) |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 212 | /* don't forward A or AAAA queries for simple names, except the empty name */ |
| 213 | flags = F_NOERR; |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 214 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 215 | if (flags == F_NXDOMAIN && check_for_local_domain(qdomain, now)) |
Simon Kelley | c1bb850 | 2004-08-11 18:40:17 +0100 | [diff] [blame] | 216 | flags = F_NOERR; |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 217 | |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 218 | if (flags) |
| 219 | { |
| 220 | int logflags = 0; |
| 221 | |
| 222 | if (flags == F_NXDOMAIN || flags == F_NOERR) |
| 223 | logflags = F_NEG | qtype; |
| 224 | |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 225 | log_query(logflags | flags | F_CONFIG | F_FORWARD, qdomain, *addrpp, NULL); |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 226 | } |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 227 | else if ((*type) & SERV_USE_RESOLV) |
| 228 | { |
| 229 | *type = 0; /* use normal servers for this domain */ |
| 230 | *domain = NULL; |
| 231 | } |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 232 | return flags; |
| 233 | } |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 234 | |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 235 | static int forward_query(int udpfd, union mysockaddr *udpaddr, |
| 236 | struct all_addr *dst_addr, unsigned int dst_iface, |
Simon Kelley | 572b41e | 2011-02-18 18:11:18 +0000 | [diff] [blame] | 237 | struct dns_header *header, size_t plen, time_t now, struct frec *forward) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 238 | { |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 239 | char *domain = NULL; |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 240 | int type = 0, norebind = 0; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 241 | struct all_addr *addrp = NULL; |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 242 | unsigned int crc = questions_crc(header, plen, daemon->namebuff); |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 243 | unsigned int flags = 0; |
| 244 | unsigned int gotname = extract_request(header, plen, daemon->namebuff, NULL); |
Simon Kelley | de37951 | 2004-06-22 20:23:33 +0100 | [diff] [blame] | 245 | struct server *start = NULL; |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 246 | |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 247 | /* RFC 4035: sect 4.6 para 2 */ |
Simon Kelley | 572b41e | 2011-02-18 18:11:18 +0000 | [diff] [blame] | 248 | header->hb4 &= ~HB4_AD; |
| 249 | |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 250 | /* may be no servers available. */ |
| 251 | if (!daemon->servers) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 252 | forward = NULL; |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 253 | else if (forward || (forward = lookup_frec_by_sender(ntohs(header->id), udpaddr, crc))) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 254 | { |
Simon Kelley | de37951 | 2004-06-22 20:23:33 +0100 | [diff] [blame] | 255 | /* retry on existing query, send to all available servers */ |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 256 | domain = forward->sentto->domain; |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 257 | forward->sentto->failed_queries++; |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 258 | if (!option_bool(OPT_ORDER)) |
Simon Kelley | de37951 | 2004-06-22 20:23:33 +0100 | [diff] [blame] | 259 | { |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 260 | forward->forwardall = 1; |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 261 | daemon->last_server = NULL; |
Simon Kelley | de37951 | 2004-06-22 20:23:33 +0100 | [diff] [blame] | 262 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 263 | type = forward->sentto->flags & SERV_TYPE; |
Simon Kelley | de37951 | 2004-06-22 20:23:33 +0100 | [diff] [blame] | 264 | if (!(start = forward->sentto->next)) |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 265 | start = daemon->servers; /* at end of list, recycle */ |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 266 | header->id = htons(forward->new_id); |
| 267 | } |
| 268 | else |
| 269 | { |
| 270 | if (gotname) |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 271 | flags = search_servers(now, &addrp, gotname, daemon->namebuff, &type, &domain, &norebind); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 272 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 273 | if (!flags && !(forward = get_new_frec(now, NULL))) |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 274 | /* table full - server failure. */ |
| 275 | flags = F_NEG; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 276 | |
| 277 | if (forward) |
| 278 | { |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 279 | forward->source = *udpaddr; |
| 280 | forward->dest = *dst_addr; |
| 281 | forward->iface = dst_iface; |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 282 | forward->orig_id = ntohs(header->id); |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 283 | forward->new_id = get_id(crc); |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 284 | forward->fd = udpfd; |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 285 | forward->crc = crc; |
| 286 | forward->forwardall = 0; |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 287 | if (norebind) |
| 288 | forward->flags |= FREC_NOREBIND; |
Simon Kelley | 572b41e | 2011-02-18 18:11:18 +0000 | [diff] [blame] | 289 | if (header->hb4 & HB4_CD) |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 290 | forward->flags |= FREC_CHECKING_DISABLED; |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 291 | |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 292 | header->id = htons(forward->new_id); |
| 293 | |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 294 | /* In strict_order mode, always try servers in the order |
| 295 | specified in resolv.conf, if a domain is given |
| 296 | always try all the available servers, |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 297 | otherwise, use the one last known to work. */ |
| 298 | |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 299 | if (type == 0) |
| 300 | { |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 301 | if (option_bool(OPT_ORDER)) |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 302 | start = daemon->servers; |
| 303 | else if (!(start = daemon->last_server) || |
| 304 | daemon->forwardcount++ > FORWARD_TEST || |
| 305 | difftime(now, daemon->forwardtime) > FORWARD_TIME) |
| 306 | { |
| 307 | start = daemon->servers; |
| 308 | forward->forwardall = 1; |
| 309 | daemon->forwardcount = 0; |
| 310 | daemon->forwardtime = now; |
| 311 | } |
| 312 | } |
| 313 | else |
Simon Kelley | de37951 | 2004-06-22 20:23:33 +0100 | [diff] [blame] | 314 | { |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 315 | start = daemon->servers; |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 316 | if (!option_bool(OPT_ORDER)) |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 317 | forward->forwardall = 1; |
Simon Kelley | de37951 | 2004-06-22 20:23:33 +0100 | [diff] [blame] | 318 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 319 | } |
| 320 | } |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 321 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 322 | /* check for send errors here (no route to host) |
| 323 | if we fail to send to all nameservers, send back an error |
| 324 | packet straight away (helps modem users when offline) */ |
| 325 | |
| 326 | if (!flags && forward) |
| 327 | { |
Simon Kelley | de37951 | 2004-06-22 20:23:33 +0100 | [diff] [blame] | 328 | struct server *firstsentto = start; |
| 329 | int forwarded = 0; |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 330 | |
Giacomo Tazzari | 797a7af | 2013-04-22 13:16:37 +0100 | [diff] [blame] | 331 | if (option_bool(OPT_ADD_MAC)) |
| 332 | plen = add_mac(header, plen, ((char *) header) + PACKETSZ, &forward->source); |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 333 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 334 | while (1) |
| 335 | { |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 336 | /* only send to servers dealing with our domain. |
| 337 | domain may be NULL, in which case server->domain |
| 338 | must be NULL also. */ |
| 339 | |
Simon Kelley | de37951 | 2004-06-22 20:23:33 +0100 | [diff] [blame] | 340 | if (type == (start->flags & SERV_TYPE) && |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 341 | (type != SERV_HAS_DOMAIN || hostname_isequal(domain, start->domain)) && |
| 342 | !(start->flags & SERV_LITERAL_ADDRESS)) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 343 | { |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 344 | int fd; |
| 345 | |
| 346 | /* find server socket to use, may need to get random one. */ |
| 347 | if (start->sfd) |
| 348 | fd = start->sfd->fd; |
| 349 | else |
| 350 | { |
| 351 | #ifdef HAVE_IPV6 |
| 352 | if (start->addr.sa.sa_family == AF_INET6) |
| 353 | { |
| 354 | if (!forward->rfd6 && |
| 355 | !(forward->rfd6 = allocate_rfd(AF_INET6))) |
| 356 | break; |
Simon Kelley | 3927da4 | 2008-07-20 15:10:39 +0100 | [diff] [blame] | 357 | daemon->rfd_save = forward->rfd6; |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 358 | fd = forward->rfd6->fd; |
| 359 | } |
| 360 | else |
| 361 | #endif |
| 362 | { |
| 363 | if (!forward->rfd4 && |
| 364 | !(forward->rfd4 = allocate_rfd(AF_INET))) |
| 365 | break; |
Simon Kelley | 3927da4 | 2008-07-20 15:10:39 +0100 | [diff] [blame] | 366 | daemon->rfd_save = forward->rfd4; |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 367 | fd = forward->rfd4->fd; |
| 368 | } |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 369 | |
| 370 | #ifdef HAVE_CONNTRACK |
| 371 | /* Copy connection mark of incoming query to outgoing connection. */ |
| 372 | if (option_bool(OPT_CONNTRACK)) |
| 373 | { |
| 374 | unsigned int mark; |
Giacomo Tazzari | 797a7af | 2013-04-22 13:16:37 +0100 | [diff] [blame] | 375 | if (get_incoming_mark(&forward->source, &forward->dest, 0, &mark)) |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 376 | setsockopt(fd, SOL_SOCKET, SO_MARK, &mark, sizeof(unsigned int)); |
| 377 | } |
| 378 | #endif |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | if (sendto(fd, (char *)header, plen, 0, |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 382 | &start->addr.sa, |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 383 | sa_len(&start->addr)) == -1) |
| 384 | { |
| 385 | if (retry_send()) |
| 386 | continue; |
| 387 | } |
| 388 | else |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 389 | { |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 390 | /* Keep info in case we want to re-send this packet */ |
| 391 | daemon->srv_save = start; |
| 392 | daemon->packet_len = plen; |
| 393 | |
Simon Kelley | de37951 | 2004-06-22 20:23:33 +0100 | [diff] [blame] | 394 | if (!gotname) |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 395 | strcpy(daemon->namebuff, "query"); |
Simon Kelley | de37951 | 2004-06-22 20:23:33 +0100 | [diff] [blame] | 396 | if (start->addr.sa.sa_family == AF_INET) |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 397 | log_query(F_SERVER | F_IPV4 | F_FORWARD, daemon->namebuff, |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 398 | (struct all_addr *)&start->addr.in.sin_addr, NULL); |
Simon Kelley | de37951 | 2004-06-22 20:23:33 +0100 | [diff] [blame] | 399 | #ifdef HAVE_IPV6 |
| 400 | else |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 401 | log_query(F_SERVER | F_IPV6 | F_FORWARD, daemon->namebuff, |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 402 | (struct all_addr *)&start->addr.in6.sin6_addr, NULL); |
Simon Kelley | de37951 | 2004-06-22 20:23:33 +0100 | [diff] [blame] | 403 | #endif |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 404 | start->queries++; |
Simon Kelley | de37951 | 2004-06-22 20:23:33 +0100 | [diff] [blame] | 405 | forwarded = 1; |
| 406 | forward->sentto = start; |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 407 | if (!forward->forwardall) |
Simon Kelley | de37951 | 2004-06-22 20:23:33 +0100 | [diff] [blame] | 408 | break; |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 409 | forward->forwardall++; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 410 | } |
| 411 | } |
| 412 | |
Simon Kelley | de37951 | 2004-06-22 20:23:33 +0100 | [diff] [blame] | 413 | if (!(start = start->next)) |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 414 | start = daemon->servers; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 415 | |
Simon Kelley | de37951 | 2004-06-22 20:23:33 +0100 | [diff] [blame] | 416 | if (start == firstsentto) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 417 | break; |
| 418 | } |
| 419 | |
Simon Kelley | de37951 | 2004-06-22 20:23:33 +0100 | [diff] [blame] | 420 | if (forwarded) |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 421 | return 1; |
Simon Kelley | de37951 | 2004-06-22 20:23:33 +0100 | [diff] [blame] | 422 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 423 | /* could not send on, prepare to return */ |
| 424 | header->id = htons(forward->orig_id); |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 425 | free_frec(forward); /* cancel */ |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | /* 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] | 429 | if (udpfd != -1) |
| 430 | { |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 431 | plen = setup_reply(header, plen, addrp, flags, daemon->local_ttl); |
Simon Kelley | 54dd393 | 2012-06-20 11:23:38 +0100 | [diff] [blame] | 432 | 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] | 433 | } |
| 434 | |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 435 | return 0; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 436 | } |
| 437 | |
Simon Kelley | 572b41e | 2011-02-18 18:11:18 +0000 | [diff] [blame] | 438 | static size_t process_reply(struct dns_header *header, time_t now, |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 439 | struct server *server, size_t n, int check_rebind, int checking_disabled) |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 440 | { |
Simon Kelley | 36717ee | 2004-09-20 19:20:58 +0100 | [diff] [blame] | 441 | unsigned char *pheader, *sizep; |
Jason A. Donenfeld | 13d86c7 | 2013-02-22 18:20:53 +0000 | [diff] [blame] | 442 | char **sets = 0; |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 443 | int munged = 0, is_sign; |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 444 | size_t plen; |
| 445 | |
Jason A. Donenfeld | 13d86c7 | 2013-02-22 18:20:53 +0000 | [diff] [blame] | 446 | #ifdef HAVE_IPSET |
| 447 | /* Similar algorithm to search_servers. */ |
| 448 | struct ipsets *ipset_pos; |
| 449 | unsigned int namelen = strlen(daemon->namebuff); |
| 450 | unsigned int matchlen = 0; |
| 451 | for (ipset_pos = daemon->ipsets; ipset_pos; ipset_pos = ipset_pos->next) |
| 452 | { |
| 453 | unsigned int domainlen = strlen(ipset_pos->domain); |
| 454 | char *matchstart = daemon->namebuff + namelen - domainlen; |
| 455 | if (namelen >= domainlen && hostname_isequal(matchstart, ipset_pos->domain) && |
| 456 | (domainlen == 0 || namelen == domainlen || *(matchstart - 1) == '.' ) && |
| 457 | domainlen >= matchlen) { |
| 458 | matchlen = domainlen; |
| 459 | sets = ipset_pos->sets; |
| 460 | } |
| 461 | } |
| 462 | #endif |
| 463 | |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 464 | /* If upstream is advertising a larger UDP packet size |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 465 | than we allow, trim it so that we don't get overlarge |
| 466 | requests for the client. We can't do this for signed packets. */ |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 467 | |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 468 | if ((pheader = find_pseudoheader(header, n, &plen, &sizep, &is_sign)) && !is_sign) |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 469 | { |
| 470 | unsigned short udpsz; |
Simon Kelley | 36717ee | 2004-09-20 19:20:58 +0100 | [diff] [blame] | 471 | unsigned char *psave = sizep; |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 472 | |
Simon Kelley | 36717ee | 2004-09-20 19:20:58 +0100 | [diff] [blame] | 473 | GETSHORT(udpsz, sizep); |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 474 | if (udpsz > daemon->edns_pktsz) |
| 475 | PUTSHORT(daemon->edns_pktsz, psave); |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 476 | } |
| 477 | |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 478 | /* RFC 4035 sect 4.6 para 3 */ |
| 479 | if (!is_sign && !option_bool(OPT_DNSSEC)) |
Simon Kelley | 572b41e | 2011-02-18 18:11:18 +0000 | [diff] [blame] | 480 | header->hb4 &= ~HB4_AD; |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 481 | |
Simon Kelley | 572b41e | 2011-02-18 18:11:18 +0000 | [diff] [blame] | 482 | if (OPCODE(header) != QUERY || (RCODE(header) != NOERROR && RCODE(header) != NXDOMAIN)) |
Simon Kelley | 36717ee | 2004-09-20 19:20:58 +0100 | [diff] [blame] | 483 | return n; |
| 484 | |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 485 | /* Complain loudly if the upstream server is non-recursive. */ |
Simon Kelley | 572b41e | 2011-02-18 18:11:18 +0000 | [diff] [blame] | 486 | if (!(header->hb4 & HB4_RA) && RCODE(header) == NOERROR && ntohs(header->ancount) == 0 && |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 487 | server && !(server->flags & SERV_WARNED_RECURSIVE)) |
| 488 | { |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 489 | prettyprint_addr(&server->addr, daemon->namebuff); |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 490 | 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] | 491 | if (!option_bool(OPT_LOG)) |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 492 | server->flags |= SERV_WARNED_RECURSIVE; |
| 493 | } |
| 494 | |
Simon Kelley | 572b41e | 2011-02-18 18:11:18 +0000 | [diff] [blame] | 495 | if (daemon->bogus_addr && RCODE(header) != NXDOMAIN && |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 496 | check_for_bogus_wildcard(header, n, daemon->namebuff, daemon->bogus_addr, now)) |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 497 | { |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 498 | munged = 1; |
Simon Kelley | 572b41e | 2011-02-18 18:11:18 +0000 | [diff] [blame] | 499 | SET_RCODE(header, NXDOMAIN); |
| 500 | header->hb3 &= ~HB3_AA; |
Simon Kelley | 36717ee | 2004-09-20 19:20:58 +0100 | [diff] [blame] | 501 | } |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 502 | else |
Simon Kelley | 36717ee | 2004-09-20 19:20:58 +0100 | [diff] [blame] | 503 | { |
Simon Kelley | 572b41e | 2011-02-18 18:11:18 +0000 | [diff] [blame] | 504 | if (RCODE(header) == NXDOMAIN && |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 505 | extract_request(header, n, daemon->namebuff, NULL) && |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 506 | check_for_local_domain(daemon->namebuff, now)) |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 507 | { |
Simon Kelley | 36717ee | 2004-09-20 19:20:58 +0100 | [diff] [blame] | 508 | /* if we forwarded a query for a locally known name (because it was for |
| 509 | an unknown type) and the answer is NXDOMAIN, convert that to NODATA, |
| 510 | since we know that the domain exists, even if upstream doesn't */ |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 511 | munged = 1; |
Simon Kelley | 572b41e | 2011-02-18 18:11:18 +0000 | [diff] [blame] | 512 | header->hb3 |= HB3_AA; |
| 513 | SET_RCODE(header, NOERROR); |
Simon Kelley | 36717ee | 2004-09-20 19:20:58 +0100 | [diff] [blame] | 514 | } |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 515 | |
Jason A. Donenfeld | 13d86c7 | 2013-02-22 18:20:53 +0000 | [diff] [blame] | 516 | if (extract_addresses(header, n, daemon->namebuff, now, sets, is_sign, check_rebind, checking_disabled)) |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 517 | { |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 518 | my_syslog(LOG_WARNING, _("possible DNS-rebind attack detected: %s"), daemon->namebuff); |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 519 | munged = 1; |
| 520 | } |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 521 | } |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 522 | |
| 523 | /* do this after extract_addresses. Ensure NODATA reply and remove |
| 524 | nameserver info. */ |
| 525 | |
| 526 | if (munged) |
| 527 | { |
| 528 | header->ancount = htons(0); |
| 529 | header->nscount = htons(0); |
| 530 | header->arcount = htons(0); |
| 531 | } |
| 532 | |
Simon Kelley | 36717ee | 2004-09-20 19:20:58 +0100 | [diff] [blame] | 533 | /* the bogus-nxdomain stuff, doctor and NXDOMAIN->NODATA munging can all elide |
| 534 | sections of the packet. Find the new length here and put back pseudoheader |
| 535 | if it was removed. */ |
| 536 | return resize_packet(header, n, pheader, plen); |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 537 | } |
| 538 | |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 539 | /* sets new last_server */ |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 540 | void reply_query(int fd, int family, time_t now) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 541 | { |
| 542 | /* packet from peer server, extract data for cache, and send to |
| 543 | original requester */ |
Simon Kelley | 572b41e | 2011-02-18 18:11:18 +0000 | [diff] [blame] | 544 | struct dns_header *header; |
Simon Kelley | de37951 | 2004-06-22 20:23:33 +0100 | [diff] [blame] | 545 | union mysockaddr serveraddr; |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 546 | struct frec *forward; |
Simon Kelley | de37951 | 2004-06-22 20:23:33 +0100 | [diff] [blame] | 547 | socklen_t addrlen = sizeof(serveraddr); |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 548 | ssize_t n = recvfrom(fd, daemon->packet, daemon->edns_pktsz, 0, &serveraddr.sa, &addrlen); |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 549 | size_t nn; |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 550 | struct server *server; |
| 551 | |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 552 | /* packet buffer overwritten */ |
| 553 | daemon->srv_save = NULL; |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 554 | |
Simon Kelley | de37951 | 2004-06-22 20:23:33 +0100 | [diff] [blame] | 555 | /* 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] | 556 | serveraddr.sa.sa_family = family; |
Simon Kelley | de37951 | 2004-06-22 20:23:33 +0100 | [diff] [blame] | 557 | #ifdef HAVE_IPV6 |
| 558 | if (serveraddr.sa.sa_family == AF_INET6) |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 559 | serveraddr.in6.sin6_flowinfo = 0; |
Simon Kelley | de37951 | 2004-06-22 20:23:33 +0100 | [diff] [blame] | 560 | #endif |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 561 | |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 562 | /* spoof check: answer must come from known server, */ |
| 563 | for (server = daemon->servers; server; server = server->next) |
| 564 | if (!(server->flags & (SERV_LITERAL_ADDRESS | SERV_NO_ADDR)) && |
| 565 | sockaddr_isequal(&server->addr, &serveraddr)) |
| 566 | break; |
| 567 | |
Simon Kelley | 572b41e | 2011-02-18 18:11:18 +0000 | [diff] [blame] | 568 | header = (struct dns_header *)daemon->packet; |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 569 | |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 570 | if (!server || |
Simon Kelley | 572b41e | 2011-02-18 18:11:18 +0000 | [diff] [blame] | 571 | n < (int)sizeof(struct dns_header) || !(header->hb3 & HB3_QR) || |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 572 | !(forward = lookup_frec(ntohs(header->id), questions_crc(header, n, daemon->namebuff)))) |
| 573 | return; |
| 574 | |
| 575 | server = forward->sentto; |
| 576 | |
Simon Kelley | 572b41e | 2011-02-18 18:11:18 +0000 | [diff] [blame] | 577 | if ((RCODE(header) == SERVFAIL || RCODE(header) == REFUSED) && |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 578 | !option_bool(OPT_ORDER) && |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 579 | forward->forwardall == 0) |
| 580 | /* for broken servers, attempt to send to another one. */ |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 581 | { |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 582 | unsigned char *pheader; |
| 583 | size_t plen; |
| 584 | int is_sign; |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 585 | |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 586 | /* recreate query from reply */ |
| 587 | pheader = find_pseudoheader(header, (size_t)n, &plen, NULL, &is_sign); |
| 588 | if (!is_sign) |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 589 | { |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 590 | header->ancount = htons(0); |
| 591 | header->nscount = htons(0); |
| 592 | header->arcount = htons(0); |
| 593 | if ((nn = resize_packet(header, (size_t)n, pheader, plen))) |
| 594 | { |
Simon Kelley | 572b41e | 2011-02-18 18:11:18 +0000 | [diff] [blame] | 595 | header->hb3 &= ~(HB3_QR | HB3_TC); |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 596 | forward_query(-1, NULL, NULL, 0, header, nn, now, forward); |
| 597 | return; |
| 598 | } |
| 599 | } |
| 600 | } |
| 601 | |
| 602 | if ((forward->sentto->flags & SERV_TYPE) == 0) |
| 603 | { |
Simon Kelley | 572b41e | 2011-02-18 18:11:18 +0000 | [diff] [blame] | 604 | if (RCODE(header) == SERVFAIL || RCODE(header) == REFUSED) |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 605 | server = NULL; |
| 606 | else |
| 607 | { |
| 608 | struct server *last_server; |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 609 | |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 610 | /* find good server by address if possible, otherwise assume the last one we sent to */ |
| 611 | for (last_server = daemon->servers; last_server; last_server = last_server->next) |
| 612 | if (!(last_server->flags & (SERV_LITERAL_ADDRESS | SERV_HAS_DOMAIN | SERV_FOR_NODOTS | SERV_NO_ADDR)) && |
| 613 | sockaddr_isequal(&last_server->addr, &serveraddr)) |
| 614 | { |
| 615 | server = last_server; |
| 616 | break; |
| 617 | } |
| 618 | } |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 619 | if (!option_bool(OPT_ALL_SERVERS)) |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 620 | daemon->last_server = server; |
| 621 | } |
| 622 | |
| 623 | /* If the answer is an error, keep the forward record in place in case |
| 624 | we get a good reply from another server. Kill it when we've |
| 625 | had replies from all to avoid filling the forwarding table when |
| 626 | everything is broken */ |
| 627 | if (forward->forwardall == 0 || --forward->forwardall == 1 || |
Simon Kelley | 572b41e | 2011-02-18 18:11:18 +0000 | [diff] [blame] | 628 | (RCODE(header) != REFUSED && RCODE(header) != SERVFAIL)) |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 629 | { |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 630 | int check_rebind = !(forward->flags & FREC_NOREBIND); |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 631 | |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 632 | if (!option_bool(OPT_NO_REBIND)) |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 633 | check_rebind = 0; |
| 634 | |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 635 | if ((nn = process_reply(header, now, server, (size_t)n, check_rebind, forward->flags & FREC_CHECKING_DISABLED))) |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 636 | { |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 637 | header->id = htons(forward->orig_id); |
Simon Kelley | 572b41e | 2011-02-18 18:11:18 +0000 | [diff] [blame] | 638 | header->hb4 |= HB4_RA; /* recursion if available */ |
Simon Kelley | 54dd393 | 2012-06-20 11:23:38 +0100 | [diff] [blame] | 639 | 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] | 640 | &forward->source, &forward->dest, forward->iface); |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 641 | } |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 642 | free_frec(forward); /* cancel */ |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 643 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 644 | } |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 645 | |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 646 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 647 | void receive_query(struct listener *listen, time_t now) |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 648 | { |
Simon Kelley | 572b41e | 2011-02-18 18:11:18 +0000 | [diff] [blame] | 649 | struct dns_header *header = (struct dns_header *)daemon->packet; |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 650 | union mysockaddr source_addr; |
Simon Kelley | c1bb850 | 2004-08-11 18:40:17 +0100 | [diff] [blame] | 651 | unsigned short type; |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 652 | struct all_addr dst_addr; |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 653 | struct in_addr netmask, dst_addr_4; |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 654 | size_t m; |
| 655 | ssize_t n; |
| 656 | int if_index = 0; |
Simon Kelley | 4f7b304 | 2012-11-28 21:27:02 +0000 | [diff] [blame] | 657 | int auth_dns = 0; |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 658 | struct iovec iov[1]; |
| 659 | struct msghdr msg; |
| 660 | struct cmsghdr *cmptr; |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 661 | union { |
| 662 | struct cmsghdr align; /* this ensures alignment */ |
| 663 | #ifdef HAVE_IPV6 |
| 664 | char control6[CMSG_SPACE(sizeof(struct in6_pktinfo))]; |
| 665 | #endif |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 666 | #if defined(HAVE_LINUX_NETWORK) |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 667 | char control[CMSG_SPACE(sizeof(struct in_pktinfo))]; |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 668 | #elif defined(IP_RECVDSTADDR) && defined(HAVE_SOLARIS_NETWORK) |
| 669 | char control[CMSG_SPACE(sizeof(struct in_addr)) + |
| 670 | CMSG_SPACE(sizeof(unsigned int))]; |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 671 | #elif defined(IP_RECVDSTADDR) |
| 672 | char control[CMSG_SPACE(sizeof(struct in_addr)) + |
| 673 | CMSG_SPACE(sizeof(struct sockaddr_dl))]; |
| 674 | #endif |
| 675 | } control_u; |
| 676 | |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 677 | /* packet buffer overwritten */ |
| 678 | daemon->srv_save = NULL; |
| 679 | |
Simon Kelley | 4f7b304 | 2012-11-28 21:27:02 +0000 | [diff] [blame] | 680 | dst_addr_4.s_addr = 0; |
| 681 | netmask.s_addr = 0; |
| 682 | |
Simon Kelley | 7e5664b | 2013-04-05 16:57:41 +0100 | [diff] [blame] | 683 | if (option_bool(OPT_NOWILD) && listen->iface) |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 684 | { |
Simon Kelley | 4f7b304 | 2012-11-28 21:27:02 +0000 | [diff] [blame] | 685 | auth_dns = listen->iface->dns_auth; |
| 686 | |
| 687 | if (listen->family == AF_INET) |
| 688 | { |
| 689 | dst_addr_4 = listen->iface->addr.in.sin_addr; |
| 690 | netmask = listen->iface->netmask; |
| 691 | } |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 692 | } |
Simon Kelley | 4f7b304 | 2012-11-28 21:27:02 +0000 | [diff] [blame] | 693 | |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 694 | iov[0].iov_base = daemon->packet; |
| 695 | iov[0].iov_len = daemon->edns_pktsz; |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 696 | |
| 697 | msg.msg_control = control_u.control; |
| 698 | msg.msg_controllen = sizeof(control_u); |
| 699 | msg.msg_flags = 0; |
| 700 | msg.msg_name = &source_addr; |
| 701 | msg.msg_namelen = sizeof(source_addr); |
| 702 | msg.msg_iov = iov; |
| 703 | msg.msg_iovlen = 1; |
| 704 | |
Simon Kelley | de37951 | 2004-06-22 20:23:33 +0100 | [diff] [blame] | 705 | if ((n = recvmsg(listen->fd, &msg, 0)) == -1) |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 706 | return; |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 707 | |
Simon Kelley | 572b41e | 2011-02-18 18:11:18 +0000 | [diff] [blame] | 708 | if (n < (int)sizeof(struct dns_header) || |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 709 | (msg.msg_flags & MSG_TRUNC) || |
Simon Kelley | 572b41e | 2011-02-18 18:11:18 +0000 | [diff] [blame] | 710 | (header->hb3 & HB3_QR)) |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 711 | return; |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 712 | |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 713 | source_addr.sa.sa_family = listen->family; |
| 714 | #ifdef HAVE_IPV6 |
| 715 | if (listen->family == AF_INET6) |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 716 | source_addr.in6.sin6_flowinfo = 0; |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 717 | #endif |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 718 | |
| 719 | if (!option_bool(OPT_NOWILD)) |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 720 | { |
Simon Kelley | 8a911cc | 2004-03-16 18:35:52 +0000 | [diff] [blame] | 721 | struct ifreq ifr; |
| 722 | |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 723 | if (msg.msg_controllen < sizeof(struct cmsghdr)) |
| 724 | return; |
| 725 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 726 | #if defined(HAVE_LINUX_NETWORK) |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 727 | if (listen->family == AF_INET) |
| 728 | for (cmptr = CMSG_FIRSTHDR(&msg); cmptr; cmptr = CMSG_NXTHDR(&msg, cmptr)) |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 729 | if (cmptr->cmsg_level == IPPROTO_IP && cmptr->cmsg_type == IP_PKTINFO) |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 730 | { |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 731 | union { |
| 732 | unsigned char *c; |
| 733 | struct in_pktinfo *p; |
| 734 | } p; |
| 735 | p.c = CMSG_DATA(cmptr); |
| 736 | dst_addr_4 = dst_addr.addr.addr4 = p.p->ipi_spec_dst; |
| 737 | if_index = p.p->ipi_ifindex; |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 738 | } |
| 739 | #elif defined(IP_RECVDSTADDR) && defined(IP_RECVIF) |
| 740 | if (listen->family == AF_INET) |
| 741 | { |
| 742 | for (cmptr = CMSG_FIRSTHDR(&msg); cmptr; cmptr = CMSG_NXTHDR(&msg, cmptr)) |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 743 | { |
| 744 | union { |
| 745 | unsigned char *c; |
| 746 | unsigned int *i; |
| 747 | struct in_addr *a; |
| 748 | #ifndef HAVE_SOLARIS_NETWORK |
| 749 | struct sockaddr_dl *s; |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 750 | #endif |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 751 | } p; |
| 752 | p.c = CMSG_DATA(cmptr); |
| 753 | if (cmptr->cmsg_level == IPPROTO_IP && cmptr->cmsg_type == IP_RECVDSTADDR) |
| 754 | dst_addr_4 = dst_addr.addr.addr4 = *(p.a); |
| 755 | else if (cmptr->cmsg_level == IPPROTO_IP && cmptr->cmsg_type == IP_RECVIF) |
| 756 | #ifdef HAVE_SOLARIS_NETWORK |
| 757 | if_index = *(p.i); |
| 758 | #else |
| 759 | if_index = p.s->sdl_index; |
| 760 | #endif |
| 761 | } |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 762 | } |
| 763 | #endif |
| 764 | |
| 765 | #ifdef HAVE_IPV6 |
| 766 | if (listen->family == AF_INET6) |
| 767 | { |
| 768 | for (cmptr = CMSG_FIRSTHDR(&msg); cmptr; cmptr = CMSG_NXTHDR(&msg, cmptr)) |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 769 | if (cmptr->cmsg_level == IPPROTO_IPV6 && cmptr->cmsg_type == daemon->v6pktinfo) |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 770 | { |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 771 | union { |
| 772 | unsigned char *c; |
| 773 | struct in6_pktinfo *p; |
| 774 | } p; |
| 775 | p.c = CMSG_DATA(cmptr); |
| 776 | |
| 777 | dst_addr.addr.addr6 = p.p->ipi6_addr; |
| 778 | if_index = p.p->ipi6_ifindex; |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 779 | } |
| 780 | } |
| 781 | #endif |
| 782 | |
| 783 | /* enforce available interface configuration */ |
| 784 | |
Simon Kelley | e25db1f | 2013-01-29 22:10:26 +0000 | [diff] [blame] | 785 | if (!indextoname(listen->fd, if_index, ifr.ifr_name)) |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 786 | return; |
| 787 | |
Simon Kelley | e25db1f | 2013-01-29 22:10:26 +0000 | [diff] [blame] | 788 | if (!iface_check(listen->family, &dst_addr, ifr.ifr_name, &auth_dns)) |
| 789 | { |
| 790 | if (!option_bool(OPT_CLEVERBIND)) |
Simon Kelley | 115ac3e | 2013-05-20 11:28:32 +0100 | [diff] [blame^] | 791 | enumerate_interfaces(0); |
Simon Kelley | 3f2873d | 2013-05-14 11:28:47 +0100 | [diff] [blame] | 792 | if (!loopback_exception(listen->fd, listen->family, &dst_addr, ifr.ifr_name) && |
| 793 | !label_exception(if_index, listen->family, &dst_addr)) |
Simon Kelley | e25db1f | 2013-01-29 22:10:26 +0000 | [diff] [blame] | 794 | return; |
| 795 | } |
| 796 | |
Simon Kelley | 552af8b | 2012-02-29 20:10:31 +0000 | [diff] [blame] | 797 | if (listen->family == AF_INET && option_bool(OPT_LOCALISE)) |
| 798 | { |
| 799 | struct irec *iface; |
| 800 | |
| 801 | /* get the netmask of the interface whch has the address we were sent to. |
| 802 | This is no neccessarily the interface we arrived on. */ |
| 803 | |
| 804 | for (iface = daemon->interfaces; iface; iface = iface->next) |
| 805 | if (iface->addr.sa.sa_family == AF_INET && |
| 806 | iface->addr.in.sin_addr.s_addr == dst_addr_4.s_addr) |
| 807 | break; |
| 808 | |
| 809 | /* interface may be new */ |
Simon Kelley | e25db1f | 2013-01-29 22:10:26 +0000 | [diff] [blame] | 810 | if (!iface && !option_bool(OPT_CLEVERBIND)) |
Simon Kelley | 115ac3e | 2013-05-20 11:28:32 +0100 | [diff] [blame^] | 811 | enumerate_interfaces(0); |
Simon Kelley | 552af8b | 2012-02-29 20:10:31 +0000 | [diff] [blame] | 812 | |
| 813 | for (iface = daemon->interfaces; iface; iface = iface->next) |
| 814 | if (iface->addr.sa.sa_family == AF_INET && |
| 815 | iface->addr.in.sin_addr.s_addr == dst_addr_4.s_addr) |
| 816 | break; |
| 817 | |
| 818 | /* If we failed, abandon localisation */ |
| 819 | if (iface) |
| 820 | netmask = iface->netmask; |
| 821 | else |
| 822 | dst_addr_4.s_addr = 0; |
| 823 | } |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 824 | } |
| 825 | |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 826 | if (extract_request(header, (size_t)n, daemon->namebuff, &type)) |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 827 | { |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 828 | char types[20]; |
| 829 | |
Simon Kelley | 4f7b304 | 2012-11-28 21:27:02 +0000 | [diff] [blame] | 830 | querystr(auth_dns ? "auth" : "query", types, type); |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 831 | |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 832 | if (listen->family == AF_INET) |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 833 | log_query(F_QUERY | F_IPV4 | F_FORWARD, daemon->namebuff, |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 834 | (struct all_addr *)&source_addr.in.sin_addr, types); |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 835 | #ifdef HAVE_IPV6 |
| 836 | else |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 837 | log_query(F_QUERY | F_IPV6 | F_FORWARD, daemon->namebuff, |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 838 | (struct all_addr *)&source_addr.in6.sin6_addr, types); |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 839 | #endif |
| 840 | } |
| 841 | |
Simon Kelley | 4820dce | 2012-12-18 18:30:30 +0000 | [diff] [blame] | 842 | #ifdef HAVE_AUTH |
Simon Kelley | 4f7b304 | 2012-11-28 21:27:02 +0000 | [diff] [blame] | 843 | if (auth_dns) |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 844 | { |
Simon Kelley | 4967876 | 2012-12-09 18:24:58 +0000 | [diff] [blame] | 845 | m = answer_auth(header, ((char *) header) + PACKETSZ, (size_t)n, now, &source_addr); |
Simon Kelley | 4f7b304 | 2012-11-28 21:27:02 +0000 | [diff] [blame] | 846 | if (m >= 1) |
| 847 | send_from(listen->fd, option_bool(OPT_NOWILD) || option_bool(OPT_CLEVERBIND), |
| 848 | (char *)header, m, &source_addr, &dst_addr, if_index); |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 849 | } |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 850 | else |
Simon Kelley | 4820dce | 2012-12-18 18:30:30 +0000 | [diff] [blame] | 851 | #endif |
Simon Kelley | 4f7b304 | 2012-11-28 21:27:02 +0000 | [diff] [blame] | 852 | { |
| 853 | m = answer_request(header, ((char *) header) + PACKETSZ, (size_t)n, |
| 854 | dst_addr_4, netmask, now); |
| 855 | |
| 856 | if (m >= 1) |
| 857 | { |
| 858 | send_from(listen->fd, option_bool(OPT_NOWILD) || option_bool(OPT_CLEVERBIND), |
| 859 | (char *)header, m, &source_addr, &dst_addr, if_index); |
| 860 | daemon->local_answer++; |
| 861 | } |
| 862 | else if (forward_query(listen->fd, &source_addr, &dst_addr, if_index, |
| 863 | header, (size_t)n, now, NULL)) |
| 864 | daemon->queries_forwarded++; |
| 865 | else |
| 866 | daemon->local_answer++; |
| 867 | } |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 868 | } |
| 869 | |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 870 | /* The daemon forks before calling this: it should deal with one connection, |
| 871 | blocking as neccessary, and then return. Note, need to be a bit careful |
| 872 | about resources for debug mode, when the fork is suppressed: that's |
| 873 | done by the caller. */ |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 874 | unsigned char *tcp_request(int confd, time_t now, |
Simon Kelley | 4f7b304 | 2012-11-28 21:27:02 +0000 | [diff] [blame] | 875 | union mysockaddr *local_addr, struct in_addr netmask, int auth_dns) |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 876 | { |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 877 | size_t size = 0; |
| 878 | int norebind = 0; |
| 879 | int checking_disabled; |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 880 | size_t m; |
Simon Kelley | ee86ce6 | 2012-12-07 11:54:46 +0000 | [diff] [blame] | 881 | unsigned short qtype; |
| 882 | unsigned int gotname; |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 883 | unsigned char c1, c2; |
Simon Kelley | 4b5ea12 | 2013-04-22 10:18:26 +0100 | [diff] [blame] | 884 | /* Max TCP packet + slop + size */ |
| 885 | unsigned char *packet = whine_malloc(65536 + MAXDNAME + RRFIXEDSZ + sizeof(u16)); |
| 886 | unsigned char *payload = &packet[2]; |
| 887 | /* largest field in header is 16-bits, so this is still sufficiently aligned */ |
| 888 | struct dns_header *header = (struct dns_header *)payload; |
| 889 | u16 *length = (u16 *)packet; |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 890 | struct server *last_server; |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 891 | struct in_addr dst_addr_4; |
| 892 | union mysockaddr peer_addr; |
| 893 | socklen_t peer_len = sizeof(union mysockaddr); |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 894 | |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 895 | if (getpeername(confd, (struct sockaddr *)&peer_addr, &peer_len) == -1) |
| 896 | return packet; |
| 897 | |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 898 | while (1) |
| 899 | { |
| 900 | if (!packet || |
| 901 | !read_write(confd, &c1, 1, 1) || !read_write(confd, &c2, 1, 1) || |
| 902 | !(size = c1 << 8 | c2) || |
Simon Kelley | 4b5ea12 | 2013-04-22 10:18:26 +0100 | [diff] [blame] | 903 | !read_write(confd, payload, size, 1)) |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 904 | return packet; |
| 905 | |
Simon Kelley | 572b41e | 2011-02-18 18:11:18 +0000 | [diff] [blame] | 906 | if (size < (int)sizeof(struct dns_header)) |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 907 | continue; |
| 908 | |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 909 | /* save state of "cd" flag in query */ |
Simon Kelley | 572b41e | 2011-02-18 18:11:18 +0000 | [diff] [blame] | 910 | checking_disabled = header->hb4 & HB4_CD; |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 911 | |
| 912 | /* RFC 4035: sect 4.6 para 2 */ |
Simon Kelley | 572b41e | 2011-02-18 18:11:18 +0000 | [diff] [blame] | 913 | header->hb4 &= ~HB4_AD; |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 914 | |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 915 | if ((gotname = extract_request(header, (unsigned int)size, daemon->namebuff, &qtype))) |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 916 | { |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 917 | char types[20]; |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 918 | |
Simon Kelley | 4f7b304 | 2012-11-28 21:27:02 +0000 | [diff] [blame] | 919 | querystr(auth_dns ? "auth" : "query", types, qtype); |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 920 | |
| 921 | if (peer_addr.sa.sa_family == AF_INET) |
| 922 | log_query(F_QUERY | F_IPV4 | F_FORWARD, daemon->namebuff, |
| 923 | (struct all_addr *)&peer_addr.in.sin_addr, types); |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 924 | #ifdef HAVE_IPV6 |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 925 | else |
| 926 | log_query(F_QUERY | F_IPV6 | F_FORWARD, daemon->namebuff, |
| 927 | (struct all_addr *)&peer_addr.in6.sin6_addr, types); |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 928 | #endif |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 929 | } |
| 930 | |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 931 | if (local_addr->sa.sa_family == AF_INET) |
| 932 | dst_addr_4 = local_addr->in.sin_addr; |
| 933 | else |
| 934 | dst_addr_4.s_addr = 0; |
| 935 | |
Simon Kelley | 4820dce | 2012-12-18 18:30:30 +0000 | [diff] [blame] | 936 | #ifdef HAVE_AUTH |
Simon Kelley | 4f7b304 | 2012-11-28 21:27:02 +0000 | [diff] [blame] | 937 | if (auth_dns) |
Simon Kelley | 4967876 | 2012-12-09 18:24:58 +0000 | [diff] [blame] | 938 | m = answer_auth(header, ((char *) header) + 65536, (size_t)size, now, &peer_addr); |
Simon Kelley | 4f7b304 | 2012-11-28 21:27:02 +0000 | [diff] [blame] | 939 | else |
Simon Kelley | 4820dce | 2012-12-18 18:30:30 +0000 | [diff] [blame] | 940 | #endif |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 941 | { |
Simon Kelley | 4f7b304 | 2012-11-28 21:27:02 +0000 | [diff] [blame] | 942 | /* m > 0 if answered from cache */ |
| 943 | m = answer_request(header, ((char *) header) + 65536, (size_t)size, |
| 944 | dst_addr_4, netmask, now); |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 945 | |
Simon Kelley | 4f7b304 | 2012-11-28 21:27:02 +0000 | [diff] [blame] | 946 | /* Do this by steam now we're not in the select() loop */ |
| 947 | check_log_writer(NULL); |
| 948 | |
| 949 | if (m == 0) |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 950 | { |
Simon Kelley | 4f7b304 | 2012-11-28 21:27:02 +0000 | [diff] [blame] | 951 | unsigned int flags = 0; |
| 952 | struct all_addr *addrp = NULL; |
| 953 | int type = 0; |
| 954 | char *domain = NULL; |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 955 | |
Simon Kelley | 4f7b304 | 2012-11-28 21:27:02 +0000 | [diff] [blame] | 956 | if (option_bool(OPT_ADD_MAC)) |
| 957 | size = add_mac(header, size, ((char *) header) + 65536, &peer_addr); |
| 958 | |
| 959 | if (gotname) |
| 960 | flags = search_servers(now, &addrp, gotname, daemon->namebuff, &type, &domain, &norebind); |
| 961 | |
| 962 | if (type != 0 || option_bool(OPT_ORDER) || !daemon->last_server) |
| 963 | last_server = daemon->servers; |
| 964 | else |
| 965 | last_server = daemon->last_server; |
| 966 | |
| 967 | if (!flags && last_server) |
| 968 | { |
| 969 | struct server *firstsendto = NULL; |
| 970 | unsigned int crc = questions_crc(header, (unsigned int)size, daemon->namebuff); |
| 971 | |
| 972 | /* Loop round available servers until we succeed in connecting to one. |
| 973 | Note that this code subtley ensures that consecutive queries on this connection |
| 974 | which can go to the same server, do so. */ |
| 975 | while (1) |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 976 | { |
Simon Kelley | 4f7b304 | 2012-11-28 21:27:02 +0000 | [diff] [blame] | 977 | if (!firstsendto) |
| 978 | firstsendto = last_server; |
| 979 | else |
| 980 | { |
| 981 | if (!(last_server = last_server->next)) |
| 982 | last_server = daemon->servers; |
| 983 | |
| 984 | if (last_server == firstsendto) |
| 985 | break; |
| 986 | } |
| 987 | |
| 988 | /* server for wrong domain */ |
| 989 | if (type != (last_server->flags & SERV_TYPE) || |
| 990 | (type == SERV_HAS_DOMAIN && !hostname_isequal(domain, last_server->domain))) |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 991 | continue; |
| 992 | |
Simon Kelley | 4f7b304 | 2012-11-28 21:27:02 +0000 | [diff] [blame] | 993 | if (last_server->tcpfd == -1) |
| 994 | { |
| 995 | if ((last_server->tcpfd = socket(last_server->addr.sa.sa_family, SOCK_STREAM, 0)) == -1) |
| 996 | continue; |
| 997 | |
| 998 | if ((!local_bind(last_server->tcpfd, &last_server->source_addr, last_server->interface, 1) || |
| 999 | connect(last_server->tcpfd, &last_server->addr.sa, sa_len(&last_server->addr)) == -1)) |
| 1000 | { |
| 1001 | close(last_server->tcpfd); |
| 1002 | last_server->tcpfd = -1; |
| 1003 | continue; |
| 1004 | } |
| 1005 | |
| 1006 | #ifdef HAVE_CONNTRACK |
| 1007 | /* Copy connection mark of incoming query to outgoing connection. */ |
| 1008 | if (option_bool(OPT_CONNTRACK)) |
| 1009 | { |
| 1010 | unsigned int mark; |
| 1011 | struct all_addr local; |
| 1012 | #ifdef HAVE_IPV6 |
| 1013 | if (local_addr->sa.sa_family == AF_INET6) |
| 1014 | local.addr.addr6 = local_addr->in6.sin6_addr; |
| 1015 | else |
| 1016 | #endif |
| 1017 | local.addr.addr4 = local_addr->in.sin_addr; |
| 1018 | |
| 1019 | if (get_incoming_mark(&peer_addr, &local, 1, &mark)) |
| 1020 | setsockopt(last_server->tcpfd, SOL_SOCKET, SO_MARK, &mark, sizeof(unsigned int)); |
| 1021 | } |
| 1022 | #endif |
| 1023 | } |
| 1024 | |
Simon Kelley | 4b5ea12 | 2013-04-22 10:18:26 +0100 | [diff] [blame] | 1025 | *length = htons(size); |
Simon Kelley | 4f7b304 | 2012-11-28 21:27:02 +0000 | [diff] [blame] | 1026 | |
Simon Kelley | 4b5ea12 | 2013-04-22 10:18:26 +0100 | [diff] [blame] | 1027 | if (!read_write(last_server->tcpfd, packet, size + sizeof(u16), 0) || |
Simon Kelley | 4f7b304 | 2012-11-28 21:27:02 +0000 | [diff] [blame] | 1028 | !read_write(last_server->tcpfd, &c1, 1, 1) || |
| 1029 | !read_write(last_server->tcpfd, &c2, 1, 1)) |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 1030 | { |
| 1031 | close(last_server->tcpfd); |
| 1032 | last_server->tcpfd = -1; |
| 1033 | continue; |
Simon Kelley | 4f7b304 | 2012-11-28 21:27:02 +0000 | [diff] [blame] | 1034 | } |
| 1035 | |
| 1036 | m = (c1 << 8) | c2; |
Simon Kelley | 4b5ea12 | 2013-04-22 10:18:26 +0100 | [diff] [blame] | 1037 | if (!read_write(last_server->tcpfd, payload, m, 1)) |
Simon Kelley | 4f7b304 | 2012-11-28 21:27:02 +0000 | [diff] [blame] | 1038 | return packet; |
| 1039 | |
| 1040 | if (!gotname) |
| 1041 | strcpy(daemon->namebuff, "query"); |
| 1042 | if (last_server->addr.sa.sa_family == AF_INET) |
| 1043 | log_query(F_SERVER | F_IPV4 | F_FORWARD, daemon->namebuff, |
| 1044 | (struct all_addr *)&last_server->addr.in.sin_addr, NULL); |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 1045 | #ifdef HAVE_IPV6 |
Simon Kelley | 4f7b304 | 2012-11-28 21:27:02 +0000 | [diff] [blame] | 1046 | else |
| 1047 | log_query(F_SERVER | F_IPV6 | F_FORWARD, daemon->namebuff, |
| 1048 | (struct all_addr *)&last_server->addr.in6.sin6_addr, NULL); |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 1049 | #endif |
Simon Kelley | 4f7b304 | 2012-11-28 21:27:02 +0000 | [diff] [blame] | 1050 | |
| 1051 | /* There's no point in updating the cache, since this process will exit and |
| 1052 | lose the information after a few queries. We make this call for the alias and |
| 1053 | bogus-nxdomain side-effects. */ |
| 1054 | /* If the crc of the question section doesn't match the crc we sent, then |
| 1055 | someone might be attempting to insert bogus values into the cache by |
| 1056 | sending replies containing questions and bogus answers. */ |
| 1057 | if (crc == questions_crc(header, (unsigned int)m, daemon->namebuff)) |
| 1058 | m = process_reply(header, now, last_server, (unsigned int)m, |
| 1059 | option_bool(OPT_NO_REBIND) && !norebind, checking_disabled); |
| 1060 | |
| 1061 | break; |
| 1062 | } |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 1063 | } |
Simon Kelley | 4f7b304 | 2012-11-28 21:27:02 +0000 | [diff] [blame] | 1064 | |
| 1065 | /* In case of local answer or no connections made. */ |
| 1066 | if (m == 0) |
| 1067 | m = setup_reply(header, (unsigned int)size, addrp, flags, daemon->local_ttl); |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 1068 | } |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 1069 | } |
Simon Kelley | 4f7b304 | 2012-11-28 21:27:02 +0000 | [diff] [blame] | 1070 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 1071 | check_log_writer(NULL); |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 1072 | |
Simon Kelley | 4b5ea12 | 2013-04-22 10:18:26 +0100 | [diff] [blame] | 1073 | *length = htons(m); |
| 1074 | |
| 1075 | if (m == 0 || !read_write(confd, packet, m + sizeof(u16), 0)) |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 1076 | return packet; |
| 1077 | } |
| 1078 | } |
| 1079 | |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 1080 | static struct frec *allocate_frec(time_t now) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1081 | { |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 1082 | struct frec *f; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1083 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 1084 | if ((f = (struct frec *)whine_malloc(sizeof(struct frec)))) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1085 | { |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1086 | f->next = daemon->frec_list; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1087 | f->time = now; |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 1088 | f->sentto = NULL; |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1089 | f->rfd4 = NULL; |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 1090 | f->flags = 0; |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1091 | #ifdef HAVE_IPV6 |
| 1092 | f->rfd6 = NULL; |
| 1093 | #endif |
| 1094 | daemon->frec_list = f; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1095 | } |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 1096 | |
| 1097 | return f; |
| 1098 | } |
| 1099 | |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1100 | static struct randfd *allocate_rfd(int family) |
| 1101 | { |
| 1102 | static int finger = 0; |
| 1103 | int i; |
| 1104 | |
| 1105 | /* limit the number of sockets we have open to avoid starvation of |
| 1106 | (eg) TFTP. Once we have a reasonable number, randomness should be OK */ |
| 1107 | |
| 1108 | for (i = 0; i < RANDOM_SOCKS; i++) |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1109 | if (daemon->randomsocks[i].refcount == 0) |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1110 | { |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1111 | if ((daemon->randomsocks[i].fd = random_sock(family)) == -1) |
| 1112 | break; |
| 1113 | |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1114 | daemon->randomsocks[i].refcount = 1; |
| 1115 | daemon->randomsocks[i].family = family; |
| 1116 | return &daemon->randomsocks[i]; |
| 1117 | } |
| 1118 | |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1119 | /* No free ones or cannot get new socket, grab an existing one */ |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1120 | for (i = 0; i < RANDOM_SOCKS; i++) |
| 1121 | { |
| 1122 | int j = (i+finger) % RANDOM_SOCKS; |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1123 | if (daemon->randomsocks[j].refcount != 0 && |
| 1124 | daemon->randomsocks[j].family == family && |
| 1125 | daemon->randomsocks[j].refcount != 0xffff) |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1126 | { |
| 1127 | finger = j; |
| 1128 | daemon->randomsocks[j].refcount++; |
| 1129 | return &daemon->randomsocks[j]; |
| 1130 | } |
| 1131 | } |
| 1132 | |
| 1133 | return NULL; /* doom */ |
| 1134 | } |
| 1135 | |
| 1136 | static void free_frec(struct frec *f) |
| 1137 | { |
| 1138 | if (f->rfd4 && --(f->rfd4->refcount) == 0) |
| 1139 | close(f->rfd4->fd); |
| 1140 | |
| 1141 | f->rfd4 = NULL; |
| 1142 | f->sentto = NULL; |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 1143 | f->flags = 0; |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1144 | |
| 1145 | #ifdef HAVE_IPV6 |
| 1146 | if (f->rfd6 && --(f->rfd6->refcount) == 0) |
| 1147 | close(f->rfd6->fd); |
| 1148 | |
| 1149 | f->rfd6 = NULL; |
| 1150 | #endif |
| 1151 | } |
| 1152 | |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 1153 | /* if wait==NULL return a free or older than TIMEOUT record. |
| 1154 | else return *wait zero if one available, or *wait is delay to |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1155 | when the oldest in-use record will expire. Impose an absolute |
| 1156 | limit of 4*TIMEOUT before we wipe things (for random sockets) */ |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 1157 | struct frec *get_new_frec(time_t now, int *wait) |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 1158 | { |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1159 | struct frec *f, *oldest, *target; |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 1160 | int count; |
| 1161 | |
| 1162 | if (wait) |
| 1163 | *wait = 0; |
| 1164 | |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1165 | 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] | 1166 | if (!f->sentto) |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1167 | target = f; |
| 1168 | else |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 1169 | { |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1170 | if (difftime(now, f->time) >= 4*TIMEOUT) |
| 1171 | { |
| 1172 | free_frec(f); |
| 1173 | target = f; |
| 1174 | } |
| 1175 | |
| 1176 | if (!oldest || difftime(f->time, oldest->time) <= 0) |
| 1177 | oldest = f; |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 1178 | } |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1179 | |
| 1180 | if (target) |
| 1181 | { |
| 1182 | target->time = now; |
| 1183 | return target; |
| 1184 | } |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 1185 | |
| 1186 | /* can't find empty one, use oldest if there is one |
| 1187 | and it's older than timeout */ |
| 1188 | if (oldest && ((int)difftime(now, oldest->time)) >= TIMEOUT) |
| 1189 | { |
| 1190 | /* keep stuff for twice timeout if we can by allocating a new |
| 1191 | record instead */ |
| 1192 | if (difftime(now, oldest->time) < 2*TIMEOUT && |
| 1193 | count <= daemon->ftabsize && |
| 1194 | (f = allocate_frec(now))) |
| 1195 | return f; |
| 1196 | |
| 1197 | if (!wait) |
| 1198 | { |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1199 | free_frec(oldest); |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 1200 | oldest->time = now; |
| 1201 | } |
| 1202 | return oldest; |
| 1203 | } |
| 1204 | |
| 1205 | /* none available, calculate time 'till oldest record expires */ |
| 1206 | if (count > daemon->ftabsize) |
| 1207 | { |
| 1208 | if (oldest && wait) |
| 1209 | *wait = oldest->time + (time_t)TIMEOUT - now; |
| 1210 | return NULL; |
| 1211 | } |
| 1212 | |
| 1213 | if (!(f = allocate_frec(now)) && wait) |
| 1214 | /* wait one second on malloc failure */ |
| 1215 | *wait = 1; |
| 1216 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1217 | return f; /* OK if malloc fails and this is NULL */ |
| 1218 | } |
| 1219 | |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 1220 | /* crc is all-ones if not known. */ |
| 1221 | static struct frec *lookup_frec(unsigned short id, unsigned int crc) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1222 | { |
| 1223 | struct frec *f; |
| 1224 | |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1225 | for(f = daemon->frec_list; f; f = f->next) |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 1226 | if (f->sentto && f->new_id == id && |
| 1227 | (f->crc == crc || crc == 0xffffffff)) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1228 | return f; |
| 1229 | |
| 1230 | return NULL; |
| 1231 | } |
| 1232 | |
| 1233 | static struct frec *lookup_frec_by_sender(unsigned short id, |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 1234 | union mysockaddr *addr, |
| 1235 | unsigned int crc) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1236 | { |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 1237 | struct frec *f; |
| 1238 | |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1239 | for(f = daemon->frec_list; f; f = f->next) |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 1240 | if (f->sentto && |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1241 | f->orig_id == id && |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 1242 | f->crc == crc && |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1243 | sockaddr_isequal(&f->source, addr)) |
| 1244 | return f; |
| 1245 | |
| 1246 | return NULL; |
| 1247 | } |
| 1248 | |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 1249 | /* A server record is going away, remove references to it */ |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 1250 | void server_gone(struct server *server) |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 1251 | { |
| 1252 | struct frec *f; |
| 1253 | |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1254 | for (f = daemon->frec_list; f; f = f->next) |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 1255 | if (f->sentto && f->sentto == server) |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1256 | free_frec(f); |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 1257 | |
| 1258 | if (daemon->last_server == server) |
| 1259 | daemon->last_server = NULL; |
| 1260 | |
| 1261 | if (daemon->srv_save == server) |
| 1262 | daemon->srv_save = NULL; |
| 1263 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1264 | |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 1265 | /* return unique random ids. */ |
| 1266 | static unsigned short get_id(unsigned int crc) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1267 | { |
| 1268 | unsigned short ret = 0; |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 1269 | |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 1270 | do |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 1271 | ret = rand16(); |
| 1272 | while (lookup_frec(ret, crc)); |
| 1273 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1274 | return ret; |
| 1275 | } |
| 1276 | |
| 1277 | |
| 1278 | |
| 1279 | |
| 1280 | |