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 dhcpOfferedAddr *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)
30 int server_socket = -1, bytes, retval, max_sock;
31 struct dhcpMessage packet;
32 uint8_t *state, *server_id, *requested;
33 uint32_t server_id_aligned = server_id_aligned; /* for compiler */
34 uint32_t requested_aligned = requested_aligned;
35 uint32_t static_lease_ip;
39 struct option_set *option;
40 struct dhcpOfferedAddr *lease, static_lease;
41 USE_FEATURE_UDHCP_PORT(char *str_P;)
43 #if ENABLE_FEATURE_UDHCP_PORT
48 opt = getopt32(argv, "fS" USE_FEATURE_UDHCP_PORT("P:", &str_P));
51 if (!(opt & 1)) { /* no -f */
52 bb_daemonize_or_rexec(0, argv);
53 logmode &= ~LOGMODE_STDIO;
56 if (opt & 2) { /* -S */
57 openlog(applet_name, LOG_PID, LOG_LOCAL0);
58 logmode |= LOGMODE_SYSLOG;
60 #if ENABLE_FEATURE_UDHCP_PORT
61 if (opt & 4) { /* -P */
62 SERVER_PORT = xatou16(str_P);
63 CLIENT_PORT = SERVER_PORT + 1;
66 /* Would rather not do read_config before daemonization -
67 * otherwise NOMMU machines will parse config twice */
68 read_config(argv[0] ? argv[0] : DHCPD_CONF_FILE);
70 /* Make sure fd 0,1,2 are open */
72 /* Equivalent of doing a fflush after every \n */
76 write_pidfile(server_config.pidfile);
77 /* if (!..) bb_perror_msg("cannot create pidfile %s", pidfile); */
79 bb_info_msg("%s (v"BB_VER") started", applet_name);
81 option = find_option(server_config.options, DHCP_LEASE_TIME);
82 server_config.lease = LEASE_TIME;
84 move_from_unaligned32(server_config.lease, option->data + 2);
85 server_config.lease = ntohl(server_config.lease);
89 num_ips = server_config.end_ip - server_config.start_ip + 1;
90 if (server_config.max_leases > num_ips) {
91 bb_error_msg("max_leases=%u is too big, setting to %u",
92 (unsigned)server_config.max_leases, num_ips);
93 server_config.max_leases = num_ips;
96 leases = xzalloc(server_config.max_leases * sizeof(*leases));
97 read_leases(server_config.lease_file);
99 if (udhcp_read_interface(server_config.interface, &server_config.ifindex,
100 &server_config.server, server_config.arp)) {
105 /* Setup the signal pipe */
108 timeout_end = monotonic_sec() + server_config.auto_time;
109 while (1) { /* loop until universe collapses */
111 if (server_socket < 0) {
112 server_socket = udhcp_listen_socket(/*INADDR_ANY,*/ SERVER_PORT,
113 server_config.interface);
116 max_sock = udhcp_sp_fd_set(&rfds, server_socket);
117 if (server_config.auto_time) {
118 tv.tv_sec = timeout_end - monotonic_sec();
122 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);
128 timeout_end = monotonic_sec() + server_config.auto_time;
131 if (retval < 0 && errno != EINTR) {
132 DEBUG("error on select");
136 switch (udhcp_sp_read(&rfds)) {
138 bb_info_msg("Received a SIGUSR1");
140 /* why not just reset the timeout, eh */
141 timeout_end = monotonic_sec() + server_config.auto_time;
144 bb_info_msg("Received a SIGTERM");
146 case 0: break; /* no signal */
147 default: continue; /* signal or error (probably EINTR) */
150 bytes = udhcp_recv_kernel_packet(&packet, server_socket); /* this waits for a packet - idle */
152 if (bytes == -1 && errno != EINTR) {
153 DEBUG("error on read, %s, reopening socket", strerror(errno));
154 close(server_socket);
160 state = get_option(&packet, DHCP_MESSAGE_TYPE);
162 bb_error_msg("cannot get option from packet, ignoring");
166 /* Look for a static lease */
167 static_lease_ip = getIpByMac(server_config.static_leases, &packet.chaddr);
169 if (static_lease_ip) {
170 bb_info_msg("Found static lease: %x", static_lease_ip);
172 memcpy(&static_lease.chaddr, &packet.chaddr, 16);
173 static_lease.yiaddr = static_lease_ip;
174 static_lease.expires = 0;
176 lease = &static_lease;
178 lease = find_lease_by_chaddr(packet.chaddr);
183 DEBUG("Received DISCOVER");
185 if (send_offer(&packet) < 0) {
186 bb_error_msg("send OFFER failed");
190 DEBUG("received REQUEST");
192 requested = get_option(&packet, DHCP_REQUESTED_IP);
193 server_id = get_option(&packet, DHCP_SERVER_ID);
196 move_from_unaligned32(requested_aligned, requested);
198 move_from_unaligned32(server_id_aligned, server_id);
202 /* SELECTING State */
203 DEBUG("server_id = %08x", ntohl(server_id_aligned));
204 if (server_id_aligned == server_config.server
206 && requested_aligned == lease->yiaddr
208 send_ACK(&packet, lease->yiaddr);
210 } else if (requested) {
211 /* INIT-REBOOT State */
212 if (lease->yiaddr == requested_aligned)
213 send_ACK(&packet, lease->yiaddr);
216 } else if (lease->yiaddr == packet.ciaddr) {
217 /* RENEWING or REBINDING State */
218 send_ACK(&packet, lease->yiaddr);
219 } else { /* don't know what to do!!!! */
223 /* what to do if we have no record of the client */
224 } else if (server_id) {
225 /* SELECTING State */
227 } else if (requested) {
228 /* INIT-REBOOT State */
229 lease = find_lease_by_yiaddr(requested_aligned);
231 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 */
238 uint32_t r = ntohl(requested_aligned);
239 if (r < server_config.start_ip
240 || r > server_config.end_ip
244 /* else remain silent */
248 /* RENEWING or REBINDING State */
252 DEBUG("Received DECLINE");
254 memset(lease->chaddr, 0, 16);
255 lease->expires = time(0) + server_config.decline_time;
259 DEBUG("Received RELEASE");
261 lease->expires = time(0);
264 DEBUG("Received INFORM");
265 send_inform(&packet);
268 bb_info_msg("Unsupported DHCP message (%02x) - ignoring", state[0]);
274 /*if (server_config.pidfile) - server_config.pidfile is never NULL */
275 remove_pidfile(server_config.pidfile);