blob: 795ac48411cb3de3f7811c92f236e5e7bf729b97 [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Denys Vlasenko8a7c1662010-03-20 03:48:11 +01002/*
Mike Frysinger7031f622006-05-08 03:20:50 +00003 * 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 *
Denys Vlasenko8a7c1662010-03-20 03:48:11 +01009 * 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.
Mike Frysinger7031f622006-05-08 03:20:50 +000022 */
23
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +000024#include <syslog.h>
Mike Frysinger7031f622006-05-08 03:20:50 +000025#include "common.h"
Denis Vlasenkod55fe3e2008-02-04 13:12:16 +000026#include "dhcpc.h"
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000027#include "dhcpd.h"
Mike Frysinger7031f622006-05-08 03:20:50 +000028
29
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +010030/* Send a packet to a specific mac address and ip address by creating our own ip packet */
31static void send_packet_to_client(struct dhcp_packet *dhcp_pkt, int force_broadcast)
Denys Vlasenko8a7c1662010-03-20 03:48:11 +010032{
33 const uint8_t *chaddr;
34 uint32_t ciaddr;
35
36 // Was:
37 //if (force_broadcast) { /* broadcast */ }
38 //else if (dhcp_pkt->ciaddr) { /* unicast to dhcp_pkt->ciaddr */ }
39 //else if (dhcp_pkt->flags & htons(BROADCAST_FLAG)) { /* broadcast */ }
40 //else { /* unicast to dhcp_pkt->yiaddr */ }
41 // But this is wrong: yiaddr is _our_ idea what client's IP is
42 // (for example, from lease file). Client may not know that,
43 // and may not have UDP socket listening on that IP!
44 // We should never unicast to dhcp_pkt->yiaddr!
45 // dhcp_pkt->ciaddr, OTOH, comes from client's request packet,
46 // and can be used.
47
48 if (force_broadcast
49 || (dhcp_pkt->flags & htons(BROADCAST_FLAG))
Denys Vlasenko53f72bb2010-03-21 06:46:09 +010050 || dhcp_pkt->ciaddr == 0
Denys Vlasenko8a7c1662010-03-20 03:48:11 +010051 ) {
52 log1("Broadcasting packet to client");
53 ciaddr = INADDR_BROADCAST;
54 chaddr = MAC_BCAST_ADDR;
55 } else {
56 log1("Unicasting packet to client ciaddr");
57 ciaddr = dhcp_pkt->ciaddr;
58 chaddr = dhcp_pkt->chaddr;
59 }
60
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +010061 udhcp_send_raw_packet(dhcp_pkt,
Denys Vlasenko8a7c1662010-03-20 03:48:11 +010062 /*src*/ server_config.server_nip, SERVER_PORT,
63 /*dst*/ ciaddr, CLIENT_PORT, chaddr,
64 server_config.ifindex);
65}
66
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +010067/* Send a packet to gateway_nip using the kernel ip stack */
68static void send_packet_to_relay(struct dhcp_packet *dhcp_pkt)
69{
70 log1("Forwarding packet to relay");
71
72 udhcp_send_kernel_packet(dhcp_pkt,
73 server_config.server_nip, SERVER_PORT,
74 dhcp_pkt->gateway_nip, SERVER_PORT);
75}
76
77static void send_packet(struct dhcp_packet *dhcp_pkt, int force_broadcast)
Denys Vlasenko8a7c1662010-03-20 03:48:11 +010078{
79 if (dhcp_pkt->gateway_nip)
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +010080 send_packet_to_relay(dhcp_pkt);
81 else
82 send_packet_to_client(dhcp_pkt, force_broadcast);
Denys Vlasenko8a7c1662010-03-20 03:48:11 +010083}
84
85static void init_packet(struct dhcp_packet *packet, struct dhcp_packet *oldpacket, char type)
86{
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +010087 /* Sets op, htype, hlen, cookie fields
88 * and adds DHCP_MESSAGE_TYPE option */
Denys Vlasenko8a7c1662010-03-20 03:48:11 +010089 udhcp_init_header(packet, type);
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +010090
Denys Vlasenko8a7c1662010-03-20 03:48:11 +010091 packet->xid = oldpacket->xid;
92 memcpy(packet->chaddr, oldpacket->chaddr, sizeof(oldpacket->chaddr));
93 packet->flags = oldpacket->flags;
94 packet->gateway_nip = oldpacket->gateway_nip;
95 packet->ciaddr = oldpacket->ciaddr;
Denys Vlasenkodde8bdc2010-03-22 14:29:13 +010096 udhcp_add_simple_option(packet->options, DHCP_SERVER_ID, server_config.server_nip);
Denys Vlasenko8a7c1662010-03-20 03:48:11 +010097}
98
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +010099/* Fill options field, siaddr_nip, and sname and boot_file fields.
100 * TODO: teach this code to use overload option.
101 */
102static void add_server_options(struct dhcp_packet *packet)
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100103{
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +0100104 struct option_set *curr = server_config.options;
105
106 while (curr) {
107 if (curr->data[OPT_CODE] != DHCP_LEASE_TIME)
Denys Vlasenkodde8bdc2010-03-22 14:29:13 +0100108 udhcp_add_option_string(packet->options, curr->data);
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +0100109 curr = curr->next;
110 }
111
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100112 packet->siaddr_nip = server_config.siaddr_nip;
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +0100113
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100114 if (server_config.sname)
115 strncpy((char*)packet->sname, server_config.sname, sizeof(packet->sname) - 1);
116 if (server_config.boot_file)
117 strncpy((char*)packet->file, server_config.boot_file, sizeof(packet->file) - 1);
118}
119
120static uint32_t select_lease_time(struct dhcp_packet *packet)
121{
122 uint32_t lease_time_sec = server_config.max_lease_sec;
Denys Vlasenkodde8bdc2010-03-22 14:29:13 +0100123 uint8_t *lease_time_opt = udhcp_get_option(packet, DHCP_LEASE_TIME);
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100124 if (lease_time_opt) {
125 move_from_unaligned32(lease_time_sec, lease_time_opt);
126 lease_time_sec = ntohl(lease_time_sec);
127 if (lease_time_sec > server_config.max_lease_sec)
128 lease_time_sec = server_config.max_lease_sec;
129 if (lease_time_sec < server_config.min_lease_sec)
130 lease_time_sec = server_config.min_lease_sec;
131 }
132 return lease_time_sec;
133}
134
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100135/* We got a DHCP DISCOVER. Send an OFFER. */
136static void send_offer(struct dhcp_packet *oldpacket, uint32_t static_lease_nip, struct dyn_lease *lease)
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100137{
138 struct dhcp_packet packet;
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100139 uint32_t lease_time_sec;
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100140 struct in_addr addr;
141
142 init_packet(&packet, oldpacket, DHCPOFFER);
143
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100144 /* If it is a static lease, use its IP */
145 packet.yiaddr = static_lease_nip;
146 /* Else: */
Denys Vlasenkoa9539872010-03-20 03:49:27 +0100147 if (!static_lease_nip) {
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100148 /* We have no static lease for client's chaddr */
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +0100149 uint32_t req_nip;
150 uint8_t *req_ip_opt;
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100151 const char *p_host_name;
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +0100152
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100153 if (lease) {
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100154 /* We have a dynamic lease for client's chaddr.
155 * Reuse its IP (even if lease is expired).
156 * Note that we ignore requested IP in this case.
157 */
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100158 packet.yiaddr = lease->lease_nip;
159 }
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100160 /* Or: if client has requested an IP */
Denys Vlasenkodde8bdc2010-03-22 14:29:13 +0100161 else if ((req_ip_opt = udhcp_get_option(oldpacket, DHCP_REQUESTED_IP)) != NULL
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100162 /* (read IP) */
163 && (move_from_unaligned32(req_nip, req_ip_opt), 1)
164 /* and the IP is in the lease range */
165 && ntohl(req_nip) >= server_config.start_ip
166 && ntohl(req_nip) <= server_config.end_ip
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +0100167 /* and */
168 && ( !(lease = find_lease_by_nip(req_nip)) /* is not already taken */
169 || is_expired_lease(lease) /* or is taken, but expired */
170 )
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100171 ) {
172 packet.yiaddr = req_nip;
173 }
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100174 else {
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100175 /* Otherwise, find a free IP */
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100176 packet.yiaddr = find_free_or_expired_nip(oldpacket->chaddr);
177 }
178
179 if (!packet.yiaddr) {
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100180 bb_error_msg("no free IP addresses. OFFER abandoned");
181 return;
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100182 }
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100183 /* Reserve the IP for a short time hoping to get DHCPREQUEST soon */
Denys Vlasenkodde8bdc2010-03-22 14:29:13 +0100184 p_host_name = (const char*) udhcp_get_option(oldpacket, DHCP_HOST_NAME);
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100185 lease = add_lease(packet.chaddr, packet.yiaddr,
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100186 server_config.offer_time,
187 p_host_name,
188 p_host_name ? (unsigned char)p_host_name[OPT_LEN - OPT_DATA] : 0
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100189 );
190 if (!lease) {
191 bb_error_msg("no free IP addresses. OFFER abandoned");
192 return;
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100193 }
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100194 }
195
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100196 lease_time_sec = select_lease_time(oldpacket);
Denys Vlasenkodde8bdc2010-03-22 14:29:13 +0100197 udhcp_add_simple_option(packet.options, DHCP_LEASE_TIME, htonl(lease_time_sec));
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +0100198 add_server_options(&packet);
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100199
200 addr.s_addr = packet.yiaddr;
201 bb_info_msg("Sending OFFER of %s", inet_ntoa(addr));
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100202 /* send_packet emits error message itself if it detects failure */
203 send_packet(&packet, /*force_bcast:*/ 0);
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100204}
205
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100206static void send_NAK(struct dhcp_packet *oldpacket)
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100207{
208 struct dhcp_packet packet;
209
210 init_packet(&packet, oldpacket, DHCPNAK);
211
212 log1("Sending NAK");
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100213 send_packet(&packet, /*force_bcast:*/ 1);
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100214}
215
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100216static void send_ACK(struct dhcp_packet *oldpacket, uint32_t yiaddr)
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100217{
218 struct dhcp_packet packet;
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100219 uint32_t lease_time_sec;
220 struct in_addr addr;
221 const char *p_host_name;
222
223 init_packet(&packet, oldpacket, DHCPACK);
224 packet.yiaddr = yiaddr;
225
226 lease_time_sec = select_lease_time(oldpacket);
Denys Vlasenkodde8bdc2010-03-22 14:29:13 +0100227 udhcp_add_simple_option(packet.options, DHCP_LEASE_TIME, htonl(lease_time_sec));
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100228
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +0100229 add_server_options(&packet);
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100230
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100231 addr.s_addr = yiaddr;
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100232 bb_info_msg("Sending ACK to %s", inet_ntoa(addr));
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100233 send_packet(&packet, /*force_bcast:*/ 0);
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100234
Denys Vlasenkodde8bdc2010-03-22 14:29:13 +0100235 p_host_name = (const char*) udhcp_get_option(oldpacket, DHCP_HOST_NAME);
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100236 add_lease(packet.chaddr, packet.yiaddr,
237 lease_time_sec,
238 p_host_name,
239 p_host_name ? (unsigned char)p_host_name[OPT_LEN - OPT_DATA] : 0
240 );
241 if (ENABLE_FEATURE_UDHCPD_WRITE_LEASES_EARLY) {
242 /* rewrite the file with leases at every new acceptance */
243 write_leases();
244 }
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100245}
246
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100247static void send_inform(struct dhcp_packet *oldpacket)
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100248{
249 struct dhcp_packet packet;
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100250
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100251 /* "The server responds to a DHCPINFORM message by sending a DHCPACK
252 * message directly to the address given in the 'ciaddr' field
253 * of the DHCPINFORM message. The server MUST NOT send a lease
254 * expiration time to the client and SHOULD NOT fill in 'yiaddr'."
255 */
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100256 init_packet(&packet, oldpacket, DHCPACK);
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +0100257 add_server_options(&packet);
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100258
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100259 send_packet(&packet, /*force_bcast:*/ 0);
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100260}
261
262
Mike Frysinger7031f622006-05-08 03:20:50 +0000263/* globals */
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200264struct dyn_lease *g_leases;
Denis Vlasenkodeabacd2007-09-30 17:55:43 +0000265/* struct server_config_t server_config is in bb_common_bufsiz1 */
Mike Frysinger7031f622006-05-08 03:20:50 +0000266
267
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000268int udhcpd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000269int udhcpd_main(int argc UNUSED_PARAM, char **argv)
Mike Frysinger7031f622006-05-08 03:20:50 +0000270{
271 fd_set rfds;
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000272 int server_socket = -1, retval, max_sock;
Denys Vlasenko31af3d52009-06-17 11:57:09 +0200273 struct dhcp_packet packet;
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200274 uint8_t *state;
Denys Vlasenkoa9539872010-03-20 03:49:27 +0100275 uint32_t static_lease_nip;
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000276 unsigned timeout_end;
277 unsigned num_ips;
Denis Vlasenko3d17d2b2007-08-14 16:45:29 +0000278 unsigned opt;
Mike Frysinger7031f622006-05-08 03:20:50 +0000279 struct option_set *option;
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200280 struct dyn_lease *lease, fake_lease;
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000281 IF_FEATURE_UDHCP_PORT(char *str_P;)
Mike Frysinger7031f622006-05-08 03:20:50 +0000282
Denis Vlasenkod55fe3e2008-02-04 13:12:16 +0000283#if ENABLE_FEATURE_UDHCP_PORT
284 SERVER_PORT = 67;
285 CLIENT_PORT = 68;
286#endif
287
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200288#if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1
289 opt_complementary = "vv";
290#endif
291 opt = getopt32(argv, "fSv"
292 IF_FEATURE_UDHCP_PORT("P:", &str_P)
293#if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1
294 , &dhcp_verbose
295#endif
296 );
Denis Vlasenko9f7b92a2007-08-15 20:03:36 +0000297 argv += optind;
Denis Vlasenko3d17d2b2007-08-14 16:45:29 +0000298 if (!(opt & 1)) { /* no -f */
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000299 bb_daemonize_or_rexec(0, argv);
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000300 logmode = LOGMODE_NONE;
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000301 }
Denis Vlasenko3d17d2b2007-08-14 16:45:29 +0000302 if (opt & 2) { /* -S */
Denis Vlasenko5e4fda02009-03-08 23:46:48 +0000303 openlog(applet_name, LOG_PID, LOG_DAEMON);
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000304 logmode |= LOGMODE_SYSLOG;
305 }
Denis Vlasenkod55fe3e2008-02-04 13:12:16 +0000306#if ENABLE_FEATURE_UDHCP_PORT
307 if (opt & 4) { /* -P */
308 SERVER_PORT = xatou16(str_P);
309 CLIENT_PORT = SERVER_PORT + 1;
310 }
311#endif
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000312 /* Would rather not do read_config before daemonization -
313 * otherwise NOMMU machines will parse config twice */
Denis Vlasenko9f7b92a2007-08-15 20:03:36 +0000314 read_config(argv[0] ? argv[0] : DHCPD_CONF_FILE);
Mike Frysinger7031f622006-05-08 03:20:50 +0000315
Denis Vlasenko80edead2007-08-02 22:31:05 +0000316 /* Make sure fd 0,1,2 are open */
317 bb_sanitize_stdio();
318 /* Equivalent of doing a fflush after every \n */
319 setlinebuf(stdout);
320
321 /* Create pidfile */
322 write_pidfile(server_config.pidfile);
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100323 /* if (!..) bb_perror_msg("can't create pidfile %s", pidfile); */
Denis Vlasenko80edead2007-08-02 22:31:05 +0000324
Denis Vlasenkodef88982007-10-07 17:06:01 +0000325 bb_info_msg("%s (v"BB_VER") started", applet_name);
Mike Frysinger7031f622006-05-08 03:20:50 +0000326
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000327 option = find_option(server_config.options, DHCP_LEASE_TIME);
Denys Vlasenko2e7aa922010-03-21 02:22:07 +0100328 server_config.max_lease_sec = DEFAULT_LEASE_TIME;
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000329 if (option) {
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200330 move_from_unaligned32(server_config.max_lease_sec, option->data + OPT_DATA);
331 server_config.max_lease_sec = ntohl(server_config.max_lease_sec);
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000332 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000333
334 /* Sanity check */
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000335 num_ips = server_config.end_ip - server_config.start_ip + 1;
Mike Frysinger7031f622006-05-08 03:20:50 +0000336 if (server_config.max_leases > num_ips) {
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000337 bb_error_msg("max_leases=%u is too big, setting to %u",
338 (unsigned)server_config.max_leases, num_ips);
Mike Frysinger7031f622006-05-08 03:20:50 +0000339 server_config.max_leases = num_ips;
340 }
341
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200342 g_leases = xzalloc(server_config.max_leases * sizeof(g_leases[0]));
Mike Frysinger7031f622006-05-08 03:20:50 +0000343 read_leases(server_config.lease_file);
344
Denys Vlasenko26918dd2009-06-16 12:04:23 +0200345 if (udhcp_read_interface(server_config.interface,
346 &server_config.ifindex,
347 &server_config.server_nip,
348 server_config.server_mac)
349 ) {
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000350 retval = 1;
351 goto ret;
352 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000353
Mike Frysinger7031f622006-05-08 03:20:50 +0000354 /* Setup the signal pipe */
355 udhcp_sp_setup();
356
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000357 timeout_end = monotonic_sec() + server_config.auto_time;
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000358 while (1) { /* loop until universe collapses */
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000359 int bytes;
360 struct timeval tv;
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100361 uint8_t *server_id_opt;
362 uint8_t *requested_opt;
363 uint32_t requested_nip = requested_nip; /* for compiler */
Mike Frysinger7031f622006-05-08 03:20:50 +0000364
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000365 if (server_socket < 0) {
Denis Vlasenkof1980f62008-09-26 09:34:59 +0000366 server_socket = udhcp_listen_socket(/*INADDR_ANY,*/ SERVER_PORT,
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000367 server_config.interface);
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000368 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000369
370 max_sock = udhcp_sp_fd_set(&rfds, server_socket);
371 if (server_config.auto_time) {
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000372 tv.tv_sec = timeout_end - monotonic_sec();
Mike Frysinger7031f622006-05-08 03:20:50 +0000373 tv.tv_usec = 0;
374 }
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000375 retval = 0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000376 if (!server_config.auto_time || tv.tv_sec > 0) {
377 retval = select(max_sock + 1, &rfds, NULL, NULL,
378 server_config.auto_time ? &tv : NULL);
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000379 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000380 if (retval == 0) {
381 write_leases();
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000382 timeout_end = monotonic_sec() + server_config.auto_time;
Mike Frysinger7031f622006-05-08 03:20:50 +0000383 continue;
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000384 }
385 if (retval < 0 && errno != EINTR) {
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200386 log1("Error on select");
Mike Frysinger7031f622006-05-08 03:20:50 +0000387 continue;
388 }
389
390 switch (udhcp_sp_read(&rfds)) {
391 case SIGUSR1:
Denys Vlasenko651a2692010-03-23 16:25:17 +0100392 bb_info_msg("Received SIGUSR1");
Mike Frysinger7031f622006-05-08 03:20:50 +0000393 write_leases();
394 /* why not just reset the timeout, eh */
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000395 timeout_end = monotonic_sec() + server_config.auto_time;
Mike Frysinger7031f622006-05-08 03:20:50 +0000396 continue;
397 case SIGTERM:
Denys Vlasenko651a2692010-03-23 16:25:17 +0100398 bb_info_msg("Received SIGTERM");
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000399 goto ret0;
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000400 case 0: /* no signal: read a packet */
401 break;
402 default: /* signal or error (probably EINTR): back to select */
403 continue;
Mike Frysinger7031f622006-05-08 03:20:50 +0000404 }
405
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000406 bytes = udhcp_recv_kernel_packet(&packet, server_socket);
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000407 if (bytes < 0) {
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000408 /* bytes can also be -2 ("bad packet data") */
Mike Frysinger7031f622006-05-08 03:20:50 +0000409 if (bytes == -1 && errno != EINTR) {
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200410 log1("Read error: %s, reopening socket", strerror(errno));
Mike Frysinger7031f622006-05-08 03:20:50 +0000411 close(server_socket);
412 server_socket = -1;
413 }
414 continue;
415 }
Denys Vlasenko31af3d52009-06-17 11:57:09 +0200416 if (packet.hlen != 6) {
417 bb_error_msg("MAC length != 6, ignoring packet");
418 continue;
419 }
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100420 if (packet.op != BOOTREQUEST) {
421 bb_error_msg("not a REQUEST, ignoring packet");
422 continue;
423 }
Denys Vlasenkodde8bdc2010-03-22 14:29:13 +0100424 state = udhcp_get_option(&packet, DHCP_MESSAGE_TYPE);
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100425 if (state == NULL || state[0] < DHCP_MINTYPE || state[0] > DHCP_MAXTYPE) {
426 bb_error_msg("no or bad message type option, ignoring packet");
Mike Frysinger7031f622006-05-08 03:20:50 +0000427 continue;
428 }
429
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100430 /* Look for a static/dynamic lease */
Denys Vlasenkoa9539872010-03-20 03:49:27 +0100431 static_lease_nip = get_static_nip_by_mac(server_config.static_leases, &packet.chaddr);
432 if (static_lease_nip) {
433 bb_info_msg("Found static lease: %x", static_lease_nip);
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200434 memcpy(&fake_lease.lease_mac, &packet.chaddr, 6);
Denys Vlasenkoa9539872010-03-20 03:49:27 +0100435 fake_lease.lease_nip = static_lease_nip;
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200436 fake_lease.expires = 0;
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200437 lease = &fake_lease;
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000438 } else {
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200439 lease = find_lease_by_mac(packet.chaddr);
Mike Frysinger7031f622006-05-08 03:20:50 +0000440 }
441
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100442 /* Get REQUESTED_IP and SERVER_ID if present */
Denys Vlasenkodde8bdc2010-03-22 14:29:13 +0100443 server_id_opt = udhcp_get_option(&packet, DHCP_SERVER_ID);
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100444 if (server_id_opt) {
445 uint32_t server_id_net;
446 move_from_unaligned32(server_id_net, server_id_opt);
447 if (server_id_net != server_config.server_nip) {
448 /* client talks to somebody else */
449 log1("server ID doesn't match, ignoring");
450 continue;
451 }
452 }
Denys Vlasenkodde8bdc2010-03-22 14:29:13 +0100453 requested_opt = udhcp_get_option(&packet, DHCP_REQUESTED_IP);
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100454 if (requested_opt) {
455 move_from_unaligned32(requested_nip, requested_opt);
456 }
457
Mike Frysinger7031f622006-05-08 03:20:50 +0000458 switch (state[0]) {
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100459
Mike Frysinger7031f622006-05-08 03:20:50 +0000460 case DHCPDISCOVER:
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200461 log1("Received DISCOVER");
Mike Frysinger7031f622006-05-08 03:20:50 +0000462
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100463 send_offer(&packet, static_lease_nip, lease);
Mike Frysinger7031f622006-05-08 03:20:50 +0000464 break;
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200465
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100466 case DHCPREQUEST:
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200467 log1("Received REQUEST");
Denys Vlasenko53f72bb2010-03-21 06:46:09 +0100468/* RFC 2131:
Mike Frysinger7031f622006-05-08 03:20:50 +0000469
Denys Vlasenko53f72bb2010-03-21 06:46:09 +0100470o DHCPREQUEST generated during SELECTING state:
471
472 Client inserts the address of the selected server in 'server
473 identifier', 'ciaddr' MUST be zero, 'requested IP address' MUST be
474 filled in with the yiaddr value from the chosen DHCPOFFER.
475
476 Note that the client may choose to collect several DHCPOFFER
477 messages and select the "best" offer. The client indicates its
478 selection by identifying the offering server in the DHCPREQUEST
479 message. If the client receives no acceptable offers, the client
480 may choose to try another DHCPDISCOVER message. Therefore, the
481 servers may not receive a specific DHCPREQUEST from which they can
482 decide whether or not the client has accepted the offer.
483
484o DHCPREQUEST generated during INIT-REBOOT state:
485
486 'server identifier' MUST NOT be filled in, 'requested IP address'
487 option MUST be filled in with client's notion of its previously
488 assigned address. 'ciaddr' MUST be zero. The client is seeking to
489 verify a previously allocated, cached configuration. Server SHOULD
490 send a DHCPNAK message to the client if the 'requested IP address'
491 is incorrect, or is on the wrong network.
492
493 Determining whether a client in the INIT-REBOOT state is on the
494 correct network is done by examining the contents of 'giaddr', the
495 'requested IP address' option, and a database lookup. If the DHCP
496 server detects that the client is on the wrong net (i.e., the
497 result of applying the local subnet mask or remote subnet mask (if
498 'giaddr' is not zero) to 'requested IP address' option value
499 doesn't match reality), then the server SHOULD send a DHCPNAK
500 message to the client.
501
502 If the network is correct, then the DHCP server should check if
503 the client's notion of its IP address is correct. If not, then the
504 server SHOULD send a DHCPNAK message to the client. If the DHCP
505 server has no record of this client, then it MUST remain silent,
506 and MAY output a warning to the network administrator. This
507 behavior is necessary for peaceful coexistence of non-
508 communicating DHCP servers on the same wire.
509
510 If 'giaddr' is 0x0 in the DHCPREQUEST message, the client is on
511 the same subnet as the server. The server MUST broadcast the
512 DHCPNAK message to the 0xffffffff broadcast address because the
513 client may not have a correct network address or subnet mask, and
514 the client may not be answering ARP requests.
515
516 If 'giaddr' is set in the DHCPREQUEST message, the client is on a
517 different subnet. The server MUST set the broadcast bit in the
518 DHCPNAK, so that the relay agent will broadcast the DHCPNAK to the
519 client, because the client may not have a correct network address
520 or subnet mask, and the client may not be answering ARP requests.
521
522o DHCPREQUEST generated during RENEWING state:
523
524 'server identifier' MUST NOT be filled in, 'requested IP address'
525 option MUST NOT be filled in, 'ciaddr' MUST be filled in with
526 client's IP address. In this situation, the client is completely
527 configured, and is trying to extend its lease. This message will
528 be unicast, so no relay agents will be involved in its
529 transmission. Because 'giaddr' is therefore not filled in, the
530 DHCP server will trust the value in 'ciaddr', and use it when
531 replying to the client.
532
533 A client MAY choose to renew or extend its lease prior to T1. The
534 server may choose not to extend the lease (as a policy decision by
535 the network administrator), but should return a DHCPACK message
536 regardless.
537
538o DHCPREQUEST generated during REBINDING state:
539
540 'server identifier' MUST NOT be filled in, 'requested IP address'
541 option MUST NOT be filled in, 'ciaddr' MUST be filled in with
542 client's IP address. In this situation, the client is completely
543 configured, and is trying to extend its lease. This message MUST
544 be broadcast to the 0xffffffff IP broadcast address. The DHCP
545 server SHOULD check 'ciaddr' for correctness before replying to
546 the DHCPREQUEST.
547
548 The DHCPREQUEST from a REBINDING client is intended to accommodate
549 sites that have multiple DHCP servers and a mechanism for
550 maintaining consistency among leases managed by multiple servers.
551 A DHCP server MAY extend a client's lease only if it has local
552 administrative authority to do so.
553*/
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100554 if (!requested_opt) {
Denys Vlasenko53f72bb2010-03-21 06:46:09 +0100555 requested_nip = packet.ciaddr;
556 if (requested_nip == 0) {
557 log1("no requested IP and no ciaddr, ignoring");
558 break;
559 }
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100560 }
561 if (lease && requested_nip == lease->lease_nip) {
Denys Vlasenko53f72bb2010-03-21 06:46:09 +0100562 /* client requested or configured IP matches the lease.
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100563 * ACK it, and bump lease expiration time. */
564 send_ACK(&packet, lease->lease_nip);
565 break;
566 }
567 if (server_id_opt) {
568 /* client was talking specifically to us.
569 * "No, we don't have this IP for you". */
570 send_NAK(&packet);
Mike Frysinger7031f622006-05-08 03:20:50 +0000571 }
572 break;
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100573
Mike Frysinger7031f622006-05-08 03:20:50 +0000574 case DHCPDECLINE:
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100575 /* RFC 2131:
576 * "If the server receives a DHCPDECLINE message,
577 * the client has discovered through some other means
578 * that the suggested network address is already
579 * in use. The server MUST mark the network address
580 * as not available and SHOULD notify the local
581 * sysadmin of a possible configuration problem."
582 *
583 * SERVER_ID must be present,
584 * REQUESTED_IP must be present,
585 * chaddr must be filled in,
586 * ciaddr must be 0 (we do not check this)
587 */
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200588 log1("Received DECLINE");
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100589 if (server_id_opt
590 && requested_opt
591 && lease /* chaddr matches this lease */
592 && requested_nip == lease->lease_nip
593 ) {
Denys Vlasenko31af3d52009-06-17 11:57:09 +0200594 memset(lease->lease_mac, 0, sizeof(lease->lease_mac));
Denis Vlasenko04158e02009-02-02 10:48:06 +0000595 lease->expires = time(NULL) + server_config.decline_time;
Mike Frysinger7031f622006-05-08 03:20:50 +0000596 }
597 break;
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100598
Mike Frysinger7031f622006-05-08 03:20:50 +0000599 case DHCPRELEASE:
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100600 /* "Upon receipt of a DHCPRELEASE message, the server
601 * marks the network address as not allocated."
602 *
603 * SERVER_ID must be present,
604 * REQUESTED_IP must not be present (we do not check this),
605 * chaddr must be filled in,
606 * ciaddr must be filled in
607 */
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200608 log1("Received RELEASE");
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100609 if (server_id_opt
610 && lease /* chaddr matches this lease */
611 && packet.ciaddr == lease->lease_nip
612 ) {
Denis Vlasenko04158e02009-02-02 10:48:06 +0000613 lease->expires = time(NULL);
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100614 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000615 break;
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100616
Mike Frysinger7031f622006-05-08 03:20:50 +0000617 case DHCPINFORM:
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200618 log1("Received INFORM");
Mike Frysinger7031f622006-05-08 03:20:50 +0000619 send_inform(&packet);
620 break;
Mike Frysinger7031f622006-05-08 03:20:50 +0000621 }
622 }
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000623 ret0:
624 retval = 0;
625 ret:
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000626 /*if (server_config.pidfile) - server_config.pidfile is never NULL */
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000627 remove_pidfile(server_config.pidfile);
628 return retval;
Mike Frysinger7031f622006-05-08 03:20:50 +0000629}