blob: dd55e70f4861def201cf2648fe4734376537c7e0 [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/*
Denys Vlasenko385b4562010-03-26 10:09:34 +01003 * udhcp server
Mike Frysinger7031f622006-05-08 03:20:50 +00004 * 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 */
Pere Orga5bc8c002011-04-11 03:29:49 +020023
24//usage:#define udhcpd_trivial_usage
25//usage: "[-fS]" IF_FEATURE_UDHCP_PORT(" [-P N]") " [CONFFILE]"
26//usage:#define udhcpd_full_usage "\n\n"
27//usage: "DHCP server\n"
28//usage: "\n -f Run in foreground"
29//usage: "\n -S Log to syslog too"
30//usage: IF_FEATURE_UDHCP_PORT(
31//usage: "\n -P N Use port N (default 67)"
32//usage: )
33
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +000034#include <syslog.h>
Mike Frysinger7031f622006-05-08 03:20:50 +000035#include "common.h"
Denis Vlasenkod55fe3e2008-02-04 13:12:16 +000036#include "dhcpc.h"
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000037#include "dhcpd.h"
Mike Frysinger7031f622006-05-08 03:20:50 +000038
39
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +010040/* Send a packet to a specific mac address and ip address by creating our own ip packet */
41static void send_packet_to_client(struct dhcp_packet *dhcp_pkt, int force_broadcast)
Denys Vlasenko8a7c1662010-03-20 03:48:11 +010042{
43 const uint8_t *chaddr;
44 uint32_t ciaddr;
45
46 // Was:
47 //if (force_broadcast) { /* broadcast */ }
48 //else if (dhcp_pkt->ciaddr) { /* unicast to dhcp_pkt->ciaddr */ }
49 //else if (dhcp_pkt->flags & htons(BROADCAST_FLAG)) { /* broadcast */ }
50 //else { /* unicast to dhcp_pkt->yiaddr */ }
51 // But this is wrong: yiaddr is _our_ idea what client's IP is
52 // (for example, from lease file). Client may not know that,
53 // and may not have UDP socket listening on that IP!
54 // We should never unicast to dhcp_pkt->yiaddr!
55 // dhcp_pkt->ciaddr, OTOH, comes from client's request packet,
56 // and can be used.
57
58 if (force_broadcast
59 || (dhcp_pkt->flags & htons(BROADCAST_FLAG))
Denys Vlasenko53f72bb2010-03-21 06:46:09 +010060 || dhcp_pkt->ciaddr == 0
Denys Vlasenko8a7c1662010-03-20 03:48:11 +010061 ) {
62 log1("Broadcasting packet to client");
63 ciaddr = INADDR_BROADCAST;
64 chaddr = MAC_BCAST_ADDR;
65 } else {
66 log1("Unicasting packet to client ciaddr");
67 ciaddr = dhcp_pkt->ciaddr;
68 chaddr = dhcp_pkt->chaddr;
69 }
70
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +010071 udhcp_send_raw_packet(dhcp_pkt,
Denys Vlasenko8a7c1662010-03-20 03:48:11 +010072 /*src*/ server_config.server_nip, SERVER_PORT,
73 /*dst*/ ciaddr, CLIENT_PORT, chaddr,
74 server_config.ifindex);
75}
76
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +010077/* Send a packet to gateway_nip using the kernel ip stack */
78static void send_packet_to_relay(struct dhcp_packet *dhcp_pkt)
79{
80 log1("Forwarding packet to relay");
81
82 udhcp_send_kernel_packet(dhcp_pkt,
83 server_config.server_nip, SERVER_PORT,
84 dhcp_pkt->gateway_nip, SERVER_PORT);
85}
86
87static void send_packet(struct dhcp_packet *dhcp_pkt, int force_broadcast)
Denys Vlasenko8a7c1662010-03-20 03:48:11 +010088{
89 if (dhcp_pkt->gateway_nip)
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +010090 send_packet_to_relay(dhcp_pkt);
91 else
92 send_packet_to_client(dhcp_pkt, force_broadcast);
Denys Vlasenko8a7c1662010-03-20 03:48:11 +010093}
94
95static void init_packet(struct dhcp_packet *packet, struct dhcp_packet *oldpacket, char type)
96{
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +010097 /* Sets op, htype, hlen, cookie fields
98 * and adds DHCP_MESSAGE_TYPE option */
Denys Vlasenko8a7c1662010-03-20 03:48:11 +010099 udhcp_init_header(packet, type);
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100100
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100101 packet->xid = oldpacket->xid;
102 memcpy(packet->chaddr, oldpacket->chaddr, sizeof(oldpacket->chaddr));
103 packet->flags = oldpacket->flags;
104 packet->gateway_nip = oldpacket->gateway_nip;
105 packet->ciaddr = oldpacket->ciaddr;
Denys Vlasenko7724c762010-03-26 09:32:09 +0100106 udhcp_add_simple_option(packet, DHCP_SERVER_ID, server_config.server_nip);
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100107}
108
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +0100109/* Fill options field, siaddr_nip, and sname and boot_file fields.
110 * TODO: teach this code to use overload option.
111 */
112static void add_server_options(struct dhcp_packet *packet)
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100113{
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +0100114 struct option_set *curr = server_config.options;
115
116 while (curr) {
117 if (curr->data[OPT_CODE] != DHCP_LEASE_TIME)
Denys Vlasenko7724c762010-03-26 09:32:09 +0100118 udhcp_add_binary_option(packet, curr->data);
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +0100119 curr = curr->next;
120 }
121
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100122 packet->siaddr_nip = server_config.siaddr_nip;
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +0100123
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100124 if (server_config.sname)
125 strncpy((char*)packet->sname, server_config.sname, sizeof(packet->sname) - 1);
126 if (server_config.boot_file)
127 strncpy((char*)packet->file, server_config.boot_file, sizeof(packet->file) - 1);
128}
129
130static uint32_t select_lease_time(struct dhcp_packet *packet)
131{
132 uint32_t lease_time_sec = server_config.max_lease_sec;
Denys Vlasenkodde8bdc2010-03-22 14:29:13 +0100133 uint8_t *lease_time_opt = udhcp_get_option(packet, DHCP_LEASE_TIME);
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100134 if (lease_time_opt) {
135 move_from_unaligned32(lease_time_sec, lease_time_opt);
136 lease_time_sec = ntohl(lease_time_sec);
137 if (lease_time_sec > server_config.max_lease_sec)
138 lease_time_sec = server_config.max_lease_sec;
139 if (lease_time_sec < server_config.min_lease_sec)
140 lease_time_sec = server_config.min_lease_sec;
141 }
142 return lease_time_sec;
143}
144
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100145/* We got a DHCP DISCOVER. Send an OFFER. */
Denys Vlasenko0bb35e12010-10-21 12:33:10 +0200146/* NOINLINE: limit stack usage in caller */
Denys Vlasenkofa5e2952010-11-28 01:10:51 +0100147static NOINLINE void send_offer(struct dhcp_packet *oldpacket,
148 uint32_t static_lease_nip,
149 struct dyn_lease *lease,
150 uint8_t *requested_ip_opt)
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100151{
152 struct dhcp_packet packet;
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100153 uint32_t lease_time_sec;
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100154 struct in_addr addr;
155
156 init_packet(&packet, oldpacket, DHCPOFFER);
157
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100158 /* If it is a static lease, use its IP */
159 packet.yiaddr = static_lease_nip;
160 /* Else: */
Denys Vlasenkoa9539872010-03-20 03:49:27 +0100161 if (!static_lease_nip) {
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100162 /* We have no static lease for client's chaddr */
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +0100163 uint32_t req_nip;
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100164 const char *p_host_name;
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +0100165
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100166 if (lease) {
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100167 /* We have a dynamic lease for client's chaddr.
168 * Reuse its IP (even if lease is expired).
169 * Note that we ignore requested IP in this case.
170 */
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100171 packet.yiaddr = lease->lease_nip;
172 }
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100173 /* Or: if client has requested an IP */
Denys Vlasenkofa5e2952010-11-28 01:10:51 +0100174 else if (requested_ip_opt != NULL
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100175 /* (read IP) */
Denys Vlasenkofa5e2952010-11-28 01:10:51 +0100176 && (move_from_unaligned32(req_nip, requested_ip_opt), 1)
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100177 /* and the IP is in the lease range */
178 && ntohl(req_nip) >= server_config.start_ip
179 && ntohl(req_nip) <= server_config.end_ip
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +0100180 /* and */
181 && ( !(lease = find_lease_by_nip(req_nip)) /* is not already taken */
182 || is_expired_lease(lease) /* or is taken, but expired */
183 )
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100184 ) {
185 packet.yiaddr = req_nip;
186 }
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100187 else {
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100188 /* Otherwise, find a free IP */
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100189 packet.yiaddr = find_free_or_expired_nip(oldpacket->chaddr);
190 }
191
192 if (!packet.yiaddr) {
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100193 bb_error_msg("no free IP addresses. OFFER abandoned");
194 return;
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100195 }
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100196 /* Reserve the IP for a short time hoping to get DHCPREQUEST soon */
Denys Vlasenkodde8bdc2010-03-22 14:29:13 +0100197 p_host_name = (const char*) udhcp_get_option(oldpacket, DHCP_HOST_NAME);
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100198 lease = add_lease(packet.chaddr, packet.yiaddr,
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100199 server_config.offer_time,
200 p_host_name,
201 p_host_name ? (unsigned char)p_host_name[OPT_LEN - OPT_DATA] : 0
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100202 );
203 if (!lease) {
204 bb_error_msg("no free IP addresses. OFFER abandoned");
205 return;
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100206 }
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100207 }
208
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100209 lease_time_sec = select_lease_time(oldpacket);
Denys Vlasenko7724c762010-03-26 09:32:09 +0100210 udhcp_add_simple_option(&packet, DHCP_LEASE_TIME, htonl(lease_time_sec));
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +0100211 add_server_options(&packet);
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100212
213 addr.s_addr = packet.yiaddr;
214 bb_info_msg("Sending OFFER of %s", inet_ntoa(addr));
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100215 /* send_packet emits error message itself if it detects failure */
216 send_packet(&packet, /*force_bcast:*/ 0);
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100217}
218
Denys Vlasenko0bb35e12010-10-21 12:33:10 +0200219/* NOINLINE: limit stack usage in caller */
220static NOINLINE void send_NAK(struct dhcp_packet *oldpacket)
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100221{
222 struct dhcp_packet packet;
223
224 init_packet(&packet, oldpacket, DHCPNAK);
225
226 log1("Sending NAK");
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100227 send_packet(&packet, /*force_bcast:*/ 1);
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100228}
229
Denys Vlasenko0bb35e12010-10-21 12:33:10 +0200230/* NOINLINE: limit stack usage in caller */
231static NOINLINE void send_ACK(struct dhcp_packet *oldpacket, uint32_t yiaddr)
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100232{
233 struct dhcp_packet packet;
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100234 uint32_t lease_time_sec;
235 struct in_addr addr;
236 const char *p_host_name;
237
238 init_packet(&packet, oldpacket, DHCPACK);
239 packet.yiaddr = yiaddr;
240
241 lease_time_sec = select_lease_time(oldpacket);
Denys Vlasenko7724c762010-03-26 09:32:09 +0100242 udhcp_add_simple_option(&packet, DHCP_LEASE_TIME, htonl(lease_time_sec));
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100243
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +0100244 add_server_options(&packet);
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100245
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100246 addr.s_addr = yiaddr;
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100247 bb_info_msg("Sending ACK to %s", inet_ntoa(addr));
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100248 send_packet(&packet, /*force_bcast:*/ 0);
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100249
Denys Vlasenkodde8bdc2010-03-22 14:29:13 +0100250 p_host_name = (const char*) udhcp_get_option(oldpacket, DHCP_HOST_NAME);
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100251 add_lease(packet.chaddr, packet.yiaddr,
252 lease_time_sec,
253 p_host_name,
254 p_host_name ? (unsigned char)p_host_name[OPT_LEN - OPT_DATA] : 0
255 );
256 if (ENABLE_FEATURE_UDHCPD_WRITE_LEASES_EARLY) {
257 /* rewrite the file with leases at every new acceptance */
258 write_leases();
259 }
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100260}
261
Denys Vlasenko0bb35e12010-10-21 12:33:10 +0200262/* NOINLINE: limit stack usage in caller */
263static NOINLINE void send_inform(struct dhcp_packet *oldpacket)
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100264{
265 struct dhcp_packet packet;
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100266
Denys Vlasenkof8fcc182010-04-04 22:36:34 +0200267 /* "If a client has obtained a network address through some other means
268 * (e.g., manual configuration), it may use a DHCPINFORM request message
269 * to obtain other local configuration parameters. Servers receiving a
270 * DHCPINFORM message construct a DHCPACK message with any local
271 * configuration parameters appropriate for the client without:
272 * allocating a new address, checking for an existing binding, filling
273 * in 'yiaddr' or including lease time parameters. The servers SHOULD
274 * unicast the DHCPACK reply to the address given in the 'ciaddr' field
275 * of the DHCPINFORM message.
276 * ...
277 * The server responds to a DHCPINFORM message by sending a DHCPACK
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100278 * message directly to the address given in the 'ciaddr' field
279 * of the DHCPINFORM message. The server MUST NOT send a lease
280 * expiration time to the client and SHOULD NOT fill in 'yiaddr'."
281 */
Denys Vlasenkof8fcc182010-04-04 22:36:34 +0200282//TODO: do a few sanity checks: is ciaddr set?
283//Better yet: is ciaddr == IP source addr?
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100284 init_packet(&packet, oldpacket, DHCPACK);
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +0100285 add_server_options(&packet);
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100286
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100287 send_packet(&packet, /*force_bcast:*/ 0);
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100288}
289
290
Mike Frysinger7031f622006-05-08 03:20:50 +0000291/* globals */
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200292struct dyn_lease *g_leases;
Denis Vlasenkodeabacd2007-09-30 17:55:43 +0000293/* struct server_config_t server_config is in bb_common_bufsiz1 */
Mike Frysinger7031f622006-05-08 03:20:50 +0000294
295
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000296int udhcpd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000297int udhcpd_main(int argc UNUSED_PARAM, char **argv)
Mike Frysinger7031f622006-05-08 03:20:50 +0000298{
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000299 int server_socket = -1, retval, max_sock;
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200300 uint8_t *state;
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000301 unsigned timeout_end;
302 unsigned num_ips;
Denis Vlasenko3d17d2b2007-08-14 16:45:29 +0000303 unsigned opt;
Mike Frysinger7031f622006-05-08 03:20:50 +0000304 struct option_set *option;
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000305 IF_FEATURE_UDHCP_PORT(char *str_P;)
Mike Frysinger7031f622006-05-08 03:20:50 +0000306
Denis Vlasenkod55fe3e2008-02-04 13:12:16 +0000307#if ENABLE_FEATURE_UDHCP_PORT
308 SERVER_PORT = 67;
309 CLIENT_PORT = 68;
310#endif
311
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200312#if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1
313 opt_complementary = "vv";
314#endif
315 opt = getopt32(argv, "fSv"
316 IF_FEATURE_UDHCP_PORT("P:", &str_P)
Leonid Lisovskiy6c9c0a12011-10-18 00:35:47 +0200317 IF_UDHCP_VERBOSE(, &dhcp_verbose)
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200318 );
Denis Vlasenko3d17d2b2007-08-14 16:45:29 +0000319 if (!(opt & 1)) { /* no -f */
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000320 bb_daemonize_or_rexec(0, argv);
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000321 logmode = LOGMODE_NONE;
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000322 }
Mike Frysinger6db13732010-06-04 13:24:50 -0400323 /* update argv after the possible vfork+exec in daemonize */
324 argv += optind;
Denis Vlasenko3d17d2b2007-08-14 16:45:29 +0000325 if (opt & 2) { /* -S */
Denis Vlasenko5e4fda02009-03-08 23:46:48 +0000326 openlog(applet_name, LOG_PID, LOG_DAEMON);
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000327 logmode |= LOGMODE_SYSLOG;
328 }
Denis Vlasenkod55fe3e2008-02-04 13:12:16 +0000329#if ENABLE_FEATURE_UDHCP_PORT
Denys Vlasenko406bd142010-03-27 23:24:57 +0100330 if (opt & 8) { /* -P */
Denis Vlasenkod55fe3e2008-02-04 13:12:16 +0000331 SERVER_PORT = xatou16(str_P);
332 CLIENT_PORT = SERVER_PORT + 1;
333 }
334#endif
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000335 /* Would rather not do read_config before daemonization -
336 * otherwise NOMMU machines will parse config twice */
Denis Vlasenko9f7b92a2007-08-15 20:03:36 +0000337 read_config(argv[0] ? argv[0] : DHCPD_CONF_FILE);
Mike Frysinger7031f622006-05-08 03:20:50 +0000338
Denis Vlasenko80edead2007-08-02 22:31:05 +0000339 /* Make sure fd 0,1,2 are open */
340 bb_sanitize_stdio();
341 /* Equivalent of doing a fflush after every \n */
342 setlinebuf(stdout);
343
344 /* Create pidfile */
345 write_pidfile(server_config.pidfile);
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100346 /* if (!..) bb_perror_msg("can't create pidfile %s", pidfile); */
Denis Vlasenko80edead2007-08-02 22:31:05 +0000347
Denis Vlasenkodef88982007-10-07 17:06:01 +0000348 bb_info_msg("%s (v"BB_VER") started", applet_name);
Mike Frysinger7031f622006-05-08 03:20:50 +0000349
Denys Vlasenko7724c762010-03-26 09:32:09 +0100350 option = udhcp_find_option(server_config.options, DHCP_LEASE_TIME);
Denys Vlasenko2e7aa922010-03-21 02:22:07 +0100351 server_config.max_lease_sec = DEFAULT_LEASE_TIME;
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000352 if (option) {
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200353 move_from_unaligned32(server_config.max_lease_sec, option->data + OPT_DATA);
354 server_config.max_lease_sec = ntohl(server_config.max_lease_sec);
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000355 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000356
357 /* Sanity check */
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000358 num_ips = server_config.end_ip - server_config.start_ip + 1;
Mike Frysinger7031f622006-05-08 03:20:50 +0000359 if (server_config.max_leases > num_ips) {
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000360 bb_error_msg("max_leases=%u is too big, setting to %u",
361 (unsigned)server_config.max_leases, num_ips);
Mike Frysinger7031f622006-05-08 03:20:50 +0000362 server_config.max_leases = num_ips;
363 }
364
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200365 g_leases = xzalloc(server_config.max_leases * sizeof(g_leases[0]));
Mike Frysinger7031f622006-05-08 03:20:50 +0000366 read_leases(server_config.lease_file);
367
Denys Vlasenko26918dd2009-06-16 12:04:23 +0200368 if (udhcp_read_interface(server_config.interface,
369 &server_config.ifindex,
370 &server_config.server_nip,
371 server_config.server_mac)
372 ) {
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000373 retval = 1;
374 goto ret;
375 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000376
Mike Frysinger7031f622006-05-08 03:20:50 +0000377 /* Setup the signal pipe */
378 udhcp_sp_setup();
379
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000380 timeout_end = monotonic_sec() + server_config.auto_time;
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000381 while (1) { /* loop until universe collapses */
Denys Vlasenkofa5e2952010-11-28 01:10:51 +0100382 fd_set rfds;
383 struct dhcp_packet packet;
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000384 int bytes;
385 struct timeval tv;
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100386 uint8_t *server_id_opt;
Denys Vlasenkofa5e2952010-11-28 01:10:51 +0100387 uint8_t *requested_ip_opt;
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100388 uint32_t requested_nip = requested_nip; /* for compiler */
Denys Vlasenkofa5e2952010-11-28 01:10:51 +0100389 uint32_t static_lease_nip;
390 struct dyn_lease *lease, fake_lease;
Mike Frysinger7031f622006-05-08 03:20:50 +0000391
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000392 if (server_socket < 0) {
Denis Vlasenkof1980f62008-09-26 09:34:59 +0000393 server_socket = udhcp_listen_socket(/*INADDR_ANY,*/ SERVER_PORT,
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000394 server_config.interface);
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000395 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000396
397 max_sock = udhcp_sp_fd_set(&rfds, server_socket);
398 if (server_config.auto_time) {
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000399 tv.tv_sec = timeout_end - monotonic_sec();
Mike Frysinger7031f622006-05-08 03:20:50 +0000400 tv.tv_usec = 0;
401 }
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000402 retval = 0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000403 if (!server_config.auto_time || tv.tv_sec > 0) {
404 retval = select(max_sock + 1, &rfds, NULL, NULL,
405 server_config.auto_time ? &tv : NULL);
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000406 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000407 if (retval == 0) {
408 write_leases();
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000409 timeout_end = monotonic_sec() + server_config.auto_time;
Mike Frysinger7031f622006-05-08 03:20:50 +0000410 continue;
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000411 }
412 if (retval < 0 && errno != EINTR) {
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200413 log1("Error on select");
Mike Frysinger7031f622006-05-08 03:20:50 +0000414 continue;
415 }
416
417 switch (udhcp_sp_read(&rfds)) {
418 case SIGUSR1:
Denys Vlasenko651a2692010-03-23 16:25:17 +0100419 bb_info_msg("Received SIGUSR1");
Mike Frysinger7031f622006-05-08 03:20:50 +0000420 write_leases();
421 /* why not just reset the timeout, eh */
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000422 timeout_end = monotonic_sec() + server_config.auto_time;
Mike Frysinger7031f622006-05-08 03:20:50 +0000423 continue;
424 case SIGTERM:
Denys Vlasenko651a2692010-03-23 16:25:17 +0100425 bb_info_msg("Received SIGTERM");
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000426 goto ret0;
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200427 case 0: /* no signal: read a packet */
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000428 break;
429 default: /* signal or error (probably EINTR): back to select */
430 continue;
Mike Frysinger7031f622006-05-08 03:20:50 +0000431 }
432
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000433 bytes = udhcp_recv_kernel_packet(&packet, server_socket);
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000434 if (bytes < 0) {
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000435 /* bytes can also be -2 ("bad packet data") */
Mike Frysinger7031f622006-05-08 03:20:50 +0000436 if (bytes == -1 && errno != EINTR) {
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200437 log1("Read error: %s, reopening socket", strerror(errno));
Mike Frysinger7031f622006-05-08 03:20:50 +0000438 close(server_socket);
439 server_socket = -1;
440 }
441 continue;
442 }
Denys Vlasenko31af3d52009-06-17 11:57:09 +0200443 if (packet.hlen != 6) {
444 bb_error_msg("MAC length != 6, ignoring packet");
445 continue;
446 }
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100447 if (packet.op != BOOTREQUEST) {
448 bb_error_msg("not a REQUEST, ignoring packet");
449 continue;
450 }
Denys Vlasenkodde8bdc2010-03-22 14:29:13 +0100451 state = udhcp_get_option(&packet, DHCP_MESSAGE_TYPE);
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100452 if (state == NULL || state[0] < DHCP_MINTYPE || state[0] > DHCP_MAXTYPE) {
453 bb_error_msg("no or bad message type option, ignoring packet");
Mike Frysinger7031f622006-05-08 03:20:50 +0000454 continue;
455 }
456
Denys Vlasenkofa5e2952010-11-28 01:10:51 +0100457 /* Get SERVER_ID if present */
458 server_id_opt = udhcp_get_option(&packet, DHCP_SERVER_ID);
459 if (server_id_opt) {
Denys Vlasenko713d2412010-11-28 21:51:44 +0100460 uint32_t server_id_network_order;
461 move_from_unaligned32(server_id_network_order, server_id_opt);
462 if (server_id_network_order != server_config.server_nip) {
Denys Vlasenkofa5e2952010-11-28 01:10:51 +0100463 /* client talks to somebody else */
464 log1("server ID doesn't match, ignoring");
465 continue;
466 }
467 }
468
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100469 /* Look for a static/dynamic lease */
Denys Vlasenkoa9539872010-03-20 03:49:27 +0100470 static_lease_nip = get_static_nip_by_mac(server_config.static_leases, &packet.chaddr);
471 if (static_lease_nip) {
472 bb_info_msg("Found static lease: %x", static_lease_nip);
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200473 memcpy(&fake_lease.lease_mac, &packet.chaddr, 6);
Denys Vlasenkoa9539872010-03-20 03:49:27 +0100474 fake_lease.lease_nip = static_lease_nip;
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200475 fake_lease.expires = 0;
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200476 lease = &fake_lease;
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000477 } else {
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200478 lease = find_lease_by_mac(packet.chaddr);
Mike Frysinger7031f622006-05-08 03:20:50 +0000479 }
480
Denys Vlasenkofa5e2952010-11-28 01:10:51 +0100481 /* Get REQUESTED_IP if present */
482 requested_ip_opt = udhcp_get_option(&packet, DHCP_REQUESTED_IP);
483 if (requested_ip_opt) {
484 move_from_unaligned32(requested_nip, requested_ip_opt);
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100485 }
486
Mike Frysinger7031f622006-05-08 03:20:50 +0000487 switch (state[0]) {
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100488
Mike Frysinger7031f622006-05-08 03:20:50 +0000489 case DHCPDISCOVER:
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200490 log1("Received DISCOVER");
Mike Frysinger7031f622006-05-08 03:20:50 +0000491
Denys Vlasenkofa5e2952010-11-28 01:10:51 +0100492 send_offer(&packet, static_lease_nip, lease, requested_ip_opt);
Mike Frysinger7031f622006-05-08 03:20:50 +0000493 break;
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200494
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100495 case DHCPREQUEST:
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200496 log1("Received REQUEST");
Denys Vlasenko53f72bb2010-03-21 06:46:09 +0100497/* RFC 2131:
Mike Frysinger7031f622006-05-08 03:20:50 +0000498
Denys Vlasenko53f72bb2010-03-21 06:46:09 +0100499o DHCPREQUEST generated during SELECTING state:
500
501 Client inserts the address of the selected server in 'server
502 identifier', 'ciaddr' MUST be zero, 'requested IP address' MUST be
503 filled in with the yiaddr value from the chosen DHCPOFFER.
504
505 Note that the client may choose to collect several DHCPOFFER
506 messages and select the "best" offer. The client indicates its
507 selection by identifying the offering server in the DHCPREQUEST
508 message. If the client receives no acceptable offers, the client
509 may choose to try another DHCPDISCOVER message. Therefore, the
510 servers may not receive a specific DHCPREQUEST from which they can
511 decide whether or not the client has accepted the offer.
512
513o DHCPREQUEST generated during INIT-REBOOT state:
514
515 'server identifier' MUST NOT be filled in, 'requested IP address'
516 option MUST be filled in with client's notion of its previously
517 assigned address. 'ciaddr' MUST be zero. The client is seeking to
518 verify a previously allocated, cached configuration. Server SHOULD
519 send a DHCPNAK message to the client if the 'requested IP address'
520 is incorrect, or is on the wrong network.
521
522 Determining whether a client in the INIT-REBOOT state is on the
523 correct network is done by examining the contents of 'giaddr', the
524 'requested IP address' option, and a database lookup. If the DHCP
525 server detects that the client is on the wrong net (i.e., the
526 result of applying the local subnet mask or remote subnet mask (if
527 'giaddr' is not zero) to 'requested IP address' option value
528 doesn't match reality), then the server SHOULD send a DHCPNAK
529 message to the client.
530
531 If the network is correct, then the DHCP server should check if
532 the client's notion of its IP address is correct. If not, then the
533 server SHOULD send a DHCPNAK message to the client. If the DHCP
534 server has no record of this client, then it MUST remain silent,
535 and MAY output a warning to the network administrator. This
536 behavior is necessary for peaceful coexistence of non-
537 communicating DHCP servers on the same wire.
538
539 If 'giaddr' is 0x0 in the DHCPREQUEST message, the client is on
540 the same subnet as the server. The server MUST broadcast the
541 DHCPNAK message to the 0xffffffff broadcast address because the
542 client may not have a correct network address or subnet mask, and
543 the client may not be answering ARP requests.
544
545 If 'giaddr' is set in the DHCPREQUEST message, the client is on a
546 different subnet. The server MUST set the broadcast bit in the
547 DHCPNAK, so that the relay agent will broadcast the DHCPNAK to the
548 client, because the client may not have a correct network address
549 or subnet mask, and the client may not be answering ARP requests.
550
551o DHCPREQUEST generated during RENEWING state:
552
553 'server identifier' MUST NOT be filled in, 'requested IP address'
554 option MUST NOT be filled in, 'ciaddr' MUST be filled in with
555 client's IP address. In this situation, the client is completely
556 configured, and is trying to extend its lease. This message will
557 be unicast, so no relay agents will be involved in its
558 transmission. Because 'giaddr' is therefore not filled in, the
559 DHCP server will trust the value in 'ciaddr', and use it when
560 replying to the client.
561
562 A client MAY choose to renew or extend its lease prior to T1. The
563 server may choose not to extend the lease (as a policy decision by
564 the network administrator), but should return a DHCPACK message
565 regardless.
566
567o DHCPREQUEST generated during REBINDING state:
568
569 'server identifier' MUST NOT be filled in, 'requested IP address'
570 option MUST NOT be filled in, 'ciaddr' MUST be filled in with
571 client's IP address. In this situation, the client is completely
572 configured, and is trying to extend its lease. This message MUST
573 be broadcast to the 0xffffffff IP broadcast address. The DHCP
574 server SHOULD check 'ciaddr' for correctness before replying to
575 the DHCPREQUEST.
576
577 The DHCPREQUEST from a REBINDING client is intended to accommodate
578 sites that have multiple DHCP servers and a mechanism for
579 maintaining consistency among leases managed by multiple servers.
580 A DHCP server MAY extend a client's lease only if it has local
581 administrative authority to do so.
582*/
Denys Vlasenkofa5e2952010-11-28 01:10:51 +0100583 if (!requested_ip_opt) {
Denys Vlasenko53f72bb2010-03-21 06:46:09 +0100584 requested_nip = packet.ciaddr;
585 if (requested_nip == 0) {
586 log1("no requested IP and no ciaddr, ignoring");
587 break;
588 }
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100589 }
590 if (lease && requested_nip == lease->lease_nip) {
Denys Vlasenko53f72bb2010-03-21 06:46:09 +0100591 /* client requested or configured IP matches the lease.
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100592 * ACK it, and bump lease expiration time. */
593 send_ACK(&packet, lease->lease_nip);
594 break;
595 }
Denys Vlasenko713d2412010-11-28 21:51:44 +0100596 /* No lease for this MAC, or lease IP != requested IP */
597
598 if (server_id_opt /* client is in SELECTING state */
599 || requested_ip_opt /* client is in INIT-REBOOT state */
600 ) {
601 /* "No, we don't have this IP for you" */
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100602 send_NAK(&packet);
Denys Vlasenko713d2412010-11-28 21:51:44 +0100603 } /* else: client is in RENEWING or REBINDING, do not answer */
604
Mike Frysinger7031f622006-05-08 03:20:50 +0000605 break;
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100606
Mike Frysinger7031f622006-05-08 03:20:50 +0000607 case DHCPDECLINE:
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100608 /* RFC 2131:
609 * "If the server receives a DHCPDECLINE message,
610 * the client has discovered through some other means
611 * that the suggested network address is already
612 * in use. The server MUST mark the network address
613 * as not available and SHOULD notify the local
614 * sysadmin of a possible configuration problem."
615 *
616 * SERVER_ID must be present,
617 * REQUESTED_IP must be present,
618 * chaddr must be filled in,
619 * ciaddr must be 0 (we do not check this)
620 */
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200621 log1("Received DECLINE");
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100622 if (server_id_opt
Denys Vlasenkofa5e2952010-11-28 01:10:51 +0100623 && requested_ip_opt
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100624 && lease /* chaddr matches this lease */
625 && requested_nip == lease->lease_nip
626 ) {
Denys Vlasenko31af3d52009-06-17 11:57:09 +0200627 memset(lease->lease_mac, 0, sizeof(lease->lease_mac));
Denis Vlasenko04158e02009-02-02 10:48:06 +0000628 lease->expires = time(NULL) + server_config.decline_time;
Mike Frysinger7031f622006-05-08 03:20:50 +0000629 }
630 break;
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100631
Mike Frysinger7031f622006-05-08 03:20:50 +0000632 case DHCPRELEASE:
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100633 /* "Upon receipt of a DHCPRELEASE message, the server
634 * marks the network address as not allocated."
635 *
636 * SERVER_ID must be present,
637 * REQUESTED_IP must not be present (we do not check this),
638 * chaddr must be filled in,
639 * ciaddr must be filled in
640 */
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200641 log1("Received RELEASE");
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100642 if (server_id_opt
643 && lease /* chaddr matches this lease */
644 && packet.ciaddr == lease->lease_nip
645 ) {
Denis Vlasenko04158e02009-02-02 10:48:06 +0000646 lease->expires = time(NULL);
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100647 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000648 break;
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100649
Mike Frysinger7031f622006-05-08 03:20:50 +0000650 case DHCPINFORM:
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200651 log1("Received INFORM");
Mike Frysinger7031f622006-05-08 03:20:50 +0000652 send_inform(&packet);
653 break;
Mike Frysinger7031f622006-05-08 03:20:50 +0000654 }
655 }
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000656 ret0:
657 retval = 0;
658 ret:
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000659 /*if (server_config.pidfile) - server_config.pidfile is never NULL */
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000660 remove_pidfile(server_config.pidfile);
661 return retval;
Mike Frysinger7031f622006-05-08 03:20:50 +0000662}