1 /* vi: set sw=4 ts=4: */
5 * Copyright (C) 1999 Matthew Ramsay <matthewr@moreton.com.au>
6 * Chris Trew <ctrew@moreton.com.au>
8 * Rewrite by Russ Dill <Russ.Dill@asu.edu> July 2001
10 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
21 struct dyn_lease *leases;
22 /* struct server_config_t server_config is in bb_common_bufsiz1 */
25 int udhcpd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
26 int udhcpd_main(int argc UNUSED_PARAM, char **argv)
29 int server_socket = -1, retval, max_sock;
30 struct dhcp_packet packet;
31 uint8_t *state, *server_id, *requested;
32 uint32_t server_id_aligned = server_id_aligned; /* for compiler */
33 uint32_t requested_aligned = requested_aligned;
34 uint32_t static_lease_ip;
38 struct option_set *option;
39 struct dyn_lease *lease, static_lease;
40 IF_FEATURE_UDHCP_PORT(char *str_P;)
42 #if ENABLE_FEATURE_UDHCP_PORT
47 #if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1
48 opt_complementary = "vv";
50 opt = getopt32(argv, "fSv"
51 IF_FEATURE_UDHCP_PORT("P:", &str_P)
52 #if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1
57 if (!(opt & 1)) { /* no -f */
58 bb_daemonize_or_rexec(0, argv);
59 logmode = LOGMODE_NONE;
61 if (opt & 2) { /* -S */
62 openlog(applet_name, LOG_PID, LOG_DAEMON);
63 logmode |= LOGMODE_SYSLOG;
65 #if ENABLE_FEATURE_UDHCP_PORT
66 if (opt & 4) { /* -P */
67 SERVER_PORT = xatou16(str_P);
68 CLIENT_PORT = SERVER_PORT + 1;
71 /* Would rather not do read_config before daemonization -
72 * otherwise NOMMU machines will parse config twice */
73 read_config(argv[0] ? argv[0] : DHCPD_CONF_FILE);
75 /* Make sure fd 0,1,2 are open */
77 /* Equivalent of doing a fflush after every \n */
81 write_pidfile(server_config.pidfile);
82 /* if (!..) bb_perror_msg("cannot create pidfile %s", pidfile); */
84 bb_info_msg("%s (v"BB_VER") started", applet_name);
86 option = find_option(server_config.options, DHCP_LEASE_TIME);
87 server_config.lease = LEASE_TIME;
89 move_from_unaligned32(server_config.lease, option->data + 2);
90 server_config.lease = ntohl(server_config.lease);
94 num_ips = server_config.end_ip - server_config.start_ip + 1;
95 if (server_config.max_leases > num_ips) {
96 bb_error_msg("max_leases=%u is too big, setting to %u",
97 (unsigned)server_config.max_leases, num_ips);
98 server_config.max_leases = num_ips;
101 leases = xzalloc(server_config.max_leases * sizeof(*leases));
102 read_leases(server_config.lease_file);
104 if (udhcp_read_interface(server_config.interface,
105 &server_config.ifindex,
106 &server_config.server_nip,
107 server_config.server_mac)
113 /* Setup the signal pipe */
116 timeout_end = monotonic_sec() + server_config.auto_time;
117 while (1) { /* loop until universe collapses */
121 if (server_socket < 0) {
122 server_socket = udhcp_listen_socket(/*INADDR_ANY,*/ SERVER_PORT,
123 server_config.interface);
126 max_sock = udhcp_sp_fd_set(&rfds, server_socket);
127 if (server_config.auto_time) {
128 tv.tv_sec = timeout_end - monotonic_sec();
132 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);
138 timeout_end = monotonic_sec() + server_config.auto_time;
141 if (retval < 0 && errno != EINTR) {
142 log1("Error on select");
146 switch (udhcp_sp_read(&rfds)) {
148 bb_info_msg("Received a SIGUSR1");
150 /* why not just reset the timeout, eh */
151 timeout_end = monotonic_sec() + server_config.auto_time;
154 bb_info_msg("Received a SIGTERM");
156 case 0: /* no signal: read a packet */
158 default: /* signal or error (probably EINTR): back to select */
162 bytes = udhcp_recv_kernel_packet(&packet, server_socket);
164 /* bytes can also be -2 ("bad packet data") */
165 if (bytes == -1 && errno != EINTR) {
166 log1("Read error: %s, reopening socket", strerror(errno));
167 close(server_socket);
173 if (packet.hlen != 6) {
174 bb_error_msg("MAC length != 6, ignoring packet");
178 state = get_option(&packet, DHCP_MESSAGE_TYPE);
180 bb_error_msg("no message type option, ignoring packet");
184 /* Look for a static lease */
185 static_lease_ip = get_static_nip_by_mac(server_config.static_leases, &packet.chaddr);
186 if (static_lease_ip) {
187 bb_info_msg("Found static lease: %x", static_lease_ip);
189 memcpy(&static_lease.lease_mac, &packet.chaddr, 6);
190 static_lease.lease_nip = static_lease_ip;
191 static_lease.expires = 0;
193 lease = &static_lease;
195 lease = find_lease_by_chaddr(packet.chaddr);
200 log1("Received DISCOVER");
202 if (send_offer(&packet) < 0) {
203 bb_error_msg("send OFFER failed");
207 log1("Received REQUEST");
209 requested = get_option(&packet, DHCP_REQUESTED_IP);
210 server_id = get_option(&packet, DHCP_SERVER_ID);
213 move_from_unaligned32(requested_aligned, requested);
215 move_from_unaligned32(server_id_aligned, server_id);
219 /* SELECTING State */
220 if (server_id_aligned == server_config.server_nip
222 && requested_aligned == lease->lease_nip
224 send_ACK(&packet, lease->lease_nip);
226 } else if (requested) {
227 /* INIT-REBOOT State */
228 if (lease->lease_nip == requested_aligned)
229 send_ACK(&packet, lease->lease_nip);
232 } else if (lease->lease_nip == packet.ciaddr) {
233 /* RENEWING or REBINDING State */
234 send_ACK(&packet, lease->lease_nip);
235 } else { /* don't know what to do!!!! */
239 /* what to do if we have no record of the client */
240 } else if (server_id) {
241 /* SELECTING State */
243 } else if (requested) {
244 /* INIT-REBOOT State */
245 lease = find_lease_by_yiaddr(requested_aligned);
247 if (lease_expired(lease)) {
248 /* probably best if we drop this lease */
249 memset(lease->lease_mac, 0, sizeof(lease->lease_mac));
251 /* make some contention for this address */
255 uint32_t r = ntohl(requested_aligned);
256 if (r < server_config.start_ip
257 || r > server_config.end_ip
261 /* else remain silent */
265 /* RENEWING or REBINDING State */
269 log1("Received DECLINE");
271 memset(lease->lease_mac, 0, sizeof(lease->lease_mac));
272 lease->expires = time(NULL) + server_config.decline_time;
276 log1("Received RELEASE");
278 lease->expires = time(NULL);
281 log1("Received INFORM");
282 send_inform(&packet);
285 bb_info_msg("Unsupported DHCP message (%02x) - ignoring", state[0]);
291 /*if (server_config.pidfile) - server_config.pidfile is never NULL */
292 remove_pidfile(server_config.pidfile);