4 * Copyright (C) 1999 Matthew Ramsay <matthewr@moreton.com.au>
5 * Chris Trew <ctrew@moreton.com.au>
7 * Rewrite by Russ Dill <Russ.Dill@asu.edu> July 2001
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #include <arpa/inet.h>
30 #include <netinet/in.h>
31 #include <sys/socket.h>
35 #include <sys/ioctl.h>
44 #include "serverpacket.h"
46 #include "signalpipe.h"
47 #include "static_leases.h"
51 struct dhcpOfferedAddr *leases;
52 struct server_config_t server_config;
55 #ifdef COMBINED_BINARY
56 int udhcpd_main(int argc, char *argv[])
58 int main(int argc, char *argv[])
63 int server_socket = -1;
65 struct dhcpMessage packet;
67 uint8_t *server_id, *requested;
68 uint32_t server_id_align, requested_align;
69 unsigned long timeout_end;
70 struct option_set *option;
71 struct dhcpOfferedAddr *lease;
72 struct dhcpOfferedAddr static_lease;
74 unsigned long num_ips;
76 uint32_t static_lease_ip;
78 memset(&server_config, 0, sizeof(struct server_config_t));
79 read_config(argc < 2 ? DHCPD_CONF_FILE : argv[1]);
81 /* Start the log, sanitize fd's, and write a pid file */
82 start_log_and_pid("udhcpd", server_config.pidfile);
84 if ((option = find_option(server_config.options, DHCP_LEASE_TIME))) {
85 memcpy(&server_config.lease, option->data + 2, 4);
86 server_config.lease = ntohl(server_config.lease);
88 else server_config.lease = LEASE_TIME;
91 // Let's try to fix http://bugs.debian.org/341139 (ericvb@debian.org - 16/12/05)
92 //num_ips = ntohl(server_config.end) - ntohl(server_config.start);
93 num_ips = ntohl(server_config.end) - ntohl(server_config.start) + 1;
94 if (server_config.max_leases > num_ips) {
95 LOG(LOG_ERR, "max_leases value (%lu) not sane, "
96 "setting to %lu instead",
97 server_config.max_leases, num_ips);
98 server_config.max_leases = num_ips;
101 leases = xcalloc(server_config.max_leases, sizeof(struct dhcpOfferedAddr));
102 read_leases(server_config.lease_file);
104 if (read_interface(server_config.interface, &server_config.ifindex,
105 &server_config.server, server_config.arp) < 0)
109 background(server_config.pidfile); /* hold lock during fork. */
112 /* Setup the signal pipe */
115 timeout_end = time(0) + server_config.auto_time;
116 while(1) { /* loop until universe collapses */
118 if (server_socket < 0)
119 if ((server_socket = listen_socket(INADDR_ANY, SERVER_PORT, server_config.interface)) < 0) {
120 LOG(LOG_ERR, "FATAL: couldn't create server socket, %m");
124 max_sock = udhcp_sp_fd_set(&rfds, server_socket);
125 if (server_config.auto_time) {
126 tv.tv_sec = timeout_end - time(0);
129 if (!server_config.auto_time || tv.tv_sec > 0) {
130 retval = select(max_sock + 1, &rfds, NULL, NULL,
131 server_config.auto_time ? &tv : NULL);
132 } else retval = 0; /* If we already timed out, fall through */
136 timeout_end = time(0) + server_config.auto_time;
138 } else if (retval < 0 && errno != EINTR) {
139 DEBUG(LOG_INFO, "error on select");
143 switch (udhcp_sp_read(&rfds)) {
145 LOG(LOG_INFO, "Received a SIGUSR1");
147 /* why not just reset the timeout, eh */
148 timeout_end = time(0) + server_config.auto_time;
151 LOG(LOG_INFO, "Received a SIGTERM");
153 case 0: break; /* no signal */
154 default: continue; /* signal or error (probably EINTR) */
157 if ((bytes = get_packet(&packet, server_socket)) < 0) { /* this waits for a packet - idle */
158 if (bytes == -1 && errno != EINTR) {
159 DEBUG(LOG_INFO, "error on read, %m, reopening socket");
160 close(server_socket);
166 if ((state = get_option(&packet, DHCP_MESSAGE_TYPE)) == NULL) {
167 DEBUG(LOG_ERR, "couldn't get option from packet, ignoring");
171 /* Look for a static lease */
172 static_lease_ip = getIpByMac(server_config.static_leases, &packet.chaddr);
176 printf("Found static lease: %x\n", static_lease_ip);
178 memcpy(&static_lease.chaddr, &packet.chaddr, 16);
179 static_lease.yiaddr = static_lease_ip;
180 static_lease.expires = 0;
182 lease = &static_lease;
187 lease = find_lease_by_chaddr(packet.chaddr);
192 DEBUG(LOG_INFO,"received DISCOVER");
194 if (sendOffer(&packet) < 0) {
195 LOG(LOG_ERR, "send OFFER failed");
199 DEBUG(LOG_INFO, "received REQUEST");
201 requested = get_option(&packet, DHCP_REQUESTED_IP);
202 server_id = get_option(&packet, DHCP_SERVER_ID);
204 if (requested) memcpy(&requested_align, requested, 4);
205 if (server_id) memcpy(&server_id_align, server_id, 4);
209 /* SELECTING State */
210 DEBUG(LOG_INFO, "server_id = %08x", ntohl(server_id_align));
211 if (server_id_align == server_config.server && requested &&
212 requested_align == lease->yiaddr) {
213 sendACK(&packet, lease->yiaddr);
217 /* INIT-REBOOT State */
218 if (lease->yiaddr == requested_align)
219 sendACK(&packet, lease->yiaddr);
220 else sendNAK(&packet);
222 /* RENEWING or REBINDING State */
223 if (lease->yiaddr == packet.ciaddr)
224 sendACK(&packet, lease->yiaddr);
226 /* don't know what to do!!!! */
232 /* what to do if we have no record of the client */
233 } else if (server_id) {
234 /* SELECTING State */
236 } else if (requested) {
237 /* INIT-REBOOT State */
238 if ((lease = find_lease_by_yiaddr(requested_align))) {
239 if (lease_expired(lease)) {
240 /* probably best if we drop this lease */
241 memset(lease->chaddr, 0, 16);
242 /* make some contention for this address */
243 } else sendNAK(&packet);
244 } else if (requested_align < server_config.start ||
245 requested_align > server_config.end) {
247 } /* else remain silent */
250 /* RENEWING or REBINDING State */
254 DEBUG(LOG_INFO,"received DECLINE");
256 memset(lease->chaddr, 0, 16);
257 lease->expires = time(0) + server_config.decline_time;
261 DEBUG(LOG_INFO,"received RELEASE");
262 if (lease) lease->expires = time(0);
265 DEBUG(LOG_INFO,"received INFORM");
266 send_inform(&packet);
269 LOG(LOG_WARNING, "unsupported DHCP message (%02x) -- ignoring", state[0]);