Simon Kelley | c8e8f5c | 2021-01-24 21:59:37 +0000 | [diff] [blame] | 1 | /* dnsmasq is Copyright (c) 2000-2021 Simon Kelley |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 2 | |
| 3 | This program is free software; you can redistribute it and/or modify |
| 4 | it under the terms of the GNU General Public License as published by |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 5 | the Free Software Foundation; version 2 dated June, 1991, or |
| 6 | (at your option) version 3 dated 29 June, 2007. |
| 7 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 8 | This program is distributed in the hope that it will be useful, |
| 9 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | GNU General Public License for more details. |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 12 | |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 13 | You should have received a copy of the GNU General Public License |
| 14 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 15 | */ |
| 16 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 17 | #include "dnsmasq.h" |
| 18 | |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 19 | #ifdef HAVE_LINUX_NETWORK |
| 20 | |
| 21 | int indextoname(int fd, int index, char *name) |
| 22 | { |
| 23 | struct ifreq ifr; |
| 24 | |
| 25 | if (index == 0) |
| 26 | return 0; |
| 27 | |
| 28 | ifr.ifr_ifindex = index; |
| 29 | if (ioctl(fd, SIOCGIFNAME, &ifr) == -1) |
| 30 | return 0; |
| 31 | |
Petr Menšík | 47b45b2 | 2018-08-15 18:17:00 +0200 | [diff] [blame] | 32 | safe_strncpy(name, ifr.ifr_name, IF_NAMESIZE); |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 33 | |
| 34 | return 1; |
| 35 | } |
| 36 | |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 37 | |
| 38 | #elif defined(HAVE_SOLARIS_NETWORK) |
| 39 | |
| 40 | #include <zone.h> |
| 41 | #include <alloca.h> |
| 42 | #ifndef LIFC_UNDER_IPMP |
| 43 | # define LIFC_UNDER_IPMP 0 |
| 44 | #endif |
| 45 | |
| 46 | int indextoname(int fd, int index, char *name) |
| 47 | { |
| 48 | int64_t lifc_flags; |
| 49 | struct lifnum lifn; |
| 50 | int numifs, bufsize, i; |
| 51 | struct lifconf lifc; |
| 52 | struct lifreq *lifrp; |
| 53 | |
| 54 | if (index == 0) |
| 55 | return 0; |
| 56 | |
| 57 | if (getzoneid() == GLOBAL_ZONEID) |
| 58 | { |
| 59 | if (!if_indextoname(index, name)) |
| 60 | return 0; |
| 61 | return 1; |
| 62 | } |
| 63 | |
| 64 | lifc_flags = LIFC_NOXMIT | LIFC_TEMPORARY | LIFC_ALLZONES | LIFC_UNDER_IPMP; |
| 65 | lifn.lifn_family = AF_UNSPEC; |
| 66 | lifn.lifn_flags = lifc_flags; |
| 67 | if (ioctl(fd, SIOCGLIFNUM, &lifn) < 0) |
| 68 | return 0; |
| 69 | |
| 70 | numifs = lifn.lifn_count; |
| 71 | bufsize = numifs * sizeof(struct lifreq); |
| 72 | |
| 73 | lifc.lifc_family = AF_UNSPEC; |
| 74 | lifc.lifc_flags = lifc_flags; |
| 75 | lifc.lifc_len = bufsize; |
| 76 | lifc.lifc_buf = alloca(bufsize); |
| 77 | |
| 78 | if (ioctl(fd, SIOCGLIFCONF, &lifc) < 0) |
| 79 | return 0; |
| 80 | |
| 81 | lifrp = lifc.lifc_req; |
| 82 | for (i = lifc.lifc_len / sizeof(struct lifreq); i; i--, lifrp++) |
| 83 | { |
| 84 | struct lifreq lifr; |
Petr Menšík | 47b45b2 | 2018-08-15 18:17:00 +0200 | [diff] [blame] | 85 | safe_strncpy(lifr.lifr_name, lifrp->lifr_name, IF_NAMESIZE); |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 86 | if (ioctl(fd, SIOCGLIFINDEX, &lifr) < 0) |
| 87 | return 0; |
| 88 | |
| 89 | if (lifr.lifr_index == index) { |
Petr Menšík | 47b45b2 | 2018-08-15 18:17:00 +0200 | [diff] [blame] | 90 | safe_strncpy(name, lifr.lifr_name, IF_NAMESIZE); |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 91 | return 1; |
| 92 | } |
| 93 | } |
| 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 98 | #else |
| 99 | |
| 100 | int indextoname(int fd, int index, char *name) |
| 101 | { |
Simon Kelley | 115ac3e | 2013-05-20 11:28:32 +0100 | [diff] [blame] | 102 | (void)fd; |
| 103 | |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 104 | if (index == 0 || !if_indextoname(index, name)) |
| 105 | return 0; |
| 106 | |
| 107 | return 1; |
| 108 | } |
| 109 | |
| 110 | #endif |
| 111 | |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 112 | int iface_check(int family, union all_addr *addr, char *name, int *auth) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 113 | { |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 114 | struct iname *tmp; |
Simon Kelley | edf0bde | 2013-07-29 17:21:48 +0100 | [diff] [blame] | 115 | int ret = 1, match_addr = 0; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 116 | |
Simon Kelley | 309331f | 2006-04-22 15:05:01 +0100 | [diff] [blame] | 117 | /* Note: have to check all and not bail out early, so that we set the |
Simon Kelley | 89500e3 | 2013-09-20 16:29:20 +0100 | [diff] [blame] | 118 | "used" flags. |
| 119 | |
| 120 | May be called with family == AF_LOCALto check interface by name only. */ |
Simon Kelley | 429798f | 2012-12-10 20:45:53 +0000 | [diff] [blame] | 121 | |
Simon Kelley | 4f7b304 | 2012-11-28 21:27:02 +0000 | [diff] [blame] | 122 | if (auth) |
Simon Kelley | 429798f | 2012-12-10 20:45:53 +0000 | [diff] [blame] | 123 | *auth = 0; |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 124 | |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 125 | if (daemon->if_names || daemon->if_addrs) |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 126 | { |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 127 | ret = 0; |
| 128 | |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 129 | for (tmp = daemon->if_names; tmp; tmp = tmp->next) |
Simon Kelley | 49333cb | 2013-03-15 20:30:51 +0000 | [diff] [blame] | 130 | if (tmp->name && wildcard_match(tmp->name, name)) |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 131 | ret = tmp->used = 1; |
| 132 | |
Simon Kelley | 429798f | 2012-12-10 20:45:53 +0000 | [diff] [blame] | 133 | if (addr) |
| 134 | for (tmp = daemon->if_addrs; tmp; tmp = tmp->next) |
| 135 | if (tmp->addr.sa.sa_family == family) |
| 136 | { |
| 137 | if (family == AF_INET && |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 138 | tmp->addr.in.sin_addr.s_addr == addr->addr4.s_addr) |
Simon Kelley | edf0bde | 2013-07-29 17:21:48 +0100 | [diff] [blame] | 139 | ret = match_addr = tmp->used = 1; |
Simon Kelley | 429798f | 2012-12-10 20:45:53 +0000 | [diff] [blame] | 140 | else if (family == AF_INET6 && |
| 141 | IN6_ARE_ADDR_EQUAL(&tmp->addr.in6.sin6_addr, |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 142 | &addr->addr6)) |
Simon Kelley | edf0bde | 2013-07-29 17:21:48 +0100 | [diff] [blame] | 143 | ret = match_addr = tmp->used = 1; |
Simon Kelley | 429798f | 2012-12-10 20:45:53 +0000 | [diff] [blame] | 144 | } |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 145 | } |
| 146 | |
Simon Kelley | edf0bde | 2013-07-29 17:21:48 +0100 | [diff] [blame] | 147 | if (!match_addr) |
| 148 | for (tmp = daemon->if_except; tmp; tmp = tmp->next) |
| 149 | if (tmp->name && wildcard_match(tmp->name, name)) |
| 150 | ret = 0; |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 151 | |
Simon Kelley | 429798f | 2012-12-10 20:45:53 +0000 | [diff] [blame] | 152 | |
| 153 | for (tmp = daemon->authinterface; tmp; tmp = tmp->next) |
| 154 | if (tmp->name) |
| 155 | { |
Simon Kelley | f25e6c6 | 2013-11-17 12:23:42 +0000 | [diff] [blame] | 156 | if (strcmp(tmp->name, name) == 0 && |
| 157 | (tmp->addr.sa.sa_family == 0 || tmp->addr.sa.sa_family == family)) |
Simon Kelley | 429798f | 2012-12-10 20:45:53 +0000 | [diff] [blame] | 158 | break; |
| 159 | } |
| 160 | else if (addr && tmp->addr.sa.sa_family == AF_INET && family == AF_INET && |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 161 | tmp->addr.in.sin_addr.s_addr == addr->addr4.s_addr) |
Simon Kelley | 429798f | 2012-12-10 20:45:53 +0000 | [diff] [blame] | 162 | break; |
Simon Kelley | 429798f | 2012-12-10 20:45:53 +0000 | [diff] [blame] | 163 | else if (addr && tmp->addr.sa.sa_family == AF_INET6 && family == AF_INET6 && |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 164 | IN6_ARE_ADDR_EQUAL(&tmp->addr.in6.sin6_addr, &addr->addr6)) |
Simon Kelley | 429798f | 2012-12-10 20:45:53 +0000 | [diff] [blame] | 165 | break; |
Simon Kelley | 429798f | 2012-12-10 20:45:53 +0000 | [diff] [blame] | 166 | |
| 167 | if (tmp && auth) |
| 168 | { |
| 169 | *auth = 1; |
| 170 | ret = 1; |
| 171 | } |
| 172 | |
Simon Kelley | 309331f | 2006-04-22 15:05:01 +0100 | [diff] [blame] | 173 | return ret; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 174 | } |
Simon Kelley | e25db1f | 2013-01-29 22:10:26 +0000 | [diff] [blame] | 175 | |
| 176 | |
Josh Soref | 730c674 | 2017-02-06 16:14:04 +0000 | [diff] [blame] | 177 | /* Fix for problem that the kernel sometimes reports the loopback interface as the |
Simon Kelley | e25db1f | 2013-01-29 22:10:26 +0000 | [diff] [blame] | 178 | arrival interface when a packet originates locally, even when sent to address of |
| 179 | an interface other than the loopback. Accept packet if it arrived via a loopback |
| 180 | interface, even when we're not accepting packets that way, as long as the destination |
| 181 | address is one we're believing. Interface list must be up-to-date before calling. */ |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 182 | int loopback_exception(int fd, int family, union all_addr *addr, char *name) |
Simon Kelley | e25db1f | 2013-01-29 22:10:26 +0000 | [diff] [blame] | 183 | { |
| 184 | struct ifreq ifr; |
| 185 | struct irec *iface; |
| 186 | |
Petr Menšík | 47b45b2 | 2018-08-15 18:17:00 +0200 | [diff] [blame] | 187 | safe_strncpy(ifr.ifr_name, name, IF_NAMESIZE); |
Simon Kelley | e25db1f | 2013-01-29 22:10:26 +0000 | [diff] [blame] | 188 | if (ioctl(fd, SIOCGIFFLAGS, &ifr) != -1 && |
| 189 | ifr.ifr_flags & IFF_LOOPBACK) |
| 190 | { |
| 191 | for (iface = daemon->interfaces; iface; iface = iface->next) |
| 192 | if (iface->addr.sa.sa_family == family) |
| 193 | { |
| 194 | if (family == AF_INET) |
| 195 | { |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 196 | if (iface->addr.in.sin_addr.s_addr == addr->addr4.s_addr) |
Simon Kelley | e25db1f | 2013-01-29 22:10:26 +0000 | [diff] [blame] | 197 | return 1; |
| 198 | } |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 199 | else if (IN6_ARE_ADDR_EQUAL(&iface->addr.in6.sin6_addr, &addr->addr6)) |
Simon Kelley | e25db1f | 2013-01-29 22:10:26 +0000 | [diff] [blame] | 200 | return 1; |
Simon Kelley | e25db1f | 2013-01-29 22:10:26 +0000 | [diff] [blame] | 201 | } |
| 202 | } |
| 203 | return 0; |
| 204 | } |
| 205 | |
Simon Kelley | 3f2873d | 2013-05-14 11:28:47 +0100 | [diff] [blame] | 206 | /* If we're configured with something like --interface=eth0:0 then we'll listen correctly |
| 207 | on the relevant address, but the name of the arrival interface, derived from the |
| 208 | index won't match the config. Check that we found an interface address for the arrival |
| 209 | interface: daemon->interfaces must be up-to-date. */ |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 210 | int label_exception(int index, int family, union all_addr *addr) |
Simon Kelley | 3f2873d | 2013-05-14 11:28:47 +0100 | [diff] [blame] | 211 | { |
| 212 | struct irec *iface; |
| 213 | |
| 214 | /* labels only supported on IPv4 addresses. */ |
| 215 | if (family != AF_INET) |
| 216 | return 0; |
| 217 | |
| 218 | for (iface = daemon->interfaces; iface; iface = iface->next) |
| 219 | if (iface->index == index && iface->addr.sa.sa_family == AF_INET && |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 220 | iface->addr.in.sin_addr.s_addr == addr->addr4.s_addr) |
Simon Kelley | 3f2873d | 2013-05-14 11:28:47 +0100 | [diff] [blame] | 221 | return 1; |
| 222 | |
| 223 | return 0; |
| 224 | } |
| 225 | |
Simon Kelley | 115ac3e | 2013-05-20 11:28:32 +0100 | [diff] [blame] | 226 | struct iface_param { |
| 227 | struct addrlist *spare; |
| 228 | int fd; |
| 229 | }; |
| 230 | |
| 231 | static int iface_allowed(struct iface_param *param, int if_index, char *label, |
Simon Kelley | 4766936 | 2014-12-17 12:41:56 +0000 | [diff] [blame] | 232 | union mysockaddr *addr, struct in_addr netmask, int prefixlen, int iface_flags) |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 233 | { |
| 234 | struct irec *iface; |
Simon Kelley | 115ac3e | 2013-05-20 11:28:32 +0100 | [diff] [blame] | 235 | int mtu = 0, loopback; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 236 | struct ifreq ifr; |
Simon Kelley | 8bc4cec | 2012-07-03 21:04:11 +0100 | [diff] [blame] | 237 | int tftp_ok = !!option_bool(OPT_TFTP); |
Simon Kelley | 9380ba7 | 2012-04-16 14:41:56 +0100 | [diff] [blame] | 238 | int dhcp_ok = 1; |
Simon Kelley | 4f7b304 | 2012-11-28 21:27:02 +0000 | [diff] [blame] | 239 | int auth_dns = 0; |
Petr Menšík | ad59f27 | 2017-03-17 17:22:19 +0000 | [diff] [blame] | 240 | int is_label = 0; |
Simon Kelley | 91543f4 | 2013-09-23 12:41:20 +0100 | [diff] [blame] | 241 | #if defined(HAVE_DHCP) || defined(HAVE_TFTP) |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 242 | struct iname *tmp; |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 243 | #endif |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 244 | |
Vladislav Grishenko | 3b19596 | 2013-11-26 11:08:21 +0000 | [diff] [blame] | 245 | (void)prefixlen; |
| 246 | |
Simon Kelley | 115ac3e | 2013-05-20 11:28:32 +0100 | [diff] [blame] | 247 | if (!indextoname(param->fd, if_index, ifr.ifr_name) || |
| 248 | ioctl(param->fd, SIOCGIFFLAGS, &ifr) == -1) |
| 249 | return 0; |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 250 | |
| 251 | loopback = ifr.ifr_flags & IFF_LOOPBACK; |
Simon Kelley | 9380ba7 | 2012-04-16 14:41:56 +0100 | [diff] [blame] | 252 | |
| 253 | if (loopback) |
Simon Kelley | 3f2873d | 2013-05-14 11:28:47 +0100 | [diff] [blame] | 254 | dhcp_ok = 0; |
| 255 | |
Simon Kelley | 115ac3e | 2013-05-20 11:28:32 +0100 | [diff] [blame] | 256 | if (ioctl(param->fd, SIOCGIFMTU, &ifr) != -1) |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 257 | mtu = ifr.ifr_mtu; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 258 | |
Simon Kelley | 115ac3e | 2013-05-20 11:28:32 +0100 | [diff] [blame] | 259 | if (!label) |
| 260 | label = ifr.ifr_name; |
Petr Menšík | ad59f27 | 2017-03-17 17:22:19 +0000 | [diff] [blame] | 261 | else |
| 262 | is_label = strcmp(label, ifr.ifr_name); |
Simon Kelley | c8a8048 | 2014-03-05 14:29:54 +0000 | [diff] [blame] | 263 | |
| 264 | /* maintain a list of all addresses on all interfaces for --local-service option */ |
| 265 | if (option_bool(OPT_LOCAL_SERVICE)) |
| 266 | { |
| 267 | struct addrlist *al; |
Simon Kelley | 115ac3e | 2013-05-20 11:28:32 +0100 | [diff] [blame] | 268 | |
Simon Kelley | c8a8048 | 2014-03-05 14:29:54 +0000 | [diff] [blame] | 269 | if (param->spare) |
| 270 | { |
| 271 | al = param->spare; |
| 272 | param->spare = al->next; |
| 273 | } |
| 274 | else |
| 275 | al = whine_malloc(sizeof(struct addrlist)); |
| 276 | |
| 277 | if (al) |
| 278 | { |
| 279 | al->next = daemon->interface_addrs; |
| 280 | daemon->interface_addrs = al; |
| 281 | al->prefixlen = prefixlen; |
| 282 | |
| 283 | if (addr->sa.sa_family == AF_INET) |
| 284 | { |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 285 | al->addr.addr4 = addr->in.sin_addr; |
Simon Kelley | c8a8048 | 2014-03-05 14:29:54 +0000 | [diff] [blame] | 286 | al->flags = 0; |
| 287 | } |
Simon Kelley | c8a8048 | 2014-03-05 14:29:54 +0000 | [diff] [blame] | 288 | else |
| 289 | { |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 290 | al->addr.addr6 = addr->in6.sin6_addr; |
Simon Kelley | c8a8048 | 2014-03-05 14:29:54 +0000 | [diff] [blame] | 291 | al->flags = ADDRLIST_IPV6; |
| 292 | } |
Simon Kelley | c8a8048 | 2014-03-05 14:29:54 +0000 | [diff] [blame] | 293 | } |
| 294 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 295 | |
Simon Kelley | 115ac3e | 2013-05-20 11:28:32 +0100 | [diff] [blame] | 296 | if (addr->sa.sa_family != AF_INET6 || !IN6_IS_ADDR_LINKLOCAL(&addr->in6.sin6_addr)) |
Simon Kelley | 115ac3e | 2013-05-20 11:28:32 +0100 | [diff] [blame] | 297 | { |
| 298 | struct interface_name *int_name; |
| 299 | struct addrlist *al; |
Simon Kelley | 376d48c | 2013-11-13 13:04:30 +0000 | [diff] [blame] | 300 | #ifdef HAVE_AUTH |
| 301 | struct auth_zone *zone; |
| 302 | struct auth_name_list *name; |
Simon Kelley | 115ac3e | 2013-05-20 11:28:32 +0100 | [diff] [blame] | 303 | |
Simon Kelley | 376d48c | 2013-11-13 13:04:30 +0000 | [diff] [blame] | 304 | /* Find subnets in auth_zones */ |
| 305 | for (zone = daemon->auth_zones; zone; zone = zone->next) |
| 306 | for (name = zone->interface_names; name; name = name->next) |
| 307 | if (wildcard_match(name->name, label)) |
| 308 | { |
| 309 | if (addr->sa.sa_family == AF_INET && (name->flags & AUTH4)) |
| 310 | { |
| 311 | if (param->spare) |
| 312 | { |
| 313 | al = param->spare; |
| 314 | param->spare = al->next; |
| 315 | } |
| 316 | else |
| 317 | al = whine_malloc(sizeof(struct addrlist)); |
| 318 | |
| 319 | if (al) |
| 320 | { |
| 321 | al->next = zone->subnet; |
| 322 | zone->subnet = al; |
| 323 | al->prefixlen = prefixlen; |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 324 | al->addr.addr4 = addr->in.sin_addr; |
Simon Kelley | 376d48c | 2013-11-13 13:04:30 +0000 | [diff] [blame] | 325 | al->flags = 0; |
| 326 | } |
| 327 | } |
| 328 | |
Simon Kelley | 376d48c | 2013-11-13 13:04:30 +0000 | [diff] [blame] | 329 | if (addr->sa.sa_family == AF_INET6 && (name->flags & AUTH6)) |
| 330 | { |
| 331 | if (param->spare) |
| 332 | { |
| 333 | al = param->spare; |
| 334 | param->spare = al->next; |
| 335 | } |
| 336 | else |
| 337 | al = whine_malloc(sizeof(struct addrlist)); |
| 338 | |
| 339 | if (al) |
| 340 | { |
| 341 | al->next = zone->subnet; |
| 342 | zone->subnet = al; |
Vladislav Grishenko | 3b19596 | 2013-11-26 11:08:21 +0000 | [diff] [blame] | 343 | al->prefixlen = prefixlen; |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 344 | al->addr.addr6 = addr->in6.sin6_addr; |
Simon Kelley | 376d48c | 2013-11-13 13:04:30 +0000 | [diff] [blame] | 345 | al->flags = ADDRLIST_IPV6; |
| 346 | } |
| 347 | } |
Simon Kelley | 376d48c | 2013-11-13 13:04:30 +0000 | [diff] [blame] | 348 | } |
| 349 | #endif |
| 350 | |
| 351 | /* Update addresses from interface_names. These are a set independent |
| 352 | of the set we're listening on. */ |
Simon Kelley | 115ac3e | 2013-05-20 11:28:32 +0100 | [diff] [blame] | 353 | for (int_name = daemon->int_names; int_name; int_name = int_name->next) |
Simon Kelley | f7029f5 | 2013-11-21 15:09:09 +0000 | [diff] [blame] | 354 | if (strncmp(label, int_name->intr, IF_NAMESIZE) == 0 && |
| 355 | (addr->sa.sa_family == int_name->family || int_name->family == 0)) |
Simon Kelley | 115ac3e | 2013-05-20 11:28:32 +0100 | [diff] [blame] | 356 | { |
| 357 | if (param->spare) |
| 358 | { |
| 359 | al = param->spare; |
| 360 | param->spare = al->next; |
| 361 | } |
| 362 | else |
| 363 | al = whine_malloc(sizeof(struct addrlist)); |
| 364 | |
| 365 | if (al) |
| 366 | { |
Simon Kelley | 376d48c | 2013-11-13 13:04:30 +0000 | [diff] [blame] | 367 | al->next = int_name->addr; |
| 368 | int_name->addr = al; |
| 369 | |
Simon Kelley | 115ac3e | 2013-05-20 11:28:32 +0100 | [diff] [blame] | 370 | if (addr->sa.sa_family == AF_INET) |
| 371 | { |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 372 | al->addr.addr4 = addr->in.sin_addr; |
Simon Kelley | 376d48c | 2013-11-13 13:04:30 +0000 | [diff] [blame] | 373 | al->flags = 0; |
Simon Kelley | 115ac3e | 2013-05-20 11:28:32 +0100 | [diff] [blame] | 374 | } |
Simon Kelley | 115ac3e | 2013-05-20 11:28:32 +0100 | [diff] [blame] | 375 | else |
| 376 | { |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 377 | al->addr.addr6 = addr->in6.sin6_addr; |
Simon Kelley | 376d48c | 2013-11-13 13:04:30 +0000 | [diff] [blame] | 378 | al->flags = ADDRLIST_IPV6; |
Simon Kelley | 4766936 | 2014-12-17 12:41:56 +0000 | [diff] [blame] | 379 | /* Privacy addresses and addresses still undergoing DAD and deprecated addresses |
| 380 | don't appear in forward queries, but will in reverse ones. */ |
| 381 | if (!(iface_flags & IFACE_PERMANENT) || (iface_flags & (IFACE_DEPRECATED | IFACE_TENTATIVE))) |
| 382 | al->flags |= ADDRLIST_REVONLY; |
Simon Kelley | 115ac3e | 2013-05-20 11:28:32 +0100 | [diff] [blame] | 383 | } |
Simon Kelley | 115ac3e | 2013-05-20 11:28:32 +0100 | [diff] [blame] | 384 | } |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | /* check whether the interface IP has been added already |
| 389 | we call this routine multiple times. */ |
| 390 | for (iface = daemon->interfaces; iface; iface = iface->next) |
Petr Mensik | 951a221 | 2019-07-03 17:02:16 +0200 | [diff] [blame] | 391 | if (sockaddr_isequal(&iface->addr, addr) && iface->index == if_index) |
Simon Kelley | 115ac3e | 2013-05-20 11:28:32 +0100 | [diff] [blame] | 392 | { |
Simon Kelley | 4766936 | 2014-12-17 12:41:56 +0000 | [diff] [blame] | 393 | iface->dad = !!(iface_flags & IFACE_TENTATIVE); |
Simon Kelley | 0861921 | 2013-12-02 14:43:48 +0000 | [diff] [blame] | 394 | iface->found = 1; /* for garbage collection */ |
Petr Mensik | 951a221 | 2019-07-03 17:02:16 +0200 | [diff] [blame] | 395 | iface->netmask = netmask; |
Simon Kelley | 115ac3e | 2013-05-20 11:28:32 +0100 | [diff] [blame] | 396 | return 1; |
| 397 | } |
| 398 | |
| 399 | /* If we are restricting the set of interfaces to use, make |
Simon Kelley | 59353a6 | 2004-11-21 19:34:28 +0000 | [diff] [blame] | 400 | sure that loopback interfaces are in that set. */ |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 401 | if (daemon->if_names && loopback) |
Simon Kelley | 59353a6 | 2004-11-21 19:34:28 +0000 | [diff] [blame] | 402 | { |
| 403 | struct iname *lo; |
| 404 | for (lo = daemon->if_names; lo; lo = lo->next) |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 405 | if (lo->name && strcmp(lo->name, ifr.ifr_name) == 0) |
Simon Kelley | 4ce4f37 | 2012-06-14 11:50:45 +0100 | [diff] [blame] | 406 | break; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 407 | |
Simon Kelley | 38365ff | 2013-02-05 14:35:54 +0000 | [diff] [blame] | 408 | if (!lo && (lo = whine_malloc(sizeof(struct iname)))) |
Simon Kelley | 59353a6 | 2004-11-21 19:34:28 +0000 | [diff] [blame] | 409 | { |
Simon Kelley | 38365ff | 2013-02-05 14:35:54 +0000 | [diff] [blame] | 410 | if ((lo->name = whine_malloc(strlen(ifr.ifr_name)+1))) |
| 411 | { |
| 412 | strcpy(lo->name, ifr.ifr_name); |
| 413 | lo->used = 1; |
| 414 | lo->next = daemon->if_names; |
| 415 | daemon->if_names = lo; |
| 416 | } |
| 417 | else |
| 418 | free(lo); |
Simon Kelley | 59353a6 | 2004-11-21 19:34:28 +0000 | [diff] [blame] | 419 | } |
| 420 | } |
| 421 | |
Simon Kelley | 8bc4cec | 2012-07-03 21:04:11 +0100 | [diff] [blame] | 422 | if (addr->sa.sa_family == AF_INET && |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 423 | !iface_check(AF_INET, (union all_addr *)&addr->in.sin_addr, label, &auth_dns)) |
Simon Kelley | 8bc4cec | 2012-07-03 21:04:11 +0100 | [diff] [blame] | 424 | return 1; |
Simon Kelley | 4f7b304 | 2012-11-28 21:27:02 +0000 | [diff] [blame] | 425 | |
Simon Kelley | 8bc4cec | 2012-07-03 21:04:11 +0100 | [diff] [blame] | 426 | if (addr->sa.sa_family == AF_INET6 && |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 427 | !iface_check(AF_INET6, (union all_addr *)&addr->in6.sin6_addr, label, &auth_dns)) |
Simon Kelley | 8bc4cec | 2012-07-03 21:04:11 +0100 | [diff] [blame] | 428 | return 1; |
Simon Kelley | 4f7b304 | 2012-11-28 21:27:02 +0000 | [diff] [blame] | 429 | |
| 430 | #ifdef HAVE_DHCP |
| 431 | /* No DHCP where we're doing auth DNS. */ |
| 432 | if (auth_dns) |
| 433 | { |
| 434 | tftp_ok = 0; |
| 435 | dhcp_ok = 0; |
| 436 | } |
| 437 | else |
| 438 | for (tmp = daemon->dhcp_except; tmp; tmp = tmp->next) |
Simon Kelley | 49333cb | 2013-03-15 20:30:51 +0000 | [diff] [blame] | 439 | if (tmp->name && wildcard_match(tmp->name, ifr.ifr_name)) |
Simon Kelley | 4f7b304 | 2012-11-28 21:27:02 +0000 | [diff] [blame] | 440 | { |
| 441 | tftp_ok = 0; |
| 442 | dhcp_ok = 0; |
| 443 | } |
| 444 | #endif |
| 445 | |
Simon Kelley | 2937f8a | 2013-07-29 19:49:07 +0100 | [diff] [blame] | 446 | |
Simon Kelley | 91543f4 | 2013-09-23 12:41:20 +0100 | [diff] [blame] | 447 | #ifdef HAVE_TFTP |
Simon Kelley | 2937f8a | 2013-07-29 19:49:07 +0100 | [diff] [blame] | 448 | if (daemon->tftp_interfaces) |
| 449 | { |
| 450 | /* dedicated tftp interface list */ |
| 451 | tftp_ok = 0; |
| 452 | for (tmp = daemon->tftp_interfaces; tmp; tmp = tmp->next) |
| 453 | if (tmp->name && wildcard_match(tmp->name, ifr.ifr_name)) |
| 454 | tftp_ok = 1; |
| 455 | } |
Simon Kelley | 91543f4 | 2013-09-23 12:41:20 +0100 | [diff] [blame] | 456 | #endif |
Simon Kelley | 2937f8a | 2013-07-29 19:49:07 +0100 | [diff] [blame] | 457 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 458 | /* add to list */ |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 459 | if ((iface = whine_malloc(sizeof(struct irec)))) |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 460 | { |
| 461 | iface->addr = *addr; |
| 462 | iface->netmask = netmask; |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 463 | iface->tftp_ok = tftp_ok; |
Simon Kelley | 9380ba7 | 2012-04-16 14:41:56 +0100 | [diff] [blame] | 464 | iface->dhcp_ok = dhcp_ok; |
Simon Kelley | 4f7b304 | 2012-11-28 21:27:02 +0000 | [diff] [blame] | 465 | iface->dns_auth = auth_dns; |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 466 | iface->mtu = mtu; |
Simon Kelley | 4766936 | 2014-12-17 12:41:56 +0000 | [diff] [blame] | 467 | iface->dad = !!(iface_flags & IFACE_TENTATIVE); |
Simon Kelley | 0861921 | 2013-12-02 14:43:48 +0000 | [diff] [blame] | 468 | iface->found = 1; |
Simon Kelley | dc27e14 | 2013-10-16 13:09:53 +0100 | [diff] [blame] | 469 | iface->done = iface->multicast_done = iface->warned = 0; |
Simon Kelley | 5d162f2 | 2012-12-20 14:55:46 +0000 | [diff] [blame] | 470 | iface->index = if_index; |
Petr Menšík | ad59f27 | 2017-03-17 17:22:19 +0000 | [diff] [blame] | 471 | iface->label = is_label; |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 472 | if ((iface->name = whine_malloc(strlen(ifr.ifr_name)+1))) |
Simon Kelley | 6f13e53 | 2012-04-17 14:25:06 +0100 | [diff] [blame] | 473 | { |
| 474 | strcpy(iface->name, ifr.ifr_name); |
Simon Kelley | 115ac3e | 2013-05-20 11:28:32 +0100 | [diff] [blame] | 475 | iface->next = daemon->interfaces; |
| 476 | daemon->interfaces = iface; |
Simon Kelley | 6f13e53 | 2012-04-17 14:25:06 +0100 | [diff] [blame] | 477 | return 1; |
| 478 | } |
| 479 | free(iface); |
Simon Kelley | 5d162f2 | 2012-12-20 14:55:46 +0000 | [diff] [blame] | 480 | |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 481 | } |
| 482 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 483 | errno = ENOMEM; |
| 484 | return 0; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 485 | } |
| 486 | |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 487 | static int iface_allowed_v6(struct in6_addr *local, int prefix, |
Simon Kelley | bad7b87 | 2012-12-20 22:00:39 +0000 | [diff] [blame] | 488 | int scope, int if_index, int flags, |
Simon Kelley | 1f77693 | 2012-12-16 19:46:08 +0000 | [diff] [blame] | 489 | int preferred, int valid, void *vparam) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 490 | { |
Simon Kelley | 59353a6 | 2004-11-21 19:34:28 +0000 | [diff] [blame] | 491 | union mysockaddr addr; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 492 | struct in_addr netmask; /* dummy */ |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 493 | netmask.s_addr = 0; |
Simon Kelley | 52b92f4 | 2012-01-22 16:05:15 +0000 | [diff] [blame] | 494 | |
Simon Kelley | 52b92f4 | 2012-01-22 16:05:15 +0000 | [diff] [blame] | 495 | (void)scope; /* warning */ |
Simon Kelley | 1f77693 | 2012-12-16 19:46:08 +0000 | [diff] [blame] | 496 | (void)preferred; |
| 497 | (void)valid; |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 498 | |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 499 | memset(&addr, 0, sizeof(addr)); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 500 | #ifdef HAVE_SOCKADDR_SA_LEN |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 501 | addr.in6.sin6_len = sizeof(addr.in6); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 502 | #endif |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 503 | addr.in6.sin6_family = AF_INET6; |
| 504 | addr.in6.sin6_addr = *local; |
| 505 | addr.in6.sin6_port = htons(daemon->port); |
Simon Kelley | 62ab3cc | 2013-12-03 11:53:53 +0000 | [diff] [blame] | 506 | /* FreeBSD insists this is zero for non-linklocal addresses */ |
| 507 | if (IN6_IS_ADDR_LINKLOCAL(local)) |
| 508 | addr.in6.sin6_scope_id = if_index; |
| 509 | else |
| 510 | addr.in6.sin6_scope_id = 0; |
Simon Kelley | 1ee9be4 | 2013-12-09 16:50:19 +0000 | [diff] [blame] | 511 | |
Simon Kelley | 4766936 | 2014-12-17 12:41:56 +0000 | [diff] [blame] | 512 | return iface_allowed((struct iface_param *)vparam, if_index, NULL, &addr, netmask, prefix, flags); |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 513 | } |
Simon Kelley | 59353a6 | 2004-11-21 19:34:28 +0000 | [diff] [blame] | 514 | |
Simon Kelley | 3f2873d | 2013-05-14 11:28:47 +0100 | [diff] [blame] | 515 | static int iface_allowed_v4(struct in_addr local, int if_index, char *label, |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 516 | struct in_addr netmask, struct in_addr broadcast, void *vparam) |
| 517 | { |
| 518 | union mysockaddr addr; |
Simon Kelley | 376d48c | 2013-11-13 13:04:30 +0000 | [diff] [blame] | 519 | int prefix, bit; |
Simon Kelley | b8ac466 | 2016-03-10 18:40:53 +0000 | [diff] [blame] | 520 | |
| 521 | (void)broadcast; /* warning */ |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 522 | |
| 523 | memset(&addr, 0, sizeof(addr)); |
Simon Kelley | 1ab84e2 | 2004-01-29 16:48:35 +0000 | [diff] [blame] | 524 | #ifdef HAVE_SOCKADDR_SA_LEN |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 525 | addr.in.sin_len = sizeof(addr.in); |
Simon Kelley | 1ab84e2 | 2004-01-29 16:48:35 +0000 | [diff] [blame] | 526 | #endif |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 527 | addr.in.sin_family = AF_INET; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 528 | addr.in.sin_addr = local; |
| 529 | addr.in.sin_port = htons(daemon->port); |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 530 | |
Simon Kelley | 376d48c | 2013-11-13 13:04:30 +0000 | [diff] [blame] | 531 | /* determine prefix length from netmask */ |
| 532 | for (prefix = 32, bit = 1; (bit & ntohl(netmask.s_addr)) == 0 && prefix != 0; bit = bit << 1, prefix--); |
| 533 | |
| 534 | return iface_allowed((struct iface_param *)vparam, if_index, label, &addr, netmask, prefix, 0); |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 535 | } |
Petr Mensik | 60a3ae1 | 2019-07-09 14:05:59 +0200 | [diff] [blame] | 536 | |
| 537 | /* |
| 538 | * Clean old interfaces no longer found. |
| 539 | */ |
| 540 | static void clean_interfaces() |
| 541 | { |
| 542 | struct irec *iface; |
| 543 | struct irec **up = &daemon->interfaces; |
| 544 | |
| 545 | for (iface = *up; iface; iface = *up) |
| 546 | { |
| 547 | if (!iface->found && !iface->done) |
| 548 | { |
| 549 | *up = iface->next; |
| 550 | free(iface->name); |
| 551 | free(iface); |
| 552 | } |
| 553 | else |
| 554 | { |
| 555 | up = &iface->next; |
| 556 | } |
| 557 | } |
| 558 | } |
| 559 | |
Petr Menšík | 49bdf1e | 2019-07-15 17:13:12 +0200 | [diff] [blame] | 560 | /** Release listener if no other interface needs it. |
| 561 | * |
| 562 | * @return 1 if released, 0 if still required |
| 563 | */ |
| 564 | static int release_listener(struct listener *l) |
| 565 | { |
| 566 | if (l->used > 1) |
| 567 | { |
| 568 | struct irec *iface; |
| 569 | for (iface = daemon->interfaces; iface; iface = iface->next) |
| 570 | if (iface->done && sockaddr_isequal(&l->addr, &iface->addr)) |
| 571 | { |
| 572 | if (iface->found) |
| 573 | { |
| 574 | /* update listener to point to active interface instead */ |
| 575 | if (!l->iface->found) |
| 576 | l->iface = iface; |
| 577 | } |
| 578 | else |
| 579 | { |
| 580 | l->used--; |
| 581 | iface->done = 0; |
| 582 | } |
| 583 | } |
| 584 | |
| 585 | /* Someone is still using this listener, skip its deletion */ |
| 586 | if (l->used > 0) |
| 587 | return 0; |
| 588 | } |
| 589 | |
| 590 | if (l->iface->done) |
| 591 | { |
| 592 | int port; |
| 593 | |
| 594 | port = prettyprint_addr(&l->iface->addr, daemon->addrbuff); |
| 595 | my_syslog(LOG_DEBUG, _("stopped listening on %s(#%d): %s port %d"), |
| 596 | l->iface->name, l->iface->index, daemon->addrbuff, port); |
| 597 | /* In case it ever returns */ |
| 598 | l->iface->done = 0; |
| 599 | } |
| 600 | |
| 601 | if (l->fd != -1) |
| 602 | close(l->fd); |
| 603 | if (l->tcpfd != -1) |
| 604 | close(l->tcpfd); |
| 605 | if (l->tftpfd != -1) |
| 606 | close(l->tftpfd); |
| 607 | |
| 608 | free(l); |
| 609 | return 1; |
| 610 | } |
| 611 | |
Simon Kelley | 115ac3e | 2013-05-20 11:28:32 +0100 | [diff] [blame] | 612 | int enumerate_interfaces(int reset) |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 613 | { |
Simon Kelley | 115ac3e | 2013-05-20 11:28:32 +0100 | [diff] [blame] | 614 | static struct addrlist *spare = NULL; |
Simon Kelley | a0358e5 | 2014-06-07 13:38:48 +0100 | [diff] [blame] | 615 | static int done = 0; |
Simon Kelley | 115ac3e | 2013-05-20 11:28:32 +0100 | [diff] [blame] | 616 | struct iface_param param; |
| 617 | int errsave, ret = 1; |
| 618 | struct addrlist *addr, *tmp; |
| 619 | struct interface_name *intname; |
Simon Kelley | 0861921 | 2013-12-02 14:43:48 +0000 | [diff] [blame] | 620 | struct irec *iface; |
Simon Kelley | 376d48c | 2013-11-13 13:04:30 +0000 | [diff] [blame] | 621 | #ifdef HAVE_AUTH |
| 622 | struct auth_zone *zone; |
| 623 | #endif |
| 624 | |
Simon Kelley | 76dd75d | 2013-05-23 10:04:25 +0100 | [diff] [blame] | 625 | /* Do this max once per select cycle - also inhibits netlink socket use |
| 626 | in TCP child processes. */ |
Simon Kelley | 397542b | 2013-09-05 11:27:34 +0100 | [diff] [blame] | 627 | |
Simon Kelley | 115ac3e | 2013-05-20 11:28:32 +0100 | [diff] [blame] | 628 | if (reset) |
| 629 | { |
| 630 | done = 0; |
| 631 | return 1; |
| 632 | } |
| 633 | |
Simon Kelley | a0358e5 | 2014-06-07 13:38:48 +0100 | [diff] [blame] | 634 | if (done) |
Simon Kelley | 115ac3e | 2013-05-20 11:28:32 +0100 | [diff] [blame] | 635 | return 1; |
| 636 | |
| 637 | done = 1; |
| 638 | |
| 639 | if ((param.fd = socket(PF_INET, SOCK_DGRAM, 0)) == -1) |
| 640 | return 0; |
Petr Menšík | 4c0aecc | 2021-03-02 18:21:32 +0000 | [diff] [blame^] | 641 | |
| 642 | again: |
Simon Kelley | 0861921 | 2013-12-02 14:43:48 +0000 | [diff] [blame] | 643 | /* Mark interfaces for garbage collection */ |
| 644 | for (iface = daemon->interfaces; iface; iface = iface->next) |
| 645 | iface->found = 0; |
| 646 | |
Simon Kelley | 115ac3e | 2013-05-20 11:28:32 +0100 | [diff] [blame] | 647 | /* remove addresses stored against interface_names */ |
| 648 | for (intname = daemon->int_names; intname; intname = intname->next) |
| 649 | { |
Simon Kelley | 376d48c | 2013-11-13 13:04:30 +0000 | [diff] [blame] | 650 | for (addr = intname->addr; addr; addr = tmp) |
Simon Kelley | 115ac3e | 2013-05-20 11:28:32 +0100 | [diff] [blame] | 651 | { |
| 652 | tmp = addr->next; |
| 653 | addr->next = spare; |
| 654 | spare = addr; |
| 655 | } |
| 656 | |
Simon Kelley | 376d48c | 2013-11-13 13:04:30 +0000 | [diff] [blame] | 657 | intname->addr = NULL; |
Simon Kelley | 115ac3e | 2013-05-20 11:28:32 +0100 | [diff] [blame] | 658 | } |
Simon Kelley | 376d48c | 2013-11-13 13:04:30 +0000 | [diff] [blame] | 659 | |
Simon Kelley | c8a8048 | 2014-03-05 14:29:54 +0000 | [diff] [blame] | 660 | /* Remove list of addresses of local interfaces */ |
| 661 | for (addr = daemon->interface_addrs; addr; addr = tmp) |
| 662 | { |
| 663 | tmp = addr->next; |
| 664 | addr->next = spare; |
| 665 | spare = addr; |
| 666 | } |
| 667 | daemon->interface_addrs = NULL; |
| 668 | |
Simon Kelley | 376d48c | 2013-11-13 13:04:30 +0000 | [diff] [blame] | 669 | #ifdef HAVE_AUTH |
| 670 | /* remove addresses stored against auth_zone subnets, but not |
| 671 | ones configured as address literals */ |
| 672 | for (zone = daemon->auth_zones; zone; zone = zone->next) |
| 673 | if (zone->interface_names) |
| 674 | { |
| 675 | struct addrlist **up; |
| 676 | for (up = &zone->subnet, addr = zone->subnet; addr; addr = tmp) |
| 677 | { |
| 678 | tmp = addr->next; |
| 679 | if (addr->flags & ADDRLIST_LITERAL) |
| 680 | up = &addr->next; |
| 681 | else |
| 682 | { |
| 683 | *up = addr->next; |
| 684 | addr->next = spare; |
| 685 | spare = addr; |
| 686 | } |
| 687 | } |
| 688 | } |
| 689 | #endif |
| 690 | |
Simon Kelley | 115ac3e | 2013-05-20 11:28:32 +0100 | [diff] [blame] | 691 | param.spare = spare; |
| 692 | |
Simon Kelley | 115ac3e | 2013-05-20 11:28:32 +0100 | [diff] [blame] | 693 | ret = iface_enumerate(AF_INET6, ¶m, iface_allowed_v6); |
Petr Menšík | 4c0aecc | 2021-03-02 18:21:32 +0000 | [diff] [blame^] | 694 | if (ret < 0) |
| 695 | goto again; |
| 696 | else if (ret) |
| 697 | { |
| 698 | ret = iface_enumerate(AF_INET, ¶m, iface_allowed_v4); |
| 699 | if (ret < 0) |
| 700 | goto again; |
| 701 | } |
Simon Kelley | 115ac3e | 2013-05-20 11:28:32 +0100 | [diff] [blame] | 702 | |
| 703 | errsave = errno; |
| 704 | close(param.fd); |
Simon Kelley | 0861921 | 2013-12-02 14:43:48 +0000 | [diff] [blame] | 705 | |
| 706 | if (option_bool(OPT_CLEVERBIND)) |
| 707 | { |
| 708 | /* Garbage-collect listeners listening on addresses that no longer exist. |
| 709 | Does nothing when not binding interfaces or for listeners on localhost, |
| 710 | since the ->iface field is NULL. Note that this needs the protections |
Josh Soref | 730c674 | 2017-02-06 16:14:04 +0000 | [diff] [blame] | 711 | against reentrancy, hence it's here. It also means there's a possibility, |
Simon Kelley | 0861921 | 2013-12-02 14:43:48 +0000 | [diff] [blame] | 712 | in OPT_CLEVERBIND mode, that at listener will just disappear after |
| 713 | a call to enumerate_interfaces, this is checked OK on all calls. */ |
| 714 | struct listener *l, *tmp, **up; |
Petr Mensik | 60a3ae1 | 2019-07-09 14:05:59 +0200 | [diff] [blame] | 715 | int freed = 0; |
Simon Kelley | 0861921 | 2013-12-02 14:43:48 +0000 | [diff] [blame] | 716 | |
| 717 | for (up = &daemon->listeners, l = daemon->listeners; l; l = tmp) |
| 718 | { |
| 719 | tmp = l->next; |
| 720 | |
| 721 | if (!l->iface || l->iface->found) |
| 722 | up = &l->next; |
Petr Menšík | 49bdf1e | 2019-07-15 17:13:12 +0200 | [diff] [blame] | 723 | else if (release_listener(l)) |
Simon Kelley | 0861921 | 2013-12-02 14:43:48 +0000 | [diff] [blame] | 724 | { |
Petr Menšík | 49bdf1e | 2019-07-15 17:13:12 +0200 | [diff] [blame] | 725 | *up = tmp; |
Simon Kelley | 619000a | 2020-04-29 00:09:58 +0100 | [diff] [blame] | 726 | freed = 1; |
Simon Kelley | 0861921 | 2013-12-02 14:43:48 +0000 | [diff] [blame] | 727 | } |
| 728 | } |
Petr Mensik | 60a3ae1 | 2019-07-09 14:05:59 +0200 | [diff] [blame] | 729 | |
| 730 | if (freed) |
| 731 | clean_interfaces(); |
Simon Kelley | 0861921 | 2013-12-02 14:43:48 +0000 | [diff] [blame] | 732 | } |
Petr Mensik | 60a3ae1 | 2019-07-09 14:05:59 +0200 | [diff] [blame] | 733 | |
Simon Kelley | 115ac3e | 2013-05-20 11:28:32 +0100 | [diff] [blame] | 734 | errno = errsave; |
Simon Kelley | 115ac3e | 2013-05-20 11:28:32 +0100 | [diff] [blame] | 735 | spare = param.spare; |
Simon Kelley | a0358e5 | 2014-06-07 13:38:48 +0100 | [diff] [blame] | 736 | |
Simon Kelley | 115ac3e | 2013-05-20 11:28:32 +0100 | [diff] [blame] | 737 | return ret; |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 738 | } |
| 739 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 740 | /* set NONBLOCK bit on fd: See Stevens 16.6 */ |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 741 | int fix_fd(int fd) |
| 742 | { |
| 743 | int flags; |
| 744 | |
| 745 | if ((flags = fcntl(fd, F_GETFL)) == -1 || |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 746 | fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 747 | return 0; |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 748 | |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 749 | return 1; |
| 750 | } |
| 751 | |
Simon Kelley | 74c95c2 | 2011-10-19 09:33:39 +0100 | [diff] [blame] | 752 | static int make_sock(union mysockaddr *addr, int type, int dienow) |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 753 | { |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 754 | int family = addr->sa.sa_family; |
| 755 | int fd, rc, opt = 1; |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 756 | |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 757 | if ((fd = socket(family, type, 0)) == -1) |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 758 | { |
Josh Soref | 730c674 | 2017-02-06 16:14:04 +0000 | [diff] [blame] | 759 | int port, errsave; |
Simon Kelley | 39f1b8e | 2012-06-20 20:04:27 +0100 | [diff] [blame] | 760 | char *s; |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 761 | |
| 762 | /* No error if the kernel just doesn't support this IP flavour */ |
| 763 | if (errno == EPROTONOSUPPORT || |
| 764 | errno == EAFNOSUPPORT || |
| 765 | errno == EINVAL) |
| 766 | return -1; |
| 767 | |
| 768 | err: |
Josh Soref | 730c674 | 2017-02-06 16:14:04 +0000 | [diff] [blame] | 769 | errsave = errno; |
Simon Kelley | 39f1b8e | 2012-06-20 20:04:27 +0100 | [diff] [blame] | 770 | port = prettyprint_addr(addr, daemon->addrbuff); |
| 771 | if (!option_bool(OPT_NOWILD) && !option_bool(OPT_CLEVERBIND)) |
| 772 | sprintf(daemon->addrbuff, "port %d", port); |
| 773 | s = _("failed to create listening socket for %s: %s"); |
| 774 | |
Simon Kelley | 5f11b3e | 2012-08-16 14:04:05 +0100 | [diff] [blame] | 775 | if (fd != -1) |
Matthias Andree | 71aaa5a | 2013-12-03 11:20:45 +0000 | [diff] [blame] | 776 | close (fd); |
| 777 | |
Josh Soref | 730c674 | 2017-02-06 16:14:04 +0000 | [diff] [blame] | 778 | errno = errsave; |
Simon Kelley | 1ee9be4 | 2013-12-09 16:50:19 +0000 | [diff] [blame] | 779 | |
Simon Kelley | 5f11b3e | 2012-08-16 14:04:05 +0100 | [diff] [blame] | 780 | if (dienow) |
| 781 | { |
| 782 | /* failure to bind addresses given by --listen-address at this point |
| 783 | is OK if we're doing bind-dynamic */ |
| 784 | if (!option_bool(OPT_CLEVERBIND)) |
| 785 | die(s, daemon->addrbuff, EC_BADNET); |
| 786 | } |
| 787 | else |
| 788 | my_syslog(LOG_WARNING, s, daemon->addrbuff, strerror(errno)); |
| 789 | |
Simon Kelley | 74c95c2 | 2011-10-19 09:33:39 +0100 | [diff] [blame] | 790 | return -1; |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 791 | } |
Simon Kelley | 5f11b3e | 2012-08-16 14:04:05 +0100 | [diff] [blame] | 792 | |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 793 | if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) == -1 || !fix_fd(fd)) |
| 794 | goto err; |
| 795 | |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 796 | if (family == AF_INET6 && setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &opt, sizeof(opt)) == -1) |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 797 | goto err; |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 798 | |
Simon Kelley | 74c95c2 | 2011-10-19 09:33:39 +0100 | [diff] [blame] | 799 | if ((rc = bind(fd, (struct sockaddr *)addr, sa_len(addr))) == -1) |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 800 | goto err; |
| 801 | |
| 802 | if (type == SOCK_STREAM) |
| 803 | { |
Simon Kelley | 608aa9f | 2019-03-10 22:44:15 +0000 | [diff] [blame] | 804 | #ifdef TCP_FASTOPEN |
| 805 | int qlen = 5; |
Matthias Andree | e39c484 | 2020-03-05 15:58:31 +0000 | [diff] [blame] | 806 | setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &qlen, sizeof(qlen)); |
Simon Kelley | 608aa9f | 2019-03-10 22:44:15 +0000 | [diff] [blame] | 807 | #endif |
| 808 | |
Simon Kelley | 09b768e | 2016-12-22 22:16:58 +0000 | [diff] [blame] | 809 | if (listen(fd, TCP_BACKLOG) == -1) |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 810 | goto err; |
| 811 | } |
Simon Kelley | 2329bef | 2013-12-03 13:41:16 +0000 | [diff] [blame] | 812 | else if (family == AF_INET) |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 813 | { |
Simon Kelley | 2329bef | 2013-12-03 13:41:16 +0000 | [diff] [blame] | 814 | if (!option_bool(OPT_NOWILD)) |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 815 | { |
| 816 | #if defined(HAVE_LINUX_NETWORK) |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 817 | if (setsockopt(fd, IPPROTO_IP, IP_PKTINFO, &opt, sizeof(opt)) == -1) |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 818 | goto err; |
| 819 | #elif defined(IP_RECVDSTADDR) && defined(IP_RECVIF) |
| 820 | if (setsockopt(fd, IPPROTO_IP, IP_RECVDSTADDR, &opt, sizeof(opt)) == -1 || |
| 821 | setsockopt(fd, IPPROTO_IP, IP_RECVIF, &opt, sizeof(opt)) == -1) |
| 822 | goto err; |
| 823 | #endif |
| 824 | } |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 825 | } |
Simon Kelley | 2329bef | 2013-12-03 13:41:16 +0000 | [diff] [blame] | 826 | else if (!set_ipv6pktinfo(fd)) |
| 827 | goto err; |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 828 | |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 829 | return fd; |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 830 | } |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 831 | |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 832 | int set_ipv6pktinfo(int fd) |
| 833 | { |
| 834 | int opt = 1; |
| 835 | |
| 836 | /* The API changed around Linux 2.6.14 but the old ABI is still supported: |
| 837 | handle all combinations of headers and kernel. |
| 838 | OpenWrt note that this fixes the problem addressed by your very broken patch. */ |
| 839 | daemon->v6pktinfo = IPV6_PKTINFO; |
| 840 | |
| 841 | #ifdef IPV6_RECVPKTINFO |
| 842 | if (setsockopt(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &opt, sizeof(opt)) != -1) |
| 843 | return 1; |
| 844 | # ifdef IPV6_2292PKTINFO |
| 845 | else if (errno == ENOPROTOOPT && setsockopt(fd, IPPROTO_IPV6, IPV6_2292PKTINFO, &opt, sizeof(opt)) != -1) |
| 846 | { |
| 847 | daemon->v6pktinfo = IPV6_2292PKTINFO; |
| 848 | return 1; |
| 849 | } |
| 850 | # endif |
| 851 | #else |
| 852 | if (setsockopt(fd, IPPROTO_IPV6, IPV6_PKTINFO, &opt, sizeof(opt)) != -1) |
| 853 | return 1; |
| 854 | #endif |
| 855 | |
| 856 | return 0; |
| 857 | } |
Simon Kelley | 22ce550 | 2013-01-22 13:53:04 +0000 | [diff] [blame] | 858 | |
| 859 | |
| 860 | /* Find the interface on which a TCP connection arrived, if possible, or zero otherwise. */ |
| 861 | int tcp_interface(int fd, int af) |
| 862 | { |
Kevin Darbyshire-Bryant | 70c50ef | 2020-03-06 10:31:15 +0000 | [diff] [blame] | 863 | (void)fd; /* suppress potential unused warning */ |
| 864 | (void)af; /* suppress potential unused warning */ |
Simon Kelley | 22ce550 | 2013-01-22 13:53:04 +0000 | [diff] [blame] | 865 | int if_index = 0; |
| 866 | |
| 867 | #ifdef HAVE_LINUX_NETWORK |
| 868 | int opt = 1; |
| 869 | struct cmsghdr *cmptr; |
| 870 | struct msghdr msg; |
Simon Kelley | 529b030 | 2016-03-16 19:00:45 +0000 | [diff] [blame] | 871 | socklen_t len; |
Simon Kelley | 22ce550 | 2013-01-22 13:53:04 +0000 | [diff] [blame] | 872 | |
Simon Kelley | 529b030 | 2016-03-16 19:00:45 +0000 | [diff] [blame] | 873 | /* use mshdr so that the CMSDG_* macros are available */ |
Simon Kelley | 22ce550 | 2013-01-22 13:53:04 +0000 | [diff] [blame] | 874 | msg.msg_control = daemon->packet; |
Simon Kelley | 529b030 | 2016-03-16 19:00:45 +0000 | [diff] [blame] | 875 | msg.msg_controllen = len = daemon->packet_buff_sz; |
Simon Kelley | 22ce550 | 2013-01-22 13:53:04 +0000 | [diff] [blame] | 876 | |
| 877 | /* we overwrote the buffer... */ |
| 878 | daemon->srv_save = NULL; |
| 879 | |
| 880 | if (af == AF_INET) |
| 881 | { |
| 882 | if (setsockopt(fd, IPPROTO_IP, IP_PKTINFO, &opt, sizeof(opt)) != -1 && |
Simon Kelley | 529b030 | 2016-03-16 19:00:45 +0000 | [diff] [blame] | 883 | getsockopt(fd, IPPROTO_IP, IP_PKTOPTIONS, msg.msg_control, &len) != -1) |
| 884 | { |
| 885 | msg.msg_controllen = len; |
| 886 | for (cmptr = CMSG_FIRSTHDR(&msg); cmptr; cmptr = CMSG_NXTHDR(&msg, cmptr)) |
| 887 | if (cmptr->cmsg_level == IPPROTO_IP && cmptr->cmsg_type == IP_PKTINFO) |
| 888 | { |
| 889 | union { |
| 890 | unsigned char *c; |
| 891 | struct in_pktinfo *p; |
| 892 | } p; |
| 893 | |
| 894 | p.c = CMSG_DATA(cmptr); |
| 895 | if_index = p.p->ipi_ifindex; |
| 896 | } |
| 897 | } |
Simon Kelley | 22ce550 | 2013-01-22 13:53:04 +0000 | [diff] [blame] | 898 | } |
Simon Kelley | 22ce550 | 2013-01-22 13:53:04 +0000 | [diff] [blame] | 899 | else |
| 900 | { |
| 901 | /* Only the RFC-2292 API has the ability to find the interface for TCP connections, |
| 902 | it was removed in RFC-3542 !!!! |
| 903 | |
| 904 | Fortunately, Linux kept the 2292 ABI when it moved to 3542. The following code always |
| 905 | uses the old ABI, and should work with pre- and post-3542 kernel headers */ |
| 906 | |
| 907 | #ifdef IPV6_2292PKTOPTIONS |
| 908 | # define PKTOPTIONS IPV6_2292PKTOPTIONS |
| 909 | #else |
| 910 | # define PKTOPTIONS IPV6_PKTOPTIONS |
| 911 | #endif |
| 912 | |
| 913 | if (set_ipv6pktinfo(fd) && |
Simon Kelley | 529b030 | 2016-03-16 19:00:45 +0000 | [diff] [blame] | 914 | getsockopt(fd, IPPROTO_IPV6, PKTOPTIONS, msg.msg_control, &len) != -1) |
Simon Kelley | 22ce550 | 2013-01-22 13:53:04 +0000 | [diff] [blame] | 915 | { |
Simon Kelley | 529b030 | 2016-03-16 19:00:45 +0000 | [diff] [blame] | 916 | msg.msg_controllen = len; |
| 917 | for (cmptr = CMSG_FIRSTHDR(&msg); cmptr; cmptr = CMSG_NXTHDR(&msg, cmptr)) |
Simon Kelley | 22ce550 | 2013-01-22 13:53:04 +0000 | [diff] [blame] | 918 | if (cmptr->cmsg_level == IPPROTO_IPV6 && cmptr->cmsg_type == daemon->v6pktinfo) |
| 919 | { |
| 920 | union { |
| 921 | unsigned char *c; |
| 922 | struct in6_pktinfo *p; |
| 923 | } p; |
| 924 | p.c = CMSG_DATA(cmptr); |
| 925 | |
| 926 | if_index = p.p->ipi6_ifindex; |
| 927 | } |
| 928 | } |
| 929 | } |
Simon Kelley | 22ce550 | 2013-01-22 13:53:04 +0000 | [diff] [blame] | 930 | #endif /* Linux */ |
| 931 | |
| 932 | return if_index; |
| 933 | } |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 934 | |
Simon Kelley | 74c95c2 | 2011-10-19 09:33:39 +0100 | [diff] [blame] | 935 | static struct listener *create_listeners(union mysockaddr *addr, int do_tftp, int dienow) |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 936 | { |
| 937 | struct listener *l = NULL; |
| 938 | int fd = -1, tcpfd = -1, tftpfd = -1; |
| 939 | |
Vladislav Grishenko | 408c368 | 2013-09-24 16:18:49 +0100 | [diff] [blame] | 940 | (void)do_tftp; |
| 941 | |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 942 | if (daemon->port != 0) |
| 943 | { |
Simon Kelley | 74c95c2 | 2011-10-19 09:33:39 +0100 | [diff] [blame] | 944 | fd = make_sock(addr, SOCK_DGRAM, dienow); |
| 945 | tcpfd = make_sock(addr, SOCK_STREAM, dienow); |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 946 | } |
| 947 | |
| 948 | #ifdef HAVE_TFTP |
| 949 | if (do_tftp) |
| 950 | { |
| 951 | if (addr->sa.sa_family == AF_INET) |
| 952 | { |
| 953 | /* port must be restored to DNS port for TCP code */ |
| 954 | short save = addr->in.sin_port; |
| 955 | addr->in.sin_port = htons(TFTP_PORT); |
Simon Kelley | 74c95c2 | 2011-10-19 09:33:39 +0100 | [diff] [blame] | 956 | tftpfd = make_sock(addr, SOCK_DGRAM, dienow); |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 957 | addr->in.sin_port = save; |
| 958 | } |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 959 | else |
| 960 | { |
| 961 | short save = addr->in6.sin6_port; |
| 962 | addr->in6.sin6_port = htons(TFTP_PORT); |
Simon Kelley | 74c95c2 | 2011-10-19 09:33:39 +0100 | [diff] [blame] | 963 | tftpfd = make_sock(addr, SOCK_DGRAM, dienow); |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 964 | addr->in6.sin6_port = save; |
| 965 | } |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 966 | } |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 967 | #endif |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 968 | |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 969 | if (fd != -1 || tcpfd != -1 || tftpfd != -1) |
| 970 | { |
| 971 | l = safe_malloc(sizeof(struct listener)); |
| 972 | l->next = NULL; |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 973 | l->fd = fd; |
| 974 | l->tcpfd = tcpfd; |
Petr Menšík | 49bdf1e | 2019-07-15 17:13:12 +0200 | [diff] [blame] | 975 | l->tftpfd = tftpfd; |
| 976 | l->addr = *addr; |
| 977 | l->used = 1; |
Simon Kelley | 0861921 | 2013-12-02 14:43:48 +0000 | [diff] [blame] | 978 | l->iface = NULL; |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 979 | } |
| 980 | |
| 981 | return l; |
| 982 | } |
| 983 | |
Simon Kelley | 74c95c2 | 2011-10-19 09:33:39 +0100 | [diff] [blame] | 984 | void create_wildcard_listeners(void) |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 985 | { |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 986 | union mysockaddr addr; |
Simon Kelley | 8bc4cec | 2012-07-03 21:04:11 +0100 | [diff] [blame] | 987 | struct listener *l, *l6; |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 988 | |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 989 | memset(&addr, 0, sizeof(addr)); |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 990 | #ifdef HAVE_SOCKADDR_SA_LEN |
| 991 | addr.in.sin_len = sizeof(addr.in); |
| 992 | #endif |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 993 | addr.in.sin_family = AF_INET; |
| 994 | addr.in.sin_addr.s_addr = INADDR_ANY; |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 995 | addr.in.sin_port = htons(daemon->port); |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 996 | |
Simon Kelley | 8bc4cec | 2012-07-03 21:04:11 +0100 | [diff] [blame] | 997 | l = create_listeners(&addr, !!option_bool(OPT_TFTP), 1); |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 998 | |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 999 | memset(&addr, 0, sizeof(addr)); |
Simon Kelley | ee87504 | 2018-10-23 22:10:17 +0100 | [diff] [blame] | 1000 | #ifdef HAVE_SOCKADDR_SA_LEN |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 1001 | addr.in6.sin6_len = sizeof(addr.in6); |
Simon Kelley | ee87504 | 2018-10-23 22:10:17 +0100 | [diff] [blame] | 1002 | #endif |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 1003 | addr.in6.sin6_family = AF_INET6; |
| 1004 | addr.in6.sin6_addr = in6addr_any; |
| 1005 | addr.in6.sin6_port = htons(daemon->port); |
Simon Kelley | 8bc4cec | 2012-07-03 21:04:11 +0100 | [diff] [blame] | 1006 | |
| 1007 | l6 = create_listeners(&addr, !!option_bool(OPT_TFTP), 1); |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 1008 | if (l) |
Simon Kelley | 8bc4cec | 2012-07-03 21:04:11 +0100 | [diff] [blame] | 1009 | l->next = l6; |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 1010 | else |
Simon Kelley | 8bc4cec | 2012-07-03 21:04:11 +0100 | [diff] [blame] | 1011 | l = l6; |
Simon Kelley | feba5c1 | 2004-07-27 20:28:58 +0100 | [diff] [blame] | 1012 | |
Simon Kelley | 74c95c2 | 2011-10-19 09:33:39 +0100 | [diff] [blame] | 1013 | daemon->listeners = l; |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 1014 | } |
| 1015 | |
Petr Menšík | 49bdf1e | 2019-07-15 17:13:12 +0200 | [diff] [blame] | 1016 | static struct listener *find_listener(union mysockaddr *addr) |
| 1017 | { |
| 1018 | struct listener *l; |
| 1019 | for (l = daemon->listeners; l; l = l->next) |
| 1020 | if (sockaddr_isequal(&l->addr, addr)) |
| 1021 | return l; |
| 1022 | return NULL; |
| 1023 | } |
| 1024 | |
Simon Kelley | 74c95c2 | 2011-10-19 09:33:39 +0100 | [diff] [blame] | 1025 | void create_bound_listeners(int dienow) |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 1026 | { |
Simon Kelley | 74c95c2 | 2011-10-19 09:33:39 +0100 | [diff] [blame] | 1027 | struct listener *new; |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 1028 | struct irec *iface; |
Simon Kelley | 52d4abf | 2012-03-21 21:39:48 +0000 | [diff] [blame] | 1029 | struct iname *if_tmp; |
Petr Menšík | 49bdf1e | 2019-07-15 17:13:12 +0200 | [diff] [blame] | 1030 | struct listener *existing; |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 1031 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 1032 | for (iface = daemon->interfaces; iface; iface = iface->next) |
Petr Menšík | 49bdf1e | 2019-07-15 17:13:12 +0200 | [diff] [blame] | 1033 | if (!iface->done && !iface->dad && iface->found) |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 1034 | { |
Petr Menšík | 49bdf1e | 2019-07-15 17:13:12 +0200 | [diff] [blame] | 1035 | existing = find_listener(&iface->addr); |
| 1036 | if (existing) |
| 1037 | { |
| 1038 | iface->done = 1; |
| 1039 | existing->used++; /* increase usage counter */ |
| 1040 | } |
| 1041 | else if ((new = create_listeners(&iface->addr, iface->tftp_ok, dienow))) |
| 1042 | { |
Petr Menšík | 49bdf1e | 2019-07-15 17:13:12 +0200 | [diff] [blame] | 1043 | new->iface = iface; |
| 1044 | new->next = daemon->listeners; |
| 1045 | daemon->listeners = new; |
| 1046 | iface->done = 1; |
Simon Kelley | 619000a | 2020-04-29 00:09:58 +0100 | [diff] [blame] | 1047 | |
| 1048 | /* Don't log the initial set of listen addresses created |
| 1049 | at startup, since this is happening before the logging |
| 1050 | system is initialised and the sign-on printed. */ |
| 1051 | if (!dienow) |
| 1052 | { |
| 1053 | int port = prettyprint_addr(&iface->addr, daemon->addrbuff); |
| 1054 | my_syslog(LOG_DEBUG, _("listening on %s(#%d): %s port %d"), |
| 1055 | iface->name, iface->index, daemon->addrbuff, port); |
| 1056 | } |
Petr Menšík | 49bdf1e | 2019-07-15 17:13:12 +0200 | [diff] [blame] | 1057 | } |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 1058 | } |
Simon Kelley | 52d4abf | 2012-03-21 21:39:48 +0000 | [diff] [blame] | 1059 | |
| 1060 | /* Check for --listen-address options that haven't been used because there's |
| 1061 | no interface with a matching address. These may be valid: eg it's possible |
| 1062 | to listen on 127.0.1.1 even if the loopback interface is 127.0.0.1 |
| 1063 | |
Simon Kelley | 5f11b3e | 2012-08-16 14:04:05 +0100 | [diff] [blame] | 1064 | If the address isn't valid the bind() will fail and we'll die() |
| 1065 | (except in bind-dynamic mode, when we'll complain but keep trying.) |
Simon Kelley | 52d4abf | 2012-03-21 21:39:48 +0000 | [diff] [blame] | 1066 | |
| 1067 | The resulting listeners have the ->iface field NULL, and this has to be |
| 1068 | handled by the DNS and TFTP code. It disables --localise-queries processing |
| 1069 | (no netmask) and some MTU login the tftp code. */ |
| 1070 | |
| 1071 | for (if_tmp = daemon->if_addrs; if_tmp; if_tmp = if_tmp->next) |
| 1072 | if (!if_tmp->used && |
Simon Kelley | 8bc4cec | 2012-07-03 21:04:11 +0100 | [diff] [blame] | 1073 | (new = create_listeners(&if_tmp->addr, !!option_bool(OPT_TFTP), dienow))) |
Simon Kelley | 52d4abf | 2012-03-21 21:39:48 +0000 | [diff] [blame] | 1074 | { |
Simon Kelley | 52d4abf | 2012-03-21 21:39:48 +0000 | [diff] [blame] | 1075 | new->next = daemon->listeners; |
| 1076 | daemon->listeners = new; |
Simon Kelley | 619000a | 2020-04-29 00:09:58 +0100 | [diff] [blame] | 1077 | |
| 1078 | if (!dienow) |
| 1079 | { |
| 1080 | int port = prettyprint_addr(&if_tmp->addr, daemon->addrbuff); |
| 1081 | my_syslog(LOG_DEBUG, _("listening on %s port %d"), daemon->addrbuff, port); |
| 1082 | } |
Simon Kelley | 52d4abf | 2012-03-21 21:39:48 +0000 | [diff] [blame] | 1083 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1084 | } |
| 1085 | |
Simon Kelley | dc27e14 | 2013-10-16 13:09:53 +0100 | [diff] [blame] | 1086 | /* In --bind-interfaces, the only access control is the addresses we're listening on. |
| 1087 | There's nothing to avoid a query to the address of an internal interface arriving via |
| 1088 | an external interface where we don't want to accept queries, except that in the usual |
| 1089 | case the addresses of internal interfaces are RFC1918. When bind-interfaces in use, |
| 1090 | and we listen on an address that looks like it's probably globally routeable, shout. |
| 1091 | |
| 1092 | The fix is to use --bind-dynamic, which actually checks the arrival interface too. |
| 1093 | Tough if your platform doesn't support this. |
Simon Kelley | 2329bef | 2013-12-03 13:41:16 +0000 | [diff] [blame] | 1094 | |
| 1095 | Note that checking the arrival interface is supported in the standard IPv6 API and |
| 1096 | always done, so we don't warn about any IPv6 addresses here. |
Simon Kelley | dc27e14 | 2013-10-16 13:09:53 +0100 | [diff] [blame] | 1097 | */ |
| 1098 | |
| 1099 | void warn_bound_listeners(void) |
| 1100 | { |
| 1101 | struct irec *iface; |
| 1102 | int advice = 0; |
| 1103 | |
| 1104 | for (iface = daemon->interfaces; iface; iface = iface->next) |
Simon Kelley | f7029f5 | 2013-11-21 15:09:09 +0000 | [diff] [blame] | 1105 | if (!iface->dns_auth) |
Simon Kelley | dc27e14 | 2013-10-16 13:09:53 +0100 | [diff] [blame] | 1106 | { |
Simon Kelley | dc27e14 | 2013-10-16 13:09:53 +0100 | [diff] [blame] | 1107 | if (iface->addr.sa.sa_family == AF_INET) |
| 1108 | { |
| 1109 | if (!private_net(iface->addr.in.sin_addr, 1)) |
| 1110 | { |
| 1111 | inet_ntop(AF_INET, &iface->addr.in.sin_addr, daemon->addrbuff, ADDRSTRLEN); |
Simon Kelley | 2329bef | 2013-12-03 13:41:16 +0000 | [diff] [blame] | 1112 | iface->warned = advice = 1; |
| 1113 | my_syslog(LOG_WARNING, |
| 1114 | _("LOUD WARNING: listening on %s may accept requests via interfaces other than %s"), |
| 1115 | daemon->addrbuff, iface->name); |
Simon Kelley | dc27e14 | 2013-10-16 13:09:53 +0100 | [diff] [blame] | 1116 | } |
| 1117 | } |
Simon Kelley | dc27e14 | 2013-10-16 13:09:53 +0100 | [diff] [blame] | 1118 | } |
| 1119 | |
| 1120 | if (advice) |
Simon Kelley | f7029f5 | 2013-11-21 15:09:09 +0000 | [diff] [blame] | 1121 | my_syslog(LOG_WARNING, _("LOUD WARNING: use --bind-dynamic rather than --bind-interfaces to avoid DNS amplification attacks via these interface(s)")); |
Simon Kelley | dc27e14 | 2013-10-16 13:09:53 +0100 | [diff] [blame] | 1122 | } |
| 1123 | |
Petr Menšík | ad59f27 | 2017-03-17 17:22:19 +0000 | [diff] [blame] | 1124 | void warn_wild_labels(void) |
| 1125 | { |
| 1126 | struct irec *iface; |
| 1127 | |
| 1128 | for (iface = daemon->interfaces; iface; iface = iface->next) |
| 1129 | if (iface->found && iface->name && iface->label) |
| 1130 | my_syslog(LOG_WARNING, _("warning: using interface %s instead"), iface->name); |
| 1131 | } |
| 1132 | |
Simon Kelley | f7029f5 | 2013-11-21 15:09:09 +0000 | [diff] [blame] | 1133 | void warn_int_names(void) |
| 1134 | { |
| 1135 | struct interface_name *intname; |
| 1136 | |
| 1137 | for (intname = daemon->int_names; intname; intname = intname->next) |
| 1138 | if (!intname->addr) |
| 1139 | my_syslog(LOG_WARNING, _("warning: no addresses found for interface %s"), intname->intr); |
| 1140 | } |
| 1141 | |
Simon Kelley | 74c95c2 | 2011-10-19 09:33:39 +0100 | [diff] [blame] | 1142 | int is_dad_listeners(void) |
| 1143 | { |
| 1144 | struct irec *iface; |
| 1145 | |
| 1146 | if (option_bool(OPT_NOWILD)) |
| 1147 | for (iface = daemon->interfaces; iface; iface = iface->next) |
| 1148 | if (iface->dad && !iface->done) |
| 1149 | return 1; |
| 1150 | |
| 1151 | return 0; |
| 1152 | } |
Simon Kelley | 5d162f2 | 2012-12-20 14:55:46 +0000 | [diff] [blame] | 1153 | |
| 1154 | #ifdef HAVE_DHCP6 |
| 1155 | void join_multicast(int dienow) |
| 1156 | { |
| 1157 | struct irec *iface, *tmp; |
| 1158 | |
| 1159 | for (iface = daemon->interfaces; iface; iface = iface->next) |
Simon Kelley | 1962446 | 2012-12-28 11:18:09 +0000 | [diff] [blame] | 1160 | if (iface->addr.sa.sa_family == AF_INET6 && iface->dhcp_ok && !iface->multicast_done) |
Simon Kelley | 5d162f2 | 2012-12-20 14:55:46 +0000 | [diff] [blame] | 1161 | { |
| 1162 | /* There's an irec per address but we only want to join for multicast |
| 1163 | once per interface. Weed out duplicates. */ |
| 1164 | for (tmp = daemon->interfaces; tmp; tmp = tmp->next) |
| 1165 | if (tmp->multicast_done && tmp->index == iface->index) |
| 1166 | break; |
| 1167 | |
| 1168 | iface->multicast_done = 1; |
| 1169 | |
| 1170 | if (!tmp) |
| 1171 | { |
| 1172 | struct ipv6_mreq mreq; |
| 1173 | int err = 0; |
| 1174 | |
| 1175 | mreq.ipv6mr_interface = iface->index; |
| 1176 | |
| 1177 | inet_pton(AF_INET6, ALL_RELAY_AGENTS_AND_SERVERS, &mreq.ipv6mr_multiaddr); |
| 1178 | |
Simon Kelley | ff7eea2 | 2013-09-04 18:01:38 +0100 | [diff] [blame] | 1179 | if ((daemon->doing_dhcp6 || daemon->relay6) && |
Simon Kelley | 5d162f2 | 2012-12-20 14:55:46 +0000 | [diff] [blame] | 1180 | setsockopt(daemon->dhcp6fd, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mreq, sizeof(mreq)) == -1) |
Simon Kelley | 9cdcfe9 | 2015-08-26 22:38:08 +0100 | [diff] [blame] | 1181 | err = errno; |
Simon Kelley | 5d162f2 | 2012-12-20 14:55:46 +0000 | [diff] [blame] | 1182 | |
| 1183 | inet_pton(AF_INET6, ALL_SERVERS, &mreq.ipv6mr_multiaddr); |
| 1184 | |
| 1185 | if (daemon->doing_dhcp6 && |
| 1186 | setsockopt(daemon->dhcp6fd, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mreq, sizeof(mreq)) == -1) |
Simon Kelley | 9cdcfe9 | 2015-08-26 22:38:08 +0100 | [diff] [blame] | 1187 | err = errno; |
Simon Kelley | 5d162f2 | 2012-12-20 14:55:46 +0000 | [diff] [blame] | 1188 | |
| 1189 | inet_pton(AF_INET6, ALL_ROUTERS, &mreq.ipv6mr_multiaddr); |
| 1190 | |
| 1191 | if (daemon->doing_ra && |
| 1192 | setsockopt(daemon->icmp6fd, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mreq, sizeof(mreq)) == -1) |
Simon Kelley | 9cdcfe9 | 2015-08-26 22:38:08 +0100 | [diff] [blame] | 1193 | err = errno; |
Simon Kelley | 5d162f2 | 2012-12-20 14:55:46 +0000 | [diff] [blame] | 1194 | |
| 1195 | if (err) |
| 1196 | { |
| 1197 | char *s = _("interface %s failed to join DHCPv6 multicast group: %s"); |
Simon Kelley | 9cdcfe9 | 2015-08-26 22:38:08 +0100 | [diff] [blame] | 1198 | errno = err; |
| 1199 | |
| 1200 | #ifdef HAVE_LINUX_NETWORK |
| 1201 | if (errno == ENOMEM) |
| 1202 | my_syslog(LOG_ERR, _("try increasing /proc/sys/net/core/optmem_max")); |
| 1203 | #endif |
| 1204 | |
Simon Kelley | 5d162f2 | 2012-12-20 14:55:46 +0000 | [diff] [blame] | 1205 | if (dienow) |
| 1206 | die(s, iface->name, EC_BADNET); |
| 1207 | else |
| 1208 | my_syslog(LOG_ERR, s, iface->name, strerror(errno)); |
| 1209 | } |
| 1210 | } |
| 1211 | } |
| 1212 | } |
| 1213 | #endif |
| 1214 | |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1215 | /* return a UDP socket bound to a random port, have to cope with straying into |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1216 | occupied port nos and reserved ones. */ |
| 1217 | int random_sock(int family) |
| 1218 | { |
| 1219 | int fd; |
| 1220 | |
| 1221 | if ((fd = socket(family, SOCK_DGRAM, 0)) != -1) |
| 1222 | { |
Simon Kelley | 3927da4 | 2008-07-20 15:10:39 +0100 | [diff] [blame] | 1223 | union mysockaddr addr; |
Hans Dedecker | 926332a | 2016-01-23 10:48:12 +0000 | [diff] [blame] | 1224 | unsigned int ports_avail = ((unsigned short)daemon->max_port - (unsigned short)daemon->min_port) + 1; |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1225 | int tries = ports_avail < 30 ? 3 * ports_avail : 100; |
Simon Kelley | 3927da4 | 2008-07-20 15:10:39 +0100 | [diff] [blame] | 1226 | |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1227 | memset(&addr, 0, sizeof(addr)); |
Simon Kelley | 3927da4 | 2008-07-20 15:10:39 +0100 | [diff] [blame] | 1228 | addr.sa.sa_family = family; |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1229 | |
Simon Kelley | 1ad24ae | 2008-07-20 20:22:50 +0100 | [diff] [blame] | 1230 | /* don't loop forever if all ports in use. */ |
| 1231 | |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1232 | if (fix_fd(fd)) |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1233 | while(tries--) |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1234 | { |
Simon Kelley | baf553d | 2018-01-29 22:49:27 +0000 | [diff] [blame] | 1235 | unsigned short port = htons(daemon->min_port + (rand16() % ((unsigned short)ports_avail))); |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1236 | |
| 1237 | if (family == AF_INET) |
| 1238 | { |
| 1239 | addr.in.sin_addr.s_addr = INADDR_ANY; |
Simon Kelley | 3927da4 | 2008-07-20 15:10:39 +0100 | [diff] [blame] | 1240 | addr.in.sin_port = port; |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1241 | #ifdef HAVE_SOCKADDR_SA_LEN |
| 1242 | addr.in.sin_len = sizeof(struct sockaddr_in); |
| 1243 | #endif |
| 1244 | } |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1245 | else |
| 1246 | { |
| 1247 | addr.in6.sin6_addr = in6addr_any; |
Simon Kelley | 3927da4 | 2008-07-20 15:10:39 +0100 | [diff] [blame] | 1248 | addr.in6.sin6_port = port; |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1249 | #ifdef HAVE_SOCKADDR_SA_LEN |
| 1250 | addr.in6.sin6_len = sizeof(struct sockaddr_in6); |
| 1251 | #endif |
| 1252 | } |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1253 | |
| 1254 | if (bind(fd, (struct sockaddr *)&addr, sa_len(&addr)) == 0) |
| 1255 | return fd; |
| 1256 | |
| 1257 | if (errno != EADDRINUSE && errno != EACCES) |
| 1258 | break; |
| 1259 | } |
| 1260 | |
| 1261 | close(fd); |
| 1262 | } |
| 1263 | |
| 1264 | return -1; |
| 1265 | } |
| 1266 | |
| 1267 | |
Simon Kelley | 9d6918d | 2017-10-13 17:55:09 +0100 | [diff] [blame] | 1268 | int local_bind(int fd, union mysockaddr *addr, char *intname, unsigned int ifindex, int is_tcp) |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1269 | { |
| 1270 | union mysockaddr addr_copy = *addr; |
Simon Kelley | a2a7e04 | 2020-12-12 23:26:45 +0000 | [diff] [blame] | 1271 | unsigned short port; |
| 1272 | int tries = 1, done = 0; |
| 1273 | unsigned int ports_avail = ((unsigned short)daemon->max_port - (unsigned short)daemon->min_port) + 1; |
| 1274 | |
| 1275 | if (addr_copy.sa.sa_family == AF_INET) |
| 1276 | port = addr_copy.in.sin_port; |
| 1277 | else |
| 1278 | port = addr_copy.in6.sin6_port; |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1279 | |
| 1280 | /* cannot set source _port_ for TCP connections. */ |
| 1281 | if (is_tcp) |
Simon Kelley | a2a7e04 | 2020-12-12 23:26:45 +0000 | [diff] [blame] | 1282 | port = 0; |
| 1283 | |
| 1284 | /* Bind a random port within the range given by min-port and max-port */ |
| 1285 | if (port == 0) |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1286 | { |
Simon Kelley | a2a7e04 | 2020-12-12 23:26:45 +0000 | [diff] [blame] | 1287 | tries = ports_avail < 30 ? 3 * ports_avail : 100; |
| 1288 | port = htons(daemon->min_port + (rand16() % ((unsigned short)ports_avail))); |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1289 | } |
| 1290 | |
Simon Kelley | a2a7e04 | 2020-12-12 23:26:45 +0000 | [diff] [blame] | 1291 | while (tries--) |
| 1292 | { |
| 1293 | if (addr_copy.sa.sa_family == AF_INET) |
| 1294 | addr_copy.in.sin_port = port; |
| 1295 | else |
| 1296 | addr_copy.in6.sin6_port = port; |
| 1297 | |
| 1298 | if (bind(fd, (struct sockaddr *)&addr_copy, sa_len(&addr_copy)) != -1) |
| 1299 | { |
| 1300 | done = 1; |
| 1301 | break; |
| 1302 | } |
| 1303 | |
| 1304 | if (errno != EADDRINUSE && errno != EACCES) |
| 1305 | return 0; |
| 1306 | |
| 1307 | port = htons(daemon->min_port + (rand16() % ((unsigned short)ports_avail))); |
| 1308 | } |
| 1309 | |
| 1310 | if (!done) |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1311 | return 0; |
Simon Kelley | 9d6918d | 2017-10-13 17:55:09 +0100 | [diff] [blame] | 1312 | |
| 1313 | if (!is_tcp && ifindex > 0) |
| 1314 | { |
| 1315 | #if defined(IP_UNICAST_IF) |
| 1316 | if (addr_copy.sa.sa_family == AF_INET) |
| 1317 | { |
| 1318 | uint32_t ifindex_opt = htonl(ifindex); |
| 1319 | return setsockopt(fd, IPPROTO_IP, IP_UNICAST_IF, &ifindex_opt, sizeof(ifindex_opt)) == 0; |
| 1320 | } |
| 1321 | #endif |
Simon Kelley | ee87504 | 2018-10-23 22:10:17 +0100 | [diff] [blame] | 1322 | #if defined (IPV6_UNICAST_IF) |
Simon Kelley | 9d6918d | 2017-10-13 17:55:09 +0100 | [diff] [blame] | 1323 | if (addr_copy.sa.sa_family == AF_INET6) |
| 1324 | { |
| 1325 | uint32_t ifindex_opt = htonl(ifindex); |
| 1326 | return setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_IF, &ifindex_opt, sizeof(ifindex_opt)) == 0; |
| 1327 | } |
| 1328 | #endif |
| 1329 | } |
| 1330 | |
Kevin Darbyshire-Bryant | 70c50ef | 2020-03-06 10:31:15 +0000 | [diff] [blame] | 1331 | (void)intname; /* suppress potential unused warning */ |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1332 | #if defined(SO_BINDTODEVICE) |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 1333 | if (intname[0] != 0 && |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 1334 | setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, intname, IF_NAMESIZE) == -1) |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1335 | return 0; |
| 1336 | #endif |
| 1337 | |
| 1338 | return 1; |
| 1339 | } |
| 1340 | |
| 1341 | static struct serverfd *allocate_sfd(union mysockaddr *addr, char *intname) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1342 | { |
| 1343 | struct serverfd *sfd; |
Beniamino Galvani | 2675f20 | 2016-08-28 20:44:05 +0100 | [diff] [blame] | 1344 | unsigned int ifindex = 0; |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1345 | int errsave; |
Simon Kelley | e83915d | 2018-04-10 21:27:26 +0100 | [diff] [blame] | 1346 | int opt = 1; |
| 1347 | |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1348 | /* when using random ports, servers which would otherwise use |
| 1349 | the INADDR_ANY/port0 socket have sfd set to NULL */ |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 1350 | if (!daemon->osport && intname[0] == 0) |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1351 | { |
| 1352 | errno = 0; |
| 1353 | |
| 1354 | if (addr->sa.sa_family == AF_INET && |
| 1355 | addr->in.sin_addr.s_addr == INADDR_ANY && |
| 1356 | addr->in.sin_port == htons(0)) |
| 1357 | return NULL; |
| 1358 | |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1359 | if (addr->sa.sa_family == AF_INET6 && |
| 1360 | memcmp(&addr->in6.sin6_addr, &in6addr_any, sizeof(in6addr_any)) == 0 && |
| 1361 | addr->in6.sin6_port == htons(0)) |
| 1362 | return NULL; |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1363 | } |
Beniamino Galvani | 2675f20 | 2016-08-28 20:44:05 +0100 | [diff] [blame] | 1364 | |
| 1365 | if (intname && strlen(intname) != 0) |
| 1366 | ifindex = if_nametoindex(intname); /* index == 0 when not binding to an interface */ |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1367 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1368 | /* may have a suitable one already */ |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1369 | for (sfd = daemon->sfds; sfd; sfd = sfd->next ) |
| 1370 | if (sockaddr_isequal(&sfd->source_addr, addr) && |
Beniamino Galvani | 2675f20 | 2016-08-28 20:44:05 +0100 | [diff] [blame] | 1371 | strcmp(intname, sfd->interface) == 0 && |
| 1372 | ifindex == sfd->ifindex) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1373 | return sfd; |
| 1374 | |
| 1375 | /* need to make a new one. */ |
| 1376 | errno = ENOMEM; /* in case malloc fails. */ |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 1377 | if (!(sfd = whine_malloc(sizeof(struct serverfd)))) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1378 | return NULL; |
| 1379 | |
| 1380 | if ((sfd->fd = socket(addr->sa.sa_family, SOCK_DGRAM, 0)) == -1) |
| 1381 | { |
| 1382 | free(sfd); |
| 1383 | return NULL; |
| 1384 | } |
Simon Kelley | e83915d | 2018-04-10 21:27:26 +0100 | [diff] [blame] | 1385 | |
| 1386 | if ((addr->sa.sa_family == AF_INET6 && setsockopt(sfd->fd, IPPROTO_IPV6, IPV6_V6ONLY, &opt, sizeof(opt)) == -1) || |
| 1387 | !local_bind(sfd->fd, addr, intname, ifindex, 0) || !fix_fd(sfd->fd)) |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1388 | { |
Simon Kelley | e83915d | 2018-04-10 21:27:26 +0100 | [diff] [blame] | 1389 | errsave = errno; /* save error from bind/setsockopt. */ |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1390 | close(sfd->fd); |
| 1391 | free(sfd); |
| 1392 | errno = errsave; |
| 1393 | return NULL; |
| 1394 | } |
Beniamino Galvani | 2675f20 | 2016-08-28 20:44:05 +0100 | [diff] [blame] | 1395 | |
Petr Menšík | 47b45b2 | 2018-08-15 18:17:00 +0200 | [diff] [blame] | 1396 | safe_strncpy(sfd->interface, intname, sizeof(sfd->interface)); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1397 | sfd->source_addr = *addr; |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1398 | sfd->next = daemon->sfds; |
Beniamino Galvani | 2675f20 | 2016-08-28 20:44:05 +0100 | [diff] [blame] | 1399 | sfd->ifindex = ifindex; |
Simon Kelley | 4441cf7 | 2018-04-10 21:39:54 +0100 | [diff] [blame] | 1400 | sfd->preallocated = 0; |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1401 | daemon->sfds = sfd; |
Beniamino Galvani | 2675f20 | 2016-08-28 20:44:05 +0100 | [diff] [blame] | 1402 | |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1403 | return sfd; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1404 | } |
| 1405 | |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1406 | /* create upstream sockets during startup, before root is dropped which may be needed |
| 1407 | this allows query_port to be a low port and interface binding */ |
| 1408 | void pre_allocate_sfds(void) |
| 1409 | { |
| 1410 | struct server *srv; |
Simon Kelley | 4441cf7 | 2018-04-10 21:39:54 +0100 | [diff] [blame] | 1411 | struct serverfd *sfd; |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1412 | |
| 1413 | if (daemon->query_port != 0) |
| 1414 | { |
| 1415 | union mysockaddr addr; |
| 1416 | memset(&addr, 0, sizeof(addr)); |
| 1417 | addr.in.sin_family = AF_INET; |
| 1418 | addr.in.sin_addr.s_addr = INADDR_ANY; |
| 1419 | addr.in.sin_port = htons(daemon->query_port); |
| 1420 | #ifdef HAVE_SOCKADDR_SA_LEN |
| 1421 | addr.in.sin_len = sizeof(struct sockaddr_in); |
| 1422 | #endif |
Simon Kelley | 4441cf7 | 2018-04-10 21:39:54 +0100 | [diff] [blame] | 1423 | if ((sfd = allocate_sfd(&addr, ""))) |
| 1424 | sfd->preallocated = 1; |
Simon Kelley | ee87504 | 2018-10-23 22:10:17 +0100 | [diff] [blame] | 1425 | |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1426 | memset(&addr, 0, sizeof(addr)); |
| 1427 | addr.in6.sin6_family = AF_INET6; |
| 1428 | addr.in6.sin6_addr = in6addr_any; |
| 1429 | addr.in6.sin6_port = htons(daemon->query_port); |
| 1430 | #ifdef HAVE_SOCKADDR_SA_LEN |
| 1431 | addr.in6.sin6_len = sizeof(struct sockaddr_in6); |
| 1432 | #endif |
Simon Kelley | 4441cf7 | 2018-04-10 21:39:54 +0100 | [diff] [blame] | 1433 | if ((sfd = allocate_sfd(&addr, ""))) |
| 1434 | sfd->preallocated = 1; |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1435 | } |
| 1436 | |
| 1437 | for (srv = daemon->servers; srv; srv = srv->next) |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 1438 | if (!(srv->flags & (SERV_LITERAL_ADDRESS | SERV_NO_ADDR | SERV_USE_RESOLV | SERV_NO_REBIND)) && |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1439 | !allocate_sfd(&srv->source_addr, srv->interface) && |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1440 | errno != 0 && |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 1441 | option_bool(OPT_NOWILD)) |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1442 | { |
Petr Mensik | 51cdd1a | 2019-07-04 20:28:08 +0200 | [diff] [blame] | 1443 | (void)prettyprint_addr(&srv->source_addr, daemon->namebuff); |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 1444 | if (srv->interface[0] != 0) |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1445 | { |
| 1446 | strcat(daemon->namebuff, " "); |
| 1447 | strcat(daemon->namebuff, srv->interface); |
| 1448 | } |
| 1449 | die(_("failed to bind server socket for %s: %s"), |
| 1450 | daemon->namebuff, EC_BADNET); |
| 1451 | } |
| 1452 | } |
| 1453 | |
Simon Kelley | d68c2ca | 2014-02-18 22:30:30 +0000 | [diff] [blame] | 1454 | void mark_servers(int flag) |
| 1455 | { |
| 1456 | struct server *serv; |
| 1457 | |
| 1458 | /* mark everything with argument flag */ |
| 1459 | for (serv = daemon->servers; serv; serv = serv->next) |
Simon Kelley | 40766e5 | 2014-07-29 16:52:00 +0100 | [diff] [blame] | 1460 | { |
| 1461 | if (serv->flags & flag) |
Simon Kelley | b5ea1cc | 2014-07-29 16:34:14 +0100 | [diff] [blame] | 1462 | serv->flags |= SERV_MARK; |
| 1463 | #ifdef HAVE_LOOP |
Simon Kelley | 40766e5 | 2014-07-29 16:52:00 +0100 | [diff] [blame] | 1464 | /* Give looped servers another chance */ |
| 1465 | serv->flags &= ~SERV_LOOP; |
Simon Kelley | b5ea1cc | 2014-07-29 16:34:14 +0100 | [diff] [blame] | 1466 | #endif |
Simon Kelley | 40766e5 | 2014-07-29 16:52:00 +0100 | [diff] [blame] | 1467 | } |
Simon Kelley | d68c2ca | 2014-02-18 22:30:30 +0000 | [diff] [blame] | 1468 | } |
| 1469 | |
| 1470 | void cleanup_servers(void) |
| 1471 | { |
| 1472 | struct server *serv, *tmp, **up; |
| 1473 | |
| 1474 | /* unlink and free anything still marked. */ |
| 1475 | for (serv = daemon->servers, up = &daemon->servers; serv; serv = tmp) |
| 1476 | { |
| 1477 | tmp = serv->next; |
| 1478 | if (serv->flags & SERV_MARK) |
| 1479 | { |
| 1480 | server_gone(serv); |
| 1481 | *up = serv->next; |
| 1482 | if (serv->domain) |
| 1483 | free(serv->domain); |
| 1484 | free(serv); |
| 1485 | } |
| 1486 | else |
| 1487 | up = &serv->next; |
| 1488 | } |
Simon Kelley | b5ea1cc | 2014-07-29 16:34:14 +0100 | [diff] [blame] | 1489 | |
| 1490 | #ifdef HAVE_LOOP |
| 1491 | /* Now we have a new set of servers, test for loops. */ |
| 1492 | loop_send_probes(); |
| 1493 | #endif |
Simon Kelley | d68c2ca | 2014-02-18 22:30:30 +0000 | [diff] [blame] | 1494 | } |
| 1495 | |
| 1496 | void add_update_server(int flags, |
| 1497 | union mysockaddr *addr, |
| 1498 | union mysockaddr *source_addr, |
| 1499 | const char *interface, |
| 1500 | const char *domain) |
| 1501 | { |
Simon Kelley | 7b1eae4 | 2014-02-20 13:43:28 +0000 | [diff] [blame] | 1502 | struct server *serv, *next = NULL; |
| 1503 | char *domain_str = NULL; |
Simon Kelley | d68c2ca | 2014-02-18 22:30:30 +0000 | [diff] [blame] | 1504 | |
| 1505 | /* See if there is a suitable candidate, and unmark */ |
| 1506 | for (serv = daemon->servers; serv; serv = serv->next) |
| 1507 | if (serv->flags & SERV_MARK) |
| 1508 | { |
| 1509 | if (domain) |
| 1510 | { |
| 1511 | if (!(serv->flags & SERV_HAS_DOMAIN) || !hostname_isequal(domain, serv->domain)) |
| 1512 | continue; |
| 1513 | } |
| 1514 | else |
| 1515 | { |
| 1516 | if (serv->flags & SERV_HAS_DOMAIN) |
| 1517 | continue; |
| 1518 | } |
| 1519 | |
Simon Kelley | d68c2ca | 2014-02-18 22:30:30 +0000 | [diff] [blame] | 1520 | break; |
| 1521 | } |
Simon Kelley | 7b1eae4 | 2014-02-20 13:43:28 +0000 | [diff] [blame] | 1522 | |
| 1523 | if (serv) |
| 1524 | { |
| 1525 | domain_str = serv->domain; |
| 1526 | next = serv->next; |
| 1527 | } |
| 1528 | else if ((serv = whine_malloc(sizeof (struct server)))) |
Simon Kelley | d68c2ca | 2014-02-18 22:30:30 +0000 | [diff] [blame] | 1529 | { |
| 1530 | /* Not found, create a new one. */ |
Simon Kelley | 7b1eae4 | 2014-02-20 13:43:28 +0000 | [diff] [blame] | 1531 | if (domain && !(domain_str = whine_malloc(strlen(domain)+1))) |
Simon Kelley | d68c2ca | 2014-02-18 22:30:30 +0000 | [diff] [blame] | 1532 | { |
| 1533 | free(serv); |
| 1534 | serv = NULL; |
| 1535 | } |
| 1536 | else |
| 1537 | { |
| 1538 | struct server *s; |
| 1539 | /* Add to the end of the chain, for order */ |
| 1540 | if (!daemon->servers) |
| 1541 | daemon->servers = serv; |
| 1542 | else |
| 1543 | { |
| 1544 | for (s = daemon->servers; s->next; s = s->next); |
| 1545 | s->next = serv; |
| 1546 | } |
| 1547 | if (domain) |
Simon Kelley | 7b1eae4 | 2014-02-20 13:43:28 +0000 | [diff] [blame] | 1548 | strcpy(domain_str, domain); |
Simon Kelley | d68c2ca | 2014-02-18 22:30:30 +0000 | [diff] [blame] | 1549 | } |
| 1550 | } |
| 1551 | |
| 1552 | if (serv) |
| 1553 | { |
Simon Kelley | 7b1eae4 | 2014-02-20 13:43:28 +0000 | [diff] [blame] | 1554 | memset(serv, 0, sizeof(struct server)); |
Simon Kelley | d68c2ca | 2014-02-18 22:30:30 +0000 | [diff] [blame] | 1555 | serv->flags = flags; |
Simon Kelley | 7b1eae4 | 2014-02-20 13:43:28 +0000 | [diff] [blame] | 1556 | serv->domain = domain_str; |
| 1557 | serv->next = next; |
Simon Kelley | d68c2ca | 2014-02-18 22:30:30 +0000 | [diff] [blame] | 1558 | serv->queries = serv->failed_queries = 0; |
Simon Kelley | b5ea1cc | 2014-07-29 16:34:14 +0100 | [diff] [blame] | 1559 | #ifdef HAVE_LOOP |
| 1560 | serv->uid = rand32(); |
| 1561 | #endif |
| 1562 | |
Simon Kelley | d68c2ca | 2014-02-18 22:30:30 +0000 | [diff] [blame] | 1563 | if (domain) |
| 1564 | serv->flags |= SERV_HAS_DOMAIN; |
| 1565 | |
| 1566 | if (interface) |
Petr Menšík | 47b45b2 | 2018-08-15 18:17:00 +0200 | [diff] [blame] | 1567 | safe_strncpy(serv->interface, interface, sizeof(serv->interface)); |
Simon Kelley | 7b1eae4 | 2014-02-20 13:43:28 +0000 | [diff] [blame] | 1568 | if (addr) |
| 1569 | serv->addr = *addr; |
| 1570 | if (source_addr) |
| 1571 | serv->source_addr = *source_addr; |
Simon Kelley | d68c2ca | 2014-02-18 22:30:30 +0000 | [diff] [blame] | 1572 | } |
| 1573 | } |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1574 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 1575 | void check_servers(void) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1576 | { |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1577 | struct irec *iface; |
Simon Kelley | 7bcca00 | 2014-02-19 17:45:17 +0000 | [diff] [blame] | 1578 | struct server *serv; |
Beniamino Galvani | 2675f20 | 2016-08-28 20:44:05 +0100 | [diff] [blame] | 1579 | struct serverfd *sfd, *tmp, **up; |
Simon Kelley | b970260 | 2016-05-03 22:34:06 +0100 | [diff] [blame] | 1580 | int port = 0, count; |
Hannu Nyman | 3e2496f | 2017-02-11 13:44:08 +0000 | [diff] [blame] | 1581 | int locals = 0; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1582 | |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 1583 | /* interface may be new since startup */ |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 1584 | if (!option_bool(OPT_NOWILD)) |
Simon Kelley | 115ac3e | 2013-05-20 11:28:32 +0100 | [diff] [blame] | 1585 | enumerate_interfaces(0); |
Simon Kelley | 4441cf7 | 2018-04-10 21:39:54 +0100 | [diff] [blame] | 1586 | |
| 1587 | /* don't garbage collect pre-allocated sfds. */ |
Beniamino Galvani | 2675f20 | 2016-08-28 20:44:05 +0100 | [diff] [blame] | 1588 | for (sfd = daemon->sfds; sfd; sfd = sfd->next) |
Simon Kelley | 4441cf7 | 2018-04-10 21:39:54 +0100 | [diff] [blame] | 1589 | sfd->used = sfd->preallocated; |
Beniamino Galvani | 2675f20 | 2016-08-28 20:44:05 +0100 | [diff] [blame] | 1590 | |
Simon Kelley | b970260 | 2016-05-03 22:34:06 +0100 | [diff] [blame] | 1591 | for (count = 0, serv = daemon->servers; serv; serv = serv->next) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1592 | { |
Simon Kelley | 367341f | 2016-01-12 15:58:23 +0000 | [diff] [blame] | 1593 | if (!(serv->flags & (SERV_LITERAL_ADDRESS | SERV_NO_ADDR | SERV_USE_RESOLV | SERV_NO_REBIND))) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1594 | { |
Simon Kelley | 14ffa07 | 2016-04-25 16:36:44 +0100 | [diff] [blame] | 1595 | /* Init edns_pktsz for newly created server records. */ |
| 1596 | if (serv->edns_pktsz == 0) |
| 1597 | serv->edns_pktsz = daemon->edns_pktsz; |
| 1598 | |
Simon Kelley | 367341f | 2016-01-12 15:58:23 +0000 | [diff] [blame] | 1599 | #ifdef HAVE_DNSSEC |
Simon Kelley | 92be34a | 2016-01-16 18:39:54 +0000 | [diff] [blame] | 1600 | if (option_bool(OPT_DNSSEC_VALID)) |
| 1601 | { |
Simon Kelley | a49c5c2 | 2017-10-10 22:04:59 +0100 | [diff] [blame] | 1602 | if (!(serv->flags & SERV_FOR_NODOTS)) |
| 1603 | serv->flags |= SERV_DO_DNSSEC; |
| 1604 | |
| 1605 | /* Disable DNSSEC validation when using server=/domain/.... servers |
| 1606 | unless there's a configured trust anchor. */ |
Simon Kelley | 92be34a | 2016-01-16 18:39:54 +0000 | [diff] [blame] | 1607 | if (serv->flags & SERV_HAS_DOMAIN) |
| 1608 | { |
| 1609 | struct ds_config *ds; |
| 1610 | char *domain = serv->domain; |
| 1611 | |
| 1612 | /* .example.com is valid */ |
| 1613 | while (*domain == '.') |
| 1614 | domain++; |
| 1615 | |
| 1616 | for (ds = daemon->ds; ds; ds = ds->next) |
| 1617 | if (ds->name[0] != 0 && hostname_isequal(domain, ds->name)) |
| 1618 | break; |
| 1619 | |
| 1620 | if (!ds) |
| 1621 | serv->flags &= ~SERV_DO_DNSSEC; |
| 1622 | } |
Simon Kelley | 367341f | 2016-01-12 15:58:23 +0000 | [diff] [blame] | 1623 | } |
| 1624 | #endif |
| 1625 | |
| 1626 | port = prettyprint_addr(&serv->addr, daemon->namebuff); |
| 1627 | |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 1628 | /* 0.0.0.0 is nothing, the stack treats it like 127.0.0.1 */ |
Simon Kelley | 7bcca00 | 2014-02-19 17:45:17 +0000 | [diff] [blame] | 1629 | if (serv->addr.sa.sa_family == AF_INET && |
| 1630 | serv->addr.in.sin_addr.s_addr == 0) |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 1631 | { |
Simon Kelley | 7bcca00 | 2014-02-19 17:45:17 +0000 | [diff] [blame] | 1632 | serv->flags |= SERV_MARK; |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 1633 | continue; |
| 1634 | } |
| 1635 | |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 1636 | for (iface = daemon->interfaces; iface; iface = iface->next) |
Simon Kelley | 7bcca00 | 2014-02-19 17:45:17 +0000 | [diff] [blame] | 1637 | if (sockaddr_isequal(&serv->addr, &iface->addr)) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1638 | break; |
| 1639 | if (iface) |
| 1640 | { |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 1641 | my_syslog(LOG_WARNING, _("ignoring nameserver %s - local interface"), daemon->namebuff); |
Simon Kelley | 7bcca00 | 2014-02-19 17:45:17 +0000 | [diff] [blame] | 1642 | serv->flags |= SERV_MARK; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1643 | continue; |
| 1644 | } |
| 1645 | |
| 1646 | /* Do we need a socket set? */ |
Simon Kelley | 7bcca00 | 2014-02-19 17:45:17 +0000 | [diff] [blame] | 1647 | if (!serv->sfd && |
| 1648 | !(serv->sfd = allocate_sfd(&serv->source_addr, serv->interface)) && |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1649 | errno != 0) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1650 | { |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 1651 | my_syslog(LOG_WARNING, |
| 1652 | _("ignoring nameserver %s - cannot make/bind socket: %s"), |
| 1653 | daemon->namebuff, strerror(errno)); |
Simon Kelley | 7bcca00 | 2014-02-19 17:45:17 +0000 | [diff] [blame] | 1654 | serv->flags |= SERV_MARK; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1655 | continue; |
| 1656 | } |
Simon Kelley | 16800ea | 2016-08-30 23:07:06 +0100 | [diff] [blame] | 1657 | |
| 1658 | if (serv->sfd) |
| 1659 | serv->sfd->used = 1; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1660 | } |
| 1661 | |
Simon Kelley | 979fe86 | 2015-03-19 22:50:22 +0000 | [diff] [blame] | 1662 | if (!(serv->flags & SERV_NO_REBIND) && !(serv->flags & SERV_LITERAL_ADDRESS)) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1663 | { |
Simon Kelley | b970260 | 2016-05-03 22:34:06 +0100 | [diff] [blame] | 1664 | if (++count > SERVERS_LOGGED) |
| 1665 | continue; |
| 1666 | |
Simon Kelley | 7bcca00 | 2014-02-19 17:45:17 +0000 | [diff] [blame] | 1667 | if (serv->flags & (SERV_HAS_DOMAIN | SERV_FOR_NODOTS | SERV_USE_RESOLV)) |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 1668 | { |
Simon Kelley | 367341f | 2016-01-12 15:58:23 +0000 | [diff] [blame] | 1669 | char *s1, *s2, *s3 = ""; |
| 1670 | #ifdef HAVE_DNSSEC |
| 1671 | if (option_bool(OPT_DNSSEC_VALID) && !(serv->flags & SERV_DO_DNSSEC)) |
| 1672 | s3 = _("(no DNSSEC)"); |
| 1673 | #endif |
Simon Kelley | 7bcca00 | 2014-02-19 17:45:17 +0000 | [diff] [blame] | 1674 | if (!(serv->flags & SERV_HAS_DOMAIN)) |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 1675 | s1 = _("unqualified"), s2 = _("names"); |
Simon Kelley | d9603ef | 2020-01-26 18:13:35 +0000 | [diff] [blame] | 1676 | else if (strlen(serv->domain) == 0) |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 1677 | s1 = _("default"), s2 = ""; |
| 1678 | else |
Simon Kelley | 7bcca00 | 2014-02-19 17:45:17 +0000 | [diff] [blame] | 1679 | s1 = _("domain"), s2 = serv->domain; |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 1680 | |
Simon Kelley | 7bcca00 | 2014-02-19 17:45:17 +0000 | [diff] [blame] | 1681 | if (serv->flags & SERV_NO_ADDR) |
Hannu Nyman | 3e2496f | 2017-02-11 13:44:08 +0000 | [diff] [blame] | 1682 | { |
| 1683 | count--; |
| 1684 | if (++locals <= LOCALS_LOGGED) |
Simon Kelley | 8bd28a8 | 2019-03-01 15:00:12 +0000 | [diff] [blame] | 1685 | my_syslog(LOG_INFO, _("using only locally-known addresses for %s %s"), s1, s2); |
Hannu Nyman | 3e2496f | 2017-02-11 13:44:08 +0000 | [diff] [blame] | 1686 | } |
Simon Kelley | 7bcca00 | 2014-02-19 17:45:17 +0000 | [diff] [blame] | 1687 | else if (serv->flags & SERV_USE_RESOLV) |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 1688 | my_syslog(LOG_INFO, _("using standard nameservers for %s %s"), s1, s2); |
Simon Kelley | 979fe86 | 2015-03-19 22:50:22 +0000 | [diff] [blame] | 1689 | else |
Simon Kelley | 367341f | 2016-01-12 15:58:23 +0000 | [diff] [blame] | 1690 | my_syslog(LOG_INFO, _("using nameserver %s#%d for %s %s %s"), daemon->namebuff, port, s1, s2, s3); |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 1691 | } |
Simon Kelley | b5ea1cc | 2014-07-29 16:34:14 +0100 | [diff] [blame] | 1692 | #ifdef HAVE_LOOP |
| 1693 | else if (serv->flags & SERV_LOOP) |
| 1694 | my_syslog(LOG_INFO, _("NOT using nameserver %s#%d - query loop detected"), daemon->namebuff, port); |
| 1695 | #endif |
Simon Kelley | 7bcca00 | 2014-02-19 17:45:17 +0000 | [diff] [blame] | 1696 | else if (serv->interface[0] != 0) |
| 1697 | my_syslog(LOG_INFO, _("using nameserver %s#%d(via %s)"), daemon->namebuff, port, serv->interface); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1698 | else |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 1699 | my_syslog(LOG_INFO, _("using nameserver %s#%d"), daemon->namebuff, port); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1700 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1701 | } |
Simon Kelley | b970260 | 2016-05-03 22:34:06 +0100 | [diff] [blame] | 1702 | |
Hannu Nyman | 3e2496f | 2017-02-11 13:44:08 +0000 | [diff] [blame] | 1703 | if (locals > LOCALS_LOGGED) |
| 1704 | my_syslog(LOG_INFO, _("using %d more local addresses"), locals - LOCALS_LOGGED); |
Simon Kelley | b970260 | 2016-05-03 22:34:06 +0100 | [diff] [blame] | 1705 | if (count - 1 > SERVERS_LOGGED) |
| 1706 | my_syslog(LOG_INFO, _("using %d more nameservers"), count - SERVERS_LOGGED - 1); |
Simon Kelley | 7bcca00 | 2014-02-19 17:45:17 +0000 | [diff] [blame] | 1707 | |
Beniamino Galvani | 2675f20 | 2016-08-28 20:44:05 +0100 | [diff] [blame] | 1708 | /* Remove unused sfds */ |
| 1709 | for (sfd = daemon->sfds, up = &daemon->sfds; sfd; sfd = tmp) |
| 1710 | { |
| 1711 | tmp = sfd->next; |
| 1712 | if (!sfd->used) |
| 1713 | { |
| 1714 | *up = sfd->next; |
| 1715 | close(sfd->fd); |
| 1716 | free(sfd); |
| 1717 | } |
| 1718 | else |
| 1719 | up = &sfd->next; |
| 1720 | } |
| 1721 | |
Simon Kelley | 7bcca00 | 2014-02-19 17:45:17 +0000 | [diff] [blame] | 1722 | cleanup_servers(); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1723 | } |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 1724 | |
| 1725 | /* Return zero if no servers found, in that case we keep polling. |
| 1726 | This is a protection against an update-time/write race on resolv.conf */ |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 1727 | int reload_servers(char *fname) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1728 | { |
| 1729 | FILE *f; |
| 1730 | char *line; |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 1731 | int gotone = 0; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1732 | |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 1733 | /* buff happens to be MAXDNAME long... */ |
| 1734 | if (!(f = fopen(fname, "r"))) |
| 1735 | { |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 1736 | my_syslog(LOG_ERR, _("failed to read %s: %s"), fname, strerror(errno)); |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 1737 | return 0; |
| 1738 | } |
Simon Kelley | d68c2ca | 2014-02-18 22:30:30 +0000 | [diff] [blame] | 1739 | |
| 1740 | mark_servers(SERV_FROM_RESOLV); |
| 1741 | |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 1742 | while ((line = fgets(daemon->namebuff, MAXDNAME, f))) |
| 1743 | { |
| 1744 | union mysockaddr addr, source_addr; |
| 1745 | char *token = strtok(line, " \t\n\r"); |
| 1746 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 1747 | if (!token) |
| 1748 | continue; |
| 1749 | if (strcmp(token, "nameserver") != 0 && strcmp(token, "server") != 0) |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 1750 | continue; |
| 1751 | if (!(token = strtok(NULL, " \t\n\r"))) |
| 1752 | continue; |
| 1753 | |
| 1754 | memset(&addr, 0, sizeof(addr)); |
| 1755 | memset(&source_addr, 0, sizeof(source_addr)); |
| 1756 | |
| 1757 | if ((addr.in.sin_addr.s_addr = inet_addr(token)) != (in_addr_t) -1) |
| 1758 | { |
| 1759 | #ifdef HAVE_SOCKADDR_SA_LEN |
| 1760 | source_addr.in.sin_len = addr.in.sin_len = sizeof(source_addr.in); |
| 1761 | #endif |
| 1762 | source_addr.in.sin_family = addr.in.sin_family = AF_INET; |
| 1763 | addr.in.sin_port = htons(NAMESERVER_PORT); |
| 1764 | source_addr.in.sin_addr.s_addr = INADDR_ANY; |
| 1765 | source_addr.in.sin_port = htons(daemon->query_port); |
| 1766 | } |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 1767 | else |
| 1768 | { |
| 1769 | int scope_index = 0; |
| 1770 | char *scope_id = strchr(token, '%'); |
| 1771 | |
| 1772 | if (scope_id) |
| 1773 | { |
| 1774 | *(scope_id++) = 0; |
| 1775 | scope_index = if_nametoindex(scope_id); |
| 1776 | } |
| 1777 | |
| 1778 | if (inet_pton(AF_INET6, token, &addr.in6.sin6_addr) > 0) |
| 1779 | { |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 1780 | #ifdef HAVE_SOCKADDR_SA_LEN |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 1781 | source_addr.in6.sin6_len = addr.in6.sin6_len = sizeof(source_addr.in6); |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 1782 | #endif |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 1783 | source_addr.in6.sin6_family = addr.in6.sin6_family = AF_INET6; |
| 1784 | source_addr.in6.sin6_flowinfo = addr.in6.sin6_flowinfo = 0; |
| 1785 | addr.in6.sin6_port = htons(NAMESERVER_PORT); |
| 1786 | addr.in6.sin6_scope_id = scope_index; |
| 1787 | source_addr.in6.sin6_addr = in6addr_any; |
| 1788 | source_addr.in6.sin6_port = htons(daemon->query_port); |
| 1789 | source_addr.in6.sin6_scope_id = 0; |
| 1790 | } |
| 1791 | else |
| 1792 | continue; |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 1793 | } |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 1794 | |
Simon Kelley | d68c2ca | 2014-02-18 22:30:30 +0000 | [diff] [blame] | 1795 | add_update_server(SERV_FROM_RESOLV, &addr, &source_addr, NULL, NULL); |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 1796 | gotone = 1; |
| 1797 | } |
| 1798 | |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 1799 | fclose(f); |
Simon Kelley | d68c2ca | 2014-02-18 22:30:30 +0000 | [diff] [blame] | 1800 | cleanup_servers(); |
Simon Kelley | 849a835 | 2006-06-09 21:02:31 +0100 | [diff] [blame] | 1801 | |
| 1802 | return gotone; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1803 | } |
| 1804 | |
Simon Kelley | 1ee9be4 | 2013-12-09 16:50:19 +0000 | [diff] [blame] | 1805 | /* Called when addresses are added or deleted from an interface */ |
| 1806 | void newaddress(time_t now) |
| 1807 | { |
| 1808 | (void)now; |
| 1809 | |
Simon Kelley | 89b12ed | 2014-03-06 13:27:57 +0000 | [diff] [blame] | 1810 | if (option_bool(OPT_CLEVERBIND) || option_bool(OPT_LOCAL_SERVICE) || |
| 1811 | daemon->doing_dhcp6 || daemon->relay6 || daemon->doing_ra) |
Simon Kelley | 1ee9be4 | 2013-12-09 16:50:19 +0000 | [diff] [blame] | 1812 | enumerate_interfaces(0); |
| 1813 | |
| 1814 | if (option_bool(OPT_CLEVERBIND)) |
| 1815 | create_bound_listeners(0); |
| 1816 | |
| 1817 | #ifdef HAVE_DHCP6 |
| 1818 | if (daemon->doing_dhcp6 || daemon->relay6 || daemon->doing_ra) |
| 1819 | join_multicast(0); |
| 1820 | |
| 1821 | if (daemon->doing_dhcp6 || daemon->doing_ra) |
| 1822 | dhcp_construct_contexts(now); |
| 1823 | |
| 1824 | if (daemon->doing_dhcp6) |
| 1825 | lease_find_interfaces(now); |
| 1826 | #endif |
| 1827 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1828 | |
| 1829 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1830 | |
| 1831 | |
| 1832 | |