blob: 23292a55d7ea606a686bfa47d7f6545e7cbba4b2 [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 */
Denys Vlasenko6947d2c2009-06-17 13:24:03 +020021struct dyn_lease *g_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;
Denys Vlasenko31af3d52009-06-17 11:57:09 +020030 struct dhcp_packet packet;
Denys Vlasenko6947d2c2009-06-17 13:24:03 +020031 uint8_t *state;
Denis Vlasenkoefb545b2008-12-08 22:56:18 +000032 uint32_t 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;
Denys Vlasenko6947d2c2009-06-17 13:24:03 +020037 struct dyn_lease *lease, fake_lease;
Denis Vlasenko5e34ff22009-04-21 11:09:40 +000038 IF_FEATURE_UDHCP_PORT(char *str_P;)
Mike Frysinger7031f622006-05-08 03:20:50 +000039
Denis Vlasenkod55fe3e2008-02-04 13:12:16 +000040#if ENABLE_FEATURE_UDHCP_PORT
41 SERVER_PORT = 67;
42 CLIENT_PORT = 68;
43#endif
44
Denys Vlasenkoac906fa2009-06-17 11:54:52 +020045#if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1
46 opt_complementary = "vv";
47#endif
48 opt = getopt32(argv, "fSv"
49 IF_FEATURE_UDHCP_PORT("P:", &str_P)
50#if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1
51 , &dhcp_verbose
52#endif
53 );
Denis Vlasenko9f7b92a2007-08-15 20:03:36 +000054 argv += optind;
Denis Vlasenko3d17d2b2007-08-14 16:45:29 +000055 if (!(opt & 1)) { /* no -f */
Denis Vlasenkoc82b5102007-07-01 17:05:57 +000056 bb_daemonize_or_rexec(0, argv);
Denis Vlasenkoa19e6492009-03-11 14:40:00 +000057 logmode = LOGMODE_NONE;
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +000058 }
Denis Vlasenko3d17d2b2007-08-14 16:45:29 +000059 if (opt & 2) { /* -S */
Denis Vlasenko5e4fda02009-03-08 23:46:48 +000060 openlog(applet_name, LOG_PID, LOG_DAEMON);
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +000061 logmode |= LOGMODE_SYSLOG;
62 }
Denis Vlasenkod55fe3e2008-02-04 13:12:16 +000063#if ENABLE_FEATURE_UDHCP_PORT
64 if (opt & 4) { /* -P */
65 SERVER_PORT = xatou16(str_P);
66 CLIENT_PORT = SERVER_PORT + 1;
67 }
68#endif
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +000069 /* Would rather not do read_config before daemonization -
70 * otherwise NOMMU machines will parse config twice */
Denis Vlasenko9f7b92a2007-08-15 20:03:36 +000071 read_config(argv[0] ? argv[0] : DHCPD_CONF_FILE);
Mike Frysinger7031f622006-05-08 03:20:50 +000072
Denis Vlasenko80edead2007-08-02 22:31:05 +000073 /* Make sure fd 0,1,2 are open */
74 bb_sanitize_stdio();
75 /* Equivalent of doing a fflush after every \n */
76 setlinebuf(stdout);
77
78 /* Create pidfile */
79 write_pidfile(server_config.pidfile);
80 /* if (!..) bb_perror_msg("cannot create pidfile %s", pidfile); */
81
Denis Vlasenkodef88982007-10-07 17:06:01 +000082 bb_info_msg("%s (v"BB_VER") started", applet_name);
Mike Frysinger7031f622006-05-08 03:20:50 +000083
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +000084 option = find_option(server_config.options, DHCP_LEASE_TIME);
Denys Vlasenko6947d2c2009-06-17 13:24:03 +020085 server_config.max_lease_sec = LEASE_TIME;
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +000086 if (option) {
Denys Vlasenko6947d2c2009-06-17 13:24:03 +020087 move_from_unaligned32(server_config.max_lease_sec, option->data + OPT_DATA);
88 server_config.max_lease_sec = ntohl(server_config.max_lease_sec);
Denis Vlasenko6e6d3312007-05-03 23:39:35 +000089 }
Mike Frysinger7031f622006-05-08 03:20:50 +000090
91 /* Sanity check */
Denis Vlasenkoc82b5102007-07-01 17:05:57 +000092 num_ips = server_config.end_ip - server_config.start_ip + 1;
Mike Frysinger7031f622006-05-08 03:20:50 +000093 if (server_config.max_leases > num_ips) {
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000094 bb_error_msg("max_leases=%u is too big, setting to %u",
95 (unsigned)server_config.max_leases, num_ips);
Mike Frysinger7031f622006-05-08 03:20:50 +000096 server_config.max_leases = num_ips;
97 }
98
Denys Vlasenko6947d2c2009-06-17 13:24:03 +020099 g_leases = xzalloc(server_config.max_leases * sizeof(g_leases[0]));
Mike Frysinger7031f622006-05-08 03:20:50 +0000100 read_leases(server_config.lease_file);
101
Denys Vlasenko26918dd2009-06-16 12:04:23 +0200102 if (udhcp_read_interface(server_config.interface,
103 &server_config.ifindex,
104 &server_config.server_nip,
105 server_config.server_mac)
106 ) {
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000107 retval = 1;
108 goto ret;
109 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000110
Mike Frysinger7031f622006-05-08 03:20:50 +0000111 /* Setup the signal pipe */
112 udhcp_sp_setup();
113
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000114 timeout_end = monotonic_sec() + server_config.auto_time;
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000115 while (1) { /* loop until universe collapses */
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000116 int bytes;
117 struct timeval tv;
Mike Frysinger7031f622006-05-08 03:20:50 +0000118
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000119 if (server_socket < 0) {
Denis Vlasenkof1980f62008-09-26 09:34:59 +0000120 server_socket = udhcp_listen_socket(/*INADDR_ANY,*/ SERVER_PORT,
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000121 server_config.interface);
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000122 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000123
124 max_sock = udhcp_sp_fd_set(&rfds, server_socket);
125 if (server_config.auto_time) {
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000126 tv.tv_sec = timeout_end - monotonic_sec();
Mike Frysinger7031f622006-05-08 03:20:50 +0000127 tv.tv_usec = 0;
128 }
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000129 retval = 0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000130 if (!server_config.auto_time || tv.tv_sec > 0) {
131 retval = select(max_sock + 1, &rfds, NULL, NULL,
132 server_config.auto_time ? &tv : NULL);
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000133 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000134 if (retval == 0) {
135 write_leases();
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000136 timeout_end = monotonic_sec() + server_config.auto_time;
Mike Frysinger7031f622006-05-08 03:20:50 +0000137 continue;
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000138 }
139 if (retval < 0 && errno != EINTR) {
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200140 log1("Error on select");
Mike Frysinger7031f622006-05-08 03:20:50 +0000141 continue;
142 }
143
144 switch (udhcp_sp_read(&rfds)) {
145 case SIGUSR1:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000146 bb_info_msg("Received a SIGUSR1");
Mike Frysinger7031f622006-05-08 03:20:50 +0000147 write_leases();
148 /* why not just reset the timeout, eh */
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000149 timeout_end = monotonic_sec() + server_config.auto_time;
Mike Frysinger7031f622006-05-08 03:20:50 +0000150 continue;
151 case SIGTERM:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000152 bb_info_msg("Received a SIGTERM");
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000153 goto ret0;
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000154 case 0: /* no signal: read a packet */
155 break;
156 default: /* signal or error (probably EINTR): back to select */
157 continue;
Mike Frysinger7031f622006-05-08 03:20:50 +0000158 }
159
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000160 bytes = udhcp_recv_kernel_packet(&packet, server_socket);
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000161 if (bytes < 0) {
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000162 /* bytes can also be -2 ("bad packet data") */
Mike Frysinger7031f622006-05-08 03:20:50 +0000163 if (bytes == -1 && errno != EINTR) {
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200164 log1("Read error: %s, reopening socket", strerror(errno));
Mike Frysinger7031f622006-05-08 03:20:50 +0000165 close(server_socket);
166 server_socket = -1;
167 }
168 continue;
169 }
170
Denys Vlasenko31af3d52009-06-17 11:57:09 +0200171 if (packet.hlen != 6) {
172 bb_error_msg("MAC length != 6, ignoring packet");
173 continue;
174 }
175
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000176 state = get_option(&packet, DHCP_MESSAGE_TYPE);
177 if (state == NULL) {
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200178 bb_error_msg("no message type option, ignoring packet");
Mike Frysinger7031f622006-05-08 03:20:50 +0000179 continue;
180 }
181
182 /* Look for a static lease */
Denys Vlasenkocab3a012009-06-16 12:03:12 +0200183 static_lease_ip = get_static_nip_by_mac(server_config.static_leases, &packet.chaddr);
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000184 if (static_lease_ip) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000185 bb_info_msg("Found static lease: %x", static_lease_ip);
Mike Frysinger7031f622006-05-08 03:20:50 +0000186
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200187 memcpy(&fake_lease.lease_mac, &packet.chaddr, 6);
188 fake_lease.lease_nip = static_lease_ip;
189 fake_lease.expires = 0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000190
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200191 lease = &fake_lease;
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000192 } else {
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200193 lease = find_lease_by_mac(packet.chaddr);
Mike Frysinger7031f622006-05-08 03:20:50 +0000194 }
195
196 switch (state[0]) {
197 case DHCPDISCOVER:
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200198 log1("Received DISCOVER");
Mike Frysinger7031f622006-05-08 03:20:50 +0000199
Denis Vlasenko6de89942008-05-21 07:05:06 +0000200 if (send_offer(&packet) < 0) {
Denis Vlasenkoa9595882006-09-29 21:30:43 +0000201 bb_error_msg("send OFFER failed");
Mike Frysinger7031f622006-05-08 03:20:50 +0000202 }
203 break;
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200204 case DHCPREQUEST: {
205 uint8_t *server_id_opt, *requested_opt;
206 uint32_t server_id_net = server_id_net; /* for compiler */
207 uint32_t requested_nip = requested_nip; /* for compiler */
208
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200209 log1("Received REQUEST");
Mike Frysinger7031f622006-05-08 03:20:50 +0000210
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200211 requested_opt = get_option(&packet, DHCP_REQUESTED_IP);
212 server_id_opt = get_option(&packet, DHCP_SERVER_ID);
213 if (requested_opt)
214 move_from_unaligned32(requested_nip, requested_opt);
215 if (server_id_opt)
216 move_from_unaligned32(server_id_net, server_id_opt);
Mike Frysinger7031f622006-05-08 03:20:50 +0000217
218 if (lease) {
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200219 if (server_id_opt) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000220 /* SELECTING State */
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200221 if (server_id_net == server_config.server_nip
222 && requested_opt
223 && requested_nip == lease->lease_nip
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000224 ) {
Denys Vlasenko1d924f52009-06-16 10:23:01 +0200225 send_ACK(&packet, lease->lease_nip);
Mike Frysinger7031f622006-05-08 03:20:50 +0000226 }
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200227 } else if (requested_opt) {
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000228 /* INIT-REBOOT State */
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200229 if (lease->lease_nip == requested_nip)
Denys Vlasenko1d924f52009-06-16 10:23:01 +0200230 send_ACK(&packet, lease->lease_nip);
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000231 else
Denis Vlasenko6de89942008-05-21 07:05:06 +0000232 send_NAK(&packet);
Denys Vlasenko1d924f52009-06-16 10:23:01 +0200233 } else if (lease->lease_nip == packet.ciaddr) {
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000234 /* RENEWING or REBINDING State */
Denys Vlasenko1d924f52009-06-16 10:23:01 +0200235 send_ACK(&packet, lease->lease_nip);
Denis Vlasenko6de89942008-05-21 07:05:06 +0000236 } else { /* don't know what to do!!!! */
237 send_NAK(&packet);
Mike Frysinger7031f622006-05-08 03:20:50 +0000238 }
239
240 /* what to do if we have no record of the client */
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200241 } else if (server_id_opt) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000242 /* SELECTING State */
243
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200244 } else if (requested_opt) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000245 /* INIT-REBOOT State */
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200246 lease = find_lease_by_nip(requested_nip);
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000247 if (lease) {
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200248 if (is_expired_lease(lease)) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000249 /* probably best if we drop this lease */
Denys Vlasenko31af3d52009-06-17 11:57:09 +0200250 memset(lease->lease_mac, 0, sizeof(lease->lease_mac));
Denys Vlasenko1d924f52009-06-16 10:23:01 +0200251 } else {
252 /* make some contention for this address */
Denis Vlasenko6de89942008-05-21 07:05:06 +0000253 send_NAK(&packet);
Denys Vlasenko1d924f52009-06-16 10:23:01 +0200254 }
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000255 } else {
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200256 uint32_t r = ntohl(requested_nip);
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000257 if (r < server_config.start_ip
258 || r > server_config.end_ip
259 ) {
Denis Vlasenko6de89942008-05-21 07:05:06 +0000260 send_NAK(&packet);
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000261 }
Denis Vlasenkoe6bce972007-07-01 17:11:54 +0000262 /* else remain silent */
263 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000264
265 } else {
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000266 /* RENEWING or REBINDING State */
Mike Frysinger7031f622006-05-08 03:20:50 +0000267 }
268 break;
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200269 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000270 case DHCPDECLINE:
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200271 log1("Received DECLINE");
Mike Frysinger7031f622006-05-08 03:20:50 +0000272 if (lease) {
Denys Vlasenko31af3d52009-06-17 11:57:09 +0200273 memset(lease->lease_mac, 0, sizeof(lease->lease_mac));
Denis Vlasenko04158e02009-02-02 10:48:06 +0000274 lease->expires = time(NULL) + server_config.decline_time;
Mike Frysinger7031f622006-05-08 03:20:50 +0000275 }
276 break;
277 case DHCPRELEASE:
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200278 log1("Received RELEASE");
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000279 if (lease)
Denis Vlasenko04158e02009-02-02 10:48:06 +0000280 lease->expires = time(NULL);
Mike Frysinger7031f622006-05-08 03:20:50 +0000281 break;
282 case DHCPINFORM:
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200283 log1("Received INFORM");
Mike Frysinger7031f622006-05-08 03:20:50 +0000284 send_inform(&packet);
285 break;
286 default:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000287 bb_info_msg("Unsupported DHCP message (%02x) - ignoring", state[0]);
Mike Frysinger7031f622006-05-08 03:20:50 +0000288 }
289 }
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000290 ret0:
291 retval = 0;
292 ret:
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000293 /*if (server_config.pidfile) - server_config.pidfile is never NULL */
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000294 remove_pidfile(server_config.pidfile);
295 return retval;
Mike Frysinger7031f622006-05-08 03:20:50 +0000296}