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