blob: 89f57bfe80560c20eeafcddf8d3842a7e8e18d02 [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 Vlasenko80edead2007-08-02 22:31:05 +000054 /* Make sure fd 0,1,2 are open */
55 bb_sanitize_stdio();
56 /* Equivalent of doing a fflush after every \n */
57 setlinebuf(stdout);
58
59 /* Create pidfile */
60 write_pidfile(server_config.pidfile);
61 /* if (!..) bb_perror_msg("cannot create pidfile %s", pidfile); */
62
63 bb_info_msg("%s (v%s) started", applet_name, BB_VER);
Mike Frysinger7031f622006-05-08 03:20:50 +000064
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +000065 option = find_option(server_config.options, DHCP_LEASE_TIME);
Denis Vlasenko6e6d3312007-05-03 23:39:35 +000066 server_config.lease = LEASE_TIME;
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +000067 if (option) {
Mike Frysinger7031f622006-05-08 03:20:50 +000068 memcpy(&server_config.lease, option->data + 2, 4);
69 server_config.lease = ntohl(server_config.lease);
Denis Vlasenko6e6d3312007-05-03 23:39:35 +000070 }
Mike Frysinger7031f622006-05-08 03:20:50 +000071
72 /* Sanity check */
Denis Vlasenkoc82b5102007-07-01 17:05:57 +000073 num_ips = server_config.end_ip - server_config.start_ip + 1;
Mike Frysinger7031f622006-05-08 03:20:50 +000074 if (server_config.max_leases > num_ips) {
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000075 bb_error_msg("max_leases=%u is too big, setting to %u",
76 (unsigned)server_config.max_leases, num_ips);
Mike Frysinger7031f622006-05-08 03:20:50 +000077 server_config.max_leases = num_ips;
78 }
79
Denis Vlasenkoc82b5102007-07-01 17:05:57 +000080 leases = xzalloc(server_config.max_leases * sizeof(*leases));
Mike Frysinger7031f622006-05-08 03:20:50 +000081 read_leases(server_config.lease_file);
82
83 if (read_interface(server_config.interface, &server_config.ifindex,
Denis Vlasenko80edead2007-08-02 22:31:05 +000084 &server_config.server, server_config.arp)) {
Denis Vlasenko6e6d3312007-05-03 23:39:35 +000085 retval = 1;
86 goto ret;
87 }
Mike Frysinger7031f622006-05-08 03:20:50 +000088
Mike Frysinger7031f622006-05-08 03:20:50 +000089 /* Setup the signal pipe */
90 udhcp_sp_setup();
91
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000092 timeout_end = monotonic_sec() + server_config.auto_time;
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000093 while (1) { /* loop until universe collapses */
Mike Frysinger7031f622006-05-08 03:20:50 +000094
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +000095 if (server_socket < 0) {
Denis Vlasenko6e6d3312007-05-03 23:39:35 +000096 server_socket = listen_socket(INADDR_ANY, SERVER_PORT,
97 server_config.interface);
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +000098 }
Mike Frysinger7031f622006-05-08 03:20:50 +000099
100 max_sock = udhcp_sp_fd_set(&rfds, server_socket);
101 if (server_config.auto_time) {
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000102 tv.tv_sec = timeout_end - monotonic_sec();
Mike Frysinger7031f622006-05-08 03:20:50 +0000103 tv.tv_usec = 0;
104 }
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000105 retval = 0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000106 if (!server_config.auto_time || tv.tv_sec > 0) {
107 retval = select(max_sock + 1, &rfds, NULL, NULL,
108 server_config.auto_time ? &tv : NULL);
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000109 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000110 if (retval == 0) {
111 write_leases();
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000112 timeout_end = monotonic_sec() + server_config.auto_time;
Mike Frysinger7031f622006-05-08 03:20:50 +0000113 continue;
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000114 }
115 if (retval < 0 && errno != EINTR) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000116 DEBUG("error on select");
Mike Frysinger7031f622006-05-08 03:20:50 +0000117 continue;
118 }
119
120 switch (udhcp_sp_read(&rfds)) {
121 case SIGUSR1:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000122 bb_info_msg("Received a SIGUSR1");
Mike Frysinger7031f622006-05-08 03:20:50 +0000123 write_leases();
124 /* why not just reset the timeout, eh */
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000125 timeout_end = monotonic_sec() + server_config.auto_time;
Mike Frysinger7031f622006-05-08 03:20:50 +0000126 continue;
127 case SIGTERM:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000128 bb_info_msg("Received a SIGTERM");
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000129 goto ret0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000130 case 0: break; /* no signal */
131 default: continue; /* signal or error (probably EINTR) */
132 }
133
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000134 bytes = udhcp_get_packet(&packet, server_socket); /* this waits for a packet - idle */
135 if (bytes < 0) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000136 if (bytes == -1 && errno != EINTR) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000137 DEBUG("error on read, %s, reopening socket", strerror(errno));
Mike Frysinger7031f622006-05-08 03:20:50 +0000138 close(server_socket);
139 server_socket = -1;
140 }
141 continue;
142 }
143
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000144 state = get_option(&packet, DHCP_MESSAGE_TYPE);
145 if (state == NULL) {
Denis Vlasenkoa9595882006-09-29 21:30:43 +0000146 bb_error_msg("cannot get option from packet, ignoring");
Mike Frysinger7031f622006-05-08 03:20:50 +0000147 continue;
148 }
149
150 /* Look for a static lease */
151 static_lease_ip = getIpByMac(server_config.static_leases, &packet.chaddr);
152
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000153 if (static_lease_ip) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000154 bb_info_msg("Found static lease: %x", static_lease_ip);
Mike Frysinger7031f622006-05-08 03:20:50 +0000155
156 memcpy(&static_lease.chaddr, &packet.chaddr, 16);
157 static_lease.yiaddr = static_lease_ip;
158 static_lease.expires = 0;
159
160 lease = &static_lease;
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000161 } else {
162 lease = find_lease_by_chaddr(packet.chaddr);
Mike Frysinger7031f622006-05-08 03:20:50 +0000163 }
164
165 switch (state[0]) {
166 case DHCPDISCOVER:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000167 DEBUG("Received DISCOVER");
Mike Frysinger7031f622006-05-08 03:20:50 +0000168
169 if (sendOffer(&packet) < 0) {
Denis Vlasenkoa9595882006-09-29 21:30:43 +0000170 bb_error_msg("send OFFER failed");
Mike Frysinger7031f622006-05-08 03:20:50 +0000171 }
172 break;
173 case DHCPREQUEST:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000174 DEBUG("received REQUEST");
Mike Frysinger7031f622006-05-08 03:20:50 +0000175
176 requested = get_option(&packet, DHCP_REQUESTED_IP);
177 server_id = get_option(&packet, DHCP_SERVER_ID);
178
179 if (requested) memcpy(&requested_align, requested, 4);
180 if (server_id) memcpy(&server_id_align, server_id, 4);
181
182 if (lease) {
183 if (server_id) {
184 /* SELECTING State */
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000185 DEBUG("server_id = %08x", ntohl(server_id_align));
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000186 if (server_id_align == server_config.server && requested
187 && requested_align == lease->yiaddr
188 ) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000189 sendACK(&packet, lease->yiaddr);
190 }
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000191 } else if (requested) {
192 /* INIT-REBOOT State */
193 if (lease->yiaddr == requested_align)
194 sendACK(&packet, lease->yiaddr);
195 else
196 sendNAK(&packet);
197 } else if (lease->yiaddr == packet.ciaddr) {
198 /* RENEWING or REBINDING State */
199 sendACK(&packet, lease->yiaddr);
Mike Frysinger7031f622006-05-08 03:20:50 +0000200 } else {
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000201 /* don't know what to do!!!! */
202 sendNAK(&packet);
Mike Frysinger7031f622006-05-08 03:20:50 +0000203 }
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 */
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000211 lease = find_lease_by_yiaddr(requested_align);
212 if (lease) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000213 if (lease_expired(lease)) {
214 /* probably best if we drop this lease */
215 memset(lease->chaddr, 0, 16);
216 /* make some contention for this address */
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000217 } else
218 sendNAK(&packet);
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000219 } else {
220 uint32_t r = ntohl(requested_align);
221 if (r < server_config.start_ip
222 || r > server_config.end_ip
223 ) {
224 sendNAK(&packet);
225 }
Denis Vlasenkoe6bce972007-07-01 17:11:54 +0000226 /* else remain silent */
227 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000228
229 } else {
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000230 /* RENEWING or REBINDING State */
Mike Frysinger7031f622006-05-08 03:20:50 +0000231 }
232 break;
233 case DHCPDECLINE:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000234 DEBUG("Received DECLINE");
Mike Frysinger7031f622006-05-08 03:20:50 +0000235 if (lease) {
236 memset(lease->chaddr, 0, 16);
237 lease->expires = time(0) + server_config.decline_time;
238 }
239 break;
240 case DHCPRELEASE:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000241 DEBUG("Received RELEASE");
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000242 if (lease)
243 lease->expires = time(0);
Mike Frysinger7031f622006-05-08 03:20:50 +0000244 break;
245 case DHCPINFORM:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000246 DEBUG("Received INFORM");
Mike Frysinger7031f622006-05-08 03:20:50 +0000247 send_inform(&packet);
248 break;
249 default:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000250 bb_info_msg("Unsupported DHCP message (%02x) - ignoring", state[0]);
Mike Frysinger7031f622006-05-08 03:20:50 +0000251 }
252 }
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000253 ret0:
254 retval = 0;
255 ret:
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000256 /*if (server_config.pidfile) - server_config.pidfile is never NULL */
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000257 remove_pidfile(server_config.pidfile);
258 return retval;
Mike Frysinger7031f622006-05-08 03:20:50 +0000259}