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