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.
17 #include <arpa/inet.h>
19 #include <netinet/in.h>
20 #include <sys/socket.h>
24 #include <sys/ioctl.h>
32 #include "serverpacket.h"
34 #include "signalpipe.h"
35 #include "static_leases.h"
39 struct dhcpOfferedAddr *leases;
40 struct server_config_t server_config;
43 int udhcpd_main(int argc, char *argv[])
47 int server_socket = -1, bytes, retval, max_sock;
48 struct dhcpMessage packet;
49 uint8_t *state, *server_id, *requested;
50 uint32_t server_id_align, requested_align, static_lease_ip;
51 unsigned long timeout_end, num_ips;
52 struct option_set *option;
53 struct dhcpOfferedAddr *lease, static_lease;
55 read_config(argc < 2 ? DHCPD_CONF_FILE : argv[1]);
57 /* Start the log, sanitize fd's, and write a pid file */
58 udhcp_start_log_and_pid("udhcpd", server_config.pidfile);
60 if ((option = find_option(server_config.options, DHCP_LEASE_TIME))) {
61 memcpy(&server_config.lease, option->data + 2, 4);
62 server_config.lease = ntohl(server_config.lease);
64 else server_config.lease = LEASE_TIME;
67 num_ips = ntohl(server_config.end) - ntohl(server_config.start) + 1;
68 if (server_config.max_leases > num_ips) {
69 LOG(LOG_ERR, "max_leases value (%lu) not sane, "
70 "setting to %lu instead",
71 server_config.max_leases, num_ips);
72 server_config.max_leases = num_ips;
75 leases = xzalloc(server_config.max_leases * sizeof(struct dhcpOfferedAddr));
76 read_leases(server_config.lease_file);
78 if (read_interface(server_config.interface, &server_config.ifindex,
79 &server_config.server, server_config.arp) < 0)
82 if (!ENABLE_FEATURE_UDHCP_DEBUG)
83 udhcp_background(server_config.pidfile); /* hold lock during fork. */
85 /* Setup the signal pipe */
88 timeout_end = time(0) + server_config.auto_time;
89 while(1) { /* loop until universe collapses */
91 if (server_socket < 0)
92 if ((server_socket = listen_socket(INADDR_ANY, SERVER_PORT, server_config.interface)) < 0) {
93 LOG(LOG_ERR, "FATAL: couldn't create server socket, %m");
97 max_sock = udhcp_sp_fd_set(&rfds, server_socket);
98 if (server_config.auto_time) {
99 tv.tv_sec = timeout_end - time(0);
102 if (!server_config.auto_time || tv.tv_sec > 0) {
103 retval = select(max_sock + 1, &rfds, NULL, NULL,
104 server_config.auto_time ? &tv : NULL);
105 } else retval = 0; /* If we already timed out, fall through */
109 timeout_end = time(0) + server_config.auto_time;
111 } else if (retval < 0 && errno != EINTR) {
112 DEBUG(LOG_INFO, "error on select");
116 switch (udhcp_sp_read(&rfds)) {
118 LOG(LOG_INFO, "Received a SIGUSR1");
120 /* why not just reset the timeout, eh */
121 timeout_end = time(0) + server_config.auto_time;
124 LOG(LOG_INFO, "Received a SIGTERM");
126 case 0: break; /* no signal */
127 default: continue; /* signal or error (probably EINTR) */
130 if ((bytes = udhcp_get_packet(&packet, server_socket)) < 0) { /* this waits for a packet - idle */
131 if (bytes == -1 && errno != EINTR) {
132 DEBUG(LOG_INFO, "error on read, %m, reopening socket");
133 close(server_socket);
139 if ((state = get_option(&packet, DHCP_MESSAGE_TYPE)) == NULL) {
140 DEBUG(LOG_ERR, "couldn't get option from packet, ignoring");
144 /* Look for a static lease */
145 static_lease_ip = getIpByMac(server_config.static_leases, &packet.chaddr);
149 printf("Found static lease: %x\n", static_lease_ip);
151 memcpy(&static_lease.chaddr, &packet.chaddr, 16);
152 static_lease.yiaddr = static_lease_ip;
153 static_lease.expires = 0;
155 lease = &static_lease;
160 lease = find_lease_by_chaddr(packet.chaddr);
165 DEBUG(LOG_INFO,"received DISCOVER");
167 if (sendOffer(&packet) < 0) {
168 LOG(LOG_ERR, "send OFFER failed");
172 DEBUG(LOG_INFO, "received REQUEST");
174 requested = get_option(&packet, DHCP_REQUESTED_IP);
175 server_id = get_option(&packet, DHCP_SERVER_ID);
177 if (requested) memcpy(&requested_align, requested, 4);
178 if (server_id) memcpy(&server_id_align, server_id, 4);
182 /* SELECTING State */
183 DEBUG(LOG_INFO, "server_id = %08x", ntohl(server_id_align));
184 if (server_id_align == server_config.server && requested &&
185 requested_align == lease->yiaddr) {
186 sendACK(&packet, lease->yiaddr);
190 /* INIT-REBOOT State */
191 if (lease->yiaddr == requested_align)
192 sendACK(&packet, lease->yiaddr);
193 else sendNAK(&packet);
195 /* RENEWING or REBINDING State */
196 if (lease->yiaddr == packet.ciaddr)
197 sendACK(&packet, lease->yiaddr);
199 /* don't know what to do!!!! */
205 /* what to do if we have no record of the client */
206 } else if (server_id) {
207 /* SELECTING State */
209 } else if (requested) {
210 /* INIT-REBOOT State */
211 if ((lease = find_lease_by_yiaddr(requested_align))) {
212 if (lease_expired(lease)) {
213 /* probably best if we drop this lease */
214 memset(lease->chaddr, 0, 16);
215 /* make some contention for this address */
216 } else sendNAK(&packet);
217 } else if (requested_align < server_config.start ||
218 requested_align > server_config.end) {
220 } /* else remain silent */
223 /* RENEWING or REBINDING State */
227 DEBUG(LOG_INFO,"received DECLINE");
229 memset(lease->chaddr, 0, 16);
230 lease->expires = time(0) + server_config.decline_time;
234 DEBUG(LOG_INFO,"received RELEASE");
235 if (lease) lease->expires = time(0);
238 DEBUG(LOG_INFO,"received INFORM");
239 send_inform(&packet);
242 LOG(LOG_WARNING, "unsupported DHCP message (%02x) -- ignoring", state[0]);