blob: 8cac681958704c0c08936ae026b345439aa74fa0 [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
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +000013#include <syslog.h>
Mike Frysinger7031f622006-05-08 03:20:50 +000014#include "common.h"
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000015#include "dhcpd.h"
16#include "options.h"
Mike Frysinger7031f622006-05-08 03:20:50 +000017
18
19/* globals */
20struct dhcpOfferedAddr *leases;
21struct server_config_t server_config;
22
23
Bernhard Reutner-Fischerfebe3c42007-04-04 20:52:03 +000024int udhcpd_main(int argc, char **argv);
25int udhcpd_main(int argc, char **argv)
Mike Frysinger7031f622006-05-08 03:20:50 +000026{
27 fd_set rfds;
28 struct timeval tv;
Rob Landley9ffd4232006-05-21 18:30:35 +000029 int server_socket = -1, bytes, retval, max_sock;
Mike Frysinger7031f622006-05-08 03:20:50 +000030 struct dhcpMessage packet;
Rob Landley9ffd4232006-05-21 18:30:35 +000031 uint8_t *state, *server_id, *requested;
32 uint32_t server_id_align, requested_align, static_lease_ip;
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000033 unsigned timeout_end;
34 unsigned num_ips;
Mike Frysinger7031f622006-05-08 03:20:50 +000035 struct option_set *option;
Rob Landley9ffd4232006-05-21 18:30:35 +000036 struct dhcpOfferedAddr *lease, static_lease;
Mike Frysinger7031f622006-05-08 03:20:50 +000037
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +000038//Huh, dhcpd don't have --foreground, --syslog options?? TODO
39
40 if (!ENABLE_FEATURE_UDHCP_DEBUG) {
Denis Vlasenkoc82b5102007-07-01 17:05:57 +000041 bb_daemonize_or_rexec(0, argv);
Denis Vlasenko4b924f32007-05-30 00:29:55 +000042 logmode &= ~LOGMODE_STDIO;
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +000043 }
44
45 if (ENABLE_FEATURE_UDHCP_SYSLOG) {
46 openlog(applet_name, LOG_PID, LOG_LOCAL0);
47 logmode |= LOGMODE_SYSLOG;
48 }
49
50 /* Would rather not do read_config before daemonization -
51 * otherwise NOMMU machines will parse config twice */
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000052 read_config(argv[1] ? argv[1] : DHCPD_CONF_FILE);
Mike Frysinger7031f622006-05-08 03:20:50 +000053
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +000054 udhcp_make_pidfile(server_config.pidfile);
Mike Frysinger7031f622006-05-08 03:20:50 +000055
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +000056 option = find_option(server_config.options, DHCP_LEASE_TIME);
Denis Vlasenko6e6d3312007-05-03 23:39:35 +000057 server_config.lease = LEASE_TIME;
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +000058 if (option) {
Mike Frysinger7031f622006-05-08 03:20:50 +000059 memcpy(&server_config.lease, option->data + 2, 4);
60 server_config.lease = ntohl(server_config.lease);
Denis Vlasenko6e6d3312007-05-03 23:39:35 +000061 }
Mike Frysinger7031f622006-05-08 03:20:50 +000062
63 /* Sanity check */
Denis Vlasenkoc82b5102007-07-01 17:05:57 +000064 num_ips = server_config.end_ip - server_config.start_ip + 1;
Mike Frysinger7031f622006-05-08 03:20:50 +000065 if (server_config.max_leases > num_ips) {
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000066 bb_error_msg("max_leases=%u is too big, setting to %u",
67 (unsigned)server_config.max_leases, num_ips);
Mike Frysinger7031f622006-05-08 03:20:50 +000068 server_config.max_leases = num_ips;
69 }
70
Denis Vlasenkoc82b5102007-07-01 17:05:57 +000071 leases = xzalloc(server_config.max_leases * sizeof(*leases));
Mike Frysinger7031f622006-05-08 03:20:50 +000072 read_leases(server_config.lease_file);
73
74 if (read_interface(server_config.interface, &server_config.ifindex,
Denis Vlasenko6e6d3312007-05-03 23:39:35 +000075 &server_config.server, server_config.arp) < 0) {
76 retval = 1;
77 goto ret;
78 }
Mike Frysinger7031f622006-05-08 03:20:50 +000079
Mike Frysinger7031f622006-05-08 03:20:50 +000080 /* Setup the signal pipe */
81 udhcp_sp_setup();
82
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000083 timeout_end = monotonic_sec() + server_config.auto_time;
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000084 while (1) { /* loop until universe collapses */
Mike Frysinger7031f622006-05-08 03:20:50 +000085
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +000086 if (server_socket < 0) {
Denis Vlasenko6e6d3312007-05-03 23:39:35 +000087 server_socket = listen_socket(INADDR_ANY, SERVER_PORT,
88 server_config.interface);
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +000089 }
Mike Frysinger7031f622006-05-08 03:20:50 +000090
91 max_sock = udhcp_sp_fd_set(&rfds, server_socket);
92 if (server_config.auto_time) {
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000093 tv.tv_sec = timeout_end - monotonic_sec();
Mike Frysinger7031f622006-05-08 03:20:50 +000094 tv.tv_usec = 0;
95 }
Denis Vlasenko6e6d3312007-05-03 23:39:35 +000096 retval = 0;
Mike Frysinger7031f622006-05-08 03:20:50 +000097 if (!server_config.auto_time || tv.tv_sec > 0) {
98 retval = select(max_sock + 1, &rfds, NULL, NULL,
99 server_config.auto_time ? &tv : NULL);
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000100 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000101 if (retval == 0) {
102 write_leases();
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000103 timeout_end = monotonic_sec() + server_config.auto_time;
Mike Frysinger7031f622006-05-08 03:20:50 +0000104 continue;
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000105 }
106 if (retval < 0 && errno != EINTR) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000107 DEBUG("error on select");
Mike Frysinger7031f622006-05-08 03:20:50 +0000108 continue;
109 }
110
111 switch (udhcp_sp_read(&rfds)) {
112 case SIGUSR1:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000113 bb_info_msg("Received a SIGUSR1");
Mike Frysinger7031f622006-05-08 03:20:50 +0000114 write_leases();
115 /* why not just reset the timeout, eh */
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000116 timeout_end = monotonic_sec() + server_config.auto_time;
Mike Frysinger7031f622006-05-08 03:20:50 +0000117 continue;
118 case SIGTERM:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000119 bb_info_msg("Received a SIGTERM");
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000120 goto ret0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000121 case 0: break; /* no signal */
122 default: continue; /* signal or error (probably EINTR) */
123 }
124
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000125 bytes = udhcp_get_packet(&packet, server_socket); /* this waits for a packet - idle */
126 if (bytes < 0) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000127 if (bytes == -1 && errno != EINTR) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000128 DEBUG("error on read, %s, reopening socket", strerror(errno));
Mike Frysinger7031f622006-05-08 03:20:50 +0000129 close(server_socket);
130 server_socket = -1;
131 }
132 continue;
133 }
134
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000135 state = get_option(&packet, DHCP_MESSAGE_TYPE);
136 if (state == NULL) {
Denis Vlasenkoa9595882006-09-29 21:30:43 +0000137 bb_error_msg("cannot get option from packet, ignoring");
Mike Frysinger7031f622006-05-08 03:20:50 +0000138 continue;
139 }
140
141 /* Look for a static lease */
142 static_lease_ip = getIpByMac(server_config.static_leases, &packet.chaddr);
143
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000144 if (static_lease_ip) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000145 bb_info_msg("Found static lease: %x", static_lease_ip);
Mike Frysinger7031f622006-05-08 03:20:50 +0000146
147 memcpy(&static_lease.chaddr, &packet.chaddr, 16);
148 static_lease.yiaddr = static_lease_ip;
149 static_lease.expires = 0;
150
151 lease = &static_lease;
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000152 } else {
153 lease = find_lease_by_chaddr(packet.chaddr);
Mike Frysinger7031f622006-05-08 03:20:50 +0000154 }
155
156 switch (state[0]) {
157 case DHCPDISCOVER:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000158 DEBUG("Received DISCOVER");
Mike Frysinger7031f622006-05-08 03:20:50 +0000159
160 if (sendOffer(&packet) < 0) {
Denis Vlasenkoa9595882006-09-29 21:30:43 +0000161 bb_error_msg("send OFFER failed");
Mike Frysinger7031f622006-05-08 03:20:50 +0000162 }
163 break;
164 case DHCPREQUEST:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000165 DEBUG("received REQUEST");
Mike Frysinger7031f622006-05-08 03:20:50 +0000166
167 requested = get_option(&packet, DHCP_REQUESTED_IP);
168 server_id = get_option(&packet, DHCP_SERVER_ID);
169
170 if (requested) memcpy(&requested_align, requested, 4);
171 if (server_id) memcpy(&server_id_align, server_id, 4);
172
173 if (lease) {
174 if (server_id) {
175 /* SELECTING State */
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000176 DEBUG("server_id = %08x", ntohl(server_id_align));
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000177 if (server_id_align == server_config.server && requested
178 && requested_align == lease->yiaddr
179 ) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000180 sendACK(&packet, lease->yiaddr);
181 }
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000182 } else if (requested) {
183 /* INIT-REBOOT State */
184 if (lease->yiaddr == requested_align)
185 sendACK(&packet, lease->yiaddr);
186 else
187 sendNAK(&packet);
188 } else if (lease->yiaddr == packet.ciaddr) {
189 /* RENEWING or REBINDING State */
190 sendACK(&packet, lease->yiaddr);
Mike Frysinger7031f622006-05-08 03:20:50 +0000191 } else {
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000192 /* don't know what to do!!!! */
193 sendNAK(&packet);
Mike Frysinger7031f622006-05-08 03:20:50 +0000194 }
195
196 /* what to do if we have no record of the client */
197 } else if (server_id) {
198 /* SELECTING State */
199
200 } else if (requested) {
201 /* INIT-REBOOT State */
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000202 lease = find_lease_by_yiaddr(requested_align);
203 if (lease) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000204 if (lease_expired(lease)) {
205 /* probably best if we drop this lease */
206 memset(lease->chaddr, 0, 16);
207 /* make some contention for this address */
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000208 } else
209 sendNAK(&packet);
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000210 } else {
211 uint32_t r = ntohl(requested_align);
212 if (r < server_config.start_ip
213 || r > server_config.end_ip
214 ) {
215 sendNAK(&packet);
216 }
Denis Vlasenkoe6bce972007-07-01 17:11:54 +0000217 /* else remain silent */
218 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000219
220 } else {
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000221 /* RENEWING or REBINDING State */
Mike Frysinger7031f622006-05-08 03:20:50 +0000222 }
223 break;
224 case DHCPDECLINE:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000225 DEBUG("Received DECLINE");
Mike Frysinger7031f622006-05-08 03:20:50 +0000226 if (lease) {
227 memset(lease->chaddr, 0, 16);
228 lease->expires = time(0) + server_config.decline_time;
229 }
230 break;
231 case DHCPRELEASE:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000232 DEBUG("Received RELEASE");
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000233 if (lease)
234 lease->expires = time(0);
Mike Frysinger7031f622006-05-08 03:20:50 +0000235 break;
236 case DHCPINFORM:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000237 DEBUG("Received INFORM");
Mike Frysinger7031f622006-05-08 03:20:50 +0000238 send_inform(&packet);
239 break;
240 default:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000241 bb_info_msg("Unsupported DHCP message (%02x) - ignoring", state[0]);
Mike Frysinger7031f622006-05-08 03:20:50 +0000242 }
243 }
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000244 ret0:
245 retval = 0;
246 ret:
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000247 /*if (server_config.pidfile) - server_config.pidfile is never NULL */
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000248 remove_pidfile(server_config.pidfile);
249 return retval;
Mike Frysinger7031f622006-05-08 03:20:50 +0000250}