]> git.ipfire.org Git - people/ms/dnsmasq.git/blame - src/dnsmasq.c
Add ra-names SLAAC-hostnames from DHCPv4 option.
[people/ms/dnsmasq.git] / src / dnsmasq.c
CommitLineData
59546085 1/* dnsmasq is Copyright (c) 2000-2012 Simon Kelley
9e4abcb5
SK
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
824af85b
SK
5 the Free Software Foundation; version 2 dated June, 1991, or
6 (at your option) version 3 dated 29 June, 2007.
7
9e4abcb5
SK
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.
824af85b 12
73a08a24
SK
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/>.
9e4abcb5
SK
15*/
16
c72daea8
SK
17/* Declare static char *compiler_opts in config.h */
18#define DNSMASQ_COMPILE_OPTS
19
9e4abcb5
SK
20#include "dnsmasq.h"
21
5aabfc78
SK
22struct daemon *daemon;
23
5aabfc78
SK
24static volatile pid_t pid = 0;
25static volatile int pipewrite;
9e4abcb5 26
5aabfc78
SK
27static int set_dns_listeners(time_t now, fd_set *set, int *maxfdp);
28static void check_dns_listeners(fd_set *set, time_t now);
3be34541 29static void sig_handler(int sig);
5aabfc78 30static void async_event(int pipe, time_t now);
c72daea8
SK
31static void fatal_event(struct event_desc *ev, char *msg);
32static int read_event(int fd, struct event_desc *evp, char **msg);
9e4abcb5
SK
33
34int main (int argc, char **argv)
35{
de37951c 36 int bind_fallback = 0;
9009d746 37 time_t now;
9e4abcb5 38 struct sigaction sigact;
26128d27 39 struct iname *if_tmp;
1a6bca81
SK
40 int piperead, pipefd[2], err_pipe[2];
41 struct passwd *ent_pw = NULL;
c72daea8 42#if defined(HAVE_SCRIPT)
1a6bca81
SK
43 uid_t script_uid = 0;
44 gid_t script_gid = 0;
7622fc06
SK
45#endif
46 struct group *gp = NULL;
5aabfc78 47 long i, max_fd = sysconf(_SC_OPEN_MAX);
1a6bca81
SK
48 char *baduser = NULL;
49 int log_err;
50#if defined(HAVE_LINUX_NETWORK)
51 cap_user_header_t hdr = NULL;
52 cap_user_data_t data = NULL;
53#endif
5aabfc78 54
824af85b 55#ifdef LOCALEDIR
b8187c80
SK
56 setlocale(LC_ALL, "");
57 bindtextdomain("dnsmasq", LOCALEDIR);
58 textdomain("dnsmasq");
59#endif
60
9e4abcb5
SK
61 sigact.sa_handler = sig_handler;
62 sigact.sa_flags = 0;
63 sigemptyset(&sigact.sa_mask);
64 sigaction(SIGUSR1, &sigact, NULL);
5aabfc78 65 sigaction(SIGUSR2, &sigact, NULL);
9e4abcb5
SK
66 sigaction(SIGHUP, &sigact, NULL);
67 sigaction(SIGTERM, &sigact, NULL);
44a2a316 68 sigaction(SIGALRM, &sigact, NULL);
feba5c1d
SK
69 sigaction(SIGCHLD, &sigact, NULL);
70
71 /* ignore SIGPIPE */
72 sigact.sa_handler = SIG_IGN;
73 sigaction(SIGPIPE, &sigact, NULL);
9e4abcb5 74
5aabfc78
SK
75 umask(022); /* known umask, create leases and pid files as 0644 */
76
77 read_opts(argc, argv, compile_opts);
78
3be34541
SK
79 if (daemon->edns_pktsz < PACKETSZ)
80 daemon->edns_pktsz = PACKETSZ;
0a852541
SK
81 daemon->packet_buff_sz = daemon->edns_pktsz > DNSMASQ_PACKETSZ ?
82 daemon->edns_pktsz : DNSMASQ_PACKETSZ;
83 daemon->packet = safe_malloc(daemon->packet_buff_sz);
1a6bca81 84
c72daea8
SK
85 daemon->addrbuff = safe_malloc(ADDRSTRLEN);
86
7622fc06 87#ifdef HAVE_DHCP
3be34541 88 if (!daemon->lease_file)
9e4abcb5 89 {
52b92f4d 90 if (daemon->dhcp || daemon->dhcp6)
3be34541 91 daemon->lease_file = LEASEFILE;
9e4abcb5 92 }
7622fc06 93#endif
9e4abcb5 94
a2761754
SK
95 /* Close any file descriptors we inherited apart from std{in|out|err}
96
97 Ensure that at least stdin, stdout and stderr (fd 0, 1, 2) exist,
98 otherwise file descriptors we create can end up being 0, 1, or 2
99 and then get accidentally closed later when we make 0, 1, and 2
100 open to /dev/null. Normally we'll be started with 0, 1 and 2 open,
101 but it's not guaranteed. By opening /dev/null three times, we
102 ensure that we're not using those fds for real stuff. */
5aabfc78
SK
103 for (i = 0; i < max_fd; i++)
104 if (i != STDOUT_FILENO && i != STDERR_FILENO && i != STDIN_FILENO)
105 close(i);
a2761754
SK
106 else
107 open("/dev/null", O_RDWR);
5aabfc78 108
801ca9a7
SK
109#ifndef HAVE_LINUX_NETWORK
110# if !(defined(IP_RECVDSTADDR) && defined(IP_RECVIF) && defined(IP_SENDSRCADDR))
28866e95 111 if (!option_bool(OPT_NOWILD))
de37951c
SK
112 {
113 bind_fallback = 1;
28866e95 114 set_option_bool(OPT_NOWILD);
de37951c 115 }
801ca9a7 116# endif
309331f5
SK
117#endif
118
832af0ba 119#ifndef HAVE_TFTP
8ef5ada2 120 if (daemon->tftp_unlimited || daemon->tftp_interfaces)
5aabfc78 121 die(_("TFTP server not available: set HAVE_TFTP in src/config.h"), NULL, EC_BADCONF);
832af0ba
SK
122#endif
123
7de060b0
SK
124#ifdef HAVE_CONNTRACK
125 if (option_bool(OPT_CONNTRACK) && (daemon->query_port != 0 || daemon->osport))
126 die (_("Cannot use --conntrack AND --query-port"), NULL, EC_BADCONF);
127#else
128 if (option_bool(OPT_CONNTRACK))
129 die(_("Conntrack support not available: set HAVE_CONNTRACK in src/config.h"), NULL, EC_BADCONF);
130#endif
131
824af85b
SK
132#ifdef HAVE_SOLARIS_NETWORK
133 if (daemon->max_logs != 0)
134 die(_("asychronous logging is not available under Solaris"), NULL, EC_BADCONF);
135#endif
136
572b41eb
SK
137#ifdef __ANDROID__
138 if (daemon->max_logs != 0)
139 die(_("asychronous logging is not available under Android"), NULL, EC_BADCONF);
140#endif
141
1a6bca81
SK
142 rand_init();
143
5aabfc78
SK
144 now = dnsmasq_time();
145
7622fc06 146#ifdef HAVE_DHCP
52b92f4d 147 if (daemon->dhcp || daemon->dhcp6)
5aabfc78 148 {
5aabfc78
SK
149 /* Note that order matters here, we must call lease_init before
150 creating any file descriptors which shouldn't be leaked
4cb1b320
SK
151 to the lease-script init process. We need to call common_init
152 before lease_init to allocate buffers it uses.*/
153 dhcp_common_init();
5aabfc78 154 lease_init(now);
843c96b4 155
52b92f4d
SK
156 if (daemon->dhcp)
157 dhcp_init();
843c96b4
SK
158 }
159
160# ifdef HAVE_DHCP6
161 /* Start RA subsystem if --enable-ra OR dhcp-range=<subnet>, ra-only */
162 if (daemon->ra_contexts || option_bool(OPT_RA))
163 {
164 /* link the DHCP6 contexts to the ra-only ones so we can traverse them all
165 from ->ra_contexts, but only the non-ra-onlies from ->dhcp6 */
166 struct dhcp_context *context;
167
168 if (!daemon->ra_contexts)
169 daemon->ra_contexts = daemon->dhcp6;
170 else
c5ad4e79 171 {
843c96b4
SK
172 for (context = daemon->ra_contexts; context->next; context = context->next);
173 context->next = daemon->dhcp6;
c5ad4e79 174 }
843c96b4 175 ra_init(now);
5aabfc78 176 }
843c96b4
SK
177
178 if (daemon->dhcp6)
179 dhcp6_init();
180
801ca9a7
SK
181# endif
182
183#endif
184
185#ifdef HAVE_LINUX_NETWORK
186 /* After lease_init */
187 netlink_init();
188#endif
189
190#ifdef HAVE_DHCP6
191 /* after netlink_init */
843c96b4
SK
192 if (daemon->ra_contexts || daemon->dhcp6)
193 join_multicast();
801ca9a7 194#endif
843c96b4 195
801ca9a7
SK
196#ifdef HAVE_DHCP
197 /* after netlink_init */
198 if (daemon->dhcp || daemon->dhcp6)
199 lease_find_interfaces();
7622fc06 200#endif
5aabfc78
SK
201
202 if (!enumerate_interfaces())
203 die(_("failed to find list of interfaces: %s"), NULL, EC_MISC);
843c96b4 204
28866e95 205 if (option_bool(OPT_NOWILD))
de37951c 206 {
74c95c25 207 create_bound_listeners(1);
de37951c 208
3be34541 209 for (if_tmp = daemon->if_names; if_tmp; if_tmp = if_tmp->next)
de37951c 210 if (if_tmp->name && !if_tmp->used)
5aabfc78 211 die(_("unknown interface %s"), if_tmp->name, EC_BADNET);
de37951c 212
3be34541 213 for (if_tmp = daemon->if_addrs; if_tmp; if_tmp = if_tmp->next)
de37951c
SK
214 if (!if_tmp->used)
215 {
3d8df260 216 prettyprint_addr(&if_tmp->addr, daemon->namebuff);
5aabfc78 217 die(_("no interface with address %s"), daemon->namebuff, EC_BADNET);
de37951c
SK
218 }
219 }
28866e95 220 else
74c95c25 221 create_wildcard_listeners();
de37951c 222
824af85b
SK
223 if (daemon->port != 0)
224 cache_init();
1a6bca81 225
28866e95 226 if (option_bool(OPT_DBUS))
3d8df260
SK
227#ifdef HAVE_DBUS
228 {
229 char *err;
230 daemon->dbus = NULL;
231 daemon->watches = NULL;
5aabfc78
SK
232 if ((err = dbus_init()))
233 die(_("DBus error: %s"), err, EC_MISC);
3d8df260
SK
234 }
235#else
5aabfc78 236 die(_("DBus not available: set HAVE_DBUS in src/config.h"), NULL, EC_BADCONF);
3d8df260
SK
237#endif
238
824af85b
SK
239 if (daemon->port != 0)
240 pre_allocate_sfds();
1a6bca81 241
c72daea8 242#if defined(HAVE_SCRIPT)
1a6bca81 243 /* Note getpwnam returns static storage */
843c96b4
SK
244 if ((daemon->dhcp || daemon->dhcp6) &&
245 daemon->scriptuser &&
c72daea8 246 (daemon->lease_change_command || daemon->luascript))
1a6bca81
SK
247 {
248 if ((ent_pw = getpwnam(daemon->scriptuser)))
249 {
250 script_uid = ent_pw->pw_uid;
251 script_gid = ent_pw->pw_gid;
252 }
253 else
254 baduser = daemon->scriptuser;
255 }
7622fc06 256#endif
9e4abcb5 257
1a6bca81
SK
258 if (daemon->username && !(ent_pw = getpwnam(daemon->username)))
259 baduser = daemon->username;
260 else if (daemon->groupname && !(gp = getgrnam(daemon->groupname)))
261 baduser = daemon->groupname;
262
263 if (baduser)
264 die(_("unknown user or group: %s"), baduser, EC_BADCONF);
265
266 /* implement group defaults, "dip" if available, or group associated with uid */
267 if (!daemon->group_set && !gp)
268 {
269 if (!(gp = getgrnam(CHGRP)) && ent_pw)
270 gp = getgrgid(ent_pw->pw_gid);
271
272 /* for error message */
273 if (gp)
274 daemon->groupname = gp->gr_name;
275 }
276
277#if defined(HAVE_LINUX_NETWORK)
278 /* determine capability API version here, while we can still
279 call safe_malloc */
280 if (ent_pw && ent_pw->pw_uid != 0)
281 {
1a6bca81 282 int capsize = 1; /* for header version 1 */
3927da46
SK
283 hdr = safe_malloc(sizeof(*hdr));
284
1a6bca81
SK
285 /* find version supported by kernel */
286 memset(hdr, 0, sizeof(*hdr));
287 capget(hdr, NULL);
288
289 if (hdr->version != LINUX_CAPABILITY_VERSION_1)
290 {
291 /* if unknown version, use largest supported version (3) */
292 if (hdr->version != LINUX_CAPABILITY_VERSION_2)
293 hdr->version = LINUX_CAPABILITY_VERSION_3;
294 capsize = 2;
295 }
296
297 data = safe_malloc(sizeof(*data) * capsize);
298 memset(data, 0, sizeof(*data) * capsize);
299 }
300#endif
301
5aabfc78 302 /* Use a pipe to carry signals and other events back to the event loop
1a6bca81
SK
303 in a race-free manner and another to carry errors to daemon-invoking process */
304 safe_pipe(pipefd, 1);
5e9e0efb
SK
305
306 piperead = pipefd[0];
307 pipewrite = pipefd[1];
308 /* prime the pipe to load stuff first time. */
c72daea8 309 send_event(pipewrite, EVENT_RELOAD, 0, NULL);
1a6bca81
SK
310
311 err_pipe[1] = -1;
1697269c 312
28866e95 313 if (!option_bool(OPT_DEBUG))
9e4abcb5 314 {
9e4abcb5
SK
315 /* The following code "daemonizes" the process.
316 See Stevens section 12.4 */
1a6bca81 317
9e038946
SK
318 if (chdir("/") != 0)
319 die(_("cannot chdir to filesystem root: %s"), NULL, EC_MISC);
320
1697269c 321#ifndef NO_FORK
28866e95 322 if (!option_bool(OPT_NO_FORK))
3be34541 323 {
5aabfc78 324 pid_t pid;
3be34541 325
1a6bca81
SK
326 /* pipe to carry errors back to original process.
327 When startup is complete we close this and the process terminates. */
328 safe_pipe(err_pipe, 0);
329
7622fc06
SK
330 if ((pid = fork()) == -1)
331 /* fd == -1 since we've not forked, never returns. */
c72daea8 332 send_event(-1, EVENT_FORK_ERR, errno, NULL);
9e038946 333
5aabfc78 334 if (pid != 0)
1a6bca81
SK
335 {
336 struct event_desc ev;
c72daea8
SK
337 char *msg;
338
1a6bca81
SK
339 /* close our copy of write-end */
340 close(err_pipe[1]);
341
342 /* check for errors after the fork */
c72daea8
SK
343 if (read_event(err_pipe[0], &ev, &msg))
344 fatal_event(&ev, msg);
1a6bca81
SK
345
346 _exit(EC_GOOD);
347 }
348
349 close(err_pipe[0]);
350
351 /* NO calls to die() from here on. */
3be34541 352
5aabfc78 353 setsid();
7622fc06
SK
354
355 if ((pid = fork()) == -1)
c72daea8 356 send_event(err_pipe[1], EVENT_FORK_ERR, errno, NULL);
7622fc06
SK
357
358 if (pid != 0)
7cebd20f 359 _exit(0);
3be34541 360 }
9e4abcb5 361#endif
9e038946 362
9e4abcb5 363 /* write pidfile _after_ forking ! */
1a6bca81
SK
364 if (daemon->runfile)
365 {
366 FILE *pidfile;
367
368 /* only complain if started as root */
369 if ((pidfile = fopen(daemon->runfile, "w")))
370 {
371 fprintf(pidfile, "%d\n", (int) getpid());
372 fclose(pidfile);
373 }
374 else if (getuid() == 0)
375 {
c72daea8 376 send_event(err_pipe[1], EVENT_PIDFILE, errno, daemon->runfile);
1a6bca81
SK
377 _exit(0);
378 }
9e4abcb5 379 }
1697269c
SK
380 }
381
8ef5ada2
SK
382 log_err = log_start(ent_pw, err_pipe[1]);
383
28866e95 384 if (!option_bool(OPT_DEBUG))
8ef5ada2
SK
385 {
386 /* open stdout etc to /dev/null */
387 int nullfd = open("/dev/null", O_RDWR);
388 dup2(nullfd, STDOUT_FILENO);
389 dup2(nullfd, STDERR_FILENO);
390 dup2(nullfd, STDIN_FILENO);
391 close(nullfd);
392 }
1a6bca81
SK
393
394 /* if we are to run scripts, we need to fork a helper before dropping root. */
395 daemon->helperfd = -1;
c72daea8 396#ifdef HAVE_SCRIPT
52b92f4d 397 if ((daemon->dhcp || daemon->dhcp6) && (daemon->lease_change_command || daemon->luascript))
1a6bca81 398 daemon->helperfd = create_helper(pipewrite, err_pipe[1], script_uid, script_gid, max_fd);
5aabfc78 399#endif
5aabfc78 400
28866e95 401 if (!option_bool(OPT_DEBUG) && getuid() == 0)
1697269c 402 {
1a6bca81
SK
403 int bad_capabilities = 0;
404 gid_t dummy;
405
406 /* remove all supplimentary groups */
407 if (gp &&
408 (setgroups(0, &dummy) == -1 ||
409 setgid(gp->gr_gid) == -1))
9e4abcb5 410 {
c72daea8 411 send_event(err_pipe[1], EVENT_GROUP_ERR, errno, daemon->groupname);
1a6bca81 412 _exit(0);
7cebd20f 413 }
1a6bca81 414
7cebd20f 415 if (ent_pw && ent_pw->pw_uid != 0)
1697269c 416 {
74c95c25 417#if defined(HAVE_LINUX_NETWORK)
1697269c 418 /* On linux, we keep CAP_NETADMIN (for ARP-injection) and
74c95c25
SK
419 CAP_NET_RAW (for icmp) if we're doing dhcp. If we have yet to bind
420 ports because of DAD, we need CAP_NET_BIND_SERVICE too. */
421 if (is_dad_listeners())
422 data->effective = data->permitted = data->inheritable =
423 (1 << CAP_NET_ADMIN) | (1 << CAP_NET_RAW) |
424 (1 << CAP_SETUID) | (1 << CAP_NET_BIND_SERVICE);
425 else
426 data->effective = data->permitted = data->inheritable =
427 (1 << CAP_NET_ADMIN) | (1 << CAP_NET_RAW) | (1 << CAP_SETUID);
5e9e0efb 428
1697269c 429 /* Tell kernel to not clear capabilities when dropping root */
572b41eb 430 if (capset(hdr, data) == -1 || prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0) == -1)
1697269c 431 bad_capabilities = errno;
1a6bca81 432
7622fc06 433#elif defined(HAVE_SOLARIS_NETWORK)
824af85b
SK
434 /* http://developers.sun.com/solaris/articles/program_privileges.html */
435 priv_set_t *priv_set;
436
437 if (!(priv_set = priv_str_to_set("basic", ",", NULL)) ||
438 priv_addset(priv_set, PRIV_NET_ICMPACCESS) == -1 ||
439 priv_addset(priv_set, PRIV_SYS_NET_CONFIG) == -1)
440 bad_capabilities = errno;
441
442 if (priv_set && bad_capabilities == 0)
443 {
444 priv_inverse(priv_set);
445
446 if (setppriv(PRIV_OFF, PRIV_LIMIT, priv_set) == -1)
447 bad_capabilities = errno;
448 }
449
450 if (priv_set)
451 priv_freeset(priv_set);
452
824af85b
SK
453#endif
454
1a6bca81 455 if (bad_capabilities != 0)
1697269c 456 {
c72daea8 457 send_event(err_pipe[1], EVENT_CAP_ERR, bad_capabilities, NULL);
1a6bca81
SK
458 _exit(0);
459 }
460
461 /* finally drop root */
462 if (setuid(ent_pw->pw_uid) == -1)
463 {
c72daea8 464 send_event(err_pipe[1], EVENT_USER_ERR, errno, daemon->username);
1a6bca81
SK
465 _exit(0);
466 }
467
1697269c 468#ifdef HAVE_LINUX_NETWORK
74c95c25
SK
469 if (is_dad_listeners())
470 data->effective = data->permitted =
471 (1 << CAP_NET_ADMIN) | (1 << CAP_NET_RAW) | (1 << CAP_NET_BIND_SERVICE);
472 else
473 data->effective = data->permitted =
474 (1 << CAP_NET_ADMIN) | (1 << CAP_NET_RAW);
1a6bca81
SK
475 data->inheritable = 0;
476
477 /* lose the setuid and setgid capbilities */
478 if (capset(hdr, data) == -1)
479 {
c72daea8 480 send_event(err_pipe[1], EVENT_CAP_ERR, errno, NULL);
1a6bca81 481 _exit(0);
1697269c 482 }
1a6bca81
SK
483#endif
484
9e4abcb5
SK
485 }
486 }
1697269c 487
1697269c 488#ifdef HAVE_LINUX_NETWORK
28866e95 489 if (option_bool(OPT_DEBUG))
572b41eb 490 prctl(PR_SET_DUMPABLE, 1, 0, 0, 0);
1697269c 491#endif
9e4abcb5 492
824af85b
SK
493 if (daemon->port == 0)
494 my_syslog(LOG_INFO, _("started, version %s DNS disabled"), VERSION);
495 else if (daemon->cachesize != 0)
f2621c7f 496 my_syslog(LOG_INFO, _("started, version %s cachesize %d"), VERSION, daemon->cachesize);
9e4abcb5 497 else
f2621c7f 498 my_syslog(LOG_INFO, _("started, version %s cache disabled"), VERSION);
1697269c 499
f2621c7f 500 my_syslog(LOG_INFO, _("compile time options: %s"), compile_opts);
1697269c 501
3d8df260 502#ifdef HAVE_DBUS
28866e95 503 if (option_bool(OPT_DBUS))
3d8df260
SK
504 {
505 if (daemon->dbus)
f2621c7f 506 my_syslog(LOG_INFO, _("DBus support enabled: connected to system bus"));
3d8df260 507 else
f2621c7f 508 my_syslog(LOG_INFO, _("DBus support enabled: bus connection pending"));
3d8df260
SK
509 }
510#endif
511
1a6bca81
SK
512 if (log_err != 0)
513 my_syslog(LOG_WARNING, _("warning: failed to change owner of %s: %s"),
514 daemon->log_file, strerror(log_err));
515
de37951c 516 if (bind_fallback)
f2621c7f 517 my_syslog(LOG_WARNING, _("setting --bind-interfaces option because of OS limitations"));
de37951c 518
28866e95 519 if (!option_bool(OPT_NOWILD))
26128d27
SK
520 for (if_tmp = daemon->if_names; if_tmp; if_tmp = if_tmp->next)
521 if (if_tmp->name && !if_tmp->used)
f2621c7f 522 my_syslog(LOG_WARNING, _("warning: interface %s does not currently exist"), if_tmp->name);
5e9e0efb 523
28866e95 524 if (daemon->port != 0 && option_bool(OPT_NO_RESOLV))
208b65c5
SK
525 {
526 if (daemon->resolv_files && !daemon->resolv_files->is_default)
f2621c7f 527 my_syslog(LOG_WARNING, _("warning: ignoring resolv-file flag because no-resolv is set"));
208b65c5 528 daemon->resolv_files = NULL;
1b7ecd11 529 if (!daemon->servers)
f2621c7f 530 my_syslog(LOG_WARNING, _("warning: no upstream servers configured"));
208b65c5
SK
531 }
532
f2621c7f
SK
533 if (daemon->max_logs != 0)
534 my_syslog(LOG_INFO, _("asynchronous logging enabled, queue limit is %d messages"), daemon->max_logs);
c5ad4e79 535
843c96b4 536 if (daemon->ra_contexts)
c5ad4e79 537 my_syslog(MS_DHCP | LOG_INFO, _("IPv6 router advertisement enabled"));
f2621c7f 538
7622fc06 539#ifdef HAVE_DHCP
52b92f4d 540 if (daemon->dhcp || daemon->dhcp6)
9e4abcb5 541 {
3be34541 542 struct dhcp_context *dhcp_tmp;
52b92f4d
SK
543 int family = AF_INET;
544 dhcp_tmp = daemon->dhcp;
545
3268e90f 546#ifdef HAVE_DHCP6
52b92f4d 547 again:
3268e90f 548#endif
52b92f4d 549 for (; dhcp_tmp; dhcp_tmp = dhcp_tmp->next)
feba5c1d 550 {
52b92f4d
SK
551 void *start = &dhcp_tmp->start;
552 void *end = &dhcp_tmp->end;
553
554#ifdef HAVE_DHCP6
555 if (family == AF_INET6)
556 {
557 start = &dhcp_tmp->start6;
558 end = &dhcp_tmp->end6;
559 }
560#endif
561
0a852541 562 prettyprint_time(daemon->dhcp_buff2, dhcp_tmp->lease_time);
52b92f4d
SK
563 inet_ntop(family, start, daemon->dhcp_buff, 256);
564 inet_ntop(family, end, daemon->dhcp_buff3, 256);
7622fc06 565 my_syslog(MS_DHCP | LOG_INFO,
f2621c7f
SK
566 (dhcp_tmp->flags & CONTEXT_STATIC) ?
567 _("DHCP, static leases only on %.0s%s, lease time %s") :
801ca9a7
SK
568 (dhcp_tmp->flags & CONTEXT_RA_NAME) ?
569 _("router advertisement with DHCPv4-derived names on %.0s%s, lifetime %s") :
c5ad4e79
SK
570 (dhcp_tmp->flags & CONTEXT_RA_ONLY) ?
571 _("router advertisement only on %.0s%s, lifetime %s") :
7622fc06
SK
572 (dhcp_tmp->flags & CONTEXT_PROXY) ?
573 _("DHCP, proxy on subnet %.0s%s%.0s") :
f2621c7f 574 _("DHCP, IP range %s -- %s, lease time %s"),
52b92f4d
SK
575 daemon->dhcp_buff, daemon->dhcp_buff3, daemon->dhcp_buff2);
576 }
577
578#ifdef HAVE_DHCP6
579 if (family == AF_INET)
580 {
581 family = AF_INET6;
c5ad4e79
SK
582 if (daemon->ra_contexts)
583 dhcp_tmp = daemon->ra_contexts;
584 else
585 dhcp_tmp = daemon->dhcp6;
52b92f4d 586 goto again;
feba5c1d 587 }
52b92f4d
SK
588#endif
589
26128d27 590 }
7622fc06 591#endif
26128d27 592
52b92f4d 593
832af0ba 594#ifdef HAVE_TFTP
8ef5ada2 595 if (daemon->tftp_unlimited || daemon->tftp_interfaces)
832af0ba 596 {
832af0ba 597#ifdef FD_SETSIZE
5aabfc78 598 if (FD_SETSIZE < (unsigned)max_fd)
832af0ba
SK
599 max_fd = FD_SETSIZE;
600#endif
601
7622fc06 602 my_syslog(MS_TFTP | LOG_INFO, "TFTP %s%s %s",
f2621c7f
SK
603 daemon->tftp_prefix ? _("root is ") : _("enabled"),
604 daemon->tftp_prefix ? daemon->tftp_prefix: "",
28866e95 605 option_bool(OPT_TFTP_SECURE) ? _("secure mode") : "");
f2621c7f 606
832af0ba 607 /* This is a guess, it assumes that for small limits,
f2621c7f 608 disjoint files might be served, but for large limits,
832af0ba
SK
609 a single file will be sent to may clients (the file only needs
610 one fd). */
611
612 max_fd -= 30; /* use other than TFTP */
613
614 if (max_fd < 0)
615 max_fd = 5;
616 else if (max_fd < 100)
617 max_fd = max_fd/2;
618 else
619 max_fd = max_fd - 20;
824af85b
SK
620
621 /* if we have to use a limited range of ports,
622 that will limit the number of transfers */
623 if (daemon->start_tftp_port != 0 &&
624 daemon->end_tftp_port - daemon->start_tftp_port + 1 < max_fd)
625 max_fd = daemon->end_tftp_port - daemon->start_tftp_port + 1;
832af0ba
SK
626
627 if (daemon->tftp_max > max_fd)
628 {
629 daemon->tftp_max = max_fd;
7622fc06 630 my_syslog(MS_TFTP | LOG_WARNING,
f2621c7f
SK
631 _("restricting maximum simultaneous TFTP transfers to %d"),
632 daemon->tftp_max);
832af0ba
SK
633 }
634 }
635#endif
636
1a6bca81
SK
637 /* finished start-up - release original process */
638 if (err_pipe[1] != -1)
639 close(err_pipe[1]);
9e4abcb5 640
824af85b
SK
641 if (daemon->port != 0)
642 check_servers();
643
7cebd20f
SK
644 pid = getpid();
645
5e9e0efb 646 while (1)
9e4abcb5 647 {
1697269c 648 int maxfd = -1;
5e9e0efb 649 struct timeval t, *tp = NULL;
3d8df260 650 fd_set rset, wset, eset;
9e4abcb5
SK
651
652 FD_ZERO(&rset);
3d8df260
SK
653 FD_ZERO(&wset);
654 FD_ZERO(&eset);
9e4abcb5 655
1697269c
SK
656 /* if we are out of resources, find how long we have to wait
657 for some to come free, we'll loop around then and restart
658 listening for queries */
5aabfc78 659 if ((t.tv_sec = set_dns_listeners(now, &rset, &maxfd)) != 0)
1697269c
SK
660 {
661 t.tv_usec = 0;
662 tp = &t;
663 }
664
832af0ba
SK
665 /* Whilst polling for the dbus, or doing a tftp transfer, wake every quarter second */
666 if (daemon->tftp_trans ||
28866e95 667 (option_bool(OPT_DBUS) && !daemon->dbus))
5e9e0efb 668 {
1697269c
SK
669 t.tv_sec = 0;
670 t.tv_usec = 250000;
5e9e0efb 671 tp = &t;
5e9e0efb 672 }
74c95c25
SK
673 /* Wake every second whilst waiting for DAD to complete */
674 else if (is_dad_listeners())
675 {
676 t.tv_sec = 1;
677 t.tv_usec = 0;
678 tp = &t;
679 }
44a2a316 680
832af0ba 681#ifdef HAVE_DBUS
5aabfc78 682 set_dbus_listeners(&maxfd, &rset, &wset, &eset);
5e9e0efb
SK
683#endif
684
7622fc06 685#ifdef HAVE_DHCP
5e9e0efb
SK
686 if (daemon->dhcp)
687 {
688 FD_SET(daemon->dhcpfd, &rset);
1697269c 689 bump_maxfd(daemon->dhcpfd, &maxfd);
316e2730
SK
690 if (daemon->pxefd != -1)
691 {
692 FD_SET(daemon->pxefd, &rset);
693 bump_maxfd(daemon->pxefd, &maxfd);
694 }
5e9e0efb 695 }
7622fc06 696#endif
cdeda28f 697
52b92f4d
SK
698#ifdef HAVE_DHCP6
699 if (daemon->dhcp6)
700 {
701 FD_SET(daemon->dhcp6fd, &rset);
c5ad4e79
SK
702 bump_maxfd(daemon->dhcp6fd, &maxfd);
703
843c96b4 704 if (daemon->ra_contexts)
c5ad4e79
SK
705 {
706 FD_SET(daemon->icmp6fd, &rset);
707 bump_maxfd(daemon->icmp6fd, &maxfd);
708 }
52b92f4d
SK
709 }
710#endif
711
5e9e0efb
SK
712#ifdef HAVE_LINUX_NETWORK
713 FD_SET(daemon->netlinkfd, &rset);
1697269c 714 bump_maxfd(daemon->netlinkfd, &maxfd);
3d8df260 715#endif
3d8df260 716
5e9e0efb 717 FD_SET(piperead, &rset);
1697269c
SK
718 bump_maxfd(piperead, &maxfd);
719
7622fc06 720#ifdef HAVE_DHCP
1f15b81d 721# ifdef HAVE_SCRIPT
5aabfc78 722 while (helper_buf_empty() && do_script_run(now));
1697269c
SK
723
724 if (!helper_buf_empty())
725 {
726 FD_SET(daemon->helperfd, &wset);
727 bump_maxfd(daemon->helperfd, &maxfd);
728 }
7622fc06 729# else
5aabfc78
SK
730 /* need this for other side-effects */
731 while (do_script_run(now));
7622fc06 732# endif
5aabfc78 733#endif
7622fc06 734
f2621c7f
SK
735 /* must do this just before select(), when we know no
736 more calls to my_syslog() can occur */
737 set_log_writer(&wset, &maxfd);
738
5e9e0efb
SK
739 if (select(maxfd+1, &rset, &wset, &eset, tp) < 0)
740 {
741 /* otherwise undefined after error */
742 FD_ZERO(&rset); FD_ZERO(&wset); FD_ZERO(&eset);
743 }
744
745 now = dnsmasq_time();
9e4abcb5 746
f2621c7f 747 check_log_writer(&wset);
74c95c25
SK
748
749 /* Check the interfaces to see if any have exited DAD state
750 and if so, bind the address. */
751 if (is_dad_listeners())
752 {
753 enumerate_interfaces();
754 /* NB, is_dad_listeners() == 1 --> we're binding interfaces */
755 create_bound_listeners(0);
756 }
f2621c7f 757
c52e1897
SK
758#ifdef HAVE_LINUX_NETWORK
759 if (FD_ISSET(daemon->netlinkfd, &rset))
760 netlink_multicast();
761#endif
762
9e4abcb5 763 /* Check for changes to resolv files once per second max. */
3d8df260 764 /* Don't go silent for long periods if the clock goes backwards. */
9009d746
SK
765 if (daemon->last_resolv == 0 ||
766 difftime(now, daemon->last_resolv) > 1.0 ||
767 difftime(now, daemon->last_resolv) < -1.0)
9e4abcb5 768 {
8ef5ada2
SK
769 /* poll_resolv doesn't need to reload first time through, since
770 that's queued anyway. */
33820b7e 771
8ef5ada2
SK
772 poll_resolv(0, daemon->last_resolv != 0, now);
773 daemon->last_resolv = now;
9e4abcb5 774 }
5aabfc78 775
5e9e0efb 776 if (FD_ISSET(piperead, &rset))
5aabfc78 777 async_event(piperead, now);
7cebd20f 778
3d8df260
SK
779#ifdef HAVE_DBUS
780 /* if we didn't create a DBus connection, retry now. */
28866e95 781 if (option_bool(OPT_DBUS) && !daemon->dbus)
3d8df260
SK
782 {
783 char *err;
5aabfc78 784 if ((err = dbus_init()))
f2621c7f 785 my_syslog(LOG_WARNING, _("DBus error: %s"), err);
3d8df260 786 if (daemon->dbus)
f2621c7f 787 my_syslog(LOG_INFO, _("connected to system DBus"));
3d8df260 788 }
5aabfc78 789 check_dbus_listeners(&rset, &wset, &eset);
3d8df260 790#endif
824af85b 791
5aabfc78 792 check_dns_listeners(&rset, now);
832af0ba
SK
793
794#ifdef HAVE_TFTP
5aabfc78 795 check_tftp_listeners(&rset, now);
832af0ba
SK
796#endif
797
7622fc06 798#ifdef HAVE_DHCP
316e2730
SK
799 if (daemon->dhcp)
800 {
801 if (FD_ISSET(daemon->dhcpfd, &rset))
802 dhcp_packet(now, 0);
803 if (daemon->pxefd != -1 && FD_ISSET(daemon->pxefd, &rset))
804 dhcp_packet(now, 1);
805 }
1697269c 806
52b92f4d
SK
807#ifdef HAVE_DHCP6
808 if (daemon->dhcp6)
809 {
810 if (FD_ISSET(daemon->dhcp6fd, &rset))
811 dhcp6_packet(now);
c5ad4e79 812
843c96b4 813 if (daemon->ra_contexts && FD_ISSET(daemon->icmp6fd, &rset))
c5ad4e79 814 icmp6_packet();
52b92f4d
SK
815 }
816#endif
817
1f15b81d 818# ifdef HAVE_SCRIPT
1697269c 819 if (daemon->helperfd != -1 && FD_ISSET(daemon->helperfd, &wset))
5aabfc78 820 helper_write();
7622fc06 821# endif
5aabfc78
SK
822#endif
823
9e4abcb5 824 }
9e4abcb5
SK
825}
826
3be34541
SK
827static void sig_handler(int sig)
828{
5e9e0efb 829 if (pid == 0)
3be34541 830 {
1697269c
SK
831 /* ignore anything other than TERM during startup
832 and in helper proc. (helper ignore TERM too) */
5e9e0efb 833 if (sig == SIGTERM)
5aabfc78 834 exit(EC_MISC);
3be34541 835 }
5aabfc78 836 else if (pid != getpid())
5e9e0efb 837 {
1697269c 838 /* alarm is used to kill TCP children after a fixed time. */
5e9e0efb 839 if (sig == SIGALRM)
7cebd20f 840 _exit(0);
3be34541 841 }
5aabfc78
SK
842 else
843 {
844 /* master process */
845 int event, errsave = errno;
846
847 if (sig == SIGHUP)
848 event = EVENT_RELOAD;
849 else if (sig == SIGCHLD)
850 event = EVENT_CHILD;
851 else if (sig == SIGALRM)
852 event = EVENT_ALARM;
853 else if (sig == SIGTERM)
854 event = EVENT_TERM;
855 else if (sig == SIGUSR1)
856 event = EVENT_DUMP;
857 else if (sig == SIGUSR2)
858 event = EVENT_REOPEN;
859 else
860 return;
861
c72daea8 862 send_event(pipewrite, event, 0, NULL);
5aabfc78
SK
863 errno = errsave;
864 }
865}
866
741c2952
SK
867void send_alarm(void)
868{
869 send_event(pipewrite, EVENT_ALARM, 0, NULL);
870}
871
c72daea8 872void send_event(int fd, int event, int data, char *msg)
5aabfc78
SK
873{
874 struct event_desc ev;
c72daea8
SK
875 struct iovec iov[2];
876
5aabfc78
SK
877 ev.event = event;
878 ev.data = data;
c72daea8
SK
879 ev.msg_sz = msg ? strlen(msg) : 0;
880
881 iov[0].iov_base = &ev;
882 iov[0].iov_len = sizeof(ev);
883 iov[1].iov_base = msg;
884 iov[1].iov_len = ev.msg_sz;
1a6bca81
SK
885
886 /* error pipe, debug mode. */
887 if (fd == -1)
c72daea8 888 fatal_event(&ev, msg);
1a6bca81
SK
889 else
890 /* pipe is non-blocking and struct event_desc is smaller than
891 PIPE_BUF, so this either fails or writes everything */
c72daea8 892 while (writev(fd, iov, msg ? 2 : 1) == -1 && errno == EINTR);
5aabfc78
SK
893}
894
c72daea8
SK
895/* NOTE: the memory used to return msg is leaked: use msgs in events only
896 to describe fatal errors. */
897static int read_event(int fd, struct event_desc *evp, char **msg)
898{
899 char *buf;
900
901 if (!read_write(fd, (unsigned char *)evp, sizeof(struct event_desc), 1))
902 return 0;
903
904 *msg = NULL;
905
906 if (evp->msg_sz != 0 &&
907 (buf = malloc(evp->msg_sz + 1)) &&
908 read_write(fd, (unsigned char *)buf, evp->msg_sz, 1))
909 {
910 buf[evp->msg_sz] = 0;
911 *msg = buf;
912 }
913
914 return 1;
915}
916
917static void fatal_event(struct event_desc *ev, char *msg)
1a6bca81
SK
918{
919 errno = ev->data;
920
921 switch (ev->event)
922 {
923 case EVENT_DIE:
924 exit(0);
7622fc06
SK
925
926 case EVENT_FORK_ERR:
927 die(_("cannot fork into background: %s"), NULL, EC_MISC);
1a6bca81
SK
928
929 case EVENT_PIPE_ERR:
930 die(_("failed to create helper: %s"), NULL, EC_MISC);
931
932 case EVENT_CAP_ERR:
933 die(_("setting capabilities failed: %s"), NULL, EC_MISC);
934
935 case EVENT_USER_ERR:
c72daea8 936 die(_("failed to change user-id to %s: %s"), msg, EC_MISC);
1a6bca81
SK
937
938 case EVENT_GROUP_ERR:
c72daea8 939 die(_("failed to change group-id to %s: %s"), msg, EC_MISC);
1a6bca81
SK
940
941 case EVENT_PIDFILE:
c72daea8 942 die(_("failed to open pidfile %s: %s"), msg, EC_FILE);
1a6bca81
SK
943
944 case EVENT_LOG_ERR:
c72daea8
SK
945 die(_("cannot open log %s: %s"), msg, EC_FILE);
946
947 case EVENT_LUA_ERR:
948 die(_("failed to load Lua script: %s"), msg, EC_MISC);
1a6bca81
SK
949 }
950}
951
5aabfc78
SK
952static void async_event(int pipe, time_t now)
953{
954 pid_t p;
955 struct event_desc ev;
956 int i;
c72daea8
SK
957 char *msg;
958
959 /* NOTE: the memory used to return msg is leaked: use msgs in events only
960 to describe fatal errors. */
961
962 if (read_event(pipe, &ev, &msg))
5aabfc78
SK
963 switch (ev.event)
964 {
965 case EVENT_RELOAD:
966 clear_cache_and_reload(now);
28866e95 967 if (daemon->port != 0 && daemon->resolv_files && option_bool(OPT_NO_POLL))
5aabfc78
SK
968 {
969 reload_servers(daemon->resolv_files->name);
970 check_servers();
971 }
7622fc06 972#ifdef HAVE_DHCP
5aabfc78 973 rerun_scripts();
7622fc06 974#endif
5aabfc78
SK
975 break;
976
977 case EVENT_DUMP:
824af85b
SK
978 if (daemon->port != 0)
979 dump_cache(now);
5aabfc78
SK
980 break;
981
982 case EVENT_ALARM:
7622fc06 983#ifdef HAVE_DHCP
52b92f4d 984 if (daemon->dhcp || daemon->dhcp6)
5aabfc78
SK
985 {
986 lease_prune(NULL, now);
987 lease_update_file(now);
988 }
843c96b4
SK
989#ifdef HAVE_DHCP6
990 else if (daemon->ra_contexts)
991 {
992 /* Not doing DHCP, so no lease system, manage
993 alarms for ra only */
994 time_t next_event = periodic_ra(now);
995 if (next_event != 0)
996 alarm((unsigned)difftime(next_event, now));
997 }
998#endif
7622fc06 999#endif
5aabfc78
SK
1000 break;
1001
1002 case EVENT_CHILD:
1003 /* See Stevens 5.10 */
1004 while ((p = waitpid(-1, NULL, WNOHANG)) != 0)
1005 if (p == -1)
1006 {
1007 if (errno != EINTR)
1008 break;
1009 }
1010 else
1011 for (i = 0 ; i < MAX_PROCS; i++)
1012 if (daemon->tcp_pids[i] == p)
1013 daemon->tcp_pids[i] = 0;
1014 break;
1015
1016 case EVENT_KILLED:
c72daea8 1017 my_syslog(LOG_WARNING, _("script process killed by signal %d"), ev.data);
5aabfc78
SK
1018 break;
1019
1020 case EVENT_EXITED:
c72daea8 1021 my_syslog(LOG_WARNING, _("script process exited with status %d"), ev.data);
5aabfc78
SK
1022 break;
1023
1024 case EVENT_EXEC_ERR:
9e038946
SK
1025 my_syslog(LOG_ERR, _("failed to execute %s: %s"),
1026 daemon->lease_change_command, strerror(ev.data));
5aabfc78
SK
1027 break;
1028
1a6bca81 1029 /* necessary for fatal errors in helper */
c72daea8 1030 case EVENT_USER_ERR:
1a6bca81 1031 case EVENT_DIE:
c72daea8
SK
1032 case EVENT_LUA_ERR:
1033 fatal_event(&ev, msg);
9e038946
SK
1034 break;
1035
5aabfc78
SK
1036 case EVENT_REOPEN:
1037 /* Note: this may leave TCP-handling processes with the old file still open.
1038 Since any such process will die in CHILD_LIFETIME or probably much sooner,
1039 we leave them logging to the old file. */
1040 if (daemon->log_file != NULL)
1041 log_reopen(daemon->log_file);
1042 break;
1043
1044 case EVENT_TERM:
1045 /* Knock all our children on the head. */
1046 for (i = 0; i < MAX_PROCS; i++)
1047 if (daemon->tcp_pids[i] != 0)
1048 kill(daemon->tcp_pids[i], SIGALRM);
1049
c72daea8 1050#if defined(HAVE_SCRIPT)
5aabfc78
SK
1051 /* handle pending lease transitions */
1052 if (daemon->helperfd != -1)
1053 {
1054 /* block in writes until all done */
1055 if ((i = fcntl(daemon->helperfd, F_GETFL)) != -1)
1056 fcntl(daemon->helperfd, F_SETFL, i & ~O_NONBLOCK);
1057 do {
1058 helper_write();
1059 } while (!helper_buf_empty() || do_script_run(now));
1060 close(daemon->helperfd);
1061 }
1062#endif
1063
1064 if (daemon->lease_stream)
1065 fclose(daemon->lease_stream);
73a08a24
SK
1066
1067 if (daemon->runfile)
1068 unlink(daemon->runfile);
5aabfc78
SK
1069
1070 my_syslog(LOG_INFO, _("exiting on receipt of SIGTERM"));
1071 flush_log();
1072 exit(EC_GOOD);
1073 }
3be34541
SK
1074}
1075
8ef5ada2 1076void poll_resolv(int force, int do_reload, time_t now)
5aabfc78
SK
1077{
1078 struct resolvc *res, *latest;
1079 struct stat statbuf;
1080 time_t last_change = 0;
1081 /* There may be more than one possible file.
1082 Go through and find the one which changed _last_.
1083 Warn of any which can't be read. */
8ef5ada2 1084
28866e95 1085 if (daemon->port == 0 || option_bool(OPT_NO_POLL))
8ef5ada2
SK
1086 return;
1087
5aabfc78
SK
1088 for (latest = NULL, res = daemon->resolv_files; res; res = res->next)
1089 if (stat(res->name, &statbuf) == -1)
1090 {
8ef5ada2
SK
1091 if (force)
1092 {
1093 res->mtime = 0;
1094 continue;
1095 }
1096
5aabfc78
SK
1097 if (!res->logged)
1098 my_syslog(LOG_WARNING, _("failed to access %s: %s"), res->name, strerror(errno));
1099 res->logged = 1;
8ef5ada2
SK
1100
1101 if (res->mtime != 0)
1102 {
1103 /* existing file evaporated, force selection of the latest
1104 file even if its mtime hasn't changed since we last looked */
1105 poll_resolv(1, do_reload, now);
1106 return;
1107 }
5aabfc78
SK
1108 }
1109 else
1110 {
1111 res->logged = 0;
8ef5ada2
SK
1112 if (force || (statbuf.st_mtime != res->mtime))
1113 {
1114 res->mtime = statbuf.st_mtime;
5aabfc78
SK
1115 if (difftime(statbuf.st_mtime, last_change) > 0.0)
1116 {
1117 last_change = statbuf.st_mtime;
1118 latest = res;
1119 }
1120 }
1121 }
1122
1123 if (latest)
1124 {
1125 static int warned = 0;
1126 if (reload_servers(latest->name))
1127 {
1128 my_syslog(LOG_INFO, _("reading %s"), latest->name);
1129 warned = 0;
1130 check_servers();
28866e95 1131 if (option_bool(OPT_RELOAD) && do_reload)
8ef5ada2 1132 clear_cache_and_reload(now);
5aabfc78
SK
1133 }
1134 else
1135 {
1136 latest->mtime = 0;
1137 if (!warned)
1138 {
1139 my_syslog(LOG_WARNING, _("no servers found in %s, will retry"), latest->name);
1140 warned = 1;
1141 }
1142 }
1143 }
1144}
3d8df260 1145
5aabfc78 1146void clear_cache_and_reload(time_t now)
3d8df260 1147{
824af85b 1148 if (daemon->port != 0)
7622fc06 1149 cache_reload();
824af85b 1150
7622fc06 1151#ifdef HAVE_DHCP
52b92f4d 1152 if (daemon->dhcp || daemon->dhcp6)
3d8df260 1153 {
28866e95 1154 if (option_bool(OPT_ETHERS))
5aabfc78 1155 dhcp_read_ethers();
824af85b 1156 reread_dhcp();
3d8df260 1157 dhcp_update_configs(daemon->dhcp_conf);
824af85b 1158 check_dhcp_hosts(0);
5aabfc78
SK
1159 lease_update_from_configs();
1160 lease_update_file(now);
1161 lease_update_dns();
3d8df260 1162 }
843c96b4
SK
1163#ifdef HAVE_DHCP6
1164 else if (daemon->ra_contexts)
1165 {
1166 /* Not doing DHCP, so no lease system, manage
1167 alarms for ra only */
1168 time_t next_event = periodic_ra(now);
1169 if (next_event != 0)
1170 alarm((unsigned)difftime(next_event, now));
1171 }
1172#endif
7622fc06 1173#endif
3d8df260
SK
1174}
1175
5aabfc78 1176static int set_dns_listeners(time_t now, fd_set *set, int *maxfdp)
3be34541
SK
1177{
1178 struct serverfd *serverfdp;
1179 struct listener *listener;
824af85b 1180 int wait = 0, i;
832af0ba
SK
1181
1182#ifdef HAVE_TFTP
1183 int tftp = 0;
1184 struct tftp_transfer *transfer;
1185 for (transfer = daemon->tftp_trans; transfer; transfer = transfer->next)
1186 {
1187 tftp++;
1188 FD_SET(transfer->sockfd, set);
1189 bump_maxfd(transfer->sockfd, maxfdp);
1190 }
1191#endif
1192
1697269c 1193 /* will we be able to get memory? */
824af85b
SK
1194 if (daemon->port != 0)
1195 get_new_frec(now, &wait);
1697269c 1196
3be34541
SK
1197 for (serverfdp = daemon->sfds; serverfdp; serverfdp = serverfdp->next)
1198 {
1199 FD_SET(serverfdp->fd, set);
1697269c 1200 bump_maxfd(serverfdp->fd, maxfdp);
3be34541 1201 }
1a6bca81
SK
1202
1203 if (daemon->port != 0 && !daemon->osport)
1204 for (i = 0; i < RANDOM_SOCKS; i++)
1205 if (daemon->randomsocks[i].refcount != 0)
1206 {
1207 FD_SET(daemon->randomsocks[i].fd, set);
1208 bump_maxfd(daemon->randomsocks[i].fd, maxfdp);
1209 }
1210
3be34541
SK
1211 for (listener = daemon->listeners; listener; listener = listener->next)
1212 {
1697269c 1213 /* only listen for queries if we have resources */
824af85b 1214 if (listener->fd != -1 && wait == 0)
1697269c
SK
1215 {
1216 FD_SET(listener->fd, set);
1217 bump_maxfd(listener->fd, maxfdp);
1218 }
1219
1220 /* death of a child goes through the select loop, so
1221 we don't need to explicitly arrange to wake up here */
824af85b
SK
1222 if (listener->tcpfd != -1)
1223 for (i = 0; i < MAX_PROCS; i++)
1224 if (daemon->tcp_pids[i] == 0)
1225 {
1226 FD_SET(listener->tcpfd, set);
1227 bump_maxfd(listener->tcpfd, maxfdp);
1228 break;
1229 }
9e4abcb5 1230
832af0ba
SK
1231#ifdef HAVE_TFTP
1232 if (tftp <= daemon->tftp_max && listener->tftpfd != -1)
1233 {
1234 FD_SET(listener->tftpfd, set);
1235 bump_maxfd(listener->tftpfd, maxfdp);
1236 }
1237#endif
1238
1239 }
1240
1697269c 1241 return wait;
3be34541 1242}
9e4abcb5 1243
5aabfc78 1244static void check_dns_listeners(fd_set *set, time_t now)
3be34541
SK
1245{
1246 struct serverfd *serverfdp;
1a6bca81
SK
1247 struct listener *listener;
1248 int i;
1249
832af0ba
SK
1250 for (serverfdp = daemon->sfds; serverfdp; serverfdp = serverfdp->next)
1251 if (FD_ISSET(serverfdp->fd, set))
1a6bca81
SK
1252 reply_query(serverfdp->fd, serverfdp->source_addr.sa.sa_family, now);
1253
1254 if (daemon->port != 0 && !daemon->osport)
1255 for (i = 0; i < RANDOM_SOCKS; i++)
1256 if (daemon->randomsocks[i].refcount != 0 &&
1257 FD_ISSET(daemon->randomsocks[i].fd, set))
1258 reply_query(daemon->randomsocks[i].fd, daemon->randomsocks[i].family, now);
832af0ba
SK
1259
1260 for (listener = daemon->listeners; listener; listener = listener->next)
1261 {
824af85b 1262 if (listener->fd != -1 && FD_ISSET(listener->fd, set))
5aabfc78 1263 receive_query(listener, now);
1a6bca81 1264
832af0ba
SK
1265#ifdef HAVE_TFTP
1266 if (listener->tftpfd != -1 && FD_ISSET(listener->tftpfd, set))
5aabfc78 1267 tftp_request(listener, now);
832af0ba 1268#endif
3be34541 1269
824af85b 1270 if (listener->tcpfd != -1 && FD_ISSET(listener->tcpfd, set))
832af0ba
SK
1271 {
1272 int confd;
1273 struct irec *iface = NULL;
1274 pid_t p;
1275
1276 while((confd = accept(listener->tcpfd, NULL, NULL)) == -1 && errno == EINTR);
1277
1278 if (confd == -1)
1279 continue;
1280
28866e95 1281 if (option_bool(OPT_NOWILD))
832af0ba
SK
1282 iface = listener->iface;
1283 else
1284 {
1285 union mysockaddr tcp_addr;
1286 socklen_t tcp_len = sizeof(union mysockaddr);
1287 /* Check for allowed interfaces when binding the wildcard address:
1288 we do this by looking for an interface with the same address as
1289 the local address of the TCP connection, then looking to see if that's
1290 an allowed interface. As a side effect, we get the netmask of the
1291 interface too, for localisation. */
3be34541 1292
832af0ba 1293 /* interface may be new since startup */
5aabfc78 1294 if (enumerate_interfaces() &&
832af0ba
SK
1295 getsockname(confd, (struct sockaddr *)&tcp_addr, &tcp_len) != -1)
1296 for (iface = daemon->interfaces; iface; iface = iface->next)
1297 if (sockaddr_isequal(&iface->addr, &tcp_addr))
1298 break;
1299 }
1300
1301 if (!iface)
1302 {
1303 shutdown(confd, SHUT_RDWR);
1304 close(confd);
1305 }
59353a6b 1306#ifndef NO_FORK
28866e95 1307 else if (!option_bool(OPT_DEBUG) && (p = fork()) != 0)
832af0ba
SK
1308 {
1309 if (p != -1)
1310 {
1311 int i;
1312 for (i = 0; i < MAX_PROCS; i++)
1313 if (daemon->tcp_pids[i] == 0)
1314 {
1315 daemon->tcp_pids[i] = p;
1316 break;
1317 }
1318 }
1319 close(confd);
1320 }
1321#endif
1322 else
1323 {
1324 unsigned char *buff;
1325 struct server *s;
1326 int flags;
832af0ba 1327
8ef5ada2
SK
1328#ifndef NO_FORK
1329 /* Arrange for SIGALARM after CHILD_LIFETIME seconds to
1330 terminate the process. */
28866e95 1331 if (!option_bool(OPT_DEBUG))
832af0ba 1332 alarm(CHILD_LIFETIME);
8ef5ada2
SK
1333#endif
1334
832af0ba
SK
1335 /* start with no upstream connections. */
1336 for (s = daemon->servers; s; s = s->next)
7cebd20f 1337 s->tcpfd = -1;
832af0ba
SK
1338
1339 /* The connected socket inherits non-blocking
1340 attribute from the listening socket.
1341 Reset that here. */
1342 if ((flags = fcntl(confd, F_GETFL, 0)) != -1)
1343 fcntl(confd, F_SETFL, flags & ~O_NONBLOCK);
1344
7de060b0 1345 buff = tcp_request(confd, now, &iface->addr, iface->netmask);
7cebd20f 1346
832af0ba
SK
1347 shutdown(confd, SHUT_RDWR);
1348 close(confd);
1349
1350 if (buff)
1351 free(buff);
1352
1353 for (s = daemon->servers; s; s = s->next)
1354 if (s->tcpfd != -1)
1355 {
1356 shutdown(s->tcpfd, SHUT_RDWR);
1357 close(s->tcpfd);
1358 }
7cebd20f 1359#ifndef NO_FORK
28866e95 1360 if (!option_bool(OPT_DEBUG))
5aabfc78
SK
1361 {
1362 flush_log();
1363 _exit(0);
1364 }
59353a6b 1365#endif
832af0ba
SK
1366 }
1367 }
1368 }
3be34541
SK
1369}
1370
7622fc06 1371#ifdef HAVE_DHCP
5e9e0efb
SK
1372int make_icmp_sock(void)
1373{
7cebd20f 1374 int fd;
5e9e0efb
SK
1375 int zeroopt = 0;
1376
1377 if ((fd = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP)) != -1)
1378 {
7cebd20f 1379 if (!fix_fd(fd) ||
5e9e0efb
SK
1380 setsockopt(fd, SOL_SOCKET, SO_DONTROUTE, &zeroopt, sizeof(zeroopt)) == -1)
1381 {
1382 close(fd);
1383 fd = -1;
1384 }
1385 }
1386
1387 return fd;
1388}
1389
5aabfc78 1390int icmp_ping(struct in_addr addr)
3be34541 1391{
5e9e0efb 1392 /* Try and get an ICMP echo from a machine. */
3be34541
SK
1393
1394 /* Note that whilst in the three second wait, we check for
832af0ba 1395 (and service) events on the DNS and TFTP sockets, (so doing that
3be34541
SK
1396 better not use any resources our caller has in use...)
1397 but we remain deaf to signals or further DHCP packets. */
1398
5e9e0efb 1399 int fd;
3be34541
SK
1400 struct sockaddr_in saddr;
1401 struct {
1402 struct ip ip;
1403 struct icmp icmp;
1404 } packet;
1405 unsigned short id = rand16();
1406 unsigned int i, j;
5e9e0efb 1407 int gotreply = 0;
3be34541 1408 time_t start, now;
5e9e0efb 1409
824af85b 1410#if defined(HAVE_LINUX_NETWORK) || defined (HAVE_SOLARIS_NETWORK)
5e9e0efb
SK
1411 if ((fd = make_icmp_sock()) == -1)
1412 return 0;
1413#else
1414 int opt = 2000;
1415 fd = daemon->dhcp_icmp_fd;
1416 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &opt, sizeof(opt));
1417#endif
1418
3be34541
SK
1419 saddr.sin_family = AF_INET;
1420 saddr.sin_port = 0;
1421 saddr.sin_addr = addr;
1422#ifdef HAVE_SOCKADDR_SA_LEN
1423 saddr.sin_len = sizeof(struct sockaddr_in);
1424#endif
1425
1426 memset(&packet.icmp, 0, sizeof(packet.icmp));
1427 packet.icmp.icmp_type = ICMP_ECHO;
1428 packet.icmp.icmp_id = id;
1429 for (j = 0, i = 0; i < sizeof(struct icmp) / 2; i++)
1430 j += ((u16 *)&packet.icmp)[i];
1431 while (j>>16)
1432 j = (j & 0xffff) + (j >> 16);
1433 packet.icmp.icmp_cksum = (j == 0xffff) ? j : ~j;
1434
5e9e0efb 1435 while (sendto(fd, (char *)&packet.icmp, sizeof(struct icmp), 0,
fd9fa481
SK
1436 (struct sockaddr *)&saddr, sizeof(saddr)) == -1 &&
1437 retry_send());
1438
5e9e0efb
SK
1439 for (now = start = dnsmasq_time();
1440 difftime(now, start) < (float)PING_WAIT;)
fd9fa481
SK
1441 {
1442 struct timeval tv;
f2621c7f 1443 fd_set rset, wset;
fd9fa481 1444 struct sockaddr_in faddr;
1697269c 1445 int maxfd = fd;
3d8df260 1446 socklen_t len = sizeof(faddr);
fd9fa481
SK
1447
1448 tv.tv_usec = 250000;
1449 tv.tv_sec = 0;
1450
1451 FD_ZERO(&rset);
f2621c7f 1452 FD_ZERO(&wset);
5e9e0efb 1453 FD_SET(fd, &rset);
5aabfc78 1454 set_dns_listeners(now, &rset, &maxfd);
f2621c7f 1455 set_log_writer(&wset, &maxfd);
c5ad4e79
SK
1456
1457#ifdef HAVE_DHCP6
843c96b4 1458 if (daemon->ra_contexts)
c5ad4e79
SK
1459 {
1460 FD_SET(daemon->icmp6fd, &rset);
1461 bump_maxfd(daemon->icmp6fd, &maxfd);
1462 }
1463#endif
1464
f2621c7f
SK
1465 if (select(maxfd+1, &rset, &wset, NULL, &tv) < 0)
1466 {
1467 FD_ZERO(&rset);
1468 FD_ZERO(&wset);
1469 }
1470
5e9e0efb 1471 now = dnsmasq_time();
f2621c7f
SK
1472
1473 check_log_writer(&wset);
5aabfc78 1474 check_dns_listeners(&rset, now);
832af0ba 1475
c5ad4e79 1476#ifdef HAVE_DHCP6
843c96b4 1477 if (daemon->ra_contexts && FD_ISSET(daemon->icmp6fd, &rset))
c5ad4e79
SK
1478 icmp6_packet();
1479#endif
1480
832af0ba 1481#ifdef HAVE_TFTP
5aabfc78 1482 check_tftp_listeners(&rset, now);
832af0ba
SK
1483#endif
1484
5e9e0efb
SK
1485 if (FD_ISSET(fd, &rset) &&
1486 recvfrom(fd, &packet, sizeof(packet), 0,
fd9fa481
SK
1487 (struct sockaddr *)&faddr, &len) == sizeof(packet) &&
1488 saddr.sin_addr.s_addr == faddr.sin_addr.s_addr &&
1489 packet.icmp.icmp_type == ICMP_ECHOREPLY &&
1490 packet.icmp.icmp_seq == 0 &&
1491 packet.icmp.icmp_id == id)
1492 {
1493 gotreply = 1;
1494 break;
1495 }
1496 }
1497
824af85b 1498#if defined(HAVE_LINUX_NETWORK) || defined(HAVE_SOLARIS_NETWORK)
5e9e0efb
SK
1499 close(fd);
1500#else
3be34541 1501 opt = 1;
5e9e0efb
SK
1502 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &opt, sizeof(opt));
1503#endif
1504
3be34541
SK
1505 return gotreply;
1506}
7622fc06 1507#endif
0a852541
SK
1508
1509