blob: 7b4596895ce97b82e96e74a54db67011becdc8b9 [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;
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;
Denis Vlasenkoefb545b2008-12-08 22:56:18 +000033 uint32_t server_id_aligned = server_id_aligned; /* for compiler */
34 uint32_t requested_aligned = requested_aligned;
35 uint32_t static_lease_ip;
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000036 unsigned timeout_end;
37 unsigned num_ips;
Denis Vlasenko3d17d2b2007-08-14 16:45:29 +000038 unsigned opt;
Mike Frysinger7031f622006-05-08 03:20:50 +000039 struct option_set *option;
Rob Landley9ffd4232006-05-21 18:30:35 +000040 struct dhcpOfferedAddr *lease, static_lease;
Denis Vlasenkod55fe3e2008-02-04 13:12:16 +000041 USE_FEATURE_UDHCP_PORT(char *str_P;)
Mike Frysinger7031f622006-05-08 03:20:50 +000042
Denis Vlasenkod55fe3e2008-02-04 13:12:16 +000043#if ENABLE_FEATURE_UDHCP_PORT
44 SERVER_PORT = 67;
45 CLIENT_PORT = 68;
46#endif
47
48 opt = getopt32(argv, "fS" USE_FEATURE_UDHCP_PORT("P:", &str_P));
Denis Vlasenko9f7b92a2007-08-15 20:03:36 +000049 argv += optind;
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +000050
Denis Vlasenko3d17d2b2007-08-14 16:45:29 +000051 if (!(opt & 1)) { /* no -f */
Denis Vlasenkoc82b5102007-07-01 17:05:57 +000052 bb_daemonize_or_rexec(0, argv);
Denis Vlasenko4b924f32007-05-30 00:29:55 +000053 logmode &= ~LOGMODE_STDIO;
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +000054 }
55
Denis Vlasenko3d17d2b2007-08-14 16:45:29 +000056 if (opt & 2) { /* -S */
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +000057 openlog(applet_name, LOG_PID, LOG_LOCAL0);
58 logmode |= LOGMODE_SYSLOG;
59 }
Denis Vlasenkod55fe3e2008-02-04 13:12:16 +000060#if ENABLE_FEATURE_UDHCP_PORT
61 if (opt & 4) { /* -P */
62 SERVER_PORT = xatou16(str_P);
63 CLIENT_PORT = SERVER_PORT + 1;
64 }
65#endif
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +000066 /* Would rather not do read_config before daemonization -
67 * otherwise NOMMU machines will parse config twice */
Denis Vlasenko9f7b92a2007-08-15 20:03:36 +000068 read_config(argv[0] ? argv[0] : DHCPD_CONF_FILE);
Mike Frysinger7031f622006-05-08 03:20:50 +000069
Denis Vlasenko80edead2007-08-02 22:31:05 +000070 /* Make sure fd 0,1,2 are open */
71 bb_sanitize_stdio();
72 /* Equivalent of doing a fflush after every \n */
73 setlinebuf(stdout);
74
75 /* Create pidfile */
76 write_pidfile(server_config.pidfile);
77 /* if (!..) bb_perror_msg("cannot create pidfile %s", pidfile); */
78
Denis Vlasenkodef88982007-10-07 17:06:01 +000079 bb_info_msg("%s (v"BB_VER") started", applet_name);
Mike Frysinger7031f622006-05-08 03:20:50 +000080
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +000081 option = find_option(server_config.options, DHCP_LEASE_TIME);
Denis Vlasenko6e6d3312007-05-03 23:39:35 +000082 server_config.lease = LEASE_TIME;
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +000083 if (option) {
Denis Vlasenkoefb545b2008-12-08 22:56:18 +000084 move_from_unaligned32(server_config.lease, option->data + 2);
Mike Frysinger7031f622006-05-08 03:20:50 +000085 server_config.lease = ntohl(server_config.lease);
Denis Vlasenko6e6d3312007-05-03 23:39:35 +000086 }
Mike Frysinger7031f622006-05-08 03:20:50 +000087
88 /* Sanity check */
Denis Vlasenkoc82b5102007-07-01 17:05:57 +000089 num_ips = server_config.end_ip - server_config.start_ip + 1;
Mike Frysinger7031f622006-05-08 03:20:50 +000090 if (server_config.max_leases > num_ips) {
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000091 bb_error_msg("max_leases=%u is too big, setting to %u",
92 (unsigned)server_config.max_leases, num_ips);
Mike Frysinger7031f622006-05-08 03:20:50 +000093 server_config.max_leases = num_ips;
94 }
95
Denis Vlasenkoc82b5102007-07-01 17:05:57 +000096 leases = xzalloc(server_config.max_leases * sizeof(*leases));
Mike Frysinger7031f622006-05-08 03:20:50 +000097 read_leases(server_config.lease_file);
98
Denis Vlasenkof1980f62008-09-26 09:34:59 +000099 if (udhcp_read_interface(server_config.interface, &server_config.ifindex,
Denis Vlasenko80edead2007-08-02 22:31:05 +0000100 &server_config.server, server_config.arp)) {
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000101 retval = 1;
102 goto ret;
103 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000104
Mike Frysinger7031f622006-05-08 03:20:50 +0000105 /* Setup the signal pipe */
106 udhcp_sp_setup();
107
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000108 timeout_end = monotonic_sec() + server_config.auto_time;
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000109 while (1) { /* loop until universe collapses */
Mike Frysinger7031f622006-05-08 03:20:50 +0000110
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000111 if (server_socket < 0) {
Denis Vlasenkof1980f62008-09-26 09:34:59 +0000112 server_socket = udhcp_listen_socket(/*INADDR_ANY,*/ SERVER_PORT,
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000113 server_config.interface);
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000114 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000115
116 max_sock = udhcp_sp_fd_set(&rfds, server_socket);
117 if (server_config.auto_time) {
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000118 tv.tv_sec = timeout_end - monotonic_sec();
Mike Frysinger7031f622006-05-08 03:20:50 +0000119 tv.tv_usec = 0;
120 }
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000121 retval = 0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000122 if (!server_config.auto_time || tv.tv_sec > 0) {
123 retval = select(max_sock + 1, &rfds, NULL, NULL,
124 server_config.auto_time ? &tv : NULL);
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000125 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000126 if (retval == 0) {
127 write_leases();
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000128 timeout_end = monotonic_sec() + server_config.auto_time;
Mike Frysinger7031f622006-05-08 03:20:50 +0000129 continue;
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000130 }
131 if (retval < 0 && errno != EINTR) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000132 DEBUG("error on select");
Mike Frysinger7031f622006-05-08 03:20:50 +0000133 continue;
134 }
135
136 switch (udhcp_sp_read(&rfds)) {
137 case SIGUSR1:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000138 bb_info_msg("Received a SIGUSR1");
Mike Frysinger7031f622006-05-08 03:20:50 +0000139 write_leases();
140 /* why not just reset the timeout, eh */
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000141 timeout_end = monotonic_sec() + server_config.auto_time;
Mike Frysinger7031f622006-05-08 03:20:50 +0000142 continue;
143 case SIGTERM:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000144 bb_info_msg("Received a SIGTERM");
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000145 goto ret0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000146 case 0: break; /* no signal */
147 default: continue; /* signal or error (probably EINTR) */
148 }
149
Denis Vlasenko6de89942008-05-21 07:05:06 +0000150 bytes = udhcp_recv_kernel_packet(&packet, server_socket); /* this waits for a packet - idle */
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000151 if (bytes < 0) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000152 if (bytes == -1 && errno != EINTR) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000153 DEBUG("error on read, %s, reopening socket", strerror(errno));
Mike Frysinger7031f622006-05-08 03:20:50 +0000154 close(server_socket);
155 server_socket = -1;
156 }
157 continue;
158 }
159
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000160 state = get_option(&packet, DHCP_MESSAGE_TYPE);
161 if (state == NULL) {
Denis Vlasenkoa9595882006-09-29 21:30:43 +0000162 bb_error_msg("cannot get option from packet, ignoring");
Mike Frysinger7031f622006-05-08 03:20:50 +0000163 continue;
164 }
165
166 /* Look for a static lease */
167 static_lease_ip = getIpByMac(server_config.static_leases, &packet.chaddr);
168
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000169 if (static_lease_ip) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000170 bb_info_msg("Found static lease: %x", static_lease_ip);
Mike Frysinger7031f622006-05-08 03:20:50 +0000171
172 memcpy(&static_lease.chaddr, &packet.chaddr, 16);
173 static_lease.yiaddr = static_lease_ip;
174 static_lease.expires = 0;
175
176 lease = &static_lease;
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000177 } else {
178 lease = find_lease_by_chaddr(packet.chaddr);
Mike Frysinger7031f622006-05-08 03:20:50 +0000179 }
180
181 switch (state[0]) {
182 case DHCPDISCOVER:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000183 DEBUG("Received DISCOVER");
Mike Frysinger7031f622006-05-08 03:20:50 +0000184
Denis Vlasenko6de89942008-05-21 07:05:06 +0000185 if (send_offer(&packet) < 0) {
Denis Vlasenkoa9595882006-09-29 21:30:43 +0000186 bb_error_msg("send OFFER failed");
Mike Frysinger7031f622006-05-08 03:20:50 +0000187 }
188 break;
189 case DHCPREQUEST:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000190 DEBUG("received REQUEST");
Mike Frysinger7031f622006-05-08 03:20:50 +0000191
192 requested = get_option(&packet, DHCP_REQUESTED_IP);
193 server_id = get_option(&packet, DHCP_SERVER_ID);
194
Denis Vlasenkoefb545b2008-12-08 22:56:18 +0000195 if (requested)
196 move_from_unaligned32(requested_aligned, requested);
197 if (server_id)
198 move_from_unaligned32(server_id_aligned, server_id);
Mike Frysinger7031f622006-05-08 03:20:50 +0000199
200 if (lease) {
201 if (server_id) {
202 /* SELECTING State */
Denis Vlasenkoefb545b2008-12-08 22:56:18 +0000203 DEBUG("server_id = %08x", ntohl(server_id_aligned));
204 if (server_id_aligned == server_config.server
205 && requested
206 && requested_aligned == lease->yiaddr
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000207 ) {
Denis Vlasenko6de89942008-05-21 07:05:06 +0000208 send_ACK(&packet, lease->yiaddr);
Mike Frysinger7031f622006-05-08 03:20:50 +0000209 }
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000210 } else if (requested) {
211 /* INIT-REBOOT State */
Denis Vlasenkoefb545b2008-12-08 22:56:18 +0000212 if (lease->yiaddr == requested_aligned)
Denis Vlasenko6de89942008-05-21 07:05:06 +0000213 send_ACK(&packet, lease->yiaddr);
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000214 else
Denis Vlasenko6de89942008-05-21 07:05:06 +0000215 send_NAK(&packet);
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000216 } else if (lease->yiaddr == packet.ciaddr) {
217 /* RENEWING or REBINDING State */
Denis Vlasenko6de89942008-05-21 07:05:06 +0000218 send_ACK(&packet, lease->yiaddr);
219 } else { /* don't know what to do!!!! */
220 send_NAK(&packet);
Mike Frysinger7031f622006-05-08 03:20:50 +0000221 }
222
223 /* what to do if we have no record of the client */
224 } else if (server_id) {
225 /* SELECTING State */
226
227 } else if (requested) {
228 /* INIT-REBOOT State */
Denis Vlasenkoefb545b2008-12-08 22:56:18 +0000229 lease = find_lease_by_yiaddr(requested_aligned);
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000230 if (lease) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000231 if (lease_expired(lease)) {
232 /* probably best if we drop this lease */
233 memset(lease->chaddr, 0, 16);
234 /* make some contention for this address */
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000235 } else
Denis Vlasenko6de89942008-05-21 07:05:06 +0000236 send_NAK(&packet);
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000237 } else {
Denis Vlasenkoefb545b2008-12-08 22:56:18 +0000238 uint32_t r = ntohl(requested_aligned);
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000239 if (r < server_config.start_ip
240 || r > server_config.end_ip
241 ) {
Denis Vlasenko6de89942008-05-21 07:05:06 +0000242 send_NAK(&packet);
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000243 }
Denis Vlasenkoe6bce972007-07-01 17:11:54 +0000244 /* else remain silent */
245 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000246
247 } else {
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000248 /* RENEWING or REBINDING State */
Mike Frysinger7031f622006-05-08 03:20:50 +0000249 }
250 break;
251 case DHCPDECLINE:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000252 DEBUG("Received DECLINE");
Mike Frysinger7031f622006-05-08 03:20:50 +0000253 if (lease) {
254 memset(lease->chaddr, 0, 16);
255 lease->expires = time(0) + server_config.decline_time;
256 }
257 break;
258 case DHCPRELEASE:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000259 DEBUG("Received RELEASE");
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000260 if (lease)
261 lease->expires = time(0);
Mike Frysinger7031f622006-05-08 03:20:50 +0000262 break;
263 case DHCPINFORM:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000264 DEBUG("Received INFORM");
Mike Frysinger7031f622006-05-08 03:20:50 +0000265 send_inform(&packet);
266 break;
267 default:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000268 bb_info_msg("Unsupported DHCP message (%02x) - ignoring", state[0]);
Mike Frysinger7031f622006-05-08 03:20:50 +0000269 }
270 }
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000271 ret0:
272 retval = 0;
273 ret:
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000274 /*if (server_config.pidfile) - server_config.pidfile is never NULL */
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000275 remove_pidfile(server_config.pidfile);
276 return retval;
Mike Frysinger7031f622006-05-08 03:20:50 +0000277}