1 /* dnsmasq is Copyright (c) 2000-2009 Simon Kelley
2 
3    This program is free software; you can redistribute it and/or modify
4    it under the terms of the GNU General Public License as published by
5    the Free Software Foundation; version 2 dated June, 1991, or
6    (at your option) version 3 dated 29 June, 2007.
7 
8    This program is distributed in the hope that it will be useful,
9    but WITHOUT ANY WARRANTY; without even the implied warranty of
10    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11    GNU General Public License for more details.
12 
13    You should have received a copy of the GNU General Public License
14    along with this program.  If not, see <http://www.gnu.org/licenses/>.
15 */
16 
17 #define VERSION "2.51"
18 
19 #define FTABSIZ 150 /* max number of outstanding requests (default) */
20 #define MAX_PROCS 20 /* max no children for TCP requests */
21 #define CHILD_LIFETIME 150 /* secs 'till terminated (RFC1035 suggests > 120s) */
22 #define EDNS_PKTSZ 1280 /* default max EDNS.0 UDP packet from RFC2671 */
23 #define TIMEOUT 10 /* drop UDP queries after TIMEOUT seconds */
24 #define FORWARD_TEST 50 /* try all servers every 50 queries */
25 #define FORWARD_TIME 10 /* or 10 seconds */
26 #define RANDOM_SOCKS 64 /* max simultaneous random ports */
27 #define LEASE_RETRY 60 /* on error, retry writing leasefile after LEASE_RETRY seconds */
28 #define CACHESIZ 150 /* default cache size */
29 #define MAXLEASES 150 /* maximum number of DHCP leases */
30 #define PING_WAIT 3 /* wait for ping address-in-use test */
31 #define PING_CACHE_TIME 30 /* Ping test assumed to be valid this long. */
32 #define DECLINE_BACKOFF 600 /* disable DECLINEd static addresses for this long */
33 #define DHCP_PACKET_MAX 16384 /* hard limit on DHCP packet size */
34 #define SMALLDNAME 40 /* most domain names are smaller than this */
35 #define HOSTSFILE "/etc/hosts"
36 #define ETHERSFILE "/etc/ethers"
37 #ifdef __uClinux__
38 #  define RESOLVFILE "/etc/config/resolv.conf"
39 #else
40 #  define RESOLVFILE "/etc/resolv.conf"
41 #endif
42 #define RUNFILE "/var/run/dnsmasq.pid"
43 
44 #ifndef LEASEFILE
45 #   if defined(__FreeBSD__) || defined (__OpenBSD__) || defined(__DragonFly__) || defined(__NetBSD__)
46 #      define LEASEFILE "/var/db/dnsmasq.leases"
47 #   elif defined(__sun__) || defined (__sun)
48 #      define LEASEFILE "/var/cache/dnsmasq.leases"
49 #   elif defined(__ANDROID__)
50 #      define LEASEFILE "/data/misc/dhcp/dnsmasq.leases"
51 #   else
52 #      define LEASEFILE "/var/lib/misc/dnsmasq.leases"
53 #   endif
54 #endif
55 
56 #ifndef CONFFILE
57 #   if defined(__FreeBSD__)
58 #      define CONFFILE "/usr/local/etc/dnsmasq.conf"
59 #   else
60 #      define CONFFILE "/etc/dnsmasq.conf"
61 #   endif
62 #endif
63 
64 #define DEFLEASE 3600 /* default lease time, 1 hour */
65 #define CHUSER "nobody"
66 #define CHGRP "dip"
67 #define DHCP_SERVER_PORT 67
68 #define DHCP_CLIENT_PORT 68
69 #define DHCP_SERVER_ALTPORT 1067
70 #define DHCP_CLIENT_ALTPORT 1068
71 #define TFTP_PORT 69
72 #define TFTP_MAX_CONNECTIONS 50 /* max simultaneous connections */
73 #define LOG_MAX 5 /* log-queue length */
74 #define RANDFILE "/dev/urandom"
75 #define DAD_WAIT 20 /* retry binding IPv6 sockets for this long */
76 
77 /* DBUS interface specifics */
78 #define DNSMASQ_SERVICE "uk.org.thekelleys.dnsmasq"
79 #define DNSMASQ_PATH "/uk/org/thekelleys/dnsmasq"
80 
81 /* A small collection of RR-types which are missing on some platforms */
82 
83 #ifndef T_SIG
84 #  define T_SIG 24
85 #endif
86 
87 #ifndef T_SRV
88 #  define T_SRV 33
89 #endif
90 
91 #ifndef T_OPT
92 #  define T_OPT 41
93 #endif
94 
95 #ifndef T_TKEY
96 #  define T_TKEY 249
97 #endif
98 
99 #ifndef T_TSIG
100 #  define T_TSIG 250
101 #endif
102 
103 
104 /* Follows system specific switches. If you run on a
105    new system, you may want to edit these.
106    May replace this with Autoconf one day.
107 
108 HAVE_LINUX_NETWORK
109 HAVE_BSD_NETWORK
110 HAVE_SOLARIS_NETWORK
111    define exactly one of these to alter interaction with kernel networking.
112 
113 HAVE_BROKEN_RTC
114    define this on embedded systems which don't have an RTC
115    which keeps time over reboots. Causes dnsmasq to use uptime
116    for timing, and keep lease lengths rather than expiry times
117    in its leases file. This also make dnsmasq "flash disk friendly".
118    Normally, dnsmasq tries very hard to keep the on-disk leases file
119    up-to-date: rewriting it after every renewal.  When HAVE_BROKEN_RTC
120    is in effect, the lease file is only written when a new lease is
121    created, or an old one destroyed. (Because those are the only times
122    it changes.) This vastly reduces the number of file writes, and makes
123    it viable to keep the lease file on a flash filesystem.
124    NOTE: when enabling or disabling this, be sure to delete any old
125    leases file, otherwise dnsmasq may get very confused.
126 
127 HAVE_TFTP
128    define this to get dnsmasq's built-in TFTP server.
129 
130 HAVE_DHCP
131    define this to get dnsmasq's DHCP server.
132 
133 HAVE_SCRIPT
134    define this to get the ability to call scripts on lease-change
135 
136 HAVE_GETOPT_LONG
137    define this if you have GNU libc or GNU getopt.
138 
139 HAVE_ARC4RANDOM
140    define this if you have arc4random() to get better security from DNS spoofs
141    by using really random ids (OpenBSD)
142 
143 HAVE_SOCKADDR_SA_LEN
144    define this if struct sockaddr has sa_len field (*BSD)
145 
146 HAVE_DBUS
147    Define this if you want to link against libdbus, and have dnsmasq
148    define some methods to allow (re)configuration of the upstream DNS
149    servers via DBus.
150 
151 NOTES:
152    For Linux you should define
153       HAVE_LINUX_NETWORK
154       HAVE_GETOPT_LONG
155   you should NOT define
156       HAVE_ARC4RANDOM
157       HAVE_SOCKADDR_SA_LEN
158 
159    For *BSD systems you should define
160      HAVE_BSD_NETWORK
161      HAVE_SOCKADDR_SA_LEN
162    and you MAY define
163      HAVE_ARC4RANDOM - OpenBSD and FreeBSD and NetBSD version 2.0 or later
164      HAVE_GETOPT_LONG - NetBSD, later FreeBSD
165                        (FreeBSD and OpenBSD only if you link GNU getopt)
166 
167 */
168 
169 /* platform independent options- uncomment to enable */
170 #define HAVE_DHCP
171 #define HAVE_TFTP
172 #define HAVE_SCRIPT
173 /* #define HAVE_BROKEN_RTC */
174 /* #define HAVE_DBUS */
175 
176 /* Allow TFTP to be disabled with COPTS=-DNO_TFTP */
177 #ifdef NO_TFTP
178 #undef HAVE_TFTP
179 #endif
180 
181 /* Allow DHCP to be disabled with COPTS=-DNO_DHCP */
182 #ifdef NO_DHCP
183 #undef HAVE_DHCP
184 #endif
185 
186 /* Allow scripts to be disabled with COPTS=-DNO_SCRIPT */
187 #ifdef NO_SCRIPT
188 #undef HAVE_SCRIPT
189 #endif
190 
191 
192 
193 /* platform dependent options. */
194 
195 /* Must preceed __linux__ since uClinux defines __linux__ too. */
196 #if defined(__uClinux__)
197 #define HAVE_LINUX_NETWORK
198 #define HAVE_GETOPT_LONG
199 #undef HAVE_ARC4RANDOM
200 #undef HAVE_SOCKADDR_SA_LEN
201 /* Never use fork() on uClinux. Note that this is subtly different from the
202    --keep-in-foreground option, since it also  suppresses forking new
203    processes for TCP connections and disables the call-a-script on leasechange
204    system. It's intended for use on MMU-less kernels. */
205 #define NO_FORK
206 
207 #elif defined(__UCLIBC__)
208 #define HAVE_LINUX_NETWORK
209 #if defined(__UCLIBC_HAS_GNU_GETOPT__) || \
210    ((__UCLIBC_MAJOR__==0) && (__UCLIBC_MINOR__==9) && (__UCLIBC_SUBLEVEL__<21))
211 #    define HAVE_GETOPT_LONG
212 #endif
213 #undef HAVE_ARC4RANDOM
214 #undef HAVE_SOCKADDR_SA_LEN
215 #if !defined(__ARCH_HAS_MMU__) && !defined(__UCLIBC_HAS_MMU__)
216 #  define NO_FORK
217 #endif
218 #if defined(__UCLIBC_HAS_IPV6__)
219 #  ifndef IPV6_V6ONLY
220 #    define IPV6_V6ONLY 26
221 #  endif
222 #endif
223 
224 /* This is for glibc 2.x */
225 #elif defined(__linux__)
226 #define HAVE_LINUX_NETWORK
227 #define HAVE_GETOPT_LONG
228 #undef HAVE_ARC4RANDOM
229 #undef HAVE_SOCKADDR_SA_LEN
230 
231 #elif defined(__FreeBSD__) || \
232       defined(__OpenBSD__) || \
233       defined(__DragonFly__) || \
234       defined (__FreeBSD_kernel__)
235 #define HAVE_BSD_NETWORK
236 /* Later verions of FreeBSD have getopt_long() */
237 #if defined(optional_argument) && defined(required_argument)
238 #   define HAVE_GETOPT_LONG
239 #endif
240 #if !defined (__FreeBSD_kernel__)
241 #   define HAVE_ARC4RANDOM
242 #endif
243 #define HAVE_SOCKADDR_SA_LEN
244 
245 #elif defined(__APPLE__)
246 #define HAVE_BSD_NETWORK
247 #undef HAVE_GETOPT_LONG
248 #define HAVE_ARC4RANDOM
249 #define HAVE_SOCKADDR_SA_LEN
250 /* Define before sys/socket.h is included so we get socklen_t */
251 #define _BSD_SOCKLEN_T_
252 
253 #elif defined(__NetBSD__)
254 #define HAVE_BSD_NETWORK
255 #define HAVE_GETOPT_LONG
256 #undef HAVE_ARC4RANDOM
257 #define HAVE_SOCKADDR_SA_LEN
258 
259 #elif defined(__sun) || defined(__sun__)
260 #define HAVE_SOLARIS_NETWORK
261 #define HAVE_GETOPT_LONG
262 #undef HAVE_ARC4RANDOM
263 #undef HAVE_SOCKADDR_SA_LEN
264 #define _XPG4_2
265 #define __EXTENSIONS__
266 #define ETHER_ADDR_LEN 6
267 
268 #endif
269 
270 /* Decide if we're going to support IPv6 */
271 /* IPv6 can be forced off with "make COPTS=-DNO_IPV6" */
272 /* We assume that systems which don't have IPv6
273    headers don't have ntop and pton either */
274 
275 #if defined(INET6_ADDRSTRLEN) && defined(IPV6_V6ONLY) && !defined(NO_IPV6)
276 #  define HAVE_IPV6
277 #  define ADDRSTRLEN INET6_ADDRSTRLEN
278 #  if defined(SOL_IPV6)
279 #    define IPV6_LEVEL SOL_IPV6
280 #  else
281 #    define IPV6_LEVEL IPPROTO_IPV6
282 #  endif
283 #elif defined(INET_ADDRSTRLEN)
284 #  undef HAVE_IPV6
285 #  define ADDRSTRLEN INET_ADDRSTRLEN
286 #else
287 #  undef HAVE_IPV6
288 #  define ADDRSTRLEN 16 /* 4*3 + 3 dots + NULL */
289 #endif
290 
291 /* Can't do scripts without fork */
292 #ifdef NOFORK
293 #  undef HAVE_SCRIPT
294 #endif
295 
296