blob: 7d0b8fad12ea6e1152a3e38ebb8cd61516569ffc [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Mike Frysinger7031f622006-05-08 03:20:50 +00002/* dhcpd.c
3 *
4 * udhcp Server
5 * Copyright (C) 1999 Matthew Ramsay <matthewr@moreton.com.au>
6 * Chris Trew <ctrew@moreton.com.au>
7 *
8 * Rewrite by Russ Dill <Russ.Dill@asu.edu> July 2001
9 *
Rob Landley3f785612006-05-28 01:06:36 +000010 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Mike Frysinger7031f622006-05-08 03:20:50 +000011 */
12
Mike Frysinger7031f622006-05-08 03:20:50 +000013#include "common.h"
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000014#include "dhcpd.h"
15#include "options.h"
Mike Frysinger7031f622006-05-08 03:20:50 +000016
17
18/* globals */
19struct dhcpOfferedAddr *leases;
20struct server_config_t server_config;
21
22
Mike Frysinger7031f622006-05-08 03:20:50 +000023int udhcpd_main(int argc, char *argv[])
Mike Frysinger7031f622006-05-08 03:20:50 +000024{
25 fd_set rfds;
26 struct timeval tv;
Rob Landley9ffd4232006-05-21 18:30:35 +000027 int server_socket = -1, bytes, retval, max_sock;
Mike Frysinger7031f622006-05-08 03:20:50 +000028 struct dhcpMessage packet;
Rob Landley9ffd4232006-05-21 18:30:35 +000029 uint8_t *state, *server_id, *requested;
30 uint32_t server_id_align, requested_align, static_lease_ip;
31 unsigned long timeout_end, num_ips;
Mike Frysinger7031f622006-05-08 03:20:50 +000032 struct option_set *option;
Rob Landley9ffd4232006-05-21 18:30:35 +000033 struct dhcpOfferedAddr *lease, static_lease;
Mike Frysinger7031f622006-05-08 03:20:50 +000034
Mike Frysinger7031f622006-05-08 03:20:50 +000035 read_config(argc < 2 ? DHCPD_CONF_FILE : argv[1]);
36
37 /* Start the log, sanitize fd's, and write a pid file */
Denis Vlasenko239369b2006-09-07 17:05:44 +000038 udhcp_start_log_and_pid(server_config.pidfile);
Mike Frysinger7031f622006-05-08 03:20:50 +000039
40 if ((option = find_option(server_config.options, DHCP_LEASE_TIME))) {
41 memcpy(&server_config.lease, option->data + 2, 4);
42 server_config.lease = ntohl(server_config.lease);
43 }
44 else server_config.lease = LEASE_TIME;
45
46 /* Sanity check */
47 num_ips = ntohl(server_config.end) - ntohl(server_config.start) + 1;
48 if (server_config.max_leases > num_ips) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000049 bb_error_msg("max_leases value (%lu) not sane, "
Mike Frysinger7031f622006-05-08 03:20:50 +000050 "setting to %lu instead",
51 server_config.max_leases, num_ips);
52 server_config.max_leases = num_ips;
53 }
54
Rob Landley9ffd4232006-05-21 18:30:35 +000055 leases = xzalloc(server_config.max_leases * sizeof(struct dhcpOfferedAddr));
Mike Frysinger7031f622006-05-08 03:20:50 +000056 read_leases(server_config.lease_file);
57
58 if (read_interface(server_config.interface, &server_config.ifindex,
59 &server_config.server, server_config.arp) < 0)
60 return 1;
61
Rob Landley3f785612006-05-28 01:06:36 +000062 if (!ENABLE_FEATURE_UDHCP_DEBUG)
63 udhcp_background(server_config.pidfile); /* hold lock during fork. */
Mike Frysinger7031f622006-05-08 03:20:50 +000064
65 /* Setup the signal pipe */
66 udhcp_sp_setup();
67
68 timeout_end = time(0) + server_config.auto_time;
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000069 while (1) { /* loop until universe collapses */
Mike Frysinger7031f622006-05-08 03:20:50 +000070
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +000071 if (server_socket < 0) {
72 server_socket = listen_socket(INADDR_ANY, SERVER_PORT, server_config.interface);
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +000073 }
Mike Frysinger7031f622006-05-08 03:20:50 +000074
75 max_sock = udhcp_sp_fd_set(&rfds, server_socket);
76 if (server_config.auto_time) {
77 tv.tv_sec = timeout_end - time(0);
78 tv.tv_usec = 0;
79 }
80 if (!server_config.auto_time || tv.tv_sec > 0) {
81 retval = select(max_sock + 1, &rfds, NULL, NULL,
82 server_config.auto_time ? &tv : NULL);
83 } else retval = 0; /* If we already timed out, fall through */
84
85 if (retval == 0) {
86 write_leases();
87 timeout_end = time(0) + server_config.auto_time;
88 continue;
89 } else if (retval < 0 && errno != EINTR) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000090 DEBUG("error on select");
Mike Frysinger7031f622006-05-08 03:20:50 +000091 continue;
92 }
93
94 switch (udhcp_sp_read(&rfds)) {
95 case SIGUSR1:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000096 bb_info_msg("Received a SIGUSR1");
Mike Frysinger7031f622006-05-08 03:20:50 +000097 write_leases();
98 /* why not just reset the timeout, eh */
99 timeout_end = time(0) + server_config.auto_time;
100 continue;
101 case SIGTERM:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000102 bb_info_msg("Received a SIGTERM");
Mike Frysinger7031f622006-05-08 03:20:50 +0000103 return 0;
104 case 0: break; /* no signal */
105 default: continue; /* signal or error (probably EINTR) */
106 }
107
Rob Landley3f785612006-05-28 01:06:36 +0000108 if ((bytes = udhcp_get_packet(&packet, server_socket)) < 0) { /* this waits for a packet - idle */
Mike Frysinger7031f622006-05-08 03:20:50 +0000109 if (bytes == -1 && errno != EINTR) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000110 DEBUG("error on read, %s, reopening socket", strerror(errno));
Mike Frysinger7031f622006-05-08 03:20:50 +0000111 close(server_socket);
112 server_socket = -1;
113 }
114 continue;
115 }
116
117 if ((state = get_option(&packet, DHCP_MESSAGE_TYPE)) == NULL) {
Denis Vlasenkoa9595882006-09-29 21:30:43 +0000118 bb_error_msg("cannot get option from packet, ignoring");
Mike Frysinger7031f622006-05-08 03:20:50 +0000119 continue;
120 }
121
122 /* Look for a static lease */
123 static_lease_ip = getIpByMac(server_config.static_leases, &packet.chaddr);
124
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000125 if (static_lease_ip) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000126 bb_info_msg("Found static lease: %x", static_lease_ip);
Mike Frysinger7031f622006-05-08 03:20:50 +0000127
128 memcpy(&static_lease.chaddr, &packet.chaddr, 16);
129 static_lease.yiaddr = static_lease_ip;
130 static_lease.expires = 0;
131
132 lease = &static_lease;
133
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000134 } else {
135 lease = find_lease_by_chaddr(packet.chaddr);
Mike Frysinger7031f622006-05-08 03:20:50 +0000136 }
137
138 switch (state[0]) {
139 case DHCPDISCOVER:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000140 DEBUG("Received DISCOVER");
Mike Frysinger7031f622006-05-08 03:20:50 +0000141
142 if (sendOffer(&packet) < 0) {
Denis Vlasenkoa9595882006-09-29 21:30:43 +0000143 bb_error_msg("send OFFER failed");
Mike Frysinger7031f622006-05-08 03:20:50 +0000144 }
145 break;
146 case DHCPREQUEST:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000147 DEBUG("received REQUEST");
Mike Frysinger7031f622006-05-08 03:20:50 +0000148
149 requested = get_option(&packet, DHCP_REQUESTED_IP);
150 server_id = get_option(&packet, DHCP_SERVER_ID);
151
152 if (requested) memcpy(&requested_align, requested, 4);
153 if (server_id) memcpy(&server_id_align, server_id, 4);
154
155 if (lease) {
156 if (server_id) {
157 /* SELECTING State */
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000158 DEBUG("server_id = %08x", ntohl(server_id_align));
Mike Frysinger7031f622006-05-08 03:20:50 +0000159 if (server_id_align == server_config.server && requested &&
160 requested_align == lease->yiaddr) {
161 sendACK(&packet, lease->yiaddr);
162 }
163 } else {
164 if (requested) {
165 /* INIT-REBOOT State */
166 if (lease->yiaddr == requested_align)
167 sendACK(&packet, lease->yiaddr);
168 else sendNAK(&packet);
169 } else {
170 /* RENEWING or REBINDING State */
171 if (lease->yiaddr == packet.ciaddr)
172 sendACK(&packet, lease->yiaddr);
173 else {
174 /* don't know what to do!!!! */
175 sendNAK(&packet);
176 }
177 }
178 }
179
180 /* what to do if we have no record of the client */
181 } else if (server_id) {
182 /* SELECTING State */
183
184 } else if (requested) {
185 /* INIT-REBOOT State */
186 if ((lease = find_lease_by_yiaddr(requested_align))) {
187 if (lease_expired(lease)) {
188 /* probably best if we drop this lease */
189 memset(lease->chaddr, 0, 16);
190 /* make some contention for this address */
191 } else sendNAK(&packet);
192 } else if (requested_align < server_config.start ||
193 requested_align > server_config.end) {
194 sendNAK(&packet);
195 } /* else remain silent */
196
197 } else {
198 /* RENEWING or REBINDING State */
199 }
200 break;
201 case DHCPDECLINE:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000202 DEBUG("Received DECLINE");
Mike Frysinger7031f622006-05-08 03:20:50 +0000203 if (lease) {
204 memset(lease->chaddr, 0, 16);
205 lease->expires = time(0) + server_config.decline_time;
206 }
207 break;
208 case DHCPRELEASE:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000209 DEBUG("Received RELEASE");
Mike Frysinger7031f622006-05-08 03:20:50 +0000210 if (lease) lease->expires = time(0);
211 break;
212 case DHCPINFORM:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000213 DEBUG("Received INFORM");
Mike Frysinger7031f622006-05-08 03:20:50 +0000214 send_inform(&packet);
215 break;
216 default:
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000217 bb_info_msg("Unsupported DHCP message (%02x) - ignoring", state[0]);
Mike Frysinger7031f622006-05-08 03:20:50 +0000218 }
219 }
220
221 return 0;
222}