blob: eb7323da704e64c25f4b8f1fbccc633c55f86ee6 [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 Vlasenkod55fe3e2008-02-04 13:12:16 +000015#include "dhcpc.h"
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000016#include "dhcpd.h"
17#include "options.h"
Mike Frysinger7031f622006-05-08 03:20:50 +000018
19
20/* globals */
21struct dhcpOfferedAddr *leases;
Denis Vlasenkodeabacd2007-09-30 17:55:43 +000022/* struct server_config_t server_config is in bb_common_bufsiz1 */
Mike Frysinger7031f622006-05-08 03:20:50 +000023
24
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000025int udhcpd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Bernhard Reutner-Fischerfebe3c42007-04-04 20:52:03 +000026int udhcpd_main(int argc, char **argv)
Mike Frysinger7031f622006-05-08 03:20:50 +000027{
28 fd_set rfds;
29 struct timeval tv;
Rob Landley9ffd4232006-05-21 18:30:35 +000030 int server_socket = -1, bytes, retval, max_sock;
Mike Frysinger7031f622006-05-08 03:20:50 +000031 struct dhcpMessage packet;
Rob Landley9ffd4232006-05-21 18:30:35 +000032 uint8_t *state, *server_id, *requested;
33 uint32_t server_id_align, requested_align, static_lease_ip;
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000034 unsigned timeout_end;
35 unsigned num_ips;
Denis Vlasenko3d17d2b2007-08-14 16:45:29 +000036 unsigned opt;
Mike Frysinger7031f622006-05-08 03:20:50 +000037 struct option_set *option;
Rob Landley9ffd4232006-05-21 18:30:35 +000038 struct dhcpOfferedAddr *lease, static_lease;
Denis Vlasenkod55fe3e2008-02-04 13:12:16 +000039 USE_FEATURE_UDHCP_PORT(char *str_P;)
Mike Frysinger7031f622006-05-08 03:20:50 +000040
Denis Vlasenkod55fe3e2008-02-04 13:12:16 +000041#if ENABLE_FEATURE_UDHCP_PORT
42 SERVER_PORT = 67;
43 CLIENT_PORT = 68;
44#endif
45
46 opt = getopt32(argv, "fS" USE_FEATURE_UDHCP_PORT("P:", &str_P));
Denis Vlasenko9f7b92a2007-08-15 20:03:36 +000047 argv += optind;
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +000048
Denis Vlasenko3d17d2b2007-08-14 16:45:29 +000049 if (!(opt & 1)) { /* no -f */
Denis Vlasenkoc82b5102007-07-01 17:05:57 +000050 bb_daemonize_or_rexec(0, argv);
Denis Vlasenko4b924f32007-05-30 00:29:55 +000051 logmode &= ~LOGMODE_STDIO;
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +000052 }
53
Denis Vlasenko3d17d2b2007-08-14 16:45:29 +000054 if (opt & 2) { /* -S */
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +000055 openlog(applet_name, LOG_PID, LOG_LOCAL0);
56 logmode |= LOGMODE_SYSLOG;
57 }
Denis Vlasenkod55fe3e2008-02-04 13:12:16 +000058#if ENABLE_FEATURE_UDHCP_PORT
59 if (opt & 4) { /* -P */
60 SERVER_PORT = xatou16(str_P);
61 CLIENT_PORT = SERVER_PORT + 1;
62 }
63#endif
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +000064 /* Would rather not do read_config before daemonization -
65 * otherwise NOMMU machines will parse config twice */
Denis Vlasenko9f7b92a2007-08-15 20:03:36 +000066 read_config(argv[0] ? argv[0] : DHCPD_CONF_FILE);
Mike Frysinger7031f622006-05-08 03:20:50 +000067
Denis Vlasenko80edead2007-08-02 22:31:05 +000068 /* Make sure fd 0,1,2 are open */
69 bb_sanitize_stdio();
70 /* Equivalent of doing a fflush after every \n */
71 setlinebuf(stdout);
72
73 /* Create pidfile */
74 write_pidfile(server_config.pidfile);
75 /* if (!..) bb_perror_msg("cannot create pidfile %s", pidfile); */
76
Denis Vlasenkodef88982007-10-07 17:06:01 +000077 bb_info_msg("%s (v"BB_VER") started", applet_name);
Mike Frysinger7031f622006-05-08 03:20:50 +000078
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +000079 option = find_option(server_config.options, DHCP_LEASE_TIME);
Denis Vlasenko6e6d3312007-05-03 23:39:35 +000080 server_config.lease = LEASE_TIME;
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +000081 if (option) {
Mike Frysinger7031f622006-05-08 03:20:50 +000082 memcpy(&server_config.lease, option->data + 2, 4);
83 server_config.lease = ntohl(server_config.lease);
Denis Vlasenko6e6d3312007-05-03 23:39:35 +000084 }
Mike Frysinger7031f622006-05-08 03:20:50 +000085
86 /* Sanity check */
Denis Vlasenkoc82b5102007-07-01 17:05:57 +000087 num_ips = server_config.end_ip - server_config.start_ip + 1;
Mike Frysinger7031f622006-05-08 03:20:50 +000088 if (server_config.max_leases > num_ips) {
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000089 bb_error_msg("max_leases=%u is too big, setting to %u",
90 (unsigned)server_config.max_leases, num_ips);
Mike Frysinger7031f622006-05-08 03:20:50 +000091 server_config.max_leases = num_ips;
92 }
93
Denis Vlasenkoc82b5102007-07-01 17:05:57 +000094 leases = xzalloc(server_config.max_leases * sizeof(*leases));
Mike Frysinger7031f622006-05-08 03:20:50 +000095 read_leases(server_config.lease_file);
96
97 if (read_interface(server_config.interface, &server_config.ifindex,
Denis Vlasenko80edead2007-08-02 22:31:05 +000098 &server_config.server, server_config.arp)) {
Denis Vlasenko6e6d3312007-05-03 23:39:35 +000099 retval = 1;
100 goto ret;
101 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000102
Mike Frysinger7031f622006-05-08 03:20:50 +0000103 /* Setup the signal pipe */
104 udhcp_sp_setup();
105
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000106 timeout_end = monotonic_sec() + server_config.auto_time;
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000107 while (1) { /* loop until universe collapses */
Mike Frysinger7031f622006-05-08 03:20:50 +0000108
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000109 if (server_socket < 0) {
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000110 server_socket = listen_socket(/*INADDR_ANY,*/ SERVER_PORT,
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000111 server_config.interface);
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000112 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000113
114 max_sock = udhcp_sp_fd_set(&rfds, server_socket);
115 if (server_config.auto_time) {
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000116 tv.tv_sec = timeout_end - monotonic_sec();
Mike Frysinger7031f622006-05-08 03:20:50 +0000117 tv.tv_usec = 0;
118 }
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000119 retval = 0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000120 if (!server_config.auto_time || tv.tv_sec > 0) {
121 retval = select(max_sock + 1, &rfds, NULL, NULL,
122 server_config.auto_time ? &tv : NULL);
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000123 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000124 if (retval == 0) {
125 write_leases();
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000126 timeout_end = monotonic_sec() + server_config.auto_time;
Mike Frysinger7031f622006-05-08 03:20:50 +0000127 continue;
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000128 }
129 if (retval < 0 && errno != EINTR) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000130 DEBUG("error on select");
Mike Frysinger7031f622006-05-08 03:20:50 +0000131 continue;
132 }
133
134 switch (udhcp_sp_read(&rfds)) {
135 case SIGUSR1:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000136 bb_info_msg("Received a SIGUSR1");
Mike Frysinger7031f622006-05-08 03:20:50 +0000137 write_leases();
138 /* why not just reset the timeout, eh */
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000139 timeout_end = monotonic_sec() + server_config.auto_time;
Mike Frysinger7031f622006-05-08 03:20:50 +0000140 continue;
141 case SIGTERM:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000142 bb_info_msg("Received a SIGTERM");
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000143 goto ret0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000144 case 0: break; /* no signal */
145 default: continue; /* signal or error (probably EINTR) */
146 }
147
Denis Vlasenkofff145d2007-12-20 21:11:38 +0000148 bytes = udhcp_recv_packet(&packet, server_socket); /* this waits for a packet - idle */
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000149 if (bytes < 0) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000150 if (bytes == -1 && errno != EINTR) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000151 DEBUG("error on read, %s, reopening socket", strerror(errno));
Mike Frysinger7031f622006-05-08 03:20:50 +0000152 close(server_socket);
153 server_socket = -1;
154 }
155 continue;
156 }
157
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000158 state = get_option(&packet, DHCP_MESSAGE_TYPE);
159 if (state == NULL) {
Denis Vlasenkoa9595882006-09-29 21:30:43 +0000160 bb_error_msg("cannot get option from packet, ignoring");
Mike Frysinger7031f622006-05-08 03:20:50 +0000161 continue;
162 }
163
164 /* Look for a static lease */
165 static_lease_ip = getIpByMac(server_config.static_leases, &packet.chaddr);
166
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000167 if (static_lease_ip) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000168 bb_info_msg("Found static lease: %x", static_lease_ip);
Mike Frysinger7031f622006-05-08 03:20:50 +0000169
170 memcpy(&static_lease.chaddr, &packet.chaddr, 16);
171 static_lease.yiaddr = static_lease_ip;
172 static_lease.expires = 0;
173
174 lease = &static_lease;
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000175 } else {
176 lease = find_lease_by_chaddr(packet.chaddr);
Mike Frysinger7031f622006-05-08 03:20:50 +0000177 }
178
179 switch (state[0]) {
180 case DHCPDISCOVER:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000181 DEBUG("Received DISCOVER");
Mike Frysinger7031f622006-05-08 03:20:50 +0000182
183 if (sendOffer(&packet) < 0) {
Denis Vlasenkoa9595882006-09-29 21:30:43 +0000184 bb_error_msg("send OFFER failed");
Mike Frysinger7031f622006-05-08 03:20:50 +0000185 }
186 break;
187 case DHCPREQUEST:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000188 DEBUG("received REQUEST");
Mike Frysinger7031f622006-05-08 03:20:50 +0000189
190 requested = get_option(&packet, DHCP_REQUESTED_IP);
191 server_id = get_option(&packet, DHCP_SERVER_ID);
192
193 if (requested) memcpy(&requested_align, requested, 4);
194 if (server_id) memcpy(&server_id_align, server_id, 4);
195
196 if (lease) {
197 if (server_id) {
198 /* SELECTING State */
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000199 DEBUG("server_id = %08x", ntohl(server_id_align));
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000200 if (server_id_align == server_config.server && requested
201 && requested_align == lease->yiaddr
202 ) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000203 sendACK(&packet, lease->yiaddr);
204 }
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000205 } else if (requested) {
206 /* INIT-REBOOT State */
207 if (lease->yiaddr == requested_align)
208 sendACK(&packet, lease->yiaddr);
209 else
210 sendNAK(&packet);
211 } else if (lease->yiaddr == packet.ciaddr) {
212 /* RENEWING or REBINDING State */
213 sendACK(&packet, lease->yiaddr);
Mike Frysinger7031f622006-05-08 03:20:50 +0000214 } else {
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000215 /* don't know what to do!!!! */
216 sendNAK(&packet);
Mike Frysinger7031f622006-05-08 03:20:50 +0000217 }
218
219 /* what to do if we have no record of the client */
220 } else if (server_id) {
221 /* SELECTING State */
222
223 } else if (requested) {
224 /* INIT-REBOOT State */
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000225 lease = find_lease_by_yiaddr(requested_align);
226 if (lease) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000227 if (lease_expired(lease)) {
228 /* probably best if we drop this lease */
229 memset(lease->chaddr, 0, 16);
230 /* make some contention for this address */
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000231 } else
232 sendNAK(&packet);
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000233 } else {
234 uint32_t r = ntohl(requested_align);
235 if (r < server_config.start_ip
236 || r > server_config.end_ip
237 ) {
238 sendNAK(&packet);
239 }
Denis Vlasenkoe6bce972007-07-01 17:11:54 +0000240 /* else remain silent */
241 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000242
243 } else {
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000244 /* RENEWING or REBINDING State */
Mike Frysinger7031f622006-05-08 03:20:50 +0000245 }
246 break;
247 case DHCPDECLINE:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000248 DEBUG("Received DECLINE");
Mike Frysinger7031f622006-05-08 03:20:50 +0000249 if (lease) {
250 memset(lease->chaddr, 0, 16);
251 lease->expires = time(0) + server_config.decline_time;
252 }
253 break;
254 case DHCPRELEASE:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000255 DEBUG("Received RELEASE");
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000256 if (lease)
257 lease->expires = time(0);
Mike Frysinger7031f622006-05-08 03:20:50 +0000258 break;
259 case DHCPINFORM:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000260 DEBUG("Received INFORM");
Mike Frysinger7031f622006-05-08 03:20:50 +0000261 send_inform(&packet);
262 break;
263 default:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000264 bb_info_msg("Unsupported DHCP message (%02x) - ignoring", state[0]);
Mike Frysinger7031f622006-05-08 03:20:50 +0000265 }
266 }
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000267 ret0:
268 retval = 0;
269 ret:
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000270 /*if (server_config.pidfile) - server_config.pidfile is never NULL */
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000271 remove_pidfile(server_config.pidfile);
272 return retval;
Mike Frysinger7031f622006-05-08 03:20:50 +0000273}