blob: 4b3ed240c490f025ae7dec88cd77dee894953700 [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
Denys Vlasenko7b5d5c12013-03-14 02:18:52 +010025//usage: "[-fS] [-I ADDR]" IF_FEATURE_UDHCP_PORT(" [-P N]") " [CONFFILE]"
Pere Orga5bc8c002011-04-11 03:29:49 +020026//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"
Michel Stam9f412712014-10-30 11:59:04 +010031//usage: "\n -a MSEC Timeout for ARP ping (default 2000)"
Pere Orga5bc8c002011-04-11 03:29:49 +020032//usage: IF_FEATURE_UDHCP_PORT(
33//usage: "\n -P N Use port N (default 67)"
34//usage: )
35
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +000036#include <syslog.h>
Mike Frysinger7031f622006-05-08 03:20:50 +000037#include "common.h"
Denis Vlasenkod55fe3e2008-02-04 13:12:16 +000038#include "dhcpc.h"
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000039#include "dhcpd.h"
Mike Frysinger7031f622006-05-08 03:20:50 +000040
41
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +010042/* Send a packet to a specific mac address and ip address by creating our own ip packet */
43static void send_packet_to_client(struct dhcp_packet *dhcp_pkt, int force_broadcast)
Denys Vlasenko8a7c1662010-03-20 03:48:11 +010044{
45 const uint8_t *chaddr;
46 uint32_t ciaddr;
47
48 // Was:
49 //if (force_broadcast) { /* broadcast */ }
50 //else if (dhcp_pkt->ciaddr) { /* unicast to dhcp_pkt->ciaddr */ }
51 //else if (dhcp_pkt->flags & htons(BROADCAST_FLAG)) { /* broadcast */ }
52 //else { /* unicast to dhcp_pkt->yiaddr */ }
53 // But this is wrong: yiaddr is _our_ idea what client's IP is
54 // (for example, from lease file). Client may not know that,
55 // and may not have UDP socket listening on that IP!
56 // We should never unicast to dhcp_pkt->yiaddr!
57 // dhcp_pkt->ciaddr, OTOH, comes from client's request packet,
58 // and can be used.
59
60 if (force_broadcast
61 || (dhcp_pkt->flags & htons(BROADCAST_FLAG))
Denys Vlasenko53f72bb2010-03-21 06:46:09 +010062 || dhcp_pkt->ciaddr == 0
Denys Vlasenko8a7c1662010-03-20 03:48:11 +010063 ) {
64 log1("Broadcasting packet to client");
65 ciaddr = INADDR_BROADCAST;
66 chaddr = MAC_BCAST_ADDR;
67 } else {
68 log1("Unicasting packet to client ciaddr");
69 ciaddr = dhcp_pkt->ciaddr;
70 chaddr = dhcp_pkt->chaddr;
71 }
72
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +010073 udhcp_send_raw_packet(dhcp_pkt,
Denys Vlasenko8a7c1662010-03-20 03:48:11 +010074 /*src*/ server_config.server_nip, SERVER_PORT,
75 /*dst*/ ciaddr, CLIENT_PORT, chaddr,
76 server_config.ifindex);
77}
78
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +010079/* Send a packet to gateway_nip using the kernel ip stack */
80static void send_packet_to_relay(struct dhcp_packet *dhcp_pkt)
81{
82 log1("Forwarding packet to relay");
83
84 udhcp_send_kernel_packet(dhcp_pkt,
85 server_config.server_nip, SERVER_PORT,
86 dhcp_pkt->gateway_nip, SERVER_PORT);
87}
88
89static void send_packet(struct dhcp_packet *dhcp_pkt, int force_broadcast)
Denys Vlasenko8a7c1662010-03-20 03:48:11 +010090{
91 if (dhcp_pkt->gateway_nip)
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +010092 send_packet_to_relay(dhcp_pkt);
93 else
94 send_packet_to_client(dhcp_pkt, force_broadcast);
Denys Vlasenko8a7c1662010-03-20 03:48:11 +010095}
96
97static void init_packet(struct dhcp_packet *packet, struct dhcp_packet *oldpacket, char type)
98{
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +010099 /* Sets op, htype, hlen, cookie fields
100 * and adds DHCP_MESSAGE_TYPE option */
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100101 udhcp_init_header(packet, type);
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100102
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100103 packet->xid = oldpacket->xid;
104 memcpy(packet->chaddr, oldpacket->chaddr, sizeof(oldpacket->chaddr));
105 packet->flags = oldpacket->flags;
106 packet->gateway_nip = oldpacket->gateway_nip;
107 packet->ciaddr = oldpacket->ciaddr;
Denys Vlasenko7724c762010-03-26 09:32:09 +0100108 udhcp_add_simple_option(packet, DHCP_SERVER_ID, server_config.server_nip);
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100109}
110
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +0100111/* Fill options field, siaddr_nip, and sname and boot_file fields.
112 * TODO: teach this code to use overload option.
113 */
114static void add_server_options(struct dhcp_packet *packet)
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100115{
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +0100116 struct option_set *curr = server_config.options;
117
118 while (curr) {
119 if (curr->data[OPT_CODE] != DHCP_LEASE_TIME)
Denys Vlasenko7724c762010-03-26 09:32:09 +0100120 udhcp_add_binary_option(packet, curr->data);
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +0100121 curr = curr->next;
122 }
123
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100124 packet->siaddr_nip = server_config.siaddr_nip;
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +0100125
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100126 if (server_config.sname)
127 strncpy((char*)packet->sname, server_config.sname, sizeof(packet->sname) - 1);
128 if (server_config.boot_file)
129 strncpy((char*)packet->file, server_config.boot_file, sizeof(packet->file) - 1);
130}
131
132static uint32_t select_lease_time(struct dhcp_packet *packet)
133{
134 uint32_t lease_time_sec = server_config.max_lease_sec;
Denys Vlasenkodde8bdc2010-03-22 14:29:13 +0100135 uint8_t *lease_time_opt = udhcp_get_option(packet, DHCP_LEASE_TIME);
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100136 if (lease_time_opt) {
137 move_from_unaligned32(lease_time_sec, lease_time_opt);
138 lease_time_sec = ntohl(lease_time_sec);
139 if (lease_time_sec > server_config.max_lease_sec)
140 lease_time_sec = server_config.max_lease_sec;
141 if (lease_time_sec < server_config.min_lease_sec)
142 lease_time_sec = server_config.min_lease_sec;
143 }
144 return lease_time_sec;
145}
146
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100147/* We got a DHCP DISCOVER. Send an OFFER. */
Denys Vlasenko0bb35e12010-10-21 12:33:10 +0200148/* NOINLINE: limit stack usage in caller */
Denys Vlasenkofa5e2952010-11-28 01:10:51 +0100149static NOINLINE void send_offer(struct dhcp_packet *oldpacket,
150 uint32_t static_lease_nip,
151 struct dyn_lease *lease,
Michel Stam9f412712014-10-30 11:59:04 +0100152 uint8_t *requested_ip_opt,
153 unsigned arpping_ms)
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100154{
155 struct dhcp_packet packet;
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100156 uint32_t lease_time_sec;
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100157 struct in_addr addr;
158
159 init_packet(&packet, oldpacket, DHCPOFFER);
160
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100161 /* If it is a static lease, use its IP */
162 packet.yiaddr = static_lease_nip;
163 /* Else: */
Denys Vlasenkoa9539872010-03-20 03:49:27 +0100164 if (!static_lease_nip) {
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100165 /* We have no static lease for client's chaddr */
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +0100166 uint32_t req_nip;
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100167 const char *p_host_name;
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +0100168
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100169 if (lease) {
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100170 /* We have a dynamic lease for client's chaddr.
171 * Reuse its IP (even if lease is expired).
172 * Note that we ignore requested IP in this case.
173 */
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100174 packet.yiaddr = lease->lease_nip;
175 }
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100176 /* Or: if client has requested an IP */
Denys Vlasenkofa5e2952010-11-28 01:10:51 +0100177 else if (requested_ip_opt != NULL
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100178 /* (read IP) */
Denys Vlasenkofa5e2952010-11-28 01:10:51 +0100179 && (move_from_unaligned32(req_nip, requested_ip_opt), 1)
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100180 /* and the IP is in the lease range */
181 && ntohl(req_nip) >= server_config.start_ip
182 && ntohl(req_nip) <= server_config.end_ip
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +0100183 /* and */
184 && ( !(lease = find_lease_by_nip(req_nip)) /* is not already taken */
185 || is_expired_lease(lease) /* or is taken, but expired */
186 )
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100187 ) {
188 packet.yiaddr = req_nip;
189 }
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100190 else {
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100191 /* Otherwise, find a free IP */
Michel Stam9f412712014-10-30 11:59:04 +0100192 packet.yiaddr = find_free_or_expired_nip(oldpacket->chaddr, arpping_ms);
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100193 }
194
195 if (!packet.yiaddr) {
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100196 bb_error_msg("no free IP addresses. OFFER abandoned");
197 return;
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100198 }
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100199 /* Reserve the IP for a short time hoping to get DHCPREQUEST soon */
Denys Vlasenkodde8bdc2010-03-22 14:29:13 +0100200 p_host_name = (const char*) udhcp_get_option(oldpacket, DHCP_HOST_NAME);
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100201 lease = add_lease(packet.chaddr, packet.yiaddr,
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100202 server_config.offer_time,
203 p_host_name,
204 p_host_name ? (unsigned char)p_host_name[OPT_LEN - OPT_DATA] : 0
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100205 );
206 if (!lease) {
207 bb_error_msg("no free IP addresses. OFFER abandoned");
208 return;
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100209 }
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100210 }
211
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100212 lease_time_sec = select_lease_time(oldpacket);
Denys Vlasenko7724c762010-03-26 09:32:09 +0100213 udhcp_add_simple_option(&packet, DHCP_LEASE_TIME, htonl(lease_time_sec));
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +0100214 add_server_options(&packet);
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100215
216 addr.s_addr = packet.yiaddr;
217 bb_info_msg("Sending OFFER of %s", inet_ntoa(addr));
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100218 /* send_packet emits error message itself if it detects failure */
219 send_packet(&packet, /*force_bcast:*/ 0);
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100220}
221
Denys Vlasenko0bb35e12010-10-21 12:33:10 +0200222/* NOINLINE: limit stack usage in caller */
223static NOINLINE void send_NAK(struct dhcp_packet *oldpacket)
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100224{
225 struct dhcp_packet packet;
226
227 init_packet(&packet, oldpacket, DHCPNAK);
228
229 log1("Sending NAK");
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100230 send_packet(&packet, /*force_bcast:*/ 1);
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100231}
232
Denys Vlasenko0bb35e12010-10-21 12:33:10 +0200233/* NOINLINE: limit stack usage in caller */
234static NOINLINE void send_ACK(struct dhcp_packet *oldpacket, uint32_t yiaddr)
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100235{
236 struct dhcp_packet packet;
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100237 uint32_t lease_time_sec;
238 struct in_addr addr;
239 const char *p_host_name;
240
241 init_packet(&packet, oldpacket, DHCPACK);
242 packet.yiaddr = yiaddr;
243
244 lease_time_sec = select_lease_time(oldpacket);
Denys Vlasenko7724c762010-03-26 09:32:09 +0100245 udhcp_add_simple_option(&packet, DHCP_LEASE_TIME, htonl(lease_time_sec));
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100246
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +0100247 add_server_options(&packet);
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100248
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100249 addr.s_addr = yiaddr;
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100250 bb_info_msg("Sending ACK to %s", inet_ntoa(addr));
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100251 send_packet(&packet, /*force_bcast:*/ 0);
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100252
Denys Vlasenkodde8bdc2010-03-22 14:29:13 +0100253 p_host_name = (const char*) udhcp_get_option(oldpacket, DHCP_HOST_NAME);
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100254 add_lease(packet.chaddr, packet.yiaddr,
255 lease_time_sec,
256 p_host_name,
257 p_host_name ? (unsigned char)p_host_name[OPT_LEN - OPT_DATA] : 0
258 );
259 if (ENABLE_FEATURE_UDHCPD_WRITE_LEASES_EARLY) {
260 /* rewrite the file with leases at every new acceptance */
261 write_leases();
262 }
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100263}
264
Denys Vlasenko0bb35e12010-10-21 12:33:10 +0200265/* NOINLINE: limit stack usage in caller */
266static NOINLINE void send_inform(struct dhcp_packet *oldpacket)
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100267{
268 struct dhcp_packet packet;
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100269
Denys Vlasenkof8fcc182010-04-04 22:36:34 +0200270 /* "If a client has obtained a network address through some other means
271 * (e.g., manual configuration), it may use a DHCPINFORM request message
272 * to obtain other local configuration parameters. Servers receiving a
273 * DHCPINFORM message construct a DHCPACK message with any local
274 * configuration parameters appropriate for the client without:
275 * allocating a new address, checking for an existing binding, filling
276 * in 'yiaddr' or including lease time parameters. The servers SHOULD
277 * unicast the DHCPACK reply to the address given in the 'ciaddr' field
278 * of the DHCPINFORM message.
279 * ...
280 * The server responds to a DHCPINFORM message by sending a DHCPACK
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100281 * message directly to the address given in the 'ciaddr' field
282 * of the DHCPINFORM message. The server MUST NOT send a lease
283 * expiration time to the client and SHOULD NOT fill in 'yiaddr'."
284 */
Denys Vlasenkof8fcc182010-04-04 22:36:34 +0200285//TODO: do a few sanity checks: is ciaddr set?
286//Better yet: is ciaddr == IP source addr?
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100287 init_packet(&packet, oldpacket, DHCPACK);
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +0100288 add_server_options(&packet);
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100289
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100290 send_packet(&packet, /*force_bcast:*/ 0);
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100291}
292
293
Mike Frysinger7031f622006-05-08 03:20:50 +0000294/* globals */
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200295struct dyn_lease *g_leases;
Denis Vlasenkodeabacd2007-09-30 17:55:43 +0000296/* struct server_config_t server_config is in bb_common_bufsiz1 */
Mike Frysinger7031f622006-05-08 03:20:50 +0000297
298
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000299int udhcpd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000300int udhcpd_main(int argc UNUSED_PARAM, char **argv)
Mike Frysinger7031f622006-05-08 03:20:50 +0000301{
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000302 int server_socket = -1, retval, max_sock;
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200303 uint8_t *state;
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000304 unsigned timeout_end;
305 unsigned num_ips;
Denis Vlasenko3d17d2b2007-08-14 16:45:29 +0000306 unsigned opt;
Mike Frysinger7031f622006-05-08 03:20:50 +0000307 struct option_set *option;
Denys Vlasenkoe3f5b732013-03-13 22:27:37 +0100308 char *str_I = str_I;
Michel Stam9f412712014-10-30 11:59:04 +0100309 const char *str_a = "2000";
310 unsigned arpping_ms;
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000311 IF_FEATURE_UDHCP_PORT(char *str_P;)
Mike Frysinger7031f622006-05-08 03:20:50 +0000312
Denis Vlasenkod55fe3e2008-02-04 13:12:16 +0000313#if ENABLE_FEATURE_UDHCP_PORT
314 SERVER_PORT = 67;
315 CLIENT_PORT = 68;
316#endif
317
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200318#if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1
319 opt_complementary = "vv";
320#endif
Michel Stam9f412712014-10-30 11:59:04 +0100321 opt = getopt32(argv, "fSI:va:"
Denys Vlasenkoe3f5b732013-03-13 22:27:37 +0100322 IF_FEATURE_UDHCP_PORT("P:")
323 , &str_I
Michel Stam9f412712014-10-30 11:59:04 +0100324 , &str_a
Denys Vlasenkoe3f5b732013-03-13 22:27:37 +0100325 IF_FEATURE_UDHCP_PORT(, &str_P)
Leonid Lisovskiy6c9c0a12011-10-18 00:35:47 +0200326 IF_UDHCP_VERBOSE(, &dhcp_verbose)
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200327 );
Denis Vlasenko3d17d2b2007-08-14 16:45:29 +0000328 if (!(opt & 1)) { /* no -f */
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000329 bb_daemonize_or_rexec(0, argv);
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000330 logmode = LOGMODE_NONE;
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000331 }
Mike Frysinger6db13732010-06-04 13:24:50 -0400332 /* update argv after the possible vfork+exec in daemonize */
333 argv += optind;
Denis Vlasenko3d17d2b2007-08-14 16:45:29 +0000334 if (opt & 2) { /* -S */
Denis Vlasenko5e4fda02009-03-08 23:46:48 +0000335 openlog(applet_name, LOG_PID, LOG_DAEMON);
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000336 logmode |= LOGMODE_SYSLOG;
337 }
Denys Vlasenkoe3f5b732013-03-13 22:27:37 +0100338 if (opt & 4) { /* -I */
339 len_and_sockaddr *lsa = xhost_and_af2sockaddr(str_I, 0, AF_INET);
340 server_config.server_nip = lsa->u.sin.sin_addr.s_addr;
341 free(lsa);
342 }
Denis Vlasenkod55fe3e2008-02-04 13:12:16 +0000343#if ENABLE_FEATURE_UDHCP_PORT
Michel Stam9f412712014-10-30 11:59:04 +0100344 if (opt & 32) { /* -P */
Denis Vlasenkod55fe3e2008-02-04 13:12:16 +0000345 SERVER_PORT = xatou16(str_P);
346 CLIENT_PORT = SERVER_PORT + 1;
347 }
348#endif
Michel Stam9f412712014-10-30 11:59:04 +0100349 arpping_ms = xatou(str_a);
350
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000351 /* Would rather not do read_config before daemonization -
352 * otherwise NOMMU machines will parse config twice */
Denis Vlasenko9f7b92a2007-08-15 20:03:36 +0000353 read_config(argv[0] ? argv[0] : DHCPD_CONF_FILE);
Mike Frysinger7031f622006-05-08 03:20:50 +0000354
Denis Vlasenko80edead2007-08-02 22:31:05 +0000355 /* Make sure fd 0,1,2 are open */
356 bb_sanitize_stdio();
357 /* Equivalent of doing a fflush after every \n */
358 setlinebuf(stdout);
359
360 /* Create pidfile */
361 write_pidfile(server_config.pidfile);
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100362 /* if (!..) bb_perror_msg("can't create pidfile %s", pidfile); */
Denis Vlasenko80edead2007-08-02 22:31:05 +0000363
Denis Vlasenkodef88982007-10-07 17:06:01 +0000364 bb_info_msg("%s (v"BB_VER") started", applet_name);
Mike Frysinger7031f622006-05-08 03:20:50 +0000365
Denys Vlasenko7724c762010-03-26 09:32:09 +0100366 option = udhcp_find_option(server_config.options, DHCP_LEASE_TIME);
Denys Vlasenko2e7aa922010-03-21 02:22:07 +0100367 server_config.max_lease_sec = DEFAULT_LEASE_TIME;
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000368 if (option) {
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200369 move_from_unaligned32(server_config.max_lease_sec, option->data + OPT_DATA);
370 server_config.max_lease_sec = ntohl(server_config.max_lease_sec);
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000371 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000372
373 /* Sanity check */
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000374 num_ips = server_config.end_ip - server_config.start_ip + 1;
Mike Frysinger7031f622006-05-08 03:20:50 +0000375 if (server_config.max_leases > num_ips) {
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000376 bb_error_msg("max_leases=%u is too big, setting to %u",
377 (unsigned)server_config.max_leases, num_ips);
Mike Frysinger7031f622006-05-08 03:20:50 +0000378 server_config.max_leases = num_ips;
379 }
380
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200381 g_leases = xzalloc(server_config.max_leases * sizeof(g_leases[0]));
Mike Frysinger7031f622006-05-08 03:20:50 +0000382 read_leases(server_config.lease_file);
383
Denys Vlasenko26918dd2009-06-16 12:04:23 +0200384 if (udhcp_read_interface(server_config.interface,
385 &server_config.ifindex,
Denys Vlasenkoe3f5b732013-03-13 22:27:37 +0100386 (server_config.server_nip == 0 ? &server_config.server_nip : NULL),
Denys Vlasenko26918dd2009-06-16 12:04:23 +0200387 server_config.server_mac)
388 ) {
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000389 retval = 1;
390 goto ret;
391 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000392
Mike Frysinger7031f622006-05-08 03:20:50 +0000393 /* Setup the signal pipe */
394 udhcp_sp_setup();
395
Denys Vlasenko71045cc2012-07-24 17:21:26 +0200396 continue_with_autotime:
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000397 timeout_end = monotonic_sec() + server_config.auto_time;
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000398 while (1) { /* loop until universe collapses */
Denys Vlasenkofa5e2952010-11-28 01:10:51 +0100399 fd_set rfds;
400 struct dhcp_packet packet;
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000401 int bytes;
402 struct timeval tv;
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100403 uint8_t *server_id_opt;
Denys Vlasenkofa5e2952010-11-28 01:10:51 +0100404 uint8_t *requested_ip_opt;
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100405 uint32_t requested_nip = requested_nip; /* for compiler */
Denys Vlasenkofa5e2952010-11-28 01:10:51 +0100406 uint32_t static_lease_nip;
407 struct dyn_lease *lease, fake_lease;
Mike Frysinger7031f622006-05-08 03:20:50 +0000408
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000409 if (server_socket < 0) {
Denis Vlasenkof1980f62008-09-26 09:34:59 +0000410 server_socket = udhcp_listen_socket(/*INADDR_ANY,*/ SERVER_PORT,
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000411 server_config.interface);
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000412 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000413
414 max_sock = udhcp_sp_fd_set(&rfds, server_socket);
415 if (server_config.auto_time) {
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000416 tv.tv_sec = timeout_end - monotonic_sec();
Mike Frysinger7031f622006-05-08 03:20:50 +0000417 tv.tv_usec = 0;
418 }
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000419 retval = 0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000420 if (!server_config.auto_time || tv.tv_sec > 0) {
421 retval = select(max_sock + 1, &rfds, NULL, NULL,
422 server_config.auto_time ? &tv : NULL);
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000423 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000424 if (retval == 0) {
425 write_leases();
Denys Vlasenko71045cc2012-07-24 17:21:26 +0200426 goto continue_with_autotime;
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000427 }
428 if (retval < 0 && errno != EINTR) {
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200429 log1("Error on select");
Mike Frysinger7031f622006-05-08 03:20:50 +0000430 continue;
431 }
432
433 switch (udhcp_sp_read(&rfds)) {
434 case SIGUSR1:
Denys Vlasenko651a2692010-03-23 16:25:17 +0100435 bb_info_msg("Received SIGUSR1");
Mike Frysinger7031f622006-05-08 03:20:50 +0000436 write_leases();
437 /* why not just reset the timeout, eh */
Denys Vlasenko71045cc2012-07-24 17:21:26 +0200438 goto continue_with_autotime;
Mike Frysinger7031f622006-05-08 03:20:50 +0000439 case SIGTERM:
Denys Vlasenko651a2692010-03-23 16:25:17 +0100440 bb_info_msg("Received SIGTERM");
Denys Vlasenko71045cc2012-07-24 17:21:26 +0200441 write_leases();
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000442 goto ret0;
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200443 case 0: /* no signal: read a packet */
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000444 break;
445 default: /* signal or error (probably EINTR): back to select */
446 continue;
Mike Frysinger7031f622006-05-08 03:20:50 +0000447 }
448
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000449 bytes = udhcp_recv_kernel_packet(&packet, server_socket);
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000450 if (bytes < 0) {
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000451 /* bytes can also be -2 ("bad packet data") */
Mike Frysinger7031f622006-05-08 03:20:50 +0000452 if (bytes == -1 && errno != EINTR) {
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200453 log1("Read error: %s, reopening socket", strerror(errno));
Mike Frysinger7031f622006-05-08 03:20:50 +0000454 close(server_socket);
455 server_socket = -1;
456 }
457 continue;
458 }
Denys Vlasenko31af3d52009-06-17 11:57:09 +0200459 if (packet.hlen != 6) {
460 bb_error_msg("MAC length != 6, ignoring packet");
461 continue;
462 }
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100463 if (packet.op != BOOTREQUEST) {
464 bb_error_msg("not a REQUEST, ignoring packet");
465 continue;
466 }
Denys Vlasenkodde8bdc2010-03-22 14:29:13 +0100467 state = udhcp_get_option(&packet, DHCP_MESSAGE_TYPE);
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100468 if (state == NULL || state[0] < DHCP_MINTYPE || state[0] > DHCP_MAXTYPE) {
469 bb_error_msg("no or bad message type option, ignoring packet");
Mike Frysinger7031f622006-05-08 03:20:50 +0000470 continue;
471 }
472
Denys Vlasenkofa5e2952010-11-28 01:10:51 +0100473 /* Get SERVER_ID if present */
474 server_id_opt = udhcp_get_option(&packet, DHCP_SERVER_ID);
475 if (server_id_opt) {
Denys Vlasenko713d2412010-11-28 21:51:44 +0100476 uint32_t server_id_network_order;
477 move_from_unaligned32(server_id_network_order, server_id_opt);
478 if (server_id_network_order != server_config.server_nip) {
Denys Vlasenkofa5e2952010-11-28 01:10:51 +0100479 /* client talks to somebody else */
480 log1("server ID doesn't match, ignoring");
481 continue;
482 }
483 }
484
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100485 /* Look for a static/dynamic lease */
Denys Vlasenkoa9539872010-03-20 03:49:27 +0100486 static_lease_nip = get_static_nip_by_mac(server_config.static_leases, &packet.chaddr);
487 if (static_lease_nip) {
488 bb_info_msg("Found static lease: %x", static_lease_nip);
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200489 memcpy(&fake_lease.lease_mac, &packet.chaddr, 6);
Denys Vlasenkoa9539872010-03-20 03:49:27 +0100490 fake_lease.lease_nip = static_lease_nip;
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200491 fake_lease.expires = 0;
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200492 lease = &fake_lease;
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000493 } else {
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200494 lease = find_lease_by_mac(packet.chaddr);
Mike Frysinger7031f622006-05-08 03:20:50 +0000495 }
496
Denys Vlasenkofa5e2952010-11-28 01:10:51 +0100497 /* Get REQUESTED_IP if present */
498 requested_ip_opt = udhcp_get_option(&packet, DHCP_REQUESTED_IP);
499 if (requested_ip_opt) {
500 move_from_unaligned32(requested_nip, requested_ip_opt);
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100501 }
502
Mike Frysinger7031f622006-05-08 03:20:50 +0000503 switch (state[0]) {
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100504
Mike Frysinger7031f622006-05-08 03:20:50 +0000505 case DHCPDISCOVER:
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200506 log1("Received DISCOVER");
Mike Frysinger7031f622006-05-08 03:20:50 +0000507
Michel Stam9f412712014-10-30 11:59:04 +0100508 send_offer(&packet, static_lease_nip, lease, requested_ip_opt, arpping_ms);
Mike Frysinger7031f622006-05-08 03:20:50 +0000509 break;
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200510
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100511 case DHCPREQUEST:
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200512 log1("Received REQUEST");
Denys Vlasenko53f72bb2010-03-21 06:46:09 +0100513/* RFC 2131:
Mike Frysinger7031f622006-05-08 03:20:50 +0000514
Denys Vlasenko53f72bb2010-03-21 06:46:09 +0100515o DHCPREQUEST generated during SELECTING state:
516
517 Client inserts the address of the selected server in 'server
518 identifier', 'ciaddr' MUST be zero, 'requested IP address' MUST be
519 filled in with the yiaddr value from the chosen DHCPOFFER.
520
521 Note that the client may choose to collect several DHCPOFFER
522 messages and select the "best" offer. The client indicates its
523 selection by identifying the offering server in the DHCPREQUEST
524 message. If the client receives no acceptable offers, the client
525 may choose to try another DHCPDISCOVER message. Therefore, the
526 servers may not receive a specific DHCPREQUEST from which they can
527 decide whether or not the client has accepted the offer.
528
529o DHCPREQUEST generated during INIT-REBOOT state:
530
531 'server identifier' MUST NOT be filled in, 'requested IP address'
532 option MUST be filled in with client's notion of its previously
533 assigned address. 'ciaddr' MUST be zero. The client is seeking to
534 verify a previously allocated, cached configuration. Server SHOULD
535 send a DHCPNAK message to the client if the 'requested IP address'
536 is incorrect, or is on the wrong network.
537
538 Determining whether a client in the INIT-REBOOT state is on the
539 correct network is done by examining the contents of 'giaddr', the
540 'requested IP address' option, and a database lookup. If the DHCP
541 server detects that the client is on the wrong net (i.e., the
542 result of applying the local subnet mask or remote subnet mask (if
543 'giaddr' is not zero) to 'requested IP address' option value
544 doesn't match reality), then the server SHOULD send a DHCPNAK
545 message to the client.
546
547 If the network is correct, then the DHCP server should check if
548 the client's notion of its IP address is correct. If not, then the
549 server SHOULD send a DHCPNAK message to the client. If the DHCP
550 server has no record of this client, then it MUST remain silent,
551 and MAY output a warning to the network administrator. This
552 behavior is necessary for peaceful coexistence of non-
553 communicating DHCP servers on the same wire.
554
555 If 'giaddr' is 0x0 in the DHCPREQUEST message, the client is on
556 the same subnet as the server. The server MUST broadcast the
557 DHCPNAK message to the 0xffffffff broadcast address because the
558 client may not have a correct network address or subnet mask, and
559 the client may not be answering ARP requests.
560
561 If 'giaddr' is set in the DHCPREQUEST message, the client is on a
562 different subnet. The server MUST set the broadcast bit in the
563 DHCPNAK, so that the relay agent will broadcast the DHCPNAK to the
564 client, because the client may not have a correct network address
565 or subnet mask, and the client may not be answering ARP requests.
566
567o DHCPREQUEST generated during RENEWING 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 will
573 be unicast, so no relay agents will be involved in its
574 transmission. Because 'giaddr' is therefore not filled in, the
575 DHCP server will trust the value in 'ciaddr', and use it when
576 replying to the client.
577
578 A client MAY choose to renew or extend its lease prior to T1. The
579 server may choose not to extend the lease (as a policy decision by
580 the network administrator), but should return a DHCPACK message
581 regardless.
582
583o DHCPREQUEST generated during REBINDING state:
584
585 'server identifier' MUST NOT be filled in, 'requested IP address'
586 option MUST NOT be filled in, 'ciaddr' MUST be filled in with
587 client's IP address. In this situation, the client is completely
588 configured, and is trying to extend its lease. This message MUST
589 be broadcast to the 0xffffffff IP broadcast address. The DHCP
590 server SHOULD check 'ciaddr' for correctness before replying to
591 the DHCPREQUEST.
592
593 The DHCPREQUEST from a REBINDING client is intended to accommodate
594 sites that have multiple DHCP servers and a mechanism for
595 maintaining consistency among leases managed by multiple servers.
596 A DHCP server MAY extend a client's lease only if it has local
597 administrative authority to do so.
598*/
Denys Vlasenkofa5e2952010-11-28 01:10:51 +0100599 if (!requested_ip_opt) {
Denys Vlasenko53f72bb2010-03-21 06:46:09 +0100600 requested_nip = packet.ciaddr;
601 if (requested_nip == 0) {
602 log1("no requested IP and no ciaddr, ignoring");
603 break;
604 }
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100605 }
606 if (lease && requested_nip == lease->lease_nip) {
Denys Vlasenko53f72bb2010-03-21 06:46:09 +0100607 /* client requested or configured IP matches the lease.
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100608 * ACK it, and bump lease expiration time. */
609 send_ACK(&packet, lease->lease_nip);
610 break;
611 }
Denys Vlasenko713d2412010-11-28 21:51:44 +0100612 /* No lease for this MAC, or lease IP != requested IP */
613
614 if (server_id_opt /* client is in SELECTING state */
615 || requested_ip_opt /* client is in INIT-REBOOT state */
616 ) {
617 /* "No, we don't have this IP for you" */
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100618 send_NAK(&packet);
Denys Vlasenko713d2412010-11-28 21:51:44 +0100619 } /* else: client is in RENEWING or REBINDING, do not answer */
620
Mike Frysinger7031f622006-05-08 03:20:50 +0000621 break;
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100622
Mike Frysinger7031f622006-05-08 03:20:50 +0000623 case DHCPDECLINE:
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100624 /* RFC 2131:
625 * "If the server receives a DHCPDECLINE message,
626 * the client has discovered through some other means
627 * that the suggested network address is already
628 * in use. The server MUST mark the network address
629 * as not available and SHOULD notify the local
630 * sysadmin of a possible configuration problem."
631 *
632 * SERVER_ID must be present,
633 * REQUESTED_IP must be present,
634 * chaddr must be filled in,
635 * ciaddr must be 0 (we do not check this)
636 */
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200637 log1("Received DECLINE");
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100638 if (server_id_opt
Denys Vlasenkofa5e2952010-11-28 01:10:51 +0100639 && requested_ip_opt
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100640 && lease /* chaddr matches this lease */
641 && requested_nip == lease->lease_nip
642 ) {
Denys Vlasenko31af3d52009-06-17 11:57:09 +0200643 memset(lease->lease_mac, 0, sizeof(lease->lease_mac));
Denis Vlasenko04158e02009-02-02 10:48:06 +0000644 lease->expires = time(NULL) + server_config.decline_time;
Mike Frysinger7031f622006-05-08 03:20:50 +0000645 }
646 break;
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100647
Mike Frysinger7031f622006-05-08 03:20:50 +0000648 case DHCPRELEASE:
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100649 /* "Upon receipt of a DHCPRELEASE message, the server
650 * marks the network address as not allocated."
651 *
652 * SERVER_ID must be present,
653 * REQUESTED_IP must not be present (we do not check this),
654 * chaddr must be filled in,
655 * ciaddr must be filled in
656 */
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200657 log1("Received RELEASE");
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100658 if (server_id_opt
659 && lease /* chaddr matches this lease */
660 && packet.ciaddr == lease->lease_nip
661 ) {
Denis Vlasenko04158e02009-02-02 10:48:06 +0000662 lease->expires = time(NULL);
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100663 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000664 break;
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100665
Mike Frysinger7031f622006-05-08 03:20:50 +0000666 case DHCPINFORM:
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200667 log1("Received INFORM");
Mike Frysinger7031f622006-05-08 03:20:50 +0000668 send_inform(&packet);
669 break;
Mike Frysinger7031f622006-05-08 03:20:50 +0000670 }
671 }
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000672 ret0:
673 retval = 0;
674 ret:
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000675 /*if (server_config.pidfile) - server_config.pidfile is never NULL */
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000676 remove_pidfile(server_config.pidfile);
677 return retval;
Mike Frysinger7031f622006-05-08 03:20:50 +0000678}