]> git.ipfire.org Git - people/ms/dnsmasq.git/blob - src/network.c
Better logging of socket-creation errors.
[people/ms/dnsmasq.git] / src / network.c
1 /* dnsmasq is Copyright (c) 2000-2012 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 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 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
98 #else
99
100 int indextoname(int fd, int index, char *name)
101 {
102 if (index == 0 || !if_indextoname(index, name))
103 return 0;
104
105 return 1;
106 }
107
108 #endif
109
110 int iface_check(int family, struct all_addr *addr, char *name)
111 {
112 struct iname *tmp;
113 int ret = 1;
114
115 /* Note: have to check all and not bail out early, so that we set the
116 "used" flags. */
117
118 if (daemon->if_names || daemon->if_addrs)
119 {
120 #ifdef HAVE_DHCP
121 struct dhcp_context *range;
122 #endif
123
124 ret = 0;
125
126 #ifdef HAVE_DHCP
127 for (range = daemon->dhcp; range; range = range->next)
128 if (range->interface && strcmp(range->interface, name) == 0)
129 ret = 1;
130 #endif
131
132 for (tmp = daemon->if_names; tmp; tmp = tmp->next)
133 if (tmp->name && (strcmp(tmp->name, name) == 0))
134 ret = tmp->used = 1;
135
136 for (tmp = daemon->if_addrs; tmp; tmp = tmp->next)
137 if (tmp->addr.sa.sa_family == family)
138 {
139 if (family == AF_INET &&
140 tmp->addr.in.sin_addr.s_addr == addr->addr.addr4.s_addr)
141 ret = tmp->used = 1;
142 #ifdef HAVE_IPV6
143 else if (family == AF_INET6 &&
144 IN6_ARE_ADDR_EQUAL(&tmp->addr.in6.sin6_addr,
145 &addr->addr.addr6))
146 ret = tmp->used = 1;
147 #endif
148 }
149 }
150
151 for (tmp = daemon->if_except; tmp; tmp = tmp->next)
152 if (tmp->name && (strcmp(tmp->name, name) == 0))
153 ret = 0;
154
155 return ret;
156 }
157
158 static int iface_allowed(struct irec **irecp, int if_index,
159 union mysockaddr *addr, struct in_addr netmask, int dad)
160 {
161 struct irec *iface;
162 int fd, mtu = 0, loopback;
163 struct ifreq ifr;
164 int tftp_ok = daemon->tftp_unlimited;
165 int dhcp_ok = 1;
166 #ifdef HAVE_DHCP
167 struct iname *tmp;
168 #endif
169 struct interface_list *ir = NULL;
170
171 /* check whether the interface IP has been added already
172 we call this routine multiple times. */
173 for (iface = *irecp; iface; iface = iface->next)
174 if (sockaddr_isequal(&iface->addr, addr))
175 {
176 iface->dad = dad;
177 return 1;
178 }
179
180 if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) == -1 ||
181 !indextoname(fd, if_index, ifr.ifr_name) ||
182 ioctl(fd, SIOCGIFFLAGS, &ifr) == -1)
183 {
184 if (fd != -1)
185 {
186 int errsave = errno;
187 close(fd);
188 errno = errsave;
189 }
190 return 0;
191 }
192
193 loopback = ifr.ifr_flags & IFF_LOOPBACK;
194
195 if (loopback)
196 dhcp_ok = 0;
197
198 if (ioctl(fd, SIOCGIFMTU, &ifr) != -1)
199 mtu = ifr.ifr_mtu;
200
201 close(fd);
202
203 /* If we are restricting the set of interfaces to use, make
204 sure that loopback interfaces are in that set. */
205 if (daemon->if_names && loopback)
206 {
207 struct iname *lo;
208 for (lo = daemon->if_names; lo; lo = lo->next)
209 if (lo->name && strcmp(lo->name, ifr.ifr_name) == 0)
210 break;
211
212 if (!lo &&
213 (lo = whine_malloc(sizeof(struct iname))) &&
214 (lo->name = whine_malloc(strlen(ifr.ifr_name)+1)))
215 {
216 strcpy(lo->name, ifr.ifr_name);
217 lo->used = 1;
218 lo->next = daemon->if_names;
219 daemon->if_names = lo;
220 }
221 }
222
223 #ifdef HAVE_TFTP
224 /* implement wierd TFTP service rules */
225 for (ir = daemon->tftp_interfaces; ir; ir = ir->next)
226 if (strcmp(ir->interface, ifr.ifr_name) == 0)
227 {
228 tftp_ok = 1;
229 break;
230 }
231 #endif
232
233 if (!ir)
234 {
235 if (addr->sa.sa_family == AF_INET &&
236 !iface_check(AF_INET, (struct all_addr *)&addr->in.sin_addr, ifr.ifr_name))
237 return 1;
238
239 #ifdef HAVE_DHCP
240 for (tmp = daemon->dhcp_except; tmp; tmp = tmp->next)
241 if (tmp->name && (strcmp(tmp->name, ifr.ifr_name) == 0))
242 {
243 tftp_ok = 0;
244 dhcp_ok = 0;
245 }
246 #endif
247
248 #ifdef HAVE_IPV6
249 if (addr->sa.sa_family == AF_INET6 &&
250 !iface_check(AF_INET6, (struct all_addr *)&addr->in6.sin6_addr, ifr.ifr_name))
251 return 1;
252 #endif
253 }
254
255 /* add to list */
256 if ((iface = whine_malloc(sizeof(struct irec))))
257 {
258 iface->addr = *addr;
259 iface->netmask = netmask;
260 iface->tftp_ok = tftp_ok;
261 iface->dhcp_ok = dhcp_ok;
262 iface->mtu = mtu;
263 iface->dad = dad;
264 iface->done = 0;
265 if ((iface->name = whine_malloc(strlen(ifr.ifr_name)+1)))
266 {
267 strcpy(iface->name, ifr.ifr_name);
268 iface->next = *irecp;
269 *irecp = iface;
270 return 1;
271 }
272 free(iface);
273 }
274
275 errno = ENOMEM;
276 return 0;
277 }
278
279 #ifdef HAVE_IPV6
280 static int iface_allowed_v6(struct in6_addr *local, int prefix,
281 int scope, int if_index, int dad, void *vparam)
282 {
283 union mysockaddr addr;
284 struct in_addr netmask; /* dummy */
285 netmask.s_addr = 0;
286
287 (void)prefix; /* warning */
288 (void)scope; /* warning */
289
290 memset(&addr, 0, sizeof(addr));
291 #ifdef HAVE_SOCKADDR_SA_LEN
292 addr.in6.sin6_len = sizeof(addr.in6);
293 #endif
294 addr.in6.sin6_family = AF_INET6;
295 addr.in6.sin6_addr = *local;
296 addr.in6.sin6_port = htons(daemon->port);
297 addr.in6.sin6_scope_id = if_index;
298
299 return iface_allowed((struct irec **)vparam, if_index, &addr, netmask, dad);
300 }
301 #endif
302
303 static int iface_allowed_v4(struct in_addr local, int if_index,
304 struct in_addr netmask, struct in_addr broadcast, void *vparam)
305 {
306 union mysockaddr addr;
307
308 memset(&addr, 0, sizeof(addr));
309 #ifdef HAVE_SOCKADDR_SA_LEN
310 addr.in.sin_len = sizeof(addr.in);
311 #endif
312 addr.in.sin_family = AF_INET;
313 addr.in.sin_addr = broadcast; /* warning */
314 addr.in.sin_addr = local;
315 addr.in.sin_port = htons(daemon->port);
316
317 return iface_allowed((struct irec **)vparam, if_index, &addr, netmask, 0);
318 }
319
320 int enumerate_interfaces(void)
321 {
322 #ifdef HAVE_IPV6
323 if (!iface_enumerate(AF_INET6, &daemon->interfaces, iface_allowed_v6))
324 return 0;
325 #endif
326
327 return iface_enumerate(AF_INET, &daemon->interfaces, iface_allowed_v4);
328 }
329
330 /* set NONBLOCK bit on fd: See Stevens 16.6 */
331 int fix_fd(int fd)
332 {
333 int flags;
334
335 if ((flags = fcntl(fd, F_GETFL)) == -1 ||
336 fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
337 return 0;
338
339 return 1;
340 }
341
342 static int make_sock(union mysockaddr *addr, int type, int dienow)
343 {
344 int family = addr->sa.sa_family;
345 int fd, rc, opt = 1;
346
347 if ((fd = socket(family, type, 0)) == -1)
348 {
349 int port;
350 char *s;
351
352 /* No error if the kernel just doesn't support this IP flavour */
353 if (errno == EPROTONOSUPPORT ||
354 errno == EAFNOSUPPORT ||
355 errno == EINVAL)
356 return -1;
357
358 err:
359 port = prettyprint_addr(addr, daemon->addrbuff);
360 if (!option_bool(OPT_NOWILD) && !option_bool(OPT_CLEVERBIND))
361 sprintf(daemon->addrbuff, "port %d", port);
362 s = _("failed to create listening socket for %s: %s");
363
364 if (dienow)
365 die(s, daemon->addrbuff, EC_BADNET);
366
367 my_syslog(LOG_ERR, s, daemon->addrbuff, strerror(errno));
368 return -1;
369 }
370
371 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) == -1 || !fix_fd(fd))
372 goto err;
373
374 #ifdef HAVE_IPV6
375 if (family == AF_INET6 && setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &opt, sizeof(opt)) == -1)
376 goto err;
377 #endif
378
379 if ((rc = bind(fd, (struct sockaddr *)addr, sa_len(addr))) == -1)
380 goto err;
381
382 if (type == SOCK_STREAM)
383 {
384 if (listen(fd, 5) == -1)
385 goto err;
386 }
387 else if (!option_bool(OPT_NOWILD))
388 {
389 if (family == AF_INET)
390 {
391 #if defined(HAVE_LINUX_NETWORK)
392 if (setsockopt(fd, IPPROTO_IP, IP_PKTINFO, &opt, sizeof(opt)) == -1)
393 goto err;
394 #elif defined(IP_RECVDSTADDR) && defined(IP_RECVIF)
395 if (setsockopt(fd, IPPROTO_IP, IP_RECVDSTADDR, &opt, sizeof(opt)) == -1 ||
396 setsockopt(fd, IPPROTO_IP, IP_RECVIF, &opt, sizeof(opt)) == -1)
397 goto err;
398 #endif
399 }
400 #ifdef HAVE_IPV6
401 else if (!set_ipv6pktinfo(fd))
402 goto err;
403 #endif
404 }
405
406 return fd;
407 }
408
409 #ifdef HAVE_IPV6
410 int set_ipv6pktinfo(int fd)
411 {
412 int opt = 1;
413
414 /* The API changed around Linux 2.6.14 but the old ABI is still supported:
415 handle all combinations of headers and kernel.
416 OpenWrt note that this fixes the problem addressed by your very broken patch. */
417 daemon->v6pktinfo = IPV6_PKTINFO;
418
419 #ifdef IPV6_RECVPKTINFO
420 if (setsockopt(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &opt, sizeof(opt)) != -1)
421 return 1;
422 # ifdef IPV6_2292PKTINFO
423 else if (errno == ENOPROTOOPT && setsockopt(fd, IPPROTO_IPV6, IPV6_2292PKTINFO, &opt, sizeof(opt)) != -1)
424 {
425 daemon->v6pktinfo = IPV6_2292PKTINFO;
426 return 1;
427 }
428 # endif
429 #else
430 if (setsockopt(fd, IPPROTO_IPV6, IPV6_PKTINFO, &opt, sizeof(opt)) != -1)
431 return 1;
432 #endif
433
434 return 0;
435 }
436 #endif
437
438 static struct listener *create_listeners(union mysockaddr *addr, int do_tftp, int dienow)
439 {
440 struct listener *l = NULL;
441 int fd = -1, tcpfd = -1, tftpfd = -1;
442
443 if (daemon->port != 0)
444 {
445 fd = make_sock(addr, SOCK_DGRAM, dienow);
446 tcpfd = make_sock(addr, SOCK_STREAM, dienow);
447 }
448
449 #ifdef HAVE_TFTP
450 if (do_tftp)
451 {
452 if (addr->sa.sa_family == AF_INET)
453 {
454 /* port must be restored to DNS port for TCP code */
455 short save = addr->in.sin_port;
456 addr->in.sin_port = htons(TFTP_PORT);
457 tftpfd = make_sock(addr, SOCK_DGRAM, dienow);
458 addr->in.sin_port = save;
459 }
460 # ifdef HAVE_IPV6
461 else
462 {
463 short save = addr->in6.sin6_port;
464 addr->in6.sin6_port = htons(TFTP_PORT);
465 tftpfd = make_sock(addr, SOCK_DGRAM, dienow);
466 addr->in6.sin6_port = save;
467 }
468 # endif
469 }
470 #endif
471
472 if (fd != -1 || tcpfd != -1 || tftpfd != -1)
473 {
474 l = safe_malloc(sizeof(struct listener));
475 l->next = NULL;
476 l->family = addr->sa.sa_family;
477 l->fd = fd;
478 l->tcpfd = tcpfd;
479 l->tftpfd = tftpfd;
480 }
481
482 return l;
483 }
484
485 void create_wildcard_listeners(void)
486 {
487 union mysockaddr addr;
488 struct listener *l;
489 int tftp_enabled = daemon->tftp_unlimited || daemon->tftp_interfaces;
490
491 memset(&addr, 0, sizeof(addr));
492 #ifdef HAVE_SOCKADDR_SA_LEN
493 addr.in.sin_len = sizeof(addr.in);
494 #endif
495 addr.in.sin_family = AF_INET;
496 addr.in.sin_addr.s_addr = INADDR_ANY;
497 addr.in.sin_port = htons(daemon->port);
498
499 l = create_listeners(&addr, tftp_enabled, 1);
500
501 #ifdef HAVE_IPV6
502 memset(&addr, 0, sizeof(addr));
503 # ifdef HAVE_SOCKADDR_SA_LEN
504 addr.in6.sin6_len = sizeof(addr.in6);
505 # endif
506 addr.in6.sin6_family = AF_INET6;
507 addr.in6.sin6_addr = in6addr_any;
508 addr.in6.sin6_port = htons(daemon->port);
509
510 if (l)
511 l->next = create_listeners(&addr, tftp_enabled, 1);
512 else
513 l = create_listeners(&addr, tftp_enabled, 1);
514 #endif
515
516 daemon->listeners = l;
517 }
518
519 void create_bound_listeners(int dienow)
520 {
521 struct listener *new;
522 struct irec *iface;
523 struct iname *if_tmp;
524
525 for (iface = daemon->interfaces; iface; iface = iface->next)
526 if (!iface->done && !iface->dad &&
527 (new = create_listeners(&iface->addr, iface->tftp_ok, dienow)))
528 {
529 new->iface = iface;
530 new->next = daemon->listeners;
531 daemon->listeners = new;
532 iface->done = 1;
533 }
534
535 /* Check for --listen-address options that haven't been used because there's
536 no interface with a matching address. These may be valid: eg it's possible
537 to listen on 127.0.1.1 even if the loopback interface is 127.0.0.1
538
539 If the address isn't valid the bind() will fail and we'll die().
540
541 The resulting listeners have the ->iface field NULL, and this has to be
542 handled by the DNS and TFTP code. It disables --localise-queries processing
543 (no netmask) and some MTU login the tftp code. */
544
545 for (if_tmp = daemon->if_addrs; if_tmp; if_tmp = if_tmp->next)
546 if (!if_tmp->used &&
547 (new = create_listeners(&if_tmp->addr, daemon->tftp_unlimited, dienow)))
548 {
549 new->iface = NULL;
550 new->next = daemon->listeners;
551 daemon->listeners = new;
552 }
553 }
554
555 int is_dad_listeners(void)
556 {
557 struct irec *iface;
558
559 if (option_bool(OPT_NOWILD))
560 for (iface = daemon->interfaces; iface; iface = iface->next)
561 if (iface->dad && !iface->done)
562 return 1;
563
564 return 0;
565 }
566 /* return a UDP socket bound to a random port, have to cope with straying into
567 occupied port nos and reserved ones. */
568 int random_sock(int family)
569 {
570 int fd;
571
572 if ((fd = socket(family, SOCK_DGRAM, 0)) != -1)
573 {
574 union mysockaddr addr;
575 unsigned int ports_avail = 65536u - (unsigned short)daemon->min_port;
576 int tries = ports_avail < 30 ? 3 * ports_avail : 100;
577
578 memset(&addr, 0, sizeof(addr));
579 addr.sa.sa_family = family;
580
581 /* don't loop forever if all ports in use. */
582
583 if (fix_fd(fd))
584 while(tries--)
585 {
586 unsigned short port = rand16();
587
588 if (daemon->min_port != 0)
589 port = htons(daemon->min_port + (port % ((unsigned short)ports_avail)));
590
591 if (family == AF_INET)
592 {
593 addr.in.sin_addr.s_addr = INADDR_ANY;
594 addr.in.sin_port = port;
595 #ifdef HAVE_SOCKADDR_SA_LEN
596 addr.in.sin_len = sizeof(struct sockaddr_in);
597 #endif
598 }
599 #ifdef HAVE_IPV6
600 else
601 {
602 addr.in6.sin6_addr = in6addr_any;
603 addr.in6.sin6_port = port;
604 #ifdef HAVE_SOCKADDR_SA_LEN
605 addr.in6.sin6_len = sizeof(struct sockaddr_in6);
606 #endif
607 }
608 #endif
609
610 if (bind(fd, (struct sockaddr *)&addr, sa_len(&addr)) == 0)
611 return fd;
612
613 if (errno != EADDRINUSE && errno != EACCES)
614 break;
615 }
616
617 close(fd);
618 }
619
620 return -1;
621 }
622
623
624 int local_bind(int fd, union mysockaddr *addr, char *intname, int is_tcp)
625 {
626 union mysockaddr addr_copy = *addr;
627
628 /* cannot set source _port_ for TCP connections. */
629 if (is_tcp)
630 {
631 if (addr_copy.sa.sa_family == AF_INET)
632 addr_copy.in.sin_port = 0;
633 #ifdef HAVE_IPV6
634 else
635 addr_copy.in6.sin6_port = 0;
636 #endif
637 }
638
639 if (bind(fd, (struct sockaddr *)&addr_copy, sa_len(&addr_copy)) == -1)
640 return 0;
641
642 #if defined(SO_BINDTODEVICE)
643 if (intname[0] != 0 &&
644 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, intname, IF_NAMESIZE) == -1)
645 return 0;
646 #endif
647
648 return 1;
649 }
650
651 static struct serverfd *allocate_sfd(union mysockaddr *addr, char *intname)
652 {
653 struct serverfd *sfd;
654 int errsave;
655
656 /* when using random ports, servers which would otherwise use
657 the INADDR_ANY/port0 socket have sfd set to NULL */
658 if (!daemon->osport && intname[0] == 0)
659 {
660 errno = 0;
661
662 if (addr->sa.sa_family == AF_INET &&
663 addr->in.sin_addr.s_addr == INADDR_ANY &&
664 addr->in.sin_port == htons(0))
665 return NULL;
666
667 #ifdef HAVE_IPV6
668 if (addr->sa.sa_family == AF_INET6 &&
669 memcmp(&addr->in6.sin6_addr, &in6addr_any, sizeof(in6addr_any)) == 0 &&
670 addr->in6.sin6_port == htons(0))
671 return NULL;
672 #endif
673 }
674
675 /* may have a suitable one already */
676 for (sfd = daemon->sfds; sfd; sfd = sfd->next )
677 if (sockaddr_isequal(&sfd->source_addr, addr) &&
678 strcmp(intname, sfd->interface) == 0)
679 return sfd;
680
681 /* need to make a new one. */
682 errno = ENOMEM; /* in case malloc fails. */
683 if (!(sfd = whine_malloc(sizeof(struct serverfd))))
684 return NULL;
685
686 if ((sfd->fd = socket(addr->sa.sa_family, SOCK_DGRAM, 0)) == -1)
687 {
688 free(sfd);
689 return NULL;
690 }
691
692 if (!local_bind(sfd->fd, addr, intname, 0) || !fix_fd(sfd->fd))
693 {
694 errsave = errno; /* save error from bind. */
695 close(sfd->fd);
696 free(sfd);
697 errno = errsave;
698 return NULL;
699 }
700
701 strcpy(sfd->interface, intname);
702 sfd->source_addr = *addr;
703 sfd->next = daemon->sfds;
704 daemon->sfds = sfd;
705 return sfd;
706 }
707
708 /* create upstream sockets during startup, before root is dropped which may be needed
709 this allows query_port to be a low port and interface binding */
710 void pre_allocate_sfds(void)
711 {
712 struct server *srv;
713
714 if (daemon->query_port != 0)
715 {
716 union mysockaddr addr;
717 memset(&addr, 0, sizeof(addr));
718 addr.in.sin_family = AF_INET;
719 addr.in.sin_addr.s_addr = INADDR_ANY;
720 addr.in.sin_port = htons(daemon->query_port);
721 #ifdef HAVE_SOCKADDR_SA_LEN
722 addr.in.sin_len = sizeof(struct sockaddr_in);
723 #endif
724 allocate_sfd(&addr, "");
725 #ifdef HAVE_IPV6
726 memset(&addr, 0, sizeof(addr));
727 addr.in6.sin6_family = AF_INET6;
728 addr.in6.sin6_addr = in6addr_any;
729 addr.in6.sin6_port = htons(daemon->query_port);
730 #ifdef HAVE_SOCKADDR_SA_LEN
731 addr.in6.sin6_len = sizeof(struct sockaddr_in6);
732 #endif
733 allocate_sfd(&addr, "");
734 #endif
735 }
736
737 for (srv = daemon->servers; srv; srv = srv->next)
738 if (!(srv->flags & (SERV_LITERAL_ADDRESS | SERV_NO_ADDR | SERV_USE_RESOLV | SERV_NO_REBIND)) &&
739 !allocate_sfd(&srv->source_addr, srv->interface) &&
740 errno != 0 &&
741 option_bool(OPT_NOWILD))
742 {
743 prettyprint_addr(&srv->source_addr, daemon->namebuff);
744 if (srv->interface[0] != 0)
745 {
746 strcat(daemon->namebuff, " ");
747 strcat(daemon->namebuff, srv->interface);
748 }
749 die(_("failed to bind server socket for %s: %s"),
750 daemon->namebuff, EC_BADNET);
751 }
752 }
753
754
755 void check_servers(void)
756 {
757 struct irec *iface;
758 struct server *new, *tmp, *ret = NULL;
759 int port = 0;
760
761 /* interface may be new since startup */
762 if (!option_bool(OPT_NOWILD))
763 enumerate_interfaces();
764
765 for (new = daemon->servers; new; new = tmp)
766 {
767 tmp = new->next;
768
769 if (!(new->flags & (SERV_LITERAL_ADDRESS | SERV_NO_ADDR | SERV_USE_RESOLV | SERV_NO_REBIND)))
770 {
771 port = prettyprint_addr(&new->addr, daemon->namebuff);
772
773 /* 0.0.0.0 is nothing, the stack treats it like 127.0.0.1 */
774 if (new->addr.sa.sa_family == AF_INET &&
775 new->addr.in.sin_addr.s_addr == 0)
776 {
777 free(new);
778 continue;
779 }
780
781 for (iface = daemon->interfaces; iface; iface = iface->next)
782 if (sockaddr_isequal(&new->addr, &iface->addr))
783 break;
784 if (iface)
785 {
786 my_syslog(LOG_WARNING, _("ignoring nameserver %s - local interface"), daemon->namebuff);
787 free(new);
788 continue;
789 }
790
791 /* Do we need a socket set? */
792 if (!new->sfd &&
793 !(new->sfd = allocate_sfd(&new->source_addr, new->interface)) &&
794 errno != 0)
795 {
796 my_syslog(LOG_WARNING,
797 _("ignoring nameserver %s - cannot make/bind socket: %s"),
798 daemon->namebuff, strerror(errno));
799 free(new);
800 continue;
801 }
802 }
803
804 /* reverse order - gets it right. */
805 new->next = ret;
806 ret = new;
807
808 if (!(new->flags & SERV_NO_REBIND))
809 {
810 if (new->flags & (SERV_HAS_DOMAIN | SERV_FOR_NODOTS | SERV_USE_RESOLV))
811 {
812 char *s1, *s2;
813 if (!(new->flags & SERV_HAS_DOMAIN))
814 s1 = _("unqualified"), s2 = _("names");
815 else if (strlen(new->domain) == 0)
816 s1 = _("default"), s2 = "";
817 else
818 s1 = _("domain"), s2 = new->domain;
819
820 if (new->flags & SERV_NO_ADDR)
821 my_syslog(LOG_INFO, _("using local addresses only for %s %s"), s1, s2);
822 else if (new->flags & SERV_USE_RESOLV)
823 my_syslog(LOG_INFO, _("using standard nameservers for %s %s"), s1, s2);
824 else if (!(new->flags & SERV_LITERAL_ADDRESS))
825 my_syslog(LOG_INFO, _("using nameserver %s#%d for %s %s"), daemon->namebuff, port, s1, s2);
826 }
827 else if (new->interface[0] != 0)
828 my_syslog(LOG_INFO, _("using nameserver %s#%d(via %s)"), daemon->namebuff, port, new->interface);
829 else
830 my_syslog(LOG_INFO, _("using nameserver %s#%d"), daemon->namebuff, port);
831 }
832 }
833
834 daemon->servers = ret;
835 }
836
837 /* Return zero if no servers found, in that case we keep polling.
838 This is a protection against an update-time/write race on resolv.conf */
839 int reload_servers(char *fname)
840 {
841 FILE *f;
842 char *line;
843 struct server *old_servers = NULL;
844 struct server *new_servers = NULL;
845 struct server *serv;
846 int gotone = 0;
847
848 /* buff happens to be MAXDNAME long... */
849 if (!(f = fopen(fname, "r")))
850 {
851 my_syslog(LOG_ERR, _("failed to read %s: %s"), fname, strerror(errno));
852 return 0;
853 }
854
855 /* move old servers to free list - we can reuse the memory
856 and not risk malloc if there are the same or fewer new servers.
857 Servers which were specced on the command line go to the new list. */
858 for (serv = daemon->servers; serv;)
859 {
860 struct server *tmp = serv->next;
861 if (serv->flags & SERV_FROM_RESOLV)
862 {
863 serv->next = old_servers;
864 old_servers = serv;
865 /* forward table rules reference servers, so have to blow them away */
866 server_gone(serv);
867 }
868 else
869 {
870 serv->next = new_servers;
871 new_servers = serv;
872 }
873 serv = tmp;
874 }
875
876 while ((line = fgets(daemon->namebuff, MAXDNAME, f)))
877 {
878 union mysockaddr addr, source_addr;
879 char *token = strtok(line, " \t\n\r");
880
881 if (!token)
882 continue;
883 if (strcmp(token, "nameserver") != 0 && strcmp(token, "server") != 0)
884 continue;
885 if (!(token = strtok(NULL, " \t\n\r")))
886 continue;
887
888 memset(&addr, 0, sizeof(addr));
889 memset(&source_addr, 0, sizeof(source_addr));
890
891 if ((addr.in.sin_addr.s_addr = inet_addr(token)) != (in_addr_t) -1)
892 {
893 #ifdef HAVE_SOCKADDR_SA_LEN
894 source_addr.in.sin_len = addr.in.sin_len = sizeof(source_addr.in);
895 #endif
896 source_addr.in.sin_family = addr.in.sin_family = AF_INET;
897 addr.in.sin_port = htons(NAMESERVER_PORT);
898 source_addr.in.sin_addr.s_addr = INADDR_ANY;
899 source_addr.in.sin_port = htons(daemon->query_port);
900 }
901 #ifdef HAVE_IPV6
902 else
903 {
904 int scope_index = 0;
905 char *scope_id = strchr(token, '%');
906
907 if (scope_id)
908 {
909 *(scope_id++) = 0;
910 scope_index = if_nametoindex(scope_id);
911 }
912
913 if (inet_pton(AF_INET6, token, &addr.in6.sin6_addr) > 0)
914 {
915 #ifdef HAVE_SOCKADDR_SA_LEN
916 source_addr.in6.sin6_len = addr.in6.sin6_len = sizeof(source_addr.in6);
917 #endif
918 source_addr.in6.sin6_family = addr.in6.sin6_family = AF_INET6;
919 source_addr.in6.sin6_flowinfo = addr.in6.sin6_flowinfo = 0;
920 addr.in6.sin6_port = htons(NAMESERVER_PORT);
921 addr.in6.sin6_scope_id = scope_index;
922 source_addr.in6.sin6_addr = in6addr_any;
923 source_addr.in6.sin6_port = htons(daemon->query_port);
924 source_addr.in6.sin6_scope_id = 0;
925 }
926 else
927 continue;
928 }
929 #else /* IPV6 */
930 else
931 continue;
932 #endif
933
934 if (old_servers)
935 {
936 serv = old_servers;
937 old_servers = old_servers->next;
938 }
939 else if (!(serv = whine_malloc(sizeof (struct server))))
940 continue;
941
942 /* this list is reverse ordered:
943 it gets reversed again in check_servers */
944 serv->next = new_servers;
945 new_servers = serv;
946 serv->addr = addr;
947 serv->source_addr = source_addr;
948 serv->domain = NULL;
949 serv->interface[0] = 0;
950 serv->sfd = NULL;
951 serv->flags = SERV_FROM_RESOLV;
952 serv->queries = serv->failed_queries = 0;
953 gotone = 1;
954 }
955
956 /* Free any memory not used. */
957 while (old_servers)
958 {
959 struct server *tmp = old_servers->next;
960 free(old_servers);
961 old_servers = tmp;
962 }
963
964 daemon->servers = new_servers;
965 fclose(f);
966
967 return gotone;
968 }
969
970
971 /* Use an IPv4 listener socket for ioctling */
972 struct in_addr get_ifaddr(char *intr)
973 {
974 struct listener *l;
975 struct ifreq ifr;
976 struct sockaddr_in ret;
977
978 ret.sin_addr.s_addr = -1;
979
980 for (l = daemon->listeners;
981 l && (l->family != AF_INET || l->fd == -1);
982 l = l->next);
983
984 strncpy(ifr.ifr_name, intr, IF_NAMESIZE);
985 ifr.ifr_addr.sa_family = AF_INET;
986
987 if (l && ioctl(l->fd, SIOCGIFADDR, &ifr) != -1)
988 memcpy(&ret, &ifr.ifr_addr, sizeof(ret));
989
990 return ret.sin_addr;
991 }
992
993
994