blob: bee4ffdb3072889a04c6dffe03ca2ef4fe80c7e6 [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Mike Frysinger7031f622006-05-08 03:20:50 +00002/* dhcpd.c
3 *
4 * udhcp Server
5 * Copyright (C) 1999 Matthew Ramsay <matthewr@moreton.com.au>
6 * Chris Trew <ctrew@moreton.com.au>
7 *
8 * Rewrite by Russ Dill <Russ.Dill@asu.edu> July 2001
9 *
Rob Landley3f785612006-05-28 01:06:36 +000010 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Mike Frysinger7031f622006-05-08 03:20:50 +000011 */
12
13#include <fcntl.h>
14#include <string.h>
15#include <stdlib.h>
16#include <sys/wait.h>
17#include <arpa/inet.h>
18#include <netdb.h>
19#include <netinet/in.h>
20#include <sys/socket.h>
21#include <unistd.h>
22#include <signal.h>
23#include <errno.h>
24#include <sys/ioctl.h>
25#include <time.h>
Mike Frysinger7031f622006-05-08 03:20:50 +000026
27#include "dhcpd.h"
28#include "arpping.h"
29#include "socket.h"
30#include "options.h"
31#include "files.h"
32#include "serverpacket.h"
33#include "common.h"
34#include "signalpipe.h"
35#include "static_leases.h"
36
37
38/* globals */
39struct dhcpOfferedAddr *leases;
40struct server_config_t server_config;
41
42
Mike Frysinger7031f622006-05-08 03:20:50 +000043int udhcpd_main(int argc, char *argv[])
Mike Frysinger7031f622006-05-08 03:20:50 +000044{
45 fd_set rfds;
46 struct timeval tv;
Rob Landley9ffd4232006-05-21 18:30:35 +000047 int server_socket = -1, bytes, retval, max_sock;
Mike Frysinger7031f622006-05-08 03:20:50 +000048 struct dhcpMessage packet;
Rob Landley9ffd4232006-05-21 18:30:35 +000049 uint8_t *state, *server_id, *requested;
50 uint32_t server_id_align, requested_align, static_lease_ip;
51 unsigned long timeout_end, num_ips;
Mike Frysinger7031f622006-05-08 03:20:50 +000052 struct option_set *option;
Rob Landley9ffd4232006-05-21 18:30:35 +000053 struct dhcpOfferedAddr *lease, static_lease;
Mike Frysinger7031f622006-05-08 03:20:50 +000054
Mike Frysinger7031f622006-05-08 03:20:50 +000055 read_config(argc < 2 ? DHCPD_CONF_FILE : argv[1]);
56
57 /* Start the log, sanitize fd's, and write a pid file */
Denis Vlasenko239369b2006-09-07 17:05:44 +000058 udhcp_start_log_and_pid(server_config.pidfile);
Mike Frysinger7031f622006-05-08 03:20:50 +000059
60 if ((option = find_option(server_config.options, DHCP_LEASE_TIME))) {
61 memcpy(&server_config.lease, option->data + 2, 4);
62 server_config.lease = ntohl(server_config.lease);
63 }
64 else server_config.lease = LEASE_TIME;
65
66 /* Sanity check */
67 num_ips = ntohl(server_config.end) - ntohl(server_config.start) + 1;
68 if (server_config.max_leases > num_ips) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000069 bb_error_msg("max_leases value (%lu) not sane, "
Mike Frysinger7031f622006-05-08 03:20:50 +000070 "setting to %lu instead",
71 server_config.max_leases, num_ips);
72 server_config.max_leases = num_ips;
73 }
74
Rob Landley9ffd4232006-05-21 18:30:35 +000075 leases = xzalloc(server_config.max_leases * sizeof(struct dhcpOfferedAddr));
Mike Frysinger7031f622006-05-08 03:20:50 +000076 read_leases(server_config.lease_file);
77
78 if (read_interface(server_config.interface, &server_config.ifindex,
79 &server_config.server, server_config.arp) < 0)
80 return 1;
81
Rob Landley3f785612006-05-28 01:06:36 +000082 if (!ENABLE_FEATURE_UDHCP_DEBUG)
83 udhcp_background(server_config.pidfile); /* hold lock during fork. */
Mike Frysinger7031f622006-05-08 03:20:50 +000084
85 /* Setup the signal pipe */
86 udhcp_sp_setup();
87
88 timeout_end = time(0) + server_config.auto_time;
89 while(1) { /* loop until universe collapses */
90
91 if (server_socket < 0)
92 if ((server_socket = listen_socket(INADDR_ANY, SERVER_PORT, server_config.interface)) < 0) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000093 bb_perror_msg("FATAL: couldn't create server socket");
Mike Frysinger7031f622006-05-08 03:20:50 +000094 return 2;
95 }
96
97 max_sock = udhcp_sp_fd_set(&rfds, server_socket);
98 if (server_config.auto_time) {
99 tv.tv_sec = timeout_end - time(0);
100 tv.tv_usec = 0;
101 }
102 if (!server_config.auto_time || tv.tv_sec > 0) {
103 retval = select(max_sock + 1, &rfds, NULL, NULL,
104 server_config.auto_time ? &tv : NULL);
105 } else retval = 0; /* If we already timed out, fall through */
106
107 if (retval == 0) {
108 write_leases();
109 timeout_end = time(0) + server_config.auto_time;
110 continue;
111 } else if (retval < 0 && errno != EINTR) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000112 DEBUG("error on select");
Mike Frysinger7031f622006-05-08 03:20:50 +0000113 continue;
114 }
115
116 switch (udhcp_sp_read(&rfds)) {
117 case SIGUSR1:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000118 bb_info_msg("Received a SIGUSR1");
Mike Frysinger7031f622006-05-08 03:20:50 +0000119 write_leases();
120 /* why not just reset the timeout, eh */
121 timeout_end = time(0) + server_config.auto_time;
122 continue;
123 case SIGTERM:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000124 bb_info_msg("Received a SIGTERM");
Mike Frysinger7031f622006-05-08 03:20:50 +0000125 return 0;
126 case 0: break; /* no signal */
127 default: continue; /* signal or error (probably EINTR) */
128 }
129
Rob Landley3f785612006-05-28 01:06:36 +0000130 if ((bytes = udhcp_get_packet(&packet, server_socket)) < 0) { /* this waits for a packet - idle */
Mike Frysinger7031f622006-05-08 03:20:50 +0000131 if (bytes == -1 && errno != EINTR) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000132 DEBUG("error on read, %s, reopening socket", strerror(errno));
Mike Frysinger7031f622006-05-08 03:20:50 +0000133 close(server_socket);
134 server_socket = -1;
135 }
136 continue;
137 }
138
139 if ((state = get_option(&packet, DHCP_MESSAGE_TYPE)) == NULL) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000140 bb_error_msg("Couldn't get option from packet, ignoring");
Mike Frysinger7031f622006-05-08 03:20:50 +0000141 continue;
142 }
143
144 /* Look for a static lease */
145 static_lease_ip = getIpByMac(server_config.static_leases, &packet.chaddr);
146
147 if(static_lease_ip)
148 {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000149 bb_info_msg("Found static lease: %x", static_lease_ip);
Mike Frysinger7031f622006-05-08 03:20:50 +0000150
151 memcpy(&static_lease.chaddr, &packet.chaddr, 16);
152 static_lease.yiaddr = static_lease_ip;
153 static_lease.expires = 0;
154
155 lease = &static_lease;
156
157 }
158 else
159 {
160 lease = find_lease_by_chaddr(packet.chaddr);
161 }
162
163 switch (state[0]) {
164 case DHCPDISCOVER:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000165 DEBUG("Received DISCOVER");
Mike Frysinger7031f622006-05-08 03:20:50 +0000166
167 if (sendOffer(&packet) < 0) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000168 bb_error_msg("Send OFFER failed");
Mike Frysinger7031f622006-05-08 03:20:50 +0000169 }
170 break;
171 case DHCPREQUEST:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000172 DEBUG("received REQUEST");
Mike Frysinger7031f622006-05-08 03:20:50 +0000173
174 requested = get_option(&packet, DHCP_REQUESTED_IP);
175 server_id = get_option(&packet, DHCP_SERVER_ID);
176
177 if (requested) memcpy(&requested_align, requested, 4);
178 if (server_id) memcpy(&server_id_align, server_id, 4);
179
180 if (lease) {
181 if (server_id) {
182 /* SELECTING State */
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000183 DEBUG("server_id = %08x", ntohl(server_id_align));
Mike Frysinger7031f622006-05-08 03:20:50 +0000184 if (server_id_align == server_config.server && requested &&
185 requested_align == lease->yiaddr) {
186 sendACK(&packet, lease->yiaddr);
187 }
188 } else {
189 if (requested) {
190 /* INIT-REBOOT State */
191 if (lease->yiaddr == requested_align)
192 sendACK(&packet, lease->yiaddr);
193 else sendNAK(&packet);
194 } else {
195 /* RENEWING or REBINDING State */
196 if (lease->yiaddr == packet.ciaddr)
197 sendACK(&packet, lease->yiaddr);
198 else {
199 /* don't know what to do!!!! */
200 sendNAK(&packet);
201 }
202 }
203 }
204
205 /* what to do if we have no record of the client */
206 } else if (server_id) {
207 /* SELECTING State */
208
209 } else if (requested) {
210 /* INIT-REBOOT State */
211 if ((lease = find_lease_by_yiaddr(requested_align))) {
212 if (lease_expired(lease)) {
213 /* probably best if we drop this lease */
214 memset(lease->chaddr, 0, 16);
215 /* make some contention for this address */
216 } else sendNAK(&packet);
217 } else if (requested_align < server_config.start ||
218 requested_align > server_config.end) {
219 sendNAK(&packet);
220 } /* else remain silent */
221
222 } else {
223 /* RENEWING or REBINDING State */
224 }
225 break;
226 case DHCPDECLINE:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000227 DEBUG("Received DECLINE");
Mike Frysinger7031f622006-05-08 03:20:50 +0000228 if (lease) {
229 memset(lease->chaddr, 0, 16);
230 lease->expires = time(0) + server_config.decline_time;
231 }
232 break;
233 case DHCPRELEASE:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000234 DEBUG("Received RELEASE");
Mike Frysinger7031f622006-05-08 03:20:50 +0000235 if (lease) lease->expires = time(0);
236 break;
237 case DHCPINFORM:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000238 DEBUG("Received INFORM");
Mike Frysinger7031f622006-05-08 03:20:50 +0000239 send_inform(&packet);
240 break;
241 default:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000242 bb_info_msg("Unsupported DHCP message (%02x) - ignoring", state[0]);
Mike Frysinger7031f622006-05-08 03:20:50 +0000243 }
244 }
245
246 return 0;
247}
248