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