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 IF_FEATURE_UDHCP_PORT(char *str_P;)
42 #if ENABLE_FEATURE_UDHCP_PORT
47 opt = getopt32(argv, "fS" IF_FEATURE_UDHCP_PORT("P:", &str_P));
49 if (!(opt & 1)) { /* no -f */
50 bb_daemonize_or_rexec(0, argv);
51 logmode = LOGMODE_NONE;
53 if (opt & 2) { /* -S */
54 openlog(applet_name, LOG_PID, LOG_DAEMON);
55 logmode |= LOGMODE_SYSLOG;
57 #if ENABLE_FEATURE_UDHCP_PORT
58 if (opt & 4) { /* -P */
59 SERVER_PORT = xatou16(str_P);
60 CLIENT_PORT = SERVER_PORT + 1;
63 /* Would rather not do read_config before daemonization -
64 * otherwise NOMMU machines will parse config twice */
65 read_config(argv[0] ? argv[0] : DHCPD_CONF_FILE);
67 /* Make sure fd 0,1,2 are open */
69 /* Equivalent of doing a fflush after every \n */
73 write_pidfile(server_config.pidfile);
74 /* if (!..) bb_perror_msg("cannot create pidfile %s", pidfile); */
76 bb_info_msg("%s (v"BB_VER") started", applet_name);
78 option = find_option(server_config.options, DHCP_LEASE_TIME);
79 server_config.lease = LEASE_TIME;
81 move_from_unaligned32(server_config.lease, option->data + 2);
82 server_config.lease = ntohl(server_config.lease);
86 num_ips = server_config.end_ip - server_config.start_ip + 1;
87 if (server_config.max_leases > num_ips) {
88 bb_error_msg("max_leases=%u is too big, setting to %u",
89 (unsigned)server_config.max_leases, num_ips);
90 server_config.max_leases = num_ips;
93 leases = xzalloc(server_config.max_leases * sizeof(*leases));
94 read_leases(server_config.lease_file);
96 if (udhcp_read_interface(server_config.interface,
97 &server_config.ifindex,
98 &server_config.server_nip,
99 server_config.server_mac)
105 /* Setup the signal pipe */
108 timeout_end = monotonic_sec() + server_config.auto_time;
109 while (1) { /* loop until universe collapses */
113 if (server_socket < 0) {
114 server_socket = udhcp_listen_socket(/*INADDR_ANY,*/ SERVER_PORT,
115 server_config.interface);
118 max_sock = udhcp_sp_fd_set(&rfds, server_socket);
119 if (server_config.auto_time) {
120 tv.tv_sec = timeout_end - monotonic_sec();
124 if (!server_config.auto_time || tv.tv_sec > 0) {
125 retval = select(max_sock + 1, &rfds, NULL, NULL,
126 server_config.auto_time ? &tv : NULL);
130 timeout_end = monotonic_sec() + server_config.auto_time;
133 if (retval < 0 && errno != EINTR) {
134 DEBUG("error on select");
138 switch (udhcp_sp_read(&rfds)) {
140 bb_info_msg("Received a SIGUSR1");
142 /* why not just reset the timeout, eh */
143 timeout_end = monotonic_sec() + server_config.auto_time;
146 bb_info_msg("Received a SIGTERM");
148 case 0: /* no signal: read a packet */
150 default: /* signal or error (probably EINTR): back to select */
154 bytes = udhcp_recv_kernel_packet(&packet, server_socket);
156 /* bytes can also be -2 ("bad packet data") */
157 if (bytes == -1 && errno != EINTR) {
158 DEBUG("error on read, %s, reopening socket", strerror(errno));
159 close(server_socket);
165 state = get_option(&packet, DHCP_MESSAGE_TYPE);
167 bb_error_msg("cannot get option from packet, ignoring");
171 /* Look for a static lease */
172 static_lease_ip = get_static_nip_by_mac(server_config.static_leases, &packet.chaddr);
173 if (static_lease_ip) {
174 bb_info_msg("Found static lease: %x", static_lease_ip);
176 memcpy(&static_lease.lease_mac16, &packet.chaddr, 16);
177 static_lease.lease_nip = static_lease_ip;
178 static_lease.expires = 0;
180 lease = &static_lease;
182 lease = find_lease_by_chaddr(packet.chaddr);
187 DEBUG("Received DISCOVER");
189 if (send_offer(&packet) < 0) {
190 bb_error_msg("send OFFER failed");
194 DEBUG("received REQUEST");
196 requested = get_option(&packet, DHCP_REQUESTED_IP);
197 server_id = get_option(&packet, DHCP_SERVER_ID);
200 move_from_unaligned32(requested_aligned, requested);
202 move_from_unaligned32(server_id_aligned, server_id);
206 /* SELECTING State */
207 DEBUG("server_id = %08x", ntohl(server_id_aligned));
208 if (server_id_aligned == server_config.server_nip
210 && requested_aligned == lease->lease_nip
212 send_ACK(&packet, lease->lease_nip);
214 } else if (requested) {
215 /* INIT-REBOOT State */
216 if (lease->lease_nip == requested_aligned)
217 send_ACK(&packet, lease->lease_nip);
220 } else if (lease->lease_nip == packet.ciaddr) {
221 /* RENEWING or REBINDING State */
222 send_ACK(&packet, lease->lease_nip);
223 } else { /* don't know what to do!!!! */
227 /* what to do if we have no record of the client */
228 } else if (server_id) {
229 /* SELECTING State */
231 } else if (requested) {
232 /* INIT-REBOOT State */
233 lease = find_lease_by_yiaddr(requested_aligned);
235 if (lease_expired(lease)) {
236 /* probably best if we drop this lease */
237 memset(lease->lease_mac16, 0, 16);
239 /* make some contention for this address */
243 uint32_t r = ntohl(requested_aligned);
244 if (r < server_config.start_ip
245 || r > server_config.end_ip
249 /* else remain silent */
253 /* RENEWING or REBINDING State */
257 DEBUG("Received DECLINE");
259 memset(lease->lease_mac16, 0, 16);
260 lease->expires = time(NULL) + server_config.decline_time;
264 DEBUG("Received RELEASE");
266 lease->expires = time(NULL);
269 DEBUG("Received INFORM");
270 send_inform(&packet);
273 bb_info_msg("Unsupported DHCP message (%02x) - ignoring", state[0]);
279 /*if (server_config.pidfile) - server_config.pidfile is never NULL */
280 remove_pidfile(server_config.pidfile);