blob: 28acb6bcc6ed247d4da689b4ffe0f14dc3d27e69 [file] [log] [blame]
Mike Frysinger7031f622006-05-08 03:20:50 +00001/* dhcpd.c
2 *
3 * udhcp Server
4 * Copyright (C) 1999 Matthew Ramsay <matthewr@moreton.com.au>
5 * Chris Trew <ctrew@moreton.com.au>
6 *
7 * Rewrite by Russ Dill <Russ.Dill@asu.edu> July 2001
8 *
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.
13 *
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.
18 *
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.
22 */
23
24#include <fcntl.h>
25#include <string.h>
26#include <stdlib.h>
27#include <sys/wait.h>
28#include <arpa/inet.h>
29#include <netdb.h>
30#include <netinet/in.h>
31#include <sys/socket.h>
32#include <unistd.h>
33#include <signal.h>
34#include <errno.h>
35#include <sys/ioctl.h>
36#include <time.h>
37#include <sys/time.h>
38
39#include "dhcpd.h"
40#include "arpping.h"
41#include "socket.h"
42#include "options.h"
43#include "files.h"
44#include "serverpacket.h"
45#include "common.h"
46#include "signalpipe.h"
47#include "static_leases.h"
48
49
50/* globals */
51struct dhcpOfferedAddr *leases;
52struct server_config_t server_config;
53
54
Mike Frysinger7031f622006-05-08 03:20:50 +000055int udhcpd_main(int argc, char *argv[])
Mike Frysinger7031f622006-05-08 03:20:50 +000056{
57 fd_set rfds;
58 struct timeval tv;
Rob Landley9ffd4232006-05-21 18:30:35 +000059 int server_socket = -1, bytes, retval, max_sock;
Mike Frysinger7031f622006-05-08 03:20:50 +000060 struct dhcpMessage packet;
Rob Landley9ffd4232006-05-21 18:30:35 +000061 uint8_t *state, *server_id, *requested;
62 uint32_t server_id_align, requested_align, static_lease_ip;
63 unsigned long timeout_end, num_ips;
Mike Frysinger7031f622006-05-08 03:20:50 +000064 struct option_set *option;
Rob Landley9ffd4232006-05-21 18:30:35 +000065 struct dhcpOfferedAddr *lease, static_lease;
Mike Frysinger7031f622006-05-08 03:20:50 +000066
Mike Frysinger7031f622006-05-08 03:20:50 +000067 read_config(argc < 2 ? DHCPD_CONF_FILE : argv[1]);
68
69 /* Start the log, sanitize fd's, and write a pid file */
70 start_log_and_pid("udhcpd", server_config.pidfile);
71
72 if ((option = find_option(server_config.options, DHCP_LEASE_TIME))) {
73 memcpy(&server_config.lease, option->data + 2, 4);
74 server_config.lease = ntohl(server_config.lease);
75 }
76 else server_config.lease = LEASE_TIME;
77
78 /* Sanity check */
79 num_ips = ntohl(server_config.end) - ntohl(server_config.start) + 1;
80 if (server_config.max_leases > num_ips) {
81 LOG(LOG_ERR, "max_leases value (%lu) not sane, "
82 "setting to %lu instead",
83 server_config.max_leases, num_ips);
84 server_config.max_leases = num_ips;
85 }
86
Rob Landley9ffd4232006-05-21 18:30:35 +000087 leases = xzalloc(server_config.max_leases * sizeof(struct dhcpOfferedAddr));
Mike Frysinger7031f622006-05-08 03:20:50 +000088 read_leases(server_config.lease_file);
89
90 if (read_interface(server_config.interface, &server_config.ifindex,
91 &server_config.server, server_config.arp) < 0)
92 return 1;
93
94#ifndef UDHCP_DEBUG
95 background(server_config.pidfile); /* hold lock during fork. */
96#endif
97
98 /* Setup the signal pipe */
99 udhcp_sp_setup();
100
101 timeout_end = time(0) + server_config.auto_time;
102 while(1) { /* loop until universe collapses */
103
104 if (server_socket < 0)
105 if ((server_socket = listen_socket(INADDR_ANY, SERVER_PORT, server_config.interface)) < 0) {
106 LOG(LOG_ERR, "FATAL: couldn't create server socket, %m");
107 return 2;
108 }
109
110 max_sock = udhcp_sp_fd_set(&rfds, server_socket);
111 if (server_config.auto_time) {
112 tv.tv_sec = timeout_end - time(0);
113 tv.tv_usec = 0;
114 }
115 if (!server_config.auto_time || tv.tv_sec > 0) {
116 retval = select(max_sock + 1, &rfds, NULL, NULL,
117 server_config.auto_time ? &tv : NULL);
118 } else retval = 0; /* If we already timed out, fall through */
119
120 if (retval == 0) {
121 write_leases();
122 timeout_end = time(0) + server_config.auto_time;
123 continue;
124 } else if (retval < 0 && errno != EINTR) {
125 DEBUG(LOG_INFO, "error on select");
126 continue;
127 }
128
129 switch (udhcp_sp_read(&rfds)) {
130 case SIGUSR1:
131 LOG(LOG_INFO, "Received a SIGUSR1");
132 write_leases();
133 /* why not just reset the timeout, eh */
134 timeout_end = time(0) + server_config.auto_time;
135 continue;
136 case SIGTERM:
137 LOG(LOG_INFO, "Received a SIGTERM");
138 return 0;
139 case 0: break; /* no signal */
140 default: continue; /* signal or error (probably EINTR) */
141 }
142
143 if ((bytes = get_packet(&packet, server_socket)) < 0) { /* this waits for a packet - idle */
144 if (bytes == -1 && errno != EINTR) {
145 DEBUG(LOG_INFO, "error on read, %m, reopening socket");
146 close(server_socket);
147 server_socket = -1;
148 }
149 continue;
150 }
151
152 if ((state = get_option(&packet, DHCP_MESSAGE_TYPE)) == NULL) {
153 DEBUG(LOG_ERR, "couldn't get option from packet, ignoring");
154 continue;
155 }
156
157 /* Look for a static lease */
158 static_lease_ip = getIpByMac(server_config.static_leases, &packet.chaddr);
159
160 if(static_lease_ip)
161 {
162 printf("Found static lease: %x\n", static_lease_ip);
163
164 memcpy(&static_lease.chaddr, &packet.chaddr, 16);
165 static_lease.yiaddr = static_lease_ip;
166 static_lease.expires = 0;
167
168 lease = &static_lease;
169
170 }
171 else
172 {
173 lease = find_lease_by_chaddr(packet.chaddr);
174 }
175
176 switch (state[0]) {
177 case DHCPDISCOVER:
178 DEBUG(LOG_INFO,"received DISCOVER");
179
180 if (sendOffer(&packet) < 0) {
181 LOG(LOG_ERR, "send OFFER failed");
182 }
183 break;
184 case DHCPREQUEST:
185 DEBUG(LOG_INFO, "received REQUEST");
186
187 requested = get_option(&packet, DHCP_REQUESTED_IP);
188 server_id = get_option(&packet, DHCP_SERVER_ID);
189
190 if (requested) memcpy(&requested_align, requested, 4);
191 if (server_id) memcpy(&server_id_align, server_id, 4);
192
193 if (lease) {
194 if (server_id) {
195 /* SELECTING State */
196 DEBUG(LOG_INFO, "server_id = %08x", ntohl(server_id_align));
197 if (server_id_align == server_config.server && requested &&
198 requested_align == lease->yiaddr) {
199 sendACK(&packet, lease->yiaddr);
200 }
201 } else {
202 if (requested) {
203 /* INIT-REBOOT State */
204 if (lease->yiaddr == requested_align)
205 sendACK(&packet, lease->yiaddr);
206 else sendNAK(&packet);
207 } else {
208 /* RENEWING or REBINDING State */
209 if (lease->yiaddr == packet.ciaddr)
210 sendACK(&packet, lease->yiaddr);
211 else {
212 /* don't know what to do!!!! */
213 sendNAK(&packet);
214 }
215 }
216 }
217
218 /* what to do if we have no record of the client */
219 } else if (server_id) {
220 /* SELECTING State */
221
222 } else if (requested) {
223 /* INIT-REBOOT State */
224 if ((lease = find_lease_by_yiaddr(requested_align))) {
225 if (lease_expired(lease)) {
226 /* probably best if we drop this lease */
227 memset(lease->chaddr, 0, 16);
228 /* make some contention for this address */
229 } else sendNAK(&packet);
230 } else if (requested_align < server_config.start ||
231 requested_align > server_config.end) {
232 sendNAK(&packet);
233 } /* else remain silent */
234
235 } else {
236 /* RENEWING or REBINDING State */
237 }
238 break;
239 case DHCPDECLINE:
240 DEBUG(LOG_INFO,"received DECLINE");
241 if (lease) {
242 memset(lease->chaddr, 0, 16);
243 lease->expires = time(0) + server_config.decline_time;
244 }
245 break;
246 case DHCPRELEASE:
247 DEBUG(LOG_INFO,"received RELEASE");
248 if (lease) lease->expires = time(0);
249 break;
250 case DHCPINFORM:
251 DEBUG(LOG_INFO,"received INFORM");
252 send_inform(&packet);
253 break;
254 default:
255 LOG(LOG_WARNING, "unsupported DHCP message (%02x) -- ignoring", state[0]);
256 }
257 }
258
259 return 0;
260}
261