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