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