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, 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_align, requested_align, static_lease_ip;
37 struct option_set *option;
38 struct dhcpOfferedAddr *lease, static_lease;
39 USE_FEATURE_UDHCP_PORT(char *str_P;)
41 #if ENABLE_FEATURE_UDHCP_PORT
46 opt = getopt32(argv, "fS" USE_FEATURE_UDHCP_PORT("P:", &str_P));
49 if (!(opt & 1)) { /* no -f */
50 bb_daemonize_or_rexec(0, argv);
51 logmode &= ~LOGMODE_STDIO;
54 if (opt & 2) { /* -S */
55 openlog(applet_name, LOG_PID, LOG_LOCAL0);
56 logmode |= LOGMODE_SYSLOG;
58 #if ENABLE_FEATURE_UDHCP_PORT
59 if (opt & 4) { /* -P */
60 SERVER_PORT = xatou16(str_P);
61 CLIENT_PORT = SERVER_PORT + 1;
64 /* Would rather not do read_config before daemonization -
65 * otherwise NOMMU machines will parse config twice */
66 read_config(argv[0] ? argv[0] : DHCPD_CONF_FILE);
68 /* Make sure fd 0,1,2 are open */
70 /* Equivalent of doing a fflush after every \n */
74 write_pidfile(server_config.pidfile);
75 /* if (!..) bb_perror_msg("cannot create pidfile %s", pidfile); */
77 bb_info_msg("%s (v"BB_VER") started", applet_name);
79 option = find_option(server_config.options, DHCP_LEASE_TIME);
80 server_config.lease = LEASE_TIME;
82 memcpy(&server_config.lease, option->data + 2, 4);
83 server_config.lease = ntohl(server_config.lease);
87 num_ips = server_config.end_ip - server_config.start_ip + 1;
88 if (server_config.max_leases > num_ips) {
89 bb_error_msg("max_leases=%u is too big, setting to %u",
90 (unsigned)server_config.max_leases, num_ips);
91 server_config.max_leases = num_ips;
94 leases = xzalloc(server_config.max_leases * sizeof(*leases));
95 read_leases(server_config.lease_file);
97 if (read_interface(server_config.interface, &server_config.ifindex,
98 &server_config.server, server_config.arp)) {
103 /* Setup the signal pipe */
106 timeout_end = monotonic_sec() + server_config.auto_time;
107 while (1) { /* loop until universe collapses */
109 if (server_socket < 0) {
110 server_socket = listen_socket(/*INADDR_ANY,*/ SERVER_PORT,
111 server_config.interface);
114 max_sock = udhcp_sp_fd_set(&rfds, server_socket);
115 if (server_config.auto_time) {
116 tv.tv_sec = timeout_end - monotonic_sec();
120 if (!server_config.auto_time || tv.tv_sec > 0) {
121 retval = select(max_sock + 1, &rfds, NULL, NULL,
122 server_config.auto_time ? &tv : NULL);
126 timeout_end = monotonic_sec() + server_config.auto_time;
129 if (retval < 0 && errno != EINTR) {
130 DEBUG("error on select");
134 switch (udhcp_sp_read(&rfds)) {
136 bb_info_msg("Received a SIGUSR1");
138 /* why not just reset the timeout, eh */
139 timeout_end = monotonic_sec() + server_config.auto_time;
142 bb_info_msg("Received a SIGTERM");
144 case 0: break; /* no signal */
145 default: continue; /* signal or error (probably EINTR) */
148 bytes = udhcp_recv_packet(&packet, server_socket); /* this waits for a packet - idle */
150 if (bytes == -1 && errno != EINTR) {
151 DEBUG("error on read, %s, reopening socket", strerror(errno));
152 close(server_socket);
158 state = get_option(&packet, DHCP_MESSAGE_TYPE);
160 bb_error_msg("cannot get option from packet, ignoring");
164 /* Look for a static lease */
165 static_lease_ip = getIpByMac(server_config.static_leases, &packet.chaddr);
167 if (static_lease_ip) {
168 bb_info_msg("Found static lease: %x", static_lease_ip);
170 memcpy(&static_lease.chaddr, &packet.chaddr, 16);
171 static_lease.yiaddr = static_lease_ip;
172 static_lease.expires = 0;
174 lease = &static_lease;
176 lease = find_lease_by_chaddr(packet.chaddr);
181 DEBUG("Received DISCOVER");
183 if (sendOffer(&packet) < 0) {
184 bb_error_msg("send OFFER failed");
188 DEBUG("received REQUEST");
190 requested = get_option(&packet, DHCP_REQUESTED_IP);
191 server_id = get_option(&packet, DHCP_SERVER_ID);
193 if (requested) memcpy(&requested_align, requested, 4);
194 if (server_id) memcpy(&server_id_align, server_id, 4);
198 /* SELECTING State */
199 DEBUG("server_id = %08x", ntohl(server_id_align));
200 if (server_id_align == server_config.server && requested
201 && requested_align == lease->yiaddr
203 sendACK(&packet, lease->yiaddr);
205 } else if (requested) {
206 /* INIT-REBOOT State */
207 if (lease->yiaddr == requested_align)
208 sendACK(&packet, lease->yiaddr);
211 } else if (lease->yiaddr == packet.ciaddr) {
212 /* RENEWING or REBINDING State */
213 sendACK(&packet, lease->yiaddr);
215 /* don't know what to do!!!! */
219 /* what to do if we have no record of the client */
220 } else if (server_id) {
221 /* SELECTING State */
223 } else if (requested) {
224 /* INIT-REBOOT State */
225 lease = find_lease_by_yiaddr(requested_align);
227 if (lease_expired(lease)) {
228 /* probably best if we drop this lease */
229 memset(lease->chaddr, 0, 16);
230 /* make some contention for this address */
234 uint32_t r = ntohl(requested_align);
235 if (r < server_config.start_ip
236 || r > server_config.end_ip
240 /* else remain silent */
244 /* RENEWING or REBINDING State */
248 DEBUG("Received DECLINE");
250 memset(lease->chaddr, 0, 16);
251 lease->expires = time(0) + server_config.decline_time;
255 DEBUG("Received RELEASE");
257 lease->expires = time(0);
260 DEBUG("Received INFORM");
261 send_inform(&packet);
264 bb_info_msg("Unsupported DHCP message (%02x) - ignoring", state[0]);
270 /*if (server_config.pidfile) - server_config.pidfile is never NULL */
271 remove_pidfile(server_config.pidfile);