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