tar -Z, uncompress support
[oweals/busybox.git] / networking / udhcp / dhcpd.c
1 /* dhcpd.c
2  *
3  * udhcp Server
4  * Copyright (C) 1999 Matthew Ramsay <matthewr@moreton.com.au>
5  *                      Chris Trew <ctrew@moreton.com.au>
6  *
7  * Rewrite by Russ Dill <Russ.Dill@asu.edu> July 2001
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  */
23
24 #include <fcntl.h>
25 #include <string.h>
26 #include <stdlib.h>
27 #include <sys/wait.h>
28 #include <arpa/inet.h>
29 #include <netdb.h>
30 #include <netinet/in.h>
31 #include <sys/socket.h>
32 #include <unistd.h>
33 #include <signal.h>
34 #include <errno.h>
35 #include <sys/ioctl.h>
36 #include <time.h>
37 #include <sys/time.h>
38
39 #include "dhcpd.h"
40 #include "arpping.h"
41 #include "socket.h"
42 #include "options.h"
43 #include "files.h"
44 #include "serverpacket.h"
45 #include "common.h"
46
47
48 /* globals */
49 struct dhcpOfferedAddr *leases;
50 struct server_config_t server_config;
51
52
53 int udhcpd_main(int argc, char *argv[])
54 {       
55         fd_set rfds;
56         struct timeval tv;
57         int server_socket = -1;
58         int bytes, retval;
59         struct dhcpMessage packet;
60         unsigned char *state;
61         unsigned char *server_id, *requested;
62         u_int32_t server_id_align, requested_align;
63         unsigned long timeout_end;
64         struct option_set *option;
65         struct dhcpOfferedAddr *lease;
66         int max_sock;
67         int sig;
68         unsigned long num_ips;
69         
70         start_log("server");
71
72         memset(&server_config, 0, sizeof(struct server_config_t));
73         
74         if (argc < 2)
75                 read_config(DHCPD_CONF_FILE);
76         else read_config(argv[1]);
77
78         if ((option = find_option(server_config.options, DHCP_LEASE_TIME))) {
79                 memcpy(&server_config.lease, option->data + 2, 4);
80                 server_config.lease = ntohl(server_config.lease);
81         }
82         else server_config.lease = LEASE_TIME;
83         
84         /* Sanity check */
85         num_ips = ntohl(server_config.end) - ntohl(server_config.start);
86         if (server_config.max_leases > num_ips) {
87                 LOG(LOG_ERR, "max_leases value (%lu) not sane, "
88                         "setting to %lu instead",
89                         server_config.max_leases, num_ips);
90                 server_config.max_leases = num_ips;
91         }
92
93         leases = xcalloc(sizeof(struct dhcpOfferedAddr), server_config.max_leases);
94         read_leases(server_config.lease_file);
95
96         if (read_interface(server_config.interface, &server_config.ifindex,
97                            &server_config.server, server_config.arp) < 0)
98                 return(1);
99
100 #ifndef CONFIG_FEATURE_UDHCP_DEBUG
101         background(server_config.pidfile);
102 #endif
103
104         udhcp_set_signal_pipe(0);
105
106         timeout_end = time(0) + server_config.auto_time;
107         while(1) { /* loop until universe collapses */
108
109                 if (server_socket < 0)
110                         if ((server_socket = listen_socket(INADDR_ANY, SERVER_PORT, server_config.interface)) < 0) {
111                                 LOG(LOG_ERR, "FATAL: couldn't create server socket, %m");
112                                 return(2);
113                         }                       
114
115                 FD_ZERO(&rfds);
116                 FD_SET(server_socket, &rfds);
117                 FD_SET(udhcp_signal_pipe[0], &rfds);
118                 if (server_config.auto_time) {
119                         tv.tv_sec = timeout_end - time(0);
120                         tv.tv_usec = 0;
121                 }
122                 if (!server_config.auto_time || tv.tv_sec > 0) {
123                         max_sock = server_socket > udhcp_signal_pipe[0] ? server_socket : udhcp_signal_pipe[0];
124                         retval = select(max_sock + 1, &rfds, NULL, NULL, 
125                                         server_config.auto_time ? &tv : NULL);
126                 } else retval = 0; /* If we already timed out, fall through */
127
128                 if (retval == 0) {
129                         write_leases();
130                         timeout_end = time(0) + server_config.auto_time;
131                         continue;
132                 } else if (retval < 0 && errno != EINTR) {
133                         DEBUG(LOG_INFO, "error on select");
134                         continue;
135                 }
136                 
137                 if (FD_ISSET(udhcp_signal_pipe[0], &rfds)) {
138                         if (read(udhcp_signal_pipe[0], &sig, sizeof(sig)) < 0)
139                                 continue; /* probably just EINTR */
140                         switch (sig) {
141                         case SIGUSR1:
142                                 LOG(LOG_INFO, "Received a SIGUSR1");
143                                 write_leases();
144                                 /* why not just reset the timeout, eh */
145                                 timeout_end = time(0) + server_config.auto_time;
146                                 continue;
147                         case SIGTERM:
148                                 LOG(LOG_INFO, "Received a SIGTERM");
149                                 return(0);
150                         }
151                 }
152
153                 if ((bytes = get_packet(&packet, server_socket)) < 0) { /* this waits for a packet - idle */
154                         if (bytes == -1 && errno != EINTR) {
155                                 DEBUG(LOG_INFO, "error on read, %m, reopening socket");
156                                 close(server_socket);
157                                 server_socket = -1;
158                         }
159                         continue;
160                 }
161
162                 if ((state = get_option(&packet, DHCP_MESSAGE_TYPE)) == NULL) {
163                         DEBUG(LOG_ERR, "couldn't get option from packet, ignoring");
164                         continue;
165                 }
166                 
167                 /* ADDME: look for a static lease */
168                 lease = find_lease_by_chaddr(packet.chaddr);
169                 switch (state[0]) {
170                 case DHCPDISCOVER:
171                         DEBUG(LOG_INFO,"received DISCOVER");
172                         
173                         if (sendOffer(&packet) < 0) {
174                                 LOG(LOG_ERR, "send OFFER failed");
175                         }
176                         break;                  
177                 case DHCPREQUEST:
178                         DEBUG(LOG_INFO, "received REQUEST");
179
180                         requested = get_option(&packet, DHCP_REQUESTED_IP);
181                         server_id = get_option(&packet, DHCP_SERVER_ID);
182
183                         if (requested) memcpy(&requested_align, requested, 4);
184                         if (server_id) memcpy(&server_id_align, server_id, 4);
185                 
186                         if (lease) { /*ADDME: or static lease */
187                                 if (server_id) {
188                                         /* SELECTING State */
189                                         DEBUG(LOG_INFO, "server_id = %08x", ntohl(server_id_align));
190                                         if (server_id_align == server_config.server && requested && 
191                                             requested_align == lease->yiaddr) {
192                                                 sendACK(&packet, lease->yiaddr);
193                                         }
194                                 } else {
195                                         if (requested) {
196                                                 /* INIT-REBOOT State */
197                                                 if (lease->yiaddr == requested_align)
198                                                         sendACK(&packet, lease->yiaddr);
199                                                 else sendNAK(&packet);
200                                         } else {
201                                                 /* RENEWING or REBINDING State */
202                                                 if (lease->yiaddr == packet.ciaddr)
203                                                         sendACK(&packet, lease->yiaddr);
204                                                 else {
205                                                         /* don't know what to do!!!! */
206                                                         sendNAK(&packet);
207                                                 }
208                                         }                                               
209                                 }
210                         
211                         /* what to do if we have no record of the client */
212                         } else if (server_id) {
213                                 /* SELECTING State */
214
215                         } else if (requested) {
216                                 /* INIT-REBOOT State */
217                                 if ((lease = find_lease_by_yiaddr(requested_align))) {
218                                         if (lease_expired(lease)) {
219                                                 /* probably best if we drop this lease */
220                                                 memset(lease->chaddr, 0, 16);
221                                         /* make some contention for this address */
222                                         } else sendNAK(&packet);
223                                 } else if (requested_align < server_config.start || 
224                                            requested_align > server_config.end) {
225                                         sendNAK(&packet);
226                                 } /* else remain silent */
227
228                         } else {
229                                  /* RENEWING or REBINDING State */
230                         }
231                         break;
232                 case DHCPDECLINE:
233                         DEBUG(LOG_INFO,"received DECLINE");
234                         if (lease) {
235                                 memset(lease->chaddr, 0, 16);
236                                 lease->expires = time(0) + server_config.decline_time;
237                         }                       
238                         break;
239                 case DHCPRELEASE:
240                         DEBUG(LOG_INFO,"received RELEASE");
241                         if (lease) lease->expires = time(0);
242                         break;
243                 case DHCPINFORM:
244                         DEBUG(LOG_INFO,"received INFORM");
245                         send_inform(&packet);
246                         break;  
247                 default:
248                         LOG(LOG_WARNING, "unsupported DHCP message (%02x) -- ignoring", state[0]);
249                 }
250         }
251
252         return 0;
253 }