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.
20 struct dhcpOfferedAddr *leases;
21 struct server_config_t server_config;
24 int udhcpd_main(int argc, char **argv);
25 int udhcpd_main(int argc, char **argv)
29 int server_socket = -1, bytes, retval, max_sock;
30 struct dhcpMessage packet;
31 uint8_t *state, *server_id, *requested;
32 uint32_t server_id_align, requested_align, static_lease_ip;
36 struct option_set *option;
37 struct dhcpOfferedAddr *lease, static_lease;
39 opt = getopt32(argv, "fS");
42 if (!(opt & 1)) { /* no -f */
43 bb_daemonize_or_rexec(0, argv);
44 logmode &= ~LOGMODE_STDIO;
47 if (opt & 2) { /* -S */
48 openlog(applet_name, LOG_PID, LOG_LOCAL0);
49 logmode |= LOGMODE_SYSLOG;
52 /* Would rather not do read_config before daemonization -
53 * otherwise NOMMU machines will parse config twice */
54 read_config(argv[0] ? argv[0] : DHCPD_CONF_FILE);
56 /* Make sure fd 0,1,2 are open */
58 /* Equivalent of doing a fflush after every \n */
62 write_pidfile(server_config.pidfile);
63 /* if (!..) bb_perror_msg("cannot create pidfile %s", pidfile); */
65 bb_info_msg("%s (v%s) started", applet_name, BB_VER);
67 option = find_option(server_config.options, DHCP_LEASE_TIME);
68 server_config.lease = LEASE_TIME;
70 memcpy(&server_config.lease, option->data + 2, 4);
71 server_config.lease = ntohl(server_config.lease);
75 num_ips = server_config.end_ip - server_config.start_ip + 1;
76 if (server_config.max_leases > num_ips) {
77 bb_error_msg("max_leases=%u is too big, setting to %u",
78 (unsigned)server_config.max_leases, num_ips);
79 server_config.max_leases = num_ips;
82 leases = xzalloc(server_config.max_leases * sizeof(*leases));
83 read_leases(server_config.lease_file);
85 if (read_interface(server_config.interface, &server_config.ifindex,
86 &server_config.server, server_config.arp)) {
91 /* Setup the signal pipe */
94 timeout_end = monotonic_sec() + server_config.auto_time;
95 while (1) { /* loop until universe collapses */
97 if (server_socket < 0) {
98 server_socket = listen_socket(/*INADDR_ANY,*/ SERVER_PORT,
99 server_config.interface);
102 max_sock = udhcp_sp_fd_set(&rfds, server_socket);
103 if (server_config.auto_time) {
104 tv.tv_sec = timeout_end - monotonic_sec();
108 if (!server_config.auto_time || tv.tv_sec > 0) {
109 retval = select(max_sock + 1, &rfds, NULL, NULL,
110 server_config.auto_time ? &tv : NULL);
114 timeout_end = monotonic_sec() + server_config.auto_time;
117 if (retval < 0 && errno != EINTR) {
118 DEBUG("error on select");
122 switch (udhcp_sp_read(&rfds)) {
124 bb_info_msg("Received a SIGUSR1");
126 /* why not just reset the timeout, eh */
127 timeout_end = monotonic_sec() + server_config.auto_time;
130 bb_info_msg("Received a SIGTERM");
132 case 0: break; /* no signal */
133 default: continue; /* signal or error (probably EINTR) */
136 bytes = udhcp_get_packet(&packet, server_socket); /* this waits for a packet - idle */
138 if (bytes == -1 && errno != EINTR) {
139 DEBUG("error on read, %s, reopening socket", strerror(errno));
140 close(server_socket);
146 state = get_option(&packet, DHCP_MESSAGE_TYPE);
148 bb_error_msg("cannot get option from packet, ignoring");
152 /* Look for a static lease */
153 static_lease_ip = getIpByMac(server_config.static_leases, &packet.chaddr);
155 if (static_lease_ip) {
156 bb_info_msg("Found static lease: %x", static_lease_ip);
158 memcpy(&static_lease.chaddr, &packet.chaddr, 16);
159 static_lease.yiaddr = static_lease_ip;
160 static_lease.expires = 0;
162 lease = &static_lease;
164 lease = find_lease_by_chaddr(packet.chaddr);
169 DEBUG("Received DISCOVER");
171 if (sendOffer(&packet) < 0) {
172 bb_error_msg("send OFFER failed");
176 DEBUG("received REQUEST");
178 requested = get_option(&packet, DHCP_REQUESTED_IP);
179 server_id = get_option(&packet, DHCP_SERVER_ID);
181 if (requested) memcpy(&requested_align, requested, 4);
182 if (server_id) memcpy(&server_id_align, server_id, 4);
186 /* SELECTING State */
187 DEBUG("server_id = %08x", ntohl(server_id_align));
188 if (server_id_align == server_config.server && requested
189 && requested_align == lease->yiaddr
191 sendACK(&packet, lease->yiaddr);
193 } else if (requested) {
194 /* INIT-REBOOT State */
195 if (lease->yiaddr == requested_align)
196 sendACK(&packet, lease->yiaddr);
199 } else if (lease->yiaddr == packet.ciaddr) {
200 /* RENEWING or REBINDING State */
201 sendACK(&packet, lease->yiaddr);
203 /* don't know what to do!!!! */
207 /* what to do if we have no record of the client */
208 } else if (server_id) {
209 /* SELECTING State */
211 } else if (requested) {
212 /* INIT-REBOOT State */
213 lease = find_lease_by_yiaddr(requested_align);
215 if (lease_expired(lease)) {
216 /* probably best if we drop this lease */
217 memset(lease->chaddr, 0, 16);
218 /* make some contention for this address */
222 uint32_t r = ntohl(requested_align);
223 if (r < server_config.start_ip
224 || r > server_config.end_ip
228 /* else remain silent */
232 /* RENEWING or REBINDING State */
236 DEBUG("Received DECLINE");
238 memset(lease->chaddr, 0, 16);
239 lease->expires = time(0) + server_config.decline_time;
243 DEBUG("Received RELEASE");
245 lease->expires = time(0);
248 DEBUG("Received INFORM");
249 send_inform(&packet);
252 bb_info_msg("Unsupported DHCP message (%02x) - ignoring", state[0]);
258 /*if (server_config.pidfile) - server_config.pidfile is never NULL */
259 remove_pidfile(server_config.pidfile);