Coverage Report

Created: 2026-11-22 06:09

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