-
-
Notifications
You must be signed in to change notification settings - Fork 105
/
server.c
5593 lines (5139 loc) · 157 KB
/
server.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
/*
* server.c -- nsd(8) network input/output
*
* Copyright (c) 2001-2006, NLnet Labs. All rights reserved.
*
* See LICENSE for the license.
*
*/
#include "config.h"
#include <sys/types.h>
#include <sys/param.h>
#include <limits.h>
#include <sys/socket.h>
#include <sys/uio.h>
#include <sys/wait.h>
#include <netinet/in.h>
#ifdef USE_TCP_FASTOPEN
#include <netinet/tcp.h>
#endif
#include <arpa/inet.h>
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <signal.h>
#include <netdb.h>
#include <poll.h>
#ifdef HAVE_SYS_RANDOM_H
#include <sys/random.h>
#endif
#ifndef SHUT_WR
#define SHUT_WR 1
#endif
#ifdef HAVE_MMAP
#include <sys/mman.h>
#endif /* HAVE_MMAP */
#ifdef HAVE_OPENSSL_RAND_H
#include <openssl/rand.h>
#endif
#ifdef HAVE_OPENSSL_SSL_H
#include <openssl/ssl.h>
#endif
#ifdef HAVE_OPENSSL_ERR_H
#include <openssl/err.h>
#endif
#ifdef HAVE_OPENSSL_OCSP_H
#include <openssl/ocsp.h>
#endif
#ifndef USE_MINI_EVENT
# ifdef HAVE_EVENT_H
# include <event.h>
# else
# include <event2/event.h>
# include "event2/event_struct.h"
# include "event2/event_compat.h"
# endif
#else
# include "mini_event.h"
#endif
#include "axfr.h"
#include "namedb.h"
#include "netio.h"
#include "xfrd.h"
#include "xfrd-tcp.h"
#include "xfrd-disk.h"
#include "difffile.h"
#include "nsec3.h"
#include "ipc.h"
#include "udb.h"
#include "remote.h"
#include "lookup3.h"
#include "rrl.h"
#include "ixfr.h"
#ifdef USE_DNSTAP
#include "dnstap/dnstap_collector.h"
#endif
#include "verify.h"
#include "util/proxy_protocol.h"
#define RELOAD_SYNC_TIMEOUT 25 /* seconds */
#ifdef USE_DNSTAP
/*
* log_addr() - the function to print sockaddr_in/sockaddr_in6 structures content
* just like its done in Unbound via the same log_addr(VERB_LEVEL, const char*, sockaddr_storage*)
*/
static void
log_addr(const char* descr,
#ifdef INET6
struct sockaddr_storage* addr
#else
struct sockaddr_in* addr
#endif
)
{
char str_buf[64];
if(verbosity < 6)
return;
if(
#ifdef INET6
addr->ss_family == AF_INET
#else
addr->sin_family == AF_INET
#endif
) {
struct sockaddr_in* s = (struct sockaddr_in*)addr;
inet_ntop(AF_INET, &s->sin_addr.s_addr, str_buf, sizeof(str_buf));
VERBOSITY(6, (LOG_INFO, "%s: address is: %s, port is: %d", descr, str_buf, ntohs(s->sin_port)));
#ifdef INET6
} else {
struct sockaddr_in6* s6 = (struct sockaddr_in6*)addr;
inet_ntop(AF_INET6, &s6->sin6_addr.s6_addr, str_buf, sizeof(str_buf));
VERBOSITY(6, (LOG_INFO, "%s: address is: %s, port is: %d", descr, str_buf, ntohs(s6->sin6_port)));
#endif
}
}
#endif /* USE_DNSTAP */
#ifdef USE_TCP_FASTOPEN
#define TCP_FASTOPEN_FILE "/proc/sys/net/ipv4/tcp_fastopen"
#define TCP_FASTOPEN_SERVER_BIT_MASK 0x2
#endif
/* header state for the PROXYv2 header (for TCP) */
enum pp2_header_state {
/* no header encounter yet */
pp2_header_none = 0,
/* read the static part of the header */
pp2_header_init,
/* read the full header */
pp2_header_done
};
/*
* Data for the UDP handlers.
*/
struct udp_handler_data
{
struct nsd *nsd;
struct nsd_socket *socket;
struct event event;
/* if set, PROXYv2 is expected on this connection */
int pp2_enabled;
};
struct tcp_accept_handler_data {
struct nsd *nsd;
struct nsd_socket *socket;
int event_added;
struct event event;
#ifdef HAVE_SSL
/* handler accepts TLS connections on the dedicated port */
int tls_accept;
int tls_auth_accept;
#endif
/* if set, PROXYv2 is expected on this connection */
int pp2_enabled;
};
/*
* These globals are used to enable the TCP accept handlers
* when the number of TCP connection drops below the maximum
* number of TCP connections.
*/
static size_t tcp_accept_handler_count;
static struct tcp_accept_handler_data *tcp_accept_handlers;
static struct event slowaccept_event;
static int slowaccept;
#ifdef HAVE_SSL
static unsigned char *ocspdata = NULL;
static long ocspdata_len = 0;
#endif
#ifdef NONBLOCKING_IS_BROKEN
/* Define NUM_RECV_PER_SELECT to 1 (one) to avoid opportunistically trying to
read multiple times from a socket when reported ready by select. */
# define NUM_RECV_PER_SELECT (1)
#else /* !NONBLOCKING_IS_BROKEN */
# define NUM_RECV_PER_SELECT (100)
#endif /* NONBLOCKING_IS_BROKEN */
#ifndef HAVE_MMSGHDR
struct mmsghdr {
struct msghdr msg_hdr;
unsigned int msg_len;
};
#endif
static struct mmsghdr msgs[NUM_RECV_PER_SELECT];
static struct iovec iovecs[NUM_RECV_PER_SELECT];
static struct query *queries[NUM_RECV_PER_SELECT];
/*
* Data for the TCP connection handlers.
*
* The TCP handlers use non-blocking I/O. This is necessary to avoid
* blocking the entire server on a slow TCP connection, but does make
* reading from and writing to the socket more complicated.
*
* Basically, whenever a read/write would block (indicated by the
* EAGAIN errno variable) we remember the position we were reading
* from/writing to and return from the TCP reading/writing event
* handler. When the socket becomes readable/writable again we
* continue from the same position.
*/
struct tcp_handler_data
{
/*
* The region used to allocate all TCP connection related
* data, including this structure. This region is destroyed
* when the connection is closed.
*/
region_type* region;
/*
* The global nsd structure.
*/
struct nsd* nsd;
/*
* The current query data for this TCP connection.
*/
query_type* query;
/*
* The query_state is used to remember if we are performing an
* AXFR, if we're done processing, or if we should discard the
* query and connection.
*/
query_state_type query_state;
/*
* The event for the file descriptor and tcp timeout
*/
struct event event;
/*
* The bytes_transmitted field is used to remember the number
* of bytes transmitted when receiving or sending a DNS
* packet. The count includes the two additional bytes used
* to specify the packet length on a TCP connection.
*/
size_t bytes_transmitted;
/* If the query is restarted and needs a reset */
int query_needs_reset;
/*
* The number of queries handled by this specific TCP connection.
*/
int query_count;
/*
* The timeout in msec for this tcp connection
*/
int tcp_timeout;
/*
* If the connection is allowed to have further queries on it.
*/
int tcp_no_more_queries;
#ifdef USE_DNSTAP
/* the socket of the accept socket to find proper service (local) address the socket is bound to. */
struct nsd_socket *socket;
#endif /* USE_DNSTAP */
/* if set, PROXYv2 is expected on this connection */
int pp2_enabled;
/* header state for the PROXYv2 header (for TCP) */
enum pp2_header_state pp2_header_state;
#ifdef HAVE_SSL
/*
* TLS objects.
*/
SSL* tls;
SSL* tls_auth;
/*
* TLS handshake state.
*/
enum { tls_hs_none, tls_hs_read, tls_hs_write,
tls_hs_read_event, tls_hs_write_event } shake_state;
#endif
/* list of connections, for service of remaining tcp channels */
struct tcp_handler_data *prev, *next;
};
/* global that is the list of active tcp channels */
static struct tcp_handler_data *tcp_active_list = NULL;
/*
* Handle incoming queries on the UDP server sockets.
*/
static void handle_udp(int fd, short event, void* arg);
/*
* Handle incoming connections on the TCP sockets. These handlers
* usually wait for the NETIO_EVENT_READ event (indicating an incoming
* connection) but are disabled when the number of current TCP
* connections is equal to the maximum number of TCP connections.
* Disabling is done by changing the handler to wait for the
* NETIO_EVENT_NONE type. This is done using the function
* configure_tcp_accept_handlers.
*/
static void handle_tcp_accept(int fd, short event, void* arg);
/*
* Handle incoming queries on a TCP connection. The TCP connections
* are configured to be non-blocking and the handler may be called
* multiple times before a complete query is received.
*/
static void handle_tcp_reading(int fd, short event, void* arg);
/*
* Handle outgoing responses on a TCP connection. The TCP connections
* are configured to be non-blocking and the handler may be called
* multiple times before a complete response is sent.
*/
static void handle_tcp_writing(int fd, short event, void* arg);
#ifdef HAVE_SSL
/* Create SSL object and associate fd */
static SSL* incoming_ssl_fd(SSL_CTX* ctx, int fd);
/*
* Handle TLS handshake. May be called multiple times if incomplete.
*/
static int tls_handshake(struct tcp_handler_data* data, int fd, int writing);
/*
* Handle incoming queries on a TLS over TCP connection. The TLS
* connections are configured to be non-blocking and the handler may
* be called multiple times before a complete query is received.
*/
static void handle_tls_reading(int fd, short event, void* arg);
/*
* Handle outgoing responses on a TLS over TCP connection. The TLS
* connections are configured to be non-blocking and the handler may
* be called multiple times before a complete response is sent.
*/
static void handle_tls_writing(int fd, short event, void* arg);
#endif
/*
* Send all children the quit nonblocking, then close pipe.
*/
static void send_children_quit(struct nsd* nsd);
/* same, for shutdown time, waits for child to exit to avoid restart issues */
static void send_children_quit_and_wait(struct nsd* nsd);
/* set childrens flags to send NSD_STATS to them */
#ifdef BIND8_STATS
static void set_children_stats(struct nsd* nsd);
#endif /* BIND8_STATS */
/*
* Change the event types the HANDLERS are interested in to EVENT_TYPES.
*/
static void configure_handler_event_types(short event_types);
static uint16_t *compressed_dname_offsets = 0;
static uint32_t compression_table_capacity = 0;
static uint32_t compression_table_size = 0;
static domain_type* compressed_dnames[MAXRRSPP];
#ifdef USE_TCP_FASTOPEN
/* Checks to see if the kernel value must be manually changed in order for
TCP Fast Open to support server mode */
static void report_tcp_fastopen_config() {
int tcp_fastopen_fp;
uint8_t tcp_fastopen_value;
if ( (tcp_fastopen_fp = open(TCP_FASTOPEN_FILE, O_RDONLY)) == -1 ) {
log_msg(LOG_INFO,"Error opening " TCP_FASTOPEN_FILE ": %s\n", strerror(errno));
}
if (read(tcp_fastopen_fp, &tcp_fastopen_value, 1) == -1 ) {
log_msg(LOG_INFO,"Error reading " TCP_FASTOPEN_FILE ": %s\n", strerror(errno));
close(tcp_fastopen_fp);
}
if (!(tcp_fastopen_value & TCP_FASTOPEN_SERVER_BIT_MASK)) {
log_msg(LOG_WARNING, "Error: TCP Fast Open support is available and configured in NSD by default.\n");
log_msg(LOG_WARNING, "However the kernel parameters are not configured to support TCP_FASTOPEN in server mode.\n");
log_msg(LOG_WARNING, "To enable TFO use the command:");
log_msg(LOG_WARNING, " 'sudo sysctl -w net.ipv4.tcp_fastopen=2' for pure server mode or\n");
log_msg(LOG_WARNING, " 'sudo sysctl -w net.ipv4.tcp_fastopen=3' for both client and server mode\n");
log_msg(LOG_WARNING, "NSD will not have TCP Fast Open available until this change is made.\n");
close(tcp_fastopen_fp);
}
close(tcp_fastopen_fp);
}
#endif
/*
* Remove the specified pid from the list of child pids. Returns -1 if
* the pid is not in the list, child_num otherwise. The field is set to 0.
*/
static int
delete_child_pid(struct nsd *nsd, pid_t pid)
{
size_t i;
for (i = 0; i < nsd->child_count; ++i) {
if (nsd->children[i].pid == pid) {
nsd->children[i].pid = 0;
if(!nsd->children[i].need_to_exit) {
if(nsd->children[i].child_fd != -1)
close(nsd->children[i].child_fd);
nsd->children[i].child_fd = -1;
if(nsd->children[i].handler)
nsd->children[i].handler->fd = -1;
}
return i;
}
}
return -1;
}
/*
* Restart child servers if necessary.
*/
static int
restart_child_servers(struct nsd *nsd, region_type* region, netio_type* netio,
int* xfrd_sock_p)
{
size_t i;
int sv[2];
/* Fork the child processes... */
for (i = 0; i < nsd->child_count; ++i) {
if (nsd->children[i].pid <= 0) {
if (nsd->children[i].child_fd != -1)
close(nsd->children[i].child_fd);
if (socketpair(AF_UNIX, SOCK_STREAM, 0, sv) == -1) {
log_msg(LOG_ERR, "socketpair: %s",
strerror(errno));
return -1;
}
nsd->children[i].child_fd = sv[0];
nsd->children[i].parent_fd = sv[1];
nsd->children[i].pid = fork();
switch (nsd->children[i].pid) {
default: /* SERVER MAIN */
close(nsd->children[i].parent_fd);
nsd->children[i].parent_fd = -1;
if (fcntl(nsd->children[i].child_fd, F_SETFL, O_NONBLOCK) == -1) {
log_msg(LOG_ERR, "cannot fcntl pipe: %s", strerror(errno));
}
if(!nsd->children[i].handler)
{
struct main_ipc_handler_data *ipc_data;
ipc_data = (struct main_ipc_handler_data*) region_alloc(
region, sizeof(struct main_ipc_handler_data));
ipc_data->nsd = nsd;
ipc_data->child = &nsd->children[i];
nsd->children[i].handler = (struct netio_handler*) region_alloc(
region, sizeof(struct netio_handler));
nsd->children[i].handler->fd = nsd->children[i].child_fd;
nsd->children[i].handler->timeout = NULL;
nsd->children[i].handler->user_data = ipc_data;
nsd->children[i].handler->event_types = NETIO_EVENT_READ;
nsd->children[i].handler->event_handler = parent_handle_child_command;
netio_add_handler(netio, nsd->children[i].handler);
}
/* restart - update fd */
nsd->children[i].handler->fd = nsd->children[i].child_fd;
break;
case 0: /* CHILD */
#ifdef MEMCLEAN /* OS collects memory pages */
region_destroy(region);
#endif
nsd->pid = 0;
nsd->child_count = 0;
nsd->server_kind = nsd->children[i].kind;
nsd->this_child = &nsd->children[i];
nsd->this_child->child_num = i;
/* remove signal flags inherited from parent
the parent will handle them. */
nsd->signal_hint_reload_hup = 0;
nsd->signal_hint_reload = 0;
nsd->signal_hint_child = 0;
nsd->signal_hint_quit = 0;
nsd->signal_hint_shutdown = 0;
nsd->signal_hint_stats = 0;
nsd->signal_hint_statsusr = 0;
close(*xfrd_sock_p);
close(nsd->this_child->child_fd);
nsd->this_child->child_fd = -1;
if (fcntl(nsd->this_child->parent_fd, F_SETFL, O_NONBLOCK) == -1) {
log_msg(LOG_ERR, "cannot fcntl pipe: %s", strerror(errno));
}
server_child(nsd);
/* NOTREACH */
exit(0);
case -1:
log_msg(LOG_ERR, "fork failed: %s",
strerror(errno));
return -1;
}
}
}
return 0;
}
#ifdef BIND8_STATS
static void set_bind8_alarm(struct nsd* nsd)
{
/* resync so that the next alarm is on the next whole minute */
if(nsd->st_period > 0) /* % by 0 gives divbyzero error */
alarm(nsd->st_period - (time(NULL) % nsd->st_period));
}
#endif
/* set zone stat ids for zones initially read in */
static void
zonestatid_tree_set(struct nsd* nsd)
{
struct radnode* n;
for(n=radix_first(nsd->db->zonetree); n; n=radix_next(n)) {
zone_type* zone = (zone_type*)n->elem;
zone->zonestatid = getzonestatid(nsd->options, zone->opts);
}
}
#ifdef USE_ZONE_STATS
void
server_zonestat_alloc(struct nsd* nsd)
{
size_t num = (nsd->options->zonestatnames->count==0?1:
nsd->options->zonestatnames->count);
size_t sz = sizeof(struct nsdst)*num;
char tmpfile[256];
uint8_t z = 0;
/* file names */
nsd->zonestatfname[0] = 0;
nsd->zonestatfname[1] = 0;
snprintf(tmpfile, sizeof(tmpfile), "%snsd-xfr-%d/nsd.%u.zstat.0",
nsd->options->xfrdir, (int)getpid(), (unsigned)getpid());
nsd->zonestatfname[0] = region_strdup(nsd->region, tmpfile);
snprintf(tmpfile, sizeof(tmpfile), "%snsd-xfr-%d/nsd.%u.zstat.1",
nsd->options->xfrdir, (int)getpid(), (unsigned)getpid());
nsd->zonestatfname[1] = region_strdup(nsd->region, tmpfile);
/* file descriptors */
nsd->zonestatfd[0] = open(nsd->zonestatfname[0], O_CREAT|O_RDWR, 0600);
if(nsd->zonestatfd[0] == -1) {
log_msg(LOG_ERR, "cannot create %s: %s", nsd->zonestatfname[0],
strerror(errno));
exit(1);
}
nsd->zonestatfd[1] = open(nsd->zonestatfname[1], O_CREAT|O_RDWR, 0600);
if(nsd->zonestatfd[0] == -1) {
log_msg(LOG_ERR, "cannot create %s: %s", nsd->zonestatfname[1],
strerror(errno));
close(nsd->zonestatfd[0]);
unlink(nsd->zonestatfname[0]);
exit(1);
}
#ifdef HAVE_MMAP
if(lseek(nsd->zonestatfd[0], (off_t)sz-1, SEEK_SET) == -1) {
log_msg(LOG_ERR, "lseek %s: %s", nsd->zonestatfname[0],
strerror(errno));
exit(1);
}
if(write(nsd->zonestatfd[0], &z, 1) == -1) {
log_msg(LOG_ERR, "cannot extend stat file %s (%s)",
nsd->zonestatfname[0], strerror(errno));
exit(1);
}
if(lseek(nsd->zonestatfd[1], (off_t)sz-1, SEEK_SET) == -1) {
log_msg(LOG_ERR, "lseek %s: %s", nsd->zonestatfname[1],
strerror(errno));
exit(1);
}
if(write(nsd->zonestatfd[1], &z, 1) == -1) {
log_msg(LOG_ERR, "cannot extend stat file %s (%s)",
nsd->zonestatfname[1], strerror(errno));
exit(1);
}
nsd->zonestat[0] = (struct nsdst*)mmap(NULL, sz, PROT_READ|PROT_WRITE,
MAP_SHARED, nsd->zonestatfd[0], 0);
if(nsd->zonestat[0] == MAP_FAILED) {
log_msg(LOG_ERR, "mmap failed: %s", strerror(errno));
unlink(nsd->zonestatfname[0]);
unlink(nsd->zonestatfname[1]);
exit(1);
}
nsd->zonestat[1] = (struct nsdst*)mmap(NULL, sz, PROT_READ|PROT_WRITE,
MAP_SHARED, nsd->zonestatfd[1], 0);
if(nsd->zonestat[1] == MAP_FAILED) {
log_msg(LOG_ERR, "mmap failed: %s", strerror(errno));
unlink(nsd->zonestatfname[0]);
unlink(nsd->zonestatfname[1]);
exit(1);
}
memset(nsd->zonestat[0], 0, sz);
memset(nsd->zonestat[1], 0, sz);
nsd->zonestatsize[0] = num;
nsd->zonestatsize[1] = num;
nsd->zonestatdesired = num;
nsd->zonestatsizenow = num;
nsd->zonestatnow = nsd->zonestat[0];
#endif /* HAVE_MMAP */
}
void
zonestat_remap(struct nsd* nsd, int idx, size_t sz)
{
#ifdef HAVE_MMAP
#ifdef MREMAP_MAYMOVE
nsd->zonestat[idx] = (struct nsdst*)mremap(nsd->zonestat[idx],
sizeof(struct nsdst)*nsd->zonestatsize[idx], sz,
MREMAP_MAYMOVE);
if(nsd->zonestat[idx] == MAP_FAILED) {
log_msg(LOG_ERR, "mremap failed: %s", strerror(errno));
exit(1);
}
#else /* !HAVE MREMAP */
if(msync(nsd->zonestat[idx],
sizeof(struct nsdst)*nsd->zonestatsize[idx], MS_ASYNC) != 0)
log_msg(LOG_ERR, "msync failed: %s", strerror(errno));
if(munmap(nsd->zonestat[idx],
sizeof(struct nsdst)*nsd->zonestatsize[idx]) != 0)
log_msg(LOG_ERR, "munmap failed: %s", strerror(errno));
nsd->zonestat[idx] = (struct nsdst*)mmap(NULL, sz,
PROT_READ|PROT_WRITE, MAP_SHARED, nsd->zonestatfd[idx], 0);
if(nsd->zonestat[idx] == MAP_FAILED) {
log_msg(LOG_ERR, "mmap failed: %s", strerror(errno));
exit(1);
}
#endif /* MREMAP */
#endif /* HAVE_MMAP */
}
/* realloc the zonestat array for the one that is not currently in use,
* to match the desired new size of the array (if applicable) */
void
server_zonestat_realloc(struct nsd* nsd)
{
#ifdef HAVE_MMAP
uint8_t z = 0;
size_t sz;
int idx = 0; /* index of the zonestat array that is not in use */
if(nsd->zonestatnow == nsd->zonestat[0])
idx = 1;
if(nsd->zonestatsize[idx] == nsd->zonestatdesired)
return;
sz = sizeof(struct nsdst)*nsd->zonestatdesired;
if(lseek(nsd->zonestatfd[idx], (off_t)sz-1, SEEK_SET) == -1) {
log_msg(LOG_ERR, "lseek %s: %s", nsd->zonestatfname[idx],
strerror(errno));
exit(1);
}
if(write(nsd->zonestatfd[idx], &z, 1) == -1) {
log_msg(LOG_ERR, "cannot extend stat file %s (%s)",
nsd->zonestatfname[idx], strerror(errno));
exit(1);
}
zonestat_remap(nsd, idx, sz);
/* zero the newly allocated region */
if(nsd->zonestatdesired > nsd->zonestatsize[idx]) {
memset(((char*)nsd->zonestat[idx])+sizeof(struct nsdst) *
nsd->zonestatsize[idx], 0, sizeof(struct nsdst) *
(nsd->zonestatdesired - nsd->zonestatsize[idx]));
}
nsd->zonestatsize[idx] = nsd->zonestatdesired;
#endif /* HAVE_MMAP */
}
/* switchover to use the other array for the new children, that
* briefly coexist with the old children. And we want to avoid them
* both writing to the same statistics arrays. */
void
server_zonestat_switch(struct nsd* nsd)
{
if(nsd->zonestatnow == nsd->zonestat[0]) {
nsd->zonestatnow = nsd->zonestat[1];
nsd->zonestatsizenow = nsd->zonestatsize[1];
} else {
nsd->zonestatnow = nsd->zonestat[0];
nsd->zonestatsizenow = nsd->zonestatsize[0];
}
}
#endif /* USE_ZONE_STATS */
#ifdef BIND8_STATS
void
server_stat_alloc(struct nsd* nsd)
{
char tmpfile[256];
size_t sz = sizeof(struct nsdst) * nsd->child_count * 2;
uint8_t z = 0;
/* file name */
nsd->statfname = 0;
snprintf(tmpfile, sizeof(tmpfile), "%snsd-xfr-%d/nsd.%u.stat",
nsd->options->xfrdir, (int)getpid(), (unsigned)getpid());
nsd->statfname = region_strdup(nsd->region, tmpfile);
/* file descriptor */
nsd->statfd = open(nsd->statfname, O_CREAT|O_RDWR, 0600);
if(nsd->statfd == -1) {
log_msg(LOG_ERR, "cannot create %s: %s", nsd->statfname,
strerror(errno));
unlink(nsd->zonestatfname[0]);
unlink(nsd->zonestatfname[1]);
exit(1);
}
#ifdef HAVE_MMAP
if(lseek(nsd->statfd, (off_t)sz-1, SEEK_SET) == -1) {
log_msg(LOG_ERR, "lseek %s: %s", nsd->statfname,
strerror(errno));
goto fail_exit;
}
if(write(nsd->statfd, &z, 1) == -1) {
log_msg(LOG_ERR, "cannot extend stat file %s (%s)",
nsd->statfname, strerror(errno));
goto fail_exit;
}
nsd->stat_map = (struct nsdst*)mmap(NULL, sz, PROT_READ|PROT_WRITE,
MAP_SHARED, nsd->statfd, 0);
if(nsd->stat_map == MAP_FAILED) {
log_msg(LOG_ERR, "mmap failed: %s", strerror(errno));
fail_exit:
close(nsd->statfd);
unlink(nsd->statfname);
unlink(nsd->zonestatfname[0]);
unlink(nsd->zonestatfname[1]);
exit(1);
}
memset(nsd->stat_map, 0, sz);
nsd->stats_per_child[0] = nsd->stat_map;
nsd->stats_per_child[1] = &nsd->stat_map[nsd->child_count];
nsd->stat_current = 0;
nsd->st = &nsd->stats_per_child[nsd->stat_current][0];
#endif /* HAVE_MMAP */
}
#endif /* BIND8_STATS */
#ifdef BIND8_STATS
void
server_stat_free(struct nsd* nsd)
{
unlink(nsd->statfname);
}
#endif /* BIND8_STATS */
static void
cleanup_dname_compression_tables(void *ptr)
{
free(ptr);
compressed_dname_offsets = NULL;
compression_table_capacity = 0;
}
static void
initialize_dname_compression_tables(struct nsd *nsd)
{
size_t needed = domain_table_count(nsd->db->domains) + 1;
needed += EXTRA_DOMAIN_NUMBERS;
if(compression_table_capacity < needed) {
if(compressed_dname_offsets) {
region_remove_cleanup(nsd->db->region,
cleanup_dname_compression_tables,
compressed_dname_offsets);
free(compressed_dname_offsets);
}
compressed_dname_offsets = (uint16_t *) xmallocarray(
needed, sizeof(uint16_t));
region_add_cleanup(nsd->db->region, cleanup_dname_compression_tables,
compressed_dname_offsets);
compression_table_capacity = needed;
compression_table_size=domain_table_count(nsd->db->domains)+1;
}
memset(compressed_dname_offsets, 0, needed * sizeof(uint16_t));
compressed_dname_offsets[0] = QHEADERSZ; /* The original query name */
}
static int
set_cloexec(struct nsd_socket *sock)
{
assert(sock != NULL);
if(fcntl(sock->s, F_SETFD, FD_CLOEXEC) == -1) {
const char *socktype =
sock->addr.ai_family == SOCK_DGRAM ? "udp" : "tcp";
log_msg(LOG_ERR, "fcntl(..., O_CLOEXEC) failed for %s: %s",
socktype, strerror(errno));
return -1;
}
return 1;
}
static int
set_reuseport(struct nsd_socket *sock)
{
#ifdef SO_REUSEPORT
int on = 1;
#ifdef SO_REUSEPORT_LB
/* FreeBSD 12 has SO_REUSEPORT_LB that does load balancing like
* SO_REUSEPORT on Linux. This is what the users want with the config
* option in nsd.conf; if we actually need local address and port reuse
* they'll also need to have SO_REUSEPORT set for them, assume it was
* _LB they want.
*/
int opt = SO_REUSEPORT_LB;
static const char optname[] = "SO_REUSEPORT_LB";
#else /* !SO_REUSEPORT_LB */
int opt = SO_REUSEPORT;
static const char optname[] = "SO_REUSEPORT";
#endif /* SO_REUSEPORT_LB */
if (0 == setsockopt(sock->s, SOL_SOCKET, opt, &on, sizeof(on))) {
return 1;
} else if(verbosity >= 3 || errno != ENOPROTOOPT) {
log_msg(LOG_ERR, "setsockopt(..., %s, ...) failed: %s",
optname, strerror(errno));
}
return -1;
#else
(void)sock;
#endif /* SO_REUSEPORT */
return 0;
}
static int
set_reuseaddr(struct nsd_socket *sock)
{
#ifdef SO_REUSEADDR
int on = 1;
if(setsockopt(sock->s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == 0) {
return 1;
}
log_msg(LOG_ERR, "setsockopt(..., SO_REUSEADDR, ...) failed: %s",
strerror(errno));
return -1;
#endif /* SO_REUSEADDR */
return 0;
}
static int
set_rcvbuf(struct nsd_socket *sock, int rcv)
{
#ifdef SO_RCVBUF
#ifdef SO_RCVBUFFORCE
if(0 == setsockopt(
sock->s, SOL_SOCKET, SO_RCVBUFFORCE, &rcv, sizeof(rcv)))
{
return 1;
}
if(errno == EPERM || errno == ENOBUFS) {
return 0;
}
log_msg(LOG_ERR, "setsockopt(..., SO_RCVBUFFORCE, ...) failed: %s",
strerror(errno));
return -1;
#else /* !SO_RCVBUFFORCE */
if (0 == setsockopt(
sock->s, SOL_SOCKET, SO_RCVBUF, &rcv, sizeof(rcv)))
{
return 1;
}
if(errno == ENOSYS || errno == ENOBUFS) {
return 0;
}
log_msg(LOG_ERR, "setsockopt(..., SO_RCVBUF, ...) failed: %s",
strerror(errno));
return -1;
#endif /* SO_RCVBUFFORCE */
#endif /* SO_RCVBUF */
return 0;
}
static int
set_sndbuf(struct nsd_socket *sock, int snd)
{
#ifdef SO_SNDBUF
#ifdef SO_SNDBUFFORCE
if(0 == setsockopt(
sock->s, SOL_SOCKET, SO_SNDBUFFORCE, &snd, sizeof(snd)))
{
return 1;
}
if(errno == EPERM || errno == ENOBUFS) {
return 0;
}
log_msg(LOG_ERR, "setsockopt(..., SO_SNDBUFFORCE, ...) failed: %s",
strerror(errno));
return -1;
#else /* !SO_SNDBUFFORCE */
if(0 == setsockopt(
sock->s, SOL_SOCKET, SO_SNDBUF, &snd, sizeof(snd)))
{
return 1;
}
if(errno == ENOSYS || errno == ENOBUFS) {
return 0;
}
log_msg(LOG_ERR, "setsockopt(..., SO_SNDBUF, ...) failed: %s",
strerror(errno));
return -1;
#endif /* SO_SNDBUFFORCE */
#endif /* SO_SNDBUF */
return 0;
}
static int
set_nonblock(struct nsd_socket *sock)
{
const char *socktype =
sock->addr.ai_socktype == SOCK_DGRAM ? "udp" : "tcp";
if(fcntl(sock->s, F_SETFL, O_NONBLOCK) == -1) {
log_msg(LOG_ERR, "fctnl(..., O_NONBLOCK) failed for %s: %s",
socktype, strerror(errno));
return -1;
}
return 1;
}
#ifdef INET6
static int
set_ipv6_v6only(struct nsd_socket *sock)
{
#ifdef IPV6_V6ONLY
int on = 1;
const char *socktype =
sock->addr.ai_socktype == SOCK_DGRAM ? "udp" : "tcp";
if(0 == setsockopt(
sock->s, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on)))
{
return 1;
}
log_msg(LOG_ERR, "setsockopt(..., IPV6_V6ONLY, ...) failed for %s: %s",
socktype, strerror(errno));
return -1;
#else
(void)sock;
#endif /* IPV6_V6ONLY */
return 0;
}
#endif /* INET6 */
#ifdef INET6
static int
set_ipv6_use_min_mtu(struct nsd_socket *sock)
{
#if defined(IPV6_USE_MIN_MTU) || defined(IPV6_MTU)
#if defined(IPV6_USE_MIN_MTU)
/* There is no fragmentation of IPv6 datagrams during forwarding in the
* network. Therefore we do not send UDP datagrams larger than the
* minimum IPv6 MTU of 1280 octets. The EDNS0 message length can be
* larger if the network stack supports IPV6_USE_MIN_MTU.
*/
int opt = IPV6_USE_MIN_MTU;
int optval = 1;
static const char optname[] = "IPV6_USE_MIN_MTU";
#elif defined(IPV6_MTU)
/* On Linux, PMTUD is disabled by default for datagrams so set the MTU
* to the MIN MTU to get the same.
*/
int opt = IPV6_MTU;
int optval = IPV6_MIN_MTU;
static const char optname[] = "IPV6_MTU";
#endif
if(0 == setsockopt(
sock->s, IPPROTO_IPV6, opt, &optval, sizeof(optval)))
{
return 1;
}
log_msg(LOG_ERR, "setsockopt(..., %s, ...) failed: %s",
optname, strerror(errno));
return -1;
#else