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