blob: cbc9684018fce6baebc283672d9259b1cf6e44e7 [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
Denys Vlasenkoac906fa2009-06-17 11:54:52 +020047#if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1
48 opt_complementary = "vv";
49#endif
50 opt = getopt32(argv, "fSv"
51 IF_FEATURE_UDHCP_PORT("P:", &str_P)
52#if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1
53 , &dhcp_verbose
54#endif
55 );
Denis Vlasenko9f7b92a2007-08-15 20:03:36 +000056 argv += optind;
Denis Vlasenko3d17d2b2007-08-14 16:45:29 +000057 if (!(opt & 1)) { /* no -f */
Denis Vlasenkoc82b5102007-07-01 17:05:57 +000058 bb_daemonize_or_rexec(0, argv);
Denis Vlasenkoa19e6492009-03-11 14:40:00 +000059 logmode = LOGMODE_NONE;
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +000060 }
Denis Vlasenko3d17d2b2007-08-14 16:45:29 +000061 if (opt & 2) { /* -S */
Denis Vlasenko5e4fda02009-03-08 23:46:48 +000062 openlog(applet_name, LOG_PID, LOG_DAEMON);
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +000063 logmode |= LOGMODE_SYSLOG;
64 }
Denis Vlasenkod55fe3e2008-02-04 13:12:16 +000065#if ENABLE_FEATURE_UDHCP_PORT
66 if (opt & 4) { /* -P */
67 SERVER_PORT = xatou16(str_P);
68 CLIENT_PORT = SERVER_PORT + 1;
69 }
70#endif
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +000071 /* Would rather not do read_config before daemonization -
72 * otherwise NOMMU machines will parse config twice */
Denis Vlasenko9f7b92a2007-08-15 20:03:36 +000073 read_config(argv[0] ? argv[0] : DHCPD_CONF_FILE);
Mike Frysinger7031f622006-05-08 03:20:50 +000074
Denis Vlasenko80edead2007-08-02 22:31:05 +000075 /* Make sure fd 0,1,2 are open */
76 bb_sanitize_stdio();
77 /* Equivalent of doing a fflush after every \n */
78 setlinebuf(stdout);
79
80 /* Create pidfile */
81 write_pidfile(server_config.pidfile);
82 /* if (!..) bb_perror_msg("cannot create pidfile %s", pidfile); */
83
Denis Vlasenkodef88982007-10-07 17:06:01 +000084 bb_info_msg("%s (v"BB_VER") started", applet_name);
Mike Frysinger7031f622006-05-08 03:20:50 +000085
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +000086 option = find_option(server_config.options, DHCP_LEASE_TIME);
Denis Vlasenko6e6d3312007-05-03 23:39:35 +000087 server_config.lease = LEASE_TIME;
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +000088 if (option) {
Denis Vlasenkoefb545b2008-12-08 22:56:18 +000089 move_from_unaligned32(server_config.lease, option->data + 2);
Mike Frysinger7031f622006-05-08 03:20:50 +000090 server_config.lease = ntohl(server_config.lease);
Denis Vlasenko6e6d3312007-05-03 23:39:35 +000091 }
Mike Frysinger7031f622006-05-08 03:20:50 +000092
93 /* Sanity check */
Denis Vlasenkoc82b5102007-07-01 17:05:57 +000094 num_ips = server_config.end_ip - server_config.start_ip + 1;
Mike Frysinger7031f622006-05-08 03:20:50 +000095 if (server_config.max_leases > num_ips) {
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000096 bb_error_msg("max_leases=%u is too big, setting to %u",
97 (unsigned)server_config.max_leases, num_ips);
Mike Frysinger7031f622006-05-08 03:20:50 +000098 server_config.max_leases = num_ips;
99 }
100
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000101 leases = xzalloc(server_config.max_leases * sizeof(*leases));
Mike Frysinger7031f622006-05-08 03:20:50 +0000102 read_leases(server_config.lease_file);
103
Denys Vlasenko26918dd2009-06-16 12:04:23 +0200104 if (udhcp_read_interface(server_config.interface,
105 &server_config.ifindex,
106 &server_config.server_nip,
107 server_config.server_mac)
108 ) {
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000109 retval = 1;
110 goto ret;
111 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000112
Mike Frysinger7031f622006-05-08 03:20:50 +0000113 /* Setup the signal pipe */
114 udhcp_sp_setup();
115
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000116 timeout_end = monotonic_sec() + server_config.auto_time;
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000117 while (1) { /* loop until universe collapses */
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000118 int bytes;
119 struct timeval tv;
Mike Frysinger7031f622006-05-08 03:20:50 +0000120
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000121 if (server_socket < 0) {
Denis Vlasenkof1980f62008-09-26 09:34:59 +0000122 server_socket = udhcp_listen_socket(/*INADDR_ANY,*/ SERVER_PORT,
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000123 server_config.interface);
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000124 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000125
126 max_sock = udhcp_sp_fd_set(&rfds, server_socket);
127 if (server_config.auto_time) {
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000128 tv.tv_sec = timeout_end - monotonic_sec();
Mike Frysinger7031f622006-05-08 03:20:50 +0000129 tv.tv_usec = 0;
130 }
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000131 retval = 0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000132 if (!server_config.auto_time || tv.tv_sec > 0) {
133 retval = select(max_sock + 1, &rfds, NULL, NULL,
134 server_config.auto_time ? &tv : NULL);
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000135 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000136 if (retval == 0) {
137 write_leases();
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000138 timeout_end = monotonic_sec() + server_config.auto_time;
Mike Frysinger7031f622006-05-08 03:20:50 +0000139 continue;
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000140 }
141 if (retval < 0 && errno != EINTR) {
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200142 log1("Error on select");
Mike Frysinger7031f622006-05-08 03:20:50 +0000143 continue;
144 }
145
146 switch (udhcp_sp_read(&rfds)) {
147 case SIGUSR1:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000148 bb_info_msg("Received a SIGUSR1");
Mike Frysinger7031f622006-05-08 03:20:50 +0000149 write_leases();
150 /* why not just reset the timeout, eh */
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000151 timeout_end = monotonic_sec() + server_config.auto_time;
Mike Frysinger7031f622006-05-08 03:20:50 +0000152 continue;
153 case SIGTERM:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000154 bb_info_msg("Received a SIGTERM");
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000155 goto ret0;
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000156 case 0: /* no signal: read a packet */
157 break;
158 default: /* signal or error (probably EINTR): back to select */
159 continue;
Mike Frysinger7031f622006-05-08 03:20:50 +0000160 }
161
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000162 bytes = udhcp_recv_kernel_packet(&packet, server_socket);
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000163 if (bytes < 0) {
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000164 /* bytes can also be -2 ("bad packet data") */
Mike Frysinger7031f622006-05-08 03:20:50 +0000165 if (bytes == -1 && errno != EINTR) {
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200166 log1("Read error: %s, reopening socket", strerror(errno));
Mike Frysinger7031f622006-05-08 03:20:50 +0000167 close(server_socket);
168 server_socket = -1;
169 }
170 continue;
171 }
172
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000173 state = get_option(&packet, DHCP_MESSAGE_TYPE);
174 if (state == NULL) {
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200175 bb_error_msg("no message type option, ignoring packet");
Mike Frysinger7031f622006-05-08 03:20:50 +0000176 continue;
177 }
178
179 /* Look for a static lease */
Denys Vlasenkocab3a012009-06-16 12:03:12 +0200180 static_lease_ip = get_static_nip_by_mac(server_config.static_leases, &packet.chaddr);
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000181 if (static_lease_ip) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000182 bb_info_msg("Found static lease: %x", static_lease_ip);
Mike Frysinger7031f622006-05-08 03:20:50 +0000183
Denys Vlasenko1d924f52009-06-16 10:23:01 +0200184 memcpy(&static_lease.lease_mac16, &packet.chaddr, 16);
185 static_lease.lease_nip = static_lease_ip;
Mike Frysinger7031f622006-05-08 03:20:50 +0000186 static_lease.expires = 0;
187
188 lease = &static_lease;
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000189 } else {
190 lease = find_lease_by_chaddr(packet.chaddr);
Mike Frysinger7031f622006-05-08 03:20:50 +0000191 }
192
193 switch (state[0]) {
194 case DHCPDISCOVER:
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200195 log1("Received DISCOVER");
Mike Frysinger7031f622006-05-08 03:20:50 +0000196
Denis Vlasenko6de89942008-05-21 07:05:06 +0000197 if (send_offer(&packet) < 0) {
Denis Vlasenkoa9595882006-09-29 21:30:43 +0000198 bb_error_msg("send OFFER failed");
Mike Frysinger7031f622006-05-08 03:20:50 +0000199 }
200 break;
201 case DHCPREQUEST:
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200202 log1("Received REQUEST");
Mike Frysinger7031f622006-05-08 03:20:50 +0000203
204 requested = get_option(&packet, DHCP_REQUESTED_IP);
205 server_id = get_option(&packet, DHCP_SERVER_ID);
206
Denis Vlasenkoefb545b2008-12-08 22:56:18 +0000207 if (requested)
208 move_from_unaligned32(requested_aligned, requested);
209 if (server_id)
210 move_from_unaligned32(server_id_aligned, server_id);
Mike Frysinger7031f622006-05-08 03:20:50 +0000211
212 if (lease) {
213 if (server_id) {
214 /* SELECTING State */
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200215 log1("server_id = %08x", ntohl(server_id_aligned));
Denys Vlasenko990a6172009-06-16 10:23:55 +0200216 if (server_id_aligned == server_config.server_nip
Denis Vlasenkoefb545b2008-12-08 22:56:18 +0000217 && requested
Denys Vlasenko1d924f52009-06-16 10:23:01 +0200218 && requested_aligned == lease->lease_nip
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000219 ) {
Denys Vlasenko1d924f52009-06-16 10:23:01 +0200220 send_ACK(&packet, lease->lease_nip);
Mike Frysinger7031f622006-05-08 03:20:50 +0000221 }
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000222 } else if (requested) {
223 /* INIT-REBOOT State */
Denys Vlasenko1d924f52009-06-16 10:23:01 +0200224 if (lease->lease_nip == requested_aligned)
225 send_ACK(&packet, lease->lease_nip);
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000226 else
Denis Vlasenko6de89942008-05-21 07:05:06 +0000227 send_NAK(&packet);
Denys Vlasenko1d924f52009-06-16 10:23:01 +0200228 } else if (lease->lease_nip == packet.ciaddr) {
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000229 /* RENEWING or REBINDING State */
Denys Vlasenko1d924f52009-06-16 10:23:01 +0200230 send_ACK(&packet, lease->lease_nip);
Denis Vlasenko6de89942008-05-21 07:05:06 +0000231 } else { /* don't know what to do!!!! */
232 send_NAK(&packet);
Mike Frysinger7031f622006-05-08 03:20:50 +0000233 }
234
235 /* what to do if we have no record of the client */
236 } else if (server_id) {
237 /* SELECTING State */
238
239 } else if (requested) {
240 /* INIT-REBOOT State */
Denis Vlasenkoefb545b2008-12-08 22:56:18 +0000241 lease = find_lease_by_yiaddr(requested_aligned);
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000242 if (lease) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000243 if (lease_expired(lease)) {
244 /* probably best if we drop this lease */
Denys Vlasenko1d924f52009-06-16 10:23:01 +0200245 memset(lease->lease_mac16, 0, 16);
246 } else {
247 /* make some contention for this address */
Denis Vlasenko6de89942008-05-21 07:05:06 +0000248 send_NAK(&packet);
Denys Vlasenko1d924f52009-06-16 10:23:01 +0200249 }
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000250 } else {
Denis Vlasenkoefb545b2008-12-08 22:56:18 +0000251 uint32_t r = ntohl(requested_aligned);
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000252 if (r < server_config.start_ip
253 || r > server_config.end_ip
254 ) {
Denis Vlasenko6de89942008-05-21 07:05:06 +0000255 send_NAK(&packet);
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000256 }
Denis Vlasenkoe6bce972007-07-01 17:11:54 +0000257 /* else remain silent */
258 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000259
260 } else {
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000261 /* RENEWING or REBINDING State */
Mike Frysinger7031f622006-05-08 03:20:50 +0000262 }
263 break;
264 case DHCPDECLINE:
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200265 log1("Received DECLINE");
Mike Frysinger7031f622006-05-08 03:20:50 +0000266 if (lease) {
Denys Vlasenko1d924f52009-06-16 10:23:01 +0200267 memset(lease->lease_mac16, 0, 16);
Denis Vlasenko04158e02009-02-02 10:48:06 +0000268 lease->expires = time(NULL) + server_config.decline_time;
Mike Frysinger7031f622006-05-08 03:20:50 +0000269 }
270 break;
271 case DHCPRELEASE:
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200272 log1("Received RELEASE");
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000273 if (lease)
Denis Vlasenko04158e02009-02-02 10:48:06 +0000274 lease->expires = time(NULL);
Mike Frysinger7031f622006-05-08 03:20:50 +0000275 break;
276 case DHCPINFORM:
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200277 log1("Received INFORM");
Mike Frysinger7031f622006-05-08 03:20:50 +0000278 send_inform(&packet);
279 break;
280 default:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000281 bb_info_msg("Unsupported DHCP message (%02x) - ignoring", state[0]);
Mike Frysinger7031f622006-05-08 03:20:50 +0000282 }
283 }
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000284 ret0:
285 retval = 0;
286 ret:
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000287 /*if (server_config.pidfile) - server_config.pidfile is never NULL */
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000288 remove_pidfile(server_config.pidfile);
289 return retval;
Mike Frysinger7031f622006-05-08 03:20:50 +0000290}