1 /* dnsmasq is Copyright (c) 2000-2009 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 static const char SEPARATOR[] = "|";
20 
21 #ifdef HAVE_LINUX_NETWORK
22 
indextoname(int fd,int index,char * name)23 int indextoname(int fd, int index, char *name)
24 {
25   struct ifreq ifr;
26 
27   if (index == 0)
28     return 0;
29 
30   ifr.ifr_ifindex = index;
31   if (ioctl(fd, SIOCGIFNAME, &ifr) == -1)
32     return 0;
33 
34   strncpy(name, ifr.ifr_name, IF_NAMESIZE);
35 
36   return 1;
37 }
38 
39 #else
40 
indextoname(int fd,int index,char * name)41 int indextoname(int fd, int index, char *name)
42 {
43   if (index == 0 || !if_indextoname(index, name))
44     return 0;
45 
46   return 1;
47 }
48 
49 #endif
50 
iface_check(int family,struct all_addr * addr,char * name,int * indexp)51 int iface_check(int family, struct all_addr *addr, char *name, int *indexp)
52 {
53   struct iname *tmp;
54   int ret = 1;
55 
56   /* Note: have to check all and not bail out early, so that we set the
57      "used" flags. */
58 
59   if (indexp)
60     {
61       /* One form of bridging on BSD has the property that packets
62 	 can be recieved on bridge interfaces which do not have an IP address.
63 	 We allow these to be treated as aliases of another interface which does have
64 	 an IP address with --dhcp-bridge=interface,alias,alias */
65       struct dhcp_bridge *bridge, *alias;
66       for (bridge = daemon->bridges; bridge; bridge = bridge->next)
67 	{
68 	  for (alias = bridge->alias; alias; alias = alias->next)
69 	    if (strncmp(name, alias->iface, IF_NAMESIZE) == 0)
70 	      {
71 		int newindex;
72 
73 		if (!(newindex = if_nametoindex(bridge->iface)))
74 		  {
75 		    my_syslog(LOG_WARNING, _("unknown interface %s in bridge-interface"), name);
76 		    return 0;
77 		  }
78 		else
79 		  {
80 		    *indexp = newindex;
81 		    strncpy(name,  bridge->iface, IF_NAMESIZE);
82 		    break;
83 		  }
84 	      }
85 	  if (alias)
86 	    break;
87 	}
88     }
89 
90   if (daemon->if_names || (addr && daemon->if_addrs))
91     {
92       ret = 0;
93 
94       for (tmp = daemon->if_names; tmp; tmp = tmp->next)
95 	if (tmp->name && (strcmp(tmp->name, name) == 0))
96 	  ret = tmp->used = 1;
97 
98       for (tmp = daemon->if_addrs; tmp; tmp = tmp->next)
99 	if (addr && tmp->addr.sa.sa_family == family)
100 	  {
101 	    if (family == AF_INET &&
102 		tmp->addr.in.sin_addr.s_addr == addr->addr.addr4.s_addr)
103 	      ret = tmp->used = 1;
104 #ifdef HAVE_IPV6
105 	    else if (family == AF_INET6 &&
106 		     IN6_ARE_ADDR_EQUAL(&tmp->addr.in6.sin6_addr,
107 					&addr->addr.addr6) &&
108 		     (!IN6_IS_ADDR_LINKLOCAL(&addr->addr.addr6) ||
109 		      (tmp->addr.in6.sin6_scope_id == (uint32_t) *indexp)))
110 	      ret = tmp->used = 1;
111 #endif
112 	  }
113     }
114 
115   for (tmp = daemon->if_except; tmp; tmp = tmp->next)
116     if (tmp->name && (strcmp(tmp->name, name) == 0))
117       ret = 0;
118 
119   return ret;
120 }
121 
iface_allowed(struct irec ** irecp,int if_index,union mysockaddr * addr,struct in_addr netmask)122 static int iface_allowed(struct irec **irecp, int if_index,
123 			 union mysockaddr *addr, struct in_addr netmask)
124 {
125   struct irec *iface;
126   int fd, mtu = 0, loopback;
127   struct ifreq ifr;
128   int dhcp_ok = 1;
129   struct iname *tmp;
130 
131   /* check whether the interface IP has been added already
132      we call this routine multiple times. */
133   for (iface = *irecp; iface; iface = iface->next)
134     if (sockaddr_isequal(&iface->addr, addr))
135       return 1;
136 
137   if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) == -1 ||
138       !indextoname(fd, if_index, ifr.ifr_name) ||
139       ioctl(fd, SIOCGIFFLAGS, &ifr) == -1)
140     {
141       if (fd != -1)
142 	{
143 	  int errsave = errno;
144 	  close(fd);
145 	  errno = errsave;
146 	}
147       return 0;
148     }
149 
150   loopback = ifr.ifr_flags & IFF_LOOPBACK;
151 
152   if (ioctl(fd, SIOCGIFMTU, &ifr) != -1)
153     mtu = ifr.ifr_mtu;
154 
155   close(fd);
156 
157   /* If we are restricting the set of interfaces to use, make
158      sure that loopback interfaces are in that set. */
159   if (daemon->if_names && loopback)
160     {
161       struct iname *lo;
162       for (lo = daemon->if_names; lo; lo = lo->next)
163 	if (lo->name && strcmp(lo->name, ifr.ifr_name) == 0)
164 	  {
165 	    lo->isloop = 1;
166 	    break;
167 	  }
168 
169       if (!lo &&
170 	  (lo = whine_malloc(sizeof(struct iname))) &&
171 	  (lo->name = whine_malloc(strlen(ifr.ifr_name)+1)))
172 	{
173 	  strcpy(lo->name, ifr.ifr_name);
174 	  lo->isloop = lo->used = 1;
175 	  lo->next = daemon->if_names;
176 	  daemon->if_names = lo;
177 	}
178     }
179 
180   if (addr->sa.sa_family == AF_INET &&
181       !iface_check(AF_INET, (struct all_addr *)&addr->in.sin_addr, ifr.ifr_name, NULL))
182     return 1;
183 
184   for (tmp = daemon->dhcp_except; tmp; tmp = tmp->next)
185     if (tmp->name && (strcmp(tmp->name, ifr.ifr_name) == 0))
186       dhcp_ok = 0;
187 
188 #ifdef HAVE_IPV6
189   int ifindex = (int) addr->in6.sin6_scope_id;
190   if (addr->sa.sa_family == AF_INET6 &&
191       !iface_check(AF_INET6, (struct all_addr *)&addr->in6.sin6_addr, ifr.ifr_name, &ifindex))
192     return 1;
193 #endif
194 
195   /* add to list */
196   if ((iface = whine_malloc(sizeof(struct irec))))
197     {
198       iface->addr = *addr;
199       iface->netmask = netmask;
200       iface->dhcp_ok = dhcp_ok;
201       iface->mtu = mtu;
202       iface->next = *irecp;
203       *irecp = iface;
204       return 1;
205     }
206 
207   errno = ENOMEM;
208   return 0;
209 }
210 
211 #ifdef HAVE_IPV6
iface_allowed_v6(struct in6_addr * local,int scope,int if_index,void * vparam)212 static int iface_allowed_v6(struct in6_addr *local,
213 			    int scope, int if_index, void *vparam)
214 {
215   union mysockaddr addr;
216   struct in_addr netmask; /* dummy */
217 
218   netmask.s_addr = 0;
219 
220   memset(&addr, 0, sizeof(addr));
221 #ifdef HAVE_SOCKADDR_SA_LEN
222   addr.in6.sin6_len = sizeof(addr.in6);
223 #endif
224   addr.in6.sin6_family = AF_INET6;
225   addr.in6.sin6_addr = *local;
226   addr.in6.sin6_port = htons(daemon->port);
227   /**
228    * Only populate the scope ID if the address is link-local.
229    * Scope IDs are not meaningful for global addresses. Also, we do not want to
230    * think that two addresses are different if they differ only in scope ID,
231    * because the kernel will treat them as if they are the same.
232    */
233   if (IN6_IS_ADDR_LINKLOCAL(local)) {
234     addr.in6.sin6_scope_id = scope;
235   }
236 
237   return iface_allowed((struct irec **)vparam, if_index, &addr, netmask);
238 }
239 #endif
240 
iface_allowed_v4(struct in_addr local,int if_index,struct in_addr netmask,struct in_addr broadcast,void * vparam)241 static int iface_allowed_v4(struct in_addr local, int if_index,
242 			    struct in_addr netmask, struct in_addr broadcast, void *vparam)
243 {
244   union mysockaddr addr;
245 
246   memset(&addr, 0, sizeof(addr));
247 #ifdef HAVE_SOCKADDR_SA_LEN
248   addr.in.sin_len = sizeof(addr.in);
249 #endif
250   addr.in.sin_family = AF_INET;
251   addr.in.sin_addr = broadcast; /* warning */
252   addr.in.sin_addr = local;
253   addr.in.sin_port = htons(daemon->port);
254 
255   return iface_allowed((struct irec **)vparam, if_index, &addr, netmask);
256 }
257 
enumerate_interfaces(void)258 int enumerate_interfaces(void)
259 {
260 #ifdef HAVE_IPV6
261   return iface_enumerate(&daemon->interfaces, iface_allowed_v4, iface_allowed_v6);
262 #else
263   return iface_enumerate(&daemon->interfaces, iface_allowed_v4, NULL);
264 #endif
265 }
266 
267 /* set NONBLOCK bit on fd: See Stevens 16.6 */
fix_fd(int fd)268 int fix_fd(int fd)
269 {
270   int flags;
271 
272   if ((flags = fcntl(fd, F_GETFL)) == -1 ||
273       fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
274     return 0;
275 
276   return 1;
277 }
278 
279 #if defined(HAVE_IPV6)
create_ipv6_listener(struct listener ** link,int port)280 static int create_ipv6_listener(struct listener **link, int port)
281 {
282   union mysockaddr addr;
283   int tcpfd, fd;
284   struct listener *l;
285   int opt = 1;
286 
287   memset(&addr, 0, sizeof(addr));
288   addr.in6.sin6_family = AF_INET6;
289   addr.in6.sin6_addr = in6addr_any;
290   addr.in6.sin6_port = htons(port);
291 #ifdef HAVE_SOCKADDR_SA_LEN
292   addr.in6.sin6_len = sizeof(addr.in6);
293 #endif
294 
295   /* No error of the kernel doesn't support IPv6 */
296   if ((fd = socket(AF_INET6, SOCK_DGRAM, 0)) == -1)
297     return (errno == EPROTONOSUPPORT ||
298 	    errno == EAFNOSUPPORT ||
299 	    errno == EINVAL);
300 
301   if ((tcpfd = socket(AF_INET6, SOCK_STREAM, 0)) == -1)
302     return 0;
303 
304   if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) == -1 ||
305       setsockopt(tcpfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) == -1 ||
306       setsockopt(fd, IPV6_LEVEL, IPV6_V6ONLY, &opt, sizeof(opt)) == -1 ||
307       setsockopt(tcpfd, IPV6_LEVEL, IPV6_V6ONLY, &opt, sizeof(opt)) == -1 ||
308       !fix_fd(fd) ||
309       !fix_fd(tcpfd) ||
310 #ifdef IPV6_RECVPKTINFO
311       setsockopt(fd, IPV6_LEVEL, IPV6_RECVPKTINFO, &opt, sizeof(opt)) == -1 ||
312 #else
313       setsockopt(fd, IPV6_LEVEL, IPV6_PKTINFO, &opt, sizeof(opt)) == -1 ||
314 #endif
315       bind(tcpfd, (struct sockaddr *)&addr, sa_len(&addr)) == -1 ||
316       listen(tcpfd, 5) == -1 ||
317       bind(fd, (struct sockaddr *)&addr, sa_len(&addr)) == -1)
318     return 0;
319 
320   l = safe_malloc(sizeof(struct listener));
321   l->fd = fd;
322   l->tcpfd = tcpfd;
323   l->tftpfd = -1;
324   l->family = AF_INET6;
325   l->iface = NULL;
326   l->next = NULL;
327   *link = l;
328 
329   return 1;
330 }
331 #endif
332 
create_wildcard_listeners(void)333 struct listener *create_wildcard_listeners(void)
334 {
335   union mysockaddr addr;
336   int opt = 1;
337   struct listener *l, *l6 = NULL;
338   int tcpfd = -1, fd = -1, tftpfd = -1;
339 
340   memset(&addr, 0, sizeof(addr));
341   addr.in.sin_family = AF_INET;
342   addr.in.sin_addr.s_addr = INADDR_ANY;
343   addr.in.sin_port = htons(daemon->port);
344 #ifdef HAVE_SOCKADDR_SA_LEN
345   addr.in.sin_len = sizeof(struct sockaddr_in);
346 #endif
347 
348   if (daemon->port != 0)
349     {
350 
351       if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1 ||
352 	  (tcpfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
353 	return NULL;
354 
355       if (setsockopt(tcpfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) == -1 ||
356 	  bind(tcpfd, (struct sockaddr *)&addr, sa_len(&addr)) == -1 ||
357 	  listen(tcpfd, 5) == -1 ||
358 	  !fix_fd(tcpfd) ||
359 #ifdef HAVE_IPV6
360 	  !create_ipv6_listener(&l6, daemon->port) ||
361 #endif
362 	  setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) == -1 ||
363 	  !fix_fd(fd) ||
364 #if defined(HAVE_LINUX_NETWORK)
365 	  setsockopt(fd, SOL_IP, IP_PKTINFO, &opt, sizeof(opt)) == -1 ||
366 #elif defined(IP_RECVDSTADDR) && defined(IP_RECVIF)
367 	  setsockopt(fd, IPPROTO_IP, IP_RECVDSTADDR, &opt, sizeof(opt)) == -1 ||
368 	  setsockopt(fd, IPPROTO_IP, IP_RECVIF, &opt, sizeof(opt)) == -1 ||
369 #endif
370 	  bind(fd, (struct sockaddr *)&addr, sa_len(&addr)) == -1)
371 	return NULL;
372 
373 #ifdef __ANDROID__
374       uint32_t mark = daemon->listen_mark;
375       if (mark != 0 &&
376 	  (setsockopt(fd, SOL_SOCKET, SO_MARK, &mark, sizeof(mark)) == -1 ||
377 	   setsockopt(tcpfd, SOL_SOCKET, SO_MARK, &mark, sizeof(mark)) == -1 ||
378 	   setsockopt(l6->fd, SOL_SOCKET, SO_MARK, &mark, sizeof(mark)) == -1 ||
379 	   setsockopt(l6->tcpfd, SOL_SOCKET, SO_MARK, &mark, sizeof(mark)) == -1))
380       {
381 	my_syslog(LOG_WARNING, _("setsockopt(SO_MARK, 0x%x: %s"), mark, strerror(errno));
382 	close(fd);
383 	close(tcpfd);
384 	close(l6->fd);
385 	close(l6->tcpfd);
386 	return NULL;
387       }
388     }
389 #endif /* __ANDROID__ */
390 
391 #ifdef HAVE_TFTP
392   if (daemon->options & OPT_TFTP)
393     {
394       addr.in.sin_port = htons(TFTP_PORT);
395       if ((tftpfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
396 	return NULL;
397 
398       if (!fix_fd(tftpfd) ||
399 #if defined(HAVE_LINUX_NETWORK)
400 	  setsockopt(tftpfd, SOL_IP, IP_PKTINFO, &opt, sizeof(opt)) == -1 ||
401 #elif defined(IP_RECVDSTADDR) && defined(IP_RECVIF)
402 	  setsockopt(tftpfd, IPPROTO_IP, IP_RECVDSTADDR, &opt, sizeof(opt)) == -1 ||
403 	  setsockopt(tftpfd, IPPROTO_IP, IP_RECVIF, &opt, sizeof(opt)) == -1 ||
404 #endif
405 	  bind(tftpfd, (struct sockaddr *)&addr, sa_len(&addr)) == -1)
406 	return NULL;
407     }
408 #endif
409 
410   l = safe_malloc(sizeof(struct listener));
411   l->family = AF_INET;
412   l->fd = fd;
413   l->tcpfd = tcpfd;
414   l->tftpfd = tftpfd;
415   l->iface = NULL;
416   l->next = l6;
417 
418   return l;
419 }
420 
421 #ifdef __ANDROID__
422 /**
423  * for a single given irec (interface name and address) create
424  * a set of sockets listening.  This is a copy of the code inside the loop
425  * of create_bound_listeners below and is added here to allow us
426  * to create just a single new listener dynamically when our interface
427  * list is changed.
428  *
429  * iface - input of the new interface details to listen on
430  * listeners - output.  Creates a new struct listener and inserts at head of the list
431  *
432  * die's on errors, so don't pass bad data.
433  */
create_bound_listener(struct listener ** listeners,struct irec * iface)434 void create_bound_listener(struct listener **listeners, struct irec *iface)
435 {
436   int rc, opt = 1;
437 #ifdef HAVE_IPV6
438   static int dad_count = 0;
439 #endif
440 
441   struct listener *new = safe_malloc(sizeof(struct listener));
442   new->family = iface->addr.sa.sa_family;
443   new->iface = iface;
444   new->next = *listeners;
445   new->tftpfd = -1;
446   new->tcpfd = -1;
447   new->fd = -1;
448   *listeners = new;
449 
450   if (daemon->port != 0)
451   {
452     if ((new->tcpfd = socket(iface->addr.sa.sa_family, SOCK_STREAM, 0)) == -1 ||
453         (new->fd = socket(iface->addr.sa.sa_family, SOCK_DGRAM, 0)) == -1 ||
454         setsockopt(new->fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) == -1 ||
455         setsockopt(new->tcpfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) == -1 ||
456         !fix_fd(new->tcpfd) ||
457         !fix_fd(new->fd))
458       die(_("failed to create listening socket: %s"), NULL, EC_BADNET);
459 
460 #ifdef HAVE_IPV6
461     if (iface->addr.sa.sa_family == AF_INET6)
462     {
463       if (setsockopt(new->fd, IPV6_LEVEL, IPV6_V6ONLY, &opt, sizeof(opt)) == -1 ||
464           setsockopt(new->tcpfd, IPV6_LEVEL, IPV6_V6ONLY, &opt, sizeof(opt)) == -1)
465         die(_("failed to set IPV6 options on listening socket: %s"), NULL, EC_BADNET);\
466     }
467 #endif
468 
469     while(1)
470     {
471       if ((rc = bind(new->fd, &iface->addr.sa, sa_len(&iface->addr))) != -1)
472         break;
473 
474 #ifdef HAVE_IPV6
475       /* An interface may have an IPv6 address which is still undergoing DAD.
476          If so, the bind will fail until the DAD completes, so we try over 20 seconds
477          before failing. */
478       /* TODO: What to do here? 20 seconds is way too long. We use optimistic addresses, so bind()
479          will only fail if the address has already failed DAD, in which case retrying won't help. */
480       if (iface->addr.sa.sa_family == AF_INET6 && (errno == ENODEV || errno == EADDRNOTAVAIL) &&
481           dad_count++ < DAD_WAIT)
482       {
483         sleep(1);
484         continue;
485       }
486 #endif
487       break;
488     }
489 
490     if (rc == -1 || bind(new->tcpfd, &iface->addr.sa, sa_len(&iface->addr)) == -1)
491     {
492       prettyprint_addr(&iface->addr, daemon->namebuff);
493       die(_("failed to bind listening socket for %s: %s"), daemon->namebuff, EC_BADNET);
494     }
495 
496     uint32_t mark = daemon->listen_mark;
497     if (mark != 0 &&
498 	(setsockopt(new->fd, SOL_SOCKET, SO_MARK, &mark, sizeof(mark)) == -1 ||
499 	 setsockopt(new->tcpfd, SOL_SOCKET, SO_MARK, &mark, sizeof(mark)) == -1))
500       die(_("failed to set SO_MARK on listen socket: %s"), NULL, EC_BADNET);
501 
502     if (listen(new->tcpfd, 5) == -1)
503       die(_("failed to listen on socket: %s"), NULL, EC_BADNET);
504   }
505 
506 #ifdef HAVE_TFTP
507   if ((daemon->options & OPT_TFTP) && iface->addr.sa.sa_family == AF_INET && iface->dhcp_ok)
508   {
509     short save = iface->addr.in.sin_port;
510     iface->addr.in.sin_port = htons(TFTP_PORT);
511     if ((new->tftpfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1 ||
512         setsockopt(new->tftpfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) == -1 ||
513         !fix_fd(new->tftpfd) ||
514         bind(new->tftpfd, &iface->addr.sa, sa_len(&iface->addr)) == -1)
515       die(_("failed to create TFTP socket: %s"), NULL, EC_BADNET);
516     iface->addr.in.sin_port = save;
517   }
518 #endif
519 }
520 
521 /**
522  * If a listener has a struct irec pointer whose address matches the newly
523  * malloc()d struct irec's address, update its pointer to refer to this new
524  * struct irec instance.
525  *
526  * Otherwise, any listeners that are preserved across interface list changes
527  * will point at interface structures that are free()d at the end of
528  * set_interfaces(), and can get overwritten by subsequent memory allocations.
529  *
530  * See b/17475756 for further discussion.
531  */
fixup_possible_existing_listener(struct irec * new_iface)532 void fixup_possible_existing_listener(struct irec *new_iface) {
533   /* find the listener, if present */
534   struct listener *l;
535   for (l = daemon->listeners; l; l = l->next) {
536     struct irec *listener_iface = l->iface;
537     if (listener_iface) {
538       if (sockaddr_isequal(&listener_iface->addr, &new_iface->addr)) {
539         l->iface = new_iface;
540         return;
541       }
542     }
543   }
544 }
545 
546 /**
547  * Closes the sockets of the specified listener, deletes it from the list, and frees it.
548  *
549  */
delete_listener(struct listener ** l)550 int delete_listener(struct listener **l)
551 {
552   struct listener *listener = *l;
553   if (listener == NULL) return 0;
554 
555   if (listener->iface) {
556     int port = prettyprint_addr(&listener->iface->addr, daemon->namebuff);
557     my_syslog(LOG_INFO, _("Closing listener [%s]:%d"), daemon->namebuff, port);
558   } else {
559     my_syslog(LOG_INFO, _("Closing wildcard listener family=%d"), listener->family);
560   }
561 
562   if (listener->tftpfd != -1)
563   {
564     close(listener->tftpfd);
565     listener->tftpfd = -1;
566   }
567   if (listener->tcpfd != -1)
568   {
569     close(listener->tcpfd);
570     listener->tcpfd = -1;
571   }
572   if (listener->fd != -1)
573   {
574     close(listener->fd);
575     listener->fd = -1;
576   }
577   *l = listener->next;
578   free(listener);
579   return -1;
580 }
581 
582 /**
583  * Close the sockets listening on the given interface
584  *
585  * This new function is needed as we're dynamically changing the interfaces
586  * we listen on.  Before they'd be opened once in create_bound_listeners and stay
587  * until we exited.  Now, if an interface moves off the to-listen list we need to
588  * close out the listeners and keep trucking.
589  *
590  * interface - input of the interface details to listen on
591  */
close_bound_listener(struct irec * iface)592 int close_bound_listener(struct irec *iface)
593 {
594   /* find the listener */
595   int ret = 0;
596   struct listener **l = &daemon->listeners;
597   while (*l) {
598     struct irec *listener_iface = (*l)->iface;
599     struct listener **next = &((*l)->next);
600     if (iface && listener_iface && sockaddr_isequal(&listener_iface->addr, &iface->addr)) {
601       // Listener bound to an IP address. There can be only one of these.
602       ret = delete_listener(l);
603       break;
604     }
605     if (iface == NULL && listener_iface == NULL) {
606       // Wildcard listener. There is one of these per address family.
607       ret = delete_listener(l);
608       continue;
609     }
610     l = next;
611   }
612   return ret;
613 }
614 #endif /* __ANDROID__ */
615 
create_bound_listeners(void)616 struct listener *create_bound_listeners(void)
617 {
618   struct listener *listeners = NULL;
619   struct irec *iface;
620 #ifndef __ANDROID__
621   int rc, opt = 1;
622 #ifdef HAVE_IPV6
623   static int dad_count = 0;
624 #endif
625 #endif
626 
627   for (iface = daemon->interfaces; iface; iface = iface->next)
628     {
629 #ifdef __ANDROID__
630       create_bound_listener(&listeners, iface);
631 #else
632       struct listener *new = safe_malloc(sizeof(struct listener));
633       new->family = iface->addr.sa.sa_family;
634       new->iface = iface;
635       new->next = listeners;
636       new->tftpfd = -1;
637       new->tcpfd = -1;
638       new->fd = -1;
639       listeners = new;
640 
641       if (daemon->port != 0)
642 	{
643 	  if ((new->tcpfd = socket(iface->addr.sa.sa_family, SOCK_STREAM, 0)) == -1 ||
644 	      (new->fd = socket(iface->addr.sa.sa_family, SOCK_DGRAM, 0)) == -1 ||
645 	      setsockopt(new->fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) == -1 ||
646 	      setsockopt(new->tcpfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) == -1 ||
647 	      !fix_fd(new->tcpfd) ||
648 	      !fix_fd(new->fd))
649 	    die(_("failed to create listening socket: %s"), NULL, EC_BADNET);
650 
651 #ifdef HAVE_IPV6
652 	  if (iface->addr.sa.sa_family == AF_INET6)
653 	    {
654 	      if (setsockopt(new->fd, IPV6_LEVEL, IPV6_V6ONLY, &opt, sizeof(opt)) == -1 ||
655 		  setsockopt(new->tcpfd, IPV6_LEVEL, IPV6_V6ONLY, &opt, sizeof(opt)) == -1)
656 		die(_("failed to set IPV6 options on listening socket: %s"), NULL, EC_BADNET);
657 	    }
658 #endif
659 
660 	  while(1)
661 	    {
662 	      if ((rc = bind(new->fd, &iface->addr.sa, sa_len(&iface->addr))) != -1)
663 		break;
664 
665 #ifdef HAVE_IPV6
666 	      /* An interface may have an IPv6 address which is still undergoing DAD.
667 		 If so, the bind will fail until the DAD completes, so we try over 20 seconds
668 		 before failing. */
669 	      if (iface->addr.sa.sa_family == AF_INET6 && (errno == ENODEV || errno == EADDRNOTAVAIL) &&
670 		  dad_count++ < DAD_WAIT)
671 		{
672 		  sleep(1);
673 		  continue;
674 		}
675 #endif
676 	      break;
677 	    }
678 
679 	  if (rc == -1 || bind(new->tcpfd, &iface->addr.sa, sa_len(&iface->addr)) == -1)
680 	    {
681 	      prettyprint_addr(&iface->addr, daemon->namebuff);
682 	      die(_("failed to bind listening socket for %s: %s"),
683 		  daemon->namebuff, EC_BADNET);
684 	    }
685 
686 	  if (listen(new->tcpfd, 5) == -1)
687 	    die(_("failed to listen on socket: %s"), NULL, EC_BADNET);
688 	}
689 
690 #ifdef HAVE_TFTP
691       if ((daemon->options & OPT_TFTP) && iface->addr.sa.sa_family == AF_INET && iface->dhcp_ok)
692 	{
693 	  short save = iface->addr.in.sin_port;
694 	  iface->addr.in.sin_port = htons(TFTP_PORT);
695 	  if ((new->tftpfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1 ||
696 	      setsockopt(new->tftpfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) == -1 ||
697 	      !fix_fd(new->tftpfd) ||
698 	      bind(new->tftpfd, &iface->addr.sa, sa_len(&iface->addr)) == -1)
699 	    die(_("failed to create TFTP socket: %s"), NULL, EC_BADNET);
700 	  iface->addr.in.sin_port = save;
701 	}
702 #endif
703 #endif /* !__ANDROID */
704     }
705 
706   return listeners;
707 }
708 
709 
710 /* return a UDP socket bound to a random port, have to cope with straying into
711    occupied port nos and reserved ones. */
random_sock(int family)712 int random_sock(int family)
713 {
714   int fd;
715 
716   if ((fd = socket(family, SOCK_DGRAM, 0)) != -1)
717     {
718       union mysockaddr addr;
719       unsigned int ports_avail = 65536u - (unsigned short)daemon->min_port;
720       int tries = ports_avail < 30 ? 3 * ports_avail : 100;
721 
722       memset(&addr, 0, sizeof(addr));
723       addr.sa.sa_family = family;
724 
725       /* don't loop forever if all ports in use. */
726 
727       if (fix_fd(fd))
728 	while(tries--)
729 	  {
730 	    unsigned short port = rand16();
731 
732 	    if (daemon->min_port != 0)
733 	      port = htons(daemon->min_port + (port % ((unsigned short)ports_avail)));
734 
735 	    if (family == AF_INET)
736 	      {
737 		addr.in.sin_addr.s_addr = INADDR_ANY;
738 		addr.in.sin_port = port;
739 #ifdef HAVE_SOCKADDR_SA_LEN
740 		addr.in.sin_len = sizeof(struct sockaddr_in);
741 #endif
742 	      }
743 #ifdef HAVE_IPV6
744 	    else
745 	      {
746 		addr.in6.sin6_addr = in6addr_any;
747 		addr.in6.sin6_port = port;
748 #ifdef HAVE_SOCKADDR_SA_LEN
749 		addr.in6.sin6_len = sizeof(struct sockaddr_in6);
750 #endif
751 	      }
752 #endif
753 
754 	    if (bind(fd, (struct sockaddr *)&addr, sa_len(&addr)) == 0)
755 	      return fd;
756 
757 	    if (errno != EADDRINUSE && errno != EACCES)
758 	      break;
759 	  }
760 
761       close(fd);
762     }
763 
764   return -1;
765 }
766 
767 
local_bind(int fd,union mysockaddr * addr,char * intname,uint32_t mark,int is_tcp)768 int local_bind(int fd, union mysockaddr *addr, char *intname, uint32_t mark, int is_tcp)
769 {
770   union mysockaddr addr_copy = *addr;
771 
772   /* cannot set source _port_ for TCP connections. */
773   if (is_tcp)
774     {
775       if (addr_copy.sa.sa_family == AF_INET)
776 	addr_copy.in.sin_port = 0;
777 #ifdef HAVE_IPV6
778       else
779 	addr_copy.in6.sin6_port = 0;
780 #endif
781     }
782 
783   if (bind(fd, (struct sockaddr *)&addr_copy, sa_len(&addr_copy)) == -1)
784     return 0;
785 
786 #if defined(SO_BINDTODEVICE)
787   if (intname[0] != 0 &&
788       setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, intname, strlen(intname)) == -1)
789     return 0;
790 #endif
791 
792   if (mark != 0 && setsockopt(fd, SOL_SOCKET, SO_MARK, &mark, sizeof(mark)) == -1)
793     return 0;
794 
795   return 1;
796 }
797 
allocate_sfd(union mysockaddr * addr,char * intname,uint32_t mark)798 static struct serverfd *allocate_sfd(union mysockaddr *addr, char *intname, uint32_t mark)
799 {
800   struct serverfd *sfd;
801   int errsave;
802 
803   /* when using random ports, servers which would otherwise use
804      the INADDR_ANY/port0 socket have sfd set to NULL */
805   if (!daemon->osport && intname[0] == 0)
806     {
807       errno = 0;
808 
809       if (addr->sa.sa_family == AF_INET &&
810 	  addr->in.sin_addr.s_addr == INADDR_ANY &&
811 	  addr->in.sin_port == htons(0))
812 	return NULL;
813 
814 #ifdef HAVE_IPV6
815       if (addr->sa.sa_family == AF_INET6 &&
816 	  memcmp(&addr->in6.sin6_addr, &in6addr_any, sizeof(in6addr_any)) == 0 &&
817 	  addr->in6.sin6_port == htons(0))
818 	return NULL;
819 #endif
820     }
821 
822   /* may have a suitable one already */
823   for (sfd = daemon->sfds; sfd; sfd = sfd->next )
824     if (sockaddr_isequal(&sfd->source_addr, addr) &&
825 	mark == sfd->mark &&
826 	strcmp(intname, sfd->interface) == 0)
827       return sfd;
828 
829   /* need to make a new one. */
830   errno = ENOMEM; /* in case malloc fails. */
831   if (!(sfd = whine_malloc(sizeof(struct serverfd))))
832     return NULL;
833 
834   if ((sfd->fd = socket(addr->sa.sa_family, SOCK_DGRAM, 0)) == -1)
835     {
836       free(sfd);
837       return NULL;
838     }
839 
840   if (!local_bind(sfd->fd, addr, intname, mark, 0) || !fix_fd(sfd->fd))
841     {
842       errsave = errno; /* save error from bind. */
843       close(sfd->fd);
844       free(sfd);
845       errno = errsave;
846       return NULL;
847     }
848 
849   strcpy(sfd->interface, intname);
850   sfd->source_addr = *addr;
851   sfd->mark = mark;
852   sfd->next = daemon->sfds;
853   daemon->sfds = sfd;
854   return sfd;
855 }
856 
857 /* create upstream sockets during startup, before root is dropped which may be needed
858    this allows query_port to be a low port and interface binding */
pre_allocate_sfds(void)859 void pre_allocate_sfds(void)
860 {
861   struct server *srv;
862 
863   if (daemon->query_port != 0)
864     {
865       union  mysockaddr addr;
866       memset(&addr, 0, sizeof(addr));
867       addr.in.sin_family = AF_INET;
868       addr.in.sin_addr.s_addr = INADDR_ANY;
869       addr.in.sin_port = htons(daemon->query_port);
870 #ifdef HAVE_SOCKADDR_SA_LEN
871       addr.in.sin_len = sizeof(struct sockaddr_in);
872 #endif
873       allocate_sfd(&addr, "", 0);
874 #ifdef HAVE_IPV6
875       memset(&addr, 0, sizeof(addr));
876       addr.in6.sin6_family = AF_INET6;
877       addr.in6.sin6_addr = in6addr_any;
878       addr.in6.sin6_port = htons(daemon->query_port);
879 #ifdef HAVE_SOCKADDR_SA_LEN
880       addr.in6.sin6_len = sizeof(struct sockaddr_in6);
881 #endif
882       allocate_sfd(&addr, "", 0);
883 #endif
884     }
885 
886   for (srv = daemon->servers; srv; srv = srv->next)
887     if (!(srv->flags & (SERV_LITERAL_ADDRESS | SERV_NO_ADDR)) &&
888 	!allocate_sfd(&srv->source_addr, srv->interface, srv->mark) &&
889 	errno != 0 &&
890 	(daemon->options & OPT_NOWILD))
891       {
892 	prettyprint_addr(&srv->addr, daemon->namebuff);
893 	if (srv->interface[0] != 0)
894 	  {
895 	    strcat(daemon->namebuff, " ");
896 	    strcat(daemon->namebuff, srv->interface);
897 	  }
898 	die(_("failed to bind server socket for %s: %s"),
899 	    daemon->namebuff, EC_BADNET);
900       }
901 }
902 
903 
check_servers(void)904 void check_servers(void)
905 {
906   struct irec *iface;
907   struct server *new, *tmp, *ret = NULL;
908   int port = 0;
909 
910   for (new = daemon->servers; new; new = tmp)
911     {
912       tmp = new->next;
913 
914       if (!(new->flags & (SERV_LITERAL_ADDRESS | SERV_NO_ADDR)))
915 	{
916 	  port = prettyprint_addr(&new->addr, daemon->namebuff);
917 
918 	  /* 0.0.0.0 is nothing, the stack treats it like 127.0.0.1 */
919 	  if (new->addr.sa.sa_family == AF_INET &&
920 	      new->addr.in.sin_addr.s_addr == 0)
921 	    {
922 	      free(new);
923 	      continue;
924 	    }
925 
926 	  for (iface = daemon->interfaces; iface; iface = iface->next)
927 	    if (sockaddr_isequal(&new->addr, &iface->addr))
928 	      break;
929 	  if (iface)
930 	    {
931 	      my_syslog(LOG_WARNING, _("ignoring nameserver %s - local interface"), daemon->namebuff);
932 	      free(new);
933 	      continue;
934 	    }
935 
936 	  /* Do we need a socket set? */
937 	  if (!new->sfd &&
938 	      !(new->sfd = allocate_sfd(&new->source_addr, new->interface, new->mark)) &&
939 	      errno != 0)
940 	    {
941 	      my_syslog(LOG_WARNING,
942 			_("ignoring nameserver %s - cannot make/bind socket: %s"),
943 			daemon->namebuff, strerror(errno));
944 	      free(new);
945 	      continue;
946 	    }
947 	}
948 
949       /* reverse order - gets it right. */
950       new->next = ret;
951       ret = new;
952 
953       if (new->flags & (SERV_HAS_DOMAIN | SERV_FOR_NODOTS))
954 	{
955 	  char *s1, *s2;
956 	  if (!(new->flags & SERV_HAS_DOMAIN))
957 	    s1 = _("unqualified"), s2 = _("names");
958 	  else if (strlen(new->domain) == 0)
959 	    s1 = _("default"), s2 = "";
960 	  else
961 	    s1 = _("domain"), s2 = new->domain;
962 
963 	  if (new->flags & SERV_NO_ADDR)
964 	    my_syslog(LOG_INFO, _("using local addresses only for %s %s"), s1, s2);
965 	  else if (!(new->flags & SERV_LITERAL_ADDRESS))
966 	    my_syslog(LOG_INFO, _("using nameserver %s#%d for %s %s"), daemon->namebuff, port, s1, s2);
967 	}
968       else if (new->interface[0] != 0)
969 	my_syslog(LOG_INFO, _("using nameserver %s#%d(via %s)"), daemon->namebuff, port, new->interface);
970       else
971 	my_syslog(LOG_INFO, _("using nameserver %s#%d"), daemon->namebuff, port);
972     }
973 
974   daemon->servers = ret;
975 }
976 
977 #if defined(__ANDROID__) && !defined(__BRILLO__)
978 /* #define __ANDROID_DEBUG__ 1 */
979 /*
980  * Ingests a new list of interfaces and starts to listen on them, adding only the new
981  * and stopping to listen to any interfaces not on the new list.
982  *
983  * interfaces - input in the format "bt-pan|eth0|wlan0|..>" up to 1024 bytes long
984  */
set_interfaces(const char * interfaces)985 void set_interfaces(const char *interfaces)
986 {
987     struct iname *if_tmp;
988     struct iname *prev_if_names;
989     struct irec *old_iface, *new_iface, *prev_interfaces;
990     char s[1024];
991     char *next = s;
992     char *interface;
993     int was_wild = 0;
994 
995 #ifdef __ANDROID_DEBUG__
996     my_syslog(LOG_DEBUG, _("set_interfaces(%s)"), interfaces);
997 #endif
998     prev_if_names = daemon->if_names;
999     daemon->if_names = NULL;
1000 
1001     prev_interfaces = daemon->interfaces;
1002     daemon->interfaces = NULL;
1003 
1004     if (strlen(interfaces) > sizeof(s)) {
1005         die(_("interface string too long: %s"), NULL, EC_BADNET);
1006     }
1007     strncpy(s, interfaces, sizeof(s));
1008     while((interface = strsep(&next, SEPARATOR))) {
1009         if (!if_nametoindex(interface)) {
1010             my_syslog(LOG_ERR,
1011                     _("interface given in %s: '%s' has no ifindex; ignoring"),
1012                     __FUNCTION__, interface);
1013             continue;
1014         }
1015         if_tmp = safe_malloc(sizeof(struct iname));
1016         memset(if_tmp, 0, sizeof(struct iname));
1017         if ((if_tmp->name = strdup(interface)) == NULL) {
1018             die(_("malloc failure in set_interfaces: %s"), NULL, EC_BADNET);
1019         }
1020         if_tmp->next = daemon->if_names;
1021         daemon->if_names = if_tmp;
1022     }
1023 
1024     /*
1025      * Enumerate IP addresses (via RTM_GETADDR), adding IP entries to
1026      * daemon->interfaces for interface names listed in daemon->if_names.
1027      * The sockets are created by the create_bound_listener call below.
1028      */
1029     if (!enumerate_interfaces()) {
1030         die(_("enumerate interfaces error in set_interfaces: %s"), NULL, EC_BADNET);
1031     }
1032 
1033     for (if_tmp = daemon->if_names; if_tmp; if_tmp = if_tmp->next) {
1034         if (if_tmp->name && !if_tmp->used) {
1035             my_syslog(LOG_ERR, _("unknown interface given %s in set_interfaces()"), if_tmp->name);
1036         }
1037     }
1038 
1039     /* success! - setup to free the old */
1040     /* check for any that have been removed */
1041     for (old_iface = prev_interfaces; old_iface; old_iface=old_iface->next) {
1042       int found = 0;
1043       for (new_iface = daemon->interfaces; new_iface; new_iface = new_iface->next) {
1044         if (sockaddr_isequal(&old_iface->addr, &new_iface->addr)) {
1045             found = 1;
1046             break;
1047         }
1048       }
1049 
1050       if (found) {
1051         fixup_possible_existing_listener(new_iface);
1052       } else {
1053 #ifdef __ANDROID_DEBUG__
1054         char debug_buff[MAXDNAME];
1055         prettyprint_addr(&old_iface->addr, debug_buff);
1056         my_syslog(LOG_DEBUG, _("closing listener for %s"), debug_buff);
1057 #endif
1058 
1059         close_bound_listener(old_iface);
1060       }
1061     }
1062 
1063     /* remove wildchar listeners */
1064     was_wild = close_bound_listener(NULL);
1065     if (was_wild) daemon->options |= OPT_NOWILD;
1066 
1067     /* check for any that have been added */
1068     for (new_iface = daemon->interfaces; new_iface; new_iface = new_iface->next) {
1069       int found = 0;
1070 
1071       /* if the previous setup used a wildchar, then add any current interfaces */
1072       if (!was_wild) {
1073         for (old_iface = prev_interfaces; old_iface; old_iface = old_iface->next) {
1074           if(sockaddr_isequal(&old_iface->addr, &new_iface->addr)) {
1075             found = -1;
1076             break;
1077           }
1078         }
1079       }
1080       if (!found) {
1081 #ifdef __ANDROID_DEBUG__
1082         char debug_buff[MAXDNAME];
1083         prettyprint_addr(&new_iface->addr, debug_buff);
1084         my_syslog(LOG_DEBUG, _("adding listener for %s"), debug_buff);
1085 #endif
1086         create_bound_listener(&(daemon->listeners), new_iface);
1087       }
1088     }
1089 
1090     while (prev_if_names) {
1091       if (prev_if_names->name) free(prev_if_names->name);
1092       if_tmp = prev_if_names->next;
1093       free(prev_if_names);
1094       prev_if_names = if_tmp;
1095     }
1096     while (prev_interfaces) {
1097       struct irec *tmp_irec = prev_interfaces->next;
1098       free(prev_interfaces);
1099       prev_interfaces = tmp_irec;
1100     }
1101 #ifdef __ANDROID_DEBUG__
1102     my_syslog(LOG_DEBUG, _("done with setInterfaces"));
1103 #endif
1104 }
1105 
1106 /*
1107  * Takes a string in the format "0x100b|1.2.3.4|1.2.3.4|..." - up to 1024 bytes in length
1108  *  - The first element is the socket mark to set on sockets that forward DNS queries.
1109  *  - The subsequent elements are the DNS servers to forward queries to.
1110  */
set_servers(const char * servers)1111 int set_servers(const char *servers)
1112 {
1113   char s[1024];
1114   struct server *old_servers = NULL;
1115   struct server *new_servers = NULL;
1116   struct server *serv;
1117   char *mark_string;
1118   uint32_t mark;
1119 
1120   strncpy(s, servers, sizeof(s));
1121 
1122   /* move old servers to free list - we can reuse the memory
1123      and not risk malloc if there are the same or fewer new servers.
1124      Servers which were specced on the command line go to the new list. */
1125   for (serv = daemon->servers; serv;)
1126     {
1127       struct server *tmp = serv->next;
1128       if (serv->flags & SERV_FROM_RESOLV)
1129 	{
1130 	  serv->next = old_servers;
1131 	  old_servers = serv;
1132 	  /* forward table rules reference servers, so have to blow them away */
1133 	  server_gone(serv);
1134 	}
1135       else
1136 	{
1137 	  serv->next = new_servers;
1138 	  new_servers = serv;
1139 	}
1140       serv = tmp;
1141     }
1142 
1143   char *next = s;
1144   char *saddr;
1145 
1146   /* Parse the mark. */
1147   mark_string = strsep(&next, SEPARATOR);
1148   mark = strtoul(mark_string, NULL, 0);
1149 
1150   while ((saddr = strsep(&next, SEPARATOR))) {
1151       union mysockaddr addr, source_addr;
1152       memset(&addr, 0, sizeof(addr));
1153       memset(&source_addr, 0, sizeof(source_addr));
1154 
1155       if (parse_addr(AF_INET, saddr, &addr) == 0)
1156 	{
1157 	  addr.in.sin_port = htons(NAMESERVER_PORT);
1158 	  source_addr.in.sin_family = AF_INET;
1159 	  source_addr.in.sin_addr.s_addr = INADDR_ANY;
1160 	  source_addr.in.sin_port = htons(daemon->query_port);
1161 	}
1162 #ifdef HAVE_IPV6
1163       else if (parse_addr(AF_INET6, saddr, &addr) == 0)
1164 	{
1165 	  addr.in6.sin6_port = htons(NAMESERVER_PORT);
1166 	  source_addr.in6.sin6_family = AF_INET6;
1167 	  source_addr.in6.sin6_addr = in6addr_any;
1168 	  source_addr.in6.sin6_port = htons(daemon->query_port);
1169 	}
1170 #endif /* IPV6 */
1171       else
1172 	continue;
1173 
1174       if (old_servers)
1175 	{
1176 	  serv = old_servers;
1177 	  old_servers = old_servers->next;
1178 	}
1179       else if (!(serv = whine_malloc(sizeof (struct server))))
1180 	continue;
1181 
1182       /* this list is reverse ordered:
1183 	 it gets reversed again in check_servers */
1184       serv->next = new_servers;
1185       new_servers = serv;
1186       serv->addr = addr;
1187       serv->source_addr = source_addr;
1188       serv->domain = NULL;
1189       serv->interface[0] = 0;
1190       serv->mark = mark;
1191       serv->sfd = NULL;
1192       serv->flags = SERV_FROM_RESOLV;
1193       serv->queries = serv->failed_queries = 0;
1194     }
1195 
1196   /* Free any memory not used. */
1197   while (old_servers)
1198     {
1199       struct server *tmp = old_servers->next;
1200       free(old_servers);
1201       old_servers = tmp;
1202     }
1203 
1204   daemon->servers = new_servers;
1205   return 0;
1206 }
1207 #endif
1208 
1209 /* Return zero if no servers found, in that case we keep polling.
1210    This is a protection against an update-time/write race on resolv.conf */
reload_servers(char * fname)1211 int reload_servers(char *fname)
1212 {
1213   FILE *f;
1214   char *line;
1215   struct server *old_servers = NULL;
1216   struct server *new_servers = NULL;
1217   struct server *serv;
1218   int gotone = 0;
1219 
1220   /* buff happens to be MAXDNAME long... */
1221   if (!(f = fopen(fname, "r")))
1222     {
1223       my_syslog(LOG_ERR, _("failed to read %s: %s"), fname, strerror(errno));
1224       return 0;
1225     }
1226 
1227   /* move old servers to free list - we can reuse the memory
1228      and not risk malloc if there are the same or fewer new servers.
1229      Servers which were specced on the command line go to the new list. */
1230   for (serv = daemon->servers; serv;)
1231     {
1232       struct server *tmp = serv->next;
1233       if (serv->flags & SERV_FROM_RESOLV)
1234 	{
1235 	  serv->next = old_servers;
1236 	  old_servers = serv;
1237 	  /* forward table rules reference servers, so have to blow them away */
1238 	  server_gone(serv);
1239 	}
1240       else
1241 	{
1242 	  serv->next = new_servers;
1243 	  new_servers = serv;
1244 	}
1245       serv = tmp;
1246     }
1247 
1248   while ((line = fgets(daemon->namebuff, MAXDNAME, f)))
1249     {
1250       union mysockaddr addr, source_addr;
1251       char *token = strtok(line, " \t\n\r");
1252 
1253       if (!token)
1254 	continue;
1255       if (strcmp(token, "nameserver") != 0 && strcmp(token, "server") != 0)
1256 	continue;
1257       if (!(token = strtok(NULL, " \t\n\r")))
1258 	continue;
1259 
1260       memset(&addr, 0, sizeof(addr));
1261       memset(&source_addr, 0, sizeof(source_addr));
1262 
1263       if (parse_addr(AF_INET, token, &addr) == 0)
1264 	{
1265 	  addr.in.sin_port = htons(NAMESERVER_PORT);
1266 	  source_addr.in.sin_family = AF_INET;
1267 	  source_addr.in.sin_addr.s_addr = INADDR_ANY;
1268 	  source_addr.in.sin_port = htons(daemon->query_port);
1269 	}
1270 #ifdef HAVE_IPV6
1271       else if (parse_addr(AF_INET6, token, &addr) == 0)
1272 	{
1273 	  addr.in6.sin6_port = htons(NAMESERVER_PORT);
1274 	  source_addr.in6.sin6_family = AF_INET6;
1275 	  source_addr.in6.sin6_addr = in6addr_any;
1276 	  source_addr.in6.sin6_port = htons(daemon->query_port);
1277 	}
1278 #endif /* IPV6 */
1279       else
1280 	continue;
1281 
1282       if (old_servers)
1283 	{
1284 	  serv = old_servers;
1285 	  old_servers = old_servers->next;
1286 	}
1287       else if (!(serv = whine_malloc(sizeof (struct server))))
1288 	continue;
1289 
1290       /* this list is reverse ordered:
1291 	 it gets reversed again in check_servers */
1292       serv->next = new_servers;
1293       new_servers = serv;
1294       serv->addr = addr;
1295       serv->source_addr = source_addr;
1296       serv->domain = NULL;
1297       serv->interface[0] = 0;
1298       serv->mark = 0;
1299       serv->sfd = NULL;
1300       serv->flags = SERV_FROM_RESOLV;
1301       serv->queries = serv->failed_queries = 0;
1302       gotone = 1;
1303     }
1304 
1305   /* Free any memory not used. */
1306   while (old_servers)
1307     {
1308       struct server *tmp = old_servers->next;
1309       free(old_servers);
1310       old_servers = tmp;
1311     }
1312 
1313   daemon->servers = new_servers;
1314   fclose(f);
1315 
1316   return gotone;
1317 }
1318 
1319 
1320 /* Use an IPv4 listener socket for ioctling */
get_ifaddr(char * intr)1321 struct in_addr get_ifaddr(char *intr)
1322 {
1323   struct listener *l;
1324   struct ifreq ifr;
1325 
1326   for (l = daemon->listeners; l && l->family != AF_INET; l = l->next);
1327 
1328   strncpy(ifr.ifr_name, intr, IF_NAMESIZE);
1329   ifr.ifr_addr.sa_family = AF_INET;
1330 
1331   if (!l || ioctl(l->fd, SIOCGIFADDR, &ifr) == -1)
1332     ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = -1;
1333 
1334   return ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr;
1335 }
1336