-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdiscover.c
1890 lines (1683 loc) · 48.4 KB
/
discover.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* discover.c
Find and identify the network interfaces. */
/*
* Copyright (c) 2004-2016 by Internet Systems Consortium, Inc. ("ISC")
* Copyright (c) 1995-2003 by Internet Software Consortium
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
* OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* Internet Systems Consortium, Inc.
* 950 Charter Street
* Redwood City, CA 94063
* https://www.isc.org/
*
*/
#include "dhcpd.h"
/* length of line we can read from the IF file, 256 is too small in some cases */
#define IF_LINE_LENGTH 1024
#define BSD_COMP /* needed on Solaris for SIOCGLIFNUM */
#include <sys/ioctl.h>
#include <errno.h>
#ifdef HAVE_NET_IF6_H
# include <net/if6.h>
#endif
struct interface_info *interfaces, *dummy_interfaces, *fallback_interface;
int interfaces_invalidated;
int quiet_interface_discovery;
u_int16_t local_port;
u_int16_t remote_port;
int dhcpv4_over_dhcpv6 = 0;
int (*dhcp_interface_setup_hook) (struct interface_info *, struct iaddr *);
int (*dhcp_interface_discovery_hook) (struct interface_info *);
isc_result_t (*dhcp_interface_startup_hook) (struct interface_info *);
int (*dhcp_interface_shutdown_hook) (struct interface_info *);
struct in_addr limited_broadcast;
int local_family = AF_INET;
struct in_addr local_address;
void (*bootp_packet_handler) (struct interface_info *,
struct dhcp_packet *, unsigned,
unsigned int,
struct iaddr, struct hardware *);
#ifdef DHCPv6
void (*dhcpv6_packet_handler)(struct interface_info *,
const char *, int,
int, const struct iaddr *,
isc_boolean_t);
#endif /* DHCPv6 */
omapi_object_type_t *dhcp_type_interface;
#if defined (TRACING)
trace_type_t *interface_trace;
trace_type_t *inpacket_trace;
trace_type_t *outpacket_trace;
#endif
struct interface_info **interface_vector;
int interface_count;
int interface_max;
OMAPI_OBJECT_ALLOC (interface, struct interface_info, dhcp_type_interface)
isc_result_t interface_setup ()
{
isc_result_t status;
status = omapi_object_type_register (&dhcp_type_interface,
"interface",
dhcp_interface_set_value,
dhcp_interface_get_value,
dhcp_interface_destroy,
dhcp_interface_signal_handler,
dhcp_interface_stuff_values,
dhcp_interface_lookup,
dhcp_interface_create,
dhcp_interface_remove,
0, 0, 0,
sizeof (struct interface_info),
interface_initialize, RC_MISC);
if (status != ISC_R_SUCCESS)
log_fatal ("Can't register interface object type: %s",
isc_result_totext (status));
return status;
}
#if defined (TRACING)
void interface_trace_setup ()
{
interface_trace = trace_type_register ("interface", (void *)0,
trace_interface_input,
trace_interface_stop, MDL);
inpacket_trace = trace_type_register ("inpacket", (void *)0,
trace_inpacket_input,
trace_inpacket_stop, MDL);
outpacket_trace = trace_type_register ("outpacket", (void *)0,
trace_outpacket_input,
trace_outpacket_stop, MDL);
}
#endif
isc_result_t interface_initialize (omapi_object_t *ipo,
const char *file, int line)
{
struct interface_info *ip = (struct interface_info *)ipo;
ip -> rfdesc = ip -> wfdesc = -1;
return ISC_R_SUCCESS;
}
/*
* Scanning for Interfaces
* -----------------------
*
* To find interfaces, we create an iterator that abstracts out most
* of the platform specifics. Use is fairly straightforward:
*
* - begin_iface_scan() starts the process.
* - Use next_iface() until it returns 0.
* - end_iface_scan() performs any necessary cleanup.
*
* We check for errors on each call to next_iface(), which returns a
* description of the error as a string if any occurs.
*
* We currently have code for Solaris and Linux. Other systems need
* to have code written.
*
* NOTE: the long-term goal is to use the interface code from BIND 9.
*/
#if defined(SIOCGLIFCONF) && defined(SIOCGLIFNUM) && defined(SIOCGLIFFLAGS)
/* HP/UX doesn't define struct lifconf, instead they define struct
* if_laddrconf. Similarly, 'struct lifreq' and 'struct lifaddrreq'.
*/
#ifdef ISC_PLATFORM_HAVEIF_LADDRCONF
# define lifc_len iflc_len
# define lifc_buf iflc_buf
# define lifc_req iflc_req
# define LIFCONF if_laddrconf
#else
# define ISC_HAVE_LIFC_FAMILY 1
# define ISC_HAVE_LIFC_FLAGS 1
# define LIFCONF lifconf
#endif
#ifdef ISC_PLATFORM_HAVEIF_LADDRREQ
# define lifr_addr iflr_addr
# define lifr_name iflr_name
# define lifr_dstaddr iflr_dstaddr
# define lifr_flags iflr_flags
# define sockaddr_storage sockaddr_ext
# define ss_family sa_family
# define LIFREQ if_laddrreq
#else
# define LIFREQ lifreq
#endif
#ifndef IF_NAMESIZE
# if defined(LIFNAMSIZ)
# define IF_NAMESIZE LIFNAMSIZ
# elif defined(IFNAMSIZ)
# define IF_NAMESIZE IFNAMSIZ
# else
# define IF_NAMESIZE 16
# endif
#endif
#elif !defined(__linux) && !defined(HAVE_IFADDRS_H)
# define SIOCGLIFCONF SIOCGIFCONF
# define SIOCGLIFFLAGS SIOCGIFFLAGS
# define LIFREQ ifreq
# define LIFCONF ifconf
# define lifr_name ifr_name
# define lifr_addr ifr_addr
# define lifr_flags ifr_flags
# define lifc_len ifc_len
# define lifc_buf ifc_buf
# define lifc_req ifc_req
#ifdef _AIX
# define ss_family __ss_family
#endif
#endif
#if defined(SIOCGLIFCONF) && defined(SIOCGLIFFLAGS)
/*
* Solaris support
* ---------------
*
* The SIOCGLIFCONF ioctl() are the extension that you need to use
* on Solaris to get information about IPv6 addresses.
*
* Solaris' extended interface is documented in the if_tcp man page.
*/
/*
* Structure holding state about the scan.
*/
struct iface_conf_list {
int sock; /* file descriptor used to get information */
int num; /* total number of interfaces */
struct LIFCONF conf; /* structure used to get information */
int next; /* next interface to retrieve when iterating */
};
/*
* Structure used to return information about a specific interface.
*/
struct iface_info {
char name[IF_NAMESIZE+1]; /* name of the interface, e.g. "bge0" */
struct sockaddr_storage addr; /* address information */
isc_uint64_t flags; /* interface flags, e.g. IFF_LOOPBACK */
};
/*
* Start a scan of interfaces.
*
* The iface_conf_list structure maintains state for this process.
*/
int
begin_iface_scan(struct iface_conf_list *ifaces) {
#ifdef ISC_PLATFORM_HAVELIFNUM
struct lifnum lifnum;
#else
int lifnum;
#endif
ifaces->sock = socket(local_family, SOCK_DGRAM, IPPROTO_UDP);
if (ifaces->sock < 0) {
log_error("Error creating socket to list interfaces; %m");
return 0;
}
memset(&lifnum, 0, sizeof(lifnum));
#ifdef ISC_PLATFORM_HAVELIFNUM
lifnum.lifn_family = AF_UNSPEC;
#endif
#ifdef SIOCGLIFNUM
if (ioctl(ifaces->sock, SIOCGLIFNUM, &lifnum) < 0) {
log_error("Error finding total number of interfaces; %m");
close(ifaces->sock);
ifaces->sock = -1;
return 0;
}
#ifdef ISC_PLATFORM_HAVELIFNUM
ifaces->num = lifnum.lifn_count;
#else
ifaces->num = lifnum;
#endif
#else
ifaces->num = 64;
#endif /* SIOCGLIFNUM */
memset(&ifaces->conf, 0, sizeof(ifaces->conf));
#ifdef ISC_HAVE_LIFC_FAMILY
ifaces->conf.lifc_family = AF_UNSPEC;
#endif
ifaces->conf.lifc_len = ifaces->num * sizeof(struct LIFREQ);
ifaces->conf.lifc_buf = dmalloc(ifaces->conf.lifc_len, MDL);
if (ifaces->conf.lifc_buf == NULL) {
log_fatal("Out of memory getting interface list.");
}
if (ioctl(ifaces->sock, SIOCGLIFCONF, &ifaces->conf) < 0) {
log_error("Error getting interfaces configuration list; %m");
dfree(ifaces->conf.lifc_buf, MDL);
close(ifaces->sock);
ifaces->sock = -1;
return 0;
}
ifaces->next = 0;
return 1;
}
/*
* Retrieve the next interface.
*
* Returns information in the info structure.
* Sets err to 1 if there is an error, otherwise 0.
*/
int
next_iface(struct iface_info *info, int *err, struct iface_conf_list *ifaces) {
struct LIFREQ *p;
struct LIFREQ tmp;
isc_boolean_t foundif;
#if defined(sun) || defined(__linux)
/* Pointer used to remove interface aliases. */
char *s;
#endif
do {
foundif = ISC_FALSE;
if (ifaces->next >= ifaces->num) {
*err = 0;
return 0;
}
p = ifaces->conf.lifc_req;
p += ifaces->next;
if (strlen(p->lifr_name) >= sizeof(info->name)) {
*err = 1;
log_error("Interface name '%s' too long", p->lifr_name);
return 0;
}
/* Reject if interface address family does not match */
if (p->lifr_addr.ss_family != local_family) {
ifaces->next++;
continue;
}
memset(info, 0, sizeof(struct iface_info));
strncpy(info->name, p->lifr_name, sizeof(info->name) - 1);
memcpy(&info->addr, &p->lifr_addr, sizeof(p->lifr_addr));
#if defined(sun) || defined(__linux)
/* interface aliases look like "eth0:1" or "wlan1:3" */
s = strchr(info->name, ':');
if (s != NULL) {
*s = '\0';
}
#endif /* defined(sun) || defined(__linux) */
foundif = ISC_TRUE;
} while ((foundif == ISC_FALSE) ||
(strncmp(info->name, "dummy", 5) == 0));
memset(&tmp, 0, sizeof(tmp));
strncpy(tmp.lifr_name, info->name, sizeof(tmp.lifr_name) - 1);
if (ioctl(ifaces->sock, SIOCGLIFFLAGS, &tmp) < 0) {
log_error("Error getting interface flags for '%s'; %m",
p->lifr_name);
*err = 1;
return 0;
}
info->flags = tmp.lifr_flags;
ifaces->next++;
*err = 0;
return 1;
}
/*
* End scan of interfaces.
*/
void
end_iface_scan(struct iface_conf_list *ifaces) {
dfree(ifaces->conf.lifc_buf, MDL);
close(ifaces->sock);
ifaces->sock = -1;
}
#elif __linux /* !HAVE_SIOCGLIFCONF */
/*
* Linux support
* -------------
*
* In Linux, we use the /proc pseudo-filesystem to get information
* about interfaces, along with selected ioctl() calls.
*
* Linux low level access is documented in the netdevice man page.
*/
/*
* Structure holding state about the scan.
*/
struct iface_conf_list {
int sock; /* file descriptor used to get information */
FILE *fp; /* input from /proc/net/dev */
#ifdef DHCPv6
FILE *fp6; /* input from /proc/net/if_inet6 */
#endif
};
/*
* Structure used to return information about a specific interface.
*/
struct iface_info {
char name[IFNAMSIZ]; /* name of the interface, e.g. "eth0" */
struct sockaddr_storage addr; /* address information */
isc_uint64_t flags; /* interface flags, e.g. IFF_LOOPBACK */
};
/*
* Start a scan of interfaces.
*
* The iface_conf_list structure maintains state for this process.
*/
int
begin_iface_scan(struct iface_conf_list *ifaces) {
char buf[IF_LINE_LENGTH];
int len;
int i;
ifaces->fp = fopen("/proc/net/dev", "r");
if (ifaces->fp == NULL) {
log_error("Error opening '/proc/net/dev' to list interfaces");
return 0;
}
/*
* The first 2 lines are header information, so read and ignore them.
*/
for (i=0; i<2; i++) {
if (fgets(buf, sizeof(buf), ifaces->fp) == NULL) {
log_error("Error reading headers from '/proc/net/dev'");
fclose(ifaces->fp);
ifaces->fp = NULL;
return 0;
}
len = strlen(buf);
if ((len <= 0) || (buf[len-1] != '\n')) {
log_error("Bad header line in '/proc/net/dev'");
fclose(ifaces->fp);
ifaces->fp = NULL;
return 0;
}
}
ifaces->sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (ifaces->sock < 0) {
log_error("Error creating socket to list interfaces; %m");
fclose(ifaces->fp);
ifaces->fp = NULL;
return 0;
}
#ifdef DHCPv6
if (local_family == AF_INET6) {
ifaces->fp6 = fopen("/proc/net/if_inet6", "r");
if (ifaces->fp6 == NULL) {
log_error("Error opening '/proc/net/if_inet6' to "
"list IPv6 interfaces; %m");
close(ifaces->sock);
ifaces->sock = -1;
fclose(ifaces->fp);
ifaces->fp = NULL;
return 0;
}
}
#endif
return 1;
}
/*
* Read our IPv4 interfaces from /proc/net/dev.
*
* The file looks something like this:
*
* Inter-| Receive ...
* face |bytes packets errs drop fifo frame ...
* lo: 1580562 4207 0 0 0 0 ...
* eth0: 0 0 0 0 0 0 ...
* eth1:1801552440 37895 0 14 0 ...
*
* We only care about the interface name, which is at the start of
* each line.
*
* We use an ioctl() to get the address and flags for each interface.
*/
static int
next_iface4(struct iface_info *info, int *err, struct iface_conf_list *ifaces) {
char buf[IF_LINE_LENGTH];
int len;
char *p;
char *name;
struct ifreq tmp;
/*
* Loop exits when we find an interface that has an address, or
* when we run out of interfaces.
*/
for (;;) {
do {
/*
* Read the next line in the file.
*/
if (fgets(buf, sizeof(buf), ifaces->fp) == NULL) {
if (ferror(ifaces->fp)) {
*err = 1;
log_error("Error reading interface "
"information");
} else {
*err = 0;
}
return 0;
}
/*
* Make sure the line is a nice,
* newline-terminated line.
*/
len = strlen(buf);
if ((len <= 0) || (buf[len-1] != '\n')) {
log_error("Bad line reading interface "
"information");
*err = 1;
return 0;
}
/*
* Figure out our name.
*/
p = strrchr(buf, ':');
if (p == NULL) {
log_error("Bad line reading interface "
"information (no colon)");
*err = 1;
return 0;
}
*p = '\0';
name = buf;
while (isspace(*name)) {
name++;
}
/*
* Copy our name into our interface structure.
*/
len = p - name;
if (len >= sizeof(info->name)) {
*err = 1;
log_error("Interface name '%s' too long", name);
return 0;
}
strncpy(info->name, name, sizeof(info->name) - 1);
#ifdef ALIAS_NAMED_PERMUTED
/* interface aliases look like "eth0:1" or "wlan1:3" */
s = strchr(info->name, ':');
if (s != NULL) {
*s = '\0';
}
#endif
#ifdef SKIP_DUMMY_INTERFACES
} while (strncmp(info->name, "dummy", 5) == 0);
#else
} while (0);
#endif
memset(&tmp, 0, sizeof(tmp));
strncpy(tmp.ifr_name, name, sizeof(tmp.ifr_name) - 1);
if (ioctl(ifaces->sock, SIOCGIFADDR, &tmp) < 0) {
if (errno == EADDRNOTAVAIL) {
continue;
}
log_error("Error getting interface address "
"for '%s'; %m", name);
*err = 1;
return 0;
}
memcpy(&info->addr, &tmp.ifr_addr, sizeof(tmp.ifr_addr));
memset(&tmp, 0, sizeof(tmp));
strncpy(tmp.ifr_name, name, sizeof(tmp.ifr_name) - 1);
if (ioctl(ifaces->sock, SIOCGIFFLAGS, &tmp) < 0) {
log_error("Error getting interface flags for '%s'; %m",
name);
*err = 1;
return 0;
}
info->flags = tmp.ifr_flags;
*err = 0;
return 1;
}
}
#ifdef DHCPv6
/*
* Read our IPv6 interfaces from /proc/net/if_inet6.
*
* The file looks something like this:
*
* fe80000000000000025056fffec00008 05 40 20 80 vmnet8
* 00000000000000000000000000000001 01 80 10 80 lo
* fe80000000000000025056fffec00001 06 40 20 80 vmnet1
* 200108881936000202166ffffe497d9b 03 40 00 00 eth1
* fe8000000000000002166ffffe497d9b 03 40 20 80 eth1
*
* We get IPv6 address from the start, the interface name from the end,
* and ioctl() to get flags.
*/
static int
next_iface6(struct iface_info *info, int *err, struct iface_conf_list *ifaces) {
char buf[IF_LINE_LENGTH];
int len;
char *p;
char *name;
int i;
struct sockaddr_in6 addr;
struct ifreq tmp;
do {
/*
* Read the next line in the file.
*/
if (fgets(buf, sizeof(buf), ifaces->fp6) == NULL) {
if (ferror(ifaces->fp6)) {
*err = 1;
log_error("Error reading IPv6 "
"interface information");
} else {
*err = 0;
}
return 0;
}
/*
* Make sure the line is a nice, newline-terminated line.
*/
len = strlen(buf);
if ((len <= 0) || (buf[len-1] != '\n')) {
log_error("Bad line reading IPv6 "
"interface information");
*err = 1;
return 0;
}
/*
* Figure out our name.
*/
buf[--len] = '\0';
p = strrchr(buf, ' ');
if (p == NULL) {
log_error("Bad line reading IPv6 interface "
"information (no space)");
*err = 1;
return 0;
}
name = p+1;
/*
* Copy our name into our interface structure.
*/
len = strlen(name);
if (len >= sizeof(info->name)) {
*err = 1;
log_error("IPv6 interface name '%s' too long", name);
return 0;
}
strncpy(info->name, name, sizeof(info->name) - 1);
#ifdef SKIP_DUMMY_INTERFACES
} while (strncmp(info->name, "dummy", 5) == 0);
#else
} while (0);
#endif
/*
* Double-check we start with the IPv6 address.
*/
for (i=0; i<32; i++) {
if (!isxdigit(buf[i]) || isupper(buf[i])) {
*err = 1;
log_error("Bad line reading IPv6 interface address "
"for '%s'", name);
return 0;
}
}
/*
* Load our socket structure.
*/
memset(&addr, 0, sizeof(addr));
addr.sin6_family = AF_INET6;
for (i=0; i<16; i++) {
unsigned char byte;
static const char hex[] = "0123456789abcdef";
byte = ((index(hex, buf[i * 2]) - hex) << 4) |
(index(hex, buf[i * 2 + 1]) - hex);
addr.sin6_addr.s6_addr[i] = byte;
}
memcpy(&info->addr, &addr, sizeof(addr));
/*
* Get our flags.
*/
memset(&tmp, 0, sizeof(tmp));
strncpy(tmp.ifr_name, name, sizeof(tmp.ifr_name) - 1);
if (ioctl(ifaces->sock, SIOCGIFFLAGS, &tmp) < 0) {
log_error("Error getting interface flags for '%s'; %m", name);
*err = 1;
return 0;
}
info->flags = tmp.ifr_flags;
*err = 0;
return 1;
}
#endif /* DHCPv6 */
/*
* Retrieve the next interface.
*
* Returns information in the info structure.
* Sets err to 1 if there is an error, otherwise 0.
*/
int
next_iface(struct iface_info *info, int *err, struct iface_conf_list *ifaces) {
memset(info, 0, sizeof(struct iface_info));
if (next_iface4(info, err, ifaces)) {
return 1;
}
#ifdef DHCPv6
if (!(*err)) {
if (local_family == AF_INET6)
return next_iface6(info, err, ifaces);
}
#endif
return 0;
}
/*
* End scan of interfaces.
*/
void
end_iface_scan(struct iface_conf_list *ifaces) {
fclose(ifaces->fp);
ifaces->fp = NULL;
close(ifaces->sock);
ifaces->sock = -1;
#ifdef DHCPv6
if (local_family == AF_INET6) {
fclose(ifaces->fp6);
ifaces->fp6 = NULL;
}
#endif
}
#else
/*
* BSD support
* -----------
*
* FreeBSD, NetBSD, OpenBSD, and OS X all have the getifaddrs()
* function.
*
* The getifaddrs() man page describes the use.
*/
#include <ifaddrs.h>
/*
* Structure holding state about the scan.
*/
struct iface_conf_list {
struct ifaddrs *head; /* beginning of the list */
struct ifaddrs *next; /* current position in the list */
};
/*
* Structure used to return information about a specific interface.
*/
struct iface_info {
char name[IFNAMSIZ]; /* name of the interface, e.g. "bge0" */
struct sockaddr_storage addr; /* address information */
isc_uint64_t flags; /* interface flags, e.g. IFF_LOOPBACK */
};
/*
* Start a scan of interfaces.
*
* The iface_conf_list structure maintains state for this process.
*/
int
begin_iface_scan(struct iface_conf_list *ifaces) {
if (getifaddrs(&ifaces->head) != 0) {
log_error("Error getting interfaces; %m");
return 0;
}
ifaces->next = ifaces->head;
return 1;
}
/*
* Retrieve the next interface.
*
* Returns information in the info structure.
* Sets err to 1 if there is an error, otherwise 0.
*/
int
next_iface(struct iface_info *info, int *err, struct iface_conf_list *ifaces) {
if (ifaces->next == NULL) {
*err = 0;
return 0;
}
if (strlen(ifaces->next->ifa_name) >= sizeof(info->name)) {
log_error("Interface name '%s' too long",
ifaces->next->ifa_name);
*err = 1;
return 0;
}
memset(info, 0, sizeof(struct iface_info));
strncpy(info->name, ifaces->next->ifa_name, sizeof(info->name) - 1);
memcpy(&info->addr, ifaces->next->ifa_addr,
ifaces->next->ifa_addr->sa_len);
info->flags = ifaces->next->ifa_flags;
ifaces->next = ifaces->next->ifa_next;
*err = 0;
return 1;
}
/*
* End scan of interfaces.
*/
void
end_iface_scan(struct iface_conf_list *ifaces) {
freeifaddrs(ifaces->head);
ifaces->head = NULL;
ifaces->next = NULL;
}
#endif
/* XXX: perhaps create drealloc() rather than do it manually */
void
add_ipv4_addr_to_interface(struct interface_info *iface,
const struct in_addr *addr) {
/*
* We don't expect a lot of addresses per IPv4 interface, so
* we use 4, as our "chunk size" for collecting addresses.
*/
if (iface->addresses == NULL) {
iface->addresses = dmalloc(4 * sizeof(struct in_addr), MDL);
if (iface->addresses == NULL) {
log_fatal("Out of memory saving IPv4 address "
"on interface.");
}
iface->address_count = 0;
iface->address_max = 4;
} else if (iface->address_count >= iface->address_max) {
struct in_addr *tmp;
int new_max;
new_max = iface->address_max + 4;
tmp = dmalloc(new_max * sizeof(struct in_addr), MDL);
if (tmp == NULL) {
log_fatal("Out of memory saving IPv4 address "
"on interface.");
}
memcpy(tmp,
iface->addresses,
iface->address_max * sizeof(struct in_addr));
dfree(iface->addresses, MDL);
iface->addresses = tmp;
iface->address_max = new_max;
}
iface->addresses[iface->address_count++] = *addr;
}
#ifdef DHCPv6
/* XXX: perhaps create drealloc() rather than do it manually */
void
add_ipv6_addr_to_interface(struct interface_info *iface,
const struct in6_addr *addr) {
/*
* Each IPv6 interface will have at least two IPv6 addresses,
* and likely quite a few more. So we use 8, as our "chunk size" for
* collecting addresses.
*/
if (iface->v6addresses == NULL) {
iface->v6addresses = dmalloc(8 * sizeof(struct in6_addr), MDL);
if (iface->v6addresses == NULL) {
log_fatal("Out of memory saving IPv6 address "
"on interface.");
}
iface->v6address_count = 0;
iface->v6address_max = 8;
} else if (iface->v6address_count >= iface->v6address_max) {
struct in6_addr *tmp;
int new_max;
new_max = iface->v6address_max + 8;
tmp = dmalloc(new_max * sizeof(struct in6_addr), MDL);
if (tmp == NULL) {
log_fatal("Out of memory saving IPv6 address "
"on interface.");
}
memcpy(tmp,
iface->v6addresses,
iface->v6address_max * sizeof(struct in6_addr));
dfree(iface->v6addresses, MDL);
iface->v6addresses = tmp;
iface->v6address_max = new_max;
}
iface->v6addresses[iface->v6address_count++] = *addr;
}
#endif /* DHCPv6 */
/* Use the SIOCGIFCONF ioctl to get a list of all the attached interfaces.
For each interface that's of type INET and not the loopback interface,
register that interface with the network I/O software, figure out what
subnet it's on, and add it to the list of interfaces. */
void
discover_interfaces(int state) {
struct iface_conf_list ifaces;
struct iface_info info;
int err;
struct interface_info *tmp;
struct interface_info *last, *next;
#ifdef DHCPv6
char abuf[sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")];
#endif /* DHCPv6 */
struct subnet *subnet;
int ir;
isc_result_t status;
int wifcount = 0;
static int setup_fallback = 0;
if (!begin_iface_scan(&ifaces)) {
log_fatal("Can't get list of interfaces.");
}
/* If we already have a list of interfaces, and we're running as
a DHCP server, the interfaces were requested. */
if (interfaces && (state == DISCOVER_SERVER ||
state == DISCOVER_RELAY ||
state == DISCOVER_REQUESTED))
ir = 0;
else if (state == DISCOVER_UNCONFIGURED)
ir = INTERFACE_REQUESTED | INTERFACE_AUTOMATIC;
else {
ir = INTERFACE_REQUESTED;
if (state == DISCOVER_RELAY && local_family == AF_INET) {
/* We're a v4 relay without specifically requested
* interfaces, so mark them all as bidirectional. */
ir |= INTERFACE_STREAMS;
}
}
/* Cycle through the list of interfaces looking for IP addresses. */
while (next_iface(&info, &err, &ifaces)) {
/* See if we've seen an interface that matches this one. */
for (tmp = interfaces; tmp; tmp = tmp->next) {
if (!strcmp(tmp->name, info.name))
break;
}
/* Skip non broadcast interfaces (plus loopback and
point-to-point in case an OS incorrectly marks them
as broadcast). Also skip down interfaces unless we're
trying to get a list of configurable interfaces. */
if ((((local_family == AF_INET &&
!(info.flags & IFF_BROADCAST)) ||
#ifdef DHCPv6
(local_family == AF_INET6 &&
!(info.flags & IFF_MULTICAST)) ||
#endif
info.flags & IFF_LOOPBACK ||
info.flags & IFF_POINTOPOINT) && !tmp) ||
(!(info.flags & IFF_UP) &&
state != DISCOVER_UNCONFIGURED))
continue;
/* If there isn't already an interface by this name,
allocate one. */
if (tmp == NULL) {
status = interface_allocate(&tmp, MDL);
if (status != ISC_R_SUCCESS) {
log_fatal("Error allocating interface %s: %s",
info.name, isc_result_totext(status));
}
strncpy(tmp->name, info.name, sizeof(tmp->name) - 1);
interface_snorf(tmp, ir);
interface_dereference(&tmp, MDL);
tmp = interfaces; /* XXX */
}