blob: d85615349bcf420875a0e98c2361361afab5b577 [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 Vlasenko5a3395b2006-11-18 19:51:32 +000015#include "dhcpd.h"
16#include "options.h"
Mike Frysinger7031f622006-05-08 03:20:50 +000017
18
19/* globals */
20struct dhcpOfferedAddr *leases;
21struct server_config_t server_config;
22
23
Bernhard Reutner-Fischerfebe3c42007-04-04 20:52:03 +000024int udhcpd_main(int argc, char **argv);
25int udhcpd_main(int argc, char **argv)
Mike Frysinger7031f622006-05-08 03:20:50 +000026{
27 fd_set rfds;
28 struct timeval tv;
Rob Landley9ffd4232006-05-21 18:30:35 +000029 int server_socket = -1, bytes, 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;
32 uint32_t server_id_align, requested_align, static_lease_ip;
33 unsigned long timeout_end, num_ips;
Mike Frysinger7031f622006-05-08 03:20:50 +000034 struct option_set *option;
Rob Landley9ffd4232006-05-21 18:30:35 +000035 struct dhcpOfferedAddr *lease, static_lease;
Mike Frysinger7031f622006-05-08 03:20:50 +000036
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +000037//Huh, dhcpd don't have --foreground, --syslog options?? TODO
38
39 if (!ENABLE_FEATURE_UDHCP_DEBUG) {
40 bb_daemonize_or_rexec(DAEMON_CHDIR_ROOT, argv);
41 logmode &= ~LOGMODE_STDIO;
42 }
43
44 if (ENABLE_FEATURE_UDHCP_SYSLOG) {
45 openlog(applet_name, LOG_PID, LOG_LOCAL0);
46 logmode |= LOGMODE_SYSLOG;
47 }
48
49 /* Would rather not do read_config before daemonization -
50 * otherwise NOMMU machines will parse config twice */
Mike Frysinger7031f622006-05-08 03:20:50 +000051 read_config(argc < 2 ? DHCPD_CONF_FILE : argv[1]);
52
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +000053 udhcp_make_pidfile(server_config.pidfile);
Mike Frysinger7031f622006-05-08 03:20:50 +000054
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +000055 option = find_option(server_config.options, DHCP_LEASE_TIME);
56 if (option) {
Mike Frysinger7031f622006-05-08 03:20:50 +000057 memcpy(&server_config.lease, option->data + 2, 4);
58 server_config.lease = ntohl(server_config.lease);
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +000059 } else
60 server_config.lease = LEASE_TIME;
Mike Frysinger7031f622006-05-08 03:20:50 +000061
62 /* Sanity check */
63 num_ips = ntohl(server_config.end) - ntohl(server_config.start) + 1;
64 if (server_config.max_leases > num_ips) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000065 bb_error_msg("max_leases value (%lu) not sane, "
Mike Frysinger7031f622006-05-08 03:20:50 +000066 "setting to %lu instead",
67 server_config.max_leases, num_ips);
68 server_config.max_leases = num_ips;
69 }
70
Rob Landley9ffd4232006-05-21 18:30:35 +000071 leases = xzalloc(server_config.max_leases * sizeof(struct dhcpOfferedAddr));
Mike Frysinger7031f622006-05-08 03:20:50 +000072 read_leases(server_config.lease_file);
73
74 if (read_interface(server_config.interface, &server_config.ifindex,
75 &server_config.server, server_config.arp) < 0)
76 return 1;
77
Mike Frysinger7031f622006-05-08 03:20:50 +000078 /* Setup the signal pipe */
79 udhcp_sp_setup();
80
81 timeout_end = time(0) + server_config.auto_time;
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000082 while (1) { /* loop until universe collapses */
Mike Frysinger7031f622006-05-08 03:20:50 +000083
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +000084 if (server_socket < 0) {
85 server_socket = listen_socket(INADDR_ANY, SERVER_PORT, server_config.interface);
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +000086 }
Mike Frysinger7031f622006-05-08 03:20:50 +000087
88 max_sock = udhcp_sp_fd_set(&rfds, server_socket);
89 if (server_config.auto_time) {
90 tv.tv_sec = timeout_end - time(0);
91 tv.tv_usec = 0;
92 }
93 if (!server_config.auto_time || tv.tv_sec > 0) {
94 retval = select(max_sock + 1, &rfds, NULL, NULL,
95 server_config.auto_time ? &tv : NULL);
96 } else retval = 0; /* If we already timed out, fall through */
97
98 if (retval == 0) {
99 write_leases();
100 timeout_end = time(0) + server_config.auto_time;
101 continue;
102 } else if (retval < 0 && errno != EINTR) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000103 DEBUG("error on select");
Mike Frysinger7031f622006-05-08 03:20:50 +0000104 continue;
105 }
106
107 switch (udhcp_sp_read(&rfds)) {
108 case SIGUSR1:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000109 bb_info_msg("Received a SIGUSR1");
Mike Frysinger7031f622006-05-08 03:20:50 +0000110 write_leases();
111 /* why not just reset the timeout, eh */
112 timeout_end = time(0) + server_config.auto_time;
113 continue;
114 case SIGTERM:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000115 bb_info_msg("Received a SIGTERM");
Mike Frysinger7031f622006-05-08 03:20:50 +0000116 return 0;
117 case 0: break; /* no signal */
118 default: continue; /* signal or error (probably EINTR) */
119 }
120
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000121 bytes = udhcp_get_packet(&packet, server_socket); /* this waits for a packet - idle */
122 if (bytes < 0) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000123 if (bytes == -1 && errno != EINTR) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000124 DEBUG("error on read, %s, reopening socket", strerror(errno));
Mike Frysinger7031f622006-05-08 03:20:50 +0000125 close(server_socket);
126 server_socket = -1;
127 }
128 continue;
129 }
130
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000131 state = get_option(&packet, DHCP_MESSAGE_TYPE);
132 if (state == NULL) {
Denis Vlasenkoa9595882006-09-29 21:30:43 +0000133 bb_error_msg("cannot get option from packet, ignoring");
Mike Frysinger7031f622006-05-08 03:20:50 +0000134 continue;
135 }
136
137 /* Look for a static lease */
138 static_lease_ip = getIpByMac(server_config.static_leases, &packet.chaddr);
139
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000140 if (static_lease_ip) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000141 bb_info_msg("Found static lease: %x", static_lease_ip);
Mike Frysinger7031f622006-05-08 03:20:50 +0000142
143 memcpy(&static_lease.chaddr, &packet.chaddr, 16);
144 static_lease.yiaddr = static_lease_ip;
145 static_lease.expires = 0;
146
147 lease = &static_lease;
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000148 } else {
149 lease = find_lease_by_chaddr(packet.chaddr);
Mike Frysinger7031f622006-05-08 03:20:50 +0000150 }
151
152 switch (state[0]) {
153 case DHCPDISCOVER:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000154 DEBUG("Received DISCOVER");
Mike Frysinger7031f622006-05-08 03:20:50 +0000155
156 if (sendOffer(&packet) < 0) {
Denis Vlasenkoa9595882006-09-29 21:30:43 +0000157 bb_error_msg("send OFFER failed");
Mike Frysinger7031f622006-05-08 03:20:50 +0000158 }
159 break;
160 case DHCPREQUEST:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000161 DEBUG("received REQUEST");
Mike Frysinger7031f622006-05-08 03:20:50 +0000162
163 requested = get_option(&packet, DHCP_REQUESTED_IP);
164 server_id = get_option(&packet, DHCP_SERVER_ID);
165
166 if (requested) memcpy(&requested_align, requested, 4);
167 if (server_id) memcpy(&server_id_align, server_id, 4);
168
169 if (lease) {
170 if (server_id) {
171 /* SELECTING State */
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000172 DEBUG("server_id = %08x", ntohl(server_id_align));
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000173 if (server_id_align == server_config.server && requested
174 && requested_align == lease->yiaddr
175 ) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000176 sendACK(&packet, lease->yiaddr);
177 }
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000178 } else if (requested) {
179 /* INIT-REBOOT State */
180 if (lease->yiaddr == requested_align)
181 sendACK(&packet, lease->yiaddr);
182 else
183 sendNAK(&packet);
184 } else if (lease->yiaddr == packet.ciaddr) {
185 /* RENEWING or REBINDING State */
186 sendACK(&packet, lease->yiaddr);
Mike Frysinger7031f622006-05-08 03:20:50 +0000187 } else {
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000188 /* don't know what to do!!!! */
189 sendNAK(&packet);
Mike Frysinger7031f622006-05-08 03:20:50 +0000190 }
191
192 /* what to do if we have no record of the client */
193 } else if (server_id) {
194 /* SELECTING State */
195
196 } else if (requested) {
197 /* INIT-REBOOT State */
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000198 lease = find_lease_by_yiaddr(requested_align);
199 if (lease) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000200 if (lease_expired(lease)) {
201 /* probably best if we drop this lease */
202 memset(lease->chaddr, 0, 16);
203 /* make some contention for this address */
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000204 } else
205 sendNAK(&packet);
206 } else if (requested_align < server_config.start
207 || requested_align > server_config.end
208 ) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000209 sendNAK(&packet);
210 } /* else remain silent */
211
212 } else {
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000213 /* RENEWING or REBINDING State */
Mike Frysinger7031f622006-05-08 03:20:50 +0000214 }
215 break;
216 case DHCPDECLINE:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000217 DEBUG("Received DECLINE");
Mike Frysinger7031f622006-05-08 03:20:50 +0000218 if (lease) {
219 memset(lease->chaddr, 0, 16);
220 lease->expires = time(0) + server_config.decline_time;
221 }
222 break;
223 case DHCPRELEASE:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000224 DEBUG("Received RELEASE");
Mike Frysinger7031f622006-05-08 03:20:50 +0000225 if (lease) lease->expires = time(0);
226 break;
227 case DHCPINFORM:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000228 DEBUG("Received INFORM");
Mike Frysinger7031f622006-05-08 03:20:50 +0000229 send_inform(&packet);
230 break;
231 default:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000232 bb_info_msg("Unsupported DHCP message (%02x) - ignoring", state[0]);
Mike Frysinger7031f622006-05-08 03:20:50 +0000233 }
234 }
235
236 return 0;
237}