blob: 3c2a9cb4e0a98015169fc28d4a074e21c9ea5c92 [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"
Denys Vlasenkoe3f5b732013-03-13 22:27:37 +010030//usage: "\n -I ADDR Local address"
Pere Orga5bc8c002011-04-11 03:29:49 +020031//usage: IF_FEATURE_UDHCP_PORT(
32//usage: "\n -P N Use port N (default 67)"
33//usage: )
34
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +000035#include <syslog.h>
Mike Frysinger7031f622006-05-08 03:20:50 +000036#include "common.h"
Denis Vlasenkod55fe3e2008-02-04 13:12:16 +000037#include "dhcpc.h"
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000038#include "dhcpd.h"
Mike Frysinger7031f622006-05-08 03:20:50 +000039
40
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +010041/* Send a packet to a specific mac address and ip address by creating our own ip packet */
42static void send_packet_to_client(struct dhcp_packet *dhcp_pkt, int force_broadcast)
Denys Vlasenko8a7c1662010-03-20 03:48:11 +010043{
44 const uint8_t *chaddr;
45 uint32_t ciaddr;
46
47 // Was:
48 //if (force_broadcast) { /* broadcast */ }
49 //else if (dhcp_pkt->ciaddr) { /* unicast to dhcp_pkt->ciaddr */ }
50 //else if (dhcp_pkt->flags & htons(BROADCAST_FLAG)) { /* broadcast */ }
51 //else { /* unicast to dhcp_pkt->yiaddr */ }
52 // But this is wrong: yiaddr is _our_ idea what client's IP is
53 // (for example, from lease file). Client may not know that,
54 // and may not have UDP socket listening on that IP!
55 // We should never unicast to dhcp_pkt->yiaddr!
56 // dhcp_pkt->ciaddr, OTOH, comes from client's request packet,
57 // and can be used.
58
59 if (force_broadcast
60 || (dhcp_pkt->flags & htons(BROADCAST_FLAG))
Denys Vlasenko53f72bb2010-03-21 06:46:09 +010061 || dhcp_pkt->ciaddr == 0
Denys Vlasenko8a7c1662010-03-20 03:48:11 +010062 ) {
63 log1("Broadcasting packet to client");
64 ciaddr = INADDR_BROADCAST;
65 chaddr = MAC_BCAST_ADDR;
66 } else {
67 log1("Unicasting packet to client ciaddr");
68 ciaddr = dhcp_pkt->ciaddr;
69 chaddr = dhcp_pkt->chaddr;
70 }
71
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +010072 udhcp_send_raw_packet(dhcp_pkt,
Denys Vlasenko8a7c1662010-03-20 03:48:11 +010073 /*src*/ server_config.server_nip, SERVER_PORT,
74 /*dst*/ ciaddr, CLIENT_PORT, chaddr,
75 server_config.ifindex);
76}
77
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +010078/* Send a packet to gateway_nip using the kernel ip stack */
79static void send_packet_to_relay(struct dhcp_packet *dhcp_pkt)
80{
81 log1("Forwarding packet to relay");
82
83 udhcp_send_kernel_packet(dhcp_pkt,
84 server_config.server_nip, SERVER_PORT,
85 dhcp_pkt->gateway_nip, SERVER_PORT);
86}
87
88static void send_packet(struct dhcp_packet *dhcp_pkt, int force_broadcast)
Denys Vlasenko8a7c1662010-03-20 03:48:11 +010089{
90 if (dhcp_pkt->gateway_nip)
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +010091 send_packet_to_relay(dhcp_pkt);
92 else
93 send_packet_to_client(dhcp_pkt, force_broadcast);
Denys Vlasenko8a7c1662010-03-20 03:48:11 +010094}
95
96static void init_packet(struct dhcp_packet *packet, struct dhcp_packet *oldpacket, char type)
97{
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +010098 /* Sets op, htype, hlen, cookie fields
99 * and adds DHCP_MESSAGE_TYPE option */
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100100 udhcp_init_header(packet, type);
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100101
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100102 packet->xid = oldpacket->xid;
103 memcpy(packet->chaddr, oldpacket->chaddr, sizeof(oldpacket->chaddr));
104 packet->flags = oldpacket->flags;
105 packet->gateway_nip = oldpacket->gateway_nip;
106 packet->ciaddr = oldpacket->ciaddr;
Denys Vlasenko7724c762010-03-26 09:32:09 +0100107 udhcp_add_simple_option(packet, DHCP_SERVER_ID, server_config.server_nip);
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100108}
109
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +0100110/* Fill options field, siaddr_nip, and sname and boot_file fields.
111 * TODO: teach this code to use overload option.
112 */
113static void add_server_options(struct dhcp_packet *packet)
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100114{
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +0100115 struct option_set *curr = server_config.options;
116
117 while (curr) {
118 if (curr->data[OPT_CODE] != DHCP_LEASE_TIME)
Denys Vlasenko7724c762010-03-26 09:32:09 +0100119 udhcp_add_binary_option(packet, curr->data);
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +0100120 curr = curr->next;
121 }
122
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100123 packet->siaddr_nip = server_config.siaddr_nip;
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +0100124
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100125 if (server_config.sname)
126 strncpy((char*)packet->sname, server_config.sname, sizeof(packet->sname) - 1);
127 if (server_config.boot_file)
128 strncpy((char*)packet->file, server_config.boot_file, sizeof(packet->file) - 1);
129}
130
131static uint32_t select_lease_time(struct dhcp_packet *packet)
132{
133 uint32_t lease_time_sec = server_config.max_lease_sec;
Denys Vlasenkodde8bdc2010-03-22 14:29:13 +0100134 uint8_t *lease_time_opt = udhcp_get_option(packet, DHCP_LEASE_TIME);
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100135 if (lease_time_opt) {
136 move_from_unaligned32(lease_time_sec, lease_time_opt);
137 lease_time_sec = ntohl(lease_time_sec);
138 if (lease_time_sec > server_config.max_lease_sec)
139 lease_time_sec = server_config.max_lease_sec;
140 if (lease_time_sec < server_config.min_lease_sec)
141 lease_time_sec = server_config.min_lease_sec;
142 }
143 return lease_time_sec;
144}
145
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100146/* We got a DHCP DISCOVER. Send an OFFER. */
Denys Vlasenko0bb35e12010-10-21 12:33:10 +0200147/* NOINLINE: limit stack usage in caller */
Denys Vlasenkofa5e2952010-11-28 01:10:51 +0100148static NOINLINE void send_offer(struct dhcp_packet *oldpacket,
149 uint32_t static_lease_nip,
150 struct dyn_lease *lease,
151 uint8_t *requested_ip_opt)
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100152{
153 struct dhcp_packet packet;
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100154 uint32_t lease_time_sec;
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100155 struct in_addr addr;
156
157 init_packet(&packet, oldpacket, DHCPOFFER);
158
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100159 /* If it is a static lease, use its IP */
160 packet.yiaddr = static_lease_nip;
161 /* Else: */
Denys Vlasenkoa9539872010-03-20 03:49:27 +0100162 if (!static_lease_nip) {
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100163 /* We have no static lease for client's chaddr */
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +0100164 uint32_t req_nip;
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100165 const char *p_host_name;
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +0100166
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100167 if (lease) {
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100168 /* We have a dynamic lease for client's chaddr.
169 * Reuse its IP (even if lease is expired).
170 * Note that we ignore requested IP in this case.
171 */
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100172 packet.yiaddr = lease->lease_nip;
173 }
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100174 /* Or: if client has requested an IP */
Denys Vlasenkofa5e2952010-11-28 01:10:51 +0100175 else if (requested_ip_opt != NULL
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100176 /* (read IP) */
Denys Vlasenkofa5e2952010-11-28 01:10:51 +0100177 && (move_from_unaligned32(req_nip, requested_ip_opt), 1)
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100178 /* and the IP is in the lease range */
179 && ntohl(req_nip) >= server_config.start_ip
180 && ntohl(req_nip) <= server_config.end_ip
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +0100181 /* and */
182 && ( !(lease = find_lease_by_nip(req_nip)) /* is not already taken */
183 || is_expired_lease(lease) /* or is taken, but expired */
184 )
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100185 ) {
186 packet.yiaddr = req_nip;
187 }
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100188 else {
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100189 /* Otherwise, find a free IP */
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100190 packet.yiaddr = find_free_or_expired_nip(oldpacket->chaddr);
191 }
192
193 if (!packet.yiaddr) {
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100194 bb_error_msg("no free IP addresses. OFFER abandoned");
195 return;
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100196 }
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100197 /* Reserve the IP for a short time hoping to get DHCPREQUEST soon */
Denys Vlasenkodde8bdc2010-03-22 14:29:13 +0100198 p_host_name = (const char*) udhcp_get_option(oldpacket, DHCP_HOST_NAME);
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100199 lease = add_lease(packet.chaddr, packet.yiaddr,
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100200 server_config.offer_time,
201 p_host_name,
202 p_host_name ? (unsigned char)p_host_name[OPT_LEN - OPT_DATA] : 0
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100203 );
204 if (!lease) {
205 bb_error_msg("no free IP addresses. OFFER abandoned");
206 return;
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100207 }
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100208 }
209
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100210 lease_time_sec = select_lease_time(oldpacket);
Denys Vlasenko7724c762010-03-26 09:32:09 +0100211 udhcp_add_simple_option(&packet, DHCP_LEASE_TIME, htonl(lease_time_sec));
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +0100212 add_server_options(&packet);
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100213
214 addr.s_addr = packet.yiaddr;
215 bb_info_msg("Sending OFFER of %s", inet_ntoa(addr));
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100216 /* send_packet emits error message itself if it detects failure */
217 send_packet(&packet, /*force_bcast:*/ 0);
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100218}
219
Denys Vlasenko0bb35e12010-10-21 12:33:10 +0200220/* NOINLINE: limit stack usage in caller */
221static NOINLINE void send_NAK(struct dhcp_packet *oldpacket)
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100222{
223 struct dhcp_packet packet;
224
225 init_packet(&packet, oldpacket, DHCPNAK);
226
227 log1("Sending NAK");
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100228 send_packet(&packet, /*force_bcast:*/ 1);
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100229}
230
Denys Vlasenko0bb35e12010-10-21 12:33:10 +0200231/* NOINLINE: limit stack usage in caller */
232static NOINLINE void send_ACK(struct dhcp_packet *oldpacket, uint32_t yiaddr)
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100233{
234 struct dhcp_packet packet;
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100235 uint32_t lease_time_sec;
236 struct in_addr addr;
237 const char *p_host_name;
238
239 init_packet(&packet, oldpacket, DHCPACK);
240 packet.yiaddr = yiaddr;
241
242 lease_time_sec = select_lease_time(oldpacket);
Denys Vlasenko7724c762010-03-26 09:32:09 +0100243 udhcp_add_simple_option(&packet, DHCP_LEASE_TIME, htonl(lease_time_sec));
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100244
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +0100245 add_server_options(&packet);
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100246
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100247 addr.s_addr = yiaddr;
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100248 bb_info_msg("Sending ACK to %s", inet_ntoa(addr));
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100249 send_packet(&packet, /*force_bcast:*/ 0);
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100250
Denys Vlasenkodde8bdc2010-03-22 14:29:13 +0100251 p_host_name = (const char*) udhcp_get_option(oldpacket, DHCP_HOST_NAME);
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100252 add_lease(packet.chaddr, packet.yiaddr,
253 lease_time_sec,
254 p_host_name,
255 p_host_name ? (unsigned char)p_host_name[OPT_LEN - OPT_DATA] : 0
256 );
257 if (ENABLE_FEATURE_UDHCPD_WRITE_LEASES_EARLY) {
258 /* rewrite the file with leases at every new acceptance */
259 write_leases();
260 }
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100261}
262
Denys Vlasenko0bb35e12010-10-21 12:33:10 +0200263/* NOINLINE: limit stack usage in caller */
264static NOINLINE void send_inform(struct dhcp_packet *oldpacket)
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100265{
266 struct dhcp_packet packet;
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100267
Denys Vlasenkof8fcc182010-04-04 22:36:34 +0200268 /* "If a client has obtained a network address through some other means
269 * (e.g., manual configuration), it may use a DHCPINFORM request message
270 * to obtain other local configuration parameters. Servers receiving a
271 * DHCPINFORM message construct a DHCPACK message with any local
272 * configuration parameters appropriate for the client without:
273 * allocating a new address, checking for an existing binding, filling
274 * in 'yiaddr' or including lease time parameters. The servers SHOULD
275 * unicast the DHCPACK reply to the address given in the 'ciaddr' field
276 * of the DHCPINFORM message.
277 * ...
278 * The server responds to a DHCPINFORM message by sending a DHCPACK
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100279 * message directly to the address given in the 'ciaddr' field
280 * of the DHCPINFORM message. The server MUST NOT send a lease
281 * expiration time to the client and SHOULD NOT fill in 'yiaddr'."
282 */
Denys Vlasenkof8fcc182010-04-04 22:36:34 +0200283//TODO: do a few sanity checks: is ciaddr set?
284//Better yet: is ciaddr == IP source addr?
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100285 init_packet(&packet, oldpacket, DHCPACK);
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +0100286 add_server_options(&packet);
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100287
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100288 send_packet(&packet, /*force_bcast:*/ 0);
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100289}
290
291
Mike Frysinger7031f622006-05-08 03:20:50 +0000292/* globals */
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200293struct dyn_lease *g_leases;
Denis Vlasenkodeabacd2007-09-30 17:55:43 +0000294/* struct server_config_t server_config is in bb_common_bufsiz1 */
Mike Frysinger7031f622006-05-08 03:20:50 +0000295
296
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000297int udhcpd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000298int udhcpd_main(int argc UNUSED_PARAM, char **argv)
Mike Frysinger7031f622006-05-08 03:20:50 +0000299{
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000300 int server_socket = -1, retval, max_sock;
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200301 uint8_t *state;
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000302 unsigned timeout_end;
303 unsigned num_ips;
Denis Vlasenko3d17d2b2007-08-14 16:45:29 +0000304 unsigned opt;
Mike Frysinger7031f622006-05-08 03:20:50 +0000305 struct option_set *option;
Denys Vlasenkoe3f5b732013-03-13 22:27:37 +0100306 char *str_I = str_I;
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000307 IF_FEATURE_UDHCP_PORT(char *str_P;)
Mike Frysinger7031f622006-05-08 03:20:50 +0000308
Denis Vlasenkod55fe3e2008-02-04 13:12:16 +0000309#if ENABLE_FEATURE_UDHCP_PORT
310 SERVER_PORT = 67;
311 CLIENT_PORT = 68;
312#endif
313
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200314#if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1
315 opt_complementary = "vv";
316#endif
Denys Vlasenkoe3f5b732013-03-13 22:27:37 +0100317 opt = getopt32(argv, "fSI:v"
318 IF_FEATURE_UDHCP_PORT("P:")
319 , &str_I
320 IF_FEATURE_UDHCP_PORT(, &str_P)
Leonid Lisovskiy6c9c0a12011-10-18 00:35:47 +0200321 IF_UDHCP_VERBOSE(, &dhcp_verbose)
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200322 );
Denis Vlasenko3d17d2b2007-08-14 16:45:29 +0000323 if (!(opt & 1)) { /* no -f */
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000324 bb_daemonize_or_rexec(0, argv);
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000325 logmode = LOGMODE_NONE;
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000326 }
Mike Frysinger6db13732010-06-04 13:24:50 -0400327 /* update argv after the possible vfork+exec in daemonize */
328 argv += optind;
Denis Vlasenko3d17d2b2007-08-14 16:45:29 +0000329 if (opt & 2) { /* -S */
Denis Vlasenko5e4fda02009-03-08 23:46:48 +0000330 openlog(applet_name, LOG_PID, LOG_DAEMON);
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000331 logmode |= LOGMODE_SYSLOG;
332 }
Denys Vlasenkoe3f5b732013-03-13 22:27:37 +0100333 if (opt & 4) { /* -I */
334 len_and_sockaddr *lsa = xhost_and_af2sockaddr(str_I, 0, AF_INET);
335 server_config.server_nip = lsa->u.sin.sin_addr.s_addr;
336 free(lsa);
337 }
Denis Vlasenkod55fe3e2008-02-04 13:12:16 +0000338#if ENABLE_FEATURE_UDHCP_PORT
Denys Vlasenkoe3f5b732013-03-13 22:27:37 +0100339 if (opt & 16) { /* -P */
Denis Vlasenkod55fe3e2008-02-04 13:12:16 +0000340 SERVER_PORT = xatou16(str_P);
341 CLIENT_PORT = SERVER_PORT + 1;
342 }
343#endif
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000344 /* Would rather not do read_config before daemonization -
345 * otherwise NOMMU machines will parse config twice */
Denis Vlasenko9f7b92a2007-08-15 20:03:36 +0000346 read_config(argv[0] ? argv[0] : DHCPD_CONF_FILE);
Mike Frysinger7031f622006-05-08 03:20:50 +0000347
Denis Vlasenko80edead2007-08-02 22:31:05 +0000348 /* Make sure fd 0,1,2 are open */
349 bb_sanitize_stdio();
350 /* Equivalent of doing a fflush after every \n */
351 setlinebuf(stdout);
352
353 /* Create pidfile */
354 write_pidfile(server_config.pidfile);
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100355 /* if (!..) bb_perror_msg("can't create pidfile %s", pidfile); */
Denis Vlasenko80edead2007-08-02 22:31:05 +0000356
Denis Vlasenkodef88982007-10-07 17:06:01 +0000357 bb_info_msg("%s (v"BB_VER") started", applet_name);
Mike Frysinger7031f622006-05-08 03:20:50 +0000358
Denys Vlasenko7724c762010-03-26 09:32:09 +0100359 option = udhcp_find_option(server_config.options, DHCP_LEASE_TIME);
Denys Vlasenko2e7aa922010-03-21 02:22:07 +0100360 server_config.max_lease_sec = DEFAULT_LEASE_TIME;
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000361 if (option) {
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200362 move_from_unaligned32(server_config.max_lease_sec, option->data + OPT_DATA);
363 server_config.max_lease_sec = ntohl(server_config.max_lease_sec);
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000364 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000365
366 /* Sanity check */
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000367 num_ips = server_config.end_ip - server_config.start_ip + 1;
Mike Frysinger7031f622006-05-08 03:20:50 +0000368 if (server_config.max_leases > num_ips) {
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000369 bb_error_msg("max_leases=%u is too big, setting to %u",
370 (unsigned)server_config.max_leases, num_ips);
Mike Frysinger7031f622006-05-08 03:20:50 +0000371 server_config.max_leases = num_ips;
372 }
373
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200374 g_leases = xzalloc(server_config.max_leases * sizeof(g_leases[0]));
Mike Frysinger7031f622006-05-08 03:20:50 +0000375 read_leases(server_config.lease_file);
376
Denys Vlasenko26918dd2009-06-16 12:04:23 +0200377 if (udhcp_read_interface(server_config.interface,
378 &server_config.ifindex,
Denys Vlasenkoe3f5b732013-03-13 22:27:37 +0100379 (server_config.server_nip == 0 ? &server_config.server_nip : NULL),
Denys Vlasenko26918dd2009-06-16 12:04:23 +0200380 server_config.server_mac)
381 ) {
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000382 retval = 1;
383 goto ret;
384 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000385
Mike Frysinger7031f622006-05-08 03:20:50 +0000386 /* Setup the signal pipe */
387 udhcp_sp_setup();
388
Denys Vlasenko71045cc2012-07-24 17:21:26 +0200389 continue_with_autotime:
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000390 timeout_end = monotonic_sec() + server_config.auto_time;
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000391 while (1) { /* loop until universe collapses */
Denys Vlasenkofa5e2952010-11-28 01:10:51 +0100392 fd_set rfds;
393 struct dhcp_packet packet;
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000394 int bytes;
395 struct timeval tv;
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100396 uint8_t *server_id_opt;
Denys Vlasenkofa5e2952010-11-28 01:10:51 +0100397 uint8_t *requested_ip_opt;
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100398 uint32_t requested_nip = requested_nip; /* for compiler */
Denys Vlasenkofa5e2952010-11-28 01:10:51 +0100399 uint32_t static_lease_nip;
400 struct dyn_lease *lease, fake_lease;
Mike Frysinger7031f622006-05-08 03:20:50 +0000401
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000402 if (server_socket < 0) {
Denis Vlasenkof1980f62008-09-26 09:34:59 +0000403 server_socket = udhcp_listen_socket(/*INADDR_ANY,*/ SERVER_PORT,
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000404 server_config.interface);
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000405 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000406
407 max_sock = udhcp_sp_fd_set(&rfds, server_socket);
408 if (server_config.auto_time) {
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000409 tv.tv_sec = timeout_end - monotonic_sec();
Mike Frysinger7031f622006-05-08 03:20:50 +0000410 tv.tv_usec = 0;
411 }
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000412 retval = 0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000413 if (!server_config.auto_time || tv.tv_sec > 0) {
414 retval = select(max_sock + 1, &rfds, NULL, NULL,
415 server_config.auto_time ? &tv : NULL);
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000416 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000417 if (retval == 0) {
418 write_leases();
Denys Vlasenko71045cc2012-07-24 17:21:26 +0200419 goto continue_with_autotime;
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000420 }
421 if (retval < 0 && errno != EINTR) {
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200422 log1("Error on select");
Mike Frysinger7031f622006-05-08 03:20:50 +0000423 continue;
424 }
425
426 switch (udhcp_sp_read(&rfds)) {
427 case SIGUSR1:
Denys Vlasenko651a2692010-03-23 16:25:17 +0100428 bb_info_msg("Received SIGUSR1");
Mike Frysinger7031f622006-05-08 03:20:50 +0000429 write_leases();
430 /* why not just reset the timeout, eh */
Denys Vlasenko71045cc2012-07-24 17:21:26 +0200431 goto continue_with_autotime;
Mike Frysinger7031f622006-05-08 03:20:50 +0000432 case SIGTERM:
Denys Vlasenko651a2692010-03-23 16:25:17 +0100433 bb_info_msg("Received SIGTERM");
Denys Vlasenko71045cc2012-07-24 17:21:26 +0200434 write_leases();
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000435 goto ret0;
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200436 case 0: /* no signal: read a packet */
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000437 break;
438 default: /* signal or error (probably EINTR): back to select */
439 continue;
Mike Frysinger7031f622006-05-08 03:20:50 +0000440 }
441
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000442 bytes = udhcp_recv_kernel_packet(&packet, server_socket);
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000443 if (bytes < 0) {
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000444 /* bytes can also be -2 ("bad packet data") */
Mike Frysinger7031f622006-05-08 03:20:50 +0000445 if (bytes == -1 && errno != EINTR) {
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200446 log1("Read error: %s, reopening socket", strerror(errno));
Mike Frysinger7031f622006-05-08 03:20:50 +0000447 close(server_socket);
448 server_socket = -1;
449 }
450 continue;
451 }
Denys Vlasenko31af3d52009-06-17 11:57:09 +0200452 if (packet.hlen != 6) {
453 bb_error_msg("MAC length != 6, ignoring packet");
454 continue;
455 }
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100456 if (packet.op != BOOTREQUEST) {
457 bb_error_msg("not a REQUEST, ignoring packet");
458 continue;
459 }
Denys Vlasenkodde8bdc2010-03-22 14:29:13 +0100460 state = udhcp_get_option(&packet, DHCP_MESSAGE_TYPE);
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100461 if (state == NULL || state[0] < DHCP_MINTYPE || state[0] > DHCP_MAXTYPE) {
462 bb_error_msg("no or bad message type option, ignoring packet");
Mike Frysinger7031f622006-05-08 03:20:50 +0000463 continue;
464 }
465
Denys Vlasenkofa5e2952010-11-28 01:10:51 +0100466 /* Get SERVER_ID if present */
467 server_id_opt = udhcp_get_option(&packet, DHCP_SERVER_ID);
468 if (server_id_opt) {
Denys Vlasenko713d2412010-11-28 21:51:44 +0100469 uint32_t server_id_network_order;
470 move_from_unaligned32(server_id_network_order, server_id_opt);
471 if (server_id_network_order != server_config.server_nip) {
Denys Vlasenkofa5e2952010-11-28 01:10:51 +0100472 /* client talks to somebody else */
473 log1("server ID doesn't match, ignoring");
474 continue;
475 }
476 }
477
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100478 /* Look for a static/dynamic lease */
Denys Vlasenkoa9539872010-03-20 03:49:27 +0100479 static_lease_nip = get_static_nip_by_mac(server_config.static_leases, &packet.chaddr);
480 if (static_lease_nip) {
481 bb_info_msg("Found static lease: %x", static_lease_nip);
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200482 memcpy(&fake_lease.lease_mac, &packet.chaddr, 6);
Denys Vlasenkoa9539872010-03-20 03:49:27 +0100483 fake_lease.lease_nip = static_lease_nip;
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200484 fake_lease.expires = 0;
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200485 lease = &fake_lease;
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000486 } else {
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200487 lease = find_lease_by_mac(packet.chaddr);
Mike Frysinger7031f622006-05-08 03:20:50 +0000488 }
489
Denys Vlasenkofa5e2952010-11-28 01:10:51 +0100490 /* Get REQUESTED_IP if present */
491 requested_ip_opt = udhcp_get_option(&packet, DHCP_REQUESTED_IP);
492 if (requested_ip_opt) {
493 move_from_unaligned32(requested_nip, requested_ip_opt);
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100494 }
495
Mike Frysinger7031f622006-05-08 03:20:50 +0000496 switch (state[0]) {
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100497
Mike Frysinger7031f622006-05-08 03:20:50 +0000498 case DHCPDISCOVER:
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200499 log1("Received DISCOVER");
Mike Frysinger7031f622006-05-08 03:20:50 +0000500
Denys Vlasenkofa5e2952010-11-28 01:10:51 +0100501 send_offer(&packet, static_lease_nip, lease, requested_ip_opt);
Mike Frysinger7031f622006-05-08 03:20:50 +0000502 break;
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200503
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100504 case DHCPREQUEST:
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200505 log1("Received REQUEST");
Denys Vlasenko53f72bb2010-03-21 06:46:09 +0100506/* RFC 2131:
Mike Frysinger7031f622006-05-08 03:20:50 +0000507
Denys Vlasenko53f72bb2010-03-21 06:46:09 +0100508o DHCPREQUEST generated during SELECTING state:
509
510 Client inserts the address of the selected server in 'server
511 identifier', 'ciaddr' MUST be zero, 'requested IP address' MUST be
512 filled in with the yiaddr value from the chosen DHCPOFFER.
513
514 Note that the client may choose to collect several DHCPOFFER
515 messages and select the "best" offer. The client indicates its
516 selection by identifying the offering server in the DHCPREQUEST
517 message. If the client receives no acceptable offers, the client
518 may choose to try another DHCPDISCOVER message. Therefore, the
519 servers may not receive a specific DHCPREQUEST from which they can
520 decide whether or not the client has accepted the offer.
521
522o DHCPREQUEST generated during INIT-REBOOT state:
523
524 'server identifier' MUST NOT be filled in, 'requested IP address'
525 option MUST be filled in with client's notion of its previously
526 assigned address. 'ciaddr' MUST be zero. The client is seeking to
527 verify a previously allocated, cached configuration. Server SHOULD
528 send a DHCPNAK message to the client if the 'requested IP address'
529 is incorrect, or is on the wrong network.
530
531 Determining whether a client in the INIT-REBOOT state is on the
532 correct network is done by examining the contents of 'giaddr', the
533 'requested IP address' option, and a database lookup. If the DHCP
534 server detects that the client is on the wrong net (i.e., the
535 result of applying the local subnet mask or remote subnet mask (if
536 'giaddr' is not zero) to 'requested IP address' option value
537 doesn't match reality), then the server SHOULD send a DHCPNAK
538 message to the client.
539
540 If the network is correct, then the DHCP server should check if
541 the client's notion of its IP address is correct. If not, then the
542 server SHOULD send a DHCPNAK message to the client. If the DHCP
543 server has no record of this client, then it MUST remain silent,
544 and MAY output a warning to the network administrator. This
545 behavior is necessary for peaceful coexistence of non-
546 communicating DHCP servers on the same wire.
547
548 If 'giaddr' is 0x0 in the DHCPREQUEST message, the client is on
549 the same subnet as the server. The server MUST broadcast the
550 DHCPNAK message to the 0xffffffff broadcast address because the
551 client may not have a correct network address or subnet mask, and
552 the client may not be answering ARP requests.
553
554 If 'giaddr' is set in the DHCPREQUEST message, the client is on a
555 different subnet. The server MUST set the broadcast bit in the
556 DHCPNAK, so that the relay agent will broadcast the DHCPNAK to the
557 client, because the client may not have a correct network address
558 or subnet mask, and the client may not be answering ARP requests.
559
560o DHCPREQUEST generated during RENEWING state:
561
562 'server identifier' MUST NOT be filled in, 'requested IP address'
563 option MUST NOT be filled in, 'ciaddr' MUST be filled in with
564 client's IP address. In this situation, the client is completely
565 configured, and is trying to extend its lease. This message will
566 be unicast, so no relay agents will be involved in its
567 transmission. Because 'giaddr' is therefore not filled in, the
568 DHCP server will trust the value in 'ciaddr', and use it when
569 replying to the client.
570
571 A client MAY choose to renew or extend its lease prior to T1. The
572 server may choose not to extend the lease (as a policy decision by
573 the network administrator), but should return a DHCPACK message
574 regardless.
575
576o DHCPREQUEST generated during REBINDING state:
577
578 'server identifier' MUST NOT be filled in, 'requested IP address'
579 option MUST NOT be filled in, 'ciaddr' MUST be filled in with
580 client's IP address. In this situation, the client is completely
581 configured, and is trying to extend its lease. This message MUST
582 be broadcast to the 0xffffffff IP broadcast address. The DHCP
583 server SHOULD check 'ciaddr' for correctness before replying to
584 the DHCPREQUEST.
585
586 The DHCPREQUEST from a REBINDING client is intended to accommodate
587 sites that have multiple DHCP servers and a mechanism for
588 maintaining consistency among leases managed by multiple servers.
589 A DHCP server MAY extend a client's lease only if it has local
590 administrative authority to do so.
591*/
Denys Vlasenkofa5e2952010-11-28 01:10:51 +0100592 if (!requested_ip_opt) {
Denys Vlasenko53f72bb2010-03-21 06:46:09 +0100593 requested_nip = packet.ciaddr;
594 if (requested_nip == 0) {
595 log1("no requested IP and no ciaddr, ignoring");
596 break;
597 }
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100598 }
599 if (lease && requested_nip == lease->lease_nip) {
Denys Vlasenko53f72bb2010-03-21 06:46:09 +0100600 /* client requested or configured IP matches the lease.
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100601 * ACK it, and bump lease expiration time. */
602 send_ACK(&packet, lease->lease_nip);
603 break;
604 }
Denys Vlasenko713d2412010-11-28 21:51:44 +0100605 /* No lease for this MAC, or lease IP != requested IP */
606
607 if (server_id_opt /* client is in SELECTING state */
608 || requested_ip_opt /* client is in INIT-REBOOT state */
609 ) {
610 /* "No, we don't have this IP for you" */
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100611 send_NAK(&packet);
Denys Vlasenko713d2412010-11-28 21:51:44 +0100612 } /* else: client is in RENEWING or REBINDING, do not answer */
613
Mike Frysinger7031f622006-05-08 03:20:50 +0000614 break;
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100615
Mike Frysinger7031f622006-05-08 03:20:50 +0000616 case DHCPDECLINE:
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100617 /* RFC 2131:
618 * "If the server receives a DHCPDECLINE message,
619 * the client has discovered through some other means
620 * that the suggested network address is already
621 * in use. The server MUST mark the network address
622 * as not available and SHOULD notify the local
623 * sysadmin of a possible configuration problem."
624 *
625 * SERVER_ID must be present,
626 * REQUESTED_IP must be present,
627 * chaddr must be filled in,
628 * ciaddr must be 0 (we do not check this)
629 */
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200630 log1("Received DECLINE");
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100631 if (server_id_opt
Denys Vlasenkofa5e2952010-11-28 01:10:51 +0100632 && requested_ip_opt
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100633 && lease /* chaddr matches this lease */
634 && requested_nip == lease->lease_nip
635 ) {
Denys Vlasenko31af3d52009-06-17 11:57:09 +0200636 memset(lease->lease_mac, 0, sizeof(lease->lease_mac));
Denis Vlasenko04158e02009-02-02 10:48:06 +0000637 lease->expires = time(NULL) + server_config.decline_time;
Mike Frysinger7031f622006-05-08 03:20:50 +0000638 }
639 break;
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100640
Mike Frysinger7031f622006-05-08 03:20:50 +0000641 case DHCPRELEASE:
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100642 /* "Upon receipt of a DHCPRELEASE message, the server
643 * marks the network address as not allocated."
644 *
645 * SERVER_ID must be present,
646 * REQUESTED_IP must not be present (we do not check this),
647 * chaddr must be filled in,
648 * ciaddr must be filled in
649 */
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200650 log1("Received RELEASE");
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100651 if (server_id_opt
652 && lease /* chaddr matches this lease */
653 && packet.ciaddr == lease->lease_nip
654 ) {
Denis Vlasenko04158e02009-02-02 10:48:06 +0000655 lease->expires = time(NULL);
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100656 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000657 break;
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100658
Mike Frysinger7031f622006-05-08 03:20:50 +0000659 case DHCPINFORM:
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200660 log1("Received INFORM");
Mike Frysinger7031f622006-05-08 03:20:50 +0000661 send_inform(&packet);
662 break;
Mike Frysinger7031f622006-05-08 03:20:50 +0000663 }
664 }
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000665 ret0:
666 retval = 0;
667 ret:
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000668 /*if (server_config.pidfile) - server_config.pidfile is never NULL */
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000669 remove_pidfile(server_config.pidfile);
670 return retval;
Mike Frysinger7031f622006-05-08 03:20:50 +0000671}