]>
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> | |
44a2a316 SK |
59 | #if defined(__OpenBSD__) |
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 | |
9e4abcb5 SK |
94 | |
95 | struct all_addr { | |
96 | union { | |
97 | struct in_addr addr4; | |
98 | #ifdef HAVE_IPV6 | |
99 | struct in6_addr addr6; | |
100 | #endif | |
101 | } addr; | |
102 | }; | |
103 | ||
104 | struct bogus_addr { | |
105 | struct in_addr addr; | |
106 | struct bogus_addr *next; | |
107 | }; | |
108 | ||
1cff166d SK |
109 | /* dns doctor param */ |
110 | struct doctor { | |
111 | struct in_addr in, out, mask; | |
112 | struct doctor *next; | |
113 | }; | |
114 | ||
9e4abcb5 SK |
115 | union bigname { |
116 | char name[MAXDNAME]; | |
117 | union bigname *next; /* freelist */ | |
118 | }; | |
119 | ||
120 | struct crec { | |
121 | struct crec *next, *prev, *hash_next; | |
122 | time_t ttd; /* time to die */ | |
123 | struct all_addr addr; | |
124 | unsigned short flags; | |
125 | union { | |
126 | char sname[SMALLDNAME]; | |
127 | union bigname *bname; | |
128 | char *namep; | |
129 | } name; | |
130 | }; | |
131 | ||
132 | #define F_IMMORTAL 1 | |
133 | #define F_CONFIG 2 | |
134 | #define F_REVERSE 4 | |
135 | #define F_FORWARD 8 | |
136 | #define F_DHCP 16 | |
137 | #define F_NEG 32 | |
138 | #define F_HOSTS 64 | |
139 | #define F_IPV4 128 | |
140 | #define F_IPV6 256 | |
141 | #define F_BIGNAME 512 | |
142 | #define F_UPSTREAM 1024 | |
143 | #define F_SERVER 2048 | |
144 | #define F_NXDOMAIN 4096 | |
145 | #define F_QUERY 8192 | |
146 | #define F_ADDN 16384 | |
147 | #define F_NOERR 32768 | |
148 | ||
149 | /* struct sockaddr is not large enough to hold any address, | |
150 | and specifically not big enough to hold and IPv6 address. | |
151 | Blech. Roll our own. */ | |
152 | union mysockaddr { | |
153 | struct sockaddr sa; | |
154 | struct sockaddr_in in; | |
155 | #ifdef HAVE_BROKEN_SOCKADDR_IN6 | |
156 | /* early versions of glibc don't include sin6_scope_id in sockaddr_in6 | |
157 | but latest kernels _require_ it to be set. The choice is to have | |
158 | dnsmasq fail to compile on back-level libc or fail to run | |
159 | on latest kernels with IPv6. Or to do this: sorry that it's so gross. */ | |
160 | struct my_sockaddr_in6 { | |
161 | sa_family_t sin6_family; /* AF_INET6 */ | |
162 | uint16_t sin6_port; /* transport layer port # */ | |
163 | uint32_t sin6_flowinfo; /* IPv6 traffic class & flow info */ | |
164 | struct in6_addr sin6_addr; /* IPv6 address */ | |
165 | uint32_t sin6_scope_id; /* set of interfaces for a scope */ | |
166 | } in6; | |
167 | #elif defined(HAVE_IPV6) | |
168 | struct sockaddr_in6 in6; | |
169 | #endif | |
170 | }; | |
171 | ||
172 | #define SERV_FROM_RESOLV 1 /* 1 for servers from resolv, 0 for command line. */ | |
173 | #define SERV_NO_ADDR 2 /* no server, this domain is local only */ | |
174 | #define SERV_LITERAL_ADDRESS 4 /* addr is the answer, not the server */ | |
175 | #define SERV_HAS_SOURCE 8 /* source address specified */ | |
176 | #define SERV_HAS_DOMAIN 16 /* server for one domain only */ | |
177 | #define SERV_FOR_NODOTS 32 /* server for names with no domain part only */ | |
178 | #define SERV_TYPE (SERV_HAS_DOMAIN | SERV_FOR_NODOTS) | |
179 | ||
180 | struct serverfd { | |
181 | int fd; | |
182 | union mysockaddr source_addr; | |
183 | struct serverfd *next; | |
184 | }; | |
185 | ||
186 | struct server { | |
187 | union mysockaddr addr, source_addr; | |
188 | struct serverfd *sfd; /* non-NULL if this server has its own fd bound to | |
189 | a source port */ | |
190 | char *domain; /* set if this server only handles a domain. */ | |
191 | int flags; | |
192 | struct server *next; | |
193 | }; | |
194 | ||
9e4abcb5 SK |
195 | struct irec { |
196 | union mysockaddr addr; | |
9e4abcb5 SK |
197 | struct irec *next; |
198 | }; | |
199 | ||
44a2a316 SK |
200 | struct listener { |
201 | int fd, family; | |
202 | struct listener *next; | |
203 | }; | |
204 | ||
9e4abcb5 SK |
205 | /* interface and address parms from command line. */ |
206 | struct iname { | |
207 | char *name; | |
208 | union mysockaddr addr; | |
9e4abcb5 SK |
209 | struct iname *next; |
210 | }; | |
211 | ||
212 | /* resolv-file parms from command-line */ | |
213 | struct resolvc { | |
214 | struct resolvc *next; | |
215 | int is_default; | |
216 | int logged; | |
217 | char *name; | |
218 | }; | |
219 | ||
220 | struct frec { | |
221 | union mysockaddr source; | |
44a2a316 | 222 | struct all_addr dest; |
9e4abcb5 SK |
223 | struct server *sentto; |
224 | unsigned short orig_id, new_id; | |
225 | int fd; | |
226 | time_t time; | |
227 | struct frec *next; | |
228 | }; | |
229 | ||
230 | struct dhcp_lease { | |
231 | int clid_len; /* length of client identifier */ | |
232 | unsigned char *clid; /* clientid */ | |
233 | char *hostname, *fqdn; /* name from client-hostname option or config */ | |
234 | time_t expires; /* lease expiry */ | |
235 | unsigned char hwaddr[ETHER_ADDR_LEN]; | |
236 | struct in_addr addr; | |
237 | struct dhcp_lease *next; | |
238 | }; | |
239 | ||
240 | struct dhcp_config { | |
241 | int clid_len; /* length of client identifier */ | |
242 | unsigned char *clid; /* clientid */ | |
243 | unsigned char hwaddr[ETHER_ADDR_LEN]; | |
244 | char *hostname; | |
245 | struct in_addr addr; | |
246 | unsigned int lease_time; | |
247 | struct dhcp_config *next; | |
248 | }; | |
249 | ||
250 | struct dhcp_opt { | |
1ab84e2f | 251 | int opt, len, is_addr; |
9e4abcb5 | 252 | unsigned char *val; |
44a2a316 | 253 | char *netid; |
9e4abcb5 SK |
254 | struct dhcp_opt *next; |
255 | }; | |
256 | ||
257 | struct dhcp_context { | |
9e4abcb5 | 258 | unsigned int lease_time; |
44a2a316 | 259 | struct in_addr netmask, broadcast; |
9e4abcb5 | 260 | struct in_addr start, end, last; /* range of available addresses */ |
44a2a316 | 261 | char *netid; |
9e4abcb5 SK |
262 | struct dhcp_context *next; |
263 | }; | |
264 | ||
265 | typedef unsigned char u8; | |
266 | typedef unsigned short u16; | |
267 | typedef unsigned int u32; | |
268 | ||
269 | ||
270 | struct udp_dhcp_packet { | |
271 | struct ip ip; | |
272 | struct udphdr { | |
273 | u16 uh_sport; /* source port */ | |
274 | u16 uh_dport; /* destination port */ | |
275 | u16 uh_ulen; /* udp length */ | |
276 | u16 uh_sum; /* udp checksum */ | |
277 | } udp; | |
278 | struct dhcp_packet { | |
279 | u8 op, htype, hlen, hops; | |
280 | u32 xid; | |
281 | u16 secs, flags; | |
282 | struct in_addr ciaddr, yiaddr, siaddr, giaddr; | |
283 | u8 chaddr[16], sname[64], file[128]; | |
284 | u32 cookie; | |
285 | u8 options[308]; | |
286 | } data; | |
287 | }; | |
288 | ||
289 | ||
290 | /* cache.c */ | |
291 | void cache_init(int cachesize, int log); | |
292 | void log_query(unsigned short flags, char *name, struct all_addr *addr); | |
293 | struct crec *cache_find_by_addr(struct crec *crecp, | |
294 | struct all_addr *addr, time_t now, | |
295 | unsigned short prot); | |
296 | struct crec *cache_find_by_name(struct crec *crecp, | |
297 | char *name, time_t now, unsigned short prot); | |
298 | void cache_end_insert(void); | |
299 | void cache_start_insert(void); | |
300 | void cache_insert(char *name, struct all_addr *addr, | |
301 | time_t now, unsigned long ttl, unsigned short flags); | |
302 | void cache_reload(int opts, char *buff, char *domain_suffix, char *addn_hosts); | |
303 | void cache_add_dhcp_entry(char *host_name, struct in_addr *host_address, | |
304 | time_t ttd, unsigned short flags); | |
305 | void cache_unhash_dhcp(void); | |
306 | void dump_cache(int debug, int size); | |
307 | char *cache_get_name(struct crec *crecp); | |
308 | ||
309 | /* rfc1035.c */ | |
310 | unsigned short extract_request(HEADER *header, unsigned int qlen, char *name); | |
311 | int setup_reply(HEADER *header, unsigned int qlen, | |
312 | struct all_addr *addrp, unsigned short flags, | |
313 | unsigned long local_ttl); | |
1cff166d SK |
314 | void extract_addresses(HEADER *header, unsigned int qlen, char *namebuff, |
315 | time_t now, struct doctor *doctors); | |
9e4abcb5 SK |
316 | void extract_neg_addrs(HEADER *header, unsigned int qlen, char *namebuff, time_t now); |
317 | int answer_request(HEADER *header, char *limit, unsigned int qlen, char *mxname, | |
318 | char *mxtarget, unsigned int options, time_t now, unsigned long local_ttl, | |
319 | char *namebuff); | |
320 | int check_for_bogus_wildcard(HEADER *header, unsigned int qlen, char *name, | |
321 | struct bogus_addr *addr, time_t now); | |
322 | ||
323 | /* util.c */ | |
324 | unsigned short rand16(void); | |
325 | int legal_char(char c); | |
326 | int canonicalise(char *s); | |
327 | void die(char *message, char *arg1); | |
328 | void complain(char *message, char *arg1); | |
329 | void *safe_malloc(int size); | |
330 | char *safe_string_alloc(char *cp); | |
331 | int sa_len(union mysockaddr *addr); | |
332 | int sockaddr_isequal(union mysockaddr *s1, union mysockaddr *s2); | |
333 | int hostname_isequal(unsigned char *a, unsigned char *b); | |
44a2a316 | 334 | time_t dnsmasq_time(int fd); |
9e4abcb5 SK |
335 | /* option.c */ |
336 | unsigned int read_opts(int argc, char **argv, char *buff, struct resolvc **resolv_file, | |
337 | char **mxname, char **mxtarget, char **lease_file, | |
338 | char **username, char **groupname, | |
339 | char **domain_suffix, char **runfile, | |
340 | struct iname **if_names, struct iname **if_addrs, struct iname **if_except, | |
341 | struct bogus_addr **bogus_addr, struct server **serv_addrs, int *cachesize, | |
342 | int *port, int *query_port, unsigned long *local_ttl, char **addn_hosts, | |
343 | struct dhcp_context **dhcp, struct dhcp_config **dhcp_conf, struct dhcp_opt **opts, | |
44a2a316 | 344 | char **dhcp_file, char **dhcp_sname, struct in_addr *dhcp_next_server, |
1cff166d | 345 | int *maxleases, unsigned int *min_leasetime, struct doctor **doctors); |
9e4abcb5 SK |
346 | |
347 | /* forward.c */ | |
348 | void forward_init(int first); | |
9e4abcb5 SK |
349 | struct server *reply_query(int fd, int options, char *packet, time_t now, |
350 | char *dnamebuff, struct server *last_server, | |
1cff166d | 351 | struct bogus_addr *bogus_nxdomain, struct doctor *doctors); |
9e4abcb5 | 352 | |
44a2a316 SK |
353 | struct server *receive_query(struct listener *listen, char *packet, char *mxname, |
354 | char *mxtarget, unsigned int options, time_t now, | |
355 | unsigned long local_ttl, char *namebuff, | |
356 | struct iname *names, struct iname *addrs, struct iname *except, | |
357 | struct server *last_server, struct server *servers); | |
9e4abcb5 SK |
358 | /* network.c */ |
359 | struct server *reload_servers(char *fname, char *buff, struct server *servers, int query_port); | |
360 | struct server *check_servers(struct server *new, struct irec *interfaces, struct serverfd **sfds); | |
44a2a316 SK |
361 | struct irec *enumerate_interfaces(struct iname *names, |
362 | struct iname *addrs, | |
363 | struct iname *except, | |
364 | int port); | |
365 | struct listener *create_wildcard_listeners(int port); | |
366 | struct listener *create_bound_listeners(struct irec *interfaces); | |
9e4abcb5 | 367 | /* dhcp.c */ |
44a2a316 SK |
368 | void dhcp_init(int *fdp, int* rfdp); |
369 | void dhcp_packet(struct dhcp_context *contexts, char *packet, | |
370 | struct dhcp_opt *dhcp_opts, struct dhcp_config *dhcp_configs, | |
9e4abcb5 | 371 | time_t now, char *namebuff, char *domain_suffix, |
44a2a316 SK |
372 | char *dhcp_file, char *dhcp_sname, |
373 | struct in_addr dhcp_next_server, int dhcp_fd, int raw_fd, | |
374 | struct iname *names, struct iname *addrs, struct iname *except); | |
9e4abcb5 SK |
375 | int address_available(struct dhcp_context *context, struct in_addr addr); |
376 | int address_allocate(struct dhcp_context *context, struct dhcp_config *configs, | |
377 | struct in_addr *addrp); | |
378 | struct dhcp_config *find_config(struct dhcp_config *configs, | |
379 | struct dhcp_context *context, | |
380 | unsigned char *clid, int clid_len, | |
381 | unsigned char *hwaddr, char *hostname); | |
44a2a316 SK |
382 | struct dhcp_config *read_ethers(struct dhcp_config *configs, char *buff); |
383 | void dhcp_update_configs(struct dhcp_config *configs); | |
384 | struct dhcp_config *dhcp_read_ethers(struct dhcp_config *configs, char *buff); | |
9e4abcb5 | 385 | /* lease.c */ |
44a2a316 SK |
386 | void lease_update_file(int force, time_t now); |
387 | void lease_update_dns(void); | |
9e4abcb5 | 388 | int lease_init(char *lease_file, char *domain, char *buff, |
44a2a316 | 389 | char *buff2, time_t now, int maxleases); |
9e4abcb5 SK |
390 | struct dhcp_lease *lease_allocate(unsigned char *clid, int clid_len, struct in_addr addr); |
391 | void lease_set_hwaddr(struct dhcp_lease *lease, unsigned char *hwaddr); | |
392 | void lease_set_hostname(struct dhcp_lease *lease, char *name, char *suffix); | |
393 | void lease_set_expires(struct dhcp_lease *lease, time_t exp); | |
394 | struct dhcp_lease *lease_find_by_client(unsigned char *clid, int clid_len); | |
395 | struct dhcp_lease *lease_find_by_addr(struct in_addr addr); | |
396 | void lease_prune(struct dhcp_lease *target, time_t now); | |
44a2a316 | 397 | void lease_update_from_configs(struct dhcp_config *dhcp_configs, char *domain); |
9e4abcb5 | 398 | /* rfc2131.c */ |
44a2a316 SK |
399 | int dhcp_reply(struct dhcp_context *context, |
400 | struct in_addr iface_addr, | |
401 | char *iface_name, | |
402 | int iface_mtu, | |
403 | struct udp_dhcp_packet *rawpacket, | |
9e4abcb5 | 404 | unsigned int sz, time_t now, char *namebuff, |
44a2a316 | 405 | struct dhcp_opt *dhcp_opts, struct dhcp_config *dhcp_configs, |
9e4abcb5 | 406 | char *domain_suffix, char *dhcp_file, char *dhcp_sname, |
8a911ccc | 407 | struct in_addr dhcp_next_server, struct in_addr router); |
44a2a316 | 408 |