]>
Commit | Line | Data |
---|---|---|
9e4abcb5 SK |
1 | /* dnsmasq is Copyright (c) 2000-2003 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. | |
6 | ||
7 | This program is distributed in the hope that it will be useful, | |
8 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
10 | GNU General Public License for more details. | |
11 | */ | |
12 | ||
13 | /* Author's email: simon@thekelleys.org.uk */ | |
44a2a316 | 14 | |
9e4abcb5 | 15 | |
44a2a316 | 16 | #ifdef __linux__ |
9e4abcb5 | 17 | /* for pselect.... */ |
44a2a316 | 18 | #define _XOPEN_SOURCE 600 |
9e4abcb5 SK |
19 | /* but then DNS headers don't compile without.... */ |
20 | #define _BSD_SOURCE | |
44a2a316 SK |
21 | #endif |
22 | ||
9e4abcb5 | 23 | /* get these before config.h for IPv6 stuff... */ |
44a2a316 | 24 | #include <sys/types.h> |
9e4abcb5 SK |
25 | #include <netinet/in.h> |
26 | ||
27 | /* get this before config.h too. */ | |
28 | #include <syslog.h> | |
29 | ||
30 | #include "config.h" | |
44a2a316 | 31 | |
9e4abcb5 SK |
32 | #include <arpa/nameser.h> |
33 | #include <arpa/inet.h> | |
34 | #include <sys/stat.h> | |
35 | #include <sys/socket.h> | |
36 | #include <sys/stat.h> | |
37 | #include <sys/ioctl.h> | |
38 | #include <sys/select.h> | |
39 | #if defined(__sun) || defined(__sun__) | |
44a2a316 | 40 | # include <sys/sockio.h> |
9e4abcb5 SK |
41 | #endif |
42 | #include <sys/time.h> | |
8a911ccc | 43 | #include <limits.h> |
9e4abcb5 SK |
44 | #include <net/if.h> |
45 | #include <unistd.h> | |
46 | #include <stdio.h> | |
47 | #include <string.h> | |
48 | #include <stdlib.h> | |
49 | #include <fcntl.h> | |
50 | #include <ctype.h> | |
51 | #include <signal.h> | |
52 | #ifdef HAVE_GETOPT_LONG | |
53 | # include <getopt.h> | |
54 | #endif | |
55 | #include <time.h> | |
56 | #include <errno.h> | |
57 | #include <pwd.h> | |
58 | #include <grp.h> | |
de37951c | 59 | #if defined(__OpenBSD__) || defined(__NetBSD__) |
44a2a316 SK |
60 | # include <netinet/if_ether.h> |
61 | #else | |
62 | # include <net/ethernet.h> | |
63 | #endif | |
9e4abcb5 SK |
64 | #include <net/if_arp.h> |
65 | #include <netinet/in_systm.h> | |
66 | #include <netinet/ip.h> | |
9e4abcb5 | 67 | #ifdef HAVE_BPF |
44a2a316 SK |
68 | # include <net/bpf.h> |
69 | # include <net/if_dl.h> | |
70 | #else | |
71 | # include <netpacket/packet.h> | |
9e4abcb5 SK |
72 | #endif |
73 | #include <sys/uio.h> | |
74 | ||
44a2a316 SK |
75 | /* Size: we check after adding each record, so there must be |
76 | memory for the largest packet, and the largest record */ | |
77 | #define DNSMASQ_PACKETSZ PACKETSZ+MAXDNAME+RRFIXEDSZ | |
78 | ||
9e4abcb5 SK |
79 | #define OPT_BOGUSPRIV 1 |
80 | #define OPT_FILTER 2 | |
81 | #define OPT_LOG 4 | |
82 | #define OPT_SELFMX 8 | |
83 | #define OPT_NO_HOSTS 16 | |
84 | #define OPT_NO_POLL 32 | |
85 | #define OPT_DEBUG 64 | |
86 | #define OPT_ORDER 128 | |
87 | #define OPT_NO_RESOLV 256 | |
88 | #define OPT_EXPAND 512 | |
89 | #define OPT_LOCALMX 1024 | |
90 | #define OPT_NO_NEG 2048 | |
91 | #define OPT_NODOTS_LOCAL 4096 | |
44a2a316 SK |
92 | #define OPT_NOWILD 8192 |
93 | #define OPT_ETHERS 16384 | |
de37951c | 94 | #define OPT_RESOLV_DOMAIN 32768 |
9e4abcb5 SK |
95 | |
96 | struct all_addr { | |
97 | union { | |
98 | struct in_addr addr4; | |
99 | #ifdef HAVE_IPV6 | |
100 | struct in6_addr addr6; | |
101 | #endif | |
102 | } addr; | |
103 | }; | |
104 | ||
105 | struct bogus_addr { | |
106 | struct in_addr addr; | |
107 | struct bogus_addr *next; | |
108 | }; | |
109 | ||
1cff166d SK |
110 | /* dns doctor param */ |
111 | struct doctor { | |
112 | struct in_addr in, out, mask; | |
113 | struct doctor *next; | |
114 | }; | |
115 | ||
de37951c SK |
116 | struct mx_record { |
117 | char *mxname, *mxtarget; | |
118 | struct mx_record *next; | |
119 | }; | |
120 | ||
9e4abcb5 SK |
121 | union bigname { |
122 | char name[MAXDNAME]; | |
123 | union bigname *next; /* freelist */ | |
124 | }; | |
125 | ||
126 | struct crec { | |
127 | struct crec *next, *prev, *hash_next; | |
128 | time_t ttd; /* time to die */ | |
129 | struct all_addr addr; | |
130 | unsigned short flags; | |
131 | union { | |
132 | char sname[SMALLDNAME]; | |
133 | union bigname *bname; | |
134 | char *namep; | |
135 | } name; | |
136 | }; | |
137 | ||
138 | #define F_IMMORTAL 1 | |
a84fa1d0 | 139 | #define F_CONFIG 2 |
9e4abcb5 SK |
140 | #define F_REVERSE 4 |
141 | #define F_FORWARD 8 | |
142 | #define F_DHCP 16 | |
143 | #define F_NEG 32 | |
144 | #define F_HOSTS 64 | |
145 | #define F_IPV4 128 | |
146 | #define F_IPV6 256 | |
147 | #define F_BIGNAME 512 | |
148 | #define F_UPSTREAM 1024 | |
149 | #define F_SERVER 2048 | |
150 | #define F_NXDOMAIN 4096 | |
151 | #define F_QUERY 8192 | |
152 | #define F_ADDN 16384 | |
153 | #define F_NOERR 32768 | |
154 | ||
155 | /* struct sockaddr is not large enough to hold any address, | |
156 | and specifically not big enough to hold and IPv6 address. | |
157 | Blech. Roll our own. */ | |
158 | union mysockaddr { | |
159 | struct sockaddr sa; | |
160 | struct sockaddr_in in; | |
161 | #ifdef HAVE_BROKEN_SOCKADDR_IN6 | |
162 | /* early versions of glibc don't include sin6_scope_id in sockaddr_in6 | |
163 | but latest kernels _require_ it to be set. The choice is to have | |
164 | dnsmasq fail to compile on back-level libc or fail to run | |
165 | on latest kernels with IPv6. Or to do this: sorry that it's so gross. */ | |
166 | struct my_sockaddr_in6 { | |
167 | sa_family_t sin6_family; /* AF_INET6 */ | |
168 | uint16_t sin6_port; /* transport layer port # */ | |
169 | uint32_t sin6_flowinfo; /* IPv6 traffic class & flow info */ | |
170 | struct in6_addr sin6_addr; /* IPv6 address */ | |
171 | uint32_t sin6_scope_id; /* set of interfaces for a scope */ | |
172 | } in6; | |
173 | #elif defined(HAVE_IPV6) | |
174 | struct sockaddr_in6 in6; | |
175 | #endif | |
176 | }; | |
177 | ||
178 | #define SERV_FROM_RESOLV 1 /* 1 for servers from resolv, 0 for command line. */ | |
179 | #define SERV_NO_ADDR 2 /* no server, this domain is local only */ | |
180 | #define SERV_LITERAL_ADDRESS 4 /* addr is the answer, not the server */ | |
181 | #define SERV_HAS_SOURCE 8 /* source address specified */ | |
182 | #define SERV_HAS_DOMAIN 16 /* server for one domain only */ | |
183 | #define SERV_FOR_NODOTS 32 /* server for names with no domain part only */ | |
184 | #define SERV_TYPE (SERV_HAS_DOMAIN | SERV_FOR_NODOTS) | |
185 | ||
186 | struct serverfd { | |
187 | int fd; | |
188 | union mysockaddr source_addr; | |
189 | struct serverfd *next; | |
190 | }; | |
191 | ||
192 | struct server { | |
193 | union mysockaddr addr, source_addr; | |
194 | struct serverfd *sfd; /* non-NULL if this server has its own fd bound to | |
195 | a source port */ | |
196 | char *domain; /* set if this server only handles a domain. */ | |
197 | int flags; | |
198 | struct server *next; | |
199 | }; | |
200 | ||
9e4abcb5 SK |
201 | struct irec { |
202 | union mysockaddr addr; | |
9e4abcb5 SK |
203 | struct irec *next; |
204 | }; | |
205 | ||
44a2a316 SK |
206 | struct listener { |
207 | int fd, family; | |
208 | struct listener *next; | |
209 | }; | |
210 | ||
9e4abcb5 SK |
211 | /* interface and address parms from command line. */ |
212 | struct iname { | |
213 | char *name; | |
214 | union mysockaddr addr; | |
de37951c | 215 | int isloop, used; |
9e4abcb5 SK |
216 | struct iname *next; |
217 | }; | |
218 | ||
219 | /* resolv-file parms from command-line */ | |
220 | struct resolvc { | |
221 | struct resolvc *next; | |
222 | int is_default; | |
223 | int logged; | |
224 | char *name; | |
225 | }; | |
226 | ||
227 | struct frec { | |
228 | union mysockaddr source; | |
44a2a316 | 229 | struct all_addr dest; |
9e4abcb5 SK |
230 | struct server *sentto; |
231 | unsigned short orig_id, new_id; | |
232 | int fd; | |
233 | time_t time; | |
234 | struct frec *next; | |
235 | }; | |
236 | ||
237 | struct dhcp_lease { | |
238 | int clid_len; /* length of client identifier */ | |
239 | unsigned char *clid; /* clientid */ | |
240 | char *hostname, *fqdn; /* name from client-hostname option or config */ | |
241 | time_t expires; /* lease expiry */ | |
242 | unsigned char hwaddr[ETHER_ADDR_LEN]; | |
243 | struct in_addr addr; | |
244 | struct dhcp_lease *next; | |
245 | }; | |
246 | ||
a222641c SK |
247 | struct dhcp_netid { |
248 | char *net; | |
249 | struct dhcp_netid *next; | |
250 | }; | |
251 | ||
9e4abcb5 | 252 | struct dhcp_config { |
33820b7e | 253 | unsigned int flags; |
9e4abcb5 SK |
254 | int clid_len; /* length of client identifier */ |
255 | unsigned char *clid; /* clientid */ | |
256 | unsigned char hwaddr[ETHER_ADDR_LEN]; | |
a222641c SK |
257 | char *hostname; |
258 | struct dhcp_netid netid; | |
9e4abcb5 SK |
259 | struct in_addr addr; |
260 | unsigned int lease_time; | |
261 | struct dhcp_config *next; | |
262 | }; | |
263 | ||
33820b7e SK |
264 | #define CONFIG_DISABLE 1 |
265 | #define CONFIG_CLID 2 | |
266 | #define CONFIG_HWADDR 4 | |
267 | #define CONFIG_TIME 8 | |
268 | #define CONFIG_NAME 16 | |
269 | #define CONFIG_ADDR 32 | |
270 | #define CONFIG_NETID 64 | |
a84fa1d0 | 271 | #define CONFIG_NOCLID 128 |
33820b7e | 272 | |
9e4abcb5 | 273 | struct dhcp_opt { |
1ab84e2f | 274 | int opt, len, is_addr; |
9e4abcb5 | 275 | unsigned char *val; |
44a2a316 | 276 | char *netid; |
9e4abcb5 | 277 | struct dhcp_opt *next; |
a84fa1d0 SK |
278 | }; |
279 | ||
280 | struct dhcp_vendor { | |
a222641c SK |
281 | int len, is_vendor, used; |
282 | char *data; | |
283 | struct dhcp_netid netid; | |
a84fa1d0 SK |
284 | struct dhcp_vendor *next; |
285 | }; | |
9e4abcb5 SK |
286 | |
287 | struct dhcp_context { | |
9e4abcb5 | 288 | unsigned int lease_time; |
44a2a316 | 289 | struct in_addr netmask, broadcast; |
a84fa1d0 | 290 | struct in_addr start, end; /* range of available addresses */ |
a222641c | 291 | struct dhcp_netid netid; |
9e4abcb5 SK |
292 | struct dhcp_context *next; |
293 | }; | |
294 | ||
295 | typedef unsigned char u8; | |
296 | typedef unsigned short u16; | |
297 | typedef unsigned int u32; | |
298 | ||
299 | ||
300 | struct udp_dhcp_packet { | |
301 | struct ip ip; | |
302 | struct udphdr { | |
303 | u16 uh_sport; /* source port */ | |
304 | u16 uh_dport; /* destination port */ | |
305 | u16 uh_ulen; /* udp length */ | |
306 | u16 uh_sum; /* udp checksum */ | |
307 | } udp; | |
308 | struct dhcp_packet { | |
309 | u8 op, htype, hlen, hops; | |
310 | u32 xid; | |
311 | u16 secs, flags; | |
312 | struct in_addr ciaddr, yiaddr, siaddr, giaddr; | |
313 | u8 chaddr[16], sname[64], file[128]; | |
314 | u32 cookie; | |
315 | u8 options[308]; | |
316 | } data; | |
317 | }; | |
318 | ||
319 | ||
320 | /* cache.c */ | |
321 | void cache_init(int cachesize, int log); | |
322 | void log_query(unsigned short flags, char *name, struct all_addr *addr); | |
323 | struct crec *cache_find_by_addr(struct crec *crecp, | |
324 | struct all_addr *addr, time_t now, | |
325 | unsigned short prot); | |
326 | struct crec *cache_find_by_name(struct crec *crecp, | |
327 | char *name, time_t now, unsigned short prot); | |
328 | void cache_end_insert(void); | |
329 | void cache_start_insert(void); | |
330 | void cache_insert(char *name, struct all_addr *addr, | |
331 | time_t now, unsigned long ttl, unsigned short flags); | |
332 | void cache_reload(int opts, char *buff, char *domain_suffix, char *addn_hosts); | |
333 | void cache_add_dhcp_entry(char *host_name, struct in_addr *host_address, | |
334 | time_t ttd, unsigned short flags); | |
335 | void cache_unhash_dhcp(void); | |
336 | void dump_cache(int debug, int size); | |
337 | char *cache_get_name(struct crec *crecp); | |
338 | ||
339 | /* rfc1035.c */ | |
340 | unsigned short extract_request(HEADER *header, unsigned int qlen, char *name); | |
341 | int setup_reply(HEADER *header, unsigned int qlen, | |
342 | struct all_addr *addrp, unsigned short flags, | |
343 | unsigned long local_ttl); | |
1cff166d SK |
344 | void extract_addresses(HEADER *header, unsigned int qlen, char *namebuff, |
345 | time_t now, struct doctor *doctors); | |
9e4abcb5 | 346 | void extract_neg_addrs(HEADER *header, unsigned int qlen, char *namebuff, time_t now); |
de37951c | 347 | int answer_request(HEADER *header, char *limit, unsigned int qlen, struct mx_record *mxnames, |
9e4abcb5 SK |
348 | char *mxtarget, unsigned int options, time_t now, unsigned long local_ttl, |
349 | char *namebuff); | |
350 | int check_for_bogus_wildcard(HEADER *header, unsigned int qlen, char *name, | |
351 | struct bogus_addr *addr, time_t now); | |
352 | ||
353 | /* util.c */ | |
354 | unsigned short rand16(void); | |
355 | int legal_char(char c); | |
356 | int canonicalise(char *s); | |
a222641c | 357 | int atoi_check(char *a, int *res); |
9e4abcb5 SK |
358 | void die(char *message, char *arg1); |
359 | void complain(char *message, char *arg1); | |
360 | void *safe_malloc(int size); | |
361 | char *safe_string_alloc(char *cp); | |
362 | int sa_len(union mysockaddr *addr); | |
363 | int sockaddr_isequal(union mysockaddr *s1, union mysockaddr *s2); | |
364 | int hostname_isequal(unsigned char *a, unsigned char *b); | |
44a2a316 | 365 | time_t dnsmasq_time(int fd); |
a84fa1d0 SK |
366 | int is_same_net(struct in_addr a, struct in_addr b, struct in_addr mask); |
367 | ||
9e4abcb5 SK |
368 | /* option.c */ |
369 | unsigned int read_opts(int argc, char **argv, char *buff, struct resolvc **resolv_file, | |
de37951c | 370 | struct mx_record **mxnames, char **mxtarget, char **lease_file, |
9e4abcb5 SK |
371 | char **username, char **groupname, |
372 | char **domain_suffix, char **runfile, | |
373 | struct iname **if_names, struct iname **if_addrs, struct iname **if_except, | |
374 | struct bogus_addr **bogus_addr, struct server **serv_addrs, int *cachesize, | |
375 | int *port, int *query_port, unsigned long *local_ttl, char **addn_hosts, | |
a84fa1d0 SK |
376 | struct dhcp_context **dhcp, struct dhcp_config **dhcp_conf, |
377 | struct dhcp_opt **opts, struct dhcp_vendor **dhcp_vendors, | |
44a2a316 | 378 | char **dhcp_file, char **dhcp_sname, struct in_addr *dhcp_next_server, |
1cff166d | 379 | int *maxleases, unsigned int *min_leasetime, struct doctor **doctors); |
9e4abcb5 SK |
380 | |
381 | /* forward.c */ | |
382 | void forward_init(int first); | |
de37951c SK |
383 | struct server *reply_query(struct serverfd *sfd, int options, char *packet, time_t now, |
384 | char *dnamebuff, struct server *servers, struct server *last_server, | |
1cff166d | 385 | struct bogus_addr *bogus_nxdomain, struct doctor *doctors); |
9e4abcb5 | 386 | |
de37951c | 387 | struct server *receive_query(struct listener *listen, char *packet, struct mx_record *mxnames, |
44a2a316 SK |
388 | char *mxtarget, unsigned int options, time_t now, |
389 | unsigned long local_ttl, char *namebuff, | |
390 | struct iname *names, struct iname *addrs, struct iname *except, | |
391 | struct server *last_server, struct server *servers); | |
9e4abcb5 SK |
392 | /* network.c */ |
393 | struct server *reload_servers(char *fname, char *buff, struct server *servers, int query_port); | |
394 | struct server *check_servers(struct server *new, struct irec *interfaces, struct serverfd **sfds); | |
a84fa1d0 SK |
395 | struct irec *enumerate_interfaces(struct iname **names, |
396 | struct iname **addrs, | |
44a2a316 SK |
397 | struct iname *except, |
398 | int port); | |
399 | struct listener *create_wildcard_listeners(int port); | |
400 | struct listener *create_bound_listeners(struct irec *interfaces); | |
9e4abcb5 | 401 | /* dhcp.c */ |
44a2a316 SK |
402 | void dhcp_init(int *fdp, int* rfdp); |
403 | void dhcp_packet(struct dhcp_context *contexts, char *packet, | |
404 | struct dhcp_opt *dhcp_opts, struct dhcp_config *dhcp_configs, | |
a84fa1d0 | 405 | struct dhcp_vendor *vendors, |
9e4abcb5 | 406 | time_t now, char *namebuff, char *domain_suffix, |
44a2a316 SK |
407 | char *dhcp_file, char *dhcp_sname, |
408 | struct in_addr dhcp_next_server, int dhcp_fd, int raw_fd, | |
409 | struct iname *names, struct iname *addrs, struct iname *except); | |
9e4abcb5 SK |
410 | int address_available(struct dhcp_context *context, struct in_addr addr); |
411 | int address_allocate(struct dhcp_context *context, struct dhcp_config *configs, | |
a84fa1d0 | 412 | struct in_addr *addrp, unsigned char *hwaddr); |
9e4abcb5 SK |
413 | struct dhcp_config *find_config(struct dhcp_config *configs, |
414 | struct dhcp_context *context, | |
415 | unsigned char *clid, int clid_len, | |
416 | unsigned char *hwaddr, char *hostname); | |
44a2a316 SK |
417 | struct dhcp_config *read_ethers(struct dhcp_config *configs, char *buff); |
418 | void dhcp_update_configs(struct dhcp_config *configs); | |
419 | struct dhcp_config *dhcp_read_ethers(struct dhcp_config *configs, char *buff); | |
a84fa1d0 | 420 | |
9e4abcb5 | 421 | /* lease.c */ |
44a2a316 SK |
422 | void lease_update_file(int force, time_t now); |
423 | void lease_update_dns(void); | |
9e4abcb5 | 424 | int lease_init(char *lease_file, char *domain, char *buff, |
44a2a316 | 425 | char *buff2, time_t now, int maxleases); |
9e4abcb5 SK |
426 | struct dhcp_lease *lease_allocate(unsigned char *clid, int clid_len, struct in_addr addr); |
427 | void lease_set_hwaddr(struct dhcp_lease *lease, unsigned char *hwaddr); | |
428 | void lease_set_hostname(struct dhcp_lease *lease, char *name, char *suffix); | |
429 | void lease_set_expires(struct dhcp_lease *lease, time_t exp); | |
430 | struct dhcp_lease *lease_find_by_client(unsigned char *clid, int clid_len); | |
431 | struct dhcp_lease *lease_find_by_addr(struct in_addr addr); | |
432 | void lease_prune(struct dhcp_lease *target, time_t now); | |
44a2a316 | 433 | void lease_update_from_configs(struct dhcp_config *dhcp_configs, char *domain); |
a84fa1d0 | 434 | |
9e4abcb5 | 435 | /* rfc2131.c */ |
44a2a316 SK |
436 | int dhcp_reply(struct dhcp_context *context, |
437 | struct in_addr iface_addr, | |
438 | char *iface_name, | |
439 | int iface_mtu, | |
440 | struct udp_dhcp_packet *rawpacket, | |
9e4abcb5 | 441 | unsigned int sz, time_t now, char *namebuff, |
44a2a316 | 442 | struct dhcp_opt *dhcp_opts, struct dhcp_config *dhcp_configs, |
a84fa1d0 | 443 | struct dhcp_vendor *vendors, |
9e4abcb5 | 444 | char *domain_suffix, char *dhcp_file, char *dhcp_sname, |
8a911ccc | 445 | struct in_addr dhcp_next_server, struct in_addr router); |
44a2a316 | 446 | |
33820b7e SK |
447 | /* isc.c */ |
448 | #ifdef HAVE_ISC_READER | |
449 | void load_dhcp(char *file, char *suffix, time_t now, char *hostname); | |
450 | #endif |