blob: ccdfd362b7fd5a32c663a8adde12620fe7efcf50 [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;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000026int udhcpd_main(int argc UNUSED_PARAM, char **argv)
Mike Frysinger7031f622006-05-08 03:20:50 +000027{
28 fd_set rfds;
Denis Vlasenko0416e3d2009-01-01 17:52:09 +000029 int server_socket = -1, 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;
Denis Vlasenkoefb545b2008-12-08 22:56:18 +000032 uint32_t server_id_aligned = server_id_aligned; /* for compiler */
33 uint32_t requested_aligned = requested_aligned;
34 uint32_t static_lease_ip;
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000035 unsigned timeout_end;
36 unsigned num_ips;
Denis Vlasenko3d17d2b2007-08-14 16:45:29 +000037 unsigned opt;
Mike Frysinger7031f622006-05-08 03:20:50 +000038 struct option_set *option;
Rob Landley9ffd4232006-05-21 18:30:35 +000039 struct dhcpOfferedAddr *lease, static_lease;
Denis Vlasenko5e34ff22009-04-21 11:09:40 +000040 IF_FEATURE_UDHCP_PORT(char *str_P;)
Mike Frysinger7031f622006-05-08 03:20:50 +000041
Denis Vlasenkod55fe3e2008-02-04 13:12:16 +000042#if ENABLE_FEATURE_UDHCP_PORT
43 SERVER_PORT = 67;
44 CLIENT_PORT = 68;
45#endif
46
Denis Vlasenko5e34ff22009-04-21 11:09:40 +000047 opt = getopt32(argv, "fS" IF_FEATURE_UDHCP_PORT("P:", &str_P));
Denis Vlasenko9f7b92a2007-08-15 20:03:36 +000048 argv += optind;
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 Vlasenkoa19e6492009-03-11 14:40:00 +000051 logmode = LOGMODE_NONE;
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +000052 }
Denis Vlasenko3d17d2b2007-08-14 16:45:29 +000053 if (opt & 2) { /* -S */
Denis Vlasenko5e4fda02009-03-08 23:46:48 +000054 openlog(applet_name, LOG_PID, LOG_DAEMON);
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +000055 logmode |= LOGMODE_SYSLOG;
56 }
Denis Vlasenkod55fe3e2008-02-04 13:12:16 +000057#if ENABLE_FEATURE_UDHCP_PORT
58 if (opt & 4) { /* -P */
59 SERVER_PORT = xatou16(str_P);
60 CLIENT_PORT = SERVER_PORT + 1;
61 }
62#endif
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +000063 /* Would rather not do read_config before daemonization -
64 * otherwise NOMMU machines will parse config twice */
Denis Vlasenko9f7b92a2007-08-15 20:03:36 +000065 read_config(argv[0] ? argv[0] : DHCPD_CONF_FILE);
Mike Frysinger7031f622006-05-08 03:20:50 +000066
Denis Vlasenko80edead2007-08-02 22:31:05 +000067 /* Make sure fd 0,1,2 are open */
68 bb_sanitize_stdio();
69 /* Equivalent of doing a fflush after every \n */
70 setlinebuf(stdout);
71
72 /* Create pidfile */
73 write_pidfile(server_config.pidfile);
74 /* if (!..) bb_perror_msg("cannot create pidfile %s", pidfile); */
75
Denis Vlasenkodef88982007-10-07 17:06:01 +000076 bb_info_msg("%s (v"BB_VER") started", applet_name);
Mike Frysinger7031f622006-05-08 03:20:50 +000077
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +000078 option = find_option(server_config.options, DHCP_LEASE_TIME);
Denis Vlasenko6e6d3312007-05-03 23:39:35 +000079 server_config.lease = LEASE_TIME;
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +000080 if (option) {
Denis Vlasenkoefb545b2008-12-08 22:56:18 +000081 move_from_unaligned32(server_config.lease, option->data + 2);
Mike Frysinger7031f622006-05-08 03:20:50 +000082 server_config.lease = ntohl(server_config.lease);
Denis Vlasenko6e6d3312007-05-03 23:39:35 +000083 }
Mike Frysinger7031f622006-05-08 03:20:50 +000084
85 /* Sanity check */
Denis Vlasenkoc82b5102007-07-01 17:05:57 +000086 num_ips = server_config.end_ip - server_config.start_ip + 1;
Mike Frysinger7031f622006-05-08 03:20:50 +000087 if (server_config.max_leases > num_ips) {
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000088 bb_error_msg("max_leases=%u is too big, setting to %u",
89 (unsigned)server_config.max_leases, num_ips);
Mike Frysinger7031f622006-05-08 03:20:50 +000090 server_config.max_leases = num_ips;
91 }
92
Denis Vlasenkoc82b5102007-07-01 17:05:57 +000093 leases = xzalloc(server_config.max_leases * sizeof(*leases));
Mike Frysinger7031f622006-05-08 03:20:50 +000094 read_leases(server_config.lease_file);
95
Denis Vlasenkof1980f62008-09-26 09:34:59 +000096 if (udhcp_read_interface(server_config.interface, &server_config.ifindex,
Denis Vlasenko80edead2007-08-02 22:31:05 +000097 &server_config.server, server_config.arp)) {
Denis Vlasenko6e6d3312007-05-03 23:39:35 +000098 retval = 1;
99 goto ret;
100 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000101
Mike Frysinger7031f622006-05-08 03:20:50 +0000102 /* Setup the signal pipe */
103 udhcp_sp_setup();
104
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000105 timeout_end = monotonic_sec() + server_config.auto_time;
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000106 while (1) { /* loop until universe collapses */
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000107 int bytes;
108 struct timeval tv;
Mike Frysinger7031f622006-05-08 03:20:50 +0000109
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000110 if (server_socket < 0) {
Denis Vlasenkof1980f62008-09-26 09:34:59 +0000111 server_socket = udhcp_listen_socket(/*INADDR_ANY,*/ SERVER_PORT,
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000112 server_config.interface);
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000113 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000114
115 max_sock = udhcp_sp_fd_set(&rfds, server_socket);
116 if (server_config.auto_time) {
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000117 tv.tv_sec = timeout_end - monotonic_sec();
Mike Frysinger7031f622006-05-08 03:20:50 +0000118 tv.tv_usec = 0;
119 }
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000120 retval = 0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000121 if (!server_config.auto_time || tv.tv_sec > 0) {
122 retval = select(max_sock + 1, &rfds, NULL, NULL,
123 server_config.auto_time ? &tv : NULL);
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000124 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000125 if (retval == 0) {
126 write_leases();
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;
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000129 }
130 if (retval < 0 && errno != EINTR) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000131 DEBUG("error on select");
Mike Frysinger7031f622006-05-08 03:20:50 +0000132 continue;
133 }
134
135 switch (udhcp_sp_read(&rfds)) {
136 case SIGUSR1:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000137 bb_info_msg("Received a SIGUSR1");
Mike Frysinger7031f622006-05-08 03:20:50 +0000138 write_leases();
139 /* why not just reset the timeout, eh */
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000140 timeout_end = monotonic_sec() + server_config.auto_time;
Mike Frysinger7031f622006-05-08 03:20:50 +0000141 continue;
142 case SIGTERM:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000143 bb_info_msg("Received a SIGTERM");
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000144 goto ret0;
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000145 case 0: /* no signal: read a packet */
146 break;
147 default: /* signal or error (probably EINTR): back to select */
148 continue;
Mike Frysinger7031f622006-05-08 03:20:50 +0000149 }
150
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000151 bytes = udhcp_recv_kernel_packet(&packet, server_socket);
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000152 if (bytes < 0) {
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000153 /* bytes can also be -2 ("bad packet data") */
Mike Frysinger7031f622006-05-08 03:20:50 +0000154 if (bytes == -1 && errno != EINTR) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000155 DEBUG("error on read, %s, reopening socket", strerror(errno));
Mike Frysinger7031f622006-05-08 03:20:50 +0000156 close(server_socket);
157 server_socket = -1;
158 }
159 continue;
160 }
161
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000162 state = get_option(&packet, DHCP_MESSAGE_TYPE);
163 if (state == NULL) {
Denis Vlasenkoa9595882006-09-29 21:30:43 +0000164 bb_error_msg("cannot get option from packet, ignoring");
Mike Frysinger7031f622006-05-08 03:20:50 +0000165 continue;
166 }
167
168 /* Look for a static lease */
169 static_lease_ip = getIpByMac(server_config.static_leases, &packet.chaddr);
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000170 if (static_lease_ip) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000171 bb_info_msg("Found static lease: %x", static_lease_ip);
Mike Frysinger7031f622006-05-08 03:20:50 +0000172
173 memcpy(&static_lease.chaddr, &packet.chaddr, 16);
174 static_lease.yiaddr = static_lease_ip;
175 static_lease.expires = 0;
176
177 lease = &static_lease;
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000178 } else {
179 lease = find_lease_by_chaddr(packet.chaddr);
Mike Frysinger7031f622006-05-08 03:20:50 +0000180 }
181
182 switch (state[0]) {
183 case DHCPDISCOVER:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000184 DEBUG("Received DISCOVER");
Mike Frysinger7031f622006-05-08 03:20:50 +0000185
Denis Vlasenko6de89942008-05-21 07:05:06 +0000186 if (send_offer(&packet) < 0) {
Denis Vlasenkoa9595882006-09-29 21:30:43 +0000187 bb_error_msg("send OFFER failed");
Mike Frysinger7031f622006-05-08 03:20:50 +0000188 }
189 break;
190 case DHCPREQUEST:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000191 DEBUG("received REQUEST");
Mike Frysinger7031f622006-05-08 03:20:50 +0000192
193 requested = get_option(&packet, DHCP_REQUESTED_IP);
194 server_id = get_option(&packet, DHCP_SERVER_ID);
195
Denis Vlasenkoefb545b2008-12-08 22:56:18 +0000196 if (requested)
197 move_from_unaligned32(requested_aligned, requested);
198 if (server_id)
199 move_from_unaligned32(server_id_aligned, server_id);
Mike Frysinger7031f622006-05-08 03:20:50 +0000200
201 if (lease) {
202 if (server_id) {
203 /* SELECTING State */
Denis Vlasenkoefb545b2008-12-08 22:56:18 +0000204 DEBUG("server_id = %08x", ntohl(server_id_aligned));
205 if (server_id_aligned == server_config.server
206 && requested
207 && requested_aligned == lease->yiaddr
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000208 ) {
Denis Vlasenko6de89942008-05-21 07:05:06 +0000209 send_ACK(&packet, lease->yiaddr);
Mike Frysinger7031f622006-05-08 03:20:50 +0000210 }
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000211 } else if (requested) {
212 /* INIT-REBOOT State */
Denis Vlasenkoefb545b2008-12-08 22:56:18 +0000213 if (lease->yiaddr == requested_aligned)
Denis Vlasenko6de89942008-05-21 07:05:06 +0000214 send_ACK(&packet, lease->yiaddr);
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000215 else
Denis Vlasenko6de89942008-05-21 07:05:06 +0000216 send_NAK(&packet);
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000217 } else if (lease->yiaddr == packet.ciaddr) {
218 /* RENEWING or REBINDING State */
Denis Vlasenko6de89942008-05-21 07:05:06 +0000219 send_ACK(&packet, lease->yiaddr);
220 } else { /* don't know what to do!!!! */
221 send_NAK(&packet);
Mike Frysinger7031f622006-05-08 03:20:50 +0000222 }
223
224 /* what to do if we have no record of the client */
225 } else if (server_id) {
226 /* SELECTING State */
227
228 } else if (requested) {
229 /* INIT-REBOOT State */
Denis Vlasenkoefb545b2008-12-08 22:56:18 +0000230 lease = find_lease_by_yiaddr(requested_aligned);
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000231 if (lease) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000232 if (lease_expired(lease)) {
233 /* probably best if we drop this lease */
234 memset(lease->chaddr, 0, 16);
235 /* make some contention for this address */
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000236 } else
Denis Vlasenko6de89942008-05-21 07:05:06 +0000237 send_NAK(&packet);
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000238 } else {
Denis Vlasenkoefb545b2008-12-08 22:56:18 +0000239 uint32_t r = ntohl(requested_aligned);
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000240 if (r < server_config.start_ip
241 || r > server_config.end_ip
242 ) {
Denis Vlasenko6de89942008-05-21 07:05:06 +0000243 send_NAK(&packet);
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000244 }
Denis Vlasenkoe6bce972007-07-01 17:11:54 +0000245 /* else remain silent */
246 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000247
248 } else {
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000249 /* RENEWING or REBINDING State */
Mike Frysinger7031f622006-05-08 03:20:50 +0000250 }
251 break;
252 case DHCPDECLINE:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000253 DEBUG("Received DECLINE");
Mike Frysinger7031f622006-05-08 03:20:50 +0000254 if (lease) {
255 memset(lease->chaddr, 0, 16);
Denis Vlasenko04158e02009-02-02 10:48:06 +0000256 lease->expires = time(NULL) + server_config.decline_time;
Mike Frysinger7031f622006-05-08 03:20:50 +0000257 }
258 break;
259 case DHCPRELEASE:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000260 DEBUG("Received RELEASE");
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000261 if (lease)
Denis Vlasenko04158e02009-02-02 10:48:06 +0000262 lease->expires = time(NULL);
Mike Frysinger7031f622006-05-08 03:20:50 +0000263 break;
264 case DHCPINFORM:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000265 DEBUG("Received INFORM");
Mike Frysinger7031f622006-05-08 03:20:50 +0000266 send_inform(&packet);
267 break;
268 default:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000269 bb_info_msg("Unsupported DHCP message (%02x) - ignoring", state[0]);
Mike Frysinger7031f622006-05-08 03:20:50 +0000270 }
271 }
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000272 ret0:
273 retval = 0;
274 ret:
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000275 /*if (server_config.pidfile) - server_config.pidfile is never NULL */
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000276 remove_pidfile(server_config.pidfile);
277 return retval;
Mike Frysinger7031f622006-05-08 03:20:50 +0000278}