Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame^] | 1 | /* dnsmasq is Copyright (c) 2000-2007 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 | |
| 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 | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 19 | static struct frec *frec_list = NULL; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 20 | |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 21 | static struct frec *lookup_frec(unsigned short id, unsigned int crc); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 22 | static struct frec *lookup_frec_by_sender(unsigned short id, |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 23 | union mysockaddr *addr, |
| 24 | unsigned int crc); |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 25 | static unsigned short get_id(int force, unsigned short force_id, unsigned int crc); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 26 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 27 | |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame^] | 28 | /* Send a UDP packet with its source address set as "source" |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 29 | unless nowild is true, when we just send it with the kernel default */ |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 30 | static void send_from(int fd, int nowild, char *packet, size_t len, |
Simon Kelley | dfa666f | 2004-08-02 18:27:27 +0100 | [diff] [blame] | 31 | union mysockaddr *to, struct all_addr *source, |
| 32 | unsigned int iface) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 33 | { |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 34 | struct msghdr msg; |
| 35 | struct iovec iov[1]; |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 36 | union { |
| 37 | struct cmsghdr align; /* this ensures alignment */ |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 38 | #if defined(HAVE_LINUX_NETWORK) |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 39 | char control[CMSG_SPACE(sizeof(struct in_pktinfo))]; |
| 40 | #elif defined(IP_SENDSRCADDR) |
| 41 | char control[CMSG_SPACE(sizeof(struct in_addr))]; |
| 42 | #endif |
| 43 | #ifdef HAVE_IPV6 |
| 44 | char control6[CMSG_SPACE(sizeof(struct in6_pktinfo))]; |
| 45 | #endif |
| 46 | } control_u; |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 47 | |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 48 | iov[0].iov_base = packet; |
| 49 | iov[0].iov_len = len; |
| 50 | |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 51 | msg.msg_control = NULL; |
| 52 | msg.msg_controllen = 0; |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 53 | msg.msg_flags = 0; |
| 54 | msg.msg_name = to; |
| 55 | msg.msg_namelen = sa_len(to); |
| 56 | msg.msg_iov = iov; |
| 57 | msg.msg_iovlen = 1; |
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 | if (!nowild) |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 60 | { |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 61 | struct cmsghdr *cmptr; |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 62 | msg.msg_control = &control_u; |
| 63 | msg.msg_controllen = sizeof(control_u); |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 64 | cmptr = CMSG_FIRSTHDR(&msg); |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 65 | |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 66 | if (to->sa.sa_family == AF_INET) |
| 67 | { |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 68 | #if defined(HAVE_LINUX_NETWORK) |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 69 | struct in_pktinfo *pkt = (struct in_pktinfo *)CMSG_DATA(cmptr); |
| 70 | pkt->ipi_ifindex = 0; |
| 71 | pkt->ipi_spec_dst = source->addr.addr4; |
| 72 | msg.msg_controllen = cmptr->cmsg_len = CMSG_LEN(sizeof(struct in_pktinfo)); |
| 73 | cmptr->cmsg_level = SOL_IP; |
| 74 | cmptr->cmsg_type = IP_PKTINFO; |
| 75 | #elif defined(IP_SENDSRCADDR) |
| 76 | struct in_addr *a = (struct in_addr *)CMSG_DATA(cmptr); |
| 77 | *a = source->addr.addr4; |
| 78 | msg.msg_controllen = cmptr->cmsg_len = CMSG_LEN(sizeof(struct in_addr)); |
| 79 | cmptr->cmsg_level = IPPROTO_IP; |
| 80 | cmptr->cmsg_type = IP_SENDSRCADDR; |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 81 | #endif |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 82 | } |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 83 | else |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 84 | #ifdef HAVE_IPV6 |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 85 | { |
| 86 | struct in6_pktinfo *pkt = (struct in6_pktinfo *)CMSG_DATA(cmptr); |
| 87 | pkt->ipi6_ifindex = iface; /* Need iface for IPv6 to handle link-local addrs */ |
| 88 | pkt->ipi6_addr = source->addr.addr6; |
| 89 | msg.msg_controllen = cmptr->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo)); |
| 90 | cmptr->cmsg_type = IPV6_PKTINFO; |
| 91 | cmptr->cmsg_level = IPV6_LEVEL; |
| 92 | } |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 93 | #else |
| 94 | iface = 0; /* 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 | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 98 | retry: |
| 99 | if (sendmsg(fd, &msg, 0) == -1) |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 100 | { |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 101 | /* certain Linux kernels seem to object to setting the source address in the IPv6 stack |
| 102 | by returning EINVAL from sendmsg. In that case, try again without setting the |
| 103 | source address, since it will nearly alway be correct anyway. IPv6 stinks. */ |
| 104 | if (errno == EINVAL && msg.msg_controllen) |
| 105 | { |
| 106 | msg.msg_controllen = 0; |
| 107 | goto retry; |
| 108 | } |
| 109 | if (retry_send()) |
| 110 | goto retry; |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 111 | } |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 114 | static unsigned short search_servers(time_t now, struct all_addr **addrpp, |
Simon Kelley | 36717ee | 2004-09-20 19:20:58 +0100 | [diff] [blame] | 115 | unsigned short qtype, char *qdomain, int *type, char **domain) |
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; |
| 125 | unsigned short flags = 0; |
| 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 | { |
| 131 | unsigned short sflag = serv->addr.sa.sa_family == AF_INET ? F_IPV4 : F_IPV6; |
| 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) && |
| 157 | domainlen >= matchlen && |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 158 | (domainlen == 0 || namelen == domainlen || *(serv->domain) == '.' || *(matchstart-1) == '.' )) |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 159 | { |
| 160 | unsigned short sflag = serv->addr.sa.sa_family == AF_INET ? F_IPV4 : F_IPV6; |
| 161 | *type = SERV_HAS_DOMAIN; |
| 162 | *domain = serv->domain; |
| 163 | matchlen = domainlen; |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 164 | if (serv->flags & SERV_NO_ADDR) |
Simon Kelley | 36717ee | 2004-09-20 19:20:58 +0100 | [diff] [blame] | 165 | flags = F_NXDOMAIN; |
| 166 | else if (serv->flags & SERV_LITERAL_ADDRESS) |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 167 | { |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame^] | 168 | if (sflag & qtype) |
Simon Kelley | 36717ee | 2004-09-20 19:20:58 +0100 | [diff] [blame] | 169 | { |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame^] | 170 | flags = sflag; |
Simon Kelley | 36717ee | 2004-09-20 19:20:58 +0100 | [diff] [blame] | 171 | if (serv->addr.sa.sa_family == AF_INET) |
| 172 | *addrpp = (struct all_addr *)&serv->addr.in.sin_addr; |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 173 | #ifdef HAVE_IPV6 |
Simon Kelley | 36717ee | 2004-09-20 19:20:58 +0100 | [diff] [blame] | 174 | else |
| 175 | *addrpp = (struct all_addr *)&serv->addr.in6.sin6_addr; |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 176 | #endif |
Simon Kelley | 36717ee | 2004-09-20 19:20:58 +0100 | [diff] [blame] | 177 | } |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame^] | 178 | else if (!flags || (flags & F_NXDOMAIN)) |
Simon Kelley | 36717ee | 2004-09-20 19:20:58 +0100 | [diff] [blame] | 179 | flags = F_NOERR; |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | } |
| 183 | |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame^] | 184 | if (flags == 0 && !(qtype & F_BIGNAME) && |
| 185 | (daemon->options & OPT_NODOTS_LOCAL) && !strchr(qdomain, '.') && namelen != 0) |
| 186 | /* don't forward simple names, make exception for NS queries and empty name. */ |
Simon Kelley | 36717ee | 2004-09-20 19:20:58 +0100 | [diff] [blame] | 187 | flags = F_NXDOMAIN; |
| 188 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 189 | if (flags == F_NXDOMAIN && check_for_local_domain(qdomain, now)) |
Simon Kelley | c1bb850 | 2004-08-11 18:40:17 +0100 | [diff] [blame] | 190 | flags = F_NOERR; |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 191 | |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame^] | 192 | if (flags) |
| 193 | { |
| 194 | int logflags = 0; |
| 195 | |
| 196 | if (flags == F_NXDOMAIN || flags == F_NOERR) |
| 197 | logflags = F_NEG | qtype; |
| 198 | |
| 199 | log_query(logflags | flags | F_CONFIG | F_FORWARD, qdomain, *addrpp, 0, NULL, 0); |
| 200 | } |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 201 | |
| 202 | return flags; |
| 203 | } |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 204 | |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame^] | 205 | static int forward_query(int udpfd, union mysockaddr *udpaddr, |
| 206 | struct all_addr *dst_addr, unsigned int dst_iface, |
| 207 | HEADER *header, size_t plen, time_t now, struct frec *forward) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 208 | { |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 209 | char *domain = NULL; |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 210 | int type = 0; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 211 | struct all_addr *addrp = NULL; |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 212 | unsigned int crc = questions_crc(header, plen, daemon->namebuff); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 213 | unsigned short flags = 0; |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 214 | unsigned short gotname = extract_request(header, plen, daemon->namebuff, NULL); |
Simon Kelley | de37951 | 2004-06-22 20:23:33 +0100 | [diff] [blame] | 215 | struct server *start = NULL; |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 216 | |
| 217 | /* may be no servers available. */ |
| 218 | if (!daemon->servers) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 219 | forward = NULL; |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 220 | 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] | 221 | { |
Simon Kelley | de37951 | 2004-06-22 20:23:33 +0100 | [diff] [blame] | 222 | /* retry on existing query, send to all available servers */ |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 223 | domain = forward->sentto->domain; |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame^] | 224 | forward->sentto->failed_queries++; |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 225 | if (!(daemon->options & OPT_ORDER)) |
Simon Kelley | de37951 | 2004-06-22 20:23:33 +0100 | [diff] [blame] | 226 | { |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 227 | forward->forwardall = 1; |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 228 | daemon->last_server = NULL; |
Simon Kelley | de37951 | 2004-06-22 20:23:33 +0100 | [diff] [blame] | 229 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 230 | type = forward->sentto->flags & SERV_TYPE; |
Simon Kelley | de37951 | 2004-06-22 20:23:33 +0100 | [diff] [blame] | 231 | if (!(start = forward->sentto->next)) |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 232 | start = daemon->servers; /* at end of list, recycle */ |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 233 | header->id = htons(forward->new_id); |
| 234 | } |
| 235 | else |
| 236 | { |
| 237 | if (gotname) |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 238 | flags = search_servers(now, &addrp, gotname, daemon->namebuff, &type, &domain); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 239 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 240 | if (!flags && !(forward = get_new_frec(now, NULL))) |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 241 | /* table full - server failure. */ |
| 242 | flags = F_NEG; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 243 | |
| 244 | if (forward) |
| 245 | { |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 246 | /* force unchanging id for signed packets */ |
| 247 | int is_sign; |
| 248 | find_pseudoheader(header, plen, NULL, NULL, &is_sign); |
| 249 | |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 250 | forward->source = *udpaddr; |
| 251 | forward->dest = *dst_addr; |
| 252 | forward->iface = dst_iface; |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 253 | forward->orig_id = ntohs(header->id); |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 254 | forward->new_id = get_id(is_sign, forward->orig_id, crc); |
| 255 | forward->fd = udpfd; |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 256 | forward->crc = crc; |
| 257 | forward->forwardall = 0; |
| 258 | header->id = htons(forward->new_id); |
| 259 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 260 | /* In strict_order mode, or when using domain specific servers |
| 261 | always try servers in the order specified in resolv.conf, |
| 262 | otherwise, use the one last known to work. */ |
| 263 | |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 264 | if (type != 0 || (daemon->options & OPT_ORDER)) |
| 265 | start = daemon->servers; |
| 266 | else if (!(start = daemon->last_server)) |
Simon Kelley | de37951 | 2004-06-22 20:23:33 +0100 | [diff] [blame] | 267 | { |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 268 | start = daemon->servers; |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 269 | forward->forwardall = 1; |
Simon Kelley | de37951 | 2004-06-22 20:23:33 +0100 | [diff] [blame] | 270 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 271 | } |
| 272 | } |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 273 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 274 | /* check for send errors here (no route to host) |
| 275 | if we fail to send to all nameservers, send back an error |
| 276 | packet straight away (helps modem users when offline) */ |
| 277 | |
| 278 | if (!flags && forward) |
| 279 | { |
Simon Kelley | de37951 | 2004-06-22 20:23:33 +0100 | [diff] [blame] | 280 | struct server *firstsentto = start; |
| 281 | int forwarded = 0; |
| 282 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 283 | while (1) |
| 284 | { |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 285 | /* only send to servers dealing with our domain. |
| 286 | domain may be NULL, in which case server->domain |
| 287 | must be NULL also. */ |
| 288 | |
Simon Kelley | de37951 | 2004-06-22 20:23:33 +0100 | [diff] [blame] | 289 | if (type == (start->flags & SERV_TYPE) && |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 290 | (type != SERV_HAS_DOMAIN || hostname_isequal(domain, start->domain)) && |
| 291 | !(start->flags & SERV_LITERAL_ADDRESS)) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 292 | { |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 293 | if (sendto(start->sfd->fd, (char *)header, plen, 0, |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 294 | &start->addr.sa, |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 295 | sa_len(&start->addr)) == -1) |
| 296 | { |
| 297 | if (retry_send()) |
| 298 | continue; |
| 299 | } |
| 300 | else |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 301 | { |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 302 | /* Keep info in case we want to re-send this packet */ |
| 303 | daemon->srv_save = start; |
| 304 | daemon->packet_len = plen; |
| 305 | |
Simon Kelley | de37951 | 2004-06-22 20:23:33 +0100 | [diff] [blame] | 306 | if (!gotname) |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 307 | strcpy(daemon->namebuff, "query"); |
Simon Kelley | de37951 | 2004-06-22 20:23:33 +0100 | [diff] [blame] | 308 | if (start->addr.sa.sa_family == AF_INET) |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 309 | log_query(F_SERVER | F_IPV4 | F_FORWARD, daemon->namebuff, |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 310 | (struct all_addr *)&start->addr.in.sin_addr, 0, |
| 311 | NULL, 0); |
Simon Kelley | de37951 | 2004-06-22 20:23:33 +0100 | [diff] [blame] | 312 | #ifdef HAVE_IPV6 |
| 313 | else |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 314 | log_query(F_SERVER | F_IPV6 | F_FORWARD, daemon->namebuff, |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 315 | (struct all_addr *)&start->addr.in6.sin6_addr, 0, |
| 316 | NULL, 0); |
Simon Kelley | de37951 | 2004-06-22 20:23:33 +0100 | [diff] [blame] | 317 | #endif |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame^] | 318 | start->queries++; |
Simon Kelley | de37951 | 2004-06-22 20:23:33 +0100 | [diff] [blame] | 319 | forwarded = 1; |
| 320 | forward->sentto = start; |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 321 | if (!forward->forwardall) |
Simon Kelley | de37951 | 2004-06-22 20:23:33 +0100 | [diff] [blame] | 322 | break; |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 323 | forward->forwardall++; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 324 | } |
| 325 | } |
| 326 | |
Simon Kelley | de37951 | 2004-06-22 20:23:33 +0100 | [diff] [blame] | 327 | if (!(start = start->next)) |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 328 | start = daemon->servers; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 329 | |
Simon Kelley | de37951 | 2004-06-22 20:23:33 +0100 | [diff] [blame] | 330 | if (start == firstsentto) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 331 | break; |
| 332 | } |
| 333 | |
Simon Kelley | de37951 | 2004-06-22 20:23:33 +0100 | [diff] [blame] | 334 | if (forwarded) |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame^] | 335 | return 1; |
Simon Kelley | de37951 | 2004-06-22 20:23:33 +0100 | [diff] [blame] | 336 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 337 | /* could not send on, prepare to return */ |
| 338 | header->id = htons(forward->orig_id); |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 339 | forward->sentto = NULL; /* cancel */ |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | /* 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] | 343 | if (udpfd != -1) |
| 344 | { |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 345 | plen = setup_reply(header, plen, addrp, flags, daemon->local_ttl); |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 346 | send_from(udpfd, daemon->options & OPT_NOWILD, (char *)header, plen, udpaddr, dst_addr, dst_iface); |
| 347 | } |
| 348 | |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame^] | 349 | return 0; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 350 | } |
| 351 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 352 | static size_t process_reply(HEADER *header, time_t now, |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 353 | struct server *server, size_t n) |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 354 | { |
Simon Kelley | 36717ee | 2004-09-20 19:20:58 +0100 | [diff] [blame] | 355 | unsigned char *pheader, *sizep; |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 356 | int munged = 0, is_sign; |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 357 | size_t plen; |
| 358 | |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 359 | /* If upstream is advertising a larger UDP packet size |
| 360 | than we allow, trim it so that we don't get overlarge |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 361 | requests for the client. We can't do this for signed packets. */ |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 362 | |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 363 | if ((pheader = find_pseudoheader(header, n, &plen, &sizep, &is_sign)) && !is_sign) |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 364 | { |
| 365 | unsigned short udpsz; |
Simon Kelley | 36717ee | 2004-09-20 19:20:58 +0100 | [diff] [blame] | 366 | unsigned char *psave = sizep; |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 367 | |
Simon Kelley | 36717ee | 2004-09-20 19:20:58 +0100 | [diff] [blame] | 368 | GETSHORT(udpsz, sizep); |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 369 | if (udpsz > daemon->edns_pktsz) |
| 370 | PUTSHORT(daemon->edns_pktsz, psave); |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 371 | } |
| 372 | |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 373 | if (header->opcode != QUERY || (header->rcode != NOERROR && header->rcode != NXDOMAIN)) |
Simon Kelley | 36717ee | 2004-09-20 19:20:58 +0100 | [diff] [blame] | 374 | return n; |
| 375 | |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 376 | /* Complain loudly if the upstream server is non-recursive. */ |
| 377 | if (!header->ra && header->rcode == NOERROR && ntohs(header->ancount) == 0 && |
| 378 | server && !(server->flags & SERV_WARNED_RECURSIVE)) |
| 379 | { |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 380 | prettyprint_addr(&server->addr, daemon->namebuff); |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 381 | my_syslog(LOG_WARNING, _("nameserver %s refused to do a recursive query"), daemon->namebuff); |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 382 | if (!(daemon->options & OPT_LOG)) |
| 383 | server->flags |= SERV_WARNED_RECURSIVE; |
| 384 | } |
| 385 | |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 386 | if (daemon->bogus_addr && header->rcode != NXDOMAIN && |
| 387 | check_for_bogus_wildcard(header, n, daemon->namebuff, daemon->bogus_addr, now)) |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 388 | { |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 389 | munged = 1; |
| 390 | header->rcode = NXDOMAIN; |
| 391 | header->aa = 0; |
Simon Kelley | 36717ee | 2004-09-20 19:20:58 +0100 | [diff] [blame] | 392 | } |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 393 | else |
Simon Kelley | 36717ee | 2004-09-20 19:20:58 +0100 | [diff] [blame] | 394 | { |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 395 | if (header->rcode == NXDOMAIN && |
| 396 | extract_request(header, n, daemon->namebuff, NULL) && |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 397 | check_for_local_domain(daemon->namebuff, now)) |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 398 | { |
Simon Kelley | 36717ee | 2004-09-20 19:20:58 +0100 | [diff] [blame] | 399 | /* if we forwarded a query for a locally known name (because it was for |
| 400 | an unknown type) and the answer is NXDOMAIN, convert that to NODATA, |
| 401 | since we know that the domain exists, even if upstream doesn't */ |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 402 | munged = 1; |
| 403 | header->aa = 1; |
| 404 | header->rcode = NOERROR; |
Simon Kelley | 36717ee | 2004-09-20 19:20:58 +0100 | [diff] [blame] | 405 | } |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 406 | |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame^] | 407 | if (extract_addresses(header, n, daemon->namebuff, now)) |
| 408 | { |
| 409 | my_syslog(LOG_WARNING, _("possible DNS-rebind attack detected")); |
| 410 | munged = 1; |
| 411 | } |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 412 | } |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 413 | |
| 414 | /* do this after extract_addresses. Ensure NODATA reply and remove |
| 415 | nameserver info. */ |
| 416 | |
| 417 | if (munged) |
| 418 | { |
| 419 | header->ancount = htons(0); |
| 420 | header->nscount = htons(0); |
| 421 | header->arcount = htons(0); |
| 422 | } |
| 423 | |
Simon Kelley | 36717ee | 2004-09-20 19:20:58 +0100 | [diff] [blame] | 424 | /* the bogus-nxdomain stuff, doctor and NXDOMAIN->NODATA munging can all elide |
| 425 | sections of the packet. Find the new length here and put back pseudoheader |
| 426 | if it was removed. */ |
| 427 | return resize_packet(header, n, pheader, plen); |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 428 | } |
| 429 | |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 430 | /* sets new last_server */ |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 431 | void reply_query(struct serverfd *sfd, time_t now) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 432 | { |
| 433 | /* packet from peer server, extract data for cache, and send to |
| 434 | original requester */ |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 435 | HEADER *header; |
Simon Kelley | de37951 | 2004-06-22 20:23:33 +0100 | [diff] [blame] | 436 | union mysockaddr serveraddr; |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 437 | struct frec *forward; |
Simon Kelley | de37951 | 2004-06-22 20:23:33 +0100 | [diff] [blame] | 438 | socklen_t addrlen = sizeof(serveraddr); |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 439 | ssize_t n = recvfrom(sfd->fd, daemon->packet, daemon->edns_pktsz, 0, &serveraddr.sa, &addrlen); |
| 440 | size_t nn; |
| 441 | |
| 442 | /* packet buffer overwritten */ |
| 443 | daemon->srv_save = NULL; |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 444 | |
Simon Kelley | de37951 | 2004-06-22 20:23:33 +0100 | [diff] [blame] | 445 | /* Determine the address of the server replying so that we can mark that as good */ |
| 446 | serveraddr.sa.sa_family = sfd->source_addr.sa.sa_family; |
| 447 | #ifdef HAVE_IPV6 |
| 448 | if (serveraddr.sa.sa_family == AF_INET6) |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 449 | serveraddr.in6.sin6_flowinfo = 0; |
Simon Kelley | de37951 | 2004-06-22 20:23:33 +0100 | [diff] [blame] | 450 | #endif |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 451 | |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 452 | header = (HEADER *)daemon->packet; |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 453 | |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 454 | if (n >= (int)sizeof(HEADER) && header->qr && |
| 455 | (forward = lookup_frec(ntohs(header->id), questions_crc(header, n, daemon->namebuff)))) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 456 | { |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 457 | struct server *server = forward->sentto; |
| 458 | |
Simon Kelley | 6b01084 | 2007-02-12 20:32:07 +0000 | [diff] [blame] | 459 | if ((header->rcode == SERVFAIL || header->rcode == REFUSED) && |
| 460 | !(daemon->options & OPT_ORDER) && |
| 461 | forward->forwardall == 0) |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 462 | /* for broken servers, attempt to send to another one. */ |
| 463 | { |
| 464 | unsigned char *pheader; |
| 465 | size_t plen; |
| 466 | int is_sign; |
| 467 | |
| 468 | /* recreate query from reply */ |
| 469 | pheader = find_pseudoheader(header, (size_t)n, &plen, NULL, &is_sign); |
| 470 | if (!is_sign) |
| 471 | { |
| 472 | header->ancount = htons(0); |
| 473 | header->nscount = htons(0); |
| 474 | header->arcount = htons(0); |
| 475 | if ((nn = resize_packet(header, (size_t)n, pheader, plen))) |
| 476 | { |
| 477 | header->qr = 0; |
| 478 | header->tc = 0; |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 479 | forward_query(-1, NULL, NULL, 0, header, nn, now, forward); |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 480 | return; |
| 481 | } |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | if ((forward->sentto->flags & SERV_TYPE) == 0) |
| 486 | { |
| 487 | if (header->rcode == SERVFAIL || header->rcode == REFUSED) |
| 488 | server = NULL; |
| 489 | else |
| 490 | { |
| 491 | struct server *last_server; |
| 492 | /* find good server by address if possible, otherwise assume the last one we sent to */ |
| 493 | for (last_server = daemon->servers; last_server; last_server = last_server->next) |
| 494 | if (!(last_server->flags & (SERV_LITERAL_ADDRESS | SERV_HAS_DOMAIN | SERV_FOR_NODOTS | SERV_NO_ADDR)) && |
| 495 | sockaddr_isequal(&last_server->addr, &serveraddr)) |
| 496 | { |
| 497 | server = last_server; |
| 498 | break; |
| 499 | } |
| 500 | } |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame^] | 501 | if (!(daemon->options & OPT_ALL_SERVERS)) |
| 502 | daemon->last_server = server; |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 503 | } |
| 504 | |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 505 | /* If the answer is an error, keep the forward record in place in case |
| 506 | we get a good reply from another server. Kill it when we've |
| 507 | had replies from all to avoid filling the forwarding table when |
| 508 | everything is broken */ |
| 509 | if (forward->forwardall == 0 || --forward->forwardall == 1 || |
| 510 | (header->rcode != REFUSED && header->rcode != SERVFAIL)) |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 511 | { |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 512 | if ((nn = process_reply(header, now, server, (size_t)n))) |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 513 | { |
| 514 | header->id = htons(forward->orig_id); |
| 515 | header->ra = 1; /* recursion if available */ |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 516 | send_from(forward->fd, daemon->options & OPT_NOWILD, daemon->packet, nn, |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 517 | &forward->source, &forward->dest, forward->iface); |
| 518 | } |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 519 | forward->sentto = NULL; /* cancel */ |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 520 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 521 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 522 | } |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 523 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 524 | void receive_query(struct listener *listen, time_t now) |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 525 | { |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 526 | HEADER *header = (HEADER *)daemon->packet; |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 527 | union mysockaddr source_addr; |
Simon Kelley | c1bb850 | 2004-08-11 18:40:17 +0100 | [diff] [blame] | 528 | unsigned short type; |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 529 | struct all_addr dst_addr; |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 530 | struct in_addr netmask, dst_addr_4; |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 531 | size_t m; |
| 532 | ssize_t n; |
| 533 | int if_index = 0; |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 534 | struct iovec iov[1]; |
| 535 | struct msghdr msg; |
| 536 | struct cmsghdr *cmptr; |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 537 | union { |
| 538 | struct cmsghdr align; /* this ensures alignment */ |
| 539 | #ifdef HAVE_IPV6 |
| 540 | char control6[CMSG_SPACE(sizeof(struct in6_pktinfo))]; |
| 541 | #endif |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 542 | #if defined(HAVE_LINUX_NETWORK) |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 543 | char control[CMSG_SPACE(sizeof(struct in_pktinfo))]; |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame^] | 544 | #elif defined(IP_RECVDSTADDR) && defined(HAVE_SOLARIS_NETWORK) |
| 545 | char control[CMSG_SPACE(sizeof(struct in_addr)) + |
| 546 | CMSG_SPACE(sizeof(unsigned int))]; |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 547 | #elif defined(IP_RECVDSTADDR) |
| 548 | char control[CMSG_SPACE(sizeof(struct in_addr)) + |
| 549 | CMSG_SPACE(sizeof(struct sockaddr_dl))]; |
| 550 | #endif |
| 551 | } control_u; |
| 552 | |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 553 | /* packet buffer overwritten */ |
| 554 | daemon->srv_save = NULL; |
| 555 | |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 556 | if (listen->family == AF_INET && (daemon->options & OPT_NOWILD)) |
| 557 | { |
| 558 | dst_addr_4 = listen->iface->addr.in.sin_addr; |
| 559 | netmask = listen->iface->netmask; |
| 560 | } |
| 561 | else |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 562 | { |
| 563 | dst_addr_4.s_addr = 0; |
| 564 | netmask.s_addr = 0; |
| 565 | } |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 566 | |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 567 | iov[0].iov_base = daemon->packet; |
| 568 | iov[0].iov_len = daemon->edns_pktsz; |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 569 | |
| 570 | msg.msg_control = control_u.control; |
| 571 | msg.msg_controllen = sizeof(control_u); |
| 572 | msg.msg_flags = 0; |
| 573 | msg.msg_name = &source_addr; |
| 574 | msg.msg_namelen = sizeof(source_addr); |
| 575 | msg.msg_iov = iov; |
| 576 | msg.msg_iovlen = 1; |
| 577 | |
Simon Kelley | de37951 | 2004-06-22 20:23:33 +0100 | [diff] [blame] | 578 | if ((n = recvmsg(listen->fd, &msg, 0)) == -1) |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 579 | return; |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 580 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 581 | if (n < (int)sizeof(HEADER) || |
| 582 | (msg.msg_flags & MSG_TRUNC) || |
| 583 | header->qr) |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 584 | return; |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 585 | |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 586 | source_addr.sa.sa_family = listen->family; |
| 587 | #ifdef HAVE_IPV6 |
| 588 | if (listen->family == AF_INET6) |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 589 | source_addr.in6.sin6_flowinfo = 0; |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 590 | #endif |
| 591 | |
| 592 | if (!(daemon->options & OPT_NOWILD)) |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 593 | { |
Simon Kelley | 8a911cc | 2004-03-16 18:35:52 +0000 | [diff] [blame] | 594 | struct ifreq ifr; |
| 595 | |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 596 | if (msg.msg_controllen < sizeof(struct cmsghdr)) |
| 597 | return; |
| 598 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 599 | #if defined(HAVE_LINUX_NETWORK) |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 600 | if (listen->family == AF_INET) |
| 601 | for (cmptr = CMSG_FIRSTHDR(&msg); cmptr; cmptr = CMSG_NXTHDR(&msg, cmptr)) |
| 602 | if (cmptr->cmsg_level == SOL_IP && cmptr->cmsg_type == IP_PKTINFO) |
| 603 | { |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 604 | dst_addr_4 = dst_addr.addr.addr4 = ((struct in_pktinfo *)CMSG_DATA(cmptr))->ipi_spec_dst; |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 605 | if_index = ((struct in_pktinfo *)CMSG_DATA(cmptr))->ipi_ifindex; |
| 606 | } |
| 607 | #elif defined(IP_RECVDSTADDR) && defined(IP_RECVIF) |
| 608 | if (listen->family == AF_INET) |
| 609 | { |
| 610 | for (cmptr = CMSG_FIRSTHDR(&msg); cmptr; cmptr = CMSG_NXTHDR(&msg, cmptr)) |
| 611 | if (cmptr->cmsg_level == IPPROTO_IP && cmptr->cmsg_type == IP_RECVDSTADDR) |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 612 | dst_addr_4 = dst_addr.addr.addr4 = *((struct in_addr *)CMSG_DATA(cmptr)); |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 613 | else if (cmptr->cmsg_level == IPPROTO_IP && cmptr->cmsg_type == IP_RECVIF) |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame^] | 614 | #ifdef HAVE_SOLARIS_NETWORK |
| 615 | if_index = *((unsigned int *)CMSG_DATA(cmptr)); |
| 616 | #else |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 617 | if_index = ((struct sockaddr_dl *)CMSG_DATA(cmptr))->sdl_index; |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame^] | 618 | #endif |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 619 | } |
| 620 | #endif |
| 621 | |
| 622 | #ifdef HAVE_IPV6 |
| 623 | if (listen->family == AF_INET6) |
| 624 | { |
| 625 | for (cmptr = CMSG_FIRSTHDR(&msg); cmptr; cmptr = CMSG_NXTHDR(&msg, cmptr)) |
| 626 | if (cmptr->cmsg_level == IPV6_LEVEL && cmptr->cmsg_type == IPV6_PKTINFO) |
| 627 | { |
| 628 | dst_addr.addr.addr6 = ((struct in6_pktinfo *)CMSG_DATA(cmptr))->ipi6_addr; |
| 629 | if_index =((struct in6_pktinfo *)CMSG_DATA(cmptr))->ipi6_ifindex; |
| 630 | } |
| 631 | } |
| 632 | #endif |
| 633 | |
| 634 | /* enforce available interface configuration */ |
| 635 | |
Simon Kelley | 8a911cc | 2004-03-16 18:35:52 +0000 | [diff] [blame] | 636 | if (if_index == 0) |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 637 | return; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 638 | |
Simon Kelley | 8a911cc | 2004-03-16 18:35:52 +0000 | [diff] [blame] | 639 | #ifdef SIOCGIFNAME |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 640 | ifr.ifr_ifindex = if_index; |
| 641 | if (ioctl(listen->fd, SIOCGIFNAME, &ifr) == -1) |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 642 | return; |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 643 | #else |
| 644 | if (!if_indextoname(if_index, ifr.ifr_name)) |
| 645 | return; |
| 646 | #endif |
| 647 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 648 | if (!iface_check(listen->family, &dst_addr, &ifr, &if_index)) |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 649 | return; |
| 650 | |
| 651 | if (listen->family == AF_INET && |
| 652 | (daemon->options & OPT_LOCALISE) && |
| 653 | ioctl(listen->fd, SIOCGIFNETMASK, &ifr) == -1) |
| 654 | return; |
| 655 | |
| 656 | netmask = ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr; |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 657 | } |
| 658 | |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 659 | if (extract_request(header, (size_t)n, daemon->namebuff, &type)) |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 660 | { |
| 661 | if (listen->family == AF_INET) |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 662 | log_query(F_QUERY | F_IPV4 | F_FORWARD, daemon->namebuff, |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 663 | (struct all_addr *)&source_addr.in.sin_addr, type, NULL, 0); |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 664 | #ifdef HAVE_IPV6 |
| 665 | else |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 666 | log_query(F_QUERY | F_IPV6 | F_FORWARD, daemon->namebuff, |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 667 | (struct all_addr *)&source_addr.in6.sin6_addr, type, NULL, 0); |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 668 | #endif |
| 669 | } |
| 670 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 671 | m = answer_request (header, ((char *) header) + PACKETSZ, (size_t)n, |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 672 | dst_addr_4, netmask, now); |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 673 | if (m >= 1) |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame^] | 674 | { |
| 675 | send_from(listen->fd, daemon->options & OPT_NOWILD, (char *)header, |
| 676 | m, &source_addr, &dst_addr, if_index); |
| 677 | daemon->local_answer++; |
| 678 | } |
| 679 | else if (forward_query(listen->fd, &source_addr, &dst_addr, if_index, |
| 680 | header, (size_t)n, now, NULL)) |
| 681 | daemon->queries_forwarded++; |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 682 | else |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame^] | 683 | daemon->local_answer++; |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 684 | } |
| 685 | |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 686 | /* The daemon forks before calling this: it should deal with one connection, |
| 687 | blocking as neccessary, and then return. Note, need to be a bit careful |
| 688 | about resources for debug mode, when the fork is suppressed: that's |
| 689 | done by the caller. */ |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 690 | unsigned char *tcp_request(int confd, time_t now, |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 691 | struct in_addr local_addr, struct in_addr netmask) |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 692 | { |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 693 | int size = 0; |
| 694 | size_t m; |
Simon Kelley | c1bb850 | 2004-08-11 18:40:17 +0100 | [diff] [blame] | 695 | unsigned short qtype, gotname; |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 696 | unsigned char c1, c2; |
| 697 | /* Max TCP packet + slop */ |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 698 | unsigned char *packet = whine_malloc(65536 + MAXDNAME + RRFIXEDSZ); |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 699 | HEADER *header; |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 700 | struct server *last_server; |
| 701 | |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 702 | while (1) |
| 703 | { |
| 704 | if (!packet || |
| 705 | !read_write(confd, &c1, 1, 1) || !read_write(confd, &c2, 1, 1) || |
| 706 | !(size = c1 << 8 | c2) || |
| 707 | !read_write(confd, packet, size, 1)) |
| 708 | return packet; |
| 709 | |
| 710 | if (size < (int)sizeof(HEADER)) |
| 711 | continue; |
| 712 | |
| 713 | header = (HEADER *)packet; |
| 714 | |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 715 | if ((gotname = extract_request(header, (unsigned int)size, daemon->namebuff, &qtype))) |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 716 | { |
| 717 | union mysockaddr peer_addr; |
| 718 | socklen_t peer_len = sizeof(union mysockaddr); |
| 719 | |
| 720 | if (getpeername(confd, (struct sockaddr *)&peer_addr, &peer_len) != -1) |
| 721 | { |
| 722 | if (peer_addr.sa.sa_family == AF_INET) |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 723 | log_query(F_QUERY | F_IPV4 | F_FORWARD, daemon->namebuff, |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 724 | (struct all_addr *)&peer_addr.in.sin_addr, qtype, NULL, 0); |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 725 | #ifdef HAVE_IPV6 |
| 726 | else |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 727 | log_query(F_QUERY | F_IPV6 | F_FORWARD, daemon->namebuff, |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 728 | (struct all_addr *)&peer_addr.in6.sin6_addr, qtype, NULL, 0); |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 729 | #endif |
| 730 | } |
| 731 | } |
| 732 | |
| 733 | /* m > 0 if answered from cache */ |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 734 | m = answer_request(header, ((char *) header) + 65536, (unsigned int)size, |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 735 | local_addr, netmask, now); |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 736 | |
| 737 | /* Do this by steam now we're not in the select() loop */ |
| 738 | check_log_writer(NULL); |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 739 | |
| 740 | if (m == 0) |
| 741 | { |
| 742 | unsigned short flags = 0; |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 743 | struct all_addr *addrp = NULL; |
| 744 | int type = 0; |
| 745 | char *domain = NULL; |
| 746 | |
| 747 | if (gotname) |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 748 | flags = search_servers(now, &addrp, gotname, daemon->namebuff, &type, &domain); |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 749 | |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 750 | if (type != 0 || (daemon->options & OPT_ORDER) || !daemon->last_server) |
| 751 | last_server = daemon->servers; |
| 752 | else |
| 753 | last_server = daemon->last_server; |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 754 | |
| 755 | if (!flags && last_server) |
| 756 | { |
| 757 | struct server *firstsendto = NULL; |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 758 | unsigned int crc = questions_crc(header, (unsigned int)size, daemon->namebuff); |
| 759 | |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 760 | /* Loop round available servers until we succeed in connecting to one. |
| 761 | Note that this code subtley ensures that consecutive queries on this connection |
| 762 | which can go to the same server, do so. */ |
| 763 | while (1) |
| 764 | { |
| 765 | if (!firstsendto) |
| 766 | firstsendto = last_server; |
| 767 | else |
| 768 | { |
| 769 | if (!(last_server = last_server->next)) |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 770 | last_server = daemon->servers; |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 771 | |
| 772 | if (last_server == firstsendto) |
| 773 | break; |
| 774 | } |
| 775 | |
| 776 | /* server for wrong domain */ |
| 777 | if (type != (last_server->flags & SERV_TYPE) || |
| 778 | (type == SERV_HAS_DOMAIN && !hostname_isequal(domain, last_server->domain))) |
| 779 | continue; |
| 780 | |
| 781 | if ((last_server->tcpfd == -1) && |
| 782 | (last_server->tcpfd = socket(last_server->addr.sa.sa_family, SOCK_STREAM, 0)) != -1 && |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame^] | 783 | (!local_bind(last_server->tcpfd, &last_server->source_addr, last_server->interface, 1) || |
| 784 | connect(last_server->tcpfd, &last_server->addr.sa, sa_len(&last_server->addr)) == -1)) |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 785 | { |
| 786 | close(last_server->tcpfd); |
| 787 | last_server->tcpfd = -1; |
| 788 | } |
| 789 | |
| 790 | if (last_server->tcpfd == -1) |
| 791 | continue; |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame^] | 792 | |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 793 | c1 = size >> 8; |
| 794 | c2 = size; |
| 795 | |
| 796 | if (!read_write(last_server->tcpfd, &c1, 1, 0) || |
| 797 | !read_write(last_server->tcpfd, &c2, 1, 0) || |
| 798 | !read_write(last_server->tcpfd, packet, size, 0) || |
| 799 | !read_write(last_server->tcpfd, &c1, 1, 1) || |
| 800 | !read_write(last_server->tcpfd, &c2, 1, 1)) |
| 801 | { |
| 802 | close(last_server->tcpfd); |
| 803 | last_server->tcpfd = -1; |
| 804 | continue; |
| 805 | } |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame^] | 806 | |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 807 | m = (c1 << 8) | c2; |
| 808 | if (!read_write(last_server->tcpfd, packet, m, 1)) |
| 809 | return packet; |
| 810 | |
| 811 | if (!gotname) |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 812 | strcpy(daemon->namebuff, "query"); |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 813 | if (last_server->addr.sa.sa_family == AF_INET) |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 814 | log_query(F_SERVER | F_IPV4 | F_FORWARD, daemon->namebuff, |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 815 | (struct all_addr *)&last_server->addr.in.sin_addr, 0, NULL, 0); |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 816 | #ifdef HAVE_IPV6 |
| 817 | else |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 818 | log_query(F_SERVER | F_IPV6 | F_FORWARD, daemon->namebuff, |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 819 | (struct all_addr *)&last_server->addr.in6.sin6_addr, 0, NULL, 0); |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 820 | #endif |
| 821 | |
| 822 | /* There's no point in updating the cache, since this process will exit and |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 823 | lose the information after a few queries. We make this call for the alias and |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 824 | bogus-nxdomain side-effects. */ |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 825 | /* If the crc of the question section doesn't match the crc we sent, then |
| 826 | someone might be attempting to insert bogus values into the cache by |
| 827 | sending replies containing questions and bogus answers. */ |
| 828 | if (crc == questions_crc(header, (unsigned int)m, daemon->namebuff)) |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 829 | m = process_reply(header, now, last_server, (unsigned int)m); |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 830 | |
| 831 | break; |
| 832 | } |
| 833 | } |
| 834 | |
| 835 | /* In case of local answer or no connections made. */ |
| 836 | if (m == 0) |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 837 | m = setup_reply(header, (unsigned int)size, addrp, flags, daemon->local_ttl); |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 838 | } |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 839 | |
| 840 | check_log_writer(NULL); |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 841 | |
| 842 | c1 = m>>8; |
| 843 | c2 = m; |
| 844 | if (!read_write(confd, &c1, 1, 0) || |
| 845 | !read_write(confd, &c2, 1, 0) || |
| 846 | !read_write(confd, packet, m, 0)) |
| 847 | return packet; |
| 848 | } |
| 849 | } |
| 850 | |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 851 | static struct frec *allocate_frec(time_t now) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 852 | { |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 853 | struct frec *f; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 854 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 855 | if ((f = (struct frec *)whine_malloc(sizeof(struct frec)))) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 856 | { |
| 857 | f->next = frec_list; |
| 858 | f->time = now; |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 859 | f->sentto = NULL; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 860 | frec_list = f; |
| 861 | } |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 862 | |
| 863 | return f; |
| 864 | } |
| 865 | |
| 866 | /* if wait==NULL return a free or older than TIMEOUT record. |
| 867 | else return *wait zero if one available, or *wait is delay to |
| 868 | when the oldest in-use record will expire. */ |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 869 | struct frec *get_new_frec(time_t now, int *wait) |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 870 | { |
| 871 | struct frec *f, *oldest; |
| 872 | int count; |
| 873 | |
| 874 | if (wait) |
| 875 | *wait = 0; |
| 876 | |
| 877 | for (f = frec_list, oldest = NULL, count = 0; f; f = f->next, count++) |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 878 | if (!f->sentto) |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 879 | { |
| 880 | f->time = now; |
| 881 | return f; |
| 882 | } |
| 883 | else if (!oldest || difftime(f->time, oldest->time) <= 0) |
| 884 | oldest = f; |
| 885 | |
| 886 | /* can't find empty one, use oldest if there is one |
| 887 | and it's older than timeout */ |
| 888 | if (oldest && ((int)difftime(now, oldest->time)) >= TIMEOUT) |
| 889 | { |
| 890 | /* keep stuff for twice timeout if we can by allocating a new |
| 891 | record instead */ |
| 892 | if (difftime(now, oldest->time) < 2*TIMEOUT && |
| 893 | count <= daemon->ftabsize && |
| 894 | (f = allocate_frec(now))) |
| 895 | return f; |
| 896 | |
| 897 | if (!wait) |
| 898 | { |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 899 | oldest->sentto = 0; |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 900 | oldest->time = now; |
| 901 | } |
| 902 | return oldest; |
| 903 | } |
| 904 | |
| 905 | /* none available, calculate time 'till oldest record expires */ |
| 906 | if (count > daemon->ftabsize) |
| 907 | { |
| 908 | if (oldest && wait) |
| 909 | *wait = oldest->time + (time_t)TIMEOUT - now; |
| 910 | return NULL; |
| 911 | } |
| 912 | |
| 913 | if (!(f = allocate_frec(now)) && wait) |
| 914 | /* wait one second on malloc failure */ |
| 915 | *wait = 1; |
| 916 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 917 | return f; /* OK if malloc fails and this is NULL */ |
| 918 | } |
| 919 | |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 920 | /* crc is all-ones if not known. */ |
| 921 | static struct frec *lookup_frec(unsigned short id, unsigned int crc) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 922 | { |
| 923 | struct frec *f; |
| 924 | |
| 925 | for(f = frec_list; f; f = f->next) |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 926 | if (f->sentto && f->new_id == id && |
| 927 | (f->crc == crc || crc == 0xffffffff)) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 928 | return f; |
| 929 | |
| 930 | return NULL; |
| 931 | } |
| 932 | |
| 933 | static struct frec *lookup_frec_by_sender(unsigned short id, |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 934 | union mysockaddr *addr, |
| 935 | unsigned int crc) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 936 | { |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 937 | struct frec *f; |
| 938 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 939 | for(f = frec_list; f; f = f->next) |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 940 | if (f->sentto && |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 941 | f->orig_id == id && |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 942 | f->crc == crc && |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 943 | sockaddr_isequal(&f->source, addr)) |
| 944 | return f; |
| 945 | |
| 946 | return NULL; |
| 947 | } |
| 948 | |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 949 | /* A server record is going away, remove references to it */ |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 950 | void server_gone(struct server *server) |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 951 | { |
| 952 | struct frec *f; |
| 953 | |
| 954 | for (f = frec_list; f; f = f->next) |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 955 | if (f->sentto && f->sentto == server) |
| 956 | f->sentto = NULL; |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 957 | |
| 958 | if (daemon->last_server == server) |
| 959 | daemon->last_server = NULL; |
| 960 | |
| 961 | if (daemon->srv_save == server) |
| 962 | daemon->srv_save = NULL; |
| 963 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 964 | |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 965 | /* return unique random ids. |
| 966 | For signed packets we can't change the ID without breaking the |
| 967 | signing, so we keep the same one. In this case force is set, and this |
| 968 | routine degenerates into killing any conflicting forward record. */ |
| 969 | static unsigned short get_id(int force, unsigned short force_id, unsigned int crc) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 970 | { |
| 971 | unsigned short ret = 0; |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 972 | |
| 973 | if (force) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 974 | { |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 975 | struct frec *f = lookup_frec(force_id, crc); |
| 976 | if (f) |
| 977 | f->sentto = NULL; /* free */ |
| 978 | ret = force_id; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 979 | } |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 980 | else do |
| 981 | ret = rand16(); |
| 982 | while (lookup_frec(ret, crc)); |
| 983 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 984 | return ret; |
| 985 | } |
| 986 | |
| 987 | |
| 988 | |
| 989 | |
| 990 | |