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)
29 int server_socket = -1, retval, max_sock;
30 struct dhcpMessage 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 dhcpOfferedAddr *lease, static_lease;
40 USE_FEATURE_UDHCP_PORT(char *str_P;)
42 #if ENABLE_FEATURE_UDHCP_PORT
47 opt = getopt32(argv, "fS" USE_FEATURE_UDHCP_PORT("P:", &str_P));
50 if (!(opt & 1)) { /* no -f */
51 bb_daemonize_or_rexec(0, argv);
52 logmode &= ~LOGMODE_STDIO;
55 if (opt & 2) { /* -S */
56 openlog(applet_name, LOG_PID, LOG_LOCAL0);
57 logmode |= LOGMODE_SYSLOG;
59 #if ENABLE_FEATURE_UDHCP_PORT
60 if (opt & 4) { /* -P */
61 SERVER_PORT = xatou16(str_P);
62 CLIENT_PORT = SERVER_PORT + 1;
65 /* Would rather not do read_config before daemonization -
66 * otherwise NOMMU machines will parse config twice */
67 read_config(argv[0] ? argv[0] : DHCPD_CONF_FILE);
69 /* Make sure fd 0,1,2 are open */
71 /* Equivalent of doing a fflush after every \n */
75 write_pidfile(server_config.pidfile);
76 /* if (!..) bb_perror_msg("cannot create pidfile %s", pidfile); */
78 bb_info_msg("%s (v"BB_VER") started", applet_name);
80 option = find_option(server_config.options, DHCP_LEASE_TIME);
81 server_config.lease = LEASE_TIME;
83 move_from_unaligned32(server_config.lease, option->data + 2);
84 server_config.lease = ntohl(server_config.lease);
88 num_ips = server_config.end_ip - server_config.start_ip + 1;
89 if (server_config.max_leases > num_ips) {
90 bb_error_msg("max_leases=%u is too big, setting to %u",
91 (unsigned)server_config.max_leases, num_ips);
92 server_config.max_leases = num_ips;
95 leases = xzalloc(server_config.max_leases * sizeof(*leases));
96 read_leases(server_config.lease_file);
98 if (udhcp_read_interface(server_config.interface, &server_config.ifindex,
99 &server_config.server, server_config.arp)) {
104 /* Setup the signal pipe */
107 timeout_end = monotonic_sec() + server_config.auto_time;
108 while (1) { /* loop until universe collapses */
112 if (server_socket < 0) {
113 server_socket = udhcp_listen_socket(/*INADDR_ANY,*/ SERVER_PORT,
114 server_config.interface);
117 max_sock = udhcp_sp_fd_set(&rfds, server_socket);
118 if (server_config.auto_time) {
119 tv.tv_sec = timeout_end - monotonic_sec();
123 if (!server_config.auto_time || tv.tv_sec > 0) {
124 retval = select(max_sock + 1, &rfds, NULL, NULL,
125 server_config.auto_time ? &tv : NULL);
129 timeout_end = monotonic_sec() + server_config.auto_time;
132 if (retval < 0 && errno != EINTR) {
133 DEBUG("error on select");
137 switch (udhcp_sp_read(&rfds)) {
139 bb_info_msg("Received a SIGUSR1");
141 /* why not just reset the timeout, eh */
142 timeout_end = monotonic_sec() + server_config.auto_time;
145 bb_info_msg("Received a SIGTERM");
147 case 0: /* no signal: read a packet */
149 default: /* signal or error (probably EINTR): back to select */
153 bytes = udhcp_recv_kernel_packet(&packet, server_socket);
155 /* bytes can also be -2 ("bad packet data") */
156 if (bytes == -1 && errno != EINTR) {
157 DEBUG("error on read, %s, reopening socket", strerror(errno));
158 close(server_socket);
164 state = get_option(&packet, DHCP_MESSAGE_TYPE);
166 bb_error_msg("cannot get option from packet, ignoring");
170 /* Look for a static lease */
171 static_lease_ip = getIpByMac(server_config.static_leases, &packet.chaddr);
172 if (static_lease_ip) {
173 bb_info_msg("Found static lease: %x", static_lease_ip);
175 memcpy(&static_lease.chaddr, &packet.chaddr, 16);
176 static_lease.yiaddr = static_lease_ip;
177 static_lease.expires = 0;
179 lease = &static_lease;
181 lease = find_lease_by_chaddr(packet.chaddr);
186 DEBUG("Received DISCOVER");
188 if (send_offer(&packet) < 0) {
189 bb_error_msg("send OFFER failed");
193 DEBUG("received REQUEST");
195 requested = get_option(&packet, DHCP_REQUESTED_IP);
196 server_id = get_option(&packet, DHCP_SERVER_ID);
199 move_from_unaligned32(requested_aligned, requested);
201 move_from_unaligned32(server_id_aligned, server_id);
205 /* SELECTING State */
206 DEBUG("server_id = %08x", ntohl(server_id_aligned));
207 if (server_id_aligned == server_config.server
209 && requested_aligned == lease->yiaddr
211 send_ACK(&packet, lease->yiaddr);
213 } else if (requested) {
214 /* INIT-REBOOT State */
215 if (lease->yiaddr == requested_aligned)
216 send_ACK(&packet, lease->yiaddr);
219 } else if (lease->yiaddr == packet.ciaddr) {
220 /* RENEWING or REBINDING State */
221 send_ACK(&packet, lease->yiaddr);
222 } else { /* don't know what to do!!!! */
226 /* what to do if we have no record of the client */
227 } else if (server_id) {
228 /* SELECTING State */
230 } else if (requested) {
231 /* INIT-REBOOT State */
232 lease = find_lease_by_yiaddr(requested_aligned);
234 if (lease_expired(lease)) {
235 /* probably best if we drop this lease */
236 memset(lease->chaddr, 0, 16);
237 /* make some contention for this address */
241 uint32_t r = ntohl(requested_aligned);
242 if (r < server_config.start_ip
243 || r > server_config.end_ip
247 /* else remain silent */
251 /* RENEWING or REBINDING State */
255 DEBUG("Received DECLINE");
257 memset(lease->chaddr, 0, 16);
258 lease->expires = time(NULL) + server_config.decline_time;
262 DEBUG("Received RELEASE");
264 lease->expires = time(NULL);
267 DEBUG("Received INFORM");
268 send_inform(&packet);
271 bb_info_msg("Unsupported DHCP message (%02x) - ignoring", state[0]);
277 /*if (server_config.pidfile) - server_config.pidfile is never NULL */
278 remove_pidfile(server_config.pidfile);