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