blob: 45f445b48a9eeb66ea3d38a3a5b6b1fae171a7dd [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;
Denis Vlasenkodeabacd2007-09-30 17:55:43 +000021/* struct server_config_t server_config is in bb_common_bufsiz1 */
Mike Frysinger7031f622006-05-08 03:20:50 +000022
23
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000024int udhcpd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Bernhard Reutner-Fischerfebe3c42007-04-04 20:52:03 +000025int 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;
Denis Vlasenko3d17d2b2007-08-14 16:45:29 +000035 unsigned opt;
Mike Frysinger7031f622006-05-08 03:20:50 +000036 struct option_set *option;
Rob Landley9ffd4232006-05-21 18:30:35 +000037 struct dhcpOfferedAddr *lease, static_lease;
Mike Frysinger7031f622006-05-08 03:20:50 +000038
Denis Vlasenkofe7cd642007-08-18 15:32:12 +000039 opt = getopt32(argv, "fS");
Denis Vlasenko9f7b92a2007-08-15 20:03:36 +000040 argv += optind;
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +000041
Denis Vlasenko3d17d2b2007-08-14 16:45:29 +000042 if (!(opt & 1)) { /* no -f */
Denis Vlasenkoc82b5102007-07-01 17:05:57 +000043 bb_daemonize_or_rexec(0, argv);
Denis Vlasenko4b924f32007-05-30 00:29:55 +000044 logmode &= ~LOGMODE_STDIO;
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +000045 }
46
Denis Vlasenko3d17d2b2007-08-14 16:45:29 +000047 if (opt & 2) { /* -S */
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +000048 openlog(applet_name, LOG_PID, LOG_LOCAL0);
49 logmode |= LOGMODE_SYSLOG;
50 }
51
52 /* Would rather not do read_config before daemonization -
53 * otherwise NOMMU machines will parse config twice */
Denis Vlasenko9f7b92a2007-08-15 20:03:36 +000054 read_config(argv[0] ? argv[0] : DHCPD_CONF_FILE);
Mike Frysinger7031f622006-05-08 03:20:50 +000055
Denis Vlasenko80edead2007-08-02 22:31:05 +000056 /* Make sure fd 0,1,2 are open */
57 bb_sanitize_stdio();
58 /* Equivalent of doing a fflush after every \n */
59 setlinebuf(stdout);
60
61 /* Create pidfile */
62 write_pidfile(server_config.pidfile);
63 /* if (!..) bb_perror_msg("cannot create pidfile %s", pidfile); */
64
Denis Vlasenkodef88982007-10-07 17:06:01 +000065 bb_info_msg("%s (v"BB_VER") started", applet_name);
Mike Frysinger7031f622006-05-08 03:20:50 +000066
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +000067 option = find_option(server_config.options, DHCP_LEASE_TIME);
Denis Vlasenko6e6d3312007-05-03 23:39:35 +000068 server_config.lease = LEASE_TIME;
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +000069 if (option) {
Mike Frysinger7031f622006-05-08 03:20:50 +000070 memcpy(&server_config.lease, option->data + 2, 4);
71 server_config.lease = ntohl(server_config.lease);
Denis Vlasenko6e6d3312007-05-03 23:39:35 +000072 }
Mike Frysinger7031f622006-05-08 03:20:50 +000073
74 /* Sanity check */
Denis Vlasenkoc82b5102007-07-01 17:05:57 +000075 num_ips = server_config.end_ip - server_config.start_ip + 1;
Mike Frysinger7031f622006-05-08 03:20:50 +000076 if (server_config.max_leases > num_ips) {
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000077 bb_error_msg("max_leases=%u is too big, setting to %u",
78 (unsigned)server_config.max_leases, num_ips);
Mike Frysinger7031f622006-05-08 03:20:50 +000079 server_config.max_leases = num_ips;
80 }
81
Denis Vlasenkoc82b5102007-07-01 17:05:57 +000082 leases = xzalloc(server_config.max_leases * sizeof(*leases));
Mike Frysinger7031f622006-05-08 03:20:50 +000083 read_leases(server_config.lease_file);
84
85 if (read_interface(server_config.interface, &server_config.ifindex,
Denis Vlasenko80edead2007-08-02 22:31:05 +000086 &server_config.server, server_config.arp)) {
Denis Vlasenko6e6d3312007-05-03 23:39:35 +000087 retval = 1;
88 goto ret;
89 }
Mike Frysinger7031f622006-05-08 03:20:50 +000090
Mike Frysinger7031f622006-05-08 03:20:50 +000091 /* Setup the signal pipe */
92 udhcp_sp_setup();
93
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000094 timeout_end = monotonic_sec() + server_config.auto_time;
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000095 while (1) { /* loop until universe collapses */
Mike Frysinger7031f622006-05-08 03:20:50 +000096
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +000097 if (server_socket < 0) {
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +000098 server_socket = listen_socket(/*INADDR_ANY,*/ SERVER_PORT,
Denis Vlasenko6e6d3312007-05-03 23:39:35 +000099 server_config.interface);
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000100 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000101
102 max_sock = udhcp_sp_fd_set(&rfds, server_socket);
103 if (server_config.auto_time) {
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000104 tv.tv_sec = timeout_end - monotonic_sec();
Mike Frysinger7031f622006-05-08 03:20:50 +0000105 tv.tv_usec = 0;
106 }
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000107 retval = 0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000108 if (!server_config.auto_time || tv.tv_sec > 0) {
109 retval = select(max_sock + 1, &rfds, NULL, NULL,
110 server_config.auto_time ? &tv : NULL);
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000111 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000112 if (retval == 0) {
113 write_leases();
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000114 timeout_end = monotonic_sec() + server_config.auto_time;
Mike Frysinger7031f622006-05-08 03:20:50 +0000115 continue;
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000116 }
117 if (retval < 0 && errno != EINTR) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000118 DEBUG("error on select");
Mike Frysinger7031f622006-05-08 03:20:50 +0000119 continue;
120 }
121
122 switch (udhcp_sp_read(&rfds)) {
123 case SIGUSR1:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000124 bb_info_msg("Received a SIGUSR1");
Mike Frysinger7031f622006-05-08 03:20:50 +0000125 write_leases();
126 /* why not just reset the timeout, eh */
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000127 timeout_end = monotonic_sec() + server_config.auto_time;
Mike Frysinger7031f622006-05-08 03:20:50 +0000128 continue;
129 case SIGTERM:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000130 bb_info_msg("Received a SIGTERM");
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000131 goto ret0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000132 case 0: break; /* no signal */
133 default: continue; /* signal or error (probably EINTR) */
134 }
135
Denis Vlasenkofff145d2007-12-20 21:11:38 +0000136 bytes = udhcp_recv_packet(&packet, server_socket); /* this waits for a packet - idle */
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000137 if (bytes < 0) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000138 if (bytes == -1 && errno != EINTR) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000139 DEBUG("error on read, %s, reopening socket", strerror(errno));
Mike Frysinger7031f622006-05-08 03:20:50 +0000140 close(server_socket);
141 server_socket = -1;
142 }
143 continue;
144 }
145
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000146 state = get_option(&packet, DHCP_MESSAGE_TYPE);
147 if (state == NULL) {
Denis Vlasenkoa9595882006-09-29 21:30:43 +0000148 bb_error_msg("cannot get option from packet, ignoring");
Mike Frysinger7031f622006-05-08 03:20:50 +0000149 continue;
150 }
151
152 /* Look for a static lease */
153 static_lease_ip = getIpByMac(server_config.static_leases, &packet.chaddr);
154
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000155 if (static_lease_ip) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000156 bb_info_msg("Found static lease: %x", static_lease_ip);
Mike Frysinger7031f622006-05-08 03:20:50 +0000157
158 memcpy(&static_lease.chaddr, &packet.chaddr, 16);
159 static_lease.yiaddr = static_lease_ip;
160 static_lease.expires = 0;
161
162 lease = &static_lease;
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000163 } else {
164 lease = find_lease_by_chaddr(packet.chaddr);
Mike Frysinger7031f622006-05-08 03:20:50 +0000165 }
166
167 switch (state[0]) {
168 case DHCPDISCOVER:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000169 DEBUG("Received DISCOVER");
Mike Frysinger7031f622006-05-08 03:20:50 +0000170
171 if (sendOffer(&packet) < 0) {
Denis Vlasenkoa9595882006-09-29 21:30:43 +0000172 bb_error_msg("send OFFER failed");
Mike Frysinger7031f622006-05-08 03:20:50 +0000173 }
174 break;
175 case DHCPREQUEST:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000176 DEBUG("received REQUEST");
Mike Frysinger7031f622006-05-08 03:20:50 +0000177
178 requested = get_option(&packet, DHCP_REQUESTED_IP);
179 server_id = get_option(&packet, DHCP_SERVER_ID);
180
181 if (requested) memcpy(&requested_align, requested, 4);
182 if (server_id) memcpy(&server_id_align, server_id, 4);
183
184 if (lease) {
185 if (server_id) {
186 /* SELECTING State */
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000187 DEBUG("server_id = %08x", ntohl(server_id_align));
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000188 if (server_id_align == server_config.server && requested
189 && requested_align == lease->yiaddr
190 ) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000191 sendACK(&packet, lease->yiaddr);
192 }
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000193 } else if (requested) {
194 /* INIT-REBOOT State */
195 if (lease->yiaddr == requested_align)
196 sendACK(&packet, lease->yiaddr);
197 else
198 sendNAK(&packet);
199 } else if (lease->yiaddr == packet.ciaddr) {
200 /* RENEWING or REBINDING State */
201 sendACK(&packet, lease->yiaddr);
Mike Frysinger7031f622006-05-08 03:20:50 +0000202 } else {
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000203 /* don't know what to do!!!! */
204 sendNAK(&packet);
Mike Frysinger7031f622006-05-08 03:20:50 +0000205 }
206
207 /* what to do if we have no record of the client */
208 } else if (server_id) {
209 /* SELECTING State */
210
211 } else if (requested) {
212 /* INIT-REBOOT State */
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000213 lease = find_lease_by_yiaddr(requested_align);
214 if (lease) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000215 if (lease_expired(lease)) {
216 /* probably best if we drop this lease */
217 memset(lease->chaddr, 0, 16);
218 /* make some contention for this address */
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000219 } else
220 sendNAK(&packet);
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000221 } else {
222 uint32_t r = ntohl(requested_align);
223 if (r < server_config.start_ip
224 || r > server_config.end_ip
225 ) {
226 sendNAK(&packet);
227 }
Denis Vlasenkoe6bce972007-07-01 17:11:54 +0000228 /* else remain silent */
229 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000230
231 } else {
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000232 /* RENEWING or REBINDING State */
Mike Frysinger7031f622006-05-08 03:20:50 +0000233 }
234 break;
235 case DHCPDECLINE:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000236 DEBUG("Received DECLINE");
Mike Frysinger7031f622006-05-08 03:20:50 +0000237 if (lease) {
238 memset(lease->chaddr, 0, 16);
239 lease->expires = time(0) + server_config.decline_time;
240 }
241 break;
242 case DHCPRELEASE:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000243 DEBUG("Received RELEASE");
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000244 if (lease)
245 lease->expires = time(0);
Mike Frysinger7031f622006-05-08 03:20:50 +0000246 break;
247 case DHCPINFORM:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000248 DEBUG("Received INFORM");
Mike Frysinger7031f622006-05-08 03:20:50 +0000249 send_inform(&packet);
250 break;
251 default:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000252 bb_info_msg("Unsupported DHCP message (%02x) - ignoring", state[0]);
Mike Frysinger7031f622006-05-08 03:20:50 +0000253 }
254 }
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000255 ret0:
256 retval = 0;
257 ret:
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000258 /*if (server_config.pidfile) - server_config.pidfile is never NULL */
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000259 remove_pidfile(server_config.pidfile);
260 return retval;
Mike Frysinger7031f622006-05-08 03:20:50 +0000261}